tftp.c 24.5 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
5 6
 * Copyright 2011 Comelit Group SpA,
 *                Luca Ceresoli <luca.ceresoli@comelit.it>
W
wdenk 已提交
7 8 9 10
 */

#include <common.h>
#include <command.h>
11
#include <mapmem.h>
W
wdenk 已提交
12
#include <net.h>
13
#include <net/tftp.h>
W
wdenk 已提交
14
#include "bootp.h"
15 16 17
#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
#include <flash.h>
#endif
W
wdenk 已提交
18

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

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

42 43
static ulong timeout_ms = TIMEOUT;
static int timeout_count_max = TIMEOUT_COUNT;
S
Simon Glass 已提交
44
static ulong time_start;   /* Record time we started tftp */
45 46 47

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

58 59 60 61 62 63 64 65 66 67
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,
};

68
static struct in_addr tftp_remote_ip;
69
/* The UDP port at their end */
70
static int	tftp_remote_port;
71
/* The UDP port at our end */
72 73
static int	tftp_our_port;
static int	timeout_count;
74
/* packet sequence number */
75
static ulong	tftp_cur_block;
76
/* last packet sequence number received */
77
static ulong	tftp_prev_block;
78
/* count of sequence number wraparounds */
79
static ulong	tftp_block_wrap;
80
/* memory offset due to wrapping */
81 82
static ulong	tftp_block_wrap_offset;
static int	tftp_state;
R
Robin Getz 已提交
83
#ifdef CONFIG_TFTP_TSIZE
84
/* The file size reported by the server */
85
static int	tftp_tsize;
86
/* The number of hashes we printed */
87
static short	tftp_tsize_num_hash;
R
Robin Getz 已提交
88
#endif
S
Simon Glass 已提交
89
#ifdef CONFIG_CMD_TFTPPUT
90 91 92 93
/* 1 if writing, else 0 */
static int	tftp_put_active;
/* 1 if we have sent the last block */
static int	tftp_put_final_block_sent;
S
Simon Glass 已提交
94
#else
95
#define tftp_put_active	0
S
Simon Glass 已提交
96
#endif
W
wdenk 已提交
97

98
#define STATE_SEND_RRQ	1
W
wdenk 已提交
99 100 101
#define STATE_DATA	2
#define STATE_TOO_LARGE	3
#define STATE_BAD_MAGIC	4
W
wdenk 已提交
102
#define STATE_OACK	5
103
#define STATE_RECV_WRQ	6
S
Simon Glass 已提交
104
#define STATE_SEND_WRQ	7
W
wdenk 已提交
105

106 107 108 109
/* default TFTP block size */
#define TFTP_BLOCK_SIZE		512
/* sequence number is 16 bit */
#define TFTP_SEQUENCE_SIZE	((ulong)(1<<16))
W
wdenk 已提交
110

W
wdenk 已提交
111 112
#define DEFAULT_NAME_LEN	(8 + 4 + 1)
static char default_filename[DEFAULT_NAME_LEN];
113 114 115 116 117 118 119 120

#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 已提交
121

122 123
/* 512 is poor choice for ethernet, MTU is typically 1500.
 * Minus eth.hdrs thats 1468.  Can get 2x better throughput with
D
David Updegraff 已提交
124
 * almost-MTU block sizes.  At least try... fall back to 512 if need be.
125
 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
D
David Updegraff 已提交
126
 */
127 128 129
#ifdef CONFIG_TFTP_BLOCKSIZE
#define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
#else
D
David Updegraff 已提交
130
#define TFTP_MTU_BLOCKSIZE 1468
131 132
#endif

133 134
static unsigned short tftp_block_size = TFTP_BLOCK_SIZE;
static unsigned short tftp_block_size_option = TFTP_MTU_BLOCKSIZE;
D
David Updegraff 已提交
135 136 137 138

#ifdef CONFIG_MCAST_TFTP
#include <malloc.h>
#define MTFTP_BITMAPSIZE	0x1000
139 140 141 142 143 144 145 146 147
static unsigned *tftp_mcast_bitmap;
static int tftp_mcast_prev_hole;
static int tftp_mcast_bitmap_size = MTFTP_BITMAPSIZE;
static int tftp_mcast_disabled;
static int tftp_mcast_master_client;
static int tftp_mcast_active;
static int tftp_mcast_port;
/* can get 'last' block before done..*/
static ulong tftp_mcast_ending_block;
D
David Updegraff 已提交
148

149
static void parse_multicast_oack(char *pkt, int len);
D
David Updegraff 已提交
150

151
static void mcast_cleanup(void)
D
David Updegraff 已提交
152
{
153 154
	if (net_mcast_addr)
		eth_mcast_join(net_mcast_addr, 0);
155 156 157
	if (tftp_mcast_bitmap)
		free(tftp_mcast_bitmap);
	tftp_mcast_bitmap = NULL;
158
	net_mcast_addr.s_addr = 0;
159 160 161
	tftp_mcast_active = 0;
	tftp_mcast_port = 0;
	tftp_mcast_ending_block = -1;
D
David Updegraff 已提交
162 163 164 165
}

