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