]>
Commit | Line | Data |
---|---|---|
fe8c2806 WD |
1 | /* |
2 | * Copyright 1994, 1995, 2000 Neil Russell. | |
3 | * (See License) | |
4 | * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de | |
5 | */ | |
6 | ||
7 | #include <common.h> | |
8 | #include <command.h> | |
9 | #include <net.h> | |
10 | #include "tftp.h" | |
11 | #include "bootp.h" | |
12 | ||
fe8c2806 | 13 | #define WELL_KNOWN_PORT 69 /* Well known TFTP port # */ |
49f3bdbb | 14 | #define TIMEOUT 5000UL /* Millisecs to timeout for lost pkt */ |
fe8c2806 WD |
15 | #ifndef CONFIG_NET_RETRY_COUNT |
16 | # define TIMEOUT_COUNT 10 /* # of timeouts before giving up */ | |
17 | #else | |
18 | # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2) | |
19 | #endif | |
20 | /* (for checking the image size) */ | |
21 | #define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */ | |
22 | ||
23 | /* | |
24 | * TFTP operations. | |
25 | */ | |
26 | #define TFTP_RRQ 1 | |
27 | #define TFTP_WRQ 2 | |
28 | #define TFTP_DATA 3 | |
29 | #define TFTP_ACK 4 | |
30 | #define TFTP_ERROR 5 | |
fbe4b5cb | 31 | #define TFTP_OACK 6 |
fe8c2806 | 32 | |
e83cc063 BS |
33 | static ulong TftpTimeoutMSecs = TIMEOUT; |
34 | static int TftpTimeoutCountMax = TIMEOUT_COUNT; | |
35 | ||
36 | /* | |
37 | * These globals govern the timeout behavior when attempting a connection to a | |
38 | * TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to | |
39 | * wait for the server to respond to initial connection. Second global, | |
40 | * TftpRRQTimeoutCountMax, gives the number of such connection retries. | |
41 | * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be | |
42 | * positive. The globals are meant to be set (and restored) by code needing | |
43 | * non-standard timeout behavior when initiating a TFTP transfer. | |
44 | */ | |
45 | ulong TftpRRQTimeoutMSecs = TIMEOUT; | |
46 | int TftpRRQTimeoutCountMax = TIMEOUT_COUNT; | |
47 | ||
aafda38f RB |
48 | enum { |
49 | TFTP_ERR_UNDEFINED = 0, | |
50 | TFTP_ERR_FILE_NOT_FOUND = 1, | |
51 | TFTP_ERR_ACCESS_DENIED = 2, | |
52 | TFTP_ERR_DISK_FULL = 3, | |
53 | TFTP_ERR_UNEXPECTED_OPCODE = 4, | |
54 | TFTP_ERR_UNKNOWN_TRANSFER_ID = 5, | |
55 | TFTP_ERR_FILE_ALREADY_EXISTS = 6, | |
56 | }; | |
57 | ||
a93907c4 | 58 | static IPaddr_t TftpServerIP; |
fe8c2806 WD |
59 | static int TftpServerPort; /* The UDP port at their end */ |
60 | static int TftpOurPort; /* The UDP port at our end */ | |
61 | static int TftpTimeoutCount; | |
3f85ce27 WD |
62 | static ulong TftpBlock; /* packet sequence number */ |
63 | static ulong TftpLastBlock; /* last packet sequence number received */ | |
64 | static ulong TftpBlockWrap; /* count of sequence number wraparounds */ | |
65 | static ulong TftpBlockWrapOffset; /* memory offset due to wrapping */ | |
fe8c2806 | 66 | static int TftpState; |
4fccb818 RG |
67 | #ifdef CONFIG_TFTP_TSIZE |
68 | static int TftpTsize; /* The file size reported by the server */ | |
69 | static short TftpNumchars; /* The number of hashes we printed */ | |
70 | #endif | |
3f85ce27 | 71 | |
fe8c2806 WD |
72 | #define STATE_RRQ 1 |
73 | #define STATE_DATA 2 | |
74 | #define STATE_TOO_LARGE 3 | |
75 | #define STATE_BAD_MAGIC 4 | |
fbe4b5cb | 76 | #define STATE_OACK 5 |
fe8c2806 | 77 | |
3f85ce27 WD |
78 | #define TFTP_BLOCK_SIZE 512 /* default TFTP block size */ |
79 | #define TFTP_SEQUENCE_SIZE ((ulong)(1<<16)) /* sequence number is 16 bit */ | |
80 | ||
fe8c2806 WD |
81 | #define DEFAULT_NAME_LEN (8 + 4 + 1) |
82 | static char default_filename[DEFAULT_NAME_LEN]; | |
a93907c4 JCPV |
83 | |
84 | #ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN | |
85 | #define MAX_LEN 128 | |
86 | #else | |
87 | #define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN | |
88 | #endif | |
89 | ||
90 | static char tftp_filename[MAX_LEN]; | |
fe8c2806 | 91 | |
6d0f6bcf | 92 | #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP |
e6f2e902 | 93 | extern flash_info_t flash_info[]; |
fe8c2806 WD |
94 | #endif |
95 | ||
85eb5caf WD |
96 | /* 512 is poor choice for ethernet, MTU is typically 1500. |
97 | * Minus eth.hdrs thats 1468. Can get 2x better throughput with | |
53a5c424 | 98 | * almost-MTU block sizes. At least try... fall back to 512 if need be. |
89ba81d1 | 99 | * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file) |
53a5c424 | 100 | */ |
89ba81d1 AR |
101 | #ifdef CONFIG_TFTP_BLOCKSIZE |
102 | #define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE | |
103 | #else | |
53a5c424 | 104 | #define TFTP_MTU_BLOCKSIZE 1468 |
89ba81d1 AR |
105 | #endif |
106 | ||
53a5c424 DU |
107 | static unsigned short TftpBlkSize=TFTP_BLOCK_SIZE; |
108 | static unsigned short TftpBlkSizeOption=TFTP_MTU_BLOCKSIZE; | |
109 | ||
110 | #ifdef CONFIG_MCAST_TFTP | |
111 | #include <malloc.h> | |
112 | #define MTFTP_BITMAPSIZE 0x1000 | |
113 | static unsigned *Bitmap; | |
114 | static int PrevBitmapHole,Mapsize=MTFTP_BITMAPSIZE; | |
115 | static uchar ProhibitMcast=0, MasterClient=0; | |
116 | static uchar Multicast=0; | |
117 | extern IPaddr_t Mcast_addr; | |
118 | static int Mcast_port; | |
119 | static ulong TftpEndingBlock; /* can get 'last' block before done..*/ | |
120 | ||
121 | static void parse_multicast_oack(char *pkt,int len); | |
122 | ||
123 | static void | |
124 | mcast_cleanup(void) | |
125 | { | |
126 | if (Mcast_addr) eth_mcast_join(Mcast_addr, 0); | |
127 | if (Bitmap) free(Bitmap); | |
128 | Bitmap=NULL; | |
129 | Mcast_addr = Multicast = Mcast_port = 0; | |
130 | TftpEndingBlock = -1; | |
131 | } | |
132 | ||
133 | #endif /* CONFIG_MCAST_TFTP */ | |
134 | ||
fe8c2806 WD |
135 | static __inline__ void |
136 | store_block (unsigned block, uchar * src, unsigned len) | |
137 | { | |
53a5c424 | 138 | ulong offset = block * TftpBlkSize + TftpBlockWrapOffset; |
3f85ce27 | 139 | ulong newsize = offset + len; |
6d0f6bcf | 140 | #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP |
fe8c2806 WD |
141 | int i, rc = 0; |
142 | ||
6d0f6bcf | 143 | for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; i++) { |
fe8c2806 | 144 | /* start address in flash? */ |
be1b0d27 JF |
145 | if (flash_info[i].flash_id == FLASH_UNKNOWN) |
146 | continue; | |
fe8c2806 WD |
147 | if (load_addr + offset >= flash_info[i].start[0]) { |
148 | rc = 1; | |
149 | break; | |
150 | } | |
151 | } | |
152 | ||
153 | if (rc) { /* Flash is destination for this packet */ | |
77ddac94 | 154 | rc = flash_write ((char *)src, (ulong)(load_addr+offset), len); |
fe8c2806 WD |
155 | if (rc) { |
156 | flash_perror (rc); | |
157 | NetState = NETLOOP_FAIL; | |
158 | return; | |
159 | } | |
160 | } | |
161 | else | |
6d0f6bcf | 162 | #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */ |
fe8c2806 WD |
163 | { |
164 | (void)memcpy((void *)(load_addr + offset), src, len); | |
165 | } | |
53a5c424 DU |
166 | #ifdef CONFIG_MCAST_TFTP |
167 | if (Multicast) | |
168 | ext2_set_bit(block, Bitmap); | |
169 | #endif | |
fe8c2806 WD |
170 | |
171 | if (NetBootFileXferSize < newsize) | |
172 | NetBootFileXferSize = newsize; | |
173 | } | |
174 | ||
175 | static void TftpSend (void); | |
176 | static void TftpTimeout (void); | |
177 | ||
178 | /**********************************************************************/ | |
179 | ||
180 | static void | |
181 | TftpSend (void) | |
182 | { | |
183 | volatile uchar * pkt; | |
184 | volatile uchar * xp; | |
185 | int len = 0; | |
7bc5ee07 | 186 | volatile ushort *s; |
fe8c2806 | 187 | |
85eb5caf | 188 | #ifdef CONFIG_MCAST_TFTP |
53a5c424 | 189 | /* Multicast TFTP.. non-MasterClients do not ACK data. */ |
85eb5caf WD |
190 | if (Multicast |
191 | && (TftpState == STATE_DATA) | |
192 | && (MasterClient == 0)) | |
53a5c424 DU |
193 | return; |
194 | #endif | |
fe8c2806 WD |
195 | /* |
196 | * We will always be sending some sort of packet, so | |
197 | * cobble together the packet headers now. | |
198 | */ | |
a3d991bd | 199 | pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE; |
fe8c2806 WD |
200 | |
201 | switch (TftpState) { | |
202 | ||
203 | case STATE_RRQ: | |
204 | xp = pkt; | |
7bc5ee07 WD |
205 | s = (ushort *)pkt; |
206 | *s++ = htons(TFTP_RRQ); | |
207 | pkt = (uchar *)s; | |
fe8c2806 WD |
208 | strcpy ((char *)pkt, tftp_filename); |
209 | pkt += strlen(tftp_filename) + 1; | |
210 | strcpy ((char *)pkt, "octet"); | |
211 | pkt += 5 /*strlen("octet")*/ + 1; | |
fbe4b5cb WD |
212 | strcpy ((char *)pkt, "timeout"); |
213 | pkt += 7 /*strlen("timeout")*/ + 1; | |
49f3bdbb | 214 | sprintf((char *)pkt, "%lu", TIMEOUT / 1000); |
0ebf04c6 | 215 | debug("send option \"timeout %s\"\n", (char *)pkt); |
fbe4b5cb | 216 | pkt += strlen((char *)pkt) + 1; |
4fccb818 RG |
217 | #ifdef CONFIG_TFTP_TSIZE |
218 | memcpy((char *)pkt, "tsize\0000\0", 8); | |
219 | pkt += 8; | |
220 | #endif | |
53a5c424 DU |
221 | /* try for more effic. blk size */ |
222 | pkt += sprintf((char *)pkt,"blksize%c%d%c", | |
ef8f2075 | 223 | 0,TftpBlkSizeOption,0); |
85eb5caf | 224 | #ifdef CONFIG_MCAST_TFTP |
53a5c424 | 225 | /* Check all preconditions before even trying the option */ |
85eb5caf WD |
226 | if (!ProhibitMcast |
227 | && (Bitmap=malloc(Mapsize)) | |
53a5c424 DU |
228 | && eth_get_dev()->mcast) { |
229 | free(Bitmap); | |
230 | Bitmap=NULL; | |
231 | pkt += sprintf((char *)pkt,"multicast%c%c",0,0); | |
232 | } | |
233 | #endif /* CONFIG_MCAST_TFTP */ | |
fe8c2806 WD |
234 | len = pkt - xp; |
235 | break; | |
236 | ||
fbe4b5cb | 237 | case STATE_OACK: |
53a5c424 DU |
238 | #ifdef CONFIG_MCAST_TFTP |
239 | /* My turn! Start at where I need blocks I missed.*/ | |
240 | if (Multicast) | |
241 | TftpBlock=ext2_find_next_zero_bit(Bitmap,(Mapsize*8),0); | |
242 | /*..falling..*/ | |
243 | #endif | |
244 | case STATE_DATA: | |
fe8c2806 | 245 | xp = pkt; |
7bc5ee07 WD |
246 | s = (ushort *)pkt; |
247 | *s++ = htons(TFTP_ACK); | |
248 | *s++ = htons(TftpBlock); | |
249 | pkt = (uchar *)s; | |
fe8c2806 WD |
250 | len = pkt - xp; |
251 | break; | |
252 | ||
253 | case STATE_TOO_LARGE: | |
254 | xp = pkt; | |
7bc5ee07 WD |
255 | s = (ushort *)pkt; |
256 | *s++ = htons(TFTP_ERROR); | |
257 | *s++ = htons(3); | |
258 | pkt = (uchar *)s; | |
fe8c2806 WD |
259 | strcpy ((char *)pkt, "File too large"); |
260 | pkt += 14 /*strlen("File too large")*/ + 1; | |
261 | len = pkt - xp; | |
262 | break; | |
263 | ||
264 | case STATE_BAD_MAGIC: | |
265 | xp = pkt; | |
7bc5ee07 WD |
266 | s = (ushort *)pkt; |
267 | *s++ = htons(TFTP_ERROR); | |
268 | *s++ = htons(2); | |
269 | pkt = (uchar *)s; | |
fe8c2806 WD |
270 | strcpy ((char *)pkt, "File has bad magic"); |
271 | pkt += 18 /*strlen("File has bad magic")*/ + 1; | |
272 | len = pkt - xp; | |
273 | break; | |
274 | } | |
275 | ||
a93907c4 | 276 | NetSendUDPPacket(NetServerEther, TftpServerIP, TftpServerPort, TftpOurPort, len); |
fe8c2806 WD |
277 | } |
278 | ||
279 | ||
280 | static void | |
281 | TftpHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len) | |
282 | { | |
283 | ushort proto; | |
7bc5ee07 | 284 | ushort *s; |
ff13ac8c | 285 | int i; |
fe8c2806 WD |
286 | |
287 | if (dest != TftpOurPort) { | |
53a5c424 | 288 | #ifdef CONFIG_MCAST_TFTP |
85eb5caf | 289 | if (Multicast |
53a5c424 DU |
290 | && (!Mcast_port || (dest != Mcast_port))) |
291 | #endif | |
fe8c2806 WD |
292 | return; |
293 | } | |
294 | if (TftpState != STATE_RRQ && src != TftpServerPort) { | |
295 | return; | |
296 | } | |
297 | ||
298 | if (len < 2) { | |
299 | return; | |
300 | } | |
301 | len -= 2; | |
302 | /* warning: don't use increment (++) in ntohs() macros!! */ | |
7bc5ee07 WD |
303 | s = (ushort *)pkt; |
304 | proto = *s++; | |
305 | pkt = (uchar *)s; | |
fe8c2806 WD |
306 | switch (ntohs(proto)) { |
307 | ||
308 | case TFTP_RRQ: | |
309 | case TFTP_WRQ: | |
310 | case TFTP_ACK: | |
311 | break; | |
312 | default: | |
313 | break; | |
314 | ||
fbe4b5cb | 315 | case TFTP_OACK: |
d371708a WD |
316 | debug("Got OACK: %s %s\n", |
317 | pkt, | |
318 | pkt + strlen((char *)pkt) + 1); | |
fbe4b5cb WD |
319 | TftpState = STATE_OACK; |
320 | TftpServerPort = src; | |
60174746 WD |
321 | /* |
322 | * Check for 'blksize' option. | |
323 | * Careful: "i" is signed, "len" is unsigned, thus | |
324 | * something like "len-8" may give a *huge* number | |
325 | */ | |
326 | for (i=0; i+8<len; i++) { | |
ff13ac8c WD |
327 | if (strcmp ((char*)pkt+i,"blksize") == 0) { |
328 | TftpBlkSize = (unsigned short) | |
329 | simple_strtoul((char*)pkt+i+8,NULL,10); | |
0ebf04c6 | 330 | debug("Blocksize ack: %s, %d\n", |
ff13ac8c | 331 | (char*)pkt+i+8,TftpBlkSize); |
ff13ac8c | 332 | } |
4fccb818 RG |
333 | #ifdef CONFIG_TFTP_TSIZE |
334 | if (strcmp ((char*)pkt+i,"tsize") == 0) { | |
335 | TftpTsize = simple_strtoul((char*)pkt+i+6,NULL,10); | |
336 | debug("size = %s, %d\n", | |
337 | (char*)pkt+i+6, TftpTsize); | |
338 | } | |
339 | #endif | |
53a5c424 DU |
340 | } |
341 | #ifdef CONFIG_MCAST_TFTP | |
342 | parse_multicast_oack((char *)pkt,len-1); | |
85eb5caf | 343 | if ((Multicast) && (!MasterClient)) |
53a5c424 DU |
344 | TftpState = STATE_DATA; /* passive.. */ |
345 | else | |
346 | #endif | |
fbe4b5cb WD |
347 | TftpSend (); /* Send ACK */ |
348 | break; | |
fe8c2806 WD |
349 | case TFTP_DATA: |
350 | if (len < 2) | |
351 | return; | |
352 | len -= 2; | |
353 | TftpBlock = ntohs(*(ushort *)pkt); | |
3f85ce27 WD |
354 | |
355 | /* | |
cd0a9de6 WD |
356 | * RFC1350 specifies that the first data packet will |
357 | * have sequence number 1. If we receive a sequence | |
358 | * number of 0 this means that there was a wrap | |
359 | * around of the (16 bit) counter. | |
3f85ce27 WD |
360 | */ |
361 | if (TftpBlock == 0) { | |
362 | TftpBlockWrap++; | |
53a5c424 | 363 | TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE; |
ddd5d9da | 364 | printf ("\n\t %lu MB received\n\t ", TftpBlockWrapOffset>>20); |
4fccb818 RG |
365 | } |
366 | #ifdef CONFIG_TFTP_TSIZE | |
367 | else if (TftpTsize) { | |
368 | while (TftpNumchars < NetBootFileXferSize * 50 / TftpTsize) { | |
369 | putc('#'); | |
370 | TftpNumchars++; | |
371 | } | |
372 | } | |
373 | #endif | |
374 | else { | |
3f85ce27 WD |
375 | if (((TftpBlock - 1) % 10) == 0) { |
376 | putc ('#'); | |
377 | } else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) { | |
378 | puts ("\n\t "); | |
379 | } | |
fe8c2806 WD |
380 | } |
381 | ||
0ebf04c6 RG |
382 | if (TftpState == STATE_RRQ) |
383 | debug("Server did not acknowledge timeout option!\n"); | |
fbe4b5cb WD |
384 | |
385 | if (TftpState == STATE_RRQ || TftpState == STATE_OACK) { | |
3f85ce27 | 386 | /* first block received */ |
fe8c2806 WD |
387 | TftpState = STATE_DATA; |
388 | TftpServerPort = src; | |
389 | TftpLastBlock = 0; | |
3f85ce27 WD |
390 | TftpBlockWrap = 0; |
391 | TftpBlockWrapOffset = 0; | |
fe8c2806 | 392 | |
53a5c424 DU |
393 | #ifdef CONFIG_MCAST_TFTP |
394 | if (Multicast) { /* start!=1 common if mcast */ | |
395 | TftpLastBlock = TftpBlock - 1; | |
396 | } else | |
397 | #endif | |
fe8c2806 WD |
398 | if (TftpBlock != 1) { /* Assertion */ |
399 | printf ("\nTFTP error: " | |
3f85ce27 | 400 | "First block is not block 1 (%ld)\n" |
fe8c2806 WD |
401 | "Starting again\n\n", |
402 | TftpBlock); | |
403 | NetStartAgain (); | |
404 | break; | |
405 | } | |
406 | } | |
407 | ||
408 | if (TftpBlock == TftpLastBlock) { | |
409 | /* | |
410 | * Same block again; ignore it. | |
411 | */ | |
412 | break; | |
413 | } | |
414 | ||
415 | TftpLastBlock = TftpBlock; | |
e83cc063 BS |
416 | TftpTimeoutMSecs = TIMEOUT; |
417 | TftpTimeoutCountMax = TIMEOUT_COUNT; | |
418 | NetSetTimeout (TftpTimeoutMSecs, TftpTimeout); | |
fe8c2806 WD |
419 | |
420 | store_block (TftpBlock - 1, pkt + 2, len); | |
421 | ||
422 | /* | |
423 | * Acknoledge the block just received, which will prompt | |
424 | * the server for the next one. | |
425 | */ | |
53a5c424 | 426 | #ifdef CONFIG_MCAST_TFTP |
85eb5caf WD |
427 | /* if I am the MasterClient, actively calculate what my next |
428 | * needed block is; else I'm passive; not ACKING | |
a93907c4 | 429 | */ |
53a5c424 DU |
430 | if (Multicast) { |
431 | if (len < TftpBlkSize) { | |
432 | TftpEndingBlock = TftpBlock; | |
433 | } else if (MasterClient) { | |
85eb5caf | 434 | TftpBlock = PrevBitmapHole = |
53a5c424 DU |
435 | ext2_find_next_zero_bit( |
436 | Bitmap, | |
437 | (Mapsize*8), | |
438 | PrevBitmapHole); | |
439 | if (TftpBlock > ((Mapsize*8) - 1)) { | |
440 | printf ("tftpfile too big\n"); | |
441 | /* try to double it and retry */ | |
442 | Mapsize<<=1; | |
443 | mcast_cleanup(); | |
444 | NetStartAgain (); | |
445 | return; | |
446 | } | |
447 | TftpLastBlock = TftpBlock; | |
448 | } | |
449 | } | |
450 | #endif | |
fe8c2806 WD |
451 | TftpSend (); |
452 | ||
53a5c424 DU |
453 | #ifdef CONFIG_MCAST_TFTP |
454 | if (Multicast) { | |
455 | if (MasterClient && (TftpBlock >= TftpEndingBlock)) { | |
456 | puts ("\nMulticast tftp done\n"); | |
457 | mcast_cleanup(); | |
458 | NetState = NETLOOP_SUCCESS; | |
85eb5caf | 459 | } |
53a5c424 DU |
460 | } |
461 | else | |
462 | #endif | |
463 | if (len < TftpBlkSize) { | |
fe8c2806 WD |
464 | /* |
465 | * We received the whole thing. Try to | |
466 | * run it. | |
467 | */ | |
4fccb818 RG |
468 | #ifdef CONFIG_TFTP_TSIZE |
469 | /* Print out the hash marks for the last packet received */ | |
470 | while (TftpTsize && TftpNumchars < 49) { | |
471 | putc('#'); | |
472 | TftpNumchars++; | |
473 | } | |
474 | #endif | |
fe8c2806 WD |
475 | puts ("\ndone\n"); |
476 | NetState = NETLOOP_SUCCESS; | |
477 | } | |
478 | break; | |
479 | ||
480 | case TFTP_ERROR: | |
481 | printf ("\nTFTP error: '%s' (%d)\n", | |
482 | pkt + 2, ntohs(*(ushort *)pkt)); | |
aafda38f RB |
483 | |
484 | switch (ntohs(*(ushort *)pkt)) { | |
485 | case TFTP_ERR_FILE_NOT_FOUND: | |
486 | case TFTP_ERR_ACCESS_DENIED: | |
487 | puts("Not retrying...\n"); | |
488 | eth_halt(); | |
489 | NetState = NETLOOP_FAIL; | |
490 | break; | |
491 | case TFTP_ERR_UNDEFINED: | |
492 | case TFTP_ERR_DISK_FULL: | |
493 | case TFTP_ERR_UNEXPECTED_OPCODE: | |
494 | case TFTP_ERR_UNKNOWN_TRANSFER_ID: | |
495 | case TFTP_ERR_FILE_ALREADY_EXISTS: | |
496 | default: | |
497 | puts("Starting again\n\n"); | |
53a5c424 | 498 | #ifdef CONFIG_MCAST_TFTP |
aafda38f | 499 | mcast_cleanup(); |
53a5c424 | 500 | #endif |
aafda38f RB |
501 | NetStartAgain(); |
502 | break; | |
503 | } | |
fe8c2806 WD |
504 | break; |
505 | } | |
506 | } | |
507 | ||
508 | ||
509 | static void | |
510 | TftpTimeout (void) | |
511 | { | |
e83cc063 | 512 | if (++TftpTimeoutCount > TftpTimeoutCountMax) { |
fe8c2806 | 513 | puts ("\nRetry count exceeded; starting again\n"); |
53a5c424 DU |
514 | #ifdef CONFIG_MCAST_TFTP |
515 | mcast_cleanup(); | |
516 | #endif | |
fe8c2806 WD |
517 | NetStartAgain (); |
518 | } else { | |
519 | puts ("T "); | |
e83cc063 | 520 | NetSetTimeout (TftpTimeoutMSecs, TftpTimeout); |
fe8c2806 WD |
521 | TftpSend (); |
522 | } | |
523 | } | |
524 | ||
525 | ||
526 | void | |
527 | TftpStart (void) | |
528 | { | |
ecb0ccd9 | 529 | char *ep; /* Environment pointer */ |
89ba81d1 AR |
530 | |
531 | /* Allow the user to choose tftpblocksize */ | |
532 | if ((ep = getenv("tftpblocksize")) != NULL) | |
533 | TftpBlkSizeOption = simple_strtol(ep, NULL, 10); | |
534 | debug("tftp block size is %i\n", TftpBlkSizeOption); | |
ecb0ccd9 | 535 | |
a93907c4 | 536 | TftpServerIP = NetServerIP; |
fe8c2806 | 537 | if (BootFile[0] == '\0') { |
fe8c2806 | 538 | sprintf(default_filename, "%02lX%02lX%02lX%02lX.img", |
c43352cc WD |
539 | NetOurIP & 0xFF, |
540 | (NetOurIP >> 8) & 0xFF, | |
541 | (NetOurIP >> 16) & 0xFF, | |
542 | (NetOurIP >> 24) & 0xFF ); | |
a93907c4 JCPV |
543 | |
544 | strncpy(tftp_filename, default_filename, MAX_LEN); | |
545 | tftp_filename[MAX_LEN-1] = 0; | |
fe8c2806 WD |
546 | |
547 | printf ("*** Warning: no boot file name; using '%s'\n", | |
548 | tftp_filename); | |
549 | } else { | |
38cc09c5 | 550 | char *p = strchr (BootFile, ':'); |
a93907c4 JCPV |
551 | |
552 | if (p == NULL) { | |
553 | strncpy(tftp_filename, BootFile, MAX_LEN); | |
554 | tftp_filename[MAX_LEN-1] = 0; | |
555 | } else { | |
a93907c4 | 556 | TftpServerIP = string_to_ip (BootFile); |
6a86bb6c | 557 | strncpy(tftp_filename, p + 1, MAX_LEN); |
a93907c4 JCPV |
558 | tftp_filename[MAX_LEN-1] = 0; |
559 | } | |
fe8c2806 WD |
560 | } |
561 | ||
5bb226e8 WD |
562 | #if defined(CONFIG_NET_MULTI) |
563 | printf ("Using %s device\n", eth_get_name()); | |
564 | #endif | |
b6446b67 MF |
565 | printf("TFTP from server %pI4" |
566 | "; our IP address is %pI4", &TftpServerIP, &NetOurIP); | |
fe8c2806 WD |
567 | |
568 | /* Check if we need to send across this subnet */ | |
569 | if (NetOurGatewayIP && NetOurSubnetMask) { | |
a93907c4 JCPV |
570 | IPaddr_t OurNet = NetOurIP & NetOurSubnetMask; |
571 | IPaddr_t ServerNet = TftpServerIP & NetOurSubnetMask; | |
fe8c2806 | 572 | |
b6446b67 MF |
573 | if (OurNet != ServerNet) |
574 | printf("; sending through gateway %pI4", &NetOurGatewayIP); | |
fe8c2806 WD |
575 | } |
576 | putc ('\n'); | |
577 | ||
578 | printf ("Filename '%s'.", tftp_filename); | |
579 | ||
580 | if (NetBootFileSize) { | |
581 | printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9); | |
582 | print_size (NetBootFileSize<<9, ""); | |
583 | } | |
584 | ||
585 | putc ('\n'); | |
586 | ||
587 | printf ("Load address: 0x%lx\n", load_addr); | |
588 | ||
589 | puts ("Loading: *\b"); | |
590 | ||
e83cc063 BS |
591 | TftpTimeoutMSecs = TftpRRQTimeoutMSecs; |
592 | TftpTimeoutCountMax = TftpRRQTimeoutCountMax; | |
593 | ||
594 | NetSetTimeout (TftpTimeoutMSecs, TftpTimeout); | |
fe8c2806 WD |
595 | NetSetHandler (TftpHandler); |
596 | ||
597 | TftpServerPort = WELL_KNOWN_PORT; | |
598 | TftpTimeoutCount = 0; | |
599 | TftpState = STATE_RRQ; | |
ecb0ccd9 | 600 | /* Use a pseudo-random port unless a specific port is set */ |
fe8c2806 | 601 | TftpOurPort = 1024 + (get_timer(0) % 3072); |
53a5c424 | 602 | |
ecb0ccd9 | 603 | #ifdef CONFIG_TFTP_PORT |
28cb9375 WD |
604 | if ((ep = getenv("tftpdstp")) != NULL) { |
605 | TftpServerPort = simple_strtol(ep, NULL, 10); | |
606 | } | |
607 | if ((ep = getenv("tftpsrcp")) != NULL) { | |
ecb0ccd9 WD |
608 | TftpOurPort= simple_strtol(ep, NULL, 10); |
609 | } | |
610 | #endif | |
fbe4b5cb | 611 | TftpBlock = 0; |
fe8c2806 | 612 | |
73a8b27c WD |
613 | /* zero out server ether in case the server ip has changed */ |
614 | memset(NetServerEther, 0, 6); | |
53a5c424 DU |
615 | /* Revert TftpBlkSize to dflt */ |
616 | TftpBlkSize = TFTP_BLOCK_SIZE; | |
617 | #ifdef CONFIG_MCAST_TFTP | |
a93907c4 | 618 | mcast_cleanup(); |
53a5c424 | 619 | #endif |
4fccb818 RG |
620 | #ifdef CONFIG_TFTP_TSIZE |
621 | TftpTsize = 0; | |
622 | TftpNumchars = 0; | |
623 | #endif | |
73a8b27c | 624 | |
fe8c2806 WD |
625 | TftpSend (); |
626 | } | |
627 | ||
53a5c424 DU |
628 | #ifdef CONFIG_MCAST_TFTP |
629 | /* Credits: atftp project. | |
630 | */ | |
631 | ||
632 | /* pick up BcastAddr, Port, and whether I am [now] the master-client. * | |
633 | * Frame: | |
634 | * +-------+-----------+---+-------~~-------+---+ | |
635 | * | opc | multicast | 0 | addr, port, mc | 0 | | |
636 | * +-------+-----------+---+-------~~-------+---+ | |
637 | * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then | |
638 | * I am the new master-client so must send ACKs to DataBlocks. If I am not | |
639 | * master-client, I'm a passive client, gathering what DataBlocks I may and | |
640 | * making note of which ones I got in my bitmask. | |
641 | * In theory, I never go from master->passive.. | |
642 | * .. this comes in with pkt already pointing just past opc | |
643 | */ | |
644 | static void parse_multicast_oack(char *pkt, int len) | |
645 | { | |
646 | int i; | |
647 | IPaddr_t addr; | |
648 | char *mc_adr, *port, *mc; | |
649 | ||
650 | mc_adr=port=mc=NULL; | |
651 | /* march along looking for 'multicast\0', which has to start at least | |
652 | * 14 bytes back from the end. | |
653 | */ | |
654 | for (i=0;i<len-14;i++) | |
655 | if (strcmp (pkt+i,"multicast") == 0) | |
656 | break; | |
657 | if (i >= (len-14)) /* non-Multicast OACK, ign. */ | |
658 | return; | |
85eb5caf | 659 | |
53a5c424 DU |
660 | i+=10; /* strlen multicast */ |
661 | mc_adr = pkt+i; | |
662 | for (;i<len;i++) { | |
663 | if (*(pkt+i) == ',') { | |
664 | *(pkt+i) = '\0'; | |
665 | if (port) { | |
666 | mc = pkt+i+1; | |
667 | break; | |
668 | } else { | |
669 | port = pkt+i+1; | |
670 | } | |
671 | } | |
672 | } | |
673 | if (!port || !mc_adr || !mc ) return; | |
674 | if (Multicast && MasterClient) { | |
675 | printf ("I got a OACK as master Client, WRONG!\n"); | |
676 | return; | |
677 | } | |
678 | /* ..I now accept packets destined for this MCAST addr, port */ | |
679 | if (!Multicast) { | |
680 | if (Bitmap) { | |
681 | printf ("Internal failure! no mcast.\n"); | |
682 | free(Bitmap); | |
683 | Bitmap=NULL; | |
684 | ProhibitMcast=1; | |
685 | return ; | |
686 | } | |
687 | /* I malloc instead of pre-declare; so that if the file ends | |
85eb5caf WD |
688 | * up being too big for this bitmap I can retry |
689 | */ | |
53a5c424 DU |
690 | if (!(Bitmap = malloc (Mapsize))) { |
691 | printf ("No Bitmap, no multicast. Sorry.\n"); | |
692 | ProhibitMcast=1; | |
693 | return; | |
694 | } | |
695 | memset (Bitmap,0,Mapsize); | |
696 | PrevBitmapHole = 0; | |
697 | Multicast = 1; | |
698 | } | |
699 | addr = string_to_ip(mc_adr); | |
700 | if (Mcast_addr != addr) { | |
701 | if (Mcast_addr) | |
702 | eth_mcast_join(Mcast_addr, 0); | |
703 | if (eth_mcast_join(Mcast_addr=addr, 1)) { | |
704 | printf ("Fail to set mcast, revert to TFTP\n"); | |
705 | ProhibitMcast=1; | |
706 | mcast_cleanup(); | |
707 | NetStartAgain(); | |
708 | } | |
709 | } | |
710 | MasterClient = (unsigned char)simple_strtoul((char *)mc,NULL,10); | |
711 | Mcast_port = (unsigned short)simple_strtoul(port,NULL,10); | |
712 | printf ("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient); | |
713 | return; | |
714 | } | |
715 | ||
716 | #endif /* Multicast TFTP */ |