and added files (forgot git commit -a again)
[u-boot.git] / net / nfs.c
1 /*
2  * NFS support driver - based on etherboot and U-BOOT's tftp.c
3  *
4  * Masami Komiya <mkomiya@sonare.it> 2004
5  *
6  */
7
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.  */
13
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.  */
19
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. */
24
25 #include <common.h>
26 #include <command.h>
27 #include <net.h>
28 #include <malloc.h>
29 #include "nfs.h"
30 #include "bootp.h"
31
32 #undef NFS_DEBUG
33
34 #if ((CONFIG_COMMANDS & CFG_CMD_NET) && (CONFIG_COMMANDS & CFG_CMD_NFS))
35
36 #define HASHES_PER_LINE 65      /* Number of "loading" hashes per line  */
37 #define NFS_TIMEOUT 60
38
39 static int fs_mounted = 0;
40 static unsigned long rpc_id = 0;
41 static int nfs_offset = -1;
42 static int nfs_len;
43
44 static char dirfh[NFS_FHSIZE];  /* file handle of directory */
45 static char filefh[NFS_FHSIZE]; /* file handle of kernel image */
46
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;
53 static int      NfsState;
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
61
62 static char default_filename[64];
63 static char *nfs_filename;
64 static char *nfs_path;
65 static char nfs_path_buff[2048];
66
67 static __inline__ int
68 store_block (uchar * src, unsigned offset, unsigned len)
69 {
70         ulong newsize = offset + len;
71 #ifdef CFG_DIRECT_FLASH_NFS
72         int i, rc = 0;
73
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]) {
77                         rc = 1;
78                         break;
79                 }
80         }
81
82         if (rc) { /* Flash is destination for this packet */
83                 rc = flash_write ((uchar *)src, (ulong)(load_addr+offset), len);
84                 if (rc) {
85                         flash_perror (rc);
86                         return -1;
87                 }
88         } else
89 #endif /* CFG_DIRECT_FLASH_NFS */
90         {
91                 (void)memcpy ((void *)(load_addr + offset), src, len);
92         }
93
94         if (NetBootFileXferSize < (offset+len))
95                 NetBootFileXferSize = newsize;
96         return 0;
97 }
98
99 static char*
100 basename (char *path)
101 {
102         char *fname;
103
104         fname = path + strlen(path) - 1;
105         while (fname >= path) {
106                 if (*fname == '/') {
107                         fname++;
108                         break;
109                 }
110                 fname--;
111         }
112         return fname;
113 }
114
115 static char*
116 dirname (char *path)
117 {
118         char *fname;
119
120         fname = basename (path);
121         --fname;
122         *fname = '\0';
123         return path;
124 }
125
126 /**************************************************************************
127 RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
128 **************************************************************************/
129 static long *rpc_add_credentials (long *p)
130 {
131         int hl;
132         int hostnamelen;
133         char hostname[256];
134
135         strcpy (hostname, "");
136         hostnamelen=strlen (hostname);
137
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
145          * hostname).  */
146
147         hl = (hostnamelen + 3) & ~3;
148
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 */
156         }
157         memcpy (p, hostname, hostnamelen);
158         p += hl / 4;
159         *p++ = 0;                       /* uid */
160         *p++ = 0;                       /* gid */
161         *p++ = 0;                       /* auxiliary gid list */
162
163         /* Provide an AUTH_NONE verifier.  */
164         *p++ = 0;                       /* AUTH_NONE */
165         *p++ = 0;                       /* auth length */
166
167         return p;
168 }
169
170 /**************************************************************************
171 RPC_LOOKUP - Lookup RPC Port numbers
172 **************************************************************************/
173 static void
174 rpc_req (int rpc_prog, int rpc_proc, uint32_t *data, int datalen)
175 {
176         struct rpc_t pkt;
177         unsigned long id;
178         uint32_t *p;
179         int pktlen;
180         int sport;
181
182         id = ++rpc_id;
183 #ifdef NFS_DEBUG
184         printf ("%s xid: %d, rpc_id: %d\n", __FUNCTION__, id, rpc_id);
185 #endif
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);
193
194         if (datalen)
195                 memcpy ((char *)p, (char *)data, datalen*sizeof(uint32_t));
196
197         pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt;
198
199         memcpy ((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, (char *)&pkt, pktlen);
200
201         if (rpc_prog == PROG_PORTMAP)
202                 sport = SUNRPC_PORT;
203         else if (rpc_prog == PROG_MOUNT)
204                 sport = NfsSrvMountPort;
205         else
206                 sport = NfsSrvNfsPort;
207
208         NetSendUDPPacket (NetServerEther, NfsServerIP, sport, NfsOurPort, pktlen);
209 }
210
211 /**************************************************************************
212 RPC_LOOKUP - Lookup RPC Port numbers
213 **************************************************************************/
214 static void
215 rpc_lookup_req (int prog, int ver)
216 {
217         uint32_t data[16];
218
219 #ifdef NFS_DEBUG
220         printf ("%s\n", __FUNCTION__);
221 #endif
222
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 */
228         data[7] = 0;
229
230         rpc_req (PROG_PORTMAP, PORTMAP_GETPORT, data, 8);
231 }
232
233 /**************************************************************************
234 NFS_MOUNT - Mount an NFS Filesystem
235 **************************************************************************/
236 static void
237 nfs_mount_req (char *path)
238 {
239         uint32_t data[1024];
240         uint32_t *p;
241         int len;
242         int pathlen;
243
244 #ifdef NFS_DEBUG
245         printf ("%s\n", __FUNCTION__);
246 #endif
247
248         pathlen = strlen (path);
249
250         p = &(data[0]);
251         p = (uint32_t *)rpc_add_credentials((long *)p);
252
253         *p++ = htonl(pathlen);
254         if (pathlen & 3) *(p + pathlen / 4) = 0;
255         memcpy (p, path, pathlen);
256         p += (pathlen + 3) / 4;
257
258         len = (uint32_t *)p - (uint32_t *)&(data[0]);
259
260         rpc_req (PROG_MOUNT, MOUNT_ADDENTRY, data, len);
261 }
262
263 /**************************************************************************
264 NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
265 **************************************************************************/
266 static void
267 nfs_umountall_req (void)
268 {
269         uint32_t data[1024];
270         uint32_t *p;
271         int len;
272
273 #ifdef NFS_DEBUG
274         printf ("%s\n", __FUNCTION__);
275 #endif
276
277         if ((NfsSrvMountPort == -1) || (!fs_mounted)) {
278                 /* Nothing mounted, nothing to umount */
279                 return;
280         }
281
282         p = &(data[0]);
283         p = (uint32_t *)rpc_add_credentials ((long *)p);
284
285         len = (uint32_t *)p - (uint32_t *)&(data[0]);
286
287         rpc_req (PROG_MOUNT, MOUNT_UMOUNTALL, data, len);
288 }
289
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  **************************************************************************/
297 static void
298 nfs_readlink_req (void)
299 {
300         uint32_t data[1024];
301         uint32_t *p;
302         int len;
303
304 #ifdef NFS_DEBUG
305         printf ("%s\n", __FUNCTION__);
306 #endif
307
308         p = &(data[0]);
309         p = (uint32_t *)rpc_add_credentials ((long *)p);
310
311         memcpy (p, filefh, NFS_FHSIZE);
312         p += (NFS_FHSIZE / 4);
313
314         len = (uint32_t *)p - (uint32_t *)&(data[0]);
315
316         rpc_req (PROG_NFS, NFS_READLINK, data, len);
317 }
318
319 /**************************************************************************
320 NFS_LOOKUP - Lookup Pathname
321 **************************************************************************/
322 static void
323 nfs_lookup_req (char *fname)
324 {
325         uint32_t data[1024];
326         uint32_t *p;
327         int len;
328         int fnamelen;
329
330 #ifdef NFS_DEBUG
331         printf ("%s\n", __FUNCTION__);
332 #endif
333
334         fnamelen = strlen (fname);
335
336         p = &(data[0]);
337         p = (uint32_t *)rpc_add_credentials ((long *)p);
338
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;
345
346         len = (uint32_t *)p - (uint32_t *)&(data[0]);
347
348         rpc_req (PROG_NFS, NFS_LOOKUP, data, len);
349 }
350
351 /**************************************************************************
352 NFS_READ - Read File on NFS Server
353 **************************************************************************/
354 static void
355 nfs_read_req (int offset, int readlen)
356 {
357         uint32_t data[1024];
358         uint32_t *p;
359         int len;
360
361 #ifdef NFS_DEBUG
362         printf ("%s\n", __FUNCTION__);
363 #endif
364
365         p = &(data[0]);
366         p = (uint32_t *)rpc_add_credentials ((long *)p);
367
368         memcpy (p, filefh, NFS_FHSIZE);
369         p += (NFS_FHSIZE / 4);
370         *p++ = htonl(offset);
371         *p++ = htonl(readlen);
372         *p++ = 0;
373
374         len = (uint32_t *)p - (uint32_t *)&(data[0]);
375
376         rpc_req (PROG_NFS, NFS_READ, data, len);
377 }
378
379 /**************************************************************************
380 RPC request dispatcher
381 **************************************************************************/
382
383 static void
384 NfsSend (void)
385 {
386 #ifdef NFS_DEBUG
387         printf ("%s\n", __FUNCTION__);
388 #endif
389
390         switch (NfsState) {
391         case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
392                 rpc_lookup_req (PROG_MOUNT, 1);
393                 break;
394         case STATE_PRCLOOKUP_PROG_NFS_REQ:
395                 rpc_lookup_req (PROG_NFS, 2);
396                 break;
397         case STATE_MOUNT_REQ:
398                 nfs_mount_req (nfs_path);
399                 break;
400         case STATE_UMOUNT_REQ:
401                 nfs_umountall_req ();
402                 break;
403         case STATE_LOOKUP_REQ:
404                 nfs_lookup_req (nfs_filename);
405                 break;
406         case STATE_READ_REQ:
407                 nfs_read_req (nfs_offset, nfs_len);
408                 break;
409         case STATE_READLINK_REQ:
410                 nfs_readlink_req ();
411                 break;
412         }
413 }
414
415 /**************************************************************************
416 Handlers for the reply from server
417 **************************************************************************/
418
419 static int
420 rpc_lookup_reply (int prog, uchar *pkt, unsigned len)
421 {
422         struct rpc_t rpc_pkt;
423
424         memcpy ((unsigned char *)&rpc_pkt, pkt, len);
425
426 #ifdef NFS_DEBUG
427         printf ("%s\n", __FUNCTION__);
428 #endif
429
430         if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
431                 return -1;
432
433         if (rpc_pkt.u.reply.rstatus  ||
434             rpc_pkt.u.reply.verifier ||
435             rpc_pkt.u.reply.astatus) {
436 #ifdef NFS_DEBUG
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);
441 #endif
442                 return -1;
443         }
444
445         switch (prog) {
446         case PROG_MOUNT:
447                 NfsSrvMountPort = ntohl(rpc_pkt.u.reply.data[0]);
448                 break;
449         case PROG_NFS:
450                 NfsSrvNfsPort = ntohl(rpc_pkt.u.reply.data[0]);
451                 break;
452         }
453
454         return 0;
455 }
456
457 static int
458 nfs_mount_reply (uchar *pkt, unsigned len)
459 {
460         struct rpc_t rpc_pkt;
461
462 #ifdef NFS_DEBUG
463         printf ("%s\n", __FUNCTION__);
464 #endif
465
466         memcpy ((unsigned char *)&rpc_pkt, pkt, len);
467
468         if (ntohl(rpc_pkt.u.reply.id) != rpc_id) {
469 #ifdef NFS_DEBUG
470                 printf ("rpc_id error. expected: %d, got: %d\n", \
471                         rpc_id, ntohl(rpc_pkt.u.reply.id));
472 #endif
473                 return -1;
474         }
475
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]) {
480 #ifdef NFS_DEBUG
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]);
485 #endif
486                 return -1;
487         }
488
489         fs_mounted = 1;
490         memcpy (dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
491
492         return 0;
493 }
494
495 static int
496 nfs_umountall_reply (uchar *pkt, unsigned len)
497 {
498         struct rpc_t rpc_pkt;
499
500 #ifdef NFS_DEBUG
501         printf ("%s\n", __FUNCTION__);
502 #endif
503
504         memcpy ((unsigned char *)&rpc_pkt, pkt, len);
505
506         if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
507                 return -1;
508
509         if (rpc_pkt.u.reply.rstatus  ||
510             rpc_pkt.u.reply.verifier ||
511             rpc_pkt.u.reply.astatus) {
512                 return -1;
513         }
514
515         fs_mounted = 0;
516         memset (dirfh, 0, sizeof(dirfh));
517
518         return 0;
519 }
520
521 static int
522 nfs_lookup_reply (uchar *pkt, unsigned len)
523 {
524         struct rpc_t rpc_pkt;
525
526 #ifdef NFS_DEBUG
527         printf ("%s\n", __FUNCTION__);
528 #endif
529
530         memcpy ((unsigned char *)&rpc_pkt, pkt, len);
531
532         if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
533                 return -1;
534
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]) {
539                 return -1;
540         }
541
542         memcpy (filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
543
544         return 0;
545 }
546
547 static int
548 nfs_readlink_reply (uchar *pkt, unsigned len)
549 {
550         struct rpc_t rpc_pkt;
551         int rlen;
552
553 #ifdef NFS_DEBUG
554         printf ("%s\n", __FUNCTION__);
555 #endif
556
557         memcpy ((unsigned char *)&rpc_pkt, pkt, len);
558
559         if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
560                 return -1;
561
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]) {
566                 return -1;
567         }
568
569         rlen = ntohl (rpc_pkt.u.reply.data[1]); /* new path length */
570
571         if (*((char *)&(rpc_pkt.u.reply.data[2])) != '/') {
572                 int pathlen;
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;
577         } else {
578                 memcpy (nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen);
579                 nfs_path[rlen] = 0;
580         }
581         return 0;
582 }
583
584 static int
585 nfs_read_reply (uchar *pkt, unsigned len)
586 {
587         struct rpc_t rpc_pkt;
588         int rlen;
589
590 #ifdef NFS_DEBUG
591         printf ("%s\n", __FUNCTION__);
592 #endif
593
594         memcpy ((uchar *)&rpc_pkt, pkt, sizeof(rpc_pkt.u.reply));
595
596         if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
597                 return -1;
598
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) {
604                         return -9999;
605                 }
606                 if (rpc_pkt.u.reply.astatus) {
607                         return -9999;
608                 }
609                 return -ntohl(rpc_pkt.u.reply.data[0]);;
610         }
611
612         if ((nfs_offset!=0) && !((nfs_offset) % (NFS_READ_SIZE/2*10*HASHES_PER_LINE))) {
613                 puts ("\n\t ");
614         }
615         if (!(nfs_offset % ((NFS_READ_SIZE/2)*10))) {
616                 putc ('#');
617         }
618
619         rlen = ntohl(rpc_pkt.u.reply.data[18]);
620         if ( store_block ((uchar *)pkt+sizeof(rpc_pkt.u.reply), nfs_offset, rlen) )
621                 return -9999;
622
623         return rlen;
624 }
625
626 /**************************************************************************
627 Interfaces of U-BOOT
628 **************************************************************************/
629
630 static void
631 NfsTimeout (void)
632 {
633         puts ("Timeout\n");
634         NetState = NETLOOP_FAIL;
635         return;
636 }
637
638 static void
639 NfsHandler (uchar *pkt, unsigned dest, unsigned src, unsigned len)
640 {
641         int rlen;
642
643 #ifdef NFS_DEBUG
644         printf ("%s\n", __FUNCTION__);
645 #endif
646
647         if (!pkt && !dest && !src && !len) /* ARP packet */
648                 return;
649         if (dest != NfsOurPort) return;
650
651         switch (NfsState) {
652         case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
653                 rpc_lookup_reply (PROG_MOUNT, pkt, len);
654                 NfsState = STATE_PRCLOOKUP_PROG_NFS_REQ;
655                 NfsSend ();
656                 break;
657
658         case STATE_PRCLOOKUP_PROG_NFS_REQ:
659                 rpc_lookup_reply (PROG_NFS, pkt, len);
660                 NfsState = STATE_MOUNT_REQ;
661                 NfsSend ();
662                 break;
663
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;
669                         NfsSend ();
670                 } else {
671                         NfsState = STATE_LOOKUP_REQ;
672                         NfsSend ();
673                 }
674                 break;
675
676         case STATE_UMOUNT_REQ:
677                 if (nfs_umountall_reply(pkt, len)) {
678                         puts ("*** ERROR: Cannot umount\n");
679                         NetState = NETLOOP_FAIL;
680                 } else {
681                         puts ("\ndone\n");
682                         NetState = NfsDownloadState;
683                 }
684                 break;
685
686         case STATE_LOOKUP_REQ:
687                 if (nfs_lookup_reply(pkt, len)) {
688                         puts ("*** ERROR: File lookup fail\n");
689                         NfsState = STATE_UMOUNT_REQ;
690                         NfsSend ();
691                 } else {
692                         NfsState = STATE_READ_REQ;
693                         nfs_offset = 0;
694                         nfs_len = NFS_READ_SIZE;
695                         NfsSend ();
696                 }
697                 break;
698
699         case STATE_READLINK_REQ:
700                 if (nfs_readlink_reply(pkt, len)) {
701                         puts ("*** ERROR: Symlink fail\n");
702                         NfsState = STATE_UMOUNT_REQ;
703                         NfsSend ();
704                 } else {
705 #ifdef NFS_DEBUG
706                         printf ("Symlink --> %s\n", nfs_path);
707 #endif
708                         nfs_filename = basename (nfs_path);
709                         nfs_path     = dirname (nfs_path);
710
711                         NfsState = STATE_MOUNT_REQ;
712                         NfsSend ();
713                 }
714                 break;
715
716         case STATE_READ_REQ:
717                 rlen = nfs_read_reply (pkt, len);
718                 NetSetTimeout (NFS_TIMEOUT * CFG_HZ, NfsTimeout);
719                 if (rlen > 0) {
720                         nfs_offset += rlen;
721                         NfsSend ();
722                 }
723                 else if ((rlen == -NFSERR_ISDIR)||(rlen == -NFSERR_INVAL)) {
724                         /* symbolic link */
725                         NfsState = STATE_READLINK_REQ;
726                         NfsSend ();
727                 } else {
728                         if ( ! rlen ) NfsDownloadState = NETLOOP_SUCCESS;
729                         NfsState = STATE_UMOUNT_REQ;
730                         NfsSend ();
731                 }
732                 break;
733         }
734 }
735
736
737 void
738 NfsStart (void)
739 {
740 #ifdef NFS_DEBUG
741         printf ("%s\n", __FUNCTION__);
742 #endif
743         NfsDownloadState = NETLOOP_FAIL;
744
745         NfsServerIP = NetServerIP;
746         nfs_path = (char *)nfs_path_buff;
747
748         if (nfs_path == NULL) {
749                 NetState = NETLOOP_FAIL;
750                 puts ("*** ERROR: Fail allocate memory\n");
751                 return;
752         }
753
754         if (BootFile[0] == '\0') {
755                 sprintf (default_filename, "/nfsroot/%02lX%02lX%02lX%02lX.img",
756                         NetOurIP & 0xFF,
757                         (NetOurIP >>  8) & 0xFF,
758                         (NetOurIP >> 16) & 0xFF,
759                         (NetOurIP >> 24) & 0xFF );
760                 strcpy (nfs_path, default_filename);
761
762                 printf ("*** Warning: no boot file name; using '%s'\n",
763                         nfs_path);
764         } else {
765                 char *p=BootFile;
766
767                 p = strchr (p, ':');
768
769                 if (p != NULL) {
770                         NfsServerIP = string_to_ip (BootFile);
771                         ++p;
772                         strcpy (nfs_path, p);
773                 } else {
774                         strcpy (nfs_path, BootFile);
775                 }
776         }
777
778         nfs_filename = basename (nfs_path);
779         nfs_path     = dirname (nfs_path);
780
781 #if defined(CONFIG_NET_MULTI)
782         printf ("Using %s device\n", eth_get_name());
783 #endif
784
785         puts ("File transfer via NFS from server "); print_IPaddr (NfsServerIP);
786         puts ("; our IP address is ");              print_IPaddr (NetOurIP);
787
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;
792
793                 if (OurNet != ServerNet) {
794                         puts ("; sending through gateway ");
795                         print_IPaddr (NetOurGatewayIP) ;
796                 }
797         }
798         printf ("\nFilename '%s/%s'.", nfs_path, nfs_filename);
799
800         if (NetBootFileSize) {
801                 printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
802                 print_size (NetBootFileSize<<9, "");
803         }
804         printf ("\nLoad address: 0x%lx\n"
805                 "Loading: *\b", load_addr);
806
807         NetSetTimeout (NFS_TIMEOUT * CFG_HZ, NfsTimeout);
808         NetSetHandler (NfsHandler);
809
810         NfsTimeoutCount = 0;
811         NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
812
813         /*NfsOurPort = 4096 + (get_ticks() % 3072);*/
814         /*FIX ME !!!*/
815         NfsOurPort = 1000;
816
817         /* zero out server ether in case the server ip has changed */
818         memset (NetServerEther, 0, 6);
819
820         NfsSend ();
821 }
822
823 #endif /* CONFIG_COMMANDS & CFG_CMD_NFS */