blob: de789e1f848d7f14d95f8badbd6976ca9341d136 [file] [log] [blame]
wdenkcbd8a352004-02-24 02:00:03 +00001/*
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/*#define 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 */
wdenke94d2cd2004-06-30 22:59:18 +000037#define NFS_TIMEOUT 60
wdenkcbd8a352004-02-24 02:00:03 +000038
39static int fs_mounted = 0;
wdenkc3f9d492004-03-14 00:59:59 +000040static unsigned long rpc_id = 0;
wdenkcbd8a352004-02-24 02:00:03 +000041static int nfs_offset = -1;
42static int nfs_len;
43
44static char dirfh[NFS_FHSIZE]; /* file handle of directory */
45static char filefh[NFS_FHSIZE]; /* file handle of kernel image */
46
Wolfgang Denk23a7a322005-08-06 01:11:12 +020047static int NfsDownloadState;
wdenkcbd8a352004-02-24 02:00:03 +000048static IPaddr_t NfsServerIP;
49static int NfsSrvMountPort;
50static int NfsSrvNfsPort;
51static int NfsOurPort;
52static int NfsTimeoutCount;
53static 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
62static char default_filename[64];
63static char *nfs_filename;
64static char *nfs_path;
65static char nfs_path_buff[2048];
66
Wolfgang Denk23a7a322005-08-06 01:11:12 +020067static __inline__ int
wdenkcbd8a352004-02-24 02:00:03 +000068store_block (uchar * src, unsigned offset, unsigned len)
69{
wdenka084f7d2004-02-24 22:33:21 +000070 ulong newsize = offset + len;
wdenkcbd8a352004-02-24 02:00:03 +000071#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);
Wolfgang Denk23a7a322005-08-06 01:11:12 +020086 return -1;
wdenkcbd8a352004-02-24 02:00:03 +000087 }
88 } else
89#endif /* CFG_DIRECT_FLASH_NFS */
wdenkcbd8a352004-02-24 02:00:03 +000090 {
91 (void)memcpy ((void *)(load_addr + offset), src, len);
92 }
wdenka084f7d2004-02-24 22:33:21 +000093
94 if (NetBootFileXferSize < (offset+len))
95 NetBootFileXferSize = newsize;
Wolfgang Denk23a7a322005-08-06 01:11:12 +020096 return 0;
wdenkcbd8a352004-02-24 02:00:03 +000097}
98
99static char*
100basename (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
115static char*
116dirname (char *path)
117{
118 char *fname;
119
120 fname = basename (path);
121 --fname;
122 *fname = '\0';
123 return path;
124}
125
126/**************************************************************************
wdenkcbd8a352004-02-24 02:00:03 +0000127RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
128**************************************************************************/
129static 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/**************************************************************************
171RPC_LOOKUP - Lookup RPC Port numbers
172**************************************************************************/
173static void
174rpc_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
wdenkc3f9d492004-03-14 00:59:59 +0000182 id = ++rpc_id;
wdenkcbd8a352004-02-24 02:00:03 +0000183 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);
190
191 if (datalen)
192 memcpy ((char *)p, (char *)data, datalen*sizeof(uint32_t));
193
194 pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt;
195
wdenka3d991b2004-04-15 21:48:45 +0000196 memcpy ((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, (char *)&pkt, pktlen);
wdenkcbd8a352004-02-24 02:00:03 +0000197
198 if (rpc_prog == PROG_PORTMAP)
199 sport = SUNRPC_PORT;
200 else if (rpc_prog == PROG_MOUNT)
201 sport = NfsSrvMountPort;
202 else
203 sport = NfsSrvNfsPort;
204
205 NetSendUDPPacket (NetServerEther, NfsServerIP, sport, NfsOurPort, pktlen);
206}
207
208/**************************************************************************
209RPC_LOOKUP - Lookup RPC Port numbers
210**************************************************************************/
211static void
212rpc_lookup_req (int prog, int ver)
213{
214 uint32_t data[16];
215
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 */
221 data[7] = 0;
222
223 rpc_req (PROG_PORTMAP, PORTMAP_GETPORT, data, 8);
224}
225
226/**************************************************************************
227NFS_MOUNT - Mount an NFS Filesystem
228**************************************************************************/
229static void
230nfs_mount_req (char *path)
231{
232 uint32_t data[1024];
233 uint32_t *p;
234 int len;
235 int pathlen;
236
237 pathlen = strlen (path);
238
239 p = &(data[0]);
240 p = (uint32_t *)rpc_add_credentials((long *)p);
241
242 *p++ = htonl(pathlen);
243 if (pathlen & 3) *(p + pathlen / 4) = 0;
244 memcpy (p, path, pathlen);
245 p += (pathlen + 3) / 4;
246
247 len = (uint32_t *)p - (uint32_t *)&(data[0]);
248
249 rpc_req (PROG_MOUNT, MOUNT_ADDENTRY, data, len);
250}
251
252/**************************************************************************
253NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
254**************************************************************************/
255static void
256nfs_umountall_req (void)
257{
258 uint32_t data[1024];
259 uint32_t *p;
260 int len;
261
262 if ((NfsSrvMountPort == -1) || (!fs_mounted)) {
263 /* Nothing mounted, nothing to umount */
264 return;
265 }
266
267 p = &(data[0]);
268 p = (uint32_t *)rpc_add_credentials ((long *)p);
269
270 len = (uint32_t *)p - (uint32_t *)&(data[0]);
271
272 rpc_req (PROG_MOUNT, MOUNT_UMOUNTALL, data, len);
273}
274
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 **************************************************************************/
282static void
283nfs_readlink_req (void)
284{
285 uint32_t data[1024];
286 uint32_t *p;
287 int len;
288
289 p = &(data[0]);
290 p = (uint32_t *)rpc_add_credentials ((long *)p);
291
292 memcpy (p, filefh, NFS_FHSIZE);
293 p += (NFS_FHSIZE / 4);
294
295 len = (uint32_t *)p - (uint32_t *)&(data[0]);
296
297 rpc_req (PROG_NFS, NFS_READLINK, data, len);
298}
299
300/**************************************************************************
301NFS_LOOKUP - Lookup Pathname
302**************************************************************************/
303static void
304nfs_lookup_req (char *fname)
305{
306 uint32_t data[1024];
307 uint32_t *p;
308 int len;
309 int fnamelen;
310
311 fnamelen = strlen (fname);
312
313 p = &(data[0]);
314 p = (uint32_t *)rpc_add_credentials ((long *)p);
315
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;
322
323 len = (uint32_t *)p - (uint32_t *)&(data[0]);
324
325 rpc_req (PROG_NFS, NFS_LOOKUP, data, len);
326}
327
328/**************************************************************************
329NFS_READ - Read File on NFS Server
330**************************************************************************/
331static void
332nfs_read_req (int offset, int readlen)
333{
334 uint32_t data[1024];
335 uint32_t *p;
336 int len;
337
338 p = &(data[0]);
339 p = (uint32_t *)rpc_add_credentials ((long *)p);
340
341 memcpy (p, filefh, NFS_FHSIZE);
342 p += (NFS_FHSIZE / 4);
343 *p++ = htonl(offset);
344 *p++ = htonl(readlen);
345 *p++ = 0;
346
347 len = (uint32_t *)p - (uint32_t *)&(data[0]);
348
349 rpc_req (PROG_NFS, NFS_READ, data, len);
350}
351
352/**************************************************************************
353RPC request dispatcher
354**************************************************************************/
355
356static void
357NfsSend (void)
358{
359#ifdef NFS_DEBUG
360 printf ("%s\n", __FUNCTION__);
361#endif
362
363 switch (NfsState) {
364 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
365 rpc_lookup_req (PROG_MOUNT, 1);
366 break;
367 case STATE_PRCLOOKUP_PROG_NFS_REQ:
368 rpc_lookup_req (PROG_NFS, 2);
369 break;
370 case STATE_MOUNT_REQ:
371 nfs_mount_req (nfs_path);
372 break;
373 case STATE_UMOUNT_REQ:
374 nfs_umountall_req ();
375 break;
376 case STATE_LOOKUP_REQ:
377 nfs_lookup_req (nfs_filename);
378 break;
379 case STATE_READ_REQ:
380 nfs_read_req (nfs_offset, nfs_len);
381 break;
382 case STATE_READLINK_REQ:
383 nfs_readlink_req ();
384 break;
385 }
386}
387
388/**************************************************************************
389Handlers for the reply from server
390**************************************************************************/
391
392static int
393rpc_lookup_reply (int prog, uchar *pkt, unsigned len)
394{
395 struct rpc_t rpc_pkt;
396
397 memcpy ((unsigned char *)&rpc_pkt, pkt, len);
398
399#ifdef NFS_DEBUG
400 printf ("%s\n", __FUNCTION__);
401#endif
402
wdenkc3f9d492004-03-14 00:59:59 +0000403 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
404 return -1;
405
wdenkcbd8a352004-02-24 02:00:03 +0000406 if (rpc_pkt.u.reply.rstatus ||
407 rpc_pkt.u.reply.verifier ||
408 rpc_pkt.u.reply.astatus ||
409 rpc_pkt.u.reply.astatus) {
wdenkc3f9d492004-03-14 00:59:59 +0000410 return -1;
wdenkcbd8a352004-02-24 02:00:03 +0000411 }
412
413 switch (prog) {
414 case PROG_MOUNT:
415 NfsSrvMountPort = ntohl(rpc_pkt.u.reply.data[0]);
416 break;
417 case PROG_NFS:
418 NfsSrvNfsPort = ntohl(rpc_pkt.u.reply.data[0]);
419 break;
420 }
421
422 return 0;
423}
424
425static int
426nfs_mount_reply (uchar *pkt, unsigned len)
427{
428 struct rpc_t rpc_pkt;
429
430#ifdef NFS_DEBUG
431 printf ("%s\n", __FUNCTION__);
432#endif
433
434 memcpy ((unsigned char *)&rpc_pkt, pkt, len);
435
wdenkc3f9d492004-03-14 00:59:59 +0000436 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
437 return -1;
438
wdenkcbd8a352004-02-24 02:00:03 +0000439 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]) {
443 return -1;
444 }
445
446 fs_mounted = 1;
447 memcpy (dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
448
449 return 0;
450}
451
452static int
453nfs_umountall_reply (uchar *pkt, unsigned len)
454{
455 struct rpc_t rpc_pkt;
456
457#ifdef NFS_DEBUG
458 printf ("%s\n", __FUNCTION__);
459#endif
460
461 memcpy ((unsigned char *)&rpc_pkt, pkt, len);
462
wdenkc3f9d492004-03-14 00:59:59 +0000463 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
464 return -1;
465
wdenkcbd8a352004-02-24 02:00:03 +0000466 if (rpc_pkt.u.reply.rstatus ||
467 rpc_pkt.u.reply.verifier ||
468 rpc_pkt.u.reply.astatus) {
469 return -1;
470 }
471
472 fs_mounted = 0;
473 memset (dirfh, 0, sizeof(dirfh));
474
475 return 0;
476}
477
478static int
479nfs_lookup_reply (uchar *pkt, unsigned len)
480{
481 struct rpc_t rpc_pkt;
482
483#ifdef NFS_DEBUG
484 printf ("%s\n", __FUNCTION__);
485#endif
486
487 memcpy ((unsigned char *)&rpc_pkt, pkt, len);
488
wdenkc3f9d492004-03-14 00:59:59 +0000489 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
490 return -1;
491
wdenkcbd8a352004-02-24 02:00:03 +0000492 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]) {
496 return -1;
497 }
498
499 memcpy (filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
500
501 return 0;
502}
503
504static int
505nfs_readlink_reply (uchar *pkt, unsigned len)
506{
507 struct rpc_t rpc_pkt;
508 int rlen;
509
510#ifdef NFS_DEBUG
511 printf ("%s\n", __FUNCTION__);
512#endif
513
514 memcpy ((unsigned char *)&rpc_pkt, pkt, len);
515
wdenkc3f9d492004-03-14 00:59:59 +0000516 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
517 return -1;
518
wdenkcbd8a352004-02-24 02:00:03 +0000519 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]) {
523 return -1;
524 }
525
526 rlen = ntohl (rpc_pkt.u.reply.data[1]); /* new path length */
527
528 if (*((char *)&(rpc_pkt.u.reply.data[2])) != '/') {
529 int pathlen;
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;
534 } else {
535 memcpy (nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen);
536 nfs_path[rlen] = 0;
537 }
538 return 0;
539}
540
541static int
542nfs_read_reply (uchar *pkt, unsigned len)
543{
544 struct rpc_t rpc_pkt;
545 int rlen;
546
547#ifdef NFS_DEBUG_nop
548 printf ("%s\n", __FUNCTION__);
549#endif
550
wdenk11dadd52004-02-27 00:07:27 +0000551 memcpy ((uchar *)&rpc_pkt, pkt, sizeof(rpc_pkt.u.reply));
wdenkcbd8a352004-02-24 02:00:03 +0000552
wdenkc3f9d492004-03-14 00:59:59 +0000553 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
554 return -1;
555
wdenkcbd8a352004-02-24 02:00:03 +0000556 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) {
561 return -9999;
562 }
563 if (rpc_pkt.u.reply.astatus) {
564 return -9999;
565 }
566 return -ntohl(rpc_pkt.u.reply.data[0]);;
567 }
568
569 if ((nfs_offset!=0) && !((nfs_offset) % (NFS_READ_SIZE/2*10*HASHES_PER_LINE))) {
570 puts ("\n\t ");
571 }
572 if (!(nfs_offset % ((NFS_READ_SIZE/2)*10))) {
573 putc ('#');
574 }
575
576 rlen = ntohl(rpc_pkt.u.reply.data[18]);
Wolfgang Denk23a7a322005-08-06 01:11:12 +0200577 if ( store_block ((uchar *)pkt+sizeof(rpc_pkt.u.reply), nfs_offset, rlen) )
578 return -9999;
wdenkcbd8a352004-02-24 02:00:03 +0000579
580 return rlen;
581}
582
583/**************************************************************************
584Interfaces of U-BOOT
585**************************************************************************/
586
587static void
wdenka5725fa2004-09-28 21:51:42 +0000588NfsTimeout (void)
589{
590 puts ("Timeout\n");
591 NetState = NETLOOP_FAIL;
592 return;
593}
594
595static void
wdenkcbd8a352004-02-24 02:00:03 +0000596NfsHandler (uchar *pkt, unsigned dest, unsigned src, unsigned len)
597{
598 int rlen;
599
600#ifdef NFS_DEBUG
601 printf ("%s\n", __FUNCTION__);
602#endif
603
604 if (dest != NfsOurPort) return;
605
606 switch (NfsState) {
607 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
608 rpc_lookup_reply (PROG_MOUNT, pkt, len);
609 NfsState = STATE_PRCLOOKUP_PROG_NFS_REQ;
610 NfsSend ();
611 break;
612
613 case STATE_PRCLOOKUP_PROG_NFS_REQ:
614 rpc_lookup_reply (PROG_NFS, pkt, len);
615 NfsState = STATE_MOUNT_REQ;
616 NfsSend ();
617 break;
618
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;
624 NfsSend ();
625 } else {
626 NfsState = STATE_LOOKUP_REQ;
627 NfsSend ();
628 }
629 break;
630
631 case STATE_UMOUNT_REQ:
632 if (nfs_umountall_reply(pkt, len)) {
633 puts ("*** ERROR: Cannot umount\n");
634 NetState = NETLOOP_FAIL;
635 } else {
wdenka084f7d2004-02-24 22:33:21 +0000636 puts ("\ndone\n");
Wolfgang Denk23a7a322005-08-06 01:11:12 +0200637 NetState = NfsDownloadState;
wdenkcbd8a352004-02-24 02:00:03 +0000638 }
639 break;
640
641 case STATE_LOOKUP_REQ:
642 if (nfs_lookup_reply(pkt, len)) {
643 puts ("*** ERROR: File lookup fail\n");
644 NfsState = STATE_UMOUNT_REQ;
645 NfsSend ();
646 } else {
647 NfsState = STATE_READ_REQ;
648 nfs_offset = 0;
649 nfs_len = NFS_READ_SIZE;
650 NfsSend ();
651 }
652 break;
653
654 case STATE_READLINK_REQ:
655 if (nfs_readlink_reply(pkt, len)) {
656 puts ("*** ERROR: Symlink fail\n");
657 NfsState = STATE_UMOUNT_REQ;
658 NfsSend ();
659 } else {
660#ifdef NFS_DEBUG
661 printf ("Symlink --> %s\n", nfs_path);
662#endif
663 nfs_filename = basename (nfs_path);
664 nfs_path = dirname (nfs_path);
665
666 NfsState = STATE_MOUNT_REQ;
667 NfsSend ();
668 }
669 break;
670
671 case STATE_READ_REQ:
672 rlen = nfs_read_reply (pkt, len);
wdenka5725fa2004-09-28 21:51:42 +0000673 NetSetTimeout (NFS_TIMEOUT * CFG_HZ, NfsTimeout);
wdenkcbd8a352004-02-24 02:00:03 +0000674 if (rlen > 0) {
675 nfs_offset += rlen;
676 NfsSend ();
677 }
678 else if ((rlen == -NFSERR_ISDIR)||(rlen == -NFSERR_INVAL)) {
679 /* symbolic link */
680 NfsState = STATE_READLINK_REQ;
681 NfsSend ();
682 } else {
Wolfgang Denk23a7a322005-08-06 01:11:12 +0200683 if ( ! rlen ) NfsDownloadState = NETLOOP_SUCCESS;
wdenkcbd8a352004-02-24 02:00:03 +0000684 NfsState = STATE_UMOUNT_REQ;
685 NfsSend ();
686 }
687 break;
688 }
689}
690
wdenkcbd8a352004-02-24 02:00:03 +0000691
692void
693NfsStart (void)
694{
695#ifdef NFS_DEBUG
696 printf ("%s\n", __FUNCTION__);
697#endif
Wolfgang Denk23a7a322005-08-06 01:11:12 +0200698 NfsDownloadState = NETLOOP_FAIL;
wdenkcbd8a352004-02-24 02:00:03 +0000699
700 NfsServerIP = NetServerIP;
701 nfs_path = (char *)nfs_path_buff;
702
703 if (nfs_path == NULL) {
704 NetState = NETLOOP_FAIL;
705 puts ("*** ERROR: Fail allocate memory\n");
706 return;
707 }
708
709 if (BootFile[0] == '\0') {
wdenkcbd8a352004-02-24 02:00:03 +0000710 sprintf (default_filename, "/nfsroot/%02lX%02lX%02lX%02lX.img",
Wolfgang Denkc43352c2005-08-04 01:09:44 +0200711 NetOurIP & 0xFF,
712 (NetOurIP >> 8) & 0xFF,
713 (NetOurIP >> 16) & 0xFF,
714 (NetOurIP >> 24) & 0xFF );
wdenkcbd8a352004-02-24 02:00:03 +0000715 strcpy (nfs_path, default_filename);
716
717 printf ("*** Warning: no boot file name; using '%s'\n",
718 nfs_path);
719 } else {
720 char *p=BootFile;
721
722 p = strchr (p, ':');
723
724 if (p != NULL) {
725 NfsServerIP = string_to_ip (BootFile);
726 ++p;
727 strcpy (nfs_path, p);
728 } else {
729 strcpy (nfs_path, BootFile);
730 }
731 }
732
733 nfs_filename = basename (nfs_path);
734 nfs_path = dirname (nfs_path);
735
736#if defined(CONFIG_NET_MULTI)
737 printf ("Using %s device\n", eth_get_name());
738#endif
739
740 puts ("File transfer via NFS from server "); print_IPaddr (NfsServerIP);
741 puts ("; our IP address is "); print_IPaddr (NetOurIP);
742
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;
747
748 if (OurNet != ServerNet) {
749 puts ("; sending through gateway ");
750 print_IPaddr (NetOurGatewayIP) ;
751 }
752 }
wdenk4b9206e2004-03-23 22:14:11 +0000753 printf ("\nFilename '%s/%s'.", nfs_path, nfs_filename);
wdenkcbd8a352004-02-24 02:00:03 +0000754
755 if (NetBootFileSize) {
756 printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
757 print_size (NetBootFileSize<<9, "");
758 }
wdenk4b9206e2004-03-23 22:14:11 +0000759 printf ("\nLoad address: 0x%lx\n"
760 "Loading: *\b", load_addr);
wdenkcbd8a352004-02-24 02:00:03 +0000761
762 NetSetTimeout (NFS_TIMEOUT * CFG_HZ, NfsTimeout);
763 NetSetHandler (NfsHandler);
764
wdenkcbd8a352004-02-24 02:00:03 +0000765 NfsTimeoutCount = 0;
766 NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
767
768 /*NfsOurPort = 4096 + (get_ticks() % 3072);*/
769 /*FIX ME !!!*/
770 NfsOurPort = 1000;
771
772 /* zero out server ether in case the server ip has changed */
773 memset (NetServerEther, 0, 6);
774
775 NfsSend ();
776}
777
778#endif /* CONFIG_COMMANDS & CFG_CMD_NFS */