tftp.c 17.9 KB
Newer Older
W
wdenk 已提交
1
/*
2 3 4
 * Copyright 1994, 1995, 2000 Neil Russell.
 * (See License)
 * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
W
wdenk 已提交
5 6 7 8 9 10 11 12
 */

#include <common.h>
#include <command.h>
#include <net.h>
#include "tftp.h"
#include "bootp.h"

13 14 15 16
/* Well known TFTP port # */
#define WELL_KNOWN_PORT	69
/* Millisecs to timeout for lost pkt */
#define TIMEOUT		5000UL
W
wdenk 已提交
17
#ifndef	CONFIG_NET_RETRY_COUNT
18 19
/* # of timeouts before giving up */
# define TIMEOUT_COUNT	10
W
wdenk 已提交
20 21 22
#else
# define TIMEOUT_COUNT  (CONFIG_NET_RETRY_COUNT * 2)
#endif
23 24
/* Number of "loading" hashes per line (for checking the image size) */
#define HASHES_PER_LINE	65
W
wdenk 已提交
25 26 27 28 29 30 31 32 33

/*
 *	TFTP operations.
 */
#define TFTP_RRQ	1
#define TFTP_WRQ	2
#define TFTP_DATA	3
#define TFTP_ACK	4
#define TFTP_ERROR	5
W
wdenk 已提交
34
#define TFTP_OACK	6
W
wdenk 已提交
35

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
static ulong TftpTimeoutMSecs = TIMEOUT;
static int TftpTimeoutCountMax = TIMEOUT_COUNT;

/*
 * These globals govern the timeout behavior when attempting a connection to a
 * TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to
 * wait for the server to respond to initial connection. Second global,
 * TftpRRQTimeoutCountMax, gives the number of such connection retries.
 * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be
 * positive. The globals are meant to be set (and restored) by code needing
 * non-standard timeout behavior when initiating a TFTP transfer.
 */
ulong TftpRRQTimeoutMSecs = TIMEOUT;
int TftpRRQTimeoutCountMax = TIMEOUT_COUNT;

51 52 53 54 55 56 57 58 59 60
enum {
	TFTP_ERR_UNDEFINED           = 0,
	TFTP_ERR_FILE_NOT_FOUND      = 1,
	TFTP_ERR_ACCESS_DENIED       = 2,
	TFTP_ERR_DISK_FULL           = 3,
	TFTP_ERR_UNEXPECTED_OPCODE   = 4,
	TFTP_ERR_UNKNOWN_TRANSFER_ID  = 5,
	TFTP_ERR_FILE_ALREADY_EXISTS = 6,
};

61
static IPaddr_t TftpServerIP;
62 63 64 65
/* The UDP port at their end */
static int	TftpServerPort;
/* The UDP port at our end */
static int	TftpOurPort;
W
wdenk 已提交
66
static int	TftpTimeoutCount;
67 68 69 70 71 72 73 74
/* packet sequence number */
static ulong	TftpBlock;
/* last packet sequence number received */
static ulong	TftpLastBlock;
/* count of sequence number wraparounds */
static ulong	TftpBlockWrap;
/* memory offset due to wrapping */
static ulong	TftpBlockWrapOffset;
W
wdenk 已提交
75
static int	TftpState;
R
Robin Getz 已提交
76
#ifdef CONFIG_TFTP_TSIZE
77 78 79 80
/* The file size reported by the server */
static int	TftpTsize;
/* The number of hashes we printed */
static short	TftpNumchars;
R
Robin Getz 已提交
81
#endif
W
wdenk 已提交
82

W
wdenk 已提交
83 84 85 86
#define STATE_RRQ	1
#define STATE_DATA	2
#define STATE_TOO_LARGE	3
#define STATE_BAD_MAGIC	4
W
wdenk 已提交
87
#define STATE_OACK	5
W
wdenk 已提交
88

89 90 91 92
/* default TFTP block size */
#define TFTP_BLOCK_SIZE		512
/* sequence number is 16 bit */
#define TFTP_SEQUENCE_SIZE	((ulong)(1<<16))
W
wdenk 已提交
93

