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