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