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