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;
48 static IPaddr_t TftpServerIP;
49 static int TftpServerPort; /* The UDP port at their end */
50 static int TftpOurPort; /* The UDP port at our end */
51 static int TftpTimeoutCount;
52 static ulong TftpBlock; /* packet sequence number */
53 static ulong TftpLastBlock; /* last packet sequence number received */
54 static ulong TftpBlockWrap; /* count of sequence number wraparounds */
55 static ulong TftpBlockWrapOffset; /* memory offset due to wrapping */
57 #ifdef CONFIG_TFTP_TSIZE
58 static int TftpTsize; /* The file size reported by the server */
59 static short TftpNumchars; /* The number of hashes we printed */
64 #define STATE_TOO_LARGE 3
65 #define STATE_BAD_MAGIC 4
68 #define TFTP_BLOCK_SIZE 512 /* default TFTP block size */
69 #define TFTP_SEQUENCE_SIZE ((ulong)(1<<16)) /* sequence number is 16 bit */
71 #define DEFAULT_NAME_LEN (8 + 4 + 1)
72 static char default_filename[DEFAULT_NAME_LEN];
74 #ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
77 #define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
80 static char tftp_filename[MAX_LEN];
82 #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
83 extern flash_info_t flash_info[];
86 /* 512 is poor choice for ethernet, MTU is typically 1500.
87 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
88 * almost-MTU block sizes. At least try... fall back to 512 if need be.
89 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
91 #ifdef CONFIG_TFTP_BLOCKSIZE
92 #define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
94 #define TFTP_MTU_BLOCKSIZE 1468
97 static unsigned short TftpBlkSize=TFTP_BLOCK_SIZE;
98 static unsigned short TftpBlkSizeOption=TFTP_MTU_BLOCKSIZE;
100 #ifdef CONFIG_MCAST_TFTP
102 #define MTFTP_BITMAPSIZE 0x1000
103 static unsigned *Bitmap;
104 static int PrevBitmapHole,Mapsize=MTFTP_BITMAPSIZE;
105 static uchar ProhibitMcast=0, MasterClient=0;
106 static uchar Multicast=0;
107 extern IPaddr_t Mcast_addr;
108 static int Mcast_port;
109 static ulong TftpEndingBlock; /* can get 'last' block before done..*/
111 static void parse_multicast_oack(char *pkt,int len);
116 if (Mcast_addr) eth_mcast_join(Mcast_addr, 0);
117 if (Bitmap) free(Bitmap);
119 Mcast_addr = Multicast = Mcast_port = 0;
120 TftpEndingBlock = -1;
123 #endif /* CONFIG_MCAST_TFTP */
125 static __inline__ void
126 store_block (unsigned block, uchar * src, unsigned len)
128 ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
129 ulong newsize = offset + len;
130 #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
133 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; i++) {
134 /* start address in flash? */
135 if (flash_info[i].flash_id == FLASH_UNKNOWN)
137 if (load_addr + offset >= flash_info[i].start[0]) {
143 if (rc) { /* Flash is destination for this packet */
144 rc = flash_write ((char *)src, (ulong)(load_addr+offset), len);
147 NetState = NETLOOP_FAIL;
152 #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
154 (void)memcpy((void *)(load_addr + offset), src, len);
156 #ifdef CONFIG_MCAST_TFTP
158 ext2_set_bit(block, Bitmap);
161 if (NetBootFileXferSize < newsize)
162 NetBootFileXferSize = newsize;
165 static void TftpSend (void);
166 static void TftpTimeout (void);
168 /**********************************************************************/
173 volatile uchar * pkt;
178 #ifdef CONFIG_MCAST_TFTP
179 /* Multicast TFTP.. non-MasterClients do not ACK data. */
181 && (TftpState == STATE_DATA)
182 && (MasterClient == 0))
186 * We will always be sending some sort of packet, so
187 * cobble together the packet headers now.
189 pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
196 *s++ = htons(TFTP_RRQ);
198 strcpy ((char *)pkt, tftp_filename);
199 pkt += strlen(tftp_filename) + 1;
200 strcpy ((char *)pkt, "octet");
201 pkt += 5 /*strlen("octet")*/ + 1;
202 strcpy ((char *)pkt, "timeout");
203 pkt += 7 /*strlen("timeout")*/ + 1;
204 sprintf((char *)pkt, "%lu", TIMEOUT / 1000);
205 debug("send option \"timeout %s\"\n", (char *)pkt);
206 pkt += strlen((char *)pkt) + 1;
207 #ifdef CONFIG_TFTP_TSIZE
208 memcpy((char *)pkt, "tsize\0000\0", 8);
211 /* try for more effic. blk size */
212 pkt += sprintf((char *)pkt,"blksize%c%d%c",
213 0,TftpBlkSizeOption,0);
214 #ifdef CONFIG_MCAST_TFTP
215 /* Check all preconditions before even trying the option */
217 && (Bitmap=malloc(Mapsize))
218 && eth_get_dev()->mcast) {
221 pkt += sprintf((char *)pkt,"multicast%c%c",0,0);
223 #endif /* CONFIG_MCAST_TFTP */
228 #ifdef CONFIG_MCAST_TFTP
229 /* My turn! Start at where I need blocks I missed.*/
231 TftpBlock=ext2_find_next_zero_bit(Bitmap,(Mapsize*8),0);
237 *s++ = htons(TFTP_ACK);
238 *s++ = htons(TftpBlock);
243 case STATE_TOO_LARGE:
246 *s++ = htons(TFTP_ERROR);
249 strcpy ((char *)pkt, "File too large");
250 pkt += 14 /*strlen("File too large")*/ + 1;
254 case STATE_BAD_MAGIC:
257 *s++ = htons(TFTP_ERROR);
260 strcpy ((char *)pkt, "File has bad magic");
261 pkt += 18 /*strlen("File has bad magic")*/ + 1;
266 NetSendUDPPacket(NetServerEther, TftpServerIP, TftpServerPort, TftpOurPort, len);
271 TftpHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
277 if (dest != TftpOurPort) {
278 #ifdef CONFIG_MCAST_TFTP
280 && (!Mcast_port || (dest != Mcast_port)))
284 if (TftpState != STATE_RRQ && src != TftpServerPort) {
292 /* warning: don't use increment (++) in ntohs() macros!! */
296 switch (ntohs(proto)) {
306 debug("Got OACK: %s %s\n",
308 pkt + strlen((char *)pkt) + 1);
309 TftpState = STATE_OACK;
310 TftpServerPort = src;
312 * Check for 'blksize' option.
313 * Careful: "i" is signed, "len" is unsigned, thus
314 * something like "len-8" may give a *huge* number
316 for (i=0; i+8<len; i++) {
317 if (strcmp ((char*)pkt+i,"blksize") == 0) {
318 TftpBlkSize = (unsigned short)
319 simple_strtoul((char*)pkt+i+8,NULL,10);
320 debug("Blocksize ack: %s, %d\n",
321 (char*)pkt+i+8,TftpBlkSize);
323 #ifdef CONFIG_TFTP_TSIZE
324 if (strcmp ((char*)pkt+i,"tsize") == 0) {
325 TftpTsize = simple_strtoul((char*)pkt+i+6,NULL,10);
326 debug("size = %s, %d\n",
327 (char*)pkt+i+6, TftpTsize);
331 #ifdef CONFIG_MCAST_TFTP
332 parse_multicast_oack((char *)pkt,len-1);
333 if ((Multicast) && (!MasterClient))
334 TftpState = STATE_DATA; /* passive.. */
337 TftpSend (); /* Send ACK */
343 TftpBlock = ntohs(*(ushort *)pkt);
346 * RFC1350 specifies that the first data packet will
347 * have sequence number 1. If we receive a sequence
348 * number of 0 this means that there was a wrap
349 * around of the (16 bit) counter.
351 if (TftpBlock == 0) {
353 TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE;
354 printf ("\n\t %lu MB received\n\t ", TftpBlockWrapOffset>>20);
356 #ifdef CONFIG_TFTP_TSIZE
357 else if (TftpTsize) {
358 while (TftpNumchars < NetBootFileXferSize * 50 / TftpTsize) {
365 if (((TftpBlock - 1) % 10) == 0) {
367 } else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) {
372 if (TftpState == STATE_RRQ)
373 debug("Server did not acknowledge timeout option!\n");
375 if (TftpState == STATE_RRQ || TftpState == STATE_OACK) {
376 /* first block received */
377 TftpState = STATE_DATA;
378 TftpServerPort = src;
381 TftpBlockWrapOffset = 0;
383 #ifdef CONFIG_MCAST_TFTP
384 if (Multicast) { /* start!=1 common if mcast */
385 TftpLastBlock = TftpBlock - 1;
388 if (TftpBlock != 1) { /* Assertion */
389 printf ("\nTFTP error: "
390 "First block is not block 1 (%ld)\n"
391 "Starting again\n\n",
398 if (TftpBlock == TftpLastBlock) {
400 * Same block again; ignore it.
405 TftpLastBlock = TftpBlock;
406 TftpTimeoutMSecs = TIMEOUT;
407 TftpTimeoutCountMax = TIMEOUT_COUNT;
408 NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
410 store_block (TftpBlock - 1, pkt + 2, len);
413 * Acknoledge the block just received, which will prompt
414 * the server for the next one.
416 #ifdef CONFIG_MCAST_TFTP
417 /* if I am the MasterClient, actively calculate what my next
418 * needed block is; else I'm passive; not ACKING
421 if (len < TftpBlkSize) {
422 TftpEndingBlock = TftpBlock;
423 } else if (MasterClient) {
424 TftpBlock = PrevBitmapHole =
425 ext2_find_next_zero_bit(
429 if (TftpBlock > ((Mapsize*8) - 1)) {
430 printf ("tftpfile too big\n");
431 /* try to double it and retry */
437 TftpLastBlock = TftpBlock;
443 #ifdef CONFIG_MCAST_TFTP
445 if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
446 puts ("\nMulticast tftp done\n");
448 NetState = NETLOOP_SUCCESS;
453 if (len < TftpBlkSize) {
455 * We received the whole thing. Try to
458 #ifdef CONFIG_TFTP_TSIZE
459 /* Print out the hash marks for the last packet received */
460 while (TftpTsize && TftpNumchars < 49) {
466 NetState = NETLOOP_SUCCESS;
471 printf ("\nTFTP error: '%s' (%d)\n",
472 pkt + 2, ntohs(*(ushort *)pkt));
473 puts ("Starting again\n\n");
474 #ifdef CONFIG_MCAST_TFTP
486 if (++TftpTimeoutCount > TftpTimeoutCountMax) {
487 puts ("\nRetry count exceeded; starting again\n");
488 #ifdef CONFIG_MCAST_TFTP
494 NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
503 char *ep; /* Environment pointer */
505 /* Allow the user to choose tftpblocksize */
506 if ((ep = getenv("tftpblocksize")) != NULL)
507 TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
508 debug("tftp block size is %i\n", TftpBlkSizeOption);
510 TftpServerIP = NetServerIP;
511 if (BootFile[0] == '\0') {
512 sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
514 (NetOurIP >> 8) & 0xFF,
515 (NetOurIP >> 16) & 0xFF,
516 (NetOurIP >> 24) & 0xFF );
518 strncpy(tftp_filename, default_filename, MAX_LEN);
519 tftp_filename[MAX_LEN-1] = 0;
521 printf ("*** Warning: no boot file name; using '%s'\n",
524 char *p = strchr (BootFile, ':');
527 strncpy(tftp_filename, BootFile, MAX_LEN);
528 tftp_filename[MAX_LEN-1] = 0;
530 TftpServerIP = string_to_ip (BootFile);
531 strncpy(tftp_filename, p + 1, MAX_LEN);
532 tftp_filename[MAX_LEN-1] = 0;
536 #if defined(CONFIG_NET_MULTI)
537 printf ("Using %s device\n", eth_get_name());
539 printf("TFTP from server %pI4"
540 "; our IP address is %pI4", &TftpServerIP, &NetOurIP);
542 /* Check if we need to send across this subnet */
543 if (NetOurGatewayIP && NetOurSubnetMask) {
544 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
545 IPaddr_t ServerNet = TftpServerIP & NetOurSubnetMask;
547 if (OurNet != ServerNet)
548 printf("; sending through gateway %pI4", &NetOurGatewayIP);
552 printf ("Filename '%s'.", tftp_filename);
554 if (NetBootFileSize) {
555 printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
556 print_size (NetBootFileSize<<9, "");
561 printf ("Load address: 0x%lx\n", load_addr);
563 puts ("Loading: *\b");
565 TftpTimeoutMSecs = TftpRRQTimeoutMSecs;
566 TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
568 NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
569 NetSetHandler (TftpHandler);
571 TftpServerPort = WELL_KNOWN_PORT;
572 TftpTimeoutCount = 0;
573 TftpState = STATE_RRQ;
574 /* Use a pseudo-random port unless a specific port is set */
575 TftpOurPort = 1024 + (get_timer(0) % 3072);
577 #ifdef CONFIG_TFTP_PORT
578 if ((ep = getenv("tftpdstp")) != NULL) {
579 TftpServerPort = simple_strtol(ep, NULL, 10);
581 if ((ep = getenv("tftpsrcp")) != NULL) {
582 TftpOurPort= simple_strtol(ep, NULL, 10);
587 /* zero out server ether in case the server ip has changed */
588 memset(NetServerEther, 0, 6);
589 /* Revert TftpBlkSize to dflt */
590 TftpBlkSize = TFTP_BLOCK_SIZE;
591 #ifdef CONFIG_MCAST_TFTP
594 #ifdef CONFIG_TFTP_TSIZE
602 #ifdef CONFIG_MCAST_TFTP
603 /* Credits: atftp project.
606 /* pick up BcastAddr, Port, and whether I am [now] the master-client. *
608 * +-------+-----------+---+-------~~-------+---+
609 * | opc | multicast | 0 | addr, port, mc | 0 |
610 * +-------+-----------+---+-------~~-------+---+
611 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
612 * I am the new master-client so must send ACKs to DataBlocks. If I am not
613 * master-client, I'm a passive client, gathering what DataBlocks I may and
614 * making note of which ones I got in my bitmask.
615 * In theory, I never go from master->passive..
616 * .. this comes in with pkt already pointing just past opc
618 static void parse_multicast_oack(char *pkt, int len)
622 char *mc_adr, *port, *mc;
625 /* march along looking for 'multicast\0', which has to start at least
626 * 14 bytes back from the end.
628 for (i=0;i<len-14;i++)
629 if (strcmp (pkt+i,"multicast") == 0)
631 if (i >= (len-14)) /* non-Multicast OACK, ign. */
634 i+=10; /* strlen multicast */
637 if (*(pkt+i) == ',') {
647 if (!port || !mc_adr || !mc ) return;
648 if (Multicast && MasterClient) {
649 printf ("I got a OACK as master Client, WRONG!\n");
652 /* ..I now accept packets destined for this MCAST addr, port */
655 printf ("Internal failure! no mcast.\n");
661 /* I malloc instead of pre-declare; so that if the file ends
662 * up being too big for this bitmap I can retry
664 if (!(Bitmap = malloc (Mapsize))) {
665 printf ("No Bitmap, no multicast. Sorry.\n");
669 memset (Bitmap,0,Mapsize);
673 addr = string_to_ip(mc_adr);
674 if (Mcast_addr != addr) {
676 eth_mcast_join(Mcast_addr, 0);
677 if (eth_mcast_join(Mcast_addr=addr, 1)) {
678 printf ("Fail to set mcast, revert to TFTP\n");
684 MasterClient = (unsigned char)simple_strtoul((char *)mc,NULL,10);
685 Mcast_port = (unsigned short)simple_strtoul(port,NULL,10);
686 printf ("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
690 #endif /* Multicast TFTP */