2 * Copyright 1994, 1995, 2000 Neil Russell.
5 * Copyright 2011 Comelit Group SpA,
9 #include <display_options.h>
10 #include <efi_loader.h>
18 #include <asm/global_data.h>
22 DECLARE_GLOBAL_DATA_PTR;
24 /* Well known TFTP port # */
25 #define WELL_KNOWN_PORT 69
26 /* Millisecs to timeout for lost pkt */
27 #define TIMEOUT 5000UL
28 /* Number of "loading" hashes per line (for checking the image size) */
29 #define HASHES_PER_LINE 65
41 static ulong timeout_ms = TIMEOUT;
42 static int timeout_count_max = (CONFIG_NET_RETRY_COUNT * 2);
43 static ulong time_start; /* Record time we started tftp */
44 static struct in6_addr tftp_remote_ip6;
47 * These globals govern the timeout behavior when attempting a connection to a
48 * TFTP server. tftp_timeout_ms specifies the number of milliseconds to
49 * wait for the server to respond to initial connection. Second global,
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
52 * positive. The globals are meant to be set (and restored) by code needing
53 * non-standard timeout behavior when initiating a TFTP transfer.
55 ulong tftp_timeout_ms = TIMEOUT;
56 int tftp_timeout_count_max = (CONFIG_NET_RETRY_COUNT * 2);
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 TFTP_ERR_OPTION_NEGOTIATION = 8,
69 static struct in_addr tftp_remote_ip;
70 /* The UDP port at their end */
71 static int tftp_remote_port;
72 /* The UDP port at our end */
73 static int tftp_our_port;
74 static int timeout_count;
75 /* packet sequence number */
76 static ulong tftp_cur_block;
77 /* last packet sequence number received */
78 static ulong tftp_prev_block;
79 /* count of sequence number wraparounds */
80 static ulong tftp_block_wrap;
81 /* memory offset due to wrapping */
82 static ulong tftp_block_wrap_offset;
83 static int tftp_state;
84 static ulong tftp_load_addr;
86 static ulong tftp_load_size;
88 #ifdef CONFIG_TFTP_TSIZE
89 /* The file size reported by the server */
90 static int tftp_tsize;
91 /* The number of hashes we printed */
92 static short tftp_tsize_num_hash;
94 /* The window size negotiated */
95 static ushort tftp_windowsize;
96 /* Next block to send ack to */
97 static ushort tftp_next_ack;
98 /* Last nack block we send */
99 static ushort tftp_last_nack;
100 #ifdef CONFIG_CMD_TFTPPUT
101 /* 1 if writing, else 0 */
102 static int tftp_put_active;
103 /* 1 if we have sent the last block */
104 static int tftp_put_final_block_sent;
106 #define tftp_put_active 0
109 #define STATE_SEND_RRQ 1
111 #define STATE_TOO_LARGE 3
112 #define STATE_BAD_MAGIC 4
114 #define STATE_RECV_WRQ 6
115 #define STATE_SEND_WRQ 7
116 #define STATE_INVALID_OPTION 8
118 /* default TFTP block size */
119 #define TFTP_BLOCK_SIZE 512
120 #define TFTP_MTU_BLOCKSIZE6 (CONFIG_TFTP_BLOCKSIZE - 20)
121 /* sequence number is 16 bit */
122 #define TFTP_SEQUENCE_SIZE ((ulong)(1<<16))
124 #define DEFAULT_NAME_LEN (8 + 4 + 1)
125 static char default_filename[DEFAULT_NAME_LEN];
127 #ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
130 #define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
133 static char tftp_filename[MAX_LEN];
135 /* 512 is poor choice for ethernet, MTU is typically 1500.
136 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
137 * almost-MTU block sizes. At least try... fall back to 512 if need be.
138 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
141 /* When windowsize is defined to 1,
142 * tftp behaves the same way as it was
145 #ifdef CONFIG_TFTP_WINDOWSIZE
146 #define TFTP_WINDOWSIZE CONFIG_TFTP_WINDOWSIZE
148 #define TFTP_WINDOWSIZE 1
151 static unsigned short tftp_block_size = TFTP_BLOCK_SIZE;
152 static unsigned short tftp_block_size_option = CONFIG_TFTP_BLOCKSIZE;
153 static unsigned short tftp_window_size_option = TFTP_WINDOWSIZE;
155 static inline int store_block(int block, uchar *src, unsigned int len)
157 ulong offset = block * tftp_block_size + tftp_block_wrap_offset -
159 ulong newsize = offset + len;
160 ulong store_addr = tftp_load_addr + offset;
164 ulong end_addr = tftp_load_addr + tftp_load_size;
167 end_addr = ULONG_MAX;
169 if (store_addr < tftp_load_addr ||
170 store_addr + len > end_addr) {
171 puts("\nTFTP error: ");
172 puts("trying to overwrite reserved memory...\n");
176 ptr = map_sysmem(store_addr, len);
177 memcpy(ptr, src, len);
180 if (net_boot_file_size < newsize)
181 net_boot_file_size = newsize;
186 /* Clear our state ready for a new transfer */
187 static void new_transfer(void)
191 tftp_block_wrap_offset = 0;
192 #ifdef CONFIG_CMD_TFTPPUT
193 tftp_put_final_block_sent = 0;
197 #ifdef CONFIG_CMD_TFTPPUT
199 * Load the next block from memory to be sent over tftp.
201 * @param block Block number to send
202 * @param dst Destination buffer for data
203 * @param len Number of bytes in block (this one and every other)
204 * Return: number of bytes loaded
206 static int load_block(unsigned block, uchar *dst, unsigned len)
208 /* We may want to get the final block from the previous set */
209 ulong offset = block * tftp_block_size + tftp_block_wrap_offset -
213 tosend = min(net_boot_file_size - offset, tosend);
214 (void)memcpy(dst, (void *)(image_save_addr + offset), tosend);
215 debug("%s: block=%u, offset=%lu, len=%u, tosend=%lu\n", __func__,
216 block, offset, len, tosend);
221 static void tftp_send(void);
222 static void tftp_timeout_handler(void);
224 /**********************************************************************/
226 static void show_block_marker(void)
230 #ifdef CONFIG_TFTP_TSIZE
232 pos = tftp_cur_block * tftp_block_size +
233 tftp_block_wrap_offset;
234 if (pos > tftp_tsize)
237 while (tftp_tsize_num_hash < pos * 50 / tftp_tsize) {
239 tftp_tsize_num_hash++;
244 pos = (tftp_cur_block - 1) +
245 (tftp_block_wrap * TFTP_SEQUENCE_SIZE);
248 else if (((pos + 1) % (10 * HASHES_PER_LINE)) == 0)
254 * restart the current transfer due to an error
256 * @param msg Message to print for user
258 static void restart(const char *msg)
260 printf("\n%s; starting again\n", msg);
265 * Check if the block number has wrapped, and update progress
267 * TODO: The egregious use of global variables in this file should be tidied.
269 static void update_block_number(void)
272 * RFC1350 specifies that the first data packet will
273 * have sequence number 1. If we receive a sequence
274 * number of 0 this means that there was a wrap
275 * around of the (16 bit) counter.
277 if (tftp_cur_block == 0 && tftp_prev_block != 0) {
279 tftp_block_wrap_offset += tftp_block_size * TFTP_SEQUENCE_SIZE;
280 timeout_count = 0; /* we've done well, reset the timeout */
285 /* The TFTP get or put is complete */
286 static void tftp_complete(void)
288 #ifdef CONFIG_TFTP_TSIZE
289 /* Print hash marks for the last packet received */
290 while (tftp_tsize && tftp_tsize_num_hash < 49) {
292 tftp_tsize_num_hash++;
295 print_size(tftp_tsize, "");
297 time_start = get_timer(time_start);
298 if (time_start > 0) {
299 puts("\n\t "); /* Line up with "Loading: " */
300 print_size(net_boot_file_size /
301 time_start * 1000, "/s");
304 if (!tftp_put_active)
305 efi_set_bootdev("Net", "", tftp_filename,
306 map_sysmem(tftp_load_addr, 0),
308 net_set_state(NETLOOP_SUCCESS);
311 static void tftp_send(void)
317 bool err_pkt = false;
320 * We will always be sending some sort of packet, so
321 * cobble together the packet headers now.
323 if (IS_ENABLED(CONFIG_IPV6) && use_ip6)
324 pkt = net_tx_packet + net_eth_hdr_size() +
325 IP6_HDR_SIZE + UDP_HDR_SIZE;
327 pkt = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
329 switch (tftp_state) {
334 #ifdef CONFIG_CMD_TFTPPUT
335 *s++ = htons(tftp_state == STATE_SEND_RRQ ? TFTP_RRQ :
338 *s++ = htons(TFTP_RRQ);
341 strcpy((char *)pkt, tftp_filename);
342 pkt += strlen(tftp_filename) + 1;
343 strcpy((char *)pkt, "octet");
344 pkt += 5 /*strlen("octet")*/ + 1;
345 strcpy((char *)pkt, "timeout");
346 pkt += 7 /*strlen("timeout")*/ + 1;
347 sprintf((char *)pkt, "%lu", timeout_ms / 1000);
348 debug("send option \"timeout %s\"\n", (char *)pkt);
349 pkt += strlen((char *)pkt) + 1;
350 #ifdef CONFIG_TFTP_TSIZE
351 pkt += sprintf((char *)pkt, "tsize%c%u%c",
352 0, net_boot_file_size, 0);
354 /* try for more effic. blk size */
355 pkt += sprintf((char *)pkt, "blksize%c%d%c",
356 0, tftp_block_size_option, 0);
358 /* try for more effic. window size.
359 * Implemented only for tftp get.
360 * Don't bother sending if it's 1
362 if (tftp_state == STATE_SEND_RRQ && tftp_window_size_option > 1)
363 pkt += sprintf((char *)pkt, "windowsize%c%d%c",
364 0, tftp_window_size_option, 0);
374 s[0] = htons(TFTP_ACK);
375 s[1] = htons(tftp_cur_block);
376 pkt = (uchar *)(s + 2);
377 #ifdef CONFIG_CMD_TFTPPUT
378 if (tftp_put_active) {
379 int toload = tftp_block_size;
380 int loaded = load_block(tftp_cur_block, pkt, toload);
382 s[0] = htons(TFTP_DATA);
384 tftp_put_final_block_sent = (loaded < toload);
390 case STATE_TOO_LARGE:
393 *s++ = htons(TFTP_ERROR);
397 strcpy((char *)pkt, "File too large");
398 pkt += 14 /*strlen("File too large")*/ + 1;
403 case STATE_BAD_MAGIC:
406 *s++ = htons(TFTP_ERROR);
409 strcpy((char *)pkt, "File has bad magic");
410 pkt += 18 /*strlen("File has bad magic")*/ + 1;
415 case STATE_INVALID_OPTION:
418 *s++ = htons(TFTP_ERROR);
419 *s++ = htons(TFTP_ERR_OPTION_NEGOTIATION);
421 strcpy((char *)pkt, "Option Negotiation Failed");
422 /* strlen("Option Negotiation Failed") + NULL*/
429 if (IS_ENABLED(CONFIG_IPV6) && use_ip6)
430 net_send_udp_packet6(net_server_ethaddr,
435 net_send_udp_packet(net_server_ethaddr, tftp_remote_ip,
436 tftp_remote_port, tftp_our_port, len);
439 net_set_state(NETLOOP_FAIL);
442 #ifdef CONFIG_CMD_TFTPPUT
443 static void icmp_handler(unsigned type, unsigned code, unsigned dest,
444 struct in_addr sip, unsigned src, uchar *pkt,
447 if (type == ICMP_NOT_REACH && code == ICMP_NOT_REACH_PORT) {
448 /* Oh dear the other end has gone away */
449 restart("TFTP server died");
454 static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
455 unsigned src, unsigned len)
460 u16 timeout_val_rcvd;
462 if (dest != tftp_our_port) {
465 if (tftp_state != STATE_SEND_RRQ && src != tftp_remote_port &&
466 tftp_state != STATE_RECV_WRQ && tftp_state != STATE_SEND_WRQ)
472 /* warning: don't use increment (++) in ntohs() macros!! */
476 switch (ntohs(proto)) {
481 #ifdef CONFIG_CMD_TFTPPUT
482 if (tftp_put_active) {
483 if (tftp_put_final_block_sent) {
487 * Move to the next block. We want our block
488 * count to wrap just like the other end!
490 int block = ntohs(*s);
491 int ack_ok = (tftp_cur_block == block);
493 tftp_prev_block = tftp_cur_block;
494 tftp_cur_block = (unsigned short)(block + 1);
495 update_block_number();
497 tftp_send(); /* Send next data block */
506 #ifdef CONFIG_CMD_TFTPSRV
509 tftp_remote_ip = sip;
510 tftp_remote_port = src;
511 tftp_our_port = 1024 + (get_timer(0) % 3072);
513 tftp_send(); /* Send ACK(0) */
519 for (i = 0; i < len; i++) {
526 tftp_state = STATE_OACK;
527 tftp_remote_port = src;
529 * Check for 'blksize' option.
530 * Careful: "i" is signed, "len" is unsigned, thus
531 * something like "len-8" may give a *huge* number
533 for (i = 0; i+8 < len; i++) {
534 if (strcasecmp((char *)pkt + i, "blksize") == 0) {
535 tftp_block_size = (unsigned short)
536 dectoul((char *)pkt + i + 8, NULL);
537 debug("Blocksize oack: %s, %d\n",
538 (char *)pkt + i + 8, tftp_block_size);
539 if (tftp_block_size > tftp_block_size_option) {
540 printf("Invalid blk size(=%d)\n",
542 tftp_state = STATE_INVALID_OPTION;
545 if (strcasecmp((char *)pkt + i, "timeout") == 0) {
546 timeout_val_rcvd = (unsigned short)
547 dectoul((char *)pkt + i + 8, NULL);
548 debug("Timeout oack: %s, %d\n",
549 (char *)pkt + i + 8, timeout_val_rcvd);
550 if (timeout_val_rcvd != (timeout_ms / 1000)) {
551 printf("Invalid timeout val(=%d s)\n",
553 tftp_state = STATE_INVALID_OPTION;
556 #ifdef CONFIG_TFTP_TSIZE
557 if (strcasecmp((char *)pkt + i, "tsize") == 0) {
558 tftp_tsize = dectoul((char *)pkt + i + 6,
560 debug("size = %s, %d\n",
561 (char *)pkt + i + 6, tftp_tsize);
564 if (strcasecmp((char *)pkt + i, "windowsize") == 0) {
566 dectoul((char *)pkt + i + 11, NULL);
567 debug("windowsize = %s, %d\n",
568 (char *)pkt + i + 11, tftp_windowsize);
572 tftp_next_ack = tftp_windowsize;
574 #ifdef CONFIG_CMD_TFTPPUT
575 if (tftp_put_active && tftp_state == STATE_OACK) {
576 /* Get ready to send the first block */
577 tftp_state = STATE_DATA;
581 tftp_send(); /* Send ACK or first data block */
588 if (ntohs(*(__be16 *)pkt) != (ushort)(tftp_cur_block + 1)) {
589 debug("Received unexpected block: %d, expected: %d\n",
590 ntohs(*(__be16 *)pkt),
591 (ushort)(tftp_cur_block + 1));
593 * Only ACK if the block count received is greater than
594 * the expected block count, otherwise skip ACK.
595 * (required to properly handle the server retransmitting
598 if ((ushort)(tftp_cur_block + 1) - (short)(ntohs(*(__be16 *)pkt)) > 0)
601 * If one packet is dropped most likely
602 * all other buffers in the window
603 * that will arrive will cause a sending NACK.
604 * This just overwellms the server, let's just send one.
606 if (tftp_last_nack != tftp_cur_block) {
608 tftp_last_nack = tftp_cur_block;
609 tftp_next_ack = (ushort)(tftp_cur_block +
616 tftp_cur_block %= TFTP_SEQUENCE_SIZE;
618 if (tftp_state == STATE_SEND_RRQ) {
619 debug("Server did not acknowledge any options!\n");
620 tftp_next_ack = tftp_windowsize;
623 if (tftp_state == STATE_SEND_RRQ || tftp_state == STATE_OACK ||
624 tftp_state == STATE_RECV_WRQ) {
625 /* first block received */
626 tftp_state = STATE_DATA;
627 tftp_remote_port = src;
630 if (tftp_cur_block != 1) { /* Assertion */
631 puts("\nTFTP error: ");
632 printf("First block is not block 1 (%ld)\n",
634 puts("Starting again\n\n");
640 if (tftp_cur_block == tftp_prev_block) {
641 /* Same block again; ignore it. */
645 update_block_number();
646 tftp_prev_block = tftp_cur_block;
647 timeout_count_max = tftp_timeout_count_max;
648 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
650 if (store_block(tftp_cur_block, pkt + 2, len)) {
652 net_set_state(NETLOOP_FAIL);
656 if (len < tftp_block_size) {
663 * Acknowledge the block just received, which will prompt
664 * the remote for the next one.
666 if (tftp_cur_block == tftp_next_ack) {
668 tftp_next_ack += tftp_windowsize;
673 printf("\nTFTP error: '%s' (%d)\n",
674 pkt + 2, ntohs(*(__be16 *)pkt));
676 switch (ntohs(*(__be16 *)pkt)) {
677 case TFTP_ERR_FILE_NOT_FOUND:
678 case TFTP_ERR_ACCESS_DENIED:
679 puts("Not retrying...\n");
681 net_set_state(NETLOOP_FAIL);
683 case TFTP_ERR_UNDEFINED:
684 case TFTP_ERR_DISK_FULL:
685 case TFTP_ERR_UNEXPECTED_OPCODE:
686 case TFTP_ERR_UNKNOWN_TRANSFER_ID:
687 case TFTP_ERR_FILE_ALREADY_EXISTS:
689 puts("Starting again\n\n");
698 static void tftp_timeout_handler(void)
700 if (++timeout_count > timeout_count_max) {
701 restart("Retry count exceeded");
704 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
705 if (tftp_state != STATE_RECV_WRQ)
710 /* Initialize tftp_load_addr and tftp_load_size from image_load_addr and lmb */
711 static int tftp_init_load_addr(void)
715 phys_size_t max_size;
717 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
719 max_size = lmb_get_free_size(&lmb, image_load_addr);
723 tftp_load_size = max_size;
725 tftp_load_addr = image_load_addr;
729 static int saved_tftp_block_size_option;
730 static void sanitize_tftp_block_size_option(enum proto_t protocol)
736 max_defrag = config_opt_enabled(CONFIG_IP_DEFRAG, CONFIG_NET_MAXDEFRAG, 0);
738 /* Account for IP, UDP and TFTP headers. */
739 cap = max_defrag - (20 + 8 + 4);
740 /* RFC2348 sets a hard upper limit. */
741 cap = min(cap, 65464);
745 * If not CONFIG_IP_DEFRAG, cap at the same value as
746 * for tftp put, namely normal MTU minus protocol
753 * U-Boot does not support IP fragmentation on TX, so
754 * this must be small enough that it fits normal MTU
755 * (and small enough that it fits net_tx_packet which
756 * has room for PKTSIZE_ALIGN bytes).
760 if (tftp_block_size_option > cap) {
761 printf("Capping tftp block size option to %d (was %d)\n",
762 cap, tftp_block_size_option);
763 saved_tftp_block_size_option = tftp_block_size_option;
764 tftp_block_size_option = cap;
768 void tftp_start(enum proto_t protocol)
770 __maybe_unused char *ep; /* Environment pointer */
772 if (saved_tftp_block_size_option) {
773 tftp_block_size_option = saved_tftp_block_size_option;
774 saved_tftp_block_size_option = 0;
777 if (IS_ENABLED(CONFIG_NET_TFTP_VARS)) {
780 * Allow the user to choose TFTP blocksize and timeout.
781 * TFTP protocol has a minimal timeout of 1 second.
784 ep = env_get("tftpblocksize");
786 tftp_block_size_option = simple_strtol(ep, NULL, 10);
788 ep = env_get("tftpwindowsize");
790 tftp_window_size_option = simple_strtol(ep, NULL, 10);
792 ep = env_get("tftptimeout");
794 timeout_ms = simple_strtol(ep, NULL, 10);
796 if (timeout_ms < 1000) {
797 printf("TFTP timeout (%ld ms) too low, set min = 1000 ms\n",
802 ep = env_get("tftptimeoutcountmax");
804 tftp_timeout_count_max = simple_strtol(ep, NULL, 10);
806 if (tftp_timeout_count_max < 0) {
807 printf("TFTP timeout count max (%d ms) negative, set to 0\n",
808 tftp_timeout_count_max);
809 tftp_timeout_count_max = 0;
813 sanitize_tftp_block_size_option(protocol);
815 debug("TFTP blocksize = %i, TFTP windowsize = %d timeout = %ld ms\n",
816 tftp_block_size_option, tftp_window_size_option, timeout_ms);
818 if (IS_ENABLED(CONFIG_IPV6))
819 tftp_remote_ip6 = net_server_ip6;
821 tftp_remote_ip = net_server_ip;
822 if (!net_parse_bootfile(&tftp_remote_ip, tftp_filename, MAX_LEN)) {
823 sprintf(default_filename, "%02X%02X%02X%02X.img",
824 net_ip.s_addr & 0xFF,
825 (net_ip.s_addr >> 8) & 0xFF,
826 (net_ip.s_addr >> 16) & 0xFF,
827 (net_ip.s_addr >> 24) & 0xFF);
829 strncpy(tftp_filename, default_filename, DEFAULT_NAME_LEN);
830 tftp_filename[DEFAULT_NAME_LEN - 1] = 0;
832 printf("*** Warning: no boot file name; using '%s'\n",
836 if (IS_ENABLED(CONFIG_IPV6)) {
841 s = strchr(net_boot_file_name, '[');
842 e = strchr(net_boot_file_name, ']');
845 string_to_ip6(s + 1, len - 1, &tftp_remote_ip6);
846 strlcpy(tftp_filename, e + 2, MAX_LEN);
848 strlcpy(tftp_filename, net_boot_file_name, MAX_LEN);
849 tftp_filename[MAX_LEN - 1] = 0;
854 printf("Using %s device\n", eth_get_name());
856 if (IS_ENABLED(CONFIG_IPV6) && use_ip6) {
857 printf("TFTP from server %pI6c; our IP address is %pI6c",
858 &tftp_remote_ip6, &net_ip6);
860 if (tftp_block_size_option > TFTP_MTU_BLOCKSIZE6)
861 tftp_block_size_option = TFTP_MTU_BLOCKSIZE6;
863 printf("TFTP %s server %pI4; our IP address is %pI4",
864 #ifdef CONFIG_CMD_TFTPPUT
865 protocol == TFTPPUT ? "to" : "from",
869 &tftp_remote_ip, &net_ip);
872 /* Check if we need to send across this subnet */
873 if (IS_ENABLED(CONFIG_IPV6) && use_ip6) {
874 if (!ip6_addr_in_subnet(&net_ip6, &tftp_remote_ip6,
876 printf("; sending through gateway %pI6c",
878 } else if (net_gateway.s_addr && net_netmask.s_addr) {
879 struct in_addr our_net;
880 struct in_addr remote_net;
882 our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
883 remote_net.s_addr = tftp_remote_ip.s_addr & net_netmask.s_addr;
884 if (our_net.s_addr != remote_net.s_addr)
885 printf("; sending through gateway %pI4", &net_gateway);
889 printf("Filename '%s'.", tftp_filename);
891 if (net_boot_file_expected_size_in_blocks) {
892 printf(" Size is 0x%x Bytes = ",
893 net_boot_file_expected_size_in_blocks << 9);
894 print_size(net_boot_file_expected_size_in_blocks << 9, "");
898 #ifdef CONFIG_CMD_TFTPPUT
899 tftp_put_active = (protocol == TFTPPUT);
900 if (tftp_put_active) {
901 printf("Save address: 0x%lx\n", image_save_addr);
902 printf("Save size: 0x%lx\n", image_save_size);
903 net_boot_file_size = image_save_size;
905 tftp_state = STATE_SEND_WRQ;
910 if (tftp_init_load_addr()) {
912 net_set_state(NETLOOP_FAIL);
913 puts("\nTFTP error: ");
914 puts("trying to overwrite reserved memory...\n");
917 printf("Load address: 0x%lx\n", tftp_load_addr);
918 puts("Loading: *\b");
919 tftp_state = STATE_SEND_RRQ;
922 time_start = get_timer(0);
923 timeout_count_max = tftp_timeout_count_max;
925 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
926 net_set_udp_handler(tftp_handler);
927 #ifdef CONFIG_CMD_TFTPPUT
928 net_set_icmp_handler(icmp_handler);
930 tftp_remote_port = WELL_KNOWN_PORT;
932 /* Use a pseudo-random port unless a specific port is set */
933 tftp_our_port = 1024 + (get_timer(0) % 3072);
935 #ifdef CONFIG_TFTP_PORT
936 ep = env_get("tftpdstp");
938 tftp_remote_port = simple_strtol(ep, NULL, 10);
939 ep = env_get("tftpsrcp");
941 tftp_our_port = simple_strtol(ep, NULL, 10);
946 /* zero out server ether in case the server ip has changed */
947 memset(net_server_ethaddr, 0, 6);
948 /* Revert tftp_block_size to dflt */
949 tftp_block_size = TFTP_BLOCK_SIZE;
950 #ifdef CONFIG_TFTP_TSIZE
952 tftp_tsize_num_hash = 0;
958 #ifdef CONFIG_CMD_TFTPSRV
959 void tftp_start_server(void)
961 tftp_filename[0] = 0;
963 if (tftp_init_load_addr()) {
965 net_set_state(NETLOOP_FAIL);
966 puts("\nTFTP error: trying to overwrite reserved memory...\n");
969 printf("Using %s device\n", eth_get_name());
970 printf("Listening for TFTP transfer on %pI4\n", &net_ip);
971 printf("Load address: 0x%lx\n", tftp_load_addr);
973 puts("Loading: *\b");
975 timeout_count_max = tftp_timeout_count_max;
977 timeout_ms = TIMEOUT;
978 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
980 /* Revert tftp_block_size to dflt */
981 tftp_block_size = TFTP_BLOCK_SIZE;
983 tftp_our_port = WELL_KNOWN_PORT;
985 tftp_next_ack = tftp_windowsize;
987 #ifdef CONFIG_TFTP_TSIZE
989 tftp_tsize_num_hash = 0;
992 tftp_state = STATE_RECV_WRQ;
993 net_set_udp_handler(tftp_handler);
995 /* zero out server ether in case the server ip has changed */
996 memset(net_server_ethaddr, 0, 6);
998 #endif /* CONFIG_CMD_TFTPSRV */