2 * Copyright 1994, 1995, 2000 Neil Russell.
4 * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
13 #if defined(CONFIG_CMD_NET)
15 #define WELL_KNOWN_PORT 69 /* Well known TFTP port # */
16 #define TIMEOUT 5000UL /* Millisecs to timeout for lost pkt */
17 #ifndef CONFIG_NET_RETRY_COUNT
18 # define TIMEOUT_COUNT 10 /* # of timeouts before giving up */
20 # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
22 /* (for checking the image size) */
23 #define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
35 static ulong TftpTimeoutMSecs
= TIMEOUT
;
36 static int TftpTimeoutCountMax
= TIMEOUT_COUNT
;
39 * These globals govern the timeout behavior when attempting a connection to a
40 * TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to
41 * wait for the server to respond to initial connection. Second global,
42 * TftpRRQTimeoutCountMax, gives the number of such connection retries.
43 * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be
44 * positive. The globals are meant to be set (and restored) by code needing
45 * non-standard timeout behavior when initiating a TFTP transfer.
47 ulong TftpRRQTimeoutMSecs
= TIMEOUT
;
48 int TftpRRQTimeoutCountMax
= TIMEOUT_COUNT
;
50 static IPaddr_t TftpServerIP
;
51 static int TftpServerPort
; /* The UDP port at their end */
52 static int TftpOurPort
; /* The UDP port at our end */
53 static int TftpTimeoutCount
;
54 static ulong TftpBlock
; /* packet sequence number */
55 static ulong TftpLastBlock
; /* last packet sequence number received */
56 static ulong TftpBlockWrap
; /* count of sequence number wraparounds */
57 static ulong TftpBlockWrapOffset
; /* memory offset due to wrapping */
62 #define STATE_TOO_LARGE 3
63 #define STATE_BAD_MAGIC 4
66 #define TFTP_BLOCK_SIZE 512 /* default TFTP block size */
67 #define TFTP_SEQUENCE_SIZE ((ulong)(1<<16)) /* sequence number is 16 bit */
69 #define DEFAULT_NAME_LEN (8 + 4 + 1)
70 static char default_filename
[DEFAULT_NAME_LEN
];
72 #ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
75 #define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
78 static char tftp_filename
[MAX_LEN
];
80 #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
81 extern flash_info_t flash_info
[];
84 /* 512 is poor choice for ethernet, MTU is typically 1500.
85 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
86 * almost-MTU block sizes. At least try... fall back to 512 if need be.
88 #define TFTP_MTU_BLOCKSIZE 1468
89 static unsigned short TftpBlkSize
=TFTP_BLOCK_SIZE
;
90 static unsigned short TftpBlkSizeOption
=TFTP_MTU_BLOCKSIZE
;
92 #ifdef CONFIG_MCAST_TFTP
94 #define MTFTP_BITMAPSIZE 0x1000
95 static unsigned *Bitmap
;
96 static int PrevBitmapHole
,Mapsize
=MTFTP_BITMAPSIZE
;
97 static uchar ProhibitMcast
=0, MasterClient
=0;
98 static uchar Multicast
=0;
99 extern IPaddr_t Mcast_addr
;
100 static int Mcast_port
;
101 static ulong TftpEndingBlock
; /* can get 'last' block before done..*/
103 static void parse_multicast_oack(char *pkt
,int len
);
108 if (Mcast_addr
) eth_mcast_join(Mcast_addr
, 0);
109 if (Bitmap
) free(Bitmap
);
111 Mcast_addr
= Multicast
= Mcast_port
= 0;
112 TftpEndingBlock
= -1;
115 #endif /* CONFIG_MCAST_TFTP */
117 static __inline__
void
118 store_block (unsigned block
, uchar
* src
, unsigned len
)
120 ulong offset
= block
* TftpBlkSize
+ TftpBlockWrapOffset
;
121 ulong newsize
= offset
+ len
;
122 #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
125 for (i
=0; i
<CONFIG_SYS_MAX_FLASH_BANKS
; i
++) {
126 /* start address in flash? */
127 if (flash_info
[i
].flash_id
== FLASH_UNKNOWN
)
129 if (load_addr
+ offset
>= flash_info
[i
].start
[0]) {
135 if (rc
) { /* Flash is destination for this packet */
136 rc
= flash_write ((char *)src
, (ulong
)(load_addr
+offset
), len
);
139 NetState
= NETLOOP_FAIL
;
144 #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
146 (void)memcpy((void *)(load_addr
+ offset
), src
, len
);
148 #ifdef CONFIG_MCAST_TFTP
150 ext2_set_bit(block
, Bitmap
);
153 if (NetBootFileXferSize
< newsize
)
154 NetBootFileXferSize
= newsize
;
157 static void TftpSend (void);
158 static void TftpTimeout (void);
160 /**********************************************************************/
165 volatile uchar
* pkt
;
170 #ifdef CONFIG_MCAST_TFTP
171 /* Multicast TFTP.. non-MasterClients do not ACK data. */
173 && (TftpState
== STATE_DATA
)
174 && (MasterClient
== 0))
178 * We will always be sending some sort of packet, so
179 * cobble together the packet headers now.
181 pkt
= NetTxPacket
+ NetEthHdrSize() + IP_HDR_SIZE
;
188 *s
++ = htons(TFTP_RRQ
);
190 strcpy ((char *)pkt
, tftp_filename
);
191 pkt
+= strlen(tftp_filename
) + 1;
192 strcpy ((char *)pkt
, "octet");
193 pkt
+= 5 /*strlen("octet")*/ + 1;
194 strcpy ((char *)pkt
, "timeout");
195 pkt
+= 7 /*strlen("timeout")*/ + 1;
196 sprintf((char *)pkt
, "%lu", TIMEOUT
/ 1000);
197 debug("send option \"timeout %s\"\n", (char *)pkt
);
198 pkt
+= strlen((char *)pkt
) + 1;
199 /* try for more effic. blk size */
200 pkt
+= sprintf((char *)pkt
,"blksize%c%d%c",
201 0,TftpBlkSizeOption
,0);
202 #ifdef CONFIG_MCAST_TFTP
203 /* Check all preconditions before even trying the option */
205 && (Bitmap
=malloc(Mapsize
))
206 && eth_get_dev()->mcast
) {
209 pkt
+= sprintf((char *)pkt
,"multicast%c%c",0,0);
211 #endif /* CONFIG_MCAST_TFTP */
216 #ifdef CONFIG_MCAST_TFTP
217 /* My turn! Start at where I need blocks I missed.*/
219 TftpBlock
=ext2_find_next_zero_bit(Bitmap
,(Mapsize
*8),0);
225 *s
++ = htons(TFTP_ACK
);
226 *s
++ = htons(TftpBlock
);
231 case STATE_TOO_LARGE
:
234 *s
++ = htons(TFTP_ERROR
);
237 strcpy ((char *)pkt
, "File too large");
238 pkt
+= 14 /*strlen("File too large")*/ + 1;
242 case STATE_BAD_MAGIC
:
245 *s
++ = htons(TFTP_ERROR
);
248 strcpy ((char *)pkt
, "File has bad magic");
249 pkt
+= 18 /*strlen("File has bad magic")*/ + 1;
254 NetSendUDPPacket(NetServerEther
, TftpServerIP
, TftpServerPort
, TftpOurPort
, len
);
259 TftpHandler (uchar
* pkt
, unsigned dest
, unsigned src
, unsigned len
)
265 if (dest
!= TftpOurPort
) {
266 #ifdef CONFIG_MCAST_TFTP
268 && (!Mcast_port
|| (dest
!= Mcast_port
)))
272 if (TftpState
!= STATE_RRQ
&& src
!= TftpServerPort
) {
280 /* warning: don't use increment (++) in ntohs() macros!! */
284 switch (ntohs(proto
)) {
294 debug("Got OACK: %s %s\n",
296 pkt
+ strlen((char *)pkt
) + 1);
297 TftpState
= STATE_OACK
;
298 TftpServerPort
= src
;
300 * Check for 'blksize' option.
301 * Careful: "i" is signed, "len" is unsigned, thus
302 * something like "len-8" may give a *huge* number
304 for (i
=0; i
+8<len
; i
++) {
305 if (strcmp ((char*)pkt
+i
,"blksize") == 0) {
306 TftpBlkSize
= (unsigned short)
307 simple_strtoul((char*)pkt
+i
+8,NULL
,10);
308 debug("Blocksize ack: %s, %d\n",
309 (char*)pkt
+i
+8,TftpBlkSize
);
313 #ifdef CONFIG_MCAST_TFTP
314 parse_multicast_oack((char *)pkt
,len
-1);
315 if ((Multicast
) && (!MasterClient
))
316 TftpState
= STATE_DATA
; /* passive.. */
319 TftpSend (); /* Send ACK */
325 TftpBlock
= ntohs(*(ushort
*)pkt
);
328 * RFC1350 specifies that the first data packet will
329 * have sequence number 1. If we receive a sequence
330 * number of 0 this means that there was a wrap
331 * around of the (16 bit) counter.
333 if (TftpBlock
== 0) {
335 TftpBlockWrapOffset
+= TftpBlkSize
* TFTP_SEQUENCE_SIZE
;
336 printf ("\n\t %lu MB received\n\t ", TftpBlockWrapOffset
>>20);
338 if (((TftpBlock
- 1) % 10) == 0) {
340 } else if ((TftpBlock
% (10 * HASHES_PER_LINE
)) == 0) {
345 if (TftpState
== STATE_RRQ
)
346 debug("Server did not acknowledge timeout option!\n");
348 if (TftpState
== STATE_RRQ
|| TftpState
== STATE_OACK
) {
349 /* first block received */
350 TftpState
= STATE_DATA
;
351 TftpServerPort
= src
;
354 TftpBlockWrapOffset
= 0;
356 #ifdef CONFIG_MCAST_TFTP
357 if (Multicast
) { /* start!=1 common if mcast */
358 TftpLastBlock
= TftpBlock
- 1;
361 if (TftpBlock
!= 1) { /* Assertion */
362 printf ("\nTFTP error: "
363 "First block is not block 1 (%ld)\n"
364 "Starting again\n\n",
371 if (TftpBlock
== TftpLastBlock
) {
373 * Same block again; ignore it.
378 TftpLastBlock
= TftpBlock
;
379 TftpTimeoutMSecs
= TIMEOUT
;
380 TftpTimeoutCountMax
= TIMEOUT_COUNT
;
381 NetSetTimeout (TftpTimeoutMSecs
, TftpTimeout
);
383 store_block (TftpBlock
- 1, pkt
+ 2, len
);
386 * Acknoledge the block just received, which will prompt
387 * the server for the next one.
389 #ifdef CONFIG_MCAST_TFTP
390 /* if I am the MasterClient, actively calculate what my next
391 * needed block is; else I'm passive; not ACKING
394 if (len
< TftpBlkSize
) {
395 TftpEndingBlock
= TftpBlock
;
396 } else if (MasterClient
) {
397 TftpBlock
= PrevBitmapHole
=
398 ext2_find_next_zero_bit(
402 if (TftpBlock
> ((Mapsize
*8) - 1)) {
403 printf ("tftpfile too big\n");
404 /* try to double it and retry */
410 TftpLastBlock
= TftpBlock
;
416 #ifdef CONFIG_MCAST_TFTP
418 if (MasterClient
&& (TftpBlock
>= TftpEndingBlock
)) {
419 puts ("\nMulticast tftp done\n");
421 NetState
= NETLOOP_SUCCESS
;
426 if (len
< TftpBlkSize
) {
428 * We received the whole thing. Try to
432 NetState
= NETLOOP_SUCCESS
;
437 printf ("\nTFTP error: '%s' (%d)\n",
438 pkt
+ 2, ntohs(*(ushort
*)pkt
));
439 puts ("Starting again\n\n");
440 #ifdef CONFIG_MCAST_TFTP
452 if (++TftpTimeoutCount
> TftpTimeoutCountMax
) {
453 puts ("\nRetry count exceeded; starting again\n");
454 #ifdef CONFIG_MCAST_TFTP
460 NetSetTimeout (TftpTimeoutMSecs
, TftpTimeout
);
469 #ifdef CONFIG_TFTP_PORT
470 char *ep
; /* Environment pointer */
473 TftpServerIP
= NetServerIP
;
474 if (BootFile
[0] == '\0') {
475 sprintf(default_filename
, "%02lX%02lX%02lX%02lX.img",
477 (NetOurIP
>> 8) & 0xFF,
478 (NetOurIP
>> 16) & 0xFF,
479 (NetOurIP
>> 24) & 0xFF );
481 strncpy(tftp_filename
, default_filename
, MAX_LEN
);
482 tftp_filename
[MAX_LEN
-1] = 0;
484 printf ("*** Warning: no boot file name; using '%s'\n",
487 char *p
= strchr (BootFile
, ':');
490 strncpy(tftp_filename
, BootFile
, MAX_LEN
);
491 tftp_filename
[MAX_LEN
-1] = 0;
493 TftpServerIP
= string_to_ip (BootFile
);
494 strncpy(tftp_filename
, p
+ 1, MAX_LEN
);
495 tftp_filename
[MAX_LEN
-1] = 0;
499 #if defined(CONFIG_NET_MULTI)
500 printf ("Using %s device\n", eth_get_name());
502 printf("TFTP from server %pI4"
503 "; our IP address is %pI4", &TftpServerIP
, &NetOurIP
);
505 /* Check if we need to send across this subnet */
506 if (NetOurGatewayIP
&& NetOurSubnetMask
) {
507 IPaddr_t OurNet
= NetOurIP
& NetOurSubnetMask
;
508 IPaddr_t ServerNet
= TftpServerIP
& NetOurSubnetMask
;
510 if (OurNet
!= ServerNet
)
511 printf("; sending through gateway %pI4", &NetOurGatewayIP
);
515 printf ("Filename '%s'.", tftp_filename
);
517 if (NetBootFileSize
) {
518 printf (" Size is 0x%x Bytes = ", NetBootFileSize
<<9);
519 print_size (NetBootFileSize
<<9, "");
524 printf ("Load address: 0x%lx\n", load_addr
);
526 puts ("Loading: *\b");
528 TftpTimeoutMSecs
= TftpRRQTimeoutMSecs
;
529 TftpTimeoutCountMax
= TftpRRQTimeoutCountMax
;
531 NetSetTimeout (TftpTimeoutMSecs
, TftpTimeout
);
532 NetSetHandler (TftpHandler
);
534 TftpServerPort
= WELL_KNOWN_PORT
;
535 TftpTimeoutCount
= 0;
536 TftpState
= STATE_RRQ
;
537 /* Use a pseudo-random port unless a specific port is set */
538 TftpOurPort
= 1024 + (get_timer(0) % 3072);
540 #ifdef CONFIG_TFTP_PORT
541 if ((ep
= getenv("tftpdstp")) != NULL
) {
542 TftpServerPort
= simple_strtol(ep
, NULL
, 10);
544 if ((ep
= getenv("tftpsrcp")) != NULL
) {
545 TftpOurPort
= simple_strtol(ep
, NULL
, 10);
550 /* zero out server ether in case the server ip has changed */
551 memset(NetServerEther
, 0, 6);
552 /* Revert TftpBlkSize to dflt */
553 TftpBlkSize
= TFTP_BLOCK_SIZE
;
554 #ifdef CONFIG_MCAST_TFTP
561 #ifdef CONFIG_MCAST_TFTP
562 /* Credits: atftp project.
565 /* pick up BcastAddr, Port, and whether I am [now] the master-client. *
567 * +-------+-----------+---+-------~~-------+---+
568 * | opc | multicast | 0 | addr, port, mc | 0 |
569 * +-------+-----------+---+-------~~-------+---+
570 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
571 * I am the new master-client so must send ACKs to DataBlocks. If I am not
572 * master-client, I'm a passive client, gathering what DataBlocks I may and
573 * making note of which ones I got in my bitmask.
574 * In theory, I never go from master->passive..
575 * .. this comes in with pkt already pointing just past opc
577 static void parse_multicast_oack(char *pkt
, int len
)
581 char *mc_adr
, *port
, *mc
;
584 /* march along looking for 'multicast\0', which has to start at least
585 * 14 bytes back from the end.
587 for (i
=0;i
<len
-14;i
++)
588 if (strcmp (pkt
+i
,"multicast") == 0)
590 if (i
>= (len
-14)) /* non-Multicast OACK, ign. */
593 i
+=10; /* strlen multicast */
596 if (*(pkt
+i
) == ',') {
606 if (!port
|| !mc_adr
|| !mc
) return;
607 if (Multicast
&& MasterClient
) {
608 printf ("I got a OACK as master Client, WRONG!\n");
611 /* ..I now accept packets destined for this MCAST addr, port */
614 printf ("Internal failure! no mcast.\n");
620 /* I malloc instead of pre-declare; so that if the file ends
621 * up being too big for this bitmap I can retry
623 if (!(Bitmap
= malloc (Mapsize
))) {
624 printf ("No Bitmap, no multicast. Sorry.\n");
628 memset (Bitmap
,0,Mapsize
);
632 addr
= string_to_ip(mc_adr
);
633 if (Mcast_addr
!= addr
) {
635 eth_mcast_join(Mcast_addr
, 0);
636 if (eth_mcast_join(Mcast_addr
=addr
, 1)) {
637 printf ("Fail to set mcast, revert to TFTP\n");
643 MasterClient
= (unsigned char)simple_strtoul((char *)mc
,NULL
,10);
644 Mcast_port
= (unsigned short)simple_strtoul(port
,NULL
,10);
645 printf ("Multicast: %s:%d [%d]\n", mc_adr
, Mcast_port
, MasterClient
);
649 #endif /* Multicast TFTP */