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