Blame view

net/tftp.c 19.7 KB
fe8c2806c   wdenk   Initial revision
1
  /*
2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
2
3
4
   * Copyright 1994, 1995, 2000 Neil Russell.
   * (See License)
   * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
5
6
   * Copyright 2011 Comelit Group SpA,
   *                Luca Ceresoli <luca.ceresoli@comelit.it>
fe8c2806c   wdenk   Initial revision
7
8
9
10
   */
  
  #include <common.h>
  #include <command.h>
0efe1bcf5   Alexander Graf   efi_loader: Add n...
11
  #include <efi_loader.h>
7b51b576d   Simon Glass   env: Move env_get...
12
  #include <env.h>
8e8ccfe1a   Simon Glass   common: Move the ...
13
  #include <image.h>
55d5fd9a8   Joe Hershberger   net: Access mappe...
14
  #include <mapmem.h>
fe8c2806c   wdenk   Initial revision
15
  #include <net.h>
346969584   Lukasz Majewski   net: tftp: Move t...
16
  #include <net/tftp.h>
fe8c2806c   wdenk   Initial revision
17
  #include "bootp.h"
13dfe9437   Joe Hershberger   net: cosmetic: tf...
18
19
20
  #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
  #include <flash.h>
  #endif
fe8c2806c   wdenk   Initial revision
21

a156c47e3   Simon Goldschmidt   tftp: prevent ove...
22
  DECLARE_GLOBAL_DATA_PTR;
2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
23
24
25
  /* Well known TFTP port # */
  #define WELL_KNOWN_PORT	69
  /* Millisecs to timeout for lost pkt */
af2ca59e6   Bin Meng   net: Revert "tftp...
26
  #define TIMEOUT		5000UL
fe8c2806c   wdenk   Initial revision
27
  #ifndef	CONFIG_NET_RETRY_COUNT
2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
28
  /* # of timeouts before giving up */
af2ca59e6   Bin Meng   net: Revert "tftp...
29
  # define TIMEOUT_COUNT	10
fe8c2806c   wdenk   Initial revision
30
31
32
  #else
  # define TIMEOUT_COUNT  (CONFIG_NET_RETRY_COUNT * 2)
  #endif
2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
33
34
  /* Number of "loading" hashes per line (for checking the image size) */
  #define HASHES_PER_LINE	65
fe8c2806c   wdenk   Initial revision
35
36
37
38
39
40
41
42
43
  
  /*
   *	TFTP operations.
   */
  #define TFTP_RRQ	1
  #define TFTP_WRQ	2
  #define TFTP_DATA	3
  #define TFTP_ACK	4
  #define TFTP_ERROR	5
fbe4b5cbd   wdenk   * Update TRAB aut...
44
  #define TFTP_OACK	6
fe8c2806c   wdenk   Initial revision
45

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
46
47
  static ulong timeout_ms = TIMEOUT;
  static int timeout_count_max = TIMEOUT_COUNT;
85b198027   Simon Glass   net: Add tftp spe...
48
  static ulong time_start;   /* Record time we started tftp */
e83cc0637   Bartlomiej Sieka   net: Make TFTP se...
49
50
51
  
  /*
   * These globals govern the timeout behavior when attempting a connection to a
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
52
   * TFTP server. tftp_timeout_ms specifies the number of milliseconds to
e83cc0637   Bartlomiej Sieka   net: Make TFTP se...
53
   * wait for the server to respond to initial connection. Second global,
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
54
55
   * 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
e83cc0637   Bartlomiej Sieka   net: Make TFTP se...
56
57
58
   * positive. The globals are meant to be set (and restored) by code needing
   * non-standard timeout behavior when initiating a TFTP transfer.
   */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
59
60
  ulong tftp_timeout_ms = TIMEOUT;
  int tftp_timeout_count_max = TIMEOUT_COUNT;
e83cc0637   Bartlomiej Sieka   net: Make TFTP se...
61

aafda38fb   Remy Bohmer   Add error codes/h...
62
63
64
65
66
67
68
69
70
  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,
  };
049a95a77   Joe Hershberger   net: cosmetic: Ch...
71
  static struct in_addr tftp_remote_ip;
2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
72
  /* The UDP port at their end */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
73
  static int	tftp_remote_port;
2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
74
  /* The UDP port at our end */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
75
76
  static int	tftp_our_port;
  static int	timeout_count;
2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
77
  /* packet sequence number */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
78
  static ulong	tftp_cur_block;
2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
79
  /* last packet sequence number received */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
80
  static ulong	tftp_prev_block;
2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
81
  /* count of sequence number wraparounds */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
82
  static ulong	tftp_block_wrap;
2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
83
  /* memory offset due to wrapping */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
84
85
  static ulong	tftp_block_wrap_offset;
  static int	tftp_state;
a156c47e3   Simon Goldschmidt   tftp: prevent ove...
86
87
88
89
  static ulong	tftp_load_addr;
  #ifdef CONFIG_LMB
  static ulong	tftp_load_size;
  #endif
4fccb818e   Robin Getz   Add Transfer Size...
90
  #ifdef CONFIG_TFTP_TSIZE
2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
91
  /* The file size reported by the server */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
92
  static int	tftp_tsize;
2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
93
  /* The number of hashes we printed */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
94
  static short	tftp_tsize_num_hash;
4fccb818e   Robin Getz   Add Transfer Size...
95
  #endif
1fb7cd498   Simon Glass   net: tftpput: imp...
96
  #ifdef CONFIG_CMD_TFTPPUT
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
97
98
99
100
  /* 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;
1fb7cd498   Simon Glass   net: tftpput: imp...
101
  #else
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
102
  #define tftp_put_active	0
1fb7cd498   Simon Glass   net: tftpput: imp...
103
  #endif
3f85ce278   wdenk   * CVS add missing...
104

e3fb0abe2   Luca Ceresoli   TFTP: rename STAT...
105
  #define STATE_SEND_RRQ	1
fe8c2806c   wdenk   Initial revision
106
107
108
  #define STATE_DATA	2
  #define STATE_TOO_LARGE	3
  #define STATE_BAD_MAGIC	4
fbe4b5cbd   wdenk   * Update TRAB aut...
109
  #define STATE_OACK	5
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
110
  #define STATE_RECV_WRQ	6
1fb7cd498   Simon Glass   net: tftpput: imp...
111
  #define STATE_SEND_WRQ	7
fe8c2806c   wdenk   Initial revision
112

2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
113
114
115
116
  /* default TFTP block size */
  #define TFTP_BLOCK_SIZE		512
  /* sequence number is 16 bit */
  #define TFTP_SEQUENCE_SIZE	((ulong)(1<<16))