W
wdenk 已提交
94 95
#define DEFAULT_NAME_LEN	(8 + 4 + 1)
static char default_filename[DEFAULT_NAME_LEN];
96 97 98 99 100 101 102 103

#ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
#define MAX_LEN 128
#else
#define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
#endif

static char tftp_filename[MAX_LEN];
W
wdenk 已提交
104

105
#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
106
extern flash_info_t flash_info[];
W
wdenk 已提交
107 108
#endif

109 110
/* 512 is poor choice for ethernet, MTU is typically 1500.
 * Minus eth.hdrs thats 1468.  Can get 2x better throughput with
D
David Updegraff 已提交
111
 * almost-MTU block sizes.  At least try... fall back to 512 if need be.
112
 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
D
David Updegraff 已提交
113
 */
114 115 116
#ifdef CONFIG_TFTP_BLOCKSIZE
#define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
#else
D
David Updegraff 已提交
117
#define TFTP_MTU_BLOCKSIZE 1468
118 119
#endif

120 121
static unsigned short TftpBlkSize = TFTP_BLOCK_SIZE;
static unsigned short TftpBlkSizeOption = TFTP_MTU_BLOCKSIZE;
D
David Updegraff 已提交
122 123 124 125 126

#ifdef CONFIG_MCAST_TFTP
#include <malloc.h>
#define MTFTP_BITMAPSIZE	0x1000
static unsigned *Bitmap;
127 128 129
static int PrevBitmapHole, Mapsize = MTFTP_BITMAPSIZE;
static uchar ProhibitMcast = 0, MasterClient = 0;
static uchar Multicast = 0;
D
David Updegraff 已提交
130 131 132 133
extern IPaddr_t Mcast_addr;
static int Mcast_port;
static ulong TftpEndingBlock; /* can get 'last' block before done..*/

134
static void parse_multicast_oack(char *pkt, int len);
D
David Updegraff 已提交
135 136 137 138 139 140

static void
mcast_cleanup(void)
{
	if (Mcast_addr) eth_mcast_join(Mcast_addr, 0);
	if (Bitmap) free(Bitmap);
141
	Bitmap = NULL;
D
David Updegraff 已提交
142 143 144 145 146 147
	Mcast_addr = Multicast = Mcast_port = 0;
	TftpEndingBlock = -1;
}

#endif	/* CONFIG_MCAST_TFTP */

W
wdenk 已提交
148
static __inline__ void
149
store_block(unsigned block, uchar * src, unsigned len)
W
wdenk 已提交
150
{
D
David Updegraff 已提交
151
	ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
W
wdenk 已提交
152
	ulong newsize = offset + len;
153
#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
W
wdenk 已提交
154 155
	int i, rc = 0;

156
	for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
W
wdenk 已提交
157
		/* start address in flash? */
J
Jochen Friedrich 已提交
158 159
		if (flash_info[i].flash_id == FLASH_UNKNOWN)
			continue;
W
wdenk 已提交
160 161 162 163 164 165 166
		if (load_addr + offset >= flash_info[i].start[0]) {
			rc = 1;
			break;
		}
	}

	if (rc) { /* Flash is destination for this packet */
167
		rc = flash_write((char *)src, (ulong)(load_addr+offset), len);
W
wdenk 已提交
168
		if (rc) {
169
			flash_perror(rc);
W
wdenk 已提交
170 171 172 173 174
			NetState = NETLOOP_FAIL;
			return;
		}
	}
	else
175
#endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
W
wdenk 已提交
176 177 178
	{
		(void)memcpy((void *)(load_addr + offset), src, len);
	}
D
David Updegraff 已提交
179 180 181 182
#ifdef CONFIG_MCAST_TFTP
	if (Multicast)
		ext2_set_bit(block, Bitmap);
#endif
W
wdenk 已提交
183 184 185 186 187

	if (NetBootFileXferSize < newsize)
		NetBootFileXferSize = newsize;
}

