]> Git Repo - u-boot.git/blame - net/nfs.c
Fix some checkpatch warnings in calls to debug()
[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
cbd8a35c
WD
29#include <common.h>
30#include <command.h>
0ee48252 31#include <flash.h>
8e8ccfe1 32#include <image.h>
cbd8a35c
WD
33#include <net.h>
34#include <malloc.h>
55d5fd9a 35#include <mapmem.h>
cbd8a35c
WD
36#include "nfs.h"
37#include "bootp.h"
1045315d 38#include <time.h>
cbd8a35c 39
cbd8a35c 40#define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
fe891ecf 41#define NFS_RETRY_COUNT 30
48a3e999
TK
42#ifndef CONFIG_NFS_TIMEOUT
43# define NFS_TIMEOUT 2000UL
44#else
45# define NFS_TIMEOUT CONFIG_NFS_TIMEOUT
46#endif
cbd8a35c 47
fa84fa70
MB
48#define NFS_RPC_ERR 1
49#define NFS_RPC_DROP 124
50
c9f6c91b
JH
51static int fs_mounted;
52static unsigned long rpc_id;
cbd8a35c
WD
53static int nfs_offset = -1;
54static int nfs_len;
fa84fa70 55static ulong nfs_timeout = NFS_TIMEOUT;
cbd8a35c 56
b0baca98 57static char dirfh[NFS_FHSIZE]; /* NFSv2 / NFSv3 file handle of directory */
5280c769
JH
58static char filefh[NFS3_FHSIZE]; /* NFSv2 / NFSv3 file handle */
59static int filefh3_length; /* (variable) length of filefh when NFSv3 */
cbd8a35c 60
22f6e99d 61static enum net_loop_state nfs_download_state;
049a95a7 62static struct in_addr nfs_server_ip;
68c76a3a
JH
63static int nfs_server_mount_port;
64static int nfs_server_port;
65static int nfs_our_port;
66static int nfs_timeout_count;
67static int nfs_state;
cbd8a35c
WD
68#define STATE_PRCLOOKUP_PROG_MOUNT_REQ 1
69#define STATE_PRCLOOKUP_PROG_NFS_REQ 2
70#define STATE_MOUNT_REQ 3
71#define STATE_UMOUNT_REQ 4
72#define STATE_LOOKUP_REQ 5
73#define STATE_READ_REQ 6
74#define STATE_READLINK_REQ 7
75
cbd8a35c
WD
76static char *nfs_filename;
77static char *nfs_path;
78static char nfs_path_buff[2048];
79
b0baca98
GG
80#define NFSV2_FLAG 1
81#define NFSV3_FLAG 1 << 1
82static char supported_nfs_versions = NFSV2_FLAG | NFSV3_FLAG;
83
68c76a3a 84static inline int store_block(uchar *src, unsigned offset, unsigned len)
cbd8a35c 85{
a084f7da 86 ulong newsize = offset + len;
6d0f6bcf 87#ifdef CONFIG_SYS_DIRECT_FLASH_NFS
cbd8a35c
WD
88 int i, rc = 0;
89
c9f6c91b 90 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
cbd8a35c 91 /* start address in flash? */
bb872dd9 92 if (image_load_addr + offset >= flash_info[i].start[0]) {
cbd8a35c
WD
93 rc = 1;
94 break;
95 }
96 }
97
98 if (rc) { /* Flash is destination for this packet */
bb872dd9
SG
99 rc = flash_write((uchar *)src, (ulong)image_load_addr + offset,
100 len);
cbd8a35c 101 if (rc) {
c9f6c91b 102 flash_perror(rc);
23a7a32d 103 return -1;
cbd8a35c
WD
104 }
105 } else
6d0f6bcf 106#endif /* CONFIG_SYS_DIRECT_FLASH_NFS */
cbd8a35c 107 {
bb872dd9 108 void *ptr = map_sysmem(image_load_addr + offset, len);
55d5fd9a
JH
109
110 memcpy(ptr, src, len);
111 unmap_sysmem(ptr);
cbd8a35c 112 }
a084f7da 113
1411157d
JH
114 if (net_boot_file_size < (offset + len))
115 net_boot_file_size = newsize;
23a7a32d 116 return 0;
cbd8a35c
WD
117}
118
68c76a3a 119static char *basename(char *path)
cbd8a35c
WD
120{
121 char *fname;
122
123 fname = path + strlen(path) - 1;
124 while (fname >= path) {
125 if (*fname == '/') {
126 fname++;
127 break;
128 }
129 fname--;
130 }
131 return fname;
132}
133
68c76a3a 134static char *dirname(char *path)
cbd8a35c
WD
135{
136 char *fname;
137
c9f6c91b 138 fname = basename(path);
cbd8a35c
WD
139 --fname;
140 *fname = '\0';
141 return path;
142}
143
cbd8a35c
WD
144/**************************************************************************
145RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
146**************************************************************************/
e4ead4a2 147static uint32_t *rpc_add_credentials(uint32_t *p)
cbd8a35c 148{
cbd8a35c
WD
149 /* Here's the executive summary on authentication requirements of the
150 * various NFS server implementations: Linux accepts both AUTH_NONE
151 * and AUTH_UNIX authentication (also accepts an empty hostname field
152 * in the AUTH_UNIX scheme). *BSD refuses AUTH_NONE, but accepts
153 * AUTH_UNIX (also accepts an empty hostname field in the AUTH_UNIX
154 * scheme). To be safe, use AUTH_UNIX and pass the hostname if we have
155 * it (if the BOOTP/DHCP reply didn't give one, just use an empty
156 * hostname). */
157
cbd8a35c
WD
158 /* Provide an AUTH_UNIX credential. */
159 *p++ = htonl(1); /* AUTH_UNIX */
1ff65d44
JH
160 *p++ = htonl(20); /* auth length */
161 *p++ = 0; /* stamp */
162 *p++ = 0; /* hostname string */
cbd8a35c
WD
163 *p++ = 0; /* uid */
164 *p++ = 0; /* gid */
165 *p++ = 0; /* auxiliary gid list */
166
167 /* Provide an AUTH_NONE verifier. */
168 *p++ = 0; /* AUTH_NONE */
169 *p++ = 0; /* auth length */
170
171 return p;
172}
173
174/**************************************************************************
175RPC_LOOKUP - Lookup RPC Port numbers
176**************************************************************************/
a73588fe 177static void rpc_req(int rpc_prog, int rpc_proc, uint32_t *data, int datalen)
cbd8a35c 178{
a73588fe 179 struct rpc_t rpc_pkt;
cbd8a35c 180 unsigned long id;
a73588fe 181 uint32_t *p;
cbd8a35c
WD
182 int pktlen;
183 int sport;
184
c3f9d493 185 id = ++rpc_id;
a73588fe
JH
186 rpc_pkt.u.call.id = htonl(id);
187 rpc_pkt.u.call.type = htonl(MSG_CALL);
188 rpc_pkt.u.call.rpcvers = htonl(2); /* use RPC version 2 */
189 rpc_pkt.u.call.prog = htonl(rpc_prog);
b0baca98
GG
190 switch (rpc_prog) {
191 case PROG_NFS:
192 if (supported_nfs_versions & NFSV2_FLAG)
a73588fe 193 rpc_pkt.u.call.vers = htonl(2); /* NFS v2 */
b0baca98 194 else /* NFSV3_FLAG */
a73588fe 195 rpc_pkt.u.call.vers = htonl(3); /* NFS v3 */
b0baca98
GG
196 break;
197 case PROG_PORTMAP:
198 case PROG_MOUNT:
199 default:
a73588fe 200 rpc_pkt.u.call.vers = htonl(2); /* portmapper is version 2 */
b0baca98 201 }
a73588fe 202 rpc_pkt.u.call.proc = htonl(rpc_proc);
15eea9a1 203 p = rpc_pkt.u.call.data;
a73588fe
JH
204
205 if (datalen)
15eea9a1 206 memcpy(p, data, datalen * sizeof(uint32_t));
a73588fe
JH
207
208 pktlen = (char *)p + datalen * sizeof(uint32_t) - (char *)&rpc_pkt;
cbd8a35c 209
a73588fe
JH
210 memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE,
211 &rpc_pkt.u.data[0], pktlen);
cbd8a35c
WD
212
213 if (rpc_prog == PROG_PORTMAP)
214 sport = SUNRPC_PORT;
215 else if (rpc_prog == PROG_MOUNT)
68c76a3a 216 sport = nfs_server_mount_port;
cbd8a35c 217 else
68c76a3a 218 sport = nfs_server_port;
cbd8a35c 219
1203fcce 220 net_send_udp_packet(net_server_ethaddr, nfs_server_ip, sport,
68c76a3a 221 nfs_our_port, pktlen);
cbd8a35c
WD
222}
223
224/**************************************************************************
225RPC_LOOKUP - Lookup RPC Port numbers
226**************************************************************************/
68c76a3a 227static void rpc_lookup_req(int prog, int ver)
cbd8a35c 228{
a73588fe 229 uint32_t data[16];
cbd8a35c
WD
230
231 data[0] = 0; data[1] = 0; /* auth credential */
232 data[2] = 0; data[3] = 0; /* auth verifier */
233 data[4] = htonl(prog);
234 data[5] = htonl(ver);
235 data[6] = htonl(17); /* IP_UDP */
236 data[7] = 0;
a73588fe 237 rpc_req(PROG_PORTMAP, PORTMAP_GETPORT, data, 8);
cbd8a35c
WD
238}
239
240/**************************************************************************
241NFS_MOUNT - Mount an NFS Filesystem
242**************************************************************************/
68c76a3a 243static void nfs_mount_req(char *path)
cbd8a35c 244{
a73588fe 245 uint32_t data[1024];
cbd8a35c
WD
246 uint32_t *p;
247 int len;
248 int pathlen;
249
c9f6c91b 250 pathlen = strlen(path);
cbd8a35c 251
a73588fe 252 p = &(data[0]);
e4ead4a2 253 p = rpc_add_credentials(p);
cbd8a35c
WD
254
255 *p++ = htonl(pathlen);
c9f6c91b
JH
256 if (pathlen & 3)
257 *(p + pathlen / 4) = 0;
258 memcpy(p, path, pathlen);
cbd8a35c
WD
259 p += (pathlen + 3) / 4;
260
a73588fe 261 len = (uint32_t *)p - (uint32_t *)&(data[0]);
cbd8a35c 262
a73588fe 263 rpc_req(PROG_MOUNT, MOUNT_ADDENTRY, data, len);
cbd8a35c
WD
264}
265
266/**************************************************************************
267NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
268**************************************************************************/
68c76a3a 269static void nfs_umountall_req(void)
cbd8a35c 270{
a73588fe 271 uint32_t data[1024];
cbd8a35c
WD
272 uint32_t *p;
273 int len;
274
68c76a3a 275 if ((nfs_server_mount_port == -1) || (!fs_mounted))
cbd8a35c
WD
276 /* Nothing mounted, nothing to umount */
277 return;
cbd8a35c 278
a73588fe 279 p = &(data[0]);
e4ead4a2 280 p = rpc_add_credentials(p);
cbd8a35c 281
a73588fe 282 len = (uint32_t *)p - (uint32_t *)&(data[0]);
cbd8a35c 283
a73588fe 284 rpc_req(PROG_MOUNT, MOUNT_UMOUNTALL, data, len);
cbd8a35c
WD
285}
286
287/***************************************************************************
288 * NFS_READLINK (AH 2003-07-14)
289 * This procedure is called when read of the first block fails -
290 * this probably happens when it's a directory or a symlink
291 * In case of successful readlink(), the dirname is manipulated,
292 * so that inside the nfs() function a recursion can be done.
293 **************************************************************************/
68c76a3a 294static void nfs_readlink_req(void)
cbd8a35c 295{
a73588fe 296 uint32_t data[1024];
cbd8a35c
WD
297 uint32_t *p;
298 int len;
299
a73588fe 300 p = &(data[0]);
e4ead4a2 301 p = rpc_add_credentials(p);
cbd8a35c 302
b0baca98
GG
303 if (supported_nfs_versions & NFSV2_FLAG) {
304 memcpy(p, filefh, NFS_FHSIZE);
305 p += (NFS_FHSIZE / 4);
306 } else { /* NFSV3_FLAG */
307 *p++ = htonl(filefh3_length);
5280c769 308 memcpy(p, filefh, filefh3_length);
b0baca98
GG
309 p += (filefh3_length / 4);
310 }
cbd8a35c 311
a73588fe 312 len = (uint32_t *)p - (uint32_t *)&(data[0]);
cbd8a35c 313
a73588fe 314 rpc_req(PROG_NFS, NFS_READLINK, data, len);
cbd8a35c
WD
315}
316
317/**************************************************************************
318NFS_LOOKUP - Lookup Pathname
319**************************************************************************/
68c76a3a 320static void nfs_lookup_req(char *fname)
cbd8a35c 321{
a73588fe 322 uint32_t data[1024];
cbd8a35c
WD
323 uint32_t *p;
324 int len;
325 int fnamelen;
326
c9f6c91b 327 fnamelen = strlen(fname);
cbd8a35c 328
a73588fe 329 p = &(data[0]);
e4ead4a2 330 p = rpc_add_credentials(p);
cbd8a35c 331
b0baca98
GG
332 if (supported_nfs_versions & NFSV2_FLAG) {
333 memcpy(p, dirfh, NFS_FHSIZE);
334 p += (NFS_FHSIZE / 4);
335 *p++ = htonl(fnamelen);
336 if (fnamelen & 3)
337 *(p + fnamelen / 4) = 0;
338 memcpy(p, fname, fnamelen);
339 p += (fnamelen + 3) / 4;
340
a73588fe 341 len = (uint32_t *)p - (uint32_t *)&(data[0]);
b0baca98 342
a73588fe 343 rpc_req(PROG_NFS, NFS_LOOKUP, data, len);
b0baca98
GG
344 } else { /* NFSV3_FLAG */
345 *p++ = htonl(NFS_FHSIZE); /* Dir handle length */
346 memcpy(p, dirfh, NFS_FHSIZE);
347 p += (NFS_FHSIZE / 4);
348 *p++ = htonl(fnamelen);
349 if (fnamelen & 3)
350 *(p + fnamelen / 4) = 0;
351 memcpy(p, fname, fnamelen);
352 p += (fnamelen + 3) / 4;
353
a73588fe 354 len = (uint32_t *)p - (uint32_t *)&(data[0]);
b0baca98 355
a73588fe 356 rpc_req(PROG_NFS, NFS3PROC_LOOKUP, data, len);
b0baca98 357 }
cbd8a35c
WD
358}
359
360/**************************************************************************
361NFS_READ - Read File on NFS Server
362**************************************************************************/
68c76a3a 363static void nfs_read_req(int offset, int readlen)
cbd8a35c 364{
a73588fe 365 uint32_t data[1024];
cbd8a35c
WD
366 uint32_t *p;
367 int len;
368
a73588fe 369 p = &(data[0]);
e4ead4a2 370 p = rpc_add_credentials(p);
cbd8a35c 371
b0baca98
GG
372 if (supported_nfs_versions & NFSV2_FLAG) {
373 memcpy(p, filefh, NFS_FHSIZE);
374 p += (NFS_FHSIZE / 4);
375 *p++ = htonl(offset);
376 *p++ = htonl(readlen);
377 *p++ = 0;
378 } else { /* NFSV3_FLAG */
379 *p++ = htonl(filefh3_length);
5280c769 380 memcpy(p, filefh, filefh3_length);
b0baca98
GG
381 p += (filefh3_length / 4);
382 *p++ = htonl(0); /* offset is 64-bit long, so fill with 0 */
383 *p++ = htonl(offset);
384 *p++ = htonl(readlen);
385 *p++ = 0;
386 }
cbd8a35c 387
a73588fe 388 len = (uint32_t *)p - (uint32_t *)&(data[0]);
cbd8a35c 389
a73588fe 390 rpc_req(PROG_NFS, NFS_READ, data, len);
cbd8a35c
WD
391}
392
393/**************************************************************************
394RPC request dispatcher
395**************************************************************************/
68c76a3a 396static void nfs_send(void)
cbd8a35c 397{
0ebf04c6 398 debug("%s\n", __func__);
cbd8a35c 399
68c76a3a 400 switch (nfs_state) {
cbd8a35c 401 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
b0baca98
GG
402 if (supported_nfs_versions & NFSV2_FLAG)
403 rpc_lookup_req(PROG_MOUNT, 1);
404 else /* NFSV3_FLAG */
405 rpc_lookup_req(PROG_MOUNT, 3);
cbd8a35c
WD
406 break;
407 case STATE_PRCLOOKUP_PROG_NFS_REQ:
b0baca98
GG
408 if (supported_nfs_versions & NFSV2_FLAG)
409 rpc_lookup_req(PROG_NFS, 2);
410 else /* NFSV3_FLAG */
411 rpc_lookup_req(PROG_NFS, 3);
cbd8a35c
WD
412 break;
413 case STATE_MOUNT_REQ:
c9f6c91b 414 nfs_mount_req(nfs_path);
cbd8a35c
WD
415 break;
416 case STATE_UMOUNT_REQ:
c9f6c91b 417 nfs_umountall_req();
cbd8a35c
WD
418 break;
419 case STATE_LOOKUP_REQ:
c9f6c91b 420 nfs_lookup_req(nfs_filename);
cbd8a35c
WD
421 break;
422 case STATE_READ_REQ:
c9f6c91b 423 nfs_read_req(nfs_offset, nfs_len);
cbd8a35c
WD
424 break;
425 case STATE_READLINK_REQ:
c9f6c91b 426 nfs_readlink_req();
cbd8a35c
WD
427 break;
428 }
429}
430
431/**************************************************************************
432Handlers for the reply from server
433**************************************************************************/
434
68c76a3a 435static int rpc_lookup_reply(int prog, uchar *pkt, unsigned len)
cbd8a35c
WD
436{
437 struct rpc_t rpc_pkt;
438
0517cc45 439 memcpy(&rpc_pkt.u.data[0], pkt, len);
cbd8a35c 440
0ebf04c6 441 debug("%s\n", __func__);
cbd8a35c 442
fa84fa70
MB
443 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
444 return -NFS_RPC_ERR;
445 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
446 return -NFS_RPC_DROP;
c3f9d493 447
cbd8a35c
WD
448 if (rpc_pkt.u.reply.rstatus ||
449 rpc_pkt.u.reply.verifier ||
c9f6c91b 450 rpc_pkt.u.reply.astatus)
c3f9d493 451 return -1;
cbd8a35c
WD
452
453 switch (prog) {
454 case PROG_MOUNT:
68c76a3a 455 nfs_server_mount_port = ntohl(rpc_pkt.u.reply.data[0]);
cbd8a35c
WD
456 break;
457 case PROG_NFS:
68c76a3a 458 nfs_server_port = ntohl(rpc_pkt.u.reply.data[0]);
cbd8a35c
WD
459 break;
460 }
461
462 return 0;
463}
464
68c76a3a 465static int nfs_mount_reply(uchar *pkt, unsigned len)
cbd8a35c
WD
466{
467 struct rpc_t rpc_pkt;
468
0ebf04c6 469 debug("%s\n", __func__);
cbd8a35c 470
0517cc45 471 memcpy(&rpc_pkt.u.data[0], pkt, len);
cbd8a35c 472
fa84fa70
MB
473 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
474 return -NFS_RPC_ERR;
475 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
476 return -NFS_RPC_DROP;
c3f9d493 477
cbd8a35c
WD
478 if (rpc_pkt.u.reply.rstatus ||
479 rpc_pkt.u.reply.verifier ||
480 rpc_pkt.u.reply.astatus ||
c9f6c91b 481 rpc_pkt.u.reply.data[0])
cbd8a35c 482 return -1;
cbd8a35c
WD
483
484 fs_mounted = 1;
b0baca98 485 /* NFSv2 and NFSv3 use same structure */
c9f6c91b 486 memcpy(dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
cbd8a35c
WD
487
488 return 0;
489}
490
68c76a3a 491static int nfs_umountall_reply(uchar *pkt, unsigned len)
cbd8a35c
WD
492{
493 struct rpc_t rpc_pkt;
494
0ebf04c6 495 debug("%s\n", __func__);
cbd8a35c 496
0517cc45 497 memcpy(&rpc_pkt.u.data[0], pkt, len);
cbd8a35c 498
fa84fa70
MB
499 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
500 return -NFS_RPC_ERR;
501 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
502 return -NFS_RPC_DROP;
c3f9d493 503
cbd8a35c
WD
504 if (rpc_pkt.u.reply.rstatus ||
505 rpc_pkt.u.reply.verifier ||
c9f6c91b 506 rpc_pkt.u.reply.astatus)
cbd8a35c 507 return -1;
cbd8a35c
WD
508
509 fs_mounted = 0;
c9f6c91b 510 memset(dirfh, 0, sizeof(dirfh));
cbd8a35c
WD
511
512 return 0;
513}
514
68c76a3a 515static int nfs_lookup_reply(uchar *pkt, unsigned len)
cbd8a35c
WD
516{
517 struct rpc_t rpc_pkt;
518
0ebf04c6 519 debug("%s\n", __func__);
cbd8a35c 520
0517cc45 521 memcpy(&rpc_pkt.u.data[0], pkt, len);
cbd8a35c 522
fa84fa70
MB
523 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
524 return -NFS_RPC_ERR;
525 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
526 return -NFS_RPC_DROP;
c3f9d493 527
cbd8a35c
WD
528 if (rpc_pkt.u.reply.rstatus ||
529 rpc_pkt.u.reply.verifier ||
530 rpc_pkt.u.reply.astatus ||
69fd0d41
GG
531 rpc_pkt.u.reply.data[0]) {
532 switch (ntohl(rpc_pkt.u.reply.astatus)) {
b0baca98 533 case NFS_RPC_SUCCESS: /* Not an error */
69fd0d41 534 break;
b0baca98
GG
535 case NFS_RPC_PROG_MISMATCH:
536 /* Remote can't support NFS version */
537 switch (ntohl(rpc_pkt.u.reply.data[0])) {
538 /* Minimal supported NFS version */
539 case 3:
faecf84a 540 debug("*** Warning: NFS version not supported: Requested: V%d, accepted: min V%d - max V%d\n",
347a9015
JH
541 (supported_nfs_versions & NFSV2_FLAG) ?
542 2 : 3,
b0baca98
GG
543 ntohl(rpc_pkt.u.reply.data[0]),
544 ntohl(rpc_pkt.u.reply.data[1]));
545 debug("Will retry with NFSv3\n");
546 /* Clear NFSV2_FLAG from supported versions */
547 supported_nfs_versions &= ~NFSV2_FLAG;
548 return -NFS_RPC_PROG_MISMATCH;
549 case 4:
550 default:
d89ff2df
JH
551 puts("*** ERROR: NFS version not supported");
552 debug(": Requested: V%d, accepted: min V%d - max V%d\n",
553 (supported_nfs_versions & NFSV2_FLAG) ?
347a9015 554 2 : 3,
d89ff2df
JH
555 ntohl(rpc_pkt.u.reply.data[0]),
556 ntohl(rpc_pkt.u.reply.data[1]));
557 puts("\n");
b0baca98 558 }
69fd0d41 559 break;
b0baca98
GG
560 case NFS_RPC_PROG_UNAVAIL:
561 case NFS_RPC_PROC_UNAVAIL:
562 case NFS_RPC_GARBAGE_ARGS:
563 case NFS_RPC_SYSTEM_ERR:
69fd0d41 564 default: /* Unknown error on 'accept state' flag */
d89ff2df
JH
565 debug("*** ERROR: accept state error (%d)\n",
566 ntohl(rpc_pkt.u.reply.astatus));
69fd0d41
GG
567 break;
568 }
cbd8a35c 569 return -1;
69fd0d41 570 }
cbd8a35c 571
b0baca98 572 if (supported_nfs_versions & NFSV2_FLAG) {
5d14ee4e 573 if (((uchar *)&(rpc_pkt.u.reply.data[0]) - (uchar *)(&rpc_pkt) + NFS_FHSIZE) > len)
574 return -NFS_RPC_DROP;
b0baca98
GG
575 memcpy(filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
576 } else { /* NFSV3_FLAG */
577 filefh3_length = ntohl(rpc_pkt.u.reply.data[1]);
578 if (filefh3_length > NFS3_FHSIZE)
579 filefh3_length = NFS3_FHSIZE;
5d14ee4e 580 if (((uchar *)&(rpc_pkt.u.reply.data[0]) - (uchar *)(&rpc_pkt) + filefh3_length) > len)
581 return -NFS_RPC_DROP;
5280c769 582 memcpy(filefh, rpc_pkt.u.reply.data + 2, filefh3_length);
b0baca98 583 }
cbd8a35c
WD
584
585 return 0;
586}
587
051ed9af
JH
588static int nfs3_get_attributes_offset(uint32_t *data)
589{
15eea9a1 590 if (data[1]) {
051ed9af 591 /* 'attributes_follow' flag is TRUE,
c629c45f 592 * so we have attributes on 21 dwords */
051ed9af
JH
593 /* Skip unused values :
594 type; 32 bits value,
595 mode; 32 bits value,
596 nlink; 32 bits value,
597 uid; 32 bits value,
598 gid; 32 bits value,
599 size; 64 bits value,
600 used; 64 bits value,
601 rdev; 64 bits value,
602 fsid; 64 bits value,
603 fileid; 64 bits value,
604 atime; 64 bits value,
605 mtime; 64 bits value,
606 ctime; 64 bits value,
607 */
608 return 22;
609 } else {
610 /* 'attributes_follow' flag is FALSE,
611 * so we don't have any attributes */
612 return 1;
613 }
614}
615
68c76a3a 616static int nfs_readlink_reply(uchar *pkt, unsigned len)
cbd8a35c
WD
617{
618 struct rpc_t rpc_pkt;
619 int rlen;
051ed9af 620 int nfsv3_data_offset = 0;
cbd8a35c 621
0ebf04c6 622 debug("%s\n", __func__);
cbd8a35c 623
c9f6c91b 624 memcpy((unsigned char *)&rpc_pkt, pkt, len);
cbd8a35c 625
fa84fa70
MB
626 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
627 return -NFS_RPC_ERR;
628 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
629 return -NFS_RPC_DROP;
c3f9d493 630
cbd8a35c
WD
631 if (rpc_pkt.u.reply.rstatus ||
632 rpc_pkt.u.reply.verifier ||
633 rpc_pkt.u.reply.astatus ||
c9f6c91b 634 rpc_pkt.u.reply.data[0])
cbd8a35c 635 return -1;
cbd8a35c 636
051ed9af
JH
637 if (!(supported_nfs_versions & NFSV2_FLAG)) { /* NFSV3_FLAG */
638 nfsv3_data_offset =
639 nfs3_get_attributes_offset(rpc_pkt.u.reply.data);
640 }
b0baca98 641
051ed9af
JH
642 /* new path length */
643 rlen = ntohl(rpc_pkt.u.reply.data[1 + nfsv3_data_offset]);
644
cf3a4f1e 645 if (((uchar *)&(rpc_pkt.u.reply.data[0]) - (uchar *)(&rpc_pkt) + rlen) > len)
646 return -NFS_RPC_DROP;
647
051ed9af
JH
648 if (*((char *)&(rpc_pkt.u.reply.data[2 + nfsv3_data_offset])) != '/') {
649 int pathlen;
650
651 strcat(nfs_path, "/");
652 pathlen = strlen(nfs_path);
653 memcpy(nfs_path + pathlen,
654 (uchar *)&(rpc_pkt.u.reply.data[2 + nfsv3_data_offset]),
655 rlen);
656 nfs_path[pathlen + rlen] = 0;
657 } else {
658 memcpy(nfs_path,
659 (uchar *)&(rpc_pkt.u.reply.data[2 + nfsv3_data_offset]),
660 rlen);
661 nfs_path[rlen] = 0;
cbd8a35c
WD
662 }
663 return 0;
664}
665
68c76a3a 666static int nfs_read_reply(uchar *pkt, unsigned len)
cbd8a35c
WD
667{
668 struct rpc_t rpc_pkt;
669 int rlen;
b0baca98 670 uchar *data_ptr;
cbd8a35c 671
0ebf04c6 672 debug("%s\n", __func__);
cbd8a35c 673
0517cc45 674 memcpy(&rpc_pkt.u.data[0], pkt, sizeof(rpc_pkt.u.reply));
cbd8a35c 675
fa84fa70
MB
676 if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
677 return -NFS_RPC_ERR;
678 else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
679 return -NFS_RPC_DROP;
c3f9d493 680
cbd8a35c
WD
681 if (rpc_pkt.u.reply.rstatus ||
682 rpc_pkt.u.reply.verifier ||
683 rpc_pkt.u.reply.astatus ||
684 rpc_pkt.u.reply.data[0]) {
c9f6c91b 685 if (rpc_pkt.u.reply.rstatus)
cbd8a35c 686 return -9999;
c9f6c91b 687 if (rpc_pkt.u.reply.astatus)
cbd8a35c 688 return -9999;
c9f6c91b 689 return -ntohl(rpc_pkt.u.reply.data[0]);
cbd8a35c
WD
690 }
691
c9f6c91b
JH
692 if ((nfs_offset != 0) && !((nfs_offset) %
693 (NFS_READ_SIZE / 2 * 10 * HASHES_PER_LINE)))
694 puts("\n\t ");
695 if (!(nfs_offset % ((NFS_READ_SIZE / 2) * 10)))
696 putc('#');
cbd8a35c 697
b0baca98
GG
698 if (supported_nfs_versions & NFSV2_FLAG) {
699 rlen = ntohl(rpc_pkt.u.reply.data[18]);
700 data_ptr = (uchar *)&(rpc_pkt.u.reply.data[19]);
701 } else { /* NFSV3_FLAG */
051ed9af
JH
702 int nfsv3_data_offset =
703 nfs3_get_attributes_offset(rpc_pkt.u.reply.data);
704
705 /* count value */
706 rlen = ntohl(rpc_pkt.u.reply.data[1 + nfsv3_data_offset]);
707 /* Skip unused values :
708 EOF: 32 bits value,
709 data_size: 32 bits value,
710 */
711 data_ptr = (uchar *)
712 &(rpc_pkt.u.reply.data[4 + nfsv3_data_offset]);
b0baca98
GG
713 }
714
aa207cf3 715 if (((uchar *)&(rpc_pkt.u.reply.data[0]) - (uchar *)(&rpc_pkt) + rlen) > len)
716 return -9999;
717
b0baca98
GG
718 if (store_block(data_ptr, nfs_offset, rlen))
719 return -9999;
cbd8a35c
WD
720
721 return rlen;
722}
723
724/**************************************************************************
725Interfaces of U-BOOT
726**************************************************************************/
68c76a3a 727static void nfs_timeout_handler(void)
a5725fab 728{
68c76a3a 729 if (++nfs_timeout_count > NFS_RETRY_COUNT) {
c9f6c91b 730 puts("\nRetry count exceeded; starting again\n");
bc0571fc 731 net_start_again();
aabb8cb0
ES
732 } else {
733 puts("T ");
bc0571fc
JH
734 net_set_timeout_handler(nfs_timeout +
735 NFS_TIMEOUT * nfs_timeout_count,
736 nfs_timeout_handler);
68c76a3a 737 nfs_send();
fe891ecf 738 }
a5725fab
WD
739}
740
049a95a7
JH
741static void nfs_handler(uchar *pkt, unsigned dest, struct in_addr sip,
742 unsigned src, unsigned len)
cbd8a35c
WD
743{
744 int rlen;
fa84fa70 745 int reply;
cbd8a35c 746
0ebf04c6 747 debug("%s\n", __func__);
cbd8a35c 748
741a8a08 749 if (len > sizeof(struct rpc_t))
750 return;
751
68c76a3a 752 if (dest != nfs_our_port)
c9f6c91b 753 return;
cbd8a35c 754
68c76a3a 755 switch (nfs_state) {
cbd8a35c 756 case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
fa84fa70
MB
757 if (rpc_lookup_reply(PROG_MOUNT, pkt, len) == -NFS_RPC_DROP)
758 break;
68c76a3a
JH
759 nfs_state = STATE_PRCLOOKUP_PROG_NFS_REQ;
760 nfs_send();
cbd8a35c
WD
761 break;
762
763 case STATE_PRCLOOKUP_PROG_NFS_REQ:
fa84fa70
MB
764 if (rpc_lookup_reply(PROG_NFS, pkt, len) == -NFS_RPC_DROP)
765 break;
68c76a3a
JH
766 nfs_state = STATE_MOUNT_REQ;
767 nfs_send();
cbd8a35c
WD
768 break;
769
770 case STATE_MOUNT_REQ:
fa84fa70 771 reply = nfs_mount_reply(pkt, len);
68c76a3a 772 if (reply == -NFS_RPC_DROP) {
fa84fa70 773 break;
68c76a3a 774 } else if (reply == -NFS_RPC_ERR) {
c9f6c91b 775 puts("*** ERROR: Cannot mount\n");
cbd8a35c 776 /* just to be sure... */
68c76a3a
JH
777 nfs_state = STATE_UMOUNT_REQ;
778 nfs_send();
cbd8a35c 779 } else {
68c76a3a
JH
780 nfs_state = STATE_LOOKUP_REQ;
781 nfs_send();
cbd8a35c
WD
782 }
783 break;
784
785 case STATE_UMOUNT_REQ:
fa84fa70 786 reply = nfs_umountall_reply(pkt, len);
68c76a3a 787 if (reply == -NFS_RPC_DROP) {
fa84fa70 788 break;
68c76a3a 789 } else if (reply == -NFS_RPC_ERR) {
d89ff2df 790 debug("*** ERROR: Cannot umount\n");
22f6e99d 791 net_set_state(NETLOOP_FAIL);
cbd8a35c 792 } else {
c9f6c91b 793 puts("\ndone\n");
22f6e99d 794 net_set_state(nfs_download_state);
cbd8a35c
WD
795 }
796 break;
797
798 case STATE_LOOKUP_REQ:
fa84fa70 799 reply = nfs_lookup_reply(pkt, len);
68c76a3a 800 if (reply == -NFS_RPC_DROP) {
fa84fa70 801 break;
68c76a3a 802 } else if (reply == -NFS_RPC_ERR) {
c9f6c91b 803 puts("*** ERROR: File lookup fail\n");
68c76a3a
JH
804 nfs_state = STATE_UMOUNT_REQ;
805 nfs_send();
347a9015
JH
806 } else if (reply == -NFS_RPC_PROG_MISMATCH &&
807 supported_nfs_versions != 0) {
b0baca98
GG
808 /* umount */
809 nfs_state = STATE_UMOUNT_REQ;
810 nfs_send();
811 /* And retry with another supported version */
812 nfs_state = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
813 nfs_send();
cbd8a35c 814 } else {
68c76a3a 815 nfs_state = STATE_READ_REQ;
cbd8a35c
WD
816 nfs_offset = 0;
817 nfs_len = NFS_READ_SIZE;
68c76a3a 818 nfs_send();
cbd8a35c
WD
819 }
820 break;
821
822 case STATE_READLINK_REQ:
fa84fa70 823 reply = nfs_readlink_reply(pkt, len);
68c76a3a 824 if (reply == -NFS_RPC_DROP) {
fa84fa70 825 break;
68c76a3a 826 } else if (reply == -NFS_RPC_ERR) {
c9f6c91b 827 puts("*** ERROR: Symlink fail\n");
68c76a3a
JH
828 nfs_state = STATE_UMOUNT_REQ;
829 nfs_send();
cbd8a35c 830 } else {
0ebf04c6 831 debug("Symlink --> %s\n", nfs_path);
c9f6c91b
JH
832 nfs_filename = basename(nfs_path);
833 nfs_path = dirname(nfs_path);
cbd8a35c 834
68c76a3a
JH
835 nfs_state = STATE_MOUNT_REQ;
836 nfs_send();
cbd8a35c
WD
837 }
838 break;
839
840 case STATE_READ_REQ:
c9f6c91b 841 rlen = nfs_read_reply(pkt, len);
d48d40a0
VK
842 if (rlen == -NFS_RPC_DROP)
843 break;
bc0571fc 844 net_set_timeout_handler(nfs_timeout, nfs_timeout_handler);
cbd8a35c
WD
845 if (rlen > 0) {
846 nfs_offset += rlen;
68c76a3a 847 nfs_send();
c9f6c91b 848 } else if ((rlen == -NFSERR_ISDIR) || (rlen == -NFSERR_INVAL)) {
cbd8a35c 849 /* symbolic link */
68c76a3a
JH
850 nfs_state = STATE_READLINK_REQ;
851 nfs_send();
cbd8a35c 852 } else {
c9f6c91b 853 if (!rlen)
22f6e99d 854 nfs_download_state = NETLOOP_SUCCESS;
b0baca98 855 if (rlen < 0)
d89ff2df 856 debug("NFS READ error (%d)\n", rlen);
68c76a3a
JH
857 nfs_state = STATE_UMOUNT_REQ;
858 nfs_send();
cbd8a35c
WD
859 }
860 break;
861 }
862}
863
cbd8a35c 864
68c76a3a 865void nfs_start(void)
cbd8a35c 866{
0ebf04c6 867 debug("%s\n", __func__);
22f6e99d 868 nfs_download_state = NETLOOP_FAIL;
cbd8a35c 869
049a95a7 870 nfs_server_ip = net_server_ip;
cbd8a35c
WD
871 nfs_path = (char *)nfs_path_buff;
872
873 if (nfs_path == NULL) {
22f6e99d 874 net_set_state(NETLOOP_FAIL);
faecf84a 875 printf("*** ERROR: Fail allocate memory\n");
cbd8a35c
WD
876 return;
877 }
878
6ab12830
JH
879 if (!net_parse_bootfile(&nfs_server_ip, nfs_path,
880 sizeof(nfs_path_buff))) {
f8b26c7a 881 sprintf(nfs_path, "/nfsroot/%02X%02X%02X%02X.img",
049a95a7
JH
882 net_ip.s_addr & 0xFF,
883 (net_ip.s_addr >> 8) & 0xFF,
884 (net_ip.s_addr >> 16) & 0xFF,
885 (net_ip.s_addr >> 24) & 0xFF);
cbd8a35c 886
faecf84a
JH
887 printf("*** Warning: no boot file name; using '%s'\n",
888 nfs_path);
cbd8a35c
WD
889 }
890
c9f6c91b
JH
891 nfs_filename = basename(nfs_path);
892 nfs_path = dirname(nfs_path);
cbd8a35c 893
faecf84a 894 printf("Using %s device\n", eth_get_name());
cbd8a35c 895
faecf84a
JH
896 printf("File transfer via NFS from server %pI4; our IP address is %pI4",
897 &nfs_server_ip, &net_ip);
cbd8a35c
WD
898
899 /* Check if we need to send across this subnet */
049a95a7
JH
900 if (net_gateway.s_addr && net_netmask.s_addr) {
901 struct in_addr our_net;
902 struct in_addr server_net;
cbd8a35c 903
049a95a7 904 our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
347e32b0 905 server_net.s_addr = nfs_server_ip.s_addr & net_netmask.s_addr;
049a95a7 906 if (our_net.s_addr != server_net.s_addr)
faecf84a
JH
907 printf("; sending through gateway %pI4",
908 &net_gateway);
cbd8a35c 909 }
faecf84a 910 printf("\nFilename '%s/%s'.", nfs_path, nfs_filename);
cbd8a35c 911
1411157d 912 if (net_boot_file_expected_size_in_blocks) {
faecf84a
JH
913 printf(" Size is 0x%x Bytes = ",
914 net_boot_file_expected_size_in_blocks << 9);
1411157d 915 print_size(net_boot_file_expected_size_in_blocks << 9, "");
cbd8a35c 916 }
bb872dd9 917 printf("\nLoad address: 0x%lx\nLoading: *\b", image_load_addr);
cbd8a35c 918
bc0571fc 919 net_set_timeout_handler(nfs_timeout, nfs_timeout_handler);
049a95a7 920 net_set_udp_handler(nfs_handler);
cbd8a35c 921
68c76a3a
JH
922 nfs_timeout_count = 0;
923 nfs_state = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
cbd8a35c 924
68c76a3a 925 /*nfs_our_port = 4096 + (get_ticks() % 3072);*/
cbd8a35c 926 /*FIX ME !!!*/
68c76a3a 927 nfs_our_port = 1000;
cbd8a35c
WD
928
929 /* zero out server ether in case the server ip has changed */
0adb5b76 930 memset(net_server_ethaddr, 0, 6);
cbd8a35c 931
68c76a3a 932 nfs_send();
cbd8a35c 933}
This page took 0.463845 seconds and 4 git commands to generate.