blob: c18282448ccd87bc11e09bfbdceb7e8ed9dddaf7 [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
Sébastien Szymanskifa3dc822024-03-26 10:34:31 +010060static char dirfh[NFS3_FHSIZE]; /* NFSv2 / NFSv3 file handle of directory */
61static unsigned int dirfh3_length; /* (variable) length of dirfh when NFSv3 */
Joe Hershbergerf59972b2016-08-15 15:03:19 -050062static char filefh[NFS3_FHSIZE]; /* NFSv2 / NFSv3 file handle */
Andrea zi0Black Cappaf12a47a2022-05-18 16:30:08 +000063static unsigned int filefh3_length; /* (variable) length of filefh when NFSv3 */
wdenkbe9c1cb2004-02-24 02:00:03 +000064
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000065static enum net_loop_state nfs_download_state;
Joe Hershberger5874dec2015-04-08 01:41:01 -050066static struct in_addr nfs_server_ip;
Joe Hershberger40d7ca92015-04-08 01:41:10 -050067static int nfs_server_mount_port;
68static int nfs_server_port;
69static int nfs_our_port;
70static int nfs_timeout_count;
71static int nfs_state;
wdenkbe9c1cb2004-02-24 02:00:03 +000072#define STATE_PRCLOOKUP_PROG_MOUNT_REQ 1
73#define STATE_PRCLOOKUP_PROG_NFS_REQ 2
74#define STATE_MOUNT_REQ 3
75#define STATE_UMOUNT_REQ 4
76#define STATE_LOOKUP_REQ 5
77#define STATE_READ_REQ 6
78#define STATE_READLINK_REQ 7
79
wdenkbe9c1cb2004-02-24 02:00:03 +000080static char *nfs_filename;
81static char *nfs_path;
82static char nfs_path_buff[2048];
83
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +010084enum nfs_version {
85 NFS_UNKOWN = 0,
Thomas RIENOESSLc8075bd2023-03-10 10:51:55 +010086 NFS_V1 = 1,
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +010087 NFS_V2 = 2,
88 NFS_V3 = 3,
89};
Guillaume GARDET2a4bc832016-07-29 11:31:00 +020090
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +010091static enum nfs_version choosen_nfs_version = NFS_V3;
Joe Hershberger40d7ca92015-04-08 01:41:10 -050092static inline int store_block(uchar *src, unsigned offset, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +000093{
wdenk77ef4c62004-02-24 22:33:21 +000094 ulong newsize = offset + len;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020095#ifdef CONFIG_SYS_DIRECT_FLASH_NFS
wdenkbe9c1cb2004-02-24 02:00:03 +000096 int i, rc = 0;
97
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +000098 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenkbe9c1cb2004-02-24 02:00:03 +000099 /* start address in flash? */
Simon Glass892265d2019-12-28 10:45:02 -0700100 if (image_load_addr + offset >= flash_info[i].start[0]) {
wdenkbe9c1cb2004-02-24 02:00:03 +0000101 rc = 1;
102 break;
103 }
104 }
105
106 if (rc) { /* Flash is destination for this packet */
Simon Glass892265d2019-12-28 10:45:02 -0700107 rc = flash_write((uchar *)src, (ulong)image_load_addr + offset,
108 len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000109 if (rc) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000110 flash_perror(rc);
Wolfgang Denkb077b502005-08-06 01:11:12 +0200111 return -1;
wdenkbe9c1cb2004-02-24 02:00:03 +0000112 }
113 } else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200114#endif /* CONFIG_SYS_DIRECT_FLASH_NFS */
wdenkbe9c1cb2004-02-24 02:00:03 +0000115 {
Simon Glass892265d2019-12-28 10:45:02 -0700116 void *ptr = map_sysmem(image_load_addr + offset, len);
Joe Hershbergered191852015-03-22 17:09:08 -0500117
118 memcpy(ptr, src, len);
119 unmap_sysmem(ptr);
wdenkbe9c1cb2004-02-24 02:00:03 +0000120 }
wdenk77ef4c62004-02-24 22:33:21 +0000121
Joe Hershberger290c8992015-04-08 01:41:02 -0500122 if (net_boot_file_size < (offset + len))
123 net_boot_file_size = newsize;
Wolfgang Denkb077b502005-08-06 01:11:12 +0200124 return 0;
wdenkbe9c1cb2004-02-24 02:00:03 +0000125}
126
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500127static char *basename(char *path)
wdenkbe9c1cb2004-02-24 02:00:03 +0000128{
129 char *fname;
130
131 fname = path + strlen(path) - 1;
132 while (fname >= path) {
133 if (*fname == '/') {
134 fname++;
135 break;
136 }
137 fname--;
138 }
139 return fname;
140}
141
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500142static char *dirname(char *path)
wdenkbe9c1cb2004-02-24 02:00:03 +0000143{
144 char *fname;
145
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000146 fname = basename(path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000147 --fname;
148 *fname = '\0';
149 return path;
150}
151
152/**************************************************************************
wdenkbe9c1cb2004-02-24 02:00:03 +0000153RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
154**************************************************************************/
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200155static uint32_t *rpc_add_credentials(uint32_t *p)
wdenkbe9c1cb2004-02-24 02:00:03 +0000156{
wdenkbe9c1cb2004-02-24 02:00:03 +0000157 /* Here's the executive summary on authentication requirements of the
158 * various NFS server implementations: Linux accepts both AUTH_NONE
159 * and AUTH_UNIX authentication (also accepts an empty hostname field
160 * in the AUTH_UNIX scheme). *BSD refuses AUTH_NONE, but accepts
161 * AUTH_UNIX (also accepts an empty hostname field in the AUTH_UNIX
162 * scheme). To be safe, use AUTH_UNIX and pass the hostname if we have
163 * it (if the BOOTP/DHCP reply didn't give one, just use an empty
164 * hostname). */
165
wdenkbe9c1cb2004-02-24 02:00:03 +0000166 /* Provide an AUTH_UNIX credential. */
167 *p++ = htonl(1); /* AUTH_UNIX */
Joe Hershberger35fd3782016-08-15 15:03:27 -0500168 *p++ = htonl(20); /* auth length */
169 *p++ = 0; /* stamp */
170 *p++ = 0; /* hostname string */
wdenkbe9c1cb2004-02-24 02:00:03 +0000171 *p++ = 0; /* uid */
172 *p++ = 0; /* gid */
173 *p++ = 0; /* auxiliary gid list */
174
175 /* Provide an AUTH_NONE verifier. */
176 *p++ = 0; /* AUTH_NONE */
177 *p++ = 0; /* auth length */
178
179 return p;
180}
181
182/**************************************************************************
183RPC_LOOKUP - Lookup RPC Port numbers
184**************************************************************************/
Joe Hershberger8481b202016-09-09 12:56:26 -0500185static void rpc_req(int rpc_prog, int rpc_proc, uint32_t *data, int datalen)
wdenkbe9c1cb2004-02-24 02:00:03 +0000186{
Joe Hershberger8481b202016-09-09 12:56:26 -0500187 struct rpc_t rpc_pkt;
wdenkbe9c1cb2004-02-24 02:00:03 +0000188 unsigned long id;
Joe Hershberger8481b202016-09-09 12:56:26 -0500189 uint32_t *p;
wdenkbe9c1cb2004-02-24 02:00:03 +0000190 int pktlen;
191 int sport;
192
wdenkacd9b102004-03-14 00:59:59 +0000193 id = ++rpc_id;
Joe Hershberger8481b202016-09-09 12:56:26 -0500194 rpc_pkt.u.call.id = htonl(id);
195 rpc_pkt.u.call.type = htonl(MSG_CALL);
196 rpc_pkt.u.call.rpcvers = htonl(2); /* use RPC version 2 */
197 rpc_pkt.u.call.prog = htonl(rpc_prog);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200198 switch (rpc_prog) {
199 case PROG_NFS:
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +0100200 switch (choosen_nfs_version) {
Thomas RIENOESSLc8075bd2023-03-10 10:51:55 +0100201 case NFS_V1:
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +0100202 case NFS_V2:
203 rpc_pkt.u.call.vers = htonl(2);
204 break;
205
206 case NFS_V3:
207 rpc_pkt.u.call.vers = htonl(3);
208 break;
209
210 case NFS_UNKOWN:
211 /* nothing to do */
212 break;
213 }
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200214 break;
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200215 case PROG_MOUNT:
Thomas RIENOESSLc8075bd2023-03-10 10:51:55 +0100216 switch (choosen_nfs_version) {
217 case NFS_V1:
218 rpc_pkt.u.call.vers = htonl(1);
219 break;
220
221 case NFS_V2:
222 rpc_pkt.u.call.vers = htonl(2);
223 break;
224
225 case NFS_V3:
226 rpc_pkt.u.call.vers = htonl(3);
227 break;
228
229 case NFS_UNKOWN:
230 /* nothing to do */
231 break;
232 }
233 break;
234 case PROG_PORTMAP:
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200235 default:
Joe Hershberger8481b202016-09-09 12:56:26 -0500236 rpc_pkt.u.call.vers = htonl(2); /* portmapper is version 2 */
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200237 }
Joe Hershberger8481b202016-09-09 12:56:26 -0500238 rpc_pkt.u.call.proc = htonl(rpc_proc);
Heinrich Schuchardt84390102019-09-02 23:55:32 +0200239 p = rpc_pkt.u.call.data;
Joe Hershberger8481b202016-09-09 12:56:26 -0500240
241 if (datalen)
Heinrich Schuchardt84390102019-09-02 23:55:32 +0200242 memcpy(p, data, datalen * sizeof(uint32_t));
wdenkbe9c1cb2004-02-24 02:00:03 +0000243
Joe Hershberger8481b202016-09-09 12:56:26 -0500244 pktlen = (char *)p + datalen * sizeof(uint32_t) - (char *)&rpc_pkt;
245
246 memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE,
247 &rpc_pkt.u.data[0], pktlen);
wdenkbe9c1cb2004-02-24 02:00:03 +0000248
249 if (rpc_prog == PROG_PORTMAP)
250 sport = SUNRPC_PORT;
251 else if (rpc_prog == PROG_MOUNT)
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500252 sport = nfs_server_mount_port;
wdenkbe9c1cb2004-02-24 02:00:03 +0000253 else
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500254 sport = nfs_server_port;
wdenkbe9c1cb2004-02-24 02:00:03 +0000255
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500256 net_send_udp_packet(net_server_ethaddr, nfs_server_ip, sport,
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500257 nfs_our_port, pktlen);
wdenkbe9c1cb2004-02-24 02:00:03 +0000258}
259
260/**************************************************************************
261RPC_LOOKUP - Lookup RPC Port numbers
262**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500263static void rpc_lookup_req(int prog, int ver)
wdenkbe9c1cb2004-02-24 02:00:03 +0000264{
Joe Hershberger8481b202016-09-09 12:56:26 -0500265 uint32_t data[16];
wdenkbe9c1cb2004-02-24 02:00:03 +0000266
267 data[0] = 0; data[1] = 0; /* auth credential */
268 data[2] = 0; data[3] = 0; /* auth verifier */
269 data[4] = htonl(prog);
270 data[5] = htonl(ver);
271 data[6] = htonl(17); /* IP_UDP */
272 data[7] = 0;
Joe Hershberger8481b202016-09-09 12:56:26 -0500273 rpc_req(PROG_PORTMAP, PORTMAP_GETPORT, data, 8);
wdenkbe9c1cb2004-02-24 02:00:03 +0000274}
275
276/**************************************************************************
277NFS_MOUNT - Mount an NFS Filesystem
278**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500279static void nfs_mount_req(char *path)
wdenkbe9c1cb2004-02-24 02:00:03 +0000280{
Joe Hershberger8481b202016-09-09 12:56:26 -0500281 uint32_t data[1024];
wdenkbe9c1cb2004-02-24 02:00:03 +0000282 uint32_t *p;
283 int len;
284 int pathlen;
285
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000286 pathlen = strlen(path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000287
Joe Hershberger8481b202016-09-09 12:56:26 -0500288 p = &(data[0]);
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200289 p = rpc_add_credentials(p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000290
291 *p++ = htonl(pathlen);
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000292 if (pathlen & 3)
293 *(p + pathlen / 4) = 0;
294 memcpy(p, path, pathlen);
wdenkbe9c1cb2004-02-24 02:00:03 +0000295 p += (pathlen + 3) / 4;
296
Joe Hershberger8481b202016-09-09 12:56:26 -0500297 len = (uint32_t *)p - (uint32_t *)&(data[0]);
wdenkbe9c1cb2004-02-24 02:00:03 +0000298
Joe Hershberger8481b202016-09-09 12:56:26 -0500299 rpc_req(PROG_MOUNT, MOUNT_ADDENTRY, data, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000300}
301
302/**************************************************************************
303NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
304**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500305static void nfs_umountall_req(void)
wdenkbe9c1cb2004-02-24 02:00:03 +0000306{
Joe Hershberger8481b202016-09-09 12:56:26 -0500307 uint32_t data[1024];
wdenkbe9c1cb2004-02-24 02:00:03 +0000308 uint32_t *p;
309 int len;
310
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500311 if ((nfs_server_mount_port == -1) || (!fs_mounted))
wdenkbe9c1cb2004-02-24 02:00:03 +0000312 /* Nothing mounted, nothing to umount */
313 return;
wdenkbe9c1cb2004-02-24 02:00:03 +0000314
Joe Hershberger8481b202016-09-09 12:56:26 -0500315 p = &(data[0]);
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200316 p = rpc_add_credentials(p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000317
Joe Hershberger8481b202016-09-09 12:56:26 -0500318 len = (uint32_t *)p - (uint32_t *)&(data[0]);
wdenkbe9c1cb2004-02-24 02:00:03 +0000319
Joe Hershberger8481b202016-09-09 12:56:26 -0500320 rpc_req(PROG_MOUNT, MOUNT_UMOUNTALL, data, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000321}
322
323/***************************************************************************
324 * NFS_READLINK (AH 2003-07-14)
325 * This procedure is called when read of the first block fails -
326 * this probably happens when it's a directory or a symlink
327 * In case of successful readlink(), the dirname is manipulated,
328 * so that inside the nfs() function a recursion can be done.
329 **************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500330static void nfs_readlink_req(void)
wdenkbe9c1cb2004-02-24 02:00:03 +0000331{
Joe Hershberger8481b202016-09-09 12:56:26 -0500332 uint32_t data[1024];
wdenkbe9c1cb2004-02-24 02:00:03 +0000333 uint32_t *p;
334 int len;
335
Joe Hershberger8481b202016-09-09 12:56:26 -0500336 p = &(data[0]);
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200337 p = rpc_add_credentials(p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000338
Thomas RIENOESSLc8075bd2023-03-10 10:51:55 +0100339 if (choosen_nfs_version != NFS_V3) {
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200340 memcpy(p, filefh, NFS_FHSIZE);
341 p += (NFS_FHSIZE / 4);
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +0100342 } else { /* NFS_V3 */
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200343 *p++ = htonl(filefh3_length);
Joe Hershbergerf59972b2016-08-15 15:03:19 -0500344 memcpy(p, filefh, filefh3_length);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200345 p += (filefh3_length / 4);
346 }
wdenkbe9c1cb2004-02-24 02:00:03 +0000347
Joe Hershberger8481b202016-09-09 12:56:26 -0500348 len = (uint32_t *)p - (uint32_t *)&(data[0]);
wdenkbe9c1cb2004-02-24 02:00:03 +0000349
Joe Hershberger8481b202016-09-09 12:56:26 -0500350 rpc_req(PROG_NFS, NFS_READLINK, data, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000351}
352
353/**************************************************************************
354NFS_LOOKUP - Lookup Pathname
355**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500356static void nfs_lookup_req(char *fname)
wdenkbe9c1cb2004-02-24 02:00:03 +0000357{
Joe Hershberger8481b202016-09-09 12:56:26 -0500358 uint32_t data[1024];
wdenkbe9c1cb2004-02-24 02:00:03 +0000359 uint32_t *p;
360 int len;
361 int fnamelen;
362
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000363 fnamelen = strlen(fname);
wdenkbe9c1cb2004-02-24 02:00:03 +0000364
Joe Hershberger8481b202016-09-09 12:56:26 -0500365 p = &(data[0]);
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200366 p = rpc_add_credentials(p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000367
Thomas RIENOESSLc8075bd2023-03-10 10:51:55 +0100368 if (choosen_nfs_version != NFS_V3) {
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200369 memcpy(p, dirfh, NFS_FHSIZE);
370 p += (NFS_FHSIZE / 4);
371 *p++ = htonl(fnamelen);
372 if (fnamelen & 3)
373 *(p + fnamelen / 4) = 0;
374 memcpy(p, fname, fnamelen);
375 p += (fnamelen + 3) / 4;
wdenkbe9c1cb2004-02-24 02:00:03 +0000376
Joe Hershberger8481b202016-09-09 12:56:26 -0500377 len = (uint32_t *)p - (uint32_t *)&(data[0]);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200378
Joe Hershberger8481b202016-09-09 12:56:26 -0500379 rpc_req(PROG_NFS, NFS_LOOKUP, data, len);
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +0100380 } else { /* NFS_V3 */
Sébastien Szymanskifa3dc822024-03-26 10:34:31 +0100381 *p++ = htonl(dirfh3_length); /* Dir handle length */
382 memcpy(p, dirfh, dirfh3_length);
383 p += (dirfh3_length / 4);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200384 *p++ = htonl(fnamelen);
385 if (fnamelen & 3)
386 *(p + fnamelen / 4) = 0;
387 memcpy(p, fname, fnamelen);
388 p += (fnamelen + 3) / 4;
wdenkbe9c1cb2004-02-24 02:00:03 +0000389
Joe Hershberger8481b202016-09-09 12:56:26 -0500390 len = (uint32_t *)p - (uint32_t *)&(data[0]);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200391
Joe Hershberger8481b202016-09-09 12:56:26 -0500392 rpc_req(PROG_NFS, NFS3PROC_LOOKUP, data, len);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200393 }
wdenkbe9c1cb2004-02-24 02:00:03 +0000394}
395
396/**************************************************************************
397NFS_READ - Read File on NFS Server
398**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500399static void nfs_read_req(int offset, int readlen)
wdenkbe9c1cb2004-02-24 02:00:03 +0000400{
Joe Hershberger8481b202016-09-09 12:56:26 -0500401 uint32_t data[1024];
wdenkbe9c1cb2004-02-24 02:00:03 +0000402 uint32_t *p;
403 int len;
404
Joe Hershberger8481b202016-09-09 12:56:26 -0500405 p = &(data[0]);
Ralf Hubert2f8641c2016-07-01 13:19:51 +0200406 p = rpc_add_credentials(p);
wdenkbe9c1cb2004-02-24 02:00:03 +0000407
Thomas RIENOESSLc8075bd2023-03-10 10:51:55 +0100408 if (choosen_nfs_version != NFS_V3) {
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200409 memcpy(p, filefh, NFS_FHSIZE);
410 p += (NFS_FHSIZE / 4);
411 *p++ = htonl(offset);
412 *p++ = htonl(readlen);
413 *p++ = 0;
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +0100414 } else { /* NFS_V3 */
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200415 *p++ = htonl(filefh3_length);
Joe Hershbergerf59972b2016-08-15 15:03:19 -0500416 memcpy(p, filefh, filefh3_length);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200417 p += (filefh3_length / 4);
418 *p++ = htonl(0); /* offset is 64-bit long, so fill with 0 */
419 *p++ = htonl(offset);
420 *p++ = htonl(readlen);
421 *p++ = 0;
422 }
wdenkbe9c1cb2004-02-24 02:00:03 +0000423
Joe Hershberger8481b202016-09-09 12:56:26 -0500424 len = (uint32_t *)p - (uint32_t *)&(data[0]);
wdenkbe9c1cb2004-02-24 02:00:03 +0000425
Joe Hershberger8481b202016-09-09 12:56:26 -0500426 rpc_req(PROG_NFS, NFS_READ, data, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000427}
428
429/**************************************************************************
430RPC request dispatcher
431**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500432static void nfs_send(void)
wdenkbe9c1cb2004-02-24 02:00:03 +0000433{
Robin Getz9e0a4d62009-07-23 03:01:03 -0400434 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000435
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500436 switch (nfs_state) {
wdenkbe9c1cb2004-02-24 02:00:03 +0000437 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
Thomas RIENOESSLc8075bd2023-03-10 10:51:55 +0100438 if (choosen_nfs_version != NFS_V3)
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200439 rpc_lookup_req(PROG_MOUNT, 1);
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +0100440 else /* NFS_V3 */
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200441 rpc_lookup_req(PROG_MOUNT, 3);
wdenkbe9c1cb2004-02-24 02:00:03 +0000442 break;
443 case STATE_PRCLOOKUP_PROG_NFS_REQ:
Thomas RIENOESSLc8075bd2023-03-10 10:51:55 +0100444 if (choosen_nfs_version != NFS_V3)
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200445 rpc_lookup_req(PROG_NFS, 2);
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +0100446 else /* NFS_V3 */
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200447 rpc_lookup_req(PROG_NFS, 3);
wdenkbe9c1cb2004-02-24 02:00:03 +0000448 break;
449 case STATE_MOUNT_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000450 nfs_mount_req(nfs_path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000451 break;
452 case STATE_UMOUNT_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000453 nfs_umountall_req();
wdenkbe9c1cb2004-02-24 02:00:03 +0000454 break;
455 case STATE_LOOKUP_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000456 nfs_lookup_req(nfs_filename);
wdenkbe9c1cb2004-02-24 02:00:03 +0000457 break;
458 case STATE_READ_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000459 nfs_read_req(nfs_offset, nfs_len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000460 break;
461 case STATE_READLINK_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000462 nfs_readlink_req();
wdenkbe9c1cb2004-02-24 02:00:03 +0000463 break;
464 }
465}
466
467/**************************************************************************
468Handlers for the reply from server
469**************************************************************************/
470
Thomas RIENOESSLe9839f92023-03-10 10:51:53 +0100471static int rpc_handle_error(struct rpc_t *rpc_pkt)
472{
473 if (rpc_pkt->u.reply.rstatus ||
474 rpc_pkt->u.reply.verifier ||
475 rpc_pkt->u.reply.astatus ||
476 rpc_pkt->u.reply.data[0]) {
477 switch (ntohl(rpc_pkt->u.reply.astatus)) {
478 case NFS_RPC_SUCCESS: /* Not an error */
479 break;
480 case NFS_RPC_PROG_MISMATCH: {
481 /* Remote can't support NFS version */
482 const int min = ntohl(rpc_pkt->u.reply.data[0]);
483 const int max = ntohl(rpc_pkt->u.reply.data[1]);
484
Thomas RIENOESSLc8075bd2023-03-10 10:51:55 +0100485 if (max < NFS_V1 || max > NFS_V3 || min > NFS_V3) {
Thomas RIENOESSLe9839f92023-03-10 10:51:53 +0100486 puts("*** ERROR: NFS version not supported");
487 debug(": Requested: V%d, accepted: min V%d - max V%d\n",
488 choosen_nfs_version,
489 ntohl(rpc_pkt->u.reply.data[0]),
490 ntohl(rpc_pkt->u.reply.data[1]));
491 puts("\n");
492 choosen_nfs_version = NFS_UNKOWN;
493 break;
494 }
495
496 debug("*** Warning: NFS version not supported: Requested: V%d, accepted: min V%d - max V%d\n",
497 choosen_nfs_version,
498 ntohl(rpc_pkt->u.reply.data[0]),
499 ntohl(rpc_pkt->u.reply.data[1]));
500 debug("Will retry with NFSv%d\n", min);
501 choosen_nfs_version = min;
502 return -NFS_RPC_PROG_MISMATCH;
503 }
504 case NFS_RPC_PROG_UNAVAIL:
505 case NFS_RPC_PROC_UNAVAIL:
506 case NFS_RPC_GARBAGE_ARGS:
507 case NFS_RPC_SYSTEM_ERR:
508 default: /* Unknown error on 'accept state' flag */
509 debug("*** ERROR: accept state error (%d)\n",
510 ntohl(rpc_pkt->u.reply.astatus));
511 break;
512 }
513 return -1;
514 }
515
516 return 0;
517}
518
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500519static int rpc_lookup_reply(int prog, uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000520{
521 struct rpc_t rpc_pkt;
522
Joe Hershberger3393d4c2016-08-15 15:03:24 -0500523 memcpy(&rpc_pkt.u.data[0], pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000524
Robin Getz9e0a4d62009-07-23 03:01:03 -0400525 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000526
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100527 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
528 return -NFS_RPC_ERR;
529 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
530 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000531
wdenkbe9c1cb2004-02-24 02:00:03 +0000532 if (rpc_pkt.u.reply.rstatus ||
533 rpc_pkt.u.reply.verifier ||
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000534 rpc_pkt.u.reply.astatus)
wdenkacd9b102004-03-14 00:59:59 +0000535 return -1;
wdenkbe9c1cb2004-02-24 02:00:03 +0000536
537 switch (prog) {
538 case PROG_MOUNT:
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500539 nfs_server_mount_port = ntohl(rpc_pkt.u.reply.data[0]);
wdenkbe9c1cb2004-02-24 02:00:03 +0000540 break;
541 case PROG_NFS:
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500542 nfs_server_port = ntohl(rpc_pkt.u.reply.data[0]);
wdenkbe9c1cb2004-02-24 02:00:03 +0000543 break;
544 }
545
546 return 0;
547}
548
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500549static int nfs_mount_reply(uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000550{
551 struct rpc_t rpc_pkt;
Thomas RIENOESSL50097442023-03-10 10:51:54 +0100552 int ret;
wdenkbe9c1cb2004-02-24 02:00:03 +0000553
Robin Getz9e0a4d62009-07-23 03:01:03 -0400554 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000555
Joe Hershberger3393d4c2016-08-15 15:03:24 -0500556 memcpy(&rpc_pkt.u.data[0], pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000557
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100558 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
559 return -NFS_RPC_ERR;
560 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
561 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000562
Thomas RIENOESSL50097442023-03-10 10:51:54 +0100563 ret = rpc_handle_error(&rpc_pkt);
564 if (ret)
565 return ret;
wdenkbe9c1cb2004-02-24 02:00:03 +0000566
567 fs_mounted = 1;
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200568 /* NFSv2 and NFSv3 use same structure */
Sébastien Szymanskifa3dc822024-03-26 10:34:31 +0100569 if (choosen_nfs_version != NFS_V3) {
570 memcpy(dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
571 } else {
572 dirfh3_length = ntohl(rpc_pkt.u.reply.data[1]);
573 if (dirfh3_length > NFS3_FHSIZE)
574 dirfh3_length = NFS3_FHSIZE;
575 memcpy(dirfh, rpc_pkt.u.reply.data + 2, dirfh3_length);
576 }
wdenkbe9c1cb2004-02-24 02:00:03 +0000577
578 return 0;
579}
580
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500581static int nfs_umountall_reply(uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000582{
583 struct rpc_t rpc_pkt;
584
Robin Getz9e0a4d62009-07-23 03:01:03 -0400585 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000586
Joe Hershberger3393d4c2016-08-15 15:03:24 -0500587 memcpy(&rpc_pkt.u.data[0], pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000588
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100589 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
590 return -NFS_RPC_ERR;
591 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
592 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000593
wdenkbe9c1cb2004-02-24 02:00:03 +0000594 if (rpc_pkt.u.reply.rstatus ||
595 rpc_pkt.u.reply.verifier ||
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000596 rpc_pkt.u.reply.astatus)
wdenkbe9c1cb2004-02-24 02:00:03 +0000597 return -1;
wdenkbe9c1cb2004-02-24 02:00:03 +0000598
599 fs_mounted = 0;
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000600 memset(dirfh, 0, sizeof(dirfh));
wdenkbe9c1cb2004-02-24 02:00:03 +0000601
602 return 0;
603}
604
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500605static int nfs_lookup_reply(uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000606{
607 struct rpc_t rpc_pkt;
Thomas RIENOESSLe9839f92023-03-10 10:51:53 +0100608 int ret;
wdenkbe9c1cb2004-02-24 02:00:03 +0000609
Robin Getz9e0a4d62009-07-23 03:01:03 -0400610 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000611
Joe Hershberger3393d4c2016-08-15 15:03:24 -0500612 memcpy(&rpc_pkt.u.data[0], pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000613
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100614 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
615 return -NFS_RPC_ERR;
616 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
617 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000618
Thomas RIENOESSLe9839f92023-03-10 10:51:53 +0100619 ret = rpc_handle_error(&rpc_pkt);
620 if (ret)
621 return ret;
wdenkbe9c1cb2004-02-24 02:00:03 +0000622
Thomas RIENOESSLc8075bd2023-03-10 10:51:55 +0100623 if (choosen_nfs_version != NFS_V3) {
liucheng (G)6d687912019-08-29 13:48:02 +0000624 if (((uchar *)&(rpc_pkt.u.reply.data[0]) - (uchar *)(&rpc_pkt) + NFS_FHSIZE) > len)
625 return -NFS_RPC_DROP;
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200626 memcpy(filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +0100627 } else { /* NFS_V3 */
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200628 filefh3_length = ntohl(rpc_pkt.u.reply.data[1]);
629 if (filefh3_length > NFS3_FHSIZE)
630 filefh3_length = NFS3_FHSIZE;
Joe Hershbergerf59972b2016-08-15 15:03:19 -0500631 memcpy(filefh, rpc_pkt.u.reply.data + 2, filefh3_length);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200632 }
wdenkbe9c1cb2004-02-24 02:00:03 +0000633
634 return 0;
635}
636
Joe Hershberger46e93122016-08-15 15:03:22 -0500637static int nfs3_get_attributes_offset(uint32_t *data)
638{
Heinrich Schuchardt84390102019-09-02 23:55:32 +0200639 if (data[1]) {
Joe Hershberger46e93122016-08-15 15:03:22 -0500640 /* 'attributes_follow' flag is TRUE,
Joe Hershberger10e40972016-08-15 15:03:23 -0500641 * so we have attributes on 21 dwords */
Joe Hershberger46e93122016-08-15 15:03:22 -0500642 /* Skip unused values :
643 type; 32 bits value,
644 mode; 32 bits value,
645 nlink; 32 bits value,
646 uid; 32 bits value,
647 gid; 32 bits value,
648 size; 64 bits value,
649 used; 64 bits value,
650 rdev; 64 bits value,
651 fsid; 64 bits value,
652 fileid; 64 bits value,
653 atime; 64 bits value,
654 mtime; 64 bits value,
655 ctime; 64 bits value,
656 */
657 return 22;
658 } else {
659 /* 'attributes_follow' flag is FALSE,
660 * so we don't have any attributes */
661 return 1;
662 }
663}
664
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500665static int nfs_readlink_reply(uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000666{
667 struct rpc_t rpc_pkt;
668 int rlen;
Joe Hershberger46e93122016-08-15 15:03:22 -0500669 int nfsv3_data_offset = 0;
wdenkbe9c1cb2004-02-24 02:00:03 +0000670
Robin Getz9e0a4d62009-07-23 03:01:03 -0400671 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000672
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000673 memcpy((unsigned char *)&rpc_pkt, pkt, len);
wdenkbe9c1cb2004-02-24 02:00:03 +0000674
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100675 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
676 return -NFS_RPC_ERR;
677 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
678 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000679
wdenkbe9c1cb2004-02-24 02:00:03 +0000680 if (rpc_pkt.u.reply.rstatus ||
681 rpc_pkt.u.reply.verifier ||
682 rpc_pkt.u.reply.astatus ||
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000683 rpc_pkt.u.reply.data[0])
wdenkbe9c1cb2004-02-24 02:00:03 +0000684 return -1;
wdenkbe9c1cb2004-02-24 02:00:03 +0000685
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +0100686 if (choosen_nfs_version == NFS_V3) {
Joe Hershberger46e93122016-08-15 15:03:22 -0500687 nfsv3_data_offset =
688 nfs3_get_attributes_offset(rpc_pkt.u.reply.data);
689 }
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200690
Joe Hershberger46e93122016-08-15 15:03:22 -0500691 /* new path length */
692 rlen = ntohl(rpc_pkt.u.reply.data[1 + nfsv3_data_offset]);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200693
liucheng (G)3bfaca12019-08-29 13:47:54 +0000694 if (((uchar *)&(rpc_pkt.u.reply.data[0]) - (uchar *)(&rpc_pkt) + rlen) > len)
695 return -NFS_RPC_DROP;
696
Joe Hershberger46e93122016-08-15 15:03:22 -0500697 if (*((char *)&(rpc_pkt.u.reply.data[2 + nfsv3_data_offset])) != '/') {
698 int pathlen;
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200699
Joe Hershberger46e93122016-08-15 15:03:22 -0500700 strcat(nfs_path, "/");
701 pathlen = strlen(nfs_path);
702 memcpy(nfs_path + pathlen,
703 (uchar *)&(rpc_pkt.u.reply.data[2 + nfsv3_data_offset]),
704 rlen);
705 nfs_path[pathlen + rlen] = 0;
706 } else {
707 memcpy(nfs_path,
708 (uchar *)&(rpc_pkt.u.reply.data[2 + nfsv3_data_offset]),
709 rlen);
710 nfs_path[rlen] = 0;
wdenkbe9c1cb2004-02-24 02:00:03 +0000711 }
712 return 0;
713}
714
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500715static int nfs_read_reply(uchar *pkt, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000716{
717 struct rpc_t rpc_pkt;
718 int rlen;
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200719 uchar *data_ptr;
wdenkbe9c1cb2004-02-24 02:00:03 +0000720
Robin Getz9e0a4d62009-07-23 03:01:03 -0400721 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000722
Joe Hershberger3393d4c2016-08-15 15:03:24 -0500723 memcpy(&rpc_pkt.u.data[0], pkt, sizeof(rpc_pkt.u.reply));
wdenkbe9c1cb2004-02-24 02:00:03 +0000724
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100725 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
726 return -NFS_RPC_ERR;
727 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
728 return -NFS_RPC_DROP;
wdenkacd9b102004-03-14 00:59:59 +0000729
wdenkbe9c1cb2004-02-24 02:00:03 +0000730 if (rpc_pkt.u.reply.rstatus ||
731 rpc_pkt.u.reply.verifier ||
732 rpc_pkt.u.reply.astatus ||
733 rpc_pkt.u.reply.data[0]) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000734 if (rpc_pkt.u.reply.rstatus)
wdenkbe9c1cb2004-02-24 02:00:03 +0000735 return -9999;
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000736 if (rpc_pkt.u.reply.astatus)
wdenkbe9c1cb2004-02-24 02:00:03 +0000737 return -9999;
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000738 return -ntohl(rpc_pkt.u.reply.data[0]);
wdenkbe9c1cb2004-02-24 02:00:03 +0000739 }
740
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000741 if ((nfs_offset != 0) && !((nfs_offset) %
742 (NFS_READ_SIZE / 2 * 10 * HASHES_PER_LINE)))
743 puts("\n\t ");
744 if (!(nfs_offset % ((NFS_READ_SIZE / 2) * 10)))
745 putc('#');
wdenkbe9c1cb2004-02-24 02:00:03 +0000746
Thomas RIENOESSLc8075bd2023-03-10 10:51:55 +0100747 if (choosen_nfs_version != NFS_V3) {
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200748 rlen = ntohl(rpc_pkt.u.reply.data[18]);
749 data_ptr = (uchar *)&(rpc_pkt.u.reply.data[19]);
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +0100750 } else { /* NFS_V3 */
Joe Hershberger46e93122016-08-15 15:03:22 -0500751 int nfsv3_data_offset =
752 nfs3_get_attributes_offset(rpc_pkt.u.reply.data);
753
754 /* count value */
755 rlen = ntohl(rpc_pkt.u.reply.data[1 + nfsv3_data_offset]);
756 /* Skip unused values :
757 EOF: 32 bits value,
758 data_size: 32 bits value,
759 */
760 data_ptr = (uchar *)
761 &(rpc_pkt.u.reply.data[4 + nfsv3_data_offset]);
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200762 }
763
liucheng (G)e837f132019-08-29 13:47:48 +0000764 if (((uchar *)&(rpc_pkt.u.reply.data[0]) - (uchar *)(&rpc_pkt) + rlen) > len)
765 return -9999;
766
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200767 if (store_block(data_ptr, nfs_offset, rlen))
768 return -9999;
wdenkbe9c1cb2004-02-24 02:00:03 +0000769
770 return rlen;
771}
772
773/**************************************************************************
774Interfaces of U-BOOT
775**************************************************************************/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500776static void nfs_timeout_handler(void)
wdenkd9ee50d2004-09-28 21:51:42 +0000777{
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500778 if (++nfs_timeout_count > NFS_RETRY_COUNT) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000779 puts("\nRetry count exceeded; starting again\n");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500780 net_start_again();
Evan Samanasb956fba2009-11-09 20:08:36 -0600781 } else {
782 puts("T ");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500783 net_set_timeout_handler(nfs_timeout +
Tom Rini0d1c4742022-03-11 09:12:05 -0500784 nfs_timeout * nfs_timeout_count,
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500785 nfs_timeout_handler);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500786 nfs_send();
Hiroshi Ito3702cd72008-01-31 18:35:04 +0900787 }
wdenkd9ee50d2004-09-28 21:51:42 +0000788}
789
Joe Hershberger5874dec2015-04-08 01:41:01 -0500790static void nfs_handler(uchar *pkt, unsigned dest, struct in_addr sip,
791 unsigned src, unsigned len)
wdenkbe9c1cb2004-02-24 02:00:03 +0000792{
793 int rlen;
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100794 int reply;
wdenkbe9c1cb2004-02-24 02:00:03 +0000795
Robin Getz9e0a4d62009-07-23 03:01:03 -0400796 debug("%s\n", __func__);
wdenkbe9c1cb2004-02-24 02:00:03 +0000797
liucheng (G)761b49f2019-08-29 13:47:40 +0000798 if (len > sizeof(struct rpc_t))
799 return;
800
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500801 if (dest != nfs_our_port)
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000802 return;
wdenkbe9c1cb2004-02-24 02:00:03 +0000803
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500804 switch (nfs_state) {
wdenkbe9c1cb2004-02-24 02:00:03 +0000805 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100806 if (rpc_lookup_reply(PROG_MOUNT, pkt, len) == -NFS_RPC_DROP)
807 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500808 nfs_state = STATE_PRCLOOKUP_PROG_NFS_REQ;
809 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000810 break;
811
812 case STATE_PRCLOOKUP_PROG_NFS_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100813 if (rpc_lookup_reply(PROG_NFS, pkt, len) == -NFS_RPC_DROP)
814 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500815 nfs_state = STATE_MOUNT_REQ;
816 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000817 break;
818
819 case STATE_MOUNT_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100820 reply = nfs_mount_reply(pkt, len);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500821 if (reply == -NFS_RPC_DROP) {
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100822 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500823 } else if (reply == -NFS_RPC_ERR) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000824 puts("*** ERROR: Cannot mount\n");
wdenkbe9c1cb2004-02-24 02:00:03 +0000825 /* just to be sure... */
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500826 nfs_state = STATE_UMOUNT_REQ;
827 nfs_send();
Thomas RIENOESSL50097442023-03-10 10:51:54 +0100828 } else if (reply == -NFS_RPC_PROG_MISMATCH &&
829 choosen_nfs_version != NFS_UNKOWN) {
830 nfs_state = STATE_MOUNT_REQ;
831 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000832 } else {
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500833 nfs_state = STATE_LOOKUP_REQ;
834 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000835 }
836 break;
837
838 case STATE_UMOUNT_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100839 reply = nfs_umountall_reply(pkt, len);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500840 if (reply == -NFS_RPC_DROP) {
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100841 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500842 } else if (reply == -NFS_RPC_ERR) {
Joe Hershberger739776a2016-08-15 15:03:25 -0500843 debug("*** ERROR: Cannot umount\n");
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000844 net_set_state(NETLOOP_FAIL);
wdenkbe9c1cb2004-02-24 02:00:03 +0000845 } else {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000846 puts("\ndone\n");
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000847 net_set_state(nfs_download_state);
wdenkbe9c1cb2004-02-24 02:00:03 +0000848 }
849 break;
850
851 case STATE_LOOKUP_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100852 reply = nfs_lookup_reply(pkt, len);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500853 if (reply == -NFS_RPC_DROP) {
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100854 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500855 } else if (reply == -NFS_RPC_ERR) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000856 puts("*** ERROR: File lookup fail\n");
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500857 nfs_state = STATE_UMOUNT_REQ;
858 nfs_send();
Joe Hershberger1f466172016-08-15 15:03:21 -0500859 } else if (reply == -NFS_RPC_PROG_MISMATCH &&
Thomas RIENOESSLc1bec0d2023-03-10 10:51:52 +0100860 choosen_nfs_version != NFS_UNKOWN) {
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200861 /* umount */
862 nfs_state = STATE_UMOUNT_REQ;
863 nfs_send();
864 /* And retry with another supported version */
865 nfs_state = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
866 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000867 } else {
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500868 nfs_state = STATE_READ_REQ;
wdenkbe9c1cb2004-02-24 02:00:03 +0000869 nfs_offset = 0;
870 nfs_len = NFS_READ_SIZE;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500871 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000872 }
873 break;
874
875 case STATE_READLINK_REQ:
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100876 reply = nfs_readlink_reply(pkt, len);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500877 if (reply == -NFS_RPC_DROP) {
Matthias Bruggerb8d5b8c2012-12-11 19:14:16 +0100878 break;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500879 } else if (reply == -NFS_RPC_ERR) {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000880 puts("*** ERROR: Symlink fail\n");
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500881 nfs_state = STATE_UMOUNT_REQ;
882 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000883 } else {
Robin Getz9e0a4d62009-07-23 03:01:03 -0400884 debug("Symlink --> %s\n", nfs_path);
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 Hershberger40d7ca92015-04-08 01:41:10 -0500888 nfs_state = STATE_MOUNT_REQ;
889 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000890 }
891 break;
892
893 case STATE_READ_REQ:
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000894 rlen = nfs_read_reply(pkt, len);
Vasily Khoruzhickb32c2202018-05-14 08:34:36 -0700895 if (rlen == -NFS_RPC_DROP)
896 break;
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500897 net_set_timeout_handler(nfs_timeout, nfs_timeout_handler);
wdenkbe9c1cb2004-02-24 02:00:03 +0000898 if (rlen > 0) {
899 nfs_offset += rlen;
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500900 nfs_send();
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000901 } else if ((rlen == -NFSERR_ISDIR) || (rlen == -NFSERR_INVAL)) {
wdenkbe9c1cb2004-02-24 02:00:03 +0000902 /* symbolic link */
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500903 nfs_state = STATE_READLINK_REQ;
904 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000905 } else {
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000906 if (!rlen)
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000907 nfs_download_state = NETLOOP_SUCCESS;
Guillaume GARDET2a4bc832016-07-29 11:31:00 +0200908 if (rlen < 0)
Joe Hershberger739776a2016-08-15 15:03:25 -0500909 debug("NFS READ error (%d)\n", rlen);
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500910 nfs_state = STATE_UMOUNT_REQ;
911 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000912 }
913 break;
914 }
915}
916
wdenkbe9c1cb2004-02-24 02:00:03 +0000917
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500918void nfs_start(void)
wdenkbe9c1cb2004-02-24 02:00:03 +0000919{
Robin Getz9e0a4d62009-07-23 03:01:03 -0400920 debug("%s\n", __func__);
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000921 nfs_download_state = NETLOOP_FAIL;
wdenkbe9c1cb2004-02-24 02:00:03 +0000922
Joe Hershberger5874dec2015-04-08 01:41:01 -0500923 nfs_server_ip = net_server_ip;
wdenkbe9c1cb2004-02-24 02:00:03 +0000924 nfs_path = (char *)nfs_path_buff;
925
926 if (nfs_path == NULL) {
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000927 net_set_state(NETLOOP_FAIL);
Joe Hershberger463624d2018-07-03 19:22:56 -0500928 printf("*** ERROR: Fail allocate memory\n");
wdenkbe9c1cb2004-02-24 02:00:03 +0000929 return;
930 }
931
Joe Hershberger9cb7b022018-07-03 19:36:43 -0500932 if (!net_parse_bootfile(&nfs_server_ip, nfs_path,
933 sizeof(nfs_path_buff))) {
Joe Hershbergera8d4f892016-08-15 14:42:16 -0500934 sprintf(nfs_path, "/nfsroot/%02X%02X%02X%02X.img",
Joe Hershberger5874dec2015-04-08 01:41:01 -0500935 net_ip.s_addr & 0xFF,
936 (net_ip.s_addr >> 8) & 0xFF,
937 (net_ip.s_addr >> 16) & 0xFF,
938 (net_ip.s_addr >> 24) & 0xFF);
wdenkbe9c1cb2004-02-24 02:00:03 +0000939
Joe Hershberger463624d2018-07-03 19:22:56 -0500940 printf("*** Warning: no boot file name; using '%s'\n",
941 nfs_path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000942 }
943
Joe Hershberger6ecfc6b2012-05-15 08:59:09 +0000944 nfs_filename = basename(nfs_path);
945 nfs_path = dirname(nfs_path);
wdenkbe9c1cb2004-02-24 02:00:03 +0000946
Joe Hershberger463624d2018-07-03 19:22:56 -0500947 printf("Using %s device\n", eth_get_name());
wdenkbe9c1cb2004-02-24 02:00:03 +0000948
Joe Hershberger463624d2018-07-03 19:22:56 -0500949 printf("File transfer via NFS from server %pI4; our IP address is %pI4",
950 &nfs_server_ip, &net_ip);
wdenkbe9c1cb2004-02-24 02:00:03 +0000951
952 /* Check if we need to send across this subnet */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500953 if (net_gateway.s_addr && net_netmask.s_addr) {
954 struct in_addr our_net;
955 struct in_addr server_net;
wdenkbe9c1cb2004-02-24 02:00:03 +0000956
Joe Hershberger5874dec2015-04-08 01:41:01 -0500957 our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
Joe Hershberger7d9c9b22018-07-03 19:22:55 -0500958 server_net.s_addr = nfs_server_ip.s_addr & net_netmask.s_addr;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500959 if (our_net.s_addr != server_net.s_addr)
Joe Hershberger463624d2018-07-03 19:22:56 -0500960 printf("; sending through gateway %pI4",
961 &net_gateway);
wdenkbe9c1cb2004-02-24 02:00:03 +0000962 }
Joe Hershberger463624d2018-07-03 19:22:56 -0500963 printf("\nFilename '%s/%s'.", nfs_path, nfs_filename);
wdenkbe9c1cb2004-02-24 02:00:03 +0000964
Joe Hershberger290c8992015-04-08 01:41:02 -0500965 if (net_boot_file_expected_size_in_blocks) {
Joe Hershberger463624d2018-07-03 19:22:56 -0500966 printf(" Size is 0x%x Bytes = ",
967 net_boot_file_expected_size_in_blocks << 9);
Joe Hershberger290c8992015-04-08 01:41:02 -0500968 print_size(net_boot_file_expected_size_in_blocks << 9, "");
wdenkbe9c1cb2004-02-24 02:00:03 +0000969 }
Simon Glass892265d2019-12-28 10:45:02 -0700970 printf("\nLoad address: 0x%lx\nLoading: *\b", image_load_addr);
wdenkbe9c1cb2004-02-24 02:00:03 +0000971
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500972 net_set_timeout_handler(nfs_timeout, nfs_timeout_handler);
Joe Hershberger5874dec2015-04-08 01:41:01 -0500973 net_set_udp_handler(nfs_handler);
wdenkbe9c1cb2004-02-24 02:00:03 +0000974
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500975 nfs_timeout_count = 0;
976 nfs_state = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
wdenkbe9c1cb2004-02-24 02:00:03 +0000977
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500978 /*nfs_our_port = 4096 + (get_ticks() % 3072);*/
wdenkbe9c1cb2004-02-24 02:00:03 +0000979 /*FIX ME !!!*/
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500980 nfs_our_port = 1000;
wdenkbe9c1cb2004-02-24 02:00:03 +0000981
982 /* zero out server ether in case the server ip has changed */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500983 memset(net_server_ethaddr, 0, 6);
wdenkbe9c1cb2004-02-24 02:00:03 +0000984
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500985 nfs_send();
wdenkbe9c1cb2004-02-24 02:00:03 +0000986}