188 189
static void TftpSend(void);
static void TftpTimeout(void);
W
wdenk 已提交
190 191 192 193

/**********************************************************************/

static void
194
TftpSend(void)
W
wdenk 已提交
195 196 197 198
{
	volatile uchar *	pkt;
	volatile uchar *	xp;
	int			len = 0;
W
Wolfgang Denk 已提交
199
	volatile ushort *s;
W
wdenk 已提交
200

201
#ifdef CONFIG_MCAST_TFTP
D
David Updegraff 已提交
202
	/* Multicast TFTP.. non-MasterClients do not ACK data. */
203 204 205
	if (Multicast
	 && (TftpState == STATE_DATA)
	 && (MasterClient == 0))
D
David Updegraff 已提交
206 207
		return;
#endif
W
wdenk 已提交
208 209 210 211
	/*
	 *	We will always be sending some sort of packet, so
	 *	cobble together the packet headers now.
	 */
212
	pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
W
wdenk 已提交
213 214 215 216 217

	switch (TftpState) {

	case STATE_RRQ:
		xp = pkt;
W
Wolfgang Denk 已提交
218 219 220
		s = (ushort *)pkt;
		*s++ = htons(TFTP_RRQ);
		pkt = (uchar *)s;
221
		strcpy((char *)pkt, tftp_filename);
W
wdenk 已提交
222
		pkt += strlen(tftp_filename) + 1;
223
		strcpy((char *)pkt, "octet");
W
wdenk 已提交
224
		pkt += 5 /*strlen("octet")*/ + 1;
225
		strcpy((char *)pkt, "timeout");
W
wdenk 已提交
226
		pkt += 7 /*strlen("timeout")*/ + 1;
227
		sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000);
R
Robin Getz 已提交
228
		debug("send option \"timeout %s\"\n", (char *)pkt);
W
wdenk 已提交
229
		pkt += strlen((char *)pkt) + 1;
R
Robin Getz 已提交
230 231 232 233
#ifdef CONFIG_TFTP_TSIZE
		memcpy((char *)pkt, "tsize\0000\0", 8);
		pkt += 8;
#endif
D
David Updegraff 已提交
234
		/* try for more effic. blk size */
235 236
		pkt += sprintf((char *)pkt, "blksize%c%d%c",
				0, TftpBlkSizeOption, 0);
237
#ifdef CONFIG_MCAST_TFTP
D
David Updegraff 已提交
238
		/* Check all preconditions before even trying the option */
239
		if (!ProhibitMcast
240
		 && (Bitmap = malloc(Mapsize))
D
David Updegraff 已提交
241 242
		 && eth_get_dev()->mcast) {
			free(Bitmap);
243 244
			Bitmap = NULL;
			pkt += sprintf((char *)pkt, "multicast%c%c", 0, 0);
D
David Updegraff 已提交
245 246
		}
#endif /* CONFIG_MCAST_TFTP */
W
wdenk 已提交
247 248 249
		len = pkt - xp;
		break;

W
wdenk 已提交
250
	case STATE_OACK:
D
David Updegraff 已提交
251 252 253
#ifdef CONFIG_MCAST_TFTP
		/* My turn!  Start at where I need blocks I missed.*/
		if (Multicast)
254 255
			TftpBlock = ext2_find_next_zero_bit(Bitmap,
							    (Mapsize*8), 0);
D
David Updegraff 已提交
256 257 258
		/*..falling..*/
#endif
	case STATE_DATA:
W
wdenk 已提交
259
		xp = pkt;
W
Wolfgang Denk 已提交
260 261 262 263
		s = (ushort *)pkt;
		*s++ = htons(TFTP_ACK);
		*s++ = htons(TftpBlock);
		pkt = (uchar *)s;
W
wdenk 已提交
264 265 266 267 268
		len = pkt - xp;
		break;

	case STATE_TOO_LARGE:
		xp = pkt;
W
Wolfgang Denk 已提交
269 270 271 272
		s = (ushort *)pkt;
		*s++ = htons(TFTP_ERROR);
		*s++ = htons(3);
		pkt = (uchar *)s;
273
		strcpy((char *)pkt, "File too large");
W
wdenk 已提交
274 275 276 277 278 279
		pkt += 14 /*strlen("File too large")*/ + 1;
		len = pkt - xp;
		break;

	case STATE_BAD_MAGIC:
		xp = pkt;
W
Wolfgang Denk 已提交
280 281 282 283
		s = (ushort *)pkt;
		*s++ = htons(TFTP_ERROR);
		*s++ = htons(2);
		pkt = (uchar *)s;
284
		strcpy((char *)pkt, "File has bad magic");
W
wdenk 已提交
285 286 287 288 289
		pkt += 18 /*strlen("File has bad magic")*/ + 1;
		len = pkt - xp;
		break;
	}

