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