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