wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * NFS support driver - based on etherboot and U-BOOT's tftp.c |
| 3 | * |
| 4 | * Masami Komiya <mkomiya@sonare.it> 2004 |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | /* NOTE: the NFS code is heavily inspired by the NetBSD netboot code (read: |
| 9 | * large portions are copied verbatim) as distributed in OSKit 0.97. A few |
| 10 | * changes were necessary to adapt the code to Etherboot and to fix several |
| 11 | * inconsistencies. Also the RPC message preparation is done "by hand" to |
| 12 | * avoid adding netsprintf() which I find hard to understand and use. */ |
| 13 | |
| 14 | /* NOTE 2: Etherboot does not care about things beyond the kernel image, so |
| 15 | * it loads the kernel image off the boot server (ARP_SERVER) and does not |
| 16 | * access the client root disk (root-path in dhcpd.conf), which would use |
| 17 | * ARP_ROOTSERVER. The root disk is something the operating system we are |
| 18 | * about to load needs to use. This is different from the OSKit 0.97 logic. */ |
| 19 | |
| 20 | /* NOTE 3: Symlink handling introduced by Anselm M Hoffmeister, 2003-July-14 |
| 21 | * If a symlink is encountered, it is followed as far as possible (recursion |
| 22 | * possible, maximum 16 steps). There is no clearing of ".."'s inside the |
| 23 | * path, so please DON'T DO THAT. thx. */ |
| 24 | |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 25 | /* NOTE 4: NFSv3 support added by Guillaume GARDET, 2016-June-20. |
| 26 | * NFSv2 is still used by default. But if server does not support NFSv2, then |
| 27 | * NFSv3 is used, if available on NFS server. */ |
| 28 | |
Thomas RIENOESSL | c8075bd | 2023-03-10 10:51:55 +0100 | [diff] [blame] | 29 | /* NOTE 5: NFSv1 support added by Christian Gmeiner, Thomas Rienoessl, |
| 30 | * September 27, 2018. As of now, NFSv3 is the default choice. If the server |
| 31 | * does not support NFSv3, we fall back to versions 2 or 1. */ |
| 32 | |
Tom Rini | abb9a04 | 2024-05-18 20:20:43 -0600 | [diff] [blame] | 33 | #include <common.h> |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 34 | #include <command.h> |
Simon Glass | 1ab1692 | 2022-07-31 12:28:48 -0600 | [diff] [blame] | 35 | #include <display_options.h> |
Tom Rini | 063c938 | 2022-07-23 13:05:03 -0400 | [diff] [blame] | 36 | #ifdef CONFIG_SYS_DIRECT_FLASH_NFS |
Simon Glass | a606ffc | 2019-12-28 10:44:40 -0700 | [diff] [blame] | 37 | #include <flash.h> |
Tom Rini | 063c938 | 2022-07-23 13:05:03 -0400 | [diff] [blame] | 38 | #endif |
Simon Glass | 85f1378 | 2019-12-28 10:45:03 -0700 | [diff] [blame] | 39 | #include <image.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 40 | #include <log.h> |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 41 | #include <net.h> |
| 42 | #include <malloc.h> |
Joe Hershberger | ed19185 | 2015-03-22 17:09:08 -0500 | [diff] [blame] | 43 | #include <mapmem.h> |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 44 | #include "nfs.h" |
| 45 | #include "bootp.h" |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 46 | #include <time.h> |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 47 | |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 48 | #define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */ |
Hiroshi Ito | 3702cd7 | 2008-01-31 18:35:04 +0900 | [diff] [blame] | 49 | #define NFS_RETRY_COUNT 30 |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 50 | |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 51 | #define NFS_RPC_ERR 1 |
| 52 | #define NFS_RPC_DROP 124 |
| 53 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 54 | static int fs_mounted; |
| 55 | static unsigned long rpc_id; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 56 | static int nfs_offset = -1; |
| 57 | static int nfs_len; |
Tom Rini | 0d1c474 | 2022-03-11 09:12:05 -0500 | [diff] [blame] | 58 | static const ulong nfs_timeout = CONFIG_NFS_TIMEOUT; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 59 | |
Sébastien Szymanski | fa3dc82 | 2024-03-26 10:34:31 +0100 | [diff] [blame] | 60 | static char dirfh[NFS3_FHSIZE]; /* NFSv2 / NFSv3 file handle of directory */ |
| 61 | static unsigned int dirfh3_length; /* (variable) length of dirfh when NFSv3 */ |
Joe Hershberger | f59972b | 2016-08-15 15:03:19 -0500 | [diff] [blame] | 62 | static char filefh[NFS3_FHSIZE]; /* NFSv2 / NFSv3 file handle */ |
Andrea zi0Black Cappa | f12a47a | 2022-05-18 16:30:08 +0000 | [diff] [blame] | 63 | static unsigned int filefh3_length; /* (variable) length of filefh when NFSv3 */ |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 64 | |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 65 | static enum net_loop_state nfs_download_state; |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 66 | static struct in_addr nfs_server_ip; |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 67 | static int nfs_server_mount_port; |
| 68 | static int nfs_server_port; |
| 69 | static int nfs_our_port; |
| 70 | static int nfs_timeout_count; |
| 71 | static int nfs_state; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 72 | #define STATE_PRCLOOKUP_PROG_MOUNT_REQ 1 |
| 73 | #define STATE_PRCLOOKUP_PROG_NFS_REQ 2 |
| 74 | #define STATE_MOUNT_REQ 3 |
| 75 | #define STATE_UMOUNT_REQ 4 |
| 76 | #define STATE_LOOKUP_REQ 5 |
| 77 | #define STATE_READ_REQ 6 |
| 78 | #define STATE_READLINK_REQ 7 |
| 79 | |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 80 | static char *nfs_filename; |
| 81 | static char *nfs_path; |
| 82 | static char nfs_path_buff[2048]; |
| 83 | |
Thomas RIENOESSL | c1bec0d | 2023-03-10 10:51:52 +0100 | [diff] [blame] | 84 | enum nfs_version { |
| 85 | NFS_UNKOWN = 0, |
Thomas RIENOESSL | c8075bd | 2023-03-10 10:51:55 +0100 | [diff] [blame] | 86 | NFS_V1 = 1, |
Thomas RIENOESSL | c1bec0d | 2023-03-10 10:51:52 +0100 | [diff] [blame] | 87 | NFS_V2 = 2, |
| 88 | NFS_V3 = 3, |
| 89 | }; |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 90 | |
Thomas RIENOESSL | c1bec0d | 2023-03-10 10:51:52 +0100 | [diff] [blame] | 91 | static enum nfs_version choosen_nfs_version = NFS_V3; |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 92 | static inline int store_block(uchar *src, unsigned offset, unsigned len) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 93 | { |
wdenk | 77ef4c6 | 2004-02-24 22:33:21 +0000 | [diff] [blame] | 94 | ulong newsize = offset + len; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 95 | #ifdef CONFIG_SYS_DIRECT_FLASH_NFS |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 96 | int i, rc = 0; |
| 97 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 98 | for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) { |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 99 | /* start address in flash? */ |
Simon Glass | 892265d | 2019-12-28 10:45:02 -0700 | [diff] [blame] | 100 | if (image_load_addr + offset >= flash_info[i].start[0]) { |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 101 | rc = 1; |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (rc) { /* Flash is destination for this packet */ |
Simon Glass | 892265d | 2019-12-28 10:45:02 -0700 | [diff] [blame] | 107 | rc = flash_write((uchar *)src, (ulong)image_load_addr + offset, |
| 108 | len); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 109 | if (rc) { |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 110 | flash_perror(rc); |
Wolfgang Denk | b077b50 | 2005-08-06 01:11:12 +0200 | [diff] [blame] | 111 | return -1; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 112 | } |
| 113 | } else |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 114 | #endif /* CONFIG_SYS_DIRECT_FLASH_NFS */ |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 115 | { |
Simon Glass | 892265d | 2019-12-28 10:45:02 -0700 | [diff] [blame] | 116 | void *ptr = map_sysmem(image_load_addr + offset, len); |
Joe Hershberger | ed19185 | 2015-03-22 17:09:08 -0500 | [diff] [blame] | 117 | |
| 118 | memcpy(ptr, src, len); |
| 119 | unmap_sysmem(ptr); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 120 | } |
wdenk | 77ef4c6 | 2004-02-24 22:33:21 +0000 | [diff] [blame] | 121 | |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 122 | if (net_boot_file_size < (offset + len)) |
| 123 | net_boot_file_size = newsize; |
Wolfgang Denk | b077b50 | 2005-08-06 01:11:12 +0200 | [diff] [blame] | 124 | return 0; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 127 | static char *basename(char *path) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 128 | { |
| 129 | char *fname; |
| 130 | |
| 131 | fname = path + strlen(path) - 1; |
| 132 | while (fname >= path) { |
| 133 | if (*fname == '/') { |
| 134 | fname++; |
| 135 | break; |
| 136 | } |
| 137 | fname--; |
| 138 | } |
| 139 | return fname; |
| 140 | } |
| 141 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 142 | static char *dirname(char *path) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 143 | { |
| 144 | char *fname; |
| 145 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 146 | fname = basename(path); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 147 | --fname; |
| 148 | *fname = '\0'; |
| 149 | return path; |
| 150 | } |
| 151 | |
| 152 | /************************************************************************** |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 153 | RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries |
| 154 | **************************************************************************/ |
Ralf Hubert | 2f8641c | 2016-07-01 13:19:51 +0200 | [diff] [blame] | 155 | static uint32_t *rpc_add_credentials(uint32_t *p) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 156 | { |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 157 | /* Here's the executive summary on authentication requirements of the |
| 158 | * various NFS server implementations: Linux accepts both AUTH_NONE |
| 159 | * and AUTH_UNIX authentication (also accepts an empty hostname field |
| 160 | * in the AUTH_UNIX scheme). *BSD refuses AUTH_NONE, but accepts |
| 161 | * AUTH_UNIX (also accepts an empty hostname field in the AUTH_UNIX |
| 162 | * scheme). To be safe, use AUTH_UNIX and pass the hostname if we have |
| 163 | * it (if the BOOTP/DHCP reply didn't give one, just use an empty |
| 164 | * hostname). */ |
| 165 | |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 166 | /* Provide an AUTH_UNIX credential. */ |
| 167 | *p++ = htonl(1); /* AUTH_UNIX */ |
Joe Hershberger | 35fd378 | 2016-08-15 15:03:27 -0500 | [diff] [blame] | 168 | *p++ = htonl(20); /* auth length */ |
| 169 | *p++ = 0; /* stamp */ |
| 170 | *p++ = 0; /* hostname string */ |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 171 | *p++ = 0; /* uid */ |
| 172 | *p++ = 0; /* gid */ |
| 173 | *p++ = 0; /* auxiliary gid list */ |
| 174 | |
| 175 | /* Provide an AUTH_NONE verifier. */ |
| 176 | *p++ = 0; /* AUTH_NONE */ |
| 177 | *p++ = 0; /* auth length */ |
| 178 | |
| 179 | return p; |
| 180 | } |
| 181 | |
| 182 | /************************************************************************** |
| 183 | RPC_LOOKUP - Lookup RPC Port numbers |
| 184 | **************************************************************************/ |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 185 | static void rpc_req(int rpc_prog, int rpc_proc, uint32_t *data, int datalen) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 186 | { |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 187 | struct rpc_t rpc_pkt; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 188 | unsigned long id; |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 189 | uint32_t *p; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 190 | int pktlen; |
| 191 | int sport; |
| 192 | |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 193 | id = ++rpc_id; |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 194 | rpc_pkt.u.call.id = htonl(id); |
| 195 | rpc_pkt.u.call.type = htonl(MSG_CALL); |
| 196 | rpc_pkt.u.call.rpcvers = htonl(2); /* use RPC version 2 */ |
| 197 | rpc_pkt.u.call.prog = htonl(rpc_prog); |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 198 | switch (rpc_prog) { |
| 199 | case PROG_NFS: |
Thomas RIENOESSL | c1bec0d | 2023-03-10 10:51:52 +0100 | [diff] [blame] | 200 | switch (choosen_nfs_version) { |
Thomas RIENOESSL | c8075bd | 2023-03-10 10:51:55 +0100 | [diff] [blame] | 201 | case NFS_V1: |
Thomas RIENOESSL | c1bec0d | 2023-03-10 10:51:52 +0100 | [diff] [blame] | 202 | case NFS_V2: |
| 203 | rpc_pkt.u.call.vers = htonl(2); |
| 204 | break; |
| 205 | |
| 206 | case NFS_V3: |
| 207 | rpc_pkt.u.call.vers = htonl(3); |
| 208 | break; |
| 209 | |
| 210 | case NFS_UNKOWN: |
| 211 | /* nothing to do */ |
| 212 | break; |
| 213 | } |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 214 | break; |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 215 | case PROG_MOUNT: |
Thomas RIENOESSL | c8075bd | 2023-03-10 10:51:55 +0100 | [diff] [blame] | 216 | switch (choosen_nfs_version) { |
| 217 | case NFS_V1: |
| 218 | rpc_pkt.u.call.vers = htonl(1); |
| 219 | break; |
| 220 | |
| 221 | case NFS_V2: |
| 222 | rpc_pkt.u.call.vers = htonl(2); |
| 223 | break; |
| 224 | |
| 225 | case NFS_V3: |
| 226 | rpc_pkt.u.call.vers = htonl(3); |
| 227 | break; |
| 228 | |
| 229 | case NFS_UNKOWN: |
| 230 | /* nothing to do */ |
| 231 | break; |
| 232 | } |
| 233 | break; |
| 234 | case PROG_PORTMAP: |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 235 | default: |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 236 | rpc_pkt.u.call.vers = htonl(2); /* portmapper is version 2 */ |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 237 | } |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 238 | rpc_pkt.u.call.proc = htonl(rpc_proc); |
Heinrich Schuchardt | 8439010 | 2019-09-02 23:55:32 +0200 | [diff] [blame] | 239 | p = rpc_pkt.u.call.data; |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 240 | |
| 241 | if (datalen) |
Heinrich Schuchardt | 8439010 | 2019-09-02 23:55:32 +0200 | [diff] [blame] | 242 | memcpy(p, data, datalen * sizeof(uint32_t)); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 243 | |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 244 | pktlen = (char *)p + datalen * sizeof(uint32_t) - (char *)&rpc_pkt; |
| 245 | |
| 246 | memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE, |
| 247 | &rpc_pkt.u.data[0], pktlen); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 248 | |
| 249 | if (rpc_prog == PROG_PORTMAP) |
| 250 | sport = SUNRPC_PORT; |
| 251 | else if (rpc_prog == PROG_MOUNT) |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 252 | sport = nfs_server_mount_port; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 253 | else |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 254 | sport = nfs_server_port; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 255 | |
Joe Hershberger | a8ca4f6 | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 256 | net_send_udp_packet(net_server_ethaddr, nfs_server_ip, sport, |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 257 | nfs_our_port, pktlen); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /************************************************************************** |
| 261 | RPC_LOOKUP - Lookup RPC Port numbers |
| 262 | **************************************************************************/ |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 263 | static void rpc_lookup_req(int prog, int ver) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 264 | { |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 265 | uint32_t data[16]; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 266 | |
| 267 | data[0] = 0; data[1] = 0; /* auth credential */ |
| 268 | data[2] = 0; data[3] = 0; /* auth verifier */ |
| 269 | data[4] = htonl(prog); |
| 270 | data[5] = htonl(ver); |
| 271 | data[6] = htonl(17); /* IP_UDP */ |
| 272 | data[7] = 0; |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 273 | rpc_req(PROG_PORTMAP, PORTMAP_GETPORT, data, 8); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /************************************************************************** |
| 277 | NFS_MOUNT - Mount an NFS Filesystem |
| 278 | **************************************************************************/ |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 279 | static void nfs_mount_req(char *path) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 280 | { |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 281 | uint32_t data[1024]; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 282 | uint32_t *p; |
| 283 | int len; |
| 284 | int pathlen; |
| 285 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 286 | pathlen = strlen(path); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 287 | |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 288 | p = &(data[0]); |
Ralf Hubert | 2f8641c | 2016-07-01 13:19:51 +0200 | [diff] [blame] | 289 | p = rpc_add_credentials(p); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 290 | |
| 291 | *p++ = htonl(pathlen); |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 292 | if (pathlen & 3) |
| 293 | *(p + pathlen / 4) = 0; |
| 294 | memcpy(p, path, pathlen); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 295 | p += (pathlen + 3) / 4; |
| 296 | |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 297 | len = (uint32_t *)p - (uint32_t *)&(data[0]); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 298 | |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 299 | rpc_req(PROG_MOUNT, MOUNT_ADDENTRY, data, len); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /************************************************************************** |
| 303 | NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server |
| 304 | **************************************************************************/ |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 305 | static void nfs_umountall_req(void) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 306 | { |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 307 | uint32_t data[1024]; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 308 | uint32_t *p; |
| 309 | int len; |
| 310 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 311 | if ((nfs_server_mount_port == -1) || (!fs_mounted)) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 312 | /* Nothing mounted, nothing to umount */ |
| 313 | return; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 314 | |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 315 | p = &(data[0]); |
Ralf Hubert | 2f8641c | 2016-07-01 13:19:51 +0200 | [diff] [blame] | 316 | p = rpc_add_credentials(p); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 317 | |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 318 | len = (uint32_t *)p - (uint32_t *)&(data[0]); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 319 | |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 320 | rpc_req(PROG_MOUNT, MOUNT_UMOUNTALL, data, len); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /*************************************************************************** |
| 324 | * NFS_READLINK (AH 2003-07-14) |
| 325 | * This procedure is called when read of the first block fails - |
| 326 | * this probably happens when it's a directory or a symlink |
| 327 | * In case of successful readlink(), the dirname is manipulated, |
| 328 | * so that inside the nfs() function a recursion can be done. |
| 329 | **************************************************************************/ |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 330 | static void nfs_readlink_req(void) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 331 | { |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 332 | uint32_t data[1024]; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 333 | uint32_t *p; |
| 334 | int len; |
| 335 | |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 336 | p = &(data[0]); |
Ralf Hubert | 2f8641c | 2016-07-01 13:19:51 +0200 | [diff] [blame] | 337 | p = rpc_add_credentials(p); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 338 | |
Thomas RIENOESSL | c8075bd | 2023-03-10 10:51:55 +0100 | [diff] [blame] | 339 | if (choosen_nfs_version != NFS_V3) { |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 340 | memcpy(p, filefh, NFS_FHSIZE); |
| 341 | p += (NFS_FHSIZE / 4); |
Thomas RIENOESSL | c1bec0d | 2023-03-10 10:51:52 +0100 | [diff] [blame] | 342 | } else { /* NFS_V3 */ |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 343 | *p++ = htonl(filefh3_length); |
Joe Hershberger | f59972b | 2016-08-15 15:03:19 -0500 | [diff] [blame] | 344 | memcpy(p, filefh, filefh3_length); |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 345 | p += (filefh3_length / 4); |
| 346 | } |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 347 | |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 348 | len = (uint32_t *)p - (uint32_t *)&(data[0]); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 349 | |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 350 | rpc_req(PROG_NFS, NFS_READLINK, data, len); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /************************************************************************** |
| 354 | NFS_LOOKUP - Lookup Pathname |
| 355 | **************************************************************************/ |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 356 | static void nfs_lookup_req(char *fname) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 357 | { |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 358 | uint32_t data[1024]; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 359 | uint32_t *p; |
| 360 | int len; |
| 361 | int fnamelen; |
| 362 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 363 | fnamelen = strlen(fname); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 364 | |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 365 | p = &(data[0]); |
Ralf Hubert | 2f8641c | 2016-07-01 13:19:51 +0200 | [diff] [blame] | 366 | p = rpc_add_credentials(p); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 367 | |
Thomas RIENOESSL | c8075bd | 2023-03-10 10:51:55 +0100 | [diff] [blame] | 368 | if (choosen_nfs_version != NFS_V3) { |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 369 | memcpy(p, dirfh, NFS_FHSIZE); |
| 370 | p += (NFS_FHSIZE / 4); |
| 371 | *p++ = htonl(fnamelen); |
| 372 | if (fnamelen & 3) |
| 373 | *(p + fnamelen / 4) = 0; |
| 374 | memcpy(p, fname, fnamelen); |
| 375 | p += (fnamelen + 3) / 4; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 376 | |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 377 | len = (uint32_t *)p - (uint32_t *)&(data[0]); |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 378 | |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 379 | rpc_req(PROG_NFS, NFS_LOOKUP, data, len); |
Thomas RIENOESSL | c1bec0d | 2023-03-10 10:51:52 +0100 | [diff] [blame] | 380 | } else { /* NFS_V3 */ |
Sébastien Szymanski | fa3dc82 | 2024-03-26 10:34:31 +0100 | [diff] [blame] | 381 | *p++ = htonl(dirfh3_length); /* Dir handle length */ |
| 382 | memcpy(p, dirfh, dirfh3_length); |
| 383 | p += (dirfh3_length / 4); |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 384 | *p++ = htonl(fnamelen); |
| 385 | if (fnamelen & 3) |
| 386 | *(p + fnamelen / 4) = 0; |
| 387 | memcpy(p, fname, fnamelen); |
| 388 | p += (fnamelen + 3) / 4; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 389 | |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 390 | len = (uint32_t *)p - (uint32_t *)&(data[0]); |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 391 | |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 392 | rpc_req(PROG_NFS, NFS3PROC_LOOKUP, data, len); |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 393 | } |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /************************************************************************** |
| 397 | NFS_READ - Read File on NFS Server |
| 398 | **************************************************************************/ |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 399 | static void nfs_read_req(int offset, int readlen) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 400 | { |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 401 | uint32_t data[1024]; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 402 | uint32_t *p; |
| 403 | int len; |
| 404 | |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 405 | p = &(data[0]); |
Ralf Hubert | 2f8641c | 2016-07-01 13:19:51 +0200 | [diff] [blame] | 406 | p = rpc_add_credentials(p); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 407 | |
Thomas RIENOESSL | c8075bd | 2023-03-10 10:51:55 +0100 | [diff] [blame] | 408 | if (choosen_nfs_version != NFS_V3) { |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 409 | memcpy(p, filefh, NFS_FHSIZE); |
| 410 | p += (NFS_FHSIZE / 4); |
| 411 | *p++ = htonl(offset); |
| 412 | *p++ = htonl(readlen); |
| 413 | *p++ = 0; |
Thomas RIENOESSL | c1bec0d | 2023-03-10 10:51:52 +0100 | [diff] [blame] | 414 | } else { /* NFS_V3 */ |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 415 | *p++ = htonl(filefh3_length); |
Joe Hershberger | f59972b | 2016-08-15 15:03:19 -0500 | [diff] [blame] | 416 | memcpy(p, filefh, filefh3_length); |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 417 | p += (filefh3_length / 4); |
| 418 | *p++ = htonl(0); /* offset is 64-bit long, so fill with 0 */ |
| 419 | *p++ = htonl(offset); |
| 420 | *p++ = htonl(readlen); |
| 421 | *p++ = 0; |
| 422 | } |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 423 | |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 424 | len = (uint32_t *)p - (uint32_t *)&(data[0]); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 425 | |
Joe Hershberger | 8481b20 | 2016-09-09 12:56:26 -0500 | [diff] [blame] | 426 | rpc_req(PROG_NFS, NFS_READ, data, len); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /************************************************************************** |
| 430 | RPC request dispatcher |
| 431 | **************************************************************************/ |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 432 | static void nfs_send(void) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 433 | { |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 434 | debug("%s\n", __func__); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 435 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 436 | switch (nfs_state) { |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 437 | case STATE_PRCLOOKUP_PROG_MOUNT_REQ: |
Thomas RIENOESSL | c8075bd | 2023-03-10 10:51:55 +0100 | [diff] [blame] | 438 | if (choosen_nfs_version != NFS_V3) |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 439 | rpc_lookup_req(PROG_MOUNT, 1); |
Thomas RIENOESSL | c1bec0d | 2023-03-10 10:51:52 +0100 | [diff] [blame] | 440 | else /* NFS_V3 */ |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 441 | rpc_lookup_req(PROG_MOUNT, 3); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 442 | break; |
| 443 | case STATE_PRCLOOKUP_PROG_NFS_REQ: |
Thomas RIENOESSL | c8075bd | 2023-03-10 10:51:55 +0100 | [diff] [blame] | 444 | if (choosen_nfs_version != NFS_V3) |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 445 | rpc_lookup_req(PROG_NFS, 2); |
Thomas RIENOESSL | c1bec0d | 2023-03-10 10:51:52 +0100 | [diff] [blame] | 446 | else /* NFS_V3 */ |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 447 | rpc_lookup_req(PROG_NFS, 3); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 448 | break; |
| 449 | case STATE_MOUNT_REQ: |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 450 | nfs_mount_req(nfs_path); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 451 | break; |
| 452 | case STATE_UMOUNT_REQ: |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 453 | nfs_umountall_req(); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 454 | break; |
| 455 | case STATE_LOOKUP_REQ: |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 456 | nfs_lookup_req(nfs_filename); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 457 | break; |
| 458 | case STATE_READ_REQ: |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 459 | nfs_read_req(nfs_offset, nfs_len); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 460 | break; |
| 461 | case STATE_READLINK_REQ: |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 462 | nfs_readlink_req(); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 463 | break; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | /************************************************************************** |
| 468 | Handlers for the reply from server |
| 469 | **************************************************************************/ |
| 470 | |
Thomas RIENOESSL | e9839f9 | 2023-03-10 10:51:53 +0100 | [diff] [blame] | 471 | static int rpc_handle_error(struct rpc_t *rpc_pkt) |
| 472 | { |
| 473 | if (rpc_pkt->u.reply.rstatus || |
| 474 | rpc_pkt->u.reply.verifier || |
| 475 | rpc_pkt->u.reply.astatus || |
| 476 | rpc_pkt->u.reply.data[0]) { |
| 477 | switch (ntohl(rpc_pkt->u.reply.astatus)) { |
| 478 | case NFS_RPC_SUCCESS: /* Not an error */ |
| 479 | break; |
| 480 | case NFS_RPC_PROG_MISMATCH: { |
| 481 | /* Remote can't support NFS version */ |
| 482 | const int min = ntohl(rpc_pkt->u.reply.data[0]); |
| 483 | const int max = ntohl(rpc_pkt->u.reply.data[1]); |
| 484 | |
Thomas RIENOESSL | c8075bd | 2023-03-10 10:51:55 +0100 | [diff] [blame] | 485 | if (max < NFS_V1 || max > NFS_V3 || min > NFS_V3) { |
Thomas RIENOESSL | e9839f9 | 2023-03-10 10:51:53 +0100 | [diff] [blame] | 486 | puts("*** ERROR: NFS version not supported"); |
| 487 | debug(": Requested: V%d, accepted: min V%d - max V%d\n", |
| 488 | choosen_nfs_version, |
| 489 | ntohl(rpc_pkt->u.reply.data[0]), |
| 490 | ntohl(rpc_pkt->u.reply.data[1])); |
| 491 | puts("\n"); |
| 492 | choosen_nfs_version = NFS_UNKOWN; |
| 493 | break; |
| 494 | } |
| 495 | |
| 496 | debug("*** Warning: NFS version not supported: Requested: V%d, accepted: min V%d - max V%d\n", |
| 497 | choosen_nfs_version, |
| 498 | ntohl(rpc_pkt->u.reply.data[0]), |
| 499 | ntohl(rpc_pkt->u.reply.data[1])); |
| 500 | debug("Will retry with NFSv%d\n", min); |
| 501 | choosen_nfs_version = min; |
| 502 | return -NFS_RPC_PROG_MISMATCH; |
| 503 | } |
| 504 | case NFS_RPC_PROG_UNAVAIL: |
| 505 | case NFS_RPC_PROC_UNAVAIL: |
| 506 | case NFS_RPC_GARBAGE_ARGS: |
| 507 | case NFS_RPC_SYSTEM_ERR: |
| 508 | default: /* Unknown error on 'accept state' flag */ |
| 509 | debug("*** ERROR: accept state error (%d)\n", |
| 510 | ntohl(rpc_pkt->u.reply.astatus)); |
| 511 | break; |
| 512 | } |
| 513 | return -1; |
| 514 | } |
| 515 | |
| 516 | return 0; |
| 517 | } |
| 518 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 519 | static int rpc_lookup_reply(int prog, uchar *pkt, unsigned len) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 520 | { |
| 521 | struct rpc_t rpc_pkt; |
| 522 | |
Joe Hershberger | 3393d4c | 2016-08-15 15:03:24 -0500 | [diff] [blame] | 523 | memcpy(&rpc_pkt.u.data[0], pkt, len); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 524 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 525 | debug("%s\n", __func__); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 526 | |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 527 | if (ntohl(rpc_pkt.u.reply.id) > rpc_id) |
| 528 | return -NFS_RPC_ERR; |
| 529 | else if (ntohl(rpc_pkt.u.reply.id) < rpc_id) |
| 530 | return -NFS_RPC_DROP; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 531 | |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 532 | if (rpc_pkt.u.reply.rstatus || |
| 533 | rpc_pkt.u.reply.verifier || |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 534 | rpc_pkt.u.reply.astatus) |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 535 | return -1; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 536 | |
| 537 | switch (prog) { |
| 538 | case PROG_MOUNT: |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 539 | nfs_server_mount_port = ntohl(rpc_pkt.u.reply.data[0]); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 540 | break; |
| 541 | case PROG_NFS: |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 542 | nfs_server_port = ntohl(rpc_pkt.u.reply.data[0]); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 543 | break; |
| 544 | } |
| 545 | |
| 546 | return 0; |
| 547 | } |
| 548 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 549 | static int nfs_mount_reply(uchar *pkt, unsigned len) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 550 | { |
| 551 | struct rpc_t rpc_pkt; |
Thomas RIENOESSL | 5009744 | 2023-03-10 10:51:54 +0100 | [diff] [blame] | 552 | int ret; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 553 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 554 | debug("%s\n", __func__); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 555 | |
Joe Hershberger | 3393d4c | 2016-08-15 15:03:24 -0500 | [diff] [blame] | 556 | memcpy(&rpc_pkt.u.data[0], pkt, len); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 557 | |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 558 | if (ntohl(rpc_pkt.u.reply.id) > rpc_id) |
| 559 | return -NFS_RPC_ERR; |
| 560 | else if (ntohl(rpc_pkt.u.reply.id) < rpc_id) |
| 561 | return -NFS_RPC_DROP; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 562 | |
Thomas RIENOESSL | 5009744 | 2023-03-10 10:51:54 +0100 | [diff] [blame] | 563 | ret = rpc_handle_error(&rpc_pkt); |
| 564 | if (ret) |
| 565 | return ret; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 566 | |
| 567 | fs_mounted = 1; |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 568 | /* NFSv2 and NFSv3 use same structure */ |
Sébastien Szymanski | fa3dc82 | 2024-03-26 10:34:31 +0100 | [diff] [blame] | 569 | if (choosen_nfs_version != NFS_V3) { |
| 570 | memcpy(dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE); |
| 571 | } else { |
| 572 | dirfh3_length = ntohl(rpc_pkt.u.reply.data[1]); |
| 573 | if (dirfh3_length > NFS3_FHSIZE) |
| 574 | dirfh3_length = NFS3_FHSIZE; |
| 575 | memcpy(dirfh, rpc_pkt.u.reply.data + 2, dirfh3_length); |
| 576 | } |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 577 | |
| 578 | return 0; |
| 579 | } |
| 580 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 581 | static int nfs_umountall_reply(uchar *pkt, unsigned len) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 582 | { |
| 583 | struct rpc_t rpc_pkt; |
| 584 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 585 | debug("%s\n", __func__); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 586 | |
Joe Hershberger | 3393d4c | 2016-08-15 15:03:24 -0500 | [diff] [blame] | 587 | memcpy(&rpc_pkt.u.data[0], pkt, len); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 588 | |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 589 | if (ntohl(rpc_pkt.u.reply.id) > rpc_id) |
| 590 | return -NFS_RPC_ERR; |
| 591 | else if (ntohl(rpc_pkt.u.reply.id) < rpc_id) |
| 592 | return -NFS_RPC_DROP; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 593 | |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 594 | if (rpc_pkt.u.reply.rstatus || |
| 595 | rpc_pkt.u.reply.verifier || |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 596 | rpc_pkt.u.reply.astatus) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 597 | return -1; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 598 | |
| 599 | fs_mounted = 0; |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 600 | memset(dirfh, 0, sizeof(dirfh)); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 601 | |
| 602 | return 0; |
| 603 | } |
| 604 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 605 | static int nfs_lookup_reply(uchar *pkt, unsigned len) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 606 | { |
| 607 | struct rpc_t rpc_pkt; |
Thomas RIENOESSL | e9839f9 | 2023-03-10 10:51:53 +0100 | [diff] [blame] | 608 | int ret; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 609 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 610 | debug("%s\n", __func__); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 611 | |
Joe Hershberger | 3393d4c | 2016-08-15 15:03:24 -0500 | [diff] [blame] | 612 | memcpy(&rpc_pkt.u.data[0], pkt, len); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 613 | |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 614 | if (ntohl(rpc_pkt.u.reply.id) > rpc_id) |
| 615 | return -NFS_RPC_ERR; |
| 616 | else if (ntohl(rpc_pkt.u.reply.id) < rpc_id) |
| 617 | return -NFS_RPC_DROP; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 618 | |
Thomas RIENOESSL | e9839f9 | 2023-03-10 10:51:53 +0100 | [diff] [blame] | 619 | ret = rpc_handle_error(&rpc_pkt); |
| 620 | if (ret) |
| 621 | return ret; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 622 | |
Thomas RIENOESSL | c8075bd | 2023-03-10 10:51:55 +0100 | [diff] [blame] | 623 | if (choosen_nfs_version != NFS_V3) { |
liucheng (G) | 6d68791 | 2019-08-29 13:48:02 +0000 | [diff] [blame] | 624 | if (((uchar *)&(rpc_pkt.u.reply.data[0]) - (uchar *)(&rpc_pkt) + NFS_FHSIZE) > len) |
| 625 | return -NFS_RPC_DROP; |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 626 | memcpy(filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE); |
Thomas RIENOESSL | c1bec0d | 2023-03-10 10:51:52 +0100 | [diff] [blame] | 627 | } else { /* NFS_V3 */ |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 628 | filefh3_length = ntohl(rpc_pkt.u.reply.data[1]); |
| 629 | if (filefh3_length > NFS3_FHSIZE) |
| 630 | filefh3_length = NFS3_FHSIZE; |
Joe Hershberger | f59972b | 2016-08-15 15:03:19 -0500 | [diff] [blame] | 631 | memcpy(filefh, rpc_pkt.u.reply.data + 2, filefh3_length); |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 632 | } |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 633 | |
| 634 | return 0; |
| 635 | } |
| 636 | |
Joe Hershberger | 46e9312 | 2016-08-15 15:03:22 -0500 | [diff] [blame] | 637 | static int nfs3_get_attributes_offset(uint32_t *data) |
| 638 | { |
Heinrich Schuchardt | 8439010 | 2019-09-02 23:55:32 +0200 | [diff] [blame] | 639 | if (data[1]) { |
Joe Hershberger | 46e9312 | 2016-08-15 15:03:22 -0500 | [diff] [blame] | 640 | /* 'attributes_follow' flag is TRUE, |
Joe Hershberger | 10e4097 | 2016-08-15 15:03:23 -0500 | [diff] [blame] | 641 | * so we have attributes on 21 dwords */ |
Joe Hershberger | 46e9312 | 2016-08-15 15:03:22 -0500 | [diff] [blame] | 642 | /* Skip unused values : |
| 643 | type; 32 bits value, |
| 644 | mode; 32 bits value, |
| 645 | nlink; 32 bits value, |
| 646 | uid; 32 bits value, |
| 647 | gid; 32 bits value, |
| 648 | size; 64 bits value, |
| 649 | used; 64 bits value, |
| 650 | rdev; 64 bits value, |
| 651 | fsid; 64 bits value, |
| 652 | fileid; 64 bits value, |
| 653 | atime; 64 bits value, |
| 654 | mtime; 64 bits value, |
| 655 | ctime; 64 bits value, |
| 656 | */ |
| 657 | return 22; |
| 658 | } else { |
| 659 | /* 'attributes_follow' flag is FALSE, |
| 660 | * so we don't have any attributes */ |
| 661 | return 1; |
| 662 | } |
| 663 | } |
| 664 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 665 | static int nfs_readlink_reply(uchar *pkt, unsigned len) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 666 | { |
| 667 | struct rpc_t rpc_pkt; |
| 668 | int rlen; |
Joe Hershberger | 46e9312 | 2016-08-15 15:03:22 -0500 | [diff] [blame] | 669 | int nfsv3_data_offset = 0; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 670 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 671 | debug("%s\n", __func__); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 672 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 673 | memcpy((unsigned char *)&rpc_pkt, pkt, len); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 674 | |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 675 | if (ntohl(rpc_pkt.u.reply.id) > rpc_id) |
| 676 | return -NFS_RPC_ERR; |
| 677 | else if (ntohl(rpc_pkt.u.reply.id) < rpc_id) |
| 678 | return -NFS_RPC_DROP; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 679 | |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 680 | if (rpc_pkt.u.reply.rstatus || |
| 681 | rpc_pkt.u.reply.verifier || |
| 682 | rpc_pkt.u.reply.astatus || |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 683 | rpc_pkt.u.reply.data[0]) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 684 | return -1; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 685 | |
Thomas RIENOESSL | c1bec0d | 2023-03-10 10:51:52 +0100 | [diff] [blame] | 686 | if (choosen_nfs_version == NFS_V3) { |
Joe Hershberger | 46e9312 | 2016-08-15 15:03:22 -0500 | [diff] [blame] | 687 | nfsv3_data_offset = |
| 688 | nfs3_get_attributes_offset(rpc_pkt.u.reply.data); |
| 689 | } |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 690 | |
Joe Hershberger | 46e9312 | 2016-08-15 15:03:22 -0500 | [diff] [blame] | 691 | /* new path length */ |
| 692 | rlen = ntohl(rpc_pkt.u.reply.data[1 + nfsv3_data_offset]); |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 693 | |
liucheng (G) | 3bfaca1 | 2019-08-29 13:47:54 +0000 | [diff] [blame] | 694 | if (((uchar *)&(rpc_pkt.u.reply.data[0]) - (uchar *)(&rpc_pkt) + rlen) > len) |
| 695 | return -NFS_RPC_DROP; |
| 696 | |
Joe Hershberger | 46e9312 | 2016-08-15 15:03:22 -0500 | [diff] [blame] | 697 | if (*((char *)&(rpc_pkt.u.reply.data[2 + nfsv3_data_offset])) != '/') { |
| 698 | int pathlen; |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 699 | |
Joe Hershberger | 46e9312 | 2016-08-15 15:03:22 -0500 | [diff] [blame] | 700 | strcat(nfs_path, "/"); |
| 701 | pathlen = strlen(nfs_path); |
| 702 | memcpy(nfs_path + pathlen, |
| 703 | (uchar *)&(rpc_pkt.u.reply.data[2 + nfsv3_data_offset]), |
| 704 | rlen); |
| 705 | nfs_path[pathlen + rlen] = 0; |
| 706 | } else { |
| 707 | memcpy(nfs_path, |
| 708 | (uchar *)&(rpc_pkt.u.reply.data[2 + nfsv3_data_offset]), |
| 709 | rlen); |
| 710 | nfs_path[rlen] = 0; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 711 | } |
| 712 | return 0; |
| 713 | } |
| 714 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 715 | static int nfs_read_reply(uchar *pkt, unsigned len) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 716 | { |
| 717 | struct rpc_t rpc_pkt; |
| 718 | int rlen; |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 719 | uchar *data_ptr; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 720 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 721 | debug("%s\n", __func__); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 722 | |
Joe Hershberger | 3393d4c | 2016-08-15 15:03:24 -0500 | [diff] [blame] | 723 | memcpy(&rpc_pkt.u.data[0], pkt, sizeof(rpc_pkt.u.reply)); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 724 | |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 725 | if (ntohl(rpc_pkt.u.reply.id) > rpc_id) |
| 726 | return -NFS_RPC_ERR; |
| 727 | else if (ntohl(rpc_pkt.u.reply.id) < rpc_id) |
| 728 | return -NFS_RPC_DROP; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 729 | |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 730 | if (rpc_pkt.u.reply.rstatus || |
| 731 | rpc_pkt.u.reply.verifier || |
| 732 | rpc_pkt.u.reply.astatus || |
| 733 | rpc_pkt.u.reply.data[0]) { |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 734 | if (rpc_pkt.u.reply.rstatus) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 735 | return -9999; |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 736 | if (rpc_pkt.u.reply.astatus) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 737 | return -9999; |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 738 | return -ntohl(rpc_pkt.u.reply.data[0]); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 741 | if ((nfs_offset != 0) && !((nfs_offset) % |
| 742 | (NFS_READ_SIZE / 2 * 10 * HASHES_PER_LINE))) |
| 743 | puts("\n\t "); |
| 744 | if (!(nfs_offset % ((NFS_READ_SIZE / 2) * 10))) |
| 745 | putc('#'); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 746 | |
Thomas RIENOESSL | c8075bd | 2023-03-10 10:51:55 +0100 | [diff] [blame] | 747 | if (choosen_nfs_version != NFS_V3) { |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 748 | rlen = ntohl(rpc_pkt.u.reply.data[18]); |
| 749 | data_ptr = (uchar *)&(rpc_pkt.u.reply.data[19]); |
Thomas RIENOESSL | c1bec0d | 2023-03-10 10:51:52 +0100 | [diff] [blame] | 750 | } else { /* NFS_V3 */ |
Joe Hershberger | 46e9312 | 2016-08-15 15:03:22 -0500 | [diff] [blame] | 751 | int nfsv3_data_offset = |
| 752 | nfs3_get_attributes_offset(rpc_pkt.u.reply.data); |
| 753 | |
| 754 | /* count value */ |
| 755 | rlen = ntohl(rpc_pkt.u.reply.data[1 + nfsv3_data_offset]); |
| 756 | /* Skip unused values : |
| 757 | EOF: 32 bits value, |
| 758 | data_size: 32 bits value, |
| 759 | */ |
| 760 | data_ptr = (uchar *) |
| 761 | &(rpc_pkt.u.reply.data[4 + nfsv3_data_offset]); |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 762 | } |
| 763 | |
liucheng (G) | e837f13 | 2019-08-29 13:47:48 +0000 | [diff] [blame] | 764 | if (((uchar *)&(rpc_pkt.u.reply.data[0]) - (uchar *)(&rpc_pkt) + rlen) > len) |
| 765 | return -9999; |
| 766 | |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 767 | if (store_block(data_ptr, nfs_offset, rlen)) |
| 768 | return -9999; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 769 | |
| 770 | return rlen; |
| 771 | } |
| 772 | |
| 773 | /************************************************************************** |
| 774 | Interfaces of U-BOOT |
| 775 | **************************************************************************/ |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 776 | static void nfs_timeout_handler(void) |
wdenk | d9ee50d | 2004-09-28 21:51:42 +0000 | [diff] [blame] | 777 | { |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 778 | if (++nfs_timeout_count > NFS_RETRY_COUNT) { |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 779 | puts("\nRetry count exceeded; starting again\n"); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 780 | net_start_again(); |
Evan Samanas | b956fba | 2009-11-09 20:08:36 -0600 | [diff] [blame] | 781 | } else { |
| 782 | puts("T "); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 783 | net_set_timeout_handler(nfs_timeout + |
Tom Rini | 0d1c474 | 2022-03-11 09:12:05 -0500 | [diff] [blame] | 784 | nfs_timeout * nfs_timeout_count, |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 785 | nfs_timeout_handler); |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 786 | nfs_send(); |
Hiroshi Ito | 3702cd7 | 2008-01-31 18:35:04 +0900 | [diff] [blame] | 787 | } |
wdenk | d9ee50d | 2004-09-28 21:51:42 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 790 | static void nfs_handler(uchar *pkt, unsigned dest, struct in_addr sip, |
| 791 | unsigned src, unsigned len) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 792 | { |
| 793 | int rlen; |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 794 | int reply; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 795 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 796 | debug("%s\n", __func__); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 797 | |
liucheng (G) | 761b49f | 2019-08-29 13:47:40 +0000 | [diff] [blame] | 798 | if (len > sizeof(struct rpc_t)) |
| 799 | return; |
| 800 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 801 | if (dest != nfs_our_port) |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 802 | return; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 803 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 804 | switch (nfs_state) { |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 805 | case STATE_PRCLOOKUP_PROG_MOUNT_REQ: |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 806 | if (rpc_lookup_reply(PROG_MOUNT, pkt, len) == -NFS_RPC_DROP) |
| 807 | break; |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 808 | nfs_state = STATE_PRCLOOKUP_PROG_NFS_REQ; |
| 809 | nfs_send(); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 810 | break; |
| 811 | |
| 812 | case STATE_PRCLOOKUP_PROG_NFS_REQ: |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 813 | if (rpc_lookup_reply(PROG_NFS, pkt, len) == -NFS_RPC_DROP) |
| 814 | break; |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 815 | nfs_state = STATE_MOUNT_REQ; |
| 816 | nfs_send(); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 817 | break; |
| 818 | |
| 819 | case STATE_MOUNT_REQ: |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 820 | reply = nfs_mount_reply(pkt, len); |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 821 | if (reply == -NFS_RPC_DROP) { |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 822 | break; |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 823 | } else if (reply == -NFS_RPC_ERR) { |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 824 | puts("*** ERROR: Cannot mount\n"); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 825 | /* just to be sure... */ |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 826 | nfs_state = STATE_UMOUNT_REQ; |
| 827 | nfs_send(); |
Thomas RIENOESSL | 5009744 | 2023-03-10 10:51:54 +0100 | [diff] [blame] | 828 | } else if (reply == -NFS_RPC_PROG_MISMATCH && |
| 829 | choosen_nfs_version != NFS_UNKOWN) { |
| 830 | nfs_state = STATE_MOUNT_REQ; |
| 831 | nfs_send(); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 832 | } else { |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 833 | nfs_state = STATE_LOOKUP_REQ; |
| 834 | nfs_send(); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 835 | } |
| 836 | break; |
| 837 | |
| 838 | case STATE_UMOUNT_REQ: |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 839 | reply = nfs_umountall_reply(pkt, len); |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 840 | if (reply == -NFS_RPC_DROP) { |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 841 | break; |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 842 | } else if (reply == -NFS_RPC_ERR) { |
Joe Hershberger | 739776a | 2016-08-15 15:03:25 -0500 | [diff] [blame] | 843 | debug("*** ERROR: Cannot umount\n"); |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 844 | net_set_state(NETLOOP_FAIL); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 845 | } else { |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 846 | puts("\ndone\n"); |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 847 | net_set_state(nfs_download_state); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 848 | } |
| 849 | break; |
| 850 | |
| 851 | case STATE_LOOKUP_REQ: |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 852 | reply = nfs_lookup_reply(pkt, len); |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 853 | if (reply == -NFS_RPC_DROP) { |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 854 | break; |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 855 | } else if (reply == -NFS_RPC_ERR) { |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 856 | puts("*** ERROR: File lookup fail\n"); |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 857 | nfs_state = STATE_UMOUNT_REQ; |
| 858 | nfs_send(); |
Joe Hershberger | 1f46617 | 2016-08-15 15:03:21 -0500 | [diff] [blame] | 859 | } else if (reply == -NFS_RPC_PROG_MISMATCH && |
Thomas RIENOESSL | c1bec0d | 2023-03-10 10:51:52 +0100 | [diff] [blame] | 860 | choosen_nfs_version != NFS_UNKOWN) { |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 861 | /* umount */ |
| 862 | nfs_state = STATE_UMOUNT_REQ; |
| 863 | nfs_send(); |
| 864 | /* And retry with another supported version */ |
| 865 | nfs_state = STATE_PRCLOOKUP_PROG_MOUNT_REQ; |
| 866 | nfs_send(); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 867 | } else { |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 868 | nfs_state = STATE_READ_REQ; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 869 | nfs_offset = 0; |
| 870 | nfs_len = NFS_READ_SIZE; |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 871 | nfs_send(); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 872 | } |
| 873 | break; |
| 874 | |
| 875 | case STATE_READLINK_REQ: |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 876 | reply = nfs_readlink_reply(pkt, len); |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 877 | if (reply == -NFS_RPC_DROP) { |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 878 | break; |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 879 | } else if (reply == -NFS_RPC_ERR) { |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 880 | puts("*** ERROR: Symlink fail\n"); |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 881 | nfs_state = STATE_UMOUNT_REQ; |
| 882 | nfs_send(); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 883 | } else { |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 884 | debug("Symlink --> %s\n", nfs_path); |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 885 | nfs_filename = basename(nfs_path); |
| 886 | nfs_path = dirname(nfs_path); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 887 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 888 | nfs_state = STATE_MOUNT_REQ; |
| 889 | nfs_send(); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 890 | } |
| 891 | break; |
| 892 | |
| 893 | case STATE_READ_REQ: |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 894 | rlen = nfs_read_reply(pkt, len); |
Vasily Khoruzhick | b32c220 | 2018-05-14 08:34:36 -0700 | [diff] [blame] | 895 | if (rlen == -NFS_RPC_DROP) |
| 896 | break; |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 897 | net_set_timeout_handler(nfs_timeout, nfs_timeout_handler); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 898 | if (rlen > 0) { |
| 899 | nfs_offset += rlen; |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 900 | nfs_send(); |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 901 | } else if ((rlen == -NFSERR_ISDIR) || (rlen == -NFSERR_INVAL)) { |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 902 | /* symbolic link */ |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 903 | nfs_state = STATE_READLINK_REQ; |
| 904 | nfs_send(); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 905 | } else { |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 906 | if (!rlen) |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 907 | nfs_download_state = NETLOOP_SUCCESS; |
Guillaume GARDET | 2a4bc83 | 2016-07-29 11:31:00 +0200 | [diff] [blame] | 908 | if (rlen < 0) |
Joe Hershberger | 739776a | 2016-08-15 15:03:25 -0500 | [diff] [blame] | 909 | debug("NFS READ error (%d)\n", rlen); |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 910 | nfs_state = STATE_UMOUNT_REQ; |
| 911 | nfs_send(); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 912 | } |
| 913 | break; |
| 914 | } |
| 915 | } |
| 916 | |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 917 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 918 | void nfs_start(void) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 919 | { |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 920 | debug("%s\n", __func__); |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 921 | nfs_download_state = NETLOOP_FAIL; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 922 | |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 923 | nfs_server_ip = net_server_ip; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 924 | nfs_path = (char *)nfs_path_buff; |
| 925 | |
| 926 | if (nfs_path == NULL) { |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 927 | net_set_state(NETLOOP_FAIL); |
Joe Hershberger | 463624d | 2018-07-03 19:22:56 -0500 | [diff] [blame] | 928 | printf("*** ERROR: Fail allocate memory\n"); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 929 | return; |
| 930 | } |
| 931 | |
Joe Hershberger | 9cb7b02 | 2018-07-03 19:36:43 -0500 | [diff] [blame] | 932 | if (!net_parse_bootfile(&nfs_server_ip, nfs_path, |
| 933 | sizeof(nfs_path_buff))) { |
Joe Hershberger | a8d4f89 | 2016-08-15 14:42:16 -0500 | [diff] [blame] | 934 | sprintf(nfs_path, "/nfsroot/%02X%02X%02X%02X.img", |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 935 | net_ip.s_addr & 0xFF, |
| 936 | (net_ip.s_addr >> 8) & 0xFF, |
| 937 | (net_ip.s_addr >> 16) & 0xFF, |
| 938 | (net_ip.s_addr >> 24) & 0xFF); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 939 | |
Joe Hershberger | 463624d | 2018-07-03 19:22:56 -0500 | [diff] [blame] | 940 | printf("*** Warning: no boot file name; using '%s'\n", |
| 941 | nfs_path); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 944 | nfs_filename = basename(nfs_path); |
| 945 | nfs_path = dirname(nfs_path); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 946 | |
Joe Hershberger | 463624d | 2018-07-03 19:22:56 -0500 | [diff] [blame] | 947 | printf("Using %s device\n", eth_get_name()); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 948 | |
Joe Hershberger | 463624d | 2018-07-03 19:22:56 -0500 | [diff] [blame] | 949 | printf("File transfer via NFS from server %pI4; our IP address is %pI4", |
| 950 | &nfs_server_ip, &net_ip); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 951 | |
| 952 | /* Check if we need to send across this subnet */ |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 953 | if (net_gateway.s_addr && net_netmask.s_addr) { |
| 954 | struct in_addr our_net; |
| 955 | struct in_addr server_net; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 956 | |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 957 | our_net.s_addr = net_ip.s_addr & net_netmask.s_addr; |
Joe Hershberger | 7d9c9b2 | 2018-07-03 19:22:55 -0500 | [diff] [blame] | 958 | server_net.s_addr = nfs_server_ip.s_addr & net_netmask.s_addr; |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 959 | if (our_net.s_addr != server_net.s_addr) |
Joe Hershberger | 463624d | 2018-07-03 19:22:56 -0500 | [diff] [blame] | 960 | printf("; sending through gateway %pI4", |
| 961 | &net_gateway); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 962 | } |
Joe Hershberger | 463624d | 2018-07-03 19:22:56 -0500 | [diff] [blame] | 963 | printf("\nFilename '%s/%s'.", nfs_path, nfs_filename); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 964 | |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 965 | if (net_boot_file_expected_size_in_blocks) { |
Joe Hershberger | 463624d | 2018-07-03 19:22:56 -0500 | [diff] [blame] | 966 | printf(" Size is 0x%x Bytes = ", |
| 967 | net_boot_file_expected_size_in_blocks << 9); |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 968 | print_size(net_boot_file_expected_size_in_blocks << 9, ""); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 969 | } |
Simon Glass | 892265d | 2019-12-28 10:45:02 -0700 | [diff] [blame] | 970 | printf("\nLoad address: 0x%lx\nLoading: *\b", image_load_addr); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 971 | |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 972 | net_set_timeout_handler(nfs_timeout, nfs_timeout_handler); |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 973 | net_set_udp_handler(nfs_handler); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 974 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 975 | nfs_timeout_count = 0; |
| 976 | nfs_state = STATE_PRCLOOKUP_PROG_MOUNT_REQ; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 977 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 978 | /*nfs_our_port = 4096 + (get_ticks() % 3072);*/ |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 979 | /*FIX ME !!!*/ |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 980 | nfs_our_port = 1000; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 981 | |
| 982 | /* zero out server ether in case the server ip has changed */ |
Joe Hershberger | 8ecdbed | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 983 | memset(net_server_ethaddr, 0, 6); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 984 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 985 | nfs_send(); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 986 | } |