3f85ce278   wdenk   * CVS add missing...
117

fe8c2806c   wdenk   Initial revision
118
119
  #define DEFAULT_NAME_LEN	(8 + 4 + 1)
  static char default_filename[DEFAULT_NAME_LEN];
a93907c43   Jean-Christophe PLAGNIOL-VILLARD   TFTP: add host ip...
120
121
122
123
124
125
126
127
  
  #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];
fe8c2806c   wdenk   Initial revision
128

85eb5caf6   Wolfgang Denk   Coding style clea...
129
130
  /* 512 is poor choice for ethernet, MTU is typically 1500.
   * Minus eth.hdrs thats 1468.  Can get 2x better throughput with
53a5c424b   David Updegraff   multicast tftp: R...
131
   * almost-MTU block sizes.  At least try... fall back to 512 if need be.
89ba81d10   Alessandro Rubini   tftp: get the tft...
132
   * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
53a5c424b   David Updegraff   multicast tftp: R...
133
   */
89ba81d10   Alessandro Rubini   tftp: get the tft...
134
135
136
  #ifdef CONFIG_TFTP_BLOCKSIZE
  #define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
  #else
53a5c424b   David Updegraff   multicast tftp: R...
137
  #define TFTP_MTU_BLOCKSIZE 1468
89ba81d10   Alessandro Rubini   tftp: get the tft...
138
  #endif
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
139
140
  static unsigned short tftp_block_size = TFTP_BLOCK_SIZE;
  static unsigned short tftp_block_size_option = TFTP_MTU_BLOCKSIZE;
53a5c424b   David Updegraff   multicast tftp: R...
141

a156c47e3   Simon Goldschmidt   tftp: prevent ove...
142
  static inline int store_block(int block, uchar *src, unsigned int len)
fe8c2806c   wdenk   Initial revision
143
  {
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
144
  	ulong offset = block * tftp_block_size + tftp_block_wrap_offset;
3f85ce278   wdenk   * CVS add missing...
145
  	ulong newsize = offset + len;
a156c47e3   Simon Goldschmidt   tftp: prevent ove...
146
  	ulong store_addr = tftp_load_addr + offset;
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
147
  #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
fe8c2806c   wdenk   Initial revision
148
  	int i, rc = 0;
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
149
  	for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
fe8c2806c   wdenk   Initial revision
150
  		/* start address in flash? */
be1b0d277   Jochen Friedrich   Don't tftp to unk...
151
152
  		if (flash_info[i].flash_id == FLASH_UNKNOWN)
  			continue;
a156c47e3   Simon Goldschmidt   tftp: prevent ove...
153
  		if (store_addr >= flash_info[i].start[0]) {
fe8c2806c   wdenk   Initial revision
154
155
156
157
158
159
  			rc = 1;
  			break;
  		}
  	}
  
  	if (rc) { /* Flash is destination for this packet */
a156c47e3   Simon Goldschmidt   tftp: prevent ove...
160
  		rc = flash_write((char *)src, store_addr, len);
fe8c2806c   wdenk   Initial revision
161
  		if (rc) {
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
162
  			flash_perror(rc);
a156c47e3   Simon Goldschmidt   tftp: prevent ove...
163
  			return rc;
fe8c2806c   wdenk   Initial revision
164
  		}
13dfe9437   Joe Hershberger   net: cosmetic: tf...
165
  	} else
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
166
  #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
fe8c2806c   wdenk   Initial revision
167
  	{
a156c47e3   Simon Goldschmidt   tftp: prevent ove...
168
169
170
  		void *ptr;
  
  #ifdef CONFIG_LMB
ca48cb402   Bin Meng   net: tftp: Fix tf...
171
172
173
174
  		ulong end_addr = tftp_load_addr + tftp_load_size;
  
  		if (!end_addr)
  			end_addr = ULONG_MAX;
a156c47e3   Simon Goldschmidt   tftp: prevent ove...
175
  		if (store_addr < tftp_load_addr ||
ca48cb402   Bin Meng   net: tftp: Fix tf...
176
  		    store_addr + len > end_addr) {
a156c47e3   Simon Goldschmidt   tftp: prevent ove...
177
178
179
180
181
182
183
184
  			puts("
  TFTP error: ");
  			puts("trying to overwrite reserved memory...
  ");
  			return -1;
  		}
  #endif
  		ptr = map_sysmem(store_addr, len);
55d5fd9a8   Joe Hershberger   net: Access mappe...
185
186
  		memcpy(ptr, src, len);
  		unmap_sysmem(ptr);
fe8c2806c   wdenk   Initial revision
187
  	}
1411157d8   Joe Hershberger   net: cosmetic: Fi...
188
189
  	if (net_boot_file_size < newsize)
  		net_boot_file_size = newsize;
a156c47e3   Simon Goldschmidt   tftp: prevent ove...
190
191
  
  	return 0;
fe8c2806c   wdenk   Initial revision
192
  }
e4cde2f70   Simon Glass   net: tftpput: Fac...
193
  /* Clear our state ready for a new transfer */
165099e75   Simon Glass   net: Make net_tra...
194
  static void new_transfer(void)
e4cde2f70   Simon Glass   net: tftpput: Fac...
195
  {
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
196
197
198
  	tftp_prev_block = 0;
  	tftp_block_wrap = 0;
  	tftp_block_wrap_offset = 0;
e4cde2f70   Simon Glass   net: tftpput: Fac...
199
  #ifdef CONFIG_CMD_TFTPPUT
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
200
  	tftp_put_final_block_sent = 0;
e4cde2f70   Simon Glass   net: tftpput: Fac...
201
202
  #endif
  }
1fb7cd498   Simon Glass   net: tftpput: imp...
203
204
205
206
207
208
209
210
211
212
213
214
  #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 */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
215
  	ulong offset = ((int)block - 1) * len + tftp_block_wrap_offset;
1fb7cd498   Simon Glass   net: tftpput: imp...
216
  	ulong tosend = len;
1411157d8   Joe Hershberger   net: cosmetic: Fi...
217
  	tosend = min(net_boot_file_size - offset, tosend);
bb872dd93   Simon Glass   image: Rename loa...
218
  	(void)memcpy(dst, (void *)(image_save_addr + offset), tosend);
2bcc43b39   Heinrich Schuchardt   net: tftp: use co...
219
220
  	debug("%s: block=%u, offset=%lu, len=%u, tosend=%lu
  ", __func__,
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
221
  	      block, offset, len, tosend);
1fb7cd498   Simon Glass   net: tftpput: imp...
222
223
224
  	return tosend;
  }
  #endif
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
225
226
  static void tftp_send(void);
  static void tftp_timeout_handler(void);