#endif	/* CONFIG_MCAST_TFTP */

166
static inline void store_block(int block, uchar *src, unsigned len)
W
wdenk 已提交
167
{
168
	ulong offset = block * tftp_block_size + tftp_block_wrap_offset;
W
wdenk 已提交
169
	ulong newsize = offset + len;
170
#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
W
wdenk 已提交
171 172
	int i, rc = 0;

173
	for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
W
wdenk 已提交
174
		/* start address in flash? */
J
Jochen Friedrich 已提交
175 176
		if (flash_info[i].flash_id == FLASH_UNKNOWN)
			continue;
W
wdenk 已提交
177 178 179 180 181 182 183
		if (load_addr + offset >= flash_info[i].start[0]) {
			rc = 1;
			break;
		}
	}

	if (rc) { /* Flash is destination for this packet */
184
		rc = flash_write((char *)src, (ulong)(load_addr+offset), len);
W
wdenk 已提交
185
		if (rc) {
186
			flash_perror(rc);
187
			net_set_state(NETLOOP_FAIL);
W
wdenk 已提交
188 189
			return;
		}
190
	} else
191
#endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
W
wdenk 已提交
192
	{
193 194 195 196
		void *ptr = map_sysmem(load_addr + offset, len);

		memcpy(ptr, src, len);
		unmap_sysmem(ptr);
W
wdenk 已提交
197
	}
D
David Updegraff 已提交
198
#ifdef CONFIG_MCAST_TFTP
199 200
	if (tftp_mcast_active)
		ext2_set_bit(block, tftp_mcast_bitmap);
D
David Updegraff 已提交
201
#endif
W
wdenk 已提交
202

203 204
	if (net_boot_file_size < newsize)
		net_boot_file_size = newsize;
W
wdenk 已提交
205 206
}

207
/* Clear our state ready for a new transfer */
208
static void new_transfer(void)
209
{
210 211 212
	tftp_prev_block = 0;
	tftp_block_wrap = 0;
	tftp_block_wrap_offset = 0;
213
#ifdef CONFIG_CMD_TFTPPUT
214
	tftp_put_final_block_sent = 0;
215 216 217
#endif
}

S
Simon Glass 已提交
218 219 220 221 222 223 224 225 226 227 228 229
#ifdef CONFIG_CMD_TFTPPUT
/**
 * Load the next block from memory to be sent over tftp.
 *
 * @param block	Block number to send
 * @param dst	Destination buffer for data
 * @param len	Number of bytes in block (this one and every other)
 * @return number of bytes loaded
 */
static int load_block(unsigned block, uchar *dst, unsigned len)
{
	/* We may want to get the final block from the previous set */
230
	ulong offset = ((int)block - 1) * len + tftp_block_wrap_offset;
S
Simon Glass 已提交
231 232
	ulong tosend = len;

233
	tosend = min(net_boot_file_size - offset, tosend);
S
Simon Glass 已提交
234 235
	(void)memcpy(dst, (void *)(save_addr + offset), tosend);
	debug("%s: block=%d, offset=%ld, len=%d, tosend=%ld\n", __func__,
236
	      block, offset, len, tosend);
S
Simon Glass 已提交
237 238 239 240
	return tosend;
}
#endif

241 242
static void tftp_send(void);
static void tftp_timeout_handler(void);
W
wdenk 已提交
243 244 245

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

246 247 248
static void show_block_marker(void)
{
#ifdef CONFIG_TFTP_TSIZE
249 250 251
	if (tftp_tsize) {
		ulong pos = tftp_cur_block * tftp_block_size +
			tftp_block_wrap_offset;
252 253
		if (pos > tftp_tsize)
			pos = tftp_tsize;
254

255
		while (tftp_tsize_num_hash < pos * 50 / tftp_tsize) {
256
			putc('#');
257
			tftp_tsize_num_hash++;
258
		}
S
Simon Glass 已提交
259
	} else
260
#endif
S
Simon Glass 已提交
261
	{
262
		if (((tftp_cur_block - 1) % 10) == 0)
263
			putc('#');
264
		else if ((tftp_cur_block % (10 * HASHES_PER_LINE)) == 0)
265 266 267 268
			puts("\n\t ");
	}
}

269 270 271 272 273 274 275 276 277 278 279
/**
 * restart the current transfer due to an error
 *
 * @param msg	Message to print for user
 */
static void restart(const char *msg)
{
	printf("\n%s; starting again\n", msg);
#ifdef CONFIG_MCAST_TFTP
	mcast_cleanup();
#endif
280
	net_start_again();
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
}

/*
 * Check if the block number has wrapped, and update progress
 *
 * TODO: The egregious use of global variables in this file should be tidied.
 */
static void update_block_number(void)
{
	/*
	 * 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.
	 */
296 297 298 299
	if (tftp_cur_block == 0 && tftp_prev_block != 0) {
		tftp_block_wrap++;
		tftp_block_wrap_offset += tftp_block_size * TFTP_SEQUENCE_SIZE;
		timeout_count = 0; /* we've done well, reset the timeout */
300 301 302 303 304
	} else {
		show_block_marker();
	}
}

