2 * Copyright 1994, 1995, 2000 Neil Russell.
4 * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
5 * Copyright 2011 Comelit Group SpA,
6 * Luca Ceresoli <luca.ceresoli@comelit.it>
11 #include <efi_loader.h>
16 #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
20 DECLARE_GLOBAL_DATA_PTR
;
22 /* Well known TFTP port # */
23 #define WELL_KNOWN_PORT 69
24 /* Millisecs to timeout for lost pkt */
25 #define TIMEOUT 5000UL
26 #ifndef CONFIG_NET_RETRY_COUNT
27 /* # of timeouts before giving up */
28 # define TIMEOUT_COUNT 10
30 # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
32 /* Number of "loading" hashes per line (for checking the image size) */
33 #define HASHES_PER_LINE 65
45 static ulong timeout_ms
= TIMEOUT
;
46 static int timeout_count_max
= TIMEOUT_COUNT
;
47 static ulong time_start
; /* Record time we started tftp */
50 * These globals govern the timeout behavior when attempting a connection to a
51 * TFTP server. tftp_timeout_ms specifies the number of milliseconds to
52 * wait for the server to respond to initial connection. Second global,
53 * tftp_timeout_count_max, gives the number of such connection retries.
54 * tftp_timeout_count_max must be non-negative and tftp_timeout_ms must be
55 * positive. The globals are meant to be set (and restored) by code needing
56 * non-standard timeout behavior when initiating a TFTP transfer.
58 ulong tftp_timeout_ms
= TIMEOUT
;
59 int tftp_timeout_count_max
= TIMEOUT_COUNT
;
62 TFTP_ERR_UNDEFINED
= 0,
63 TFTP_ERR_FILE_NOT_FOUND
= 1,
64 TFTP_ERR_ACCESS_DENIED
= 2,
65 TFTP_ERR_DISK_FULL
= 3,
66 TFTP_ERR_UNEXPECTED_OPCODE
= 4,
67 TFTP_ERR_UNKNOWN_TRANSFER_ID
= 5,
68 TFTP_ERR_FILE_ALREADY_EXISTS
= 6,
71 static struct in_addr tftp_remote_ip
;
72 /* The UDP port at their end */
73 static int tftp_remote_port
;
74 /* The UDP port at our end */
75 static int tftp_our_port
;
76 static int timeout_count
;
77 /* packet sequence number */
78 static ulong tftp_cur_block
;
79 /* last packet sequence number received */
80 static ulong tftp_prev_block
;
81 /* count of sequence number wraparounds */
82 static ulong tftp_block_wrap
;
83 /* memory offset due to wrapping */
84 static ulong tftp_block_wrap_offset
;
85 static int tftp_state
;
86 static ulong tftp_load_addr
;
88 static ulong tftp_load_size
;
90 #ifdef CONFIG_TFTP_TSIZE
91 /* The file size reported by the server */
92 static int tftp_tsize
;
93 /* The number of hashes we printed */
94 static short tftp_tsize_num_hash
;
96 #ifdef CONFIG_CMD_TFTPPUT
97 /* 1 if writing, else 0 */
98 static int tftp_put_active
;
99 /* 1 if we have sent the last block */
100 static int tftp_put_final_block_sent
;
102 #define tftp_put_active 0
105 #define STATE_SEND_RRQ 1
107 #define STATE_TOO_LARGE 3
108 #define STATE_BAD_MAGIC 4
110 #define STATE_RECV_WRQ 6
111 #define STATE_SEND_WRQ 7
113 /* default TFTP block size */
114 #define TFTP_BLOCK_SIZE 512
115 /* sequence number is 16 bit */
116 #define TFTP_SEQUENCE_SIZE ((ulong)(1<<16))
118 #define DEFAULT_NAME_LEN (8 + 4 + 1)
119 static char default_filename
[DEFAULT_NAME_LEN
];
121 #ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
124 #define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
127 static char tftp_filename
[MAX_LEN
];
129 /* 512 is poor choice for ethernet, MTU is typically 1500.
130 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
131 * almost-MTU block sizes. At least try... fall back to 512 if need be.
132 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
134 #ifdef CONFIG_TFTP_BLOCKSIZE
135 #define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
137 #define TFTP_MTU_BLOCKSIZE 1468
140 static unsigned short tftp_block_size
= TFTP_BLOCK_SIZE
;
141 static unsigned short tftp_block_size_option
= TFTP_MTU_BLOCKSIZE
;
143 #ifdef CONFIG_MCAST_TFTP
145 #define MTFTP_BITMAPSIZE 0x1000
146 static unsigned *tftp_mcast_bitmap
;
147 static int tftp_mcast_prev_hole
;
148 static int tftp_mcast_bitmap_size
= MTFTP_BITMAPSIZE
;
149 static int tftp_mcast_disabled
;
150 static int tftp_mcast_master_client
;
151 static int tftp_mcast_active
;
152 static int tftp_mcast_port
;
153 /* can get 'last' block before done..*/
154 static ulong tftp_mcast_ending_block
;
156 static void parse_multicast_oack(char *pkt
, int len
);
158 static void mcast_cleanup(void)
161 eth_mcast_join(net_mcast_addr
, 0);
162 if (tftp_mcast_bitmap
)
163 free(tftp_mcast_bitmap
);
164 tftp_mcast_bitmap
= NULL
;
165 net_mcast_addr
.s_addr
= 0;
166 tftp_mcast_active
= 0;
168 tftp_mcast_ending_block
= -1;
171 #endif /* CONFIG_MCAST_TFTP */
173 static inline int store_block(int block
, uchar
*src
, unsigned int len
)
175 ulong offset
= block
* tftp_block_size
+ tftp_block_wrap_offset
;
176 ulong newsize
= offset
+ len
;
177 ulong store_addr
= tftp_load_addr
+ offset
;
178 #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
181 for (i
= 0; i
< CONFIG_SYS_MAX_FLASH_BANKS
; i
++) {
182 /* start address in flash? */
183 if (flash_info
[i
].flash_id
== FLASH_UNKNOWN
)
185 if (store_addr
>= flash_info
[i
].start
[0]) {
191 if (rc
) { /* Flash is destination for this packet */
192 rc
= flash_write((char *)src
, store_addr
, len
);
198 #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
203 if (store_addr
< tftp_load_addr
||
204 store_addr
+ len
> tftp_load_addr
+ tftp_load_size
) {
205 puts("\nTFTP error: ");
206 puts("trying to overwrite reserved memory...\n");
210 ptr
= map_sysmem(store_addr
, len
);
211 memcpy(ptr
, src
, len
);
214 #ifdef CONFIG_MCAST_TFTP
215 if (tftp_mcast_active
)
216 ext2_set_bit(block
, tftp_mcast_bitmap
);
219 if (net_boot_file_size
< newsize
)
220 net_boot_file_size
= newsize
;
225 /* Clear our state ready for a new transfer */
226 static void new_transfer(void)
230 tftp_block_wrap_offset
= 0;
231 #ifdef CONFIG_CMD_TFTPPUT
232 tftp_put_final_block_sent
= 0;
236 #ifdef CONFIG_CMD_TFTPPUT
238 * Load the next block from memory to be sent over tftp.
240 * @param block Block number to send
241 * @param dst Destination buffer for data
242 * @param len Number of bytes in block (this one and every other)
243 * @return number of bytes loaded
245 static int load_block(unsigned block
, uchar
*dst
, unsigned len
)
247 /* We may want to get the final block from the previous set */
248 ulong offset
= ((int)block
- 1) * len
+ tftp_block_wrap_offset
;
251 tosend
= min(net_boot_file_size
- offset
, tosend
);
252 (void)memcpy(dst
, (void *)(save_addr
+ offset
), tosend
);
253 debug("%s: block=%d, offset=%ld, len=%d, tosend=%ld\n", __func__
,
254 block
, offset
, len
, tosend
);
259 static void tftp_send(void);
260 static void tftp_timeout_handler(void);
262 /**********************************************************************/
264 static void show_block_marker(void)
266 #ifdef CONFIG_TFTP_TSIZE
268 ulong pos
= tftp_cur_block
* tftp_block_size
+
269 tftp_block_wrap_offset
;
270 if (pos
> tftp_tsize
)
273 while (tftp_tsize_num_hash
< pos
* 50 / tftp_tsize
) {
275 tftp_tsize_num_hash
++;
280 if (((tftp_cur_block
- 1) % 10) == 0)
282 else if ((tftp_cur_block
% (10 * HASHES_PER_LINE
)) == 0)
288 * restart the current transfer due to an error
290 * @param msg Message to print for user
292 static void restart(const char *msg
)
294 printf("\n%s; starting again\n", msg
);
295 #ifdef CONFIG_MCAST_TFTP
302 * Check if the block number has wrapped, and update progress
304 * TODO: The egregious use of global variables in this file should be tidied.
306 static void update_block_number(void)
309 * RFC1350 specifies that the first data packet will
310 * have sequence number 1. If we receive a sequence
311 * number of 0 this means that there was a wrap
312 * around of the (16 bit) counter.
314 if (tftp_cur_block
== 0 && tftp_prev_block
!= 0) {
316 tftp_block_wrap_offset
+= tftp_block_size
* TFTP_SEQUENCE_SIZE
;
317 timeout_count
= 0; /* we've done well, reset the timeout */
323 /* The TFTP get or put is complete */
324 static void tftp_complete(void)
326 #ifdef CONFIG_TFTP_TSIZE
327 /* Print hash marks for the last packet received */
328 while (tftp_tsize
&& tftp_tsize_num_hash
< 49) {
330 tftp_tsize_num_hash
++;
333 print_size(tftp_tsize
, "");
335 time_start
= get_timer(time_start
);
336 if (time_start
> 0) {
337 puts("\n\t "); /* Line up with "Loading: " */
338 print_size(net_boot_file_size
/
339 time_start
* 1000, "/s");
342 net_set_state(NETLOOP_SUCCESS
);
345 static void tftp_send(void)
352 #ifdef CONFIG_MCAST_TFTP
353 /* Multicast TFTP.. non-MasterClients do not ACK data. */
354 if (tftp_mcast_active
&& tftp_state
== STATE_DATA
&&
355 tftp_mcast_master_client
== 0)
359 * We will always be sending some sort of packet, so
360 * cobble together the packet headers now.
362 pkt
= net_tx_packet
+ net_eth_hdr_size() + IP_UDP_HDR_SIZE
;
364 switch (tftp_state
) {
369 #ifdef CONFIG_CMD_TFTPPUT
370 *s
++ = htons(tftp_state
== STATE_SEND_RRQ
? TFTP_RRQ
:
373 *s
++ = htons(TFTP_RRQ
);
376 strcpy((char *)pkt
, tftp_filename
);
377 pkt
+= strlen(tftp_filename
) + 1;
378 strcpy((char *)pkt
, "octet");
379 pkt
+= 5 /*strlen("octet")*/ + 1;
380 strcpy((char *)pkt
, "timeout");
381 pkt
+= 7 /*strlen("timeout")*/ + 1;
382 sprintf((char *)pkt
, "%lu", timeout_ms
/ 1000);
383 debug("send option \"timeout %s\"\n", (char *)pkt
);
384 pkt
+= strlen((char *)pkt
) + 1;
385 #ifdef CONFIG_TFTP_TSIZE
386 pkt
+= sprintf((char *)pkt
, "tsize%c%u%c",
387 0, net_boot_file_size
, 0);
389 /* try for more effic. blk size */
390 pkt
+= sprintf((char *)pkt
, "blksize%c%d%c",
391 0, tftp_block_size_option
, 0);
392 #ifdef CONFIG_MCAST_TFTP
393 /* Check all preconditions before even trying the option */
394 if (!tftp_mcast_disabled
) {
395 tftp_mcast_bitmap
= malloc(tftp_mcast_bitmap_size
);
396 if (tftp_mcast_bitmap
&& eth_get_dev()->mcast
) {
397 free(tftp_mcast_bitmap
);
398 tftp_mcast_bitmap
= NULL
;
399 pkt
+= sprintf((char *)pkt
, "multicast%c%c",
403 #endif /* CONFIG_MCAST_TFTP */
408 #ifdef CONFIG_MCAST_TFTP
409 /* My turn! Start at where I need blocks I missed. */
410 if (tftp_mcast_active
)
411 tftp_cur_block
= ext2_find_next_zero_bit(
413 tftp_mcast_bitmap_size
* 8, 0);
421 s
[0] = htons(TFTP_ACK
);
422 s
[1] = htons(tftp_cur_block
);
423 pkt
= (uchar
*)(s
+ 2);
424 #ifdef CONFIG_CMD_TFTPPUT
425 if (tftp_put_active
) {
426 int toload
= tftp_block_size
;
427 int loaded
= load_block(tftp_cur_block
, pkt
, toload
);
429 s
[0] = htons(TFTP_DATA
);
431 tftp_put_final_block_sent
= (loaded
< toload
);
437 case STATE_TOO_LARGE
:
440 *s
++ = htons(TFTP_ERROR
);
444 strcpy((char *)pkt
, "File too large");
445 pkt
+= 14 /*strlen("File too large")*/ + 1;
449 case STATE_BAD_MAGIC
:
452 *s
++ = htons(TFTP_ERROR
);
455 strcpy((char *)pkt
, "File has bad magic");
456 pkt
+= 18 /*strlen("File has bad magic")*/ + 1;
461 net_send_udp_packet(net_server_ethaddr
, tftp_remote_ip
,
462 tftp_remote_port
, tftp_our_port
, len
);
465 #ifdef CONFIG_CMD_TFTPPUT
466 static void icmp_handler(unsigned type
, unsigned code
, unsigned dest
,
467 struct in_addr sip
, unsigned src
, uchar
*pkt
,
470 if (type
== ICMP_NOT_REACH
&& code
== ICMP_NOT_REACH_PORT
) {
471 /* Oh dear the other end has gone away */
472 restart("TFTP server died");
477 static void tftp_handler(uchar
*pkt
, unsigned dest
, struct in_addr sip
,
478 unsigned src
, unsigned len
)
484 if (dest
!= tftp_our_port
) {
485 #ifdef CONFIG_MCAST_TFTP
486 if (tftp_mcast_active
&&
487 (!tftp_mcast_port
|| dest
!= tftp_mcast_port
))
491 if (tftp_state
!= STATE_SEND_RRQ
&& src
!= tftp_remote_port
&&
492 tftp_state
!= STATE_RECV_WRQ
&& tftp_state
!= STATE_SEND_WRQ
)
498 /* warning: don't use increment (++) in ntohs() macros!! */
502 switch (ntohs(proto
)) {
507 #ifdef CONFIG_CMD_TFTPPUT
508 if (tftp_put_active
) {
509 if (tftp_put_final_block_sent
) {
513 * Move to the next block. We want our block
514 * count to wrap just like the other end!
516 int block
= ntohs(*s
);
517 int ack_ok
= (tftp_cur_block
== block
);
519 tftp_cur_block
= (unsigned short)(block
+ 1);
520 update_block_number();
522 tftp_send(); /* Send next data block */
531 #ifdef CONFIG_CMD_TFTPSRV
534 tftp_remote_ip
= sip
;
535 tftp_remote_port
= src
;
536 tftp_our_port
= 1024 + (get_timer(0) % 3072);
538 tftp_send(); /* Send ACK(0) */
543 debug("Got OACK: %s %s\n",
544 pkt
, pkt
+ strlen((char *)pkt
) + 1);
545 tftp_state
= STATE_OACK
;
546 tftp_remote_port
= src
;
548 * Check for 'blksize' option.
549 * Careful: "i" is signed, "len" is unsigned, thus
550 * something like "len-8" may give a *huge* number
552 for (i
= 0; i
+8 < len
; i
++) {
553 if (strcmp((char *)pkt
+ i
, "blksize") == 0) {
554 tftp_block_size
= (unsigned short)
555 simple_strtoul((char *)pkt
+ i
+ 8,
557 debug("Blocksize ack: %s, %d\n",
558 (char *)pkt
+ i
+ 8, tftp_block_size
);
560 #ifdef CONFIG_TFTP_TSIZE
561 if (strcmp((char *)pkt
+i
, "tsize") == 0) {
562 tftp_tsize
= simple_strtoul((char *)pkt
+ i
+ 6,
564 debug("size = %s, %d\n",
565 (char *)pkt
+ i
+ 6, tftp_tsize
);
569 #ifdef CONFIG_MCAST_TFTP
570 parse_multicast_oack((char *)pkt
, len
- 1);
571 if ((tftp_mcast_active
) && (!tftp_mcast_master_client
))
572 tftp_state
= STATE_DATA
; /* passive.. */
575 #ifdef CONFIG_CMD_TFTPPUT
576 if (tftp_put_active
) {
577 /* Get ready to send the first block */
578 tftp_state
= STATE_DATA
;
582 tftp_send(); /* Send ACK or first data block */
588 tftp_cur_block
= ntohs(*(__be16
*)pkt
);
590 update_block_number();
592 if (tftp_state
== STATE_SEND_RRQ
)
593 debug("Server did not acknowledge timeout option!\n");
595 if (tftp_state
== STATE_SEND_RRQ
|| tftp_state
== STATE_OACK
||
596 tftp_state
== STATE_RECV_WRQ
) {
597 /* first block received */
598 tftp_state
= STATE_DATA
;
599 tftp_remote_port
= src
;
602 #ifdef CONFIG_MCAST_TFTP
603 if (tftp_mcast_active
) { /* start!=1 common if mcast */
604 tftp_prev_block
= tftp_cur_block
- 1;
607 if (tftp_cur_block
!= 1) { /* Assertion */
608 puts("\nTFTP error: ");
609 printf("First block is not block 1 (%ld)\n",
611 puts("Starting again\n\n");
617 if (tftp_cur_block
== tftp_prev_block
) {
618 /* Same block again; ignore it. */
622 tftp_prev_block
= tftp_cur_block
;
623 timeout_count_max
= tftp_timeout_count_max
;
624 net_set_timeout_handler(timeout_ms
, tftp_timeout_handler
);
626 if (store_block(tftp_cur_block
- 1, pkt
+ 2, len
)) {
628 net_set_state(NETLOOP_FAIL
);
633 * Acknowledge the block just received, which will prompt
634 * the remote for the next one.
636 #ifdef CONFIG_MCAST_TFTP
637 /* if I am the MasterClient, actively calculate what my next
638 * needed block is; else I'm passive; not ACKING
640 if (tftp_mcast_active
) {
641 if (len
< tftp_block_size
) {
642 tftp_mcast_ending_block
= tftp_cur_block
;
643 } else if (tftp_mcast_master_client
) {
644 tftp_mcast_prev_hole
= ext2_find_next_zero_bit(
646 tftp_mcast_bitmap_size
* 8,
647 tftp_mcast_prev_hole
);
648 tftp_cur_block
= tftp_mcast_prev_hole
;
650 ((tftp_mcast_bitmap_size
* 8) - 1)) {
651 debug("tftpfile too big\n");
652 /* try to double it and retry */
653 tftp_mcast_bitmap_size
<<= 1;
658 tftp_prev_block
= tftp_cur_block
;
664 #ifdef CONFIG_MCAST_TFTP
665 if (tftp_mcast_active
) {
666 if (tftp_mcast_master_client
&&
667 (tftp_cur_block
>= tftp_mcast_ending_block
)) {
668 puts("\nMulticast tftp done\n");
670 net_set_state(NETLOOP_SUCCESS
);
674 if (len
< tftp_block_size
)
679 printf("\nTFTP error: '%s' (%d)\n",
680 pkt
+ 2, ntohs(*(__be16
*)pkt
));
682 switch (ntohs(*(__be16
*)pkt
)) {
683 case TFTP_ERR_FILE_NOT_FOUND
:
684 case TFTP_ERR_ACCESS_DENIED
:
685 puts("Not retrying...\n");
687 net_set_state(NETLOOP_FAIL
);
689 case TFTP_ERR_UNDEFINED
:
690 case TFTP_ERR_DISK_FULL
:
691 case TFTP_ERR_UNEXPECTED_OPCODE
:
692 case TFTP_ERR_UNKNOWN_TRANSFER_ID
:
693 case TFTP_ERR_FILE_ALREADY_EXISTS
:
695 puts("Starting again\n\n");
696 #ifdef CONFIG_MCAST_TFTP
707 static void tftp_timeout_handler(void)
709 if (++timeout_count
> timeout_count_max
) {
710 restart("Retry count exceeded");
713 net_set_timeout_handler(timeout_ms
, tftp_timeout_handler
);
714 if (tftp_state
!= STATE_RECV_WRQ
)
719 /* Initialize tftp_load_addr and tftp_load_size from load_addr and lmb */
720 static int tftp_init_load_addr(void)
724 phys_size_t max_size
;
726 lmb_init_and_reserve(&lmb
, gd
->bd
->bi_dram
[0].start
,
727 gd
->bd
->bi_dram
[0].size
, (void *)gd
->fdt_blob
);
729 max_size
= lmb_get_unreserved_size(&lmb
, load_addr
);
733 tftp_load_size
= max_size
;
735 tftp_load_addr
= load_addr
;
739 void tftp_start(enum proto_t protocol
)
741 #if CONFIG_NET_TFTP_VARS
742 char *ep
; /* Environment pointer */
745 * Allow the user to choose TFTP blocksize and timeout.
746 * TFTP protocol has a minimal timeout of 1 second.
749 ep
= env_get("tftpblocksize");
751 tftp_block_size_option
= simple_strtol(ep
, NULL
, 10);
753 ep
= env_get("tftptimeout");
755 timeout_ms
= simple_strtol(ep
, NULL
, 10);
757 if (timeout_ms
< 1000) {
758 printf("TFTP timeout (%ld ms) too low, set min = 1000 ms\n",
763 ep
= env_get("tftptimeoutcountmax");
765 tftp_timeout_count_max
= simple_strtol(ep
, NULL
, 10);
767 if (tftp_timeout_count_max
< 0) {
768 printf("TFTP timeout count max (%d ms) negative, set to 0\n",
769 tftp_timeout_count_max
);
770 tftp_timeout_count_max
= 0;
774 debug("TFTP blocksize = %i, timeout = %ld ms\n",
775 tftp_block_size_option
, timeout_ms
);
777 tftp_remote_ip
= net_server_ip
;
778 if (!net_parse_bootfile(&tftp_remote_ip
, tftp_filename
, MAX_LEN
)) {
779 sprintf(default_filename
, "%02X%02X%02X%02X.img",
780 net_ip
.s_addr
& 0xFF,
781 (net_ip
.s_addr
>> 8) & 0xFF,
782 (net_ip
.s_addr
>> 16) & 0xFF,
783 (net_ip
.s_addr
>> 24) & 0xFF);
785 strncpy(tftp_filename
, default_filename
, DEFAULT_NAME_LEN
);
786 tftp_filename
[DEFAULT_NAME_LEN
- 1] = 0;
788 printf("*** Warning: no boot file name; using '%s'\n",
792 printf("Using %s device\n", eth_get_name());
793 printf("TFTP %s server %pI4; our IP address is %pI4",
794 #ifdef CONFIG_CMD_TFTPPUT
795 protocol
== TFTPPUT
? "to" : "from",
799 &tftp_remote_ip
, &net_ip
);
801 /* Check if we need to send across this subnet */
802 if (net_gateway
.s_addr
&& net_netmask
.s_addr
) {
803 struct in_addr our_net
;
804 struct in_addr remote_net
;
806 our_net
.s_addr
= net_ip
.s_addr
& net_netmask
.s_addr
;
807 remote_net
.s_addr
= tftp_remote_ip
.s_addr
& net_netmask
.s_addr
;
808 if (our_net
.s_addr
!= remote_net
.s_addr
)
809 printf("; sending through gateway %pI4", &net_gateway
);
813 printf("Filename '%s'.", tftp_filename
);
815 if (net_boot_file_expected_size_in_blocks
) {
816 printf(" Size is 0x%x Bytes = ",
817 net_boot_file_expected_size_in_blocks
<< 9);
818 print_size(net_boot_file_expected_size_in_blocks
<< 9, "");
822 #ifdef CONFIG_CMD_TFTPPUT
823 tftp_put_active
= (protocol
== TFTPPUT
);
824 if (tftp_put_active
) {
825 printf("Save address: 0x%lx\n", save_addr
);
826 printf("Save size: 0x%lx\n", save_size
);
827 net_boot_file_size
= save_size
;
829 tftp_state
= STATE_SEND_WRQ
;
834 if (tftp_init_load_addr()) {
836 net_set_state(NETLOOP_FAIL
);
837 puts("\nTFTP error: ");
838 puts("trying to overwrite reserved memory...\n");
841 printf("Load address: 0x%lx\n", tftp_load_addr
);
842 puts("Loading: *\b");
843 tftp_state
= STATE_SEND_RRQ
;
844 #ifdef CONFIG_CMD_BOOTEFI
845 efi_set_bootdev("Net", "", tftp_filename
);
849 time_start
= get_timer(0);
850 timeout_count_max
= tftp_timeout_count_max
;
852 net_set_timeout_handler(timeout_ms
, tftp_timeout_handler
);
853 net_set_udp_handler(tftp_handler
);
854 #ifdef CONFIG_CMD_TFTPPUT
855 net_set_icmp_handler(icmp_handler
);
857 tftp_remote_port
= WELL_KNOWN_PORT
;
859 /* Use a pseudo-random port unless a specific port is set */
860 tftp_our_port
= 1024 + (get_timer(0) % 3072);
862 #ifdef CONFIG_TFTP_PORT
863 ep
= env_get("tftpdstp");
865 tftp_remote_port
= simple_strtol(ep
, NULL
, 10);
866 ep
= env_get("tftpsrcp");
868 tftp_our_port
= simple_strtol(ep
, NULL
, 10);
872 /* zero out server ether in case the server ip has changed */
873 memset(net_server_ethaddr
, 0, 6);
874 /* Revert tftp_block_size to dflt */
875 tftp_block_size
= TFTP_BLOCK_SIZE
;
876 #ifdef CONFIG_MCAST_TFTP
879 #ifdef CONFIG_TFTP_TSIZE
881 tftp_tsize_num_hash
= 0;
887 #ifdef CONFIG_CMD_TFTPSRV
888 void tftp_start_server(void)
890 tftp_filename
[0] = 0;
892 if (tftp_init_load_addr()) {
894 net_set_state(NETLOOP_FAIL
);
895 puts("\nTFTP error: trying to overwrite reserved memory...\n");
898 printf("Using %s device\n", eth_get_name());
899 printf("Listening for TFTP transfer on %pI4\n", &net_ip
);
900 printf("Load address: 0x%lx\n", tftp_load_addr
);
902 puts("Loading: *\b");
904 timeout_count_max
= tftp_timeout_count_max
;
906 timeout_ms
= TIMEOUT
;
907 net_set_timeout_handler(timeout_ms
, tftp_timeout_handler
);
909 /* Revert tftp_block_size to dflt */
910 tftp_block_size
= TFTP_BLOCK_SIZE
;
912 tftp_our_port
= WELL_KNOWN_PORT
;
914 #ifdef CONFIG_TFTP_TSIZE
916 tftp_tsize_num_hash
= 0;
919 tftp_state
= STATE_RECV_WRQ
;
920 net_set_udp_handler(tftp_handler
);
922 /* zero out server ether in case the server ip has changed */
923 memset(net_server_ethaddr
, 0, 6);
925 #endif /* CONFIG_CMD_TFTPSRV */
927 #ifdef CONFIG_MCAST_TFTP
929 * Credits: atftp project.
933 * Pick up BcastAddr, Port, and whether I am [now] the master-client.
935 * +-------+-----------+---+-------~~-------+---+
936 * | opc | multicast | 0 | addr, port, mc | 0 |
937 * +-------+-----------+---+-------~~-------+---+
938 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
939 * I am the new master-client so must send ACKs to DataBlocks. If I am not
940 * master-client, I'm a passive client, gathering what DataBlocks I may and
941 * making note of which ones I got in my bitmask.
942 * In theory, I never go from master->passive..
943 * .. this comes in with pkt already pointing just past opc
945 static void parse_multicast_oack(char *pkt
, int len
)
956 /* march along looking for 'multicast\0', which has to start at least
957 * 14 bytes back from the end.
959 for (i
= 0; i
< len
- 14; i
++)
960 if (strcmp(pkt
+ i
, "multicast") == 0)
962 if (i
>= (len
- 14)) /* non-Multicast OACK, ign. */
965 i
+= 10; /* strlen multicast */
967 for (; i
< len
; i
++) {
968 if (*(pkt
+ i
) == ',') {
978 if (!port
|| !mc_adr
|| !mc
)
980 if (tftp_mcast_active
&& tftp_mcast_master_client
) {
981 printf("I got a OACK as master Client, WRONG!\n");
984 /* ..I now accept packets destined for this MCAST addr, port */
985 if (!tftp_mcast_active
) {
986 if (tftp_mcast_bitmap
) {
987 printf("Internal failure! no mcast.\n");
988 free(tftp_mcast_bitmap
);
989 tftp_mcast_bitmap
= NULL
;
990 tftp_mcast_disabled
= 1;
993 /* I malloc instead of pre-declare; so that if the file ends
994 * up being too big for this bitmap I can retry
996 tftp_mcast_bitmap
= malloc(tftp_mcast_bitmap_size
);
997 if (!tftp_mcast_bitmap
) {
998 printf("No bitmap, no multicast. Sorry.\n");
999 tftp_mcast_disabled
= 1;
1002 memset(tftp_mcast_bitmap
, 0, tftp_mcast_bitmap_size
);
1003 tftp_mcast_prev_hole
= 0;
1004 tftp_mcast_active
= 1;
1006 addr
= string_to_ip(mc_adr
);
1007 if (net_mcast_addr
.s_addr
!= addr
.s_addr
) {
1008 if (net_mcast_addr
.s_addr
)
1009 eth_mcast_join(net_mcast_addr
, 0);
1010 net_mcast_addr
= addr
;
1011 if (eth_mcast_join(net_mcast_addr
, 1)) {
1012 printf("Fail to set mcast, revert to TFTP\n");
1013 tftp_mcast_disabled
= 1;
1018 tftp_mcast_master_client
= simple_strtoul((char *)mc
, NULL
, 10);
1019 tftp_mcast_port
= (unsigned short)simple_strtoul(port
, NULL
, 10);
1020 printf("Multicast: %s:%d [%d]\n", mc_adr
, tftp_mcast_port
,
1021 tftp_mcast_master_client
);
1025 #endif /* Multicast TFTP */