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 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)
184 printf ("%s xid: %d, rpc_id: %d\n", __FUNCTION__, id, rpc_id);
186 pkt.u.call.id = htonl(id);
187 pkt.u.call.type = htonl(MSG_CALL);
188 pkt.u.call.rpcvers = htonl(2); /* use RPC version 2 */
189 pkt.u.call.prog = htonl(rpc_prog);
190 pkt.u.call.vers = htonl(2); /* portmapper is version 2 */
191 pkt.u.call.proc = htonl(rpc_proc);
192 p = (uint32_t *)&(pkt.u.call.data);
195 memcpy ((char *)p, (char *)data, datalen*sizeof(uint32_t));
197 pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt;
199 memcpy ((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, (char *)&pkt, pktlen);
201 if (rpc_prog == PROG_PORTMAP)
203 else if (rpc_prog == PROG_MOUNT)
204 sport = NfsSrvMountPort;
206 sport = NfsSrvNfsPort;
208 NetSendUDPPacket (NetServerEther, NfsServerIP, sport, NfsOurPort, pktlen);
211 /**************************************************************************
212 RPC_LOOKUP - Lookup RPC Port numbers
213 **************************************************************************/
215 rpc_lookup_req (int prog, int ver)
220 printf ("%s\n", __FUNCTION__);
223 data[0] = 0; data[1] = 0; /* auth credential */
224 data[2] = 0; data[3] = 0; /* auth verifier */
225 data[4] = htonl(prog);
226 data[5] = htonl(ver);
227 data[6] = htonl(17); /* IP_UDP */
230 rpc_req (PROG_PORTMAP, PORTMAP_GETPORT, data, 8);
233 /**************************************************************************
234 NFS_MOUNT - Mount an NFS Filesystem
235 **************************************************************************/
237 nfs_mount_req (char *path)
245 printf ("%s\n", __FUNCTION__);
248 pathlen = strlen (path);
251 p = (uint32_t *)rpc_add_credentials((long *)p);
253 *p++ = htonl(pathlen);
254 if (pathlen & 3) *(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)
274 printf ("%s\n", __FUNCTION__);
277 if ((NfsSrvMountPort == -1) || (!fs_mounted)) {
278 /* Nothing mounted, nothing to umount */
283 p = (uint32_t *)rpc_add_credentials ((long *)p);
285 len = (uint32_t *)p - (uint32_t *)&(data[0]);
287 rpc_req (PROG_MOUNT, MOUNT_UMOUNTALL, data, len);
290 /***************************************************************************
291 * NFS_READLINK (AH 2003-07-14)
292 * This procedure is called when read of the first block fails -
293 * this probably happens when it's a directory or a symlink
294 * In case of successful readlink(), the dirname is manipulated,
295 * so that inside the nfs() function a recursion can be done.
296 **************************************************************************/
298 nfs_readlink_req (void)
305 printf ("%s\n", __FUNCTION__);
309 p = (uint32_t *)rpc_add_credentials ((long *)p);
311 memcpy (p, filefh, NFS_FHSIZE);
312 p += (NFS_FHSIZE / 4);
314 len = (uint32_t *)p - (uint32_t *)&(data[0]);
316 rpc_req (PROG_NFS, NFS_READLINK, data, len);
319 /**************************************************************************
320 NFS_LOOKUP - Lookup Pathname
321 **************************************************************************/
323 nfs_lookup_req (char *fname)
331 printf ("%s\n", __FUNCTION__);
334 fnamelen = strlen (fname);
337 p = (uint32_t *)rpc_add_credentials ((long *)p);
339 memcpy (p, dirfh, NFS_FHSIZE);
340 p += (NFS_FHSIZE / 4);
341 *p++ = htonl(fnamelen);
342 if (fnamelen & 3) *(p + fnamelen / 4) = 0;
343 memcpy (p, fname, fnamelen);
344 p += (fnamelen + 3) / 4;
346 len = (uint32_t *)p - (uint32_t *)&(data[0]);
348 rpc_req (PROG_NFS, NFS_LOOKUP, data, len);
351 /**************************************************************************
352 NFS_READ - Read File on NFS Server
353 **************************************************************************/
355 nfs_read_req (int offset, int readlen)
362 printf ("%s\n", __FUNCTION__);
366 p = (uint32_t *)rpc_add_credentials ((long *)p);
368 memcpy (p, filefh, NFS_FHSIZE);
369 p += (NFS_FHSIZE / 4);
370 *p++ = htonl(offset);
371 *p++ = htonl(readlen);
374 len = (uint32_t *)p - (uint32_t *)&(data[0]);
376 rpc_req (PROG_NFS, NFS_READ, data, len);
379 /**************************************************************************
380 RPC request dispatcher
381 **************************************************************************/
387 printf ("%s\n", __FUNCTION__);
391 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
392 rpc_lookup_req (PROG_MOUNT, 1);
394 case STATE_PRCLOOKUP_PROG_NFS_REQ:
395 rpc_lookup_req (PROG_NFS, 2);
397 case STATE_MOUNT_REQ:
398 nfs_mount_req (nfs_path);
400 case STATE_UMOUNT_REQ:
401 nfs_umountall_req ();
403 case STATE_LOOKUP_REQ:
404 nfs_lookup_req (nfs_filename);
407 nfs_read_req (nfs_offset, nfs_len);
409 case STATE_READLINK_REQ:
415 /**************************************************************************
416 Handlers for the reply from server
417 **************************************************************************/
420 rpc_lookup_reply (int prog, uchar *pkt, unsigned len)
422 struct rpc_t rpc_pkt;
424 memcpy ((unsigned char *)&rpc_pkt, pkt, len);
427 printf ("%s\n", __FUNCTION__);
430 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
433 if (rpc_pkt.u.reply.rstatus ||
434 rpc_pkt.u.reply.verifier ||
435 rpc_pkt.u.reply.astatus) {
437 printf ("rstatus: %d\n", rpc_pkt.u.reply.rstatus);
438 printf ("verifier: %08lx\n", rpc_pkt.u.reply.verifier);
439 printf ("v2: %08lx\n", rpc_pkt.u.reply.v2);
440 printf ("astatus: %d\n", rpc_pkt.u.reply.astatus);
447 NfsSrvMountPort = ntohl(rpc_pkt.u.reply.data[0]);
450 NfsSrvNfsPort = ntohl(rpc_pkt.u.reply.data[0]);
458 nfs_mount_reply (uchar *pkt, unsigned len)
460 struct rpc_t rpc_pkt;
463 printf ("%s\n", __FUNCTION__);
466 memcpy ((unsigned char *)&rpc_pkt, pkt, len);
468 if (ntohl(rpc_pkt.u.reply.id) != rpc_id) {
470 printf ("rpc_id error. expected: %d, got: %d\n", \
471 rpc_id, ntohl(rpc_pkt.u.reply.id));
476 if (rpc_pkt.u.reply.rstatus ||
477 rpc_pkt.u.reply.verifier ||
478 rpc_pkt.u.reply.astatus ||
479 rpc_pkt.u.reply.data[0]) {
481 printf ("rstatus: %d\n", rpc_pkt.u.reply.rstatus);
482 printf ("verifier: %08lx\n", rpc_pkt.u.reply.verifier);
483 printf ("astatus: %d\n", rpc_pkt.u.reply.astatus);
484 printf ("data[0]: %08lx\n", rpc_pkt.u.reply.data[0]);
490 memcpy (dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
496 nfs_umountall_reply (uchar *pkt, unsigned len)
498 struct rpc_t rpc_pkt;
501 printf ("%s\n", __FUNCTION__);
504 memcpy ((unsigned char *)&rpc_pkt, pkt, len);
506 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
509 if (rpc_pkt.u.reply.rstatus ||
510 rpc_pkt.u.reply.verifier ||
511 rpc_pkt.u.reply.astatus) {
516 memset (dirfh, 0, sizeof(dirfh));
522 nfs_lookup_reply (uchar *pkt, unsigned len)
524 struct rpc_t rpc_pkt;
527 printf ("%s\n", __FUNCTION__);
530 memcpy ((unsigned char *)&rpc_pkt, pkt, len);
532 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
535 if (rpc_pkt.u.reply.rstatus ||
536 rpc_pkt.u.reply.verifier ||
537 rpc_pkt.u.reply.astatus ||
538 rpc_pkt.u.reply.data[0]) {
542 memcpy (filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
548 nfs_readlink_reply (uchar *pkt, unsigned len)
550 struct rpc_t rpc_pkt;
554 printf ("%s\n", __FUNCTION__);
557 memcpy ((unsigned char *)&rpc_pkt, pkt, len);
559 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
562 if (rpc_pkt.u.reply.rstatus ||
563 rpc_pkt.u.reply.verifier ||
564 rpc_pkt.u.reply.astatus ||
565 rpc_pkt.u.reply.data[0]) {
569 rlen = ntohl (rpc_pkt.u.reply.data[1]); /* new path length */
571 if (*((char *)&(rpc_pkt.u.reply.data[2])) != '/') {
573 strcat (nfs_path, "/");
574 pathlen = strlen(nfs_path);
575 memcpy (nfs_path+pathlen, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen);
576 nfs_path[pathlen+rlen+1] = 0;
578 memcpy (nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen);
585 nfs_read_reply (uchar *pkt, unsigned len)
587 struct rpc_t rpc_pkt;
591 printf ("%s\n", __FUNCTION__);
594 memcpy ((uchar *)&rpc_pkt, pkt, sizeof(rpc_pkt.u.reply));
596 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
599 if (rpc_pkt.u.reply.rstatus ||
600 rpc_pkt.u.reply.verifier ||
601 rpc_pkt.u.reply.astatus ||
602 rpc_pkt.u.reply.data[0]) {
603 if (rpc_pkt.u.reply.rstatus) {
606 if (rpc_pkt.u.reply.astatus) {
609 return -ntohl(rpc_pkt.u.reply.data[0]);;
612 if ((nfs_offset!=0) && !((nfs_offset) % (NFS_READ_SIZE/2*10*HASHES_PER_LINE))) {
615 if (!(nfs_offset % ((NFS_READ_SIZE/2)*10))) {
619 rlen = ntohl(rpc_pkt.u.reply.data[18]);
620 if ( store_block ((uchar *)pkt+sizeof(rpc_pkt.u.reply), nfs_offset, rlen) )
626 /**************************************************************************
628 **************************************************************************/
634 NetState = NETLOOP_FAIL;
639 NfsHandler (uchar *pkt, unsigned dest, unsigned src, unsigned len)
644 printf ("%s\n", __FUNCTION__);
647 if (!pkt && !dest && !src && !len) /* ARP packet */
649 if (dest != NfsOurPort) return;
652 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
653 rpc_lookup_reply (PROG_MOUNT, pkt, len);
654 NfsState = STATE_PRCLOOKUP_PROG_NFS_REQ;
658 case STATE_PRCLOOKUP_PROG_NFS_REQ:
659 rpc_lookup_reply (PROG_NFS, pkt, len);
660 NfsState = STATE_MOUNT_REQ;
664 case STATE_MOUNT_REQ:
665 if (nfs_mount_reply(pkt, len)) {
666 puts ("*** ERROR: Cannot mount\n");
667 /* just to be sure... */
668 NfsState = STATE_UMOUNT_REQ;
671 NfsState = STATE_LOOKUP_REQ;
676 case STATE_UMOUNT_REQ:
677 if (nfs_umountall_reply(pkt, len)) {
678 puts ("*** ERROR: Cannot umount\n");
679 NetState = NETLOOP_FAIL;
682 NetState = NfsDownloadState;
686 case STATE_LOOKUP_REQ:
687 if (nfs_lookup_reply(pkt, len)) {
688 puts ("*** ERROR: File lookup fail\n");
689 NfsState = STATE_UMOUNT_REQ;
692 NfsState = STATE_READ_REQ;
694 nfs_len = NFS_READ_SIZE;
699 case STATE_READLINK_REQ:
700 if (nfs_readlink_reply(pkt, len)) {
701 puts ("*** ERROR: Symlink fail\n");
702 NfsState = STATE_UMOUNT_REQ;
706 printf ("Symlink --> %s\n", nfs_path);
708 nfs_filename = basename (nfs_path);
709 nfs_path = dirname (nfs_path);
711 NfsState = STATE_MOUNT_REQ;
717 rlen = nfs_read_reply (pkt, len);
718 NetSetTimeout (NFS_TIMEOUT * CFG_HZ, NfsTimeout);
723 else if ((rlen == -NFSERR_ISDIR)||(rlen == -NFSERR_INVAL)) {
725 NfsState = STATE_READLINK_REQ;
728 if ( ! rlen ) NfsDownloadState = NETLOOP_SUCCESS;
729 NfsState = STATE_UMOUNT_REQ;
741 printf ("%s\n", __FUNCTION__);
743 NfsDownloadState = NETLOOP_FAIL;
745 NfsServerIP = NetServerIP;
746 nfs_path = (char *)nfs_path_buff;
748 if (nfs_path == NULL) {
749 NetState = NETLOOP_FAIL;
750 puts ("*** ERROR: Fail allocate memory\n");
754 if (BootFile[0] == '\0') {
755 sprintf (default_filename, "/nfsroot/%02lX%02lX%02lX%02lX.img",
757 (NetOurIP >> 8) & 0xFF,
758 (NetOurIP >> 16) & 0xFF,
759 (NetOurIP >> 24) & 0xFF );
760 strcpy (nfs_path, default_filename);
762 printf ("*** Warning: no boot file name; using '%s'\n",
770 NfsServerIP = string_to_ip (BootFile);
772 strcpy (nfs_path, p);
774 strcpy (nfs_path, BootFile);
778 nfs_filename = basename (nfs_path);
779 nfs_path = dirname (nfs_path);
781 #if defined(CONFIG_NET_MULTI)
782 printf ("Using %s device\n", eth_get_name());
785 puts ("File transfer via NFS from server "); print_IPaddr (NfsServerIP);
786 puts ("; our IP address is "); print_IPaddr (NetOurIP);
788 /* Check if we need to send across this subnet */
789 if (NetOurGatewayIP && NetOurSubnetMask) {
790 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
791 IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask;
793 if (OurNet != ServerNet) {
794 puts ("; sending through gateway ");
795 print_IPaddr (NetOurGatewayIP) ;
798 printf ("\nFilename '%s/%s'.", nfs_path, nfs_filename);
800 if (NetBootFileSize) {
801 printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
802 print_size (NetBootFileSize<<9, "");
804 printf ("\nLoad address: 0x%lx\n"
805 "Loading: *\b", load_addr);
807 NetSetTimeout (NFS_TIMEOUT * CFG_HZ, NfsTimeout);
808 NetSetHandler (NfsHandler);
811 NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
813 /*NfsOurPort = 4096 + (get_ticks() % 3072);*/
817 /* zero out server ether in case the server ip has changed */
818 memset (NetServerEther, 0, 6);
823 #endif /* CONFIG_COMMANDS & CFG_CMD_NFS */