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 | |
| 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 */ |
wdenk | 0168663 | 2004-06-30 22:59:18 +0000 | [diff] [blame^] | 37 | #define NFS_TIMEOUT 60 |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 38 | |
| 39 | static int fs_mounted = 0; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 40 | static unsigned long rpc_id = 0; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 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 | 77ef4c6 | 2004-02-24 22:33:21 +0000 | [diff] [blame] | 69 | ulong newsize = offset + len; |
wdenk | be9c1cb | 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 | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 90 | { |
| 91 | (void)memcpy ((void *)(load_addr + offset), src, len); |
| 92 | } |
wdenk | 77ef4c6 | 2004-02-24 22:33:21 +0000 | [diff] [blame] | 93 | |
| 94 | if (NetBootFileXferSize < (offset+len)) |
| 95 | NetBootFileXferSize = newsize; |
wdenk | be9c1cb | 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 | be9c1cb | 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 | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 181 | id = ++rpc_id; |
wdenk | be9c1cb | 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 | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 195 | memcpy ((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, (char *)&pkt, pktlen); |
wdenk | be9c1cb | 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 | { |
| 358 | #ifdef NFS_DEBUG |
| 359 | printf ("%s\n", __FUNCTION__); |
| 360 | #endif |
| 361 | |
| 362 | switch (NfsState) { |
| 363 | case STATE_PRCLOOKUP_PROG_MOUNT_REQ: |
| 364 | rpc_lookup_req (PROG_MOUNT, 1); |
| 365 | break; |
| 366 | case STATE_PRCLOOKUP_PROG_NFS_REQ: |
| 367 | rpc_lookup_req (PROG_NFS, 2); |
| 368 | break; |
| 369 | case STATE_MOUNT_REQ: |
| 370 | nfs_mount_req (nfs_path); |
| 371 | break; |
| 372 | case STATE_UMOUNT_REQ: |
| 373 | nfs_umountall_req (); |
| 374 | break; |
| 375 | case STATE_LOOKUP_REQ: |
| 376 | nfs_lookup_req (nfs_filename); |
| 377 | break; |
| 378 | case STATE_READ_REQ: |
| 379 | nfs_read_req (nfs_offset, nfs_len); |
| 380 | break; |
| 381 | case STATE_READLINK_REQ: |
| 382 | nfs_readlink_req (); |
| 383 | break; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | /************************************************************************** |
| 388 | Handlers for the reply from server |
| 389 | **************************************************************************/ |
| 390 | |
| 391 | static int |
| 392 | rpc_lookup_reply (int prog, uchar *pkt, unsigned len) |
| 393 | { |
| 394 | struct rpc_t rpc_pkt; |
| 395 | |
| 396 | memcpy ((unsigned char *)&rpc_pkt, pkt, len); |
| 397 | |
| 398 | #ifdef NFS_DEBUG |
| 399 | printf ("%s\n", __FUNCTION__); |
| 400 | #endif |
| 401 | |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 402 | if (ntohl(rpc_pkt.u.reply.id) != rpc_id) |
| 403 | return -1; |
| 404 | |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 405 | if (rpc_pkt.u.reply.rstatus || |
| 406 | rpc_pkt.u.reply.verifier || |
| 407 | rpc_pkt.u.reply.astatus || |
| 408 | rpc_pkt.u.reply.astatus) { |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 409 | return -1; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | switch (prog) { |
| 413 | case PROG_MOUNT: |
| 414 | NfsSrvMountPort = ntohl(rpc_pkt.u.reply.data[0]); |
| 415 | break; |
| 416 | case PROG_NFS: |
| 417 | NfsSrvNfsPort = ntohl(rpc_pkt.u.reply.data[0]); |
| 418 | break; |
| 419 | } |
| 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | static int |
| 425 | nfs_mount_reply (uchar *pkt, unsigned len) |
| 426 | { |
| 427 | struct rpc_t rpc_pkt; |
| 428 | |
| 429 | #ifdef NFS_DEBUG |
| 430 | printf ("%s\n", __FUNCTION__); |
| 431 | #endif |
| 432 | |
| 433 | memcpy ((unsigned char *)&rpc_pkt, pkt, len); |
| 434 | |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 435 | if (ntohl(rpc_pkt.u.reply.id) != rpc_id) |
| 436 | return -1; |
| 437 | |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 438 | if (rpc_pkt.u.reply.rstatus || |
| 439 | rpc_pkt.u.reply.verifier || |
| 440 | rpc_pkt.u.reply.astatus || |
| 441 | rpc_pkt.u.reply.data[0]) { |
| 442 | return -1; |
| 443 | } |
| 444 | |
| 445 | fs_mounted = 1; |
| 446 | memcpy (dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE); |
| 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | static int |
| 452 | nfs_umountall_reply (uchar *pkt, unsigned len) |
| 453 | { |
| 454 | struct rpc_t rpc_pkt; |
| 455 | |
| 456 | #ifdef NFS_DEBUG |
| 457 | printf ("%s\n", __FUNCTION__); |
| 458 | #endif |
| 459 | |
| 460 | memcpy ((unsigned char *)&rpc_pkt, pkt, len); |
| 461 | |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 462 | if (ntohl(rpc_pkt.u.reply.id) != rpc_id) |
| 463 | return -1; |
| 464 | |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 465 | if (rpc_pkt.u.reply.rstatus || |
| 466 | rpc_pkt.u.reply.verifier || |
| 467 | rpc_pkt.u.reply.astatus) { |
| 468 | return -1; |
| 469 | } |
| 470 | |
| 471 | fs_mounted = 0; |
| 472 | memset (dirfh, 0, sizeof(dirfh)); |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | static int |
| 478 | nfs_lookup_reply (uchar *pkt, unsigned len) |
| 479 | { |
| 480 | struct rpc_t rpc_pkt; |
| 481 | |
| 482 | #ifdef NFS_DEBUG |
| 483 | printf ("%s\n", __FUNCTION__); |
| 484 | #endif |
| 485 | |
| 486 | memcpy ((unsigned char *)&rpc_pkt, pkt, len); |
| 487 | |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 488 | if (ntohl(rpc_pkt.u.reply.id) != rpc_id) |
| 489 | return -1; |
| 490 | |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 491 | if (rpc_pkt.u.reply.rstatus || |
| 492 | rpc_pkt.u.reply.verifier || |
| 493 | rpc_pkt.u.reply.astatus || |
| 494 | rpc_pkt.u.reply.data[0]) { |
| 495 | return -1; |
| 496 | } |
| 497 | |
| 498 | memcpy (filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE); |
| 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | static int |
| 504 | nfs_readlink_reply (uchar *pkt, unsigned len) |
| 505 | { |
| 506 | struct rpc_t rpc_pkt; |
| 507 | int rlen; |
| 508 | |
| 509 | #ifdef NFS_DEBUG |
| 510 | printf ("%s\n", __FUNCTION__); |
| 511 | #endif |
| 512 | |
| 513 | memcpy ((unsigned char *)&rpc_pkt, pkt, len); |
| 514 | |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 515 | if (ntohl(rpc_pkt.u.reply.id) != rpc_id) |
| 516 | return -1; |
| 517 | |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 518 | if (rpc_pkt.u.reply.rstatus || |
| 519 | rpc_pkt.u.reply.verifier || |
| 520 | rpc_pkt.u.reply.astatus || |
| 521 | rpc_pkt.u.reply.data[0]) { |
| 522 | return -1; |
| 523 | } |
| 524 | |
| 525 | rlen = ntohl (rpc_pkt.u.reply.data[1]); /* new path length */ |
| 526 | |
| 527 | if (*((char *)&(rpc_pkt.u.reply.data[2])) != '/') { |
| 528 | int pathlen; |
| 529 | strcat (nfs_path, "/"); |
| 530 | pathlen = strlen(nfs_path); |
| 531 | memcpy (nfs_path+pathlen, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen); |
| 532 | nfs_path[pathlen+rlen+1] = 0; |
| 533 | } else { |
| 534 | memcpy (nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen); |
| 535 | nfs_path[rlen] = 0; |
| 536 | } |
| 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | static int |
| 541 | nfs_read_reply (uchar *pkt, unsigned len) |
| 542 | { |
| 543 | struct rpc_t rpc_pkt; |
| 544 | int rlen; |
| 545 | |
| 546 | #ifdef NFS_DEBUG_nop |
| 547 | printf ("%s\n", __FUNCTION__); |
| 548 | #endif |
| 549 | |
wdenk | d729d30 | 2004-02-27 00:07:27 +0000 | [diff] [blame] | 550 | memcpy ((uchar *)&rpc_pkt, pkt, sizeof(rpc_pkt.u.reply)); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 551 | |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 552 | if (ntohl(rpc_pkt.u.reply.id) != rpc_id) |
| 553 | return -1; |
| 554 | |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 555 | if (rpc_pkt.u.reply.rstatus || |
| 556 | rpc_pkt.u.reply.verifier || |
| 557 | rpc_pkt.u.reply.astatus || |
| 558 | rpc_pkt.u.reply.data[0]) { |
| 559 | if (rpc_pkt.u.reply.rstatus) { |
| 560 | return -9999; |
| 561 | } |
| 562 | if (rpc_pkt.u.reply.astatus) { |
| 563 | return -9999; |
| 564 | } |
| 565 | return -ntohl(rpc_pkt.u.reply.data[0]);; |
| 566 | } |
| 567 | |
| 568 | if ((nfs_offset!=0) && !((nfs_offset) % (NFS_READ_SIZE/2*10*HASHES_PER_LINE))) { |
| 569 | puts ("\n\t "); |
| 570 | } |
| 571 | if (!(nfs_offset % ((NFS_READ_SIZE/2)*10))) { |
| 572 | putc ('#'); |
| 573 | } |
| 574 | |
| 575 | rlen = ntohl(rpc_pkt.u.reply.data[18]); |
wdenk | d729d30 | 2004-02-27 00:07:27 +0000 | [diff] [blame] | 576 | store_block ((uchar *)pkt+sizeof(rpc_pkt.u.reply), nfs_offset, rlen); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 577 | |
| 578 | return rlen; |
| 579 | } |
| 580 | |
| 581 | /************************************************************************** |
| 582 | Interfaces of U-BOOT |
| 583 | **************************************************************************/ |
| 584 | |
| 585 | static void |
| 586 | NfsHandler (uchar *pkt, unsigned dest, unsigned src, unsigned len) |
| 587 | { |
| 588 | int rlen; |
| 589 | |
| 590 | #ifdef NFS_DEBUG |
| 591 | printf ("%s\n", __FUNCTION__); |
| 592 | #endif |
| 593 | |
| 594 | if (dest != NfsOurPort) return; |
| 595 | |
| 596 | switch (NfsState) { |
| 597 | case STATE_PRCLOOKUP_PROG_MOUNT_REQ: |
| 598 | rpc_lookup_reply (PROG_MOUNT, pkt, len); |
| 599 | NfsState = STATE_PRCLOOKUP_PROG_NFS_REQ; |
| 600 | NfsSend (); |
| 601 | break; |
| 602 | |
| 603 | case STATE_PRCLOOKUP_PROG_NFS_REQ: |
| 604 | rpc_lookup_reply (PROG_NFS, pkt, len); |
| 605 | NfsState = STATE_MOUNT_REQ; |
| 606 | NfsSend (); |
| 607 | break; |
| 608 | |
| 609 | case STATE_MOUNT_REQ: |
| 610 | if (nfs_mount_reply(pkt, len)) { |
| 611 | puts ("*** ERROR: Cannot mount\n"); |
| 612 | /* just to be sure... */ |
| 613 | NfsState = STATE_UMOUNT_REQ; |
| 614 | NfsSend (); |
| 615 | } else { |
| 616 | NfsState = STATE_LOOKUP_REQ; |
| 617 | NfsSend (); |
| 618 | } |
| 619 | break; |
| 620 | |
| 621 | case STATE_UMOUNT_REQ: |
| 622 | if (nfs_umountall_reply(pkt, len)) { |
| 623 | puts ("*** ERROR: Cannot umount\n"); |
| 624 | NetState = NETLOOP_FAIL; |
| 625 | } else { |
wdenk | 77ef4c6 | 2004-02-24 22:33:21 +0000 | [diff] [blame] | 626 | puts ("\ndone\n"); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 627 | NetState = NETLOOP_SUCCESS; |
| 628 | } |
| 629 | break; |
| 630 | |
| 631 | case STATE_LOOKUP_REQ: |
| 632 | if (nfs_lookup_reply(pkt, len)) { |
| 633 | puts ("*** ERROR: File lookup fail\n"); |
| 634 | NfsState = STATE_UMOUNT_REQ; |
| 635 | NfsSend (); |
| 636 | } else { |
| 637 | NfsState = STATE_READ_REQ; |
| 638 | nfs_offset = 0; |
| 639 | nfs_len = NFS_READ_SIZE; |
| 640 | NfsSend (); |
| 641 | } |
| 642 | break; |
| 643 | |
| 644 | case STATE_READLINK_REQ: |
| 645 | if (nfs_readlink_reply(pkt, len)) { |
| 646 | puts ("*** ERROR: Symlink fail\n"); |
| 647 | NfsState = STATE_UMOUNT_REQ; |
| 648 | NfsSend (); |
| 649 | } else { |
| 650 | #ifdef NFS_DEBUG |
| 651 | printf ("Symlink --> %s\n", nfs_path); |
| 652 | #endif |
| 653 | nfs_filename = basename (nfs_path); |
| 654 | nfs_path = dirname (nfs_path); |
| 655 | |
| 656 | NfsState = STATE_MOUNT_REQ; |
| 657 | NfsSend (); |
| 658 | } |
| 659 | break; |
| 660 | |
| 661 | case STATE_READ_REQ: |
| 662 | rlen = nfs_read_reply (pkt, len); |
| 663 | if (rlen > 0) { |
| 664 | nfs_offset += rlen; |
| 665 | NfsSend (); |
| 666 | } |
| 667 | else if ((rlen == -NFSERR_ISDIR)||(rlen == -NFSERR_INVAL)) { |
| 668 | /* symbolic link */ |
| 669 | NfsState = STATE_READLINK_REQ; |
| 670 | NfsSend (); |
| 671 | } else { |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 672 | NfsState = STATE_UMOUNT_REQ; |
| 673 | NfsSend (); |
| 674 | } |
| 675 | break; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | static void |
| 680 | NfsTimeout (void) |
| 681 | { |
| 682 | puts ("Timeout\n"); |
| 683 | NetState = NETLOOP_FAIL; |
| 684 | return; |
| 685 | } |
| 686 | |
| 687 | void |
| 688 | NfsStart (void) |
| 689 | { |
| 690 | #ifdef NFS_DEBUG |
| 691 | printf ("%s\n", __FUNCTION__); |
| 692 | #endif |
| 693 | |
| 694 | NfsServerIP = NetServerIP; |
| 695 | nfs_path = (char *)nfs_path_buff; |
| 696 | |
| 697 | if (nfs_path == NULL) { |
| 698 | NetState = NETLOOP_FAIL; |
| 699 | puts ("*** ERROR: Fail allocate memory\n"); |
| 700 | return; |
| 701 | } |
| 702 | |
| 703 | if (BootFile[0] == '\0') { |
| 704 | IPaddr_t OurIP = ntohl (NetOurIP); |
| 705 | |
| 706 | sprintf (default_filename, "/nfsroot/%02lX%02lX%02lX%02lX.img", |
| 707 | OurIP & 0xFF, |
| 708 | (OurIP >> 8) & 0xFF, |
| 709 | (OurIP >> 16) & 0xFF, |
| 710 | (OurIP >> 24) & 0xFF ); |
| 711 | strcpy (nfs_path, default_filename); |
| 712 | |
| 713 | printf ("*** Warning: no boot file name; using '%s'\n", |
| 714 | nfs_path); |
| 715 | } else { |
| 716 | char *p=BootFile; |
| 717 | |
| 718 | p = strchr (p, ':'); |
| 719 | |
| 720 | if (p != NULL) { |
| 721 | NfsServerIP = string_to_ip (BootFile); |
| 722 | ++p; |
| 723 | strcpy (nfs_path, p); |
| 724 | } else { |
| 725 | strcpy (nfs_path, BootFile); |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | nfs_filename = basename (nfs_path); |
| 730 | nfs_path = dirname (nfs_path); |
| 731 | |
| 732 | #if defined(CONFIG_NET_MULTI) |
| 733 | printf ("Using %s device\n", eth_get_name()); |
| 734 | #endif |
| 735 | |
| 736 | puts ("File transfer via NFS from server "); print_IPaddr (NfsServerIP); |
| 737 | puts ("; our IP address is "); print_IPaddr (NetOurIP); |
| 738 | |
| 739 | /* Check if we need to send across this subnet */ |
| 740 | if (NetOurGatewayIP && NetOurSubnetMask) { |
| 741 | IPaddr_t OurNet = NetOurIP & NetOurSubnetMask; |
| 742 | IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask; |
| 743 | |
| 744 | if (OurNet != ServerNet) { |
| 745 | puts ("; sending through gateway "); |
| 746 | print_IPaddr (NetOurGatewayIP) ; |
| 747 | } |
| 748 | } |
wdenk | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 749 | printf ("\nFilename '%s/%s'.", nfs_path, nfs_filename); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 750 | |
| 751 | if (NetBootFileSize) { |
| 752 | printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9); |
| 753 | print_size (NetBootFileSize<<9, ""); |
| 754 | } |
wdenk | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 755 | printf ("\nLoad address: 0x%lx\n" |
| 756 | "Loading: *\b", load_addr); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 757 | |
| 758 | NetSetTimeout (NFS_TIMEOUT * CFG_HZ, NfsTimeout); |
| 759 | NetSetHandler (NfsHandler); |
| 760 | |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 761 | NfsTimeoutCount = 0; |
| 762 | NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ; |
| 763 | |
| 764 | /*NfsOurPort = 4096 + (get_ticks() % 3072);*/ |
| 765 | /*FIX ME !!!*/ |
| 766 | NfsOurPort = 1000; |
| 767 | |
| 768 | /* zero out server ether in case the server ip has changed */ |
| 769 | memset (NetServerEther, 0, 6); |
| 770 | |
| 771 | NfsSend (); |
| 772 | } |
| 773 | |
| 774 | #endif /* CONFIG_COMMANDS & CFG_CMD_NFS */ |