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 /* Well known TFTP port # */
16 #define WELL_KNOWN_PORT 69
17 /* Millisecs to timeout for lost pkt */
18 #define TIMEOUT 5000UL
19 #ifndef CONFIG_NET_RETRY_COUNT
20 /* # of timeouts before giving up */
21 # define TIMEOUT_COUNT 10
23 # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
25 /* Number of "loading" hashes per line (for checking the image size) */
26 #define HASHES_PER_LINE 65
38 static ulong TftpTimeoutMSecs
= TIMEOUT
;
39 static int TftpTimeoutCountMax
= TIMEOUT_COUNT
;
42 * These globals govern the timeout behavior when attempting a connection to a
43 * TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to
44 * wait for the server to respond to initial connection. Second global,
45 * TftpRRQTimeoutCountMax, gives the number of such connection retries.
46 * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be
47 * positive. The globals are meant to be set (and restored) by code needing
48 * non-standard timeout behavior when initiating a TFTP transfer.
50 ulong TftpRRQTimeoutMSecs
= TIMEOUT
;
51 int TftpRRQTimeoutCountMax
= TIMEOUT_COUNT
;
54 TFTP_ERR_UNDEFINED
= 0,
55 TFTP_ERR_FILE_NOT_FOUND
= 1,
56 TFTP_ERR_ACCESS_DENIED
= 2,
57 TFTP_ERR_DISK_FULL
= 3,
58 TFTP_ERR_UNEXPECTED_OPCODE
= 4,
59 TFTP_ERR_UNKNOWN_TRANSFER_ID
= 5,
60 TFTP_ERR_FILE_ALREADY_EXISTS
= 6,
63 static IPaddr_t TftpRemoteIP
;
64 /* The UDP port at their end */
65 static int TftpRemotePort
;
66 /* The UDP port at our end */
67 static int TftpOurPort
;
68 static int TftpTimeoutCount
;
69 /* packet sequence number */
70 static ulong TftpBlock
;
71 /* last packet sequence number received */
72 static ulong TftpLastBlock
;
73 /* count of sequence number wraparounds */
74 static ulong TftpBlockWrap
;
75 /* memory offset due to wrapping */
76 static ulong TftpBlockWrapOffset
;
78 #ifdef CONFIG_TFTP_TSIZE
79 /* The file size reported by the server */
81 /* The number of hashes we printed */
82 static short TftpNumchars
;
85 #define STATE_SEND_RRQ 1
87 #define STATE_TOO_LARGE 3
88 #define STATE_BAD_MAGIC 4
90 #define STATE_RECV_WRQ 6
92 /* default TFTP block size */
93 #define TFTP_BLOCK_SIZE 512
94 /* sequence number is 16 bit */
95 #define TFTP_SEQUENCE_SIZE ((ulong)(1<<16))
97 #define DEFAULT_NAME_LEN (8 + 4 + 1)
98 static char default_filename
[DEFAULT_NAME_LEN
];
100 #ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
103 #define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
106 static char tftp_filename
[MAX_LEN
];
108 #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
109 extern flash_info_t flash_info
[];
112 /* 512 is poor choice for ethernet, MTU is typically 1500.
113 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
114 * almost-MTU block sizes. At least try... fall back to 512 if need be.
115 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
117 #ifdef CONFIG_TFTP_BLOCKSIZE
118 #define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
120 #define TFTP_MTU_BLOCKSIZE 1468
123 static unsigned short TftpBlkSize
= TFTP_BLOCK_SIZE
;
124 static unsigned short TftpBlkSizeOption
= TFTP_MTU_BLOCKSIZE
;
126 #ifdef CONFIG_MCAST_TFTP
128 #define MTFTP_BITMAPSIZE 0x1000
129 static unsigned *Bitmap
;
130 static int PrevBitmapHole
, Mapsize
= MTFTP_BITMAPSIZE
;
131 static uchar ProhibitMcast
, MasterClient
;
132 static uchar Multicast
;
133 extern IPaddr_t Mcast_addr
;
134 static int Mcast_port
;
135 static ulong TftpEndingBlock
; /* can get 'last' block before done..*/
137 static void parse_multicast_oack(char *pkt
, int len
);
143 eth_mcast_join(Mcast_addr
, 0);
147 Mcast_addr
= Multicast
= Mcast_port
= 0;
148 TftpEndingBlock
= -1;
151 #endif /* CONFIG_MCAST_TFTP */
153 static __inline__
void
154 store_block(unsigned block
, uchar
*src
, unsigned len
)
156 ulong offset
= block
* TftpBlkSize
+ TftpBlockWrapOffset
;
157 ulong newsize
= offset
+ len
;
158 #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
161 for (i
= 0; i
< CONFIG_SYS_MAX_FLASH_BANKS
; i
++) {
162 /* start address in flash? */
163 if (flash_info
[i
].flash_id
== FLASH_UNKNOWN
)
165 if (load_addr
+ offset
>= flash_info
[i
].start
[0]) {
171 if (rc
) { /* Flash is destination for this packet */
172 rc
= flash_write((char *)src
, (ulong
)(load_addr
+offset
), len
);
175 NetState
= NETLOOP_FAIL
;
180 #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
182 (void)memcpy((void *)(load_addr
+ offset
), src
, len
);
184 #ifdef CONFIG_MCAST_TFTP
186 ext2_set_bit(block
, Bitmap
);
189 if (NetBootFileXferSize
< newsize
)
190 NetBootFileXferSize
= newsize
;
193 static void TftpSend(void);
194 static void TftpTimeout(void);
196 /**********************************************************************/
206 #ifdef CONFIG_MCAST_TFTP
207 /* Multicast TFTP.. non-MasterClients do not ACK data. */
209 && (TftpState
== STATE_DATA
)
210 && (MasterClient
== 0))
214 * We will always be sending some sort of packet, so
215 * cobble together the packet headers now.
217 pkt
= NetTxPacket
+ NetEthHdrSize() + IP_HDR_SIZE
;
224 *s
++ = htons(TFTP_RRQ
);
226 strcpy((char *)pkt
, tftp_filename
);
227 pkt
+= strlen(tftp_filename
) + 1;
228 strcpy((char *)pkt
, "octet");
229 pkt
+= 5 /*strlen("octet")*/ + 1;
230 strcpy((char *)pkt
, "timeout");
231 pkt
+= 7 /*strlen("timeout")*/ + 1;
232 sprintf((char *)pkt
, "%lu", TftpTimeoutMSecs
/ 1000);
233 debug("send option \"timeout %s\"\n", (char *)pkt
);
234 pkt
+= strlen((char *)pkt
) + 1;
235 #ifdef CONFIG_TFTP_TSIZE
236 memcpy((char *)pkt
, "tsize\0000\0", 8);
239 /* try for more effic. blk size */
240 pkt
+= sprintf((char *)pkt
, "blksize%c%d%c",
241 0, TftpBlkSizeOption
, 0);
242 #ifdef CONFIG_MCAST_TFTP
243 /* Check all preconditions before even trying the option */
245 && (Bitmap
= malloc(Mapsize
))
246 && eth_get_dev()->mcast
) {
249 pkt
+= sprintf((char *)pkt
, "multicast%c%c", 0, 0);
251 #endif /* CONFIG_MCAST_TFTP */
256 #ifdef CONFIG_MCAST_TFTP
257 /* My turn! Start at where I need blocks I missed.*/
259 TftpBlock
= ext2_find_next_zero_bit(Bitmap
,
268 *s
++ = htons(TFTP_ACK
);
269 *s
++ = htons(TftpBlock
);
274 case STATE_TOO_LARGE
:
277 *s
++ = htons(TFTP_ERROR
);
280 strcpy((char *)pkt
, "File too large");
281 pkt
+= 14 /*strlen("File too large")*/ + 1;
285 case STATE_BAD_MAGIC
:
288 *s
++ = htons(TFTP_ERROR
);
291 strcpy((char *)pkt
, "File has bad magic");
292 pkt
+= 18 /*strlen("File has bad magic")*/ + 1;
297 NetSendUDPPacket(NetServerEther
, TftpRemoteIP
, TftpRemotePort
,
303 TftpHandler(uchar
*pkt
, unsigned dest
, IPaddr_t sip
, unsigned src
,
310 if (dest
!= TftpOurPort
) {
311 #ifdef CONFIG_MCAST_TFTP
313 && (!Mcast_port
|| (dest
!= Mcast_port
)))
317 if (TftpState
!= STATE_SEND_RRQ
&& src
!= TftpRemotePort
&&
318 TftpState
!= STATE_RECV_WRQ
)
324 /* warning: don't use increment (++) in ntohs() macros!! */
328 switch (ntohs(proto
)) {
336 #ifdef CONFIG_CMD_TFTPSRV
340 TftpRemotePort
= src
;
341 TftpOurPort
= 1024 + (get_timer(0) % 3072);
344 TftpBlockWrapOffset
= 0;
345 TftpSend(); /* Send ACK(0) */
350 debug("Got OACK: %s %s\n",
352 pkt
+ strlen((char *)pkt
) + 1);
353 TftpState
= STATE_OACK
;
354 TftpRemotePort
= src
;
356 * Check for 'blksize' option.
357 * Careful: "i" is signed, "len" is unsigned, thus
358 * something like "len-8" may give a *huge* number
360 for (i
= 0; i
+8 < len
; i
++) {
361 if (strcmp((char *)pkt
+i
, "blksize") == 0) {
362 TftpBlkSize
= (unsigned short)
363 simple_strtoul((char *)pkt
+i
+8, NULL
,
365 debug("Blocksize ack: %s, %d\n",
366 (char *)pkt
+i
+8, TftpBlkSize
);
368 #ifdef CONFIG_TFTP_TSIZE
369 if (strcmp((char *)pkt
+i
, "tsize") == 0) {
370 TftpTsize
= simple_strtoul((char *)pkt
+i
+6,
372 debug("size = %s, %d\n",
373 (char *)pkt
+i
+6, TftpTsize
);
377 #ifdef CONFIG_MCAST_TFTP
378 parse_multicast_oack((char *)pkt
, len
-1);
379 if ((Multicast
) && (!MasterClient
))
380 TftpState
= STATE_DATA
; /* passive.. */
383 TftpSend(); /* Send ACK */
389 TftpBlock
= ntohs(*(ushort
*)pkt
);
392 * RFC1350 specifies that the first data packet will
393 * have sequence number 1. If we receive a sequence
394 * number of 0 this means that there was a wrap
395 * around of the (16 bit) counter.
397 if (TftpBlock
== 0) {
399 TftpBlockWrapOffset
+=
400 TftpBlkSize
* TFTP_SEQUENCE_SIZE
;
401 printf("\n\t %lu MB received\n\t ",
402 TftpBlockWrapOffset
>>20);
404 #ifdef CONFIG_TFTP_TSIZE
405 else if (TftpTsize
) {
406 while (TftpNumchars
<
407 NetBootFileXferSize
* 50 / TftpTsize
) {
414 if (((TftpBlock
- 1) % 10) == 0)
416 else if ((TftpBlock
% (10 * HASHES_PER_LINE
)) == 0)
420 if (TftpState
== STATE_SEND_RRQ
)
421 debug("Server did not acknowledge timeout option!\n");
423 if (TftpState
== STATE_SEND_RRQ
|| TftpState
== STATE_OACK
||
424 TftpState
== STATE_RECV_WRQ
) {
425 /* first block received */
426 TftpState
= STATE_DATA
;
427 TftpRemotePort
= src
;
430 TftpBlockWrapOffset
= 0;
432 #ifdef CONFIG_MCAST_TFTP
433 if (Multicast
) { /* start!=1 common if mcast */
434 TftpLastBlock
= TftpBlock
- 1;
437 if (TftpBlock
!= 1) { /* Assertion */
438 printf("\nTFTP error: "
439 "First block is not block 1 (%ld)\n"
440 "Starting again\n\n",
447 if (TftpBlock
== TftpLastBlock
) {
449 * Same block again; ignore it.
454 TftpLastBlock
= TftpBlock
;
455 TftpTimeoutCountMax
= TIMEOUT_COUNT
;
456 NetSetTimeout(TftpTimeoutMSecs
, TftpTimeout
);
458 store_block(TftpBlock
- 1, pkt
+ 2, len
);
461 * Acknowledge the block just received, which will prompt
462 * the remote for the next one.
464 #ifdef CONFIG_MCAST_TFTP
465 /* if I am the MasterClient, actively calculate what my next
466 * needed block is; else I'm passive; not ACKING
469 if (len
< TftpBlkSize
) {
470 TftpEndingBlock
= TftpBlock
;
471 } else if (MasterClient
) {
472 TftpBlock
= PrevBitmapHole
=
473 ext2_find_next_zero_bit(
477 if (TftpBlock
> ((Mapsize
*8) - 1)) {
478 printf("tftpfile too big\n");
479 /* try to double it and retry */
485 TftpLastBlock
= TftpBlock
;
491 #ifdef CONFIG_MCAST_TFTP
493 if (MasterClient
&& (TftpBlock
>= TftpEndingBlock
)) {
494 puts("\nMulticast tftp done\n");
496 NetState
= NETLOOP_SUCCESS
;
501 if (len
< TftpBlkSize
) {
503 * We received the whole thing. Try to
506 #ifdef CONFIG_TFTP_TSIZE
507 /* Print hash marks for the last packet received */
508 while (TftpTsize
&& TftpNumchars
< 49) {
514 NetState
= NETLOOP_SUCCESS
;
519 printf("\nTFTP error: '%s' (%d)\n",
520 pkt
+ 2, ntohs(*(ushort
*)pkt
));
522 switch (ntohs(*(ushort
*)pkt
)) {
523 case TFTP_ERR_FILE_NOT_FOUND
:
524 case TFTP_ERR_ACCESS_DENIED
:
525 puts("Not retrying...\n");
527 NetState
= NETLOOP_FAIL
;
529 case TFTP_ERR_UNDEFINED
:
530 case TFTP_ERR_DISK_FULL
:
531 case TFTP_ERR_UNEXPECTED_OPCODE
:
532 case TFTP_ERR_UNKNOWN_TRANSFER_ID
:
533 case TFTP_ERR_FILE_ALREADY_EXISTS
:
535 puts("Starting again\n\n");
536 #ifdef CONFIG_MCAST_TFTP
550 if (++TftpTimeoutCount
> TftpTimeoutCountMax
) {
551 puts("\nRetry count exceeded; starting again\n");
552 #ifdef CONFIG_MCAST_TFTP
558 NetSetTimeout(TftpTimeoutMSecs
, TftpTimeout
);
559 if (TftpState
!= STATE_RECV_WRQ
)
568 char *ep
; /* Environment pointer */
571 * Allow the user to choose TFTP blocksize and timeout.
572 * TFTP protocol has a minimal timeout of 1 second.
574 ep
= getenv("tftpblocksize");
576 TftpBlkSizeOption
= simple_strtol(ep
, NULL
, 10);
578 ep
= getenv("tftptimeout");
580 TftpTimeoutMSecs
= simple_strtol(ep
, NULL
, 10);
582 if (TftpTimeoutMSecs
< 1000) {
583 printf("TFTP timeout (%ld ms) too low, "
584 "set minimum = 1000 ms\n",
586 TftpTimeoutMSecs
= 1000;
589 debug("TFTP blocksize = %i, timeout = %ld ms\n",
590 TftpBlkSizeOption
, TftpTimeoutMSecs
);
592 TftpRemoteIP
= NetServerIP
;
593 if (BootFile
[0] == '\0') {
594 sprintf(default_filename
, "%02lX%02lX%02lX%02lX.img",
596 (NetOurIP
>> 8) & 0xFF,
597 (NetOurIP
>> 16) & 0xFF,
598 (NetOurIP
>> 24) & 0xFF);
600 strncpy(tftp_filename
, default_filename
, MAX_LEN
);
601 tftp_filename
[MAX_LEN
-1] = 0;
603 printf("*** Warning: no boot file name; using '%s'\n",
606 char *p
= strchr(BootFile
, ':');
609 strncpy(tftp_filename
, BootFile
, MAX_LEN
);
610 tftp_filename
[MAX_LEN
-1] = 0;
612 TftpRemoteIP
= string_to_ip(BootFile
);
613 strncpy(tftp_filename
, p
+ 1, MAX_LEN
);
614 tftp_filename
[MAX_LEN
-1] = 0;
618 #if defined(CONFIG_NET_MULTI)
619 printf("Using %s device\n", eth_get_name());
621 printf("TFTP from server %pI4"
622 "; our IP address is %pI4", &TftpRemoteIP
, &NetOurIP
);
624 /* Check if we need to send across this subnet */
625 if (NetOurGatewayIP
&& NetOurSubnetMask
) {
626 IPaddr_t OurNet
= NetOurIP
& NetOurSubnetMask
;
627 IPaddr_t RemoteNet
= TftpRemoteIP
& NetOurSubnetMask
;
629 if (OurNet
!= RemoteNet
)
630 printf("; sending through gateway %pI4",
635 printf("Filename '%s'.", tftp_filename
);
637 if (NetBootFileSize
) {
638 printf(" Size is 0x%x Bytes = ", NetBootFileSize
<<9);
639 print_size(NetBootFileSize
<<9, "");
644 printf("Load address: 0x%lx\n", load_addr
);
646 puts("Loading: *\b");
648 TftpTimeoutCountMax
= TftpRRQTimeoutCountMax
;
650 NetSetTimeout(TftpTimeoutMSecs
, TftpTimeout
);
651 NetSetHandler(TftpHandler
);
653 TftpRemotePort
= WELL_KNOWN_PORT
;
654 TftpTimeoutCount
= 0;
655 TftpState
= STATE_SEND_RRQ
;
656 /* Use a pseudo-random port unless a specific port is set */
657 TftpOurPort
= 1024 + (get_timer(0) % 3072);
659 #ifdef CONFIG_TFTP_PORT
660 ep
= getenv("tftpdstp");
662 TftpRemotePort
= simple_strtol(ep
, NULL
, 10);
663 ep
= getenv("tftpsrcp");
665 TftpOurPort
= simple_strtol(ep
, NULL
, 10);
669 /* zero out server ether in case the server ip has changed */
670 memset(NetServerEther
, 0, 6);
671 /* Revert TftpBlkSize to dflt */
672 TftpBlkSize
= TFTP_BLOCK_SIZE
;
673 #ifdef CONFIG_MCAST_TFTP
676 #ifdef CONFIG_TFTP_TSIZE
684 #ifdef CONFIG_CMD_TFTPSRV
686 TftpStartServer(void)
688 tftp_filename
[0] = 0;
690 #if defined(CONFIG_NET_MULTI)
691 printf("Using %s device\n", eth_get_name());
693 printf("Listening for TFTP transfer on %pI4\n", &NetOurIP
);
694 printf("Load address: 0x%lx\n", load_addr
);
696 puts("Loading: *\b");
698 TftpTimeoutCountMax
= TIMEOUT_COUNT
;
699 TftpTimeoutCount
= 0;
700 TftpTimeoutMSecs
= TIMEOUT
;
701 NetSetTimeout(TftpTimeoutMSecs
, TftpTimeout
);
703 /* Revert TftpBlkSize to dflt */
704 TftpBlkSize
= TFTP_BLOCK_SIZE
;
706 TftpOurPort
= WELL_KNOWN_PORT
;
708 #ifdef CONFIG_TFTP_TSIZE
713 TftpState
= STATE_RECV_WRQ
;
714 NetSetHandler(TftpHandler
);
716 #endif /* CONFIG_CMD_TFTPSRV */
718 #ifdef CONFIG_MCAST_TFTP
719 /* Credits: atftp project.
722 /* pick up BcastAddr, Port, and whether I am [now] the master-client. *
724 * +-------+-----------+---+-------~~-------+---+
725 * | opc | multicast | 0 | addr, port, mc | 0 |
726 * +-------+-----------+---+-------~~-------+---+
727 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
728 * I am the new master-client so must send ACKs to DataBlocks. If I am not
729 * master-client, I'm a passive client, gathering what DataBlocks I may and
730 * making note of which ones I got in my bitmask.
731 * In theory, I never go from master->passive..
732 * .. this comes in with pkt already pointing just past opc
734 static void parse_multicast_oack(char *pkt
, int len
)
738 char *mc_adr
, *port
, *mc
;
740 mc_adr
= port
= mc
= NULL
;
741 /* march along looking for 'multicast\0', which has to start at least
742 * 14 bytes back from the end.
744 for (i
= 0; i
< len
-14; i
++)
745 if (strcmp(pkt
+i
, "multicast") == 0)
747 if (i
>= (len
-14)) /* non-Multicast OACK, ign. */
750 i
+= 10; /* strlen multicast */
752 for (; i
< len
; i
++) {
753 if (*(pkt
+i
) == ',') {
763 if (!port
|| !mc_adr
|| !mc
)
765 if (Multicast
&& MasterClient
) {
766 printf("I got a OACK as master Client, WRONG!\n");
769 /* ..I now accept packets destined for this MCAST addr, port */
772 printf("Internal failure! no mcast.\n");
778 /* I malloc instead of pre-declare; so that if the file ends
779 * up being too big for this bitmap I can retry
781 Bitmap
= malloc(Mapsize
);
783 printf("No Bitmap, no multicast. Sorry.\n");
787 memset(Bitmap
, 0, Mapsize
);
791 addr
= string_to_ip(mc_adr
);
792 if (Mcast_addr
!= addr
) {
794 eth_mcast_join(Mcast_addr
, 0);
796 if (eth_mcast_join(Mcast_addr
, 1)) {
797 printf("Fail to set mcast, revert to TFTP\n");
803 MasterClient
= (unsigned char)simple_strtoul((char *)mc
, NULL
, 10);
804 Mcast_port
= (unsigned short)simple_strtoul(port
, NULL
, 10);
805 printf("Multicast: %s:%d [%d]\n", mc_adr
, Mcast_port
, MasterClient
);
809 #endif /* Multicast TFTP */