Blame view

net/tftp.c 19.5 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>
55d5fd9a8   Joe Hershberger   net: Access mappe...
12
  #include <mapmem.h>
fe8c2806c   wdenk   Initial revision
13
  #include <net.h>
346969584   Lukasz Majewski   net: tftp: Move t...
14
  #include <net/tftp.h>
fe8c2806c   wdenk   Initial revision
15
  #include "bootp.h"
13dfe9437   Joe Hershberger   net: cosmetic: tf...
16
17
18
  #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
  #include <flash.h>
  #endif
fe8c2806c   wdenk   Initial revision
19

a156c47e3   Simon Goldschmidt   tftp: prevent ove...
20
  DECLARE_GLOBAL_DATA_PTR;
2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
21
22
23
  /* Well known TFTP port # */
  #define WELL_KNOWN_PORT	69
  /* Millisecs to timeout for lost pkt */
af2ca59e6   Bin Meng   net: Revert "tftp...
24
  #define TIMEOUT		5000UL
fe8c2806c   wdenk   Initial revision
25
  #ifndef	CONFIG_NET_RETRY_COUNT
2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
26
  /* # of timeouts before giving up */
af2ca59e6   Bin Meng   net: Revert "tftp...
27
  # define TIMEOUT_COUNT	10
fe8c2806c   wdenk   Initial revision
28
29
30
  #else
  # define TIMEOUT_COUNT  (CONFIG_NET_RETRY_COUNT * 2)
  #endif
2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
31
32
  /* Number of "loading" hashes per line (for checking the image size) */
  #define HASHES_PER_LINE	65
fe8c2806c   wdenk   Initial revision
33
34
35
36
37
38
39
40
41
  
  /*
   *	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...
42
  #define TFTP_OACK	6
fe8c2806c   wdenk   Initial revision
43

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

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

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

2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
111
112
113
114
  /* 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...
115

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

85eb5caf6   Wolfgang Denk   Coding style clea...
127
128
  /* 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...
129
   * almost-MTU block sizes.  At least try... fall back to 512 if need be.
89ba81d10   Alessandro Rubini   tftp: get the tft...
130
   * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
53a5c424b   David Updegraff   multicast tftp: R...
131
   */
89ba81d10   Alessandro Rubini   tftp: get the tft...
132
133
134
  #ifdef CONFIG_TFTP_BLOCKSIZE
  #define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
  #else
53a5c424b   David Updegraff   multicast tftp: R...
135
  #define TFTP_MTU_BLOCKSIZE 1468
89ba81d10   Alessandro Rubini   tftp: get the tft...
136
  #endif
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
137
138
  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...
139

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

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

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

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

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
414
  	if (dest != tftp_our_port) {
0bdd8acc3   Luca Ceresoli   net/tftp.c: cosme...
415
  			return;
fe8c2806c   wdenk   Initial revision
416
  	}
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
417
418
  	if (tftp_state != STATE_SEND_RRQ && src != tftp_remote_port &&
  	    tftp_state != STATE_RECV_WRQ && tftp_state != STATE_SEND_WRQ)
fe8c2806c   wdenk   Initial revision
419
  		return;
fe8c2806c   wdenk   Initial revision
420

7bc325a1b   Luca Ceresoli   net/tftp.c: cosme...
421
  	if (len < 2)
fe8c2806c   wdenk   Initial revision
422
  		return;
fe8c2806c   wdenk   Initial revision
423
424
  	len -= 2;
  	/* warning: don't use increment (++) in ntohs() macros!! */
61fdd4f7c   Kim Phillips   net/tftp: sparse ...
425
  	s = (__be16 *)pkt;
7bc5ee078   Wolfgang Denk   Prepare U-Boot fo...
426
427
  	proto = *s++;
  	pkt = (uchar *)s;
fe8c2806c   wdenk   Initial revision
428
  	switch (ntohs(proto)) {
fe8c2806c   wdenk   Initial revision
429
  	case TFTP_RRQ:
1fb7cd498   Simon Glass   net: tftpput: imp...
430
  		break;
fe8c2806c   wdenk   Initial revision
431
  	case TFTP_ACK:
1fb7cd498   Simon Glass   net: tftpput: imp...
432
  #ifdef CONFIG_CMD_TFTPPUT
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
433
434
  		if (tftp_put_active) {
  			if (tftp_put_final_block_sent) {
1fb7cd498   Simon Glass   net: tftpput: imp...
435
436
437
438
439
440
441
  				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...
442
  				int ack_ok = (tftp_cur_block == block);
1fb7cd498   Simon Glass   net: tftpput: imp...
443

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
444
  				tftp_cur_block = (unsigned short)(block + 1);
1fb7cd498   Simon Glass   net: tftpput: imp...
445
446
  				update_block_number();
  				if (ack_ok)
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
447
  					tftp_send(); /* Send next data block */
1fb7cd498   Simon Glass   net: tftpput: imp...
448
449
450
  			}
  		}
  #endif
fe8c2806c   wdenk   Initial revision
451
  		break;
1fb7cd498   Simon Glass   net: tftpput: imp...
452

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

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

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

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
517
518
  		if (tftp_state == STATE_SEND_RRQ || tftp_state == STATE_OACK ||
  		    tftp_state == STATE_RECV_WRQ) {
3f85ce278   wdenk   * CVS add missing...
519
  			/* first block received */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
520
521
  			tftp_state = STATE_DATA;
  			tftp_remote_port = src;
e4cde2f70   Simon Glass   net: tftpput: Fac...
522
  			new_transfer();
fe8c2806c   wdenk   Initial revision
523

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
524
525
526
527
528
529
530
531
532
  			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...
533
  				net_start_again();
fe8c2806c   wdenk   Initial revision
534
535
536
  				break;
  			}
  		}
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
537
538
  		if (tftp_cur_block == tftp_prev_block) {
  			/* Same block again; ignore it. */
fe8c2806c   wdenk   Initial revision
539
540
  			break;
  		}
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
541
  		tftp_prev_block = tftp_cur_block;