290 291
	NetSendUDPPacket(NetServerEther, TftpServerIP, TftpServerPort,
			 TftpOurPort, len);
W
wdenk 已提交
292 293 294 295
}


static void
296 297
TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
	    unsigned len)
W
wdenk 已提交
298 299
{
	ushort proto;
W
Wolfgang Denk 已提交
300
	ushort *s;
301
	int i;
W
wdenk 已提交
302 303

	if (dest != TftpOurPort) {
D
David Updegraff 已提交
304
#ifdef CONFIG_MCAST_TFTP
305
		if (Multicast
D
David Updegraff 已提交
306 307
		 && (!Mcast_port || (dest != Mcast_port)))
#endif
W
wdenk 已提交
308 309 310 311 312 313 314 315 316 317 318
		return;
	}
	if (TftpState != STATE_RRQ && src != TftpServerPort) {
		return;
	}

	if (len < 2) {
		return;
	}
	len -= 2;
	/* warning: don't use increment (++) in ntohs() macros!! */
W
Wolfgang Denk 已提交
319 320 321
	s = (ushort *)pkt;
	proto = *s++;
	pkt = (uchar *)s;
W
wdenk 已提交
322 323 324 325 326 327 328 329 330
	switch (ntohs(proto)) {

	case TFTP_RRQ:
	case TFTP_WRQ:
	case TFTP_ACK:
		break;
	default:
		break;

W
wdenk 已提交
331
	case TFTP_OACK:
332 333 334
		debug("Got OACK: %s %s\n",
			pkt,
			pkt + strlen((char *)pkt) + 1);
W
wdenk 已提交
335 336
		TftpState = STATE_OACK;
		TftpServerPort = src;
337 338 339 340 341
		/*
		 * Check for 'blksize' option.
		 * Careful: "i" is signed, "len" is unsigned, thus
		 * something like "len-8" may give a *huge* number
		 */
342 343
		for (i = 0; i+8 < len; i++) {
			if (strcmp((char*)pkt+i, "blksize") == 0) {
344
				TftpBlkSize = (unsigned short)
345 346
					simple_strtoul((char*)pkt+i+8, NULL,
						       10);
R
Robin Getz 已提交
347
				debug("Blocksize ack: %s, %d\n",
348
					(char*)pkt+i+8, TftpBlkSize);
349
			}
R
Robin Getz 已提交
350
#ifdef CONFIG_TFTP_TSIZE
351
			if (strcmp((char*)pkt+i, "tsize") == 0) {
352 353
				TftpTsize = simple_strtoul((char*)pkt+i+6,
							   NULL, 10);
R
Robin Getz 已提交
354 355 356 357
				debug("size = %s, %d\n",
					 (char*)pkt+i+6, TftpTsize);
			}
#endif
D
David Updegraff 已提交
358 359
		}
#ifdef CONFIG_MCAST_TFTP
360
		parse_multicast_oack((char *)pkt, len-1);
361
		if ((Multicast) && (!MasterClient))
D
David Updegraff 已提交
362 363 364
			TftpState = STATE_DATA;	/* passive.. */
		else
#endif
365
		TftpSend(); /* Send ACK */
W
wdenk 已提交
366
		break;
W
wdenk 已提交
367 368 369 370 371
	case TFTP_DATA:
		if (len < 2)
			return;
		len -= 2;
		TftpBlock = ntohs(*(ushort *)pkt);
W
wdenk 已提交
372 373

		/*
W
wdenk 已提交
374 375 376 377
		 * RFC1350 specifies that the first data packet will
		 * have sequence number 1. If we receive a sequence
		 * number of 0 this means that there was a wrap
		 * around of the (16 bit) counter.
W
wdenk 已提交
378 379 380
		 */
		if (TftpBlock == 0) {
			TftpBlockWrap++;
381 382
			TftpBlockWrapOffset +=
				TftpBlkSize * TFTP_SEQUENCE_SIZE;
383
			printf("\n\t %lu MB received\n\t ",
384
				TftpBlockWrapOffset>>20);
R
Robin Getz 已提交
385 386 387
		}
#ifdef CONFIG_TFTP_TSIZE
		else if (TftpTsize) {
388 389
			while (TftpNumchars <
			       NetBootFileXferSize * 50 / TftpTsize) {
R
Robin Getz 已提交
390 391 392 393 394 395
				putc('#');
				TftpNumchars++;
			}
		}
#endif
		else {
W
wdenk 已提交
396
			if (((TftpBlock - 1) % 10) == 0) {
397
				putc('#');
W
wdenk 已提交
398
			} else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) {
399
				puts("\n\t ");
W
wdenk 已提交
400
			}
W
wdenk 已提交
401 402
		}

R
Robin Getz 已提交
403 404
		if (TftpState == STATE_RRQ)
			debug("Server did not acknowledge timeout option!\n");
W
wdenk 已提交
405 406

		if (TftpState == STATE_RRQ || TftpState == STATE_OACK) {
W
wdenk 已提交
407
			/* first block received */
W
wdenk 已提交
408 409 410
			TftpState = STATE_DATA;
			TftpServerPort = src;
			TftpLastBlock = 0;
W
wdenk 已提交
411 412
			TftpBlockWrap = 0;
			TftpBlockWrapOffset = 0;
W
wdenk 已提交
413

D
David Updegraff 已提交
414 415 416 417 418
#ifdef CONFIG_MCAST_TFTP
			if (Multicast) { /* start!=1 common if mcast */
				TftpLastBlock = TftpBlock - 1;
			} else
#endif
W
wdenk 已提交
419
			if (TftpBlock != 1) {	/* Assertion */
420 421 422
				printf("\nTFTP error: "
				       "First block is not block 1 (%ld)\n"
				       "Starting again\n\n",
W
wdenk 已提交
423
					TftpBlock);
424
				NetStartAgain();
W
wdenk 已提交
425 426 427 428 429 430 431 432 433 434 435 436
				break;
			}
		}

		if (TftpBlock == TftpLastBlock) {
			/*
			 *	Same block again; ignore it.
			 */
			break;
		}

		TftpLastBlock = TftpBlock;
437
		TftpTimeoutCountMax = TIMEOUT_COUNT;
438
		NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
W
wdenk 已提交
439

440
		store_block(TftpBlock - 1, pkt + 2, len);
W
wdenk 已提交
441 442 443 444 445

		/*
		 *	Acknoledge the block just received, which will prompt
		 *	the server for the next one.
		 */
D
David Updegraff 已提交
446
#ifdef CONFIG_MCAST_TFTP
447 448
		/* if I am the MasterClient, actively calculate what my next
		 * needed block is; else I'm passive; not ACKING
449
		 */
D
David Updegraff 已提交
450 451 452 453
		if (Multicast) {
			if (len < TftpBlkSize)  {
				TftpEndingBlock = TftpBlock;
			} else if (MasterClient) {
454
				TftpBlock = PrevBitmapHole =
D
David Updegraff 已提交
455 456 457 458 459
					ext2_find_next_zero_bit(
						Bitmap,
						(Mapsize*8),
						PrevBitmapHole);
				if (TftpBlock > ((Mapsize*8) - 1)) {
460
					printf("tftpfile too big\n");
D
David Updegraff 已提交
461
					/* try to double it and retry */
462
					Mapsize <<= 1;
D
David Updegraff 已提交
463
					mcast_cleanup();
464
					NetStartAgain();
D
David Updegraff 已提交
465 466 467 468 469 470
					return;
				}
				TftpLastBlock = TftpBlock;
			}
		}
#endif
471
		TftpSend();
W
wdenk 已提交
472

D
David Updegraff 已提交
473 474 475
#ifdef CONFIG_MCAST_TFTP
		if (Multicast) {
			if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
476
				puts("\nMulticast tftp done\n");
D
David Updegraff 已提交
477 478
				mcast_cleanup();
				NetState = NETLOOP_SUCCESS;
479
			}
D
David Updegraff 已提交
480 481 482 483
		}
		else
