]>
Commit | Line | Data |
---|---|---|
cbd8a35c WD |
1 | /* |
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 | ||
25 | #include <common.h> | |
26 | #include <command.h> | |
27 | #include <net.h> | |
28 | #include <malloc.h> | |
55d5fd9a | 29 | #include <mapmem.h> |
cbd8a35c WD |
30 | #include "nfs.h" |
31 | #include "bootp.h" | |
32 | ||
cbd8a35c | 33 | #define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */ |
fe891ecf | 34 | #define NFS_RETRY_COUNT 30 |
48a3e999 TK |
35 | #ifndef CONFIG_NFS_TIMEOUT |
36 | # define NFS_TIMEOUT 2000UL | |
37 | #else | |
38 | # define NFS_TIMEOUT CONFIG_NFS_TIMEOUT | |
39 | #endif | |
cbd8a35c | 40 | |
fa84fa70 MB |
41 | #define NFS_RPC_ERR 1 |
42 | #define NFS_RPC_DROP 124 | |
43 | ||
c9f6c91b JH |
44 | static int fs_mounted; |
45 | static unsigned long rpc_id; | |
cbd8a35c WD |
46 | static int nfs_offset = -1; |
47 | static int nfs_len; | |
fa84fa70 | 48 | static ulong nfs_timeout = NFS_TIMEOUT; |
cbd8a35c WD |
49 | |
50 | static char dirfh[NFS_FHSIZE]; /* file handle of directory */ | |
51 | static char filefh[NFS_FHSIZE]; /* file handle of kernel image */ | |
52 | ||
22f6e99d | 53 | static enum net_loop_state nfs_download_state; |
049a95a7 | 54 | static struct in_addr nfs_server_ip; |
cbd8a35c WD |
55 | static int NfsSrvMountPort; |
56 | static int NfsSrvNfsPort; | |
57 | static int NfsOurPort; | |
58 | static int NfsTimeoutCount; | |
59 | static int NfsState; | |
60 | #define STATE_PRCLOOKUP_PROG_MOUNT_REQ 1 | |
61 | #define STATE_PRCLOOKUP_PROG_NFS_REQ 2 | |
62 | #define STATE_MOUNT_REQ 3 | |
63 | #define STATE_UMOUNT_REQ 4 | |
64 | #define STATE_LOOKUP_REQ 5 | |
65 | #define STATE_READ_REQ 6 | |
66 | #define STATE_READLINK_REQ 7 | |
67 | ||
68 | static char default_filename[64]; | |
69 | static char *nfs_filename; | |
70 | static char *nfs_path; | |
71 | static char nfs_path_buff[2048]; | |
72 | ||
c9f6c91b JH |
73 | static inline int |
74 | store_block(uchar *src, unsigned offset, unsigned len) | |
cbd8a35c | 75 | { |
a084f7da | 76 | ulong newsize = offset + len; |
6d0f6bcf | 77 | #ifdef CONFIG_SYS_DIRECT_FLASH_NFS |
cbd8a35c WD |
78 | int i, rc = 0; |
79 | ||
c9f6c91b | 80 | for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) { |
cbd8a35c WD |
81 | /* start address in flash? */ |
82 | if (load_addr + offset >= flash_info[i].start[0]) { | |
83 | rc = 1; | |
84 | break; | |
85 | } | |
86 | } | |
87 | ||
88 | if (rc) { /* Flash is destination for this packet */ | |
c9f6c91b | 89 | rc = flash_write((uchar *)src, (ulong)(load_addr+offset), len); |
cbd8a35c | 90 | if (rc) { |
c9f6c91b | 91 | flash_perror(rc); |
23a7a32d | 92 | return -1; |
cbd8a35c WD |
93 | } |
94 | } else | |
6d0f6bcf | 95 | #endif /* CONFIG_SYS_DIRECT_FLASH_NFS */ |
cbd8a35c | 96 | { |
55d5fd9a JH |
97 | void *ptr = map_sysmem(load_addr + offset, len); |
98 | ||
99 | memcpy(ptr, src, len); | |
100 | unmap_sysmem(ptr); | |
cbd8a35c | 101 | } |
a084f7da | 102 | |
1411157d JH |
103 | if (net_boot_file_size < (offset + len)) |
104 | net_boot_file_size = newsize; | |
23a7a32d | 105 | return 0; |
cbd8a35c WD |
106 | } |
107 | ||
108 | static char* | |
c9f6c91b | 109 | basename(char *path) |
cbd8a35c WD |
110 | { |
111 | char *fname; | |
112 | ||
113 | fname = path + strlen(path) - 1; | |
114 | while (fname >= path) { | |
115 | if (*fname == '/') { | |
116 | fname++; | |
117 | break; | |
118 | } | |
119 | fname--; | |
120 | } | |
121 | return fname; | |
122 | } | |
123 | ||
124 | static char* | |
c9f6c91b | 125 | dirname(char *path) |
cbd8a35c WD |
126 | { |
127 | char *fname; | |
128 | ||
c9f6c91b | 129 | fname = basename(path); |
cbd8a35c WD |
130 | --fname; |
131 | *fname = '\0'; | |
132 | return path; | |
133 | } | |
134 | ||
cbd8a35c WD |
135 | /************************************************************************** |
136 | RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries | |
137 | **************************************************************************/ | |
c9f6c91b | 138 | static long *rpc_add_credentials(long *p) |
cbd8a35c WD |
139 | { |
140 | int hl; | |
141 | int hostnamelen; | |
142 | char hostname[256]; | |
143 | ||
c9f6c91b JH |
144 | strcpy(hostname, ""); |
145 | hostnamelen = strlen(hostname); | |
cbd8a35c WD |
146 | |
147 | /* Here's the executive summary on authentication requirements of the | |
148 | * various NFS server implementations: Linux accepts both AUTH_NONE | |
149 | * and AUTH_UNIX authentication (also accepts an empty hostname field | |
150 | * in the AUTH_UNIX scheme). *BSD refuses AUTH_NONE, but accepts | |
151 | * AUTH_UNIX (also accepts an empty hostname field in the AUTH_UNIX | |
152 | * scheme). To be safe, use AUTH_UNIX and pass the hostname if we have | |
153 | * it (if the BOOTP/DHCP reply didn't give one, just use an empty | |
154 | * hostname). */ | |
155 | ||
156 | hl = (hostnamelen + 3) & ~3; | |
157 | ||
158 | /* Provide an AUTH_UNIX credential. */ | |
159 | *p++ = htonl(1); /* AUTH_UNIX */ | |
160 | *p++ = htonl(hl+20); /* auth length */ | |
161 | *p++ = htonl(0); /* stamp */ | |
162 | *p++ = htonl(hostnamelen); /* hostname string */ | |
c9f6c91b | 163 | if (hostnamelen & 3) |
cbd8a35c | 164 | *(p + hostnamelen / 4) = 0; /* add zero padding */ |
c9f6c91b | 165 | memcpy(p, hostname, hostnamelen); |
cbd8a35c WD |
166 | p += hl / 4; |
167 | *p++ = 0; /* uid */ | |
168 | *p++ = 0; /* gid */ | |
169 | *p++ = 0; /* auxiliary gid list */ | |
170 | ||
171 | /* Provide an AUTH_NONE verifier. */ | |
172 | *p++ = 0; /* AUTH_NONE */ | |
173 | *p++ = 0; /* auth length */ | |
174 | ||
175 | return p; | |
176 | } | |
177 | ||
178 | /************************************************************************** | |
179 | RPC_LOOKUP - Lookup RPC Port numbers | |
180 | **************************************************************************/ | |
181 | static void | |
c9f6c91b | 182 | rpc_req(int rpc_prog, int rpc_proc, uint32_t *data, int datalen) |
cbd8a35c WD |
183 | { |
184 | struct rpc_t pkt; | |
185 | unsigned long id; | |
186 | uint32_t *p; | |
187 | int pktlen; | |
188 | int sport; | |
189 | ||
c3f9d493 | 190 | id = ++rpc_id; |
cbd8a35c WD |
191 | pkt.u.call.id = htonl(id); |
192 | pkt.u.call.type = htonl(MSG_CALL); | |
193 | pkt.u.call.rpcvers = htonl(2); /* use RPC version 2 */ | |
194 | pkt.u.call.prog = htonl(rpc_prog); | |
195 | pkt.u.call.vers = htonl(2); /* portmapper is version 2 */ | |
196 | pkt.u.call.proc = htonl(rpc_proc); | |
197 | p = (uint32_t *)&(pkt.u.call.data); | |
198 | ||
199 | if (datalen) | |
c9f6c91b | 200 | memcpy((char *)p, (char *)data, datalen*sizeof(uint32_t)); |
cbd8a35c WD |
201 | |
202 | pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt; | |
203 | ||
1203fcce JH |
204 | memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE, |
205 | (char *)&pkt, pktlen); | |
cbd8a35c WD |
206 | |
207 | if (rpc_prog == PROG_PORTMAP) | |
208 | sport = SUNRPC_PORT; | |
209 | else if (rpc_prog == PROG_MOUNT) | |
210 | sport = NfsSrvMountPort; | |
211 | else | |
212 | sport = NfsSrvNfsPort; | |
213 | ||
1203fcce JH |
214 | net_send_udp_packet(net_server_ethaddr, nfs_server_ip, sport, |
215 | NfsOurPort, pktlen); | |
cbd8a35c WD |
216 | } |
217 | ||
218 | /************************************************************************** | |
219 | RPC_LOOKUP - Lookup RPC Port numbers | |
220 | **************************************************************************/ | |
221 | static void | |
c9f6c91b | 222 | rpc_lookup_req(int prog, int ver) |
cbd8a35c WD |
223 | { |
224 | uint32_t data[16]; | |
225 | ||
226 | data[0] = 0; data[1] = 0; /* auth credential */ | |
227 | data[2] = 0; data[3] = 0; /* auth verifier */ | |
228 | data[4] = htonl(prog); | |
229 | data[5] = htonl(ver); | |
230 | data[6] = htonl(17); /* IP_UDP */ | |
231 | data[7] = 0; | |
232 | ||
c9f6c91b | 233 | rpc_req(PROG_PORTMAP, PORTMAP_GETPORT, data, 8); |
cbd8a35c WD |
234 | } |
235 | ||
236 | /************************************************************************** | |
237 | NFS_MOUNT - Mount an NFS Filesystem | |
238 | **************************************************************************/ | |
239 | static void | |
c9f6c91b | 240 | nfs_mount_req(char *path) |
cbd8a35c WD |
241 | { |
242 | uint32_t data[1024]; | |
243 | uint32_t *p; | |
244 | int len; | |
245 | int pathlen; | |
246 | ||
c9f6c91b | 247 | pathlen = strlen(path); |
cbd8a35c WD |
248 | |
249 | p = &(data[0]); | |
250 | p = (uint32_t *)rpc_add_credentials((long *)p); | |
251 | ||
252 | *p++ = htonl(pathlen); | |
c9f6c91b JH |
253 | if (pathlen & 3) |
254 | *(p + pathlen / 4) = 0; | |
255 | memcpy(p, path, pathlen); | |
cbd8a35c WD |
256 | p += (pathlen + 3) / 4; |
257 | ||
258 | len = (uint32_t *)p - (uint32_t *)&(data[0]); | |
259 | ||
c9f6c91b | 260 | rpc_req(PROG_MOUNT, MOUNT_ADDENTRY, data, len); |
cbd8a35c WD |
261 | } |
262 | ||
263 | /************************************************************************** | |
264 | NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server | |
265 | **************************************************************************/ | |
266 | static void | |
c9f6c91b | 267 | nfs_umountall_req(void) |
cbd8a35c WD |
268 | { |
269 | uint32_t data[1024]; | |
270 | uint32_t *p; | |
271 | int len; | |
272 | ||
c9f6c91b | 273 | if ((NfsSrvMountPort == -1) || (!fs_mounted)) |
cbd8a35c WD |
274 | /* Nothing mounted, nothing to umount */ |
275 | return; | |
cbd8a35c WD |
276 | |
277 | p = &(data[0]); | |
c9f6c91b | 278 | p = (uint32_t *)rpc_add_credentials((long *)p); |
cbd8a35c WD |
279 | |
280 | len = (uint32_t *)p - (uint32_t *)&(data[0]); | |
281 | ||
c9f6c91b | 282 | rpc_req(PROG_MOUNT, MOUNT_UMOUNTALL, data, len); |
cbd8a35c WD |
283 | } |
284 | ||
285 | /*************************************************************************** | |
286 | * NFS_READLINK (AH 2003-07-14) | |
287 | * This procedure is called when read of the first block fails - | |
288 | * this probably happens when it's a directory or a symlink | |
289 | * In case of successful readlink(), the dirname is manipulated, | |
290 | * so that inside the nfs() function a recursion can be done. | |
291 | **************************************************************************/ | |
292 | static void | |
c9f6c91b | 293 | nfs_readlink_req(void) |
cbd8a35c WD |
294 | { |
295 | uint32_t data[1024]; | |
296 | uint32_t *p; | |
297 | int len; | |
298 | ||
299 | p = &(data[0]); | |
c9f6c91b | 300 | p = (uint32_t *)rpc_add_credentials((long *)p); |
cbd8a35c | 301 | |
c9f6c91b | 302 | memcpy(p, filefh, NFS_FHSIZE); |
cbd8a35c WD |
303 | p += (NFS_FHSIZE / 4); |
304 | ||
305 | len = (uint32_t *)p - (uint32_t *)&(data[0]); | |
306 | ||
c9f6c91b | 307 | rpc_req(PROG_NFS, NFS_READLINK, data, len); |
cbd8a35c WD |
308 | } |
309 | ||
310 | /************************************************************************** | |
311 | NFS_LOOKUP - Lookup Pathname | |
312 | **************************************************************************/ | |
313 | static void | |
c9f6c91b | 314 | nfs_lookup_req(char *fname) |
cbd8a35c WD |
315 | { |
316 | uint32_t data[1024]; | |
317 | uint32_t *p; | |
318 | int len; | |
319 | int fnamelen; | |
320 | ||
c9f6c91b | 321 | fnamelen = strlen(fname); |
cbd8a35c WD |
322 | |
323 | p = &(data[0]); | |
c9f6c91b | 324 | p = (uint32_t *)rpc_add_credentials((long *)p); |
cbd8a35c | 325 | |
c9f6c91b | 326 | memcpy(p, dirfh, NFS_FHSIZE); |
cbd8a35c WD |
327 | p += (NFS_FHSIZE / 4); |
328 | *p++ = htonl(fnamelen); | |
c9f6c91b JH |
329 | if (fnamelen & 3) |
330 | *(p + fnamelen / 4) = 0; | |
331 | memcpy(p, fname, fnamelen); | |
cbd8a35c WD |
332 | p += (fnamelen + 3) / 4; |
333 | ||
334 | len = (uint32_t *)p - (uint32_t *)&(data[0]); | |
335 | ||
c9f6c91b | 336 | rpc_req(PROG_NFS, NFS_LOOKUP, data, len); |
cbd8a35c WD |
337 | } |
338 | ||
339 | /************************************************************************** | |
340 | NFS_READ - Read File on NFS Server | |
341 | **************************************************************************/ | |
342 | static void | |
c9f6c91b | 343 | nfs_read_req(int offset, int readlen) |
cbd8a35c WD |
344 | { |
345 | uint32_t data[1024]; | |
346 | uint32_t *p; | |
347 | int len; | |
348 | ||
349 | p = &(data[0]); | |
c9f6c91b | 350 | p = (uint32_t *)rpc_add_credentials((long *)p); |
cbd8a35c | 351 | |
c9f6c91b | 352 | memcpy(p, filefh, NFS_FHSIZE); |
cbd8a35c WD |
353 | p += (NFS_FHSIZE / 4); |
354 | *p++ = htonl(offset); | |
355 | *p++ = htonl(readlen); | |
356 | *p++ = 0; | |
357 | ||
358 | len = (uint32_t *)p - (uint32_t *)&(data[0]); | |
359 | ||
c9f6c91b | 360 | rpc_req(PROG_NFS, NFS_READ, data, len); |
cbd8a35c WD |
361 | } |
362 | ||
363 | /************************************************************************** | |
364 | RPC request dispatcher | |
365 | **************************************************************************/ | |
366 | ||
367 | static void | |
c9f6c91b | 368 | NfsSend(void) |
cbd8a35c | 369 | { |
0ebf04c6 | 370 | debug("%s\n", __func__); |
cbd8a35c WD |
371 | |
372 | switch (NfsState) { | |
373 | case STATE_PRCLOOKUP_PROG_MOUNT_REQ: | |
c9f6c91b | 374 | rpc_lookup_req(PROG_MOUNT, 1); |
cbd8a35c WD |
375 | break; |
376 | case STATE_PRCLOOKUP_PROG_NFS_REQ: | |
c9f6c91b | 377 | rpc_lookup_req(PROG_NFS, 2); |
cbd8a35c WD |
378 | break; |
379 | case STATE_MOUNT_REQ: | |
c9f6c91b | 380 | nfs_mount_req(nfs_path); |
cbd8a35c WD |
381 | break; |
382 | case STATE_UMOUNT_REQ: | |
c9f6c91b | 383 | nfs_umountall_req(); |
cbd8a35c WD |
384 | break; |
385 | case STATE_LOOKUP_REQ: | |
c9f6c91b | 386 | nfs_lookup_req(nfs_filename); |
cbd8a35c WD |
387 | break; |
388 | case STATE_READ_REQ: | |
c9f6c91b | 389 | nfs_read_req(nfs_offset, nfs_len); |
cbd8a35c WD |
390 | break; |
391 | case STATE_READLINK_REQ: | |
c9f6c91b | 392 | nfs_readlink_req(); |
cbd8a35c WD |
393 | break; |
394 | } | |
395 | } | |
396 | ||
397 | /************************************************************************** | |
398 | Handlers for the reply from server | |
399 | **************************************************************************/ | |
400 | ||
401 | static int | |
c9f6c91b | 402 | rpc_lookup_reply(int prog, uchar *pkt, unsigned len) |
cbd8a35c WD |
403 | { |
404 | struct rpc_t rpc_pkt; | |
405 | ||
c9f6c91b | 406 | memcpy((unsigned char *)&rpc_pkt, pkt, len); |
cbd8a35c | 407 | |
0ebf04c6 | 408 | debug("%s\n", __func__); |
cbd8a35c | 409 | |
fa84fa70 MB |
410 | if (ntohl(rpc_pkt.u.reply.id) > rpc_id) |
411 | return -NFS_RPC_ERR; | |
412 | else if (ntohl(rpc_pkt.u.reply.id) < rpc_id) | |
413 | return -NFS_RPC_DROP; | |
c3f9d493 | 414 | |
cbd8a35c WD |
415 | if (rpc_pkt.u.reply.rstatus || |
416 | rpc_pkt.u.reply.verifier || | |
c9f6c91b | 417 | rpc_pkt.u.reply.astatus) |
c3f9d493 | 418 | return -1; |
cbd8a35c WD |
419 | |
420 | switch (prog) { | |
421 | case PROG_MOUNT: | |
422 | NfsSrvMountPort = ntohl(rpc_pkt.u.reply.data[0]); | |
423 | break; | |
424 | case PROG_NFS: | |
425 | NfsSrvNfsPort = ntohl(rpc_pkt.u.reply.data[0]); | |
426 | break; | |
427 | } | |
428 | ||
429 | return 0; | |
430 | } | |
431 | ||
432 | static int | |
c9f6c91b | 433 | nfs_mount_reply(uchar *pkt, unsigned len) |
cbd8a35c WD |
434 | { |
435 | struct rpc_t rpc_pkt; | |
436 | ||
0ebf04c6 | 437 | debug("%s\n", __func__); |
cbd8a35c | 438 | |
c9f6c91b | 439 | memcpy((unsigned char *)&rpc_pkt, pkt, len); |
cbd8a35c | 440 | |
fa84fa70 MB |
441 | if (ntohl(rpc_pkt.u.reply.id) > rpc_id) |
442 | return -NFS_RPC_ERR; | |
443 | else if (ntohl(rpc_pkt.u.reply.id) < rpc_id) | |
444 | return -NFS_RPC_DROP; | |
c3f9d493 | 445 | |
cbd8a35c WD |
446 | if (rpc_pkt.u.reply.rstatus || |
447 | rpc_pkt.u.reply.verifier || | |
448 | rpc_pkt.u.reply.astatus || | |
c9f6c91b | 449 | rpc_pkt.u.reply.data[0]) |
cbd8a35c | 450 | return -1; |
cbd8a35c WD |
451 | |
452 | fs_mounted = 1; | |
c9f6c91b | 453 | memcpy(dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE); |
cbd8a35c WD |
454 | |
455 | return 0; | |
456 | } | |
457 | ||
458 | static int | |
c9f6c91b | 459 | nfs_umountall_reply(uchar *pkt, unsigned len) |
cbd8a35c WD |
460 | { |
461 | struct rpc_t rpc_pkt; | |
462 | ||
0ebf04c6 | 463 | debug("%s\n", __func__); |
cbd8a35c | 464 | |
c9f6c91b | 465 | memcpy((unsigned char *)&rpc_pkt, pkt, len); |
cbd8a35c | 466 | |
fa84fa70 MB |
467 | if (ntohl(rpc_pkt.u.reply.id) > rpc_id) |
468 | return -NFS_RPC_ERR; | |
469 | else if (ntohl(rpc_pkt.u.reply.id) < rpc_id) | |
470 | return -NFS_RPC_DROP; | |
c3f9d493 | 471 | |
cbd8a35c WD |
472 | if (rpc_pkt.u.reply.rstatus || |
473 | rpc_pkt.u.reply.verifier || | |
c9f6c91b | 474 | rpc_pkt.u.reply.astatus) |
cbd8a35c | 475 | return -1; |
cbd8a35c WD |
476 | |
477 | fs_mounted = 0; | |
c9f6c91b | 478 | memset(dirfh, 0, sizeof(dirfh)); |
cbd8a35c WD |
479 | |
480 | return 0; | |
481 | } | |
482 | ||
483 | static int | |
c9f6c91b | 484 | nfs_lookup_reply(uchar *pkt, unsigned len) |
cbd8a35c WD |
485 | { |
486 | struct rpc_t rpc_pkt; | |
487 | ||
0ebf04c6 | 488 | debug("%s\n", __func__); |
cbd8a35c | 489 | |
c9f6c91b | 490 | memcpy((unsigned char *)&rpc_pkt, pkt, len); |
cbd8a35c | 491 | |
fa84fa70 MB |
492 | if (ntohl(rpc_pkt.u.reply.id) > rpc_id) |
493 | return -NFS_RPC_ERR; | |
494 | else if (ntohl(rpc_pkt.u.reply.id) < rpc_id) | |
495 | return -NFS_RPC_DROP; | |
c3f9d493 | 496 | |
cbd8a35c WD |
497 | if (rpc_pkt.u.reply.rstatus || |
498 | rpc_pkt.u.reply.verifier || | |
499 | rpc_pkt.u.reply.astatus || | |
c9f6c91b | 500 | rpc_pkt.u.reply.data[0]) |
cbd8a35c | 501 | return -1; |
cbd8a35c | 502 | |
c9f6c91b | 503 | memcpy(filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE); |
cbd8a35c WD |
504 | |
505 | return 0; | |
506 | } | |
507 | ||
508 | static int | |
c9f6c91b | 509 | nfs_readlink_reply(uchar *pkt, unsigned len) |
cbd8a35c WD |
510 | { |
511 | struct rpc_t rpc_pkt; | |
512 | int rlen; | |
513 | ||
0ebf04c6 | 514 | debug("%s\n", __func__); |
cbd8a35c | 515 | |
c9f6c91b | 516 | memcpy((unsigned char *)&rpc_pkt, pkt, len); |
cbd8a35c | 517 | |
fa84fa70 MB |
518 | if (ntohl(rpc_pkt.u.reply.id) > rpc_id) |
519 | return -NFS_RPC_ERR; | |
520 | else if (ntohl(rpc_pkt.u.reply.id) < rpc_id) | |
521 | return -NFS_RPC_DROP; | |
c3f9d493 | 522 | |
cbd8a35c WD |
523 | if (rpc_pkt.u.reply.rstatus || |
524 | rpc_pkt.u.reply.verifier || | |
525 | rpc_pkt.u.reply.astatus || | |
c9f6c91b | 526 | rpc_pkt.u.reply.data[0]) |
cbd8a35c | 527 | return -1; |
cbd8a35c | 528 | |
c9f6c91b | 529 | rlen = ntohl(rpc_pkt.u.reply.data[1]); /* new path length */ |
cbd8a35c WD |
530 | |
531 | if (*((char *)&(rpc_pkt.u.reply.data[2])) != '/') { | |
532 | int pathlen; | |
c9f6c91b | 533 | strcat(nfs_path, "/"); |
cbd8a35c | 534 | pathlen = strlen(nfs_path); |
c9f6c91b JH |
535 | memcpy(nfs_path + pathlen, (uchar *)&(rpc_pkt.u.reply.data[2]), |
536 | rlen); | |
f64ef9bb | 537 | nfs_path[pathlen + rlen] = 0; |
cbd8a35c | 538 | } else { |
c9f6c91b | 539 | memcpy(nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen); |
cbd8a35c WD |
540 | nfs_path[rlen] = 0; |
541 | } | |
542 | return 0; | |
543 | } | |
544 | ||
545 | static int | |
c9f6c91b | 546 | nfs_read_reply(uchar *pkt, unsigned len) |
cbd8a35c WD |
547 | { |
548 | struct rpc_t rpc_pkt; | |
549 | int rlen; | |
550 | ||
0ebf04c6 | 551 | debug("%s\n", __func__); |
cbd8a35c | 552 | |
c9f6c91b | 553 | memcpy((uchar *)&rpc_pkt, pkt, sizeof(rpc_pkt.u.reply)); |
cbd8a35c | 554 | |
fa84fa70 MB |
555 | if (ntohl(rpc_pkt.u.reply.id) > rpc_id) |
556 | return -NFS_RPC_ERR; | |
557 | else if (ntohl(rpc_pkt.u.reply.id) < rpc_id) | |
558 | return -NFS_RPC_DROP; | |
c3f9d493 | 559 | |
cbd8a35c WD |
560 | if (rpc_pkt.u.reply.rstatus || |
561 | rpc_pkt.u.reply.verifier || | |
562 | rpc_pkt.u.reply.astatus || | |
563 | rpc_pkt.u.reply.data[0]) { | |
c9f6c91b | 564 | if (rpc_pkt.u.reply.rstatus) |
cbd8a35c | 565 | return -9999; |
c9f6c91b | 566 | if (rpc_pkt.u.reply.astatus) |
cbd8a35c | 567 | return -9999; |
c9f6c91b | 568 | return -ntohl(rpc_pkt.u.reply.data[0]); |
cbd8a35c WD |
569 | } |
570 | ||
c9f6c91b JH |
571 | if ((nfs_offset != 0) && !((nfs_offset) % |
572 | (NFS_READ_SIZE / 2 * 10 * HASHES_PER_LINE))) | |
573 | puts("\n\t "); | |
574 | if (!(nfs_offset % ((NFS_READ_SIZE / 2) * 10))) | |
575 | putc('#'); | |
cbd8a35c WD |
576 | |
577 | rlen = ntohl(rpc_pkt.u.reply.data[18]); | |
c9f6c91b JH |
578 | if (store_block((uchar *)pkt + sizeof(rpc_pkt.u.reply), |
579 | nfs_offset, rlen)) | |
23a7a32d | 580 | return -9999; |
cbd8a35c WD |
581 | |
582 | return rlen; | |
583 | } | |
584 | ||
585 | /************************************************************************** | |
586 | Interfaces of U-BOOT | |
587 | **************************************************************************/ | |
588 | ||
a5725fab | 589 | static void |
c9f6c91b | 590 | NfsTimeout(void) |
a5725fab | 591 | { |
c9f6c91b JH |
592 | if (++NfsTimeoutCount > NFS_RETRY_COUNT) { |
593 | puts("\nRetry count exceeded; starting again\n"); | |
594 | NetStartAgain(); | |
aabb8cb0 ES |
595 | } else { |
596 | puts("T "); | |
fa84fa70 MB |
597 | NetSetTimeout(nfs_timeout + NFS_TIMEOUT * NfsTimeoutCount, |
598 | NfsTimeout); | |
c9f6c91b | 599 | NfsSend(); |
fe891ecf | 600 | } |
a5725fab WD |
601 | } |
602 | ||
049a95a7 JH |
603 | static void nfs_handler(uchar *pkt, unsigned dest, struct in_addr sip, |
604 | unsigned src, unsigned len) | |
cbd8a35c WD |
605 | { |
606 | int rlen; | |
fa84fa70 | 607 | int reply; |
cbd8a35c | 608 | |
0ebf04c6 | 609 | debug("%s\n", __func__); |
cbd8a35c | 610 | |
c9f6c91b JH |
611 | if (dest != NfsOurPort) |
612 | return; | |
cbd8a35c WD |
613 | |
614 | switch (NfsState) { | |
615 | case STATE_PRCLOOKUP_PROG_MOUNT_REQ: | |
fa84fa70 MB |
616 | if (rpc_lookup_reply(PROG_MOUNT, pkt, len) == -NFS_RPC_DROP) |
617 | break; | |
cbd8a35c | 618 | NfsState = STATE_PRCLOOKUP_PROG_NFS_REQ; |
c9f6c91b | 619 | NfsSend(); |
cbd8a35c WD |
620 | break; |
621 | ||
622 | case STATE_PRCLOOKUP_PROG_NFS_REQ: | |
fa84fa70 MB |
623 | if (rpc_lookup_reply(PROG_NFS, pkt, len) == -NFS_RPC_DROP) |
624 | break; | |
cbd8a35c | 625 | NfsState = STATE_MOUNT_REQ; |
c9f6c91b | 626 | NfsSend(); |
cbd8a35c WD |
627 | break; |
628 | ||
629 | case STATE_MOUNT_REQ: | |
fa84fa70 MB |
630 | reply = nfs_mount_reply(pkt, len); |
631 | if (reply == -NFS_RPC_DROP) | |
632 | break; | |
633 | else if (reply == -NFS_RPC_ERR) { | |
c9f6c91b | 634 | puts("*** ERROR: Cannot mount\n"); |
cbd8a35c WD |
635 | /* just to be sure... */ |
636 | NfsState = STATE_UMOUNT_REQ; | |
c9f6c91b | 637 | NfsSend(); |
cbd8a35c WD |
638 | } else { |
639 | NfsState = STATE_LOOKUP_REQ; | |
c9f6c91b | 640 | NfsSend(); |
cbd8a35c WD |
641 | } |
642 | break; | |
643 | ||
644 | case STATE_UMOUNT_REQ: | |
fa84fa70 MB |
645 | reply = nfs_umountall_reply(pkt, len); |
646 | if (reply == -NFS_RPC_DROP) | |
647 | break; | |
648 | else if (reply == -NFS_RPC_ERR) { | |
c9f6c91b | 649 | puts("*** ERROR: Cannot umount\n"); |
22f6e99d | 650 | net_set_state(NETLOOP_FAIL); |
cbd8a35c | 651 | } else { |
c9f6c91b | 652 | puts("\ndone\n"); |
22f6e99d | 653 | net_set_state(nfs_download_state); |
cbd8a35c WD |
654 | } |
655 | break; | |
656 | ||
657 | case STATE_LOOKUP_REQ: | |
fa84fa70 MB |
658 | reply = nfs_lookup_reply(pkt, len); |
659 | if (reply == -NFS_RPC_DROP) | |
660 | break; | |
661 | else if (reply == -NFS_RPC_ERR) { | |
c9f6c91b | 662 | puts("*** ERROR: File lookup fail\n"); |
cbd8a35c | 663 | NfsState = STATE_UMOUNT_REQ; |
c9f6c91b | 664 | NfsSend(); |
cbd8a35c WD |
665 | } else { |
666 | NfsState = STATE_READ_REQ; | |
667 | nfs_offset = 0; | |
668 | nfs_len = NFS_READ_SIZE; | |
c9f6c91b | 669 | NfsSend(); |
cbd8a35c WD |
670 | } |
671 | break; | |
672 | ||
673 | case STATE_READLINK_REQ: | |
fa84fa70 MB |
674 | reply = nfs_readlink_reply(pkt, len); |
675 | if (reply == -NFS_RPC_DROP) | |
676 | break; | |
677 | else if (reply == -NFS_RPC_ERR) { | |
c9f6c91b | 678 | puts("*** ERROR: Symlink fail\n"); |
cbd8a35c | 679 | NfsState = STATE_UMOUNT_REQ; |
c9f6c91b | 680 | NfsSend(); |
cbd8a35c | 681 | } else { |
0ebf04c6 | 682 | debug("Symlink --> %s\n", nfs_path); |
c9f6c91b JH |
683 | nfs_filename = basename(nfs_path); |
684 | nfs_path = dirname(nfs_path); | |
cbd8a35c WD |
685 | |
686 | NfsState = STATE_MOUNT_REQ; | |
c9f6c91b | 687 | NfsSend(); |
cbd8a35c WD |
688 | } |
689 | break; | |
690 | ||
691 | case STATE_READ_REQ: | |
c9f6c91b | 692 | rlen = nfs_read_reply(pkt, len); |
fa84fa70 | 693 | NetSetTimeout(nfs_timeout, NfsTimeout); |
cbd8a35c WD |
694 | if (rlen > 0) { |
695 | nfs_offset += rlen; | |
c9f6c91b JH |
696 | NfsSend(); |
697 | } else if ((rlen == -NFSERR_ISDIR) || (rlen == -NFSERR_INVAL)) { | |
cbd8a35c WD |
698 | /* symbolic link */ |
699 | NfsState = STATE_READLINK_REQ; | |
c9f6c91b | 700 | NfsSend(); |
cbd8a35c | 701 | } else { |
c9f6c91b | 702 | if (!rlen) |
22f6e99d | 703 | nfs_download_state = NETLOOP_SUCCESS; |
cbd8a35c | 704 | NfsState = STATE_UMOUNT_REQ; |
c9f6c91b | 705 | NfsSend(); |
cbd8a35c WD |
706 | } |
707 | break; | |
708 | } | |
709 | } | |
710 | ||
cbd8a35c WD |
711 | |
712 | void | |
c9f6c91b | 713 | NfsStart(void) |
cbd8a35c | 714 | { |
0ebf04c6 | 715 | debug("%s\n", __func__); |
22f6e99d | 716 | nfs_download_state = NETLOOP_FAIL; |
cbd8a35c | 717 | |
049a95a7 | 718 | nfs_server_ip = net_server_ip; |
cbd8a35c WD |
719 | nfs_path = (char *)nfs_path_buff; |
720 | ||
721 | if (nfs_path == NULL) { | |
22f6e99d | 722 | net_set_state(NETLOOP_FAIL); |
c9f6c91b | 723 | puts("*** ERROR: Fail allocate memory\n"); |
cbd8a35c WD |
724 | return; |
725 | } | |
726 | ||
1411157d | 727 | if (net_boot_file_name[0] == '\0') { |
ea45cb0a | 728 | sprintf(default_filename, "/nfsroot/%02X%02X%02X%02X.img", |
049a95a7 JH |
729 | net_ip.s_addr & 0xFF, |
730 | (net_ip.s_addr >> 8) & 0xFF, | |
731 | (net_ip.s_addr >> 16) & 0xFF, | |
732 | (net_ip.s_addr >> 24) & 0xFF); | |
c9f6c91b | 733 | strcpy(nfs_path, default_filename); |
cbd8a35c | 734 | |
c9f6c91b | 735 | printf("*** Warning: no boot file name; using '%s'\n", |
cbd8a35c WD |
736 | nfs_path); |
737 | } else { | |
1411157d | 738 | char *p = net_boot_file_name; |
cbd8a35c | 739 | |
c9f6c91b | 740 | p = strchr(p, ':'); |
cbd8a35c WD |
741 | |
742 | if (p != NULL) { | |
1411157d | 743 | nfs_server_ip = string_to_ip(net_boot_file_name); |
cbd8a35c | 744 | ++p; |
c9f6c91b | 745 | strcpy(nfs_path, p); |
cbd8a35c | 746 | } else { |
1411157d | 747 | strcpy(nfs_path, net_boot_file_name); |
cbd8a35c WD |
748 | } |
749 | } | |
750 | ||
c9f6c91b JH |
751 | nfs_filename = basename(nfs_path); |
752 | nfs_path = dirname(nfs_path); | |
cbd8a35c | 753 | |
c9f6c91b | 754 | printf("Using %s device\n", eth_get_name()); |
cbd8a35c | 755 | |
049a95a7 JH |
756 | printf("File transfer via NFS from server %pI4; our IP address is %pI4", |
757 | &nfs_server_ip, &net_ip); | |
cbd8a35c WD |
758 | |
759 | /* Check if we need to send across this subnet */ | |
049a95a7 JH |
760 | if (net_gateway.s_addr && net_netmask.s_addr) { |
761 | struct in_addr our_net; | |
762 | struct in_addr server_net; | |
cbd8a35c | 763 | |
049a95a7 JH |
764 | our_net.s_addr = net_ip.s_addr & net_netmask.s_addr; |
765 | server_net.s_addr = net_server_ip.s_addr & net_netmask.s_addr; | |
766 | if (our_net.s_addr != server_net.s_addr) | |
c9f6c91b | 767 | printf("; sending through gateway %pI4", |
049a95a7 | 768 | &net_gateway); |
cbd8a35c | 769 | } |
c9f6c91b | 770 | printf("\nFilename '%s/%s'.", nfs_path, nfs_filename); |
cbd8a35c | 771 | |
1411157d JH |
772 | if (net_boot_file_expected_size_in_blocks) { |
773 | printf(" Size is 0x%x Bytes = ", | |
774 | net_boot_file_expected_size_in_blocks << 9); | |
775 | print_size(net_boot_file_expected_size_in_blocks << 9, ""); | |
cbd8a35c | 776 | } |
c9f6c91b | 777 | printf("\nLoad address: 0x%lx\n" |
4b9206ed | 778 | "Loading: *\b", load_addr); |
cbd8a35c | 779 | |
fa84fa70 | 780 | NetSetTimeout(nfs_timeout, NfsTimeout); |
049a95a7 | 781 | net_set_udp_handler(nfs_handler); |
cbd8a35c | 782 | |
cbd8a35c WD |
783 | NfsTimeoutCount = 0; |
784 | NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ; | |
785 | ||
786 | /*NfsOurPort = 4096 + (get_ticks() % 3072);*/ | |
787 | /*FIX ME !!!*/ | |
788 | NfsOurPort = 1000; | |
789 | ||
790 | /* zero out server ether in case the server ip has changed */ | |
0adb5b76 | 791 | memset(net_server_ethaddr, 0, 6); |
cbd8a35c | 792 | |
c9f6c91b | 793 | NfsSend(); |
cbd8a35c | 794 | } |