blob: 381b75f1c5b091799876aa77ec8997c97ea14fa9 [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
Matthias Bruggerfa84fa72012-12-11 19:14:16 +010040#define NFS_RPC_ERR 1
41#define NFS_RPC_DROP 124
42
Joe Hershbergerc9f6c912012-05-15 08:59:09 +000043static int fs_mounted;
44static unsigned long rpc_id;
wdenkcbd8a352004-02-24 02:00:03 +000045static int nfs_offset = -1;
46static int nfs_len;
Matthias Bruggerfa84fa72012-12-11 19:14:16 +010047static ulong nfs_timeout = NFS_TIMEOUT;
wdenkcbd8a352004-02-24 02:00:03 +000048
49static char dirfh[NFS_FHSIZE]; /* file handle of directory */
50static char filefh[NFS_FHSIZE]; /* file handle of kernel image */
51
Joe Hershberger22f6e992012-05-23 07:59:14 +000052static enum net_loop_state nfs_download_state;
wdenkcbd8a352004-02-24 02:00:03 +000053static IPaddr_t NfsServerIP;
54static int NfsSrvMountPort;
55static int NfsSrvNfsPort;
56static int NfsOurPort;
57static int NfsTimeoutCount;
58static int NfsState;
59#define STATE_PRCLOOKUP_PROG_MOUNT_REQ 1
60#define STATE_PRCLOOKUP_PROG_NFS_REQ 2
61#define STATE_MOUNT_REQ 3
62#define STATE_UMOUNT_REQ 4
63#define STATE_LOOKUP_REQ 5
64#define STATE_READ_REQ 6
65#define STATE_READLINK_REQ 7
66
67static char default_filename[64];
68static char *nfs_filename;
69static char *nfs_path;
70static char nfs_path_buff[2048];
71
Joe Hershbergerc9f6c912012-05-15 08:59:09 +000072static inline int
73store_block(uchar *src, unsigned offset, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +000074{
wdenka084f7d2004-02-24 22:33:21 +000075 ulong newsize = offset + len;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020076#ifdef CONFIG_SYS_DIRECT_FLASH_NFS
wdenkcbd8a352004-02-24 02:00:03 +000077 int i, rc = 0;
78
Joe Hershbergerc9f6c912012-05-15 08:59:09 +000079 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenkcbd8a352004-02-24 02:00:03 +000080 /* start address in flash? */
81 if (load_addr + offset >= flash_info[i].start[0]) {
82 rc = 1;
83 break;
84 }
85 }
86
87 if (rc) { /* Flash is destination for this packet */
Joe Hershbergerc9f6c912012-05-15 08:59:09 +000088 rc = flash_write((uchar *)src, (ulong)(load_addr+offset), len);
wdenkcbd8a352004-02-24 02:00:03 +000089 if (rc) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +000090 flash_perror(rc);
Wolfgang Denk23a7a322005-08-06 01:11:12 +020091 return -1;
wdenkcbd8a352004-02-24 02:00:03 +000092 }
93 } else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020094#endif /* CONFIG_SYS_DIRECT_FLASH_NFS */
wdenkcbd8a352004-02-24 02:00:03 +000095 {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +000096 (void)memcpy((void *)(load_addr + offset), src, len);
wdenkcbd8a352004-02-24 02:00:03 +000097 }
wdenka084f7d2004-02-24 22:33:21 +000098
99 if (NetBootFileXferSize < (offset+len))
100 NetBootFileXferSize = newsize;
Wolfgang Denk23a7a322005-08-06 01:11:12 +0200101 return 0;
wdenkcbd8a352004-02-24 02:00:03 +0000102}
103
104static char*
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000105basename(char *path)
wdenkcbd8a352004-02-24 02:00:03 +0000106{
107 char *fname;
108
109 fname = path + strlen(path) - 1;
110 while (fname >= path) {
111 if (*fname == '/') {
112 fname++;
113 break;
114 }
115 fname--;
116 }
117 return fname;
118}
119
120static char*
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000121dirname(char *path)
wdenkcbd8a352004-02-24 02:00:03 +0000122{
123 char *fname;
124
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000125 fname = basename(path);
wdenkcbd8a352004-02-24 02:00:03 +0000126 --fname;
127 *fname = '\0';
128 return path;
129}
130
131/**************************************************************************
wdenkcbd8a352004-02-24 02:00:03 +0000132RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
133**************************************************************************/
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000134static long *rpc_add_credentials(long *p)
wdenkcbd8a352004-02-24 02:00:03 +0000135{
136 int hl;
137 int hostnamelen;
138 char hostname[256];
139
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000140 strcpy(hostname, "");
141 hostnamelen = strlen(hostname);
wdenkcbd8a352004-02-24 02:00:03 +0000142
143 /* Here's the executive summary on authentication requirements of the
144 * various NFS server implementations: Linux accepts both AUTH_NONE
145 * and AUTH_UNIX authentication (also accepts an empty hostname field
146 * in the AUTH_UNIX scheme). *BSD refuses AUTH_NONE, but accepts
147 * AUTH_UNIX (also accepts an empty hostname field in the AUTH_UNIX
148 * scheme). To be safe, use AUTH_UNIX and pass the hostname if we have
149 * it (if the BOOTP/DHCP reply didn't give one, just use an empty
150 * hostname). */
151
152 hl = (hostnamelen + 3) & ~3;
153
154 /* Provide an AUTH_UNIX credential. */
155 *p++ = htonl(1); /* AUTH_UNIX */
156 *p++ = htonl(hl+20); /* auth length */
157 *p++ = htonl(0); /* stamp */
158 *p++ = htonl(hostnamelen); /* hostname string */
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000159 if (hostnamelen & 3)
wdenkcbd8a352004-02-24 02:00:03 +0000160 *(p + hostnamelen / 4) = 0; /* add zero padding */
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000161 memcpy(p, hostname, hostnamelen);
wdenkcbd8a352004-02-24 02:00:03 +0000162 p += hl / 4;
163 *p++ = 0; /* uid */
164 *p++ = 0; /* gid */
165 *p++ = 0; /* auxiliary gid list */
166
167 /* Provide an AUTH_NONE verifier. */
168 *p++ = 0; /* AUTH_NONE */
169 *p++ = 0; /* auth length */
170
171 return p;
172}
173
174/**************************************************************************
175RPC_LOOKUP - Lookup RPC Port numbers
176**************************************************************************/
177static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000178rpc_req(int rpc_prog, int rpc_proc, uint32_t *data, int datalen)
wdenkcbd8a352004-02-24 02:00:03 +0000179{
180 struct rpc_t pkt;
181 unsigned long id;
182 uint32_t *p;
183 int pktlen;
184 int sport;
185
wdenkc3f9d492004-03-14 00:59:59 +0000186 id = ++rpc_id;
wdenkcbd8a352004-02-24 02:00:03 +0000187 pkt.u.call.id = htonl(id);
188 pkt.u.call.type = htonl(MSG_CALL);
189 pkt.u.call.rpcvers = htonl(2); /* use RPC version 2 */
190 pkt.u.call.prog = htonl(rpc_prog);
191 pkt.u.call.vers = htonl(2); /* portmapper is version 2 */
192 pkt.u.call.proc = htonl(rpc_proc);
193 p = (uint32_t *)&(pkt.u.call.data);
194
195 if (datalen)
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000196 memcpy((char *)p, (char *)data, datalen*sizeof(uint32_t));
wdenkcbd8a352004-02-24 02:00:03 +0000197
198 pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt;
199
Joe Hershberger594c26f2012-05-23 07:58:04 +0000200 memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE,
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000201 (char *)&pkt, pktlen);
wdenkcbd8a352004-02-24 02:00:03 +0000202
203 if (rpc_prog == PROG_PORTMAP)
204 sport = SUNRPC_PORT;
205 else if (rpc_prog == PROG_MOUNT)
206 sport = NfsSrvMountPort;
207 else
208 sport = NfsSrvNfsPort;
209
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000210 NetSendUDPPacket(NetServerEther, NfsServerIP, sport, NfsOurPort,
211 pktlen);
wdenkcbd8a352004-02-24 02:00:03 +0000212}
213
214/**************************************************************************
215RPC_LOOKUP - Lookup RPC Port numbers
216**************************************************************************/
217static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000218rpc_lookup_req(int prog, int ver)
wdenkcbd8a352004-02-24 02:00:03 +0000219{
220 uint32_t data[16];
221
222 data[0] = 0; data[1] = 0; /* auth credential */
223 data[2] = 0; data[3] = 0; /* auth verifier */
224 data[4] = htonl(prog);
225 data[5] = htonl(ver);
226 data[6] = htonl(17); /* IP_UDP */
227 data[7] = 0;
228
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000229 rpc_req(PROG_PORTMAP, PORTMAP_GETPORT, data, 8);
wdenkcbd8a352004-02-24 02:00:03 +0000230}
231
232/**************************************************************************
233NFS_MOUNT - Mount an NFS Filesystem
234**************************************************************************/
235static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000236nfs_mount_req(char *path)
wdenkcbd8a352004-02-24 02:00:03 +0000237{
238 uint32_t data[1024];
239 uint32_t *p;
240 int len;
241 int pathlen;
242
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000243 pathlen = strlen(path);
wdenkcbd8a352004-02-24 02:00:03 +0000244
245 p = &(data[0]);
246 p = (uint32_t *)rpc_add_credentials((long *)p);
247
248 *p++ = htonl(pathlen);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000249 if (pathlen & 3)
250 *(p + pathlen / 4) = 0;
251 memcpy(p, path, pathlen);
wdenkcbd8a352004-02-24 02:00:03 +0000252 p += (pathlen + 3) / 4;
253
254 len = (uint32_t *)p - (uint32_t *)&(data[0]);
255
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000256 rpc_req(PROG_MOUNT, MOUNT_ADDENTRY, data, len);
wdenkcbd8a352004-02-24 02:00:03 +0000257}
258
259/**************************************************************************
260NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
261**************************************************************************/
262static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000263nfs_umountall_req(void)
wdenkcbd8a352004-02-24 02:00:03 +0000264{
265 uint32_t data[1024];
266 uint32_t *p;
267 int len;
268
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000269 if ((NfsSrvMountPort == -1) || (!fs_mounted))
wdenkcbd8a352004-02-24 02:00:03 +0000270 /* Nothing mounted, nothing to umount */
271 return;
wdenkcbd8a352004-02-24 02:00:03 +0000272
273 p = &(data[0]);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000274 p = (uint32_t *)rpc_add_credentials((long *)p);
wdenkcbd8a352004-02-24 02:00:03 +0000275
276 len = (uint32_t *)p - (uint32_t *)&(data[0]);
277
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000278 rpc_req(PROG_MOUNT, MOUNT_UMOUNTALL, data, len);
wdenkcbd8a352004-02-24 02:00:03 +0000279}
280
281/***************************************************************************
282 * NFS_READLINK (AH 2003-07-14)
283 * This procedure is called when read of the first block fails -
284 * this probably happens when it's a directory or a symlink
285 * In case of successful readlink(), the dirname is manipulated,
286 * so that inside the nfs() function a recursion can be done.
287 **************************************************************************/
288static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000289nfs_readlink_req(void)
wdenkcbd8a352004-02-24 02:00:03 +0000290{
291 uint32_t data[1024];
292 uint32_t *p;
293 int len;
294
295 p = &(data[0]);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000296 p = (uint32_t *)rpc_add_credentials((long *)p);
wdenkcbd8a352004-02-24 02:00:03 +0000297
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000298 memcpy(p, filefh, NFS_FHSIZE);
wdenkcbd8a352004-02-24 02:00:03 +0000299 p += (NFS_FHSIZE / 4);
300
301 len = (uint32_t *)p - (uint32_t *)&(data[0]);
302
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000303 rpc_req(PROG_NFS, NFS_READLINK, data, len);
wdenkcbd8a352004-02-24 02:00:03 +0000304}
305
306/**************************************************************************
307NFS_LOOKUP - Lookup Pathname
308**************************************************************************/
309static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000310nfs_lookup_req(char *fname)
wdenkcbd8a352004-02-24 02:00:03 +0000311{
312 uint32_t data[1024];
313 uint32_t *p;
314 int len;
315 int fnamelen;
316
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000317 fnamelen = strlen(fname);
wdenkcbd8a352004-02-24 02:00:03 +0000318
319 p = &(data[0]);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000320 p = (uint32_t *)rpc_add_credentials((long *)p);
wdenkcbd8a352004-02-24 02:00:03 +0000321
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000322 memcpy(p, dirfh, NFS_FHSIZE);
wdenkcbd8a352004-02-24 02:00:03 +0000323 p += (NFS_FHSIZE / 4);
324 *p++ = htonl(fnamelen);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000325 if (fnamelen & 3)
326 *(p + fnamelen / 4) = 0;
327 memcpy(p, fname, fnamelen);
wdenkcbd8a352004-02-24 02:00:03 +0000328 p += (fnamelen + 3) / 4;
329
330 len = (uint32_t *)p - (uint32_t *)&(data[0]);
331
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000332 rpc_req(PROG_NFS, NFS_LOOKUP, data, len);
wdenkcbd8a352004-02-24 02:00:03 +0000333}
334
335/**************************************************************************
336NFS_READ - Read File on NFS Server
337**************************************************************************/
338static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000339nfs_read_req(int offset, int readlen)
wdenkcbd8a352004-02-24 02:00:03 +0000340{
341 uint32_t data[1024];
342 uint32_t *p;
343 int len;
344
345 p = &(data[0]);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000346 p = (uint32_t *)rpc_add_credentials((long *)p);
wdenkcbd8a352004-02-24 02:00:03 +0000347
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000348 memcpy(p, filefh, NFS_FHSIZE);
wdenkcbd8a352004-02-24 02:00:03 +0000349 p += (NFS_FHSIZE / 4);
350 *p++ = htonl(offset);
351 *p++ = htonl(readlen);
352 *p++ = 0;
353
354 len = (uint32_t *)p - (uint32_t *)&(data[0]);
355
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000356 rpc_req(PROG_NFS, NFS_READ, data, len);
wdenkcbd8a352004-02-24 02:00:03 +0000357}
358
359/**************************************************************************
360RPC request dispatcher
361**************************************************************************/
362
363static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000364NfsSend(void)
wdenkcbd8a352004-02-24 02:00:03 +0000365{
Robin Getz0ebf04c2009-07-23 03:01:03 -0400366 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000367
368 switch (NfsState) {
369 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000370 rpc_lookup_req(PROG_MOUNT, 1);
wdenkcbd8a352004-02-24 02:00:03 +0000371 break;
372 case STATE_PRCLOOKUP_PROG_NFS_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000373 rpc_lookup_req(PROG_NFS, 2);
wdenkcbd8a352004-02-24 02:00:03 +0000374 break;
375 case STATE_MOUNT_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000376 nfs_mount_req(nfs_path);
wdenkcbd8a352004-02-24 02:00:03 +0000377 break;
378 case STATE_UMOUNT_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000379 nfs_umountall_req();
wdenkcbd8a352004-02-24 02:00:03 +0000380 break;
381 case STATE_LOOKUP_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000382 nfs_lookup_req(nfs_filename);
wdenkcbd8a352004-02-24 02:00:03 +0000383 break;
384 case STATE_READ_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000385 nfs_read_req(nfs_offset, nfs_len);
wdenkcbd8a352004-02-24 02:00:03 +0000386 break;
387 case STATE_READLINK_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000388 nfs_readlink_req();
wdenkcbd8a352004-02-24 02:00:03 +0000389 break;
390 }
391}
392
393/**************************************************************************
394Handlers for the reply from server
395**************************************************************************/
396
397static int
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000398rpc_lookup_reply(int prog, uchar *pkt, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000399{
400 struct rpc_t rpc_pkt;
401
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000402 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkcbd8a352004-02-24 02:00:03 +0000403
Robin Getz0ebf04c2009-07-23 03:01:03 -0400404 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000405
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100406 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
407 return -NFS_RPC_ERR;
408 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
409 return -NFS_RPC_DROP;
wdenkc3f9d492004-03-14 00:59:59 +0000410
wdenkcbd8a352004-02-24 02:00:03 +0000411 if (rpc_pkt.u.reply.rstatus ||
412 rpc_pkt.u.reply.verifier ||
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000413 rpc_pkt.u.reply.astatus)
wdenkc3f9d492004-03-14 00:59:59 +0000414 return -1;
wdenkcbd8a352004-02-24 02:00:03 +0000415
416 switch (prog) {
417 case PROG_MOUNT:
418 NfsSrvMountPort = ntohl(rpc_pkt.u.reply.data[0]);
419 break;
420 case PROG_NFS:
421 NfsSrvNfsPort = ntohl(rpc_pkt.u.reply.data[0]);
422 break;
423 }
424
425 return 0;
426}
427
428static int
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000429nfs_mount_reply(uchar *pkt, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000430{
431 struct rpc_t rpc_pkt;
432
Robin Getz0ebf04c2009-07-23 03:01:03 -0400433 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000434
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000435 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkcbd8a352004-02-24 02:00:03 +0000436
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100437 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
438 return -NFS_RPC_ERR;
439 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
440 return -NFS_RPC_DROP;
wdenkc3f9d492004-03-14 00:59:59 +0000441
wdenkcbd8a352004-02-24 02:00:03 +0000442 if (rpc_pkt.u.reply.rstatus ||
443 rpc_pkt.u.reply.verifier ||
444 rpc_pkt.u.reply.astatus ||
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000445 rpc_pkt.u.reply.data[0])
wdenkcbd8a352004-02-24 02:00:03 +0000446 return -1;
wdenkcbd8a352004-02-24 02:00:03 +0000447
448 fs_mounted = 1;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000449 memcpy(dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
wdenkcbd8a352004-02-24 02:00:03 +0000450
451 return 0;
452}
453
454static int
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000455nfs_umountall_reply(uchar *pkt, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000456{
457 struct rpc_t rpc_pkt;
458
Robin Getz0ebf04c2009-07-23 03:01:03 -0400459 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000460
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000461 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkcbd8a352004-02-24 02:00:03 +0000462
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100463 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
464 return -NFS_RPC_ERR;
465 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
466 return -NFS_RPC_DROP;
wdenkc3f9d492004-03-14 00:59:59 +0000467
wdenkcbd8a352004-02-24 02:00:03 +0000468 if (rpc_pkt.u.reply.rstatus ||
469 rpc_pkt.u.reply.verifier ||
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000470 rpc_pkt.u.reply.astatus)
wdenkcbd8a352004-02-24 02:00:03 +0000471 return -1;
wdenkcbd8a352004-02-24 02:00:03 +0000472
473 fs_mounted = 0;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000474 memset(dirfh, 0, sizeof(dirfh));
wdenkcbd8a352004-02-24 02:00:03 +0000475
476 return 0;
477}
478
479static int
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000480nfs_lookup_reply(uchar *pkt, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000481{
482 struct rpc_t rpc_pkt;
483
Robin Getz0ebf04c2009-07-23 03:01:03 -0400484 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000485
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000486 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkcbd8a352004-02-24 02:00:03 +0000487
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100488 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
489 return -NFS_RPC_ERR;
490 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
491 return -NFS_RPC_DROP;
wdenkc3f9d492004-03-14 00:59:59 +0000492
wdenkcbd8a352004-02-24 02:00:03 +0000493 if (rpc_pkt.u.reply.rstatus ||
494 rpc_pkt.u.reply.verifier ||
495 rpc_pkt.u.reply.astatus ||
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000496 rpc_pkt.u.reply.data[0])
wdenkcbd8a352004-02-24 02:00:03 +0000497 return -1;
wdenkcbd8a352004-02-24 02:00:03 +0000498
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000499 memcpy(filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
wdenkcbd8a352004-02-24 02:00:03 +0000500
501 return 0;
502}
503
504static int
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000505nfs_readlink_reply(uchar *pkt, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000506{
507 struct rpc_t rpc_pkt;
508 int rlen;
509
Robin Getz0ebf04c2009-07-23 03:01:03 -0400510 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000511
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000512 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkcbd8a352004-02-24 02:00:03 +0000513
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100514 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
515 return -NFS_RPC_ERR;
516 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
517 return -NFS_RPC_DROP;
wdenkc3f9d492004-03-14 00:59:59 +0000518
wdenkcbd8a352004-02-24 02:00:03 +0000519 if (rpc_pkt.u.reply.rstatus ||
520 rpc_pkt.u.reply.verifier ||
521 rpc_pkt.u.reply.astatus ||
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000522 rpc_pkt.u.reply.data[0])
wdenkcbd8a352004-02-24 02:00:03 +0000523 return -1;
wdenkcbd8a352004-02-24 02:00:03 +0000524
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000525 rlen = ntohl(rpc_pkt.u.reply.data[1]); /* new path length */
wdenkcbd8a352004-02-24 02:00:03 +0000526
527 if (*((char *)&(rpc_pkt.u.reply.data[2])) != '/') {
528 int pathlen;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000529 strcat(nfs_path, "/");
wdenkcbd8a352004-02-24 02:00:03 +0000530 pathlen = strlen(nfs_path);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000531 memcpy(nfs_path + pathlen, (uchar *)&(rpc_pkt.u.reply.data[2]),
532 rlen);
Ed Swarthoutf64ef9b2009-11-19 02:47:28 -0600533 nfs_path[pathlen + rlen] = 0;
wdenkcbd8a352004-02-24 02:00:03 +0000534 } else {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000535 memcpy(nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen);
wdenkcbd8a352004-02-24 02:00:03 +0000536 nfs_path[rlen] = 0;
537 }
538 return 0;
539}
540
541static int
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000542nfs_read_reply(uchar *pkt, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000543{
544 struct rpc_t rpc_pkt;
545 int rlen;
546
Robin Getz0ebf04c2009-07-23 03:01:03 -0400547 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000548
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000549 memcpy((uchar *)&rpc_pkt, pkt, sizeof(rpc_pkt.u.reply));
wdenkcbd8a352004-02-24 02:00:03 +0000550
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100551 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
552 return -NFS_RPC_ERR;
553 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
554 return -NFS_RPC_DROP;
wdenkc3f9d492004-03-14 00:59:59 +0000555
wdenkcbd8a352004-02-24 02:00:03 +0000556 if (rpc_pkt.u.reply.rstatus ||
557 rpc_pkt.u.reply.verifier ||
558 rpc_pkt.u.reply.astatus ||
559 rpc_pkt.u.reply.data[0]) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000560 if (rpc_pkt.u.reply.rstatus)
wdenkcbd8a352004-02-24 02:00:03 +0000561 return -9999;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000562 if (rpc_pkt.u.reply.astatus)
wdenkcbd8a352004-02-24 02:00:03 +0000563 return -9999;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000564 return -ntohl(rpc_pkt.u.reply.data[0]);
wdenkcbd8a352004-02-24 02:00:03 +0000565 }
566
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000567 if ((nfs_offset != 0) && !((nfs_offset) %
568 (NFS_READ_SIZE / 2 * 10 * HASHES_PER_LINE)))
569 puts("\n\t ");
570 if (!(nfs_offset % ((NFS_READ_SIZE / 2) * 10)))
571 putc('#');
wdenkcbd8a352004-02-24 02:00:03 +0000572
573 rlen = ntohl(rpc_pkt.u.reply.data[18]);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000574 if (store_block((uchar *)pkt + sizeof(rpc_pkt.u.reply),
575 nfs_offset, rlen))
Wolfgang Denk23a7a322005-08-06 01:11:12 +0200576 return -9999;
wdenkcbd8a352004-02-24 02:00:03 +0000577
578 return rlen;
579}
580
581/**************************************************************************
582Interfaces of U-BOOT
583**************************************************************************/
584
585static void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000586NfsTimeout(void)
wdenka5725fa2004-09-28 21:51:42 +0000587{
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000588 if (++NfsTimeoutCount > NFS_RETRY_COUNT) {
589 puts("\nRetry count exceeded; starting again\n");
590 NetStartAgain();
Evan Samanasaabb8cb2009-11-09 20:08:36 -0600591 } else {
592 puts("T ");
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100593 NetSetTimeout(nfs_timeout + NFS_TIMEOUT * NfsTimeoutCount,
594 NfsTimeout);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000595 NfsSend();
Hiroshi Itofe891ec2008-01-31 18:35:04 +0900596 }
wdenka5725fa2004-09-28 21:51:42 +0000597}
598
599static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000600NfsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len)
wdenkcbd8a352004-02-24 02:00:03 +0000601{
602 int rlen;
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100603 int reply;
wdenkcbd8a352004-02-24 02:00:03 +0000604
Robin Getz0ebf04c2009-07-23 03:01:03 -0400605 debug("%s\n", __func__);
wdenkcbd8a352004-02-24 02:00:03 +0000606
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000607 if (dest != NfsOurPort)
608 return;
wdenkcbd8a352004-02-24 02:00:03 +0000609
610 switch (NfsState) {
611 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100612 if (rpc_lookup_reply(PROG_MOUNT, pkt, len) == -NFS_RPC_DROP)
613 break;
wdenkcbd8a352004-02-24 02:00:03 +0000614 NfsState = STATE_PRCLOOKUP_PROG_NFS_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000615 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000616 break;
617
618 case STATE_PRCLOOKUP_PROG_NFS_REQ:
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100619 if (rpc_lookup_reply(PROG_NFS, pkt, len) == -NFS_RPC_DROP)
620 break;
wdenkcbd8a352004-02-24 02:00:03 +0000621 NfsState = STATE_MOUNT_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000622 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000623 break;
624
625 case STATE_MOUNT_REQ:
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100626 reply = nfs_mount_reply(pkt, len);
627 if (reply == -NFS_RPC_DROP)
628 break;
629 else if (reply == -NFS_RPC_ERR) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000630 puts("*** ERROR: Cannot mount\n");
wdenkcbd8a352004-02-24 02:00:03 +0000631 /* just to be sure... */
632 NfsState = STATE_UMOUNT_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000633 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000634 } else {
635 NfsState = STATE_LOOKUP_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000636 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000637 }
638 break;
639
640 case STATE_UMOUNT_REQ:
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100641 reply = nfs_umountall_reply(pkt, len);
642 if (reply == -NFS_RPC_DROP)
643 break;
644 else if (reply == -NFS_RPC_ERR) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000645 puts("*** ERROR: Cannot umount\n");
Joe Hershberger22f6e992012-05-23 07:59:14 +0000646 net_set_state(NETLOOP_FAIL);
wdenkcbd8a352004-02-24 02:00:03 +0000647 } else {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000648 puts("\ndone\n");
Joe Hershberger22f6e992012-05-23 07:59:14 +0000649 net_set_state(nfs_download_state);
wdenkcbd8a352004-02-24 02:00:03 +0000650 }
651 break;
652
653 case STATE_LOOKUP_REQ:
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100654 reply = nfs_lookup_reply(pkt, len);
655 if (reply == -NFS_RPC_DROP)
656 break;
657 else if (reply == -NFS_RPC_ERR) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000658 puts("*** ERROR: File lookup fail\n");
wdenkcbd8a352004-02-24 02:00:03 +0000659 NfsState = STATE_UMOUNT_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000660 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000661 } else {
662 NfsState = STATE_READ_REQ;
663 nfs_offset = 0;
664 nfs_len = NFS_READ_SIZE;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000665 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000666 }
667 break;
668
669 case STATE_READLINK_REQ:
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100670 reply = nfs_readlink_reply(pkt, len);
671 if (reply == -NFS_RPC_DROP)
672 break;
673 else if (reply == -NFS_RPC_ERR) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000674 puts("*** ERROR: Symlink fail\n");
wdenkcbd8a352004-02-24 02:00:03 +0000675 NfsState = STATE_UMOUNT_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000676 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000677 } else {
Robin Getz0ebf04c2009-07-23 03:01:03 -0400678 debug("Symlink --> %s\n", nfs_path);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000679 nfs_filename = basename(nfs_path);
680 nfs_path = dirname(nfs_path);
wdenkcbd8a352004-02-24 02:00:03 +0000681
682 NfsState = STATE_MOUNT_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000683 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000684 }
685 break;
686
687 case STATE_READ_REQ:
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000688 rlen = nfs_read_reply(pkt, len);
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100689 NetSetTimeout(nfs_timeout, NfsTimeout);
wdenkcbd8a352004-02-24 02:00:03 +0000690 if (rlen > 0) {
691 nfs_offset += rlen;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000692 NfsSend();
693 } else if ((rlen == -NFSERR_ISDIR) || (rlen == -NFSERR_INVAL)) {
wdenkcbd8a352004-02-24 02:00:03 +0000694 /* symbolic link */
695 NfsState = STATE_READLINK_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000696 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000697 } else {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000698 if (!rlen)
Joe Hershberger22f6e992012-05-23 07:59:14 +0000699 nfs_download_state = NETLOOP_SUCCESS;
wdenkcbd8a352004-02-24 02:00:03 +0000700 NfsState = STATE_UMOUNT_REQ;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000701 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000702 }
703 break;
704 }
705}
706
wdenkcbd8a352004-02-24 02:00:03 +0000707
708void
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000709NfsStart(void)
wdenkcbd8a352004-02-24 02:00:03 +0000710{
Robin Getz0ebf04c2009-07-23 03:01:03 -0400711 debug("%s\n", __func__);
Joe Hershberger22f6e992012-05-23 07:59:14 +0000712 nfs_download_state = NETLOOP_FAIL;
wdenkcbd8a352004-02-24 02:00:03 +0000713
714 NfsServerIP = NetServerIP;
715 nfs_path = (char *)nfs_path_buff;
716
717 if (nfs_path == NULL) {
Joe Hershberger22f6e992012-05-23 07:59:14 +0000718 net_set_state(NETLOOP_FAIL);
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000719 puts("*** ERROR: Fail allocate memory\n");
wdenkcbd8a352004-02-24 02:00:03 +0000720 return;
721 }
722
723 if (BootFile[0] == '\0') {
Matthias Weisserea45cb02011-12-03 03:29:44 +0000724 sprintf(default_filename, "/nfsroot/%02X%02X%02X%02X.img",
Wolfgang Denkc43352c2005-08-04 01:09:44 +0200725 NetOurIP & 0xFF,
726 (NetOurIP >> 8) & 0xFF,
727 (NetOurIP >> 16) & 0xFF,
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000728 (NetOurIP >> 24) & 0xFF);
729 strcpy(nfs_path, default_filename);
wdenkcbd8a352004-02-24 02:00:03 +0000730
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000731 printf("*** Warning: no boot file name; using '%s'\n",
wdenkcbd8a352004-02-24 02:00:03 +0000732 nfs_path);
733 } else {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000734 char *p = BootFile;
wdenkcbd8a352004-02-24 02:00:03 +0000735
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000736 p = strchr(p, ':');
wdenkcbd8a352004-02-24 02:00:03 +0000737
738 if (p != NULL) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000739 NfsServerIP = string_to_ip(BootFile);
wdenkcbd8a352004-02-24 02:00:03 +0000740 ++p;
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000741 strcpy(nfs_path, p);
wdenkcbd8a352004-02-24 02:00:03 +0000742 } else {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000743 strcpy(nfs_path, BootFile);
wdenkcbd8a352004-02-24 02:00:03 +0000744 }
745 }
746
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000747 nfs_filename = basename(nfs_path);
748 nfs_path = dirname(nfs_path);
wdenkcbd8a352004-02-24 02:00:03 +0000749
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000750 printf("Using %s device\n", eth_get_name());
wdenkcbd8a352004-02-24 02:00:03 +0000751
Mike Frysingerb6446b62009-02-17 00:00:53 -0500752 printf("File transfer via NFS from server %pI4"
753 "; our IP address is %pI4", &NfsServerIP, &NetOurIP);
wdenkcbd8a352004-02-24 02:00:03 +0000754
755 /* Check if we need to send across this subnet */
756 if (NetOurGatewayIP && NetOurSubnetMask) {
757 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
758 IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask;
759
Mike Frysingerb6446b62009-02-17 00:00:53 -0500760 if (OurNet != ServerNet)
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000761 printf("; sending through gateway %pI4",
762 &NetOurGatewayIP);
wdenkcbd8a352004-02-24 02:00:03 +0000763 }
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000764 printf("\nFilename '%s/%s'.", nfs_path, nfs_filename);
wdenkcbd8a352004-02-24 02:00:03 +0000765
766 if (NetBootFileSize) {
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000767 printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
768 print_size(NetBootFileSize<<9, "");
wdenkcbd8a352004-02-24 02:00:03 +0000769 }
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000770 printf("\nLoad address: 0x%lx\n"
wdenk4b9206e2004-03-23 22:14:11 +0000771 "Loading: *\b", load_addr);
wdenkcbd8a352004-02-24 02:00:03 +0000772
Matthias Bruggerfa84fa72012-12-11 19:14:16 +0100773 NetSetTimeout(nfs_timeout, NfsTimeout);
Joe Hershbergerece223b2012-05-23 07:59:15 +0000774 net_set_udp_handler(NfsHandler);
wdenkcbd8a352004-02-24 02:00:03 +0000775
wdenkcbd8a352004-02-24 02:00:03 +0000776 NfsTimeoutCount = 0;
777 NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
778
779 /*NfsOurPort = 4096 + (get_ticks() % 3072);*/
780 /*FIX ME !!!*/
781 NfsOurPort = 1000;
782
783 /* zero out server ether in case the server ip has changed */
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000784 memset(NetServerEther, 0, 6);
wdenkcbd8a352004-02-24 02:00:03 +0000785
Joe Hershbergerc9f6c912012-05-15 08:59:09 +0000786 NfsSend();
wdenkcbd8a352004-02-24 02:00:03 +0000787}