305 306 307 308 309
/* The TFTP get or put is complete */
static void tftp_complete(void)
{
#ifdef CONFIG_TFTP_TSIZE
	/* Print hash marks for the last packet received */
310
	while (tftp_tsize && tftp_tsize_num_hash < 49) {
311
		putc('#');
312
		tftp_tsize_num_hash++;
313
	}
314
	puts("  ");
315
	print_size(tftp_tsize, "");
316
#endif
S
Simon Glass 已提交
317 318 319
	time_start = get_timer(time_start);
	if (time_start > 0) {
		puts("\n\t ");	/* Line up with "Loading: " */
320
		print_size(net_boot_file_size /
S
Simon Glass 已提交
321 322
			time_start * 1000, "/s");
	}
323
	puts("\ndone\n");
324
	net_set_state(NETLOOP_SUCCESS);
325 326
}

327
static void tftp_send(void)
W
wdenk 已提交
328
{
S
Simon Glass 已提交
329
	uchar *pkt;
330 331 332
	uchar *xp;
	int len = 0;
	ushort *s;
W
wdenk 已提交
333

334
#ifdef CONFIG_MCAST_TFTP
D
David Updegraff 已提交
335
	/* Multicast TFTP.. non-MasterClients do not ACK data. */
336 337
	if (tftp_mcast_active && tftp_state == STATE_DATA &&
	    tftp_mcast_master_client == 0)
D
David Updegraff 已提交
338 339
		return;
#endif
W
wdenk 已提交
340 341 342 343
	/*
	 *	We will always be sending some sort of packet, so
	 *	cobble together the packet headers now.
	 */
344
	pkt = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
W
wdenk 已提交
345

346
	switch (tftp_state) {
347
	case STATE_SEND_RRQ:
S
Simon Glass 已提交
348
	case STATE_SEND_WRQ:
W
wdenk 已提交
349
		xp = pkt;
W
Wolfgang Denk 已提交
350
		s = (ushort *)pkt;
351
#ifdef CONFIG_CMD_TFTPPUT
352
		*s++ = htons(tftp_state == STATE_SEND_RRQ ? TFTP_RRQ :
S
Simon Glass 已提交
353
			TFTP_WRQ);
354 355 356
#else
		*s++ = htons(TFTP_RRQ);
#endif
W
Wolfgang Denk 已提交
357
		pkt = (uchar *)s;
358
		strcpy((char *)pkt, tftp_filename);
W
wdenk 已提交
359
		pkt += strlen(tftp_filename) + 1;
360
		strcpy((char *)pkt, "octet");
W
wdenk 已提交
361
		pkt += 5 /*strlen("octet")*/ + 1;
362
		strcpy((char *)pkt, "timeout");
W
wdenk 已提交
363
		pkt += 7 /*strlen("timeout")*/ + 1;
364
		sprintf((char *)pkt, "%lu", timeout_ms / 1000);
R
Robin Getz 已提交
365
		debug("send option \"timeout %s\"\n", (char *)pkt);
W
wdenk 已提交
366
		pkt += strlen((char *)pkt) + 1;
R
Robin Getz 已提交
367
#ifdef CONFIG_TFTP_TSIZE
368 369
		pkt += sprintf((char *)pkt, "tsize%c%u%c",
				0, net_boot_file_size, 0);
R
Robin Getz 已提交
370
#endif
D
David Updegraff 已提交
371
		/* try for more effic. blk size */
372
		pkt += sprintf((char *)pkt, "blksize%c%d%c",
373
				0, tftp_block_size_option, 0);
374
#ifdef CONFIG_MCAST_TFTP
D
David Updegraff 已提交
375
		/* Check all preconditions before even trying the option */
376 377 378 379 380
		if (!tftp_mcast_disabled) {
			tftp_mcast_bitmap = malloc(tftp_mcast_bitmap_size);
			if (tftp_mcast_bitmap && eth_get_dev()->mcast) {
				free(tftp_mcast_bitmap);
				tftp_mcast_bitmap = NULL;
381 382 383
				pkt += sprintf((char *)pkt, "multicast%c%c",
					0, 0);
			}
D
David Updegraff 已提交
384 385
		}
#endif /* CONFIG_MCAST_TFTP */
W
wdenk 已提交
386 387 388
		len = pkt - xp;
		break;

W
wdenk 已提交
389
	case STATE_OACK:
D
David Updegraff 已提交
390
#ifdef CONFIG_MCAST_TFTP
391 392 393 394 395 396
		/* My turn!  Start at where I need blocks I missed. */
		if (tftp_mcast_active)
			tftp_cur_block = ext2_find_next_zero_bit(
				tftp_mcast_bitmap,
				tftp_mcast_bitmap_size * 8, 0);
		/* fall through */
D
David Updegraff 已提交
397
#endif
398 399

	case STATE_RECV_WRQ:
D
David Updegraff 已提交
400
	case STATE_DATA:
W
wdenk 已提交
401
		xp = pkt;
W
Wolfgang Denk 已提交
402
		s = (ushort *)pkt;
S
Simon Glass 已提交
403
		s[0] = htons(TFTP_ACK);
404
		s[1] = htons(tftp_cur_block);
S
Simon Glass 已提交
405 406
		pkt = (uchar *)(s + 2);
#ifdef CONFIG_CMD_TFTPPUT
407 408 409
		if (tftp_put_active) {
			int toload = tftp_block_size;
			int loaded = load_block(tftp_cur_block, pkt, toload);
S
Simon Glass 已提交
410 411 412

			s[0] = htons(TFTP_DATA);
			pkt += loaded;
413
			tftp_put_final_block_sent = (loaded < toload);
S
Simon Glass 已提交
414 415
		}
#endif
W
wdenk 已提交
416 417 418 419 420
		len = pkt - xp;
		break;

	case STATE_TOO_LARGE:
		xp = pkt;
W
Wolfgang Denk 已提交
421 422
		s = (ushort *)pkt;
		*s++ = htons(TFTP_ERROR);
S
Simon Glass 已提交
423 424
			*s++ = htons(3);

W
Wolfgang Denk 已提交
425
		pkt = (uchar *)s;
426
		strcpy((char *)pkt, "File too large");
W
wdenk 已提交
427 428 429 430 431 432
		pkt += 14 /*strlen("File too large")*/ + 1;
		len = pkt - xp;
		break;

	case STATE_BAD_MAGIC:
		xp = pkt;
W
Wolfgang Denk 已提交
433 434 435 436
		s = (ushort *)pkt;
		*s++ = htons(TFTP_ERROR);
		*s++ = htons(2);
		pkt = (uchar *)s;
437
		strcpy((char *)pkt, "File has bad magic");
W
wdenk 已提交
438 439 440 441 442
		pkt += 18 /*strlen("File has bad magic")*/ + 1;
		len = pkt - xp;
		break;
	}

443 444
	net_send_udp_packet(net_server_ethaddr, tftp_remote_ip,
			    tftp_remote_port, tftp_our_port, len);
W
wdenk 已提交
445 446
}

