blob: 8e05ae57cd883cc8ac23cd8b1dabb0a1db86d725 [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>
Joe Hershberger55d5fd92015-03-22 17:09:08 -050029#include <mapmem.h>
wdenkcbd8a352004-02-24 02:00:03 +000030#include "nfs.h"
31#include "bootp.h"
32
wdenkcbd8a352004-02-24 02:00:03 +000033#define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
Hiroshi Itofe891ec2008-01-31 18:35:04 +090034#define NFS_RETRY_COUNT 30
Tetsuyuki Kobayashi48a3e992012-07-03 22:25:21 +000035#ifndef CONFIG_NFS_TIMEOUT
36# define NFS_TIMEOUT 2000UL
37#else
38# define NFS_TIMEOUT CONFIG_NFS_TIMEOUT
39#endif
wdenkcbd8a352004-02-24 02:00:03 +000040
Matthias Bruggerfa84fa72012-12-11 19:14:16 +010041#define NFS_RPC_ERR 1
42#define NFS_RPC_DROP 124
43
Joe Hershbergerc9f6c912012-05-15 08:59:09 +000044static int fs_mounted;
45static unsigned long rpc_id;
wdenkcbd8a352004-02-24 02:00:03 +000046static int nfs_offset = -1;
47static int nfs_len;
Matthias Bruggerfa84fa72012-12-11 19:14:16 +010048static ulong nfs_timeout = NFS_TIMEOUT;
wdenkcbd8a352004-02-24 02:00:03 +000049
50static char dirfh[NFS_FHSIZE]; /* file handle of directory */
51static char filefh[NFS_FHSIZE]; /* file handle of kernel image */
52
Joe Hershberger22f6e992012-05-23 07:59:14 +000053static enum net_loop_state nfs_download_state;
wdenkcbd8a352004-02-24 02:00:03 +000054static IPaddr_t NfsServerIP;
55static int NfsSrvMountPort;
56static int NfsSrvNfsPort;
57static int NfsOurPort;
58static int NfsTimeoutCount;
59static int NfsState;
60#define STATE_PRCLOOKUP_PROG_MOUNT_REQ 1
61#define STATE_PRCLOOKUP_PROG_NFS_REQ 2
62#define STATE_MOUNT_REQ 3
63#define STATE_UMOUNT_REQ 4
64#define STATE_LOOKUP_REQ 5
65#define STATE_READ_REQ 6
66#define STATE_READLINK_REQ 7
67
68static char default_filename[64];
69static char *nfs_filename;
70static char *nfs_path;
71static char nfs_path_buff[2048];
72
Joe Hershbergerc9f6c912012-05-15 08:59:09 +000073static inline int
74store_block(uchar *src, unsigned offset, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +000075{
wdenka084f7d2004-02-24 22:33:21 +000076 ulong newsize = offset + len;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020077#ifdef CONFIG_SYS_DIRECT_FLASH_NFS
wdenkcbd8a352004-02-24 02:00:03 +000078 int i, rc = 0;
79
Joe Hershbergerc9f6c912012-05-15 08:59:09 +000080 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenkcbd8a352004-02-24 02:00:03 +000081 /* start address in flash? */
82 if (load_addr + offset >= flash_info[i].start[0]) {
83 rc = 1;
84 break;
85 }
86 }
87
88 if (rc) { /* Flash is destination for this packet */
Joe Hershbergerc9f6c912012-05-15 08:59:09 +000089 rc = flash_write((uchar *)src, (ulong)(load_addr+offset), len);
wdenkcbd8a352004-02-24 02:00:03 +000090 if (rc) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +000091 flash_perror(rc);
Wolfgang Denk23a7a322005-08-06 01:11:12 +020092 return -1;
wdenkcbd8a352004-02-24 02:00:03 +000093 }
94 } else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020095#endif /* CONFIG_SYS_DIRECT_FLASH_NFS */
wdenkcbd8a352004-02-24 02:00:03 +000096 {
Joe Hershberger55d5fd92015-03-22 17:09:08 -050097 void *ptr = map_sysmem(load_addr + offset, len);
98
99 memcpy(ptr, src, len);
100 unmap_sysmem(ptr);
wdenkcbd8a352004-02-24 02:00:03 +0000101 }
wdenka084f7d2004-02-24 22:33:21 +0000102
103 if (NetBootFileXferSize < (offset+len))
104 NetBootFileXferSize = newsize;
Wolfgang Denk23a7a322005-08-06 01:11:12 +0200105 return 0;
wdenkcbd8a352004-02-24 02:00:03 +0000106}
107
108static char*
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000109basename(char *path)
wdenkcbd8a352004-02-24 02:00:03 +0000110{
111 char *fname;
112
113 fname = path + strlen(path) - 1;
114 while (fname >= path) {
115 if (*fname == '/') {
116 fname++;
117 break;
118 }
119 fname--;
120 }
121 return fname;
122}
123
124static char*
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000125dirname(char *path)
wdenkcbd8a352004-02-24 02:00:03 +0000126{
127 char *fname;
128
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000129 fname = basename(path);
wdenkcbd8a352004-02-24 02:00:03 +0000130 --fname;
131 *fname = '\0';
132 return path;
133}
134
135/**************************************************************************
wdenkcbd8a352004-02-24 02:00:03 +0000136RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
137**************************************************************************/
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000138static long *rpc_add_credentials(long *p)
wdenkcbd8a352004-02-24 02:00:03 +0000139{
140 int hl;
141 int hostnamelen;
142 char hostname[256];
143
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000144 strcpy(hostname, "");
145 hostnamelen = strlen(hostname);
wdenkcbd8a352004-02-24 02:00:03 +0000146
147 /* Here's the executive summary on authentication requirements of the
148 * various NFS server implementations: Linux accepts both AUTH_NONE
149 * and AUTH_UNIX authentication (also accepts an empty hostname field
150 * in the AUTH_UNIX scheme). *BSD refuses AUTH_NONE, but accepts
151 * AUTH_UNIX (also accepts an empty hostname field in the AUTH_UNIX
152 * scheme). To be safe, use AUTH_UNIX and pass the hostname if we have
153 * it (if the BOOTP/DHCP reply didn't give one, just use an empty
154 * hostname). */
155
156 hl = (hostnamelen + 3) & ~3;
157
158 /* Provide an AUTH_UNIX credential. */
159 *p++ = htonl(1); /* AUTH_UNIX */
160 *p++ = htonl(hl+20); /* auth length */
161 *p++ = htonl(0); /* stamp */
162 *p++ = htonl(hostnamelen); /* hostname string */
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000163 if (hostnamelen & 3)
wdenkcbd8a352004-02-24 02:00:03 +0000164 *(p + hostnamelen / 4) = 0; /* add zero padding */
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000165 memcpy(p, hostname, hostnamelen);
wdenkcbd8a352004-02-24 02:00:03 +0000166 p += hl / 4;
167 *p++ = 0; /* uid */
168 *p++ = 0; /* gid */
169 *p++ = 0; /* auxiliary gid list */
170
171 /* Provide an AUTH_NONE verifier. */
172 *p++ = 0; /* AUTH_NONE */
173 *p++ = 0; /* auth length */
174
175 return p;
176}
177
178/**************************************************************************
179RPC_LOOKUP - Lookup RPC Port numbers
180**************************************************************************/
181static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000182rpc_req(int rpc_prog, int rpc_proc, uint32_t *data, int datalen)
wdenkcbd8a352004-02-24 02:00:03 +0000183{
184 struct rpc_t pkt;
185 unsigned long id;
186 uint32_t *p;
187 int pktlen;
188 int sport;
189
wdenkc3f9d492004-03-14 00:59:59 +0000190 id = ++rpc_id;
wdenkcbd8a352004-02-24 02:00:03 +0000191 pkt.u.call.id = htonl(id);
192 pkt.u.call.type = htonl(MSG_CALL);
193 pkt.u.call.rpcvers = htonl(2); /* use RPC version 2 */
194 pkt.u.call.prog = htonl(rpc_prog);
195 pkt.u.call.vers = htonl(2); /* portmapper is version 2 */
196 pkt.u.call.proc = htonl(rpc_proc);
197 p = (uint32_t *)&(pkt.u.call.data);
198
199 if (datalen)
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000200 memcpy((char *)p, (char *)data, datalen*sizeof(uint32_t));
wdenkcbd8a352004-02-24 02:00:03 +0000201
202 pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt;
203
Joe Hershberger594c26f2012-05-23 07:58:04 +0000204 memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE,
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000205 (char *)&pkt, pktlen);
wdenkcbd8a352004-02-24 02:00:03 +0000206
207 if (rpc_prog == PROG_PORTMAP)
208 sport = SUNRPC_PORT;
209 else if (rpc_prog == PROG_MOUNT)
210 sport = NfsSrvMountPort;
211 else
212 sport = NfsSrvNfsPort;
213
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000214 NetSendUDPPacket(NetServerEther, NfsServerIP, sport, NfsOurPort,
215 pktlen);
wdenkcbd8a352004-02-24 02:00:03 +0000216}
217
218/**************************************************************************
219RPC_LOOKUP - Lookup RPC Port numbers
220**************************************************************************/
221static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000222rpc_lookup_req(int prog, int ver)
wdenkcbd8a352004-02-24 02:00:03 +0000223{
224 uint32_t data[16];
225
226 data[0] = 0; data[1] = 0; /* auth credential */
227 data[2] = 0; data[3] = 0; /* auth verifier */
228 data[4] = htonl(prog);
229 data[5] = htonl(ver);
230 data[6] = htonl(17); /* IP_UDP */
231 data[7] = 0;
232
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000233 rpc_req(PROG_PORTMAP, PORTMAP_GETPORT, data, 8);
wdenkcbd8a352004-02-24 02:00:03 +0000234}
235
236/**************************************************************************
237NFS_MOUNT - Mount an NFS Filesystem
238**************************************************************************/
239static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000240nfs_mount_req(char *path)
wdenkcbd8a352004-02-24 02:00:03 +0000241{
242 uint32_t data[1024];
243 uint32_t *p;
244 int len;
245 int pathlen;
246
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000247 pathlen = strlen(path);
wdenkcbd8a352004-02-24 02:00:03 +0000248
249 p = &(data[0]);
250 p = (uint32_t *)rpc_add_credentials((long *)p);
251
252 *p++ = htonl(pathlen);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000253 if (pathlen & 3)
254 *(p + pathlen / 4) = 0;
255 memcpy(p, path, pathlen);
wdenkcbd8a352004-02-24 02:00:03 +0000256 p += (pathlen + 3) / 4;
257
258 len = (uint32_t *)p - (uint32_t *)&(data[0]);
259
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000260 rpc_req(PROG_MOUNT, MOUNT_ADDENTRY, data, len);
wdenkcbd8a352004-02-24 02:00:03 +0000261}
262
263/**************************************************************************
264NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
265**************************************************************************/
266static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000267nfs_umountall_req(void)
wdenkcbd8a352004-02-24 02:00:03 +0000268{
269 uint32_t data[1024];
270 uint32_t *p;
271 int len;
272
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000273 if ((NfsSrvMountPort == -1) || (!fs_mounted))
wdenkcbd8a352004-02-24 02:00:03 +0000274 /* Nothing mounted, nothing to umount */
275 return;
wdenkcbd8a352004-02-24 02:00:03 +0000276
277 p = &(data[0]);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000278 p = (uint32_t *)rpc_add_credentials((long *)p);
wdenkcbd8a352004-02-24 02:00:03 +0000279
280 len = (uint32_t *)p - (uint32_t *)&(data[0]);
281
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000282 rpc_req(PROG_MOUNT, MOUNT_UMOUNTALL, data, len);
wdenkcbd8a352004-02-24 02:00:03 +0000283}
284
285/***************************************************************************
286 * NFS_READLINK (AH 2003-07-14)
287 * This procedure is called when read of the first block fails -
288 * this probably happens when it's a directory or a symlink
289 * In case of successful readlink(), the dirname is manipulated,
290 * so that inside the nfs() function a recursion can be done.
291 **************************************************************************/
292static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000293nfs_readlink_req(void)
wdenkcbd8a352004-02-24 02:00:03 +0000294{
295 uint32_t data[1024];
296 uint32_t *p;
297 int len;
298
299 p = &(data[0]);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000300 p = (uint32_t *)rpc_add_credentials((long *)p);
wdenkcbd8a352004-02-24 02:00:03 +0000301
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000302 memcpy(p, filefh, NFS_FHSIZE);
wdenkcbd8a352004-02-24 02:00:03 +0000303 p += (NFS_FHSIZE / 4);
304
305 len = (uint32_t *)p - (uint32_t *)&(data[0]);
306
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000307 rpc_req(PROG_NFS, NFS_READLINK, data, len);
wdenkcbd8a352004-02-24 02:00:03 +0000308}
309
310/**************************************************************************
311NFS_LOOKUP - Lookup Pathname
312**************************************************************************/
313static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000314nfs_lookup_req(char *fname)
wdenkcbd8a352004-02-24 02:00:03 +0000315{
316 uint32_t data[1024];
317 uint32_t *p;
318 int len;
319 int fnamelen;
320
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000321 fnamelen = strlen(fname);
wdenkcbd8a352004-02-24 02:00:03 +0000322
323 p = &(data[0]);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000324 p = (uint32_t *)rpc_add_credentials((long *)p);
wdenkcbd8a352004-02-24 02:00:03 +0000325
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000326 memcpy(p, dirfh, NFS_FHSIZE);
wdenkcbd8a352004-02-24 02:00:03 +0000327 p += (NFS_FHSIZE / 4);
328 *p++ = htonl(fnamelen);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000329 if (fnamelen & 3)
330 *(p + fnamelen / 4) = 0;
331 memcpy(p, fname, fnamelen);
wdenkcbd8a352004-02-24 02:00:03 +0000332 p += (fnamelen + 3) / 4;
333
334 len = (uint32_t *)p - (uint32_t *)&(data[0]);
335
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000336 rpc_req(PROG_NFS, NFS_LOOKUP, data, len);
wdenkcbd8a352004-02-24 02:00:03 +0000337}
338
339/**************************************************************************
340NFS_READ - Read File on NFS Server
341**************************************************************************/
342static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000343nfs_read_req(int offset, int readlen)
wdenkcbd8a352004-02-24 02:00:03 +0000344{
345 uint32_t data[1024];
346 uint32_t *p;
347 int len;
348
349 p = &(data[0]);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000350 p = (uint32_t *)rpc_add_credentials((long *)p);
wdenkcbd8a352004-02-24 02:00:03 +0000351
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000352 memcpy(p, filefh, NFS_FHSIZE);
wdenkcbd8a352004-02-24 02:00:03 +0000353 p += (NFS_FHSIZE / 4);
354 *p++ = htonl(offset);
355 *p++ = htonl(readlen);
356 *p++ = 0;
357
358 len = (uint32_t *)p - (uint32_t *)&(data[0]);
359
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000360 rpc_req(PROG_NFS, NFS_READ, data, len);
wdenkcbd8a352004-02-24 02:00:03 +0000361}
362
363/**************************************************************************
364RPC request dispatcher
365**************************************************************************/
366
367static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000368NfsSend(void)
wdenkcbd8a352004-02-24 02:00:03 +0000369{
Robin Getz0ebf04c2009-07-23 03:01:03 -0400370 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000371
372 switch (NfsState) {
373 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000374 rpc_lookup_req(PROG_MOUNT, 1);
wdenkcbd8a352004-02-24 02:00:03 +0000375 break;
376 case STATE_PRCLOOKUP_PROG_NFS_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000377 rpc_lookup_req(PROG_NFS, 2);
wdenkcbd8a352004-02-24 02:00:03 +0000378 break;
379 case STATE_MOUNT_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000380 nfs_mount_req(nfs_path);
wdenkcbd8a352004-02-24 02:00:03 +0000381 break;
382 case STATE_UMOUNT_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000383 nfs_umountall_req();
wdenkcbd8a352004-02-24 02:00:03 +0000384 break;
385 case STATE_LOOKUP_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000386 nfs_lookup_req(nfs_filename);
wdenkcbd8a352004-02-24 02:00:03 +0000387 break;
388 case STATE_READ_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000389 nfs_read_req(nfs_offset, nfs_len);
wdenkcbd8a352004-02-24 02:00:03 +0000390 break;
391 case STATE_READLINK_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000392 nfs_readlink_req();
wdenkcbd8a352004-02-24 02:00:03 +0000393 break;
394 }
395}
396
397/**************************************************************************
398Handlers for the reply from server
399**************************************************************************/
400
401static int
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000402rpc_lookup_reply(int prog, uchar *pkt, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000403{
404 struct rpc_t rpc_pkt;
405
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000406 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkcbd8a352004-02-24 02:00:03 +0000407
Robin Getz0ebf04c2009-07-23 03:01:03 -0400408 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000409
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100410 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
411 return -NFS_RPC_ERR;
412 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
413 return -NFS_RPC_DROP;
wdenkc3f9d492004-03-14 00:59:59 +0000414
wdenkcbd8a352004-02-24 02:00:03 +0000415 if (rpc_pkt.u.reply.rstatus ||
416 rpc_pkt.u.reply.verifier ||
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000417 rpc_pkt.u.reply.astatus)
wdenkc3f9d492004-03-14 00:59:59 +0000418 return -1;
wdenkcbd8a352004-02-24 02:00:03 +0000419
420 switch (prog) {
421 case PROG_MOUNT:
422 NfsSrvMountPort = ntohl(rpc_pkt.u.reply.data[0]);
423 break;
424 case PROG_NFS:
425 NfsSrvNfsPort = ntohl(rpc_pkt.u.reply.data[0]);
426 break;
427 }
428
429 return 0;
430}
431
432static int
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000433nfs_mount_reply(uchar *pkt, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000434{
435 struct rpc_t rpc_pkt;
436
Robin Getz0ebf04c2009-07-23 03:01:03 -0400437 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000438
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000439 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkcbd8a352004-02-24 02:00:03 +0000440
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100441 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
442 return -NFS_RPC_ERR;
443 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
444 return -NFS_RPC_DROP;
wdenkc3f9d492004-03-14 00:59:59 +0000445
wdenkcbd8a352004-02-24 02:00:03 +0000446 if (rpc_pkt.u.reply.rstatus ||
447 rpc_pkt.u.reply.verifier ||
448 rpc_pkt.u.reply.astatus ||
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000449 rpc_pkt.u.reply.data[0])
wdenkcbd8a352004-02-24 02:00:03 +0000450 return -1;
wdenkcbd8a352004-02-24 02:00:03 +0000451
452 fs_mounted = 1;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000453 memcpy(dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
wdenkcbd8a352004-02-24 02:00:03 +0000454
455 return 0;
456}
457
458static int
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000459nfs_umountall_reply(uchar *pkt, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000460{
461 struct rpc_t rpc_pkt;
462
Robin Getz0ebf04c2009-07-23 03:01:03 -0400463 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000464
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000465 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkcbd8a352004-02-24 02:00:03 +0000466
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100467 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
468 return -NFS_RPC_ERR;
469 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
470 return -NFS_RPC_DROP;
wdenkc3f9d492004-03-14 00:59:59 +0000471
wdenkcbd8a352004-02-24 02:00:03 +0000472 if (rpc_pkt.u.reply.rstatus ||
473 rpc_pkt.u.reply.verifier ||
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000474 rpc_pkt.u.reply.astatus)
wdenkcbd8a352004-02-24 02:00:03 +0000475 return -1;
wdenkcbd8a352004-02-24 02:00:03 +0000476
477 fs_mounted = 0;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000478 memset(dirfh, 0, sizeof(dirfh));
wdenkcbd8a352004-02-24 02:00:03 +0000479
480 return 0;
481}
482
483static int
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000484nfs_lookup_reply(uchar *pkt, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000485{
486 struct rpc_t rpc_pkt;
487
Robin Getz0ebf04c2009-07-23 03:01:03 -0400488 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000489
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000490 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkcbd8a352004-02-24 02:00:03 +0000491
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100492 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
493 return -NFS_RPC_ERR;
494 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
495 return -NFS_RPC_DROP;
wdenkc3f9d492004-03-14 00:59:59 +0000496
wdenkcbd8a352004-02-24 02:00:03 +0000497 if (rpc_pkt.u.reply.rstatus ||
498 rpc_pkt.u.reply.verifier ||
499 rpc_pkt.u.reply.astatus ||
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000500 rpc_pkt.u.reply.data[0])
wdenkcbd8a352004-02-24 02:00:03 +0000501 return -1;
wdenkcbd8a352004-02-24 02:00:03 +0000502
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000503 memcpy(filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
wdenkcbd8a352004-02-24 02:00:03 +0000504
505 return 0;
506}
507
508static int
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000509nfs_readlink_reply(uchar *pkt, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000510{
511 struct rpc_t rpc_pkt;
512 int rlen;
513
Robin Getz0ebf04c2009-07-23 03:01:03 -0400514 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000515
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000516 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkcbd8a352004-02-24 02:00:03 +0000517
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100518 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
519 return -NFS_RPC_ERR;
520 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
521 return -NFS_RPC_DROP;
wdenkc3f9d492004-03-14 00:59:59 +0000522
wdenkcbd8a352004-02-24 02:00:03 +0000523 if (rpc_pkt.u.reply.rstatus ||
524 rpc_pkt.u.reply.verifier ||
525 rpc_pkt.u.reply.astatus ||
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000526 rpc_pkt.u.reply.data[0])
wdenkcbd8a352004-02-24 02:00:03 +0000527 return -1;
wdenkcbd8a352004-02-24 02:00:03 +0000528
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000529 rlen = ntohl(rpc_pkt.u.reply.data[1]); /* new path length */
wdenkcbd8a352004-02-24 02:00:03 +0000530
531 if (*((char *)&(rpc_pkt.u.reply.data[2])) != '/') {
532 int pathlen;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000533 strcat(nfs_path, "/");
wdenkcbd8a352004-02-24 02:00:03 +0000534 pathlen = strlen(nfs_path);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000535 memcpy(nfs_path + pathlen, (uchar *)&(rpc_pkt.u.reply.data[2]),
536 rlen);
Ed Swarthoutf64ef9b2009-11-19 02:47:28 -0600537 nfs_path[pathlen + rlen] = 0;
wdenkcbd8a352004-02-24 02:00:03 +0000538 } else {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000539 memcpy(nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen);
wdenkcbd8a352004-02-24 02:00:03 +0000540 nfs_path[rlen] = 0;
541 }
542 return 0;
543}
544
545static int
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000546nfs_read_reply(uchar *pkt, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000547{
548 struct rpc_t rpc_pkt;
549 int rlen;
550
Robin Getz0ebf04c2009-07-23 03:01:03 -0400551 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000552
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000553 memcpy((uchar *)&rpc_pkt, pkt, sizeof(rpc_pkt.u.reply));
wdenkcbd8a352004-02-24 02:00:03 +0000554
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100555 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
556 return -NFS_RPC_ERR;
557 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
558 return -NFS_RPC_DROP;
wdenkc3f9d492004-03-14 00:59:59 +0000559
wdenkcbd8a352004-02-24 02:00:03 +0000560 if (rpc_pkt.u.reply.rstatus ||
561 rpc_pkt.u.reply.verifier ||
562 rpc_pkt.u.reply.astatus ||
563 rpc_pkt.u.reply.data[0]) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000564 if (rpc_pkt.u.reply.rstatus)
wdenkcbd8a352004-02-24 02:00:03 +0000565 return -9999;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000566 if (rpc_pkt.u.reply.astatus)
wdenkcbd8a352004-02-24 02:00:03 +0000567 return -9999;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000568 return -ntohl(rpc_pkt.u.reply.data[0]);
wdenkcbd8a352004-02-24 02:00:03 +0000569 }
570
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000571 if ((nfs_offset != 0) && !((nfs_offset) %
572 (NFS_READ_SIZE / 2 * 10 * HASHES_PER_LINE)))
573 puts("\n\t ");
574 if (!(nfs_offset % ((NFS_READ_SIZE / 2) * 10)))
575 putc('#');
wdenkcbd8a352004-02-24 02:00:03 +0000576
577 rlen = ntohl(rpc_pkt.u.reply.data[18]);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000578 if (store_block((uchar *)pkt + sizeof(rpc_pkt.u.reply),
579 nfs_offset, rlen))
Wolfgang Denk23a7a322005-08-06 01:11:12 +0200580 return -9999;
wdenkcbd8a352004-02-24 02:00:03 +0000581
582 return rlen;
583}
584
585/**************************************************************************
586Interfaces of U-BOOT
587**************************************************************************/
588
589static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000590NfsTimeout(void)
wdenka5725fa2004-09-28 21:51:42 +0000591{
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000592 if (++NfsTimeoutCount > NFS_RETRY_COUNT) {
593 puts("\nRetry count exceeded; starting again\n");
594 NetStartAgain();
Evan Samanasaabb8cb2009-11-09 20:08:36 -0600595 } else {
596 puts("T ");
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100597 NetSetTimeout(nfs_timeout + NFS_TIMEOUT * NfsTimeoutCount,
598 NfsTimeout);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000599 NfsSend();
Hiroshi Itofe891ec2008-01-31 18:35:04 +0900600 }
wdenka5725fa2004-09-28 21:51:42 +0000601}
602
603static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000604NfsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000605{
606 int rlen;
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100607 int reply;
wdenkcbd8a352004-02-24 02:00:03 +0000608
Robin Getz0ebf04c2009-07-23 03:01:03 -0400609 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000610
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000611 if (dest != NfsOurPort)
612 return;
wdenkcbd8a352004-02-24 02:00:03 +0000613
614 switch (NfsState) {
615 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100616 if (rpc_lookup_reply(PROG_MOUNT, pkt, len) == -NFS_RPC_DROP)
617 break;
wdenkcbd8a352004-02-24 02:00:03 +0000618 NfsState = STATE_PRCLOOKUP_PROG_NFS_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000619 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000620 break;
621
622 case STATE_PRCLOOKUP_PROG_NFS_REQ:
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100623 if (rpc_lookup_reply(PROG_NFS, pkt, len) == -NFS_RPC_DROP)
624 break;
wdenkcbd8a352004-02-24 02:00:03 +0000625 NfsState = STATE_MOUNT_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000626 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000627 break;
628
629 case STATE_MOUNT_REQ:
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100630 reply = nfs_mount_reply(pkt, len);
631 if (reply == -NFS_RPC_DROP)
632 break;
633 else if (reply == -NFS_RPC_ERR) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000634 puts("*** ERROR: Cannot mount\n");
wdenkcbd8a352004-02-24 02:00:03 +0000635 /* just to be sure... */
636 NfsState = STATE_UMOUNT_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000637 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000638 } else {
639 NfsState = STATE_LOOKUP_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000640 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000641 }
642 break;
643
644 case STATE_UMOUNT_REQ:
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100645 reply = nfs_umountall_reply(pkt, len);
646 if (reply == -NFS_RPC_DROP)
647 break;
648 else if (reply == -NFS_RPC_ERR) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000649 puts("*** ERROR: Cannot umount\n");
Joe Hershberger22f6e992012-05-23 07:59:14 +0000650 net_set_state(NETLOOP_FAIL);
wdenkcbd8a352004-02-24 02:00:03 +0000651 } else {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000652 puts("\ndone\n");
Joe Hershberger22f6e992012-05-23 07:59:14 +0000653 net_set_state(nfs_download_state);
wdenkcbd8a352004-02-24 02:00:03 +0000654 }
655 break;
656
657 case STATE_LOOKUP_REQ:
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100658 reply = nfs_lookup_reply(pkt, len);
659 if (reply == -NFS_RPC_DROP)
660 break;
661 else if (reply == -NFS_RPC_ERR) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000662 puts("*** ERROR: File lookup fail\n");
wdenkcbd8a352004-02-24 02:00:03 +0000663 NfsState = STATE_UMOUNT_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000664 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000665 } else {
666 NfsState = STATE_READ_REQ;
667 nfs_offset = 0;
668 nfs_len = NFS_READ_SIZE;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000669 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000670 }
671 break;
672
673 case STATE_READLINK_REQ:
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100674 reply = nfs_readlink_reply(pkt, len);
675 if (reply == -NFS_RPC_DROP)
676 break;
677 else if (reply == -NFS_RPC_ERR) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000678 puts("*** ERROR: Symlink fail\n");
wdenkcbd8a352004-02-24 02:00:03 +0000679 NfsState = STATE_UMOUNT_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000680 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000681 } else {
Robin Getz0ebf04c2009-07-23 03:01:03 -0400682 debug("Symlink --> %s\n", nfs_path);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000683 nfs_filename = basename(nfs_path);
684 nfs_path = dirname(nfs_path);
wdenkcbd8a352004-02-24 02:00:03 +0000685
686 NfsState = STATE_MOUNT_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000687 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000688 }
689 break;
690
691 case STATE_READ_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000692 rlen = nfs_read_reply(pkt, len);
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100693 NetSetTimeout(nfs_timeout, NfsTimeout);
wdenkcbd8a352004-02-24 02:00:03 +0000694 if (rlen > 0) {
695 nfs_offset += rlen;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000696 NfsSend();
697 } else if ((rlen == -NFSERR_ISDIR) || (rlen == -NFSERR_INVAL)) {
wdenkcbd8a352004-02-24 02:00:03 +0000698 /* symbolic link */
699 NfsState = STATE_READLINK_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000700 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000701 } else {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000702 if (!rlen)
Joe Hershberger22f6e992012-05-23 07:59:14 +0000703 nfs_download_state = NETLOOP_SUCCESS;
wdenkcbd8a352004-02-24 02:00:03 +0000704 NfsState = STATE_UMOUNT_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000705 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000706 }
707 break;
708 }
709}
710
wdenkcbd8a352004-02-24 02:00:03 +0000711
712void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000713NfsStart(void)
wdenkcbd8a352004-02-24 02:00:03 +0000714{
Robin Getz0ebf04c2009-07-23 03:01:03 -0400715 debug("%s\n", __func__);
Joe Hershberger22f6e992012-05-23 07:59:14 +0000716 nfs_download_state = NETLOOP_FAIL;
wdenkcbd8a352004-02-24 02:00:03 +0000717
718 NfsServerIP = NetServerIP;
719 nfs_path = (char *)nfs_path_buff;
720
721 if (nfs_path == NULL) {
Joe Hershberger22f6e992012-05-23 07:59:14 +0000722 net_set_state(NETLOOP_FAIL);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000723 puts("*** ERROR: Fail allocate memory\n");
wdenkcbd8a352004-02-24 02:00:03 +0000724 return;
725 }
726
727 if (BootFile[0] == '\0') {
Matthias Weisserea45cb02011-12-03 03:29:44 +0000728 sprintf(default_filename, "/nfsroot/%02X%02X%02X%02X.img",
Wolfgang Denkc43352c2005-08-04 01:09:44 +0200729 NetOurIP & 0xFF,
730 (NetOurIP >> 8) & 0xFF,
731 (NetOurIP >> 16) & 0xFF,
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000732 (NetOurIP >> 24) & 0xFF);
733 strcpy(nfs_path, default_filename);
wdenkcbd8a352004-02-24 02:00:03 +0000734
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000735 printf("*** Warning: no boot file name; using '%s'\n",
wdenkcbd8a352004-02-24 02:00:03 +0000736 nfs_path);
737 } else {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000738 char *p = BootFile;
wdenkcbd8a352004-02-24 02:00:03 +0000739
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000740 p = strchr(p, ':');
wdenkcbd8a352004-02-24 02:00:03 +0000741
742 if (p != NULL) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000743 NfsServerIP = string_to_ip(BootFile);
wdenkcbd8a352004-02-24 02:00:03 +0000744 ++p;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000745 strcpy(nfs_path, p);
wdenkcbd8a352004-02-24 02:00:03 +0000746 } else {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000747 strcpy(nfs_path, BootFile);
wdenkcbd8a352004-02-24 02:00:03 +0000748 }
749 }
750
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000751 nfs_filename = basename(nfs_path);
752 nfs_path = dirname(nfs_path);
wdenkcbd8a352004-02-24 02:00:03 +0000753
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000754 printf("Using %s device\n", eth_get_name());
wdenkcbd8a352004-02-24 02:00:03 +0000755
Mike Frysingerb6446b62009-02-17 00:00:53 -0500756 printf("File transfer via NFS from server %pI4"
757 "; our IP address is %pI4", &NfsServerIP, &NetOurIP);
wdenkcbd8a352004-02-24 02:00:03 +0000758
759 /* Check if we need to send across this subnet */
760 if (NetOurGatewayIP && NetOurSubnetMask) {
761 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
762 IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask;
763
Mike Frysingerb6446b62009-02-17 00:00:53 -0500764 if (OurNet != ServerNet)
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000765 printf("; sending through gateway %pI4",
766 &NetOurGatewayIP);
wdenkcbd8a352004-02-24 02:00:03 +0000767 }
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000768 printf("\nFilename '%s/%s'.", nfs_path, nfs_filename);
wdenkcbd8a352004-02-24 02:00:03 +0000769
770 if (NetBootFileSize) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000771 printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
772 print_size(NetBootFileSize<<9, "");
wdenkcbd8a352004-02-24 02:00:03 +0000773 }
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000774 printf("\nLoad address: 0x%lx\n"
wdenk4b9206e2004-03-23 22:14:11 +0000775 "Loading: *\b", load_addr);
wdenkcbd8a352004-02-24 02:00:03 +0000776
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100777 NetSetTimeout(nfs_timeout, NfsTimeout);
Joe Hershbergerece223b2012-05-23 07:59:15 +0000778 net_set_udp_handler(NfsHandler);
wdenkcbd8a352004-02-24 02:00:03 +0000779
wdenkcbd8a352004-02-24 02:00:03 +0000780 NfsTimeoutCount = 0;
781 NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
782
783 /*NfsOurPort = 4096 + (get_ticks() % 3072);*/
784 /*FIX ME !!!*/
785 NfsOurPort = 1000;
786
787 /* zero out server ether in case the server ip has changed */
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000788 memset(NetServerEther, 0, 6);
wdenkcbd8a352004-02-24 02:00:03 +0000789
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000790 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000791}