2 * Copyright 1994, 1995, 2000 Neil Russell.
4 * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
13 #define WELL_KNOWN_PORT 69 /* Well known TFTP port # */
14 #define TIMEOUT 5000UL /* Millisecs to timeout for lost pkt */
15 #ifndef CONFIG_NET_RETRY_COUNT
16 # define TIMEOUT_COUNT 10 /* # of timeouts before giving up */
18 # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
20 /* (for checking the image size) */
21 #define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
33 static ulong TftpTimeoutMSecs
= TIMEOUT
;
34 static int TftpTimeoutCountMax
= TIMEOUT_COUNT
;
37 * These globals govern the timeout behavior when attempting a connection to a
38 * TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to
39 * wait for the server to respond to initial connection. Second global,
40 * TftpRRQTimeoutCountMax, gives the number of such connection retries.
41 * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be
42 * positive. The globals are meant to be set (and restored) by code needing
43 * non-standard timeout behavior when initiating a TFTP transfer.
45 ulong TftpRRQTimeoutMSecs
= TIMEOUT
;
46 int TftpRRQTimeoutCountMax
= TIMEOUT_COUNT
;
49 TFTP_ERR_UNDEFINED
= 0,
50 TFTP_ERR_FILE_NOT_FOUND
= 1,
51 TFTP_ERR_ACCESS_DENIED
= 2,
52 TFTP_ERR_DISK_FULL
= 3,
53 TFTP_ERR_UNEXPECTED_OPCODE
= 4,
54 TFTP_ERR_UNKNOWN_TRANSFER_ID
= 5,
55 TFTP_ERR_FILE_ALREADY_EXISTS
= 6,
58 static IPaddr_t TftpServerIP
;
59 static int TftpServerPort
; /* The UDP port at their end */
60 static int TftpOurPort
; /* The UDP port at our end */
61 static int TftpTimeoutCount
;
62 static ulong TftpBlock
; /* packet sequence number */
63 static ulong TftpLastBlock
; /* last packet sequence number received */
64 static ulong TftpBlockWrap
; /* count of sequence number wraparounds */
65 static ulong TftpBlockWrapOffset
; /* memory offset due to wrapping */
67 #ifdef CONFIG_TFTP_TSIZE
68 static int TftpTsize
; /* The file size reported by the server */
69 static short TftpNumchars
; /* The number of hashes we printed */
74 #define STATE_TOO_LARGE 3
75 #define STATE_BAD_MAGIC 4
78 #define TFTP_BLOCK_SIZE 512 /* default TFTP block size */
79 #define TFTP_SEQUENCE_SIZE ((ulong)(1<<16)) /* sequence number is 16 bit */
81 #define DEFAULT_NAME_LEN (8 + 4 + 1)
82 static char default_filename
[DEFAULT_NAME_LEN
];
84 #ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
87 #define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
90 static char tftp_filename
[MAX_LEN
];
92 #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
93 extern flash_info_t flash_info
[];
96 /* 512 is poor choice for ethernet, MTU is typically 1500.
97 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
98 * almost-MTU block sizes. At least try... fall back to 512 if need be.
99 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
101 #ifdef CONFIG_TFTP_BLOCKSIZE
102 #define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
104 #define TFTP_MTU_BLOCKSIZE 1468
107 static unsigned short TftpBlkSize
=TFTP_BLOCK_SIZE
;
108 static unsigned short TftpBlkSizeOption
=TFTP_MTU_BLOCKSIZE
;
110 #ifdef CONFIG_MCAST_TFTP
112 #define MTFTP_BITMAPSIZE 0x1000
113 static unsigned *Bitmap
;
114 static int PrevBitmapHole
,Mapsize
=MTFTP_BITMAPSIZE
;
115 static uchar ProhibitMcast
=0, MasterClient
=0;
116 static uchar Multicast
=0;
117 extern IPaddr_t Mcast_addr
;
118 static int Mcast_port
;
119 static ulong TftpEndingBlock
; /* can get 'last' block before done..*/
121 static void parse_multicast_oack(char *pkt
,int len
);
126 if (Mcast_addr
) eth_mcast_join(Mcast_addr
, 0);
127 if (Bitmap
) free(Bitmap
);
129 Mcast_addr
= Multicast
= Mcast_port
= 0;
130 TftpEndingBlock
= -1;
133 #endif /* CONFIG_MCAST_TFTP */
135 static __inline__
void
136 store_block (unsigned block
, uchar
* src
, unsigned len
)
138 ulong offset
= block
* TftpBlkSize
+ TftpBlockWrapOffset
;
139 ulong newsize
= offset
+ len
;
140 #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
143 for (i
=0; i
<CONFIG_SYS_MAX_FLASH_BANKS
; i
++) {
144 /* start address in flash? */
145 if (flash_info
[i
].flash_id
== FLASH_UNKNOWN
)
147 if (load_addr
+ offset
>= flash_info
[i
].start
[0]) {
153 if (rc
) { /* Flash is destination for this packet */
154 rc
= flash_write ((char *)src
, (ulong
)(load_addr
+offset
), len
);
157 NetState
= NETLOOP_FAIL
;
162 #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
164 (void)memcpy((void *)(load_addr
+ offset
), src
, len
);
166 #ifdef CONFIG_MCAST_TFTP
168 ext2_set_bit(block
, Bitmap
);
171 if (NetBootFileXferSize
< newsize
)
172 NetBootFileXferSize
= newsize
;
175 static void TftpSend (void);
176 static void TftpTimeout (void);
178 /**********************************************************************/
183 volatile uchar
* pkt
;
188 #ifdef CONFIG_MCAST_TFTP
189 /* Multicast TFTP.. non-MasterClients do not ACK data. */
191 && (TftpState
== STATE_DATA
)
192 && (MasterClient
== 0))
196 * We will always be sending some sort of packet, so
197 * cobble together the packet headers now.
199 pkt
= NetTxPacket
+ NetEthHdrSize() + IP_HDR_SIZE
;
206 *s
++ = htons(TFTP_RRQ
);
208 strcpy ((char *)pkt
, tftp_filename
);
209 pkt
+= strlen(tftp_filename
) + 1;
210 strcpy ((char *)pkt
, "octet");
211 pkt
+= 5 /*strlen("octet")*/ + 1;
212 strcpy ((char *)pkt
, "timeout");
213 pkt
+= 7 /*strlen("timeout")*/ + 1;
214 sprintf((char *)pkt
, "%lu", TftpTimeoutMSecs
/ 1000);
215 debug("send option \"timeout %s\"\n", (char *)pkt
);
216 pkt
+= strlen((char *)pkt
) + 1;
217 #ifdef CONFIG_TFTP_TSIZE
218 memcpy((char *)pkt
, "tsize\0000\0", 8);
221 /* try for more effic. blk size */
222 pkt
+= sprintf((char *)pkt
,"blksize%c%d%c",
223 0,TftpBlkSizeOption
,0);
224 #ifdef CONFIG_MCAST_TFTP
225 /* Check all preconditions before even trying the option */
227 && (Bitmap
=malloc(Mapsize
))
228 && eth_get_dev()->mcast
) {
231 pkt
+= sprintf((char *)pkt
,"multicast%c%c",0,0);
233 #endif /* CONFIG_MCAST_TFTP */
238 #ifdef CONFIG_MCAST_TFTP
239 /* My turn! Start at where I need blocks I missed.*/
241 TftpBlock
=ext2_find_next_zero_bit(Bitmap
,(Mapsize
*8),0);
247 *s
++ = htons(TFTP_ACK
);
248 *s
++ = htons(TftpBlock
);
253 case STATE_TOO_LARGE
:
256 *s
++ = htons(TFTP_ERROR
);
259 strcpy ((char *)pkt
, "File too large");
260 pkt
+= 14 /*strlen("File too large")*/ + 1;
264 case STATE_BAD_MAGIC
:
267 *s
++ = htons(TFTP_ERROR
);
270 strcpy ((char *)pkt
, "File has bad magic");
271 pkt
+= 18 /*strlen("File has bad magic")*/ + 1;
276 NetSendUDPPacket(NetServerEther
, TftpServerIP
, TftpServerPort
, TftpOurPort
, len
);
281 TftpHandler (uchar
* pkt
, unsigned dest
, unsigned src
, unsigned len
)
287 if (dest
!= TftpOurPort
) {
288 #ifdef CONFIG_MCAST_TFTP
290 && (!Mcast_port
|| (dest
!= Mcast_port
)))
294 if (TftpState
!= STATE_RRQ
&& src
!= TftpServerPort
) {
302 /* warning: don't use increment (++) in ntohs() macros!! */
306 switch (ntohs(proto
)) {
316 debug("Got OACK: %s %s\n",
318 pkt
+ strlen((char *)pkt
) + 1);
319 TftpState
= STATE_OACK
;
320 TftpServerPort
= src
;
322 * Check for 'blksize' option.
323 * Careful: "i" is signed, "len" is unsigned, thus
324 * something like "len-8" may give a *huge* number
326 for (i
=0; i
+8<len
; i
++) {
327 if (strcmp ((char*)pkt
+i
,"blksize") == 0) {
328 TftpBlkSize
= (unsigned short)
329 simple_strtoul((char*)pkt
+i
+8,NULL
,10);
330 debug("Blocksize ack: %s, %d\n",
331 (char*)pkt
+i
+8,TftpBlkSize
);
333 #ifdef CONFIG_TFTP_TSIZE
334 if (strcmp ((char*)pkt
+i
,"tsize") == 0) {
335 TftpTsize
= simple_strtoul((char*)pkt
+i
+6,NULL
,10);
336 debug("size = %s, %d\n",
337 (char*)pkt
+i
+6, TftpTsize
);
341 #ifdef CONFIG_MCAST_TFTP
342 parse_multicast_oack((char *)pkt
,len
-1);
343 if ((Multicast
) && (!MasterClient
))
344 TftpState
= STATE_DATA
; /* passive.. */
347 TftpSend (); /* Send ACK */
353 TftpBlock
= ntohs(*(ushort
*)pkt
);
356 * RFC1350 specifies that the first data packet will
357 * have sequence number 1. If we receive a sequence
358 * number of 0 this means that there was a wrap
359 * around of the (16 bit) counter.
361 if (TftpBlock
== 0) {
363 TftpBlockWrapOffset
+= TftpBlkSize
* TFTP_SEQUENCE_SIZE
;
364 printf ("\n\t %lu MB received\n\t ", TftpBlockWrapOffset
>>20);
366 #ifdef CONFIG_TFTP_TSIZE
367 else if (TftpTsize
) {
368 while (TftpNumchars
< NetBootFileXferSize
* 50 / TftpTsize
) {
375 if (((TftpBlock
- 1) % 10) == 0) {
377 } else if ((TftpBlock
% (10 * HASHES_PER_LINE
)) == 0) {
382 if (TftpState
== STATE_RRQ
)
383 debug("Server did not acknowledge timeout option!\n");
385 if (TftpState
== STATE_RRQ
|| TftpState
== STATE_OACK
) {
386 /* first block received */
387 TftpState
= STATE_DATA
;
388 TftpServerPort
= src
;
391 TftpBlockWrapOffset
= 0;
393 #ifdef CONFIG_MCAST_TFTP
394 if (Multicast
) { /* start!=1 common if mcast */
395 TftpLastBlock
= TftpBlock
- 1;
398 if (TftpBlock
!= 1) { /* Assertion */
399 printf ("\nTFTP error: "
400 "First block is not block 1 (%ld)\n"
401 "Starting again\n\n",
408 if (TftpBlock
== TftpLastBlock
) {
410 * Same block again; ignore it.
415 TftpLastBlock
= TftpBlock
;
416 TftpTimeoutCountMax
= TIMEOUT_COUNT
;
417 NetSetTimeout (TftpTimeoutMSecs
, TftpTimeout
);
419 store_block (TftpBlock
- 1, pkt
+ 2, len
);
422 * Acknoledge the block just received, which will prompt
423 * the server for the next one.
425 #ifdef CONFIG_MCAST_TFTP
426 /* if I am the MasterClient, actively calculate what my next
427 * needed block is; else I'm passive; not ACKING
430 if (len
< TftpBlkSize
) {
431 TftpEndingBlock
= TftpBlock
;
432 } else if (MasterClient
) {
433 TftpBlock
= PrevBitmapHole
=
434 ext2_find_next_zero_bit(
438 if (TftpBlock
> ((Mapsize
*8) - 1)) {
439 printf ("tftpfile too big\n");
440 /* try to double it and retry */
446 TftpLastBlock
= TftpBlock
;
452 #ifdef CONFIG_MCAST_TFTP
454 if (MasterClient
&& (TftpBlock
>= TftpEndingBlock
)) {
455 puts ("\nMulticast tftp done\n");
457 NetState
= NETLOOP_SUCCESS
;
462 if (len
< TftpBlkSize
) {
464 * We received the whole thing. Try to
467 #ifdef CONFIG_TFTP_TSIZE
468 /* Print out the hash marks for the last packet received */
469 while (TftpTsize
&& TftpNumchars
< 49) {
475 NetState
= NETLOOP_SUCCESS
;
480 printf ("\nTFTP error: '%s' (%d)\n",
481 pkt
+ 2, ntohs(*(ushort
*)pkt
));
483 switch (ntohs(*(ushort
*)pkt
)) {
484 case TFTP_ERR_FILE_NOT_FOUND
:
485 case TFTP_ERR_ACCESS_DENIED
:
486 puts("Not retrying...\n");
488 NetState
= NETLOOP_FAIL
;
490 case TFTP_ERR_UNDEFINED
:
491 case TFTP_ERR_DISK_FULL
:
492 case TFTP_ERR_UNEXPECTED_OPCODE
:
493 case TFTP_ERR_UNKNOWN_TRANSFER_ID
:
494 case TFTP_ERR_FILE_ALREADY_EXISTS
:
496 puts("Starting again\n\n");
497 #ifdef CONFIG_MCAST_TFTP
511 if (++TftpTimeoutCount
> TftpTimeoutCountMax
) {
512 puts ("\nRetry count exceeded; starting again\n");
513 #ifdef CONFIG_MCAST_TFTP
519 NetSetTimeout (TftpTimeoutMSecs
, TftpTimeout
);
528 char *ep
; /* Environment pointer */
531 * Allow the user to choose TFTP blocksize and timeout.
532 * TFTP protocol has a minimal timeout of 1 second.
534 if ((ep
= getenv("tftpblocksize")) != NULL
)
535 TftpBlkSizeOption
= simple_strtol(ep
, NULL
, 10);
537 if ((ep
= getenv("tftptimeout")) != NULL
)
538 TftpTimeoutMSecs
= simple_strtol(ep
, NULL
, 10);
540 if (TftpTimeoutMSecs
< 1000) {
541 printf("TFTP timeout (%ld ms) too low, "
542 "set minimum = 1000 ms\n",
544 TftpTimeoutMSecs
= 1000;
547 debug("TFTP blocksize = %i, timeout = %ld ms\n",
548 TftpBlkSizeOption
, TftpTimeoutMSecs
);
550 TftpServerIP
= NetServerIP
;
551 if (BootFile
[0] == '\0') {
552 sprintf(default_filename
, "%02lX%02lX%02lX%02lX.img",
554 (NetOurIP
>> 8) & 0xFF,
555 (NetOurIP
>> 16) & 0xFF,
556 (NetOurIP
>> 24) & 0xFF );
558 strncpy(tftp_filename
, default_filename
, MAX_LEN
);
559 tftp_filename
[MAX_LEN
-1] = 0;
561 printf ("*** Warning: no boot file name; using '%s'\n",
564 char *p
= strchr (BootFile
, ':');
567 strncpy(tftp_filename
, BootFile
, MAX_LEN
);
568 tftp_filename
[MAX_LEN
-1] = 0;
570 TftpServerIP
= string_to_ip (BootFile
);
571 strncpy(tftp_filename
, p
+ 1, MAX_LEN
);
572 tftp_filename
[MAX_LEN
-1] = 0;
576 #if defined(CONFIG_NET_MULTI)
577 printf ("Using %s device\n", eth_get_name());
579 printf("TFTP from server %pI4"
580 "; our IP address is %pI4", &TftpServerIP
, &NetOurIP
);
582 /* Check if we need to send across this subnet */
583 if (NetOurGatewayIP
&& NetOurSubnetMask
) {
584 IPaddr_t OurNet
= NetOurIP
& NetOurSubnetMask
;
585 IPaddr_t ServerNet
= TftpServerIP
& NetOurSubnetMask
;
587 if (OurNet
!= ServerNet
)
588 printf("; sending through gateway %pI4", &NetOurGatewayIP
);
592 printf ("Filename '%s'.", tftp_filename
);
594 if (NetBootFileSize
) {
595 printf (" Size is 0x%x Bytes = ", NetBootFileSize
<<9);
596 print_size (NetBootFileSize
<<9, "");
601 printf ("Load address: 0x%lx\n", load_addr
);
603 puts ("Loading: *\b");
605 TftpTimeoutCountMax
= TftpRRQTimeoutCountMax
;
607 NetSetTimeout (TftpTimeoutMSecs
, TftpTimeout
);
608 NetSetHandler (TftpHandler
);
610 TftpServerPort
= WELL_KNOWN_PORT
;
611 TftpTimeoutCount
= 0;
612 TftpState
= STATE_RRQ
;
613 /* Use a pseudo-random port unless a specific port is set */
614 TftpOurPort
= 1024 + (get_timer(0) % 3072);
616 #ifdef CONFIG_TFTP_PORT
617 if ((ep
= getenv("tftpdstp")) != NULL
) {
618 TftpServerPort
= simple_strtol(ep
, NULL
, 10);
620 if ((ep
= getenv("tftpsrcp")) != NULL
) {
621 TftpOurPort
= simple_strtol(ep
, NULL
, 10);
626 /* zero out server ether in case the server ip has changed */
627 memset(NetServerEther
, 0, 6);
628 /* Revert TftpBlkSize to dflt */
629 TftpBlkSize
= TFTP_BLOCK_SIZE
;
630 #ifdef CONFIG_MCAST_TFTP
633 #ifdef CONFIG_TFTP_TSIZE
641 #ifdef CONFIG_MCAST_TFTP
642 /* Credits: atftp project.
645 /* pick up BcastAddr, Port, and whether I am [now] the master-client. *
647 * +-------+-----------+---+-------~~-------+---+
648 * | opc | multicast | 0 | addr, port, mc | 0 |
649 * +-------+-----------+---+-------~~-------+---+
650 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
651 * I am the new master-client so must send ACKs to DataBlocks. If I am not
652 * master-client, I'm a passive client, gathering what DataBlocks I may and
653 * making note of which ones I got in my bitmask.
654 * In theory, I never go from master->passive..
655 * .. this comes in with pkt already pointing just past opc
657 static void parse_multicast_oack(char *pkt
, int len
)
661 char *mc_adr
, *port
, *mc
;
664 /* march along looking for 'multicast\0', which has to start at least
665 * 14 bytes back from the end.
667 for (i
=0;i
<len
-14;i
++)
668 if (strcmp (pkt
+i
,"multicast") == 0)
670 if (i
>= (len
-14)) /* non-Multicast OACK, ign. */
673 i
+=10; /* strlen multicast */
676 if (*(pkt
+i
) == ',') {
686 if (!port
|| !mc_adr
|| !mc
) return;
687 if (Multicast
&& MasterClient
) {
688 printf ("I got a OACK as master Client, WRONG!\n");
691 /* ..I now accept packets destined for this MCAST addr, port */
694 printf ("Internal failure! no mcast.\n");
700 /* I malloc instead of pre-declare; so that if the file ends
701 * up being too big for this bitmap I can retry
703 if (!(Bitmap
= malloc (Mapsize
))) {
704 printf ("No Bitmap, no multicast. Sorry.\n");
708 memset (Bitmap
,0,Mapsize
);
712 addr
= string_to_ip(mc_adr
);
713 if (Mcast_addr
!= addr
) {
715 eth_mcast_join(Mcast_addr
, 0);
716 if (eth_mcast_join(Mcast_addr
=addr
, 1)) {
717 printf ("Fail to set mcast, revert to TFTP\n");
723 MasterClient
= (unsigned char)simple_strtoul((char *)mc
,NULL
,10);
724 Mcast_port
= (unsigned short)simple_strtoul(port
,NULL
,10);
725 printf ("Multicast: %s:%d [%d]\n", mc_adr
, Mcast_port
, MasterClient
);
729 #endif /* Multicast TFTP */