blob: 814751b0a0cb4ad0c5256702203783168da45c7a [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
Guillaume GARDET2a4bc832016-07-29 11:31:00 +020025/* NOTE 4: NFSv3 support added by Guillaume GARDET, 2016-June-20.
26 * NFSv2 is still used by default. But if server does not support NFSv2, then
27 * NFSv3 is used, if available on NFS server. */
28
wdenkbe9c1cb2004-02-24 02:00:03 +000029#include <common.h>
30#include <command.h>
31#include <net.h>
32#include <malloc.h>
Joe Hershbergered191852015-03-22 17:09:08 -050033#include <mapmem.h>
wdenkbe9c1cb2004-02-24 02:00:03 +000034#include "nfs.h"
35#include "bootp.h"
36
wdenkbe9c1cb2004-02-24 02:00:03 +000037#define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
Hiroshi Ito3702cd72008-01-31 18:35:04 +090038#define NFS_RETRY_COUNT 30
Tetsuyuki Kobayashi147e3902012-07-03 22:25:21 +000039#ifndef CONFIG_NFS_TIMEOUT
40# define NFS_TIMEOUT 2000UL
41#else
42# define NFS_TIMEOUT CONFIG_NFS_TIMEOUT
43#endif
wdenkbe9c1cb2004-02-24 02:00:03 +000044
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +010045#define NFS_RPC_ERR 1
46#define NFS_RPC_DROP 124
47
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +000048static int fs_mounted;
49static unsigned long rpc_id;
wdenkbe9c1cb2004-02-24 02:00:03 +000050static int nfs_offset = -1;
51static int nfs_len;
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +010052static ulong nfs_timeout = NFS_TIMEOUT;
wdenkbe9c1cb2004-02-24 02:00:03 +000053
Guillaume GARDET2a4bc832016-07-29 11:31:00 +020054static char dirfh[NFS_FHSIZE]; /* NFSv2 / NFSv3 file handle of directory */
Joe Hershbergerf59972b2016-08-15 15:03:19 -050055static char filefh[NFS3_FHSIZE]; /* NFSv2 / NFSv3 file handle */
56static int filefh3_length; /* (variable) length of filefh when NFSv3 */
wdenkbe9c1cb2004-02-24 02:00:03 +000057
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000058static enum net_loop_state nfs_download_state;
Joe Hershberger5874dec2015-04-08 01:41:01 -050059static struct in_addr nfs_server_ip;
Joe Hershberger40d7ca92015-04-08 01:41:10 -050060static int nfs_server_mount_port;
61static int nfs_server_port;
62static int nfs_our_port;
63static int nfs_timeout_count;
64static int nfs_state;
wdenkbe9c1cb2004-02-24 02:00:03 +000065#define STATE_PRCLOOKUP_PROG_MOUNT_REQ 1
66#define STATE_PRCLOOKUP_PROG_NFS_REQ 2
67#define STATE_MOUNT_REQ 3
68#define STATE_UMOUNT_REQ 4
69#define STATE_LOOKUP_REQ 5
70#define STATE_READ_REQ 6
71#define STATE_READLINK_REQ 7
72
wdenkbe9c1cb2004-02-24 02:00:03 +000073static char *nfs_filename;
74static char *nfs_path;
75static char nfs_path_buff[2048];
76
Guillaume GARDET2a4bc832016-07-29 11:31:00 +020077#define NFSV2_FLAG 1
78#define NFSV3_FLAG 1 << 1
79static char supported_nfs_versions = NFSV2_FLAG | NFSV3_FLAG;
80
Joe Hershberger40d7ca92015-04-08 01:41:10 -050081static inline int store_block(uchar *src, unsigned offset, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +000082{
wdenk77ef4c62004-02-24 22:33:21 +000083 ulong newsize = offset + len;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020084#ifdef CONFIG_SYS_DIRECT_FLASH_NFS
wdenkbe9c1cb2004-02-24 02:00:03 +000085 int i, rc = 0;
86
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +000087 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenkbe9c1cb2004-02-24 02:00:03 +000088 /* start address in flash? */
89 if (load_addr + offset >= flash_info[i].start[0]) {
90 rc = 1;
91 break;
92 }
93 }
94
95 if (rc) { /* Flash is destination for this packet */
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +000096 rc = flash_write((uchar *)src, (ulong)(load_addr+offset), len);
wdenkbe9c1cb2004-02-24 02:00:03 +000097 if (rc) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +000098 flash_perror(rc);
Wolfgang Denkb077b502005-08-06 01:11:12 +020099 return -1;
wdenkbe9c1cb2004-02-24 02:00:03 +0000100 }
101 } else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200102#endif /* CONFIG_SYS_DIRECT_FLASH_NFS */
wdenkbe9c1cb2004-02-24 02:00:03 +0000103 {
Joe Hershbergered191852015-03-22 17:09:08 -0500104 void *ptr = map_sysmem(load_addr + offset, len);
105
106 memcpy(ptr, src, len);
107 unmap_sysmem(ptr);
wdenkbe9c1cb2004-02-24 02:00:03 +0000108 }
wdenk77ef4c62004-02-24 22:33:21 +0000109
Joe Hershberger290c8992015-04-08 01:41:02 -0500110 if (net_boot_file_size < (offset + len))
111 net_boot_file_size = newsize;
Wolfgang Denkb077b502005-08-06 01:11:12 +0200112 return 0;
wdenkbe9c1cb2004-02-24 02:00:03 +0000113}
114
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500115static char *basename(char *path)
wdenkbe9c1cb2004-02-24 02:00:03 +0000116{
117 char *fname;
118
119 fname = path + strlen(path) - 1;
120 while (fname >= path) {
121 if (*fname == '/') {
122 fname++;
123 break;
124 }
125 fname--;
126 }
127 return fname;
128}
129
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500130static char *dirname(char *path)
wdenkbe9c1cb2004-02-24 02:00:03 +0000131{
132 char *fname;
133
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000134 fname = basename(path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000135 --fname;
136 *fname = '\0';
137 return path;
138}
139
140/**************************************************************************
wdenkbe9c1cb2004-02-24 02:00:03 +0000141RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
142**************************************************************************/
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200143static uint32_t *rpc_add_credentials(uint32_t *p)
wdenkbe9c1cb2004-02-24 02:00:03 +0000144{
wdenkbe9c1cb2004-02-24 02:00:03 +0000145 /* Here's the executive summary on authentication requirements of the
146 * various NFS server implementations: Linux accepts both AUTH_NONE
147 * and AUTH_UNIX authentication (also accepts an empty hostname field
148 * in the AUTH_UNIX scheme). *BSD refuses AUTH_NONE, but accepts
149 * AUTH_UNIX (also accepts an empty hostname field in the AUTH_UNIX
150 * scheme). To be safe, use AUTH_UNIX and pass the hostname if we have
151 * it (if the BOOTP/DHCP reply didn't give one, just use an empty
152 * hostname). */
153
wdenkbe9c1cb2004-02-24 02:00:03 +0000154 /* Provide an AUTH_UNIX credential. */
155 *p++ = htonl(1); /* AUTH_UNIX */
Joe Hershberger35fd3782016-08-15 15:03:27 -0500156 *p++ = htonl(20); /* auth length */
157 *p++ = 0; /* stamp */
158 *p++ = 0; /* hostname string */
wdenkbe9c1cb2004-02-24 02:00:03 +0000159 *p++ = 0; /* uid */
160 *p++ = 0; /* gid */
161 *p++ = 0; /* auxiliary gid list */
162
163 /* Provide an AUTH_NONE verifier. */
164 *p++ = 0; /* AUTH_NONE */
165 *p++ = 0; /* auth length */
166
167 return p;
168}
169
170/**************************************************************************
171RPC_LOOKUP - Lookup RPC Port numbers
172**************************************************************************/
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500173static struct rpc_t *rpc_req_prep(void)
wdenkbe9c1cb2004-02-24 02:00:03 +0000174{
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500175 return (struct rpc_t *)(net_tx_packet + net_eth_hdr_size() +
176 IP_UDP_HDR_SIZE);
177}
178
179static void rpc_req(int rpc_prog, int rpc_proc, struct rpc_t *rpc_pkt,
180 int datalen)
181{
wdenkbe9c1cb2004-02-24 02:00:03 +0000182 unsigned long id;
wdenkbe9c1cb2004-02-24 02:00:03 +0000183 int pktlen;
184 int sport;
185
wdenkacd9b102004-03-14 00:59:59 +0000186 id = ++rpc_id;
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500187 rpc_pkt->u.call.id = htonl(id);
188 rpc_pkt->u.call.type = htonl(MSG_CALL);
189 rpc_pkt->u.call.rpcvers = htonl(2); /* use RPC version 2 */
190 rpc_pkt->u.call.prog = htonl(rpc_prog);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200191 switch (rpc_prog) {
192 case PROG_NFS:
193 if (supported_nfs_versions & NFSV2_FLAG)
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500194 rpc_pkt->u.call.vers = htonl(2); /* NFS v2 */
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200195 else /* NFSV3_FLAG */
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500196 rpc_pkt->u.call.vers = htonl(3); /* NFS v3 */
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200197 break;
198 case PROG_PORTMAP:
199 case PROG_MOUNT:
200 default:
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500201 /* portmapper is version 2 */
202 rpc_pkt->u.call.vers = htonl(2);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200203 }
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500204 rpc_pkt->u.call.proc = htonl(rpc_proc);
wdenkbe9c1cb2004-02-24 02:00:03 +0000205
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500206 pktlen = ((char *)&rpc_pkt->u.call.data - (char *)&rpc_pkt) +
207 datalen * sizeof(uint32_t);
wdenkbe9c1cb2004-02-24 02:00:03 +0000208
209 if (rpc_prog == PROG_PORTMAP)
210 sport = SUNRPC_PORT;
211 else if (rpc_prog == PROG_MOUNT)
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500212 sport = nfs_server_mount_port;
wdenkbe9c1cb2004-02-24 02:00:03 +0000213 else
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500214 sport = nfs_server_port;
wdenkbe9c1cb2004-02-24 02:00:03 +0000215
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500216 net_send_udp_packet(net_server_ethaddr, nfs_server_ip, sport,
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500217 nfs_our_port, pktlen);
wdenkbe9c1cb2004-02-24 02:00:03 +0000218}
219
220/**************************************************************************
221RPC_LOOKUP - Lookup RPC Port numbers
222**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500223static void rpc_lookup_req(int prog, int ver)
wdenkbe9c1cb2004-02-24 02:00:03 +0000224{
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500225 uint32_t *data;
226 struct rpc_t *rpc_pkt = rpc_req_prep();
wdenkbe9c1cb2004-02-24 02:00:03 +0000227
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500228 data = rpc_pkt->u.call.data;
wdenkbe9c1cb2004-02-24 02:00:03 +0000229 data[0] = 0; data[1] = 0; /* auth credential */
230 data[2] = 0; data[3] = 0; /* auth verifier */
231 data[4] = htonl(prog);
232 data[5] = htonl(ver);
233 data[6] = htonl(17); /* IP_UDP */
234 data[7] = 0;
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500235 rpc_req(PROG_PORTMAP, PORTMAP_GETPORT, rpc_pkt, 8);
wdenkbe9c1cb2004-02-24 02:00:03 +0000236}
237
238/**************************************************************************
239NFS_MOUNT - Mount an NFS Filesystem
240**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500241static void nfs_mount_req(char *path)
wdenkbe9c1cb2004-02-24 02:00:03 +0000242{
wdenkbe9c1cb2004-02-24 02:00:03 +0000243 uint32_t *p;
244 int len;
245 int pathlen;
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500246 struct rpc_t *rpc_pkt = rpc_req_prep();
wdenkbe9c1cb2004-02-24 02:00:03 +0000247
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000248 pathlen = strlen(path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000249
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500250 p = rpc_pkt->u.call.data;
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200251 p = rpc_add_credentials(p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000252
253 *p++ = htonl(pathlen);
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000254 if (pathlen & 3)
255 *(p + pathlen / 4) = 0;
256 memcpy(p, path, pathlen);
wdenkbe9c1cb2004-02-24 02:00:03 +0000257 p += (pathlen + 3) / 4;
258
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500259 len = (uint32_t *)p - (uint32_t *)&(rpc_pkt->u.call.data);
wdenkbe9c1cb2004-02-24 02:00:03 +0000260
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500261 rpc_req(PROG_MOUNT, MOUNT_ADDENTRY, rpc_pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000262}
263
264/**************************************************************************
265NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
266**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500267static void nfs_umountall_req(void)
wdenkbe9c1cb2004-02-24 02:00:03 +0000268{
wdenkbe9c1cb2004-02-24 02:00:03 +0000269 uint32_t *p;
270 int len;
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500271 struct rpc_t *rpc_pkt = rpc_req_prep();
wdenkbe9c1cb2004-02-24 02:00:03 +0000272
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500273 if ((nfs_server_mount_port == -1) || (!fs_mounted))
wdenkbe9c1cb2004-02-24 02:00:03 +0000274 /* Nothing mounted, nothing to umount */
275 return;
wdenkbe9c1cb2004-02-24 02:00:03 +0000276
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500277 p = rpc_pkt->u.call.data;
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200278 p = rpc_add_credentials(p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000279
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500280 len = (uint32_t *)p - (uint32_t *)&(rpc_pkt->u.call.data);
wdenkbe9c1cb2004-02-24 02:00:03 +0000281
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500282 rpc_req(PROG_MOUNT, MOUNT_UMOUNTALL, rpc_pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000283}
284
285/***************************************************************************
286 * NFS_READLINK (AH 2003-07-14)
287 * This procedure is called when read of the first block fails -
288 * this probably happens when it's a directory or a symlink
289 * In case of successful readlink(), the dirname is manipulated,
290 * so that inside the nfs() function a recursion can be done.
291 **************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500292static void nfs_readlink_req(void)
wdenkbe9c1cb2004-02-24 02:00:03 +0000293{
wdenkbe9c1cb2004-02-24 02:00:03 +0000294 uint32_t *p;
295 int len;
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500296 struct rpc_t *rpc_pkt = rpc_req_prep();
wdenkbe9c1cb2004-02-24 02:00:03 +0000297
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500298 p = rpc_pkt->u.call.data;
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200299 p = rpc_add_credentials(p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000300
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200301 if (supported_nfs_versions & NFSV2_FLAG) {
302 memcpy(p, filefh, NFS_FHSIZE);
303 p += (NFS_FHSIZE / 4);
304 } else { /* NFSV3_FLAG */
305 *p++ = htonl(filefh3_length);
Joe Hershbergerf59972b2016-08-15 15:03:19 -0500306 memcpy(p, filefh, filefh3_length);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200307 p += (filefh3_length / 4);
308 }
wdenkbe9c1cb2004-02-24 02:00:03 +0000309
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500310 len = (uint32_t *)p - (uint32_t *)&(rpc_pkt->u.call.data);
wdenkbe9c1cb2004-02-24 02:00:03 +0000311
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500312 rpc_req(PROG_NFS, NFS_READLINK, rpc_pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000313}
314
315/**************************************************************************
316NFS_LOOKUP - Lookup Pathname
317**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500318static void nfs_lookup_req(char *fname)
wdenkbe9c1cb2004-02-24 02:00:03 +0000319{
wdenkbe9c1cb2004-02-24 02:00:03 +0000320 uint32_t *p;
321 int len;
322 int fnamelen;
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500323 struct rpc_t *rpc_pkt = rpc_req_prep();
wdenkbe9c1cb2004-02-24 02:00:03 +0000324
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000325 fnamelen = strlen(fname);
wdenkbe9c1cb2004-02-24 02:00:03 +0000326
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500327 p = rpc_pkt->u.call.data;
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200328 p = rpc_add_credentials(p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000329
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200330 if (supported_nfs_versions & NFSV2_FLAG) {
331 memcpy(p, dirfh, NFS_FHSIZE);
332 p += (NFS_FHSIZE / 4);
333 *p++ = htonl(fnamelen);
334 if (fnamelen & 3)
335 *(p + fnamelen / 4) = 0;
336 memcpy(p, fname, fnamelen);
337 p += (fnamelen + 3) / 4;
wdenkbe9c1cb2004-02-24 02:00:03 +0000338
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500339 len = (uint32_t *)p - (uint32_t *)&(rpc_pkt->u.call.data);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200340
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500341 rpc_req(PROG_NFS, NFS_LOOKUP, rpc_pkt, len);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200342 } else { /* NFSV3_FLAG */
343 *p++ = htonl(NFS_FHSIZE); /* Dir handle length */
344 memcpy(p, dirfh, NFS_FHSIZE);
345 p += (NFS_FHSIZE / 4);
346 *p++ = htonl(fnamelen);
347 if (fnamelen & 3)
348 *(p + fnamelen / 4) = 0;
349 memcpy(p, fname, fnamelen);
350 p += (fnamelen + 3) / 4;
wdenkbe9c1cb2004-02-24 02:00:03 +0000351
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500352 len = (uint32_t *)p - (uint32_t *)&(rpc_pkt->u.call.data);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200353
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500354 rpc_req(PROG_NFS, NFS3PROC_LOOKUP, rpc_pkt, len);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200355 }
wdenkbe9c1cb2004-02-24 02:00:03 +0000356}
357
358/**************************************************************************
359NFS_READ - Read File on NFS Server
360**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500361static void nfs_read_req(int offset, int readlen)
wdenkbe9c1cb2004-02-24 02:00:03 +0000362{
wdenkbe9c1cb2004-02-24 02:00:03 +0000363 uint32_t *p;
364 int len;
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500365 struct rpc_t *rpc_pkt = rpc_req_prep();
wdenkbe9c1cb2004-02-24 02:00:03 +0000366
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500367 p = rpc_pkt->u.call.data;
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200368 p = rpc_add_credentials(p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000369
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200370 if (supported_nfs_versions & NFSV2_FLAG) {
371 memcpy(p, filefh, NFS_FHSIZE);
372 p += (NFS_FHSIZE / 4);
373 *p++ = htonl(offset);
374 *p++ = htonl(readlen);
375 *p++ = 0;
376 } else { /* NFSV3_FLAG */
377 *p++ = htonl(filefh3_length);
Joe Hershbergerf59972b2016-08-15 15:03:19 -0500378 memcpy(p, filefh, filefh3_length);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200379 p += (filefh3_length / 4);
380 *p++ = htonl(0); /* offset is 64-bit long, so fill with 0 */
381 *p++ = htonl(offset);
382 *p++ = htonl(readlen);
383 *p++ = 0;
384 }
wdenkbe9c1cb2004-02-24 02:00:03 +0000385
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500386 len = (uint32_t *)p - (uint32_t *)&(rpc_pkt->u.call.data);
wdenkbe9c1cb2004-02-24 02:00:03 +0000387
Joe Hershbergerf6b19eb2016-08-15 15:03:26 -0500388 rpc_req(PROG_NFS, NFS_READ, rpc_pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000389}
390
391/**************************************************************************
392RPC request dispatcher
393**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500394static void nfs_send(void)
wdenkbe9c1cb2004-02-24 02:00:03 +0000395{
Robin Getz9e0a4d62009-07-23 03:01:03 -0400396 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000397
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500398 switch (nfs_state) {
wdenkbe9c1cb2004-02-24 02:00:03 +0000399 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200400 if (supported_nfs_versions & NFSV2_FLAG)
401 rpc_lookup_req(PROG_MOUNT, 1);
402 else /* NFSV3_FLAG */
403 rpc_lookup_req(PROG_MOUNT, 3);
wdenkbe9c1cb2004-02-24 02:00:03 +0000404 break;
405 case STATE_PRCLOOKUP_PROG_NFS_REQ:
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200406 if (supported_nfs_versions & NFSV2_FLAG)
407 rpc_lookup_req(PROG_NFS, 2);
408 else /* NFSV3_FLAG */
409 rpc_lookup_req(PROG_NFS, 3);
wdenkbe9c1cb2004-02-24 02:00:03 +0000410 break;
411 case STATE_MOUNT_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000412 nfs_mount_req(nfs_path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000413 break;
414 case STATE_UMOUNT_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000415 nfs_umountall_req();
wdenkbe9c1cb2004-02-24 02:00:03 +0000416 break;
417 case STATE_LOOKUP_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000418 nfs_lookup_req(nfs_filename);
wdenkbe9c1cb2004-02-24 02:00:03 +0000419 break;
420 case STATE_READ_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000421 nfs_read_req(nfs_offset, nfs_len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000422 break;
423 case STATE_READLINK_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000424 nfs_readlink_req();
wdenkbe9c1cb2004-02-24 02:00:03 +0000425 break;
426 }
427}
428
429/**************************************************************************
430Handlers for the reply from server
431**************************************************************************/
432
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500433static int rpc_lookup_reply(int prog, uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000434{
435 struct rpc_t rpc_pkt;
436
Joe Hershberger3393d4c2016-08-15 15:03:24 -0500437 memcpy(&rpc_pkt.u.data[0], pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000438
Robin Getz9e0a4d62009-07-23 03:01:03 -0400439 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000440
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100441 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
442 return -NFS_RPC_ERR;
443 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
444 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000445
wdenkbe9c1cb2004-02-24 02:00:03 +0000446 if (rpc_pkt.u.reply.rstatus ||
447 rpc_pkt.u.reply.verifier ||
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000448 rpc_pkt.u.reply.astatus)
wdenkacd9b102004-03-14 00:59:59 +0000449 return -1;
wdenkbe9c1cb2004-02-24 02:00:03 +0000450
451 switch (prog) {
452 case PROG_MOUNT:
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500453 nfs_server_mount_port = ntohl(rpc_pkt.u.reply.data[0]);
wdenkbe9c1cb2004-02-24 02:00:03 +0000454 break;
455 case PROG_NFS:
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500456 nfs_server_port = ntohl(rpc_pkt.u.reply.data[0]);
wdenkbe9c1cb2004-02-24 02:00:03 +0000457 break;
458 }
459
460 return 0;
461}
462
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500463static int nfs_mount_reply(uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000464{
465 struct rpc_t rpc_pkt;
466
Robin Getz9e0a4d62009-07-23 03:01:03 -0400467 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000468
Joe Hershberger3393d4c2016-08-15 15:03:24 -0500469 memcpy(&rpc_pkt.u.data[0], pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000470
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100471 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
472 return -NFS_RPC_ERR;
473 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
474 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000475
wdenkbe9c1cb2004-02-24 02:00:03 +0000476 if (rpc_pkt.u.reply.rstatus ||
477 rpc_pkt.u.reply.verifier ||
478 rpc_pkt.u.reply.astatus ||
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000479 rpc_pkt.u.reply.data[0])
wdenkbe9c1cb2004-02-24 02:00:03 +0000480 return -1;
wdenkbe9c1cb2004-02-24 02:00:03 +0000481
482 fs_mounted = 1;
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200483 /* NFSv2 and NFSv3 use same structure */
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000484 memcpy(dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
wdenkbe9c1cb2004-02-24 02:00:03 +0000485
486 return 0;
487}
488
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500489static int nfs_umountall_reply(uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000490{
491 struct rpc_t rpc_pkt;
492
Robin Getz9e0a4d62009-07-23 03:01:03 -0400493 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000494
Joe Hershberger3393d4c2016-08-15 15:03:24 -0500495 memcpy(&rpc_pkt.u.data[0], pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000496
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100497 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
498 return -NFS_RPC_ERR;
499 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
500 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000501
wdenkbe9c1cb2004-02-24 02:00:03 +0000502 if (rpc_pkt.u.reply.rstatus ||
503 rpc_pkt.u.reply.verifier ||
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000504 rpc_pkt.u.reply.astatus)
wdenkbe9c1cb2004-02-24 02:00:03 +0000505 return -1;
wdenkbe9c1cb2004-02-24 02:00:03 +0000506
507 fs_mounted = 0;
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000508 memset(dirfh, 0, sizeof(dirfh));
wdenkbe9c1cb2004-02-24 02:00:03 +0000509
510 return 0;
511}
512
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500513static int nfs_lookup_reply(uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000514{
515 struct rpc_t rpc_pkt;
516
Robin Getz9e0a4d62009-07-23 03:01:03 -0400517 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000518
Joe Hershberger3393d4c2016-08-15 15:03:24 -0500519 memcpy(&rpc_pkt.u.data[0], pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000520
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100521 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
522 return -NFS_RPC_ERR;
523 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
524 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000525
wdenkbe9c1cb2004-02-24 02:00:03 +0000526 if (rpc_pkt.u.reply.rstatus ||
527 rpc_pkt.u.reply.verifier ||
528 rpc_pkt.u.reply.astatus ||
Guillaume GARDET7ba4c672016-06-06 15:11:45 +0200529 rpc_pkt.u.reply.data[0]) {
530 switch (ntohl(rpc_pkt.u.reply.astatus)) {
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200531 case NFS_RPC_SUCCESS: /* Not an error */
Guillaume GARDET7ba4c672016-06-06 15:11:45 +0200532 break;
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200533 case NFS_RPC_PROG_MISMATCH:
534 /* Remote can't support NFS version */
535 switch (ntohl(rpc_pkt.u.reply.data[0])) {
536 /* Minimal supported NFS version */
537 case 3:
538 debug("*** Waring: NFS version not supported: Requested: V%d, accepted: min V%d - max V%d\n",
Joe Hershberger1f466172016-08-15 15:03:21 -0500539 (supported_nfs_versions & NFSV2_FLAG) ?
540 2 : 3,
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200541 ntohl(rpc_pkt.u.reply.data[0]),
542 ntohl(rpc_pkt.u.reply.data[1]));
543 debug("Will retry with NFSv3\n");
544 /* Clear NFSV2_FLAG from supported versions */
545 supported_nfs_versions &= ~NFSV2_FLAG;
546 return -NFS_RPC_PROG_MISMATCH;
547 case 4:
548 default:
Joe Hershberger739776a2016-08-15 15:03:25 -0500549 puts("*** ERROR: NFS version not supported");
550 debug(": Requested: V%d, accepted: min V%d - max V%d\n",
551 (supported_nfs_versions & NFSV2_FLAG) ?
Joe Hershberger1f466172016-08-15 15:03:21 -0500552 2 : 3,
Joe Hershberger739776a2016-08-15 15:03:25 -0500553 ntohl(rpc_pkt.u.reply.data[0]),
554 ntohl(rpc_pkt.u.reply.data[1]));
555 puts("\n");
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200556 }
Guillaume GARDET7ba4c672016-06-06 15:11:45 +0200557 break;
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200558 case NFS_RPC_PROG_UNAVAIL:
559 case NFS_RPC_PROC_UNAVAIL:
560 case NFS_RPC_GARBAGE_ARGS:
561 case NFS_RPC_SYSTEM_ERR:
Guillaume GARDET7ba4c672016-06-06 15:11:45 +0200562 default: /* Unknown error on 'accept state' flag */
Joe Hershberger739776a2016-08-15 15:03:25 -0500563 debug("*** ERROR: accept state error (%d)\n",
564 ntohl(rpc_pkt.u.reply.astatus));
Guillaume GARDET7ba4c672016-06-06 15:11:45 +0200565 break;
566 }
wdenkbe9c1cb2004-02-24 02:00:03 +0000567 return -1;
Guillaume GARDET7ba4c672016-06-06 15:11:45 +0200568 }
wdenkbe9c1cb2004-02-24 02:00:03 +0000569
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200570 if (supported_nfs_versions & NFSV2_FLAG) {
571 memcpy(filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
572 } else { /* NFSV3_FLAG */
573 filefh3_length = ntohl(rpc_pkt.u.reply.data[1]);
574 if (filefh3_length > NFS3_FHSIZE)
575 filefh3_length = NFS3_FHSIZE;
Joe Hershbergerf59972b2016-08-15 15:03:19 -0500576 memcpy(filefh, rpc_pkt.u.reply.data + 2, filefh3_length);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200577 }
wdenkbe9c1cb2004-02-24 02:00:03 +0000578
579 return 0;
580}
581
Joe Hershberger46e93122016-08-15 15:03:22 -0500582static int nfs3_get_attributes_offset(uint32_t *data)
583{
584 if (ntohl(data[1]) != 0) {
585 /* 'attributes_follow' flag is TRUE,
Joe Hershberger10e40972016-08-15 15:03:23 -0500586 * so we have attributes on 21 dwords */
Joe Hershberger46e93122016-08-15 15:03:22 -0500587 /* Skip unused values :
588 type; 32 bits value,
589 mode; 32 bits value,
590 nlink; 32 bits value,
591 uid; 32 bits value,
592 gid; 32 bits value,
593 size; 64 bits value,
594 used; 64 bits value,
595 rdev; 64 bits value,
596 fsid; 64 bits value,
597 fileid; 64 bits value,
598 atime; 64 bits value,
599 mtime; 64 bits value,
600 ctime; 64 bits value,
601 */
602 return 22;
603 } else {
604 /* 'attributes_follow' flag is FALSE,
605 * so we don't have any attributes */
606 return 1;
607 }
608}
609
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500610static int nfs_readlink_reply(uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000611{
612 struct rpc_t rpc_pkt;
613 int rlen;
Joe Hershberger46e93122016-08-15 15:03:22 -0500614 int nfsv3_data_offset = 0;
wdenkbe9c1cb2004-02-24 02:00:03 +0000615
Robin Getz9e0a4d62009-07-23 03:01:03 -0400616 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000617
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000618 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000619
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100620 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
621 return -NFS_RPC_ERR;
622 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
623 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000624
wdenkbe9c1cb2004-02-24 02:00:03 +0000625 if (rpc_pkt.u.reply.rstatus ||
626 rpc_pkt.u.reply.verifier ||
627 rpc_pkt.u.reply.astatus ||
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000628 rpc_pkt.u.reply.data[0])
wdenkbe9c1cb2004-02-24 02:00:03 +0000629 return -1;
wdenkbe9c1cb2004-02-24 02:00:03 +0000630
Joe Hershberger46e93122016-08-15 15:03:22 -0500631 if (!(supported_nfs_versions & NFSV2_FLAG)) { /* NFSV3_FLAG */
632 nfsv3_data_offset =
633 nfs3_get_attributes_offset(rpc_pkt.u.reply.data);
634 }
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200635
Joe Hershberger46e93122016-08-15 15:03:22 -0500636 /* new path length */
637 rlen = ntohl(rpc_pkt.u.reply.data[1 + nfsv3_data_offset]);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200638
Joe Hershberger46e93122016-08-15 15:03:22 -0500639 if (*((char *)&(rpc_pkt.u.reply.data[2 + nfsv3_data_offset])) != '/') {
640 int pathlen;
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200641
Joe Hershberger46e93122016-08-15 15:03:22 -0500642 strcat(nfs_path, "/");
643 pathlen = strlen(nfs_path);
644 memcpy(nfs_path + pathlen,
645 (uchar *)&(rpc_pkt.u.reply.data[2 + nfsv3_data_offset]),
646 rlen);
647 nfs_path[pathlen + rlen] = 0;
648 } else {
649 memcpy(nfs_path,
650 (uchar *)&(rpc_pkt.u.reply.data[2 + nfsv3_data_offset]),
651 rlen);
652 nfs_path[rlen] = 0;
wdenkbe9c1cb2004-02-24 02:00:03 +0000653 }
654 return 0;
655}
656
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500657static int nfs_read_reply(uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000658{
659 struct rpc_t rpc_pkt;
660 int rlen;
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200661 uchar *data_ptr;
wdenkbe9c1cb2004-02-24 02:00:03 +0000662
Robin Getz9e0a4d62009-07-23 03:01:03 -0400663 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000664
Joe Hershberger3393d4c2016-08-15 15:03:24 -0500665 memcpy(&rpc_pkt.u.data[0], pkt, sizeof(rpc_pkt.u.reply));
wdenkbe9c1cb2004-02-24 02:00:03 +0000666
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100667 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
668 return -NFS_RPC_ERR;
669 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
670 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000671
wdenkbe9c1cb2004-02-24 02:00:03 +0000672 if (rpc_pkt.u.reply.rstatus ||
673 rpc_pkt.u.reply.verifier ||
674 rpc_pkt.u.reply.astatus ||
675 rpc_pkt.u.reply.data[0]) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000676 if (rpc_pkt.u.reply.rstatus)
wdenkbe9c1cb2004-02-24 02:00:03 +0000677 return -9999;
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000678 if (rpc_pkt.u.reply.astatus)
wdenkbe9c1cb2004-02-24 02:00:03 +0000679 return -9999;
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000680 return -ntohl(rpc_pkt.u.reply.data[0]);
wdenkbe9c1cb2004-02-24 02:00:03 +0000681 }
682
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000683 if ((nfs_offset != 0) && !((nfs_offset) %
684 (NFS_READ_SIZE / 2 * 10 * HASHES_PER_LINE)))
685 puts("\n\t ");
686 if (!(nfs_offset % ((NFS_READ_SIZE / 2) * 10)))
687 putc('#');
wdenkbe9c1cb2004-02-24 02:00:03 +0000688
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200689 if (supported_nfs_versions & NFSV2_FLAG) {
690 rlen = ntohl(rpc_pkt.u.reply.data[18]);
691 data_ptr = (uchar *)&(rpc_pkt.u.reply.data[19]);
692 } else { /* NFSV3_FLAG */
Joe Hershberger46e93122016-08-15 15:03:22 -0500693 int nfsv3_data_offset =
694 nfs3_get_attributes_offset(rpc_pkt.u.reply.data);
695
696 /* count value */
697 rlen = ntohl(rpc_pkt.u.reply.data[1 + nfsv3_data_offset]);
698 /* Skip unused values :
699 EOF: 32 bits value,
700 data_size: 32 bits value,
701 */
702 data_ptr = (uchar *)
703 &(rpc_pkt.u.reply.data[4 + nfsv3_data_offset]);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200704 }
705
706 if (store_block(data_ptr, nfs_offset, rlen))
707 return -9999;
wdenkbe9c1cb2004-02-24 02:00:03 +0000708
709 return rlen;
710}
711
712/**************************************************************************
713Interfaces of U-BOOT
714**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500715static void nfs_timeout_handler(void)
wdenkd9ee50d2004-09-28 21:51:42 +0000716{
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500717 if (++nfs_timeout_count > NFS_RETRY_COUNT) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000718 puts("\nRetry count exceeded; starting again\n");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500719 net_start_again();
Evan Samanasb956fba2009-11-09 20:08:36 -0600720 } else {
721 puts("T ");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500722 net_set_timeout_handler(nfs_timeout +
723 NFS_TIMEOUT * nfs_timeout_count,
724 nfs_timeout_handler);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500725 nfs_send();
Hiroshi Ito3702cd72008-01-31 18:35:04 +0900726 }
wdenkd9ee50d2004-09-28 21:51:42 +0000727}
728
Joe Hershberger5874dec2015-04-08 01:41:01 -0500729static void nfs_handler(uchar *pkt, unsigned dest, struct in_addr sip,
730 unsigned src, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000731{
732 int rlen;
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100733 int reply;
wdenkbe9c1cb2004-02-24 02:00:03 +0000734
Robin Getz9e0a4d62009-07-23 03:01:03 -0400735 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000736
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500737 if (dest != nfs_our_port)
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000738 return;
wdenkbe9c1cb2004-02-24 02:00:03 +0000739
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500740 switch (nfs_state) {
wdenkbe9c1cb2004-02-24 02:00:03 +0000741 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100742 if (rpc_lookup_reply(PROG_MOUNT, pkt, len) == -NFS_RPC_DROP)
743 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500744 nfs_state = STATE_PRCLOOKUP_PROG_NFS_REQ;
745 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000746 break;
747
748 case STATE_PRCLOOKUP_PROG_NFS_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100749 if (rpc_lookup_reply(PROG_NFS, pkt, len) == -NFS_RPC_DROP)
750 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500751 nfs_state = STATE_MOUNT_REQ;
752 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000753 break;
754
755 case STATE_MOUNT_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100756 reply = nfs_mount_reply(pkt, len);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500757 if (reply == -NFS_RPC_DROP) {
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100758 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500759 } else if (reply == -NFS_RPC_ERR) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000760 puts("*** ERROR: Cannot mount\n");
wdenkbe9c1cb2004-02-24 02:00:03 +0000761 /* just to be sure... */
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500762 nfs_state = STATE_UMOUNT_REQ;
763 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000764 } else {
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500765 nfs_state = STATE_LOOKUP_REQ;
766 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000767 }
768 break;
769
770 case STATE_UMOUNT_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100771 reply = nfs_umountall_reply(pkt, len);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500772 if (reply == -NFS_RPC_DROP) {
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100773 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500774 } else if (reply == -NFS_RPC_ERR) {
Joe Hershberger739776a2016-08-15 15:03:25 -0500775 debug("*** ERROR: Cannot umount\n");
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000776 net_set_state(NETLOOP_FAIL);
wdenkbe9c1cb2004-02-24 02:00:03 +0000777 } else {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000778 puts("\ndone\n");
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000779 net_set_state(nfs_download_state);
wdenkbe9c1cb2004-02-24 02:00:03 +0000780 }
781 break;
782
783 case STATE_LOOKUP_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100784 reply = nfs_lookup_reply(pkt, len);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500785 if (reply == -NFS_RPC_DROP) {
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100786 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500787 } else if (reply == -NFS_RPC_ERR) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000788 puts("*** ERROR: File lookup fail\n");
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500789 nfs_state = STATE_UMOUNT_REQ;
790 nfs_send();
Joe Hershberger1f466172016-08-15 15:03:21 -0500791 } else if (reply == -NFS_RPC_PROG_MISMATCH &&
792 supported_nfs_versions != 0) {
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200793 /* umount */
794 nfs_state = STATE_UMOUNT_REQ;
795 nfs_send();
796 /* And retry with another supported version */
797 nfs_state = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
798 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000799 } else {
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500800 nfs_state = STATE_READ_REQ;
wdenkbe9c1cb2004-02-24 02:00:03 +0000801 nfs_offset = 0;
802 nfs_len = NFS_READ_SIZE;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500803 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000804 }
805 break;
806
807 case STATE_READLINK_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100808 reply = nfs_readlink_reply(pkt, len);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500809 if (reply == -NFS_RPC_DROP) {
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100810 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500811 } else if (reply == -NFS_RPC_ERR) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000812 puts("*** ERROR: Symlink fail\n");
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500813 nfs_state = STATE_UMOUNT_REQ;
814 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000815 } else {
Robin Getz9e0a4d62009-07-23 03:01:03 -0400816 debug("Symlink --> %s\n", nfs_path);
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000817 nfs_filename = basename(nfs_path);
818 nfs_path = dirname(nfs_path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000819
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500820 nfs_state = STATE_MOUNT_REQ;
821 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000822 }
823 break;
824
825 case STATE_READ_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000826 rlen = nfs_read_reply(pkt, len);
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500827 net_set_timeout_handler(nfs_timeout, nfs_timeout_handler);
wdenkbe9c1cb2004-02-24 02:00:03 +0000828 if (rlen > 0) {
829 nfs_offset += rlen;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500830 nfs_send();
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000831 } else if ((rlen == -NFSERR_ISDIR) || (rlen == -NFSERR_INVAL)) {
wdenkbe9c1cb2004-02-24 02:00:03 +0000832 /* symbolic link */
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500833 nfs_state = STATE_READLINK_REQ;
834 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000835 } else {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000836 if (!rlen)
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000837 nfs_download_state = NETLOOP_SUCCESS;
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200838 if (rlen < 0)
Joe Hershberger739776a2016-08-15 15:03:25 -0500839 debug("NFS READ error (%d)\n", rlen);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500840 nfs_state = STATE_UMOUNT_REQ;
841 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000842 }
843 break;
844 }
845}
846
wdenkbe9c1cb2004-02-24 02:00:03 +0000847
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500848void nfs_start(void)
wdenkbe9c1cb2004-02-24 02:00:03 +0000849{
Robin Getz9e0a4d62009-07-23 03:01:03 -0400850 debug("%s\n", __func__);
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000851 nfs_download_state = NETLOOP_FAIL;
wdenkbe9c1cb2004-02-24 02:00:03 +0000852
Joe Hershberger5874dec2015-04-08 01:41:01 -0500853 nfs_server_ip = net_server_ip;
wdenkbe9c1cb2004-02-24 02:00:03 +0000854 nfs_path = (char *)nfs_path_buff;
855
856 if (nfs_path == NULL) {
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000857 net_set_state(NETLOOP_FAIL);
Joe Hershberger739776a2016-08-15 15:03:25 -0500858 debug("*** ERROR: Fail allocate memory\n");
wdenkbe9c1cb2004-02-24 02:00:03 +0000859 return;
860 }
861
Joe Hershberger290c8992015-04-08 01:41:02 -0500862 if (net_boot_file_name[0] == '\0') {
Joe Hershbergera8d4f892016-08-15 14:42:16 -0500863 sprintf(nfs_path, "/nfsroot/%02X%02X%02X%02X.img",
Joe Hershberger5874dec2015-04-08 01:41:01 -0500864 net_ip.s_addr & 0xFF,
865 (net_ip.s_addr >> 8) & 0xFF,
866 (net_ip.s_addr >> 16) & 0xFF,
867 (net_ip.s_addr >> 24) & 0xFF);
wdenkbe9c1cb2004-02-24 02:00:03 +0000868
Joe Hershberger739776a2016-08-15 15:03:25 -0500869 debug("*** Warning: no boot file name; using '%s'\n",
870 nfs_path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000871 } else {
Joe Hershberger290c8992015-04-08 01:41:02 -0500872 char *p = net_boot_file_name;
wdenkbe9c1cb2004-02-24 02:00:03 +0000873
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000874 p = strchr(p, ':');
wdenkbe9c1cb2004-02-24 02:00:03 +0000875
876 if (p != NULL) {
Joe Hershberger290c8992015-04-08 01:41:02 -0500877 nfs_server_ip = string_to_ip(net_boot_file_name);
wdenkbe9c1cb2004-02-24 02:00:03 +0000878 ++p;
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000879 strcpy(nfs_path, p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000880 } else {
Joe Hershberger290c8992015-04-08 01:41:02 -0500881 strcpy(nfs_path, net_boot_file_name);
wdenkbe9c1cb2004-02-24 02:00:03 +0000882 }
883 }
884
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000885 nfs_filename = basename(nfs_path);
886 nfs_path = dirname(nfs_path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000887
Joe Hershberger739776a2016-08-15 15:03:25 -0500888 debug("Using %s device\n", eth_get_name());
wdenkbe9c1cb2004-02-24 02:00:03 +0000889
Joe Hershberger739776a2016-08-15 15:03:25 -0500890 debug("File transfer via NFS from server %pI4; our IP address is %pI4",
891 &nfs_server_ip, &net_ip);
wdenkbe9c1cb2004-02-24 02:00:03 +0000892
893 /* Check if we need to send across this subnet */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500894 if (net_gateway.s_addr && net_netmask.s_addr) {
895 struct in_addr our_net;
896 struct in_addr server_net;
wdenkbe9c1cb2004-02-24 02:00:03 +0000897
Joe Hershberger5874dec2015-04-08 01:41:01 -0500898 our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
899 server_net.s_addr = net_server_ip.s_addr & net_netmask.s_addr;
900 if (our_net.s_addr != server_net.s_addr)
Joe Hershberger739776a2016-08-15 15:03:25 -0500901 debug("; sending through gateway %pI4",
902 &net_gateway);
wdenkbe9c1cb2004-02-24 02:00:03 +0000903 }
Joe Hershberger739776a2016-08-15 15:03:25 -0500904 debug("\nFilename '%s/%s'.", nfs_path, nfs_filename);
wdenkbe9c1cb2004-02-24 02:00:03 +0000905
Joe Hershberger290c8992015-04-08 01:41:02 -0500906 if (net_boot_file_expected_size_in_blocks) {
Joe Hershberger739776a2016-08-15 15:03:25 -0500907 debug(" Size is 0x%x Bytes = ",
908 net_boot_file_expected_size_in_blocks << 9);
Joe Hershberger290c8992015-04-08 01:41:02 -0500909 print_size(net_boot_file_expected_size_in_blocks << 9, "");
wdenkbe9c1cb2004-02-24 02:00:03 +0000910 }
Joe Hershberger739776a2016-08-15 15:03:25 -0500911 debug("\nLoad address: 0x%lx\nLoading: *\b", load_addr);
wdenkbe9c1cb2004-02-24 02:00:03 +0000912
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500913 net_set_timeout_handler(nfs_timeout, nfs_timeout_handler);
Joe Hershberger5874dec2015-04-08 01:41:01 -0500914 net_set_udp_handler(nfs_handler);
wdenkbe9c1cb2004-02-24 02:00:03 +0000915
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500916 nfs_timeout_count = 0;
917 nfs_state = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
wdenkbe9c1cb2004-02-24 02:00:03 +0000918
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500919 /*nfs_our_port = 4096 + (get_ticks() % 3072);*/
wdenkbe9c1cb2004-02-24 02:00:03 +0000920 /*FIX ME !!!*/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500921 nfs_our_port = 1000;
wdenkbe9c1cb2004-02-24 02:00:03 +0000922
923 /* zero out server ether in case the server ip has changed */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500924 memset(net_server_ethaddr, 0, 6);
wdenkbe9c1cb2004-02-24 02:00:03 +0000925
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500926 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000927}