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. */
34 #if ((CONFIG_COMMANDS & CFG_CMD_NET) && (CONFIG_COMMANDS & CFG_CMD_NFS))
36 #define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
37 #define NFS_TIMEOUT 10
39 static int fs_mounted
= 0;
40 static unsigned long rpc_id
;
41 static int nfs_offset
= -1;
44 static char dirfh
[NFS_FHSIZE
]; /* file handle of directory */
45 static char filefh
[NFS_FHSIZE
]; /* file handle of kernel image */
47 static IPaddr_t NfsServerIP
;
48 static int NfsSrvMountPort
;
49 static int NfsSrvNfsPort
;
50 static int NfsOurPort
;
51 static int NfsTimeoutCount
;
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
61 static char default_filename
[64];
62 static char *nfs_filename
;
63 static char *nfs_path
;
64 static char nfs_path_buff
[2048];
66 static __inline__
void
67 store_block (uchar
* src
, unsigned offset
, unsigned len
)
69 ulong newsize
= offset
+ len
;
70 #ifdef CFG_DIRECT_FLASH_NFS
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]) {
81 if (rc
) { /* Flash is destination for this packet */
82 rc
= flash_write ((uchar
*)src
, (ulong
)(load_addr
+offset
), len
);
85 NetState
= NETLOOP_FAIL
;
89 #endif /* CFG_DIRECT_FLASH_NFS */
91 (void)memcpy ((void *)(load_addr
+ offset
), src
, len
);
94 if (NetBootFileXferSize
< (offset
+len
))
95 NetBootFileXferSize
= newsize
;
103 fname
= path
+ strlen(path
) - 1;
104 while (fname
>= path
) {
119 fname
= basename (path
);
125 /**************************************************************************
126 RPC_INIT - set up the ID counter to something fairly random
127 **************************************************************************/
133 rpc_id
= t
^ (t
<< 8) ^ (t
<< 16);
136 /**************************************************************************
137 RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
138 **************************************************************************/
139 static long *rpc_add_credentials (long *p
)
145 strcpy (hostname
, "");
146 hostnamelen
=strlen (hostname
);
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
157 hl
= (hostnamelen
+ 3) & ~3;
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 */
167 memcpy (p
, hostname
, hostnamelen
);
171 *p
++ = 0; /* auxiliary gid list */
173 /* Provide an AUTH_NONE verifier. */
174 *p
++ = 0; /* AUTH_NONE */
175 *p
++ = 0; /* auth length */
180 /**************************************************************************
181 RPC_LOOKUP - Lookup RPC Port numbers
182 **************************************************************************/
184 rpc_req (int rpc_prog
, int rpc_proc
, uint32_t *data
, int datalen
)
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
);
202 memcpy ((char *)p
, (char *)data
, datalen
*sizeof(uint32_t));
204 pktlen
= (char *)p
+ datalen
*sizeof(uint32_t) - (char *)&pkt
;
206 memcpy ((char *)NetTxPacket
+ETHER_HDR_SIZE
+IP_HDR_SIZE
, (char *)&pkt
, pktlen
);
208 if (rpc_prog
== PROG_PORTMAP
)
210 else if (rpc_prog
== PROG_MOUNT
)
211 sport
= NfsSrvMountPort
;
213 sport
= NfsSrvNfsPort
;
215 NetSendUDPPacket (NetServerEther
, NfsServerIP
, sport
, NfsOurPort
, pktlen
);
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
);
253 if (pathlen
& 3) *(p
+ pathlen
/ 4) = 0;
254 memcpy (p
, path
, pathlen
);
255 p
+= (pathlen
+ 3) / 4;
257 len
= (uint32_t *)p
- (uint32_t *)&(data
[0]);
259 rpc_req (PROG_MOUNT
, MOUNT_ADDENTRY
, data
, len
);
262 /**************************************************************************
263 NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
264 **************************************************************************/
266 nfs_umountall_req (void)
272 if ((NfsSrvMountPort
== -1) || (!fs_mounted
)) {
273 /* 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
);
329 if (fnamelen
& 3) *(p
+ fnamelen
/ 4) = 0;
330 memcpy (p
, fname
, fnamelen
);
331 p
+= (fnamelen
+ 3) / 4;
333 len
= (uint32_t *)p
- (uint32_t *)&(data
[0]);
335 rpc_req (PROG_NFS
, NFS_LOOKUP
, data
, len
);
338 /**************************************************************************
339 NFS_READ - Read File on NFS Server
340 **************************************************************************/
342 nfs_read_req (int offset
, int readlen
)
349 p
= (uint32_t *)rpc_add_credentials ((long *)p
);
351 memcpy (p
, filefh
, NFS_FHSIZE
);
352 p
+= (NFS_FHSIZE
/ 4);
353 *p
++ = htonl(offset
);
354 *p
++ = htonl(readlen
);
357 len
= (uint32_t *)p
- (uint32_t *)&(data
[0]);
359 rpc_req (PROG_NFS
, NFS_READ
, data
, len
);
362 /**************************************************************************
363 RPC request dispatcher
364 **************************************************************************/
370 printf ("%s\n", __FUNCTION__
);
374 case STATE_PRCLOOKUP_PROG_MOUNT_REQ
:
375 rpc_lookup_req (PROG_MOUNT
, 1);
377 case STATE_PRCLOOKUP_PROG_NFS_REQ
:
378 rpc_lookup_req (PROG_NFS
, 2);
380 case STATE_MOUNT_REQ
:
381 nfs_mount_req (nfs_path
);
383 case STATE_UMOUNT_REQ
:
384 nfs_umountall_req ();
386 case STATE_LOOKUP_REQ
:
387 nfs_lookup_req (nfs_filename
);
390 nfs_read_req (nfs_offset
, nfs_len
);
392 case STATE_READLINK_REQ
:
398 /**************************************************************************
399 Handlers for the reply from server
400 **************************************************************************/
403 rpc_lookup_reply (int prog
, uchar
*pkt
, unsigned len
)
405 struct rpc_t rpc_pkt
;
407 memcpy ((unsigned char *)&rpc_pkt
, pkt
, len
);
410 printf ("%s\n", __FUNCTION__
);
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
) {
421 NfsSrvMountPort
= ntohl(rpc_pkt
.u
.reply
.data
[0]);
424 NfsSrvNfsPort
= ntohl(rpc_pkt
.u
.reply
.data
[0]);
432 nfs_mount_reply (uchar
*pkt
, unsigned len
)
434 struct rpc_t rpc_pkt
;
437 printf ("%s\n", __FUNCTION__
);
440 memcpy ((unsigned char *)&rpc_pkt
, pkt
, len
);
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]) {
450 memcpy (dirfh
, rpc_pkt
.u
.reply
.data
+ 1, NFS_FHSIZE
);
456 nfs_umountall_reply (uchar
*pkt
, unsigned len
)
458 struct rpc_t rpc_pkt
;
461 printf ("%s\n", __FUNCTION__
);
464 memcpy ((unsigned char *)&rpc_pkt
, pkt
, len
);
466 if (rpc_pkt
.u
.reply
.rstatus
||
467 rpc_pkt
.u
.reply
.verifier
||
468 rpc_pkt
.u
.reply
.astatus
) {
473 memset (dirfh
, 0, sizeof(dirfh
));
479 nfs_lookup_reply (uchar
*pkt
, unsigned len
)
481 struct rpc_t rpc_pkt
;
484 printf ("%s\n", __FUNCTION__
);
487 memcpy ((unsigned char *)&rpc_pkt
, pkt
, len
);
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]) {
496 memcpy (filefh
, rpc_pkt
.u
.reply
.data
+ 1, NFS_FHSIZE
);
502 nfs_readlink_reply (uchar
*pkt
, unsigned len
)
504 struct rpc_t rpc_pkt
;
508 printf ("%s\n", __FUNCTION__
);
511 memcpy ((unsigned char *)&rpc_pkt
, pkt
, len
);
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]) {
520 rlen
= ntohl (rpc_pkt
.u
.reply
.data
[1]); /* new path length */
522 if (*((char *)&(rpc_pkt
.u
.reply
.data
[2])) != '/') {
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;
529 memcpy (nfs_path
, (uchar
*)&(rpc_pkt
.u
.reply
.data
[2]), rlen
);
536 nfs_read_reply (uchar
*pkt
, unsigned len
)
538 struct rpc_t rpc_pkt
;
542 printf ("%s\n", __FUNCTION__
);
545 memcpy ((uchar
*)&rpc_pkt
, pkt
, len
);
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
) {
554 if (rpc_pkt
.u
.reply
.astatus
) {
557 return -ntohl(rpc_pkt
.u
.reply
.data
[0]);;
560 if ((nfs_offset
!=0) && !((nfs_offset
) % (NFS_READ_SIZE
/2*10*HASHES_PER_LINE
))) {
563 if (!(nfs_offset
% ((NFS_READ_SIZE
/2)*10))) {
567 rlen
= ntohl(rpc_pkt
.u
.reply
.data
[18]);
568 store_block ((uchar
*)&(rpc_pkt
.u
.reply
.data
[19]), nfs_offset
, rlen
);
573 /**************************************************************************
575 **************************************************************************/
578 NfsHandler (uchar
*pkt
, unsigned dest
, unsigned src
, unsigned len
)
583 printf ("%s\n", __FUNCTION__
);
586 if (dest
!= NfsOurPort
) return;
589 case STATE_PRCLOOKUP_PROG_MOUNT_REQ
:
590 rpc_lookup_reply (PROG_MOUNT
, pkt
, len
);
591 NfsState
= STATE_PRCLOOKUP_PROG_NFS_REQ
;
595 case STATE_PRCLOOKUP_PROG_NFS_REQ
:
596 rpc_lookup_reply (PROG_NFS
, pkt
, len
);
597 NfsState
= STATE_MOUNT_REQ
;
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
;
608 NfsState
= STATE_LOOKUP_REQ
;
613 case STATE_UMOUNT_REQ
:
614 if (nfs_umountall_reply(pkt
, len
)) {
615 puts ("*** ERROR: Cannot umount\n");
616 NetState
= NETLOOP_FAIL
;
619 NetState
= NETLOOP_SUCCESS
;
623 case STATE_LOOKUP_REQ
:
624 if (nfs_lookup_reply(pkt
, len
)) {
625 puts ("*** ERROR: File lookup fail\n");
626 NfsState
= STATE_UMOUNT_REQ
;
629 NfsState
= STATE_READ_REQ
;
631 nfs_len
= NFS_READ_SIZE
;
636 case STATE_READLINK_REQ
:
637 if (nfs_readlink_reply(pkt
, len
)) {
638 puts ("*** ERROR: Symlink fail\n");
639 NfsState
= STATE_UMOUNT_REQ
;
643 printf ("Symlink --> %s\n", nfs_path
);
645 nfs_filename
= basename (nfs_path
);
646 nfs_path
= dirname (nfs_path
);
648 NfsState
= STATE_MOUNT_REQ
;
654 rlen
= nfs_read_reply (pkt
, len
);
659 else if ((rlen
== -NFSERR_ISDIR
)||(rlen
== -NFSERR_INVAL
)) {
661 NfsState
= STATE_READLINK_REQ
;
664 NfsState
= STATE_UMOUNT_REQ
;
675 NetState
= NETLOOP_FAIL
;
683 printf ("%s\n", __FUNCTION__
);
686 NfsServerIP
= NetServerIP
;
687 nfs_path
= (char *)nfs_path_buff
;
689 if (nfs_path
== NULL
) {
690 NetState
= NETLOOP_FAIL
;
691 puts ("*** ERROR: Fail allocate memory\n");
695 if (BootFile
[0] == '\0') {
696 IPaddr_t OurIP
= ntohl (NetOurIP
);
698 sprintf (default_filename
, "/nfsroot/%02lX%02lX%02lX%02lX.img",
701 (OurIP
>> 16) & 0xFF,
702 (OurIP
>> 24) & 0xFF );
703 strcpy (nfs_path
, default_filename
);
705 printf ("*** Warning: no boot file name; using '%s'\n",
713 NfsServerIP
= string_to_ip (BootFile
);
715 strcpy (nfs_path
, p
);
717 strcpy (nfs_path
, BootFile
);
721 nfs_filename
= basename (nfs_path
);
722 nfs_path
= dirname (nfs_path
);
724 #if defined(CONFIG_NET_MULTI)
725 printf ("Using %s device\n", eth_get_name());
728 puts ("File transfer via NFS from server "); print_IPaddr (NfsServerIP
);
729 puts ("; our IP address is "); print_IPaddr (NetOurIP
);
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
;
736 if (OurNet
!= ServerNet
) {
737 puts ("; sending through gateway ");
738 print_IPaddr (NetOurGatewayIP
) ;
743 printf ("Filename '%s/%s'.", nfs_path
, nfs_filename
);
745 if (NetBootFileSize
) {
746 printf (" Size is 0x%x Bytes = ", NetBootFileSize
<<9);
747 print_size (NetBootFileSize
<<9, "");
751 printf ("Load address: 0x%lx\n", load_addr
);
753 printf ("Loading: *\b");
755 NetSetTimeout (NFS_TIMEOUT
* CFG_HZ
, NfsTimeout
);
756 NetSetHandler (NfsHandler
);
760 NfsState
= STATE_PRCLOOKUP_PROG_MOUNT_REQ
;
762 /*NfsOurPort = 4096 + (get_ticks() % 3072);*/
766 /* zero out server ether in case the server ip has changed */
767 memset (NetServerEther
, 0, 6);
772 #endif /* CONFIG_COMMANDS & CFG_CMD_NFS */