447
#ifdef CONFIG_CMD_TFTPPUT
S
Simon Glass 已提交
448
static void icmp_handler(unsigned type, unsigned code, unsigned dest,
449 450
			 struct in_addr sip, unsigned src, uchar *pkt,
			 unsigned len)
S
Simon Glass 已提交
451 452 453 454 455 456
{
	if (type == ICMP_NOT_REACH && code == ICMP_NOT_REACH_PORT) {
		/* Oh dear the other end has gone away */
		restart("TFTP server died");
	}
}
457
#endif
S
Simon Glass 已提交
458

459 460
static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
			 unsigned src, unsigned len)
W
wdenk 已提交
461
{
K
Kim Phillips 已提交
462 463
	__be16 proto;
	__be16 *s;
464
	int i;
W
wdenk 已提交
465

466
	if (dest != tftp_our_port) {
D
David Updegraff 已提交
467
#ifdef CONFIG_MCAST_TFTP
468 469
		if (tftp_mcast_active &&
		    (!tftp_mcast_port || dest != tftp_mcast_port))
D
David Updegraff 已提交
470
#endif
471
			return;
W
wdenk 已提交
472
	}
473 474
	if (tftp_state != STATE_SEND_RRQ && src != tftp_remote_port &&
	    tftp_state != STATE_RECV_WRQ && tftp_state != STATE_SEND_WRQ)
W
wdenk 已提交
475 476
		return;

477
	if (len < 2)
W
wdenk 已提交
478 479 480
		return;
	len -= 2;
	/* warning: don't use increment (++) in ntohs() macros!! */
K
Kim Phillips 已提交
481
	s = (__be16 *)pkt;
W
Wolfgang Denk 已提交
482 483
	proto = *s++;
	pkt = (uchar *)s;
W
wdenk 已提交
484 485
	switch (ntohs(proto)) {
	case TFTP_RRQ:
S
Simon Glass 已提交
486 487
		break;

W
wdenk 已提交
488
	case TFTP_ACK:
S
Simon Glass 已提交
489
#ifdef CONFIG_CMD_TFTPPUT
490 491
		if (tftp_put_active) {
			if (tftp_put_final_block_sent) {
S
Simon Glass 已提交
492 493 494 495 496 497 498
				tftp_complete();
			} else {
				/*
				 * Move to the next block. We want our block
				 * count to wrap just like the other end!
				 */
				int block = ntohs(*s);
499
				int ack_ok = (tftp_cur_block == block);
S
Simon Glass 已提交
500

501
				tftp_cur_block = (unsigned short)(block + 1);
S
Simon Glass 已提交
502 503
				update_block_number();
				if (ack_ok)
504
					tftp_send(); /* Send next data block */
S
Simon Glass 已提交
505 506 507
			}
		}
#endif
W
wdenk 已提交
508
		break;
S
Simon Glass 已提交
509

W
wdenk 已提交
510 511 512
	default:
		break;

513 514 515
#ifdef CONFIG_CMD_TFTPSRV
	case TFTP_WRQ:
		debug("Got WRQ\n");
516
		tftp_remote_ip = sip;
517 518
		tftp_remote_port = src;
		tftp_our_port = 1024 + (get_timer(0) % 3072);
519
		new_transfer();
520
		tftp_send(); /* Send ACK(0) */
521 522 523
		break;
#endif

W
wdenk 已提交
524
	case TFTP_OACK:
525
		debug("Got OACK: %s %s\n",
526 527 528
		      pkt, pkt + strlen((char *)pkt) + 1);
		tftp_state = STATE_OACK;
		tftp_remote_port = src;
529 530 531 532 533
		/*
		 * Check for 'blksize' option.
		 * Careful: "i" is signed, "len" is unsigned, thus
		 * something like "len-8" may give a *huge* number
		 */
534
		for (i = 0; i+8 < len; i++) {
535 536 537 538
			if (strcmp((char *)pkt + i, "blksize") == 0) {
				tftp_block_size = (unsigned short)
					simple_strtoul((char *)pkt + i + 8,
						       NULL, 10);
R
Robin Getz 已提交
539
				debug("Blocksize ack: %s, %d\n",
540
				      (char *)pkt + i + 8, tftp_block_size);
541
			}
R
Robin Getz 已提交
542
#ifdef CONFIG_TFTP_TSIZE
543
			if (strcmp((char *)pkt+i, "tsize") == 0) {
544
				tftp_tsize = simple_strtoul((char *)pkt + i + 6,
545
							   NULL, 10);
R
Robin Getz 已提交
546
				debug("size = %s, %d\n",
547
				      (char *)pkt + i + 6, tftp_tsize);
R
Robin Getz 已提交
548 549
			}
#endif
D
David Updegraff 已提交
550 551
		}
#ifdef CONFIG_MCAST_TFTP
552 553 554
		parse_multicast_oack((char *)pkt, len - 1);
		if ((tftp_mcast_active) && (!tftp_mcast_master_client))
			tftp_state = STATE_DATA;	/* passive.. */
D
David Updegraff 已提交
555 556
		else
#endif
S
Simon Glass 已提交
557
#ifdef CONFIG_CMD_TFTPPUT
558
		if (tftp_put_active) {
S
Simon Glass 已提交
559
			/* Get ready to send the first block */
560 561
			tftp_state = STATE_DATA;
			tftp_cur_block++;
S
Simon Glass 已提交
562 563
		}
#endif
564
		tftp_send(); /* Send ACK or first data block */
W
wdenk 已提交
565
		break;
W
wdenk 已提交
566 567 568 569
	case TFTP_DATA:
		if (len < 2)
			return;
		len -= 2;
570
		tftp_cur_block = ntohs(*(__be16 *)pkt);
W
wdenk 已提交
571

572
		update_block_number();
W
wdenk 已提交
573

574
		if (tftp_state == STATE_SEND_RRQ)
R
Robin Getz 已提交
575
			debug("Server did not acknowledge timeout option!\n");
W
wdenk 已提交
576

577 578
		if (tftp_state == STATE_SEND_RRQ || tftp_state == STATE_OACK ||
		    tftp_state == STATE_RECV_WRQ) {
W
wdenk 已提交
579
			/* first block received */
580 581
			tftp_state = STATE_DATA;
			tftp_remote_port = src;
582
			new_transfer();
W
wdenk 已提交
583

D
David Updegraff 已提交
584
#ifdef CONFIG_MCAST_TFTP
585 586
			if (tftp_mcast_active) { /* start!=1 common if mcast */
				tftp_prev_block = tftp_cur_block - 1;
D
David Updegraff 已提交
587 588
			} else
#endif
589 590 591 592 593
			if (tftp_cur_block != 1) {	/* Assertion */
				puts("\nTFTP error: ");
				printf("First block is not block 1 (%ld)\n",
				       tftp_cur_block);
				puts("Starting again\n\n");
594
				net_start_again();
W
wdenk 已提交
595 596 597 598
				break;
			}
		}

599 600
		if (tftp_cur_block == tftp_prev_block) {
			/* Same block again; ignore it. */
W
wdenk 已提交
601 602 603
			break;
		}

604
		tftp_prev_block = tftp_cur_block;
605
		timeout_count_max = tftp_timeout_count_max;
606
		net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
W
wdenk 已提交
607

608
		store_block(tftp_cur_block - 1, pkt + 2, len);
W
wdenk 已提交
609 610

		/*
L
Luca Ceresoli 已提交
611
		 *	Acknowledge the block just received, which will prompt
612
		 *	the remote for the next one.
W
wdenk 已提交
613
		 */
D
David Updegraff 已提交
614
#ifdef CONFIG_MCAST_TFTP
615 616
		/* if I am the MasterClient, actively calculate what my next
		 * needed block is; else I'm passive; not ACKING
617
		 */
618 619 620 621 622 623 624 625 626 627 628 629
		if (tftp_mcast_active) {
			if (len < tftp_block_size)  {
				tftp_mcast_ending_block = tftp_cur_block;
			} else if (tftp_mcast_master_client) {
				tftp_mcast_prev_hole = ext2_find_next_zero_bit(
					tftp_mcast_bitmap,
					tftp_mcast_bitmap_size * 8,
					tftp_mcast_prev_hole);
				tftp_cur_block = tftp_mcast_prev_hole;
				if (tftp_cur_block >
				    ((tftp_mcast_bitmap_size * 8) - 1)) {
					debug("tftpfile too big\n");
D
David Updegraff 已提交
630
					/* try to double it and retry */
631
					tftp_mcast_bitmap_size <<= 1;
D
David Updegraff 已提交
632
					mcast_cleanup();
633
					net_start_again();
D
David Updegraff 已提交
634 635
					return;
				}
636
				tftp_prev_block = tftp_cur_block;
D
David Updegraff 已提交
637 638 639
			}
		}
#endif
640
		tftp_send();
W
wdenk 已提交
641

D
David Updegraff 已提交
642
#ifdef CONFIG_MCAST_TFTP
643 644 645
		if (tftp_mcast_active) {
			if (tftp_mcast_master_client &&
			    (tftp_cur_block >= tftp_mcast_ending_block)) {
646
				puts("\nMulticast tftp done\n");
D
David Updegraff 已提交
647
				mcast_cleanup();
648
				net_set_state(NETLOOP_SUCCESS);
649
			}
650
		} else
D
David Updegraff 已提交
651
#endif
652
		if (len < tftp_block_size)
653
			tftp_complete();
W
wdenk 已提交
654 655 656
		break;

	case TFTP_ERROR:
657
		printf("\nTFTP error: '%s' (%d)\n",
K
Kim Phillips 已提交
658
		       pkt + 2, ntohs(*(__be16 *)pkt));
659

K
Kim Phillips 已提交
660
		switch (ntohs(*(__be16 *)pkt)) {
661 662 663 664
		case TFTP_ERR_FILE_NOT_FOUND:
		case TFTP_ERR_ACCESS_DENIED:
			puts("Not retrying...\n");
			eth_halt();
665
			net_set_state(NETLOOP_FAIL);
666 667 668 669 670 671 672 673
			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 已提交
674
#ifdef CONFIG_MCAST_TFTP
675
			mcast_cleanup();
D
David Updegraff 已提交
676
#endif
677
			net_start_again();
678 679
			break;
		}
W
wdenk 已提交
680 681 682 683 684
		break;
	}
}


