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