blob: 7a8887ef2368381c1cad61638ed3580e4824aed0 [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
Thomas RIENOESSLc8075bd2023-03-10 10:51:55 +010029/* NOTE 5: NFSv1 support added by Christian Gmeiner, Thomas Rienoessl,
30 * September 27, 2018. As of now, NFSv3 is the default choice. If the server
31 * does not support NFSv3, we fall back to versions 2 or 1. */
32
wdenkbe9c1cb2004-02-24 02:00:03 +000033#include <common.h>
34#include <command.h>
Simon Glass1ab16922022-07-31 12:28:48 -060035#include <display_options.h>
Tom Rini063c9382022-07-23 13:05:03 -040036#ifdef CONFIG_SYS_DIRECT_FLASH_NFS
Simon Glassa606ffc2019-12-28 10:44:40 -070037#include <flash.h>
Tom Rini063c9382022-07-23 13:05:03 -040038#endif
Simon Glass85f13782019-12-28 10:45:03 -070039#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060040#include <log.h>
wdenkbe9c1cb2004-02-24 02:00:03 +000041#include <net.h>
42#include <malloc.h>
Joe Hershbergered191852015-03-22 17:09:08 -050043#include <mapmem.h>
wdenkbe9c1cb2004-02-24 02:00:03 +000044#include "nfs.h"
45#include "bootp.h"
Simon Glass495a5dc2019-11-14 12:57:30 -070046#include <time.h>
wdenkbe9c1cb2004-02-24 02:00:03 +000047
wdenkbe9c1cb2004-02-24 02:00:03 +000048#define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
Hiroshi Ito3702cd72008-01-31 18:35:04 +090049#define NFS_RETRY_COUNT 30
wdenkbe9c1cb2004-02-24 02:00:03 +000050
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +010051#define NFS_RPC_ERR 1
52#define NFS_RPC_DROP 124
53
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +000054static int fs_mounted;
55static unsigned long rpc_id;
wdenkbe9c1cb2004-02-24 02:00:03 +000056static int nfs_offset = -1;
57static int nfs_len;
Tom Rini0d1c4742022-03-11 09:12:05 -050058static const ulong nfs_timeout = CONFIG_NFS_TIMEOUT;
wdenkbe9c1cb2004-02-24 02:00:03 +000059
Guillaume GARDET2a4bc832016-07-29 11:31:00 +020060static char dirfh[NFS_FHSIZE]; /* NFSv2 / NFSv3 file handle of directory */
Joe Hershbergerf59972b2016-08-15 15:03:19 -050061static char filefh[NFS3_FHSIZE]; /* NFSv2 / NFSv3 file handle */
Andrea zi0Black Cappaf12a47a2022-05-18 16:30:08 +000062static unsigned int filefh3_length; /* (variable) length of filefh when NFSv3 */
wdenkbe9c1cb2004-02-24 02:00:03 +000063
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000064static enum net_loop_state nfs_download_state;
Joe Hershberger5874dec2015-04-08 01:41:01 -050065static struct in_addr nfs_server_ip;
Joe Hershberger40d7ca92015-04-08 01:41:10 -050066static int nfs_server_mount_port;
67static int nfs_server_port;
68static int nfs_our_port;
69static int nfs_timeout_count;
70static int nfs_state;
wdenkbe9c1cb2004-02-24 02:00:03 +000071#define STATE_PRCLOOKUP_PROG_MOUNT_REQ 1
72#define STATE_PRCLOOKUP_PROG_NFS_REQ 2
73#define STATE_MOUNT_REQ 3
74#define STATE_UMOUNT_REQ 4
75#define STATE_LOOKUP_REQ 5
76#define STATE_READ_REQ 6
77#define STATE_READLINK_REQ 7
78
wdenkbe9c1cb2004-02-24 02:00:03 +000079static char *nfs_filename;
80static char *nfs_path;
81static char nfs_path_buff[2048];
82
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +010083enum nfs_version {
84 NFS_UNKOWN = 0,
Thomas RIENOESSLc8075bd2023-03-10 10:51:55 +010085 NFS_V1 = 1,
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +010086 NFS_V2 = 2,
87 NFS_V3 = 3,
88};
Guillaume GARDET2a4bc832016-07-29 11:31:00 +020089
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +010090static enum nfs_version choosen_nfs_version = NFS_V3;
Joe Hershberger40d7ca92015-04-08 01:41:10 -050091static inline int store_block(uchar *src, unsigned offset, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +000092{
wdenk77ef4c62004-02-24 22:33:21 +000093 ulong newsize = offset + len;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020094#ifdef CONFIG_SYS_DIRECT_FLASH_NFS
wdenkbe9c1cb2004-02-24 02:00:03 +000095 int i, rc = 0;
96
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +000097 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenkbe9c1cb2004-02-24 02:00:03 +000098 /* start address in flash? */
Simon Glass892265d2019-12-28 10:45:02 -070099 if (image_load_addr + offset >= flash_info[i].start[0]) {
wdenkbe9c1cb2004-02-24 02:00:03 +0000100 rc = 1;
101 break;
102 }
103 }
104
105 if (rc) { /* Flash is destination for this packet */
Simon Glass892265d2019-12-28 10:45:02 -0700106 rc = flash_write((uchar *)src, (ulong)image_load_addr + offset,
107 len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000108 if (rc) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000109 flash_perror(rc);
Wolfgang Denkb077b502005-08-06 01:11:12 +0200110 return -1;
wdenkbe9c1cb2004-02-24 02:00:03 +0000111 }
112 } else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200113#endif /* CONFIG_SYS_DIRECT_FLASH_NFS */
wdenkbe9c1cb2004-02-24 02:00:03 +0000114 {
Simon Glass892265d2019-12-28 10:45:02 -0700115 void *ptr = map_sysmem(image_load_addr + offset, len);
Joe Hershbergered191852015-03-22 17:09:08 -0500116
117 memcpy(ptr, src, len);
118 unmap_sysmem(ptr);
wdenkbe9c1cb2004-02-24 02:00:03 +0000119 }
wdenk77ef4c62004-02-24 22:33:21 +0000120
Joe Hershberger290c8992015-04-08 01:41:02 -0500121 if (net_boot_file_size < (offset + len))
122 net_boot_file_size = newsize;
Wolfgang Denkb077b502005-08-06 01:11:12 +0200123 return 0;
wdenkbe9c1cb2004-02-24 02:00:03 +0000124}
125
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500126static char *basename(char *path)
wdenkbe9c1cb2004-02-24 02:00:03 +0000127{
128 char *fname;
129
130 fname = path + strlen(path) - 1;
131 while (fname >= path) {
132 if (*fname == '/') {
133 fname++;
134 break;
135 }
136 fname--;
137 }
138 return fname;
139}
140
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500141static char *dirname(char *path)
wdenkbe9c1cb2004-02-24 02:00:03 +0000142{
143 char *fname;
144
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000145 fname = basename(path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000146 --fname;
147 *fname = '\0';
148 return path;
149}
150
151/**************************************************************************
wdenkbe9c1cb2004-02-24 02:00:03 +0000152RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
153**************************************************************************/
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200154static uint32_t *rpc_add_credentials(uint32_t *p)
wdenkbe9c1cb2004-02-24 02:00:03 +0000155{
wdenkbe9c1cb2004-02-24 02:00:03 +0000156 /* Here's the executive summary on authentication requirements of the
157 * various NFS server implementations: Linux accepts both AUTH_NONE
158 * and AUTH_UNIX authentication (also accepts an empty hostname field
159 * in the AUTH_UNIX scheme). *BSD refuses AUTH_NONE, but accepts
160 * AUTH_UNIX (also accepts an empty hostname field in the AUTH_UNIX
161 * scheme). To be safe, use AUTH_UNIX and pass the hostname if we have
162 * it (if the BOOTP/DHCP reply didn't give one, just use an empty
163 * hostname). */
164
wdenkbe9c1cb2004-02-24 02:00:03 +0000165 /* Provide an AUTH_UNIX credential. */
166 *p++ = htonl(1); /* AUTH_UNIX */
Joe Hershberger35fd3782016-08-15 15:03:27 -0500167 *p++ = htonl(20); /* auth length */
168 *p++ = 0; /* stamp */
169 *p++ = 0; /* hostname string */
wdenkbe9c1cb2004-02-24 02:00:03 +0000170 *p++ = 0; /* uid */
171 *p++ = 0; /* gid */
172 *p++ = 0; /* auxiliary gid list */
173
174 /* Provide an AUTH_NONE verifier. */
175 *p++ = 0; /* AUTH_NONE */
176 *p++ = 0; /* auth length */
177
178 return p;
179}
180
181/**************************************************************************
182RPC_LOOKUP - Lookup RPC Port numbers
183**************************************************************************/
Joe Hershberger8481b202016-09-09 12:56:26 -0500184static void rpc_req(int rpc_prog, int rpc_proc, uint32_t *data, int datalen)
wdenkbe9c1cb2004-02-24 02:00:03 +0000185{
Joe Hershberger8481b202016-09-09 12:56:26 -0500186 struct rpc_t rpc_pkt;
wdenkbe9c1cb2004-02-24 02:00:03 +0000187 unsigned long id;
Joe Hershberger8481b202016-09-09 12:56:26 -0500188 uint32_t *p;
wdenkbe9c1cb2004-02-24 02:00:03 +0000189 int pktlen;
190 int sport;
191
wdenkacd9b102004-03-14 00:59:59 +0000192 id = ++rpc_id;
Joe Hershberger8481b202016-09-09 12:56:26 -0500193 rpc_pkt.u.call.id = htonl(id);
194 rpc_pkt.u.call.type = htonl(MSG_CALL);
195 rpc_pkt.u.call.rpcvers = htonl(2); /* use RPC version 2 */
196 rpc_pkt.u.call.prog = htonl(rpc_prog);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200197 switch (rpc_prog) {
198 case PROG_NFS:
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +0100199 switch (choosen_nfs_version) {
Thomas RIENOESSLc8075bd2023-03-10 10:51:55 +0100200 case NFS_V1:
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +0100201 case NFS_V2:
202 rpc_pkt.u.call.vers = htonl(2);
203 break;
204
205 case NFS_V3:
206 rpc_pkt.u.call.vers = htonl(3);
207 break;
208
209 case NFS_UNKOWN:
210 /* nothing to do */
211 break;
212 }
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200213 break;
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200214 case PROG_MOUNT:
Thomas RIENOESSLc8075bd2023-03-10 10:51:55 +0100215 switch (choosen_nfs_version) {
216 case NFS_V1:
217 rpc_pkt.u.call.vers = htonl(1);
218 break;
219
220 case NFS_V2:
221 rpc_pkt.u.call.vers = htonl(2);
222 break;
223
224 case NFS_V3:
225 rpc_pkt.u.call.vers = htonl(3);
226 break;
227
228 case NFS_UNKOWN:
229 /* nothing to do */
230 break;
231 }
232 break;
233 case PROG_PORTMAP:
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200234 default:
Joe Hershberger8481b202016-09-09 12:56:26 -0500235 rpc_pkt.u.call.vers = htonl(2); /* portmapper is version 2 */
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200236 }
Joe Hershberger8481b202016-09-09 12:56:26 -0500237 rpc_pkt.u.call.proc = htonl(rpc_proc);
Heinrich Schuchardt84390102019-09-02 23:55:32 +0200238 p = rpc_pkt.u.call.data;
Joe Hershberger8481b202016-09-09 12:56:26 -0500239
240 if (datalen)
Heinrich Schuchardt84390102019-09-02 23:55:32 +0200241 memcpy(p, data, datalen * sizeof(uint32_t));
wdenkbe9c1cb2004-02-24 02:00:03 +0000242
Joe Hershberger8481b202016-09-09 12:56:26 -0500243 pktlen = (char *)p + datalen * sizeof(uint32_t) - (char *)&rpc_pkt;
244
245 memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE,
246 &rpc_pkt.u.data[0], pktlen);
wdenkbe9c1cb2004-02-24 02:00:03 +0000247
248 if (rpc_prog == PROG_PORTMAP)
249 sport = SUNRPC_PORT;
250 else if (rpc_prog == PROG_MOUNT)
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500251 sport = nfs_server_mount_port;
wdenkbe9c1cb2004-02-24 02:00:03 +0000252 else
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500253 sport = nfs_server_port;
wdenkbe9c1cb2004-02-24 02:00:03 +0000254
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500255 net_send_udp_packet(net_server_ethaddr, nfs_server_ip, sport,
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500256 nfs_our_port, pktlen);
wdenkbe9c1cb2004-02-24 02:00:03 +0000257}
258
259/**************************************************************************
260RPC_LOOKUP - Lookup RPC Port numbers
261**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500262static void rpc_lookup_req(int prog, int ver)
wdenkbe9c1cb2004-02-24 02:00:03 +0000263{
Joe Hershberger8481b202016-09-09 12:56:26 -0500264 uint32_t data[16];
wdenkbe9c1cb2004-02-24 02:00:03 +0000265
266 data[0] = 0; data[1] = 0; /* auth credential */
267 data[2] = 0; data[3] = 0; /* auth verifier */
268 data[4] = htonl(prog);
269 data[5] = htonl(ver);
270 data[6] = htonl(17); /* IP_UDP */
271 data[7] = 0;
Joe Hershberger8481b202016-09-09 12:56:26 -0500272 rpc_req(PROG_PORTMAP, PORTMAP_GETPORT, data, 8);
wdenkbe9c1cb2004-02-24 02:00:03 +0000273}
274
275/**************************************************************************
276NFS_MOUNT - Mount an NFS Filesystem
277**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500278static void nfs_mount_req(char *path)
wdenkbe9c1cb2004-02-24 02:00:03 +0000279{
Joe Hershberger8481b202016-09-09 12:56:26 -0500280 uint32_t data[1024];
wdenkbe9c1cb2004-02-24 02:00:03 +0000281 uint32_t *p;
282 int len;
283 int pathlen;
284
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000285 pathlen = strlen(path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000286
Joe Hershberger8481b202016-09-09 12:56:26 -0500287 p = &(data[0]);
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200288 p = rpc_add_credentials(p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000289
290 *p++ = htonl(pathlen);
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000291 if (pathlen & 3)
292 *(p + pathlen / 4) = 0;
293 memcpy(p, path, pathlen);
wdenkbe9c1cb2004-02-24 02:00:03 +0000294 p += (pathlen + 3) / 4;
295
Joe Hershberger8481b202016-09-09 12:56:26 -0500296 len = (uint32_t *)p - (uint32_t *)&(data[0]);
wdenkbe9c1cb2004-02-24 02:00:03 +0000297
Joe Hershberger8481b202016-09-09 12:56:26 -0500298 rpc_req(PROG_MOUNT, MOUNT_ADDENTRY, data, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000299}
300
301/**************************************************************************
302NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
303**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500304static void nfs_umountall_req(void)
wdenkbe9c1cb2004-02-24 02:00:03 +0000305{
Joe Hershberger8481b202016-09-09 12:56:26 -0500306 uint32_t data[1024];
wdenkbe9c1cb2004-02-24 02:00:03 +0000307 uint32_t *p;
308 int len;
309
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500310 if ((nfs_server_mount_port == -1) || (!fs_mounted))
wdenkbe9c1cb2004-02-24 02:00:03 +0000311 /* Nothing mounted, nothing to umount */
312 return;
wdenkbe9c1cb2004-02-24 02:00:03 +0000313
Joe Hershberger8481b202016-09-09 12:56:26 -0500314 p = &(data[0]);
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200315 p = rpc_add_credentials(p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000316
Joe Hershberger8481b202016-09-09 12:56:26 -0500317 len = (uint32_t *)p - (uint32_t *)&(data[0]);
wdenkbe9c1cb2004-02-24 02:00:03 +0000318
Joe Hershberger8481b202016-09-09 12:56:26 -0500319 rpc_req(PROG_MOUNT, MOUNT_UMOUNTALL, data, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000320}
321
322/***************************************************************************
323 * NFS_READLINK (AH 2003-07-14)
324 * This procedure is called when read of the first block fails -
325 * this probably happens when it's a directory or a symlink
326 * In case of successful readlink(), the dirname is manipulated,
327 * so that inside the nfs() function a recursion can be done.
328 **************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500329static void nfs_readlink_req(void)
wdenkbe9c1cb2004-02-24 02:00:03 +0000330{
Joe Hershberger8481b202016-09-09 12:56:26 -0500331 uint32_t data[1024];
wdenkbe9c1cb2004-02-24 02:00:03 +0000332 uint32_t *p;
333 int len;
334
Joe Hershberger8481b202016-09-09 12:56:26 -0500335 p = &(data[0]);
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200336 p = rpc_add_credentials(p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000337
Thomas RIENOESSLc8075bd2023-03-10 10:51:55 +0100338 if (choosen_nfs_version != NFS_V3) {
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200339 memcpy(p, filefh, NFS_FHSIZE);
340 p += (NFS_FHSIZE / 4);
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +0100341 } else { /* NFS_V3 */
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200342 *p++ = htonl(filefh3_length);
Joe Hershbergerf59972b2016-08-15 15:03:19 -0500343 memcpy(p, filefh, filefh3_length);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200344 p += (filefh3_length / 4);
345 }
wdenkbe9c1cb2004-02-24 02:00:03 +0000346
Joe Hershberger8481b202016-09-09 12:56:26 -0500347 len = (uint32_t *)p - (uint32_t *)&(data[0]);
wdenkbe9c1cb2004-02-24 02:00:03 +0000348
Joe Hershberger8481b202016-09-09 12:56:26 -0500349 rpc_req(PROG_NFS, NFS_READLINK, data, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000350}
351
352/**************************************************************************
353NFS_LOOKUP - Lookup Pathname
354**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500355static void nfs_lookup_req(char *fname)
wdenkbe9c1cb2004-02-24 02:00:03 +0000356{
Joe Hershberger8481b202016-09-09 12:56:26 -0500357 uint32_t data[1024];
wdenkbe9c1cb2004-02-24 02:00:03 +0000358 uint32_t *p;
359 int len;
360 int fnamelen;
361
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000362 fnamelen = strlen(fname);
wdenkbe9c1cb2004-02-24 02:00:03 +0000363
Joe Hershberger8481b202016-09-09 12:56:26 -0500364 p = &(data[0]);
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200365 p = rpc_add_credentials(p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000366
Thomas RIENOESSLc8075bd2023-03-10 10:51:55 +0100367 if (choosen_nfs_version != NFS_V3) {
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200368 memcpy(p, dirfh, NFS_FHSIZE);
369 p += (NFS_FHSIZE / 4);
370 *p++ = htonl(fnamelen);
371 if (fnamelen & 3)
372 *(p + fnamelen / 4) = 0;
373 memcpy(p, fname, fnamelen);
374 p += (fnamelen + 3) / 4;
wdenkbe9c1cb2004-02-24 02:00:03 +0000375
Joe Hershberger8481b202016-09-09 12:56:26 -0500376 len = (uint32_t *)p - (uint32_t *)&(data[0]);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200377
Joe Hershberger8481b202016-09-09 12:56:26 -0500378 rpc_req(PROG_NFS, NFS_LOOKUP, data, len);
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +0100379 } else { /* NFS_V3 */
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200380 *p++ = htonl(NFS_FHSIZE); /* Dir handle length */
381 memcpy(p, dirfh, NFS_FHSIZE);
382 p += (NFS_FHSIZE / 4);
383 *p++ = htonl(fnamelen);
384 if (fnamelen & 3)
385 *(p + fnamelen / 4) = 0;
386 memcpy(p, fname, fnamelen);
387 p += (fnamelen + 3) / 4;
wdenkbe9c1cb2004-02-24 02:00:03 +0000388
Joe Hershberger8481b202016-09-09 12:56:26 -0500389 len = (uint32_t *)p - (uint32_t *)&(data[0]);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200390
Joe Hershberger8481b202016-09-09 12:56:26 -0500391 rpc_req(PROG_NFS, NFS3PROC_LOOKUP, data, len);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200392 }
wdenkbe9c1cb2004-02-24 02:00:03 +0000393}
394
395/**************************************************************************
396NFS_READ - Read File on NFS Server
397**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500398static void nfs_read_req(int offset, int readlen)
wdenkbe9c1cb2004-02-24 02:00:03 +0000399{
Joe Hershberger8481b202016-09-09 12:56:26 -0500400 uint32_t data[1024];
wdenkbe9c1cb2004-02-24 02:00:03 +0000401 uint32_t *p;
402 int len;
403
Joe Hershberger8481b202016-09-09 12:56:26 -0500404 p = &(data[0]);
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200405 p = rpc_add_credentials(p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000406
Thomas RIENOESSLc8075bd2023-03-10 10:51:55 +0100407 if (choosen_nfs_version != NFS_V3) {
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200408 memcpy(p, filefh, NFS_FHSIZE);
409 p += (NFS_FHSIZE / 4);
410 *p++ = htonl(offset);
411 *p++ = htonl(readlen);
412 *p++ = 0;
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +0100413 } else { /* NFS_V3 */
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200414 *p++ = htonl(filefh3_length);
Joe Hershbergerf59972b2016-08-15 15:03:19 -0500415 memcpy(p, filefh, filefh3_length);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200416 p += (filefh3_length / 4);
417 *p++ = htonl(0); /* offset is 64-bit long, so fill with 0 */
418 *p++ = htonl(offset);
419 *p++ = htonl(readlen);
420 *p++ = 0;
421 }
wdenkbe9c1cb2004-02-24 02:00:03 +0000422
Joe Hershberger8481b202016-09-09 12:56:26 -0500423 len = (uint32_t *)p - (uint32_t *)&(data[0]);
wdenkbe9c1cb2004-02-24 02:00:03 +0000424
Joe Hershberger8481b202016-09-09 12:56:26 -0500425 rpc_req(PROG_NFS, NFS_READ, data, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000426}
427
428/**************************************************************************
429RPC request dispatcher
430**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500431static void nfs_send(void)
wdenkbe9c1cb2004-02-24 02:00:03 +0000432{
Robin Getz9e0a4d62009-07-23 03:01:03 -0400433 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000434
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500435 switch (nfs_state) {
wdenkbe9c1cb2004-02-24 02:00:03 +0000436 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
Thomas RIENOESSLc8075bd2023-03-10 10:51:55 +0100437 if (choosen_nfs_version != NFS_V3)
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200438 rpc_lookup_req(PROG_MOUNT, 1);
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +0100439 else /* NFS_V3 */
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200440 rpc_lookup_req(PROG_MOUNT, 3);
wdenkbe9c1cb2004-02-24 02:00:03 +0000441 break;
442 case STATE_PRCLOOKUP_PROG_NFS_REQ:
Thomas RIENOESSLc8075bd2023-03-10 10:51:55 +0100443 if (choosen_nfs_version != NFS_V3)
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200444 rpc_lookup_req(PROG_NFS, 2);
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +0100445 else /* NFS_V3 */
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200446 rpc_lookup_req(PROG_NFS, 3);
wdenkbe9c1cb2004-02-24 02:00:03 +0000447 break;
448 case STATE_MOUNT_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000449 nfs_mount_req(nfs_path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000450 break;
451 case STATE_UMOUNT_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000452 nfs_umountall_req();
wdenkbe9c1cb2004-02-24 02:00:03 +0000453 break;
454 case STATE_LOOKUP_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000455 nfs_lookup_req(nfs_filename);
wdenkbe9c1cb2004-02-24 02:00:03 +0000456 break;
457 case STATE_READ_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000458 nfs_read_req(nfs_offset, nfs_len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000459 break;
460 case STATE_READLINK_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000461 nfs_readlink_req();
wdenkbe9c1cb2004-02-24 02:00:03 +0000462 break;
463 }
464}
465
466/**************************************************************************
467Handlers for the reply from server
468**************************************************************************/
469
Thomas RIENOESSLe9839f92023-03-10 10:51:53 +0100470static int rpc_handle_error(struct rpc_t *rpc_pkt)
471{
472 if (rpc_pkt->u.reply.rstatus ||
473 rpc_pkt->u.reply.verifier ||
474 rpc_pkt->u.reply.astatus ||
475 rpc_pkt->u.reply.data[0]) {
476 switch (ntohl(rpc_pkt->u.reply.astatus)) {
477 case NFS_RPC_SUCCESS: /* Not an error */
478 break;
479 case NFS_RPC_PROG_MISMATCH: {
480 /* Remote can't support NFS version */
481 const int min = ntohl(rpc_pkt->u.reply.data[0]);
482 const int max = ntohl(rpc_pkt->u.reply.data[1]);
483
Thomas RIENOESSLc8075bd2023-03-10 10:51:55 +0100484 if (max < NFS_V1 || max > NFS_V3 || min > NFS_V3) {
Thomas RIENOESSLe9839f92023-03-10 10:51:53 +0100485 puts("*** ERROR: NFS version not supported");
486 debug(": Requested: V%d, accepted: min V%d - max V%d\n",
487 choosen_nfs_version,
488 ntohl(rpc_pkt->u.reply.data[0]),
489 ntohl(rpc_pkt->u.reply.data[1]));
490 puts("\n");
491 choosen_nfs_version = NFS_UNKOWN;
492 break;
493 }
494
495 debug("*** Warning: NFS version not supported: Requested: V%d, accepted: min V%d - max V%d\n",
496 choosen_nfs_version,
497 ntohl(rpc_pkt->u.reply.data[0]),
498 ntohl(rpc_pkt->u.reply.data[1]));
499 debug("Will retry with NFSv%d\n", min);
500 choosen_nfs_version = min;
501 return -NFS_RPC_PROG_MISMATCH;
502 }
503 case NFS_RPC_PROG_UNAVAIL:
504 case NFS_RPC_PROC_UNAVAIL:
505 case NFS_RPC_GARBAGE_ARGS:
506 case NFS_RPC_SYSTEM_ERR:
507 default: /* Unknown error on 'accept state' flag */
508 debug("*** ERROR: accept state error (%d)\n",
509 ntohl(rpc_pkt->u.reply.astatus));
510 break;
511 }
512 return -1;
513 }
514
515 return 0;
516}
517
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500518static int rpc_lookup_reply(int prog, uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000519{
520 struct rpc_t rpc_pkt;
521
Joe Hershberger3393d4c2016-08-15 15:03:24 -0500522 memcpy(&rpc_pkt.u.data[0], pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000523
Robin Getz9e0a4d62009-07-23 03:01:03 -0400524 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000525
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100526 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
527 return -NFS_RPC_ERR;
528 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
529 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000530
wdenkbe9c1cb2004-02-24 02:00:03 +0000531 if (rpc_pkt.u.reply.rstatus ||
532 rpc_pkt.u.reply.verifier ||
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000533 rpc_pkt.u.reply.astatus)
wdenkacd9b102004-03-14 00:59:59 +0000534 return -1;
wdenkbe9c1cb2004-02-24 02:00:03 +0000535
536 switch (prog) {
537 case PROG_MOUNT:
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500538 nfs_server_mount_port = ntohl(rpc_pkt.u.reply.data[0]);
wdenkbe9c1cb2004-02-24 02:00:03 +0000539 break;
540 case PROG_NFS:
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500541 nfs_server_port = ntohl(rpc_pkt.u.reply.data[0]);
wdenkbe9c1cb2004-02-24 02:00:03 +0000542 break;
543 }
544
545 return 0;
546}
547
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500548static int nfs_mount_reply(uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000549{
550 struct rpc_t rpc_pkt;
Thomas RIENOESSL50097442023-03-10 10:51:54 +0100551 int ret;
wdenkbe9c1cb2004-02-24 02:00:03 +0000552
Robin Getz9e0a4d62009-07-23 03:01:03 -0400553 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000554
Joe Hershberger3393d4c2016-08-15 15:03:24 -0500555 memcpy(&rpc_pkt.u.data[0], pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000556
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100557 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
558 return -NFS_RPC_ERR;
559 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
560 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000561
Thomas RIENOESSL50097442023-03-10 10:51:54 +0100562 ret = rpc_handle_error(&rpc_pkt);
563 if (ret)
564 return ret;
wdenkbe9c1cb2004-02-24 02:00:03 +0000565
566 fs_mounted = 1;
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200567 /* NFSv2 and NFSv3 use same structure */
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000568 memcpy(dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
wdenkbe9c1cb2004-02-24 02:00:03 +0000569
570 return 0;
571}
572
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500573static int nfs_umountall_reply(uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000574{
575 struct rpc_t rpc_pkt;
576
Robin Getz9e0a4d62009-07-23 03:01:03 -0400577 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000578
Joe Hershberger3393d4c2016-08-15 15:03:24 -0500579 memcpy(&rpc_pkt.u.data[0], pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000580
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100581 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
582 return -NFS_RPC_ERR;
583 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
584 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000585
wdenkbe9c1cb2004-02-24 02:00:03 +0000586 if (rpc_pkt.u.reply.rstatus ||
587 rpc_pkt.u.reply.verifier ||
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000588 rpc_pkt.u.reply.astatus)
wdenkbe9c1cb2004-02-24 02:00:03 +0000589 return -1;
wdenkbe9c1cb2004-02-24 02:00:03 +0000590
591 fs_mounted = 0;
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000592 memset(dirfh, 0, sizeof(dirfh));
wdenkbe9c1cb2004-02-24 02:00:03 +0000593
594 return 0;
595}
596
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500597static int nfs_lookup_reply(uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000598{
599 struct rpc_t rpc_pkt;
Thomas RIENOESSLe9839f92023-03-10 10:51:53 +0100600 int ret;
wdenkbe9c1cb2004-02-24 02:00:03 +0000601
Robin Getz9e0a4d62009-07-23 03:01:03 -0400602 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000603
Joe Hershberger3393d4c2016-08-15 15:03:24 -0500604 memcpy(&rpc_pkt.u.data[0], pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000605
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100606 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
607 return -NFS_RPC_ERR;
608 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
609 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000610
Thomas RIENOESSLe9839f92023-03-10 10:51:53 +0100611 ret = rpc_handle_error(&rpc_pkt);
612 if (ret)
613 return ret;
wdenkbe9c1cb2004-02-24 02:00:03 +0000614
Thomas RIENOESSLc8075bd2023-03-10 10:51:55 +0100615 if (choosen_nfs_version != NFS_V3) {
liucheng (G)6d687912019-08-29 13:48:02 +0000616 if (((uchar *)&(rpc_pkt.u.reply.data[0]) - (uchar *)(&rpc_pkt) + NFS_FHSIZE) > len)
617 return -NFS_RPC_DROP;
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200618 memcpy(filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +0100619 } else { /* NFS_V3 */
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200620 filefh3_length = ntohl(rpc_pkt.u.reply.data[1]);
621 if (filefh3_length > NFS3_FHSIZE)
622 filefh3_length = NFS3_FHSIZE;
Joe Hershbergerf59972b2016-08-15 15:03:19 -0500623 memcpy(filefh, rpc_pkt.u.reply.data + 2, filefh3_length);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200624 }
wdenkbe9c1cb2004-02-24 02:00:03 +0000625
626 return 0;
627}
628
Joe Hershberger46e93122016-08-15 15:03:22 -0500629static int nfs3_get_attributes_offset(uint32_t *data)
630{
Heinrich Schuchardt84390102019-09-02 23:55:32 +0200631 if (data[1]) {
Joe Hershberger46e93122016-08-15 15:03:22 -0500632 /* 'attributes_follow' flag is TRUE,
Joe Hershberger10e40972016-08-15 15:03:23 -0500633 * so we have attributes on 21 dwords */
Joe Hershberger46e93122016-08-15 15:03:22 -0500634 /* Skip unused values :
635 type; 32 bits value,
636 mode; 32 bits value,
637 nlink; 32 bits value,
638 uid; 32 bits value,
639 gid; 32 bits value,
640 size; 64 bits value,
641 used; 64 bits value,
642 rdev; 64 bits value,
643 fsid; 64 bits value,
644 fileid; 64 bits value,
645 atime; 64 bits value,
646 mtime; 64 bits value,
647 ctime; 64 bits value,
648 */
649 return 22;
650 } else {
651 /* 'attributes_follow' flag is FALSE,
652 * so we don't have any attributes */
653 return 1;
654 }
655}
656
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500657static int nfs_readlink_reply(uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000658{
659 struct rpc_t rpc_pkt;
660 int rlen;
Joe Hershberger46e93122016-08-15 15:03:22 -0500661 int nfsv3_data_offset = 0;
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 Hershberger6ecfc6b2012-05-15 08:59:09 +0000665 memcpy((unsigned char *)&rpc_pkt, pkt, len);
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 ||
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000675 rpc_pkt.u.reply.data[0])
wdenkbe9c1cb2004-02-24 02:00:03 +0000676 return -1;
wdenkbe9c1cb2004-02-24 02:00:03 +0000677
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +0100678 if (choosen_nfs_version == NFS_V3) {
Joe Hershberger46e93122016-08-15 15:03:22 -0500679 nfsv3_data_offset =
680 nfs3_get_attributes_offset(rpc_pkt.u.reply.data);
681 }
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200682
Joe Hershberger46e93122016-08-15 15:03:22 -0500683 /* new path length */
684 rlen = ntohl(rpc_pkt.u.reply.data[1 + nfsv3_data_offset]);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200685
liucheng (G)3bfaca12019-08-29 13:47:54 +0000686 if (((uchar *)&(rpc_pkt.u.reply.data[0]) - (uchar *)(&rpc_pkt) + rlen) > len)
687 return -NFS_RPC_DROP;
688
Joe Hershberger46e93122016-08-15 15:03:22 -0500689 if (*((char *)&(rpc_pkt.u.reply.data[2 + nfsv3_data_offset])) != '/') {
690 int pathlen;
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200691
Joe Hershberger46e93122016-08-15 15:03:22 -0500692 strcat(nfs_path, "/");
693 pathlen = strlen(nfs_path);
694 memcpy(nfs_path + pathlen,
695 (uchar *)&(rpc_pkt.u.reply.data[2 + nfsv3_data_offset]),
696 rlen);
697 nfs_path[pathlen + rlen] = 0;
698 } else {
699 memcpy(nfs_path,
700 (uchar *)&(rpc_pkt.u.reply.data[2 + nfsv3_data_offset]),
701 rlen);
702 nfs_path[rlen] = 0;
wdenkbe9c1cb2004-02-24 02:00:03 +0000703 }
704 return 0;
705}
706
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500707static int nfs_read_reply(uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000708{
709 struct rpc_t rpc_pkt;
710 int rlen;
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200711 uchar *data_ptr;
wdenkbe9c1cb2004-02-24 02:00:03 +0000712
Robin Getz9e0a4d62009-07-23 03:01:03 -0400713 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000714
Joe Hershberger3393d4c2016-08-15 15:03:24 -0500715 memcpy(&rpc_pkt.u.data[0], pkt, sizeof(rpc_pkt.u.reply));
wdenkbe9c1cb2004-02-24 02:00:03 +0000716
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100717 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
718 return -NFS_RPC_ERR;
719 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
720 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000721
wdenkbe9c1cb2004-02-24 02:00:03 +0000722 if (rpc_pkt.u.reply.rstatus ||
723 rpc_pkt.u.reply.verifier ||
724 rpc_pkt.u.reply.astatus ||
725 rpc_pkt.u.reply.data[0]) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000726 if (rpc_pkt.u.reply.rstatus)
wdenkbe9c1cb2004-02-24 02:00:03 +0000727 return -9999;
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000728 if (rpc_pkt.u.reply.astatus)
wdenkbe9c1cb2004-02-24 02:00:03 +0000729 return -9999;
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000730 return -ntohl(rpc_pkt.u.reply.data[0]);
wdenkbe9c1cb2004-02-24 02:00:03 +0000731 }
732
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000733 if ((nfs_offset != 0) && !((nfs_offset) %
734 (NFS_READ_SIZE / 2 * 10 * HASHES_PER_LINE)))
735 puts("\n\t ");
736 if (!(nfs_offset % ((NFS_READ_SIZE / 2) * 10)))
737 putc('#');
wdenkbe9c1cb2004-02-24 02:00:03 +0000738
Thomas RIENOESSLc8075bd2023-03-10 10:51:55 +0100739 if (choosen_nfs_version != NFS_V3) {
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200740 rlen = ntohl(rpc_pkt.u.reply.data[18]);
741 data_ptr = (uchar *)&(rpc_pkt.u.reply.data[19]);
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +0100742 } else { /* NFS_V3 */
Joe Hershberger46e93122016-08-15 15:03:22 -0500743 int nfsv3_data_offset =
744 nfs3_get_attributes_offset(rpc_pkt.u.reply.data);
745
746 /* count value */
747 rlen = ntohl(rpc_pkt.u.reply.data[1 + nfsv3_data_offset]);
748 /* Skip unused values :
749 EOF: 32 bits value,
750 data_size: 32 bits value,
751 */
752 data_ptr = (uchar *)
753 &(rpc_pkt.u.reply.data[4 + nfsv3_data_offset]);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200754 }
755
liucheng (G)e837f132019-08-29 13:47:48 +0000756 if (((uchar *)&(rpc_pkt.u.reply.data[0]) - (uchar *)(&rpc_pkt) + rlen) > len)
757 return -9999;
758
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200759 if (store_block(data_ptr, nfs_offset, rlen))
760 return -9999;
wdenkbe9c1cb2004-02-24 02:00:03 +0000761
762 return rlen;
763}
764
765/**************************************************************************
766Interfaces of U-BOOT
767**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500768static void nfs_timeout_handler(void)
wdenkd9ee50d2004-09-28 21:51:42 +0000769{
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500770 if (++nfs_timeout_count > NFS_RETRY_COUNT) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000771 puts("\nRetry count exceeded; starting again\n");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500772 net_start_again();
Evan Samanasb956fba2009-11-09 20:08:36 -0600773 } else {
774 puts("T ");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500775 net_set_timeout_handler(nfs_timeout +
Tom Rini0d1c4742022-03-11 09:12:05 -0500776 nfs_timeout * nfs_timeout_count,
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500777 nfs_timeout_handler);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500778 nfs_send();
Hiroshi Ito3702cd72008-01-31 18:35:04 +0900779 }
wdenkd9ee50d2004-09-28 21:51:42 +0000780}
781
Joe Hershberger5874dec2015-04-08 01:41:01 -0500782static void nfs_handler(uchar *pkt, unsigned dest, struct in_addr sip,
783 unsigned src, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000784{
785 int rlen;
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100786 int reply;
wdenkbe9c1cb2004-02-24 02:00:03 +0000787
Robin Getz9e0a4d62009-07-23 03:01:03 -0400788 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000789
liucheng (G)761b49f2019-08-29 13:47:40 +0000790 if (len > sizeof(struct rpc_t))
791 return;
792
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500793 if (dest != nfs_our_port)
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000794 return;
wdenkbe9c1cb2004-02-24 02:00:03 +0000795
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500796 switch (nfs_state) {
wdenkbe9c1cb2004-02-24 02:00:03 +0000797 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100798 if (rpc_lookup_reply(PROG_MOUNT, pkt, len) == -NFS_RPC_DROP)
799 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500800 nfs_state = STATE_PRCLOOKUP_PROG_NFS_REQ;
801 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000802 break;
803
804 case STATE_PRCLOOKUP_PROG_NFS_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100805 if (rpc_lookup_reply(PROG_NFS, pkt, len) == -NFS_RPC_DROP)
806 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500807 nfs_state = STATE_MOUNT_REQ;
808 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000809 break;
810
811 case STATE_MOUNT_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100812 reply = nfs_mount_reply(pkt, len);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500813 if (reply == -NFS_RPC_DROP) {
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100814 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500815 } else if (reply == -NFS_RPC_ERR) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000816 puts("*** ERROR: Cannot mount\n");
wdenkbe9c1cb2004-02-24 02:00:03 +0000817 /* just to be sure... */
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500818 nfs_state = STATE_UMOUNT_REQ;
819 nfs_send();
Thomas RIENOESSL50097442023-03-10 10:51:54 +0100820 } else if (reply == -NFS_RPC_PROG_MISMATCH &&
821 choosen_nfs_version != NFS_UNKOWN) {
822 nfs_state = STATE_MOUNT_REQ;
823 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000824 } else {
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500825 nfs_state = STATE_LOOKUP_REQ;
826 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000827 }
828 break;
829
830 case STATE_UMOUNT_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100831 reply = nfs_umountall_reply(pkt, len);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500832 if (reply == -NFS_RPC_DROP) {
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100833 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500834 } else if (reply == -NFS_RPC_ERR) {
Joe Hershberger739776a2016-08-15 15:03:25 -0500835 debug("*** ERROR: Cannot umount\n");
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000836 net_set_state(NETLOOP_FAIL);
wdenkbe9c1cb2004-02-24 02:00:03 +0000837 } else {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000838 puts("\ndone\n");
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000839 net_set_state(nfs_download_state);
wdenkbe9c1cb2004-02-24 02:00:03 +0000840 }
841 break;
842
843 case STATE_LOOKUP_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100844 reply = nfs_lookup_reply(pkt, len);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500845 if (reply == -NFS_RPC_DROP) {
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100846 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500847 } else if (reply == -NFS_RPC_ERR) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000848 puts("*** ERROR: File lookup fail\n");
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500849 nfs_state = STATE_UMOUNT_REQ;
850 nfs_send();
Joe Hershberger1f466172016-08-15 15:03:21 -0500851 } else if (reply == -NFS_RPC_PROG_MISMATCH &&
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +0100852 choosen_nfs_version != NFS_UNKOWN) {
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200853 /* umount */
854 nfs_state = STATE_UMOUNT_REQ;
855 nfs_send();
856 /* And retry with another supported version */
857 nfs_state = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
858 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000859 } else {
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500860 nfs_state = STATE_READ_REQ;
wdenkbe9c1cb2004-02-24 02:00:03 +0000861 nfs_offset = 0;
862 nfs_len = NFS_READ_SIZE;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500863 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000864 }
865 break;
866
867 case STATE_READLINK_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100868 reply = nfs_readlink_reply(pkt, len);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500869 if (reply == -NFS_RPC_DROP) {
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100870 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500871 } else if (reply == -NFS_RPC_ERR) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000872 puts("*** ERROR: Symlink fail\n");
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500873 nfs_state = STATE_UMOUNT_REQ;
874 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000875 } else {
Robin Getz9e0a4d62009-07-23 03:01:03 -0400876 debug("Symlink --> %s\n", nfs_path);
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000877 nfs_filename = basename(nfs_path);
878 nfs_path = dirname(nfs_path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000879
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500880 nfs_state = STATE_MOUNT_REQ;
881 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000882 }
883 break;
884
885 case STATE_READ_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000886 rlen = nfs_read_reply(pkt, len);
Vasily Khoruzhickb32c2202018-05-14 08:34:36 -0700887 if (rlen == -NFS_RPC_DROP)
888 break;
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500889 net_set_timeout_handler(nfs_timeout, nfs_timeout_handler);
wdenkbe9c1cb2004-02-24 02:00:03 +0000890 if (rlen > 0) {
891 nfs_offset += rlen;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500892 nfs_send();
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000893 } else if ((rlen == -NFSERR_ISDIR) || (rlen == -NFSERR_INVAL)) {
wdenkbe9c1cb2004-02-24 02:00:03 +0000894 /* symbolic link */
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500895 nfs_state = STATE_READLINK_REQ;
896 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000897 } else {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000898 if (!rlen)
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000899 nfs_download_state = NETLOOP_SUCCESS;
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200900 if (rlen < 0)
Joe Hershberger739776a2016-08-15 15:03:25 -0500901 debug("NFS READ error (%d)\n", rlen);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500902 nfs_state = STATE_UMOUNT_REQ;
903 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000904 }
905 break;
906 }
907}
908
wdenkbe9c1cb2004-02-24 02:00:03 +0000909
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500910void nfs_start(void)
wdenkbe9c1cb2004-02-24 02:00:03 +0000911{
Robin Getz9e0a4d62009-07-23 03:01:03 -0400912 debug("%s\n", __func__);
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000913 nfs_download_state = NETLOOP_FAIL;
wdenkbe9c1cb2004-02-24 02:00:03 +0000914
Joe Hershberger5874dec2015-04-08 01:41:01 -0500915 nfs_server_ip = net_server_ip;
wdenkbe9c1cb2004-02-24 02:00:03 +0000916 nfs_path = (char *)nfs_path_buff;
917
918 if (nfs_path == NULL) {
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000919 net_set_state(NETLOOP_FAIL);
Joe Hershberger463624d2018-07-03 19:22:56 -0500920 printf("*** ERROR: Fail allocate memory\n");
wdenkbe9c1cb2004-02-24 02:00:03 +0000921 return;
922 }
923
Joe Hershberger9cb7b022018-07-03 19:36:43 -0500924 if (!net_parse_bootfile(&nfs_server_ip, nfs_path,
925 sizeof(nfs_path_buff))) {
Joe Hershbergera8d4f892016-08-15 14:42:16 -0500926 sprintf(nfs_path, "/nfsroot/%02X%02X%02X%02X.img",
Joe Hershberger5874dec2015-04-08 01:41:01 -0500927 net_ip.s_addr & 0xFF,
928 (net_ip.s_addr >> 8) & 0xFF,
929 (net_ip.s_addr >> 16) & 0xFF,
930 (net_ip.s_addr >> 24) & 0xFF);
wdenkbe9c1cb2004-02-24 02:00:03 +0000931
Joe Hershberger463624d2018-07-03 19:22:56 -0500932 printf("*** Warning: no boot file name; using '%s'\n",
933 nfs_path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000934 }
935
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000936 nfs_filename = basename(nfs_path);
937 nfs_path = dirname(nfs_path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000938
Joe Hershberger463624d2018-07-03 19:22:56 -0500939 printf("Using %s device\n", eth_get_name());
wdenkbe9c1cb2004-02-24 02:00:03 +0000940
Joe Hershberger463624d2018-07-03 19:22:56 -0500941 printf("File transfer via NFS from server %pI4; our IP address is %pI4",
942 &nfs_server_ip, &net_ip);
wdenkbe9c1cb2004-02-24 02:00:03 +0000943
944 /* Check if we need to send across this subnet */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500945 if (net_gateway.s_addr && net_netmask.s_addr) {
946 struct in_addr our_net;
947 struct in_addr server_net;
wdenkbe9c1cb2004-02-24 02:00:03 +0000948
Joe Hershberger5874dec2015-04-08 01:41:01 -0500949 our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
Joe Hershberger7d9c9b22018-07-03 19:22:55 -0500950 server_net.s_addr = nfs_server_ip.s_addr & net_netmask.s_addr;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500951 if (our_net.s_addr != server_net.s_addr)
Joe Hershberger463624d2018-07-03 19:22:56 -0500952 printf("; sending through gateway %pI4",
953 &net_gateway);
wdenkbe9c1cb2004-02-24 02:00:03 +0000954 }
Joe Hershberger463624d2018-07-03 19:22:56 -0500955 printf("\nFilename '%s/%s'.", nfs_path, nfs_filename);
wdenkbe9c1cb2004-02-24 02:00:03 +0000956
Joe Hershberger290c8992015-04-08 01:41:02 -0500957 if (net_boot_file_expected_size_in_blocks) {
Joe Hershberger463624d2018-07-03 19:22:56 -0500958 printf(" Size is 0x%x Bytes = ",
959 net_boot_file_expected_size_in_blocks << 9);
Joe Hershberger290c8992015-04-08 01:41:02 -0500960 print_size(net_boot_file_expected_size_in_blocks << 9, "");
wdenkbe9c1cb2004-02-24 02:00:03 +0000961 }
Simon Glass892265d2019-12-28 10:45:02 -0700962 printf("\nLoad address: 0x%lx\nLoading: *\b", image_load_addr);
wdenkbe9c1cb2004-02-24 02:00:03 +0000963
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500964 net_set_timeout_handler(nfs_timeout, nfs_timeout_handler);
Joe Hershberger5874dec2015-04-08 01:41:01 -0500965 net_set_udp_handler(nfs_handler);
wdenkbe9c1cb2004-02-24 02:00:03 +0000966
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500967 nfs_timeout_count = 0;
968 nfs_state = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
wdenkbe9c1cb2004-02-24 02:00:03 +0000969
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500970 /*nfs_our_port = 4096 + (get_ticks() % 3072);*/
wdenkbe9c1cb2004-02-24 02:00:03 +0000971 /*FIX ME !!!*/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500972 nfs_our_port = 1000;
wdenkbe9c1cb2004-02-24 02:00:03 +0000973
974 /* zero out server ether in case the server ip has changed */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500975 memset(net_server_ethaddr, 0, 6);
wdenkbe9c1cb2004-02-24 02:00:03 +0000976
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500977 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000978}