685
static void tftp_timeout_handler(void)
W
wdenk 已提交
686
{
687
	if (++timeout_count > timeout_count_max) {
688
		restart("Retry count exceeded");
W
wdenk 已提交
689
	} else {
690
		puts("T ");
691
		net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
692 693
		if (tftp_state != STATE_RECV_WRQ)
			tftp_send();
W
wdenk 已提交
694 695 696 697
	}
}


698
void tftp_start(enum proto_t protocol)
W
wdenk 已提交
699
{
700
#if CONFIG_NET_TFTP_VARS
701
	char *ep;             /* Environment pointer */
702

703 704 705 706
	/*
	 * Allow the user to choose TFTP blocksize and timeout.
	 * TFTP protocol has a minimal timeout of 1 second.
	 */
707

708 709
	ep = getenv("tftpblocksize");
	if (ep != NULL)
710
		tftp_block_size_option = simple_strtol(ep, NULL, 10);
711

712 713
	ep = getenv("tftptimeout");
	if (ep != NULL)
714
		timeout_ms = simple_strtol(ep, NULL, 10);
715

716 717
	if (timeout_ms < 1000) {
		printf("TFTP timeout (%ld ms) too low, set min = 1000 ms\n",
718
		       timeout_ms);
719
		timeout_ms = 1000;
720 721
	}

722 723 724 725 726 727 728 729 730 731 732
	ep = getenv("tftptimeoutcountmax");
	if (ep != NULL)
		tftp_timeout_count_max = simple_strtol(ep, NULL, 10);

	if (tftp_timeout_count_max < 0) {
		printf("TFTP timeout count max (%d ms) negative, set to 0\n",
		       tftp_timeout_count_max);
		tftp_timeout_count_max = 0;
	}
#endif

733
	debug("TFTP blocksize = %i, timeout = %ld ms\n",
734
	      tftp_block_size_option, timeout_ms);
735

736
	tftp_remote_ip = net_server_ip;
737
	if (net_boot_file_name[0] == '\0') {
738
		sprintf(default_filename, "%02X%02X%02X%02X.img",
739 740 741 742
			net_ip.s_addr & 0xFF,
			(net_ip.s_addr >>  8) & 0xFF,
			(net_ip.s_addr >> 16) & 0xFF,
			(net_ip.s_addr >> 24) & 0xFF);
743 744

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

747
		printf("*** Warning: no boot file name; using '%s'\n",
748
		       tftp_filename);
W
wdenk 已提交
749
	} else {
750
		char *p = strchr(net_boot_file_name, ':');
751 752

		if (p == NULL) {
753
			strncpy(tftp_filename, net_boot_file_name, MAX_LEN);
754
			tftp_filename[MAX_LEN - 1] = 0;
755
		} else {
756
			tftp_remote_ip = string_to_ip(net_boot_file_name);
P
Peter Tyser 已提交
757
			strncpy(tftp_filename, p + 1, MAX_LEN);
758
			tftp_filename[MAX_LEN - 1] = 0;
759
		}
W
wdenk 已提交
760 761
	}

762
	printf("Using %s device\n", eth_get_name());
S
Simon Glass 已提交
763
	printf("TFTP %s server %pI4; our IP address is %pI4",
764 765 766
#ifdef CONFIG_CMD_TFTPPUT
	       protocol == TFTPPUT ? "to" : "from",
#else
767
	       "from",
768
#endif
769
	       &tftp_remote_ip, &net_ip);
W
wdenk 已提交
770 771

	/* Check if we need to send across this subnet */
772 773 774 775 776 777 778 779
	if (net_gateway.s_addr && net_netmask.s_addr) {
		struct in_addr our_net;
		struct in_addr remote_net;

		our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
		remote_net.s_addr = tftp_remote_ip.s_addr & net_netmask.s_addr;
		if (our_net.s_addr != remote_net.s_addr)
			printf("; sending through gateway %pI4", &net_gateway);
W
wdenk 已提交
780
	}
781
	putc('\n');
W
wdenk 已提交
782

783
	printf("Filename '%s'.", tftp_filename);
W
wdenk 已提交
784

785 786 787 788
	if (net_boot_file_expected_size_in_blocks) {
		printf(" Size is 0x%x Bytes = ",
		       net_boot_file_expected_size_in_blocks << 9);
		print_size(net_boot_file_expected_size_in_blocks << 9, "");
W
wdenk 已提交
789 790
	}

791
	putc('\n');
S
Simon Glass 已提交
792
#ifdef CONFIG_CMD_TFTPPUT
793 794
	tftp_put_active = (protocol == TFTPPUT);
	if (tftp_put_active) {
S
Simon Glass 已提交
795 796
		printf("Save address: 0x%lx\n", save_addr);
		printf("Save size:    0x%lx\n", save_size);
797
		net_boot_file_size = save_size;
S
Simon Glass 已提交
798
		puts("Saving: *\b");
799
		tftp_state = STATE_SEND_WRQ;
S
Simon Glass 已提交
800 801 802 803 804 805
		new_transfer();
	} else
#endif
	{
		printf("Load address: 0x%lx\n", load_addr);
		puts("Loading: *\b");
806
		tftp_state = STATE_SEND_RRQ;
S
Simon Glass 已提交
807
	}
W
wdenk 已提交
808

S
Simon Glass 已提交
809
	time_start = get_timer(0);
810
	timeout_count_max = tftp_timeout_count_max;
811

812
	net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
813
	net_set_udp_handler(tftp_handler);
814
#ifdef CONFIG_CMD_TFTPPUT
S
Simon Glass 已提交
815
	net_set_icmp_handler(icmp_handler);
816
#endif
817 818
	tftp_remote_port = WELL_KNOWN_PORT;
	timeout_count = 0;
819
	/* Use a pseudo-random port unless a specific port is set */
820
	tftp_our_port = 1024 + (get_timer(0) % 3072);
D
David Updegraff 已提交
821

822
#ifdef CONFIG_TFTP_PORT
823
	ep = getenv("tftpdstp");
824
	if (ep != NULL)
825
		tftp_remote_port = simple_strtol(ep, NULL, 10);
826
	ep = getenv("tftpsrcp");
827
	if (ep != NULL)
828
		tftp_our_port = simple_strtol(ep, NULL, 10);
829
#endif
830
	tftp_cur_block = 0;
W
wdenk 已提交
831

W
wdenk 已提交
832
	/* zero out server ether in case the server ip has changed */
833
	memset(net_server_ethaddr, 0, 6);
834 835
	/* Revert tftp_block_size to dflt */
	tftp_block_size = TFTP_BLOCK_SIZE;
D
David Updegraff 已提交
836
#ifdef CONFIG_MCAST_TFTP
837
	mcast_cleanup();
D
David Updegraff 已提交
838
#endif
R
Robin Getz 已提交
839
#ifdef CONFIG_TFTP_TSIZE
840 841
	tftp_tsize = 0;
	tftp_tsize_num_hash = 0;
R
Robin Getz 已提交
842
#endif
W
wdenk 已提交
843

844
	tftp_send();
W
wdenk 已提交
845 846
}

