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 5UL /* Seconds to timeout for a 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 IPaddr_t TftpServerIP;
38 static int TftpServerPort; /* The UDP port at their end */
39 static int TftpOurPort; /* The UDP port at our end */
40 static int TftpTimeoutCount;
41 static ulong TftpBlock; /* packet sequence number */
42 static ulong TftpLastBlock; /* last packet sequence number received */
43 static ulong TftpBlockWrap; /* count of sequence number wraparounds */
44 static ulong TftpBlockWrapOffset; /* memory offset due to wrapping */
49 #define STATE_TOO_LARGE 3
50 #define STATE_BAD_MAGIC 4
53 #define TFTP_BLOCK_SIZE 512 /* default TFTP block size */
54 #define TFTP_SEQUENCE_SIZE ((ulong)(1<<16)) /* sequence number is 16 bit */
56 #define DEFAULT_NAME_LEN (8 + 4 + 1)
57 static char default_filename[DEFAULT_NAME_LEN];
59 #ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
62 #define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
65 static char tftp_filename[MAX_LEN];
67 #ifdef CFG_DIRECT_FLASH_TFTP
68 extern flash_info_t flash_info[];
71 /* 512 is poor choice for ethernet, MTU is typically 1500.
72 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
73 * almost-MTU block sizes. At least try... fall back to 512 if need be.
75 #define TFTP_MTU_BLOCKSIZE 1468
76 static unsigned short TftpBlkSize=TFTP_BLOCK_SIZE;
77 static unsigned short TftpBlkSizeOption=TFTP_MTU_BLOCKSIZE;
79 #ifdef CONFIG_MCAST_TFTP
81 #define MTFTP_BITMAPSIZE 0x1000
82 static unsigned *Bitmap;
83 static int PrevBitmapHole,Mapsize=MTFTP_BITMAPSIZE;
84 static uchar ProhibitMcast=0, MasterClient=0;
85 static uchar Multicast=0;
86 extern IPaddr_t Mcast_addr;
87 static int Mcast_port;
88 static ulong TftpEndingBlock; /* can get 'last' block before done..*/
90 static void parse_multicast_oack(char *pkt,int len);
95 if (Mcast_addr) eth_mcast_join(Mcast_addr, 0);
96 if (Bitmap) free(Bitmap);
98 Mcast_addr = Multicast = Mcast_port = 0;
102 #endif /* CONFIG_MCAST_TFTP */
104 static __inline__ void
105 store_block (unsigned block, uchar * src, unsigned len)
107 ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
108 ulong newsize = offset + len;
109 #ifdef CFG_DIRECT_FLASH_TFTP
112 for (i=0; i<CFG_MAX_FLASH_BANKS; i++) {
113 /* start address in flash? */
114 if (flash_info[i].flash_id == FLASH_UNKNOWN)
116 if (load_addr + offset >= flash_info[i].start[0]) {
122 if (rc) { /* Flash is destination for this packet */
123 rc = flash_write ((char *)src, (ulong)(load_addr+offset), len);
126 NetState = NETLOOP_FAIL;
131 #endif /* CFG_DIRECT_FLASH_TFTP */
133 (void)memcpy((void *)(load_addr + offset), src, len);
135 #ifdef CONFIG_MCAST_TFTP
137 ext2_set_bit(block, Bitmap);
140 if (NetBootFileXferSize < newsize)
141 NetBootFileXferSize = newsize;
144 static void TftpSend (void);
145 static void TftpTimeout (void);
147 /**********************************************************************/
152 volatile uchar * pkt;
157 #ifdef CONFIG_MCAST_TFTP
158 /* Multicast TFTP.. non-MasterClients do not ACK data. */
160 && (TftpState == STATE_DATA)
161 && (MasterClient == 0))
165 * We will always be sending some sort of packet, so
166 * cobble together the packet headers now.
168 pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
175 *s++ = htons(TFTP_RRQ);
177 strcpy ((char *)pkt, tftp_filename);
178 pkt += strlen(tftp_filename) + 1;
179 strcpy ((char *)pkt, "octet");
180 pkt += 5 /*strlen("octet")*/ + 1;
181 strcpy ((char *)pkt, "timeout");
182 pkt += 7 /*strlen("timeout")*/ + 1;
183 sprintf((char *)pkt, "%lu", TIMEOUT);
185 printf("send option \"timeout %s\"\n", (char *)pkt);
187 pkt += strlen((char *)pkt) + 1;
188 /* try for more effic. blk size */
189 pkt += sprintf((char *)pkt,"blksize%c%d%c",
190 0,TftpBlkSizeOption,0);
191 #ifdef CONFIG_MCAST_TFTP
192 /* Check all preconditions before even trying the option */
194 && (Bitmap=malloc(Mapsize))
195 && eth_get_dev()->mcast) {
198 pkt += sprintf((char *)pkt,"multicast%c%c",0,0);
200 #endif /* CONFIG_MCAST_TFTP */
205 #ifdef CONFIG_MCAST_TFTP
206 /* My turn! Start at where I need blocks I missed.*/
208 TftpBlock=ext2_find_next_zero_bit(Bitmap,(Mapsize*8),0);
214 *s++ = htons(TFTP_ACK);
215 *s++ = htons(TftpBlock);
220 case STATE_TOO_LARGE:
223 *s++ = htons(TFTP_ERROR);
226 strcpy ((char *)pkt, "File too large");
227 pkt += 14 /*strlen("File too large")*/ + 1;
231 case STATE_BAD_MAGIC:
234 *s++ = htons(TFTP_ERROR);
237 strcpy ((char *)pkt, "File has bad magic");
238 pkt += 18 /*strlen("File has bad magic")*/ + 1;
243 NetSendUDPPacket(NetServerEther, TftpServerIP, TftpServerPort, TftpOurPort, len);
248 TftpHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
254 if (dest != TftpOurPort) {
255 #ifdef CONFIG_MCAST_TFTP
257 && (!Mcast_port || (dest != Mcast_port)))
261 if (TftpState != STATE_RRQ && src != TftpServerPort) {
269 /* warning: don't use increment (++) in ntohs() macros!! */
273 switch (ntohs(proto)) {
284 printf("Got OACK: %s %s\n", pkt, pkt+strlen(pkt)+1);
286 TftpState = STATE_OACK;
287 TftpServerPort = src;
289 * Check for 'blksize' option.
290 * Careful: "i" is signed, "len" is unsigned, thus
291 * something like "len-8" may give a *huge* number
293 for (i=0; i+8<len; i++) {
294 if (strcmp ((char*)pkt+i,"blksize") == 0) {
295 TftpBlkSize = (unsigned short)
296 simple_strtoul((char*)pkt+i+8,NULL,10);
298 printf ("Blocksize ack: %s, %d\n",
299 (char*)pkt+i+8,TftpBlkSize);
304 #ifdef CONFIG_MCAST_TFTP
305 parse_multicast_oack((char *)pkt,len-1);
306 if ((Multicast) && (!MasterClient))
307 TftpState = STATE_DATA; /* passive.. */
310 TftpSend (); /* Send ACK */
316 TftpBlock = ntohs(*(ushort *)pkt);
319 * RFC1350 specifies that the first data packet will
320 * have sequence number 1. If we receive a sequence
321 * number of 0 this means that there was a wrap
322 * around of the (16 bit) counter.
324 if (TftpBlock == 0) {
326 TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE;
327 printf ("\n\t %lu MB received\n\t ", TftpBlockWrapOffset>>20);
329 if (((TftpBlock - 1) % 10) == 0) {
331 } else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) {
337 if (TftpState == STATE_RRQ) {
338 puts ("Server did not acknowledge timeout option!\n");
342 if (TftpState == STATE_RRQ || TftpState == STATE_OACK) {
343 /* first block received */
344 TftpState = STATE_DATA;
345 TftpServerPort = src;
348 TftpBlockWrapOffset = 0;
350 #ifdef CONFIG_MCAST_TFTP
351 if (Multicast) { /* start!=1 common if mcast */
352 TftpLastBlock = TftpBlock - 1;
355 if (TftpBlock != 1) { /* Assertion */
356 printf ("\nTFTP error: "
357 "First block is not block 1 (%ld)\n"
358 "Starting again\n\n",
365 if (TftpBlock == TftpLastBlock) {
367 * Same block again; ignore it.
372 TftpLastBlock = TftpBlock;
373 NetSetTimeout (TIMEOUT * CFG_HZ, TftpTimeout);
375 store_block (TftpBlock - 1, pkt + 2, len);
378 * Acknoledge the block just received, which will prompt
379 * the server for the next one.
381 #ifdef CONFIG_MCAST_TFTP
382 /* if I am the MasterClient, actively calculate what my next
383 * needed block is; else I'm passive; not ACKING
386 if (len < TftpBlkSize) {
387 TftpEndingBlock = TftpBlock;
388 } else if (MasterClient) {
389 TftpBlock = PrevBitmapHole =
390 ext2_find_next_zero_bit(
394 if (TftpBlock > ((Mapsize*8) - 1)) {
395 printf ("tftpfile too big\n");
396 /* try to double it and retry */
402 TftpLastBlock = TftpBlock;
408 #ifdef CONFIG_MCAST_TFTP
410 if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
411 puts ("\nMulticast tftp done\n");
413 NetState = NETLOOP_SUCCESS;
418 if (len < TftpBlkSize) {
420 * We received the whole thing. Try to
424 NetState = NETLOOP_SUCCESS;
429 printf ("\nTFTP error: '%s' (%d)\n",
430 pkt + 2, ntohs(*(ushort *)pkt));
431 puts ("Starting again\n\n");
432 #ifdef CONFIG_MCAST_TFTP
444 if (++TftpTimeoutCount > TIMEOUT_COUNT) {
445 puts ("\nRetry count exceeded; starting again\n");
446 #ifdef CONFIG_MCAST_TFTP
452 NetSetTimeout (TIMEOUT * CFG_HZ, TftpTimeout);
461 #ifdef CONFIG_TFTP_PORT
462 char *ep; /* Environment pointer */
465 TftpServerIP = NetServerIP;
466 if (BootFile[0] == '\0') {
467 sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
469 (NetOurIP >> 8) & 0xFF,
470 (NetOurIP >> 16) & 0xFF,
471 (NetOurIP >> 24) & 0xFF );
473 strncpy(tftp_filename, default_filename, MAX_LEN);
474 tftp_filename[MAX_LEN-1] = 0;
476 printf ("*** Warning: no boot file name; using '%s'\n",
479 char *p = strchr (BootFile, ':');
482 strncpy(tftp_filename, BootFile, MAX_LEN);
483 tftp_filename[MAX_LEN-1] = 0;
486 TftpServerIP = string_to_ip (BootFile);
487 strncpy(tftp_filename, p, MAX_LEN);
488 tftp_filename[MAX_LEN-1] = 0;
492 #if defined(CONFIG_NET_MULTI)
493 printf ("Using %s device\n", eth_get_name());
495 puts ("TFTP from server "); print_IPaddr (TftpServerIP);
496 puts ("; our IP address is "); print_IPaddr (NetOurIP);
498 /* Check if we need to send across this subnet */
499 if (NetOurGatewayIP && NetOurSubnetMask) {
500 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
501 IPaddr_t ServerNet = TftpServerIP & NetOurSubnetMask;
503 if (OurNet != ServerNet) {
504 puts ("; sending through gateway ");
505 print_IPaddr (NetOurGatewayIP) ;
510 printf ("Filename '%s'.", tftp_filename);
512 if (NetBootFileSize) {
513 printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
514 print_size (NetBootFileSize<<9, "");
519 printf ("Load address: 0x%lx\n", load_addr);
521 puts ("Loading: *\b");
523 NetSetTimeout (TIMEOUT * CFG_HZ, TftpTimeout);
524 NetSetHandler (TftpHandler);
526 TftpServerPort = WELL_KNOWN_PORT;
527 TftpTimeoutCount = 0;
528 TftpState = STATE_RRQ;
529 /* Use a pseudo-random port unless a specific port is set */
530 TftpOurPort = 1024 + (get_timer(0) % 3072);
532 #ifdef CONFIG_TFTP_PORT
533 if ((ep = getenv("tftpdstp")) != NULL) {
534 TftpServerPort = simple_strtol(ep, NULL, 10);
536 if ((ep = getenv("tftpsrcp")) != NULL) {
537 TftpOurPort= simple_strtol(ep, NULL, 10);
542 /* zero out server ether in case the server ip has changed */
543 memset(NetServerEther, 0, 6);
544 /* Revert TftpBlkSize to dflt */
545 TftpBlkSize = TFTP_BLOCK_SIZE;
546 #ifdef CONFIG_MCAST_TFTP
553 #ifdef CONFIG_MCAST_TFTP
554 /* Credits: atftp project.
557 /* pick up BcastAddr, Port, and whether I am [now] the master-client. *
559 * +-------+-----------+---+-------~~-------+---+
560 * | opc | multicast | 0 | addr, port, mc | 0 |
561 * +-------+-----------+---+-------~~-------+---+
562 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
563 * I am the new master-client so must send ACKs to DataBlocks. If I am not
564 * master-client, I'm a passive client, gathering what DataBlocks I may and
565 * making note of which ones I got in my bitmask.
566 * In theory, I never go from master->passive..
567 * .. this comes in with pkt already pointing just past opc
569 static void parse_multicast_oack(char *pkt, int len)
573 char *mc_adr, *port, *mc;
576 /* march along looking for 'multicast\0', which has to start at least
577 * 14 bytes back from the end.
579 for (i=0;i<len-14;i++)
580 if (strcmp (pkt+i,"multicast") == 0)
582 if (i >= (len-14)) /* non-Multicast OACK, ign. */
585 i+=10; /* strlen multicast */
588 if (*(pkt+i) == ',') {
598 if (!port || !mc_adr || !mc ) return;
599 if (Multicast && MasterClient) {
600 printf ("I got a OACK as master Client, WRONG!\n");
603 /* ..I now accept packets destined for this MCAST addr, port */
606 printf ("Internal failure! no mcast.\n");
612 /* I malloc instead of pre-declare; so that if the file ends
613 * up being too big for this bitmap I can retry
615 if (!(Bitmap = malloc (Mapsize))) {
616 printf ("No Bitmap, no multicast. Sorry.\n");
620 memset (Bitmap,0,Mapsize);
624 addr = string_to_ip(mc_adr);
625 if (Mcast_addr != addr) {
627 eth_mcast_join(Mcast_addr, 0);
628 if (eth_mcast_join(Mcast_addr=addr, 1)) {
629 printf ("Fail to set mcast, revert to TFTP\n");
635 MasterClient = (unsigned char)simple_strtoul((char *)mc,NULL,10);
636 Mcast_port = (unsigned short)simple_strtoul(port,NULL,10);
637 printf ("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
641 #endif /* Multicast TFTP */