blob: 78968d82e94b25d5accec9b655c6bb37918dc38b [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**************************************************************************/
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000135static long *rpc_add_credentials(long *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]);
244 p = (uint32_t *)rpc_add_credentials((long *)p);
245
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]);
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000271 p = (uint32_t *)rpc_add_credentials((long *)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]);
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000292 p = (uint32_t *)rpc_add_credentials((long *)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]);
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000315 p = (uint32_t *)rpc_add_credentials((long *)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]);
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000340 p = (uint32_t *)rpc_add_credentials((long *)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 ||
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000484 rpc_pkt.u.reply.data[0])
wdenkbe9c1cb2004-02-24 02:00:03 +0000485 return -1;
wdenkbe9c1cb2004-02-24 02:00:03 +0000486
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000487 memcpy(filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
wdenkbe9c1cb2004-02-24 02:00:03 +0000488
489 return 0;
490}
491
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500492static int nfs_readlink_reply(uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000493{
494 struct rpc_t rpc_pkt;
495 int rlen;
496
Robin Getz9e0a4d62009-07-23 03:01:03 -0400497 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000498
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000499 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000500
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100501 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
502 return -NFS_RPC_ERR;
503 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
504 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000505
wdenkbe9c1cb2004-02-24 02:00:03 +0000506 if (rpc_pkt.u.reply.rstatus ||
507 rpc_pkt.u.reply.verifier ||
508 rpc_pkt.u.reply.astatus ||
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000509 rpc_pkt.u.reply.data[0])
wdenkbe9c1cb2004-02-24 02:00:03 +0000510 return -1;
wdenkbe9c1cb2004-02-24 02:00:03 +0000511
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000512 rlen = ntohl(rpc_pkt.u.reply.data[1]); /* new path length */
wdenkbe9c1cb2004-02-24 02:00:03 +0000513
514 if (*((char *)&(rpc_pkt.u.reply.data[2])) != '/') {
515 int pathlen;
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000516 strcat(nfs_path, "/");
wdenkbe9c1cb2004-02-24 02:00:03 +0000517 pathlen = strlen(nfs_path);
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000518 memcpy(nfs_path + pathlen, (uchar *)&(rpc_pkt.u.reply.data[2]),
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500519 rlen);
Ed Swarthout867d8702009-11-19 02:47:28 -0600520 nfs_path[pathlen + rlen] = 0;
wdenkbe9c1cb2004-02-24 02:00:03 +0000521 } else {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000522 memcpy(nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen);
wdenkbe9c1cb2004-02-24 02:00:03 +0000523 nfs_path[rlen] = 0;
524 }
525 return 0;
526}
527
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500528static int nfs_read_reply(uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000529{
530 struct rpc_t rpc_pkt;
531 int rlen;
532
Robin Getz9e0a4d62009-07-23 03:01:03 -0400533 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000534
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000535 memcpy((uchar *)&rpc_pkt, pkt, sizeof(rpc_pkt.u.reply));
wdenkbe9c1cb2004-02-24 02:00:03 +0000536
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100537 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
538 return -NFS_RPC_ERR;
539 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
540 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000541
wdenkbe9c1cb2004-02-24 02:00:03 +0000542 if (rpc_pkt.u.reply.rstatus ||
543 rpc_pkt.u.reply.verifier ||
544 rpc_pkt.u.reply.astatus ||
545 rpc_pkt.u.reply.data[0]) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000546 if (rpc_pkt.u.reply.rstatus)
wdenkbe9c1cb2004-02-24 02:00:03 +0000547 return -9999;
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000548 if (rpc_pkt.u.reply.astatus)
wdenkbe9c1cb2004-02-24 02:00:03 +0000549 return -9999;
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000550 return -ntohl(rpc_pkt.u.reply.data[0]);
wdenkbe9c1cb2004-02-24 02:00:03 +0000551 }
552
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000553 if ((nfs_offset != 0) && !((nfs_offset) %
554 (NFS_READ_SIZE / 2 * 10 * HASHES_PER_LINE)))
555 puts("\n\t ");
556 if (!(nfs_offset % ((NFS_READ_SIZE / 2) * 10)))
557 putc('#');
wdenkbe9c1cb2004-02-24 02:00:03 +0000558
559 rlen = ntohl(rpc_pkt.u.reply.data[18]);
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000560 if (store_block((uchar *)pkt + sizeof(rpc_pkt.u.reply),
561 nfs_offset, rlen))
Wolfgang Denkb077b502005-08-06 01:11:12 +0200562 return -9999;
wdenkbe9c1cb2004-02-24 02:00:03 +0000563
564 return rlen;
565}
566
567/**************************************************************************
568Interfaces of U-BOOT
569**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500570static void nfs_timeout_handler(void)
wdenkd9ee50d2004-09-28 21:51:42 +0000571{
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500572 if (++nfs_timeout_count > NFS_RETRY_COUNT) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000573 puts("\nRetry count exceeded; starting again\n");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500574 net_start_again();
Evan Samanasb956fba2009-11-09 20:08:36 -0600575 } else {
576 puts("T ");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500577 net_set_timeout_handler(nfs_timeout +
578 NFS_TIMEOUT * nfs_timeout_count,
579 nfs_timeout_handler);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500580 nfs_send();
Hiroshi Ito3702cd72008-01-31 18:35:04 +0900581 }
wdenkd9ee50d2004-09-28 21:51:42 +0000582}
583
Joe Hershberger5874dec2015-04-08 01:41:01 -0500584static void nfs_handler(uchar *pkt, unsigned dest, struct in_addr sip,
585 unsigned src, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000586{
587 int rlen;
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100588 int reply;
wdenkbe9c1cb2004-02-24 02:00:03 +0000589
Robin Getz9e0a4d62009-07-23 03:01:03 -0400590 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000591
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500592 if (dest != nfs_our_port)
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000593 return;
wdenkbe9c1cb2004-02-24 02:00:03 +0000594
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500595 switch (nfs_state) {
wdenkbe9c1cb2004-02-24 02:00:03 +0000596 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100597 if (rpc_lookup_reply(PROG_MOUNT, pkt, len) == -NFS_RPC_DROP)
598 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500599 nfs_state = STATE_PRCLOOKUP_PROG_NFS_REQ;
600 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000601 break;
602
603 case STATE_PRCLOOKUP_PROG_NFS_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100604 if (rpc_lookup_reply(PROG_NFS, pkt, len) == -NFS_RPC_DROP)
605 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500606 nfs_state = STATE_MOUNT_REQ;
607 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000608 break;
609
610 case STATE_MOUNT_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100611 reply = nfs_mount_reply(pkt, len);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500612 if (reply == -NFS_RPC_DROP) {
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100613 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500614 } else if (reply == -NFS_RPC_ERR) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000615 puts("*** ERROR: Cannot mount\n");
wdenkbe9c1cb2004-02-24 02:00:03 +0000616 /* just to be sure... */
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500617 nfs_state = STATE_UMOUNT_REQ;
618 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000619 } else {
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500620 nfs_state = STATE_LOOKUP_REQ;
621 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000622 }
623 break;
624
625 case STATE_UMOUNT_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100626 reply = nfs_umountall_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 umount\n");
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000631 net_set_state(NETLOOP_FAIL);
wdenkbe9c1cb2004-02-24 02:00:03 +0000632 } else {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000633 puts("\ndone\n");
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000634 net_set_state(nfs_download_state);
wdenkbe9c1cb2004-02-24 02:00:03 +0000635 }
636 break;
637
638 case STATE_LOOKUP_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100639 reply = nfs_lookup_reply(pkt, len);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500640 if (reply == -NFS_RPC_DROP) {
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100641 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500642 } else if (reply == -NFS_RPC_ERR) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000643 puts("*** ERROR: File lookup fail\n");
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500644 nfs_state = STATE_UMOUNT_REQ;
645 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000646 } else {
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500647 nfs_state = STATE_READ_REQ;
wdenkbe9c1cb2004-02-24 02:00:03 +0000648 nfs_offset = 0;
649 nfs_len = NFS_READ_SIZE;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500650 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000651 }
652 break;
653
654 case STATE_READLINK_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100655 reply = nfs_readlink_reply(pkt, len);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500656 if (reply == -NFS_RPC_DROP) {
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100657 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500658 } else if (reply == -NFS_RPC_ERR) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000659 puts("*** ERROR: Symlink fail\n");
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500660 nfs_state = STATE_UMOUNT_REQ;
661 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000662 } else {
Robin Getz9e0a4d62009-07-23 03:01:03 -0400663 debug("Symlink --> %s\n", nfs_path);
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000664 nfs_filename = basename(nfs_path);
665 nfs_path = dirname(nfs_path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000666
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500667 nfs_state = STATE_MOUNT_REQ;
668 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000669 }
670 break;
671
672 case STATE_READ_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000673 rlen = nfs_read_reply(pkt, len);
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500674 net_set_timeout_handler(nfs_timeout, nfs_timeout_handler);
wdenkbe9c1cb2004-02-24 02:00:03 +0000675 if (rlen > 0) {
676 nfs_offset += rlen;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500677 nfs_send();
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000678 } else if ((rlen == -NFSERR_ISDIR) || (rlen == -NFSERR_INVAL)) {
wdenkbe9c1cb2004-02-24 02:00:03 +0000679 /* symbolic link */
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500680 nfs_state = STATE_READLINK_REQ;
681 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000682 } else {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000683 if (!rlen)
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000684 nfs_download_state = NETLOOP_SUCCESS;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500685 nfs_state = STATE_UMOUNT_REQ;
686 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000687 }
688 break;
689 }
690}
691
wdenkbe9c1cb2004-02-24 02:00:03 +0000692
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500693void nfs_start(void)
wdenkbe9c1cb2004-02-24 02:00:03 +0000694{
Robin Getz9e0a4d62009-07-23 03:01:03 -0400695 debug("%s\n", __func__);
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000696 nfs_download_state = NETLOOP_FAIL;
wdenkbe9c1cb2004-02-24 02:00:03 +0000697
Joe Hershberger5874dec2015-04-08 01:41:01 -0500698 nfs_server_ip = net_server_ip;
wdenkbe9c1cb2004-02-24 02:00:03 +0000699 nfs_path = (char *)nfs_path_buff;
700
701 if (nfs_path == NULL) {
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000702 net_set_state(NETLOOP_FAIL);
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000703 puts("*** ERROR: Fail allocate memory\n");
wdenkbe9c1cb2004-02-24 02:00:03 +0000704 return;
705 }
706
Joe Hershberger290c8992015-04-08 01:41:02 -0500707 if (net_boot_file_name[0] == '\0') {
Matthias Weisser6cc63262011-12-03 03:29:44 +0000708 sprintf(default_filename, "/nfsroot/%02X%02X%02X%02X.img",
Joe Hershberger5874dec2015-04-08 01:41:01 -0500709 net_ip.s_addr & 0xFF,
710 (net_ip.s_addr >> 8) & 0xFF,
711 (net_ip.s_addr >> 16) & 0xFF,
712 (net_ip.s_addr >> 24) & 0xFF);
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000713 strcpy(nfs_path, default_filename);
wdenkbe9c1cb2004-02-24 02:00:03 +0000714
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000715 printf("*** Warning: no boot file name; using '%s'\n",
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500716 nfs_path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000717 } else {
Joe Hershberger290c8992015-04-08 01:41:02 -0500718 char *p = net_boot_file_name;
wdenkbe9c1cb2004-02-24 02:00:03 +0000719
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000720 p = strchr(p, ':');
wdenkbe9c1cb2004-02-24 02:00:03 +0000721
722 if (p != NULL) {
Joe Hershberger290c8992015-04-08 01:41:02 -0500723 nfs_server_ip = string_to_ip(net_boot_file_name);
wdenkbe9c1cb2004-02-24 02:00:03 +0000724 ++p;
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000725 strcpy(nfs_path, p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000726 } else {
Joe Hershberger290c8992015-04-08 01:41:02 -0500727 strcpy(nfs_path, net_boot_file_name);
wdenkbe9c1cb2004-02-24 02:00:03 +0000728 }
729 }
730
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000731 nfs_filename = basename(nfs_path);
732 nfs_path = dirname(nfs_path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000733
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000734 printf("Using %s device\n", eth_get_name());
wdenkbe9c1cb2004-02-24 02:00:03 +0000735
Joe Hershberger5874dec2015-04-08 01:41:01 -0500736 printf("File transfer via NFS from server %pI4; our IP address is %pI4",
737 &nfs_server_ip, &net_ip);
wdenkbe9c1cb2004-02-24 02:00:03 +0000738
739 /* Check if we need to send across this subnet */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500740 if (net_gateway.s_addr && net_netmask.s_addr) {
741 struct in_addr our_net;
742 struct in_addr server_net;
wdenkbe9c1cb2004-02-24 02:00:03 +0000743
Joe Hershberger5874dec2015-04-08 01:41:01 -0500744 our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
745 server_net.s_addr = net_server_ip.s_addr & net_netmask.s_addr;
746 if (our_net.s_addr != server_net.s_addr)
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000747 printf("; sending through gateway %pI4",
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500748 &net_gateway);
wdenkbe9c1cb2004-02-24 02:00:03 +0000749 }
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000750 printf("\nFilename '%s/%s'.", nfs_path, nfs_filename);
wdenkbe9c1cb2004-02-24 02:00:03 +0000751
Joe Hershberger290c8992015-04-08 01:41:02 -0500752 if (net_boot_file_expected_size_in_blocks) {
753 printf(" Size is 0x%x Bytes = ",
754 net_boot_file_expected_size_in_blocks << 9);
755 print_size(net_boot_file_expected_size_in_blocks << 9, "");
wdenkbe9c1cb2004-02-24 02:00:03 +0000756 }
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000757 printf("\nLoad address: 0x%lx\n"
wdenk42c05472004-03-23 22:14:11 +0000758 "Loading: *\b", load_addr);
wdenkbe9c1cb2004-02-24 02:00:03 +0000759
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500760 net_set_timeout_handler(nfs_timeout, nfs_timeout_handler);
Joe Hershberger5874dec2015-04-08 01:41:01 -0500761 net_set_udp_handler(nfs_handler);
wdenkbe9c1cb2004-02-24 02:00:03 +0000762
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500763 nfs_timeout_count = 0;
764 nfs_state = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
wdenkbe9c1cb2004-02-24 02:00:03 +0000765
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500766 /*nfs_our_port = 4096 + (get_ticks() % 3072);*/
wdenkbe9c1cb2004-02-24 02:00:03 +0000767 /*FIX ME !!!*/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500768 nfs_our_port = 1000;
wdenkbe9c1cb2004-02-24 02:00:03 +0000769
770 /* zero out server ether in case the server ip has changed */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500771 memset(net_server_ethaddr, 0, 6);
wdenkbe9c1cb2004-02-24 02:00:03 +0000772
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500773 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000774}