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>
15 #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
19 /* Well known TFTP port # */
20 #define WELL_KNOWN_PORT 69
21 /* Millisecs to timeout for lost pkt */
22 #define TIMEOUT 5000UL
23 #ifndef CONFIG_NET_RETRY_COUNT
24 /* # of timeouts before giving up */
25 # define TIMEOUT_COUNT 10
27 # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
29 /* Number of "loading" hashes per line (for checking the image size) */
30 #define HASHES_PER_LINE 65
42 static ulong timeout_ms = TIMEOUT;
43 static int timeout_count_max = TIMEOUT_COUNT;
44 static ulong time_start; /* Record time we started tftp */
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 = TIMEOUT_COUNT;
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,
68 static struct in_addr tftp_remote_ip;
69 /* The UDP port at their end */
70 static int tftp_remote_port;
71 /* The UDP port at our end */
72 static int tftp_our_port;
73 static int timeout_count;
74 /* packet sequence number */
75 static ulong tftp_cur_block;
76 /* last packet sequence number received */
77 static ulong tftp_prev_block;
78 /* count of sequence number wraparounds */
79 static ulong tftp_block_wrap;
80 /* memory offset due to wrapping */
81 static ulong tftp_block_wrap_offset;
82 static int tftp_state;
83 #ifdef CONFIG_TFTP_TSIZE
84 /* The file size reported by the server */
85 static int tftp_tsize;
86 /* The number of hashes we printed */
87 static short tftp_tsize_num_hash;
89 #ifdef CONFIG_CMD_TFTPPUT
90 /* 1 if writing, else 0 */
91 static int tftp_put_active;
92 /* 1 if we have sent the last block */
93 static int tftp_put_final_block_sent;
95 #define tftp_put_active 0
98 #define STATE_SEND_RRQ 1
100 #define STATE_TOO_LARGE 3
101 #define STATE_BAD_MAGIC 4
103 #define STATE_RECV_WRQ 6
104 #define STATE_SEND_WRQ 7
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))
111 #define DEFAULT_NAME_LEN (8 + 4 + 1)
112 static char default_filename[DEFAULT_NAME_LEN];
114 #ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
117 #define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
120 static char tftp_filename[MAX_LEN];
122 /* 512 is poor choice for ethernet, MTU is typically 1500.
123 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
124 * almost-MTU block sizes. At least try... fall back to 512 if need be.
125 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
127 #ifdef CONFIG_TFTP_BLOCKSIZE
128 #define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
130 #define TFTP_MTU_BLOCKSIZE 1468
133 static unsigned short tftp_block_size = TFTP_BLOCK_SIZE;
134 static unsigned short tftp_block_size_option = TFTP_MTU_BLOCKSIZE;
136 #ifdef CONFIG_MCAST_TFTP
138 #define MTFTP_BITMAPSIZE 0x1000
139 static unsigned *tftp_mcast_bitmap;
140 static int tftp_mcast_prev_hole;
141 static int tftp_mcast_bitmap_size = MTFTP_BITMAPSIZE;
142 static int tftp_mcast_disabled;
143 static int tftp_mcast_master_client;
144 static int tftp_mcast_active;
145 static int tftp_mcast_port;
146 /* can get 'last' block before done..*/
147 static ulong tftp_mcast_ending_block;
149 static void parse_multicast_oack(char *pkt, int len);
151 static void mcast_cleanup(void)
154 eth_mcast_join(net_mcast_addr, 0);
155 if (tftp_mcast_bitmap)
156 free(tftp_mcast_bitmap);
157 tftp_mcast_bitmap = NULL;
158 net_mcast_addr.s_addr = 0;
159 tftp_mcast_active = 0;
161 tftp_mcast_ending_block = -1;
164 #endif /* CONFIG_MCAST_TFTP */
166 static inline void store_block(int block, uchar *src, unsigned len)
168 ulong offset = block * tftp_block_size + tftp_block_wrap_offset;
169 ulong newsize = offset + len;
170 #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
173 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
174 /* start address in flash? */
175 if (flash_info[i].flash_id == FLASH_UNKNOWN)
177 if (load_addr + offset >= flash_info[i].start[0]) {
183 if (rc) { /* Flash is destination for this packet */
184 rc = flash_write((char *)src, (ulong)(load_addr+offset), len);
187 net_set_state(NETLOOP_FAIL);
191 #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
193 void *ptr = map_sysmem(load_addr + offset, len);
195 memcpy(ptr, src, len);
198 #ifdef CONFIG_MCAST_TFTP
199 if (tftp_mcast_active)
200 ext2_set_bit(block, tftp_mcast_bitmap);
203 if (net_boot_file_size < newsize)
204 net_boot_file_size = newsize;
207 /* Clear our state ready for a new transfer */
208 static void new_transfer(void)
212 tftp_block_wrap_offset = 0;
213 #ifdef CONFIG_CMD_TFTPPUT
214 tftp_put_final_block_sent = 0;
218 #ifdef CONFIG_CMD_TFTPPUT
220 * Load the next block from memory to be sent over tftp.
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
227 static int load_block(unsigned block, uchar *dst, unsigned len)
229 /* We may want to get the final block from the previous set */
230 ulong offset = ((int)block - 1) * len + tftp_block_wrap_offset;
233 tosend = min(net_boot_file_size - offset, tosend);
234 (void)memcpy(dst, (void *)(save_addr + offset), tosend);
235 debug("%s: block=%d, offset=%ld, len=%d, tosend=%ld\n", __func__,
236 block, offset, len, tosend);
241 static void tftp_send(void);
242 static void tftp_timeout_handler(void);
244 /**********************************************************************/
246 static void show_block_marker(void)
248 #ifdef CONFIG_TFTP_TSIZE
250 ulong pos = tftp_cur_block * tftp_block_size +
251 tftp_block_wrap_offset;
253 while (tftp_tsize_num_hash < pos * 50 / tftp_tsize) {
255 tftp_tsize_num_hash++;
260 if (((tftp_cur_block - 1) % 10) == 0)
262 else if ((tftp_cur_block % (10 * HASHES_PER_LINE)) == 0)
268 * restart the current transfer due to an error
270 * @param msg Message to print for user
272 static void restart(const char *msg)
274 printf("\n%s; starting again\n", msg);
275 #ifdef CONFIG_MCAST_TFTP
282 * Check if the block number has wrapped, and update progress
284 * TODO: The egregious use of global variables in this file should be tidied.
286 static void update_block_number(void)
289 * RFC1350 specifies that the first data packet will
290 * have sequence number 1. If we receive a sequence
291 * number of 0 this means that there was a wrap
292 * around of the (16 bit) counter.
294 if (tftp_cur_block == 0 && tftp_prev_block != 0) {
296 tftp_block_wrap_offset += tftp_block_size * TFTP_SEQUENCE_SIZE;
297 timeout_count = 0; /* we've done well, reset the timeout */
303 /* The TFTP get or put is complete */
304 static void tftp_complete(void)
306 #ifdef CONFIG_TFTP_TSIZE
307 /* Print hash marks for the last packet received */
308 while (tftp_tsize && tftp_tsize_num_hash < 49) {
310 tftp_tsize_num_hash++;
313 print_size(tftp_tsize, "");
315 time_start = get_timer(time_start);
316 if (time_start > 0) {
317 puts("\n\t "); /* Line up with "Loading: " */
318 print_size(net_boot_file_size /
319 time_start * 1000, "/s");
322 net_set_state(NETLOOP_SUCCESS);
325 static void tftp_send(void)
332 #ifdef CONFIG_MCAST_TFTP
333 /* Multicast TFTP.. non-MasterClients do not ACK data. */
334 if (tftp_mcast_active && tftp_state == STATE_DATA &&
335 tftp_mcast_master_client == 0)
339 * We will always be sending some sort of packet, so
340 * cobble together the packet headers now.
342 pkt = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
344 switch (tftp_state) {
349 #ifdef CONFIG_CMD_TFTPPUT
350 *s++ = htons(tftp_state == STATE_SEND_RRQ ? TFTP_RRQ :
353 *s++ = htons(TFTP_RRQ);
356 strcpy((char *)pkt, tftp_filename);
357 pkt += strlen(tftp_filename) + 1;
358 strcpy((char *)pkt, "octet");
359 pkt += 5 /*strlen("octet")*/ + 1;
360 strcpy((char *)pkt, "timeout");
361 pkt += 7 /*strlen("timeout")*/ + 1;
362 sprintf((char *)pkt, "%lu", timeout_ms / 1000);
363 debug("send option \"timeout %s\"\n", (char *)pkt);
364 pkt += strlen((char *)pkt) + 1;
365 #ifdef CONFIG_TFTP_TSIZE
366 pkt += sprintf((char *)pkt, "tsize%c%u%c",
367 0, net_boot_file_size, 0);
369 /* try for more effic. blk size */
370 pkt += sprintf((char *)pkt, "blksize%c%d%c",
371 0, tftp_block_size_option, 0);
372 #ifdef CONFIG_MCAST_TFTP
373 /* Check all preconditions before even trying the option */
374 if (!tftp_mcast_disabled) {
375 tftp_mcast_bitmap = malloc(tftp_mcast_bitmap_size);
376 if (tftp_mcast_bitmap && eth_get_dev()->mcast) {
377 free(tftp_mcast_bitmap);
378 tftp_mcast_bitmap = NULL;
379 pkt += sprintf((char *)pkt, "multicast%c%c",
383 #endif /* CONFIG_MCAST_TFTP */
388 #ifdef CONFIG_MCAST_TFTP
389 /* My turn! Start at where I need blocks I missed. */
390 if (tftp_mcast_active)
391 tftp_cur_block = ext2_find_next_zero_bit(
393 tftp_mcast_bitmap_size * 8, 0);
401 s[0] = htons(TFTP_ACK);
402 s[1] = htons(tftp_cur_block);
403 pkt = (uchar *)(s + 2);
404 #ifdef CONFIG_CMD_TFTPPUT
405 if (tftp_put_active) {
406 int toload = tftp_block_size;
407 int loaded = load_block(tftp_cur_block, pkt, toload);
409 s[0] = htons(TFTP_DATA);
411 tftp_put_final_block_sent = (loaded < toload);
417 case STATE_TOO_LARGE:
420 *s++ = htons(TFTP_ERROR);
424 strcpy((char *)pkt, "File too large");
425 pkt += 14 /*strlen("File too large")*/ + 1;
429 case STATE_BAD_MAGIC:
432 *s++ = htons(TFTP_ERROR);
435 strcpy((char *)pkt, "File has bad magic");
436 pkt += 18 /*strlen("File has bad magic")*/ + 1;
441 net_send_udp_packet(net_server_ethaddr, tftp_remote_ip,
442 tftp_remote_port, tftp_our_port, len);
445 #ifdef CONFIG_CMD_TFTPPUT
446 static void icmp_handler(unsigned type, unsigned code, unsigned dest,
447 struct in_addr sip, unsigned src, uchar *pkt,
450 if (type == ICMP_NOT_REACH && code == ICMP_NOT_REACH_PORT) {
451 /* Oh dear the other end has gone away */
452 restart("TFTP server died");
457 static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
458 unsigned src, unsigned len)
464 if (dest != tftp_our_port) {
465 #ifdef CONFIG_MCAST_TFTP
466 if (tftp_mcast_active &&
467 (!tftp_mcast_port || dest != tftp_mcast_port))
471 if (tftp_state != STATE_SEND_RRQ && src != tftp_remote_port &&
472 tftp_state != STATE_RECV_WRQ && tftp_state != STATE_SEND_WRQ)
478 /* warning: don't use increment (++) in ntohs() macros!! */
482 switch (ntohs(proto)) {
487 #ifdef CONFIG_CMD_TFTPPUT
488 if (tftp_put_active) {
489 if (tftp_put_final_block_sent) {
493 * Move to the next block. We want our block
494 * count to wrap just like the other end!
496 int block = ntohs(*s);
497 int ack_ok = (tftp_cur_block == block);
499 tftp_cur_block = (unsigned short)(block + 1);
500 update_block_number();
502 tftp_send(); /* Send next data block */
511 #ifdef CONFIG_CMD_TFTPSRV
514 tftp_remote_ip = sip;
515 tftp_remote_port = src;
516 tftp_our_port = 1024 + (get_timer(0) % 3072);
518 tftp_send(); /* Send ACK(0) */
523 debug("Got OACK: %s %s\n",
524 pkt, pkt + strlen((char *)pkt) + 1);
525 tftp_state = STATE_OACK;
526 tftp_remote_port = src;
528 * Check for 'blksize' option.
529 * Careful: "i" is signed, "len" is unsigned, thus
530 * something like "len-8" may give a *huge* number
532 for (i = 0; i+8 < len; i++) {
533 if (strcmp((char *)pkt + i, "blksize") == 0) {
534 tftp_block_size = (unsigned short)
535 simple_strtoul((char *)pkt + i + 8,
537 debug("Blocksize ack: %s, %d\n",
538 (char *)pkt + i + 8, tftp_block_size);
540 #ifdef CONFIG_TFTP_TSIZE
541 if (strcmp((char *)pkt+i, "tsize") == 0) {
542 tftp_tsize = simple_strtoul((char *)pkt + i + 6,
544 debug("size = %s, %d\n",
545 (char *)pkt + i + 6, tftp_tsize);
549 #ifdef CONFIG_MCAST_TFTP
550 parse_multicast_oack((char *)pkt, len - 1);
551 if ((tftp_mcast_active) && (!tftp_mcast_master_client))
552 tftp_state = STATE_DATA; /* passive.. */
555 #ifdef CONFIG_CMD_TFTPPUT
556 if (tftp_put_active) {
557 /* Get ready to send the first block */
558 tftp_state = STATE_DATA;
562 tftp_send(); /* Send ACK or first data block */
568 tftp_cur_block = ntohs(*(__be16 *)pkt);
570 update_block_number();
572 if (tftp_state == STATE_SEND_RRQ)
573 debug("Server did not acknowledge timeout option!\n");
575 if (tftp_state == STATE_SEND_RRQ || tftp_state == STATE_OACK ||
576 tftp_state == STATE_RECV_WRQ) {
577 /* first block received */
578 tftp_state = STATE_DATA;
579 tftp_remote_port = src;
582 #ifdef CONFIG_MCAST_TFTP
583 if (tftp_mcast_active) { /* start!=1 common if mcast */
584 tftp_prev_block = tftp_cur_block - 1;
587 if (tftp_cur_block != 1) { /* Assertion */
588 puts("\nTFTP error: ");
589 printf("First block is not block 1 (%ld)\n",
591 puts("Starting again\n\n");
597 if (tftp_cur_block == tftp_prev_block) {
598 /* Same block again; ignore it. */
602 tftp_prev_block = tftp_cur_block;
603 timeout_count_max = TIMEOUT_COUNT;
604 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
606 store_block(tftp_cur_block - 1, pkt + 2, len);
609 * Acknowledge the block just received, which will prompt
610 * the remote for the next one.
612 #ifdef CONFIG_MCAST_TFTP
613 /* if I am the MasterClient, actively calculate what my next
614 * needed block is; else I'm passive; not ACKING
616 if (tftp_mcast_active) {
617 if (len < tftp_block_size) {
618 tftp_mcast_ending_block = tftp_cur_block;
619 } else if (tftp_mcast_master_client) {
620 tftp_mcast_prev_hole = ext2_find_next_zero_bit(
622 tftp_mcast_bitmap_size * 8,
623 tftp_mcast_prev_hole);
624 tftp_cur_block = tftp_mcast_prev_hole;
626 ((tftp_mcast_bitmap_size * 8) - 1)) {
627 debug("tftpfile too big\n");
628 /* try to double it and retry */
629 tftp_mcast_bitmap_size <<= 1;
634 tftp_prev_block = tftp_cur_block;
640 #ifdef CONFIG_MCAST_TFTP
641 if (tftp_mcast_active) {
642 if (tftp_mcast_master_client &&
643 (tftp_cur_block >= tftp_mcast_ending_block)) {
644 puts("\nMulticast tftp done\n");
646 net_set_state(NETLOOP_SUCCESS);
650 if (len < tftp_block_size)
655 printf("\nTFTP error: '%s' (%d)\n",
656 pkt + 2, ntohs(*(__be16 *)pkt));
658 switch (ntohs(*(__be16 *)pkt)) {
659 case TFTP_ERR_FILE_NOT_FOUND:
660 case TFTP_ERR_ACCESS_DENIED:
661 puts("Not retrying...\n");
663 net_set_state(NETLOOP_FAIL);
665 case TFTP_ERR_UNDEFINED:
666 case TFTP_ERR_DISK_FULL:
667 case TFTP_ERR_UNEXPECTED_OPCODE:
668 case TFTP_ERR_UNKNOWN_TRANSFER_ID:
669 case TFTP_ERR_FILE_ALREADY_EXISTS:
671 puts("Starting again\n\n");
672 #ifdef CONFIG_MCAST_TFTP
683 static void tftp_timeout_handler(void)
685 if (++timeout_count > timeout_count_max) {
686 restart("Retry count exceeded");
689 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
690 if (tftp_state != STATE_RECV_WRQ)
696 void tftp_start(enum proto_t protocol)
698 char *ep; /* Environment pointer */
701 * Allow the user to choose TFTP blocksize and timeout.
702 * TFTP protocol has a minimal timeout of 1 second.
704 ep = getenv("tftpblocksize");
706 tftp_block_size_option = simple_strtol(ep, NULL, 10);
708 ep = getenv("tftptimeout");
710 timeout_ms = simple_strtol(ep, NULL, 10);
712 if (timeout_ms < 1000) {
713 printf("TFTP timeout (%ld ms) too low, set min = 1000 ms\n",
718 debug("TFTP blocksize = %i, timeout = %ld ms\n",
719 tftp_block_size_option, timeout_ms);
721 tftp_remote_ip = net_server_ip;
722 if (net_boot_file_name[0] == '\0') {
723 sprintf(default_filename, "%02X%02X%02X%02X.img",
724 net_ip.s_addr & 0xFF,
725 (net_ip.s_addr >> 8) & 0xFF,
726 (net_ip.s_addr >> 16) & 0xFF,
727 (net_ip.s_addr >> 24) & 0xFF);
729 strncpy(tftp_filename, default_filename, MAX_LEN);
730 tftp_filename[MAX_LEN - 1] = 0;
732 printf("*** Warning: no boot file name; using '%s'\n",
735 char *p = strchr(net_boot_file_name, ':');
738 strncpy(tftp_filename, net_boot_file_name, MAX_LEN);
739 tftp_filename[MAX_LEN - 1] = 0;
741 tftp_remote_ip = string_to_ip(net_boot_file_name);
742 strncpy(tftp_filename, p + 1, MAX_LEN);
743 tftp_filename[MAX_LEN - 1] = 0;
747 printf("Using %s device\n", eth_get_name());
748 printf("TFTP %s server %pI4; our IP address is %pI4",
749 #ifdef CONFIG_CMD_TFTPPUT
750 protocol == TFTPPUT ? "to" : "from",
754 &tftp_remote_ip, &net_ip);
756 /* Check if we need to send across this subnet */
757 if (net_gateway.s_addr && net_netmask.s_addr) {
758 struct in_addr our_net;
759 struct in_addr remote_net;
761 our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
762 remote_net.s_addr = tftp_remote_ip.s_addr & net_netmask.s_addr;
763 if (our_net.s_addr != remote_net.s_addr)
764 printf("; sending through gateway %pI4", &net_gateway);
768 printf("Filename '%s'.", tftp_filename);
770 if (net_boot_file_expected_size_in_blocks) {
771 printf(" Size is 0x%x Bytes = ",
772 net_boot_file_expected_size_in_blocks << 9);
773 print_size(net_boot_file_expected_size_in_blocks << 9, "");
777 #ifdef CONFIG_CMD_TFTPPUT
778 tftp_put_active = (protocol == TFTPPUT);
779 if (tftp_put_active) {
780 printf("Save address: 0x%lx\n", save_addr);
781 printf("Save size: 0x%lx\n", save_size);
782 net_boot_file_size = save_size;
784 tftp_state = STATE_SEND_WRQ;
789 printf("Load address: 0x%lx\n", load_addr);
790 puts("Loading: *\b");
791 tftp_state = STATE_SEND_RRQ;
794 time_start = get_timer(0);
795 timeout_count_max = tftp_timeout_count_max;
797 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
798 net_set_udp_handler(tftp_handler);
799 #ifdef CONFIG_CMD_TFTPPUT
800 net_set_icmp_handler(icmp_handler);
802 tftp_remote_port = WELL_KNOWN_PORT;
804 /* Use a pseudo-random port unless a specific port is set */
805 tftp_our_port = 1024 + (get_timer(0) % 3072);
807 #ifdef CONFIG_TFTP_PORT
808 ep = getenv("tftpdstp");
810 tftp_remote_port = simple_strtol(ep, NULL, 10);
811 ep = getenv("tftpsrcp");
813 tftp_our_port = simple_strtol(ep, NULL, 10);
817 /* zero out server ether in case the server ip has changed */
818 memset(net_server_ethaddr, 0, 6);
819 /* Revert tftp_block_size to dflt */
820 tftp_block_size = TFTP_BLOCK_SIZE;
821 #ifdef CONFIG_MCAST_TFTP
824 #ifdef CONFIG_TFTP_TSIZE
826 tftp_tsize_num_hash = 0;
832 #ifdef CONFIG_CMD_TFTPSRV
833 void tftp_start_server(void)
835 tftp_filename[0] = 0;
837 printf("Using %s device\n", eth_get_name());
838 printf("Listening for TFTP transfer on %pI4\n", &net_ip);
839 printf("Load address: 0x%lx\n", load_addr);
841 puts("Loading: *\b");
843 timeout_count_max = TIMEOUT_COUNT;
845 timeout_ms = TIMEOUT;
846 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
848 /* Revert tftp_block_size to dflt */
849 tftp_block_size = TFTP_BLOCK_SIZE;
851 tftp_our_port = WELL_KNOWN_PORT;
853 #ifdef CONFIG_TFTP_TSIZE
855 tftp_tsize_num_hash = 0;
858 tftp_state = STATE_RECV_WRQ;
859 net_set_udp_handler(tftp_handler);
861 /* zero out server ether in case the server ip has changed */
862 memset(net_server_ethaddr, 0, 6);
864 #endif /* CONFIG_CMD_TFTPSRV */
866 #ifdef CONFIG_MCAST_TFTP
868 * Credits: atftp project.
872 * Pick up BcastAddr, Port, and whether I am [now] the master-client.
874 * +-------+-----------+---+-------~~-------+---+
875 * | opc | multicast | 0 | addr, port, mc | 0 |
876 * +-------+-----------+---+-------~~-------+---+
877 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
878 * I am the new master-client so must send ACKs to DataBlocks. If I am not
879 * master-client, I'm a passive client, gathering what DataBlocks I may and
880 * making note of which ones I got in my bitmask.
881 * In theory, I never go from master->passive..
882 * .. this comes in with pkt already pointing just past opc
884 static void parse_multicast_oack(char *pkt, int len)
895 /* march along looking for 'multicast\0', which has to start at least
896 * 14 bytes back from the end.
898 for (i = 0; i < len - 14; i++)
899 if (strcmp(pkt + i, "multicast") == 0)
901 if (i >= (len - 14)) /* non-Multicast OACK, ign. */
904 i += 10; /* strlen multicast */
906 for (; i < len; i++) {
907 if (*(pkt + i) == ',') {
917 if (!port || !mc_adr || !mc)
919 if (tftp_mcast_active && tftp_mcast_master_client) {
920 printf("I got a OACK as master Client, WRONG!\n");
923 /* ..I now accept packets destined for this MCAST addr, port */
924 if (!tftp_mcast_active) {
925 if (tftp_mcast_bitmap) {
926 printf("Internal failure! no mcast.\n");
927 free(tftp_mcast_bitmap);
928 tftp_mcast_bitmap = NULL;
929 tftp_mcast_disabled = 1;
932 /* I malloc instead of pre-declare; so that if the file ends
933 * up being too big for this bitmap I can retry
935 tftp_mcast_bitmap = malloc(tftp_mcast_bitmap_size);
936 if (!tftp_mcast_bitmap) {
937 printf("No bitmap, no multicast. Sorry.\n");
938 tftp_mcast_disabled = 1;
941 memset(tftp_mcast_bitmap, 0, tftp_mcast_bitmap_size);
942 tftp_mcast_prev_hole = 0;
943 tftp_mcast_active = 1;
945 addr = string_to_ip(mc_adr);
946 if (net_mcast_addr.s_addr != addr.s_addr) {
947 if (net_mcast_addr.s_addr)
948 eth_mcast_join(net_mcast_addr, 0);
949 net_mcast_addr = addr;
950 if (eth_mcast_join(net_mcast_addr, 1)) {
951 printf("Fail to set mcast, revert to TFTP\n");
952 tftp_mcast_disabled = 1;
957 tftp_mcast_master_client = simple_strtoul((char *)mc, NULL, 10);
958 tftp_mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
959 printf("Multicast: %s:%d [%d]\n", mc_adr, tftp_mcast_port,
960 tftp_mcast_master_client);
964 #endif /* Multicast TFTP */