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