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 */
59 #ifdef CONFIG_TFTP_TSIZE
60 static int TftpTsize; /* The file size reported by the server */
61 static short TftpNumchars; /* The number of hashes we printed */
66 #define STATE_TOO_LARGE 3
67 #define STATE_BAD_MAGIC 4
70 #define TFTP_BLOCK_SIZE 512 /* default TFTP block size */
71 #define TFTP_SEQUENCE_SIZE ((ulong)(1<<16)) /* sequence number is 16 bit */
73 #define DEFAULT_NAME_LEN (8 + 4 + 1)
74 static char default_filename[DEFAULT_NAME_LEN];
76 #ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
79 #define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
82 static char tftp_filename[MAX_LEN];
84 #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
85 extern flash_info_t flash_info[];
88 /* 512 is poor choice for ethernet, MTU is typically 1500.
89 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
90 * almost-MTU block sizes. At least try... fall back to 512 if need be.
91 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
93 #ifdef CONFIG_TFTP_BLOCKSIZE
94 #define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
96 #define TFTP_MTU_BLOCKSIZE 1468
99 static unsigned short TftpBlkSize=TFTP_BLOCK_SIZE;
100 static unsigned short TftpBlkSizeOption=TFTP_MTU_BLOCKSIZE;
102 #ifdef CONFIG_MCAST_TFTP
104 #define MTFTP_BITMAPSIZE 0x1000
105 static unsigned *Bitmap;
106 static int PrevBitmapHole,Mapsize=MTFTP_BITMAPSIZE;
107 static uchar ProhibitMcast=0, MasterClient=0;
108 static uchar Multicast=0;
109 extern IPaddr_t Mcast_addr;
110 static int Mcast_port;
111 static ulong TftpEndingBlock; /* can get 'last' block before done..*/
113 static void parse_multicast_oack(char *pkt,int len);
118 if (Mcast_addr) eth_mcast_join(Mcast_addr, 0);
119 if (Bitmap) free(Bitmap);
121 Mcast_addr = Multicast = Mcast_port = 0;
122 TftpEndingBlock = -1;
125 #endif /* CONFIG_MCAST_TFTP */
127 static __inline__ void
128 store_block (unsigned block, uchar * src, unsigned len)
130 ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
131 ulong newsize = offset + len;
132 #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
135 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; i++) {
136 /* start address in flash? */
137 if (flash_info[i].flash_id == FLASH_UNKNOWN)
139 if (load_addr + offset >= flash_info[i].start[0]) {
145 if (rc) { /* Flash is destination for this packet */
146 rc = flash_write ((char *)src, (ulong)(load_addr+offset), len);
149 NetState = NETLOOP_FAIL;
154 #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
156 (void)memcpy((void *)(load_addr + offset), src, len);
158 #ifdef CONFIG_MCAST_TFTP
160 ext2_set_bit(block, Bitmap);
163 if (NetBootFileXferSize < newsize)
164 NetBootFileXferSize = newsize;
167 static void TftpSend (void);
168 static void TftpTimeout (void);
170 /**********************************************************************/
175 volatile uchar * pkt;
180 #ifdef CONFIG_MCAST_TFTP
181 /* Multicast TFTP.. non-MasterClients do not ACK data. */
183 && (TftpState == STATE_DATA)
184 && (MasterClient == 0))
188 * We will always be sending some sort of packet, so
189 * cobble together the packet headers now.
191 pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
198 *s++ = htons(TFTP_RRQ);
200 strcpy ((char *)pkt, tftp_filename);
201 pkt += strlen(tftp_filename) + 1;
202 strcpy ((char *)pkt, "octet");
203 pkt += 5 /*strlen("octet")*/ + 1;
204 strcpy ((char *)pkt, "timeout");
205 pkt += 7 /*strlen("timeout")*/ + 1;
206 sprintf((char *)pkt, "%lu", TIMEOUT / 1000);
207 debug("send option \"timeout %s\"\n", (char *)pkt);
208 pkt += strlen((char *)pkt) + 1;
209 #ifdef CONFIG_TFTP_TSIZE
210 memcpy((char *)pkt, "tsize\0000\0", 8);
213 /* try for more effic. blk size */
214 pkt += sprintf((char *)pkt,"blksize%c%d%c",
215 0,TftpBlkSizeOption,0);
216 #ifdef CONFIG_MCAST_TFTP
217 /* Check all preconditions before even trying the option */
219 && (Bitmap=malloc(Mapsize))
220 && eth_get_dev()->mcast) {
223 pkt += sprintf((char *)pkt,"multicast%c%c",0,0);
225 #endif /* CONFIG_MCAST_TFTP */
230 #ifdef CONFIG_MCAST_TFTP
231 /* My turn! Start at where I need blocks I missed.*/
233 TftpBlock=ext2_find_next_zero_bit(Bitmap,(Mapsize*8),0);
239 *s++ = htons(TFTP_ACK);
240 *s++ = htons(TftpBlock);
245 case STATE_TOO_LARGE:
248 *s++ = htons(TFTP_ERROR);
251 strcpy ((char *)pkt, "File too large");
252 pkt += 14 /*strlen("File too large")*/ + 1;
256 case STATE_BAD_MAGIC:
259 *s++ = htons(TFTP_ERROR);
262 strcpy ((char *)pkt, "File has bad magic");
263 pkt += 18 /*strlen("File has bad magic")*/ + 1;
268 NetSendUDPPacket(NetServerEther, TftpServerIP, TftpServerPort, TftpOurPort, len);
273 TftpHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
279 if (dest != TftpOurPort) {
280 #ifdef CONFIG_MCAST_TFTP
282 && (!Mcast_port || (dest != Mcast_port)))
286 if (TftpState != STATE_RRQ && src != TftpServerPort) {
294 /* warning: don't use increment (++) in ntohs() macros!! */
298 switch (ntohs(proto)) {
308 debug("Got OACK: %s %s\n",
310 pkt + strlen((char *)pkt) + 1);
311 TftpState = STATE_OACK;
312 TftpServerPort = src;
314 * Check for 'blksize' option.
315 * Careful: "i" is signed, "len" is unsigned, thus
316 * something like "len-8" may give a *huge* number
318 for (i=0; i+8<len; i++) {
319 if (strcmp ((char*)pkt+i,"blksize") == 0) {
320 TftpBlkSize = (unsigned short)
321 simple_strtoul((char*)pkt+i+8,NULL,10);
322 debug("Blocksize ack: %s, %d\n",
323 (char*)pkt+i+8,TftpBlkSize);
325 #ifdef CONFIG_TFTP_TSIZE
326 if (strcmp ((char*)pkt+i,"tsize") == 0) {
327 TftpTsize = simple_strtoul((char*)pkt+i+6,NULL,10);
328 debug("size = %s, %d\n",
329 (char*)pkt+i+6, TftpTsize);
333 #ifdef CONFIG_MCAST_TFTP
334 parse_multicast_oack((char *)pkt,len-1);
335 if ((Multicast) && (!MasterClient))
336 TftpState = STATE_DATA; /* passive.. */
339 TftpSend (); /* Send ACK */
345 TftpBlock = ntohs(*(ushort *)pkt);
348 * RFC1350 specifies that the first data packet will
349 * have sequence number 1. If we receive a sequence
350 * number of 0 this means that there was a wrap
351 * around of the (16 bit) counter.
353 if (TftpBlock == 0) {
355 TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE;
356 printf ("\n\t %lu MB received\n\t ", TftpBlockWrapOffset>>20);
358 #ifdef CONFIG_TFTP_TSIZE
359 else if (TftpTsize) {
360 while (TftpNumchars < NetBootFileXferSize * 50 / TftpTsize) {
367 if (((TftpBlock - 1) % 10) == 0) {
369 } else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) {
374 if (TftpState == STATE_RRQ)
375 debug("Server did not acknowledge timeout option!\n");
377 if (TftpState == STATE_RRQ || TftpState == STATE_OACK) {
378 /* first block received */
379 TftpState = STATE_DATA;
380 TftpServerPort = src;
383 TftpBlockWrapOffset = 0;
385 #ifdef CONFIG_MCAST_TFTP
386 if (Multicast) { /* start!=1 common if mcast */
387 TftpLastBlock = TftpBlock - 1;
390 if (TftpBlock != 1) { /* Assertion */
391 printf ("\nTFTP error: "
392 "First block is not block 1 (%ld)\n"
393 "Starting again\n\n",
400 if (TftpBlock == TftpLastBlock) {
402 * Same block again; ignore it.
407 TftpLastBlock = TftpBlock;
408 TftpTimeoutMSecs = TIMEOUT;
409 TftpTimeoutCountMax = TIMEOUT_COUNT;
410 NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
412 store_block (TftpBlock - 1, pkt + 2, len);
415 * Acknoledge the block just received, which will prompt
416 * the server for the next one.
418 #ifdef CONFIG_MCAST_TFTP
419 /* if I am the MasterClient, actively calculate what my next
420 * needed block is; else I'm passive; not ACKING
423 if (len < TftpBlkSize) {
424 TftpEndingBlock = TftpBlock;
425 } else if (MasterClient) {
426 TftpBlock = PrevBitmapHole =
427 ext2_find_next_zero_bit(
431 if (TftpBlock > ((Mapsize*8) - 1)) {
432 printf ("tftpfile too big\n");
433 /* try to double it and retry */
439 TftpLastBlock = TftpBlock;
445 #ifdef CONFIG_MCAST_TFTP
447 if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
448 puts ("\nMulticast tftp done\n");
450 NetState = NETLOOP_SUCCESS;
455 if (len < TftpBlkSize) {
457 * We received the whole thing. Try to
460 #ifdef CONFIG_TFTP_TSIZE
461 /* Print out the hash marks for the last packet received */
462 while (TftpTsize && TftpNumchars < 49) {
468 NetState = NETLOOP_SUCCESS;
473 printf ("\nTFTP error: '%s' (%d)\n",
474 pkt + 2, ntohs(*(ushort *)pkt));
475 puts ("Starting again\n\n");
476 #ifdef CONFIG_MCAST_TFTP
488 if (++TftpTimeoutCount > TftpTimeoutCountMax) {
489 puts ("\nRetry count exceeded; starting again\n");
490 #ifdef CONFIG_MCAST_TFTP
496 NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
505 char *ep; /* Environment pointer */
507 /* Allow the user to choose tftpblocksize */
508 if ((ep = getenv("tftpblocksize")) != NULL)
509 TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
510 debug("tftp block size is %i\n", TftpBlkSizeOption);
512 TftpServerIP = NetServerIP;
513 if (BootFile[0] == '\0') {
514 sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
516 (NetOurIP >> 8) & 0xFF,
517 (NetOurIP >> 16) & 0xFF,
518 (NetOurIP >> 24) & 0xFF );
520 strncpy(tftp_filename, default_filename, MAX_LEN);
521 tftp_filename[MAX_LEN-1] = 0;
523 printf ("*** Warning: no boot file name; using '%s'\n",
526 char *p = strchr (BootFile, ':');
529 strncpy(tftp_filename, BootFile, MAX_LEN);
530 tftp_filename[MAX_LEN-1] = 0;
532 TftpServerIP = string_to_ip (BootFile);
533 strncpy(tftp_filename, p + 1, MAX_LEN);
534 tftp_filename[MAX_LEN-1] = 0;
538 #if defined(CONFIG_NET_MULTI)
539 printf ("Using %s device\n", eth_get_name());
541 printf("TFTP from server %pI4"
542 "; our IP address is %pI4", &TftpServerIP, &NetOurIP);
544 /* Check if we need to send across this subnet */
545 if (NetOurGatewayIP && NetOurSubnetMask) {
546 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
547 IPaddr_t ServerNet = TftpServerIP & NetOurSubnetMask;
549 if (OurNet != ServerNet)
550 printf("; sending through gateway %pI4", &NetOurGatewayIP);
554 printf ("Filename '%s'.", tftp_filename);
556 if (NetBootFileSize) {
557 printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
558 print_size (NetBootFileSize<<9, "");
563 printf ("Load address: 0x%lx\n", load_addr);
565 puts ("Loading: *\b");
567 TftpTimeoutMSecs = TftpRRQTimeoutMSecs;
568 TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
570 NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
571 NetSetHandler (TftpHandler);
573 TftpServerPort = WELL_KNOWN_PORT;
574 TftpTimeoutCount = 0;
575 TftpState = STATE_RRQ;
576 /* Use a pseudo-random port unless a specific port is set */
577 TftpOurPort = 1024 + (get_timer(0) % 3072);
579 #ifdef CONFIG_TFTP_PORT
580 if ((ep = getenv("tftpdstp")) != NULL) {
581 TftpServerPort = simple_strtol(ep, NULL, 10);
583 if ((ep = getenv("tftpsrcp")) != NULL) {
584 TftpOurPort= simple_strtol(ep, NULL, 10);
589 /* zero out server ether in case the server ip has changed */
590 memset(NetServerEther, 0, 6);
591 /* Revert TftpBlkSize to dflt */
592 TftpBlkSize = TFTP_BLOCK_SIZE;
593 #ifdef CONFIG_MCAST_TFTP
596 #ifdef CONFIG_TFTP_TSIZE
604 #ifdef CONFIG_MCAST_TFTP
605 /* Credits: atftp project.
608 /* pick up BcastAddr, Port, and whether I am [now] the master-client. *
610 * +-------+-----------+---+-------~~-------+---+
611 * | opc | multicast | 0 | addr, port, mc | 0 |
612 * +-------+-----------+---+-------~~-------+---+
613 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
614 * I am the new master-client so must send ACKs to DataBlocks. If I am not
615 * master-client, I'm a passive client, gathering what DataBlocks I may and
616 * making note of which ones I got in my bitmask.
617 * In theory, I never go from master->passive..
618 * .. this comes in with pkt already pointing just past opc
620 static void parse_multicast_oack(char *pkt, int len)
624 char *mc_adr, *port, *mc;
627 /* march along looking for 'multicast\0', which has to start at least
628 * 14 bytes back from the end.
630 for (i=0;i<len-14;i++)
631 if (strcmp (pkt+i,"multicast") == 0)
633 if (i >= (len-14)) /* non-Multicast OACK, ign. */
636 i+=10; /* strlen multicast */
639 if (*(pkt+i) == ',') {
649 if (!port || !mc_adr || !mc ) return;
650 if (Multicast && MasterClient) {
651 printf ("I got a OACK as master Client, WRONG!\n");
654 /* ..I now accept packets destined for this MCAST addr, port */
657 printf ("Internal failure! no mcast.\n");
663 /* I malloc instead of pre-declare; so that if the file ends
664 * up being too big for this bitmap I can retry
666 if (!(Bitmap = malloc (Mapsize))) {
667 printf ("No Bitmap, no multicast. Sorry.\n");
671 memset (Bitmap,0,Mapsize);
675 addr = string_to_ip(mc_adr);
676 if (Mcast_addr != addr) {
678 eth_mcast_join(Mcast_addr, 0);
679 if (eth_mcast_join(Mcast_addr=addr, 1)) {
680 printf ("Fail to set mcast, revert to TFTP\n");
686 MasterClient = (unsigned char)simple_strtoul((char *)mc,NULL,10);
687 Mcast_port = (unsigned short)simple_strtoul(port,NULL,10);
688 printf ("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
692 #endif /* Multicast TFTP */