#endif
		if (len < TftpBlkSize) {
W
wdenk 已提交
484 485 486 487
			/*
			 *	We received the whole thing.  Try to
			 *	run it.
			 */
R
Robin Getz 已提交
488
#ifdef CONFIG_TFTP_TSIZE
489
			/* Print hash marks for the last packet received */
R
Robin Getz 已提交
490 491 492 493 494
			while (TftpTsize && TftpNumchars < 49) {
				putc('#');
				TftpNumchars++;
			}
#endif
495
			puts("\ndone\n");
W
wdenk 已提交
496 497 498 499 500
			NetState = NETLOOP_SUCCESS;
		}
		break;

	case TFTP_ERROR:
501 502
		printf("\nTFTP error: '%s' (%d)\n",
		       pkt + 2, ntohs(*(ushort *)pkt));
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517

		switch (ntohs(*(ushort *)pkt)) {
		case TFTP_ERR_FILE_NOT_FOUND:
		case TFTP_ERR_ACCESS_DENIED:
			puts("Not retrying...\n");
			eth_halt();
			NetState = NETLOOP_FAIL;
			break;
		case TFTP_ERR_UNDEFINED:
		case TFTP_ERR_DISK_FULL:
		case TFTP_ERR_UNEXPECTED_OPCODE:
		case TFTP_ERR_UNKNOWN_TRANSFER_ID:
		case TFTP_ERR_FILE_ALREADY_EXISTS:
		default:
			puts("Starting again\n\n");
D
David Updegraff 已提交
518
#ifdef CONFIG_MCAST_TFTP
519
			mcast_cleanup();
D
David Updegraff 已提交
520
#endif
521 522 523
			NetStartAgain();
			break;
		}
