2
* NFS support driver - based on etherboot and U-BOOT's tftp.c
4
* Masami Komiya <mkomiya@sonare.it> 2004
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. */
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. */
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. */
32
#define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
33
#define NFS_RETRY_COUNT 30
34
#ifndef CONFIG_NFS_TIMEOUT
35
# define NFS_TIMEOUT 2000UL
37
# define NFS_TIMEOUT CONFIG_NFS_TIMEOUT
41
#define NFS_RPC_DROP 124
43
static int fs_mounted;
44
static unsigned long rpc_id;
45
static int nfs_offset = -1;
47
static ulong nfs_timeout = NFS_TIMEOUT;
49
static char dirfh[NFS_FHSIZE]; /* file handle of directory */
50
static char filefh[NFS_FHSIZE]; /* file handle of kernel image */
52
static enum net_loop_state nfs_download_state;
53
static IPaddr_t NfsServerIP;
54
static int NfsSrvMountPort;
55
static int NfsSrvNfsPort;
56
static int NfsOurPort;
57
static int NfsTimeoutCount;
59
#define STATE_PRCLOOKUP_PROG_MOUNT_REQ 1
60
#define STATE_PRCLOOKUP_PROG_NFS_REQ 2
61
#define STATE_MOUNT_REQ 3
62
#define STATE_UMOUNT_REQ 4
63
#define STATE_LOOKUP_REQ 5
64
#define STATE_READ_REQ 6
65
#define STATE_READLINK_REQ 7
67
static char default_filename[64];
68
static char *nfs_filename;
69
static char *nfs_path;
70
static char nfs_path_buff[2048];
73
store_block(uchar *src, unsigned offset, unsigned len)
75
ulong newsize = offset + len;
76
#ifdef CONFIG_SYS_DIRECT_FLASH_NFS
79
for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
80
/* start address in flash? */
81
if (load_addr + offset >= flash_info[i].start[0]) {
87
if (rc) { /* Flash is destination for this packet */
88
rc = flash_write((uchar *)src, (ulong)(load_addr+offset), len);
94
#endif /* CONFIG_SYS_DIRECT_FLASH_NFS */
96
(void)memcpy((void *)(load_addr + offset), src, len);
99
if (NetBootFileXferSize < (offset+len))
100
NetBootFileXferSize = newsize;
109
fname = path + strlen(path) - 1;
110
while (fname >= path) {
125
fname = basename(path);
131
/**************************************************************************
132
RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
133
**************************************************************************/
134
static long *rpc_add_credentials(long *p)
140
strcpy(hostname, "");
141
hostnamelen = strlen(hostname);
143
/* Here's the executive summary on authentication requirements of the
144
* various NFS server implementations: Linux accepts both AUTH_NONE
145
* and AUTH_UNIX authentication (also accepts an empty hostname field
146
* in the AUTH_UNIX scheme). *BSD refuses AUTH_NONE, but accepts
147
* AUTH_UNIX (also accepts an empty hostname field in the AUTH_UNIX
148
* scheme). To be safe, use AUTH_UNIX and pass the hostname if we have
149
* it (if the BOOTP/DHCP reply didn't give one, just use an empty
152
hl = (hostnamelen + 3) & ~3;
154
/* Provide an AUTH_UNIX credential. */
155
*p++ = htonl(1); /* AUTH_UNIX */
156
*p++ = htonl(hl+20); /* auth length */
157
*p++ = htonl(0); /* stamp */
158
*p++ = htonl(hostnamelen); /* hostname string */
160
*(p + hostnamelen / 4) = 0; /* add zero padding */
161
memcpy(p, hostname, hostnamelen);
165
*p++ = 0; /* auxiliary gid list */
167
/* Provide an AUTH_NONE verifier. */
168
*p++ = 0; /* AUTH_NONE */
169
*p++ = 0; /* auth length */
174
/**************************************************************************
175
RPC_LOOKUP - Lookup RPC Port numbers
176
**************************************************************************/
178
rpc_req(int rpc_prog, int rpc_proc, uint32_t *data, int datalen)
187
pkt.u.call.id = htonl(id);
188
pkt.u.call.type = htonl(MSG_CALL);
189
pkt.u.call.rpcvers = htonl(2); /* use RPC version 2 */
190
pkt.u.call.prog = htonl(rpc_prog);
191
pkt.u.call.vers = htonl(2); /* portmapper is version 2 */
192
pkt.u.call.proc = htonl(rpc_proc);
193
p = (uint32_t *)&(pkt.u.call.data);
196
memcpy((char *)p, (char *)data, datalen*sizeof(uint32_t));
198
pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt;
200
memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE,
201
(char *)&pkt, pktlen);
203
if (rpc_prog == PROG_PORTMAP)
205
else if (rpc_prog == PROG_MOUNT)
206
sport = NfsSrvMountPort;
208
sport = NfsSrvNfsPort;
210
NetSendUDPPacket(NetServerEther, NfsServerIP, sport, NfsOurPort,
214
/**************************************************************************
215
RPC_LOOKUP - Lookup RPC Port numbers
216
**************************************************************************/
218
rpc_lookup_req(int prog, int ver)
222
data[0] = 0; data[1] = 0; /* auth credential */
223
data[2] = 0; data[3] = 0; /* auth verifier */
224
data[4] = htonl(prog);
225
data[5] = htonl(ver);
226
data[6] = htonl(17); /* IP_UDP */
229
rpc_req(PROG_PORTMAP, PORTMAP_GETPORT, data, 8);
232
/**************************************************************************
233
NFS_MOUNT - Mount an NFS Filesystem
234
**************************************************************************/
236
nfs_mount_req(char *path)
243
pathlen = strlen(path);
246
p = (uint32_t *)rpc_add_credentials((long *)p);
248
*p++ = htonl(pathlen);
250
*(p + pathlen / 4) = 0;
251
memcpy(p, path, pathlen);
252
p += (pathlen + 3) / 4;
254
len = (uint32_t *)p - (uint32_t *)&(data[0]);
256
rpc_req(PROG_MOUNT, MOUNT_ADDENTRY, data, len);
259
/**************************************************************************
260
NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
261
**************************************************************************/
263
nfs_umountall_req(void)
269
if ((NfsSrvMountPort == -1) || (!fs_mounted))
270
/* Nothing mounted, nothing to umount */
274
p = (uint32_t *)rpc_add_credentials((long *)p);
276
len = (uint32_t *)p - (uint32_t *)&(data[0]);
278
rpc_req(PROG_MOUNT, MOUNT_UMOUNTALL, data, len);
281
/***************************************************************************
282
* NFS_READLINK (AH 2003-07-14)
283
* This procedure is called when read of the first block fails -
284
* this probably happens when it's a directory or a symlink
285
* In case of successful readlink(), the dirname is manipulated,
286
* so that inside the nfs() function a recursion can be done.
287
**************************************************************************/
289
nfs_readlink_req(void)
296
p = (uint32_t *)rpc_add_credentials((long *)p);
298
memcpy(p, filefh, NFS_FHSIZE);
299
p += (NFS_FHSIZE / 4);
301
len = (uint32_t *)p - (uint32_t *)&(data[0]);
303
rpc_req(PROG_NFS, NFS_READLINK, data, len);
306
/**************************************************************************
307
NFS_LOOKUP - Lookup Pathname
308
**************************************************************************/
310
nfs_lookup_req(char *fname)
317
fnamelen = strlen(fname);
320
p = (uint32_t *)rpc_add_credentials((long *)p);
322
memcpy(p, dirfh, NFS_FHSIZE);
323
p += (NFS_FHSIZE / 4);
324
*p++ = htonl(fnamelen);
326
*(p + fnamelen / 4) = 0;
327
memcpy(p, fname, fnamelen);
328
p += (fnamelen + 3) / 4;
330
len = (uint32_t *)p - (uint32_t *)&(data[0]);
332
rpc_req(PROG_NFS, NFS_LOOKUP, data, len);
335
/**************************************************************************
336
NFS_READ - Read File on NFS Server
337
**************************************************************************/
339
nfs_read_req(int offset, int readlen)
346
p = (uint32_t *)rpc_add_credentials((long *)p);
348
memcpy(p, filefh, NFS_FHSIZE);
349
p += (NFS_FHSIZE / 4);
350
*p++ = htonl(offset);
351
*p++ = htonl(readlen);
354
len = (uint32_t *)p - (uint32_t *)&(data[0]);
356
rpc_req(PROG_NFS, NFS_READ, data, len);
359
/**************************************************************************
360
RPC request dispatcher
361
**************************************************************************/
366
debug("%s\n", __func__);
369
case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
370
rpc_lookup_req(PROG_MOUNT, 1);
372
case STATE_PRCLOOKUP_PROG_NFS_REQ:
373
rpc_lookup_req(PROG_NFS, 2);
375
case STATE_MOUNT_REQ:
376
nfs_mount_req(nfs_path);
378
case STATE_UMOUNT_REQ:
381
case STATE_LOOKUP_REQ:
382
nfs_lookup_req(nfs_filename);
385
nfs_read_req(nfs_offset, nfs_len);
387
case STATE_READLINK_REQ:
393
/**************************************************************************
394
Handlers for the reply from server
395
**************************************************************************/
398
rpc_lookup_reply(int prog, uchar *pkt, unsigned len)
400
struct rpc_t rpc_pkt;
402
memcpy((unsigned char *)&rpc_pkt, pkt, len);
404
debug("%s\n", __func__);
406
if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
408
else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
409
return -NFS_RPC_DROP;
411
if (rpc_pkt.u.reply.rstatus ||
412
rpc_pkt.u.reply.verifier ||
413
rpc_pkt.u.reply.astatus)
418
NfsSrvMountPort = ntohl(rpc_pkt.u.reply.data[0]);
421
NfsSrvNfsPort = ntohl(rpc_pkt.u.reply.data[0]);
429
nfs_mount_reply(uchar *pkt, unsigned len)
431
struct rpc_t rpc_pkt;
433
debug("%s\n", __func__);
435
memcpy((unsigned char *)&rpc_pkt, pkt, len);
437
if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
439
else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
440
return -NFS_RPC_DROP;
442
if (rpc_pkt.u.reply.rstatus ||
443
rpc_pkt.u.reply.verifier ||
444
rpc_pkt.u.reply.astatus ||
445
rpc_pkt.u.reply.data[0])
449
memcpy(dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
455
nfs_umountall_reply(uchar *pkt, unsigned len)
457
struct rpc_t rpc_pkt;
459
debug("%s\n", __func__);
461
memcpy((unsigned char *)&rpc_pkt, pkt, len);
463
if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
465
else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
466
return -NFS_RPC_DROP;
468
if (rpc_pkt.u.reply.rstatus ||
469
rpc_pkt.u.reply.verifier ||
470
rpc_pkt.u.reply.astatus)
474
memset(dirfh, 0, sizeof(dirfh));
480
nfs_lookup_reply(uchar *pkt, unsigned len)
482
struct rpc_t rpc_pkt;
484
debug("%s\n", __func__);
486
memcpy((unsigned char *)&rpc_pkt, pkt, len);
488
if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
490
else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
491
return -NFS_RPC_DROP;
493
if (rpc_pkt.u.reply.rstatus ||
494
rpc_pkt.u.reply.verifier ||
495
rpc_pkt.u.reply.astatus ||
496
rpc_pkt.u.reply.data[0])
499
memcpy(filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
505
nfs_readlink_reply(uchar *pkt, unsigned len)
507
struct rpc_t rpc_pkt;
510
debug("%s\n", __func__);
512
memcpy((unsigned char *)&rpc_pkt, pkt, len);
514
if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
516
else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
517
return -NFS_RPC_DROP;
519
if (rpc_pkt.u.reply.rstatus ||
520
rpc_pkt.u.reply.verifier ||
521
rpc_pkt.u.reply.astatus ||
522
rpc_pkt.u.reply.data[0])
525
rlen = ntohl(rpc_pkt.u.reply.data[1]); /* new path length */
527
if (*((char *)&(rpc_pkt.u.reply.data[2])) != '/') {
529
strcat(nfs_path, "/");
530
pathlen = strlen(nfs_path);
531
memcpy(nfs_path + pathlen, (uchar *)&(rpc_pkt.u.reply.data[2]),
533
nfs_path[pathlen + rlen] = 0;
535
memcpy(nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen);
542
nfs_read_reply(uchar *pkt, unsigned len)
544
struct rpc_t rpc_pkt;
547
debug("%s\n", __func__);
549
memcpy((uchar *)&rpc_pkt, pkt, sizeof(rpc_pkt.u.reply));
551
if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
553
else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
554
return -NFS_RPC_DROP;
556
if (rpc_pkt.u.reply.rstatus ||
557
rpc_pkt.u.reply.verifier ||
558
rpc_pkt.u.reply.astatus ||
559
rpc_pkt.u.reply.data[0]) {
560
if (rpc_pkt.u.reply.rstatus)
562
if (rpc_pkt.u.reply.astatus)
564
return -ntohl(rpc_pkt.u.reply.data[0]);
567
if ((nfs_offset != 0) && !((nfs_offset) %
568
(NFS_READ_SIZE / 2 * 10 * HASHES_PER_LINE)))
570
if (!(nfs_offset % ((NFS_READ_SIZE / 2) * 10)))
573
rlen = ntohl(rpc_pkt.u.reply.data[18]);
574
if (store_block((uchar *)pkt + sizeof(rpc_pkt.u.reply),
581
/**************************************************************************
583
**************************************************************************/
588
if (++NfsTimeoutCount > NFS_RETRY_COUNT) {
589
puts("\nRetry count exceeded; starting again\n");
593
NetSetTimeout(nfs_timeout + NFS_TIMEOUT * NfsTimeoutCount,
600
NfsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len)
605
debug("%s\n", __func__);
607
if (dest != NfsOurPort)
611
case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
612
if (rpc_lookup_reply(PROG_MOUNT, pkt, len) == -NFS_RPC_DROP)
614
NfsState = STATE_PRCLOOKUP_PROG_NFS_REQ;
618
case STATE_PRCLOOKUP_PROG_NFS_REQ:
619
if (rpc_lookup_reply(PROG_NFS, pkt, len) == -NFS_RPC_DROP)
621
NfsState = STATE_MOUNT_REQ;
625
case STATE_MOUNT_REQ:
626
reply = nfs_mount_reply(pkt, len);
627
if (reply == -NFS_RPC_DROP)
629
else if (reply == -NFS_RPC_ERR) {
630
puts("*** ERROR: Cannot mount\n");
631
/* just to be sure... */
632
NfsState = STATE_UMOUNT_REQ;
635
NfsState = STATE_LOOKUP_REQ;
640
case STATE_UMOUNT_REQ:
641
reply = nfs_umountall_reply(pkt, len);
642
if (reply == -NFS_RPC_DROP)
644
else if (reply == -NFS_RPC_ERR) {
645
puts("*** ERROR: Cannot umount\n");
646
net_set_state(NETLOOP_FAIL);
649
net_set_state(nfs_download_state);
653
case STATE_LOOKUP_REQ:
654
reply = nfs_lookup_reply(pkt, len);
655
if (reply == -NFS_RPC_DROP)
657
else if (reply == -NFS_RPC_ERR) {
658
puts("*** ERROR: File lookup fail\n");
659
NfsState = STATE_UMOUNT_REQ;
662
NfsState = STATE_READ_REQ;
664
nfs_len = NFS_READ_SIZE;
669
case STATE_READLINK_REQ:
670
reply = nfs_readlink_reply(pkt, len);
671
if (reply == -NFS_RPC_DROP)
673
else if (reply == -NFS_RPC_ERR) {
674
puts("*** ERROR: Symlink fail\n");
675
NfsState = STATE_UMOUNT_REQ;
678
debug("Symlink --> %s\n", nfs_path);
679
nfs_filename = basename(nfs_path);
680
nfs_path = dirname(nfs_path);
682
NfsState = STATE_MOUNT_REQ;
688
rlen = nfs_read_reply(pkt, len);
689
NetSetTimeout(nfs_timeout, NfsTimeout);
693
} else if ((rlen == -NFSERR_ISDIR) || (rlen == -NFSERR_INVAL)) {
695
NfsState = STATE_READLINK_REQ;
699
nfs_download_state = NETLOOP_SUCCESS;
700
NfsState = STATE_UMOUNT_REQ;
711
debug("%s\n", __func__);
712
nfs_download_state = NETLOOP_FAIL;
714
NfsServerIP = NetServerIP;
715
nfs_path = (char *)nfs_path_buff;
717
if (nfs_path == NULL) {
718
net_set_state(NETLOOP_FAIL);
719
puts("*** ERROR: Fail allocate memory\n");
723
if (BootFile[0] == '\0') {
724
sprintf(default_filename, "/nfsroot/%02X%02X%02X%02X.img",
726
(NetOurIP >> 8) & 0xFF,
727
(NetOurIP >> 16) & 0xFF,
728
(NetOurIP >> 24) & 0xFF);
729
strcpy(nfs_path, default_filename);
731
printf("*** Warning: no boot file name; using '%s'\n",
739
NfsServerIP = string_to_ip(BootFile);
743
strcpy(nfs_path, BootFile);
747
nfs_filename = basename(nfs_path);
748
nfs_path = dirname(nfs_path);
750
printf("Using %s device\n", eth_get_name());
752
printf("File transfer via NFS from server %pI4"
753
"; our IP address is %pI4", &NfsServerIP, &NetOurIP);
755
/* Check if we need to send across this subnet */
756
if (NetOurGatewayIP && NetOurSubnetMask) {
757
IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
758
IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask;
760
if (OurNet != ServerNet)
761
printf("; sending through gateway %pI4",
764
printf("\nFilename '%s/%s'.", nfs_path, nfs_filename);
766
if (NetBootFileSize) {
767
printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
768
print_size(NetBootFileSize<<9, "");
770
printf("\nLoad address: 0x%lx\n"
771
"Loading: *\b", load_addr);
773
NetSetTimeout(nfs_timeout, NfsTimeout);
774
net_set_udp_handler(NfsHandler);
777
NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
779
/*NfsOurPort = 4096 + (get_ticks() % 3072);*/
783
/* zero out server ether in case the server ip has changed */
784
memset(NetServerEther, 0, 6);