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. */
32 #define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
33 #define NFS_RETRY_COUNT 30
34 #ifndef CONFIG_NFS_TIMEOUT
35 # define NFS_TIMEOUT 2000UL
37 # define NFS_TIMEOUT CONFIG_NFS_TIMEOUT
40 static int fs_mounted
;
41 static unsigned long rpc_id
;
42 static int nfs_offset
= -1;
45 static char dirfh
[NFS_FHSIZE
]; /* file handle of directory */
46 static char filefh
[NFS_FHSIZE
]; /* file handle of kernel image */
48 static enum net_loop_state nfs_download_state
;
49 static IPaddr_t NfsServerIP
;
50 static int NfsSrvMountPort
;
51 static int NfsSrvNfsPort
;
52 static int NfsOurPort
;
53 static int NfsTimeoutCount
;
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
63 static char default_filename
[64];
64 static char *nfs_filename
;
65 static char *nfs_path
;
66 static char nfs_path_buff
[2048];
69 store_block(uchar
*src
, unsigned offset
, unsigned len
)
71 ulong newsize
= offset
+ len
;
72 #ifdef CONFIG_SYS_DIRECT_FLASH_NFS
75 for (i
= 0; i
< CONFIG_SYS_MAX_FLASH_BANKS
; i
++) {
76 /* start address in flash? */
77 if (load_addr
+ offset
>= flash_info
[i
].start
[0]) {
83 if (rc
) { /* Flash is destination for this packet */
84 rc
= flash_write((uchar
*)src
, (ulong
)(load_addr
+offset
), len
);
90 #endif /* CONFIG_SYS_DIRECT_FLASH_NFS */
92 (void)memcpy((void *)(load_addr
+ offset
), src
, len
);
95 if (NetBootFileXferSize
< (offset
+len
))
96 NetBootFileXferSize
= newsize
;
105 fname
= path
+ strlen(path
) - 1;
106 while (fname
>= path
) {
121 fname
= basename(path
);
127 /**************************************************************************
128 RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
129 **************************************************************************/
130 static long *rpc_add_credentials(long *p
)
136 strcpy(hostname
, "");
137 hostnamelen
= strlen(hostname
);
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
148 hl
= (hostnamelen
+ 3) & ~3;
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 */
156 *(p
+ hostnamelen
/ 4) = 0; /* add zero padding */
157 memcpy(p
, hostname
, hostnamelen
);
161 *p
++ = 0; /* auxiliary gid list */
163 /* Provide an AUTH_NONE verifier. */
164 *p
++ = 0; /* AUTH_NONE */
165 *p
++ = 0; /* auth length */
170 /**************************************************************************
171 RPC_LOOKUP - Lookup RPC Port numbers
172 **************************************************************************/
174 rpc_req(int rpc_prog
, int rpc_proc
, uint32_t *data
, int datalen
)
183 pkt
.u
.call
.id
= htonl(id
);
184 pkt
.u
.call
.type
= htonl(MSG_CALL
);
185 pkt
.u
.call
.rpcvers
= htonl(2); /* use RPC version 2 */
186 pkt
.u
.call
.prog
= htonl(rpc_prog
);
187 pkt
.u
.call
.vers
= htonl(2); /* portmapper is version 2 */
188 pkt
.u
.call
.proc
= htonl(rpc_proc
);
189 p
= (uint32_t *)&(pkt
.u
.call
.data
);
192 memcpy((char *)p
, (char *)data
, datalen
*sizeof(uint32_t));
194 pktlen
= (char *)p
+ datalen
*sizeof(uint32_t) - (char *)&pkt
;
196 memcpy((char *)NetTxPacket
+ NetEthHdrSize() + IP_UDP_HDR_SIZE
,
197 (char *)&pkt
, pktlen
);
199 if (rpc_prog
== PROG_PORTMAP
)
201 else if (rpc_prog
== PROG_MOUNT
)
202 sport
= NfsSrvMountPort
;
204 sport
= NfsSrvNfsPort
;
206 NetSendUDPPacket(NetServerEther
, NfsServerIP
, sport
, NfsOurPort
,
210 /**************************************************************************
211 RPC_LOOKUP - Lookup RPC Port numbers
212 **************************************************************************/
214 rpc_lookup_req(int prog
, int ver
)
218 data
[0] = 0; data
[1] = 0; /* auth credential */
219 data
[2] = 0; data
[3] = 0; /* auth verifier */
220 data
[4] = htonl(prog
);
221 data
[5] = htonl(ver
);
222 data
[6] = htonl(17); /* IP_UDP */
225 rpc_req(PROG_PORTMAP
, PORTMAP_GETPORT
, data
, 8);
228 /**************************************************************************
229 NFS_MOUNT - Mount an NFS Filesystem
230 **************************************************************************/
232 nfs_mount_req(char *path
)
239 pathlen
= strlen(path
);
242 p
= (uint32_t *)rpc_add_credentials((long *)p
);
244 *p
++ = htonl(pathlen
);
246 *(p
+ pathlen
/ 4) = 0;
247 memcpy(p
, path
, pathlen
);
248 p
+= (pathlen
+ 3) / 4;
250 len
= (uint32_t *)p
- (uint32_t *)&(data
[0]);
252 rpc_req(PROG_MOUNT
, MOUNT_ADDENTRY
, data
, len
);
255 /**************************************************************************
256 NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
257 **************************************************************************/
259 nfs_umountall_req(void)
265 if ((NfsSrvMountPort
== -1) || (!fs_mounted
))
266 /* Nothing mounted, nothing to umount */
270 p
= (uint32_t *)rpc_add_credentials((long *)p
);
272 len
= (uint32_t *)p
- (uint32_t *)&(data
[0]);
274 rpc_req(PROG_MOUNT
, MOUNT_UMOUNTALL
, data
, len
);
277 /***************************************************************************
278 * NFS_READLINK (AH 2003-07-14)
279 * This procedure is called when read of the first block fails -
280 * this probably happens when it's a directory or a symlink
281 * In case of successful readlink(), the dirname is manipulated,
282 * so that inside the nfs() function a recursion can be done.
283 **************************************************************************/
285 nfs_readlink_req(void)
292 p
= (uint32_t *)rpc_add_credentials((long *)p
);
294 memcpy(p
, filefh
, NFS_FHSIZE
);
295 p
+= (NFS_FHSIZE
/ 4);
297 len
= (uint32_t *)p
- (uint32_t *)&(data
[0]);
299 rpc_req(PROG_NFS
, NFS_READLINK
, data
, len
);
302 /**************************************************************************
303 NFS_LOOKUP - Lookup Pathname
304 **************************************************************************/
306 nfs_lookup_req(char *fname
)
313 fnamelen
= strlen(fname
);
316 p
= (uint32_t *)rpc_add_credentials((long *)p
);
318 memcpy(p
, dirfh
, NFS_FHSIZE
);
319 p
+= (NFS_FHSIZE
/ 4);
320 *p
++ = htonl(fnamelen
);
322 *(p
+ fnamelen
/ 4) = 0;
323 memcpy(p
, fname
, fnamelen
);
324 p
+= (fnamelen
+ 3) / 4;
326 len
= (uint32_t *)p
- (uint32_t *)&(data
[0]);
328 rpc_req(PROG_NFS
, NFS_LOOKUP
, data
, len
);
331 /**************************************************************************
332 NFS_READ - Read File on NFS Server
333 **************************************************************************/
335 nfs_read_req(int offset
, int readlen
)
342 p
= (uint32_t *)rpc_add_credentials((long *)p
);
344 memcpy(p
, filefh
, NFS_FHSIZE
);
345 p
+= (NFS_FHSIZE
/ 4);
346 *p
++ = htonl(offset
);
347 *p
++ = htonl(readlen
);
350 len
= (uint32_t *)p
- (uint32_t *)&(data
[0]);
352 rpc_req(PROG_NFS
, NFS_READ
, data
, len
);
355 /**************************************************************************
356 RPC request dispatcher
357 **************************************************************************/
362 debug("%s\n", __func__
);
365 case STATE_PRCLOOKUP_PROG_MOUNT_REQ
:
366 rpc_lookup_req(PROG_MOUNT
, 1);
368 case STATE_PRCLOOKUP_PROG_NFS_REQ
:
369 rpc_lookup_req(PROG_NFS
, 2);
371 case STATE_MOUNT_REQ
:
372 nfs_mount_req(nfs_path
);
374 case STATE_UMOUNT_REQ
:
377 case STATE_LOOKUP_REQ
:
378 nfs_lookup_req(nfs_filename
);
381 nfs_read_req(nfs_offset
, nfs_len
);
383 case STATE_READLINK_REQ
:
389 /**************************************************************************
390 Handlers for the reply from server
391 **************************************************************************/
394 rpc_lookup_reply(int prog
, uchar
*pkt
, unsigned len
)
396 struct rpc_t rpc_pkt
;
398 memcpy((unsigned char *)&rpc_pkt
, pkt
, len
);
400 debug("%s\n", __func__
);
402 if (ntohl(rpc_pkt
.u
.reply
.id
) != rpc_id
)
405 if (rpc_pkt
.u
.reply
.rstatus
||
406 rpc_pkt
.u
.reply
.verifier
||
407 rpc_pkt
.u
.reply
.astatus
)
412 NfsSrvMountPort
= ntohl(rpc_pkt
.u
.reply
.data
[0]);
415 NfsSrvNfsPort
= ntohl(rpc_pkt
.u
.reply
.data
[0]);
423 nfs_mount_reply(uchar
*pkt
, unsigned len
)
425 struct rpc_t rpc_pkt
;
427 debug("%s\n", __func__
);
429 memcpy((unsigned char *)&rpc_pkt
, pkt
, len
);
431 if (ntohl(rpc_pkt
.u
.reply
.id
) != rpc_id
)
434 if (rpc_pkt
.u
.reply
.rstatus
||
435 rpc_pkt
.u
.reply
.verifier
||
436 rpc_pkt
.u
.reply
.astatus
||
437 rpc_pkt
.u
.reply
.data
[0])
441 memcpy(dirfh
, rpc_pkt
.u
.reply
.data
+ 1, NFS_FHSIZE
);
447 nfs_umountall_reply(uchar
*pkt
, unsigned len
)
449 struct rpc_t rpc_pkt
;
451 debug("%s\n", __func__
);
453 memcpy((unsigned char *)&rpc_pkt
, pkt
, len
);
455 if (ntohl(rpc_pkt
.u
.reply
.id
) != rpc_id
)
458 if (rpc_pkt
.u
.reply
.rstatus
||
459 rpc_pkt
.u
.reply
.verifier
||
460 rpc_pkt
.u
.reply
.astatus
)
464 memset(dirfh
, 0, sizeof(dirfh
));
470 nfs_lookup_reply(uchar
*pkt
, unsigned len
)
472 struct rpc_t rpc_pkt
;
474 debug("%s\n", __func__
);
476 memcpy((unsigned char *)&rpc_pkt
, pkt
, len
);
478 if (ntohl(rpc_pkt
.u
.reply
.id
) != rpc_id
)
481 if (rpc_pkt
.u
.reply
.rstatus
||
482 rpc_pkt
.u
.reply
.verifier
||
483 rpc_pkt
.u
.reply
.astatus
||
484 rpc_pkt
.u
.reply
.data
[0])
487 memcpy(filefh
, rpc_pkt
.u
.reply
.data
+ 1, NFS_FHSIZE
);
493 nfs_readlink_reply(uchar
*pkt
, unsigned len
)
495 struct rpc_t rpc_pkt
;
498 debug("%s\n", __func__
);
500 memcpy((unsigned char *)&rpc_pkt
, pkt
, len
);
502 if (ntohl(rpc_pkt
.u
.reply
.id
) != rpc_id
)
505 if (rpc_pkt
.u
.reply
.rstatus
||
506 rpc_pkt
.u
.reply
.verifier
||
507 rpc_pkt
.u
.reply
.astatus
||
508 rpc_pkt
.u
.reply
.data
[0])
511 rlen
= ntohl(rpc_pkt
.u
.reply
.data
[1]); /* new path length */
513 if (*((char *)&(rpc_pkt
.u
.reply
.data
[2])) != '/') {
515 strcat(nfs_path
, "/");
516 pathlen
= strlen(nfs_path
);
517 memcpy(nfs_path
+ pathlen
, (uchar
*)&(rpc_pkt
.u
.reply
.data
[2]),
519 nfs_path
[pathlen
+ rlen
] = 0;
521 memcpy(nfs_path
, (uchar
*)&(rpc_pkt
.u
.reply
.data
[2]), rlen
);
528 nfs_read_reply(uchar
*pkt
, unsigned len
)
530 struct rpc_t rpc_pkt
;
533 debug("%s\n", __func__
);
535 memcpy((uchar
*)&rpc_pkt
, pkt
, sizeof(rpc_pkt
.u
.reply
));
537 if (ntohl(rpc_pkt
.u
.reply
.id
) != rpc_id
)
540 if (rpc_pkt
.u
.reply
.rstatus
||
541 rpc_pkt
.u
.reply
.verifier
||
542 rpc_pkt
.u
.reply
.astatus
||
543 rpc_pkt
.u
.reply
.data
[0]) {
544 if (rpc_pkt
.u
.reply
.rstatus
)
546 if (rpc_pkt
.u
.reply
.astatus
)
548 return -ntohl(rpc_pkt
.u
.reply
.data
[0]);
551 if ((nfs_offset
!= 0) && !((nfs_offset
) %
552 (NFS_READ_SIZE
/ 2 * 10 * HASHES_PER_LINE
)))
554 if (!(nfs_offset
% ((NFS_READ_SIZE
/ 2) * 10)))
557 rlen
= ntohl(rpc_pkt
.u
.reply
.data
[18]);
558 if (store_block((uchar
*)pkt
+ sizeof(rpc_pkt
.u
.reply
),
565 /**************************************************************************
567 **************************************************************************/
572 if (++NfsTimeoutCount
> NFS_RETRY_COUNT
) {
573 puts("\nRetry count exceeded; starting again\n");
577 NetSetTimeout(NFS_TIMEOUT
, NfsTimeout
);
583 NfsHandler(uchar
*pkt
, unsigned dest
, IPaddr_t sip
, unsigned src
, unsigned len
)
587 debug("%s\n", __func__
);
589 if (dest
!= NfsOurPort
)
593 case STATE_PRCLOOKUP_PROG_MOUNT_REQ
:
594 rpc_lookup_reply(PROG_MOUNT
, pkt
, len
);
595 NfsState
= STATE_PRCLOOKUP_PROG_NFS_REQ
;
599 case STATE_PRCLOOKUP_PROG_NFS_REQ
:
600 rpc_lookup_reply(PROG_NFS
, pkt
, len
);
601 NfsState
= STATE_MOUNT_REQ
;
605 case STATE_MOUNT_REQ
:
606 if (nfs_mount_reply(pkt
, len
)) {
607 puts("*** ERROR: Cannot mount\n");
608 /* just to be sure... */
609 NfsState
= STATE_UMOUNT_REQ
;
612 NfsState
= STATE_LOOKUP_REQ
;
617 case STATE_UMOUNT_REQ
:
618 if (nfs_umountall_reply(pkt
, len
)) {
619 puts("*** ERROR: Cannot umount\n");
620 net_set_state(NETLOOP_FAIL
);
623 net_set_state(nfs_download_state
);
627 case STATE_LOOKUP_REQ
:
628 if (nfs_lookup_reply(pkt
, len
)) {
629 puts("*** ERROR: File lookup fail\n");
630 NfsState
= STATE_UMOUNT_REQ
;
633 NfsState
= STATE_READ_REQ
;
635 nfs_len
= NFS_READ_SIZE
;
640 case STATE_READLINK_REQ
:
641 if (nfs_readlink_reply(pkt
, len
)) {
642 puts("*** ERROR: Symlink fail\n");
643 NfsState
= STATE_UMOUNT_REQ
;
646 debug("Symlink --> %s\n", nfs_path
);
647 nfs_filename
= basename(nfs_path
);
648 nfs_path
= dirname(nfs_path
);
650 NfsState
= STATE_MOUNT_REQ
;
656 rlen
= nfs_read_reply(pkt
, len
);
657 NetSetTimeout(NFS_TIMEOUT
, NfsTimeout
);
661 } else if ((rlen
== -NFSERR_ISDIR
) || (rlen
== -NFSERR_INVAL
)) {
663 NfsState
= STATE_READLINK_REQ
;
667 nfs_download_state
= NETLOOP_SUCCESS
;
668 NfsState
= STATE_UMOUNT_REQ
;
679 debug("%s\n", __func__
);
680 nfs_download_state
= NETLOOP_FAIL
;
682 NfsServerIP
= NetServerIP
;
683 nfs_path
= (char *)nfs_path_buff
;
685 if (nfs_path
== NULL
) {
686 net_set_state(NETLOOP_FAIL
);
687 puts("*** ERROR: Fail allocate memory\n");
691 if (BootFile
[0] == '\0') {
692 sprintf(default_filename
, "/nfsroot/%02X%02X%02X%02X.img",
694 (NetOurIP
>> 8) & 0xFF,
695 (NetOurIP
>> 16) & 0xFF,
696 (NetOurIP
>> 24) & 0xFF);
697 strcpy(nfs_path
, default_filename
);
699 printf("*** Warning: no boot file name; using '%s'\n",
707 NfsServerIP
= string_to_ip(BootFile
);
711 strcpy(nfs_path
, BootFile
);
715 nfs_filename
= basename(nfs_path
);
716 nfs_path
= dirname(nfs_path
);
718 printf("Using %s device\n", eth_get_name());
720 printf("File transfer via NFS from server %pI4"
721 "; our IP address is %pI4", &NfsServerIP
, &NetOurIP
);
723 /* Check if we need to send across this subnet */
724 if (NetOurGatewayIP
&& NetOurSubnetMask
) {
725 IPaddr_t OurNet
= NetOurIP
& NetOurSubnetMask
;
726 IPaddr_t ServerNet
= NetServerIP
& NetOurSubnetMask
;
728 if (OurNet
!= ServerNet
)
729 printf("; sending through gateway %pI4",
732 printf("\nFilename '%s/%s'.", nfs_path
, nfs_filename
);
734 if (NetBootFileSize
) {
735 printf(" Size is 0x%x Bytes = ", NetBootFileSize
<<9);
736 print_size(NetBootFileSize
<<9, "");
738 printf("\nLoad address: 0x%lx\n"
739 "Loading: *\b", load_addr
);
741 NetSetTimeout(NFS_TIMEOUT
, NfsTimeout
);
742 net_set_udp_handler(NfsHandler
);
745 NfsState
= STATE_PRCLOOKUP_PROG_MOUNT_REQ
;
747 /*NfsOurPort = 4096 + (get_ticks() % 3072);*/
751 /* zero out server ether in case the server ip has changed */
752 memset(NetServerEther
, 0, 6);