W
wdenk 已提交
524 525 526 527 528 529
		break;
	}
}


static void
530
TftpTimeout(void)
W
wdenk 已提交
531
{
532
	if (++TftpTimeoutCount > TftpTimeoutCountMax) {
533
		puts("\nRetry count exceeded; starting again\n");
D
David Updegraff 已提交
534 535 536
#ifdef CONFIG_MCAST_TFTP
		mcast_cleanup();
#endif
537
		NetStartAgain();
W
wdenk 已提交
538
	} else {
539 540 541
		puts("T ");
		NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
		TftpSend();
W
wdenk 已提交
542 543 544 545 546
	}
}


void
547
TftpStart(void)
W
wdenk 已提交
548
{
549
	char *ep;             /* Environment pointer */
550

551 552 553 554
	/*
	 * Allow the user to choose TFTP blocksize and timeout.
	 * TFTP protocol has a minimal timeout of 1 second.
	 */
555 556
	if ((ep = getenv("tftpblocksize")) != NULL)
		TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
557 558 559 560 561 562 563 564 565 566 567 568 569

	if ((ep = getenv("tftptimeout")) != NULL)
		TftpTimeoutMSecs = simple_strtol(ep, NULL, 10);

	if (TftpTimeoutMSecs < 1000) {
		printf("TFTP timeout (%ld ms) too low, "
			"set minimum = 1000 ms\n",
			TftpTimeoutMSecs);
		TftpTimeoutMSecs = 1000;
	}

	debug("TFTP blocksize = %i, timeout = %ld ms\n",
		TftpBlkSizeOption, TftpTimeoutMSecs);
570

571
	TftpServerIP = NetServerIP;
W
wdenk 已提交
572 573
	if (BootFile[0] == '\0') {
		sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
574 575 576
			NetOurIP & 0xFF,
			(NetOurIP >>  8) & 0xFF,
			(NetOurIP >> 16) & 0xFF,
577
			(NetOurIP >> 24) & 0xFF);
578 579 580

		strncpy(tftp_filename, default_filename, MAX_LEN);
		tftp_filename[MAX_LEN-1] = 0;
W
wdenk 已提交
581

582
		printf("*** Warning: no boot file name; using '%s'\n",
W
wdenk 已提交
583 584
			tftp_filename);
	} else {
585
		char *p = strchr(BootFile, ':');
586 587 588 589 590

		if (p == NULL) {
			strncpy(tftp_filename, BootFile, MAX_LEN);
			tftp_filename[MAX_LEN-1] = 0;
		} else {
591
			TftpServerIP = string_to_ip(BootFile);
P
Peter Tyser 已提交
592
			strncpy(tftp_filename, p + 1, MAX_LEN);
593 594
			tftp_filename[MAX_LEN-1] = 0;
		}
W
wdenk 已提交
595 596
	}

