]>
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, [email protected] | |
e59e3562 LC |
5 | * Copyright 2011 Comelit Group SpA, |
6 | * Luca Ceresoli <[email protected]> | |
fe8c2806 WD |
7 | */ |
8 | ||
9 | #include <common.h> | |
10 | #include <command.h> | |
11 | #include <net.h> | |
12 | #include "tftp.h" | |
13 | #include "bootp.h" | |
13dfe943 JH |
14 | #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP |
15 | #include <flash.h> | |
16 | #endif | |
fe8c2806 | 17 | |
2f09413f LC |
18 | /* Well known TFTP port # */ |
19 | #define WELL_KNOWN_PORT 69 | |
20 | /* Millisecs to timeout for lost pkt */ | |
21 | #define TIMEOUT 5000UL | |
fe8c2806 | 22 | #ifndef CONFIG_NET_RETRY_COUNT |
2f09413f LC |
23 | /* # of timeouts before giving up */ |
24 | # define TIMEOUT_COUNT 10 | |
fe8c2806 WD |
25 | #else |
26 | # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2) | |
27 | #endif | |
2f09413f LC |
28 | /* Number of "loading" hashes per line (for checking the image size) */ |
29 | #define HASHES_PER_LINE 65 | |
fe8c2806 WD |
30 | |
31 | /* | |
32 | * TFTP operations. | |
33 | */ | |
34 | #define TFTP_RRQ 1 | |
35 | #define TFTP_WRQ 2 | |
36 | #define TFTP_DATA 3 | |
37 | #define TFTP_ACK 4 | |
38 | #define TFTP_ERROR 5 | |
fbe4b5cb | 39 | #define TFTP_OACK 6 |
fe8c2806 | 40 | |
e83cc063 BS |
41 | static ulong TftpTimeoutMSecs = TIMEOUT; |
42 | static int TftpTimeoutCountMax = TIMEOUT_COUNT; | |
43 | ||
44 | /* | |
45 | * These globals govern the timeout behavior when attempting a connection to a | |
46 | * TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to | |
47 | * wait for the server to respond to initial connection. Second global, | |
48 | * TftpRRQTimeoutCountMax, gives the number of such connection retries. | |
49 | * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be | |
50 | * positive. The globals are meant to be set (and restored) by code needing | |
51 | * non-standard timeout behavior when initiating a TFTP transfer. | |
52 | */ | |
53 | ulong TftpRRQTimeoutMSecs = TIMEOUT; | |
54 | int TftpRRQTimeoutCountMax = TIMEOUT_COUNT; | |
55 | ||
aafda38f RB |
56 | enum { |
57 | TFTP_ERR_UNDEFINED = 0, | |
58 | TFTP_ERR_FILE_NOT_FOUND = 1, | |
59 | TFTP_ERR_ACCESS_DENIED = 2, | |
60 | TFTP_ERR_DISK_FULL = 3, | |
61 | TFTP_ERR_UNEXPECTED_OPCODE = 4, | |
62 | TFTP_ERR_UNKNOWN_TRANSFER_ID = 5, | |
63 | TFTP_ERR_FILE_ALREADY_EXISTS = 6, | |
64 | }; | |
65 | ||
20478cea | 66 | static IPaddr_t TftpRemoteIP; |
2f09413f | 67 | /* The UDP port at their end */ |
20478cea | 68 | static int TftpRemotePort; |
2f09413f LC |
69 | /* The UDP port at our end */ |
70 | static int TftpOurPort; | |
fe8c2806 | 71 | static int TftpTimeoutCount; |
2f09413f LC |
72 | /* packet sequence number */ |
73 | static ulong TftpBlock; | |
74 | /* last packet sequence number received */ | |
75 | static ulong TftpLastBlock; | |
76 | /* count of sequence number wraparounds */ | |
77 | static ulong TftpBlockWrap; | |
78 | /* memory offset due to wrapping */ | |
79 | static ulong TftpBlockWrapOffset; | |
fe8c2806 | 80 | static int TftpState; |
4fccb818 | 81 | #ifdef CONFIG_TFTP_TSIZE |
2f09413f LC |
82 | /* The file size reported by the server */ |
83 | static int TftpTsize; | |
84 | /* The number of hashes we printed */ | |
85 | static short TftpNumchars; | |
4fccb818 | 86 | #endif |
1fb7cd49 SG |
87 | #ifdef CONFIG_CMD_TFTPPUT |
88 | static int TftpWriting; /* 1 if writing, else 0 */ | |
89 | static int TftpFinalBlock; /* 1 if we have sent the last block */ | |
90 | #else | |
91 | #define TftpWriting 0 | |
92 | #endif | |
3f85ce27 | 93 | |
e3fb0abe | 94 | #define STATE_SEND_RRQ 1 |
fe8c2806 WD |
95 | #define STATE_DATA 2 |
96 | #define STATE_TOO_LARGE 3 | |
97 | #define STATE_BAD_MAGIC 4 | |
fbe4b5cb | 98 | #define STATE_OACK 5 |
e59e3562 | 99 | #define STATE_RECV_WRQ 6 |
1fb7cd49 | 100 | #define STATE_SEND_WRQ 7 |
fe8c2806 | 101 | |
2f09413f LC |
102 | /* default TFTP block size */ |
103 | #define TFTP_BLOCK_SIZE 512 | |
104 | /* sequence number is 16 bit */ | |
105 | #define TFTP_SEQUENCE_SIZE ((ulong)(1<<16)) | |
3f85ce27 | 106 | |
fe8c2806 WD |
107 | #define DEFAULT_NAME_LEN (8 + 4 + 1) |
108 | static char default_filename[DEFAULT_NAME_LEN]; | |
a93907c4 JCPV |
109 | |
110 | #ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN | |
111 | #define MAX_LEN 128 | |
112 | #else | |
113 | #define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN | |
114 | #endif | |
115 | ||
116 | static char tftp_filename[MAX_LEN]; | |
fe8c2806 | 117 | |
85eb5caf WD |
118 | /* 512 is poor choice for ethernet, MTU is typically 1500. |
119 | * Minus eth.hdrs thats 1468. Can get 2x better throughput with | |
53a5c424 | 120 | * almost-MTU block sizes. At least try... fall back to 512 if need be. |
89ba81d1 | 121 | * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file) |
53a5c424 | 122 | */ |
89ba81d1 AR |
123 | #ifdef CONFIG_TFTP_BLOCKSIZE |
124 | #define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE | |
125 | #else | |
53a5c424 | 126 | #define TFTP_MTU_BLOCKSIZE 1468 |
89ba81d1 AR |
127 | #endif |
128 | ||
c718b143 LC |
129 | static unsigned short TftpBlkSize = TFTP_BLOCK_SIZE; |
130 | static unsigned short TftpBlkSizeOption = TFTP_MTU_BLOCKSIZE; | |
53a5c424 DU |
131 | |
132 | #ifdef CONFIG_MCAST_TFTP | |
133 | #include <malloc.h> | |
134 | #define MTFTP_BITMAPSIZE 0x1000 | |
135 | static unsigned *Bitmap; | |
c718b143 | 136 | static int PrevBitmapHole, Mapsize = MTFTP_BITMAPSIZE; |
9bb0a1bf LC |
137 | static uchar ProhibitMcast, MasterClient; |
138 | static uchar Multicast; | |
53a5c424 DU |
139 | static int Mcast_port; |
140 | static ulong TftpEndingBlock; /* can get 'last' block before done..*/ | |
141 | ||
c718b143 | 142 | static void parse_multicast_oack(char *pkt, int len); |
53a5c424 DU |
143 | |
144 | static void | |
145 | mcast_cleanup(void) | |
146 | { | |
6d2231e8 LC |
147 | if (Mcast_addr) |
148 | eth_mcast_join(Mcast_addr, 0); | |
149 | if (Bitmap) | |
150 | free(Bitmap); | |
c718b143 | 151 | Bitmap = NULL; |
53a5c424 DU |
152 | Mcast_addr = Multicast = Mcast_port = 0; |
153 | TftpEndingBlock = -1; | |
154 | } | |
155 | ||
156 | #endif /* CONFIG_MCAST_TFTP */ | |
157 | ||
13dfe943 | 158 | static inline void |
bc46dfac | 159 | store_block(int block, uchar *src, unsigned len) |
fe8c2806 | 160 | { |
53a5c424 | 161 | ulong offset = block * TftpBlkSize + TftpBlockWrapOffset; |
3f85ce27 | 162 | ulong newsize = offset + len; |
6d0f6bcf | 163 | #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP |
fe8c2806 WD |
164 | int i, rc = 0; |
165 | ||
c718b143 | 166 | for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) { |
fe8c2806 | 167 | /* start address in flash? */ |
be1b0d27 JF |
168 | if (flash_info[i].flash_id == FLASH_UNKNOWN) |
169 | continue; | |
fe8c2806 WD |
170 | if (load_addr + offset >= flash_info[i].start[0]) { |
171 | rc = 1; | |
172 | break; | |
173 | } | |
174 | } | |
175 | ||
176 | if (rc) { /* Flash is destination for this packet */ | |
c718b143 | 177 | rc = flash_write((char *)src, (ulong)(load_addr+offset), len); |
fe8c2806 | 178 | if (rc) { |
c718b143 | 179 | flash_perror(rc); |
22f6e99d | 180 | net_set_state(NETLOOP_FAIL); |
fe8c2806 WD |
181 | return; |
182 | } | |
13dfe943 | 183 | } else |
6d0f6bcf | 184 | #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */ |
fe8c2806 WD |
185 | { |
186 | (void)memcpy((void *)(load_addr + offset), src, len); | |
187 | } | |
53a5c424 DU |
188 | #ifdef CONFIG_MCAST_TFTP |
189 | if (Multicast) | |
190 | ext2_set_bit(block, Bitmap); | |
191 | #endif | |
fe8c2806 WD |
192 | |
193 | if (NetBootFileXferSize < newsize) | |
194 | NetBootFileXferSize = newsize; | |
195 | } | |
196 | ||
e4cde2f7 | 197 | /* Clear our state ready for a new transfer */ |
165099e7 | 198 | static void new_transfer(void) |
e4cde2f7 SG |
199 | { |
200 | TftpLastBlock = 0; | |
201 | TftpBlockWrap = 0; | |
202 | TftpBlockWrapOffset = 0; | |
203 | #ifdef CONFIG_CMD_TFTPPUT | |
204 | TftpFinalBlock = 0; | |
205 | #endif | |
206 | } | |
207 | ||
1fb7cd49 SG |
208 | #ifdef CONFIG_CMD_TFTPPUT |
209 | /** | |
210 | * Load the next block from memory to be sent over tftp. | |
211 | * | |
212 | * @param block Block number to send | |
213 | * @param dst Destination buffer for data | |
214 | * @param len Number of bytes in block (this one and every other) | |
215 | * @return number of bytes loaded | |
216 | */ | |
217 | static int load_block(unsigned block, uchar *dst, unsigned len) | |
218 | { | |
219 | /* We may want to get the final block from the previous set */ | |
220 | ulong offset = ((int)block - 1) * len + TftpBlockWrapOffset; | |
221 | ulong tosend = len; | |
222 | ||
223 | tosend = min(NetBootFileXferSize - offset, tosend); | |
224 | (void)memcpy(dst, (void *)(save_addr + offset), tosend); | |
225 | debug("%s: block=%d, offset=%ld, len=%d, tosend=%ld\n", __func__, | |
226 | block, offset, len, tosend); | |
227 | return tosend; | |
228 | } | |
229 | #endif | |
230 | ||
c718b143 LC |
231 | static void TftpSend(void); |
232 | static void TftpTimeout(void); | |
fe8c2806 WD |
233 | |
234 | /**********************************************************************/ | |
235 | ||
f5329bbc SG |
236 | static void show_block_marker(void) |
237 | { | |
238 | #ifdef CONFIG_TFTP_TSIZE | |
239 | if (TftpTsize) { | |
240 | ulong pos = TftpBlock * TftpBlkSize + TftpBlockWrapOffset; | |
241 | ||
242 | while (TftpNumchars < pos * 50 / TftpTsize) { | |
243 | putc('#'); | |
244 | TftpNumchars++; | |
245 | } | |
1fb7cd49 | 246 | } else |
f5329bbc | 247 | #endif |
1fb7cd49 | 248 | { |
f5329bbc SG |
249 | if (((TftpBlock - 1) % 10) == 0) |
250 | putc('#'); | |
251 | else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) | |
252 | puts("\n\t "); | |
253 | } | |
254 | } | |
255 | ||
e4cde2f7 SG |
256 | /** |
257 | * restart the current transfer due to an error | |
258 | * | |
259 | * @param msg Message to print for user | |
260 | */ | |
261 | static void restart(const char *msg) | |
262 | { | |
263 | printf("\n%s; starting again\n", msg); | |
264 | #ifdef CONFIG_MCAST_TFTP | |
265 | mcast_cleanup(); | |
266 | #endif | |
267 | NetStartAgain(); | |
268 | } | |
269 | ||
270 | /* | |
271 | * Check if the block number has wrapped, and update progress | |
272 | * | |
273 | * TODO: The egregious use of global variables in this file should be tidied. | |
274 | */ | |
275 | static void update_block_number(void) | |
276 | { | |
277 | /* | |
278 | * RFC1350 specifies that the first data packet will | |
279 | * have sequence number 1. If we receive a sequence | |
280 | * number of 0 this means that there was a wrap | |
281 | * around of the (16 bit) counter. | |
282 | */ | |
283 | if (TftpBlock == 0) { | |
284 | TftpBlockWrap++; | |
285 | TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE; | |
286 | TftpTimeoutCount = 0; /* we've done well, reset thhe timeout */ | |
287 | } else { | |
288 | show_block_marker(); | |
289 | } | |
290 | } | |
291 | ||
f5329bbc SG |
292 | /* The TFTP get or put is complete */ |
293 | static void tftp_complete(void) | |
294 | { | |
295 | #ifdef CONFIG_TFTP_TSIZE | |
296 | /* Print hash marks for the last packet received */ | |
297 | while (TftpTsize && TftpNumchars < 49) { | |
298 | putc('#'); | |
299 | TftpNumchars++; | |
300 | } | |
301 | #endif | |
302 | puts("\ndone\n"); | |
22f6e99d | 303 | net_set_state(NETLOOP_SUCCESS); |
f5329bbc SG |
304 | } |
305 | ||
fe8c2806 | 306 | static void |
c718b143 | 307 | TftpSend(void) |
fe8c2806 | 308 | { |
1fb7cd49 | 309 | uchar *pkt; |
db288a96 JH |
310 | uchar *xp; |
311 | int len = 0; | |
312 | ushort *s; | |
fe8c2806 | 313 | |
85eb5caf | 314 | #ifdef CONFIG_MCAST_TFTP |
53a5c424 | 315 | /* Multicast TFTP.. non-MasterClients do not ACK data. */ |
85eb5caf WD |
316 | if (Multicast |
317 | && (TftpState == STATE_DATA) | |
318 | && (MasterClient == 0)) | |
53a5c424 DU |
319 | return; |
320 | #endif | |
fe8c2806 WD |
321 | /* |
322 | * We will always be sending some sort of packet, so | |
323 | * cobble together the packet headers now. | |
324 | */ | |
594c26f8 | 325 | pkt = NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE; |
fe8c2806 WD |
326 | |
327 | switch (TftpState) { | |
e3fb0abe | 328 | case STATE_SEND_RRQ: |
1fb7cd49 | 329 | case STATE_SEND_WRQ: |
fe8c2806 | 330 | xp = pkt; |
7bc5ee07 | 331 | s = (ushort *)pkt; |
8c6914f1 | 332 | #ifdef CONFIG_CMD_TFTPPUT |
1fb7cd49 SG |
333 | *s++ = htons(TftpState == STATE_SEND_RRQ ? TFTP_RRQ : |
334 | TFTP_WRQ); | |
8c6914f1 SG |
335 | #else |
336 | *s++ = htons(TFTP_RRQ); | |
337 | #endif | |
7bc5ee07 | 338 | pkt = (uchar *)s; |
c718b143 | 339 | strcpy((char *)pkt, tftp_filename); |
fe8c2806 | 340 | pkt += strlen(tftp_filename) + 1; |
c718b143 | 341 | strcpy((char *)pkt, "octet"); |
fe8c2806 | 342 | pkt += 5 /*strlen("octet")*/ + 1; |
c718b143 | 343 | strcpy((char *)pkt, "timeout"); |
fbe4b5cb | 344 | pkt += 7 /*strlen("timeout")*/ + 1; |
c96f86ee | 345 | sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000); |
0ebf04c6 | 346 | debug("send option \"timeout %s\"\n", (char *)pkt); |
fbe4b5cb | 347 | pkt += strlen((char *)pkt) + 1; |
4fccb818 | 348 | #ifdef CONFIG_TFTP_TSIZE |
1fb7cd49 SG |
349 | pkt += sprintf((char *)pkt, "tsize%c%lu%c", |
350 | 0, NetBootFileXferSize, 0); | |
4fccb818 | 351 | #endif |
53a5c424 | 352 | /* try for more effic. blk size */ |
c718b143 LC |
353 | pkt += sprintf((char *)pkt, "blksize%c%d%c", |
354 | 0, TftpBlkSizeOption, 0); | |
85eb5caf | 355 | #ifdef CONFIG_MCAST_TFTP |
53a5c424 | 356 | /* Check all preconditions before even trying the option */ |
13dfe943 JH |
357 | if (!ProhibitMcast) { |
358 | Bitmap = malloc(Mapsize); | |
359 | if (Bitmap && eth_get_dev()->mcast) { | |
360 | free(Bitmap); | |
361 | Bitmap = NULL; | |
362 | pkt += sprintf((char *)pkt, "multicast%c%c", | |
363 | 0, 0); | |
364 | } | |
53a5c424 DU |
365 | } |
366 | #endif /* CONFIG_MCAST_TFTP */ | |
fe8c2806 WD |
367 | len = pkt - xp; |
368 | break; | |
369 | ||
fbe4b5cb | 370 | case STATE_OACK: |
53a5c424 DU |
371 | #ifdef CONFIG_MCAST_TFTP |
372 | /* My turn! Start at where I need blocks I missed.*/ | |
373 | if (Multicast) | |
c718b143 LC |
374 | TftpBlock = ext2_find_next_zero_bit(Bitmap, |
375 | (Mapsize*8), 0); | |
53a5c424 DU |
376 | /*..falling..*/ |
377 | #endif | |
e59e3562 LC |
378 | |
379 | case STATE_RECV_WRQ: | |
53a5c424 | 380 | case STATE_DATA: |
fe8c2806 | 381 | xp = pkt; |
7bc5ee07 | 382 | s = (ushort *)pkt; |
1fb7cd49 SG |
383 | s[0] = htons(TFTP_ACK); |
384 | s[1] = htons(TftpBlock); | |
385 | pkt = (uchar *)(s + 2); | |
386 | #ifdef CONFIG_CMD_TFTPPUT | |
387 | if (TftpWriting) { | |
388 | int toload = TftpBlkSize; | |
389 | int loaded = load_block(TftpBlock, pkt, toload); | |
390 | ||
391 | s[0] = htons(TFTP_DATA); | |
392 | pkt += loaded; | |
393 | TftpFinalBlock = (loaded < toload); | |
394 | } | |
395 | #endif | |
fe8c2806 WD |
396 | len = pkt - xp; |
397 | break; | |
398 | ||
399 | case STATE_TOO_LARGE: | |
400 | xp = pkt; | |
7bc5ee07 WD |
401 | s = (ushort *)pkt; |
402 | *s++ = htons(TFTP_ERROR); | |
1fb7cd49 SG |
403 | *s++ = htons(3); |
404 | ||
7bc5ee07 | 405 | pkt = (uchar *)s; |
c718b143 | 406 | strcpy((char *)pkt, "File too large"); |
fe8c2806 WD |
407 | pkt += 14 /*strlen("File too large")*/ + 1; |
408 | len = pkt - xp; | |
409 | break; | |
410 | ||
411 | case STATE_BAD_MAGIC: | |
412 | xp = pkt; | |
7bc5ee07 WD |
413 | s = (ushort *)pkt; |
414 | *s++ = htons(TFTP_ERROR); | |
415 | *s++ = htons(2); | |
416 | pkt = (uchar *)s; | |
c718b143 | 417 | strcpy((char *)pkt, "File has bad magic"); |
fe8c2806 WD |
418 | pkt += 18 /*strlen("File has bad magic")*/ + 1; |
419 | len = pkt - xp; | |
420 | break; | |
421 | } | |
422 | ||
20478cea | 423 | NetSendUDPPacket(NetServerEther, TftpRemoteIP, TftpRemotePort, |
2f09413f | 424 | TftpOurPort, len); |
fe8c2806 WD |
425 | } |
426 | ||
39bccd21 | 427 | #ifdef CONFIG_CMD_TFTPPUT |
1fb7cd49 SG |
428 | static void icmp_handler(unsigned type, unsigned code, unsigned dest, |
429 | IPaddr_t sip, unsigned src, uchar *pkt, unsigned len) | |
430 | { | |
431 | if (type == ICMP_NOT_REACH && code == ICMP_NOT_REACH_PORT) { | |
432 | /* Oh dear the other end has gone away */ | |
433 | restart("TFTP server died"); | |
434 | } | |
435 | } | |
39bccd21 | 436 | #endif |
1fb7cd49 | 437 | |
fe8c2806 | 438 | static void |
03eb129f LC |
439 | TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, |
440 | unsigned len) | |
fe8c2806 WD |
441 | { |
442 | ushort proto; | |
7bc5ee07 | 443 | ushort *s; |
ff13ac8c | 444 | int i; |
fe8c2806 WD |
445 | |
446 | if (dest != TftpOurPort) { | |
53a5c424 | 447 | #ifdef CONFIG_MCAST_TFTP |
85eb5caf | 448 | if (Multicast |
53a5c424 DU |
449 | && (!Mcast_port || (dest != Mcast_port))) |
450 | #endif | |
0bdd8acc | 451 | return; |
fe8c2806 | 452 | } |
e59e3562 | 453 | if (TftpState != STATE_SEND_RRQ && src != TftpRemotePort && |
1fb7cd49 | 454 | TftpState != STATE_RECV_WRQ && TftpState != STATE_SEND_WRQ) |
fe8c2806 | 455 | return; |
fe8c2806 | 456 | |
7bc325a1 | 457 | if (len < 2) |
fe8c2806 | 458 | return; |
fe8c2806 WD |
459 | len -= 2; |
460 | /* warning: don't use increment (++) in ntohs() macros!! */ | |
7bc5ee07 WD |
461 | s = (ushort *)pkt; |
462 | proto = *s++; | |
463 | pkt = (uchar *)s; | |
fe8c2806 WD |
464 | switch (ntohs(proto)) { |
465 | ||
466 | case TFTP_RRQ: | |
1fb7cd49 SG |
467 | break; |
468 | ||
fe8c2806 | 469 | case TFTP_ACK: |
1fb7cd49 SG |
470 | #ifdef CONFIG_CMD_TFTPPUT |
471 | if (TftpWriting) { | |
472 | if (TftpFinalBlock) { | |
473 | tftp_complete(); | |
474 | } else { | |
475 | /* | |
476 | * Move to the next block. We want our block | |
477 | * count to wrap just like the other end! | |
478 | */ | |
479 | int block = ntohs(*s); | |
480 | int ack_ok = (TftpBlock == block); | |
481 | ||
482 | TftpBlock = (unsigned short)(block + 1); | |
483 | update_block_number(); | |
484 | if (ack_ok) | |
485 | TftpSend(); /* Send next data block */ | |
486 | } | |
487 | } | |
488 | #endif | |
fe8c2806 | 489 | break; |
1fb7cd49 | 490 | |
fe8c2806 WD |
491 | default: |
492 | break; | |
493 | ||
e59e3562 LC |
494 | #ifdef CONFIG_CMD_TFTPSRV |
495 | case TFTP_WRQ: | |
496 | debug("Got WRQ\n"); | |
497 | TftpRemoteIP = sip; | |
498 | TftpRemotePort = src; | |
499 | TftpOurPort = 1024 + (get_timer(0) % 3072); | |
e4cde2f7 | 500 | new_transfer(); |
e59e3562 LC |
501 | TftpSend(); /* Send ACK(0) */ |
502 | break; | |
503 | #endif | |
504 | ||
fbe4b5cb | 505 | case TFTP_OACK: |
d371708a WD |
506 | debug("Got OACK: %s %s\n", |
507 | pkt, | |
508 | pkt + strlen((char *)pkt) + 1); | |
fbe4b5cb | 509 | TftpState = STATE_OACK; |
20478cea | 510 | TftpRemotePort = src; |
60174746 WD |
511 | /* |
512 | * Check for 'blksize' option. | |
513 | * Careful: "i" is signed, "len" is unsigned, thus | |
514 | * something like "len-8" may give a *huge* number | |
515 | */ | |
c718b143 | 516 | for (i = 0; i+8 < len; i++) { |
2e320257 | 517 | if (strcmp((char *)pkt+i, "blksize") == 0) { |
ff13ac8c | 518 | TftpBlkSize = (unsigned short) |
2e320257 | 519 | simple_strtoul((char *)pkt+i+8, NULL, |
c718b143 | 520 | 10); |
0ebf04c6 | 521 | debug("Blocksize ack: %s, %d\n", |
2e320257 | 522 | (char *)pkt+i+8, TftpBlkSize); |
ff13ac8c | 523 | } |
4fccb818 | 524 | #ifdef CONFIG_TFTP_TSIZE |
2e320257 LC |
525 | if (strcmp((char *)pkt+i, "tsize") == 0) { |
526 | TftpTsize = simple_strtoul((char *)pkt+i+6, | |
2f09413f | 527 | NULL, 10); |
4fccb818 | 528 | debug("size = %s, %d\n", |
2e320257 | 529 | (char *)pkt+i+6, TftpTsize); |
4fccb818 RG |
530 | } |
531 | #endif | |
53a5c424 DU |
532 | } |
533 | #ifdef CONFIG_MCAST_TFTP | |
c718b143 | 534 | parse_multicast_oack((char *)pkt, len-1); |
85eb5caf | 535 | if ((Multicast) && (!MasterClient)) |
53a5c424 DU |
536 | TftpState = STATE_DATA; /* passive.. */ |
537 | else | |
538 | #endif | |
1fb7cd49 SG |
539 | #ifdef CONFIG_CMD_TFTPPUT |
540 | if (TftpWriting) { | |
541 | /* Get ready to send the first block */ | |
542 | TftpState = STATE_DATA; | |
543 | TftpBlock++; | |
544 | } | |
545 | #endif | |
546 | TftpSend(); /* Send ACK or first data block */ | |
fbe4b5cb | 547 | break; |
fe8c2806 WD |
548 | case TFTP_DATA: |
549 | if (len < 2) | |
550 | return; | |
551 | len -= 2; | |
552 | TftpBlock = ntohs(*(ushort *)pkt); | |
3f85ce27 | 553 | |
e4cde2f7 | 554 | update_block_number(); |
fe8c2806 | 555 | |
e3fb0abe | 556 | if (TftpState == STATE_SEND_RRQ) |
0ebf04c6 | 557 | debug("Server did not acknowledge timeout option!\n"); |
fbe4b5cb | 558 | |
e59e3562 LC |
559 | if (TftpState == STATE_SEND_RRQ || TftpState == STATE_OACK || |
560 | TftpState == STATE_RECV_WRQ) { | |
3f85ce27 | 561 | /* first block received */ |
fe8c2806 | 562 | TftpState = STATE_DATA; |
20478cea | 563 | TftpRemotePort = src; |
e4cde2f7 | 564 | new_transfer(); |
fe8c2806 | 565 | |
53a5c424 DU |
566 | #ifdef CONFIG_MCAST_TFTP |
567 | if (Multicast) { /* start!=1 common if mcast */ | |
568 | TftpLastBlock = TftpBlock - 1; | |
569 | } else | |
570 | #endif | |
fe8c2806 | 571 | if (TftpBlock != 1) { /* Assertion */ |
c718b143 LC |
572 | printf("\nTFTP error: " |
573 | "First block is not block 1 (%ld)\n" | |
574 | "Starting again\n\n", | |
fe8c2806 | 575 | TftpBlock); |
c718b143 | 576 | NetStartAgain(); |
fe8c2806 WD |
577 | break; |
578 | } | |
579 | } | |
580 | ||
581 | if (TftpBlock == TftpLastBlock) { | |
582 | /* | |
583 | * Same block again; ignore it. | |
584 | */ | |
585 | break; | |
586 | } | |
587 | ||
588 | TftpLastBlock = TftpBlock; | |
e83cc063 | 589 | TftpTimeoutCountMax = TIMEOUT_COUNT; |
c718b143 | 590 | NetSetTimeout(TftpTimeoutMSecs, TftpTimeout); |
fe8c2806 | 591 | |
c718b143 | 592 | store_block(TftpBlock - 1, pkt + 2, len); |
fe8c2806 WD |
593 | |
594 | /* | |
4d69e98c | 595 | * Acknowledge the block just received, which will prompt |
20478cea | 596 | * the remote for the next one. |
fe8c2806 | 597 | */ |
53a5c424 | 598 | #ifdef CONFIG_MCAST_TFTP |
85eb5caf WD |
599 | /* if I am the MasterClient, actively calculate what my next |
600 | * needed block is; else I'm passive; not ACKING | |
a93907c4 | 601 | */ |
53a5c424 DU |
602 | if (Multicast) { |
603 | if (len < TftpBlkSize) { | |
604 | TftpEndingBlock = TftpBlock; | |
605 | } else if (MasterClient) { | |
85eb5caf | 606 | TftpBlock = PrevBitmapHole = |
53a5c424 DU |
607 | ext2_find_next_zero_bit( |
608 | Bitmap, | |
609 | (Mapsize*8), | |
610 | PrevBitmapHole); | |
611 | if (TftpBlock > ((Mapsize*8) - 1)) { | |
c718b143 | 612 | printf("tftpfile too big\n"); |
53a5c424 | 613 | /* try to double it and retry */ |
c718b143 | 614 | Mapsize <<= 1; |
53a5c424 | 615 | mcast_cleanup(); |
c718b143 | 616 | NetStartAgain(); |
53a5c424 DU |
617 | return; |
618 | } | |
619 | TftpLastBlock = TftpBlock; | |
620 | } | |
621 | } | |
622 | #endif | |
c718b143 | 623 | TftpSend(); |
fe8c2806 | 624 | |
53a5c424 DU |
625 | #ifdef CONFIG_MCAST_TFTP |
626 | if (Multicast) { | |
627 | if (MasterClient && (TftpBlock >= TftpEndingBlock)) { | |
c718b143 | 628 | puts("\nMulticast tftp done\n"); |
53a5c424 | 629 | mcast_cleanup(); |
22f6e99d | 630 | net_set_state(NETLOOP_SUCCESS); |
85eb5caf | 631 | } |
13dfe943 | 632 | } else |
53a5c424 | 633 | #endif |
f5329bbc SG |
634 | if (len < TftpBlkSize) |
635 | tftp_complete(); | |
fe8c2806 WD |
636 | break; |
637 | ||
638 | case TFTP_ERROR: | |
c718b143 LC |
639 | printf("\nTFTP error: '%s' (%d)\n", |
640 | pkt + 2, ntohs(*(ushort *)pkt)); | |
aafda38f RB |
641 | |
642 | switch (ntohs(*(ushort *)pkt)) { | |
643 | case TFTP_ERR_FILE_NOT_FOUND: | |
644 | case TFTP_ERR_ACCESS_DENIED: | |
645 | puts("Not retrying...\n"); | |
646 | eth_halt(); | |
22f6e99d | 647 | net_set_state(NETLOOP_FAIL); |
aafda38f RB |
648 | break; |
649 | case TFTP_ERR_UNDEFINED: | |
650 | case TFTP_ERR_DISK_FULL: | |
651 | case TFTP_ERR_UNEXPECTED_OPCODE: | |
652 | case TFTP_ERR_UNKNOWN_TRANSFER_ID: | |
653 | case TFTP_ERR_FILE_ALREADY_EXISTS: | |
654 | default: | |
655 | puts("Starting again\n\n"); | |
53a5c424 | 656 | #ifdef CONFIG_MCAST_TFTP |
aafda38f | 657 | mcast_cleanup(); |
53a5c424 | 658 | #endif |
aafda38f RB |
659 | NetStartAgain(); |
660 | break; | |
661 | } | |
fe8c2806 WD |
662 | break; |
663 | } | |
664 | } | |
665 | ||
666 | ||
667 | static void | |
c718b143 | 668 | TftpTimeout(void) |
fe8c2806 | 669 | { |
e83cc063 | 670 | if (++TftpTimeoutCount > TftpTimeoutCountMax) { |
e4cde2f7 | 671 | restart("Retry count exceeded"); |
fe8c2806 | 672 | } else { |
c718b143 LC |
673 | puts("T "); |
674 | NetSetTimeout(TftpTimeoutMSecs, TftpTimeout); | |
e59e3562 LC |
675 | if (TftpState != STATE_RECV_WRQ) |
676 | TftpSend(); | |
fe8c2806 WD |
677 | } |
678 | } | |
679 | ||
680 | ||
58f317d1 | 681 | void TftpStart(enum proto_t protocol) |
fe8c2806 | 682 | { |
ecb0ccd9 | 683 | char *ep; /* Environment pointer */ |
89ba81d1 | 684 | |
c96f86ee WD |
685 | /* |
686 | * Allow the user to choose TFTP blocksize and timeout. | |
687 | * TFTP protocol has a minimal timeout of 1 second. | |
688 | */ | |
2cb53608 LC |
689 | ep = getenv("tftpblocksize"); |
690 | if (ep != NULL) | |
89ba81d1 | 691 | TftpBlkSizeOption = simple_strtol(ep, NULL, 10); |
c96f86ee | 692 | |
2cb53608 LC |
693 | ep = getenv("tftptimeout"); |
694 | if (ep != NULL) | |
c96f86ee WD |
695 | TftpTimeoutMSecs = simple_strtol(ep, NULL, 10); |
696 | ||
697 | if (TftpTimeoutMSecs < 1000) { | |
698 | printf("TFTP timeout (%ld ms) too low, " | |
699 | "set minimum = 1000 ms\n", | |
700 | TftpTimeoutMSecs); | |
701 | TftpTimeoutMSecs = 1000; | |
702 | } | |
703 | ||
704 | debug("TFTP blocksize = %i, timeout = %ld ms\n", | |
705 | TftpBlkSizeOption, TftpTimeoutMSecs); | |
ecb0ccd9 | 706 | |
20478cea | 707 | TftpRemoteIP = NetServerIP; |
fe8c2806 | 708 | if (BootFile[0] == '\0') { |
ea45cb0a | 709 | sprintf(default_filename, "%02X%02X%02X%02X.img", |
c43352cc WD |
710 | NetOurIP & 0xFF, |
711 | (NetOurIP >> 8) & 0xFF, | |
712 | (NetOurIP >> 16) & 0xFF, | |
c718b143 | 713 | (NetOurIP >> 24) & 0xFF); |
a93907c4 JCPV |
714 | |
715 | strncpy(tftp_filename, default_filename, MAX_LEN); | |
716 | tftp_filename[MAX_LEN-1] = 0; | |
fe8c2806 | 717 | |
c718b143 | 718 | printf("*** Warning: no boot file name; using '%s'\n", |
fe8c2806 WD |
719 | tftp_filename); |
720 | } else { | |
c718b143 | 721 | char *p = strchr(BootFile, ':'); |
a93907c4 JCPV |
722 | |
723 | if (p == NULL) { | |
724 | strncpy(tftp_filename, BootFile, MAX_LEN); | |
725 | tftp_filename[MAX_LEN-1] = 0; | |
726 | } else { | |
20478cea | 727 | TftpRemoteIP = string_to_ip(BootFile); |
6a86bb6c | 728 | strncpy(tftp_filename, p + 1, MAX_LEN); |
a93907c4 JCPV |
729 | tftp_filename[MAX_LEN-1] = 0; |
730 | } | |
fe8c2806 WD |
731 | } |
732 | ||
c718b143 | 733 | printf("Using %s device\n", eth_get_name()); |
1fb7cd49 | 734 | printf("TFTP %s server %pI4; our IP address is %pI4", |
8c6914f1 SG |
735 | #ifdef CONFIG_CMD_TFTPPUT |
736 | protocol == TFTPPUT ? "to" : "from", | |
737 | #else | |
738 | "from", | |
739 | #endif | |
740 | &TftpRemoteIP, &NetOurIP); | |
fe8c2806 WD |
741 | |
742 | /* Check if we need to send across this subnet */ | |
743 | if (NetOurGatewayIP && NetOurSubnetMask) { | |
0bdd8acc | 744 | IPaddr_t OurNet = NetOurIP & NetOurSubnetMask; |
20478cea | 745 | IPaddr_t RemoteNet = TftpRemoteIP & NetOurSubnetMask; |
fe8c2806 | 746 | |
20478cea | 747 | if (OurNet != RemoteNet) |
0bdd8acc LC |
748 | printf("; sending through gateway %pI4", |
749 | &NetOurGatewayIP); | |
fe8c2806 | 750 | } |
c718b143 | 751 | putc('\n'); |
fe8c2806 | 752 | |
c718b143 | 753 | printf("Filename '%s'.", tftp_filename); |
fe8c2806 WD |
754 | |
755 | if (NetBootFileSize) { | |
c718b143 LC |
756 | printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9); |
757 | print_size(NetBootFileSize<<9, ""); | |
fe8c2806 WD |
758 | } |
759 | ||
c718b143 | 760 | putc('\n'); |
1fb7cd49 SG |
761 | #ifdef CONFIG_CMD_TFTPPUT |
762 | TftpWriting = (protocol == TFTPPUT); | |
763 | if (TftpWriting) { | |
764 | printf("Save address: 0x%lx\n", save_addr); | |
765 | printf("Save size: 0x%lx\n", save_size); | |
766 | NetBootFileXferSize = save_size; | |
767 | puts("Saving: *\b"); | |
768 | TftpState = STATE_SEND_WRQ; | |
769 | new_transfer(); | |
770 | } else | |
771 | #endif | |
772 | { | |
773 | printf("Load address: 0x%lx\n", load_addr); | |
774 | puts("Loading: *\b"); | |
775 | TftpState = STATE_SEND_RRQ; | |
776 | } | |
fe8c2806 | 777 | |
e83cc063 BS |
778 | TftpTimeoutCountMax = TftpRRQTimeoutCountMax; |
779 | ||
c718b143 | 780 | NetSetTimeout(TftpTimeoutMSecs, TftpTimeout); |
ece223b5 | 781 | net_set_udp_handler(TftpHandler); |
39bccd21 | 782 | #ifdef CONFIG_CMD_TFTPPUT |
1fb7cd49 | 783 | net_set_icmp_handler(icmp_handler); |
39bccd21 | 784 | #endif |
20478cea | 785 | TftpRemotePort = WELL_KNOWN_PORT; |
fe8c2806 | 786 | TftpTimeoutCount = 0; |
ecb0ccd9 | 787 | /* Use a pseudo-random port unless a specific port is set */ |
fe8c2806 | 788 | TftpOurPort = 1024 + (get_timer(0) % 3072); |
53a5c424 | 789 | |
ecb0ccd9 | 790 | #ifdef CONFIG_TFTP_PORT |
2cb53608 | 791 | ep = getenv("tftpdstp"); |
7bc325a1 | 792 | if (ep != NULL) |
20478cea | 793 | TftpRemotePort = simple_strtol(ep, NULL, 10); |
2cb53608 | 794 | ep = getenv("tftpsrcp"); |
7bc325a1 | 795 | if (ep != NULL) |
c718b143 | 796 | TftpOurPort = simple_strtol(ep, NULL, 10); |
ecb0ccd9 | 797 | #endif |
fbe4b5cb | 798 | TftpBlock = 0; |
fe8c2806 | 799 | |
73a8b27c WD |
800 | /* zero out server ether in case the server ip has changed */ |
801 | memset(NetServerEther, 0, 6); | |
53a5c424 DU |
802 | /* Revert TftpBlkSize to dflt */ |
803 | TftpBlkSize = TFTP_BLOCK_SIZE; | |
804 | #ifdef CONFIG_MCAST_TFTP | |
a93907c4 | 805 | mcast_cleanup(); |
53a5c424 | 806 | #endif |
4fccb818 RG |
807 | #ifdef CONFIG_TFTP_TSIZE |
808 | TftpTsize = 0; | |
809 | TftpNumchars = 0; | |
810 | #endif | |
73a8b27c | 811 | |
c718b143 | 812 | TftpSend(); |
fe8c2806 WD |
813 | } |
814 | ||
e59e3562 LC |
815 | #ifdef CONFIG_CMD_TFTPSRV |
816 | void | |
817 | TftpStartServer(void) | |
818 | { | |
819 | tftp_filename[0] = 0; | |
820 | ||
e59e3562 | 821 | printf("Using %s device\n", eth_get_name()); |
e59e3562 LC |
822 | printf("Listening for TFTP transfer on %pI4\n", &NetOurIP); |
823 | printf("Load address: 0x%lx\n", load_addr); | |
824 | ||
825 | puts("Loading: *\b"); | |
826 | ||
827 | TftpTimeoutCountMax = TIMEOUT_COUNT; | |
828 | TftpTimeoutCount = 0; | |
829 | TftpTimeoutMSecs = TIMEOUT; | |
830 | NetSetTimeout(TftpTimeoutMSecs, TftpTimeout); | |
831 | ||
832 | /* Revert TftpBlkSize to dflt */ | |
833 | TftpBlkSize = TFTP_BLOCK_SIZE; | |
834 | TftpBlock = 0; | |
835 | TftpOurPort = WELL_KNOWN_PORT; | |
836 | ||
837 | #ifdef CONFIG_TFTP_TSIZE | |
838 | TftpTsize = 0; | |
839 | TftpNumchars = 0; | |
840 | #endif | |
841 | ||
842 | TftpState = STATE_RECV_WRQ; | |
ece223b5 | 843 | net_set_udp_handler(TftpHandler); |
e59e3562 LC |
844 | } |
845 | #endif /* CONFIG_CMD_TFTPSRV */ | |
846 | ||
53a5c424 DU |
847 | #ifdef CONFIG_MCAST_TFTP |
848 | /* Credits: atftp project. | |
849 | */ | |
850 | ||
851 | /* pick up BcastAddr, Port, and whether I am [now] the master-client. * | |
852 | * Frame: | |
853 | * +-------+-----------+---+-------~~-------+---+ | |
854 | * | opc | multicast | 0 | addr, port, mc | 0 | | |
855 | * +-------+-----------+---+-------~~-------+---+ | |
856 | * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then | |
857 | * I am the new master-client so must send ACKs to DataBlocks. If I am not | |
858 | * master-client, I'm a passive client, gathering what DataBlocks I may and | |
859 | * making note of which ones I got in my bitmask. | |
860 | * In theory, I never go from master->passive.. | |
861 | * .. this comes in with pkt already pointing just past opc | |
862 | */ | |
863 | static void parse_multicast_oack(char *pkt, int len) | |
864 | { | |
c718b143 LC |
865 | int i; |
866 | IPaddr_t addr; | |
867 | char *mc_adr, *port, *mc; | |
53a5c424 | 868 | |
c718b143 | 869 | mc_adr = port = mc = NULL; |
53a5c424 DU |
870 | /* march along looking for 'multicast\0', which has to start at least |
871 | * 14 bytes back from the end. | |
872 | */ | |
c718b143 LC |
873 | for (i = 0; i < len-14; i++) |
874 | if (strcmp(pkt+i, "multicast") == 0) | |
53a5c424 DU |
875 | break; |
876 | if (i >= (len-14)) /* non-Multicast OACK, ign. */ | |
877 | return; | |
85eb5caf | 878 | |
c718b143 | 879 | i += 10; /* strlen multicast */ |
53a5c424 | 880 | mc_adr = pkt+i; |
c718b143 | 881 | for (; i < len; i++) { |
53a5c424 DU |
882 | if (*(pkt+i) == ',') { |
883 | *(pkt+i) = '\0'; | |
884 | if (port) { | |
885 | mc = pkt+i+1; | |
886 | break; | |
887 | } else { | |
888 | port = pkt+i+1; | |
889 | } | |
890 | } | |
891 | } | |
6d2231e8 LC |
892 | if (!port || !mc_adr || !mc) |
893 | return; | |
53a5c424 | 894 | if (Multicast && MasterClient) { |
c718b143 | 895 | printf("I got a OACK as master Client, WRONG!\n"); |
53a5c424 DU |
896 | return; |
897 | } | |
898 | /* ..I now accept packets destined for this MCAST addr, port */ | |
899 | if (!Multicast) { | |
900 | if (Bitmap) { | |
c718b143 | 901 | printf("Internal failure! no mcast.\n"); |
53a5c424 | 902 | free(Bitmap); |
c718b143 LC |
903 | Bitmap = NULL; |
904 | ProhibitMcast = 1; | |
53a5c424 DU |
905 | return ; |
906 | } | |
907 | /* I malloc instead of pre-declare; so that if the file ends | |
85eb5caf WD |
908 | * up being too big for this bitmap I can retry |
909 | */ | |
2cb53608 LC |
910 | Bitmap = malloc(Mapsize); |
911 | if (!Bitmap) { | |
c718b143 LC |
912 | printf("No Bitmap, no multicast. Sorry.\n"); |
913 | ProhibitMcast = 1; | |
53a5c424 DU |
914 | return; |
915 | } | |
c718b143 | 916 | memset(Bitmap, 0, Mapsize); |
53a5c424 DU |
917 | PrevBitmapHole = 0; |
918 | Multicast = 1; | |
919 | } | |
920 | addr = string_to_ip(mc_adr); | |
921 | if (Mcast_addr != addr) { | |
922 | if (Mcast_addr) | |
923 | eth_mcast_join(Mcast_addr, 0); | |
2cb53608 LC |
924 | Mcast_addr = addr; |
925 | if (eth_mcast_join(Mcast_addr, 1)) { | |
c718b143 LC |
926 | printf("Fail to set mcast, revert to TFTP\n"); |
927 | ProhibitMcast = 1; | |
53a5c424 DU |
928 | mcast_cleanup(); |
929 | NetStartAgain(); | |
930 | } | |
931 | } | |
c718b143 LC |
932 | MasterClient = (unsigned char)simple_strtoul((char *)mc, NULL, 10); |
933 | Mcast_port = (unsigned short)simple_strtoul(port, NULL, 10); | |
934 | printf("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient); | |
53a5c424 DU |
935 | return; |
936 | } | |
937 | ||
938 | #endif /* Multicast TFTP */ |