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 #if defined(CONFIG_CMD_NET) && defined(CONFIG_CMD_NFS)
34 #define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
35 #define NFS_RETRY_COUNT 30
36 #define NFS_TIMEOUT 2000UL
38 static int fs_mounted = 0;
39 static unsigned long rpc_id = 0;
40 static int nfs_offset = -1;
43 static char dirfh[NFS_FHSIZE]; /* file handle of directory */
44 static char filefh[NFS_FHSIZE]; /* file handle of kernel image */
46 static int NfsDownloadState;
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];
67 store_block (uchar * src, unsigned offset, unsigned len)
69 ulong newsize = offset + len;
70 #ifdef CONFIG_SYS_DIRECT_FLASH_NFS
73 for (i=0; i<CONFIG_SYS_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);
88 #endif /* CONFIG_SYS_DIRECT_FLASH_NFS */
90 (void)memcpy ((void *)(load_addr + offset), src, len);
93 if (NetBootFileXferSize < (offset+len))
94 NetBootFileXferSize = newsize;
103 fname = path + strlen(path) - 1;
104 while (fname >= path) {
119 fname = basename (path);
125 /**************************************************************************
126 RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
127 **************************************************************************/
128 static long *rpc_add_credentials (long *p)
134 strcpy (hostname, "");
135 hostnamelen=strlen (hostname);
137 /* Here's the executive summary on authentication requirements of the
138 * various NFS server implementations: Linux accepts both AUTH_NONE
139 * and AUTH_UNIX authentication (also accepts an empty hostname field
140 * in the AUTH_UNIX scheme). *BSD refuses AUTH_NONE, but accepts
141 * AUTH_UNIX (also accepts an empty hostname field in the AUTH_UNIX
142 * scheme). To be safe, use AUTH_UNIX and pass the hostname if we have
143 * it (if the BOOTP/DHCP reply didn't give one, just use an empty
146 hl = (hostnamelen + 3) & ~3;
148 /* Provide an AUTH_UNIX credential. */
149 *p++ = htonl(1); /* AUTH_UNIX */
150 *p++ = htonl(hl+20); /* auth length */
151 *p++ = htonl(0); /* stamp */
152 *p++ = htonl(hostnamelen); /* hostname string */
153 if (hostnamelen & 3) {
154 *(p + hostnamelen / 4) = 0; /* add zero padding */
156 memcpy (p, hostname, hostnamelen);
160 *p++ = 0; /* auxiliary gid list */
162 /* Provide an AUTH_NONE verifier. */
163 *p++ = 0; /* AUTH_NONE */
164 *p++ = 0; /* auth length */
169 /**************************************************************************
170 RPC_LOOKUP - Lookup RPC Port numbers
171 **************************************************************************/
173 rpc_req (int rpc_prog, int rpc_proc, uint32_t *data, int datalen)
182 pkt.u.call.id = htonl(id);
183 pkt.u.call.type = htonl(MSG_CALL);
184 pkt.u.call.rpcvers = htonl(2); /* use RPC version 2 */
185 pkt.u.call.prog = htonl(rpc_prog);
186 pkt.u.call.vers = htonl(2); /* portmapper is version 2 */
187 pkt.u.call.proc = htonl(rpc_proc);
188 p = (uint32_t *)&(pkt.u.call.data);
191 memcpy ((char *)p, (char *)data, datalen*sizeof(uint32_t));
193 pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt;
195 memcpy ((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, (char *)&pkt, pktlen);
197 if (rpc_prog == PROG_PORTMAP)
199 else if (rpc_prog == PROG_MOUNT)
200 sport = NfsSrvMountPort;
202 sport = NfsSrvNfsPort;
204 NetSendUDPPacket (NetServerEther, NfsServerIP, sport, NfsOurPort, pktlen);
207 /**************************************************************************
208 RPC_LOOKUP - Lookup RPC Port numbers
209 **************************************************************************/
211 rpc_lookup_req (int prog, int ver)
215 data[0] = 0; data[1] = 0; /* auth credential */
216 data[2] = 0; data[3] = 0; /* auth verifier */
217 data[4] = htonl(prog);
218 data[5] = htonl(ver);
219 data[6] = htonl(17); /* IP_UDP */
222 rpc_req (PROG_PORTMAP, PORTMAP_GETPORT, data, 8);
225 /**************************************************************************
226 NFS_MOUNT - Mount an NFS Filesystem
227 **************************************************************************/
229 nfs_mount_req (char *path)
236 pathlen = strlen (path);
239 p = (uint32_t *)rpc_add_credentials((long *)p);
241 *p++ = htonl(pathlen);
242 if (pathlen & 3) *(p + pathlen / 4) = 0;
243 memcpy (p, path, pathlen);
244 p += (pathlen + 3) / 4;
246 len = (uint32_t *)p - (uint32_t *)&(data[0]);
248 rpc_req (PROG_MOUNT, MOUNT_ADDENTRY, data, len);
251 /**************************************************************************
252 NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
253 **************************************************************************/
255 nfs_umountall_req (void)
261 if ((NfsSrvMountPort == -1) || (!fs_mounted)) {
262 /* Nothing mounted, nothing to umount */
267 p = (uint32_t *)rpc_add_credentials ((long *)p);
269 len = (uint32_t *)p - (uint32_t *)&(data[0]);
271 rpc_req (PROG_MOUNT, MOUNT_UMOUNTALL, data, len);
274 /***************************************************************************
275 * NFS_READLINK (AH 2003-07-14)
276 * This procedure is called when read of the first block fails -
277 * this probably happens when it's a directory or a symlink
278 * In case of successful readlink(), the dirname is manipulated,
279 * so that inside the nfs() function a recursion can be done.
280 **************************************************************************/
282 nfs_readlink_req (void)
289 p = (uint32_t *)rpc_add_credentials ((long *)p);
291 memcpy (p, filefh, NFS_FHSIZE);
292 p += (NFS_FHSIZE / 4);
294 len = (uint32_t *)p - (uint32_t *)&(data[0]);
296 rpc_req (PROG_NFS, NFS_READLINK, data, len);
299 /**************************************************************************
300 NFS_LOOKUP - Lookup Pathname
301 **************************************************************************/
303 nfs_lookup_req (char *fname)
310 fnamelen = strlen (fname);
313 p = (uint32_t *)rpc_add_credentials ((long *)p);
315 memcpy (p, dirfh, NFS_FHSIZE);
316 p += (NFS_FHSIZE / 4);
317 *p++ = htonl(fnamelen);
318 if (fnamelen & 3) *(p + fnamelen / 4) = 0;
319 memcpy (p, fname, fnamelen);
320 p += (fnamelen + 3) / 4;
322 len = (uint32_t *)p - (uint32_t *)&(data[0]);
324 rpc_req (PROG_NFS, NFS_LOOKUP, data, len);
327 /**************************************************************************
328 NFS_READ - Read File on NFS Server
329 **************************************************************************/
331 nfs_read_req (int offset, int readlen)
338 p = (uint32_t *)rpc_add_credentials ((long *)p);
340 memcpy (p, filefh, NFS_FHSIZE);
341 p += (NFS_FHSIZE / 4);
342 *p++ = htonl(offset);
343 *p++ = htonl(readlen);
346 len = (uint32_t *)p - (uint32_t *)&(data[0]);
348 rpc_req (PROG_NFS, NFS_READ, data, len);
351 /**************************************************************************
352 RPC request dispatcher
353 **************************************************************************/
358 debug("%s\n", __func__);
361 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
362 rpc_lookup_req (PROG_MOUNT, 1);
364 case STATE_PRCLOOKUP_PROG_NFS_REQ:
365 rpc_lookup_req (PROG_NFS, 2);
367 case STATE_MOUNT_REQ:
368 nfs_mount_req (nfs_path);
370 case STATE_UMOUNT_REQ:
371 nfs_umountall_req ();
373 case STATE_LOOKUP_REQ:
374 nfs_lookup_req (nfs_filename);
377 nfs_read_req (nfs_offset, nfs_len);
379 case STATE_READLINK_REQ:
385 /**************************************************************************
386 Handlers for the reply from server
387 **************************************************************************/
390 rpc_lookup_reply (int prog, uchar *pkt, unsigned len)
392 struct rpc_t rpc_pkt;
394 memcpy ((unsigned char *)&rpc_pkt, pkt, len);
396 debug("%s\n", __func__);
398 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
401 if (rpc_pkt.u.reply.rstatus ||
402 rpc_pkt.u.reply.verifier ||
403 rpc_pkt.u.reply.astatus) {
409 NfsSrvMountPort = ntohl(rpc_pkt.u.reply.data[0]);
412 NfsSrvNfsPort = ntohl(rpc_pkt.u.reply.data[0]);
420 nfs_mount_reply (uchar *pkt, unsigned len)
422 struct rpc_t rpc_pkt;
424 debug("%s\n", __func__);
426 memcpy ((unsigned char *)&rpc_pkt, pkt, len);
428 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
431 if (rpc_pkt.u.reply.rstatus ||
432 rpc_pkt.u.reply.verifier ||
433 rpc_pkt.u.reply.astatus ||
434 rpc_pkt.u.reply.data[0]) {
439 memcpy (dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
445 nfs_umountall_reply (uchar *pkt, unsigned len)
447 struct rpc_t rpc_pkt;
449 debug("%s\n", __func__);
451 memcpy ((unsigned char *)&rpc_pkt, pkt, len);
453 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
456 if (rpc_pkt.u.reply.rstatus ||
457 rpc_pkt.u.reply.verifier ||
458 rpc_pkt.u.reply.astatus) {
463 memset (dirfh, 0, sizeof(dirfh));
469 nfs_lookup_reply (uchar *pkt, unsigned len)
471 struct rpc_t rpc_pkt;
473 debug("%s\n", __func__);
475 memcpy ((unsigned char *)&rpc_pkt, pkt, len);
477 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
480 if (rpc_pkt.u.reply.rstatus ||
481 rpc_pkt.u.reply.verifier ||
482 rpc_pkt.u.reply.astatus ||
483 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]) {
512 rlen = ntohl (rpc_pkt.u.reply.data[1]); /* new path length */
514 if (*((char *)&(rpc_pkt.u.reply.data[2])) != '/') {
516 strcat (nfs_path, "/");
517 pathlen = strlen(nfs_path);
518 memcpy (nfs_path+pathlen, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen);
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) {
547 if (rpc_pkt.u.reply.astatus) {
550 return -ntohl(rpc_pkt.u.reply.data[0]);;
553 if ((nfs_offset!=0) && !((nfs_offset) % (NFS_READ_SIZE/2*10*HASHES_PER_LINE))) {
556 if (!(nfs_offset % ((NFS_READ_SIZE/2)*10))) {
560 rlen = ntohl(rpc_pkt.u.reply.data[18]);
561 if ( store_block ((uchar *)pkt+sizeof(rpc_pkt.u.reply), nfs_offset, rlen) )
567 /**************************************************************************
569 **************************************************************************/
574 if ( NfsTimeoutCount++ < NFS_RETRY_COUNT ) {
579 NetState = NETLOOP_FAIL;
584 NfsHandler (uchar *pkt, unsigned dest, unsigned src, unsigned len)
588 debug("%s\n", __func__);
590 if (dest != NfsOurPort) return;
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 NetState = NETLOOP_FAIL;
623 NetState = NfsDownloadState;
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);
662 else if ((rlen == -NFSERR_ISDIR)||(rlen == -NFSERR_INVAL)) {
664 NfsState = STATE_READLINK_REQ;
667 if ( ! rlen ) NfsDownloadState = NETLOOP_SUCCESS;
668 NfsState = STATE_UMOUNT_REQ;
679 debug("%s\n", __func__);
680 NfsDownloadState = NETLOOP_FAIL;
682 NfsServerIP = NetServerIP;
683 nfs_path = (char *)nfs_path_buff;
685 if (nfs_path == NULL) {
686 NetState = NETLOOP_FAIL;
687 puts ("*** ERROR: Fail allocate memory\n");
691 if (BootFile[0] == '\0') {
692 sprintf (default_filename, "/nfsroot/%02lX%02lX%02lX%02lX.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);
709 strcpy (nfs_path, p);
711 strcpy (nfs_path, BootFile);
715 nfs_filename = basename (nfs_path);
716 nfs_path = dirname (nfs_path);
718 #if defined(CONFIG_NET_MULTI)
719 printf ("Using %s device\n", eth_get_name());
722 printf("File transfer via NFS from server %pI4"
723 "; our IP address is %pI4", &NfsServerIP, &NetOurIP);
725 /* Check if we need to send across this subnet */
726 if (NetOurGatewayIP && NetOurSubnetMask) {
727 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
728 IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask;
730 if (OurNet != ServerNet)
731 printf("; sending through gateway %pI4", &NetOurGatewayIP);
733 printf ("\nFilename '%s/%s'.", nfs_path, nfs_filename);
735 if (NetBootFileSize) {
736 printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
737 print_size (NetBootFileSize<<9, "");
739 printf ("\nLoad address: 0x%lx\n"
740 "Loading: *\b", load_addr);
742 NetSetTimeout (NFS_TIMEOUT, NfsTimeout);
743 NetSetHandler (NfsHandler);
746 NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
748 /*NfsOurPort = 4096 + (get_ticks() % 3072);*/
752 /* zero out server ether in case the server ip has changed */
753 memset (NetServerEther, 0, 6);