fe8c2806c   wdenk   Initial revision
227
228
  
  /**********************************************************************/
f5329bbc3   Simon Glass   net: tftpput: mov...
229
230
231
  static void show_block_marker(void)
  {
  #ifdef CONFIG_TFTP_TSIZE
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
232
233
234
  	if (tftp_tsize) {
  		ulong pos = tftp_cur_block * tftp_block_size +
  			tftp_block_wrap_offset;
7628afebe   Max Krummenacher   tftp.c: fix CONFI...
235
236
  		if (pos > tftp_tsize)
  			pos = tftp_tsize;
f5329bbc3   Simon Glass   net: tftpput: mov...
237

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
238
  		while (tftp_tsize_num_hash < pos * 50 / tftp_tsize) {
f5329bbc3   Simon Glass   net: tftpput: mov...
239
  			putc('#');
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
240
  			tftp_tsize_num_hash++;
f5329bbc3   Simon Glass   net: tftpput: mov...
241
  		}
1fb7cd498   Simon Glass   net: tftpput: imp...
242
  	} else
f5329bbc3   Simon Glass   net: tftpput: mov...
243
  #endif
1fb7cd498   Simon Glass   net: tftpput: imp...
244
  	{
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
245
  		if (((tftp_cur_block - 1) % 10) == 0)
f5329bbc3   Simon Glass   net: tftpput: mov...
246
  			putc('#');
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
247
  		else if ((tftp_cur_block % (10 * HASHES_PER_LINE)) == 0)
f5329bbc3   Simon Glass   net: tftpput: mov...
248
249
250
251
  			puts("
  \t ");
  	}
  }
e4cde2f70   Simon Glass   net: tftpput: Fac...
252
253
254
255
256
257
258
259
260
261
  /**
   * restart the current transfer due to an error
   *
   * @param msg	Message to print for user
   */
  static void restart(const char *msg)
  {
  	printf("
  %s; starting again
  ", msg);
bc0571fc1   Joe Hershberger   net: cosmetic: Fi...
262
  	net_start_again();
e4cde2f70   Simon Glass   net: tftpput: Fac...
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
  }
  
  /*
   * 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.
  	 */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
278
279
280
281
  	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 */
e4cde2f70   Simon Glass   net: tftpput: Fac...
282
283
284
285
  	} else {
  		show_block_marker();
  	}
  }
