2 * NFS support driver - based on etherboot and U-BOOT's tftp.c
4 * Masami Komiya <mkomiya@sonare.it> 2004
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. */
33 #define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
34 #define NFS_RETRY_COUNT 30
35 #ifndef CONFIG_NFS_TIMEOUT
36 # define NFS_TIMEOUT 2000UL
38 # define NFS_TIMEOUT CONFIG_NFS_TIMEOUT
42 #define NFS_RPC_DROP 124
44 static int fs_mounted
;
45 static unsigned long rpc_id
;
46 static int nfs_offset
= -1;
48 static ulong nfs_timeout
= NFS_TIMEOUT
;
50 static char dirfh
[NFS_FHSIZE
]; /* file handle of directory */
51 static char filefh
[NFS_FHSIZE
]; /* file handle of kernel image */
53 static enum net_loop_state nfs_download_state
;
54 static struct in_addr nfs_server_ip
;
55 static int NfsSrvMountPort
;
56 static int NfsSrvNfsPort
;
57 static int NfsOurPort
;
58 static int NfsTimeoutCount
;
60 #define STATE_PRCLOOKUP_PROG_MOUNT_REQ 1
61 #define STATE_PRCLOOKUP_PROG_NFS_REQ 2
62 #define STATE_MOUNT_REQ 3
63 #define STATE_UMOUNT_REQ 4
64 #define STATE_LOOKUP_REQ 5
65 #define STATE_READ_REQ 6
66 #define STATE_READLINK_REQ 7
68 static char default_filename
[64];
69 static char *nfs_filename
;
70 static char *nfs_path
;
71 static char nfs_path_buff
[2048];
74 store_block(uchar
*src
, unsigned offset
, unsigned len
)
76 ulong newsize
= offset
+ len
;
77 #ifdef CONFIG_SYS_DIRECT_FLASH_NFS
80 for (i
= 0; i
< CONFIG_SYS_MAX_FLASH_BANKS
; i
++) {
81 /* start address in flash? */
82 if (load_addr
+ offset
>= flash_info
[i
].start
[0]) {
88 if (rc
) { /* Flash is destination for this packet */
89 rc
= flash_write((uchar
*)src
, (ulong
)(load_addr
+offset
), len
);
95 #endif /* CONFIG_SYS_DIRECT_FLASH_NFS */
97 void *ptr
= map_sysmem(load_addr
+ offset
, len
);
99 memcpy(ptr
, src
, len
);
103 if (NetBootFileXferSize
< (offset
+len
))
104 NetBootFileXferSize
= newsize
;
113 fname
= path
+ strlen(path
) - 1;
114 while (fname
>= path
) {
129 fname
= basename(path
);
135 /**************************************************************************
136 RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
137 **************************************************************************/
138 static long *rpc_add_credentials(long *p
)
144 strcpy(hostname
, "");
145 hostnamelen
= strlen(hostname
);
147 /* Here's the executive summary on authentication requirements of the
148 * various NFS server implementations: Linux accepts both AUTH_NONE
149 * and AUTH_UNIX authentication (also accepts an empty hostname field
150 * in the AUTH_UNIX scheme). *BSD refuses AUTH_NONE, but accepts
151 * AUTH_UNIX (also accepts an empty hostname field in the AUTH_UNIX
152 * scheme). To be safe, use AUTH_UNIX and pass the hostname if we have
153 * it (if the BOOTP/DHCP reply didn't give one, just use an empty
156 hl
= (hostnamelen
+ 3) & ~3;
158 /* Provide an AUTH_UNIX credential. */
159 *p
++ = htonl(1); /* AUTH_UNIX */
160 *p
++ = htonl(hl
+20); /* auth length */
161 *p
++ = htonl(0); /* stamp */
162 *p
++ = htonl(hostnamelen
); /* hostname string */
164 *(p
+ hostnamelen
/ 4) = 0; /* add zero padding */
165 memcpy(p
, hostname
, hostnamelen
);
169 *p
++ = 0; /* auxiliary gid list */
171 /* Provide an AUTH_NONE verifier. */
172 *p
++ = 0; /* AUTH_NONE */
173 *p
++ = 0; /* auth length */
178 /**************************************************************************
179 RPC_LOOKUP - Lookup RPC Port numbers
180 **************************************************************************/
182 rpc_req(int rpc_prog
, int rpc_proc
, uint32_t *data
, int datalen
)
191 pkt
.u
.call
.id
= htonl(id
);
192 pkt
.u
.call
.type
= htonl(MSG_CALL
);
193 pkt
.u
.call
.rpcvers
= htonl(2); /* use RPC version 2 */
194 pkt
.u
.call
.prog
= htonl(rpc_prog
);
195 pkt
.u
.call
.vers
= htonl(2); /* portmapper is version 2 */
196 pkt
.u
.call
.proc
= htonl(rpc_proc
);
197 p
= (uint32_t *)&(pkt
.u
.call
.data
);
200 memcpy((char *)p
, (char *)data
, datalen
*sizeof(uint32_t));
202 pktlen
= (char *)p
+ datalen
*sizeof(uint32_t) - (char *)&pkt
;
204 memcpy((char *)NetTxPacket
+ NetEthHdrSize() + IP_UDP_HDR_SIZE
,
205 (char *)&pkt
, pktlen
);
207 if (rpc_prog
== PROG_PORTMAP
)
209 else if (rpc_prog
== PROG_MOUNT
)
210 sport
= NfsSrvMountPort
;
212 sport
= NfsSrvNfsPort
;
214 NetSendUDPPacket(NetServerEther
, nfs_server_ip
, sport
, NfsOurPort
,
218 /**************************************************************************
219 RPC_LOOKUP - Lookup RPC Port numbers
220 **************************************************************************/
222 rpc_lookup_req(int prog
, int ver
)
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 */
233 rpc_req(PROG_PORTMAP
, PORTMAP_GETPORT
, data
, 8);
236 /**************************************************************************
237 NFS_MOUNT - Mount an NFS Filesystem
238 **************************************************************************/
240 nfs_mount_req(char *path
)
247 pathlen
= strlen(path
);
250 p
= (uint32_t *)rpc_add_credentials((long *)p
);
252 *p
++ = htonl(pathlen
);
254 *(p
+ pathlen
/ 4) = 0;
255 memcpy(p
, path
, pathlen
);
256 p
+= (pathlen
+ 3) / 4;
258 len
= (uint32_t *)p
- (uint32_t *)&(data
[0]);
260 rpc_req(PROG_MOUNT
, MOUNT_ADDENTRY
, data
, len
);
263 /**************************************************************************
264 NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
265 **************************************************************************/
267 nfs_umountall_req(void)
273 if ((NfsSrvMountPort
== -1) || (!fs_mounted
))
274 /* Nothing mounted, nothing to umount */
278 p
= (uint32_t *)rpc_add_credentials((long *)p
);
280 len
= (uint32_t *)p
- (uint32_t *)&(data
[0]);
282 rpc_req(PROG_MOUNT
, MOUNT_UMOUNTALL
, data
, len
);
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 **************************************************************************/
293 nfs_readlink_req(void)
300 p
= (uint32_t *)rpc_add_credentials((long *)p
);
302 memcpy(p
, filefh
, NFS_FHSIZE
);
303 p
+= (NFS_FHSIZE
/ 4);
305 len
= (uint32_t *)p
- (uint32_t *)&(data
[0]);
307 rpc_req(PROG_NFS
, NFS_READLINK
, data
, len
);
310 /**************************************************************************
311 NFS_LOOKUP - Lookup Pathname
312 **************************************************************************/
314 nfs_lookup_req(char *fname
)
321 fnamelen
= strlen(fname
);
324 p
= (uint32_t *)rpc_add_credentials((long *)p
);
326 memcpy(p
, dirfh
, NFS_FHSIZE
);
327 p
+= (NFS_FHSIZE
/ 4);
328 *p
++ = htonl(fnamelen
);
330 *(p
+ fnamelen
/ 4) = 0;
331 memcpy(p
, fname
, fnamelen
);
332 p
+= (fnamelen
+ 3) / 4;
334 len
= (uint32_t *)p
- (uint32_t *)&(data
[0]);
336 rpc_req(PROG_NFS
, NFS_LOOKUP
, data
, len
);
339 /**************************************************************************
340 NFS_READ - Read File on NFS Server
341 **************************************************************************/
343 nfs_read_req(int offset
, int readlen
)
350 p
= (uint32_t *)rpc_add_credentials((long *)p
);
352 memcpy(p
, filefh
, NFS_FHSIZE
);
353 p
+= (NFS_FHSIZE
/ 4);
354 *p
++ = htonl(offset
);
355 *p
++ = htonl(readlen
);
358 len
= (uint32_t *)p
- (uint32_t *)&(data
[0]);
360 rpc_req(PROG_NFS
, NFS_READ
, data
, len
);
363 /**************************************************************************
364 RPC request dispatcher
365 **************************************************************************/
370 debug("%s\n", __func__
);
373 case STATE_PRCLOOKUP_PROG_MOUNT_REQ
:
374 rpc_lookup_req(PROG_MOUNT
, 1);
376 case STATE_PRCLOOKUP_PROG_NFS_REQ
:
377 rpc_lookup_req(PROG_NFS
, 2);
379 case STATE_MOUNT_REQ
:
380 nfs_mount_req(nfs_path
);
382 case STATE_UMOUNT_REQ
:
385 case STATE_LOOKUP_REQ
:
386 nfs_lookup_req(nfs_filename
);
389 nfs_read_req(nfs_offset
, nfs_len
);
391 case STATE_READLINK_REQ
:
397 /**************************************************************************
398 Handlers for the reply from server
399 **************************************************************************/
402 rpc_lookup_reply(int prog
, uchar
*pkt
, unsigned len
)
404 struct rpc_t rpc_pkt
;
406 memcpy((unsigned char *)&rpc_pkt
, pkt
, len
);
408 debug("%s\n", __func__
);
410 if (ntohl(rpc_pkt
.u
.reply
.id
) > rpc_id
)
412 else if (ntohl(rpc_pkt
.u
.reply
.id
) < rpc_id
)
413 return -NFS_RPC_DROP
;
415 if (rpc_pkt
.u
.reply
.rstatus
||
416 rpc_pkt
.u
.reply
.verifier
||
417 rpc_pkt
.u
.reply
.astatus
)
422 NfsSrvMountPort
= ntohl(rpc_pkt
.u
.reply
.data
[0]);
425 NfsSrvNfsPort
= ntohl(rpc_pkt
.u
.reply
.data
[0]);
433 nfs_mount_reply(uchar
*pkt
, unsigned len
)
435 struct rpc_t rpc_pkt
;
437 debug("%s\n", __func__
);
439 memcpy((unsigned char *)&rpc_pkt
, pkt
, len
);
441 if (ntohl(rpc_pkt
.u
.reply
.id
) > rpc_id
)
443 else if (ntohl(rpc_pkt
.u
.reply
.id
) < rpc_id
)
444 return -NFS_RPC_DROP
;
446 if (rpc_pkt
.u
.reply
.rstatus
||
447 rpc_pkt
.u
.reply
.verifier
||
448 rpc_pkt
.u
.reply
.astatus
||
449 rpc_pkt
.u
.reply
.data
[0])
453 memcpy(dirfh
, rpc_pkt
.u
.reply
.data
+ 1, NFS_FHSIZE
);
459 nfs_umountall_reply(uchar
*pkt
, unsigned len
)
461 struct rpc_t rpc_pkt
;
463 debug("%s\n", __func__
);
465 memcpy((unsigned char *)&rpc_pkt
, pkt
, len
);
467 if (ntohl(rpc_pkt
.u
.reply
.id
) > rpc_id
)
469 else if (ntohl(rpc_pkt
.u
.reply
.id
) < rpc_id
)
470 return -NFS_RPC_DROP
;
472 if (rpc_pkt
.u
.reply
.rstatus
||
473 rpc_pkt
.u
.reply
.verifier
||
474 rpc_pkt
.u
.reply
.astatus
)
478 memset(dirfh
, 0, sizeof(dirfh
));
484 nfs_lookup_reply(uchar
*pkt
, unsigned len
)
486 struct rpc_t rpc_pkt
;
488 debug("%s\n", __func__
);
490 memcpy((unsigned char *)&rpc_pkt
, pkt
, len
);
492 if (ntohl(rpc_pkt
.u
.reply
.id
) > rpc_id
)
494 else if (ntohl(rpc_pkt
.u
.reply
.id
) < rpc_id
)
495 return -NFS_RPC_DROP
;
497 if (rpc_pkt
.u
.reply
.rstatus
||
498 rpc_pkt
.u
.reply
.verifier
||
499 rpc_pkt
.u
.reply
.astatus
||
500 rpc_pkt
.u
.reply
.data
[0])
503 memcpy(filefh
, rpc_pkt
.u
.reply
.data
+ 1, NFS_FHSIZE
);
509 nfs_readlink_reply(uchar
*pkt
, unsigned len
)
511 struct rpc_t rpc_pkt
;
514 debug("%s\n", __func__
);
516 memcpy((unsigned char *)&rpc_pkt
, pkt
, len
);
518 if (ntohl(rpc_pkt
.u
.reply
.id
) > rpc_id
)
520 else if (ntohl(rpc_pkt
.u
.reply
.id
) < rpc_id
)
521 return -NFS_RPC_DROP
;
523 if (rpc_pkt
.u
.reply
.rstatus
||
524 rpc_pkt
.u
.reply
.verifier
||
525 rpc_pkt
.u
.reply
.astatus
||
526 rpc_pkt
.u
.reply
.data
[0])
529 rlen
= ntohl(rpc_pkt
.u
.reply
.data
[1]); /* new path length */
531 if (*((char *)&(rpc_pkt
.u
.reply
.data
[2])) != '/') {
533 strcat(nfs_path
, "/");
534 pathlen
= strlen(nfs_path
);
535 memcpy(nfs_path
+ pathlen
, (uchar
*)&(rpc_pkt
.u
.reply
.data
[2]),
537 nfs_path
[pathlen
+ rlen
] = 0;
539 memcpy(nfs_path
, (uchar
*)&(rpc_pkt
.u
.reply
.data
[2]), rlen
);
546 nfs_read_reply(uchar
*pkt
, unsigned len
)
548 struct rpc_t rpc_pkt
;
551 debug("%s\n", __func__
);
553 memcpy((uchar
*)&rpc_pkt
, pkt
, sizeof(rpc_pkt
.u
.reply
));
555 if (ntohl(rpc_pkt
.u
.reply
.id
) > rpc_id
)
557 else if (ntohl(rpc_pkt
.u
.reply
.id
) < rpc_id
)
558 return -NFS_RPC_DROP
;
560 if (rpc_pkt
.u
.reply
.rstatus
||
561 rpc_pkt
.u
.reply
.verifier
||
562 rpc_pkt
.u
.reply
.astatus
||
563 rpc_pkt
.u
.reply
.data
[0]) {
564 if (rpc_pkt
.u
.reply
.rstatus
)
566 if (rpc_pkt
.u
.reply
.astatus
)
568 return -ntohl(rpc_pkt
.u
.reply
.data
[0]);
571 if ((nfs_offset
!= 0) && !((nfs_offset
) %
572 (NFS_READ_SIZE
/ 2 * 10 * HASHES_PER_LINE
)))
574 if (!(nfs_offset
% ((NFS_READ_SIZE
/ 2) * 10)))
577 rlen
= ntohl(rpc_pkt
.u
.reply
.data
[18]);
578 if (store_block((uchar
*)pkt
+ sizeof(rpc_pkt
.u
.reply
),
585 /**************************************************************************
587 **************************************************************************/
592 if (++NfsTimeoutCount
> NFS_RETRY_COUNT
) {
593 puts("\nRetry count exceeded; starting again\n");
597 NetSetTimeout(nfs_timeout
+ NFS_TIMEOUT
* NfsTimeoutCount
,
603 static void nfs_handler(uchar
*pkt
, unsigned dest
, struct in_addr sip
,
604 unsigned src
, unsigned len
)
609 debug("%s\n", __func__
);
611 if (dest
!= NfsOurPort
)
615 case STATE_PRCLOOKUP_PROG_MOUNT_REQ
:
616 if (rpc_lookup_reply(PROG_MOUNT
, pkt
, len
) == -NFS_RPC_DROP
)
618 NfsState
= STATE_PRCLOOKUP_PROG_NFS_REQ
;
622 case STATE_PRCLOOKUP_PROG_NFS_REQ
:
623 if (rpc_lookup_reply(PROG_NFS
, pkt
, len
) == -NFS_RPC_DROP
)
625 NfsState
= STATE_MOUNT_REQ
;
629 case STATE_MOUNT_REQ
:
630 reply
= nfs_mount_reply(pkt
, len
);
631 if (reply
== -NFS_RPC_DROP
)
633 else if (reply
== -NFS_RPC_ERR
) {
634 puts("*** ERROR: Cannot mount\n");
635 /* just to be sure... */
636 NfsState
= STATE_UMOUNT_REQ
;
639 NfsState
= STATE_LOOKUP_REQ
;
644 case STATE_UMOUNT_REQ
:
645 reply
= nfs_umountall_reply(pkt
, len
);
646 if (reply
== -NFS_RPC_DROP
)
648 else if (reply
== -NFS_RPC_ERR
) {
649 puts("*** ERROR: Cannot umount\n");
650 net_set_state(NETLOOP_FAIL
);
653 net_set_state(nfs_download_state
);
657 case STATE_LOOKUP_REQ
:
658 reply
= nfs_lookup_reply(pkt
, len
);
659 if (reply
== -NFS_RPC_DROP
)
661 else if (reply
== -NFS_RPC_ERR
) {
662 puts("*** ERROR: File lookup fail\n");
663 NfsState
= STATE_UMOUNT_REQ
;
666 NfsState
= STATE_READ_REQ
;
668 nfs_len
= NFS_READ_SIZE
;
673 case STATE_READLINK_REQ
:
674 reply
= nfs_readlink_reply(pkt
, len
);
675 if (reply
== -NFS_RPC_DROP
)
677 else if (reply
== -NFS_RPC_ERR
) {
678 puts("*** ERROR: Symlink fail\n");
679 NfsState
= STATE_UMOUNT_REQ
;
682 debug("Symlink --> %s\n", nfs_path
);
683 nfs_filename
= basename(nfs_path
);
684 nfs_path
= dirname(nfs_path
);
686 NfsState
= STATE_MOUNT_REQ
;
692 rlen
= nfs_read_reply(pkt
, len
);
693 NetSetTimeout(nfs_timeout
, NfsTimeout
);
697 } else if ((rlen
== -NFSERR_ISDIR
) || (rlen
== -NFSERR_INVAL
)) {
699 NfsState
= STATE_READLINK_REQ
;
703 nfs_download_state
= NETLOOP_SUCCESS
;
704 NfsState
= STATE_UMOUNT_REQ
;
715 debug("%s\n", __func__
);
716 nfs_download_state
= NETLOOP_FAIL
;
718 nfs_server_ip
= net_server_ip
;
719 nfs_path
= (char *)nfs_path_buff
;
721 if (nfs_path
== NULL
) {
722 net_set_state(NETLOOP_FAIL
);
723 puts("*** ERROR: Fail allocate memory\n");
727 if (BootFile
[0] == '\0') {
728 sprintf(default_filename
, "/nfsroot/%02X%02X%02X%02X.img",
729 net_ip
.s_addr
& 0xFF,
730 (net_ip
.s_addr
>> 8) & 0xFF,
731 (net_ip
.s_addr
>> 16) & 0xFF,
732 (net_ip
.s_addr
>> 24) & 0xFF);
733 strcpy(nfs_path
, default_filename
);
735 printf("*** Warning: no boot file name; using '%s'\n",
743 nfs_server_ip
= string_to_ip(BootFile
);
747 strcpy(nfs_path
, BootFile
);
751 nfs_filename
= basename(nfs_path
);
752 nfs_path
= dirname(nfs_path
);
754 printf("Using %s device\n", eth_get_name());
756 printf("File transfer via NFS from server %pI4; our IP address is %pI4",
757 &nfs_server_ip
, &net_ip
);
759 /* Check if we need to send across this subnet */
760 if (net_gateway
.s_addr
&& net_netmask
.s_addr
) {
761 struct in_addr our_net
;
762 struct in_addr server_net
;
764 our_net
.s_addr
= net_ip
.s_addr
& net_netmask
.s_addr
;
765 server_net
.s_addr
= net_server_ip
.s_addr
& net_netmask
.s_addr
;
766 if (our_net
.s_addr
!= server_net
.s_addr
)
767 printf("; sending through gateway %pI4",
770 printf("\nFilename '%s/%s'.", nfs_path
, nfs_filename
);
772 if (NetBootFileSize
) {
773 printf(" Size is 0x%x Bytes = ", NetBootFileSize
<<9);
774 print_size(NetBootFileSize
<<9, "");
776 printf("\nLoad address: 0x%lx\n"
777 "Loading: *\b", load_addr
);
779 NetSetTimeout(nfs_timeout
, NfsTimeout
);
780 net_set_udp_handler(nfs_handler
);
783 NfsState
= STATE_PRCLOOKUP_PROG_MOUNT_REQ
;
785 /*NfsOurPort = 4096 + (get_ticks() % 3072);*/
789 /* zero out server ether in case the server ip has changed */
790 memset(NetServerEther
, 0, 6);