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 TftpTimeoutMSecs
= TIMEOUT
;
43 static int TftpTimeoutCountMax
= 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. TftpRRQTimeoutMSecs specifies the number of milliseconds to
49 * wait for the server to respond to initial connection. Second global,
50 * TftpRRQTimeoutCountMax, gives the number of such connection retries.
51 * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs 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 TftpRRQTimeoutMSecs
= TIMEOUT
;
56 int TftpRRQTimeoutCountMax
= 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 TftpRemotePort
;
71 /* The UDP port at our end */
72 static int TftpOurPort
;
73 static int TftpTimeoutCount
;
74 /* packet sequence number */
75 static ulong TftpBlock
;
76 /* last packet sequence number received */
77 static ulong TftpLastBlock
;
78 /* count of sequence number wraparounds */
79 static ulong TftpBlockWrap
;
80 /* memory offset due to wrapping */
81 static ulong TftpBlockWrapOffset
;
83 #ifdef CONFIG_TFTP_TSIZE
84 /* The file size reported by the server */
86 /* The number of hashes we printed */
87 static short TftpNumchars
;
89 #ifdef CONFIG_CMD_TFTPPUT
90 static int TftpWriting
; /* 1 if writing, else 0 */
91 static int TftpFinalBlock
; /* 1 if we have sent the last block */
96 #define STATE_SEND_RRQ 1
98 #define STATE_TOO_LARGE 3
99 #define STATE_BAD_MAGIC 4
101 #define STATE_RECV_WRQ 6
102 #define STATE_SEND_WRQ 7
104 /* default TFTP block size */
105 #define TFTP_BLOCK_SIZE 512
106 /* sequence number is 16 bit */
107 #define TFTP_SEQUENCE_SIZE ((ulong)(1<<16))
109 #define DEFAULT_NAME_LEN (8 + 4 + 1)
110 static char default_filename
[DEFAULT_NAME_LEN
];
112 #ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
115 #define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
118 static char tftp_filename
[MAX_LEN
];
120 /* 512 is poor choice for ethernet, MTU is typically 1500.
121 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
122 * almost-MTU block sizes. At least try... fall back to 512 if need be.
123 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
125 #ifdef CONFIG_TFTP_BLOCKSIZE
126 #define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
128 #define TFTP_MTU_BLOCKSIZE 1468
131 static unsigned short TftpBlkSize
= TFTP_BLOCK_SIZE
;
132 static unsigned short TftpBlkSizeOption
= TFTP_MTU_BLOCKSIZE
;
134 #ifdef CONFIG_MCAST_TFTP
136 #define MTFTP_BITMAPSIZE 0x1000
137 static unsigned *Bitmap
;
138 static int PrevBitmapHole
, Mapsize
= MTFTP_BITMAPSIZE
;
139 static uchar ProhibitMcast
, MasterClient
;
140 static uchar Multicast
;
141 static int Mcast_port
;
142 static ulong TftpEndingBlock
; /* can get 'last' block before done..*/
144 static void parse_multicast_oack(char *pkt
, int len
);
150 eth_mcast_join(net_mcast_addr
, 0);
154 net_mcast_addr
.s_addr
= 0;
157 TftpEndingBlock
= -1;
160 #endif /* CONFIG_MCAST_TFTP */
163 store_block(int block
, uchar
*src
, unsigned len
)
165 ulong offset
= block
* TftpBlkSize
+ TftpBlockWrapOffset
;
166 ulong newsize
= offset
+ len
;
167 #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
170 for (i
= 0; i
< CONFIG_SYS_MAX_FLASH_BANKS
; i
++) {
171 /* start address in flash? */
172 if (flash_info
[i
].flash_id
== FLASH_UNKNOWN
)
174 if (load_addr
+ offset
>= flash_info
[i
].start
[0]) {
180 if (rc
) { /* Flash is destination for this packet */
181 rc
= flash_write((char *)src
, (ulong
)(load_addr
+offset
), len
);
184 net_set_state(NETLOOP_FAIL
);
188 #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
190 void *ptr
= map_sysmem(load_addr
+ offset
, len
);
192 memcpy(ptr
, src
, len
);
195 #ifdef CONFIG_MCAST_TFTP
197 ext2_set_bit(block
, Bitmap
);
200 if (NetBootFileXferSize
< newsize
)
201 NetBootFileXferSize
= newsize
;
204 /* Clear our state ready for a new transfer */
205 static void new_transfer(void)
209 TftpBlockWrapOffset
= 0;
210 #ifdef CONFIG_CMD_TFTPPUT
215 #ifdef CONFIG_CMD_TFTPPUT
217 * Load the next block from memory to be sent over tftp.
219 * @param block Block number to send
220 * @param dst Destination buffer for data
221 * @param len Number of bytes in block (this one and every other)
222 * @return number of bytes loaded
224 static int load_block(unsigned block
, uchar
*dst
, unsigned len
)
226 /* We may want to get the final block from the previous set */
227 ulong offset
= ((int)block
- 1) * len
+ TftpBlockWrapOffset
;
230 tosend
= min(NetBootFileXferSize
- offset
, tosend
);
231 (void)memcpy(dst
, (void *)(save_addr
+ offset
), tosend
);
232 debug("%s: block=%d, offset=%ld, len=%d, tosend=%ld\n", __func__
,
233 block
, offset
, len
, tosend
);
238 static void TftpSend(void);
239 static void TftpTimeout(void);
241 /**********************************************************************/
243 static void show_block_marker(void)
245 #ifdef CONFIG_TFTP_TSIZE
247 ulong pos
= TftpBlock
* TftpBlkSize
+ TftpBlockWrapOffset
;
249 while (TftpNumchars
< pos
* 50 / TftpTsize
) {
256 if (((TftpBlock
- 1) % 10) == 0)
258 else if ((TftpBlock
% (10 * HASHES_PER_LINE
)) == 0)
264 * restart the current transfer due to an error
266 * @param msg Message to print for user
268 static void restart(const char *msg
)
270 printf("\n%s; starting again\n", msg
);
271 #ifdef CONFIG_MCAST_TFTP
278 * Check if the block number has wrapped, and update progress
280 * TODO: The egregious use of global variables in this file should be tidied.
282 static void update_block_number(void)
285 * RFC1350 specifies that the first data packet will
286 * have sequence number 1. If we receive a sequence
287 * number of 0 this means that there was a wrap
288 * around of the (16 bit) counter.
290 if (TftpBlock
== 0 && TftpLastBlock
!= 0) {
292 TftpBlockWrapOffset
+= TftpBlkSize
* TFTP_SEQUENCE_SIZE
;
293 TftpTimeoutCount
= 0; /* we've done well, reset thhe timeout */
299 /* The TFTP get or put is complete */
300 static void tftp_complete(void)
302 #ifdef CONFIG_TFTP_TSIZE
303 /* Print hash marks for the last packet received */
304 while (TftpTsize
&& TftpNumchars
< 49) {
309 print_size(TftpTsize
, "");
311 time_start
= get_timer(time_start
);
312 if (time_start
> 0) {
313 puts("\n\t "); /* Line up with "Loading: " */
314 print_size(NetBootFileXferSize
/
315 time_start
* 1000, "/s");
318 net_set_state(NETLOOP_SUCCESS
);
329 #ifdef CONFIG_MCAST_TFTP
330 /* Multicast TFTP.. non-MasterClients do not ACK data. */
332 && (TftpState
== STATE_DATA
)
333 && (MasterClient
== 0))
337 * We will always be sending some sort of packet, so
338 * cobble together the packet headers now.
340 pkt
= NetTxPacket
+ NetEthHdrSize() + IP_UDP_HDR_SIZE
;
347 #ifdef CONFIG_CMD_TFTPPUT
348 *s
++ = htons(TftpState
== STATE_SEND_RRQ
? TFTP_RRQ
:
351 *s
++ = htons(TFTP_RRQ
);
354 strcpy((char *)pkt
, tftp_filename
);
355 pkt
+= strlen(tftp_filename
) + 1;
356 strcpy((char *)pkt
, "octet");
357 pkt
+= 5 /*strlen("octet")*/ + 1;
358 strcpy((char *)pkt
, "timeout");
359 pkt
+= 7 /*strlen("timeout")*/ + 1;
360 sprintf((char *)pkt
, "%lu", TftpTimeoutMSecs
/ 1000);
361 debug("send option \"timeout %s\"\n", (char *)pkt
);
362 pkt
+= strlen((char *)pkt
) + 1;
363 #ifdef CONFIG_TFTP_TSIZE
364 pkt
+= sprintf((char *)pkt
, "tsize%c%lu%c",
365 0, NetBootFileXferSize
, 0);
367 /* try for more effic. blk size */
368 pkt
+= sprintf((char *)pkt
, "blksize%c%d%c",
369 0, TftpBlkSizeOption
, 0);
370 #ifdef CONFIG_MCAST_TFTP
371 /* Check all preconditions before even trying the option */
372 if (!ProhibitMcast
) {
373 Bitmap
= malloc(Mapsize
);
374 if (Bitmap
&& eth_get_dev()->mcast
) {
377 pkt
+= sprintf((char *)pkt
, "multicast%c%c",
381 #endif /* CONFIG_MCAST_TFTP */
386 #ifdef CONFIG_MCAST_TFTP
387 /* My turn! Start at where I need blocks I missed.*/
389 TftpBlock
= ext2_find_next_zero_bit(Bitmap
,
398 s
[0] = htons(TFTP_ACK
);
399 s
[1] = htons(TftpBlock
);
400 pkt
= (uchar
*)(s
+ 2);
401 #ifdef CONFIG_CMD_TFTPPUT
403 int toload
= TftpBlkSize
;
404 int loaded
= load_block(TftpBlock
, pkt
, toload
);
406 s
[0] = htons(TFTP_DATA
);
408 TftpFinalBlock
= (loaded
< toload
);
414 case STATE_TOO_LARGE
:
417 *s
++ = htons(TFTP_ERROR
);
421 strcpy((char *)pkt
, "File too large");
422 pkt
+= 14 /*strlen("File too large")*/ + 1;
426 case STATE_BAD_MAGIC
:
429 *s
++ = htons(TFTP_ERROR
);
432 strcpy((char *)pkt
, "File has bad magic");
433 pkt
+= 18 /*strlen("File has bad magic")*/ + 1;
438 NetSendUDPPacket(NetServerEther
, tftp_remote_ip
, TftpRemotePort
,
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
)
461 if (dest
!= TftpOurPort
) {
462 #ifdef CONFIG_MCAST_TFTP
464 && (!Mcast_port
|| (dest
!= Mcast_port
)))
468 if (TftpState
!= STATE_SEND_RRQ
&& src
!= TftpRemotePort
&&
469 TftpState
!= STATE_RECV_WRQ
&& TftpState
!= STATE_SEND_WRQ
)
475 /* warning: don't use increment (++) in ntohs() macros!! */
479 switch (ntohs(proto
)) {
485 #ifdef CONFIG_CMD_TFTPPUT
487 if (TftpFinalBlock
) {
491 * Move to the next block. We want our block
492 * count to wrap just like the other end!
494 int block
= ntohs(*s
);
495 int ack_ok
= (TftpBlock
== block
);
497 TftpBlock
= (unsigned short)(block
+ 1);
498 update_block_number();
500 TftpSend(); /* Send next data block */
509 #ifdef CONFIG_CMD_TFTPSRV
512 tftp_remote_ip
= sip
;
513 TftpRemotePort
= src
;
514 TftpOurPort
= 1024 + (get_timer(0) % 3072);
516 TftpSend(); /* Send ACK(0) */
521 debug("Got OACK: %s %s\n",
523 pkt
+ strlen((char *)pkt
) + 1);
524 TftpState
= STATE_OACK
;
525 TftpRemotePort
= src
;
527 * Check for 'blksize' option.
528 * Careful: "i" is signed, "len" is unsigned, thus
529 * something like "len-8" may give a *huge* number
531 for (i
= 0; i
+8 < len
; i
++) {
532 if (strcmp((char *)pkt
+i
, "blksize") == 0) {
533 TftpBlkSize
= (unsigned short)
534 simple_strtoul((char *)pkt
+i
+8, NULL
,
536 debug("Blocksize ack: %s, %d\n",
537 (char *)pkt
+i
+8, TftpBlkSize
);
539 #ifdef CONFIG_TFTP_TSIZE
540 if (strcmp((char *)pkt
+i
, "tsize") == 0) {
541 TftpTsize
= simple_strtoul((char *)pkt
+i
+6,
543 debug("size = %s, %d\n",
544 (char *)pkt
+i
+6, TftpTsize
);
548 #ifdef CONFIG_MCAST_TFTP
549 parse_multicast_oack((char *)pkt
, len
-1);
550 if ((Multicast
) && (!MasterClient
))
551 TftpState
= STATE_DATA
; /* passive.. */
554 #ifdef CONFIG_CMD_TFTPPUT
556 /* Get ready to send the first block */
557 TftpState
= STATE_DATA
;
561 TftpSend(); /* Send ACK or first data block */
567 TftpBlock
= ntohs(*(__be16
*)pkt
);
569 update_block_number();
571 if (TftpState
== STATE_SEND_RRQ
)
572 debug("Server did not acknowledge timeout option!\n");
574 if (TftpState
== STATE_SEND_RRQ
|| TftpState
== STATE_OACK
||
575 TftpState
== STATE_RECV_WRQ
) {
576 /* first block received */
577 TftpState
= STATE_DATA
;
578 TftpRemotePort
= src
;
581 #ifdef CONFIG_MCAST_TFTP
582 if (Multicast
) { /* start!=1 common if mcast */
583 TftpLastBlock
= TftpBlock
- 1;
586 if (TftpBlock
!= 1) { /* Assertion */
587 printf("\nTFTP error: "
588 "First block is not block 1 (%ld)\n"
589 "Starting again\n\n",
596 if (TftpBlock
== TftpLastBlock
) {
598 * Same block again; ignore it.
603 TftpLastBlock
= TftpBlock
;
604 TftpTimeoutCountMax
= TIMEOUT_COUNT
;
605 NetSetTimeout(TftpTimeoutMSecs
, TftpTimeout
);
607 store_block(TftpBlock
- 1, pkt
+ 2, len
);
610 * Acknowledge the block just received, which will prompt
611 * the remote for the next one.
613 #ifdef CONFIG_MCAST_TFTP
614 /* if I am the MasterClient, actively calculate what my next
615 * needed block is; else I'm passive; not ACKING
618 if (len
< TftpBlkSize
) {
619 TftpEndingBlock
= TftpBlock
;
620 } else if (MasterClient
) {
621 TftpBlock
= PrevBitmapHole
=
622 ext2_find_next_zero_bit(
626 if (TftpBlock
> ((Mapsize
*8) - 1)) {
627 printf("tftpfile too big\n");
628 /* try to double it and retry */
634 TftpLastBlock
= TftpBlock
;
640 #ifdef CONFIG_MCAST_TFTP
642 if (MasterClient
&& (TftpBlock
>= TftpEndingBlock
)) {
643 puts("\nMulticast tftp done\n");
645 net_set_state(NETLOOP_SUCCESS
);
649 if (len
< TftpBlkSize
)
654 printf("\nTFTP error: '%s' (%d)\n",
655 pkt
+ 2, ntohs(*(__be16
*)pkt
));
657 switch (ntohs(*(__be16
*)pkt
)) {
658 case TFTP_ERR_FILE_NOT_FOUND
:
659 case TFTP_ERR_ACCESS_DENIED
:
660 puts("Not retrying...\n");
662 net_set_state(NETLOOP_FAIL
);
664 case TFTP_ERR_UNDEFINED
:
665 case TFTP_ERR_DISK_FULL
:
666 case TFTP_ERR_UNEXPECTED_OPCODE
:
667 case TFTP_ERR_UNKNOWN_TRANSFER_ID
:
668 case TFTP_ERR_FILE_ALREADY_EXISTS
:
670 puts("Starting again\n\n");
671 #ifdef CONFIG_MCAST_TFTP
685 if (++TftpTimeoutCount
> TftpTimeoutCountMax
) {
686 restart("Retry count exceeded");
689 NetSetTimeout(TftpTimeoutMSecs
, TftpTimeout
);
690 if (TftpState
!= STATE_RECV_WRQ
)
696 void TftpStart(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 TftpBlkSizeOption
= simple_strtol(ep
, NULL
, 10);
708 ep
= getenv("tftptimeout");
710 TftpTimeoutMSecs
= simple_strtol(ep
, NULL
, 10);
712 if (TftpTimeoutMSecs
< 1000) {
713 printf("TFTP timeout (%ld ms) too low, "
714 "set minimum = 1000 ms\n",
716 TftpTimeoutMSecs
= 1000;
719 debug("TFTP blocksize = %i, timeout = %ld ms\n",
720 TftpBlkSizeOption
, TftpTimeoutMSecs
);
722 tftp_remote_ip
= net_server_ip
;
723 if (BootFile
[0] == '\0') {
724 sprintf(default_filename
, "%02X%02X%02X%02X.img",
725 net_ip
.s_addr
& 0xFF,
726 (net_ip
.s_addr
>> 8) & 0xFF,
727 (net_ip
.s_addr
>> 16) & 0xFF,
728 (net_ip
.s_addr
>> 24) & 0xFF);
730 strncpy(tftp_filename
, default_filename
, MAX_LEN
);
731 tftp_filename
[MAX_LEN
-1] = 0;
733 printf("*** Warning: no boot file name; using '%s'\n",
736 char *p
= strchr(BootFile
, ':');
739 strncpy(tftp_filename
, BootFile
, MAX_LEN
);
740 tftp_filename
[MAX_LEN
-1] = 0;
742 tftp_remote_ip
= string_to_ip(BootFile
);
743 strncpy(tftp_filename
, p
+ 1, MAX_LEN
);
744 tftp_filename
[MAX_LEN
-1] = 0;
748 printf("Using %s device\n", eth_get_name());
749 printf("TFTP %s server %pI4; our IP address is %pI4",
750 #ifdef CONFIG_CMD_TFTPPUT
751 protocol
== TFTPPUT
? "to" : "from",
755 &tftp_remote_ip
, &net_ip
);
757 /* Check if we need to send across this subnet */
758 if (net_gateway
.s_addr
&& net_netmask
.s_addr
) {
759 struct in_addr our_net
;
760 struct in_addr remote_net
;
762 our_net
.s_addr
= net_ip
.s_addr
& net_netmask
.s_addr
;
763 remote_net
.s_addr
= tftp_remote_ip
.s_addr
& net_netmask
.s_addr
;
764 if (our_net
.s_addr
!= remote_net
.s_addr
)
765 printf("; sending through gateway %pI4", &net_gateway
);
769 printf("Filename '%s'.", tftp_filename
);
771 if (NetBootFileSize
) {
772 printf(" Size is 0x%x Bytes = ", NetBootFileSize
<<9);
773 print_size(NetBootFileSize
<<9, "");
777 #ifdef CONFIG_CMD_TFTPPUT
778 TftpWriting
= (protocol
== TFTPPUT
);
780 printf("Save address: 0x%lx\n", save_addr
);
781 printf("Save size: 0x%lx\n", save_size
);
782 NetBootFileXferSize
= save_size
;
784 TftpState
= STATE_SEND_WRQ
;
789 printf("Load address: 0x%lx\n", load_addr
);
790 puts("Loading: *\b");
791 TftpState
= STATE_SEND_RRQ
;
794 time_start
= get_timer(0);
795 TftpTimeoutCountMax
= TftpRRQTimeoutCountMax
;
797 NetSetTimeout(TftpTimeoutMSecs
, TftpTimeout
);
798 net_set_udp_handler(tftp_handler
);
799 #ifdef CONFIG_CMD_TFTPPUT
800 net_set_icmp_handler(icmp_handler
);
802 TftpRemotePort
= WELL_KNOWN_PORT
;
803 TftpTimeoutCount
= 0;
804 /* Use a pseudo-random port unless a specific port is set */
805 TftpOurPort
= 1024 + (get_timer(0) % 3072);
807 #ifdef CONFIG_TFTP_PORT
808 ep
= getenv("tftpdstp");
810 TftpRemotePort
= simple_strtol(ep
, NULL
, 10);
811 ep
= getenv("tftpsrcp");
813 TftpOurPort
= simple_strtol(ep
, NULL
, 10);
817 /* zero out server ether in case the server ip has changed */
818 memset(NetServerEther
, 0, 6);
819 /* Revert TftpBlkSize to dflt */
820 TftpBlkSize
= TFTP_BLOCK_SIZE
;
821 #ifdef CONFIG_MCAST_TFTP
824 #ifdef CONFIG_TFTP_TSIZE
832 #ifdef CONFIG_CMD_TFTPSRV
834 TftpStartServer(void)
836 tftp_filename
[0] = 0;
838 printf("Using %s device\n", eth_get_name());
839 printf("Listening for TFTP transfer on %pI4\n", &net_ip
);
840 printf("Load address: 0x%lx\n", load_addr
);
842 puts("Loading: *\b");
844 TftpTimeoutCountMax
= TIMEOUT_COUNT
;
845 TftpTimeoutCount
= 0;
846 TftpTimeoutMSecs
= TIMEOUT
;
847 NetSetTimeout(TftpTimeoutMSecs
, TftpTimeout
);
849 /* Revert TftpBlkSize to dflt */
850 TftpBlkSize
= TFTP_BLOCK_SIZE
;
852 TftpOurPort
= WELL_KNOWN_PORT
;
854 #ifdef CONFIG_TFTP_TSIZE
859 TftpState
= STATE_RECV_WRQ
;
860 net_set_udp_handler(tftp_handler
);
862 /* zero out server ether in case the server ip has changed */
863 memset(NetServerEther
, 0, 6);
865 #endif /* CONFIG_CMD_TFTPSRV */
867 #ifdef CONFIG_MCAST_TFTP
868 /* Credits: atftp project.
871 /* pick up BcastAddr, Port, and whether I am [now] the master-client. *
873 * +-------+-----------+---+-------~~-------+---+
874 * | opc | multicast | 0 | addr, port, mc | 0 |
875 * +-------+-----------+---+-------~~-------+---+
876 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
877 * I am the new master-client so must send ACKs to DataBlocks. If I am not
878 * master-client, I'm a passive client, gathering what DataBlocks I may and
879 * making note of which ones I got in my bitmask.
880 * In theory, I never go from master->passive..
881 * .. this comes in with pkt already pointing just past opc
883 static void parse_multicast_oack(char *pkt
, int len
)
887 char *mc_adr
, *port
, *mc
;
889 mc_adr
= port
= mc
= NULL
;
890 /* march along looking for 'multicast\0', which has to start at least
891 * 14 bytes back from the end.
893 for (i
= 0; i
< len
-14; i
++)
894 if (strcmp(pkt
+i
, "multicast") == 0)
896 if (i
>= (len
-14)) /* non-Multicast OACK, ign. */
899 i
+= 10; /* strlen multicast */
901 for (; i
< len
; i
++) {
902 if (*(pkt
+i
) == ',') {
912 if (!port
|| !mc_adr
|| !mc
)
914 if (Multicast
&& MasterClient
) {
915 printf("I got a OACK as master Client, WRONG!\n");
918 /* ..I now accept packets destined for this MCAST addr, port */
921 printf("Internal failure! no mcast.\n");
927 /* I malloc instead of pre-declare; so that if the file ends
928 * up being too big for this bitmap I can retry
930 Bitmap
= malloc(Mapsize
);
932 printf("No Bitmap, no multicast. Sorry.\n");
936 memset(Bitmap
, 0, Mapsize
);
940 addr
= string_to_ip(mc_adr
);
941 if (net_mcast_addr
.s_addr
!= addr
.s_addr
) {
942 if (net_mcast_addr
.s_addr
)
943 eth_mcast_join(net_mcast_addr
, 0);
944 net_mcast_addr
= addr
;
945 if (eth_mcast_join(net_mcast_addr
, 1)) {
946 printf("Fail to set mcast, revert to TFTP\n");
952 MasterClient
= (unsigned char)simple_strtoul((char *)mc
, NULL
, 10);
953 Mcast_port
= (unsigned short)simple_strtoul(port
, NULL
, 10);
954 printf("Multicast: %s:%d [%d]\n", mc_adr
, Mcast_port
, MasterClient
);
958 #endif /* Multicast TFTP */