597
#if defined(CONFIG_NET_MULTI)
598
	printf("Using %s device\n", eth_get_name());
599
#endif
M
Mike Frysinger 已提交
600 601
	printf("TFTP from server %pI4"
		"; our IP address is %pI4", &TftpServerIP, &NetOurIP);
W
wdenk 已提交
602 603 604

	/* Check if we need to send across this subnet */
	if (NetOurGatewayIP && NetOurSubnetMask) {
605 606
	    IPaddr_t OurNet	= NetOurIP    & NetOurSubnetMask;
	    IPaddr_t ServerNet	= TftpServerIP & NetOurSubnetMask;
W
wdenk 已提交
607

M
Mike Frysinger 已提交
608 609
	    if (OurNet != ServerNet)
		printf("; sending through gateway %pI4", &NetOurGatewayIP);
W
wdenk 已提交
610
	}
611
	putc('\n');
W
wdenk 已提交
612

613
	printf("Filename '%s'.", tftp_filename);
W
wdenk 已提交
614 615

	if (NetBootFileSize) {
616 617
		printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
		print_size(NetBootFileSize<<9, "");
W
wdenk 已提交
618 619
	}

620
	putc('\n');
W
wdenk 已提交
621

622
	printf("Load address: 0x%lx\n", load_addr);
W
wdenk 已提交
623

624
	puts("Loading: *\b");
W
wdenk 已提交
625

626 627
	TftpTimeoutCountMax = TftpRRQTimeoutCountMax;

628 629
	NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
	NetSetHandler(TftpHandler);
W
wdenk 已提交
630 631 632 633

	TftpServerPort = WELL_KNOWN_PORT;
	TftpTimeoutCount = 0;
	TftpState = STATE_RRQ;
634
	/* Use a pseudo-random port unless a specific port is set */
W
wdenk 已提交
635
	TftpOurPort = 1024 + (get_timer(0) % 3072);
D
David Updegraff 已提交
636

637
#ifdef CONFIG_TFTP_PORT
638 639 640 641
	if ((ep = getenv("tftpdstp")) != NULL) {
		TftpServerPort = simple_strtol(ep, NULL, 10);
	}
	if ((ep = getenv("tftpsrcp")) != NULL) {
642
		TftpOurPort = simple_strtol(ep, NULL, 10);
643 644
	}
#endif
W
wdenk 已提交
645
	TftpBlock = 0;
W
wdenk 已提交
646

W
wdenk 已提交
647 648
	/* zero out server ether in case the server ip has changed */
	memset(NetServerEther, 0, 6);
D
David Updegraff 已提交
649 650 651
	/* Revert TftpBlkSize to dflt */
	TftpBlkSize = TFTP_BLOCK_SIZE;
#ifdef CONFIG_MCAST_TFTP
652
	mcast_cleanup();
