blob: 27395fbf2e4b7068dc0de94d7f2df7f3118696f3 [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
Stefan Roese3865b1f2007-07-11 12:13:53 +020032#if defined(CONFIG_CMD_NET) && defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +000033
34#define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
Hiroshi Itofe891ec2008-01-31 18:35:04 +090035#define NFS_RETRY_COUNT 30
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +020036#define NFS_TIMEOUT 2000UL
wdenkcbd8a352004-02-24 02:00:03 +000037
38static int fs_mounted = 0;
wdenkc3f9d492004-03-14 00:59:59 +000039static unsigned long rpc_id = 0;
wdenkcbd8a352004-02-24 02:00:03 +000040static int nfs_offset = -1;
41static int nfs_len;
42
43static char dirfh[NFS_FHSIZE]; /* file handle of directory */
44static char filefh[NFS_FHSIZE]; /* file handle of kernel image */
45
Wolfgang Denk23a7a322005-08-06 01:11:12 +020046static int NfsDownloadState;
wdenkcbd8a352004-02-24 02:00:03 +000047static IPaddr_t NfsServerIP;
48static int NfsSrvMountPort;
49static int NfsSrvNfsPort;
50static int NfsOurPort;
51static int NfsTimeoutCount;
52static int NfsState;
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
60
61static char default_filename[64];
62static char *nfs_filename;
63static char *nfs_path;
64static char nfs_path_buff[2048];
65
Wolfgang Denk23a7a322005-08-06 01:11:12 +020066static __inline__ int
wdenkcbd8a352004-02-24 02:00:03 +000067store_block (uchar * src, unsigned offset, unsigned len)
68{
wdenka084f7d2004-02-24 22:33:21 +000069 ulong newsize = offset + len;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020070#ifdef CONFIG_SYS_DIRECT_FLASH_NFS
wdenkcbd8a352004-02-24 02:00:03 +000071 int i, rc = 0;
72
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020073 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenkcbd8a352004-02-24 02:00:03 +000074 /* start address in flash? */
75 if (load_addr + offset >= flash_info[i].start[0]) {
76 rc = 1;
77 break;
78 }
79 }
80
81 if (rc) { /* Flash is destination for this packet */
82 rc = flash_write ((uchar *)src, (ulong)(load_addr+offset), len);
83 if (rc) {
84 flash_perror (rc);
Wolfgang Denk23a7a322005-08-06 01:11:12 +020085 return -1;
wdenkcbd8a352004-02-24 02:00:03 +000086 }
87 } else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020088#endif /* CONFIG_SYS_DIRECT_FLASH_NFS */
wdenkcbd8a352004-02-24 02:00:03 +000089 {
90 (void)memcpy ((void *)(load_addr + offset), src, len);
91 }
wdenka084f7d2004-02-24 22:33:21 +000092
93 if (NetBootFileXferSize < (offset+len))
94 NetBootFileXferSize = newsize;
Wolfgang Denk23a7a322005-08-06 01:11:12 +020095 return 0;
wdenkcbd8a352004-02-24 02:00:03 +000096}
97
98static char*
99basename (char *path)
100{
101 char *fname;
102
103 fname = path + strlen(path) - 1;
104 while (fname >= path) {
105 if (*fname == '/') {
106 fname++;
107 break;
108 }
109 fname--;
110 }
111 return fname;
112}
113
114static char*
115dirname (char *path)
116{
117 char *fname;
118
119 fname = basename (path);
120 --fname;
121 *fname = '\0';
122 return path;
123}
124
125/**************************************************************************
wdenkcbd8a352004-02-24 02:00:03 +0000126RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
127**************************************************************************/
128static long *rpc_add_credentials (long *p)
129{
130 int hl;
131 int hostnamelen;
132 char hostname[256];
133
134 strcpy (hostname, "");
135 hostnamelen=strlen (hostname);
136
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
144 * hostname). */
145
146 hl = (hostnamelen + 3) & ~3;
147
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 */
155 }
156 memcpy (p, hostname, hostnamelen);
157 p += hl / 4;
158 *p++ = 0; /* uid */
159 *p++ = 0; /* gid */
160 *p++ = 0; /* auxiliary gid list */
161
162 /* Provide an AUTH_NONE verifier. */
163 *p++ = 0; /* AUTH_NONE */
164 *p++ = 0; /* auth length */
165
166 return p;
167}
168
169/**************************************************************************
170RPC_LOOKUP - Lookup RPC Port numbers
171**************************************************************************/
172static void
173rpc_req (int rpc_prog, int rpc_proc, uint32_t *data, int datalen)
174{
175 struct rpc_t pkt;
176 unsigned long id;
177 uint32_t *p;
178 int pktlen;
179 int sport;
180
wdenkc3f9d492004-03-14 00:59:59 +0000181 id = ++rpc_id;
wdenkcbd8a352004-02-24 02:00:03 +0000182 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);
189
190 if (datalen)
191 memcpy ((char *)p, (char *)data, datalen*sizeof(uint32_t));
192
193 pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt;
194
wdenka3d991b2004-04-15 21:48:45 +0000195 memcpy ((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, (char *)&pkt, pktlen);
wdenkcbd8a352004-02-24 02:00:03 +0000196
197 if (rpc_prog == PROG_PORTMAP)
198 sport = SUNRPC_PORT;
199 else if (rpc_prog == PROG_MOUNT)
200 sport = NfsSrvMountPort;
201 else
202 sport = NfsSrvNfsPort;
203
204 NetSendUDPPacket (NetServerEther, NfsServerIP, sport, NfsOurPort, pktlen);
205}
206
207/**************************************************************************
208RPC_LOOKUP - Lookup RPC Port numbers
209**************************************************************************/
210static void
211rpc_lookup_req (int prog, int ver)
212{
213 uint32_t data[16];
214
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 */
220 data[7] = 0;
221
222 rpc_req (PROG_PORTMAP, PORTMAP_GETPORT, data, 8);
223}
224
225/**************************************************************************
226NFS_MOUNT - Mount an NFS Filesystem
227**************************************************************************/
228static void
229nfs_mount_req (char *path)
230{
231 uint32_t data[1024];
232 uint32_t *p;
233 int len;
234 int pathlen;
235
236 pathlen = strlen (path);
237
238 p = &(data[0]);
239 p = (uint32_t *)rpc_add_credentials((long *)p);
240
241 *p++ = htonl(pathlen);
242 if (pathlen & 3) *(p + pathlen / 4) = 0;
243 memcpy (p, path, pathlen);
244 p += (pathlen + 3) / 4;
245
246 len = (uint32_t *)p - (uint32_t *)&(data[0]);
247
248 rpc_req (PROG_MOUNT, MOUNT_ADDENTRY, data, len);
249}
250
251/**************************************************************************
252NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
253**************************************************************************/
254static void
255nfs_umountall_req (void)
256{
257 uint32_t data[1024];
258 uint32_t *p;
259 int len;
260
261 if ((NfsSrvMountPort == -1) || (!fs_mounted)) {
262 /* Nothing mounted, nothing to umount */
263 return;
264 }
265
266 p = &(data[0]);
267 p = (uint32_t *)rpc_add_credentials ((long *)p);
268
269 len = (uint32_t *)p - (uint32_t *)&(data[0]);
270
271 rpc_req (PROG_MOUNT, MOUNT_UMOUNTALL, data, len);
272}
273
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 **************************************************************************/
281static void
282nfs_readlink_req (void)
283{
284 uint32_t data[1024];
285 uint32_t *p;
286 int len;
287
288 p = &(data[0]);
289 p = (uint32_t *)rpc_add_credentials ((long *)p);
290
291 memcpy (p, filefh, NFS_FHSIZE);
292 p += (NFS_FHSIZE / 4);
293
294 len = (uint32_t *)p - (uint32_t *)&(data[0]);
295
296 rpc_req (PROG_NFS, NFS_READLINK, data, len);
297}
298
299/**************************************************************************
300NFS_LOOKUP - Lookup Pathname
301**************************************************************************/
302static void
303nfs_lookup_req (char *fname)
304{
305 uint32_t data[1024];
306 uint32_t *p;
307 int len;
308 int fnamelen;
309
310 fnamelen = strlen (fname);
311
312 p = &(data[0]);
313 p = (uint32_t *)rpc_add_credentials ((long *)p);
314
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;
321
322 len = (uint32_t *)p - (uint32_t *)&(data[0]);
323
324 rpc_req (PROG_NFS, NFS_LOOKUP, data, len);
325}
326
327/**************************************************************************
328NFS_READ - Read File on NFS Server
329**************************************************************************/
330static void
331nfs_read_req (int offset, int readlen)
332{
333 uint32_t data[1024];
334 uint32_t *p;
335 int len;
336
337 p = &(data[0]);
338 p = (uint32_t *)rpc_add_credentials ((long *)p);
339
340 memcpy (p, filefh, NFS_FHSIZE);
341 p += (NFS_FHSIZE / 4);
342 *p++ = htonl(offset);
343 *p++ = htonl(readlen);
344 *p++ = 0;
345
346 len = (uint32_t *)p - (uint32_t *)&(data[0]);
347
348 rpc_req (PROG_NFS, NFS_READ, data, len);
349}
350
351/**************************************************************************
352RPC request dispatcher
353**************************************************************************/
354
355static void
356NfsSend (void)
357{
Robin Getz0ebf04c2009-07-23 03:01:03 -0400358 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000359
360 switch (NfsState) {
361 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
362 rpc_lookup_req (PROG_MOUNT, 1);
363 break;
364 case STATE_PRCLOOKUP_PROG_NFS_REQ:
365 rpc_lookup_req (PROG_NFS, 2);
366 break;
367 case STATE_MOUNT_REQ:
368 nfs_mount_req (nfs_path);
369 break;
370 case STATE_UMOUNT_REQ:
371 nfs_umountall_req ();
372 break;
373 case STATE_LOOKUP_REQ:
374 nfs_lookup_req (nfs_filename);
375 break;
376 case STATE_READ_REQ:
377 nfs_read_req (nfs_offset, nfs_len);
378 break;
379 case STATE_READLINK_REQ:
380 nfs_readlink_req ();
381 break;
382 }
383}
384
385/**************************************************************************
386Handlers for the reply from server
387**************************************************************************/
388
389static int
390rpc_lookup_reply (int prog, uchar *pkt, unsigned len)
391{
392 struct rpc_t rpc_pkt;
393
394 memcpy ((unsigned char *)&rpc_pkt, pkt, len);
395
Robin Getz0ebf04c2009-07-23 03:01:03 -0400396 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000397
wdenkc3f9d492004-03-14 00:59:59 +0000398 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
399 return -1;
400
wdenkcbd8a352004-02-24 02:00:03 +0000401 if (rpc_pkt.u.reply.rstatus ||
402 rpc_pkt.u.reply.verifier ||
wdenkcbd8a352004-02-24 02:00:03 +0000403 rpc_pkt.u.reply.astatus) {
wdenkc3f9d492004-03-14 00:59:59 +0000404 return -1;
wdenkcbd8a352004-02-24 02:00:03 +0000405 }
406
407 switch (prog) {
408 case PROG_MOUNT:
409 NfsSrvMountPort = ntohl(rpc_pkt.u.reply.data[0]);
410 break;
411 case PROG_NFS:
412 NfsSrvNfsPort = ntohl(rpc_pkt.u.reply.data[0]);
413 break;
414 }
415
416 return 0;
417}
418
419static int
420nfs_mount_reply (uchar *pkt, unsigned len)
421{
422 struct rpc_t rpc_pkt;
423
Robin Getz0ebf04c2009-07-23 03:01:03 -0400424 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000425
426 memcpy ((unsigned char *)&rpc_pkt, pkt, len);
427
wdenkc3f9d492004-03-14 00:59:59 +0000428 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
429 return -1;
430
wdenkcbd8a352004-02-24 02:00:03 +0000431 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]) {
435 return -1;
436 }
437
438 fs_mounted = 1;
439 memcpy (dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
440
441 return 0;
442}
443
444static int
445nfs_umountall_reply (uchar *pkt, unsigned len)
446{
447 struct rpc_t rpc_pkt;
448
Robin Getz0ebf04c2009-07-23 03:01:03 -0400449 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000450
451 memcpy ((unsigned char *)&rpc_pkt, pkt, len);
452
wdenkc3f9d492004-03-14 00:59:59 +0000453 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
454 return -1;
455
wdenkcbd8a352004-02-24 02:00:03 +0000456 if (rpc_pkt.u.reply.rstatus ||
457 rpc_pkt.u.reply.verifier ||
458 rpc_pkt.u.reply.astatus) {
459 return -1;
460 }
461
462 fs_mounted = 0;
463 memset (dirfh, 0, sizeof(dirfh));
464
465 return 0;
466}
467
468static int
469nfs_lookup_reply (uchar *pkt, unsigned len)
470{
471 struct rpc_t rpc_pkt;
472
Robin Getz0ebf04c2009-07-23 03:01:03 -0400473 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000474
475 memcpy ((unsigned char *)&rpc_pkt, pkt, len);
476
wdenkc3f9d492004-03-14 00:59:59 +0000477 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
478 return -1;
479
wdenkcbd8a352004-02-24 02:00:03 +0000480 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]) {
484 return -1;
485 }
486
487 memcpy (filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
488
489 return 0;
490}
491
492static int
493nfs_readlink_reply (uchar *pkt, unsigned len)
494{
495 struct rpc_t rpc_pkt;
496 int rlen;
497
Robin Getz0ebf04c2009-07-23 03:01:03 -0400498 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000499
500 memcpy ((unsigned char *)&rpc_pkt, pkt, len);
501
wdenkc3f9d492004-03-14 00:59:59 +0000502 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
503 return -1;
504
wdenkcbd8a352004-02-24 02:00:03 +0000505 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]) {
509 return -1;
510 }
511
512 rlen = ntohl (rpc_pkt.u.reply.data[1]); /* new path length */
513
514 if (*((char *)&(rpc_pkt.u.reply.data[2])) != '/') {
515 int pathlen;
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+1] = 0;
520 } else {
521 memcpy (nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen);
522 nfs_path[rlen] = 0;
523 }
524 return 0;
525}
526
527static int
528nfs_read_reply (uchar *pkt, unsigned len)
529{
530 struct rpc_t rpc_pkt;
531 int rlen;
532
Robin Getz0ebf04c2009-07-23 03:01:03 -0400533 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000534
wdenk11dadd52004-02-27 00:07:27 +0000535 memcpy ((uchar *)&rpc_pkt, pkt, sizeof(rpc_pkt.u.reply));
wdenkcbd8a352004-02-24 02:00:03 +0000536
wdenkc3f9d492004-03-14 00:59:59 +0000537 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
538 return -1;
539
wdenkcbd8a352004-02-24 02:00:03 +0000540 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) {
545 return -9999;
546 }
547 if (rpc_pkt.u.reply.astatus) {
548 return -9999;
549 }
550 return -ntohl(rpc_pkt.u.reply.data[0]);;
551 }
552
553 if ((nfs_offset!=0) && !((nfs_offset) % (NFS_READ_SIZE/2*10*HASHES_PER_LINE))) {
554 puts ("\n\t ");
555 }
556 if (!(nfs_offset % ((NFS_READ_SIZE/2)*10))) {
557 putc ('#');
558 }
559
560 rlen = ntohl(rpc_pkt.u.reply.data[18]);
Wolfgang Denk23a7a322005-08-06 01:11:12 +0200561 if ( store_block ((uchar *)pkt+sizeof(rpc_pkt.u.reply), nfs_offset, rlen) )
562 return -9999;
wdenkcbd8a352004-02-24 02:00:03 +0000563
564 return rlen;
565}
566
567/**************************************************************************
568Interfaces of U-BOOT
569**************************************************************************/
570
571static void
wdenka5725fa2004-09-28 21:51:42 +0000572NfsTimeout (void)
573{
Hiroshi Itofe891ec2008-01-31 18:35:04 +0900574 if ( NfsTimeoutCount++ < NFS_RETRY_COUNT ) {
575 NfsSend ();
576 return;
577 }
wdenka5725fa2004-09-28 21:51:42 +0000578 puts ("Timeout\n");
579 NetState = NETLOOP_FAIL;
580 return;
581}
582
583static void
wdenkcbd8a352004-02-24 02:00:03 +0000584NfsHandler (uchar *pkt, unsigned dest, unsigned src, unsigned len)
585{
586 int rlen;
587
Robin Getz0ebf04c2009-07-23 03:01:03 -0400588 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000589
590 if (dest != NfsOurPort) return;
591
592 switch (NfsState) {
593 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
594 rpc_lookup_reply (PROG_MOUNT, pkt, len);
595 NfsState = STATE_PRCLOOKUP_PROG_NFS_REQ;
596 NfsSend ();
597 break;
598
599 case STATE_PRCLOOKUP_PROG_NFS_REQ:
600 rpc_lookup_reply (PROG_NFS, pkt, len);
601 NfsState = STATE_MOUNT_REQ;
602 NfsSend ();
603 break;
604
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;
610 NfsSend ();
611 } else {
612 NfsState = STATE_LOOKUP_REQ;
613 NfsSend ();
614 }
615 break;
616
617 case STATE_UMOUNT_REQ:
618 if (nfs_umountall_reply(pkt, len)) {
619 puts ("*** ERROR: Cannot umount\n");
620 NetState = NETLOOP_FAIL;
621 } else {
wdenka084f7d2004-02-24 22:33:21 +0000622 puts ("\ndone\n");
Wolfgang Denk23a7a322005-08-06 01:11:12 +0200623 NetState = NfsDownloadState;
wdenkcbd8a352004-02-24 02:00:03 +0000624 }
625 break;
626
627 case STATE_LOOKUP_REQ:
628 if (nfs_lookup_reply(pkt, len)) {
629 puts ("*** ERROR: File lookup fail\n");
630 NfsState = STATE_UMOUNT_REQ;
631 NfsSend ();
632 } else {
633 NfsState = STATE_READ_REQ;
634 nfs_offset = 0;
635 nfs_len = NFS_READ_SIZE;
636 NfsSend ();
637 }
638 break;
639
640 case STATE_READLINK_REQ:
641 if (nfs_readlink_reply(pkt, len)) {
642 puts ("*** ERROR: Symlink fail\n");
643 NfsState = STATE_UMOUNT_REQ;
644 NfsSend ();
645 } else {
Robin Getz0ebf04c2009-07-23 03:01:03 -0400646 debug("Symlink --> %s\n", nfs_path);
wdenkcbd8a352004-02-24 02:00:03 +0000647 nfs_filename = basename (nfs_path);
648 nfs_path = dirname (nfs_path);
649
650 NfsState = STATE_MOUNT_REQ;
651 NfsSend ();
652 }
653 break;
654
655 case STATE_READ_REQ:
656 rlen = nfs_read_reply (pkt, len);
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200657 NetSetTimeout (NFS_TIMEOUT, NfsTimeout);
wdenkcbd8a352004-02-24 02:00:03 +0000658 if (rlen > 0) {
659 nfs_offset += rlen;
660 NfsSend ();
661 }
662 else if ((rlen == -NFSERR_ISDIR)||(rlen == -NFSERR_INVAL)) {
663 /* symbolic link */
664 NfsState = STATE_READLINK_REQ;
665 NfsSend ();
666 } else {
Wolfgang Denk23a7a322005-08-06 01:11:12 +0200667 if ( ! rlen ) NfsDownloadState = NETLOOP_SUCCESS;
wdenkcbd8a352004-02-24 02:00:03 +0000668 NfsState = STATE_UMOUNT_REQ;
669 NfsSend ();
670 }
671 break;
672 }
673}
674
wdenkcbd8a352004-02-24 02:00:03 +0000675
676void
677NfsStart (void)
678{
Robin Getz0ebf04c2009-07-23 03:01:03 -0400679 debug("%s\n", __func__);
Wolfgang Denk23a7a322005-08-06 01:11:12 +0200680 NfsDownloadState = NETLOOP_FAIL;
wdenkcbd8a352004-02-24 02:00:03 +0000681
682 NfsServerIP = NetServerIP;
683 nfs_path = (char *)nfs_path_buff;
684
685 if (nfs_path == NULL) {
686 NetState = NETLOOP_FAIL;
687 puts ("*** ERROR: Fail allocate memory\n");
688 return;
689 }
690
691 if (BootFile[0] == '\0') {
wdenkcbd8a352004-02-24 02:00:03 +0000692 sprintf (default_filename, "/nfsroot/%02lX%02lX%02lX%02lX.img",
Wolfgang Denkc43352c2005-08-04 01:09:44 +0200693 NetOurIP & 0xFF,
694 (NetOurIP >> 8) & 0xFF,
695 (NetOurIP >> 16) & 0xFF,
696 (NetOurIP >> 24) & 0xFF );
wdenkcbd8a352004-02-24 02:00:03 +0000697 strcpy (nfs_path, default_filename);
698
699 printf ("*** Warning: no boot file name; using '%s'\n",
700 nfs_path);
701 } else {
702 char *p=BootFile;
703
704 p = strchr (p, ':');
705
706 if (p != NULL) {
707 NfsServerIP = string_to_ip (BootFile);
708 ++p;
709 strcpy (nfs_path, p);
710 } else {
711 strcpy (nfs_path, BootFile);
712 }
713 }
714
715 nfs_filename = basename (nfs_path);
716 nfs_path = dirname (nfs_path);
717
718#if defined(CONFIG_NET_MULTI)
719 printf ("Using %s device\n", eth_get_name());
720#endif
721
Mike Frysingerb6446b62009-02-17 00:00:53 -0500722 printf("File transfer via NFS from server %pI4"
723 "; our IP address is %pI4", &NfsServerIP, &NetOurIP);
wdenkcbd8a352004-02-24 02:00:03 +0000724
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;
729
Mike Frysingerb6446b62009-02-17 00:00:53 -0500730 if (OurNet != ServerNet)
731 printf("; sending through gateway %pI4", &NetOurGatewayIP);
wdenkcbd8a352004-02-24 02:00:03 +0000732 }
wdenk4b9206e2004-03-23 22:14:11 +0000733 printf ("\nFilename '%s/%s'.", nfs_path, nfs_filename);
wdenkcbd8a352004-02-24 02:00:03 +0000734
735 if (NetBootFileSize) {
736 printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
737 print_size (NetBootFileSize<<9, "");
738 }
wdenk4b9206e2004-03-23 22:14:11 +0000739 printf ("\nLoad address: 0x%lx\n"
740 "Loading: *\b", load_addr);
wdenkcbd8a352004-02-24 02:00:03 +0000741
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200742 NetSetTimeout (NFS_TIMEOUT, NfsTimeout);
wdenkcbd8a352004-02-24 02:00:03 +0000743 NetSetHandler (NfsHandler);
744
wdenkcbd8a352004-02-24 02:00:03 +0000745 NfsTimeoutCount = 0;
746 NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
747
748 /*NfsOurPort = 4096 + (get_ticks() % 3072);*/
749 /*FIX ME !!!*/
750 NfsOurPort = 1000;
751
752 /* zero out server ether in case the server ip has changed */
753 memset (NetServerEther, 0, 6);
754
755 NfsSend ();
756}
757
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500758#endif