2 * Copyright 1994, 1995, 2000 Neil Russell.
4 * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
15 #if defined(CONFIG_CMD_NET)
17 #define WELL_KNOWN_PORT 69 /* Well known TFTP port # */
18 #define TIMEOUT 5000UL /* Millisecs to timeout for lost pkt */
19 #ifndef CONFIG_NET_RETRY_COUNT
20 # define TIMEOUT_COUNT 10 /* # of timeouts before giving up */
22 # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
24 /* (for checking the image size) */
25 #define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
37 static ulong TftpTimeoutMSecs = TIMEOUT;
38 static int TftpTimeoutCountMax = TIMEOUT_COUNT;
41 * These globals govern the timeout behavior when attempting a connection to a
42 * TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to
43 * wait for the server to respond to initial connection. Second global,
44 * TftpRRQTimeoutCountMax, gives the number of such connection retries.
45 * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be
46 * positive. The globals are meant to be set (and restored) by code needing
47 * non-standard timeout behavior when initiating a TFTP transfer.
49 ulong TftpRRQTimeoutMSecs = TIMEOUT;
50 int TftpRRQTimeoutCountMax = TIMEOUT_COUNT;
52 static IPaddr_t TftpServerIP;
53 static int TftpServerPort; /* The UDP port at their end */
54 static int TftpOurPort; /* The UDP port at our end */
55 static int TftpTimeoutCount;
56 static ulong TftpBlock; /* packet sequence number */
57 static ulong TftpLastBlock; /* last packet sequence number received */
58 static ulong TftpBlockWrap; /* count of sequence number wraparounds */
59 static ulong TftpBlockWrapOffset; /* memory offset due to wrapping */
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.
90 #define TFTP_MTU_BLOCKSIZE 1468
91 static unsigned short TftpBlkSize=TFTP_BLOCK_SIZE;
92 static unsigned short TftpBlkSizeOption=TFTP_MTU_BLOCKSIZE;
94 #ifdef CONFIG_MCAST_TFTP
96 #define MTFTP_BITMAPSIZE 0x1000
97 static unsigned *Bitmap;
98 static int PrevBitmapHole,Mapsize=MTFTP_BITMAPSIZE;
99 static uchar ProhibitMcast=0, MasterClient=0;
100 static uchar Multicast=0;
101 extern IPaddr_t Mcast_addr;
102 static int Mcast_port;
103 static ulong TftpEndingBlock; /* can get 'last' block before done..*/
105 static void parse_multicast_oack(char *pkt,int len);
110 if (Mcast_addr) eth_mcast_join(Mcast_addr, 0);
111 if (Bitmap) free(Bitmap);
113 Mcast_addr = Multicast = Mcast_port = 0;
114 TftpEndingBlock = -1;
117 #endif /* CONFIG_MCAST_TFTP */
119 static __inline__ void
120 store_block (unsigned block, uchar * src, unsigned len)
122 ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
123 ulong newsize = offset + len;
124 #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
127 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; i++) {
128 /* start address in flash? */
129 if (flash_info[i].flash_id == FLASH_UNKNOWN)
131 if (load_addr + offset >= flash_info[i].start[0]) {
137 if (rc) { /* Flash is destination for this packet */
138 rc = flash_write ((char *)src, (ulong)(load_addr+offset), len);
141 NetState = NETLOOP_FAIL;
146 #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
148 (void)memcpy((void *)(load_addr + offset), src, len);
150 #ifdef CONFIG_MCAST_TFTP
152 ext2_set_bit(block, Bitmap);
155 if (NetBootFileXferSize < newsize)
156 NetBootFileXferSize = newsize;
159 static void TftpSend (void);
160 static void TftpTimeout (void);
162 /**********************************************************************/
167 volatile uchar * pkt;
172 #ifdef CONFIG_MCAST_TFTP
173 /* Multicast TFTP.. non-MasterClients do not ACK data. */
175 && (TftpState == STATE_DATA)
176 && (MasterClient == 0))
180 * We will always be sending some sort of packet, so
181 * cobble together the packet headers now.
183 pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
190 *s++ = htons(TFTP_RRQ);
192 strcpy ((char *)pkt, tftp_filename);
193 pkt += strlen(tftp_filename) + 1;
194 strcpy ((char *)pkt, "octet");
195 pkt += 5 /*strlen("octet")*/ + 1;
196 strcpy ((char *)pkt, "timeout");
197 pkt += 7 /*strlen("timeout")*/ + 1;
198 sprintf((char *)pkt, "%lu", TIMEOUT / 1000);
200 printf("send option \"timeout %s\"\n", (char *)pkt);
202 pkt += strlen((char *)pkt) + 1;
203 /* try for more effic. blk size */
204 pkt += sprintf((char *)pkt,"blksize%c%d%c",
205 0,TftpBlkSizeOption,0);
206 #ifdef CONFIG_MCAST_TFTP
207 /* Check all preconditions before even trying the option */
209 && (Bitmap=malloc(Mapsize))
210 && eth_get_dev()->mcast) {
213 pkt += sprintf((char *)pkt,"multicast%c%c",0,0);
215 #endif /* CONFIG_MCAST_TFTP */
220 #ifdef CONFIG_MCAST_TFTP
221 /* My turn! Start at where I need blocks I missed.*/
223 TftpBlock=ext2_find_next_zero_bit(Bitmap,(Mapsize*8),0);
229 *s++ = htons(TFTP_ACK);
230 *s++ = htons(TftpBlock);
235 case STATE_TOO_LARGE:
238 *s++ = htons(TFTP_ERROR);
241 strcpy ((char *)pkt, "File too large");
242 pkt += 14 /*strlen("File too large")*/ + 1;
246 case STATE_BAD_MAGIC:
249 *s++ = htons(TFTP_ERROR);
252 strcpy ((char *)pkt, "File has bad magic");
253 pkt += 18 /*strlen("File has bad magic")*/ + 1;
258 NetSendUDPPacket(NetServerEther, TftpServerIP, TftpServerPort, TftpOurPort, len);
263 TftpHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
269 if (dest != TftpOurPort) {
270 #ifdef CONFIG_MCAST_TFTP
272 && (!Mcast_port || (dest != Mcast_port)))
276 if (TftpState != STATE_RRQ && src != TftpServerPort) {
284 /* warning: don't use increment (++) in ntohs() macros!! */
288 switch (ntohs(proto)) {
299 printf("Got OACK: %s %s\n", pkt, pkt+strlen(pkt)+1);
301 TftpState = STATE_OACK;
302 TftpServerPort = src;
304 * Check for 'blksize' option.
305 * Careful: "i" is signed, "len" is unsigned, thus
306 * something like "len-8" may give a *huge* number
308 for (i=0; i+8<len; i++) {
309 if (strcmp ((char*)pkt+i,"blksize") == 0) {
310 TftpBlkSize = (unsigned short)
311 simple_strtoul((char*)pkt+i+8,NULL,10);
313 printf ("Blocksize ack: %s, %d\n",
314 (char*)pkt+i+8,TftpBlkSize);
319 #ifdef CONFIG_MCAST_TFTP
320 parse_multicast_oack((char *)pkt,len-1);
321 if ((Multicast) && (!MasterClient))
322 TftpState = STATE_DATA; /* passive.. */
325 TftpSend (); /* Send ACK */
331 TftpBlock = ntohs(*(ushort *)pkt);
334 * RFC1350 specifies that the first data packet will
335 * have sequence number 1. If we receive a sequence
336 * number of 0 this means that there was a wrap
337 * around of the (16 bit) counter.
339 if (TftpBlock == 0) {
341 TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE;
342 printf ("\n\t %lu MB received\n\t ", TftpBlockWrapOffset>>20);
344 if (((TftpBlock - 1) % 10) == 0) {
346 } else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) {
352 if (TftpState == STATE_RRQ) {
353 puts ("Server did not acknowledge timeout option!\n");
357 if (TftpState == STATE_RRQ || TftpState == STATE_OACK) {
358 /* first block received */
359 TftpState = STATE_DATA;
360 TftpServerPort = src;
363 TftpBlockWrapOffset = 0;
365 #ifdef CONFIG_MCAST_TFTP
366 if (Multicast) { /* start!=1 common if mcast */
367 TftpLastBlock = TftpBlock - 1;
370 if (TftpBlock != 1) { /* Assertion */
371 printf ("\nTFTP error: "
372 "First block is not block 1 (%ld)\n"
373 "Starting again\n\n",
380 if (TftpBlock == TftpLastBlock) {
382 * Same block again; ignore it.
387 TftpLastBlock = TftpBlock;
388 TftpTimeoutMSecs = TIMEOUT;
389 TftpTimeoutCountMax = TIMEOUT_COUNT;
390 NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
392 store_block (TftpBlock - 1, pkt + 2, len);
395 * Acknoledge the block just received, which will prompt
396 * the server for the next one.
398 #ifdef CONFIG_MCAST_TFTP
399 /* if I am the MasterClient, actively calculate what my next
400 * needed block is; else I'm passive; not ACKING
403 if (len < TftpBlkSize) {
404 TftpEndingBlock = TftpBlock;
405 } else if (MasterClient) {
406 TftpBlock = PrevBitmapHole =
407 ext2_find_next_zero_bit(
411 if (TftpBlock > ((Mapsize*8) - 1)) {
412 printf ("tftpfile too big\n");
413 /* try to double it and retry */
419 TftpLastBlock = TftpBlock;
425 #ifdef CONFIG_MCAST_TFTP
427 if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
428 puts ("\nMulticast tftp done\n");
430 NetState = NETLOOP_SUCCESS;
435 if (len < TftpBlkSize) {
437 * We received the whole thing. Try to
441 NetState = NETLOOP_SUCCESS;
446 printf ("\nTFTP error: '%s' (%d)\n",
447 pkt + 2, ntohs(*(ushort *)pkt));
448 puts ("Starting again\n\n");
449 #ifdef CONFIG_MCAST_TFTP
461 if (++TftpTimeoutCount > TftpTimeoutCountMax) {
462 puts ("\nRetry count exceeded; starting again\n");
463 #ifdef CONFIG_MCAST_TFTP
469 NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
478 #ifdef CONFIG_TFTP_PORT
479 char *ep; /* Environment pointer */
482 TftpServerIP = NetServerIP;
483 if (BootFile[0] == '\0') {
484 sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
486 (NetOurIP >> 8) & 0xFF,
487 (NetOurIP >> 16) & 0xFF,
488 (NetOurIP >> 24) & 0xFF );
490 strncpy(tftp_filename, default_filename, MAX_LEN);
491 tftp_filename[MAX_LEN-1] = 0;
493 printf ("*** Warning: no boot file name; using '%s'\n",
496 char *p = strchr (BootFile, ':');
499 strncpy(tftp_filename, BootFile, MAX_LEN);
500 tftp_filename[MAX_LEN-1] = 0;
503 TftpServerIP = string_to_ip (BootFile);
504 strncpy(tftp_filename, p, MAX_LEN);
505 tftp_filename[MAX_LEN-1] = 0;
509 #if defined(CONFIG_NET_MULTI)
510 printf ("Using %s device\n", eth_get_name());
512 puts ("TFTP from server "); print_IPaddr (TftpServerIP);
513 puts ("; our IP address is "); print_IPaddr (NetOurIP);
515 /* Check if we need to send across this subnet */
516 if (NetOurGatewayIP && NetOurSubnetMask) {
517 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
518 IPaddr_t ServerNet = TftpServerIP & NetOurSubnetMask;
520 if (OurNet != ServerNet) {
521 puts ("; sending through gateway ");
522 print_IPaddr (NetOurGatewayIP) ;
527 printf ("Filename '%s'.", tftp_filename);
529 if (NetBootFileSize) {
530 printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
531 print_size (NetBootFileSize<<9, "");
536 printf ("Load address: 0x%lx\n", load_addr);
538 puts ("Loading: *\b");
540 TftpTimeoutMSecs = TftpRRQTimeoutMSecs;
541 TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
543 NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
544 NetSetHandler (TftpHandler);
546 TftpServerPort = WELL_KNOWN_PORT;
547 TftpTimeoutCount = 0;
548 TftpState = STATE_RRQ;
549 /* Use a pseudo-random port unless a specific port is set */
550 TftpOurPort = 1024 + (get_timer(0) % 3072);
552 #ifdef CONFIG_TFTP_PORT
553 if ((ep = getenv("tftpdstp")) != NULL) {
554 TftpServerPort = simple_strtol(ep, NULL, 10);
556 if ((ep = getenv("tftpsrcp")) != NULL) {
557 TftpOurPort= simple_strtol(ep, NULL, 10);
562 /* zero out server ether in case the server ip has changed */
563 memset(NetServerEther, 0, 6);
564 /* Revert TftpBlkSize to dflt */
565 TftpBlkSize = TFTP_BLOCK_SIZE;
566 #ifdef CONFIG_MCAST_TFTP
573 #ifdef CONFIG_MCAST_TFTP
574 /* Credits: atftp project.
577 /* pick up BcastAddr, Port, and whether I am [now] the master-client. *
579 * +-------+-----------+---+-------~~-------+---+
580 * | opc | multicast | 0 | addr, port, mc | 0 |
581 * +-------+-----------+---+-------~~-------+---+
582 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
583 * I am the new master-client so must send ACKs to DataBlocks. If I am not
584 * master-client, I'm a passive client, gathering what DataBlocks I may and
585 * making note of which ones I got in my bitmask.
586 * In theory, I never go from master->passive..
587 * .. this comes in with pkt already pointing just past opc
589 static void parse_multicast_oack(char *pkt, int len)
593 char *mc_adr, *port, *mc;
596 /* march along looking for 'multicast\0', which has to start at least
597 * 14 bytes back from the end.
599 for (i=0;i<len-14;i++)
600 if (strcmp (pkt+i,"multicast") == 0)
602 if (i >= (len-14)) /* non-Multicast OACK, ign. */
605 i+=10; /* strlen multicast */
608 if (*(pkt+i) == ',') {
618 if (!port || !mc_adr || !mc ) return;
619 if (Multicast && MasterClient) {
620 printf ("I got a OACK as master Client, WRONG!\n");
623 /* ..I now accept packets destined for this MCAST addr, port */
626 printf ("Internal failure! no mcast.\n");
632 /* I malloc instead of pre-declare; so that if the file ends
633 * up being too big for this bitmap I can retry
635 if (!(Bitmap = malloc (Mapsize))) {
636 printf ("No Bitmap, no multicast. Sorry.\n");
640 memset (Bitmap,0,Mapsize);
644 addr = string_to_ip(mc_adr);
645 if (Mcast_addr != addr) {
647 eth_mcast_join(Mcast_addr, 0);
648 if (eth_mcast_join(Mcast_addr=addr, 1)) {
649 printf ("Fail to set mcast, revert to TFTP\n");
655 MasterClient = (unsigned char)simple_strtoul((char *)mc,NULL,10);
656 Mcast_port = (unsigned short)simple_strtoul(port,NULL,10);
657 printf ("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
661 #endif /* Multicast TFTP */