blob: 7f2393f6064c62976ea3bf0c5f10a2ac22e7d443 [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
Tetsuyuki Kobayashi48a3e992012-07-03 22:25:21 +000034#ifndef CONFIG_NFS_TIMEOUT
35# define NFS_TIMEOUT 2000UL
36#else
37# define NFS_TIMEOUT CONFIG_NFS_TIMEOUT
38#endif
wdenkcbd8a352004-02-24 02:00:03 +000039
Joe Hershbergerc9f6c912012-05-15 08:59:09 +000040static int fs_mounted;
41static unsigned long rpc_id;
wdenkcbd8a352004-02-24 02:00:03 +000042static int nfs_offset = -1;
43static int nfs_len;
44
45static char dirfh[NFS_FHSIZE]; /* file handle of directory */
46static char filefh[NFS_FHSIZE]; /* file handle of kernel image */
47
Joe Hershberger22f6e992012-05-23 07:59:14 +000048static enum net_loop_state nfs_download_state;
wdenkcbd8a352004-02-24 02:00:03 +000049static IPaddr_t NfsServerIP;
50static int NfsSrvMountPort;
51static int NfsSrvNfsPort;
52static int NfsOurPort;
53static int NfsTimeoutCount;
54static int NfsState;
55#define STATE_PRCLOOKUP_PROG_MOUNT_REQ 1
56#define STATE_PRCLOOKUP_PROG_NFS_REQ 2
57#define STATE_MOUNT_REQ 3
58#define STATE_UMOUNT_REQ 4
59#define STATE_LOOKUP_REQ 5
60#define STATE_READ_REQ 6
61#define STATE_READLINK_REQ 7
62
63static char default_filename[64];
64static char *nfs_filename;
65static char *nfs_path;
66static char nfs_path_buff[2048];
67
Joe Hershbergerc9f6c912012-05-15 08:59:09 +000068static inline int
69store_block(uchar *src, unsigned offset, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +000070{
wdenka084f7d2004-02-24 22:33:21 +000071 ulong newsize = offset + len;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020072#ifdef CONFIG_SYS_DIRECT_FLASH_NFS
wdenkcbd8a352004-02-24 02:00:03 +000073 int i, rc = 0;
74
Joe Hershbergerc9f6c912012-05-15 08:59:09 +000075 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenkcbd8a352004-02-24 02:00:03 +000076 /* start address in flash? */
77 if (load_addr + offset >= flash_info[i].start[0]) {
78 rc = 1;
79 break;
80 }
81 }
82
83 if (rc) { /* Flash is destination for this packet */
Joe Hershbergerc9f6c912012-05-15 08:59:09 +000084 rc = flash_write((uchar *)src, (ulong)(load_addr+offset), len);
wdenkcbd8a352004-02-24 02:00:03 +000085 if (rc) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +000086 flash_perror(rc);
Wolfgang Denk23a7a322005-08-06 01:11:12 +020087 return -1;
wdenkcbd8a352004-02-24 02:00:03 +000088 }
89 } else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020090#endif /* CONFIG_SYS_DIRECT_FLASH_NFS */
wdenkcbd8a352004-02-24 02:00:03 +000091 {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +000092 (void)memcpy((void *)(load_addr + offset), src, len);
wdenkcbd8a352004-02-24 02:00:03 +000093 }
wdenka084f7d2004-02-24 22:33:21 +000094
95 if (NetBootFileXferSize < (offset+len))
96 NetBootFileXferSize = newsize;
Wolfgang Denk23a7a322005-08-06 01:11:12 +020097 return 0;
wdenkcbd8a352004-02-24 02:00:03 +000098}
99
100static char*
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000101basename(char *path)
wdenkcbd8a352004-02-24 02:00:03 +0000102{
103 char *fname;
104
105 fname = path + strlen(path) - 1;
106 while (fname >= path) {
107 if (*fname == '/') {
108 fname++;
109 break;
110 }
111 fname--;
112 }
113 return fname;
114}
115
116static char*
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000117dirname(char *path)
wdenkcbd8a352004-02-24 02:00:03 +0000118{
119 char *fname;
120
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000121 fname = basename(path);
wdenkcbd8a352004-02-24 02:00:03 +0000122 --fname;
123 *fname = '\0';
124 return path;
125}
126
127/**************************************************************************
wdenkcbd8a352004-02-24 02:00:03 +0000128RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
129**************************************************************************/
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000130static long *rpc_add_credentials(long *p)
wdenkcbd8a352004-02-24 02:00:03 +0000131{
132 int hl;
133 int hostnamelen;
134 char hostname[256];
135
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000136 strcpy(hostname, "");
137 hostnamelen = strlen(hostname);
wdenkcbd8a352004-02-24 02:00:03 +0000138
139 /* Here's the executive summary on authentication requirements of the
140 * various NFS server implementations: Linux accepts both AUTH_NONE
141 * and AUTH_UNIX authentication (also accepts an empty hostname field
142 * in the AUTH_UNIX scheme). *BSD refuses AUTH_NONE, but accepts
143 * AUTH_UNIX (also accepts an empty hostname field in the AUTH_UNIX
144 * scheme). To be safe, use AUTH_UNIX and pass the hostname if we have
145 * it (if the BOOTP/DHCP reply didn't give one, just use an empty
146 * hostname). */
147
148 hl = (hostnamelen + 3) & ~3;
149
150 /* Provide an AUTH_UNIX credential. */
151 *p++ = htonl(1); /* AUTH_UNIX */
152 *p++ = htonl(hl+20); /* auth length */
153 *p++ = htonl(0); /* stamp */
154 *p++ = htonl(hostnamelen); /* hostname string */
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000155 if (hostnamelen & 3)
wdenkcbd8a352004-02-24 02:00:03 +0000156 *(p + hostnamelen / 4) = 0; /* add zero padding */
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000157 memcpy(p, hostname, hostnamelen);
wdenkcbd8a352004-02-24 02:00:03 +0000158 p += hl / 4;
159 *p++ = 0; /* uid */
160 *p++ = 0; /* gid */
161 *p++ = 0; /* auxiliary gid list */
162
163 /* Provide an AUTH_NONE verifier. */
164 *p++ = 0; /* AUTH_NONE */
165 *p++ = 0; /* auth length */
166
167 return p;
168}
169
170/**************************************************************************
171RPC_LOOKUP - Lookup RPC Port numbers
172**************************************************************************/
173static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000174rpc_req(int rpc_prog, int rpc_proc, uint32_t *data, int datalen)
wdenkcbd8a352004-02-24 02:00:03 +0000175{
176 struct rpc_t pkt;
177 unsigned long id;
178 uint32_t *p;
179 int pktlen;
180 int sport;
181
wdenkc3f9d492004-03-14 00:59:59 +0000182 id = ++rpc_id;
wdenkcbd8a352004-02-24 02:00:03 +0000183 pkt.u.call.id = htonl(id);
184 pkt.u.call.type = htonl(MSG_CALL);
185 pkt.u.call.rpcvers = htonl(2); /* use RPC version 2 */
186 pkt.u.call.prog = htonl(rpc_prog);
187 pkt.u.call.vers = htonl(2); /* portmapper is version 2 */
188 pkt.u.call.proc = htonl(rpc_proc);
189 p = (uint32_t *)&(pkt.u.call.data);
190
191 if (datalen)
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000192 memcpy((char *)p, (char *)data, datalen*sizeof(uint32_t));
wdenkcbd8a352004-02-24 02:00:03 +0000193
194 pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt;
195
Joe Hershberger594c26f2012-05-23 07:58:04 +0000196 memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE,
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000197 (char *)&pkt, pktlen);
wdenkcbd8a352004-02-24 02:00:03 +0000198
199 if (rpc_prog == PROG_PORTMAP)
200 sport = SUNRPC_PORT;
201 else if (rpc_prog == PROG_MOUNT)
202 sport = NfsSrvMountPort;
203 else
204 sport = NfsSrvNfsPort;
205
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000206 NetSendUDPPacket(NetServerEther, NfsServerIP, sport, NfsOurPort,
207 pktlen);
wdenkcbd8a352004-02-24 02:00:03 +0000208}
209
210/**************************************************************************
211RPC_LOOKUP - Lookup RPC Port numbers
212**************************************************************************/
213static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000214rpc_lookup_req(int prog, int ver)
wdenkcbd8a352004-02-24 02:00:03 +0000215{
216 uint32_t data[16];
217
218 data[0] = 0; data[1] = 0; /* auth credential */
219 data[2] = 0; data[3] = 0; /* auth verifier */
220 data[4] = htonl(prog);
221 data[5] = htonl(ver);
222 data[6] = htonl(17); /* IP_UDP */
223 data[7] = 0;
224
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000225 rpc_req(PROG_PORTMAP, PORTMAP_GETPORT, data, 8);
wdenkcbd8a352004-02-24 02:00:03 +0000226}
227
228/**************************************************************************
229NFS_MOUNT - Mount an NFS Filesystem
230**************************************************************************/
231static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000232nfs_mount_req(char *path)
wdenkcbd8a352004-02-24 02:00:03 +0000233{
234 uint32_t data[1024];
235 uint32_t *p;
236 int len;
237 int pathlen;
238
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000239 pathlen = strlen(path);
wdenkcbd8a352004-02-24 02:00:03 +0000240
241 p = &(data[0]);
242 p = (uint32_t *)rpc_add_credentials((long *)p);
243
244 *p++ = htonl(pathlen);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000245 if (pathlen & 3)
246 *(p + pathlen / 4) = 0;
247 memcpy(p, path, pathlen);
wdenkcbd8a352004-02-24 02:00:03 +0000248 p += (pathlen + 3) / 4;
249
250 len = (uint32_t *)p - (uint32_t *)&(data[0]);
251
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000252 rpc_req(PROG_MOUNT, MOUNT_ADDENTRY, data, len);
wdenkcbd8a352004-02-24 02:00:03 +0000253}
254
255/**************************************************************************
256NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
257**************************************************************************/
258static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000259nfs_umountall_req(void)
wdenkcbd8a352004-02-24 02:00:03 +0000260{
261 uint32_t data[1024];
262 uint32_t *p;
263 int len;
264
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000265 if ((NfsSrvMountPort == -1) || (!fs_mounted))
wdenkcbd8a352004-02-24 02:00:03 +0000266 /* Nothing mounted, nothing to umount */
267 return;
wdenkcbd8a352004-02-24 02:00:03 +0000268
269 p = &(data[0]);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000270 p = (uint32_t *)rpc_add_credentials((long *)p);
wdenkcbd8a352004-02-24 02:00:03 +0000271
272 len = (uint32_t *)p - (uint32_t *)&(data[0]);
273
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000274 rpc_req(PROG_MOUNT, MOUNT_UMOUNTALL, data, len);
wdenkcbd8a352004-02-24 02:00:03 +0000275}
276
277/***************************************************************************
278 * NFS_READLINK (AH 2003-07-14)
279 * This procedure is called when read of the first block fails -
280 * this probably happens when it's a directory or a symlink
281 * In case of successful readlink(), the dirname is manipulated,
282 * so that inside the nfs() function a recursion can be done.
283 **************************************************************************/
284static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000285nfs_readlink_req(void)
wdenkcbd8a352004-02-24 02:00:03 +0000286{
287 uint32_t data[1024];
288 uint32_t *p;
289 int len;
290
291 p = &(data[0]);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000292 p = (uint32_t *)rpc_add_credentials((long *)p);
wdenkcbd8a352004-02-24 02:00:03 +0000293
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000294 memcpy(p, filefh, NFS_FHSIZE);
wdenkcbd8a352004-02-24 02:00:03 +0000295 p += (NFS_FHSIZE / 4);
296
297 len = (uint32_t *)p - (uint32_t *)&(data[0]);
298
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000299 rpc_req(PROG_NFS, NFS_READLINK, data, len);
wdenkcbd8a352004-02-24 02:00:03 +0000300}
301
302/**************************************************************************
303NFS_LOOKUP - Lookup Pathname
304**************************************************************************/
305static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000306nfs_lookup_req(char *fname)
wdenkcbd8a352004-02-24 02:00:03 +0000307{
308 uint32_t data[1024];
309 uint32_t *p;
310 int len;
311 int fnamelen;
312
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000313 fnamelen = strlen(fname);
wdenkcbd8a352004-02-24 02:00:03 +0000314
315 p = &(data[0]);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000316 p = (uint32_t *)rpc_add_credentials((long *)p);
wdenkcbd8a352004-02-24 02:00:03 +0000317
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000318 memcpy(p, dirfh, NFS_FHSIZE);
wdenkcbd8a352004-02-24 02:00:03 +0000319 p += (NFS_FHSIZE / 4);
320 *p++ = htonl(fnamelen);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000321 if (fnamelen & 3)
322 *(p + fnamelen / 4) = 0;
323 memcpy(p, fname, fnamelen);
wdenkcbd8a352004-02-24 02:00:03 +0000324 p += (fnamelen + 3) / 4;
325
326 len = (uint32_t *)p - (uint32_t *)&(data[0]);
327
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000328 rpc_req(PROG_NFS, NFS_LOOKUP, data, len);
wdenkcbd8a352004-02-24 02:00:03 +0000329}
330
331/**************************************************************************
332NFS_READ - Read File on NFS Server
333**************************************************************************/
334static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000335nfs_read_req(int offset, int readlen)
wdenkcbd8a352004-02-24 02:00:03 +0000336{
337 uint32_t data[1024];
338 uint32_t *p;
339 int len;
340
341 p = &(data[0]);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000342 p = (uint32_t *)rpc_add_credentials((long *)p);
wdenkcbd8a352004-02-24 02:00:03 +0000343
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000344 memcpy(p, filefh, NFS_FHSIZE);
wdenkcbd8a352004-02-24 02:00:03 +0000345 p += (NFS_FHSIZE / 4);
346 *p++ = htonl(offset);
347 *p++ = htonl(readlen);
348 *p++ = 0;
349
350 len = (uint32_t *)p - (uint32_t *)&(data[0]);
351
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000352 rpc_req(PROG_NFS, NFS_READ, data, len);
wdenkcbd8a352004-02-24 02:00:03 +0000353}
354
355/**************************************************************************
356RPC request dispatcher
357**************************************************************************/
358
359static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000360NfsSend(void)
wdenkcbd8a352004-02-24 02:00:03 +0000361{
Robin Getz0ebf04c2009-07-23 03:01:03 -0400362 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000363
364 switch (NfsState) {
365 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000366 rpc_lookup_req(PROG_MOUNT, 1);
wdenkcbd8a352004-02-24 02:00:03 +0000367 break;
368 case STATE_PRCLOOKUP_PROG_NFS_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000369 rpc_lookup_req(PROG_NFS, 2);
wdenkcbd8a352004-02-24 02:00:03 +0000370 break;
371 case STATE_MOUNT_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000372 nfs_mount_req(nfs_path);
wdenkcbd8a352004-02-24 02:00:03 +0000373 break;
374 case STATE_UMOUNT_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000375 nfs_umountall_req();
wdenkcbd8a352004-02-24 02:00:03 +0000376 break;
377 case STATE_LOOKUP_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000378 nfs_lookup_req(nfs_filename);
wdenkcbd8a352004-02-24 02:00:03 +0000379 break;
380 case STATE_READ_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000381 nfs_read_req(nfs_offset, nfs_len);
wdenkcbd8a352004-02-24 02:00:03 +0000382 break;
383 case STATE_READLINK_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000384 nfs_readlink_req();
wdenkcbd8a352004-02-24 02:00:03 +0000385 break;
386 }
387}
388
389/**************************************************************************
390Handlers for the reply from server
391**************************************************************************/
392
393static int
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000394rpc_lookup_reply(int prog, uchar *pkt, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000395{
396 struct rpc_t rpc_pkt;
397
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000398 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkcbd8a352004-02-24 02:00:03 +0000399
Robin Getz0ebf04c2009-07-23 03:01:03 -0400400 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000401
wdenkc3f9d492004-03-14 00:59:59 +0000402 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
403 return -1;
404
wdenkcbd8a352004-02-24 02:00:03 +0000405 if (rpc_pkt.u.reply.rstatus ||
406 rpc_pkt.u.reply.verifier ||
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000407 rpc_pkt.u.reply.astatus)
wdenkc3f9d492004-03-14 00:59:59 +0000408 return -1;
wdenkcbd8a352004-02-24 02:00:03 +0000409
410 switch (prog) {
411 case PROG_MOUNT:
412 NfsSrvMountPort = ntohl(rpc_pkt.u.reply.data[0]);
413 break;
414 case PROG_NFS:
415 NfsSrvNfsPort = ntohl(rpc_pkt.u.reply.data[0]);
416 break;
417 }
418
419 return 0;
420}
421
422static int
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000423nfs_mount_reply(uchar *pkt, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000424{
425 struct rpc_t rpc_pkt;
426
Robin Getz0ebf04c2009-07-23 03:01:03 -0400427 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000428
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000429 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkcbd8a352004-02-24 02:00:03 +0000430
wdenkc3f9d492004-03-14 00:59:59 +0000431 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
432 return -1;
433
wdenkcbd8a352004-02-24 02:00:03 +0000434 if (rpc_pkt.u.reply.rstatus ||
435 rpc_pkt.u.reply.verifier ||
436 rpc_pkt.u.reply.astatus ||
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000437 rpc_pkt.u.reply.data[0])
wdenkcbd8a352004-02-24 02:00:03 +0000438 return -1;
wdenkcbd8a352004-02-24 02:00:03 +0000439
440 fs_mounted = 1;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000441 memcpy(dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
wdenkcbd8a352004-02-24 02:00:03 +0000442
443 return 0;
444}
445
446static int
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000447nfs_umountall_reply(uchar *pkt, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000448{
449 struct rpc_t rpc_pkt;
450
Robin Getz0ebf04c2009-07-23 03:01:03 -0400451 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000452
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000453 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkcbd8a352004-02-24 02:00:03 +0000454
wdenkc3f9d492004-03-14 00:59:59 +0000455 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
456 return -1;
457
wdenkcbd8a352004-02-24 02:00:03 +0000458 if (rpc_pkt.u.reply.rstatus ||
459 rpc_pkt.u.reply.verifier ||
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000460 rpc_pkt.u.reply.astatus)
wdenkcbd8a352004-02-24 02:00:03 +0000461 return -1;
wdenkcbd8a352004-02-24 02:00:03 +0000462
463 fs_mounted = 0;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000464 memset(dirfh, 0, sizeof(dirfh));
wdenkcbd8a352004-02-24 02:00:03 +0000465
466 return 0;
467}
468
469static int
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000470nfs_lookup_reply(uchar *pkt, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000471{
472 struct rpc_t rpc_pkt;
473
Robin Getz0ebf04c2009-07-23 03:01:03 -0400474 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000475
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000476 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkcbd8a352004-02-24 02:00:03 +0000477
wdenkc3f9d492004-03-14 00:59:59 +0000478 if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
479 return -1;
480
wdenkcbd8a352004-02-24 02:00:03 +0000481 if (rpc_pkt.u.reply.rstatus ||
482 rpc_pkt.u.reply.verifier ||
483 rpc_pkt.u.reply.astatus ||
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000484 rpc_pkt.u.reply.data[0])
wdenkcbd8a352004-02-24 02:00:03 +0000485 return -1;
wdenkcbd8a352004-02-24 02:00:03 +0000486
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000487 memcpy(filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
wdenkcbd8a352004-02-24 02:00:03 +0000488
489 return 0;
490}
491
492static int
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000493nfs_readlink_reply(uchar *pkt, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000494{
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
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000500 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkcbd8a352004-02-24 02:00:03 +0000501
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 ||
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000508 rpc_pkt.u.reply.data[0])
wdenkcbd8a352004-02-24 02:00:03 +0000509 return -1;
wdenkcbd8a352004-02-24 02:00:03 +0000510
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000511 rlen = ntohl(rpc_pkt.u.reply.data[1]); /* new path length */
wdenkcbd8a352004-02-24 02:00:03 +0000512
513 if (*((char *)&(rpc_pkt.u.reply.data[2])) != '/') {
514 int pathlen;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000515 strcat(nfs_path, "/");
wdenkcbd8a352004-02-24 02:00:03 +0000516 pathlen = strlen(nfs_path);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000517 memcpy(nfs_path + pathlen, (uchar *)&(rpc_pkt.u.reply.data[2]),
518 rlen);
Ed Swarthoutf64ef9b2009-11-19 02:47:28 -0600519 nfs_path[pathlen + rlen] = 0;
wdenkcbd8a352004-02-24 02:00:03 +0000520 } else {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000521 memcpy(nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen);
wdenkcbd8a352004-02-24 02:00:03 +0000522 nfs_path[rlen] = 0;
523 }
524 return 0;
525}
526
527static int
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000528nfs_read_reply(uchar *pkt, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000529{
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
Joe Hershbergerc9f6c912012-05-15 08:59:09 +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]) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000544 if (rpc_pkt.u.reply.rstatus)
wdenkcbd8a352004-02-24 02:00:03 +0000545 return -9999;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000546 if (rpc_pkt.u.reply.astatus)
wdenkcbd8a352004-02-24 02:00:03 +0000547 return -9999;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000548 return -ntohl(rpc_pkt.u.reply.data[0]);
wdenkcbd8a352004-02-24 02:00:03 +0000549 }
550
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000551 if ((nfs_offset != 0) && !((nfs_offset) %
552 (NFS_READ_SIZE / 2 * 10 * HASHES_PER_LINE)))
553 puts("\n\t ");
554 if (!(nfs_offset % ((NFS_READ_SIZE / 2) * 10)))
555 putc('#');
wdenkcbd8a352004-02-24 02:00:03 +0000556
557 rlen = ntohl(rpc_pkt.u.reply.data[18]);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000558 if (store_block((uchar *)pkt + sizeof(rpc_pkt.u.reply),
559 nfs_offset, rlen))
Wolfgang Denk23a7a322005-08-06 01:11:12 +0200560 return -9999;
wdenkcbd8a352004-02-24 02:00:03 +0000561
562 return rlen;
563}
564
565/**************************************************************************
566Interfaces of U-BOOT
567**************************************************************************/
568
569static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000570NfsTimeout(void)
wdenka5725fa2004-09-28 21:51:42 +0000571{
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000572 if (++NfsTimeoutCount > NFS_RETRY_COUNT) {
573 puts("\nRetry count exceeded; starting again\n");
574 NetStartAgain();
Evan Samanasaabb8cb2009-11-09 20:08:36 -0600575 } else {
576 puts("T ");
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000577 NetSetTimeout(NFS_TIMEOUT, NfsTimeout);
578 NfsSend();
Hiroshi Itofe891ec2008-01-31 18:35:04 +0900579 }
wdenka5725fa2004-09-28 21:51:42 +0000580}
581
582static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000583NfsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000584{
585 int rlen;
586
Robin Getz0ebf04c2009-07-23 03:01:03 -0400587 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000588
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000589 if (dest != NfsOurPort)
590 return;
wdenkcbd8a352004-02-24 02:00:03 +0000591
592 switch (NfsState) {
593 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000594 rpc_lookup_reply(PROG_MOUNT, pkt, len);
wdenkcbd8a352004-02-24 02:00:03 +0000595 NfsState = STATE_PRCLOOKUP_PROG_NFS_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000596 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000597 break;
598
599 case STATE_PRCLOOKUP_PROG_NFS_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000600 rpc_lookup_reply(PROG_NFS, pkt, len);
wdenkcbd8a352004-02-24 02:00:03 +0000601 NfsState = STATE_MOUNT_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000602 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000603 break;
604
605 case STATE_MOUNT_REQ:
606 if (nfs_mount_reply(pkt, len)) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000607 puts("*** ERROR: Cannot mount\n");
wdenkcbd8a352004-02-24 02:00:03 +0000608 /* just to be sure... */
609 NfsState = STATE_UMOUNT_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000610 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000611 } else {
612 NfsState = STATE_LOOKUP_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000613 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000614 }
615 break;
616
617 case STATE_UMOUNT_REQ:
618 if (nfs_umountall_reply(pkt, len)) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000619 puts("*** ERROR: Cannot umount\n");
Joe Hershberger22f6e992012-05-23 07:59:14 +0000620 net_set_state(NETLOOP_FAIL);
wdenkcbd8a352004-02-24 02:00:03 +0000621 } else {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000622 puts("\ndone\n");
Joe Hershberger22f6e992012-05-23 07:59:14 +0000623 net_set_state(nfs_download_state);
wdenkcbd8a352004-02-24 02:00:03 +0000624 }
625 break;
626
627 case STATE_LOOKUP_REQ:
628 if (nfs_lookup_reply(pkt, len)) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000629 puts("*** ERROR: File lookup fail\n");
wdenkcbd8a352004-02-24 02:00:03 +0000630 NfsState = STATE_UMOUNT_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000631 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000632 } else {
633 NfsState = STATE_READ_REQ;
634 nfs_offset = 0;
635 nfs_len = NFS_READ_SIZE;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000636 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000637 }
638 break;
639
640 case STATE_READLINK_REQ:
641 if (nfs_readlink_reply(pkt, len)) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000642 puts("*** ERROR: Symlink fail\n");
wdenkcbd8a352004-02-24 02:00:03 +0000643 NfsState = STATE_UMOUNT_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000644 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000645 } else {
Robin Getz0ebf04c2009-07-23 03:01:03 -0400646 debug("Symlink --> %s\n", nfs_path);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000647 nfs_filename = basename(nfs_path);
648 nfs_path = dirname(nfs_path);
wdenkcbd8a352004-02-24 02:00:03 +0000649
650 NfsState = STATE_MOUNT_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000651 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000652 }
653 break;
654
655 case STATE_READ_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000656 rlen = nfs_read_reply(pkt, len);
657 NetSetTimeout(NFS_TIMEOUT, NfsTimeout);
wdenkcbd8a352004-02-24 02:00:03 +0000658 if (rlen > 0) {
659 nfs_offset += rlen;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000660 NfsSend();
661 } else if ((rlen == -NFSERR_ISDIR) || (rlen == -NFSERR_INVAL)) {
wdenkcbd8a352004-02-24 02:00:03 +0000662 /* symbolic link */
663 NfsState = STATE_READLINK_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000664 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000665 } else {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000666 if (!rlen)
Joe Hershberger22f6e992012-05-23 07:59:14 +0000667 nfs_download_state = NETLOOP_SUCCESS;
wdenkcbd8a352004-02-24 02:00:03 +0000668 NfsState = STATE_UMOUNT_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000669 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000670 }
671 break;
672 }
673}
674
wdenkcbd8a352004-02-24 02:00:03 +0000675
676void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000677NfsStart(void)
wdenkcbd8a352004-02-24 02:00:03 +0000678{
Robin Getz0ebf04c2009-07-23 03:01:03 -0400679 debug("%s\n", __func__);
Joe Hershberger22f6e992012-05-23 07:59:14 +0000680 nfs_download_state = 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) {
Joe Hershberger22f6e992012-05-23 07:59:14 +0000686 net_set_state(NETLOOP_FAIL);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000687 puts("*** ERROR: Fail allocate memory\n");
wdenkcbd8a352004-02-24 02:00:03 +0000688 return;
689 }
690
691 if (BootFile[0] == '\0') {
Matthias Weisserea45cb02011-12-03 03:29:44 +0000692 sprintf(default_filename, "/nfsroot/%02X%02X%02X%02X.img",
Wolfgang Denkc43352c2005-08-04 01:09:44 +0200693 NetOurIP & 0xFF,
694 (NetOurIP >> 8) & 0xFF,
695 (NetOurIP >> 16) & 0xFF,
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000696 (NetOurIP >> 24) & 0xFF);
697 strcpy(nfs_path, default_filename);
wdenkcbd8a352004-02-24 02:00:03 +0000698
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000699 printf("*** Warning: no boot file name; using '%s'\n",
wdenkcbd8a352004-02-24 02:00:03 +0000700 nfs_path);
701 } else {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000702 char *p = BootFile;
wdenkcbd8a352004-02-24 02:00:03 +0000703
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000704 p = strchr(p, ':');
wdenkcbd8a352004-02-24 02:00:03 +0000705
706 if (p != NULL) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000707 NfsServerIP = string_to_ip(BootFile);
wdenkcbd8a352004-02-24 02:00:03 +0000708 ++p;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000709 strcpy(nfs_path, p);
wdenkcbd8a352004-02-24 02:00:03 +0000710 } else {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000711 strcpy(nfs_path, BootFile);
wdenkcbd8a352004-02-24 02:00:03 +0000712 }
713 }
714
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000715 nfs_filename = basename(nfs_path);
716 nfs_path = dirname(nfs_path);
wdenkcbd8a352004-02-24 02:00:03 +0000717
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000718 printf("Using %s device\n", eth_get_name());
wdenkcbd8a352004-02-24 02:00:03 +0000719
Mike Frysingerb6446b62009-02-17 00:00:53 -0500720 printf("File transfer via NFS from server %pI4"
721 "; our IP address is %pI4", &NfsServerIP, &NetOurIP);
wdenkcbd8a352004-02-24 02:00:03 +0000722
723 /* Check if we need to send across this subnet */
724 if (NetOurGatewayIP && NetOurSubnetMask) {
725 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
726 IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask;
727
Mike Frysingerb6446b62009-02-17 00:00:53 -0500728 if (OurNet != ServerNet)
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000729 printf("; sending through gateway %pI4",
730 &NetOurGatewayIP);
wdenkcbd8a352004-02-24 02:00:03 +0000731 }
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000732 printf("\nFilename '%s/%s'.", nfs_path, nfs_filename);
wdenkcbd8a352004-02-24 02:00:03 +0000733
734 if (NetBootFileSize) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000735 printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
736 print_size(NetBootFileSize<<9, "");
wdenkcbd8a352004-02-24 02:00:03 +0000737 }
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000738 printf("\nLoad address: 0x%lx\n"
wdenk4b9206e2004-03-23 22:14:11 +0000739 "Loading: *\b", load_addr);
wdenkcbd8a352004-02-24 02:00:03 +0000740
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000741 NetSetTimeout(NFS_TIMEOUT, NfsTimeout);
Joe Hershbergerece223b2012-05-23 07:59:15 +0000742 net_set_udp_handler(NfsHandler);
wdenkcbd8a352004-02-24 02:00:03 +0000743
wdenkcbd8a352004-02-24 02:00:03 +0000744 NfsTimeoutCount = 0;
745 NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
746
747 /*NfsOurPort = 4096 + (get_ticks() % 3072);*/
748 /*FIX ME !!!*/
749 NfsOurPort = 1000;
750
751 /* zero out server ether in case the server ip has changed */
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000752 memset(NetServerEther, 0, 6);
wdenkcbd8a352004-02-24 02:00:03 +0000753
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000754 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000755}