D
David Updegraff 已提交
653
#endif
R
Robin Getz 已提交
654 655 656 657
#ifdef CONFIG_TFTP_TSIZE
	TftpTsize = 0;
	TftpNumchars = 0;
#endif
W
wdenk 已提交
658

659
	TftpSend();
W
wdenk 已提交
660 661
}

D
David Updegraff 已提交
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
#ifdef CONFIG_MCAST_TFTP
/* Credits: atftp project.
 */

/* pick up BcastAddr, Port, and whether I am [now] the master-client. *
 * Frame:
 *    +-------+-----------+---+-------~~-------+---+
 *    |  opc  | multicast | 0 | addr, port, mc | 0 |
 *    +-------+-----------+---+-------~~-------+---+
 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
 * I am the new master-client so must send ACKs to DataBlocks.  If I am not
 * master-client, I'm a passive client, gathering what DataBlocks I may and
 * making note of which ones I got in my bitmask.
 * In theory, I never go from master->passive..
 * .. this comes in with pkt already pointing just past opc
 */
static void parse_multicast_oack(char *pkt, int len)
{
680 681 682
	int i;
	IPaddr_t addr;
	char *mc_adr, *port,  *mc;
D
David Updegraff 已提交
683

684
	mc_adr = port = mc = NULL;
D
David Updegraff 已提交
685 686 687
	/* march along looking for 'multicast\0', which has to start at least
	 * 14 bytes back from the end.
	 */
688 689
	for (i = 0; i < len-14; i++)
		if (strcmp(pkt+i, "multicast") == 0)
D
David Updegraff 已提交
690 691 692
			break;
	if (i >= (len-14)) /* non-Multicast OACK, ign. */
		return;
693

694
	i += 10; /* strlen multicast */
D
David Updegraff 已提交
695
	mc_adr = pkt+i;
696
	for (; i < len; i++) {
D
David Updegraff 已提交
697 698 699 700 701 702 703 704 705 706
		if (*(pkt+i) == ',') {
			*(pkt+i) = '\0';
			if (port) {
				mc = pkt+i+1;
				break;
			} else {
				port = pkt+i+1;
			}
		}
	}
707
	if (!port || !mc_adr || !mc) return;
D
David Updegraff 已提交
708
	if (Multicast && MasterClient) {
709
		printf("I got a OACK as master Client, WRONG!\n");
D
David Updegraff 已提交
710 711 712 713 714
		return;
	}
	/* ..I now accept packets destined for this MCAST addr, port */
	if (!Multicast) {
		if (Bitmap) {
715
			printf("Internal failure! no mcast.\n");
D
David Updegraff 已提交
716
			free(Bitmap);
717 718
			Bitmap = NULL;
			ProhibitMcast = 1;
D
David Updegraff 已提交
719 720 721
			return ;
		}
		/* I malloc instead of pre-declare; so that if the file ends
722 723
		 * up being too big for this bitmap I can retry
		 */
724 725 726
		if (!(Bitmap = malloc(Mapsize))) {
			printf("No Bitmap, no multicast. Sorry.\n");
			ProhibitMcast = 1;
D
David Updegraff 已提交
727 728
			return;
		}
729
		memset(Bitmap, 0, Mapsize);
D
David Updegraff 已提交
730 731 732 733 734 735 736
		PrevBitmapHole = 0;
		Multicast = 1;
	}
	addr = string_to_ip(mc_adr);
	if (Mcast_addr != addr) {
		if (Mcast_addr)
			eth_mcast_join(Mcast_addr, 0);
737 738 739
		if (eth_mcast_join(Mcast_addr = addr, 1)) {
			printf("Fail to set mcast, revert to TFTP\n");
			ProhibitMcast = 1;
D
David Updegraff 已提交
740 741 742 743
			mcast_cleanup();
			NetStartAgain();
		}
	}
744 745 746
	MasterClient = (unsigned char)simple_strtoul((char *)mc, NULL, 10);
	Mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
	printf("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
D
David Updegraff 已提交
747 748 749 750
	return;
}

#endif /* Multicast TFTP */