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_RETRY_COUNT 30
38 #define NFS_TIMEOUT 2000UL
40 static int fs_mounted = 0;
41 static unsigned long rpc_id = 0;
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 int NfsDownloadState;
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;
101 basename (char *path)
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 */
155 if (hostnamelen & 3) {
156 *(p + hostnamelen / 4) = 0; /* add zero padding */
158 memcpy (p, hostname, hostnamelen);
162 *p++ = 0; /* auxiliary gid list */
164 /* Provide an AUTH_NONE verifier. */
165 *p++ = 0; /* AUTH_NONE */
166 *p++ = 0; /* auth length */
171 /**************************************************************************
172 RPC_LOOKUP - Lookup RPC Port numbers
173 **************************************************************************/
175 rpc_req (int rpc_prog, int rpc_proc, uint32_t *data, int datalen)
184 pkt.u.call.id = htonl(id);
185 pkt.u.call.type = htonl(MSG_CALL);
186 pkt.u.call.rpcvers = htonl(2); /* use RPC version 2 */
187 pkt.u.call.prog = htonl(rpc_prog);
188 pkt.u.call.vers = htonl(2); /* portmapper is version 2 */
189 pkt.u.call.proc = htonl(rpc_proc);
190 p = (uint32_t *)&(pkt.u.call.data);
193 memcpy ((char *)p, (char *)data, datalen*sizeof(uint32_t));
195 pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt;
197 memcpy ((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, (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, pktlen);
209 /**************************************************************************
210 RPC_LOOKUP - Lookup RPC Port numbers
211 **************************************************************************/
213 rpc_lookup_req (int prog, int ver)
217 data[0] = 0; data[1] = 0; /* auth credential */
218 data[2] = 0; data[3] = 0; /* auth verifier */
219 data[4] = htonl(prog);
220 data[5] = htonl(ver);
221 data[6] = htonl(17); /* IP_UDP */
224 rpc_req (PROG_PORTMAP, PORTMAP_GETPORT, data, 8);
227 /**************************************************************************
228 NFS_MOUNT - Mount an NFS Filesystem
229 **************************************************************************/
231 nfs_mount_req (char *path)
238 pathlen = strlen (path);
241 p = (uint32_t *)rpc_add_credentials((long *)p);
243 *p++ = htonl(pathlen);
244 if (pathlen & 3) *(p + pathlen / 4) = 0;
245 memcpy (p, path, pathlen);
246 p += (pathlen + 3) / 4;
248 len = (uint32_t *)p - (uint32_t *)&(data[0]);
250 rpc_req (PROG_MOUNT, MOUNT_ADDENTRY, data, len);
253 /**************************************************************************
254 NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
255 **************************************************************************/
257 nfs_umountall_req (void)
263 if ((NfsSrvMountPort == -1) || (!fs_mounted)) {
264 /* Nothing mounted, nothing to umount */
269 p = (uint32_t *)rpc_add_credentials ((long *)p);
271 len = (uint32_t *)p - (uint32_t *)&(data[0]);
273 rpc_req (PROG_MOUNT, MOUNT_UMOUNTALL, data, len);
276 /***************************************************************************
277 * NFS_READLINK (AH 2003-07-14)
278 * This procedure is called when read of the first block fails -
279 * this probably happens when it's a directory or a symlink
280 * In case of successful readlink(), the dirname is manipulated,
281 * so that inside the nfs() function a recursion can be done.
282 **************************************************************************/
284 nfs_readlink_req (void)
291 p = (uint32_t *)rpc_add_credentials ((long *)p);
293 memcpy (p, filefh, NFS_FHSIZE);
294 p += (NFS_FHSIZE / 4);
296 len = (uint32_t *)p - (uint32_t *)&(data[0]);
298 rpc_req (PROG_NFS, NFS_READLINK, data, len);
301 /**************************************************************************
302 NFS_LOOKUP - Lookup Pathname
303 **************************************************************************/
305 nfs_lookup_req (char *fname)
312 fnamelen = strlen (fname);
315 p = (uint32_t *)rpc_add_credentials ((long *)p);
317 memcpy (p, dirfh, NFS_FHSIZE);
318 p += (NFS_FHSIZE / 4);
319 *p++ = htonl(fnamelen);
320 if (fnamelen & 3) *(p + fnamelen / 4) = 0;
321 memcpy (p, fname, fnamelen);
322 p += (fnamelen + 3) / 4;
324 len = (uint32_t *)p - (uint32_t *)&(data[0]);
326 rpc_req (PROG_NFS, NFS_LOOKUP, data, len);
329 /**************************************************************************
330 NFS_READ - Read File on NFS Server
331 **************************************************************************/
333 nfs_read_req (int offset, int readlen)
340 p = (uint32_t *)rpc_add_credentials ((long *)p);
342 memcpy (p, filefh, NFS_FHSIZE);
343 p += (NFS_FHSIZE / 4);
344 *p++ = htonl(offset);
345 *p++ = htonl(readlen);
348 len = (uint32_t *)p - (uint32_t *)&(data[0]);
350 rpc_req (PROG_NFS, NFS_READ, data, len);
353 /**************************************************************************
354 RPC request dispatcher
355 **************************************************************************/
361 printf ("%s\n", __FUNCTION__);
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:
375 nfs_umountall_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);
401 printf ("%s\n", __FUNCTION__);
404 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
407 if (rpc_pkt.u.reply.rstatus ||
408 rpc_pkt.u.reply.verifier ||
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 **************************************************************************/
590 if ( NfsTimeoutCount++ < NFS_RETRY_COUNT ) {
595 NetState = NETLOOP_FAIL;
600 NfsHandler (uchar *pkt, unsigned dest, unsigned src, unsigned len)
605 printf ("%s\n", __FUNCTION__);
608 if (dest != NfsOurPort) return;
611 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
612 rpc_lookup_reply (PROG_MOUNT, pkt, len);
613 NfsState = STATE_PRCLOOKUP_PROG_NFS_REQ;
617 case STATE_PRCLOOKUP_PROG_NFS_REQ:
618 rpc_lookup_reply (PROG_NFS, pkt, len);
619 NfsState = STATE_MOUNT_REQ;
623 case STATE_MOUNT_REQ:
624 if (nfs_mount_reply(pkt, len)) {
625 puts ("*** ERROR: Cannot mount\n");
626 /* just to be sure... */
627 NfsState = STATE_UMOUNT_REQ;
630 NfsState = STATE_LOOKUP_REQ;
635 case STATE_UMOUNT_REQ:
636 if (nfs_umountall_reply(pkt, len)) {
637 puts ("*** ERROR: Cannot umount\n");
638 NetState = NETLOOP_FAIL;
641 NetState = NfsDownloadState;
645 case STATE_LOOKUP_REQ:
646 if (nfs_lookup_reply(pkt, len)) {
647 puts ("*** ERROR: File lookup fail\n");
648 NfsState = STATE_UMOUNT_REQ;
651 NfsState = STATE_READ_REQ;
653 nfs_len = NFS_READ_SIZE;
658 case STATE_READLINK_REQ:
659 if (nfs_readlink_reply(pkt, len)) {
660 puts ("*** ERROR: Symlink fail\n");
661 NfsState = STATE_UMOUNT_REQ;
665 printf ("Symlink --> %s\n", nfs_path);
667 nfs_filename = basename (nfs_path);
668 nfs_path = dirname (nfs_path);
670 NfsState = STATE_MOUNT_REQ;
676 rlen = nfs_read_reply (pkt, len);
677 NetSetTimeout (NFS_TIMEOUT, NfsTimeout);
682 else if ((rlen == -NFSERR_ISDIR)||(rlen == -NFSERR_INVAL)) {
684 NfsState = STATE_READLINK_REQ;
687 if ( ! rlen ) NfsDownloadState = NETLOOP_SUCCESS;
688 NfsState = STATE_UMOUNT_REQ;
700 printf ("%s\n", __FUNCTION__);
702 NfsDownloadState = NETLOOP_FAIL;
704 NfsServerIP = NetServerIP;
705 nfs_path = (char *)nfs_path_buff;
707 if (nfs_path == NULL) {
708 NetState = NETLOOP_FAIL;
709 puts ("*** ERROR: Fail allocate memory\n");
713 if (BootFile[0] == '\0') {
714 sprintf (default_filename, "/nfsroot/%02lX%02lX%02lX%02lX.img",
716 (NetOurIP >> 8) & 0xFF,
717 (NetOurIP >> 16) & 0xFF,
718 (NetOurIP >> 24) & 0xFF );
719 strcpy (nfs_path, default_filename);
721 printf ("*** Warning: no boot file name; using '%s'\n",
729 NfsServerIP = string_to_ip (BootFile);
731 strcpy (nfs_path, p);
733 strcpy (nfs_path, BootFile);
737 nfs_filename = basename (nfs_path);
738 nfs_path = dirname (nfs_path);
740 #if defined(CONFIG_NET_MULTI)
741 printf ("Using %s device\n", eth_get_name());
744 printf("File transfer via NFS from server %pI4"
745 "; our IP address is %pI4", &NfsServerIP, &NetOurIP);
747 /* Check if we need to send across this subnet */
748 if (NetOurGatewayIP && NetOurSubnetMask) {
749 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
750 IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask;
752 if (OurNet != ServerNet)
753 printf("; sending through gateway %pI4", &NetOurGatewayIP);
755 printf ("\nFilename '%s/%s'.", nfs_path, nfs_filename);
757 if (NetBootFileSize) {
758 printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
759 print_size (NetBootFileSize<<9, "");
761 printf ("\nLoad address: 0x%lx\n"
762 "Loading: *\b", load_addr);
764 NetSetTimeout (NFS_TIMEOUT, NfsTimeout);
765 NetSetHandler (NfsHandler);
768 NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
770 /*NfsOurPort = 4096 + (get_ticks() % 3072);*/
774 /* zero out server ether in case the server ip has changed */
775 memset (NetServerEther, 0, 6);