f5fb73467   Albert ARIBAUD \(3ADEV\)   net: TFTP: variab...
542
  		timeout_count_max = tftp_timeout_count_max;
bc0571fc1   Joe Hershberger   net: cosmetic: Fi...
543
  		net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
fe8c2806c   wdenk   Initial revision
544

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

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

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

65304aade   Simon Goldschmidt   lib: lmb: rename ...
609
  	max_size = lmb_get_free_size(&lmb, load_addr);
a156c47e3   Simon Goldschmidt   tftp: prevent ove...
610
611
612
613
614
615
616
617
  	if (!max_size)
  		return -1;
  
  	tftp_load_size = max_size;
  #endif
  	tftp_load_addr = load_addr;
  	return 0;
  }
fe8c2806c   wdenk   Initial revision
618

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

c96f86eef   Wolfgang Denk   TFTP: allow for a...
624
625
626
627
  	/*
  	 * 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...
628

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

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

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

049a95a77   Joe Hershberger   net: cosmetic: Ch...
658
  	tftp_remote_ip = net_server_ip;
6ab128309   Joe Hershberger   net: Consolidate ...
659
  	if (!net_parse_bootfile(&tftp_remote_ip, tftp_filename, MAX_LEN)) {
ea45cb0ad   Matthias Weisser   net: Make sure IP...
660
  		sprintf(default_filename, "%02X%02X%02X%02X.img",
049a95a77   Joe Hershberger   net: cosmetic: Ch...
661
662
663
664
  			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...
665

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

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

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

1411157d8   Joe Hershberger   net: cosmetic: Fi...
698
699
700
701
  	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
702
  	}
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
703
704
  	putc('
  ');
1fb7cd498   Simon Glass   net: tftpput: imp...
705
  #ifdef CONFIG_CMD_TFTPPUT
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
706
707
  	tftp_put_active = (protocol == TFTPPUT);
  	if (tftp_put_active) {
1fb7cd498   Simon Glass   net: tftpput: imp...
708
709
710
711
  		printf("Save address: 0x%lx
  ", save_addr);
  		printf("Save size:    0x%lx
  ", save_size);
1411157d8   Joe Hershberger   net: cosmetic: Fi...
712
  		net_boot_file_size = save_size;
1fb7cd498   Simon Glass   net: tftpput: imp...
713
  		puts("Saving: *\b");
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
714
  		tftp_state = STATE_SEND_WRQ;
1fb7cd498   Simon Glass   net: tftpput: imp...
715
716
717
718
  		new_transfer();
  	} else
  #endif
  	{
a156c47e3   Simon Goldschmidt   tftp: prevent ove...
719
720
721
722
723
724
725
726
727
728
729
  		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...
730
  		puts("Loading: *\b");
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
731
  		tftp_state = STATE_SEND_RRQ;
64b8d7a67   Jörg Krause   net/tftp: fix bui...
732
  #ifdef CONFIG_CMD_BOOTEFI
0efe1bcf5   Alexander Graf   efi_loader: Add n...
733
  		efi_set_bootdev("Net", "", tftp_filename);
64b8d7a67   Jörg Krause   net/tftp: fix bui...
734
  #endif
1fb7cd498   Simon Glass   net: tftpput: imp...
735
  	}
fe8c2806c   wdenk   Initial revision
736

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

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

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

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

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
769
  	tftp_send();
fe8c2806c   wdenk   Initial revision
770
  }
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
771
  #ifdef CONFIG_CMD_TFTPSRV
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
772
  void tftp_start_server(void)
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
773
774
  {
  	tftp_filename[0] = 0;
a156c47e3   Simon Goldschmidt   tftp: prevent ove...
775
776
777
778
779
780
781
782
  	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:...
783
784
  	printf("Using %s device
  ", eth_get_name());
049a95a77   Joe Hershberger   net: cosmetic: Ch...
785
786
  	printf("Listening for TFTP transfer on %pI4
  ", &net_ip);
a156c47e3   Simon Goldschmidt   tftp: prevent ove...
787
788
  	printf("Load address: 0x%lx
  ", tftp_load_addr);
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
789
790
  
  	puts("Loading: *\b");
f5fb73467   Albert ARIBAUD \(3ADEV\)   net: TFTP: variab...
791
  	timeout_count_max = tftp_timeout_count_max;
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
792
793
  	timeout_count = 0;
  	timeout_ms = TIMEOUT;
bc0571fc1   Joe Hershberger   net: cosmetic: Fi...
794
  	net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
795

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
796
797
798
799
  	/* 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:...
800
801
  
  #ifdef CONFIG_TFTP_TSIZE
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
802
803
  	tftp_tsize = 0;
  	tftp_tsize_num_hash = 0;
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
804
  #endif
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
805
  	tftp_state = STATE_RECV_WRQ;
049a95a77   Joe Hershberger   net: cosmetic: Ch...
806
  	net_set_udp_handler(tftp_handler);
8e52533d1   Andrew Ruder   net: tftpsrv: Get...
807
808
  
  	/* zero out server ether in case the server ip has changed */
0adb5b761   Joe Hershberger   net: cosmetic: Na...
809
  	memset(net_server_ethaddr, 0, 6);
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
810
811
  }
  #endif /* CONFIG_CMD_TFTPSRV */