847
#ifdef CONFIG_CMD_TFTPSRV
848
void tftp_start_server(void)
849 850 851 852
{
	tftp_filename[0] = 0;

	printf("Using %s device\n", eth_get_name());
853
	printf("Listening for TFTP transfer on %pI4\n", &net_ip);
854 855 856 857
	printf("Load address: 0x%lx\n", load_addr);

	puts("Loading: *\b");

858
	timeout_count_max = tftp_timeout_count_max;
859 860
	timeout_count = 0;
	timeout_ms = TIMEOUT;
861
	net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
862

863 864 865 866
	/* Revert tftp_block_size to dflt */
	tftp_block_size = TFTP_BLOCK_SIZE;
	tftp_cur_block = 0;
	tftp_our_port = WELL_KNOWN_PORT;
867 868

#ifdef CONFIG_TFTP_TSIZE
869 870
	tftp_tsize = 0;
	tftp_tsize_num_hash = 0;
871 872
#endif

873
	tftp_state = STATE_RECV_WRQ;
874
	net_set_udp_handler(tftp_handler);
875 876

	/* zero out server ether in case the server ip has changed */
877
	memset(net_server_ethaddr, 0, 6);
878 879 880
}
#endif /* CONFIG_CMD_TFTPSRV */

D
David Updegraff 已提交
881
#ifdef CONFIG_MCAST_TFTP
882 883
/*
 * Credits: atftp project.
D
David Updegraff 已提交
884 885
 */

