2 * NFS support driver - based on etherboot and U-BOOT's tftp.c
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. */
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. */
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. */
25 /* NOTE 4: NFSv3 support added by Guillaume GARDET, 2016-June-20.
26 * NFSv2 is still used by default. But if server does not support NFSv2, then
27 * NFSv3 is used, if available on NFS server. */
29 /* NOTE 5: NFSv1 support added by Christian Gmeiner, Thomas Rienoessl,
30 * September 27, 2018. As of now, NFSv3 is the default choice. If the server
31 * does not support NFSv3, we fall back to versions 2 or 1. */
34 #include <display_options.h>
35 #ifdef CONFIG_SYS_DIRECT_FLASH_NFS
47 #define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
48 #define NFS_RETRY_COUNT 30
51 #define NFS_RPC_DROP 124
53 static int fs_mounted;
54 static unsigned long rpc_id;
55 static int nfs_offset = -1;
57 static const ulong nfs_timeout = CONFIG_NFS_TIMEOUT;
59 static char dirfh[NFS3_FHSIZE]; /* NFSv2 / NFSv3 file handle of directory */
60 static unsigned int dirfh3_length; /* (variable) length of dirfh when NFSv3 */
61 static char filefh[NFS3_FHSIZE]; /* NFSv2 / NFSv3 file handle */
62 static unsigned int filefh3_length; /* (variable) length of filefh when NFSv3 */
64 static enum net_loop_state nfs_download_state;
65 static struct in_addr nfs_server_ip;
66 static int nfs_server_mount_port;
67 static int nfs_server_port;
68 static int nfs_our_port;
69 static int nfs_timeout_count;
71 #define STATE_PRCLOOKUP_PROG_MOUNT_REQ 1
72 #define STATE_PRCLOOKUP_PROG_NFS_REQ 2
73 #define STATE_MOUNT_REQ 3
74 #define STATE_UMOUNT_REQ 4
75 #define STATE_LOOKUP_REQ 5
76 #define STATE_READ_REQ 6
77 #define STATE_READLINK_REQ 7
79 static char *nfs_filename;
80 static char *nfs_path;
81 static char nfs_path_buff[2048];
90 static enum nfs_version choosen_nfs_version = NFS_V3;
91 static inline int store_block(uchar *src, unsigned offset, unsigned len)
93 ulong newsize = offset + len;
94 #ifdef CONFIG_SYS_DIRECT_FLASH_NFS
97 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
98 /* start address in flash? */
99 if (image_load_addr + offset >= flash_info[i].start[0]) {
105 if (rc) { /* Flash is destination for this packet */
106 rc = flash_write((uchar *)src, (ulong)image_load_addr + offset,
113 #endif /* CONFIG_SYS_DIRECT_FLASH_NFS */
115 void *ptr = map_sysmem(image_load_addr + offset, len);
117 memcpy(ptr, src, len);
121 if (net_boot_file_size < (offset + len))
122 net_boot_file_size = newsize;
126 static char *basename(char *path)
130 fname = path + strlen(path) - 1;
131 while (fname >= path) {
141 static char *dirname(char *path)
145 fname = basename(path);
151 /**************************************************************************
152 RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
153 **************************************************************************/
154 static uint32_t *rpc_add_credentials(uint32_t *p)
156 /* Here's the executive summary on authentication requirements of the
157 * various NFS server implementations: Linux accepts both AUTH_NONE
158 * and AUTH_UNIX authentication (also accepts an empty hostname field
159 * in the AUTH_UNIX scheme). *BSD refuses AUTH_NONE, but accepts
160 * AUTH_UNIX (also accepts an empty hostname field in the AUTH_UNIX
161 * scheme). To be safe, use AUTH_UNIX and pass the hostname if we have
162 * it (if the BOOTP/DHCP reply didn't give one, just use an empty
165 /* Provide an AUTH_UNIX credential. */
166 *p++ = htonl(1); /* AUTH_UNIX */
167 *p++ = htonl(20); /* auth length */
168 *p++ = 0; /* stamp */
169 *p++ = 0; /* hostname string */
172 *p++ = 0; /* auxiliary gid list */
174 /* Provide an AUTH_NONE verifier. */
175 *p++ = 0; /* AUTH_NONE */
176 *p++ = 0; /* auth length */
181 /**************************************************************************
182 RPC_LOOKUP - Lookup RPC Port numbers
183 **************************************************************************/
184 static void rpc_req(int rpc_prog, int rpc_proc, uint32_t *data, int datalen)
186 struct rpc_t rpc_pkt;
193 rpc_pkt.u.call.id = htonl(id);
194 rpc_pkt.u.call.type = htonl(MSG_CALL);
195 rpc_pkt.u.call.rpcvers = htonl(2); /* use RPC version 2 */
196 rpc_pkt.u.call.prog = htonl(rpc_prog);
199 switch (choosen_nfs_version) {
202 rpc_pkt.u.call.vers = htonl(2);
206 rpc_pkt.u.call.vers = htonl(3);
215 switch (choosen_nfs_version) {
217 rpc_pkt.u.call.vers = htonl(1);
221 rpc_pkt.u.call.vers = htonl(2);
225 rpc_pkt.u.call.vers = htonl(3);
235 rpc_pkt.u.call.vers = htonl(2); /* portmapper is version 2 */
237 rpc_pkt.u.call.proc = htonl(rpc_proc);
238 p = rpc_pkt.u.call.data;
241 memcpy(p, data, datalen * sizeof(uint32_t));
243 pktlen = (char *)p + datalen * sizeof(uint32_t) - (char *)&rpc_pkt;
245 memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE,
246 &rpc_pkt.u.data[0], pktlen);
248 if (rpc_prog == PROG_PORTMAP)
250 else if (rpc_prog == PROG_MOUNT)
251 sport = nfs_server_mount_port;
253 sport = nfs_server_port;
255 net_send_udp_packet(net_server_ethaddr, nfs_server_ip, sport,
256 nfs_our_port, pktlen);
259 /**************************************************************************
260 RPC_LOOKUP - Lookup RPC Port numbers
261 **************************************************************************/
262 static void rpc_lookup_req(int prog, int ver)
266 data[0] = 0; data[1] = 0; /* auth credential */
267 data[2] = 0; data[3] = 0; /* auth verifier */
268 data[4] = htonl(prog);
269 data[5] = htonl(ver);
270 data[6] = htonl(17); /* IP_UDP */
272 rpc_req(PROG_PORTMAP, PORTMAP_GETPORT, data, 8);
275 /**************************************************************************
276 NFS_MOUNT - Mount an NFS Filesystem
277 **************************************************************************/
278 static void nfs_mount_req(char *path)
285 pathlen = strlen(path);
288 p = rpc_add_credentials(p);
290 *p++ = htonl(pathlen);
292 *(p + pathlen / 4) = 0;
293 memcpy(p, path, pathlen);
294 p += (pathlen + 3) / 4;
296 len = (uint32_t *)p - (uint32_t *)&(data[0]);
298 rpc_req(PROG_MOUNT, MOUNT_ADDENTRY, data, len);
301 /**************************************************************************
302 NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
303 **************************************************************************/
304 static void nfs_umountall_req(void)
310 if ((nfs_server_mount_port == -1) || (!fs_mounted))
311 /* Nothing mounted, nothing to umount */
315 p = rpc_add_credentials(p);
317 len = (uint32_t *)p - (uint32_t *)&(data[0]);
319 rpc_req(PROG_MOUNT, MOUNT_UMOUNTALL, data, len);
322 /***************************************************************************
323 * NFS_READLINK (AH 2003-07-14)
324 * This procedure is called when read of the first block fails -
325 * this probably happens when it's a directory or a symlink
326 * In case of successful readlink(), the dirname is manipulated,
327 * so that inside the nfs() function a recursion can be done.
328 **************************************************************************/
329 static void nfs_readlink_req(void)
336 p = rpc_add_credentials(p);
338 if (choosen_nfs_version != NFS_V3) {
339 memcpy(p, filefh, NFS_FHSIZE);
340 p += (NFS_FHSIZE / 4);
341 } else { /* NFS_V3 */
342 *p++ = htonl(filefh3_length);
343 memcpy(p, filefh, filefh3_length);
344 p += (filefh3_length / 4);
347 len = (uint32_t *)p - (uint32_t *)&(data[0]);
349 rpc_req(PROG_NFS, NFS_READLINK, data, len);
352 /**************************************************************************
353 NFS_LOOKUP - Lookup Pathname
354 **************************************************************************/
355 static void nfs_lookup_req(char *fname)
362 fnamelen = strlen(fname);
365 p = rpc_add_credentials(p);
367 if (choosen_nfs_version != NFS_V3) {
368 memcpy(p, dirfh, NFS_FHSIZE);
369 p += (NFS_FHSIZE / 4);
370 *p++ = htonl(fnamelen);
372 *(p + fnamelen / 4) = 0;
373 memcpy(p, fname, fnamelen);
374 p += (fnamelen + 3) / 4;
376 len = (uint32_t *)p - (uint32_t *)&(data[0]);
378 rpc_req(PROG_NFS, NFS_LOOKUP, data, len);
379 } else { /* NFS_V3 */
380 *p++ = htonl(dirfh3_length); /* Dir handle length */
381 memcpy(p, dirfh, dirfh3_length);
382 p += (dirfh3_length / 4);
383 *p++ = htonl(fnamelen);
385 *(p + fnamelen / 4) = 0;
386 memcpy(p, fname, fnamelen);
387 p += (fnamelen + 3) / 4;
389 len = (uint32_t *)p - (uint32_t *)&(data[0]);
391 rpc_req(PROG_NFS, NFS3PROC_LOOKUP, data, len);
395 /**************************************************************************
396 NFS_READ - Read File on NFS Server
397 **************************************************************************/
398 static void nfs_read_req(int offset, int readlen)
405 p = rpc_add_credentials(p);
407 if (choosen_nfs_version != NFS_V3) {
408 memcpy(p, filefh, NFS_FHSIZE);
409 p += (NFS_FHSIZE / 4);
410 *p++ = htonl(offset);
411 *p++ = htonl(readlen);
413 } else { /* NFS_V3 */
414 *p++ = htonl(filefh3_length);
415 memcpy(p, filefh, filefh3_length);
416 p += (filefh3_length / 4);
417 *p++ = htonl(0); /* offset is 64-bit long, so fill with 0 */
418 *p++ = htonl(offset);
419 *p++ = htonl(readlen);
423 len = (uint32_t *)p - (uint32_t *)&(data[0]);
425 rpc_req(PROG_NFS, NFS_READ, data, len);
428 /**************************************************************************
429 RPC request dispatcher
430 **************************************************************************/
431 static void nfs_send(void)
433 debug("%s\n", __func__);
436 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
437 if (choosen_nfs_version != NFS_V3)
438 rpc_lookup_req(PROG_MOUNT, 1);
440 rpc_lookup_req(PROG_MOUNT, 3);
442 case STATE_PRCLOOKUP_PROG_NFS_REQ:
443 if (choosen_nfs_version != NFS_V3)
444 rpc_lookup_req(PROG_NFS, 2);
446 rpc_lookup_req(PROG_NFS, 3);
448 case STATE_MOUNT_REQ:
449 nfs_mount_req(nfs_path);
451 case STATE_UMOUNT_REQ:
454 case STATE_LOOKUP_REQ:
455 nfs_lookup_req(nfs_filename);
458 nfs_read_req(nfs_offset, nfs_len);
460 case STATE_READLINK_REQ:
466 /**************************************************************************
467 Handlers for the reply from server
468 **************************************************************************/
470 static int rpc_handle_error(struct rpc_t *rpc_pkt)
472 if (rpc_pkt->u.reply.rstatus ||
473 rpc_pkt->u.reply.verifier ||
474 rpc_pkt->u.reply.astatus ||
475 rpc_pkt->u.reply.data[0]) {
476 switch (ntohl(rpc_pkt->u.reply.astatus)) {
477 case NFS_RPC_SUCCESS: /* Not an error */
479 case NFS_RPC_PROG_MISMATCH: {
480 /* Remote can't support NFS version */
481 const int min = ntohl(rpc_pkt->u.reply.data[0]);
482 const int max = ntohl(rpc_pkt->u.reply.data[1]);
484 if (max < NFS_V1 || max > NFS_V3 || min > NFS_V3) {
485 puts("*** ERROR: NFS version not supported");
486 debug(": Requested: V%d, accepted: min V%d - max V%d\n",
488 ntohl(rpc_pkt->u.reply.data[0]),
489 ntohl(rpc_pkt->u.reply.data[1]));
491 choosen_nfs_version = NFS_UNKOWN;
495 debug("*** Warning: NFS version not supported: Requested: V%d, accepted: min V%d - max V%d\n",
497 ntohl(rpc_pkt->u.reply.data[0]),
498 ntohl(rpc_pkt->u.reply.data[1]));
499 debug("Will retry with NFSv%d\n", min);
500 choosen_nfs_version = min;
501 return -NFS_RPC_PROG_MISMATCH;
503 case NFS_RPC_PROG_UNAVAIL:
504 case NFS_RPC_PROC_UNAVAIL:
505 case NFS_RPC_GARBAGE_ARGS:
506 case NFS_RPC_SYSTEM_ERR:
507 default: /* Unknown error on 'accept state' flag */
508 debug("*** ERROR: accept state error (%d)\n",
509 ntohl(rpc_pkt->u.reply.astatus));
518 static int rpc_lookup_reply(int prog, uchar *pkt, unsigned len)
520 struct rpc_t rpc_pkt;
522 memcpy(&rpc_pkt.u.data[0], pkt, len);
524 debug("%s\n", __func__);
526 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
528 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
529 return -NFS_RPC_DROP;
531 if (rpc_pkt.u.reply.rstatus ||
532 rpc_pkt.u.reply.verifier ||
533 rpc_pkt.u.reply.astatus)
538 nfs_server_mount_port = ntohl(rpc_pkt.u.reply.data[0]);
541 nfs_server_port = ntohl(rpc_pkt.u.reply.data[0]);
548 static int nfs_mount_reply(uchar *pkt, unsigned len)
550 struct rpc_t rpc_pkt;
553 debug("%s\n", __func__);
555 memcpy(&rpc_pkt.u.data[0], pkt, len);
557 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
559 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
560 return -NFS_RPC_DROP;
562 ret = rpc_handle_error(&rpc_pkt);
567 /* NFSv2 and NFSv3 use same structure */
568 if (choosen_nfs_version != NFS_V3) {
569 memcpy(dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
571 dirfh3_length = ntohl(rpc_pkt.u.reply.data[1]);
572 if (dirfh3_length > NFS3_FHSIZE)
573 dirfh3_length = NFS3_FHSIZE;
574 memcpy(dirfh, rpc_pkt.u.reply.data + 2, dirfh3_length);
580 static int nfs_umountall_reply(uchar *pkt, unsigned len)
582 struct rpc_t rpc_pkt;
584 debug("%s\n", __func__);
586 memcpy(&rpc_pkt.u.data[0], pkt, len);
588 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
590 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
591 return -NFS_RPC_DROP;
593 if (rpc_pkt.u.reply.rstatus ||
594 rpc_pkt.u.reply.verifier ||
595 rpc_pkt.u.reply.astatus)
599 memset(dirfh, 0, sizeof(dirfh));
604 static int nfs_lookup_reply(uchar *pkt, unsigned len)
606 struct rpc_t rpc_pkt;
609 debug("%s\n", __func__);
611 memcpy(&rpc_pkt.u.data[0], pkt, len);
613 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
615 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
616 return -NFS_RPC_DROP;
618 ret = rpc_handle_error(&rpc_pkt);
622 if (choosen_nfs_version != NFS_V3) {
623 if (((uchar *)&(rpc_pkt.u.reply.data[0]) - (uchar *)(&rpc_pkt) + NFS_FHSIZE) > len)
624 return -NFS_RPC_DROP;
625 memcpy(filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
626 } else { /* NFS_V3 */
627 filefh3_length = ntohl(rpc_pkt.u.reply.data[1]);
628 if (filefh3_length > NFS3_FHSIZE)
629 filefh3_length = NFS3_FHSIZE;
630 memcpy(filefh, rpc_pkt.u.reply.data + 2, filefh3_length);
636 static int nfs3_get_attributes_offset(uint32_t *data)
639 /* 'attributes_follow' flag is TRUE,
640 * so we have attributes on 21 dwords */
641 /* Skip unused values :
644 nlink; 32 bits value,
651 fileid; 64 bits value,
652 atime; 64 bits value,
653 mtime; 64 bits value,
654 ctime; 64 bits value,
658 /* 'attributes_follow' flag is FALSE,
659 * so we don't have any attributes */
664 static int nfs_readlink_reply(uchar *pkt, unsigned len)
666 struct rpc_t rpc_pkt;
668 int nfsv3_data_offset = 0;
670 debug("%s\n", __func__);
672 memcpy((unsigned char *)&rpc_pkt, pkt, len);
674 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
676 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
677 return -NFS_RPC_DROP;
679 if (rpc_pkt.u.reply.rstatus ||
680 rpc_pkt.u.reply.verifier ||
681 rpc_pkt.u.reply.astatus ||
682 rpc_pkt.u.reply.data[0])
685 if (choosen_nfs_version == NFS_V3) {
687 nfs3_get_attributes_offset(rpc_pkt.u.reply.data);
690 /* new path length */
691 rlen = ntohl(rpc_pkt.u.reply.data[1 + nfsv3_data_offset]);
693 if (((uchar *)&(rpc_pkt.u.reply.data[0]) - (uchar *)(&rpc_pkt) + rlen) > len)
694 return -NFS_RPC_DROP;
696 if (*((char *)&(rpc_pkt.u.reply.data[2 + nfsv3_data_offset])) != '/') {
699 strcat(nfs_path, "/");
700 pathlen = strlen(nfs_path);
701 memcpy(nfs_path + pathlen,
702 (uchar *)&(rpc_pkt.u.reply.data[2 + nfsv3_data_offset]),
704 nfs_path[pathlen + rlen] = 0;
707 (uchar *)&(rpc_pkt.u.reply.data[2 + nfsv3_data_offset]),
714 static int nfs_read_reply(uchar *pkt, unsigned len)
716 struct rpc_t rpc_pkt;
720 debug("%s\n", __func__);
722 memcpy(&rpc_pkt.u.data[0], pkt, sizeof(rpc_pkt.u.reply));
724 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
726 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
727 return -NFS_RPC_DROP;
729 if (rpc_pkt.u.reply.rstatus ||
730 rpc_pkt.u.reply.verifier ||
731 rpc_pkt.u.reply.astatus ||
732 rpc_pkt.u.reply.data[0]) {
733 if (rpc_pkt.u.reply.rstatus)
735 if (rpc_pkt.u.reply.astatus)
737 return -ntohl(rpc_pkt.u.reply.data[0]);
740 if ((nfs_offset != 0) && !((nfs_offset) %
741 (NFS_READ_SIZE / 2 * 10 * HASHES_PER_LINE)))
743 if (!(nfs_offset % ((NFS_READ_SIZE / 2) * 10)))
746 if (choosen_nfs_version != NFS_V3) {
747 rlen = ntohl(rpc_pkt.u.reply.data[18]);
748 data_ptr = (uchar *)&(rpc_pkt.u.reply.data[19]);
749 } else { /* NFS_V3 */
750 int nfsv3_data_offset =
751 nfs3_get_attributes_offset(rpc_pkt.u.reply.data);
754 rlen = ntohl(rpc_pkt.u.reply.data[1 + nfsv3_data_offset]);
755 /* Skip unused values :
757 data_size: 32 bits value,
760 &(rpc_pkt.u.reply.data[4 + nfsv3_data_offset]);
763 if (((uchar *)&(rpc_pkt.u.reply.data[0]) - (uchar *)(&rpc_pkt) + rlen) > len)
766 if (store_block(data_ptr, nfs_offset, rlen))
772 /**************************************************************************
774 **************************************************************************/
775 static void nfs_timeout_handler(void)
777 if (++nfs_timeout_count > NFS_RETRY_COUNT) {
778 puts("\nRetry count exceeded; starting again\n");
782 net_set_timeout_handler(nfs_timeout +
783 nfs_timeout * nfs_timeout_count,
784 nfs_timeout_handler);
789 static void nfs_handler(uchar *pkt, unsigned dest, struct in_addr sip,
790 unsigned src, unsigned len)
795 debug("%s\n", __func__);
797 if (len > sizeof(struct rpc_t))
800 if (dest != nfs_our_port)
804 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
805 if (rpc_lookup_reply(PROG_MOUNT, pkt, len) == -NFS_RPC_DROP)
807 nfs_state = STATE_PRCLOOKUP_PROG_NFS_REQ;
811 case STATE_PRCLOOKUP_PROG_NFS_REQ:
812 if (rpc_lookup_reply(PROG_NFS, pkt, len) == -NFS_RPC_DROP)
814 nfs_state = STATE_MOUNT_REQ;
818 case STATE_MOUNT_REQ:
819 reply = nfs_mount_reply(pkt, len);
820 if (reply == -NFS_RPC_DROP) {
822 } else if (reply == -NFS_RPC_ERR) {
823 puts("*** ERROR: Cannot mount\n");
824 /* just to be sure... */
825 nfs_state = STATE_UMOUNT_REQ;
827 } else if (reply == -NFS_RPC_PROG_MISMATCH &&
828 choosen_nfs_version != NFS_UNKOWN) {
829 nfs_state = STATE_MOUNT_REQ;
832 nfs_state = STATE_LOOKUP_REQ;
837 case STATE_UMOUNT_REQ:
838 reply = nfs_umountall_reply(pkt, len);
839 if (reply == -NFS_RPC_DROP) {
841 } else if (reply == -NFS_RPC_ERR) {
842 debug("*** ERROR: Cannot umount\n");
843 net_set_state(NETLOOP_FAIL);
846 net_set_state(nfs_download_state);
850 case STATE_LOOKUP_REQ:
851 reply = nfs_lookup_reply(pkt, len);
852 if (reply == -NFS_RPC_DROP) {
854 } else if (reply == -NFS_RPC_ERR) {
855 puts("*** ERROR: File lookup fail\n");
856 nfs_state = STATE_UMOUNT_REQ;
858 } else if (reply == -NFS_RPC_PROG_MISMATCH &&
859 choosen_nfs_version != NFS_UNKOWN) {
861 nfs_state = STATE_UMOUNT_REQ;
863 /* And retry with another supported version */
864 nfs_state = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
867 nfs_state = STATE_READ_REQ;
869 nfs_len = NFS_READ_SIZE;
874 case STATE_READLINK_REQ:
875 reply = nfs_readlink_reply(pkt, len);
876 if (reply == -NFS_RPC_DROP) {
878 } else if (reply == -NFS_RPC_ERR) {
879 puts("*** ERROR: Symlink fail\n");
880 nfs_state = STATE_UMOUNT_REQ;
883 debug("Symlink --> %s\n", nfs_path);
884 nfs_filename = basename(nfs_path);
885 nfs_path = dirname(nfs_path);
887 nfs_state = STATE_MOUNT_REQ;
893 rlen = nfs_read_reply(pkt, len);
894 if (rlen == -NFS_RPC_DROP)
896 net_set_timeout_handler(nfs_timeout, nfs_timeout_handler);
900 } else if ((rlen == -NFSERR_ISDIR) || (rlen == -NFSERR_INVAL)) {
902 nfs_state = STATE_READLINK_REQ;
906 nfs_download_state = NETLOOP_SUCCESS;
908 debug("NFS READ error (%d)\n", rlen);
909 nfs_state = STATE_UMOUNT_REQ;
918 debug("%s\n", __func__);
919 nfs_download_state = NETLOOP_FAIL;
921 nfs_server_ip = net_server_ip;
922 nfs_path = (char *)nfs_path_buff;
924 if (nfs_path == NULL) {
925 net_set_state(NETLOOP_FAIL);
926 printf("*** ERROR: Fail allocate memory\n");
930 if (!net_parse_bootfile(&nfs_server_ip, nfs_path,
931 sizeof(nfs_path_buff))) {
932 sprintf(nfs_path, "/nfsroot/%02X%02X%02X%02X.img",
933 net_ip.s_addr & 0xFF,
934 (net_ip.s_addr >> 8) & 0xFF,
935 (net_ip.s_addr >> 16) & 0xFF,
936 (net_ip.s_addr >> 24) & 0xFF);
938 printf("*** Warning: no boot file name; using '%s'\n",
942 nfs_filename = basename(nfs_path);
943 nfs_path = dirname(nfs_path);
945 printf("Using %s device\n", eth_get_name());
947 printf("File transfer via NFS from server %pI4; our IP address is %pI4",
948 &nfs_server_ip, &net_ip);
950 /* Check if we need to send across this subnet */
951 if (net_gateway.s_addr && net_netmask.s_addr) {
952 struct in_addr our_net;
953 struct in_addr server_net;
955 our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
956 server_net.s_addr = nfs_server_ip.s_addr & net_netmask.s_addr;
957 if (our_net.s_addr != server_net.s_addr)
958 printf("; sending through gateway %pI4",
961 printf("\nFilename '%s/%s'.", nfs_path, nfs_filename);
963 if (net_boot_file_expected_size_in_blocks) {
964 printf(" Size is 0x%x Bytes = ",
965 net_boot_file_expected_size_in_blocks << 9);
966 print_size(net_boot_file_expected_size_in_blocks << 9, "");
968 printf("\nLoad address: 0x%lx\nLoading: *\b", image_load_addr);
970 net_set_timeout_handler(nfs_timeout, nfs_timeout_handler);
971 net_set_udp_handler(nfs_handler);
973 nfs_timeout_count = 0;
974 nfs_state = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
976 /*nfs_our_port = 4096 + (get_ticks() % 3072);*/
980 /* zero out server ether in case the server ip has changed */
981 memset(net_server_ethaddr, 0, 6);