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