wdenk | cbd8a35 | 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 | |
| 25 | #include <common.h> |
| 26 | #include <command.h> |
| 27 | #include <net.h> |
| 28 | #include <malloc.h> |
Joe Hershberger | 55d5fd9 | 2015-03-22 17:09:08 -0500 | [diff] [blame] | 29 | #include <mapmem.h> |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 30 | #include "nfs.h" |
| 31 | #include "bootp.h" |
| 32 | |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 33 | #define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */ |
Hiroshi Ito | fe891ec | 2008-01-31 18:35:04 +0900 | [diff] [blame] | 34 | #define NFS_RETRY_COUNT 30 |
Tetsuyuki Kobayashi | 48a3e99 | 2012-07-03 22:25:21 +0000 | [diff] [blame] | 35 | #ifndef CONFIG_NFS_TIMEOUT |
| 36 | # define NFS_TIMEOUT 2000UL |
| 37 | #else |
| 38 | # define NFS_TIMEOUT CONFIG_NFS_TIMEOUT |
| 39 | #endif |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 40 | |
Matthias Brugger | fa84fa7 | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 41 | #define NFS_RPC_ERR 1 |
| 42 | #define NFS_RPC_DROP 124 |
| 43 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 44 | static int fs_mounted; |
| 45 | static unsigned long rpc_id; |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 46 | static int nfs_offset = -1; |
| 47 | static int nfs_len; |
Matthias Brugger | fa84fa7 | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 48 | static ulong nfs_timeout = NFS_TIMEOUT; |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 49 | |
| 50 | static char dirfh[NFS_FHSIZE]; /* file handle of directory */ |
| 51 | static char filefh[NFS_FHSIZE]; /* file handle of kernel image */ |
| 52 | |
Joe Hershberger | 22f6e99 | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 53 | static enum net_loop_state nfs_download_state; |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 54 | static IPaddr_t NfsServerIP; |
| 55 | static int NfsSrvMountPort; |
| 56 | static int NfsSrvNfsPort; |
| 57 | static int NfsOurPort; |
| 58 | static int NfsTimeoutCount; |
| 59 | static int NfsState; |
| 60 | #define STATE_PRCLOOKUP_PROG_MOUNT_REQ 1 |
| 61 | #define STATE_PRCLOOKUP_PROG_NFS_REQ 2 |
| 62 | #define STATE_MOUNT_REQ 3 |
| 63 | #define STATE_UMOUNT_REQ 4 |
| 64 | #define STATE_LOOKUP_REQ 5 |
| 65 | #define STATE_READ_REQ 6 |
| 66 | #define STATE_READLINK_REQ 7 |
| 67 | |
| 68 | static char default_filename[64]; |
| 69 | static char *nfs_filename; |
| 70 | static char *nfs_path; |
| 71 | static char nfs_path_buff[2048]; |
| 72 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 73 | static inline int |
| 74 | store_block(uchar *src, unsigned offset, unsigned len) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 75 | { |
wdenk | a084f7d | 2004-02-24 22:33:21 +0000 | [diff] [blame] | 76 | ulong newsize = offset + len; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 77 | #ifdef CONFIG_SYS_DIRECT_FLASH_NFS |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 78 | int i, rc = 0; |
| 79 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 80 | for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) { |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 81 | /* start address in flash? */ |
| 82 | if (load_addr + offset >= flash_info[i].start[0]) { |
| 83 | rc = 1; |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | if (rc) { /* Flash is destination for this packet */ |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 89 | rc = flash_write((uchar *)src, (ulong)(load_addr+offset), len); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 90 | if (rc) { |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 91 | flash_perror(rc); |
Wolfgang Denk | 23a7a32 | 2005-08-06 01:11:12 +0200 | [diff] [blame] | 92 | return -1; |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 93 | } |
| 94 | } else |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 95 | #endif /* CONFIG_SYS_DIRECT_FLASH_NFS */ |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 96 | { |
Joe Hershberger | 55d5fd9 | 2015-03-22 17:09:08 -0500 | [diff] [blame] | 97 | void *ptr = map_sysmem(load_addr + offset, len); |
| 98 | |
| 99 | memcpy(ptr, src, len); |
| 100 | unmap_sysmem(ptr); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 101 | } |
wdenk | a084f7d | 2004-02-24 22:33:21 +0000 | [diff] [blame] | 102 | |
| 103 | if (NetBootFileXferSize < (offset+len)) |
| 104 | NetBootFileXferSize = newsize; |
Wolfgang Denk | 23a7a32 | 2005-08-06 01:11:12 +0200 | [diff] [blame] | 105 | return 0; |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static char* |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 109 | basename(char *path) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 110 | { |
| 111 | char *fname; |
| 112 | |
| 113 | fname = path + strlen(path) - 1; |
| 114 | while (fname >= path) { |
| 115 | if (*fname == '/') { |
| 116 | fname++; |
| 117 | break; |
| 118 | } |
| 119 | fname--; |
| 120 | } |
| 121 | return fname; |
| 122 | } |
| 123 | |
| 124 | static char* |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 125 | dirname(char *path) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 126 | { |
| 127 | char *fname; |
| 128 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 129 | fname = basename(path); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 130 | --fname; |
| 131 | *fname = '\0'; |
| 132 | return path; |
| 133 | } |
| 134 | |
| 135 | /************************************************************************** |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 136 | RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries |
| 137 | **************************************************************************/ |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 138 | static long *rpc_add_credentials(long *p) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 139 | { |
| 140 | int hl; |
| 141 | int hostnamelen; |
| 142 | char hostname[256]; |
| 143 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 144 | strcpy(hostname, ""); |
| 145 | hostnamelen = strlen(hostname); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 146 | |
| 147 | /* Here's the executive summary on authentication requirements of the |
| 148 | * various NFS server implementations: Linux accepts both AUTH_NONE |
| 149 | * and AUTH_UNIX authentication (also accepts an empty hostname field |
| 150 | * in the AUTH_UNIX scheme). *BSD refuses AUTH_NONE, but accepts |
| 151 | * AUTH_UNIX (also accepts an empty hostname field in the AUTH_UNIX |
| 152 | * scheme). To be safe, use AUTH_UNIX and pass the hostname if we have |
| 153 | * it (if the BOOTP/DHCP reply didn't give one, just use an empty |
| 154 | * hostname). */ |
| 155 | |
| 156 | hl = (hostnamelen + 3) & ~3; |
| 157 | |
| 158 | /* Provide an AUTH_UNIX credential. */ |
| 159 | *p++ = htonl(1); /* AUTH_UNIX */ |
| 160 | *p++ = htonl(hl+20); /* auth length */ |
| 161 | *p++ = htonl(0); /* stamp */ |
| 162 | *p++ = htonl(hostnamelen); /* hostname string */ |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 163 | if (hostnamelen & 3) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 164 | *(p + hostnamelen / 4) = 0; /* add zero padding */ |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 165 | memcpy(p, hostname, hostnamelen); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 166 | p += hl / 4; |
| 167 | *p++ = 0; /* uid */ |
| 168 | *p++ = 0; /* gid */ |
| 169 | *p++ = 0; /* auxiliary gid list */ |
| 170 | |
| 171 | /* Provide an AUTH_NONE verifier. */ |
| 172 | *p++ = 0; /* AUTH_NONE */ |
| 173 | *p++ = 0; /* auth length */ |
| 174 | |
| 175 | return p; |
| 176 | } |
| 177 | |
| 178 | /************************************************************************** |
| 179 | RPC_LOOKUP - Lookup RPC Port numbers |
| 180 | **************************************************************************/ |
| 181 | static void |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 182 | rpc_req(int rpc_prog, int rpc_proc, uint32_t *data, int datalen) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 183 | { |
| 184 | struct rpc_t pkt; |
| 185 | unsigned long id; |
| 186 | uint32_t *p; |
| 187 | int pktlen; |
| 188 | int sport; |
| 189 | |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 190 | id = ++rpc_id; |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 191 | pkt.u.call.id = htonl(id); |
| 192 | pkt.u.call.type = htonl(MSG_CALL); |
| 193 | pkt.u.call.rpcvers = htonl(2); /* use RPC version 2 */ |
| 194 | pkt.u.call.prog = htonl(rpc_prog); |
| 195 | pkt.u.call.vers = htonl(2); /* portmapper is version 2 */ |
| 196 | pkt.u.call.proc = htonl(rpc_proc); |
| 197 | p = (uint32_t *)&(pkt.u.call.data); |
| 198 | |
| 199 | if (datalen) |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 200 | memcpy((char *)p, (char *)data, datalen*sizeof(uint32_t)); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 201 | |
| 202 | pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt; |
| 203 | |
Joe Hershberger | 594c26f | 2012-05-23 07:58:04 +0000 | [diff] [blame] | 204 | memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE, |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 205 | (char *)&pkt, pktlen); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 206 | |
| 207 | if (rpc_prog == PROG_PORTMAP) |
| 208 | sport = SUNRPC_PORT; |
| 209 | else if (rpc_prog == PROG_MOUNT) |
| 210 | sport = NfsSrvMountPort; |
| 211 | else |
| 212 | sport = NfsSrvNfsPort; |
| 213 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 214 | NetSendUDPPacket(NetServerEther, NfsServerIP, sport, NfsOurPort, |
| 215 | pktlen); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /************************************************************************** |
| 219 | RPC_LOOKUP - Lookup RPC Port numbers |
| 220 | **************************************************************************/ |
| 221 | static void |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 222 | rpc_lookup_req(int prog, int ver) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 223 | { |
| 224 | uint32_t data[16]; |
| 225 | |
| 226 | data[0] = 0; data[1] = 0; /* auth credential */ |
| 227 | data[2] = 0; data[3] = 0; /* auth verifier */ |
| 228 | data[4] = htonl(prog); |
| 229 | data[5] = htonl(ver); |
| 230 | data[6] = htonl(17); /* IP_UDP */ |
| 231 | data[7] = 0; |
| 232 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 233 | rpc_req(PROG_PORTMAP, PORTMAP_GETPORT, data, 8); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /************************************************************************** |
| 237 | NFS_MOUNT - Mount an NFS Filesystem |
| 238 | **************************************************************************/ |
| 239 | static void |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 240 | nfs_mount_req(char *path) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 241 | { |
| 242 | uint32_t data[1024]; |
| 243 | uint32_t *p; |
| 244 | int len; |
| 245 | int pathlen; |
| 246 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 247 | pathlen = strlen(path); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 248 | |
| 249 | p = &(data[0]); |
| 250 | p = (uint32_t *)rpc_add_credentials((long *)p); |
| 251 | |
| 252 | *p++ = htonl(pathlen); |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 253 | if (pathlen & 3) |
| 254 | *(p + pathlen / 4) = 0; |
| 255 | memcpy(p, path, pathlen); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 256 | p += (pathlen + 3) / 4; |
| 257 | |
| 258 | len = (uint32_t *)p - (uint32_t *)&(data[0]); |
| 259 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 260 | rpc_req(PROG_MOUNT, MOUNT_ADDENTRY, data, len); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /************************************************************************** |
| 264 | NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server |
| 265 | **************************************************************************/ |
| 266 | static void |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 267 | nfs_umountall_req(void) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 268 | { |
| 269 | uint32_t data[1024]; |
| 270 | uint32_t *p; |
| 271 | int len; |
| 272 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 273 | if ((NfsSrvMountPort == -1) || (!fs_mounted)) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 274 | /* Nothing mounted, nothing to umount */ |
| 275 | return; |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 276 | |
| 277 | p = &(data[0]); |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 278 | p = (uint32_t *)rpc_add_credentials((long *)p); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 279 | |
| 280 | len = (uint32_t *)p - (uint32_t *)&(data[0]); |
| 281 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 282 | rpc_req(PROG_MOUNT, MOUNT_UMOUNTALL, data, len); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /*************************************************************************** |
| 286 | * NFS_READLINK (AH 2003-07-14) |
| 287 | * This procedure is called when read of the first block fails - |
| 288 | * this probably happens when it's a directory or a symlink |
| 289 | * In case of successful readlink(), the dirname is manipulated, |
| 290 | * so that inside the nfs() function a recursion can be done. |
| 291 | **************************************************************************/ |
| 292 | static void |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 293 | nfs_readlink_req(void) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 294 | { |
| 295 | uint32_t data[1024]; |
| 296 | uint32_t *p; |
| 297 | int len; |
| 298 | |
| 299 | p = &(data[0]); |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 300 | p = (uint32_t *)rpc_add_credentials((long *)p); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 301 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 302 | memcpy(p, filefh, NFS_FHSIZE); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 303 | p += (NFS_FHSIZE / 4); |
| 304 | |
| 305 | len = (uint32_t *)p - (uint32_t *)&(data[0]); |
| 306 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 307 | rpc_req(PROG_NFS, NFS_READLINK, data, len); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | /************************************************************************** |
| 311 | NFS_LOOKUP - Lookup Pathname |
| 312 | **************************************************************************/ |
| 313 | static void |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 314 | nfs_lookup_req(char *fname) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 315 | { |
| 316 | uint32_t data[1024]; |
| 317 | uint32_t *p; |
| 318 | int len; |
| 319 | int fnamelen; |
| 320 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 321 | fnamelen = strlen(fname); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 322 | |
| 323 | p = &(data[0]); |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 324 | p = (uint32_t *)rpc_add_credentials((long *)p); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 325 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 326 | memcpy(p, dirfh, NFS_FHSIZE); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 327 | p += (NFS_FHSIZE / 4); |
| 328 | *p++ = htonl(fnamelen); |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 329 | if (fnamelen & 3) |
| 330 | *(p + fnamelen / 4) = 0; |
| 331 | memcpy(p, fname, fnamelen); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 332 | p += (fnamelen + 3) / 4; |
| 333 | |
| 334 | len = (uint32_t *)p - (uint32_t *)&(data[0]); |
| 335 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 336 | rpc_req(PROG_NFS, NFS_LOOKUP, data, len); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | /************************************************************************** |
| 340 | NFS_READ - Read File on NFS Server |
| 341 | **************************************************************************/ |
| 342 | static void |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 343 | nfs_read_req(int offset, int readlen) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 344 | { |
| 345 | uint32_t data[1024]; |
| 346 | uint32_t *p; |
| 347 | int len; |
| 348 | |
| 349 | p = &(data[0]); |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 350 | p = (uint32_t *)rpc_add_credentials((long *)p); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 351 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 352 | memcpy(p, filefh, NFS_FHSIZE); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 353 | p += (NFS_FHSIZE / 4); |
| 354 | *p++ = htonl(offset); |
| 355 | *p++ = htonl(readlen); |
| 356 | *p++ = 0; |
| 357 | |
| 358 | len = (uint32_t *)p - (uint32_t *)&(data[0]); |
| 359 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 360 | rpc_req(PROG_NFS, NFS_READ, data, len); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | /************************************************************************** |
| 364 | RPC request dispatcher |
| 365 | **************************************************************************/ |
| 366 | |
| 367 | static void |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 368 | NfsSend(void) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 369 | { |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 370 | debug("%s\n", __func__); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 371 | |
| 372 | switch (NfsState) { |
| 373 | case STATE_PRCLOOKUP_PROG_MOUNT_REQ: |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 374 | rpc_lookup_req(PROG_MOUNT, 1); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 375 | break; |
| 376 | case STATE_PRCLOOKUP_PROG_NFS_REQ: |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 377 | rpc_lookup_req(PROG_NFS, 2); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 378 | break; |
| 379 | case STATE_MOUNT_REQ: |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 380 | nfs_mount_req(nfs_path); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 381 | break; |
| 382 | case STATE_UMOUNT_REQ: |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 383 | nfs_umountall_req(); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 384 | break; |
| 385 | case STATE_LOOKUP_REQ: |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 386 | nfs_lookup_req(nfs_filename); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 387 | break; |
| 388 | case STATE_READ_REQ: |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 389 | nfs_read_req(nfs_offset, nfs_len); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 390 | break; |
| 391 | case STATE_READLINK_REQ: |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 392 | nfs_readlink_req(); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 393 | break; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | /************************************************************************** |
| 398 | Handlers for the reply from server |
| 399 | **************************************************************************/ |
| 400 | |
| 401 | static int |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 402 | rpc_lookup_reply(int prog, uchar *pkt, unsigned len) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 403 | { |
| 404 | struct rpc_t rpc_pkt; |
| 405 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 406 | memcpy((unsigned char *)&rpc_pkt, pkt, len); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 407 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 408 | debug("%s\n", __func__); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 409 | |
Matthias Brugger | fa84fa7 | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 410 | if (ntohl(rpc_pkt.u.reply.id) > rpc_id) |
| 411 | return -NFS_RPC_ERR; |
| 412 | else if (ntohl(rpc_pkt.u.reply.id) < rpc_id) |
| 413 | return -NFS_RPC_DROP; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 414 | |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 415 | if (rpc_pkt.u.reply.rstatus || |
| 416 | rpc_pkt.u.reply.verifier || |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 417 | rpc_pkt.u.reply.astatus) |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 418 | return -1; |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 419 | |
| 420 | switch (prog) { |
| 421 | case PROG_MOUNT: |
| 422 | NfsSrvMountPort = ntohl(rpc_pkt.u.reply.data[0]); |
| 423 | break; |
| 424 | case PROG_NFS: |
| 425 | NfsSrvNfsPort = ntohl(rpc_pkt.u.reply.data[0]); |
| 426 | break; |
| 427 | } |
| 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | static int |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 433 | nfs_mount_reply(uchar *pkt, unsigned len) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 434 | { |
| 435 | struct rpc_t rpc_pkt; |
| 436 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 437 | debug("%s\n", __func__); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 438 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 439 | memcpy((unsigned char *)&rpc_pkt, pkt, len); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 440 | |
Matthias Brugger | fa84fa7 | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 441 | if (ntohl(rpc_pkt.u.reply.id) > rpc_id) |
| 442 | return -NFS_RPC_ERR; |
| 443 | else if (ntohl(rpc_pkt.u.reply.id) < rpc_id) |
| 444 | return -NFS_RPC_DROP; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 445 | |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 446 | if (rpc_pkt.u.reply.rstatus || |
| 447 | rpc_pkt.u.reply.verifier || |
| 448 | rpc_pkt.u.reply.astatus || |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 449 | rpc_pkt.u.reply.data[0]) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 450 | return -1; |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 451 | |
| 452 | fs_mounted = 1; |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 453 | memcpy(dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | static int |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 459 | nfs_umountall_reply(uchar *pkt, unsigned len) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 460 | { |
| 461 | struct rpc_t rpc_pkt; |
| 462 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 463 | debug("%s\n", __func__); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 464 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 465 | memcpy((unsigned char *)&rpc_pkt, pkt, len); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 466 | |
Matthias Brugger | fa84fa7 | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 467 | if (ntohl(rpc_pkt.u.reply.id) > rpc_id) |
| 468 | return -NFS_RPC_ERR; |
| 469 | else if (ntohl(rpc_pkt.u.reply.id) < rpc_id) |
| 470 | return -NFS_RPC_DROP; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 471 | |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 472 | if (rpc_pkt.u.reply.rstatus || |
| 473 | rpc_pkt.u.reply.verifier || |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 474 | rpc_pkt.u.reply.astatus) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 475 | return -1; |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 476 | |
| 477 | fs_mounted = 0; |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 478 | memset(dirfh, 0, sizeof(dirfh)); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 479 | |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | static int |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 484 | nfs_lookup_reply(uchar *pkt, unsigned len) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 485 | { |
| 486 | struct rpc_t rpc_pkt; |
| 487 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 488 | debug("%s\n", __func__); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 489 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 490 | memcpy((unsigned char *)&rpc_pkt, pkt, len); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 491 | |
Matthias Brugger | fa84fa7 | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 492 | if (ntohl(rpc_pkt.u.reply.id) > rpc_id) |
| 493 | return -NFS_RPC_ERR; |
| 494 | else if (ntohl(rpc_pkt.u.reply.id) < rpc_id) |
| 495 | return -NFS_RPC_DROP; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 496 | |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 497 | if (rpc_pkt.u.reply.rstatus || |
| 498 | rpc_pkt.u.reply.verifier || |
| 499 | rpc_pkt.u.reply.astatus || |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 500 | rpc_pkt.u.reply.data[0]) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 501 | return -1; |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 502 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 503 | memcpy(filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 504 | |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | static int |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 509 | nfs_readlink_reply(uchar *pkt, unsigned len) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 510 | { |
| 511 | struct rpc_t rpc_pkt; |
| 512 | int rlen; |
| 513 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 514 | debug("%s\n", __func__); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 515 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 516 | memcpy((unsigned char *)&rpc_pkt, pkt, len); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 517 | |
Matthias Brugger | fa84fa7 | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 518 | if (ntohl(rpc_pkt.u.reply.id) > rpc_id) |
| 519 | return -NFS_RPC_ERR; |
| 520 | else if (ntohl(rpc_pkt.u.reply.id) < rpc_id) |
| 521 | return -NFS_RPC_DROP; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 522 | |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 523 | if (rpc_pkt.u.reply.rstatus || |
| 524 | rpc_pkt.u.reply.verifier || |
| 525 | rpc_pkt.u.reply.astatus || |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 526 | rpc_pkt.u.reply.data[0]) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 527 | return -1; |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 528 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 529 | rlen = ntohl(rpc_pkt.u.reply.data[1]); /* new path length */ |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 530 | |
| 531 | if (*((char *)&(rpc_pkt.u.reply.data[2])) != '/') { |
| 532 | int pathlen; |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 533 | strcat(nfs_path, "/"); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 534 | pathlen = strlen(nfs_path); |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 535 | memcpy(nfs_path + pathlen, (uchar *)&(rpc_pkt.u.reply.data[2]), |
| 536 | rlen); |
Ed Swarthout | f64ef9b | 2009-11-19 02:47:28 -0600 | [diff] [blame] | 537 | nfs_path[pathlen + rlen] = 0; |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 538 | } else { |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 539 | memcpy(nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 540 | nfs_path[rlen] = 0; |
| 541 | } |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | static int |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 546 | nfs_read_reply(uchar *pkt, unsigned len) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 547 | { |
| 548 | struct rpc_t rpc_pkt; |
| 549 | int rlen; |
| 550 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 551 | debug("%s\n", __func__); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 552 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 553 | memcpy((uchar *)&rpc_pkt, pkt, sizeof(rpc_pkt.u.reply)); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 554 | |
Matthias Brugger | fa84fa7 | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 555 | if (ntohl(rpc_pkt.u.reply.id) > rpc_id) |
| 556 | return -NFS_RPC_ERR; |
| 557 | else if (ntohl(rpc_pkt.u.reply.id) < rpc_id) |
| 558 | return -NFS_RPC_DROP; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 559 | |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 560 | if (rpc_pkt.u.reply.rstatus || |
| 561 | rpc_pkt.u.reply.verifier || |
| 562 | rpc_pkt.u.reply.astatus || |
| 563 | rpc_pkt.u.reply.data[0]) { |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 564 | if (rpc_pkt.u.reply.rstatus) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 565 | return -9999; |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 566 | if (rpc_pkt.u.reply.astatus) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 567 | return -9999; |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 568 | return -ntohl(rpc_pkt.u.reply.data[0]); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 571 | if ((nfs_offset != 0) && !((nfs_offset) % |
| 572 | (NFS_READ_SIZE / 2 * 10 * HASHES_PER_LINE))) |
| 573 | puts("\n\t "); |
| 574 | if (!(nfs_offset % ((NFS_READ_SIZE / 2) * 10))) |
| 575 | putc('#'); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 576 | |
| 577 | rlen = ntohl(rpc_pkt.u.reply.data[18]); |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 578 | if (store_block((uchar *)pkt + sizeof(rpc_pkt.u.reply), |
| 579 | nfs_offset, rlen)) |
Wolfgang Denk | 23a7a32 | 2005-08-06 01:11:12 +0200 | [diff] [blame] | 580 | return -9999; |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 581 | |
| 582 | return rlen; |
| 583 | } |
| 584 | |
| 585 | /************************************************************************** |
| 586 | Interfaces of U-BOOT |
| 587 | **************************************************************************/ |
| 588 | |
| 589 | static void |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 590 | NfsTimeout(void) |
wdenk | a5725fa | 2004-09-28 21:51:42 +0000 | [diff] [blame] | 591 | { |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 592 | if (++NfsTimeoutCount > NFS_RETRY_COUNT) { |
| 593 | puts("\nRetry count exceeded; starting again\n"); |
| 594 | NetStartAgain(); |
Evan Samanas | aabb8cb | 2009-11-09 20:08:36 -0600 | [diff] [blame] | 595 | } else { |
| 596 | puts("T "); |
Matthias Brugger | fa84fa7 | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 597 | NetSetTimeout(nfs_timeout + NFS_TIMEOUT * NfsTimeoutCount, |
| 598 | NfsTimeout); |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 599 | NfsSend(); |
Hiroshi Ito | fe891ec | 2008-01-31 18:35:04 +0900 | [diff] [blame] | 600 | } |
wdenk | a5725fa | 2004-09-28 21:51:42 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | static void |
Luca Ceresoli | 03eb129 | 2011-04-18 06:19:50 +0000 | [diff] [blame] | 604 | NfsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 605 | { |
| 606 | int rlen; |
Matthias Brugger | fa84fa7 | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 607 | int reply; |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 608 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 609 | debug("%s\n", __func__); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 610 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 611 | if (dest != NfsOurPort) |
| 612 | return; |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 613 | |
| 614 | switch (NfsState) { |
| 615 | case STATE_PRCLOOKUP_PROG_MOUNT_REQ: |
Matthias Brugger | fa84fa7 | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 616 | if (rpc_lookup_reply(PROG_MOUNT, pkt, len) == -NFS_RPC_DROP) |
| 617 | break; |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 618 | NfsState = STATE_PRCLOOKUP_PROG_NFS_REQ; |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 619 | NfsSend(); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 620 | break; |
| 621 | |
| 622 | case STATE_PRCLOOKUP_PROG_NFS_REQ: |
Matthias Brugger | fa84fa7 | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 623 | if (rpc_lookup_reply(PROG_NFS, pkt, len) == -NFS_RPC_DROP) |
| 624 | break; |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 625 | NfsState = STATE_MOUNT_REQ; |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 626 | NfsSend(); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 627 | break; |
| 628 | |
| 629 | case STATE_MOUNT_REQ: |
Matthias Brugger | fa84fa7 | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 630 | reply = nfs_mount_reply(pkt, len); |
| 631 | if (reply == -NFS_RPC_DROP) |
| 632 | break; |
| 633 | else if (reply == -NFS_RPC_ERR) { |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 634 | puts("*** ERROR: Cannot mount\n"); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 635 | /* just to be sure... */ |
| 636 | NfsState = STATE_UMOUNT_REQ; |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 637 | NfsSend(); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 638 | } else { |
| 639 | NfsState = STATE_LOOKUP_REQ; |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 640 | NfsSend(); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 641 | } |
| 642 | break; |
| 643 | |
| 644 | case STATE_UMOUNT_REQ: |
Matthias Brugger | fa84fa7 | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 645 | reply = nfs_umountall_reply(pkt, len); |
| 646 | if (reply == -NFS_RPC_DROP) |
| 647 | break; |
| 648 | else if (reply == -NFS_RPC_ERR) { |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 649 | puts("*** ERROR: Cannot umount\n"); |
Joe Hershberger | 22f6e99 | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 650 | net_set_state(NETLOOP_FAIL); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 651 | } else { |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 652 | puts("\ndone\n"); |
Joe Hershberger | 22f6e99 | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 653 | net_set_state(nfs_download_state); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 654 | } |
| 655 | break; |
| 656 | |
| 657 | case STATE_LOOKUP_REQ: |
Matthias Brugger | fa84fa7 | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 658 | reply = nfs_lookup_reply(pkt, len); |
| 659 | if (reply == -NFS_RPC_DROP) |
| 660 | break; |
| 661 | else if (reply == -NFS_RPC_ERR) { |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 662 | puts("*** ERROR: File lookup fail\n"); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 663 | NfsState = STATE_UMOUNT_REQ; |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 664 | NfsSend(); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 665 | } else { |
| 666 | NfsState = STATE_READ_REQ; |
| 667 | nfs_offset = 0; |
| 668 | nfs_len = NFS_READ_SIZE; |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 669 | NfsSend(); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 670 | } |
| 671 | break; |
| 672 | |
| 673 | case STATE_READLINK_REQ: |
Matthias Brugger | fa84fa7 | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 674 | reply = nfs_readlink_reply(pkt, len); |
| 675 | if (reply == -NFS_RPC_DROP) |
| 676 | break; |
| 677 | else if (reply == -NFS_RPC_ERR) { |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 678 | puts("*** ERROR: Symlink fail\n"); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 679 | NfsState = STATE_UMOUNT_REQ; |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 680 | NfsSend(); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 681 | } else { |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 682 | debug("Symlink --> %s\n", nfs_path); |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 683 | nfs_filename = basename(nfs_path); |
| 684 | nfs_path = dirname(nfs_path); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 685 | |
| 686 | NfsState = STATE_MOUNT_REQ; |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 687 | NfsSend(); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 688 | } |
| 689 | break; |
| 690 | |
| 691 | case STATE_READ_REQ: |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 692 | rlen = nfs_read_reply(pkt, len); |
Matthias Brugger | fa84fa7 | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 693 | NetSetTimeout(nfs_timeout, NfsTimeout); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 694 | if (rlen > 0) { |
| 695 | nfs_offset += rlen; |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 696 | NfsSend(); |
| 697 | } else if ((rlen == -NFSERR_ISDIR) || (rlen == -NFSERR_INVAL)) { |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 698 | /* symbolic link */ |
| 699 | NfsState = STATE_READLINK_REQ; |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 700 | NfsSend(); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 701 | } else { |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 702 | if (!rlen) |
Joe Hershberger | 22f6e99 | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 703 | nfs_download_state = NETLOOP_SUCCESS; |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 704 | NfsState = STATE_UMOUNT_REQ; |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 705 | NfsSend(); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 706 | } |
| 707 | break; |
| 708 | } |
| 709 | } |
| 710 | |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 711 | |
| 712 | void |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 713 | NfsStart(void) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 714 | { |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 715 | debug("%s\n", __func__); |
Joe Hershberger | 22f6e99 | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 716 | nfs_download_state = NETLOOP_FAIL; |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 717 | |
| 718 | NfsServerIP = NetServerIP; |
| 719 | nfs_path = (char *)nfs_path_buff; |
| 720 | |
| 721 | if (nfs_path == NULL) { |
Joe Hershberger | 22f6e99 | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 722 | net_set_state(NETLOOP_FAIL); |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 723 | puts("*** ERROR: Fail allocate memory\n"); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 724 | return; |
| 725 | } |
| 726 | |
| 727 | if (BootFile[0] == '\0') { |
Matthias Weisser | ea45cb0 | 2011-12-03 03:29:44 +0000 | [diff] [blame] | 728 | sprintf(default_filename, "/nfsroot/%02X%02X%02X%02X.img", |
Wolfgang Denk | c43352c | 2005-08-04 01:09:44 +0200 | [diff] [blame] | 729 | NetOurIP & 0xFF, |
| 730 | (NetOurIP >> 8) & 0xFF, |
| 731 | (NetOurIP >> 16) & 0xFF, |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 732 | (NetOurIP >> 24) & 0xFF); |
| 733 | strcpy(nfs_path, default_filename); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 734 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 735 | printf("*** Warning: no boot file name; using '%s'\n", |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 736 | nfs_path); |
| 737 | } else { |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 738 | char *p = BootFile; |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 739 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 740 | p = strchr(p, ':'); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 741 | |
| 742 | if (p != NULL) { |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 743 | NfsServerIP = string_to_ip(BootFile); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 744 | ++p; |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 745 | strcpy(nfs_path, p); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 746 | } else { |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 747 | strcpy(nfs_path, BootFile); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 751 | nfs_filename = basename(nfs_path); |
| 752 | nfs_path = dirname(nfs_path); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 753 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 754 | printf("Using %s device\n", eth_get_name()); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 755 | |
Mike Frysinger | b6446b6 | 2009-02-17 00:00:53 -0500 | [diff] [blame] | 756 | printf("File transfer via NFS from server %pI4" |
| 757 | "; our IP address is %pI4", &NfsServerIP, &NetOurIP); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 758 | |
| 759 | /* Check if we need to send across this subnet */ |
| 760 | if (NetOurGatewayIP && NetOurSubnetMask) { |
| 761 | IPaddr_t OurNet = NetOurIP & NetOurSubnetMask; |
| 762 | IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask; |
| 763 | |
Mike Frysinger | b6446b6 | 2009-02-17 00:00:53 -0500 | [diff] [blame] | 764 | if (OurNet != ServerNet) |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 765 | printf("; sending through gateway %pI4", |
| 766 | &NetOurGatewayIP); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 767 | } |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 768 | printf("\nFilename '%s/%s'.", nfs_path, nfs_filename); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 769 | |
| 770 | if (NetBootFileSize) { |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 771 | printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9); |
| 772 | print_size(NetBootFileSize<<9, ""); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 773 | } |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 774 | printf("\nLoad address: 0x%lx\n" |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 775 | "Loading: *\b", load_addr); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 776 | |
Matthias Brugger | fa84fa7 | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 777 | NetSetTimeout(nfs_timeout, NfsTimeout); |
Joe Hershberger | ece223b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 778 | net_set_udp_handler(NfsHandler); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 779 | |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 780 | NfsTimeoutCount = 0; |
| 781 | NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ; |
| 782 | |
| 783 | /*NfsOurPort = 4096 + (get_ticks() % 3072);*/ |
| 784 | /*FIX ME !!!*/ |
| 785 | NfsOurPort = 1000; |
| 786 | |
| 787 | /* zero out server ether in case the server ip has changed */ |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 788 | memset(NetServerEther, 0, 6); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 789 | |
Joe Hershberger | c9f6c91 | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 790 | NfsSend(); |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 791 | } |