]> Git Repo - u-boot.git/blame - net/nfs.c
Revert "Merge patch series "arm: dts: am62-beagleplay: Fix Beagleplay Ethernet""
[u-boot.git] / net / nfs.c
CommitLineData
cbd8a35c
WD
1/*
2 * NFS support driver - based on etherboot and U-BOOT's tftp.c
3 *
4 * Masami Komiya <[email protected]> 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
b0baca98
GG
25/* 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
e4bd95bb
TR
29/* 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
d678a59d 33#include <common.h>
cbd8a35c 34#include <command.h>
4e4bf944 35#include <display_options.h>
17ead040 36#ifdef CONFIG_SYS_DIRECT_FLASH_NFS
0ee48252 37#include <flash.h>
17ead040 38#endif
8e8ccfe1 39#include <image.h>
f7ae49fc 40#include <log.h>
cbd8a35c
WD
41#include <net.h>
42#include <malloc.h>
55d5fd9a 43#include <mapmem.h>
cbd8a35c
WD
44#include "nfs.h"
45#include "bootp.h"
1045315d 46#include <time.h>
cbd8a35c 47
cbd8a35c 48#define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
fe891ecf 49#define NFS_RETRY_COUNT 30
cbd8a35c 50
fa84fa70
MB
51#define NFS_RPC_ERR 1
52#define NFS_RPC_DROP 124
53
c9f6c91b
JH
54static int fs_mounted;
55static unsigned long rpc_id;
cbd8a35c
WD
56static int nfs_offset = -1;
57static int nfs_len;
eeda762a 58static const ulong nfs_timeout = CONFIG_NFS_TIMEOUT;
cbd8a35c 59
d2986567
SS
60static char dirfh[NFS3_FHSIZE]; /* NFSv2 / NFSv3 file handle of directory */
61static unsigned int dirfh3_length; /* (variable) length of dirfh when NFSv3 */
5280c769 62static char filefh[NFS3_FHSIZE]; /* NFSv2 / NFSv3 file handle */
bdbf7a05 63static unsigned int filefh3_length; /* (variable) length of filefh when NFSv3 */
cbd8a35c 64
22f6e99d 65static enum net_loop_state nfs_download_state;
049a95a7 66static struct in_addr nfs_server_ip;
68c76a3a
JH
67static int nfs_server_mount_port;
68static int nfs_server_port;
69static int nfs_our_port;
70static int nfs_timeout_count;
71static int nfs_state;
cbd8a35c
WD
72#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
cbd8a35c
WD
80static char *nfs_filename;
81static char *nfs_path;
82static char nfs_path_buff[2048];
83
948d7a60
TR
84enum nfs_version {
85 NFS_UNKOWN = 0,
e4bd95bb 86 NFS_V1 = 1,
948d7a60
TR
87 NFS_V2 = 2,
88 NFS_V3 = 3,
89};
b0baca98 90
948d7a60 91static enum nfs_version choosen_nfs_version = NFS_V3;
68c76a3a 92static inline int store_block(uchar *src, unsigned offset, unsigned len)
cbd8a35c 93{
a084f7da 94 ulong newsize = offset + len;
6d0f6bcf 95#ifdef CONFIG_SYS_DIRECT_FLASH_NFS
cbd8a35c
WD
96 int i, rc = 0;
97
c9f6c91b 98 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
cbd8a35c 99 /* start address in flash? */
bb872dd9 100 if (image_load_addr + offset >= flash_info[i].start[0]) {
cbd8a35c
WD
101 rc = 1;
102 break;
103 }
104 }
105
106 if (rc) { /* Flash is destination for this packet */
bb872dd9
SG
107 rc = flash_write((uchar *)src, (ulong)image_load_addr + offset,
108 len);
cbd8a35c 109 if (rc) {
c9f6c91b 110 flash_perror(rc);
23a7a32d 111 return -1;
cbd8a35c
WD
112 }
113 } else
6d0f6bcf 114#endif /* CONFIG_SYS_DIRECT_FLASH_NFS */
cbd8a35c 115 {
bb872dd9 116 void *ptr = map_sysmem(image_load_addr + offset, len);
55d5fd9a
JH
117
118 memcpy(ptr, src, len);
119 unmap_sysmem(ptr);
cbd8a35c 120 }
a084f7da 121
1411157d
JH
122 if (net_boot_file_size < (offset + len))
123 net_boot_file_size = newsize;
23a7a32d 124 return 0;
cbd8a35c
WD
125}
126
68c76a3a 127static char *basename(char *path)
cbd8a35c
WD
128{
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
68c76a3a 142static char *dirname(char *path)
cbd8a35c
WD
143{
144 char *fname;
145
c9f6c91b 146 fname = basename(path);
cbd8a35c
WD
147 --fname;
148 *fname = '\0';
149 return path;
150}
151
cbd8a35c
WD
152/**************************************************************************
153RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
154**************************************************************************/
e4ead4a2 155static uint32_t *rpc_add_credentials(uint32_t *p)
cbd8a35c 156{
cbd8a35c
WD
157 /* 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
cbd8a35c
WD
166 /* Provide an AUTH_UNIX credential. */
167 *p++ = htonl(1); /* AUTH_UNIX */
1ff65d44
JH
168 *p++ = htonl(20); /* auth length */
169 *p++ = 0; /* stamp */
170 *p++ = 0; /* hostname string */
cbd8a35c
WD
171 *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**************************************************************************/
a73588fe 185static void rpc_req(int rpc_prog, int rpc_proc, uint32_t *data, int datalen)
cbd8a35c 186{
a73588fe 187 struct rpc_t rpc_pkt;
cbd8a35c 188 unsigned long id;
a73588fe 189 uint32_t *p;
cbd8a35c
WD
190 int pktlen;
191 int sport;
192
c3f9d493 193 id = ++rpc_id;
a73588fe
JH
194 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);
b0baca98
GG
198 switch (rpc_prog) {
199 case PROG_NFS:
948d7a60 200 switch (choosen_nfs_version) {
e4bd95bb 201 case NFS_V1:
948d7a60
TR
202 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 }
b0baca98 214 break;
b0baca98 215 case PROG_MOUNT:
e4bd95bb
TR
216 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:
b0baca98 235 default:
a73588fe 236 rpc_pkt.u.call.vers = htonl(2); /* portmapper is version 2 */
b0baca98 237 }
a73588fe 238 rpc_pkt.u.call.proc = htonl(rpc_proc);
15eea9a1 239 p = rpc_pkt.u.call.data;
a73588fe
JH
240
241 if (datalen)
15eea9a1 242 memcpy(p, data, datalen * sizeof(uint32_t));
a73588fe
JH
243
244 pktlen = (char *)p + datalen * sizeof(uint32_t) - (char *)&rpc_pkt;
cbd8a35c 245
a73588fe
JH
246 memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE,
247 &rpc_pkt.u.data[0], pktlen);
cbd8a35c
WD
248
249 if (rpc_prog == PROG_PORTMAP)
250 sport = SUNRPC_PORT;
251 else if (rpc_prog == PROG_MOUNT)
68c76a3a 252 sport = nfs_server_mount_port;
cbd8a35c 253 else
68c76a3a 254 sport = nfs_server_port;
cbd8a35c 255
1203fcce 256 net_send_udp_packet(net_server_ethaddr, nfs_server_ip, sport,
68c76a3a 257 nfs_our_port, pktlen);
cbd8a35c
WD
258}
259
260/**************************************************************************
261RPC_LOOKUP - Lookup RPC Port numbers
262**************************************************************************/
68c76a3a 263static void rpc_lookup_req(int prog, int ver)
cbd8a35c 264{
a73588fe 265 uint32_t data[16];
cbd8a35c
WD
266
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;
a73588fe 273 rpc_req(PROG_PORTMAP, PORTMAP_GETPORT, data, 8);
cbd8a35c
WD
274}
275
276/**************************************************************************
277NFS_MOUNT - Mount an NFS Filesystem
278**************************************************************************/
68c76a3a 279static void nfs_mount_req(char *path)
cbd8a35c 280{
a73588fe 281 uint32_t data[1024];
cbd8a35c
WD
282 uint32_t *p;
283 int len;
284 int pathlen;
285
c9f6c91b 286 pathlen = strlen(path);
cbd8a35c 287
a73588fe 288 p = &(data[0]);
e4ead4a2 289 p = rpc_add_credentials(p);
cbd8a35c
WD
290
291 *p++ = htonl(pathlen);
c9f6c91b
JH
292 if (pathlen & 3)
293 *(p + pathlen / 4) = 0;
294 memcpy(p, path, pathlen);
cbd8a35c
WD
295 p += (pathlen + 3) / 4;
296
a73588fe 297 len = (uint32_t *)p - (uint32_t *)&(data[0]);
cbd8a35c 298
a73588fe 299 rpc_req(PROG_MOUNT, MOUNT_ADDENTRY, data, len);
cbd8a35c
WD
300}
301
302/**************************************************************************
303NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
304**************************************************************************/
68c76a3a 305static void nfs_umountall_req(void)
cbd8a35c 306{
a73588fe 307 uint32_t data[1024];
cbd8a35c
WD
308 uint32_t *p;
309 int len;
310
68c76a3a 311 if ((nfs_server_mount_port == -1) || (!fs_mounted))
cbd8a35c
WD
312 /* Nothing mounted, nothing to umount */
313 return;
cbd8a35c 314
a73588fe 315 p = &(data[0]);
e4ead4a2 316 p = rpc_add_credentials(p);
cbd8a35c 317
a73588fe 318 len = (uint32_t *)p - (uint32_t *)&(data[0]);
cbd8a35c 319
a73588fe 320 rpc_req(PROG_MOUNT, MOUNT_UMOUNTALL, data, len);
cbd8a35c
WD
321}
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 **************************************************************************/
68c76a3a 330static void nfs_readlink_req(void)
cbd8a35c 331{
a73588fe 332 uint32_t data[1024];
cbd8a35c
WD
333 uint32_t *p;
334 int len;
335
a73588fe 336 p = &(data[0]);
e4ead4a2 337 p = rpc_add_credentials(p);
cbd8a35c 338
e4bd95bb 339 if (choosen_nfs_version != NFS_V3) {
b0baca98
GG
340 memcpy(p, filefh, NFS_FHSIZE);
341 p += (NFS_FHSIZE / 4);
948d7a60 342 } else { /* NFS_V3 */
b0baca98 343 *p++ = htonl(filefh3_length);
5280c769 344 memcpy(p, filefh, filefh3_length);
b0baca98
GG
345 p += (filefh3_length / 4);
346 }
cbd8a35c 347
a73588fe 348 len = (uint32_t *)p - (uint32_t *)&(data[0]);
cbd8a35c 349
a73588fe 350 rpc_req(PROG_NFS, NFS_READLINK, data, len);
cbd8a35c
WD
351}
352
353/**************************************************************************
354NFS_LOOKUP - Lookup Pathname
355**************************************************************************/
68c76a3a 356static void nfs_lookup_req(char *fname)
cbd8a35c 357{
a73588fe 358 uint32_t data[1024];
cbd8a35c
WD
359 uint32_t *p;
360 int len;
361 int fnamelen;
362
c9f6c91b 363 fnamelen = strlen(fname);
cbd8a35c 364
a73588fe 365 p = &(data[0]);
e4ead4a2 366 p = rpc_add_credentials(p);
cbd8a35c 367
e4bd95bb 368 if (choosen_nfs_version != NFS_V3) {
b0baca98
GG
369 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;
376
a73588fe 377 len = (uint32_t *)p - (uint32_t *)&(data[0]);
b0baca98 378
a73588fe 379 rpc_req(PROG_NFS, NFS_LOOKUP, data, len);
948d7a60 380 } else { /* NFS_V3 */
d2986567
SS
381 *p++ = htonl(dirfh3_length); /* Dir handle length */
382 memcpy(p, dirfh, dirfh3_length);
383 p += (dirfh3_length / 4);
b0baca98
GG
384 *p++ = htonl(fnamelen);
385 if (fnamelen & 3)
386 *(p + fnamelen / 4) = 0;
387 memcpy(p, fname, fnamelen);
388 p += (fnamelen + 3) / 4;
389
a73588fe 390 len = (uint32_t *)p - (uint32_t *)&(data[0]);
b0baca98 391
a73588fe 392 rpc_req(PROG_NFS, NFS3PROC_LOOKUP, data, len);
b0baca98 393 }
cbd8a35c
WD
394}
395
396/**************************************************************************
397NFS_READ - Read File on NFS Server
398**************************************************************************/
68c76a3a 399static void nfs_read_req(int offset, int readlen)
cbd8a35c 400{
a73588fe 401 uint32_t data[1024];
cbd8a35c
WD
402 uint32_t *p;
403 int len;
404
a73588fe 405 p = &(data[0]);
e4ead4a2 406 p = rpc_add_credentials(p);
cbd8a35c 407
e4bd95bb 408 if (choosen_nfs_version != NFS_V3) {
b0baca98
GG
409 memcpy(p, filefh, NFS_FHSIZE);
410 p += (NFS_FHSIZE / 4);
411 *p++ = htonl(offset);
412 *p++ = htonl(readlen);
413 *p++ = 0;
948d7a60 414 } else { /* NFS_V3 */
b0baca98 415 *p++ = htonl(filefh3_length);
5280c769 416 memcpy(p, filefh, filefh3_length);
b0baca98
GG
417 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 }
cbd8a35c 423
a73588fe 424 len = (uint32_t *)p - (uint32_t *)&(data[0]);
cbd8a35c 425
a73588fe 426 rpc_req(PROG_NFS, NFS_READ, data, len);
cbd8a35c
WD
427}
428
429/**************************************************************************
430RPC request dispatcher
431**************************************************************************/
68c76a3a 432static void nfs_send(void)
cbd8a35c 433{
0ebf04c6 434 debug("%s\n", __func__);
cbd8a35c 435
68c76a3a 436 switch (nfs_state) {
cbd8a35c 437 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
e4bd95bb 438 if (choosen_nfs_version != NFS_V3)
b0baca98 439 rpc_lookup_req(PROG_MOUNT, 1);
948d7a60 440 else /* NFS_V3 */
b0baca98 441 rpc_lookup_req(PROG_MOUNT, 3);
cbd8a35c
WD
442 break;
443 case STATE_PRCLOOKUP_PROG_NFS_REQ:
e4bd95bb 444 if (choosen_nfs_version != NFS_V3)
b0baca98 445 rpc_lookup_req(PROG_NFS, 2);
948d7a60 446 else /* NFS_V3 */
b0baca98 447 rpc_lookup_req(PROG_NFS, 3);
cbd8a35c
WD
448 break;
449 case STATE_MOUNT_REQ:
c9f6c91b 450 nfs_mount_req(nfs_path);
cbd8a35c
WD
451 break;
452 case STATE_UMOUNT_REQ:
c9f6c91b 453 nfs_umountall_req();
cbd8a35c
WD
454 break;
455 case STATE_LOOKUP_REQ:
c9f6c91b 456 nfs_lookup_req(nfs_filename);
cbd8a35c
WD
457 break;
458 case STATE_READ_REQ:
c9f6c91b 459 nfs_read_req(nfs_offset, nfs_len);
cbd8a35c
WD
460 break;
461 case STATE_READLINK_REQ:
c9f6c91b 462 nfs_readlink_req();
cbd8a35c
WD
463 break;
464 }
465}
466
467/**************************************************************************
468Handlers for the reply from server
469**************************************************************************/
470
791a43ea
TR
471static 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
e4bd95bb 485 if (max < NFS_V1 || max > NFS_V3 || min > NFS_V3) {
791a43ea
TR
486 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
68c76a3a 519static int rpc_lookup_reply(int prog, uchar *pkt, unsigned len)
cbd8a35c
WD
520{
521 struct rpc_t rpc_pkt;
522
0517cc45 523 memcpy(&rpc_pkt.u.data[0], pkt, len);
cbd8a35c 524
0ebf04c6 525 debug("%s\n", __func__);
cbd8a35c 526
fa84fa70
MB
527 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;
c3f9d493 531
cbd8a35c
WD
532 if (rpc_pkt.u.reply.rstatus ||
533 rpc_pkt.u.reply.verifier ||
c9f6c91b 534 rpc_pkt.u.reply.astatus)
c3f9d493 535 return -1;
cbd8a35c
WD
536
537 switch (prog) {
538 case PROG_MOUNT:
68c76a3a 539 nfs_server_mount_port = ntohl(rpc_pkt.u.reply.data[0]);
cbd8a35c
WD
540 break;
541 case PROG_NFS:
68c76a3a 542 nfs_server_port = ntohl(rpc_pkt.u.reply.data[0]);
cbd8a35c
WD
543 break;
544 }
545
546 return 0;
547}
548
68c76a3a 549static int nfs_mount_reply(uchar *pkt, unsigned len)
cbd8a35c
WD
550{
551 struct rpc_t rpc_pkt;
1b6064b3 552 int ret;
cbd8a35c 553
0ebf04c6 554 debug("%s\n", __func__);
cbd8a35c 555
0517cc45 556 memcpy(&rpc_pkt.u.data[0], pkt, len);
cbd8a35c 557
fa84fa70
MB
558 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;
c3f9d493 562
1b6064b3
TR
563 ret = rpc_handle_error(&rpc_pkt);
564 if (ret)
565 return ret;
cbd8a35c
WD
566
567 fs_mounted = 1;
b0baca98 568 /* NFSv2 and NFSv3 use same structure */
d2986567
SS
569 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 }
cbd8a35c
WD
577
578 return 0;
579}
580
68c76a3a 581static int nfs_umountall_reply(uchar *pkt, unsigned len)
cbd8a35c
WD
582{
583 struct rpc_t rpc_pkt;
584
0ebf04c6 585 debug("%s\n", __func__);
cbd8a35c 586
0517cc45 587 memcpy(&rpc_pkt.u.data[0], pkt, len);
cbd8a35c 588
fa84fa70
MB
589 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;
c3f9d493 593
cbd8a35c
WD
594 if (rpc_pkt.u.reply.rstatus ||
595 rpc_pkt.u.reply.verifier ||
c9f6c91b 596 rpc_pkt.u.reply.astatus)
cbd8a35c 597 return -1;
cbd8a35c
WD
598
599 fs_mounted = 0;
c9f6c91b 600 memset(dirfh, 0, sizeof(dirfh));
cbd8a35c
WD
601
602 return 0;
603}
604
68c76a3a 605static int nfs_lookup_reply(uchar *pkt, unsigned len)
cbd8a35c
WD
606{
607 struct rpc_t rpc_pkt;
791a43ea 608 int ret;
cbd8a35c 609
0ebf04c6 610 debug("%s\n", __func__);
cbd8a35c 611
0517cc45 612 memcpy(&rpc_pkt.u.data[0], pkt, len);
cbd8a35c 613
fa84fa70
MB
614 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;
c3f9d493 618
791a43ea
TR
619 ret = rpc_handle_error(&rpc_pkt);
620 if (ret)
621 return ret;
cbd8a35c 622
e4bd95bb 623 if (choosen_nfs_version != NFS_V3) {
5d14ee4e 624 if (((uchar *)&(rpc_pkt.u.reply.data[0]) - (uchar *)(&rpc_pkt) + NFS_FHSIZE) > len)
625 return -NFS_RPC_DROP;
b0baca98 626 memcpy(filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
948d7a60 627 } else { /* NFS_V3 */
b0baca98
GG
628 filefh3_length = ntohl(rpc_pkt.u.reply.data[1]);
629 if (filefh3_length > NFS3_FHSIZE)
630 filefh3_length = NFS3_FHSIZE;
5280c769 631 memcpy(filefh, rpc_pkt.u.reply.data + 2, filefh3_length);
b0baca98 632 }
cbd8a35c
WD
633
634 return 0;
635}
636
051ed9af
JH
637static int nfs3_get_attributes_offset(uint32_t *data)
638{
15eea9a1 639 if (data[1]) {
051ed9af 640 /* 'attributes_follow' flag is TRUE,
c629c45f 641 * so we have attributes on 21 dwords */
051ed9af
JH
642 /* 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
68c76a3a 665static int nfs_readlink_reply(uchar *pkt, unsigned len)
cbd8a35c
WD
666{
667 struct rpc_t rpc_pkt;
668 int rlen;
051ed9af 669 int nfsv3_data_offset = 0;
cbd8a35c 670
0ebf04c6 671 debug("%s\n", __func__);
cbd8a35c 672
c9f6c91b 673 memcpy((unsigned char *)&rpc_pkt, pkt, len);
cbd8a35c 674
fa84fa70
MB
675 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;
c3f9d493 679
cbd8a35c
WD
680 if (rpc_pkt.u.reply.rstatus ||
681 rpc_pkt.u.reply.verifier ||
682 rpc_pkt.u.reply.astatus ||
c9f6c91b 683 rpc_pkt.u.reply.data[0])
cbd8a35c 684 return -1;
cbd8a35c 685
948d7a60 686 if (choosen_nfs_version == NFS_V3) {
051ed9af
JH
687 nfsv3_data_offset =
688 nfs3_get_attributes_offset(rpc_pkt.u.reply.data);
689 }
b0baca98 690
051ed9af
JH
691 /* new path length */
692 rlen = ntohl(rpc_pkt.u.reply.data[1 + nfsv3_data_offset]);
693
cf3a4f1e 694 if (((uchar *)&(rpc_pkt.u.reply.data[0]) - (uchar *)(&rpc_pkt) + rlen) > len)
695 return -NFS_RPC_DROP;
696
051ed9af
JH
697 if (*((char *)&(rpc_pkt.u.reply.data[2 + nfsv3_data_offset])) != '/') {
698 int pathlen;
699
700 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;
cbd8a35c
WD
711 }
712 return 0;
713}
714
68c76a3a 715static int nfs_read_reply(uchar *pkt, unsigned len)
cbd8a35c
WD
716{
717 struct rpc_t rpc_pkt;
718 int rlen;
b0baca98 719 uchar *data_ptr;
cbd8a35c 720
0ebf04c6 721 debug("%s\n", __func__);
cbd8a35c 722
0517cc45 723 memcpy(&rpc_pkt.u.data[0], pkt, sizeof(rpc_pkt.u.reply));
cbd8a35c 724
fa84fa70
MB
725 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;
c3f9d493 729
cbd8a35c
WD
730 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]) {
c9f6c91b 734 if (rpc_pkt.u.reply.rstatus)
cbd8a35c 735 return -9999;
c9f6c91b 736 if (rpc_pkt.u.reply.astatus)
cbd8a35c 737 return -9999;
c9f6c91b 738 return -ntohl(rpc_pkt.u.reply.data[0]);
cbd8a35c
WD
739 }
740
c9f6c91b
JH
741 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('#');
cbd8a35c 746
e4bd95bb 747 if (choosen_nfs_version != NFS_V3) {
b0baca98
GG
748 rlen = ntohl(rpc_pkt.u.reply.data[18]);
749 data_ptr = (uchar *)&(rpc_pkt.u.reply.data[19]);
948d7a60 750 } else { /* NFS_V3 */
051ed9af
JH
751 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]);
b0baca98
GG
762 }
763
aa207cf3 764 if (((uchar *)&(rpc_pkt.u.reply.data[0]) - (uchar *)(&rpc_pkt) + rlen) > len)
765 return -9999;
766
b0baca98
GG
767 if (store_block(data_ptr, nfs_offset, rlen))
768 return -9999;
cbd8a35c
WD
769
770 return rlen;
771}
772
773/**************************************************************************
774Interfaces of U-BOOT
775**************************************************************************/
68c76a3a 776static void nfs_timeout_handler(void)
a5725fab 777{
68c76a3a 778 if (++nfs_timeout_count > NFS_RETRY_COUNT) {
c9f6c91b 779 puts("\nRetry count exceeded; starting again\n");
bc0571fc 780 net_start_again();
aabb8cb0
ES
781 } else {
782 puts("T ");
bc0571fc 783 net_set_timeout_handler(nfs_timeout +
eeda762a 784 nfs_timeout * nfs_timeout_count,
bc0571fc 785 nfs_timeout_handler);
68c76a3a 786 nfs_send();
fe891ecf 787 }
a5725fab
WD
788}
789
049a95a7
JH
790static void nfs_handler(uchar *pkt, unsigned dest, struct in_addr sip,
791 unsigned src, unsigned len)
cbd8a35c
WD
792{
793 int rlen;
fa84fa70 794 int reply;
cbd8a35c 795
0ebf04c6 796 debug("%s\n", __func__);
cbd8a35c 797
741a8a08 798 if (len > sizeof(struct rpc_t))
799 return;
800
68c76a3a 801 if (dest != nfs_our_port)
c9f6c91b 802 return;
cbd8a35c 803
68c76a3a 804 switch (nfs_state) {
cbd8a35c 805 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
fa84fa70
MB
806 if (rpc_lookup_reply(PROG_MOUNT, pkt, len) == -NFS_RPC_DROP)
807 break;
68c76a3a
JH
808 nfs_state = STATE_PRCLOOKUP_PROG_NFS_REQ;
809 nfs_send();
cbd8a35c
WD
810 break;
811
812 case STATE_PRCLOOKUP_PROG_NFS_REQ:
fa84fa70
MB
813 if (rpc_lookup_reply(PROG_NFS, pkt, len) == -NFS_RPC_DROP)
814 break;
68c76a3a
JH
815 nfs_state = STATE_MOUNT_REQ;
816 nfs_send();
cbd8a35c
WD
817 break;
818
819 case STATE_MOUNT_REQ:
fa84fa70 820 reply = nfs_mount_reply(pkt, len);
68c76a3a 821 if (reply == -NFS_RPC_DROP) {
fa84fa70 822 break;
68c76a3a 823 } else if (reply == -NFS_RPC_ERR) {
c9f6c91b 824 puts("*** ERROR: Cannot mount\n");
cbd8a35c 825 /* just to be sure... */
68c76a3a
JH
826 nfs_state = STATE_UMOUNT_REQ;
827 nfs_send();
1b6064b3
TR
828 } else if (reply == -NFS_RPC_PROG_MISMATCH &&
829 choosen_nfs_version != NFS_UNKOWN) {
830 nfs_state = STATE_MOUNT_REQ;
831 nfs_send();
cbd8a35c 832 } else {
68c76a3a
JH
833 nfs_state = STATE_LOOKUP_REQ;
834 nfs_send();
cbd8a35c
WD
835 }
836 break;
837
838 case STATE_UMOUNT_REQ:
fa84fa70 839 reply = nfs_umountall_reply(pkt, len);
68c76a3a 840 if (reply == -NFS_RPC_DROP) {
fa84fa70 841 break;
68c76a3a 842 } else if (reply == -NFS_RPC_ERR) {
d89ff2df 843 debug("*** ERROR: Cannot umount\n");
22f6e99d 844 net_set_state(NETLOOP_FAIL);
cbd8a35c 845 } else {
c9f6c91b 846 puts("\ndone\n");
22f6e99d 847 net_set_state(nfs_download_state);
cbd8a35c
WD
848 }
849 break;
850
851 case STATE_LOOKUP_REQ:
fa84fa70 852 reply = nfs_lookup_reply(pkt, len);
68c76a3a 853 if (reply == -NFS_RPC_DROP) {
fa84fa70 854 break;
68c76a3a 855 } else if (reply == -NFS_RPC_ERR) {
c9f6c91b 856 puts("*** ERROR: File lookup fail\n");
68c76a3a
JH
857 nfs_state = STATE_UMOUNT_REQ;
858 nfs_send();
347a9015 859 } else if (reply == -NFS_RPC_PROG_MISMATCH &&
948d7a60 860 choosen_nfs_version != NFS_UNKOWN) {
b0baca98
GG
861 /* 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();
cbd8a35c 867 } else {
68c76a3a 868 nfs_state = STATE_READ_REQ;
cbd8a35c
WD
869 nfs_offset = 0;
870 nfs_len = NFS_READ_SIZE;
68c76a3a 871 nfs_send();
cbd8a35c
WD
872 }
873 break;
874
875 case STATE_READLINK_REQ:
fa84fa70 876 reply = nfs_readlink_reply(pkt, len);
68c76a3a 877 if (reply == -NFS_RPC_DROP) {
fa84fa70 878 break;
68c76a3a 879 } else if (reply == -NFS_RPC_ERR) {
c9f6c91b 880 puts("*** ERROR: Symlink fail\n");
68c76a3a
JH
881 nfs_state = STATE_UMOUNT_REQ;
882 nfs_send();
cbd8a35c 883 } else {
0ebf04c6 884 debug("Symlink --> %s\n", nfs_path);
c9f6c91b
JH
885 nfs_filename = basename(nfs_path);
886 nfs_path = dirname(nfs_path);
cbd8a35c 887
68c76a3a
JH
888 nfs_state = STATE_MOUNT_REQ;
889 nfs_send();
cbd8a35c
WD
890 }
891 break;
892
893 case STATE_READ_REQ:
c9f6c91b 894 rlen = nfs_read_reply(pkt, len);
d48d40a0
VK
895 if (rlen == -NFS_RPC_DROP)
896 break;
bc0571fc 897 net_set_timeout_handler(nfs_timeout, nfs_timeout_handler);
cbd8a35c
WD
898 if (rlen > 0) {
899 nfs_offset += rlen;
68c76a3a 900 nfs_send();
c9f6c91b 901 } else if ((rlen == -NFSERR_ISDIR) || (rlen == -NFSERR_INVAL)) {
cbd8a35c 902 /* symbolic link */
68c76a3a
JH
903 nfs_state = STATE_READLINK_REQ;
904 nfs_send();
cbd8a35c 905 } else {
c9f6c91b 906 if (!rlen)
22f6e99d 907 nfs_download_state = NETLOOP_SUCCESS;
b0baca98 908 if (rlen < 0)
d89ff2df 909 debug("NFS READ error (%d)\n", rlen);
68c76a3a
JH
910 nfs_state = STATE_UMOUNT_REQ;
911 nfs_send();
cbd8a35c
WD
912 }
913 break;
914 }
915}
916
cbd8a35c 917
68c76a3a 918void nfs_start(void)
cbd8a35c 919{
0ebf04c6 920 debug("%s\n", __func__);
22f6e99d 921 nfs_download_state = NETLOOP_FAIL;
cbd8a35c 922
049a95a7 923 nfs_server_ip = net_server_ip;
cbd8a35c
WD
924 nfs_path = (char *)nfs_path_buff;
925
926 if (nfs_path == NULL) {
22f6e99d 927 net_set_state(NETLOOP_FAIL);
faecf84a 928 printf("*** ERROR: Fail allocate memory\n");
cbd8a35c
WD
929 return;
930 }
931
6ab12830
JH
932 if (!net_parse_bootfile(&nfs_server_ip, nfs_path,
933 sizeof(nfs_path_buff))) {
f8b26c7a 934 sprintf(nfs_path, "/nfsroot/%02X%02X%02X%02X.img",
049a95a7
JH
935 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);
cbd8a35c 939
faecf84a
JH
940 printf("*** Warning: no boot file name; using '%s'\n",
941 nfs_path);
cbd8a35c
WD
942 }
943
c9f6c91b
JH
944 nfs_filename = basename(nfs_path);
945 nfs_path = dirname(nfs_path);
cbd8a35c 946
faecf84a 947 printf("Using %s device\n", eth_get_name());
cbd8a35c 948
faecf84a
JH
949 printf("File transfer via NFS from server %pI4; our IP address is %pI4",
950 &nfs_server_ip, &net_ip);
cbd8a35c
WD
951
952 /* Check if we need to send across this subnet */
049a95a7
JH
953 if (net_gateway.s_addr && net_netmask.s_addr) {
954 struct in_addr our_net;
955 struct in_addr server_net;
cbd8a35c 956
049a95a7 957 our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
347e32b0 958 server_net.s_addr = nfs_server_ip.s_addr & net_netmask.s_addr;
049a95a7 959 if (our_net.s_addr != server_net.s_addr)
faecf84a
JH
960 printf("; sending through gateway %pI4",
961 &net_gateway);
cbd8a35c 962 }
faecf84a 963 printf("\nFilename '%s/%s'.", nfs_path, nfs_filename);
cbd8a35c 964
1411157d 965 if (net_boot_file_expected_size_in_blocks) {
faecf84a
JH
966 printf(" Size is 0x%x Bytes = ",
967 net_boot_file_expected_size_in_blocks << 9);
1411157d 968 print_size(net_boot_file_expected_size_in_blocks << 9, "");
cbd8a35c 969 }
bb872dd9 970 printf("\nLoad address: 0x%lx\nLoading: *\b", image_load_addr);
cbd8a35c 971
bc0571fc 972 net_set_timeout_handler(nfs_timeout, nfs_timeout_handler);
049a95a7 973 net_set_udp_handler(nfs_handler);
cbd8a35c 974
68c76a3a
JH
975 nfs_timeout_count = 0;
976 nfs_state = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
cbd8a35c 977
68c76a3a 978 /*nfs_our_port = 4096 + (get_ticks() % 3072);*/
cbd8a35c 979 /*FIX ME !!!*/
68c76a3a 980 nfs_our_port = 1000;
cbd8a35c
WD
981
982 /* zero out server ether in case the server ip has changed */
0adb5b76 983 memset(net_server_ethaddr, 0, 6);
cbd8a35c 984
68c76a3a 985 nfs_send();
cbd8a35c 986}
This page took 0.474564 seconds and 4 git commands to generate.