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 defined(CONFIG_CMD_NET) && defined(CONFIG_CMD_NFS)
36 #define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
37 #define NFS_TIMEOUT 60
39 static int fs_mounted = 0;
40 static unsigned long rpc_id = 0;
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 int NfsDownloadState;
48 static IPaddr_t NfsServerIP;
49 static int NfsSrvMountPort;
50 static int NfsSrvNfsPort;
51 static int NfsOurPort;
52 static int NfsTimeoutCount;
54 #define STATE_PRCLOOKUP_PROG_MOUNT_REQ 1
55 #define STATE_PRCLOOKUP_PROG_NFS_REQ 2
56 #define STATE_MOUNT_REQ 3
57 #define STATE_UMOUNT_REQ 4
58 #define STATE_LOOKUP_REQ 5
59 #define STATE_READ_REQ 6
60 #define STATE_READLINK_REQ 7
62 static char default_filename[64];
63 static char *nfs_filename;
64 static char *nfs_path;
65 static char nfs_path_buff[2048];
68 store_block (uchar * src, unsigned offset, unsigned len)
70 ulong newsize = offset + len;
71 #ifdef CFG_DIRECT_FLASH_NFS
74 for (i=0; i<CFG_MAX_FLASH_BANKS; i++) {
75 /* start address in flash? */
76 if (load_addr + offset >= flash_info[i].start[0]) {
82 if (rc) { /* Flash is destination for this packet */
83 rc = flash_write ((uchar *)src, (ulong)(load_addr+offset), len);
89 #endif /* CFG_DIRECT_FLASH_NFS */
91 (void)memcpy ((void *)(load_addr + offset), src, len);
94 if (NetBootFileXferSize < (offset+len))
95 NetBootFileXferSize = newsize;
100 basename (char *path)
104 fname = path + strlen(path) - 1;
105 while (fname >= path) {
120 fname = basename (path);
126 /**************************************************************************
127 RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
128 **************************************************************************/
129 static long *rpc_add_credentials (long *p)
135 strcpy (hostname, "");
136 hostnamelen=strlen (hostname);
138 /* Here's the executive summary on authentication requirements of the
139 * various NFS server implementations: Linux accepts both AUTH_NONE
140 * and AUTH_UNIX authentication (also accepts an empty hostname field
141 * in the AUTH_UNIX scheme). *BSD refuses AUTH_NONE, but accepts
142 * AUTH_UNIX (also accepts an empty hostname field in the AUTH_UNIX
143 * scheme). To be safe, use AUTH_UNIX and pass the hostname if we have
144 * it (if the BOOTP/DHCP reply didn't give one, just use an empty
147 hl = (hostnamelen + 3) & ~3;
149 /* Provide an AUTH_UNIX credential. */
150 *p++ = htonl(1); /* AUTH_UNIX */
151 *p++ = htonl(hl+20); /* auth length */
152 *p++ = htonl(0); /* stamp */
153 *p++ = htonl(hostnamelen); /* hostname string */
154 if (hostnamelen & 3) {
155 *(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_HDR_SIZE, (char *)&pkt, pktlen);
198 if (rpc_prog == PROG_PORTMAP)
200 else if (rpc_prog == PROG_MOUNT)
201 sport = NfsSrvMountPort;
203 sport = NfsSrvNfsPort;
205 NetSendUDPPacket (NetServerEther, NfsServerIP, sport, NfsOurPort, pktlen);
208 /**************************************************************************
209 RPC_LOOKUP - Lookup RPC Port numbers
210 **************************************************************************/
212 rpc_lookup_req (int prog, int ver)
216 data[0] = 0; data[1] = 0; /* auth credential */
217 data[2] = 0; data[3] = 0; /* auth verifier */
218 data[4] = htonl(prog);
219 data[5] = htonl(ver);
220 data[6] = htonl(17); /* IP_UDP */
223 rpc_req (PROG_PORTMAP, PORTMAP_GETPORT, data, 8);
226 /**************************************************************************
227 NFS_MOUNT - Mount an NFS Filesystem
228 **************************************************************************/
230 nfs_mount_req (char *path)
237 pathlen = strlen (path);
240 p = (uint32_t *)rpc_add_credentials((long *)p);
242 *p++ = htonl(pathlen);
243 if (pathlen & 3) *(p + pathlen / 4) = 0;
244 memcpy (p, path, pathlen);
245 p += (pathlen + 3) / 4;
247 len = (uint32_t *)p - (uint32_t *)&(data[0]);
249 rpc_req (PROG_MOUNT, MOUNT_ADDENTRY, data, len);
252 /**************************************************************************
253 NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
254 **************************************************************************/
256 nfs_umountall_req (void)
262 if ((NfsSrvMountPort == -1) || (!fs_mounted)) {
263 /* Nothing mounted, nothing to umount */
268 p = (uint32_t *)rpc_add_credentials ((long *)p);
270 len = (uint32_t *)p - (uint32_t *)&(data[0]);
272 rpc_req (PROG_MOUNT, MOUNT_UMOUNTALL, data, len);
275 /***************************************************************************
276 * NFS_READLINK (AH 2003-07-14)
277 * This procedure is called when read of the first block fails -
278 * this probably happens when it's a directory or a symlink
279 * In case of successful readlink(), the dirname is manipulated,
280 * so that inside the nfs() function a recursion can be done.
281 **************************************************************************/
283 nfs_readlink_req (void)
290 p = (uint32_t *)rpc_add_credentials ((long *)p);
292 memcpy (p, filefh, NFS_FHSIZE);
293 p += (NFS_FHSIZE / 4);
295 len = (uint32_t *)p - (uint32_t *)&(data[0]);
297 rpc_req (PROG_NFS, NFS_READLINK, data, len);
300 /**************************************************************************
301 NFS_LOOKUP - Lookup Pathname
302 **************************************************************************/
304 nfs_lookup_req (char *fname)
311 fnamelen = strlen (fname);
314 p = (uint32_t *)rpc_add_credentials ((long *)p);
316 memcpy (p, dirfh, NFS_FHSIZE);
317 p += (NFS_FHSIZE / 4);
318 *p++ = htonl(fnamelen);
319 if (fnamelen & 3) *(p + fnamelen / 4) = 0;
320 memcpy (p, fname, fnamelen);
321 p += (fnamelen + 3) / 4;
323 len = (uint32_t *)p - (uint32_t *)&(data[0]);
325 rpc_req (PROG_NFS, NFS_LOOKUP, data, len);
328 /**************************************************************************
329 NFS_READ - Read File on NFS Server
330 **************************************************************************/
332 nfs_read_req (int offset, int readlen)
339 p = (uint32_t *)rpc_add_credentials ((long *)p);
341 memcpy (p, filefh, NFS_FHSIZE);
342 p += (NFS_FHSIZE / 4);
343 *p++ = htonl(offset);
344 *p++ = htonl(readlen);
347 len = (uint32_t *)p - (uint32_t *)&(data[0]);
349 rpc_req (PROG_NFS, NFS_READ, data, len);
352 /**************************************************************************
353 RPC request dispatcher
354 **************************************************************************/
360 printf ("%s\n", __FUNCTION__);
364 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
365 rpc_lookup_req (PROG_MOUNT, 1);
367 case STATE_PRCLOOKUP_PROG_NFS_REQ:
368 rpc_lookup_req (PROG_NFS, 2);
370 case STATE_MOUNT_REQ:
371 nfs_mount_req (nfs_path);
373 case STATE_UMOUNT_REQ:
374 nfs_umountall_req ();
376 case STATE_LOOKUP_REQ:
377 nfs_lookup_req (nfs_filename);
380 nfs_read_req (nfs_offset, nfs_len);
382 case STATE_READLINK_REQ:
388 /**************************************************************************
389 Handlers for the reply from server
390 **************************************************************************/
393 rpc_lookup_reply (int prog, uchar *pkt, unsigned len)
395 struct rpc_t rpc_pkt;
397 memcpy ((unsigned char *)&rpc_pkt, pkt, len);
400 printf ("%s\n", __FUNCTION__);
403 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
406 if (rpc_pkt.u.reply.rstatus ||
407 rpc_pkt.u.reply.verifier ||
408 rpc_pkt.u.reply.astatus ||
409 rpc_pkt.u.reply.astatus) {
415 NfsSrvMountPort = ntohl(rpc_pkt.u.reply.data[0]);
418 NfsSrvNfsPort = ntohl(rpc_pkt.u.reply.data[0]);
426 nfs_mount_reply (uchar *pkt, unsigned len)
428 struct rpc_t rpc_pkt;
431 printf ("%s\n", __FUNCTION__);
434 memcpy ((unsigned char *)&rpc_pkt, pkt, len);
436 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
439 if (rpc_pkt.u.reply.rstatus ||
440 rpc_pkt.u.reply.verifier ||
441 rpc_pkt.u.reply.astatus ||
442 rpc_pkt.u.reply.data[0]) {
447 memcpy (dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
453 nfs_umountall_reply (uchar *pkt, unsigned len)
455 struct rpc_t rpc_pkt;
458 printf ("%s\n", __FUNCTION__);
461 memcpy ((unsigned char *)&rpc_pkt, pkt, len);
463 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
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 (ntohl(rpc_pkt.u.reply.id) != rpc_id)
492 if (rpc_pkt.u.reply.rstatus ||
493 rpc_pkt.u.reply.verifier ||
494 rpc_pkt.u.reply.astatus ||
495 rpc_pkt.u.reply.data[0]) {
499 memcpy (filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
505 nfs_readlink_reply (uchar *pkt, unsigned len)
507 struct rpc_t rpc_pkt;
511 printf ("%s\n", __FUNCTION__);
514 memcpy ((unsigned char *)&rpc_pkt, pkt, len);
516 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
519 if (rpc_pkt.u.reply.rstatus ||
520 rpc_pkt.u.reply.verifier ||
521 rpc_pkt.u.reply.astatus ||
522 rpc_pkt.u.reply.data[0]) {
526 rlen = ntohl (rpc_pkt.u.reply.data[1]); /* new path length */
528 if (*((char *)&(rpc_pkt.u.reply.data[2])) != '/') {
530 strcat (nfs_path, "/");
531 pathlen = strlen(nfs_path);
532 memcpy (nfs_path+pathlen, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen);
533 nfs_path[pathlen+rlen+1] = 0;
535 memcpy (nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen);
542 nfs_read_reply (uchar *pkt, unsigned len)
544 struct rpc_t rpc_pkt;
548 printf ("%s\n", __FUNCTION__);
551 memcpy ((uchar *)&rpc_pkt, pkt, sizeof(rpc_pkt.u.reply));
553 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
556 if (rpc_pkt.u.reply.rstatus ||
557 rpc_pkt.u.reply.verifier ||
558 rpc_pkt.u.reply.astatus ||
559 rpc_pkt.u.reply.data[0]) {
560 if (rpc_pkt.u.reply.rstatus) {
563 if (rpc_pkt.u.reply.astatus) {
566 return -ntohl(rpc_pkt.u.reply.data[0]);;
569 if ((nfs_offset!=0) && !((nfs_offset) % (NFS_READ_SIZE/2*10*HASHES_PER_LINE))) {
572 if (!(nfs_offset % ((NFS_READ_SIZE/2)*10))) {
576 rlen = ntohl(rpc_pkt.u.reply.data[18]);
577 if ( store_block ((uchar *)pkt+sizeof(rpc_pkt.u.reply), nfs_offset, rlen) )
583 /**************************************************************************
585 **************************************************************************/
591 NetState = NETLOOP_FAIL;
596 NfsHandler (uchar *pkt, unsigned dest, unsigned src, unsigned len)
601 printf ("%s\n", __FUNCTION__);
604 if (dest != NfsOurPort) return;
607 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
608 rpc_lookup_reply (PROG_MOUNT, pkt, len);
609 NfsState = STATE_PRCLOOKUP_PROG_NFS_REQ;
613 case STATE_PRCLOOKUP_PROG_NFS_REQ:
614 rpc_lookup_reply (PROG_NFS, pkt, len);
615 NfsState = STATE_MOUNT_REQ;
619 case STATE_MOUNT_REQ:
620 if (nfs_mount_reply(pkt, len)) {
621 puts ("*** ERROR: Cannot mount\n");
622 /* just to be sure... */
623 NfsState = STATE_UMOUNT_REQ;
626 NfsState = STATE_LOOKUP_REQ;
631 case STATE_UMOUNT_REQ:
632 if (nfs_umountall_reply(pkt, len)) {
633 puts ("*** ERROR: Cannot umount\n");
634 NetState = NETLOOP_FAIL;
637 NetState = NfsDownloadState;
641 case STATE_LOOKUP_REQ:
642 if (nfs_lookup_reply(pkt, len)) {
643 puts ("*** ERROR: File lookup fail\n");
644 NfsState = STATE_UMOUNT_REQ;
647 NfsState = STATE_READ_REQ;
649 nfs_len = NFS_READ_SIZE;
654 case STATE_READLINK_REQ:
655 if (nfs_readlink_reply(pkt, len)) {
656 puts ("*** ERROR: Symlink fail\n");
657 NfsState = STATE_UMOUNT_REQ;
661 printf ("Symlink --> %s\n", nfs_path);
663 nfs_filename = basename (nfs_path);
664 nfs_path = dirname (nfs_path);
666 NfsState = STATE_MOUNT_REQ;
672 rlen = nfs_read_reply (pkt, len);
673 NetSetTimeout (NFS_TIMEOUT * CFG_HZ, NfsTimeout);
678 else if ((rlen == -NFSERR_ISDIR)||(rlen == -NFSERR_INVAL)) {
680 NfsState = STATE_READLINK_REQ;
683 if ( ! rlen ) NfsDownloadState = NETLOOP_SUCCESS;
684 NfsState = STATE_UMOUNT_REQ;
696 printf ("%s\n", __FUNCTION__);
698 NfsDownloadState = NETLOOP_FAIL;
700 NfsServerIP = NetServerIP;
701 nfs_path = (char *)nfs_path_buff;
703 if (nfs_path == NULL) {
704 NetState = NETLOOP_FAIL;
705 puts ("*** ERROR: Fail allocate memory\n");
709 if (BootFile[0] == '\0') {
710 sprintf (default_filename, "/nfsroot/%02lX%02lX%02lX%02lX.img",
712 (NetOurIP >> 8) & 0xFF,
713 (NetOurIP >> 16) & 0xFF,
714 (NetOurIP >> 24) & 0xFF );
715 strcpy (nfs_path, default_filename);
717 printf ("** Warning: no boot file name; using '%s'\n",
725 NfsServerIP = string_to_ip (BootFile);
727 strcpy (nfs_path, p);
729 strcpy (nfs_path, BootFile);
733 nfs_filename = basename (nfs_path);
734 nfs_path = dirname (nfs_path);
736 #if defined(CONFIG_NET_MULTI)
737 printf ("Using %s device\n", eth_get_name());
740 puts ("File transfer via NFS from server "); print_IPaddr (NfsServerIP);
741 puts ("; our IP address is "); print_IPaddr (NetOurIP);
743 /* Check if we need to send across this subnet */
744 if (NetOurGatewayIP && NetOurSubnetMask) {
745 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
746 IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask;
748 if (OurNet != ServerNet) {
749 puts ("; sending through gateway ");
750 print_IPaddr (NetOurGatewayIP) ;
753 printf ("\nFilename '%s/%s'.", nfs_path, nfs_filename);
755 if (NetBootFileSize) {
756 printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
757 print_size (NetBootFileSize<<9, "");
759 printf ("\nLoad address: 0x%lx\n"
760 "Loading: *\b", load_addr);
762 NetSetTimeout (NFS_TIMEOUT * CFG_HZ, NfsTimeout);
763 NetSetHandler (NfsHandler);
766 NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
768 /*NfsOurPort = 4096 + (get_ticks() % 3072);*/
772 /* zero out server ether in case the server ip has changed */
773 memset (NetServerEther, 0, 6);
778 #endif /* CONFIG_CMD_NET && CONFIG_CMD_NFS */