blob: 4017c3e3539bc277d2fc95eb0770c649efd8e0f6 [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);
Ed Swarthoutf64ef9b2009-11-19 02:47:28 -0600519 nfs_path[pathlen + rlen] = 0;
wdenkcbd8a352004-02-24 02:00:03 +0000520 } 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
wdenk11dadd542004-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{
Evan Samanasaabb8cb2009-11-09 20:08:36 -0600574 if ( ++NfsTimeoutCount > NFS_RETRY_COUNT ) {
575 puts ("\nRetry count exceeded; starting again\n");
576 NetStartAgain ();
577 } else {
578 puts("T ");
579 NetSetTimeout (NFS_TIMEOUT, NfsTimeout);
Hiroshi Itofe891ec2008-01-31 18:35:04 +0900580 NfsSend ();
Hiroshi Itofe891ec2008-01-31 18:35:04 +0900581 }
wdenka5725fa2004-09-28 21:51:42 +0000582}
583
584static void
wdenkcbd8a352004-02-24 02:00:03 +0000585NfsHandler (uchar *pkt, unsigned dest, unsigned src, unsigned len)
586{
587 int rlen;
588
Robin Getz0ebf04c2009-07-23 03:01:03 -0400589 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000590
591 if (dest != NfsOurPort) return;
592
593 switch (NfsState) {
594 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
595 rpc_lookup_reply (PROG_MOUNT, pkt, len);
596 NfsState = STATE_PRCLOOKUP_PROG_NFS_REQ;
597 NfsSend ();
598 break;
599
600 case STATE_PRCLOOKUP_PROG_NFS_REQ:
601 rpc_lookup_reply (PROG_NFS, pkt, len);
602 NfsState = STATE_MOUNT_REQ;
603 NfsSend ();
604 break;
605
606 case STATE_MOUNT_REQ:
607 if (nfs_mount_reply(pkt, len)) {
608 puts ("*** ERROR: Cannot mount\n");
609 /* just to be sure... */
610 NfsState = STATE_UMOUNT_REQ;
611 NfsSend ();
612 } else {
613 NfsState = STATE_LOOKUP_REQ;
614 NfsSend ();
615 }
616 break;
617
618 case STATE_UMOUNT_REQ:
619 if (nfs_umountall_reply(pkt, len)) {
620 puts ("*** ERROR: Cannot umount\n");
621 NetState = NETLOOP_FAIL;
622 } else {
wdenka084f7d2004-02-24 22:33:21 +0000623 puts ("\ndone\n");
Wolfgang Denk23a7a322005-08-06 01:11:12 +0200624 NetState = NfsDownloadState;
wdenkcbd8a352004-02-24 02:00:03 +0000625 }
626 break;
627
628 case STATE_LOOKUP_REQ:
629 if (nfs_lookup_reply(pkt, len)) {
630 puts ("*** ERROR: File lookup fail\n");
631 NfsState = STATE_UMOUNT_REQ;
632 NfsSend ();
633 } else {
634 NfsState = STATE_READ_REQ;
635 nfs_offset = 0;
636 nfs_len = NFS_READ_SIZE;
637 NfsSend ();
638 }
639 break;
640
641 case STATE_READLINK_REQ:
642 if (nfs_readlink_reply(pkt, len)) {
643 puts ("*** ERROR: Symlink fail\n");
644 NfsState = STATE_UMOUNT_REQ;
645 NfsSend ();
646 } else {
Robin Getz0ebf04c2009-07-23 03:01:03 -0400647 debug("Symlink --> %s\n", nfs_path);
wdenkcbd8a352004-02-24 02:00:03 +0000648 nfs_filename = basename (nfs_path);
649 nfs_path = dirname (nfs_path);
650
651 NfsState = STATE_MOUNT_REQ;
652 NfsSend ();
653 }
654 break;
655
656 case STATE_READ_REQ:
657 rlen = nfs_read_reply (pkt, len);
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200658 NetSetTimeout (NFS_TIMEOUT, NfsTimeout);
wdenkcbd8a352004-02-24 02:00:03 +0000659 if (rlen > 0) {
660 nfs_offset += rlen;
661 NfsSend ();
662 }
663 else if ((rlen == -NFSERR_ISDIR)||(rlen == -NFSERR_INVAL)) {
664 /* symbolic link */
665 NfsState = STATE_READLINK_REQ;
666 NfsSend ();
667 } else {
Wolfgang Denk23a7a322005-08-06 01:11:12 +0200668 if ( ! rlen ) NfsDownloadState = NETLOOP_SUCCESS;
wdenkcbd8a352004-02-24 02:00:03 +0000669 NfsState = STATE_UMOUNT_REQ;
670 NfsSend ();
671 }
672 break;
673 }
674}
675
wdenkcbd8a352004-02-24 02:00:03 +0000676
677void
678NfsStart (void)
679{
Robin Getz0ebf04c2009-07-23 03:01:03 -0400680 debug("%s\n", __func__);
Wolfgang Denk23a7a322005-08-06 01:11:12 +0200681 NfsDownloadState = NETLOOP_FAIL;
wdenkcbd8a352004-02-24 02:00:03 +0000682
683 NfsServerIP = NetServerIP;
684 nfs_path = (char *)nfs_path_buff;
685
686 if (nfs_path == NULL) {
687 NetState = NETLOOP_FAIL;
688 puts ("*** ERROR: Fail allocate memory\n");
689 return;
690 }
691
692 if (BootFile[0] == '\0') {
wdenkcbd8a352004-02-24 02:00:03 +0000693 sprintf (default_filename, "/nfsroot/%02lX%02lX%02lX%02lX.img",
Wolfgang Denkc43352c2005-08-04 01:09:44 +0200694 NetOurIP & 0xFF,
695 (NetOurIP >> 8) & 0xFF,
696 (NetOurIP >> 16) & 0xFF,
697 (NetOurIP >> 24) & 0xFF );
wdenkcbd8a352004-02-24 02:00:03 +0000698 strcpy (nfs_path, default_filename);
699
700 printf ("*** Warning: no boot file name; using '%s'\n",
701 nfs_path);
702 } else {
703 char *p=BootFile;
704
705 p = strchr (p, ':');
706
707 if (p != NULL) {
708 NfsServerIP = string_to_ip (BootFile);
709 ++p;
710 strcpy (nfs_path, p);
711 } else {
712 strcpy (nfs_path, BootFile);
713 }
714 }
715
716 nfs_filename = basename (nfs_path);
717 nfs_path = dirname (nfs_path);
718
719#if defined(CONFIG_NET_MULTI)
720 printf ("Using %s device\n", eth_get_name());
721#endif
722
Mike Frysingerb6446b62009-02-17 00:00:53 -0500723 printf("File transfer via NFS from server %pI4"
724 "; our IP address is %pI4", &NfsServerIP, &NetOurIP);
wdenkcbd8a352004-02-24 02:00:03 +0000725
726 /* Check if we need to send across this subnet */
727 if (NetOurGatewayIP && NetOurSubnetMask) {
728 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
729 IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask;
730
Mike Frysingerb6446b62009-02-17 00:00:53 -0500731 if (OurNet != ServerNet)
732 printf("; sending through gateway %pI4", &NetOurGatewayIP);
wdenkcbd8a352004-02-24 02:00:03 +0000733 }
wdenk4b9206e2004-03-23 22:14:11 +0000734 printf ("\nFilename '%s/%s'.", nfs_path, nfs_filename);
wdenkcbd8a352004-02-24 02:00:03 +0000735
736 if (NetBootFileSize) {
737 printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
738 print_size (NetBootFileSize<<9, "");
739 }
wdenk4b9206e2004-03-23 22:14:11 +0000740 printf ("\nLoad address: 0x%lx\n"
741 "Loading: *\b", load_addr);
wdenkcbd8a352004-02-24 02:00:03 +0000742
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200743 NetSetTimeout (NFS_TIMEOUT, NfsTimeout);
wdenkcbd8a352004-02-24 02:00:03 +0000744 NetSetHandler (NfsHandler);
745
wdenkcbd8a352004-02-24 02:00:03 +0000746 NfsTimeoutCount = 0;
747 NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
748
749 /*NfsOurPort = 4096 + (get_ticks() % 3072);*/
750 /*FIX ME !!!*/
751 NfsOurPort = 1000;
752
753 /* zero out server ether in case the server ip has changed */
754 memset (NetServerEther, 0, 6);
755
756 NfsSend ();
757}
758
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500759#endif