2 * Copyright 1994, 1995, 2000 Neil Russell.
4 * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
13 /* Well known TFTP port # */
14 #define WELL_KNOWN_PORT 69
15 /* Millisecs to timeout for lost pkt */
16 #define TIMEOUT 5000UL
17 #ifndef CONFIG_NET_RETRY_COUNT
18 /* # of timeouts before giving up */
19 # define TIMEOUT_COUNT 10
21 # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
23 /* Number of "loading" hashes per line (for checking the image size) */
24 #define HASHES_PER_LINE 65
36 static ulong TftpTimeoutMSecs
= TIMEOUT
;
37 static int TftpTimeoutCountMax
= TIMEOUT_COUNT
;
40 * These globals govern the timeout behavior when attempting a connection to a
41 * TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to
42 * wait for the server to respond to initial connection. Second global,
43 * TftpRRQTimeoutCountMax, gives the number of such connection retries.
44 * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be
45 * positive. The globals are meant to be set (and restored) by code needing
46 * non-standard timeout behavior when initiating a TFTP transfer.
48 ulong TftpRRQTimeoutMSecs
= TIMEOUT
;
49 int TftpRRQTimeoutCountMax
= TIMEOUT_COUNT
;
52 TFTP_ERR_UNDEFINED
= 0,
53 TFTP_ERR_FILE_NOT_FOUND
= 1,
54 TFTP_ERR_ACCESS_DENIED
= 2,
55 TFTP_ERR_DISK_FULL
= 3,
56 TFTP_ERR_UNEXPECTED_OPCODE
= 4,
57 TFTP_ERR_UNKNOWN_TRANSFER_ID
= 5,
58 TFTP_ERR_FILE_ALREADY_EXISTS
= 6,
61 static IPaddr_t TftpServerIP
;
62 /* The UDP port at their end */
63 static int TftpServerPort
;
64 /* The UDP port at our end */
65 static int TftpOurPort
;
66 static int TftpTimeoutCount
;
67 /* packet sequence number */
68 static ulong TftpBlock
;
69 /* last packet sequence number received */
70 static ulong TftpLastBlock
;
71 /* count of sequence number wraparounds */
72 static ulong TftpBlockWrap
;
73 /* memory offset due to wrapping */
74 static ulong TftpBlockWrapOffset
;
76 #ifdef CONFIG_TFTP_TSIZE
77 /* The file size reported by the server */
79 /* The number of hashes we printed */
80 static short TftpNumchars
;
85 #define STATE_TOO_LARGE 3
86 #define STATE_BAD_MAGIC 4
89 /* default TFTP block size */
90 #define TFTP_BLOCK_SIZE 512
91 /* sequence number is 16 bit */
92 #define TFTP_SEQUENCE_SIZE ((ulong)(1<<16))
94 #define DEFAULT_NAME_LEN (8 + 4 + 1)
95 static char default_filename
[DEFAULT_NAME_LEN
];
97 #ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
100 #define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
103 static char tftp_filename
[MAX_LEN
];
105 #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
106 extern flash_info_t flash_info
[];
109 /* 512 is poor choice for ethernet, MTU is typically 1500.
110 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
111 * almost-MTU block sizes. At least try... fall back to 512 if need be.
112 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
114 #ifdef CONFIG_TFTP_BLOCKSIZE
115 #define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
117 #define TFTP_MTU_BLOCKSIZE 1468
120 static unsigned short TftpBlkSize
= TFTP_BLOCK_SIZE
;
121 static unsigned short TftpBlkSizeOption
= TFTP_MTU_BLOCKSIZE
;
123 #ifdef CONFIG_MCAST_TFTP
125 #define MTFTP_BITMAPSIZE 0x1000
126 static unsigned *Bitmap
;
127 static int PrevBitmapHole
, Mapsize
= MTFTP_BITMAPSIZE
;
128 static uchar ProhibitMcast
= 0, MasterClient
= 0;
129 static uchar Multicast
= 0;
130 extern IPaddr_t Mcast_addr
;
131 static int Mcast_port
;
132 static ulong TftpEndingBlock
; /* can get 'last' block before done..*/
134 static void parse_multicast_oack(char *pkt
, int len
);
139 if (Mcast_addr
) eth_mcast_join(Mcast_addr
, 0);
140 if (Bitmap
) free(Bitmap
);
142 Mcast_addr
= Multicast
= Mcast_port
= 0;
143 TftpEndingBlock
= -1;
146 #endif /* CONFIG_MCAST_TFTP */
148 static __inline__
void
149 store_block(unsigned block
, uchar
*src
, unsigned len
)
151 ulong offset
= block
* TftpBlkSize
+ TftpBlockWrapOffset
;
152 ulong newsize
= offset
+ len
;
153 #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
156 for (i
= 0; i
< CONFIG_SYS_MAX_FLASH_BANKS
; i
++) {
157 /* start address in flash? */
158 if (flash_info
[i
].flash_id
== FLASH_UNKNOWN
)
160 if (load_addr
+ offset
>= flash_info
[i
].start
[0]) {
166 if (rc
) { /* Flash is destination for this packet */
167 rc
= flash_write((char *)src
, (ulong
)(load_addr
+offset
), len
);
170 NetState
= NETLOOP_FAIL
;
175 #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
177 (void)memcpy((void *)(load_addr
+ offset
), src
, len
);
179 #ifdef CONFIG_MCAST_TFTP
181 ext2_set_bit(block
, Bitmap
);
184 if (NetBootFileXferSize
< newsize
)
185 NetBootFileXferSize
= newsize
;
188 static void TftpSend(void);
189 static void TftpTimeout(void);
191 /**********************************************************************/
201 #ifdef CONFIG_MCAST_TFTP
202 /* Multicast TFTP.. non-MasterClients do not ACK data. */
204 && (TftpState
== STATE_DATA
)
205 && (MasterClient
== 0))
209 * We will always be sending some sort of packet, so
210 * cobble together the packet headers now.
212 pkt
= NetTxPacket
+ NetEthHdrSize() + IP_HDR_SIZE
;
219 *s
++ = htons(TFTP_RRQ
);
221 strcpy((char *)pkt
, tftp_filename
);
222 pkt
+= strlen(tftp_filename
) + 1;
223 strcpy((char *)pkt
, "octet");
224 pkt
+= 5 /*strlen("octet")*/ + 1;
225 strcpy((char *)pkt
, "timeout");
226 pkt
+= 7 /*strlen("timeout")*/ + 1;
227 sprintf((char *)pkt
, "%lu", TftpTimeoutMSecs
/ 1000);
228 debug("send option \"timeout %s\"\n", (char *)pkt
);
229 pkt
+= strlen((char *)pkt
) + 1;
230 #ifdef CONFIG_TFTP_TSIZE
231 memcpy((char *)pkt
, "tsize\0000\0", 8);
234 /* try for more effic. blk size */
235 pkt
+= sprintf((char *)pkt
, "blksize%c%d%c",
236 0, TftpBlkSizeOption
, 0);
237 #ifdef CONFIG_MCAST_TFTP
238 /* Check all preconditions before even trying the option */
240 && (Bitmap
= malloc(Mapsize
))
241 && eth_get_dev()->mcast
) {
244 pkt
+= sprintf((char *)pkt
, "multicast%c%c", 0, 0);
246 #endif /* CONFIG_MCAST_TFTP */
251 #ifdef CONFIG_MCAST_TFTP
252 /* My turn! Start at where I need blocks I missed.*/
254 TftpBlock
= ext2_find_next_zero_bit(Bitmap
,
261 *s
++ = htons(TFTP_ACK
);
262 *s
++ = htons(TftpBlock
);
267 case STATE_TOO_LARGE
:
270 *s
++ = htons(TFTP_ERROR
);
273 strcpy((char *)pkt
, "File too large");
274 pkt
+= 14 /*strlen("File too large")*/ + 1;
278 case STATE_BAD_MAGIC
:
281 *s
++ = htons(TFTP_ERROR
);
284 strcpy((char *)pkt
, "File has bad magic");
285 pkt
+= 18 /*strlen("File has bad magic")*/ + 1;
290 NetSendUDPPacket(NetServerEther
, TftpServerIP
, TftpServerPort
,
296 TftpHandler(uchar
*pkt
, unsigned dest
, IPaddr_t sip
, unsigned src
,
303 if (dest
!= TftpOurPort
) {
304 #ifdef CONFIG_MCAST_TFTP
306 && (!Mcast_port
|| (dest
!= Mcast_port
)))
310 if (TftpState
!= STATE_RRQ
&& src
!= TftpServerPort
) {
318 /* warning: don't use increment (++) in ntohs() macros!! */
322 switch (ntohs(proto
)) {
332 debug("Got OACK: %s %s\n",
334 pkt
+ strlen((char *)pkt
) + 1);
335 TftpState
= STATE_OACK
;
336 TftpServerPort
= src
;
338 * Check for 'blksize' option.
339 * Careful: "i" is signed, "len" is unsigned, thus
340 * something like "len-8" may give a *huge* number
342 for (i
= 0; i
+8 < len
; i
++) {
343 if (strcmp((char *)pkt
+i
, "blksize") == 0) {
344 TftpBlkSize
= (unsigned short)
345 simple_strtoul((char *)pkt
+i
+8, NULL
,
347 debug("Blocksize ack: %s, %d\n",
348 (char *)pkt
+i
+8, TftpBlkSize
);
350 #ifdef CONFIG_TFTP_TSIZE
351 if (strcmp((char *)pkt
+i
, "tsize") == 0) {
352 TftpTsize
= simple_strtoul((char *)pkt
+i
+6,
354 debug("size = %s, %d\n",
355 (char *)pkt
+i
+6, TftpTsize
);
359 #ifdef CONFIG_MCAST_TFTP
360 parse_multicast_oack((char *)pkt
, len
-1);
361 if ((Multicast
) && (!MasterClient
))
362 TftpState
= STATE_DATA
; /* passive.. */
365 TftpSend(); /* Send ACK */
371 TftpBlock
= ntohs(*(ushort
*)pkt
);
374 * RFC1350 specifies that the first data packet will
375 * have sequence number 1. If we receive a sequence
376 * number of 0 this means that there was a wrap
377 * around of the (16 bit) counter.
379 if (TftpBlock
== 0) {
381 TftpBlockWrapOffset
+=
382 TftpBlkSize
* TFTP_SEQUENCE_SIZE
;
383 printf("\n\t %lu MB received\n\t ",
384 TftpBlockWrapOffset
>>20);
386 #ifdef CONFIG_TFTP_TSIZE
387 else if (TftpTsize
) {
388 while (TftpNumchars
<
389 NetBootFileXferSize
* 50 / TftpTsize
) {
396 if (((TftpBlock
- 1) % 10) == 0) {
398 } else if ((TftpBlock
% (10 * HASHES_PER_LINE
)) == 0) {
403 if (TftpState
== STATE_RRQ
)
404 debug("Server did not acknowledge timeout option!\n");
406 if (TftpState
== STATE_RRQ
|| TftpState
== STATE_OACK
) {
407 /* first block received */
408 TftpState
= STATE_DATA
;
409 TftpServerPort
= src
;
412 TftpBlockWrapOffset
= 0;
414 #ifdef CONFIG_MCAST_TFTP
415 if (Multicast
) { /* start!=1 common if mcast */
416 TftpLastBlock
= TftpBlock
- 1;
419 if (TftpBlock
!= 1) { /* Assertion */
420 printf("\nTFTP error: "
421 "First block is not block 1 (%ld)\n"
422 "Starting again\n\n",
429 if (TftpBlock
== TftpLastBlock
) {
431 * Same block again; ignore it.
436 TftpLastBlock
= TftpBlock
;
437 TftpTimeoutCountMax
= TIMEOUT_COUNT
;
438 NetSetTimeout(TftpTimeoutMSecs
, TftpTimeout
);
440 store_block(TftpBlock
- 1, pkt
+ 2, len
);
443 * Acknoledge the block just received, which will prompt
444 * the server for the next one.
446 #ifdef CONFIG_MCAST_TFTP
447 /* if I am the MasterClient, actively calculate what my next
448 * needed block is; else I'm passive; not ACKING
451 if (len
< TftpBlkSize
) {
452 TftpEndingBlock
= TftpBlock
;
453 } else if (MasterClient
) {
454 TftpBlock
= PrevBitmapHole
=
455 ext2_find_next_zero_bit(
459 if (TftpBlock
> ((Mapsize
*8) - 1)) {
460 printf("tftpfile too big\n");
461 /* try to double it and retry */
467 TftpLastBlock
= TftpBlock
;
473 #ifdef CONFIG_MCAST_TFTP
475 if (MasterClient
&& (TftpBlock
>= TftpEndingBlock
)) {
476 puts("\nMulticast tftp done\n");
478 NetState
= NETLOOP_SUCCESS
;
483 if (len
< TftpBlkSize
) {
485 * We received the whole thing. Try to
488 #ifdef CONFIG_TFTP_TSIZE
489 /* Print hash marks for the last packet received */
490 while (TftpTsize
&& TftpNumchars
< 49) {
496 NetState
= NETLOOP_SUCCESS
;
501 printf("\nTFTP error: '%s' (%d)\n",
502 pkt
+ 2, ntohs(*(ushort
*)pkt
));
504 switch (ntohs(*(ushort
*)pkt
)) {
505 case TFTP_ERR_FILE_NOT_FOUND
:
506 case TFTP_ERR_ACCESS_DENIED
:
507 puts("Not retrying...\n");
509 NetState
= NETLOOP_FAIL
;
511 case TFTP_ERR_UNDEFINED
:
512 case TFTP_ERR_DISK_FULL
:
513 case TFTP_ERR_UNEXPECTED_OPCODE
:
514 case TFTP_ERR_UNKNOWN_TRANSFER_ID
:
515 case TFTP_ERR_FILE_ALREADY_EXISTS
:
517 puts("Starting again\n\n");
518 #ifdef CONFIG_MCAST_TFTP
532 if (++TftpTimeoutCount
> TftpTimeoutCountMax
) {
533 puts("\nRetry count exceeded; starting again\n");
534 #ifdef CONFIG_MCAST_TFTP
540 NetSetTimeout(TftpTimeoutMSecs
, TftpTimeout
);
549 char *ep
; /* Environment pointer */
552 * Allow the user to choose TFTP blocksize and timeout.
553 * TFTP protocol has a minimal timeout of 1 second.
555 ep
= getenv("tftpblocksize");
557 TftpBlkSizeOption
= simple_strtol(ep
, NULL
, 10);
559 ep
= getenv("tftptimeout");
561 TftpTimeoutMSecs
= simple_strtol(ep
, NULL
, 10);
563 if (TftpTimeoutMSecs
< 1000) {
564 printf("TFTP timeout (%ld ms) too low, "
565 "set minimum = 1000 ms\n",
567 TftpTimeoutMSecs
= 1000;
570 debug("TFTP blocksize = %i, timeout = %ld ms\n",
571 TftpBlkSizeOption
, TftpTimeoutMSecs
);
573 TftpServerIP
= NetServerIP
;
574 if (BootFile
[0] == '\0') {
575 sprintf(default_filename
, "%02lX%02lX%02lX%02lX.img",
577 (NetOurIP
>> 8) & 0xFF,
578 (NetOurIP
>> 16) & 0xFF,
579 (NetOurIP
>> 24) & 0xFF);
581 strncpy(tftp_filename
, default_filename
, MAX_LEN
);
582 tftp_filename
[MAX_LEN
-1] = 0;
584 printf("*** Warning: no boot file name; using '%s'\n",
587 char *p
= strchr(BootFile
, ':');
590 strncpy(tftp_filename
, BootFile
, MAX_LEN
);
591 tftp_filename
[MAX_LEN
-1] = 0;
593 TftpServerIP
= string_to_ip(BootFile
);
594 strncpy(tftp_filename
, p
+ 1, MAX_LEN
);
595 tftp_filename
[MAX_LEN
-1] = 0;
599 #if defined(CONFIG_NET_MULTI)
600 printf("Using %s device\n", eth_get_name());
602 printf("TFTP from server %pI4"
603 "; our IP address is %pI4", &TftpServerIP
, &NetOurIP
);
605 /* Check if we need to send across this subnet */
606 if (NetOurGatewayIP
&& NetOurSubnetMask
) {
607 IPaddr_t OurNet
= NetOurIP
& NetOurSubnetMask
;
608 IPaddr_t ServerNet
= TftpServerIP
& NetOurSubnetMask
;
610 if (OurNet
!= ServerNet
)
611 printf("; sending through gateway %pI4", &NetOurGatewayIP
);
615 printf("Filename '%s'.", tftp_filename
);
617 if (NetBootFileSize
) {
618 printf(" Size is 0x%x Bytes = ", NetBootFileSize
<<9);
619 print_size(NetBootFileSize
<<9, "");
624 printf("Load address: 0x%lx\n", load_addr
);
626 puts("Loading: *\b");
628 TftpTimeoutCountMax
= TftpRRQTimeoutCountMax
;
630 NetSetTimeout(TftpTimeoutMSecs
, TftpTimeout
);
631 NetSetHandler(TftpHandler
);
633 TftpServerPort
= WELL_KNOWN_PORT
;
634 TftpTimeoutCount
= 0;
635 TftpState
= STATE_RRQ
;
636 /* Use a pseudo-random port unless a specific port is set */
637 TftpOurPort
= 1024 + (get_timer(0) % 3072);
639 #ifdef CONFIG_TFTP_PORT
640 ep
= getenv("tftpdstp");
642 TftpServerPort
= simple_strtol(ep
, NULL
, 10);
644 ep
= getenv("tftpsrcp");
646 TftpOurPort
= simple_strtol(ep
, NULL
, 10);
651 /* zero out server ether in case the server ip has changed */
652 memset(NetServerEther
, 0, 6);
653 /* Revert TftpBlkSize to dflt */
654 TftpBlkSize
= TFTP_BLOCK_SIZE
;
655 #ifdef CONFIG_MCAST_TFTP
658 #ifdef CONFIG_TFTP_TSIZE
666 #ifdef CONFIG_MCAST_TFTP
667 /* Credits: atftp project.
670 /* pick up BcastAddr, Port, and whether I am [now] the master-client. *
672 * +-------+-----------+---+-------~~-------+---+
673 * | opc | multicast | 0 | addr, port, mc | 0 |
674 * +-------+-----------+---+-------~~-------+---+
675 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
676 * I am the new master-client so must send ACKs to DataBlocks. If I am not
677 * master-client, I'm a passive client, gathering what DataBlocks I may and
678 * making note of which ones I got in my bitmask.
679 * In theory, I never go from master->passive..
680 * .. this comes in with pkt already pointing just past opc
682 static void parse_multicast_oack(char *pkt
, int len
)
686 char *mc_adr
, *port
, *mc
;
688 mc_adr
= port
= mc
= NULL
;
689 /* march along looking for 'multicast\0', which has to start at least
690 * 14 bytes back from the end.
692 for (i
= 0; i
< len
-14; i
++)
693 if (strcmp(pkt
+i
, "multicast") == 0)
695 if (i
>= (len
-14)) /* non-Multicast OACK, ign. */
698 i
+= 10; /* strlen multicast */
700 for (; i
< len
; i
++) {
701 if (*(pkt
+i
) == ',') {
711 if (!port
|| !mc_adr
|| !mc
) return;
712 if (Multicast
&& MasterClient
) {
713 printf("I got a OACK as master Client, WRONG!\n");
716 /* ..I now accept packets destined for this MCAST addr, port */
719 printf("Internal failure! no mcast.\n");
725 /* I malloc instead of pre-declare; so that if the file ends
726 * up being too big for this bitmap I can retry
728 Bitmap
= malloc(Mapsize
);
730 printf("No Bitmap, no multicast. Sorry.\n");
734 memset(Bitmap
, 0, Mapsize
);
738 addr
= string_to_ip(mc_adr
);
739 if (Mcast_addr
!= addr
) {
741 eth_mcast_join(Mcast_addr
, 0);
743 if (eth_mcast_join(Mcast_addr
, 1)) {
744 printf("Fail to set mcast, revert to TFTP\n");
750 MasterClient
= (unsigned char)simple_strtoul((char *)mc
, NULL
, 10);
751 Mcast_port
= (unsigned short)simple_strtoul(port
, NULL
, 10);
752 printf("Multicast: %s:%d [%d]\n", mc_adr
, Mcast_port
, MasterClient
);
756 #endif /* Multicast TFTP */