blob: 01016290283aacfad5d966773ffc50b72c3148f6 [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
Stefan Roese3865b1f2007-07-11 12:13:53 +020034#if defined(CONFIG_CMD_NET) && defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +000035
36#define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
Hiroshi Itofe891ec2008-01-31 18:35:04 +090037#define NFS_RETRY_COUNT 30
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +020038#define NFS_TIMEOUT 2000UL
wdenkcbd8a352004-02-24 02:00:03 +000039
40static int fs_mounted = 0;
wdenkc3f9d492004-03-14 00:59:59 +000041static unsigned long rpc_id = 0;
wdenkcbd8a352004-02-24 02:00:03 +000042static int nfs_offset = -1;
43static int nfs_len;
44
45static char dirfh[NFS_FHSIZE]; /* file handle of directory */
46static char filefh[NFS_FHSIZE]; /* file handle of kernel image */
47
Wolfgang Denk23a7a322005-08-06 01:11:12 +020048static int NfsDownloadState;
wdenkcbd8a352004-02-24 02:00:03 +000049static IPaddr_t NfsServerIP;
50static int NfsSrvMountPort;
51static int NfsSrvNfsPort;
52static int NfsOurPort;
53static int NfsTimeoutCount;
54static int NfsState;
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
62
63static char default_filename[64];
64static char *nfs_filename;
65static char *nfs_path;
66static char nfs_path_buff[2048];
67
Wolfgang Denk23a7a322005-08-06 01:11:12 +020068static __inline__ int
wdenkcbd8a352004-02-24 02:00:03 +000069store_block (uchar * src, unsigned offset, unsigned len)
70{
wdenka084f7d2004-02-24 22:33:21 +000071 ulong newsize = offset + len;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020072#ifdef CONFIG_SYS_DIRECT_FLASH_NFS
wdenkcbd8a352004-02-24 02:00:03 +000073 int i, rc = 0;
74
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020075 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenkcbd8a352004-02-24 02:00:03 +000076 /* start address in flash? */
77 if (load_addr + offset >= flash_info[i].start[0]) {
78 rc = 1;
79 break;
80 }
81 }
82
83 if (rc) { /* Flash is destination for this packet */
84 rc = flash_write ((uchar *)src, (ulong)(load_addr+offset), len);
85 if (rc) {
86 flash_perror (rc);
Wolfgang Denk23a7a322005-08-06 01:11:12 +020087 return -1;
wdenkcbd8a352004-02-24 02:00:03 +000088 }
89 } else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020090#endif /* CONFIG_SYS_DIRECT_FLASH_NFS */
wdenkcbd8a352004-02-24 02:00:03 +000091 {
92 (void)memcpy ((void *)(load_addr + offset), src, len);
93 }
wdenka084f7d2004-02-24 22:33:21 +000094
95 if (NetBootFileXferSize < (offset+len))
96 NetBootFileXferSize = newsize;
Wolfgang Denk23a7a322005-08-06 01:11:12 +020097 return 0;
wdenkcbd8a352004-02-24 02:00:03 +000098}
99
100static char*
101basename (char *path)
102{
103 char *fname;
104
105 fname = path + strlen(path) - 1;
106 while (fname >= path) {
107 if (*fname == '/') {
108 fname++;
109 break;
110 }
111 fname--;
112 }
113 return fname;
114}
115
116static char*
117dirname (char *path)
118{
119 char *fname;
120
121 fname = basename (path);
122 --fname;
123 *fname = '\0';
124 return path;
125}
126
127/**************************************************************************
wdenkcbd8a352004-02-24 02:00:03 +0000128RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
129**************************************************************************/
130static long *rpc_add_credentials (long *p)
131{
132 int hl;
133 int hostnamelen;
134 char hostname[256];
135
136 strcpy (hostname, "");
137 hostnamelen=strlen (hostname);
138
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
146 * hostname). */
147
148 hl = (hostnamelen + 3) & ~3;
149
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 */
157 }
158 memcpy (p, hostname, hostnamelen);
159 p += hl / 4;
160 *p++ = 0; /* uid */
161 *p++ = 0; /* gid */
162 *p++ = 0; /* auxiliary gid list */
163
164 /* Provide an AUTH_NONE verifier. */
165 *p++ = 0; /* AUTH_NONE */
166 *p++ = 0; /* auth length */
167
168 return p;
169}
170
171/**************************************************************************
172RPC_LOOKUP - Lookup RPC Port numbers
173**************************************************************************/
174static void
175rpc_req (int rpc_prog, int rpc_proc, uint32_t *data, int datalen)
176{
177 struct rpc_t pkt;
178 unsigned long id;
179 uint32_t *p;
180 int pktlen;
181 int sport;
182
wdenkc3f9d492004-03-14 00:59:59 +0000183 id = ++rpc_id;
wdenkcbd8a352004-02-24 02:00:03 +0000184 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);
191
192 if (datalen)
193 memcpy ((char *)p, (char *)data, datalen*sizeof(uint32_t));
194
195 pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt;
196
wdenka3d991b2004-04-15 21:48:45 +0000197 memcpy ((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, (char *)&pkt, pktlen);
wdenkcbd8a352004-02-24 02:00:03 +0000198
199 if (rpc_prog == PROG_PORTMAP)
200 sport = SUNRPC_PORT;
201 else if (rpc_prog == PROG_MOUNT)
202 sport = NfsSrvMountPort;
203 else
204 sport = NfsSrvNfsPort;
205
206 NetSendUDPPacket (NetServerEther, NfsServerIP, sport, NfsOurPort, pktlen);
207}
208
209/**************************************************************************
210RPC_LOOKUP - Lookup RPC Port numbers
211**************************************************************************/
212static void
213rpc_lookup_req (int prog, int ver)
214{
215 uint32_t data[16];
216
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 */
222 data[7] = 0;
223
224 rpc_req (PROG_PORTMAP, PORTMAP_GETPORT, data, 8);
225}
226
227/**************************************************************************
228NFS_MOUNT - Mount an NFS Filesystem
229**************************************************************************/
230static void
231nfs_mount_req (char *path)
232{
233 uint32_t data[1024];
234 uint32_t *p;
235 int len;
236 int pathlen;
237
238 pathlen = strlen (path);
239
240 p = &(data[0]);
241 p = (uint32_t *)rpc_add_credentials((long *)p);
242
243 *p++ = htonl(pathlen);
244 if (pathlen & 3) *(p + pathlen / 4) = 0;
245 memcpy (p, path, pathlen);
246 p += (pathlen + 3) / 4;
247
248 len = (uint32_t *)p - (uint32_t *)&(data[0]);
249
250 rpc_req (PROG_MOUNT, MOUNT_ADDENTRY, data, len);
251}
252
253/**************************************************************************
254NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
255**************************************************************************/
256static void
257nfs_umountall_req (void)
258{
259 uint32_t data[1024];
260 uint32_t *p;
261 int len;
262
263 if ((NfsSrvMountPort == -1) || (!fs_mounted)) {
264 /* Nothing mounted, nothing to umount */
265 return;
266 }
267
268 p = &(data[0]);
269 p = (uint32_t *)rpc_add_credentials ((long *)p);
270
271 len = (uint32_t *)p - (uint32_t *)&(data[0]);
272
273 rpc_req (PROG_MOUNT, MOUNT_UMOUNTALL, data, len);
274}
275
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 **************************************************************************/
283static void
284nfs_readlink_req (void)
285{
286 uint32_t data[1024];
287 uint32_t *p;
288 int len;
289
290 p = &(data[0]);
291 p = (uint32_t *)rpc_add_credentials ((long *)p);
292
293 memcpy (p, filefh, NFS_FHSIZE);
294 p += (NFS_FHSIZE / 4);
295
296 len = (uint32_t *)p - (uint32_t *)&(data[0]);
297
298 rpc_req (PROG_NFS, NFS_READLINK, data, len);
299}
300
301/**************************************************************************
302NFS_LOOKUP - Lookup Pathname
303**************************************************************************/
304static void
305nfs_lookup_req (char *fname)
306{
307 uint32_t data[1024];
308 uint32_t *p;
309 int len;
310 int fnamelen;
311
312 fnamelen = strlen (fname);
313
314 p = &(data[0]);
315 p = (uint32_t *)rpc_add_credentials ((long *)p);
316
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;
323
324 len = (uint32_t *)p - (uint32_t *)&(data[0]);
325
326 rpc_req (PROG_NFS, NFS_LOOKUP, data, len);
327}
328
329/**************************************************************************
330NFS_READ - Read File on NFS Server
331**************************************************************************/
332static void
333nfs_read_req (int offset, int readlen)
334{
335 uint32_t data[1024];
336 uint32_t *p;
337 int len;
338
339 p = &(data[0]);
340 p = (uint32_t *)rpc_add_credentials ((long *)p);
341
342 memcpy (p, filefh, NFS_FHSIZE);
343 p += (NFS_FHSIZE / 4);
344 *p++ = htonl(offset);
345 *p++ = htonl(readlen);
346 *p++ = 0;
347
348 len = (uint32_t *)p - (uint32_t *)&(data[0]);
349
350 rpc_req (PROG_NFS, NFS_READ, data, len);
351}
352
353/**************************************************************************
354RPC request dispatcher
355**************************************************************************/
356
357static void
358NfsSend (void)
359{
360#ifdef NFS_DEBUG
361 printf ("%s\n", __FUNCTION__);
362#endif
363
364 switch (NfsState) {
365 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
366 rpc_lookup_req (PROG_MOUNT, 1);
367 break;
368 case STATE_PRCLOOKUP_PROG_NFS_REQ:
369 rpc_lookup_req (PROG_NFS, 2);
370 break;
371 case STATE_MOUNT_REQ:
372 nfs_mount_req (nfs_path);
373 break;
374 case STATE_UMOUNT_REQ:
375 nfs_umountall_req ();
376 break;
377 case STATE_LOOKUP_REQ:
378 nfs_lookup_req (nfs_filename);
379 break;
380 case STATE_READ_REQ:
381 nfs_read_req (nfs_offset, nfs_len);
382 break;
383 case STATE_READLINK_REQ:
384 nfs_readlink_req ();
385 break;
386 }
387}
388
389/**************************************************************************
390Handlers for the reply from server
391**************************************************************************/
392
393static int
394rpc_lookup_reply (int prog, uchar *pkt, unsigned len)
395{
396 struct rpc_t rpc_pkt;
397
398 memcpy ((unsigned char *)&rpc_pkt, pkt, len);
399
400#ifdef NFS_DEBUG
401 printf ("%s\n", __FUNCTION__);
402#endif
403
wdenkc3f9d492004-03-14 00:59:59 +0000404 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
405 return -1;
406
wdenkcbd8a352004-02-24 02:00:03 +0000407 if (rpc_pkt.u.reply.rstatus ||
408 rpc_pkt.u.reply.verifier ||
wdenkcbd8a352004-02-24 02:00:03 +0000409 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
wdenk11dadd542004-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{
Hiroshi Itofe891ec2008-01-31 18:35:04 +0900590 if ( NfsTimeoutCount++ < NFS_RETRY_COUNT ) {
591 NfsSend ();
592 return;
593 }
wdenka5725fa2004-09-28 21:51:42 +0000594 puts ("Timeout\n");
595 NetState = NETLOOP_FAIL;
596 return;
597}
598
599static void
wdenkcbd8a352004-02-24 02:00:03 +0000600NfsHandler (uchar *pkt, unsigned dest, unsigned src, unsigned len)
601{
602 int rlen;
603
604#ifdef NFS_DEBUG
605 printf ("%s\n", __FUNCTION__);
606#endif
607
608 if (dest != NfsOurPort) return;
609
610 switch (NfsState) {
611 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
612 rpc_lookup_reply (PROG_MOUNT, pkt, len);
613 NfsState = STATE_PRCLOOKUP_PROG_NFS_REQ;
614 NfsSend ();
615 break;
616
617 case STATE_PRCLOOKUP_PROG_NFS_REQ:
618 rpc_lookup_reply (PROG_NFS, pkt, len);
619 NfsState = STATE_MOUNT_REQ;
620 NfsSend ();
621 break;
622
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;
628 NfsSend ();
629 } else {
630 NfsState = STATE_LOOKUP_REQ;
631 NfsSend ();
632 }
633 break;
634
635 case STATE_UMOUNT_REQ:
636 if (nfs_umountall_reply(pkt, len)) {
637 puts ("*** ERROR: Cannot umount\n");
638 NetState = NETLOOP_FAIL;
639 } else {
wdenka084f7d2004-02-24 22:33:21 +0000640 puts ("\ndone\n");
Wolfgang Denk23a7a322005-08-06 01:11:12 +0200641 NetState = NfsDownloadState;
wdenkcbd8a352004-02-24 02:00:03 +0000642 }
643 break;
644
645 case STATE_LOOKUP_REQ:
646 if (nfs_lookup_reply(pkt, len)) {
647 puts ("*** ERROR: File lookup fail\n");
648 NfsState = STATE_UMOUNT_REQ;
649 NfsSend ();
650 } else {
651 NfsState = STATE_READ_REQ;
652 nfs_offset = 0;
653 nfs_len = NFS_READ_SIZE;
654 NfsSend ();
655 }
656 break;
657
658 case STATE_READLINK_REQ:
659 if (nfs_readlink_reply(pkt, len)) {
660 puts ("*** ERROR: Symlink fail\n");
661 NfsState = STATE_UMOUNT_REQ;
662 NfsSend ();
663 } else {
664#ifdef NFS_DEBUG
665 printf ("Symlink --> %s\n", nfs_path);
666#endif
667 nfs_filename = basename (nfs_path);
668 nfs_path = dirname (nfs_path);
669
670 NfsState = STATE_MOUNT_REQ;
671 NfsSend ();
672 }
673 break;
674
675 case STATE_READ_REQ:
676 rlen = nfs_read_reply (pkt, len);
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200677 NetSetTimeout (NFS_TIMEOUT, NfsTimeout);
wdenkcbd8a352004-02-24 02:00:03 +0000678 if (rlen > 0) {
679 nfs_offset += rlen;
680 NfsSend ();
681 }
682 else if ((rlen == -NFSERR_ISDIR)||(rlen == -NFSERR_INVAL)) {
683 /* symbolic link */
684 NfsState = STATE_READLINK_REQ;
685 NfsSend ();
686 } else {
Wolfgang Denk23a7a322005-08-06 01:11:12 +0200687 if ( ! rlen ) NfsDownloadState = NETLOOP_SUCCESS;
wdenkcbd8a352004-02-24 02:00:03 +0000688 NfsState = STATE_UMOUNT_REQ;
689 NfsSend ();
690 }
691 break;
692 }
693}
694
wdenkcbd8a352004-02-24 02:00:03 +0000695
696void
697NfsStart (void)
698{
699#ifdef NFS_DEBUG
700 printf ("%s\n", __FUNCTION__);
701#endif
Wolfgang Denk23a7a322005-08-06 01:11:12 +0200702 NfsDownloadState = NETLOOP_FAIL;
wdenkcbd8a352004-02-24 02:00:03 +0000703
704 NfsServerIP = NetServerIP;
705 nfs_path = (char *)nfs_path_buff;
706
707 if (nfs_path == NULL) {
708 NetState = NETLOOP_FAIL;
709 puts ("*** ERROR: Fail allocate memory\n");
710 return;
711 }
712
713 if (BootFile[0] == '\0') {
wdenkcbd8a352004-02-24 02:00:03 +0000714 sprintf (default_filename, "/nfsroot/%02lX%02lX%02lX%02lX.img",
Wolfgang Denkc43352c2005-08-04 01:09:44 +0200715 NetOurIP & 0xFF,
716 (NetOurIP >> 8) & 0xFF,
717 (NetOurIP >> 16) & 0xFF,
718 (NetOurIP >> 24) & 0xFF );
wdenkcbd8a352004-02-24 02:00:03 +0000719 strcpy (nfs_path, default_filename);
720
721 printf ("*** Warning: no boot file name; using '%s'\n",
722 nfs_path);
723 } else {
724 char *p=BootFile;
725
726 p = strchr (p, ':');
727
728 if (p != NULL) {
729 NfsServerIP = string_to_ip (BootFile);
730 ++p;
731 strcpy (nfs_path, p);
732 } else {
733 strcpy (nfs_path, BootFile);
734 }
735 }
736
737 nfs_filename = basename (nfs_path);
738 nfs_path = dirname (nfs_path);
739
740#if defined(CONFIG_NET_MULTI)
741 printf ("Using %s device\n", eth_get_name());
742#endif
743
Mike Frysingerb6446b62009-02-17 00:00:53 -0500744 printf("File transfer via NFS from server %pI4"
745 "; our IP address is %pI4", &NfsServerIP, &NetOurIP);
wdenkcbd8a352004-02-24 02:00:03 +0000746
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;
751
Mike Frysingerb6446b62009-02-17 00:00:53 -0500752 if (OurNet != ServerNet)
753 printf("; sending through gateway %pI4", &NetOurGatewayIP);
wdenkcbd8a352004-02-24 02:00:03 +0000754 }
wdenk4b9206e2004-03-23 22:14:11 +0000755 printf ("\nFilename '%s/%s'.", nfs_path, nfs_filename);
wdenkcbd8a352004-02-24 02:00:03 +0000756
757 if (NetBootFileSize) {
758 printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
759 print_size (NetBootFileSize<<9, "");
760 }
wdenk4b9206e2004-03-23 22:14:11 +0000761 printf ("\nLoad address: 0x%lx\n"
762 "Loading: *\b", load_addr);
wdenkcbd8a352004-02-24 02:00:03 +0000763
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200764 NetSetTimeout (NFS_TIMEOUT, NfsTimeout);
wdenkcbd8a352004-02-24 02:00:03 +0000765 NetSetHandler (NfsHandler);
766
wdenkcbd8a352004-02-24 02:00:03 +0000767 NfsTimeoutCount = 0;
768 NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
769
770 /*NfsOurPort = 4096 + (get_ticks() % 3072);*/
771 /*FIX ME !!!*/
772 NfsOurPort = 1000;
773
774 /* zero out server ether in case the server ip has changed */
775 memset (NetServerEther, 0, 6);
776
777 NfsSend ();
778}
779
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500780#endif