f5329bbc3   Simon Glass   net: tftpput: mov...
286
287
288
289
290
  /* The TFTP get or put is complete */
  static void tftp_complete(void)
  {
  #ifdef CONFIG_TFTP_TSIZE
  	/* Print hash marks for the last packet received */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
291
  	while (tftp_tsize && tftp_tsize_num_hash < 49) {
f5329bbc3   Simon Glass   net: tftpput: mov...
292
  		putc('#');
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
293
  		tftp_tsize_num_hash++;
f5329bbc3   Simon Glass   net: tftpput: mov...
294
  	}
8104f5462   Simon Glass   net: Display the ...
295
  	puts("  ");
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
296
  	print_size(tftp_tsize, "");
f5329bbc3   Simon Glass   net: tftpput: mov...
297
  #endif
85b198027   Simon Glass   net: Add tftp spe...
298
299
300
301
  	time_start = get_timer(time_start);
  	if (time_start > 0) {
  		puts("
  \t ");	/* Line up with "Loading: " */
1411157d8   Joe Hershberger   net: cosmetic: Fi...
302
  		print_size(net_boot_file_size /
85b198027   Simon Glass   net: Add tftp spe...
303
304
  			time_start * 1000, "/s");
  	}
f5329bbc3   Simon Glass   net: tftpput: mov...
305
306
307
  	puts("
  done
  ");
22f6e99d5   Joe Hershberger   net: Refactor to ...
308
  	net_set_state(NETLOOP_SUCCESS);
f5329bbc3   Simon Glass   net: tftpput: mov...
309
  }
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
310
  static void tftp_send(void)
fe8c2806c   wdenk   Initial revision
311
  {
1fb7cd498   Simon Glass   net: tftpput: imp...
312
  	uchar *pkt;
db288a960   Joe Hershberger   net: Remove volat...
313
314
315
  	uchar *xp;
  	int len = 0;
  	ushort *s;
fe8c2806c   wdenk   Initial revision
316
317
318
319
320
  
  	/*
  	 *	We will always be sending some sort of packet, so
  	 *	cobble together the packet headers now.
  	 */
1203fccee   Joe Hershberger   net: cosmetic: Cl...
321
  	pkt = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
fe8c2806c   wdenk   Initial revision
322

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
323
  	switch (tftp_state) {
e3fb0abe2   Luca Ceresoli   TFTP: rename STAT...
324
  	case STATE_SEND_RRQ:
1fb7cd498   Simon Glass   net: tftpput: imp...
325
  	case STATE_SEND_WRQ:
fe8c2806c   wdenk   Initial revision
326
  		xp = pkt;
7bc5ee078   Wolfgang Denk   Prepare U-Boot fo...
327
  		s = (ushort *)pkt;
8c6914f10   Simon Glass   net: Add more #if...
328
  #ifdef CONFIG_CMD_TFTPPUT
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
329
  		*s++ = htons(tftp_state == STATE_SEND_RRQ ? TFTP_RRQ :
1fb7cd498   Simon Glass   net: tftpput: imp...
330
  			TFTP_WRQ);
8c6914f10   Simon Glass   net: Add more #if...
331
332
333
  #else
  		*s++ = htons(TFTP_RRQ);
  #endif
7bc5ee078   Wolfgang Denk   Prepare U-Boot fo...
334
  		pkt = (uchar *)s;
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
335
  		strcpy((char *)pkt, tftp_filename);
fe8c2806c   wdenk   Initial revision
336
  		pkt += strlen(tftp_filename) + 1;
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
337
  		strcpy((char *)pkt, "octet");
fe8c2806c   wdenk   Initial revision
338
  		pkt += 5 /*strlen("octet")*/ + 1;
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
339
  		strcpy((char *)pkt, "timeout");
fbe4b5cbd   wdenk   * Update TRAB aut...
340
  		pkt += 7 /*strlen("timeout")*/ + 1;
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
341
  		sprintf((char *)pkt, "%lu", timeout_ms / 1000);
0ebf04c60   Robin Getz   minor debug clean...
342
343
  		debug("send option \"timeout %s\"
  ", (char *)pkt);
fbe4b5cbd   wdenk   * Update TRAB aut...
344
  		pkt += strlen((char *)pkt) + 1;
4fccb818e   Robin Getz   Add Transfer Size...
345
  #ifdef CONFIG_TFTP_TSIZE
1411157d8   Joe Hershberger   net: cosmetic: Fi...
346
347
  		pkt += sprintf((char *)pkt, "tsize%c%u%c",
  				0, net_boot_file_size, 0);
4fccb818e   Robin Getz   Add Transfer Size...
348
  #endif
53a5c424b   David Updegraff   multicast tftp: R...
349
  		/* try for more effic. blk size */
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
350
  		pkt += sprintf((char *)pkt, "blksize%c%d%c",
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
351
  				0, tftp_block_size_option, 0);
fe8c2806c   wdenk   Initial revision
352
353
  		len = pkt - xp;
  		break;
fbe4b5cbd   wdenk   * Update TRAB aut...
354
  	case STATE_OACK:
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
355
356
  
  	case STATE_RECV_WRQ:
53a5c424b   David Updegraff   multicast tftp: R...
357
  	case STATE_DATA:
fe8c2806c   wdenk   Initial revision
358
  		xp = pkt;
7bc5ee078   Wolfgang Denk   Prepare U-Boot fo...
359
  		s = (ushort *)pkt;
1fb7cd498   Simon Glass   net: tftpput: imp...
360
  		s[0] = htons(TFTP_ACK);
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
361
  		s[1] = htons(tftp_cur_block);
1fb7cd498   Simon Glass   net: tftpput: imp...
362
363
  		pkt = (uchar *)(s + 2);
  #ifdef CONFIG_CMD_TFTPPUT
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
364
365
366
  		if (tftp_put_active) {
  			int toload = tftp_block_size;
  			int loaded = load_block(tftp_cur_block, pkt, toload);
1fb7cd498   Simon Glass   net: tftpput: imp...
367
368
369
  
  			s[0] = htons(TFTP_DATA);
  			pkt += loaded;
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
370
  			tftp_put_final_block_sent = (loaded < toload);
1fb7cd498   Simon Glass   net: tftpput: imp...
371
372
  		}
  #endif
fe8c2806c   wdenk   Initial revision
373
374
375
376
377
  		len = pkt - xp;
  		break;
  
  	case STATE_TOO_LARGE:
  		xp = pkt;
7bc5ee078   Wolfgang Denk   Prepare U-Boot fo...
378
379
  		s = (ushort *)pkt;
  		*s++ = htons(TFTP_ERROR);
1fb7cd498   Simon Glass   net: tftpput: imp...
380
  			*s++ = htons(3);
7bc5ee078   Wolfgang Denk   Prepare U-Boot fo...
381
  		pkt = (uchar *)s;
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
382
  		strcpy((char *)pkt, "File too large");
fe8c2806c   wdenk   Initial revision
383
384
385
386
387
388
  		pkt += 14 /*strlen("File too large")*/ + 1;
  		len = pkt - xp;
  		break;
  
  	case STATE_BAD_MAGIC:
  		xp = pkt;
7bc5ee078   Wolfgang Denk   Prepare U-Boot fo...
389
390
391
392
  		s = (ushort *)pkt;
  		*s++ = htons(TFTP_ERROR);
  		*s++ = htons(2);
  		pkt = (uchar *)s;
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
393
  		strcpy((char *)pkt, "File has bad magic");
fe8c2806c   wdenk   Initial revision
394
395
396
397
  		pkt += 18 /*strlen("File has bad magic")*/ + 1;
  		len = pkt - xp;
  		break;
  	}
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
398
399
  	net_send_udp_packet(net_server_ethaddr, tftp_remote_ip,
  			    tftp_remote_port, tftp_our_port, len);
fe8c2806c   wdenk   Initial revision
400
  }
39bccd21d   Simon Glass   net: Hide more co...
401
  #ifdef CONFIG_CMD_TFTPPUT
1fb7cd498   Simon Glass   net: tftpput: imp...
402
  static void icmp_handler(unsigned type, unsigned code, unsigned dest,
049a95a77   Joe Hershberger   net: cosmetic: Ch...
403
404
  			 struct in_addr sip, unsigned src, uchar *pkt,
  			 unsigned len)
1fb7cd498   Simon Glass   net: tftpput: imp...
405
406
407
408
409
410
  {
  	if (type == ICMP_NOT_REACH && code == ICMP_NOT_REACH_PORT) {
  		/* Oh dear the other end has gone away */
  		restart("TFTP server died");
  	}
  }
39bccd21d   Simon Glass   net: Hide more co...
411
  #endif
1fb7cd498   Simon Glass   net: tftpput: imp...
412

049a95a77   Joe Hershberger   net: cosmetic: Ch...
413
414
  static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
  			 unsigned src, unsigned len)
fe8c2806c   wdenk   Initial revision
415
  {
61fdd4f7c   Kim Phillips   net/tftp: sparse ...
416
417
  	__be16 proto;
  	__be16 *s;
ff13ac8c7   Wolfgang Denk   Backout commit 8f...
418
  	int i;
fe8c2806c   wdenk   Initial revision
419

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
420
  	if (dest != tftp_our_port) {
0bdd8acc3   Luca Ceresoli   net/tftp.c: cosme...
421
  			return;
fe8c2806c   wdenk   Initial revision
422
  	}
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
423
424
  	if (tftp_state != STATE_SEND_RRQ && src != tftp_remote_port &&
  	    tftp_state != STATE_RECV_WRQ && tftp_state != STATE_SEND_WRQ)
fe8c2806c   wdenk   Initial revision
425
  		return;
fe8c2806c   wdenk   Initial revision
426

7bc325a1b   Luca Ceresoli   net/tftp.c: cosme...
427
  	if (len < 2)
fe8c2806c   wdenk   Initial revision
428
  		return;
fe8c2806c   wdenk   Initial revision
429
430
  	len -= 2;
  	/* warning: don't use increment (++) in ntohs() macros!! */
61fdd4f7c   Kim Phillips   net/tftp: sparse ...
431
  	s = (__be16 *)pkt;
7bc5ee078   Wolfgang Denk   Prepare U-Boot fo...
432
433
  	proto = *s++;
  	pkt = (uchar *)s;
fe8c2806c   wdenk   Initial revision
434
  	switch (ntohs(proto)) {
fe8c2806c   wdenk   Initial revision
435
  	case TFTP_RRQ:
1fb7cd498   Simon Glass   net: tftpput: imp...
436
  		break;
fe8c2806c   wdenk   Initial revision
437
  	case TFTP_ACK:
1fb7cd498   Simon Glass   net: tftpput: imp...
438
  #ifdef CONFIG_CMD_TFTPPUT
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
439
440
  		if (tftp_put_active) {
  			if (tftp_put_final_block_sent) {
1fb7cd498   Simon Glass   net: tftpput: imp...
441
442
443
444
445
446
447
  				tftp_complete();
  			} else {
  				/*
  				 * Move to the next block. We want our block
  				 * count to wrap just like the other end!
  				 */
  				int block = ntohs(*s);
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
448
  				int ack_ok = (tftp_cur_block == block);
1fb7cd498   Simon Glass   net: tftpput: imp...
449

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
450
  				tftp_cur_block = (unsigned short)(block + 1);
1fb7cd498   Simon Glass   net: tftpput: imp...
451
452
  				update_block_number();
  				if (ack_ok)
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
453
  					tftp_send(); /* Send next data block */
1fb7cd498   Simon Glass   net: tftpput: imp...
454
455
456
  			}
  		}
  #endif
fe8c2806c   wdenk   Initial revision
457
  		break;
1fb7cd498   Simon Glass   net: tftpput: imp...
458

fe8c2806c   wdenk   Initial revision
459
460
  	default:
  		break;
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
461
462
463
464
  #ifdef CONFIG_CMD_TFTPSRV
  	case TFTP_WRQ:
  		debug("Got WRQ
  ");
049a95a77   Joe Hershberger   net: cosmetic: Ch...
465
  		tftp_remote_ip = sip;
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
466
467
  		tftp_remote_port = src;
  		tftp_our_port = 1024 + (get_timer(0) % 3072);
e4cde2f70   Simon Glass   net: tftpput: Fac...
468
  		new_transfer();
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
469
  		tftp_send(); /* Send ACK(0) */
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
470
471
  		break;
  #endif
fbe4b5cbd   wdenk   * Update TRAB aut...
472
  	case TFTP_OACK:
d371708a1   Wolfgang Denk   net/tftp.c: fix w...
473
474
  		debug("Got OACK: %s %s
  ",
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
475
476
477
  		      pkt, pkt + strlen((char *)pkt) + 1);
  		tftp_state = STATE_OACK;
  		tftp_remote_port = src;
60174746c   Wolfgang Denk   Fix TFTP OACK cod...
478
479
480
481
482
  		/*
  		 * Check for 'blksize' option.
  		 * Careful: "i" is signed, "len" is unsigned, thus
  		 * something like "len-8" may give a *huge* number
  		 */
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
483
  		for (i = 0; i+8 < len; i++) {
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
484
485
486
487
  			if (strcmp((char *)pkt + i, "blksize") == 0) {
  				tftp_block_size = (unsigned short)
  					simple_strtoul((char *)pkt + i + 8,
  						       NULL, 10);
0ebf04c60   Robin Getz   minor debug clean...
488
489
  				debug("Blocksize ack: %s, %d
  ",
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
490
  				      (char *)pkt + i + 8, tftp_block_size);
ff13ac8c7   Wolfgang Denk   Backout commit 8f...
491
  			}
4fccb818e   Robin Getz   Add Transfer Size...
492
  #ifdef CONFIG_TFTP_TSIZE
2e320257c   Luca Ceresoli   net/tftp.c: cosme...
493
  			if (strcmp((char *)pkt+i, "tsize") == 0) {
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
494
  				tftp_tsize = simple_strtoul((char *)pkt + i + 6,
2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
495
  							   NULL, 10);
4fccb818e   Robin Getz   Add Transfer Size...
496
497
  				debug("size = %s, %d
  ",
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
498
  				      (char *)pkt + i + 6, tftp_tsize);
4fccb818e   Robin Getz   Add Transfer Size...
499
500
  			}
  #endif
53a5c424b   David Updegraff   multicast tftp: R...
501
  		}
1fb7cd498   Simon Glass   net: tftpput: imp...
502
  #ifdef CONFIG_CMD_TFTPPUT
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
503
  		if (tftp_put_active) {
1fb7cd498   Simon Glass   net: tftpput: imp...
504
  			/* Get ready to send the first block */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
505
506
  			tftp_state = STATE_DATA;
  			tftp_cur_block++;
1fb7cd498   Simon Glass   net: tftpput: imp...
507
508
  		}
  #endif
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
509
  		tftp_send(); /* Send ACK or first data block */
fbe4b5cbd   wdenk   * Update TRAB aut...
510
  		break;
fe8c2806c   wdenk   Initial revision
511
512
513
514
  	case TFTP_DATA:
  		if (len < 2)
  			return;
  		len -= 2;
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
515
  		tftp_cur_block = ntohs(*(__be16 *)pkt);
3f85ce278   wdenk   * CVS add missing...
516

e4cde2f70   Simon Glass   net: tftpput: Fac...
517
  		update_block_number();
fe8c2806c   wdenk   Initial revision
518

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
519
  		if (tftp_state == STATE_SEND_RRQ)
0ebf04c60   Robin Getz   minor debug clean...
520
521
  			debug("Server did not acknowledge timeout option!
  ");
fbe4b5cbd   wdenk   * Update TRAB aut...
522

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
523
524
  		if (tftp_state == STATE_SEND_RRQ || tftp_state == STATE_OACK ||
  		    tftp_state == STATE_RECV_WRQ) {
3f85ce278   wdenk   * CVS add missing...
525
  			/* first block received */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
526
527
  			tftp_state = STATE_DATA;
  			tftp_remote_port = src;
e4cde2f70   Simon Glass   net: tftpput: Fac...
528
  			new_transfer();
fe8c2806c   wdenk   Initial revision
529

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
530
531
532
533
534
535
536
537
538
  			if (tftp_cur_block != 1) {	/* Assertion */
  				puts("
  TFTP error: ");
  				printf("First block is not block 1 (%ld)
  ",
  				       tftp_cur_block);
  				puts("Starting again
  
  ");
bc0571fc1   Joe Hershberger   net: cosmetic: Fi...
539
  				net_start_again();
fe8c2806c   wdenk   Initial revision
540
541
542
  				break;
  			}
  		}
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
543
544
  		if (tftp_cur_block == tftp_prev_block) {
  			/* Same block again; ignore it. */
fe8c2806c   wdenk   Initial revision
545
546
  			break;
  		}
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
547
  		tftp_prev_block = tftp_cur_block;
f5fb73467   Albert ARIBAUD \(3ADEV\)   net: TFTP: variab...
548
  		timeout_count_max = tftp_timeout_count_max;
bc0571fc1   Joe Hershberger   net: cosmetic: Fi...
549
  		net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
fe8c2806c   wdenk   Initial revision
550

a156c47e3   Simon Goldschmidt   tftp: prevent ove...
551
552
553
554
555
  		if (store_block(tftp_cur_block - 1, pkt + 2, len)) {
  			eth_halt();
  			net_set_state(NETLOOP_FAIL);
  			break;
  		}
fe8c2806c   wdenk   Initial revision
556
557
  
  		/*
4d69e98c0   Luca Ceresoli   net/tftp.c: fix typo
558
  		 *	Acknowledge the block just received, which will prompt
20478ceaa   Luca Ceresoli   TFTP: replace "se...
559
  		 *	the remote for the next one.
fe8c2806c   wdenk   Initial revision
560
  		 */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
561
  		tftp_send();
fe8c2806c   wdenk   Initial revision
562

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
563
  		if (len < tftp_block_size)
f5329bbc3   Simon Glass   net: tftpput: mov...
564
  			tftp_complete();
fe8c2806c   wdenk   Initial revision
565
566
567
  		break;
  
  	case TFTP_ERROR:
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
568
569
570
  		printf("
  TFTP error: '%s' (%d)
  ",
61fdd4f7c   Kim Phillips   net/tftp: sparse ...
571
  		       pkt + 2, ntohs(*(__be16 *)pkt));
aafda38fb   Remy Bohmer   Add error codes/h...
572

61fdd4f7c   Kim Phillips   net/tftp: sparse ...
573
  		switch (ntohs(*(__be16 *)pkt)) {
aafda38fb   Remy Bohmer   Add error codes/h...
574
575
576
577
578
  		case TFTP_ERR_FILE_NOT_FOUND:
  		case TFTP_ERR_ACCESS_DENIED:
  			puts("Not retrying...
  ");
  			eth_halt();
22f6e99d5   Joe Hershberger   net: Refactor to ...
579
  			net_set_state(NETLOOP_FAIL);
aafda38fb   Remy Bohmer   Add error codes/h...
580
581
582
583
584
585
586
587
588
589
  			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
  
  ");
bc0571fc1   Joe Hershberger   net: cosmetic: Fi...
590
  			net_start_again();
aafda38fb   Remy Bohmer   Add error codes/h...
591
592
  			break;
  		}
fe8c2806c   wdenk   Initial revision
593
594
595
  		break;
  	}
  }
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
596
  static void tftp_timeout_handler(void)
fe8c2806c   wdenk   Initial revision
597
  {
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
598
  	if (++timeout_count > timeout_count_max) {
e4cde2f70   Simon Glass   net: tftpput: Fac...
599
  		restart("Retry count exceeded");
fe8c2806c   wdenk   Initial revision
600
  	} else {
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
601
  		puts("T ");
bc0571fc1   Joe Hershberger   net: cosmetic: Fi...
602
  		net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
603
604
  		if (tftp_state != STATE_RECV_WRQ)
  			tftp_send();
fe8c2806c   wdenk   Initial revision
605
606
  	}
  }
bb872dd93   Simon Glass   image: Rename loa...
607
  /* Initialize tftp_load_addr and tftp_load_size from image_load_addr and lmb */
a156c47e3   Simon Goldschmidt   tftp: prevent ove...
608
609
610
611
612
  static int tftp_init_load_addr(void)
  {
  #ifdef CONFIG_LMB
  	struct lmb lmb;
  	phys_size_t max_size;
9cc2323fe   Simon Goldschmidt   lmb: handle more ...
613
  	lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
a156c47e3   Simon Goldschmidt   tftp: prevent ove...
614

bb872dd93   Simon Glass   image: Rename loa...
615
  	max_size = lmb_get_free_size(&lmb, image_load_addr);
a156c47e3   Simon Goldschmidt   tftp: prevent ove...
616
617
618
619
620
  	if (!max_size)
  		return -1;
  
  	tftp_load_size = max_size;
  #endif
bb872dd93   Simon Glass   image: Rename loa...
621
  	tftp_load_addr = image_load_addr;
a156c47e3   Simon Goldschmidt   tftp: prevent ove...
622
623
  	return 0;
  }
fe8c2806c   wdenk   Initial revision
624

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
625
  void tftp_start(enum proto_t protocol)
fe8c2806c   wdenk   Initial revision
626
  {
f5fb73467   Albert ARIBAUD \(3ADEV\)   net: TFTP: variab...
627
  #if CONFIG_NET_TFTP_VARS
ecb0ccd9c   Wolfgang Denk   Allow to force TF...
628
  	char *ep;             /* Environment pointer */
89ba81d10   Alessandro Rubini   tftp: get the tft...
629

c96f86eef   Wolfgang Denk   TFTP: allow for a...
630
631
632
633
  	/*
  	 * Allow the user to choose TFTP blocksize and timeout.
  	 * TFTP protocol has a minimal timeout of 1 second.
  	 */
f5fb73467   Albert ARIBAUD \(3ADEV\)   net: TFTP: variab...
634

00caae6d4   Simon Glass   env: Rename geten...
635
  	ep = env_get("tftpblocksize");
2cb536080   Luca Ceresoli   net/tftp.c: cosme...
636
  	if (ep != NULL)
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
637
  		tftp_block_size_option = simple_strtol(ep, NULL, 10);
c96f86eef   Wolfgang Denk   TFTP: allow for a...
638

00caae6d4   Simon Glass   env: Rename geten...
639
  	ep = env_get("tftptimeout");
2cb536080   Luca Ceresoli   net/tftp.c: cosme...
640
  	if (ep != NULL)
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
641
  		timeout_ms = simple_strtol(ep, NULL, 10);
c96f86eef   Wolfgang Denk   TFTP: allow for a...
642

af2ca59e6   Bin Meng   net: Revert "tftp...
643
644
645
  	if (timeout_ms < 1000) {
  		printf("TFTP timeout (%ld ms) too low, set min = 1000 ms
  ",
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
646
  		       timeout_ms);
af2ca59e6   Bin Meng   net: Revert "tftp...
647
  		timeout_ms = 1000;
c96f86eef   Wolfgang Denk   TFTP: allow for a...
648
  	}
00caae6d4   Simon Glass   env: Rename geten...
649
  	ep = env_get("tftptimeoutcountmax");
f5fb73467   Albert ARIBAUD \(3ADEV\)   net: TFTP: variab...
650
651
652
653
654
655
656
657
658
659
  	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
  ",
  		       tftp_timeout_count_max);
  		tftp_timeout_count_max = 0;
  	}
  #endif
c96f86eef   Wolfgang Denk   TFTP: allow for a...
660
661
  	debug("TFTP blocksize = %i, timeout = %ld ms
  ",
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
662
  	      tftp_block_size_option, timeout_ms);
ecb0ccd9c   Wolfgang Denk   Allow to force TF...
663

049a95a77   Joe Hershberger   net: cosmetic: Ch...
664
  	tftp_remote_ip = net_server_ip;
6ab128309   Joe Hershberger   net: Consolidate ...
665
  	if (!net_parse_bootfile(&tftp_remote_ip, tftp_filename, MAX_LEN)) {
ea45cb0ad   Matthias Weisser   net: Make sure IP...
666
  		sprintf(default_filename, "%02X%02X%02X%02X.img",
049a95a77   Joe Hershberger   net: cosmetic: Ch...
667
668
669
670
  			net_ip.s_addr & 0xFF,
  			(net_ip.s_addr >>  8) & 0xFF,
  			(net_ip.s_addr >> 16) & 0xFF,
  			(net_ip.s_addr >> 24) & 0xFF);
a93907c43   Jean-Christophe PLAGNIOL-VILLARD   TFTP: add host ip...
671

0c17b1b79   Vladimir Zapolskiy   net: tftp: silenc...
672
673
  		strncpy(tftp_filename, default_filename, DEFAULT_NAME_LEN);
  		tftp_filename[DEFAULT_NAME_LEN - 1] = 0;
fe8c2806c   wdenk   Initial revision
674

c718b1439   Luca Ceresoli   net/tftp.c: cosme...
675
676
  		printf("*** Warning: no boot file name; using '%s'
  ",
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
677
  		       tftp_filename);
fe8c2806c   wdenk   Initial revision
678
  	}
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
679
680
  	printf("Using %s device
  ", eth_get_name());
1fb7cd498   Simon Glass   net: tftpput: imp...
681
  	printf("TFTP %s server %pI4; our IP address is %pI4",
8c6914f10   Simon Glass   net: Add more #if...
682
683
684
  #ifdef CONFIG_CMD_TFTPPUT
  	       protocol == TFTPPUT ? "to" : "from",
  #else
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
685
  	       "from",
8c6914f10   Simon Glass   net: Add more #if...
686
  #endif
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
687
  	       &tftp_remote_ip, &net_ip);
fe8c2806c   wdenk   Initial revision
688
689
  
  	/* Check if we need to send across this subnet */
049a95a77   Joe Hershberger   net: cosmetic: Ch...
690
691
692
693
694
695
696
697
  	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);
fe8c2806c   wdenk   Initial revision
698
  	}
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
699
700
  	putc('
  ');
fe8c2806c   wdenk   Initial revision
701

c718b1439   Luca Ceresoli   net/tftp.c: cosme...
702
  	printf("Filename '%s'.", tftp_filename);
fe8c2806c   wdenk   Initial revision
703

1411157d8   Joe Hershberger   net: cosmetic: Fi...
704
705
706
707
  	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, "");
fe8c2806c   wdenk   Initial revision
708
  	}
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
709
710
  	putc('
  ');
1fb7cd498   Simon Glass   net: tftpput: imp...
711
  #ifdef CONFIG_CMD_TFTPPUT
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
712
713
  	tftp_put_active = (protocol == TFTPPUT);
  	if (tftp_put_active) {
bb872dd93   Simon Glass   image: Rename loa...
714
715
716
717
718
  		printf("Save address: 0x%lx
  ", image_save_addr);
  		printf("Save size:    0x%lx
  ", image_save_size);
  		net_boot_file_size = image_save_size;
1fb7cd498   Simon Glass   net: tftpput: imp...
719
  		puts("Saving: *\b");
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
720
  		tftp_state = STATE_SEND_WRQ;
1fb7cd498   Simon Glass   net: tftpput: imp...
721
722
723
724
  		new_transfer();
  	} else
  #endif
  	{
a156c47e3   Simon Goldschmidt   tftp: prevent ove...
725
726
727
728
729
730
731
732
733
734
735
  		if (tftp_init_load_addr()) {
  			eth_halt();
  			net_set_state(NETLOOP_FAIL);
  			puts("
  TFTP error: ");
  			puts("trying to overwrite reserved memory...
  ");
  			return;
  		}
  		printf("Load address: 0x%lx
  ", tftp_load_addr);
1fb7cd498   Simon Glass   net: tftpput: imp...
736
  		puts("Loading: *\b");
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
737
  		tftp_state = STATE_SEND_RRQ;
64b8d7a67   Jörg Krause   net/tftp: fix bui...
738
  #ifdef CONFIG_CMD_BOOTEFI
0efe1bcf5   Alexander Graf   efi_loader: Add n...
739
  		efi_set_bootdev("Net", "", tftp_filename);
64b8d7a67   Jörg Krause   net/tftp: fix bui...
740
  #endif
1fb7cd498   Simon Glass   net: tftpput: imp...
741
  	}
fe8c2806c   wdenk   Initial revision
742

85b198027   Simon Glass   net: Add tftp spe...
743
  	time_start = get_timer(0);
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
744
  	timeout_count_max = tftp_timeout_count_max;
e83cc0637   Bartlomiej Sieka   net: Make TFTP se...
745

bc0571fc1   Joe Hershberger   net: cosmetic: Fi...
746
  	net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
049a95a77   Joe Hershberger   net: cosmetic: Ch...
747
  	net_set_udp_handler(tftp_handler);
39bccd21d   Simon Glass   net: Hide more co...
748
  #ifdef CONFIG_CMD_TFTPPUT
1fb7cd498   Simon Glass   net: tftpput: imp...
749
  	net_set_icmp_handler(icmp_handler);
39bccd21d   Simon Glass   net: Hide more co...
750
  #endif
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
751
752
  	tftp_remote_port = WELL_KNOWN_PORT;
  	timeout_count = 0;
ecb0ccd9c   Wolfgang Denk   Allow to force TF...
753
  	/* Use a pseudo-random port unless a specific port is set */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
754
  	tftp_our_port = 1024 + (get_timer(0) % 3072);
53a5c424b   David Updegraff   multicast tftp: R...
755

ecb0ccd9c   Wolfgang Denk   Allow to force TF...
756
  #ifdef CONFIG_TFTP_PORT
00caae6d4   Simon Glass   env: Rename geten...
757
  	ep = env_get("tftpdstp");
7bc325a1b   Luca Ceresoli   net/tftp.c: cosme...
758
  	if (ep != NULL)
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
759
  		tftp_remote_port = simple_strtol(ep, NULL, 10);
00caae6d4   Simon Glass   env: Rename geten...
760
  	ep = env_get("tftpsrcp");
7bc325a1b   Luca Ceresoli   net/tftp.c: cosme...
761
  	if (ep != NULL)
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
762
  		tftp_our_port = simple_strtol(ep, NULL, 10);
ecb0ccd9c   Wolfgang Denk   Allow to force TF...
763
  #endif
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
764
  	tftp_cur_block = 0;
fe8c2806c   wdenk   Initial revision
765

73a8b27c5   wdenk   * Add support for...
766
  	/* zero out server ether in case the server ip has changed */
0adb5b761   Joe Hershberger   net: cosmetic: Na...
767
  	memset(net_server_ethaddr, 0, 6);
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
768
769
  	/* Revert tftp_block_size to dflt */
  	tftp_block_size = TFTP_BLOCK_SIZE;
4fccb818e   Robin Getz   Add Transfer Size...
770
  #ifdef CONFIG_TFTP_TSIZE
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
771
772
  	tftp_tsize = 0;
  	tftp_tsize_num_hash = 0;
4fccb818e   Robin Getz   Add Transfer Size...
773
  #endif
73a8b27c5   wdenk   * Add support for...
774

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
775
  	tftp_send();
fe8c2806c   wdenk   Initial revision
776
  }
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
777
  #ifdef CONFIG_CMD_TFTPSRV
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
778
  void tftp_start_server(void)
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
779
780
  {
  	tftp_filename[0] = 0;
a156c47e3   Simon Goldschmidt   tftp: prevent ove...
781
782
783
784
785
786
787
788
  	if (tftp_init_load_addr()) {
  		eth_halt();
  		net_set_state(NETLOOP_FAIL);
  		puts("
  TFTP error: trying to overwrite reserved memory...
  ");
  		return;
  	}
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
789
790
  	printf("Using %s device
  ", eth_get_name());
049a95a77   Joe Hershberger   net: cosmetic: Ch...
791
792
  	printf("Listening for TFTP transfer on %pI4
  ", &net_ip);
a156c47e3   Simon Goldschmidt   tftp: prevent ove...
793
794
  	printf("Load address: 0x%lx
  ", tftp_load_addr);
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
795
796
  
  	puts("Loading: *\b");
f5fb73467   Albert ARIBAUD \(3ADEV\)   net: TFTP: variab...
797
  	timeout_count_max = tftp_timeout_count_max;
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
798
799
  	timeout_count = 0;
  	timeout_ms = TIMEOUT;
bc0571fc1   Joe Hershberger   net: cosmetic: Fi...
800
  	net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
801

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
802
803
804
805
  	/* Revert tftp_block_size to dflt */
  	tftp_block_size = TFTP_BLOCK_SIZE;
  	tftp_cur_block = 0;
  	tftp_our_port = WELL_KNOWN_PORT;
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
806
807
  
  #ifdef CONFIG_TFTP_TSIZE
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
808
809
  	tftp_tsize = 0;
  	tftp_tsize_num_hash = 0;
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
810
  #endif
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
811
  	tftp_state = STATE_RECV_WRQ;
049a95a77   Joe Hershberger   net: cosmetic: Ch...
812
  	net_set_udp_handler(tftp_handler);
8e52533d1   Andrew Ruder   net: tftpsrv: Get...
813
814
  
  	/* zero out server ether in case the server ip has changed */
0adb5b761   Joe Hershberger   net: cosmetic: Na...
815
  	memset(net_server_ethaddr, 0, 6);
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
816
817
  }
  #endif /* CONFIG_CMD_TFTPSRV */