blob: d7aeef916207d037f579e09b3c5576247c4f51a0 [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
wdenkbe9c1cb2004-02-24 02:00:03 +000068static char *nfs_filename;
69static char *nfs_path;
70static char nfs_path_buff[2048];
71
Joe Hershberger40d7ca92015-04-08 01:41:10 -050072static inline int store_block(uchar *src, unsigned offset, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +000073{
wdenk77ef4c62004-02-24 22:33:21 +000074 ulong newsize = offset + len;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020075#ifdef CONFIG_SYS_DIRECT_FLASH_NFS
wdenkbe9c1cb2004-02-24 02:00:03 +000076 int i, rc = 0;
77
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +000078 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenkbe9c1cb2004-02-24 02:00:03 +000079 /* start address in flash? */
80 if (load_addr + offset >= flash_info[i].start[0]) {
81 rc = 1;
82 break;
83 }
84 }
85
86 if (rc) { /* Flash is destination for this packet */
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +000087 rc = flash_write((uchar *)src, (ulong)(load_addr+offset), len);
wdenkbe9c1cb2004-02-24 02:00:03 +000088 if (rc) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +000089 flash_perror(rc);
Wolfgang Denkb077b502005-08-06 01:11:12 +020090 return -1;
wdenkbe9c1cb2004-02-24 02:00:03 +000091 }
92 } else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020093#endif /* CONFIG_SYS_DIRECT_FLASH_NFS */
wdenkbe9c1cb2004-02-24 02:00:03 +000094 {
Joe Hershbergered191852015-03-22 17:09:08 -050095 void *ptr = map_sysmem(load_addr + offset, len);
96
97 memcpy(ptr, src, len);
98 unmap_sysmem(ptr);
wdenkbe9c1cb2004-02-24 02:00:03 +000099 }
wdenk77ef4c62004-02-24 22:33:21 +0000100
Joe Hershberger290c8992015-04-08 01:41:02 -0500101 if (net_boot_file_size < (offset + len))
102 net_boot_file_size = newsize;
Wolfgang Denkb077b502005-08-06 01:11:12 +0200103 return 0;
wdenkbe9c1cb2004-02-24 02:00:03 +0000104}
105
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500106static char *basename(char *path)
wdenkbe9c1cb2004-02-24 02:00:03 +0000107{
108 char *fname;
109
110 fname = path + strlen(path) - 1;
111 while (fname >= path) {
112 if (*fname == '/') {
113 fname++;
114 break;
115 }
116 fname--;
117 }
118 return fname;
119}
120
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500121static char *dirname(char *path)
wdenkbe9c1cb2004-02-24 02:00:03 +0000122{
123 char *fname;
124
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000125 fname = basename(path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000126 --fname;
127 *fname = '\0';
128 return path;
129}
130
131/**************************************************************************
wdenkbe9c1cb2004-02-24 02:00:03 +0000132RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
133**************************************************************************/
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200134static uint32_t *rpc_add_credentials(uint32_t *p)
wdenkbe9c1cb2004-02-24 02:00:03 +0000135{
136 int hl;
137 int hostnamelen;
138 char hostname[256];
139
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000140 strcpy(hostname, "");
141 hostnamelen = strlen(hostname);
wdenkbe9c1cb2004-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 Hershberger6ecfc6b2012-05-15 08:59:09 +0000159 if (hostnamelen & 3)
wdenkbe9c1cb2004-02-24 02:00:03 +0000160 *(p + hostnamelen / 4) = 0; /* add zero padding */
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000161 memcpy(p, hostname, hostnamelen);
wdenkbe9c1cb2004-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**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500177static void rpc_req(int rpc_prog, int rpc_proc, uint32_t *data, int datalen)
wdenkbe9c1cb2004-02-24 02:00:03 +0000178{
179 struct rpc_t pkt;
180 unsigned long id;
181 uint32_t *p;
182 int pktlen;
183 int sport;
184
wdenkacd9b102004-03-14 00:59:59 +0000185 id = ++rpc_id;
wdenkbe9c1cb2004-02-24 02:00:03 +0000186 pkt.u.call.id = htonl(id);
187 pkt.u.call.type = htonl(MSG_CALL);
188 pkt.u.call.rpcvers = htonl(2); /* use RPC version 2 */
189 pkt.u.call.prog = htonl(rpc_prog);
190 pkt.u.call.vers = htonl(2); /* portmapper is version 2 */
191 pkt.u.call.proc = htonl(rpc_proc);
192 p = (uint32_t *)&(pkt.u.call.data);
193
194 if (datalen)
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000195 memcpy((char *)p, (char *)data, datalen*sizeof(uint32_t));
wdenkbe9c1cb2004-02-24 02:00:03 +0000196
197 pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt;
198
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500199 memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE,
200 (char *)&pkt, pktlen);
wdenkbe9c1cb2004-02-24 02:00:03 +0000201
202 if (rpc_prog == PROG_PORTMAP)
203 sport = SUNRPC_PORT;
204 else if (rpc_prog == PROG_MOUNT)
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500205 sport = nfs_server_mount_port;
wdenkbe9c1cb2004-02-24 02:00:03 +0000206 else
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500207 sport = nfs_server_port;
wdenkbe9c1cb2004-02-24 02:00:03 +0000208
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500209 net_send_udp_packet(net_server_ethaddr, nfs_server_ip, sport,
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500210 nfs_our_port, pktlen);
wdenkbe9c1cb2004-02-24 02:00:03 +0000211}
212
213/**************************************************************************
214RPC_LOOKUP - Lookup RPC Port numbers
215**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500216static void rpc_lookup_req(int prog, int ver)
wdenkbe9c1cb2004-02-24 02:00:03 +0000217{
218 uint32_t data[16];
219
220 data[0] = 0; data[1] = 0; /* auth credential */
221 data[2] = 0; data[3] = 0; /* auth verifier */
222 data[4] = htonl(prog);
223 data[5] = htonl(ver);
224 data[6] = htonl(17); /* IP_UDP */
225 data[7] = 0;
226
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000227 rpc_req(PROG_PORTMAP, PORTMAP_GETPORT, data, 8);
wdenkbe9c1cb2004-02-24 02:00:03 +0000228}
229
230/**************************************************************************
231NFS_MOUNT - Mount an NFS Filesystem
232**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500233static void nfs_mount_req(char *path)
wdenkbe9c1cb2004-02-24 02:00:03 +0000234{
235 uint32_t data[1024];
236 uint32_t *p;
237 int len;
238 int pathlen;
239
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000240 pathlen = strlen(path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000241
242 p = &(data[0]);
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200243 p = rpc_add_credentials(p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000244
245 *p++ = htonl(pathlen);
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000246 if (pathlen & 3)
247 *(p + pathlen / 4) = 0;
248 memcpy(p, path, pathlen);
wdenkbe9c1cb2004-02-24 02:00:03 +0000249 p += (pathlen + 3) / 4;
250
251 len = (uint32_t *)p - (uint32_t *)&(data[0]);
252
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000253 rpc_req(PROG_MOUNT, MOUNT_ADDENTRY, data, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000254}
255
256/**************************************************************************
257NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
258**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500259static void nfs_umountall_req(void)
wdenkbe9c1cb2004-02-24 02:00:03 +0000260{
261 uint32_t data[1024];
262 uint32_t *p;
263 int len;
264
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500265 if ((nfs_server_mount_port == -1) || (!fs_mounted))
wdenkbe9c1cb2004-02-24 02:00:03 +0000266 /* Nothing mounted, nothing to umount */
267 return;
wdenkbe9c1cb2004-02-24 02:00:03 +0000268
269 p = &(data[0]);
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200270 p = rpc_add_credentials(p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000271
272 len = (uint32_t *)p - (uint32_t *)&(data[0]);
273
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000274 rpc_req(PROG_MOUNT, MOUNT_UMOUNTALL, data, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000275}
276
277/***************************************************************************
278 * NFS_READLINK (AH 2003-07-14)
279 * This procedure is called when read of the first block fails -
280 * this probably happens when it's a directory or a symlink
281 * In case of successful readlink(), the dirname is manipulated,
282 * so that inside the nfs() function a recursion can be done.
283 **************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500284static void nfs_readlink_req(void)
wdenkbe9c1cb2004-02-24 02:00:03 +0000285{
286 uint32_t data[1024];
287 uint32_t *p;
288 int len;
289
290 p = &(data[0]);
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200291 p = rpc_add_credentials(p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000292
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000293 memcpy(p, filefh, NFS_FHSIZE);
wdenkbe9c1cb2004-02-24 02:00:03 +0000294 p += (NFS_FHSIZE / 4);
295
296 len = (uint32_t *)p - (uint32_t *)&(data[0]);
297
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000298 rpc_req(PROG_NFS, NFS_READLINK, data, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000299}
300
301/**************************************************************************
302NFS_LOOKUP - Lookup Pathname
303**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500304static void nfs_lookup_req(char *fname)
wdenkbe9c1cb2004-02-24 02:00:03 +0000305{
306 uint32_t data[1024];
307 uint32_t *p;
308 int len;
309 int fnamelen;
310
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000311 fnamelen = strlen(fname);
wdenkbe9c1cb2004-02-24 02:00:03 +0000312
313 p = &(data[0]);
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200314 p = rpc_add_credentials(p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000315
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000316 memcpy(p, dirfh, NFS_FHSIZE);
wdenkbe9c1cb2004-02-24 02:00:03 +0000317 p += (NFS_FHSIZE / 4);
318 *p++ = htonl(fnamelen);
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000319 if (fnamelen & 3)
320 *(p + fnamelen / 4) = 0;
321 memcpy(p, fname, fnamelen);
wdenkbe9c1cb2004-02-24 02:00:03 +0000322 p += (fnamelen + 3) / 4;
323
324 len = (uint32_t *)p - (uint32_t *)&(data[0]);
325
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000326 rpc_req(PROG_NFS, NFS_LOOKUP, data, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000327}
328
329/**************************************************************************
330NFS_READ - Read File on NFS Server
331**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500332static void nfs_read_req(int offset, int readlen)
wdenkbe9c1cb2004-02-24 02:00:03 +0000333{
334 uint32_t data[1024];
335 uint32_t *p;
336 int len;
337
338 p = &(data[0]);
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200339 p = rpc_add_credentials(p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000340
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000341 memcpy(p, filefh, NFS_FHSIZE);
wdenkbe9c1cb2004-02-24 02:00:03 +0000342 p += (NFS_FHSIZE / 4);
343 *p++ = htonl(offset);
344 *p++ = htonl(readlen);
345 *p++ = 0;
346
347 len = (uint32_t *)p - (uint32_t *)&(data[0]);
348
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000349 rpc_req(PROG_NFS, NFS_READ, data, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000350}
351
352/**************************************************************************
353RPC request dispatcher
354**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500355static void nfs_send(void)
wdenkbe9c1cb2004-02-24 02:00:03 +0000356{
Robin Getz9e0a4d62009-07-23 03:01:03 -0400357 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000358
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500359 switch (nfs_state) {
wdenkbe9c1cb2004-02-24 02:00:03 +0000360 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000361 rpc_lookup_req(PROG_MOUNT, 1);
wdenkbe9c1cb2004-02-24 02:00:03 +0000362 break;
363 case STATE_PRCLOOKUP_PROG_NFS_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000364 rpc_lookup_req(PROG_NFS, 2);
wdenkbe9c1cb2004-02-24 02:00:03 +0000365 break;
366 case STATE_MOUNT_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000367 nfs_mount_req(nfs_path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000368 break;
369 case STATE_UMOUNT_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000370 nfs_umountall_req();
wdenkbe9c1cb2004-02-24 02:00:03 +0000371 break;
372 case STATE_LOOKUP_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000373 nfs_lookup_req(nfs_filename);
wdenkbe9c1cb2004-02-24 02:00:03 +0000374 break;
375 case STATE_READ_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000376 nfs_read_req(nfs_offset, nfs_len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000377 break;
378 case STATE_READLINK_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000379 nfs_readlink_req();
wdenkbe9c1cb2004-02-24 02:00:03 +0000380 break;
381 }
382}
383
384/**************************************************************************
385Handlers for the reply from server
386**************************************************************************/
387
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500388static int rpc_lookup_reply(int prog, uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000389{
390 struct rpc_t rpc_pkt;
391
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000392 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000393
Robin Getz9e0a4d62009-07-23 03:01:03 -0400394 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000395
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100396 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
397 return -NFS_RPC_ERR;
398 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
399 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000400
wdenkbe9c1cb2004-02-24 02:00:03 +0000401 if (rpc_pkt.u.reply.rstatus ||
402 rpc_pkt.u.reply.verifier ||
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000403 rpc_pkt.u.reply.astatus)
wdenkacd9b102004-03-14 00:59:59 +0000404 return -1;
wdenkbe9c1cb2004-02-24 02:00:03 +0000405
406 switch (prog) {
407 case PROG_MOUNT:
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500408 nfs_server_mount_port = ntohl(rpc_pkt.u.reply.data[0]);
wdenkbe9c1cb2004-02-24 02:00:03 +0000409 break;
410 case PROG_NFS:
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500411 nfs_server_port = ntohl(rpc_pkt.u.reply.data[0]);
wdenkbe9c1cb2004-02-24 02:00:03 +0000412 break;
413 }
414
415 return 0;
416}
417
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500418static int nfs_mount_reply(uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000419{
420 struct rpc_t rpc_pkt;
421
Robin Getz9e0a4d62009-07-23 03:01:03 -0400422 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000423
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000424 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000425
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100426 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
427 return -NFS_RPC_ERR;
428 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
429 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000430
wdenkbe9c1cb2004-02-24 02:00:03 +0000431 if (rpc_pkt.u.reply.rstatus ||
432 rpc_pkt.u.reply.verifier ||
433 rpc_pkt.u.reply.astatus ||
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000434 rpc_pkt.u.reply.data[0])
wdenkbe9c1cb2004-02-24 02:00:03 +0000435 return -1;
wdenkbe9c1cb2004-02-24 02:00:03 +0000436
437 fs_mounted = 1;
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000438 memcpy(dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
wdenkbe9c1cb2004-02-24 02:00:03 +0000439
440 return 0;
441}
442
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500443static int nfs_umountall_reply(uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000444{
445 struct rpc_t rpc_pkt;
446
Robin Getz9e0a4d62009-07-23 03:01:03 -0400447 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000448
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000449 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000450
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100451 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
452 return -NFS_RPC_ERR;
453 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
454 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000455
wdenkbe9c1cb2004-02-24 02:00:03 +0000456 if (rpc_pkt.u.reply.rstatus ||
457 rpc_pkt.u.reply.verifier ||
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000458 rpc_pkt.u.reply.astatus)
wdenkbe9c1cb2004-02-24 02:00:03 +0000459 return -1;
wdenkbe9c1cb2004-02-24 02:00:03 +0000460
461 fs_mounted = 0;
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000462 memset(dirfh, 0, sizeof(dirfh));
wdenkbe9c1cb2004-02-24 02:00:03 +0000463
464 return 0;
465}
466
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500467static int nfs_lookup_reply(uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000468{
469 struct rpc_t rpc_pkt;
470
Robin Getz9e0a4d62009-07-23 03:01:03 -0400471 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000472
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000473 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000474
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100475 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
476 return -NFS_RPC_ERR;
477 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
478 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000479
wdenkbe9c1cb2004-02-24 02:00:03 +0000480 if (rpc_pkt.u.reply.rstatus ||
481 rpc_pkt.u.reply.verifier ||
482 rpc_pkt.u.reply.astatus ||
Guillaume GARDET7ba4c672016-06-06 15:11:45 +0200483 rpc_pkt.u.reply.data[0]) {
484 switch (ntohl(rpc_pkt.u.reply.astatus)) {
485 case 0: /* Not an error */
486 break;
487 case 2: /* Remote can't support NFS version */
488 printf("*** ERROR: NFS version not supported: Requested: V%d, accepted: min V%d - max V%d\n",
489 2,
490 ntohl(rpc_pkt.u.reply.data[0]),
491 ntohl(rpc_pkt.u.reply.data[1]));
492 break;
493 default: /* Unknown error on 'accept state' flag */
494 printf("*** ERROR: accept state error (%d)\n",
495 ntohl(rpc_pkt.u.reply.astatus));
496 break;
497 }
wdenkbe9c1cb2004-02-24 02:00:03 +0000498 return -1;
Guillaume GARDET7ba4c672016-06-06 15:11:45 +0200499 }
wdenkbe9c1cb2004-02-24 02:00:03 +0000500
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000501 memcpy(filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
wdenkbe9c1cb2004-02-24 02:00:03 +0000502
503 return 0;
504}
505
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500506static int nfs_readlink_reply(uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000507{
508 struct rpc_t rpc_pkt;
509 int rlen;
510
Robin Getz9e0a4d62009-07-23 03:01:03 -0400511 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000512
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000513 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000514
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100515 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
516 return -NFS_RPC_ERR;
517 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
518 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000519
wdenkbe9c1cb2004-02-24 02:00:03 +0000520 if (rpc_pkt.u.reply.rstatus ||
521 rpc_pkt.u.reply.verifier ||
522 rpc_pkt.u.reply.astatus ||
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000523 rpc_pkt.u.reply.data[0])
wdenkbe9c1cb2004-02-24 02:00:03 +0000524 return -1;
wdenkbe9c1cb2004-02-24 02:00:03 +0000525
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000526 rlen = ntohl(rpc_pkt.u.reply.data[1]); /* new path length */
wdenkbe9c1cb2004-02-24 02:00:03 +0000527
528 if (*((char *)&(rpc_pkt.u.reply.data[2])) != '/') {
529 int pathlen;
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000530 strcat(nfs_path, "/");
wdenkbe9c1cb2004-02-24 02:00:03 +0000531 pathlen = strlen(nfs_path);
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000532 memcpy(nfs_path + pathlen, (uchar *)&(rpc_pkt.u.reply.data[2]),
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500533 rlen);
Ed Swarthout867d8702009-11-19 02:47:28 -0600534 nfs_path[pathlen + rlen] = 0;
wdenkbe9c1cb2004-02-24 02:00:03 +0000535 } else {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000536 memcpy(nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen);
wdenkbe9c1cb2004-02-24 02:00:03 +0000537 nfs_path[rlen] = 0;
538 }
539 return 0;
540}
541
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500542static int nfs_read_reply(uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000543{
544 struct rpc_t rpc_pkt;
545 int rlen;
546
Robin Getz9e0a4d62009-07-23 03:01:03 -0400547 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000548
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000549 memcpy((uchar *)&rpc_pkt, pkt, sizeof(rpc_pkt.u.reply));
wdenkbe9c1cb2004-02-24 02:00:03 +0000550
Matthias Bruggerb8d5b8c2012-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;
wdenkacd9b102004-03-14 00:59:59 +0000555
wdenkbe9c1cb2004-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 Hershberger6ecfc6b2012-05-15 08:59:09 +0000560 if (rpc_pkt.u.reply.rstatus)
wdenkbe9c1cb2004-02-24 02:00:03 +0000561 return -9999;
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000562 if (rpc_pkt.u.reply.astatus)
wdenkbe9c1cb2004-02-24 02:00:03 +0000563 return -9999;
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000564 return -ntohl(rpc_pkt.u.reply.data[0]);
wdenkbe9c1cb2004-02-24 02:00:03 +0000565 }
566
Joe Hershberger6ecfc6b2012-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('#');
wdenkbe9c1cb2004-02-24 02:00:03 +0000572
573 rlen = ntohl(rpc_pkt.u.reply.data[18]);
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000574 if (store_block((uchar *)pkt + sizeof(rpc_pkt.u.reply),
575 nfs_offset, rlen))
Wolfgang Denkb077b502005-08-06 01:11:12 +0200576 return -9999;
wdenkbe9c1cb2004-02-24 02:00:03 +0000577
578 return rlen;
579}
580
581/**************************************************************************
582Interfaces of U-BOOT
583**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500584static void nfs_timeout_handler(void)
wdenkd9ee50d2004-09-28 21:51:42 +0000585{
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500586 if (++nfs_timeout_count > NFS_RETRY_COUNT) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000587 puts("\nRetry count exceeded; starting again\n");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500588 net_start_again();
Evan Samanasb956fba2009-11-09 20:08:36 -0600589 } else {
590 puts("T ");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500591 net_set_timeout_handler(nfs_timeout +
592 NFS_TIMEOUT * nfs_timeout_count,
593 nfs_timeout_handler);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500594 nfs_send();
Hiroshi Ito3702cd72008-01-31 18:35:04 +0900595 }
wdenkd9ee50d2004-09-28 21:51:42 +0000596}
597
Joe Hershberger5874dec2015-04-08 01:41:01 -0500598static void nfs_handler(uchar *pkt, unsigned dest, struct in_addr sip,
599 unsigned src, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000600{
601 int rlen;
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100602 int reply;
wdenkbe9c1cb2004-02-24 02:00:03 +0000603
Robin Getz9e0a4d62009-07-23 03:01:03 -0400604 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000605
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500606 if (dest != nfs_our_port)
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000607 return;
wdenkbe9c1cb2004-02-24 02:00:03 +0000608
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500609 switch (nfs_state) {
wdenkbe9c1cb2004-02-24 02:00:03 +0000610 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100611 if (rpc_lookup_reply(PROG_MOUNT, pkt, len) == -NFS_RPC_DROP)
612 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500613 nfs_state = STATE_PRCLOOKUP_PROG_NFS_REQ;
614 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000615 break;
616
617 case STATE_PRCLOOKUP_PROG_NFS_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100618 if (rpc_lookup_reply(PROG_NFS, pkt, len) == -NFS_RPC_DROP)
619 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500620 nfs_state = STATE_MOUNT_REQ;
621 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000622 break;
623
624 case STATE_MOUNT_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100625 reply = nfs_mount_reply(pkt, len);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500626 if (reply == -NFS_RPC_DROP) {
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100627 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500628 } else if (reply == -NFS_RPC_ERR) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000629 puts("*** ERROR: Cannot mount\n");
wdenkbe9c1cb2004-02-24 02:00:03 +0000630 /* just to be sure... */
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500631 nfs_state = STATE_UMOUNT_REQ;
632 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000633 } else {
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500634 nfs_state = STATE_LOOKUP_REQ;
635 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000636 }
637 break;
638
639 case STATE_UMOUNT_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100640 reply = nfs_umountall_reply(pkt, len);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500641 if (reply == -NFS_RPC_DROP) {
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100642 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500643 } else if (reply == -NFS_RPC_ERR) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000644 puts("*** ERROR: Cannot umount\n");
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000645 net_set_state(NETLOOP_FAIL);
wdenkbe9c1cb2004-02-24 02:00:03 +0000646 } else {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000647 puts("\ndone\n");
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000648 net_set_state(nfs_download_state);
wdenkbe9c1cb2004-02-24 02:00:03 +0000649 }
650 break;
651
652 case STATE_LOOKUP_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100653 reply = nfs_lookup_reply(pkt, len);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500654 if (reply == -NFS_RPC_DROP) {
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100655 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500656 } else if (reply == -NFS_RPC_ERR) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000657 puts("*** ERROR: File lookup fail\n");
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500658 nfs_state = STATE_UMOUNT_REQ;
659 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000660 } else {
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500661 nfs_state = STATE_READ_REQ;
wdenkbe9c1cb2004-02-24 02:00:03 +0000662 nfs_offset = 0;
663 nfs_len = NFS_READ_SIZE;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500664 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000665 }
666 break;
667
668 case STATE_READLINK_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100669 reply = nfs_readlink_reply(pkt, len);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500670 if (reply == -NFS_RPC_DROP) {
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100671 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500672 } else if (reply == -NFS_RPC_ERR) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000673 puts("*** ERROR: Symlink fail\n");
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500674 nfs_state = STATE_UMOUNT_REQ;
675 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000676 } else {
Robin Getz9e0a4d62009-07-23 03:01:03 -0400677 debug("Symlink --> %s\n", nfs_path);
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000678 nfs_filename = basename(nfs_path);
679 nfs_path = dirname(nfs_path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000680
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500681 nfs_state = STATE_MOUNT_REQ;
682 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000683 }
684 break;
685
686 case STATE_READ_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000687 rlen = nfs_read_reply(pkt, len);
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500688 net_set_timeout_handler(nfs_timeout, nfs_timeout_handler);
wdenkbe9c1cb2004-02-24 02:00:03 +0000689 if (rlen > 0) {
690 nfs_offset += rlen;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500691 nfs_send();
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000692 } else if ((rlen == -NFSERR_ISDIR) || (rlen == -NFSERR_INVAL)) {
wdenkbe9c1cb2004-02-24 02:00:03 +0000693 /* symbolic link */
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500694 nfs_state = STATE_READLINK_REQ;
695 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000696 } else {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000697 if (!rlen)
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000698 nfs_download_state = NETLOOP_SUCCESS;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500699 nfs_state = STATE_UMOUNT_REQ;
700 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000701 }
702 break;
703 }
704}
705
wdenkbe9c1cb2004-02-24 02:00:03 +0000706
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500707void nfs_start(void)
wdenkbe9c1cb2004-02-24 02:00:03 +0000708{
Robin Getz9e0a4d62009-07-23 03:01:03 -0400709 debug("%s\n", __func__);
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000710 nfs_download_state = NETLOOP_FAIL;
wdenkbe9c1cb2004-02-24 02:00:03 +0000711
Joe Hershberger5874dec2015-04-08 01:41:01 -0500712 nfs_server_ip = net_server_ip;
wdenkbe9c1cb2004-02-24 02:00:03 +0000713 nfs_path = (char *)nfs_path_buff;
714
715 if (nfs_path == NULL) {
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000716 net_set_state(NETLOOP_FAIL);
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000717 puts("*** ERROR: Fail allocate memory\n");
wdenkbe9c1cb2004-02-24 02:00:03 +0000718 return;
719 }
720
Joe Hershberger290c8992015-04-08 01:41:02 -0500721 if (net_boot_file_name[0] == '\0') {
Joe Hershbergera8d4f892016-08-15 14:42:16 -0500722 sprintf(nfs_path, "/nfsroot/%02X%02X%02X%02X.img",
Joe Hershberger5874dec2015-04-08 01:41:01 -0500723 net_ip.s_addr & 0xFF,
724 (net_ip.s_addr >> 8) & 0xFF,
725 (net_ip.s_addr >> 16) & 0xFF,
726 (net_ip.s_addr >> 24) & 0xFF);
wdenkbe9c1cb2004-02-24 02:00:03 +0000727
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000728 printf("*** Warning: no boot file name; using '%s'\n",
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500729 nfs_path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000730 } else {
Joe Hershberger290c8992015-04-08 01:41:02 -0500731 char *p = net_boot_file_name;
wdenkbe9c1cb2004-02-24 02:00:03 +0000732
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000733 p = strchr(p, ':');
wdenkbe9c1cb2004-02-24 02:00:03 +0000734
735 if (p != NULL) {
Joe Hershberger290c8992015-04-08 01:41:02 -0500736 nfs_server_ip = string_to_ip(net_boot_file_name);
wdenkbe9c1cb2004-02-24 02:00:03 +0000737 ++p;
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000738 strcpy(nfs_path, p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000739 } else {
Joe Hershberger290c8992015-04-08 01:41:02 -0500740 strcpy(nfs_path, net_boot_file_name);
wdenkbe9c1cb2004-02-24 02:00:03 +0000741 }
742 }
743
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000744 nfs_filename = basename(nfs_path);
745 nfs_path = dirname(nfs_path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000746
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000747 printf("Using %s device\n", eth_get_name());
wdenkbe9c1cb2004-02-24 02:00:03 +0000748
Joe Hershberger5874dec2015-04-08 01:41:01 -0500749 printf("File transfer via NFS from server %pI4; our IP address is %pI4",
750 &nfs_server_ip, &net_ip);
wdenkbe9c1cb2004-02-24 02:00:03 +0000751
752 /* Check if we need to send across this subnet */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500753 if (net_gateway.s_addr && net_netmask.s_addr) {
754 struct in_addr our_net;
755 struct in_addr server_net;
wdenkbe9c1cb2004-02-24 02:00:03 +0000756
Joe Hershberger5874dec2015-04-08 01:41:01 -0500757 our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
758 server_net.s_addr = net_server_ip.s_addr & net_netmask.s_addr;
759 if (our_net.s_addr != server_net.s_addr)
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000760 printf("; sending through gateway %pI4",
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500761 &net_gateway);
wdenkbe9c1cb2004-02-24 02:00:03 +0000762 }
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000763 printf("\nFilename '%s/%s'.", nfs_path, nfs_filename);
wdenkbe9c1cb2004-02-24 02:00:03 +0000764
Joe Hershberger290c8992015-04-08 01:41:02 -0500765 if (net_boot_file_expected_size_in_blocks) {
766 printf(" Size is 0x%x Bytes = ",
767 net_boot_file_expected_size_in_blocks << 9);
768 print_size(net_boot_file_expected_size_in_blocks << 9, "");
wdenkbe9c1cb2004-02-24 02:00:03 +0000769 }
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000770 printf("\nLoad address: 0x%lx\n"
wdenk42c05472004-03-23 22:14:11 +0000771 "Loading: *\b", load_addr);
wdenkbe9c1cb2004-02-24 02:00:03 +0000772
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500773 net_set_timeout_handler(nfs_timeout, nfs_timeout_handler);
Joe Hershberger5874dec2015-04-08 01:41:01 -0500774 net_set_udp_handler(nfs_handler);
wdenkbe9c1cb2004-02-24 02:00:03 +0000775
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500776 nfs_timeout_count = 0;
777 nfs_state = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
wdenkbe9c1cb2004-02-24 02:00:03 +0000778
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500779 /*nfs_our_port = 4096 + (get_ticks() % 3072);*/
wdenkbe9c1cb2004-02-24 02:00:03 +0000780 /*FIX ME !!!*/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500781 nfs_our_port = 1000;
wdenkbe9c1cb2004-02-24 02:00:03 +0000782
783 /* zero out server ether in case the server ip has changed */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500784 memset(net_server_ethaddr, 0, 6);
wdenkbe9c1cb2004-02-24 02:00:03 +0000785
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500786 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000787}