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