886 887
/*
 * Pick up BcastAddr, Port, and whether I am [now] the master-client.
D
David Updegraff 已提交
888 889 890 891 892 893 894 895 896 897 898 899 900
 * 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)
{
901
	int i;
902
	struct in_addr addr;
903 904 905
	char *mc_adr;
	char *port;
	char *mc;
D
David Updegraff 已提交
906

907 908 909
	mc_adr = NULL;
	port = NULL;
	mc = NULL;
D
David Updegraff 已提交
910 911 912
	/* march along looking for 'multicast\0', which has to start at least
	 * 14 bytes back from the end.
	 */
913 914
	for (i = 0; i < len - 14; i++)
		if (strcmp(pkt + i, "multicast") == 0)
D
David Updegraff 已提交
915
			break;
916
	if (i >= (len - 14)) /* non-Multicast OACK, ign. */
D
David Updegraff 已提交
917
		return;
918

919
	i += 10; /* strlen multicast */
920
	mc_adr = pkt + i;
921
	for (; i < len; i++) {
922 923
		if (*(pkt + i) == ',') {
			*(pkt + i) = '\0';
D
David Updegraff 已提交
924
			if (port) {
925
				mc = pkt + i + 1;
D
David Updegraff 已提交
926 927
				break;
			} else {
928
				port = pkt + i + 1;
D
David Updegraff 已提交
929 930 931
			}
		}
	}
