blob: 4a5a1ab2a918b91cef552d046d01715cdbebcca4 [file] [log] [blame]
wdenkbe9c1cb2004-02-24 02:00:03 +00001/*
2 * NFS support driver - based on etherboot and U-BOOT's tftp.c
3 *
4 * Masami Komiya <mkomiya@sonare.it> 2004
5 *
6 */
7
8/* NOTE: the NFS code is heavily inspired by the NetBSD netboot code (read:
9 * large portions are copied verbatim) as distributed in OSKit 0.97. A few
10 * changes were necessary to adapt the code to Etherboot and to fix several
11 * inconsistencies. Also the RPC message preparation is done "by hand" to
12 * avoid adding netsprintf() which I find hard to understand and use. */
13
14/* NOTE 2: Etherboot does not care about things beyond the kernel image, so
15 * it loads the kernel image off the boot server (ARP_SERVER) and does not
16 * access the client root disk (root-path in dhcpd.conf), which would use
17 * ARP_ROOTSERVER. The root disk is something the operating system we are
18 * about to load needs to use. This is different from the OSKit 0.97 logic. */
19
20/* NOTE 3: Symlink handling introduced by Anselm M Hoffmeister, 2003-July-14
21 * If a symlink is encountered, it is followed as far as possible (recursion
22 * possible, maximum 16 steps). There is no clearing of ".."'s inside the
23 * path, so please DON'T DO THAT. thx. */
24
25#include <common.h>
26#include <command.h>
27#include <net.h>
28#include <malloc.h>
Joe Hershbergered191852015-03-22 17:09:08 -050029#include <mapmem.h>
wdenkbe9c1cb2004-02-24 02:00:03 +000030#include "nfs.h"
31#include "bootp.h"
32
wdenkbe9c1cb2004-02-24 02:00:03 +000033#define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
Hiroshi Ito3702cd72008-01-31 18:35:04 +090034#define NFS_RETRY_COUNT 30
Tetsuyuki Kobayashi147e3902012-07-03 22:25:21 +000035#ifndef CONFIG_NFS_TIMEOUT
36# define NFS_TIMEOUT 2000UL
37#else
38# define NFS_TIMEOUT CONFIG_NFS_TIMEOUT
39#endif
wdenkbe9c1cb2004-02-24 02:00:03 +000040
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +010041#define NFS_RPC_ERR 1
42#define NFS_RPC_DROP 124
43
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +000044static int fs_mounted;
45static unsigned long rpc_id;
wdenkbe9c1cb2004-02-24 02:00:03 +000046static int nfs_offset = -1;
47static int nfs_len;
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +010048static ulong nfs_timeout = NFS_TIMEOUT;
wdenkbe9c1cb2004-02-24 02:00:03 +000049
50static char dirfh[NFS_FHSIZE]; /* file handle of directory */
51static char filefh[NFS_FHSIZE]; /* file handle of kernel image */
52
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000053static enum net_loop_state nfs_download_state;
Joe Hershberger5874dec2015-04-08 01:41:01 -050054static struct in_addr nfs_server_ip;
Joe Hershberger40d7ca92015-04-08 01:41:10 -050055static int nfs_server_mount_port;
56static int nfs_server_port;
57static int nfs_our_port;
58static int nfs_timeout_count;
59static int nfs_state;
wdenkbe9c1cb2004-02-24 02:00:03 +000060#define STATE_PRCLOOKUP_PROG_MOUNT_REQ 1
61#define STATE_PRCLOOKUP_PROG_NFS_REQ 2
62#define STATE_MOUNT_REQ 3
63#define STATE_UMOUNT_REQ 4
64#define STATE_LOOKUP_REQ 5
65#define STATE_READ_REQ 6
66#define STATE_READLINK_REQ 7
67
68static char default_filename[64];
69static char *nfs_filename;
70static char *nfs_path;
71static char nfs_path_buff[2048];
72
Joe Hershberger40d7ca92015-04-08 01:41:10 -050073static inline int store_block(uchar *src, unsigned offset, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +000074{
wdenk77ef4c62004-02-24 22:33:21 +000075 ulong newsize = offset + len;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020076#ifdef CONFIG_SYS_DIRECT_FLASH_NFS
wdenkbe9c1cb2004-02-24 02:00:03 +000077 int i, rc = 0;
78
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +000079 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenkbe9c1cb2004-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 Hershberger6ecfc6b2012-05-15 08:59:09 +000088 rc = flash_write((uchar *)src, (ulong)(load_addr+offset), len);
wdenkbe9c1cb2004-02-24 02:00:03 +000089 if (rc) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +000090 flash_perror(rc);
Wolfgang Denkb077b502005-08-06 01:11:12 +020091 return -1;
wdenkbe9c1cb2004-02-24 02:00:03 +000092 }
93 } else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020094#endif /* CONFIG_SYS_DIRECT_FLASH_NFS */
wdenkbe9c1cb2004-02-24 02:00:03 +000095 {
Joe Hershbergered191852015-03-22 17:09:08 -050096 void *ptr = map_sysmem(load_addr + offset, len);
97
98 memcpy(ptr, src, len);
99 unmap_sysmem(ptr);
wdenkbe9c1cb2004-02-24 02:00:03 +0000100 }
wdenk77ef4c62004-02-24 22:33:21 +0000101
Joe Hershberger290c8992015-04-08 01:41:02 -0500102 if (net_boot_file_size < (offset + len))
103 net_boot_file_size = newsize;
Wolfgang Denkb077b502005-08-06 01:11:12 +0200104 return 0;
wdenkbe9c1cb2004-02-24 02:00:03 +0000105}
106
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500107static char *basename(char *path)
wdenkbe9c1cb2004-02-24 02:00:03 +0000108{
109 char *fname;
110
111 fname = path + strlen(path) - 1;
112 while (fname >= path) {
113 if (*fname == '/') {
114 fname++;
115 break;
116 }
117 fname--;
118 }
119 return fname;
120}
121
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500122static char *dirname(char *path)
wdenkbe9c1cb2004-02-24 02:00:03 +0000123{
124 char *fname;
125
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000126 fname = basename(path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000127 --fname;
128 *fname = '\0';
129 return path;
130}
131
132/**************************************************************************
wdenkbe9c1cb2004-02-24 02:00:03 +0000133RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
134**************************************************************************/
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200135static uint32_t *rpc_add_credentials(uint32_t *p)
wdenkbe9c1cb2004-02-24 02:00:03 +0000136{
137 int hl;
138 int hostnamelen;
139 char hostname[256];
140
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000141 strcpy(hostname, "");
142 hostnamelen = strlen(hostname);
wdenkbe9c1cb2004-02-24 02:00:03 +0000143
144 /* Here's the executive summary on authentication requirements of the
145 * various NFS server implementations: Linux accepts both AUTH_NONE
146 * and AUTH_UNIX authentication (also accepts an empty hostname field
147 * in the AUTH_UNIX scheme). *BSD refuses AUTH_NONE, but accepts
148 * AUTH_UNIX (also accepts an empty hostname field in the AUTH_UNIX
149 * scheme). To be safe, use AUTH_UNIX and pass the hostname if we have
150 * it (if the BOOTP/DHCP reply didn't give one, just use an empty
151 * hostname). */
152
153 hl = (hostnamelen + 3) & ~3;
154
155 /* Provide an AUTH_UNIX credential. */
156 *p++ = htonl(1); /* AUTH_UNIX */
157 *p++ = htonl(hl+20); /* auth length */
158 *p++ = htonl(0); /* stamp */
159 *p++ = htonl(hostnamelen); /* hostname string */
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000160 if (hostnamelen & 3)
wdenkbe9c1cb2004-02-24 02:00:03 +0000161 *(p + hostnamelen / 4) = 0; /* add zero padding */
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000162 memcpy(p, hostname, hostnamelen);
wdenkbe9c1cb2004-02-24 02:00:03 +0000163 p += hl / 4;
164 *p++ = 0; /* uid */
165 *p++ = 0; /* gid */
166 *p++ = 0; /* auxiliary gid list */
167
168 /* Provide an AUTH_NONE verifier. */
169 *p++ = 0; /* AUTH_NONE */
170 *p++ = 0; /* auth length */
171
172 return p;
173}
174
175/**************************************************************************
176RPC_LOOKUP - Lookup RPC Port numbers
177**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500178static void rpc_req(int rpc_prog, int rpc_proc, uint32_t *data, int datalen)
wdenkbe9c1cb2004-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
wdenkacd9b102004-03-14 00:59:59 +0000186 id = ++rpc_id;
wdenkbe9c1cb2004-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 Hershberger6ecfc6b2012-05-15 08:59:09 +0000196 memcpy((char *)p, (char *)data, datalen*sizeof(uint32_t));
wdenkbe9c1cb2004-02-24 02:00:03 +0000197
198 pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt;
199
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500200 memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE,
201 (char *)&pkt, pktlen);
wdenkbe9c1cb2004-02-24 02:00:03 +0000202
203 if (rpc_prog == PROG_PORTMAP)
204 sport = SUNRPC_PORT;
205 else if (rpc_prog == PROG_MOUNT)
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500206 sport = nfs_server_mount_port;
wdenkbe9c1cb2004-02-24 02:00:03 +0000207 else
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500208 sport = nfs_server_port;
wdenkbe9c1cb2004-02-24 02:00:03 +0000209
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500210 net_send_udp_packet(net_server_ethaddr, nfs_server_ip, sport,
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500211 nfs_our_port, pktlen);
wdenkbe9c1cb2004-02-24 02:00:03 +0000212}
213
214/**************************************************************************
215RPC_LOOKUP - Lookup RPC Port numbers
216**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500217static void rpc_lookup_req(int prog, int ver)
wdenkbe9c1cb2004-02-24 02:00:03 +0000218{
219 uint32_t data[16];
220
221 data[0] = 0; data[1] = 0; /* auth credential */
222 data[2] = 0; data[3] = 0; /* auth verifier */
223 data[4] = htonl(prog);
224 data[5] = htonl(ver);
225 data[6] = htonl(17); /* IP_UDP */
226 data[7] = 0;
227
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000228 rpc_req(PROG_PORTMAP, PORTMAP_GETPORT, data, 8);
wdenkbe9c1cb2004-02-24 02:00:03 +0000229}
230
231/**************************************************************************
232NFS_MOUNT - Mount an NFS Filesystem
233**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500234static void nfs_mount_req(char *path)
wdenkbe9c1cb2004-02-24 02:00:03 +0000235{
236 uint32_t data[1024];
237 uint32_t *p;
238 int len;
239 int pathlen;
240
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000241 pathlen = strlen(path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000242
243 p = &(data[0]);
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200244 p = rpc_add_credentials(p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000245
246 *p++ = htonl(pathlen);
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000247 if (pathlen & 3)
248 *(p + pathlen / 4) = 0;
249 memcpy(p, path, pathlen);
wdenkbe9c1cb2004-02-24 02:00:03 +0000250 p += (pathlen + 3) / 4;
251
252 len = (uint32_t *)p - (uint32_t *)&(data[0]);
253
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000254 rpc_req(PROG_MOUNT, MOUNT_ADDENTRY, data, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000255}
256
257/**************************************************************************
258NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
259**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500260static void nfs_umountall_req(void)
wdenkbe9c1cb2004-02-24 02:00:03 +0000261{
262 uint32_t data[1024];
263 uint32_t *p;
264 int len;
265
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500266 if ((nfs_server_mount_port == -1) || (!fs_mounted))
wdenkbe9c1cb2004-02-24 02:00:03 +0000267 /* Nothing mounted, nothing to umount */
268 return;
wdenkbe9c1cb2004-02-24 02:00:03 +0000269
270 p = &(data[0]);
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200271 p = rpc_add_credentials(p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000272
273 len = (uint32_t *)p - (uint32_t *)&(data[0]);
274
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000275 rpc_req(PROG_MOUNT, MOUNT_UMOUNTALL, data, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000276}
277
278/***************************************************************************
279 * NFS_READLINK (AH 2003-07-14)
280 * This procedure is called when read of the first block fails -
281 * this probably happens when it's a directory or a symlink
282 * In case of successful readlink(), the dirname is manipulated,
283 * so that inside the nfs() function a recursion can be done.
284 **************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500285static void nfs_readlink_req(void)
wdenkbe9c1cb2004-02-24 02:00:03 +0000286{
287 uint32_t data[1024];
288 uint32_t *p;
289 int len;
290
291 p = &(data[0]);
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200292 p = rpc_add_credentials(p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000293
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000294 memcpy(p, filefh, NFS_FHSIZE);
wdenkbe9c1cb2004-02-24 02:00:03 +0000295 p += (NFS_FHSIZE / 4);
296
297 len = (uint32_t *)p - (uint32_t *)&(data[0]);
298
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000299 rpc_req(PROG_NFS, NFS_READLINK, data, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000300}
301
302/**************************************************************************
303NFS_LOOKUP - Lookup Pathname
304**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500305static void nfs_lookup_req(char *fname)
wdenkbe9c1cb2004-02-24 02:00:03 +0000306{
307 uint32_t data[1024];
308 uint32_t *p;
309 int len;
310 int fnamelen;
311
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000312 fnamelen = strlen(fname);
wdenkbe9c1cb2004-02-24 02:00:03 +0000313
314 p = &(data[0]);
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200315 p = rpc_add_credentials(p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000316
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000317 memcpy(p, dirfh, NFS_FHSIZE);
wdenkbe9c1cb2004-02-24 02:00:03 +0000318 p += (NFS_FHSIZE / 4);
319 *p++ = htonl(fnamelen);
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000320 if (fnamelen & 3)
321 *(p + fnamelen / 4) = 0;
322 memcpy(p, fname, fnamelen);
wdenkbe9c1cb2004-02-24 02:00:03 +0000323 p += (fnamelen + 3) / 4;
324
325 len = (uint32_t *)p - (uint32_t *)&(data[0]);
326
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000327 rpc_req(PROG_NFS, NFS_LOOKUP, data, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000328}
329
330/**************************************************************************
331NFS_READ - Read File on NFS Server
332**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500333static void nfs_read_req(int offset, int readlen)
wdenkbe9c1cb2004-02-24 02:00:03 +0000334{
335 uint32_t data[1024];
336 uint32_t *p;
337 int len;
338
339 p = &(data[0]);
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200340 p = rpc_add_credentials(p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000341
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000342 memcpy(p, filefh, NFS_FHSIZE);
wdenkbe9c1cb2004-02-24 02:00:03 +0000343 p += (NFS_FHSIZE / 4);
344 *p++ = htonl(offset);
345 *p++ = htonl(readlen);
346 *p++ = 0;
347
348 len = (uint32_t *)p - (uint32_t *)&(data[0]);
349
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000350 rpc_req(PROG_NFS, NFS_READ, data, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000351}
352
353/**************************************************************************
354RPC request dispatcher
355**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500356static void nfs_send(void)
wdenkbe9c1cb2004-02-24 02:00:03 +0000357{
Robin Getz9e0a4d62009-07-23 03:01:03 -0400358 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000359
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500360 switch (nfs_state) {
wdenkbe9c1cb2004-02-24 02:00:03 +0000361 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000362 rpc_lookup_req(PROG_MOUNT, 1);
wdenkbe9c1cb2004-02-24 02:00:03 +0000363 break;
364 case STATE_PRCLOOKUP_PROG_NFS_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000365 rpc_lookup_req(PROG_NFS, 2);
wdenkbe9c1cb2004-02-24 02:00:03 +0000366 break;
367 case STATE_MOUNT_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000368 nfs_mount_req(nfs_path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000369 break;
370 case STATE_UMOUNT_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000371 nfs_umountall_req();
wdenkbe9c1cb2004-02-24 02:00:03 +0000372 break;
373 case STATE_LOOKUP_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000374 nfs_lookup_req(nfs_filename);
wdenkbe9c1cb2004-02-24 02:00:03 +0000375 break;
376 case STATE_READ_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000377 nfs_read_req(nfs_offset, nfs_len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000378 break;
379 case STATE_READLINK_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000380 nfs_readlink_req();
wdenkbe9c1cb2004-02-24 02:00:03 +0000381 break;
382 }
383}
384
385/**************************************************************************
386Handlers for the reply from server
387**************************************************************************/
388
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500389static int rpc_lookup_reply(int prog, uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000390{
391 struct rpc_t rpc_pkt;
392
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000393 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000394
Robin Getz9e0a4d62009-07-23 03:01:03 -0400395 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000396
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100397 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
398 return -NFS_RPC_ERR;
399 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
400 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000401
wdenkbe9c1cb2004-02-24 02:00:03 +0000402 if (rpc_pkt.u.reply.rstatus ||
403 rpc_pkt.u.reply.verifier ||
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000404 rpc_pkt.u.reply.astatus)
wdenkacd9b102004-03-14 00:59:59 +0000405 return -1;
wdenkbe9c1cb2004-02-24 02:00:03 +0000406
407 switch (prog) {
408 case PROG_MOUNT:
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500409 nfs_server_mount_port = ntohl(rpc_pkt.u.reply.data[0]);
wdenkbe9c1cb2004-02-24 02:00:03 +0000410 break;
411 case PROG_NFS:
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500412 nfs_server_port = ntohl(rpc_pkt.u.reply.data[0]);
wdenkbe9c1cb2004-02-24 02:00:03 +0000413 break;
414 }
415
416 return 0;
417}
418
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500419static int nfs_mount_reply(uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000420{
421 struct rpc_t rpc_pkt;
422
Robin Getz9e0a4d62009-07-23 03:01:03 -0400423 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000424
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000425 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000426
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100427 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
428 return -NFS_RPC_ERR;
429 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
430 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000431
wdenkbe9c1cb2004-02-24 02:00:03 +0000432 if (rpc_pkt.u.reply.rstatus ||
433 rpc_pkt.u.reply.verifier ||
434 rpc_pkt.u.reply.astatus ||
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000435 rpc_pkt.u.reply.data[0])
wdenkbe9c1cb2004-02-24 02:00:03 +0000436 return -1;
wdenkbe9c1cb2004-02-24 02:00:03 +0000437
438 fs_mounted = 1;
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000439 memcpy(dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
wdenkbe9c1cb2004-02-24 02:00:03 +0000440
441 return 0;
442}
443
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500444static int nfs_umountall_reply(uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000445{
446 struct rpc_t rpc_pkt;
447
Robin Getz9e0a4d62009-07-23 03:01:03 -0400448 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000449
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000450 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000451
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100452 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
453 return -NFS_RPC_ERR;
454 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
455 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000456
wdenkbe9c1cb2004-02-24 02:00:03 +0000457 if (rpc_pkt.u.reply.rstatus ||
458 rpc_pkt.u.reply.verifier ||
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000459 rpc_pkt.u.reply.astatus)
wdenkbe9c1cb2004-02-24 02:00:03 +0000460 return -1;
wdenkbe9c1cb2004-02-24 02:00:03 +0000461
462 fs_mounted = 0;
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000463 memset(dirfh, 0, sizeof(dirfh));
wdenkbe9c1cb2004-02-24 02:00:03 +0000464
465 return 0;
466}
467
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500468static int nfs_lookup_reply(uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000469{
470 struct rpc_t rpc_pkt;
471
Robin Getz9e0a4d62009-07-23 03:01:03 -0400472 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000473
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000474 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000475
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100476 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
477 return -NFS_RPC_ERR;
478 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
479 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000480
wdenkbe9c1cb2004-02-24 02:00:03 +0000481 if (rpc_pkt.u.reply.rstatus ||
482 rpc_pkt.u.reply.verifier ||
483 rpc_pkt.u.reply.astatus ||
Guillaume GARDET7ba4c672016-06-06 15:11:45 +0200484 rpc_pkt.u.reply.data[0]) {
485 switch (ntohl(rpc_pkt.u.reply.astatus)) {
486 case 0: /* Not an error */
487 break;
488 case 2: /* Remote can't support NFS version */
489 printf("*** ERROR: NFS version not supported: Requested: V%d, accepted: min V%d - max V%d\n",
490 2,
491 ntohl(rpc_pkt.u.reply.data[0]),
492 ntohl(rpc_pkt.u.reply.data[1]));
493 break;
494 default: /* Unknown error on 'accept state' flag */
495 printf("*** ERROR: accept state error (%d)\n",
496 ntohl(rpc_pkt.u.reply.astatus));
497 break;
498 }
wdenkbe9c1cb2004-02-24 02:00:03 +0000499 return -1;
Guillaume GARDET7ba4c672016-06-06 15:11:45 +0200500 }
wdenkbe9c1cb2004-02-24 02:00:03 +0000501
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000502 memcpy(filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
wdenkbe9c1cb2004-02-24 02:00:03 +0000503
504 return 0;
505}
506
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500507static int nfs_readlink_reply(uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000508{
509 struct rpc_t rpc_pkt;
510 int rlen;
511
Robin Getz9e0a4d62009-07-23 03:01:03 -0400512 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000513
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000514 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000515
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100516 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
517 return -NFS_RPC_ERR;
518 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
519 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000520
wdenkbe9c1cb2004-02-24 02:00:03 +0000521 if (rpc_pkt.u.reply.rstatus ||
522 rpc_pkt.u.reply.verifier ||
523 rpc_pkt.u.reply.astatus ||
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000524 rpc_pkt.u.reply.data[0])
wdenkbe9c1cb2004-02-24 02:00:03 +0000525 return -1;
wdenkbe9c1cb2004-02-24 02:00:03 +0000526
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000527 rlen = ntohl(rpc_pkt.u.reply.data[1]); /* new path length */
wdenkbe9c1cb2004-02-24 02:00:03 +0000528
529 if (*((char *)&(rpc_pkt.u.reply.data[2])) != '/') {
530 int pathlen;
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000531 strcat(nfs_path, "/");
wdenkbe9c1cb2004-02-24 02:00:03 +0000532 pathlen = strlen(nfs_path);
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000533 memcpy(nfs_path + pathlen, (uchar *)&(rpc_pkt.u.reply.data[2]),
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500534 rlen);
Ed Swarthout867d8702009-11-19 02:47:28 -0600535 nfs_path[pathlen + rlen] = 0;
wdenkbe9c1cb2004-02-24 02:00:03 +0000536 } else {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000537 memcpy(nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen);
wdenkbe9c1cb2004-02-24 02:00:03 +0000538 nfs_path[rlen] = 0;
539 }
540 return 0;
541}
542
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500543static int nfs_read_reply(uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000544{
545 struct rpc_t rpc_pkt;
546 int rlen;
547
Robin Getz9e0a4d62009-07-23 03:01:03 -0400548 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000549
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000550 memcpy((uchar *)&rpc_pkt, pkt, sizeof(rpc_pkt.u.reply));
wdenkbe9c1cb2004-02-24 02:00:03 +0000551
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100552 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
553 return -NFS_RPC_ERR;
554 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
555 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000556
wdenkbe9c1cb2004-02-24 02:00:03 +0000557 if (rpc_pkt.u.reply.rstatus ||
558 rpc_pkt.u.reply.verifier ||
559 rpc_pkt.u.reply.astatus ||
560 rpc_pkt.u.reply.data[0]) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000561 if (rpc_pkt.u.reply.rstatus)
wdenkbe9c1cb2004-02-24 02:00:03 +0000562 return -9999;
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000563 if (rpc_pkt.u.reply.astatus)
wdenkbe9c1cb2004-02-24 02:00:03 +0000564 return -9999;
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000565 return -ntohl(rpc_pkt.u.reply.data[0]);
wdenkbe9c1cb2004-02-24 02:00:03 +0000566 }
567
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000568 if ((nfs_offset != 0) && !((nfs_offset) %
569 (NFS_READ_SIZE / 2 * 10 * HASHES_PER_LINE)))
570 puts("\n\t ");
571 if (!(nfs_offset % ((NFS_READ_SIZE / 2) * 10)))
572 putc('#');
wdenkbe9c1cb2004-02-24 02:00:03 +0000573
574 rlen = ntohl(rpc_pkt.u.reply.data[18]);
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000575 if (store_block((uchar *)pkt + sizeof(rpc_pkt.u.reply),
576 nfs_offset, rlen))
Wolfgang Denkb077b502005-08-06 01:11:12 +0200577 return -9999;
wdenkbe9c1cb2004-02-24 02:00:03 +0000578
579 return rlen;
580}
581
582/**************************************************************************
583Interfaces of U-BOOT
584**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500585static void nfs_timeout_handler(void)
wdenkd9ee50d2004-09-28 21:51:42 +0000586{
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500587 if (++nfs_timeout_count > NFS_RETRY_COUNT) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000588 puts("\nRetry count exceeded; starting again\n");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500589 net_start_again();
Evan Samanasb956fba2009-11-09 20:08:36 -0600590 } else {
591 puts("T ");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500592 net_set_timeout_handler(nfs_timeout +
593 NFS_TIMEOUT * nfs_timeout_count,
594 nfs_timeout_handler);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500595 nfs_send();
Hiroshi Ito3702cd72008-01-31 18:35:04 +0900596 }
wdenkd9ee50d2004-09-28 21:51:42 +0000597}
598
Joe Hershberger5874dec2015-04-08 01:41:01 -0500599static void nfs_handler(uchar *pkt, unsigned dest, struct in_addr sip,
600 unsigned src, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000601{
602 int rlen;
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100603 int reply;
wdenkbe9c1cb2004-02-24 02:00:03 +0000604
Robin Getz9e0a4d62009-07-23 03:01:03 -0400605 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000606
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500607 if (dest != nfs_our_port)
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000608 return;
wdenkbe9c1cb2004-02-24 02:00:03 +0000609
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500610 switch (nfs_state) {
wdenkbe9c1cb2004-02-24 02:00:03 +0000611 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100612 if (rpc_lookup_reply(PROG_MOUNT, pkt, len) == -NFS_RPC_DROP)
613 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500614 nfs_state = STATE_PRCLOOKUP_PROG_NFS_REQ;
615 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000616 break;
617
618 case STATE_PRCLOOKUP_PROG_NFS_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100619 if (rpc_lookup_reply(PROG_NFS, pkt, len) == -NFS_RPC_DROP)
620 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500621 nfs_state = STATE_MOUNT_REQ;
622 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000623 break;
624
625 case STATE_MOUNT_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100626 reply = nfs_mount_reply(pkt, len);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500627 if (reply == -NFS_RPC_DROP) {
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100628 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500629 } else if (reply == -NFS_RPC_ERR) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000630 puts("*** ERROR: Cannot mount\n");
wdenkbe9c1cb2004-02-24 02:00:03 +0000631 /* just to be sure... */
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500632 nfs_state = STATE_UMOUNT_REQ;
633 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000634 } else {
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500635 nfs_state = STATE_LOOKUP_REQ;
636 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000637 }
638 break;
639
640 case STATE_UMOUNT_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100641 reply = nfs_umountall_reply(pkt, len);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500642 if (reply == -NFS_RPC_DROP) {
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100643 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500644 } else if (reply == -NFS_RPC_ERR) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000645 puts("*** ERROR: Cannot umount\n");
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000646 net_set_state(NETLOOP_FAIL);
wdenkbe9c1cb2004-02-24 02:00:03 +0000647 } else {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000648 puts("\ndone\n");
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000649 net_set_state(nfs_download_state);
wdenkbe9c1cb2004-02-24 02:00:03 +0000650 }
651 break;
652
653 case STATE_LOOKUP_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100654 reply = nfs_lookup_reply(pkt, len);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500655 if (reply == -NFS_RPC_DROP) {
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100656 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500657 } else if (reply == -NFS_RPC_ERR) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000658 puts("*** ERROR: File lookup fail\n");
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500659 nfs_state = STATE_UMOUNT_REQ;
660 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000661 } else {
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500662 nfs_state = STATE_READ_REQ;
wdenkbe9c1cb2004-02-24 02:00:03 +0000663 nfs_offset = 0;
664 nfs_len = NFS_READ_SIZE;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500665 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000666 }
667 break;
668
669 case STATE_READLINK_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100670 reply = nfs_readlink_reply(pkt, len);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500671 if (reply == -NFS_RPC_DROP) {
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100672 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500673 } else if (reply == -NFS_RPC_ERR) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000674 puts("*** ERROR: Symlink fail\n");
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500675 nfs_state = STATE_UMOUNT_REQ;
676 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000677 } else {
Robin Getz9e0a4d62009-07-23 03:01:03 -0400678 debug("Symlink --> %s\n", nfs_path);
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000679 nfs_filename = basename(nfs_path);
680 nfs_path = dirname(nfs_path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000681
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500682 nfs_state = STATE_MOUNT_REQ;
683 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000684 }
685 break;
686
687 case STATE_READ_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000688 rlen = nfs_read_reply(pkt, len);
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500689 net_set_timeout_handler(nfs_timeout, nfs_timeout_handler);
wdenkbe9c1cb2004-02-24 02:00:03 +0000690 if (rlen > 0) {
691 nfs_offset += rlen;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500692 nfs_send();
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000693 } else if ((rlen == -NFSERR_ISDIR) || (rlen == -NFSERR_INVAL)) {
wdenkbe9c1cb2004-02-24 02:00:03 +0000694 /* symbolic link */
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500695 nfs_state = STATE_READLINK_REQ;
696 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000697 } else {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000698 if (!rlen)
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000699 nfs_download_state = NETLOOP_SUCCESS;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500700 nfs_state = STATE_UMOUNT_REQ;
701 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000702 }
703 break;
704 }
705}
706
wdenkbe9c1cb2004-02-24 02:00:03 +0000707
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500708void nfs_start(void)
wdenkbe9c1cb2004-02-24 02:00:03 +0000709{
Robin Getz9e0a4d62009-07-23 03:01:03 -0400710 debug("%s\n", __func__);
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000711 nfs_download_state = NETLOOP_FAIL;
wdenkbe9c1cb2004-02-24 02:00:03 +0000712
Joe Hershberger5874dec2015-04-08 01:41:01 -0500713 nfs_server_ip = net_server_ip;
wdenkbe9c1cb2004-02-24 02:00:03 +0000714 nfs_path = (char *)nfs_path_buff;
715
716 if (nfs_path == NULL) {
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000717 net_set_state(NETLOOP_FAIL);
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000718 puts("*** ERROR: Fail allocate memory\n");
wdenkbe9c1cb2004-02-24 02:00:03 +0000719 return;
720 }
721
Joe Hershberger290c8992015-04-08 01:41:02 -0500722 if (net_boot_file_name[0] == '\0') {
Matthias Weisser6cc63262011-12-03 03:29:44 +0000723 sprintf(default_filename, "/nfsroot/%02X%02X%02X%02X.img",
Joe Hershberger5874dec2015-04-08 01:41:01 -0500724 net_ip.s_addr & 0xFF,
725 (net_ip.s_addr >> 8) & 0xFF,
726 (net_ip.s_addr >> 16) & 0xFF,
727 (net_ip.s_addr >> 24) & 0xFF);
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000728 strcpy(nfs_path, default_filename);
wdenkbe9c1cb2004-02-24 02:00:03 +0000729
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000730 printf("*** Warning: no boot file name; using '%s'\n",
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500731 nfs_path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000732 } else {
Joe Hershberger290c8992015-04-08 01:41:02 -0500733 char *p = net_boot_file_name;
wdenkbe9c1cb2004-02-24 02:00:03 +0000734
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000735 p = strchr(p, ':');
wdenkbe9c1cb2004-02-24 02:00:03 +0000736
737 if (p != NULL) {
Joe Hershberger290c8992015-04-08 01:41:02 -0500738 nfs_server_ip = string_to_ip(net_boot_file_name);
wdenkbe9c1cb2004-02-24 02:00:03 +0000739 ++p;
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000740 strcpy(nfs_path, p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000741 } else {
Joe Hershberger290c8992015-04-08 01:41:02 -0500742 strcpy(nfs_path, net_boot_file_name);
wdenkbe9c1cb2004-02-24 02:00:03 +0000743 }
744 }
745
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000746 nfs_filename = basename(nfs_path);
747 nfs_path = dirname(nfs_path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000748
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000749 printf("Using %s device\n", eth_get_name());
wdenkbe9c1cb2004-02-24 02:00:03 +0000750
Joe Hershberger5874dec2015-04-08 01:41:01 -0500751 printf("File transfer via NFS from server %pI4; our IP address is %pI4",
752 &nfs_server_ip, &net_ip);
wdenkbe9c1cb2004-02-24 02:00:03 +0000753
754 /* Check if we need to send across this subnet */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500755 if (net_gateway.s_addr && net_netmask.s_addr) {
756 struct in_addr our_net;
757 struct in_addr server_net;
wdenkbe9c1cb2004-02-24 02:00:03 +0000758
Joe Hershberger5874dec2015-04-08 01:41:01 -0500759 our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
760 server_net.s_addr = net_server_ip.s_addr & net_netmask.s_addr;
761 if (our_net.s_addr != server_net.s_addr)
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000762 printf("; sending through gateway %pI4",
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500763 &net_gateway);
wdenkbe9c1cb2004-02-24 02:00:03 +0000764 }
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000765 printf("\nFilename '%s/%s'.", nfs_path, nfs_filename);
wdenkbe9c1cb2004-02-24 02:00:03 +0000766
Joe Hershberger290c8992015-04-08 01:41:02 -0500767 if (net_boot_file_expected_size_in_blocks) {
768 printf(" Size is 0x%x Bytes = ",
769 net_boot_file_expected_size_in_blocks << 9);
770 print_size(net_boot_file_expected_size_in_blocks << 9, "");
wdenkbe9c1cb2004-02-24 02:00:03 +0000771 }
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000772 printf("\nLoad address: 0x%lx\n"
wdenk42c05472004-03-23 22:14:11 +0000773 "Loading: *\b", load_addr);
wdenkbe9c1cb2004-02-24 02:00:03 +0000774
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500775 net_set_timeout_handler(nfs_timeout, nfs_timeout_handler);
Joe Hershberger5874dec2015-04-08 01:41:01 -0500776 net_set_udp_handler(nfs_handler);
wdenkbe9c1cb2004-02-24 02:00:03 +0000777
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500778 nfs_timeout_count = 0;
779 nfs_state = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
wdenkbe9c1cb2004-02-24 02:00:03 +0000780
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500781 /*nfs_our_port = 4096 + (get_ticks() % 3072);*/
wdenkbe9c1cb2004-02-24 02:00:03 +0000782 /*FIX ME !!!*/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500783 nfs_our_port = 1000;
wdenkbe9c1cb2004-02-24 02:00:03 +0000784
785 /* zero out server ether in case the server ip has changed */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500786 memset(net_server_ethaddr, 0, 6);
wdenkbe9c1cb2004-02-24 02:00:03 +0000787
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500788 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000789}