blob: 54f56c4611cde41091465cc8fa37401912aa05b5 [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
wdenkcbd8a352004-02-24 02:00:03 +000032#define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
Hiroshi Itofe891ec2008-01-31 18:35:04 +090033#define NFS_RETRY_COUNT 30
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +020034#define NFS_TIMEOUT 2000UL
wdenkcbd8a352004-02-24 02:00:03 +000035
Joe Hershbergerc9f6c912012-05-15 08:59:09 +000036static int fs_mounted;
37static unsigned long rpc_id;
wdenkcbd8a352004-02-24 02:00:03 +000038static int nfs_offset = -1;
39static int nfs_len;
40
41static char dirfh[NFS_FHSIZE]; /* file handle of directory */
42static char filefh[NFS_FHSIZE]; /* file handle of kernel image */
43
Wolfgang Denk23a7a322005-08-06 01:11:12 +020044static int NfsDownloadState;
wdenkcbd8a352004-02-24 02:00:03 +000045static IPaddr_t NfsServerIP;
46static int NfsSrvMountPort;
47static int NfsSrvNfsPort;
48static int NfsOurPort;
49static int NfsTimeoutCount;
50static int NfsState;
51#define STATE_PRCLOOKUP_PROG_MOUNT_REQ 1
52#define STATE_PRCLOOKUP_PROG_NFS_REQ 2
53#define STATE_MOUNT_REQ 3
54#define STATE_UMOUNT_REQ 4
55#define STATE_LOOKUP_REQ 5
56#define STATE_READ_REQ 6
57#define STATE_READLINK_REQ 7
58
59static char default_filename[64];
60static char *nfs_filename;
61static char *nfs_path;
62static char nfs_path_buff[2048];
63
Joe Hershbergerc9f6c912012-05-15 08:59:09 +000064static inline int
65store_block(uchar *src, unsigned offset, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +000066{
wdenka084f7d2004-02-24 22:33:21 +000067 ulong newsize = offset + len;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020068#ifdef CONFIG_SYS_DIRECT_FLASH_NFS
wdenkcbd8a352004-02-24 02:00:03 +000069 int i, rc = 0;
70
Joe Hershbergerc9f6c912012-05-15 08:59:09 +000071 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenkcbd8a352004-02-24 02:00:03 +000072 /* start address in flash? */
73 if (load_addr + offset >= flash_info[i].start[0]) {
74 rc = 1;
75 break;
76 }
77 }
78
79 if (rc) { /* Flash is destination for this packet */
Joe Hershbergerc9f6c912012-05-15 08:59:09 +000080 rc = flash_write((uchar *)src, (ulong)(load_addr+offset), len);
wdenkcbd8a352004-02-24 02:00:03 +000081 if (rc) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +000082 flash_perror(rc);
Wolfgang Denk23a7a322005-08-06 01:11:12 +020083 return -1;
wdenkcbd8a352004-02-24 02:00:03 +000084 }
85 } else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020086#endif /* CONFIG_SYS_DIRECT_FLASH_NFS */
wdenkcbd8a352004-02-24 02:00:03 +000087 {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +000088 (void)memcpy((void *)(load_addr + offset), src, len);
wdenkcbd8a352004-02-24 02:00:03 +000089 }
wdenka084f7d2004-02-24 22:33:21 +000090
91 if (NetBootFileXferSize < (offset+len))
92 NetBootFileXferSize = newsize;
Wolfgang Denk23a7a322005-08-06 01:11:12 +020093 return 0;
wdenkcbd8a352004-02-24 02:00:03 +000094}
95
96static char*
Joe Hershbergerc9f6c912012-05-15 08:59:09 +000097basename(char *path)
wdenkcbd8a352004-02-24 02:00:03 +000098{
99 char *fname;
100
101 fname = path + strlen(path) - 1;
102 while (fname >= path) {
103 if (*fname == '/') {
104 fname++;
105 break;
106 }
107 fname--;
108 }
109 return fname;
110}
111
112static char*
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000113dirname(char *path)
wdenkcbd8a352004-02-24 02:00:03 +0000114{
115 char *fname;
116
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000117 fname = basename(path);
wdenkcbd8a352004-02-24 02:00:03 +0000118 --fname;
119 *fname = '\0';
120 return path;
121}
122
123/**************************************************************************
wdenkcbd8a352004-02-24 02:00:03 +0000124RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
125**************************************************************************/
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000126static long *rpc_add_credentials(long *p)
wdenkcbd8a352004-02-24 02:00:03 +0000127{
128 int hl;
129 int hostnamelen;
130 char hostname[256];
131
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000132 strcpy(hostname, "");
133 hostnamelen = strlen(hostname);
wdenkcbd8a352004-02-24 02:00:03 +0000134
135 /* Here's the executive summary on authentication requirements of the
136 * various NFS server implementations: Linux accepts both AUTH_NONE
137 * and AUTH_UNIX authentication (also accepts an empty hostname field
138 * in the AUTH_UNIX scheme). *BSD refuses AUTH_NONE, but accepts
139 * AUTH_UNIX (also accepts an empty hostname field in the AUTH_UNIX
140 * scheme). To be safe, use AUTH_UNIX and pass the hostname if we have
141 * it (if the BOOTP/DHCP reply didn't give one, just use an empty
142 * hostname). */
143
144 hl = (hostnamelen + 3) & ~3;
145
146 /* Provide an AUTH_UNIX credential. */
147 *p++ = htonl(1); /* AUTH_UNIX */
148 *p++ = htonl(hl+20); /* auth length */
149 *p++ = htonl(0); /* stamp */
150 *p++ = htonl(hostnamelen); /* hostname string */
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000151 if (hostnamelen & 3)
wdenkcbd8a352004-02-24 02:00:03 +0000152 *(p + hostnamelen / 4) = 0; /* add zero padding */
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000153 memcpy(p, hostname, hostnamelen);
wdenkcbd8a352004-02-24 02:00:03 +0000154 p += hl / 4;
155 *p++ = 0; /* uid */
156 *p++ = 0; /* gid */
157 *p++ = 0; /* auxiliary gid list */
158
159 /* Provide an AUTH_NONE verifier. */
160 *p++ = 0; /* AUTH_NONE */
161 *p++ = 0; /* auth length */
162
163 return p;
164}
165
166/**************************************************************************
167RPC_LOOKUP - Lookup RPC Port numbers
168**************************************************************************/
169static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000170rpc_req(int rpc_prog, int rpc_proc, uint32_t *data, int datalen)
wdenkcbd8a352004-02-24 02:00:03 +0000171{
172 struct rpc_t pkt;
173 unsigned long id;
174 uint32_t *p;
175 int pktlen;
176 int sport;
177
wdenkc3f9d492004-03-14 00:59:59 +0000178 id = ++rpc_id;
wdenkcbd8a352004-02-24 02:00:03 +0000179 pkt.u.call.id = htonl(id);
180 pkt.u.call.type = htonl(MSG_CALL);
181 pkt.u.call.rpcvers = htonl(2); /* use RPC version 2 */
182 pkt.u.call.prog = htonl(rpc_prog);
183 pkt.u.call.vers = htonl(2); /* portmapper is version 2 */
184 pkt.u.call.proc = htonl(rpc_proc);
185 p = (uint32_t *)&(pkt.u.call.data);
186
187 if (datalen)
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000188 memcpy((char *)p, (char *)data, datalen*sizeof(uint32_t));
wdenkcbd8a352004-02-24 02:00:03 +0000189
190 pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt;
191
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000192 memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE,
193 (char *)&pkt, pktlen);
wdenkcbd8a352004-02-24 02:00:03 +0000194
195 if (rpc_prog == PROG_PORTMAP)
196 sport = SUNRPC_PORT;
197 else if (rpc_prog == PROG_MOUNT)
198 sport = NfsSrvMountPort;
199 else
200 sport = NfsSrvNfsPort;
201
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000202 NetSendUDPPacket(NetServerEther, NfsServerIP, sport, NfsOurPort,
203 pktlen);
wdenkcbd8a352004-02-24 02:00:03 +0000204}
205
206/**************************************************************************
207RPC_LOOKUP - Lookup RPC Port numbers
208**************************************************************************/
209static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000210rpc_lookup_req(int prog, int ver)
wdenkcbd8a352004-02-24 02:00:03 +0000211{
212 uint32_t data[16];
213
214 data[0] = 0; data[1] = 0; /* auth credential */
215 data[2] = 0; data[3] = 0; /* auth verifier */
216 data[4] = htonl(prog);
217 data[5] = htonl(ver);
218 data[6] = htonl(17); /* IP_UDP */
219 data[7] = 0;
220
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000221 rpc_req(PROG_PORTMAP, PORTMAP_GETPORT, data, 8);
wdenkcbd8a352004-02-24 02:00:03 +0000222}
223
224/**************************************************************************
225NFS_MOUNT - Mount an NFS Filesystem
226**************************************************************************/
227static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000228nfs_mount_req(char *path)
wdenkcbd8a352004-02-24 02:00:03 +0000229{
230 uint32_t data[1024];
231 uint32_t *p;
232 int len;
233 int pathlen;
234
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000235 pathlen = strlen(path);
wdenkcbd8a352004-02-24 02:00:03 +0000236
237 p = &(data[0]);
238 p = (uint32_t *)rpc_add_credentials((long *)p);
239
240 *p++ = htonl(pathlen);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000241 if (pathlen & 3)
242 *(p + pathlen / 4) = 0;
243 memcpy(p, path, pathlen);
wdenkcbd8a352004-02-24 02:00:03 +0000244 p += (pathlen + 3) / 4;
245
246 len = (uint32_t *)p - (uint32_t *)&(data[0]);
247
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000248 rpc_req(PROG_MOUNT, MOUNT_ADDENTRY, data, len);
wdenkcbd8a352004-02-24 02:00:03 +0000249}
250
251/**************************************************************************
252NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
253**************************************************************************/
254static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000255nfs_umountall_req(void)
wdenkcbd8a352004-02-24 02:00:03 +0000256{
257 uint32_t data[1024];
258 uint32_t *p;
259 int len;
260
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000261 if ((NfsSrvMountPort == -1) || (!fs_mounted))
wdenkcbd8a352004-02-24 02:00:03 +0000262 /* Nothing mounted, nothing to umount */
263 return;
wdenkcbd8a352004-02-24 02:00:03 +0000264
265 p = &(data[0]);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000266 p = (uint32_t *)rpc_add_credentials((long *)p);
wdenkcbd8a352004-02-24 02:00:03 +0000267
268 len = (uint32_t *)p - (uint32_t *)&(data[0]);
269
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000270 rpc_req(PROG_MOUNT, MOUNT_UMOUNTALL, data, len);
wdenkcbd8a352004-02-24 02:00:03 +0000271}
272
273/***************************************************************************
274 * NFS_READLINK (AH 2003-07-14)
275 * This procedure is called when read of the first block fails -
276 * this probably happens when it's a directory or a symlink
277 * In case of successful readlink(), the dirname is manipulated,
278 * so that inside the nfs() function a recursion can be done.
279 **************************************************************************/
280static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000281nfs_readlink_req(void)
wdenkcbd8a352004-02-24 02:00:03 +0000282{
283 uint32_t data[1024];
284 uint32_t *p;
285 int len;
286
287 p = &(data[0]);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000288 p = (uint32_t *)rpc_add_credentials((long *)p);
wdenkcbd8a352004-02-24 02:00:03 +0000289
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000290 memcpy(p, filefh, NFS_FHSIZE);
wdenkcbd8a352004-02-24 02:00:03 +0000291 p += (NFS_FHSIZE / 4);
292
293 len = (uint32_t *)p - (uint32_t *)&(data[0]);
294
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000295 rpc_req(PROG_NFS, NFS_READLINK, data, len);
wdenkcbd8a352004-02-24 02:00:03 +0000296}
297
298/**************************************************************************
299NFS_LOOKUP - Lookup Pathname
300**************************************************************************/
301static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000302nfs_lookup_req(char *fname)
wdenkcbd8a352004-02-24 02:00:03 +0000303{
304 uint32_t data[1024];
305 uint32_t *p;
306 int len;
307 int fnamelen;
308
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000309 fnamelen = strlen(fname);
wdenkcbd8a352004-02-24 02:00:03 +0000310
311 p = &(data[0]);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000312 p = (uint32_t *)rpc_add_credentials((long *)p);
wdenkcbd8a352004-02-24 02:00:03 +0000313
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000314 memcpy(p, dirfh, NFS_FHSIZE);
wdenkcbd8a352004-02-24 02:00:03 +0000315 p += (NFS_FHSIZE / 4);
316 *p++ = htonl(fnamelen);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000317 if (fnamelen & 3)
318 *(p + fnamelen / 4) = 0;
319 memcpy(p, fname, fnamelen);
wdenkcbd8a352004-02-24 02:00:03 +0000320 p += (fnamelen + 3) / 4;
321
322 len = (uint32_t *)p - (uint32_t *)&(data[0]);
323
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000324 rpc_req(PROG_NFS, NFS_LOOKUP, data, len);
wdenkcbd8a352004-02-24 02:00:03 +0000325}
326
327/**************************************************************************
328NFS_READ - Read File on NFS Server
329**************************************************************************/
330static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000331nfs_read_req(int offset, int readlen)
wdenkcbd8a352004-02-24 02:00:03 +0000332{
333 uint32_t data[1024];
334 uint32_t *p;
335 int len;
336
337 p = &(data[0]);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000338 p = (uint32_t *)rpc_add_credentials((long *)p);
wdenkcbd8a352004-02-24 02:00:03 +0000339
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000340 memcpy(p, filefh, NFS_FHSIZE);
wdenkcbd8a352004-02-24 02:00:03 +0000341 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
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000348 rpc_req(PROG_NFS, NFS_READ, data, len);
wdenkcbd8a352004-02-24 02:00:03 +0000349}
350
351/**************************************************************************
352RPC request dispatcher
353**************************************************************************/
354
355static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000356NfsSend(void)
wdenkcbd8a352004-02-24 02:00:03 +0000357{
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:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000362 rpc_lookup_req(PROG_MOUNT, 1);
wdenkcbd8a352004-02-24 02:00:03 +0000363 break;
364 case STATE_PRCLOOKUP_PROG_NFS_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000365 rpc_lookup_req(PROG_NFS, 2);
wdenkcbd8a352004-02-24 02:00:03 +0000366 break;
367 case STATE_MOUNT_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000368 nfs_mount_req(nfs_path);
wdenkcbd8a352004-02-24 02:00:03 +0000369 break;
370 case STATE_UMOUNT_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000371 nfs_umountall_req();
wdenkcbd8a352004-02-24 02:00:03 +0000372 break;
373 case STATE_LOOKUP_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000374 nfs_lookup_req(nfs_filename);
wdenkcbd8a352004-02-24 02:00:03 +0000375 break;
376 case STATE_READ_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000377 nfs_read_req(nfs_offset, nfs_len);
wdenkcbd8a352004-02-24 02:00:03 +0000378 break;
379 case STATE_READLINK_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000380 nfs_readlink_req();
wdenkcbd8a352004-02-24 02:00:03 +0000381 break;
382 }
383}
384
385/**************************************************************************
386Handlers for the reply from server
387**************************************************************************/
388
389static int
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000390rpc_lookup_reply(int prog, uchar *pkt, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000391{
392 struct rpc_t rpc_pkt;
393
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000394 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkcbd8a352004-02-24 02:00:03 +0000395
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 ||
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000403 rpc_pkt.u.reply.astatus)
wdenkc3f9d492004-03-14 00:59:59 +0000404 return -1;
wdenkcbd8a352004-02-24 02:00:03 +0000405
406 switch (prog) {
407 case PROG_MOUNT:
408 NfsSrvMountPort = ntohl(rpc_pkt.u.reply.data[0]);
409 break;
410 case PROG_NFS:
411 NfsSrvNfsPort = ntohl(rpc_pkt.u.reply.data[0]);
412 break;
413 }
414
415 return 0;
416}
417
418static int
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000419nfs_mount_reply(uchar *pkt, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000420{
421 struct rpc_t rpc_pkt;
422
Robin Getz0ebf04c2009-07-23 03:01:03 -0400423 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000424
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000425 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkcbd8a352004-02-24 02:00:03 +0000426
wdenkc3f9d492004-03-14 00:59:59 +0000427 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
428 return -1;
429
wdenkcbd8a352004-02-24 02:00:03 +0000430 if (rpc_pkt.u.reply.rstatus ||
431 rpc_pkt.u.reply.verifier ||
432 rpc_pkt.u.reply.astatus ||
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000433 rpc_pkt.u.reply.data[0])
wdenkcbd8a352004-02-24 02:00:03 +0000434 return -1;
wdenkcbd8a352004-02-24 02:00:03 +0000435
436 fs_mounted = 1;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000437 memcpy(dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
wdenkcbd8a352004-02-24 02:00:03 +0000438
439 return 0;
440}
441
442static int
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000443nfs_umountall_reply(uchar *pkt, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000444{
445 struct rpc_t rpc_pkt;
446
Robin Getz0ebf04c2009-07-23 03:01:03 -0400447 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000448
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000449 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkcbd8a352004-02-24 02:00:03 +0000450
wdenkc3f9d492004-03-14 00:59:59 +0000451 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
452 return -1;
453
wdenkcbd8a352004-02-24 02:00:03 +0000454 if (rpc_pkt.u.reply.rstatus ||
455 rpc_pkt.u.reply.verifier ||
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000456 rpc_pkt.u.reply.astatus)
wdenkcbd8a352004-02-24 02:00:03 +0000457 return -1;
wdenkcbd8a352004-02-24 02:00:03 +0000458
459 fs_mounted = 0;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000460 memset(dirfh, 0, sizeof(dirfh));
wdenkcbd8a352004-02-24 02:00:03 +0000461
462 return 0;
463}
464
465static int
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000466nfs_lookup_reply(uchar *pkt, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000467{
468 struct rpc_t rpc_pkt;
469
Robin Getz0ebf04c2009-07-23 03:01:03 -0400470 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000471
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000472 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkcbd8a352004-02-24 02:00:03 +0000473
wdenkc3f9d492004-03-14 00:59:59 +0000474 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
475 return -1;
476
wdenkcbd8a352004-02-24 02:00:03 +0000477 if (rpc_pkt.u.reply.rstatus ||
478 rpc_pkt.u.reply.verifier ||
479 rpc_pkt.u.reply.astatus ||
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000480 rpc_pkt.u.reply.data[0])
wdenkcbd8a352004-02-24 02:00:03 +0000481 return -1;
wdenkcbd8a352004-02-24 02:00:03 +0000482
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000483 memcpy(filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
wdenkcbd8a352004-02-24 02:00:03 +0000484
485 return 0;
486}
487
488static int
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000489nfs_readlink_reply(uchar *pkt, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000490{
491 struct rpc_t rpc_pkt;
492 int rlen;
493
Robin Getz0ebf04c2009-07-23 03:01:03 -0400494 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000495
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000496 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkcbd8a352004-02-24 02:00:03 +0000497
wdenkc3f9d492004-03-14 00:59:59 +0000498 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
499 return -1;
500
wdenkcbd8a352004-02-24 02:00:03 +0000501 if (rpc_pkt.u.reply.rstatus ||
502 rpc_pkt.u.reply.verifier ||
503 rpc_pkt.u.reply.astatus ||
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000504 rpc_pkt.u.reply.data[0])
wdenkcbd8a352004-02-24 02:00:03 +0000505 return -1;
wdenkcbd8a352004-02-24 02:00:03 +0000506
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000507 rlen = ntohl(rpc_pkt.u.reply.data[1]); /* new path length */
wdenkcbd8a352004-02-24 02:00:03 +0000508
509 if (*((char *)&(rpc_pkt.u.reply.data[2])) != '/') {
510 int pathlen;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000511 strcat(nfs_path, "/");
wdenkcbd8a352004-02-24 02:00:03 +0000512 pathlen = strlen(nfs_path);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000513 memcpy(nfs_path + pathlen, (uchar *)&(rpc_pkt.u.reply.data[2]),
514 rlen);
Ed Swarthoutf64ef9b2009-11-19 02:47:28 -0600515 nfs_path[pathlen + rlen] = 0;
wdenkcbd8a352004-02-24 02:00:03 +0000516 } else {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000517 memcpy(nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen);
wdenkcbd8a352004-02-24 02:00:03 +0000518 nfs_path[rlen] = 0;
519 }
520 return 0;
521}
522
523static int
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000524nfs_read_reply(uchar *pkt, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000525{
526 struct rpc_t rpc_pkt;
527 int rlen;
528
Robin Getz0ebf04c2009-07-23 03:01:03 -0400529 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000530
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000531 memcpy((uchar *)&rpc_pkt, pkt, sizeof(rpc_pkt.u.reply));
wdenkcbd8a352004-02-24 02:00:03 +0000532
wdenkc3f9d492004-03-14 00:59:59 +0000533 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
534 return -1;
535
wdenkcbd8a352004-02-24 02:00:03 +0000536 if (rpc_pkt.u.reply.rstatus ||
537 rpc_pkt.u.reply.verifier ||
538 rpc_pkt.u.reply.astatus ||
539 rpc_pkt.u.reply.data[0]) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000540 if (rpc_pkt.u.reply.rstatus)
wdenkcbd8a352004-02-24 02:00:03 +0000541 return -9999;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000542 if (rpc_pkt.u.reply.astatus)
wdenkcbd8a352004-02-24 02:00:03 +0000543 return -9999;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000544 return -ntohl(rpc_pkt.u.reply.data[0]);
wdenkcbd8a352004-02-24 02:00:03 +0000545 }
546
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000547 if ((nfs_offset != 0) && !((nfs_offset) %
548 (NFS_READ_SIZE / 2 * 10 * HASHES_PER_LINE)))
549 puts("\n\t ");
550 if (!(nfs_offset % ((NFS_READ_SIZE / 2) * 10)))
551 putc('#');
wdenkcbd8a352004-02-24 02:00:03 +0000552
553 rlen = ntohl(rpc_pkt.u.reply.data[18]);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000554 if (store_block((uchar *)pkt + sizeof(rpc_pkt.u.reply),
555 nfs_offset, rlen))
Wolfgang Denk23a7a322005-08-06 01:11:12 +0200556 return -9999;
wdenkcbd8a352004-02-24 02:00:03 +0000557
558 return rlen;
559}
560
561/**************************************************************************
562Interfaces of U-BOOT
563**************************************************************************/
564
565static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000566NfsTimeout(void)
wdenka5725fa2004-09-28 21:51:42 +0000567{
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000568 if (++NfsTimeoutCount > NFS_RETRY_COUNT) {
569 puts("\nRetry count exceeded; starting again\n");
570 NetStartAgain();
Evan Samanasaabb8cb2009-11-09 20:08:36 -0600571 } else {
572 puts("T ");
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000573 NetSetTimeout(NFS_TIMEOUT, NfsTimeout);
574 NfsSend();
Hiroshi Itofe891ec2008-01-31 18:35:04 +0900575 }
wdenka5725fa2004-09-28 21:51:42 +0000576}
577
578static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000579NfsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000580{
581 int rlen;
582
Robin Getz0ebf04c2009-07-23 03:01:03 -0400583 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000584
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000585 if (dest != NfsOurPort)
586 return;
wdenkcbd8a352004-02-24 02:00:03 +0000587
588 switch (NfsState) {
589 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000590 rpc_lookup_reply(PROG_MOUNT, pkt, len);
wdenkcbd8a352004-02-24 02:00:03 +0000591 NfsState = STATE_PRCLOOKUP_PROG_NFS_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000592 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000593 break;
594
595 case STATE_PRCLOOKUP_PROG_NFS_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000596 rpc_lookup_reply(PROG_NFS, pkt, len);
wdenkcbd8a352004-02-24 02:00:03 +0000597 NfsState = STATE_MOUNT_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000598 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000599 break;
600
601 case STATE_MOUNT_REQ:
602 if (nfs_mount_reply(pkt, len)) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000603 puts("*** ERROR: Cannot mount\n");
wdenkcbd8a352004-02-24 02:00:03 +0000604 /* just to be sure... */
605 NfsState = STATE_UMOUNT_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000606 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000607 } else {
608 NfsState = STATE_LOOKUP_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000609 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000610 }
611 break;
612
613 case STATE_UMOUNT_REQ:
614 if (nfs_umountall_reply(pkt, len)) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000615 puts("*** ERROR: Cannot umount\n");
wdenkcbd8a352004-02-24 02:00:03 +0000616 NetState = NETLOOP_FAIL;
617 } else {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000618 puts("\ndone\n");
Wolfgang Denk23a7a322005-08-06 01:11:12 +0200619 NetState = NfsDownloadState;
wdenkcbd8a352004-02-24 02:00:03 +0000620 }
621 break;
622
623 case STATE_LOOKUP_REQ:
624 if (nfs_lookup_reply(pkt, len)) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000625 puts("*** ERROR: File lookup fail\n");
wdenkcbd8a352004-02-24 02:00:03 +0000626 NfsState = STATE_UMOUNT_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000627 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000628 } else {
629 NfsState = STATE_READ_REQ;
630 nfs_offset = 0;
631 nfs_len = NFS_READ_SIZE;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000632 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000633 }
634 break;
635
636 case STATE_READLINK_REQ:
637 if (nfs_readlink_reply(pkt, len)) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000638 puts("*** ERROR: Symlink fail\n");
wdenkcbd8a352004-02-24 02:00:03 +0000639 NfsState = STATE_UMOUNT_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000640 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000641 } else {
Robin Getz0ebf04c2009-07-23 03:01:03 -0400642 debug("Symlink --> %s\n", nfs_path);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000643 nfs_filename = basename(nfs_path);
644 nfs_path = dirname(nfs_path);
wdenkcbd8a352004-02-24 02:00:03 +0000645
646 NfsState = STATE_MOUNT_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000647 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000648 }
649 break;
650
651 case STATE_READ_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000652 rlen = nfs_read_reply(pkt, len);
653 NetSetTimeout(NFS_TIMEOUT, NfsTimeout);
wdenkcbd8a352004-02-24 02:00:03 +0000654 if (rlen > 0) {
655 nfs_offset += rlen;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000656 NfsSend();
657 } else if ((rlen == -NFSERR_ISDIR) || (rlen == -NFSERR_INVAL)) {
wdenkcbd8a352004-02-24 02:00:03 +0000658 /* symbolic link */
659 NfsState = STATE_READLINK_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000660 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000661 } else {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000662 if (!rlen)
663 NfsDownloadState = NETLOOP_SUCCESS;
wdenkcbd8a352004-02-24 02:00:03 +0000664 NfsState = STATE_UMOUNT_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000665 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000666 }
667 break;
668 }
669}
670
wdenkcbd8a352004-02-24 02:00:03 +0000671
672void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000673NfsStart(void)
wdenkcbd8a352004-02-24 02:00:03 +0000674{
Robin Getz0ebf04c2009-07-23 03:01:03 -0400675 debug("%s\n", __func__);
Wolfgang Denk23a7a322005-08-06 01:11:12 +0200676 NfsDownloadState = NETLOOP_FAIL;
wdenkcbd8a352004-02-24 02:00:03 +0000677
678 NfsServerIP = NetServerIP;
679 nfs_path = (char *)nfs_path_buff;
680
681 if (nfs_path == NULL) {
682 NetState = NETLOOP_FAIL;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000683 puts("*** ERROR: Fail allocate memory\n");
wdenkcbd8a352004-02-24 02:00:03 +0000684 return;
685 }
686
687 if (BootFile[0] == '\0') {
Matthias Weisserea45cb02011-12-03 03:29:44 +0000688 sprintf(default_filename, "/nfsroot/%02X%02X%02X%02X.img",
Wolfgang Denkc43352c2005-08-04 01:09:44 +0200689 NetOurIP & 0xFF,
690 (NetOurIP >> 8) & 0xFF,
691 (NetOurIP >> 16) & 0xFF,
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000692 (NetOurIP >> 24) & 0xFF);
693 strcpy(nfs_path, default_filename);
wdenkcbd8a352004-02-24 02:00:03 +0000694
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000695 printf("*** Warning: no boot file name; using '%s'\n",
wdenkcbd8a352004-02-24 02:00:03 +0000696 nfs_path);
697 } else {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000698 char *p = BootFile;
wdenkcbd8a352004-02-24 02:00:03 +0000699
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000700 p = strchr(p, ':');
wdenkcbd8a352004-02-24 02:00:03 +0000701
702 if (p != NULL) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000703 NfsServerIP = string_to_ip(BootFile);
wdenkcbd8a352004-02-24 02:00:03 +0000704 ++p;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000705 strcpy(nfs_path, p);
wdenkcbd8a352004-02-24 02:00:03 +0000706 } else {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000707 strcpy(nfs_path, BootFile);
wdenkcbd8a352004-02-24 02:00:03 +0000708 }
709 }
710
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000711 nfs_filename = basename(nfs_path);
712 nfs_path = dirname(nfs_path);
wdenkcbd8a352004-02-24 02:00:03 +0000713
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000714 printf("Using %s device\n", eth_get_name());
wdenkcbd8a352004-02-24 02:00:03 +0000715
Mike Frysingerb6446b62009-02-17 00:00:53 -0500716 printf("File transfer via NFS from server %pI4"
717 "; our IP address is %pI4", &NfsServerIP, &NetOurIP);
wdenkcbd8a352004-02-24 02:00:03 +0000718
719 /* Check if we need to send across this subnet */
720 if (NetOurGatewayIP && NetOurSubnetMask) {
721 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
722 IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask;
723
Mike Frysingerb6446b62009-02-17 00:00:53 -0500724 if (OurNet != ServerNet)
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000725 printf("; sending through gateway %pI4",
726 &NetOurGatewayIP);
wdenkcbd8a352004-02-24 02:00:03 +0000727 }
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000728 printf("\nFilename '%s/%s'.", nfs_path, nfs_filename);
wdenkcbd8a352004-02-24 02:00:03 +0000729
730 if (NetBootFileSize) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000731 printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
732 print_size(NetBootFileSize<<9, "");
wdenkcbd8a352004-02-24 02:00:03 +0000733 }
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000734 printf("\nLoad address: 0x%lx\n"
wdenk4b9206e2004-03-23 22:14:11 +0000735 "Loading: *\b", load_addr);
wdenkcbd8a352004-02-24 02:00:03 +0000736
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000737 NetSetTimeout(NFS_TIMEOUT, NfsTimeout);
738 NetSetHandler(NfsHandler);
wdenkcbd8a352004-02-24 02:00:03 +0000739
wdenkcbd8a352004-02-24 02:00:03 +0000740 NfsTimeoutCount = 0;
741 NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
742
743 /*NfsOurPort = 4096 + (get_ticks() % 3072);*/
744 /*FIX ME !!!*/
745 NfsOurPort = 1000;
746
747 /* zero out server ether in case the server ip has changed */
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000748 memset(NetServerEther, 0, 6);
wdenkcbd8a352004-02-24 02:00:03 +0000749
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000750 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000751}