932 933
	if (!port || !mc_adr || !mc)
		return;
934
	if (tftp_mcast_active && tftp_mcast_master_client) {
935
		printf("I got a OACK as master Client, WRONG!\n");
D
David Updegraff 已提交
936 937 938
		return;
	}
	/* ..I now accept packets destined for this MCAST addr, port */
939 940
	if (!tftp_mcast_active) {
		if (tftp_mcast_bitmap) {
941
			printf("Internal failure! no mcast.\n");
942 943 944 945
			free(tftp_mcast_bitmap);
			tftp_mcast_bitmap = NULL;
			tftp_mcast_disabled = 1;
			return;
D
David Updegraff 已提交
946 947
		}
		/* I malloc instead of pre-declare; so that if the file ends
948 949
		 * up being too big for this bitmap I can retry
		 */
950 951 952 953
		tftp_mcast_bitmap = malloc(tftp_mcast_bitmap_size);
		if (!tftp_mcast_bitmap) {
			printf("No bitmap, no multicast. Sorry.\n");
			tftp_mcast_disabled = 1;
D
David Updegraff 已提交
954 955
			return;
		}
956 957 958
		memset(tftp_mcast_bitmap, 0, tftp_mcast_bitmap_size);
		tftp_mcast_prev_hole = 0;
		tftp_mcast_active = 1;
D
David Updegraff 已提交
959 960
	}
	addr = string_to_ip(mc_adr);
961 962 963 964 965
	if (net_mcast_addr.s_addr != addr.s_addr) {
		if (net_mcast_addr.s_addr)
			eth_mcast_join(net_mcast_addr, 0);
		net_mcast_addr = addr;
		if (eth_mcast_join(net_mcast_addr, 1)) {
966
			printf("Fail to set mcast, revert to TFTP\n");
967
			tftp_mcast_disabled = 1;
D
David Updegraff 已提交
968
			mcast_cleanup();
969
			net_start_again();
D
David Updegraff 已提交
970 971
		}
	}
972 973 974 975
	tftp_mcast_master_client = simple_strtoul((char *)mc, NULL, 10);
	tftp_mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
	printf("Multicast: %s:%d [%d]\n", mc_adr, tftp_mcast_port,
	       tftp_mcast_master_client);
D
David Updegraff 已提交
976 977 978 979
	return;
}

#endif /* Multicast TFTP */