Blame view

net/tftp.c 24.6 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

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

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

aafda38fb   Remy Bohmer   Add error codes/h...
59
60
61
62
63
64
65
66
67
  enum {
  	TFTP_ERR_UNDEFINED           = 0,
  	TFTP_ERR_FILE_NOT_FOUND      = 1,
  	TFTP_ERR_ACCESS_DENIED       = 2,
  	TFTP_ERR_DISK_FULL           = 3,
  	TFTP_ERR_UNEXPECTED_OPCODE   = 4,
  	TFTP_ERR_UNKNOWN_TRANSFER_ID  = 5,
  	TFTP_ERR_FILE_ALREADY_EXISTS = 6,
  };
049a95a77   Joe Hershberger   net: cosmetic: Ch...
68
  static struct in_addr tftp_remote_ip;
2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
69
  /* The UDP port at their end */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
70
  static int	tftp_remote_port;
2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
71
  /* The UDP port at our end */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
72
73
  static int	tftp_our_port;
  static int	timeout_count;
2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
74
  /* packet sequence number */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
75
  static ulong	tftp_cur_block;
2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
76
  /* last packet sequence number received */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
77
  static ulong	tftp_prev_block;
2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
78
  /* count of sequence number wraparounds */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
79
  static ulong	tftp_block_wrap;
2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
80
  /* memory offset due to wrapping */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
81
82
  static ulong	tftp_block_wrap_offset;
  static int	tftp_state;
4fccb818e   Robin Getz   Add Transfer Size...
83
  #ifdef CONFIG_TFTP_TSIZE
2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
84
  /* The file size reported by the server */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
85
  static int	tftp_tsize;
2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
86
  /* The number of hashes we printed */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
87
  static short	tftp_tsize_num_hash;
4fccb818e   Robin Getz   Add Transfer Size...
88
  #endif
1fb7cd498   Simon Glass   net: tftpput: imp...
89
  #ifdef CONFIG_CMD_TFTPPUT
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
90
91
92
93
  /* 1 if writing, else 0 */
  static int	tftp_put_active;
  /* 1 if we have sent the last block */
  static int	tftp_put_final_block_sent;
1fb7cd498   Simon Glass   net: tftpput: imp...
94
  #else
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
95
  #define tftp_put_active	0
1fb7cd498   Simon Glass   net: tftpput: imp...
96
  #endif
3f85ce278   wdenk   * CVS add missing...
97

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

2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
106
107
108
109
  /* default TFTP block size */
  #define TFTP_BLOCK_SIZE		512
  /* sequence number is 16 bit */
  #define TFTP_SEQUENCE_SIZE	((ulong)(1<<16))
3f85ce278   wdenk   * CVS add missing...
110

fe8c2806c   wdenk   Initial revision
111
112
  #define DEFAULT_NAME_LEN	(8 + 4 + 1)
  static char default_filename[DEFAULT_NAME_LEN];
a93907c43   Jean-Christophe PLAGNIOL-VILLARD   TFTP: add host ip...
113
114
115
116
117
118
119
120
  
  #ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
  #define MAX_LEN 128
  #else
  #define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
  #endif
  
  static char tftp_filename[MAX_LEN];
fe8c2806c   wdenk   Initial revision
121

85eb5caf6   Wolfgang Denk   Coding style clea...
122
123
  /* 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...
124
   * almost-MTU block sizes.  At least try... fall back to 512 if need be.
89ba81d10   Alessandro Rubini   tftp: get the tft...
125
   * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
53a5c424b   David Updegraff   multicast tftp: R...
126
   */
89ba81d10   Alessandro Rubini   tftp: get the tft...
127
128
129
  #ifdef CONFIG_TFTP_BLOCKSIZE
  #define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
  #else
53a5c424b   David Updegraff   multicast tftp: R...
130
  #define TFTP_MTU_BLOCKSIZE 1468
89ba81d10   Alessandro Rubini   tftp: get the tft...
131
  #endif
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
132
133
  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...
134
135
136
137
  
  #ifdef CONFIG_MCAST_TFTP
  #include <malloc.h>
  #define MTFTP_BITMAPSIZE	0x1000
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
138
139
140
141
142
143
144
145
146
  static unsigned *tftp_mcast_bitmap;
  static int tftp_mcast_prev_hole;
  static int tftp_mcast_bitmap_size = MTFTP_BITMAPSIZE;
  static int tftp_mcast_disabled;
  static int tftp_mcast_master_client;
  static int tftp_mcast_active;
  static int tftp_mcast_port;
  /* can get 'last' block before done..*/
  static ulong tftp_mcast_ending_block;
53a5c424b   David Updegraff   multicast tftp: R...
147

c718b1439   Luca Ceresoli   net/tftp.c: cosme...
148
  static void parse_multicast_oack(char *pkt, int len);
53a5c424b   David Updegraff   multicast tftp: R...
149

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
150
  static void mcast_cleanup(void)
53a5c424b   David Updegraff   multicast tftp: R...
151
  {
049a95a77   Joe Hershberger   net: cosmetic: Ch...
152
153
  	if (net_mcast_addr)
  		eth_mcast_join(net_mcast_addr, 0);
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
154
155
156
  	if (tftp_mcast_bitmap)
  		free(tftp_mcast_bitmap);
  	tftp_mcast_bitmap = NULL;
049a95a77   Joe Hershberger   net: cosmetic: Ch...
157
  	net_mcast_addr.s_addr = 0;
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
158
159
160
  	tftp_mcast_active = 0;
  	tftp_mcast_port = 0;
  	tftp_mcast_ending_block = -1;
53a5c424b   David Updegraff   multicast tftp: R...
161
162
163
  }
  
  #endif	/* CONFIG_MCAST_TFTP */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
164
  static inline void store_block(int block, uchar *src, unsigned len)
fe8c2806c   wdenk   Initial revision
165
  {
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
166
  	ulong offset = block * tftp_block_size + tftp_block_wrap_offset;
3f85ce278   wdenk   * CVS add missing...
167
  	ulong newsize = offset + len;
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
168
  #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
fe8c2806c   wdenk   Initial revision
169
  	int i, rc = 0;
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
170
  	for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
fe8c2806c   wdenk   Initial revision
171
  		/* start address in flash? */
be1b0d277   Jochen Friedrich   Don't tftp to unk...
172
173
  		if (flash_info[i].flash_id == FLASH_UNKNOWN)
  			continue;
fe8c2806c   wdenk   Initial revision
174
175
176
177
178
179
180
  		if (load_addr + offset >= flash_info[i].start[0]) {
  			rc = 1;
  			break;
  		}
  	}
  
  	if (rc) { /* Flash is destination for this packet */
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
181
  		rc = flash_write((char *)src, (ulong)(load_addr+offset), len);
fe8c2806c   wdenk   Initial revision
182
  		if (rc) {
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
183
  			flash_perror(rc);
22f6e99d5   Joe Hershberger   net: Refactor to ...
184
  			net_set_state(NETLOOP_FAIL);
fe8c2806c   wdenk   Initial revision
185
186
  			return;
  		}
13dfe9437   Joe Hershberger   net: cosmetic: tf...
187
  	} else
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
188
  #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
fe8c2806c   wdenk   Initial revision
189
  	{
55d5fd9a8   Joe Hershberger   net: Access mappe...
190
191
192
193
  		void *ptr = map_sysmem(load_addr + offset, len);
  
  		memcpy(ptr, src, len);
  		unmap_sysmem(ptr);
fe8c2806c   wdenk   Initial revision
194
  	}
53a5c424b   David Updegraff   multicast tftp: R...
195
  #ifdef CONFIG_MCAST_TFTP
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
196
197
  	if (tftp_mcast_active)
  		ext2_set_bit(block, tftp_mcast_bitmap);
53a5c424b   David Updegraff   multicast tftp: R...
198
  #endif
fe8c2806c   wdenk   Initial revision
199

1411157d8   Joe Hershberger   net: cosmetic: Fi...
200
201
  	if (net_boot_file_size < newsize)
  		net_boot_file_size = newsize;
fe8c2806c   wdenk   Initial revision
202
  }
e4cde2f70   Simon Glass   net: tftpput: Fac...
203
  /* Clear our state ready for a new transfer */
165099e75   Simon Glass   net: Make net_tra...
204
  static void new_transfer(void)
e4cde2f70   Simon Glass   net: tftpput: Fac...
205
  {
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
206
207
208
  	tftp_prev_block = 0;
  	tftp_block_wrap = 0;
  	tftp_block_wrap_offset = 0;
e4cde2f70   Simon Glass   net: tftpput: Fac...
209
  #ifdef CONFIG_CMD_TFTPPUT
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
210
  	tftp_put_final_block_sent = 0;
e4cde2f70   Simon Glass   net: tftpput: Fac...
211
212
  #endif
  }
1fb7cd498   Simon Glass   net: tftpput: imp...
213
214
215
216
217
218
219
220
221
222
223
224
  #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...
225
  	ulong offset = ((int)block - 1) * len + tftp_block_wrap_offset;
1fb7cd498   Simon Glass   net: tftpput: imp...
226
  	ulong tosend = len;
1411157d8   Joe Hershberger   net: cosmetic: Fi...
227
  	tosend = min(net_boot_file_size - offset, tosend);
1fb7cd498   Simon Glass   net: tftpput: imp...
228
229
230
  	(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...
231
  	      block, offset, len, tosend);
1fb7cd498   Simon Glass   net: tftpput: imp...
232
233
234
  	return tosend;
  }
  #endif
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
235
236
  static void tftp_send(void);
  static void tftp_timeout_handler(void);
fe8c2806c   wdenk   Initial revision
237
238
  
  /**********************************************************************/
f5329bbc3   Simon Glass   net: tftpput: mov...
239
240
241
  static void show_block_marker(void)
  {
  #ifdef CONFIG_TFTP_TSIZE
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
242
243
244
  	if (tftp_tsize) {
  		ulong pos = tftp_cur_block * tftp_block_size +
  			tftp_block_wrap_offset;
7628afebe   Max Krummenacher   tftp.c: fix CONFI...
245
246
  		if (pos > tftp_tsize)
  			pos = tftp_tsize;
f5329bbc3   Simon Glass   net: tftpput: mov...
247

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
248
  		while (tftp_tsize_num_hash < pos * 50 / tftp_tsize) {
f5329bbc3   Simon Glass   net: tftpput: mov...
249
  			putc('#');
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
250
  			tftp_tsize_num_hash++;
f5329bbc3   Simon Glass   net: tftpput: mov...
251
  		}
1fb7cd498   Simon Glass   net: tftpput: imp...
252
  	} else
f5329bbc3   Simon Glass   net: tftpput: mov...
253
  #endif
1fb7cd498   Simon Glass   net: tftpput: imp...
254
  	{
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
255
  		if (((tftp_cur_block - 1) % 10) == 0)
f5329bbc3   Simon Glass   net: tftpput: mov...
256
  			putc('#');
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
257
  		else if ((tftp_cur_block % (10 * HASHES_PER_LINE)) == 0)
f5329bbc3   Simon Glass   net: tftpput: mov...
258
259
260
261
  			puts("
  \t ");
  	}
  }
e4cde2f70   Simon Glass   net: tftpput: Fac...
262
263
264
265
266
267
268
269
270
271
272
273
274
  /**
   * 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);
  #ifdef CONFIG_MCAST_TFTP
  	mcast_cleanup();
  #endif
bc0571fc1   Joe Hershberger   net: cosmetic: Fi...
275
  	net_start_again();
e4cde2f70   Simon Glass   net: tftpput: Fac...
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
  }
  
  /*
   * 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...
291
292
293
294
  	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...
295
296
297
298
  	} else {
  		show_block_marker();
  	}
  }
f5329bbc3   Simon Glass   net: tftpput: mov...
299
300
301
302
303
  /* 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...
304
  	while (tftp_tsize && tftp_tsize_num_hash < 49) {
f5329bbc3   Simon Glass   net: tftpput: mov...
305
  		putc('#');
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
306
  		tftp_tsize_num_hash++;
f5329bbc3   Simon Glass   net: tftpput: mov...
307
  	}
8104f5462   Simon Glass   net: Display the ...
308
  	puts("  ");
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
309
  	print_size(tftp_tsize, "");
f5329bbc3   Simon Glass   net: tftpput: mov...
310
  #endif
85b198027   Simon Glass   net: Add tftp spe...
311
312
313
314
  	time_start = get_timer(time_start);
  	if (time_start > 0) {
  		puts("
  \t ");	/* Line up with "Loading: " */
1411157d8   Joe Hershberger   net: cosmetic: Fi...
315
  		print_size(net_boot_file_size /
85b198027   Simon Glass   net: Add tftp spe...
316
317
  			time_start * 1000, "/s");
  	}
f5329bbc3   Simon Glass   net: tftpput: mov...
318
319
320
  	puts("
  done
  ");
22f6e99d5   Joe Hershberger   net: Refactor to ...
321
  	net_set_state(NETLOOP_SUCCESS);
f5329bbc3   Simon Glass   net: tftpput: mov...
322
  }
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
323
  static void tftp_send(void)
fe8c2806c   wdenk   Initial revision
324
  {
1fb7cd498   Simon Glass   net: tftpput: imp...
325
  	uchar *pkt;
db288a960   Joe Hershberger   net: Remove volat...
326
327
328
  	uchar *xp;
  	int len = 0;
  	ushort *s;
fe8c2806c   wdenk   Initial revision
329

85eb5caf6   Wolfgang Denk   Coding style clea...
330
  #ifdef CONFIG_MCAST_TFTP
53a5c424b   David Updegraff   multicast tftp: R...
331
  	/* Multicast TFTP.. non-MasterClients do not ACK data. */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
332
333
  	if (tftp_mcast_active && tftp_state == STATE_DATA &&
  	    tftp_mcast_master_client == 0)
53a5c424b   David Updegraff   multicast tftp: R...
334
335
  		return;
  #endif
fe8c2806c   wdenk   Initial revision
336
337
338
339
  	/*
  	 *	We will always be sending some sort of packet, so
  	 *	cobble together the packet headers now.
  	 */
1203fccee   Joe Hershberger   net: cosmetic: Cl...
340
  	pkt = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
fe8c2806c   wdenk   Initial revision
341

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
342
  	switch (tftp_state) {
e3fb0abe2   Luca Ceresoli   TFTP: rename STAT...
343
  	case STATE_SEND_RRQ:
1fb7cd498   Simon Glass   net: tftpput: imp...
344
  	case STATE_SEND_WRQ:
fe8c2806c   wdenk   Initial revision
345
  		xp = pkt;
7bc5ee078   Wolfgang Denk   Prepare U-Boot fo...
346
  		s = (ushort *)pkt;
8c6914f10   Simon Glass   net: Add more #if...
347
  #ifdef CONFIG_CMD_TFTPPUT
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
348
  		*s++ = htons(tftp_state == STATE_SEND_RRQ ? TFTP_RRQ :
1fb7cd498   Simon Glass   net: tftpput: imp...
349
  			TFTP_WRQ);
8c6914f10   Simon Glass   net: Add more #if...
350
351
352
  #else
  		*s++ = htons(TFTP_RRQ);
  #endif
7bc5ee078   Wolfgang Denk   Prepare U-Boot fo...
353
  		pkt = (uchar *)s;
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
354
  		strcpy((char *)pkt, tftp_filename);
fe8c2806c   wdenk   Initial revision
355
  		pkt += strlen(tftp_filename) + 1;
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
356
  		strcpy((char *)pkt, "octet");
fe8c2806c   wdenk   Initial revision
357
  		pkt += 5 /*strlen("octet")*/ + 1;
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
358
  		strcpy((char *)pkt, "timeout");
fbe4b5cbd   wdenk   * Update TRAB aut...
359
  		pkt += 7 /*strlen("timeout")*/ + 1;
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
360
  		sprintf((char *)pkt, "%lu", timeout_ms / 1000);
0ebf04c60   Robin Getz   minor debug clean...
361
362
  		debug("send option \"timeout %s\"
  ", (char *)pkt);
fbe4b5cbd   wdenk   * Update TRAB aut...
363
  		pkt += strlen((char *)pkt) + 1;
4fccb818e   Robin Getz   Add Transfer Size...
364
  #ifdef CONFIG_TFTP_TSIZE
1411157d8   Joe Hershberger   net: cosmetic: Fi...
365
366
  		pkt += sprintf((char *)pkt, "tsize%c%u%c",
  				0, net_boot_file_size, 0);
4fccb818e   Robin Getz   Add Transfer Size...
367
  #endif
53a5c424b   David Updegraff   multicast tftp: R...
368
  		/* try for more effic. blk size */
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
369
  		pkt += sprintf((char *)pkt, "blksize%c%d%c",
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
370
  				0, tftp_block_size_option, 0);
85eb5caf6   Wolfgang Denk   Coding style clea...
371
  #ifdef CONFIG_MCAST_TFTP
53a5c424b   David Updegraff   multicast tftp: R...
372
  		/* Check all preconditions before even trying the option */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
373
374
375
376
377
  		if (!tftp_mcast_disabled) {
  			tftp_mcast_bitmap = malloc(tftp_mcast_bitmap_size);
  			if (tftp_mcast_bitmap && eth_get_dev()->mcast) {
  				free(tftp_mcast_bitmap);
  				tftp_mcast_bitmap = NULL;
13dfe9437   Joe Hershberger   net: cosmetic: tf...
378
379
380
  				pkt += sprintf((char *)pkt, "multicast%c%c",
  					0, 0);
  			}
53a5c424b   David Updegraff   multicast tftp: R...
381
382
  		}
  #endif /* CONFIG_MCAST_TFTP */
fe8c2806c   wdenk   Initial revision
383
384
  		len = pkt - xp;
  		break;
fbe4b5cbd   wdenk   * Update TRAB aut...
385
  	case STATE_OACK:
53a5c424b   David Updegraff   multicast tftp: R...
386
  #ifdef CONFIG_MCAST_TFTP
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
387
388
389
390
391
392
  		/* My turn!  Start at where I need blocks I missed. */
  		if (tftp_mcast_active)
  			tftp_cur_block = ext2_find_next_zero_bit(
  				tftp_mcast_bitmap,
  				tftp_mcast_bitmap_size * 8, 0);
  		/* fall through */
53a5c424b   David Updegraff   multicast tftp: R...
393
  #endif
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
394
395
  
  	case STATE_RECV_WRQ:
53a5c424b   David Updegraff   multicast tftp: R...
396
  	case STATE_DATA:
fe8c2806c   wdenk   Initial revision
397
  		xp = pkt;
7bc5ee078   Wolfgang Denk   Prepare U-Boot fo...
398
  		s = (ushort *)pkt;
1fb7cd498   Simon Glass   net: tftpput: imp...
399
  		s[0] = htons(TFTP_ACK);
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
400
  		s[1] = htons(tftp_cur_block);
1fb7cd498   Simon Glass   net: tftpput: imp...
401
402
  		pkt = (uchar *)(s + 2);
  #ifdef CONFIG_CMD_TFTPPUT
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
403
404
405
  		if (tftp_put_active) {
  			int toload = tftp_block_size;
  			int loaded = load_block(tftp_cur_block, pkt, toload);
1fb7cd498   Simon Glass   net: tftpput: imp...
406
407
408
  
  			s[0] = htons(TFTP_DATA);
  			pkt += loaded;
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
409
  			tftp_put_final_block_sent = (loaded < toload);
1fb7cd498   Simon Glass   net: tftpput: imp...
410
411
  		}
  #endif
fe8c2806c   wdenk   Initial revision
412
413
414
415
416
  		len = pkt - xp;
  		break;
  
  	case STATE_TOO_LARGE:
  		xp = pkt;
7bc5ee078   Wolfgang Denk   Prepare U-Boot fo...
417
418
  		s = (ushort *)pkt;
  		*s++ = htons(TFTP_ERROR);
1fb7cd498   Simon Glass   net: tftpput: imp...
419
  			*s++ = htons(3);
7bc5ee078   Wolfgang Denk   Prepare U-Boot fo...
420
  		pkt = (uchar *)s;
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
421
  		strcpy((char *)pkt, "File too large");
fe8c2806c   wdenk   Initial revision
422
423
424
425
426
427
  		pkt += 14 /*strlen("File too large")*/ + 1;
  		len = pkt - xp;
  		break;
  
  	case STATE_BAD_MAGIC:
  		xp = pkt;
7bc5ee078   Wolfgang Denk   Prepare U-Boot fo...
428
429
430
431
  		s = (ushort *)pkt;
  		*s++ = htons(TFTP_ERROR);
  		*s++ = htons(2);
  		pkt = (uchar *)s;
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
432
  		strcpy((char *)pkt, "File has bad magic");
fe8c2806c   wdenk   Initial revision
433
434
435
436
  		pkt += 18 /*strlen("File has bad magic")*/ + 1;
  		len = pkt - xp;
  		break;
  	}
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
437
438
  	net_send_udp_packet(net_server_ethaddr, tftp_remote_ip,
  			    tftp_remote_port, tftp_our_port, len);
fe8c2806c   wdenk   Initial revision
439
  }
39bccd21d   Simon Glass   net: Hide more co...
440
  #ifdef CONFIG_CMD_TFTPPUT
1fb7cd498   Simon Glass   net: tftpput: imp...
441
  static void icmp_handler(unsigned type, unsigned code, unsigned dest,
049a95a77   Joe Hershberger   net: cosmetic: Ch...
442
443
  			 struct in_addr sip, unsigned src, uchar *pkt,
  			 unsigned len)
1fb7cd498   Simon Glass   net: tftpput: imp...
444
445
446
447
448
449
  {
  	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...
450
  #endif
1fb7cd498   Simon Glass   net: tftpput: imp...
451

049a95a77   Joe Hershberger   net: cosmetic: Ch...
452
453
  static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
  			 unsigned src, unsigned len)
fe8c2806c   wdenk   Initial revision
454
  {
61fdd4f7c   Kim Phillips   net/tftp: sparse ...
455
456
  	__be16 proto;
  	__be16 *s;
ff13ac8c7   Wolfgang Denk   Backout commit 8f...
457
  	int i;
fe8c2806c   wdenk   Initial revision
458

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
459
  	if (dest != tftp_our_port) {
53a5c424b   David Updegraff   multicast tftp: R...
460
  #ifdef CONFIG_MCAST_TFTP
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
461
462
  		if (tftp_mcast_active &&
  		    (!tftp_mcast_port || dest != tftp_mcast_port))
53a5c424b   David Updegraff   multicast tftp: R...
463
  #endif
0bdd8acc3   Luca Ceresoli   net/tftp.c: cosme...
464
  			return;
fe8c2806c   wdenk   Initial revision
465
  	}
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
466
467
  	if (tftp_state != STATE_SEND_RRQ && src != tftp_remote_port &&
  	    tftp_state != STATE_RECV_WRQ && tftp_state != STATE_SEND_WRQ)
fe8c2806c   wdenk   Initial revision
468
  		return;
fe8c2806c   wdenk   Initial revision
469

7bc325a1b   Luca Ceresoli   net/tftp.c: cosme...
470
  	if (len < 2)
fe8c2806c   wdenk   Initial revision
471
  		return;
fe8c2806c   wdenk   Initial revision
472
473
  	len -= 2;
  	/* warning: don't use increment (++) in ntohs() macros!! */
61fdd4f7c   Kim Phillips   net/tftp: sparse ...
474
  	s = (__be16 *)pkt;
7bc5ee078   Wolfgang Denk   Prepare U-Boot fo...
475
476
  	proto = *s++;
  	pkt = (uchar *)s;
fe8c2806c   wdenk   Initial revision
477
  	switch (ntohs(proto)) {
fe8c2806c   wdenk   Initial revision
478
  	case TFTP_RRQ:
1fb7cd498   Simon Glass   net: tftpput: imp...
479
  		break;
fe8c2806c   wdenk   Initial revision
480
  	case TFTP_ACK:
1fb7cd498   Simon Glass   net: tftpput: imp...
481
  #ifdef CONFIG_CMD_TFTPPUT
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
482
483
  		if (tftp_put_active) {
  			if (tftp_put_final_block_sent) {
1fb7cd498   Simon Glass   net: tftpput: imp...
484
485
486
487
488
489
490
  				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...
491
  				int ack_ok = (tftp_cur_block == block);
1fb7cd498   Simon Glass   net: tftpput: imp...
492

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
493
  				tftp_cur_block = (unsigned short)(block + 1);
1fb7cd498   Simon Glass   net: tftpput: imp...
494
495
  				update_block_number();
  				if (ack_ok)
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
496
  					tftp_send(); /* Send next data block */
1fb7cd498   Simon Glass   net: tftpput: imp...
497
498
499
  			}
  		}
  #endif
fe8c2806c   wdenk   Initial revision
500
  		break;
1fb7cd498   Simon Glass   net: tftpput: imp...
501

fe8c2806c   wdenk   Initial revision
502
503
  	default:
  		break;
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
504
505
506
507
  #ifdef CONFIG_CMD_TFTPSRV
  	case TFTP_WRQ:
  		debug("Got WRQ
  ");
049a95a77   Joe Hershberger   net: cosmetic: Ch...
508
  		tftp_remote_ip = sip;
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
509
510
  		tftp_remote_port = src;
  		tftp_our_port = 1024 + (get_timer(0) % 3072);
e4cde2f70   Simon Glass   net: tftpput: Fac...
511
  		new_transfer();
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
512
  		tftp_send(); /* Send ACK(0) */
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
513
514
  		break;
  #endif
fbe4b5cbd   wdenk   * Update TRAB aut...
515
  	case TFTP_OACK:
d371708a1   Wolfgang Denk   net/tftp.c: fix w...
516
517
  		debug("Got OACK: %s %s
  ",
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
518
519
520
  		      pkt, pkt + strlen((char *)pkt) + 1);
  		tftp_state = STATE_OACK;
  		tftp_remote_port = src;
60174746c   Wolfgang Denk   Fix TFTP OACK cod...
521
522
523
524
525
  		/*
  		 * 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...
526
  		for (i = 0; i+8 < len; i++) {
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
527
528
529
530
  			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...
531
532
  				debug("Blocksize ack: %s, %d
  ",
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
533
  				      (char *)pkt + i + 8, tftp_block_size);
ff13ac8c7   Wolfgang Denk   Backout commit 8f...
534
  			}
4fccb818e   Robin Getz   Add Transfer Size...
535
  #ifdef CONFIG_TFTP_TSIZE
2e320257c   Luca Ceresoli   net/tftp.c: cosme...
536
  			if (strcmp((char *)pkt+i, "tsize") == 0) {
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
537
  				tftp_tsize = simple_strtoul((char *)pkt + i + 6,
2f09413fd   Luca Ceresoli   net/tftp.c: cosme...
538
  							   NULL, 10);
4fccb818e   Robin Getz   Add Transfer Size...
539
540
  				debug("size = %s, %d
  ",
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
541
  				      (char *)pkt + i + 6, tftp_tsize);
4fccb818e   Robin Getz   Add Transfer Size...
542
543
  			}
  #endif
53a5c424b   David Updegraff   multicast tftp: R...
544
545
  		}
  #ifdef CONFIG_MCAST_TFTP
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
546
547
548
  		parse_multicast_oack((char *)pkt, len - 1);
  		if ((tftp_mcast_active) && (!tftp_mcast_master_client))
  			tftp_state = STATE_DATA;	/* passive.. */
53a5c424b   David Updegraff   multicast tftp: R...
549
550
  		else
  #endif
1fb7cd498   Simon Glass   net: tftpput: imp...
551
  #ifdef CONFIG_CMD_TFTPPUT
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
552
  		if (tftp_put_active) {
1fb7cd498   Simon Glass   net: tftpput: imp...
553
  			/* Get ready to send the first block */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
554
555
  			tftp_state = STATE_DATA;
  			tftp_cur_block++;
1fb7cd498   Simon Glass   net: tftpput: imp...
556
557
  		}
  #endif
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
558
  		tftp_send(); /* Send ACK or first data block */
fbe4b5cbd   wdenk   * Update TRAB aut...
559
  		break;
fe8c2806c   wdenk   Initial revision
560
561
562
563
  	case TFTP_DATA:
  		if (len < 2)
  			return;
  		len -= 2;
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
564
  		tftp_cur_block = ntohs(*(__be16 *)pkt);
3f85ce278   wdenk   * CVS add missing...
565

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

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

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
572
573
  		if (tftp_state == STATE_SEND_RRQ || tftp_state == STATE_OACK ||
  		    tftp_state == STATE_RECV_WRQ) {
3f85ce278   wdenk   * CVS add missing...
574
  			/* first block received */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
575
576
  			tftp_state = STATE_DATA;
  			tftp_remote_port = src;
e4cde2f70   Simon Glass   net: tftpput: Fac...
577
  			new_transfer();
fe8c2806c   wdenk   Initial revision
578

53a5c424b   David Updegraff   multicast tftp: R...
579
  #ifdef CONFIG_MCAST_TFTP
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
580
581
  			if (tftp_mcast_active) { /* start!=1 common if mcast */
  				tftp_prev_block = tftp_cur_block - 1;
53a5c424b   David Updegraff   multicast tftp: R...
582
583
  			} else
  #endif
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
584
585
586
587
588
589
590
591
592
  			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...
593
  				net_start_again();
fe8c2806c   wdenk   Initial revision
594
595
596
  				break;
  			}
  		}
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
597
598
  		if (tftp_cur_block == tftp_prev_block) {
  			/* Same block again; ignore it. */
fe8c2806c   wdenk   Initial revision
599
600
  			break;
  		}
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
601
  		tftp_prev_block = tftp_cur_block;
f5fb73467   Albert ARIBAUD \(3ADEV\)   net: TFTP: variab...
602
  		timeout_count_max = tftp_timeout_count_max;
bc0571fc1   Joe Hershberger   net: cosmetic: Fi...
603
  		net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
fe8c2806c   wdenk   Initial revision
604

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
605
  		store_block(tftp_cur_block - 1, pkt + 2, len);
fe8c2806c   wdenk   Initial revision
606
607
  
  		/*
4d69e98c0   Luca Ceresoli   net/tftp.c: fix typo
608
  		 *	Acknowledge the block just received, which will prompt
20478ceaa   Luca Ceresoli   TFTP: replace "se...
609
  		 *	the remote for the next one.
fe8c2806c   wdenk   Initial revision
610
  		 */
53a5c424b   David Updegraff   multicast tftp: R...
611
  #ifdef CONFIG_MCAST_TFTP
85eb5caf6   Wolfgang Denk   Coding style clea...
612
613
  		/* if I am the MasterClient, actively calculate what my next
  		 * needed block is; else I'm passive; not ACKING
a93907c43   Jean-Christophe PLAGNIOL-VILLARD   TFTP: add host ip...
614
  		 */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
615
616
617
618
619
620
621
622
623
624
625
626
627
  		if (tftp_mcast_active) {
  			if (len < tftp_block_size)  {
  				tftp_mcast_ending_block = tftp_cur_block;
  			} else if (tftp_mcast_master_client) {
  				tftp_mcast_prev_hole = ext2_find_next_zero_bit(
  					tftp_mcast_bitmap,
  					tftp_mcast_bitmap_size * 8,
  					tftp_mcast_prev_hole);
  				tftp_cur_block = tftp_mcast_prev_hole;
  				if (tftp_cur_block >
  				    ((tftp_mcast_bitmap_size * 8) - 1)) {
  					debug("tftpfile too big
  ");
53a5c424b   David Updegraff   multicast tftp: R...
628
  					/* try to double it and retry */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
629
  					tftp_mcast_bitmap_size <<= 1;
53a5c424b   David Updegraff   multicast tftp: R...
630
  					mcast_cleanup();
bc0571fc1   Joe Hershberger   net: cosmetic: Fi...
631
  					net_start_again();
53a5c424b   David Updegraff   multicast tftp: R...
632
633
  					return;
  				}
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
634
  				tftp_prev_block = tftp_cur_block;
53a5c424b   David Updegraff   multicast tftp: R...
635
636
637
  			}
  		}
  #endif
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
638
  		tftp_send();
fe8c2806c   wdenk   Initial revision
639

53a5c424b   David Updegraff   multicast tftp: R...
640
  #ifdef CONFIG_MCAST_TFTP
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
641
642
643
  		if (tftp_mcast_active) {
  			if (tftp_mcast_master_client &&
  			    (tftp_cur_block >= tftp_mcast_ending_block)) {
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
644
645
646
  				puts("
  Multicast tftp done
  ");
53a5c424b   David Updegraff   multicast tftp: R...
647
  				mcast_cleanup();
22f6e99d5   Joe Hershberger   net: Refactor to ...
648
  				net_set_state(NETLOOP_SUCCESS);
85eb5caf6   Wolfgang Denk   Coding style clea...
649
  			}
13dfe9437   Joe Hershberger   net: cosmetic: tf...
650
  		} else
53a5c424b   David Updegraff   multicast tftp: R...
651
  #endif
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
652
  		if (len < tftp_block_size)
f5329bbc3   Simon Glass   net: tftpput: mov...
653
  			tftp_complete();
fe8c2806c   wdenk   Initial revision
654
655
656
  		break;
  
  	case TFTP_ERROR:
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
657
658
659
  		printf("
  TFTP error: '%s' (%d)
  ",
61fdd4f7c   Kim Phillips   net/tftp: sparse ...
660
  		       pkt + 2, ntohs(*(__be16 *)pkt));
aafda38fb   Remy Bohmer   Add error codes/h...
661

61fdd4f7c   Kim Phillips   net/tftp: sparse ...
662
  		switch (ntohs(*(__be16 *)pkt)) {
aafda38fb   Remy Bohmer   Add error codes/h...
663
664
665
666
667
  		case TFTP_ERR_FILE_NOT_FOUND:
  		case TFTP_ERR_ACCESS_DENIED:
  			puts("Not retrying...
  ");
  			eth_halt();
22f6e99d5   Joe Hershberger   net: Refactor to ...
668
  			net_set_state(NETLOOP_FAIL);
aafda38fb   Remy Bohmer   Add error codes/h...
669
670
671
672
673
674
675
676
677
678
  			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
  
  ");
53a5c424b   David Updegraff   multicast tftp: R...
679
  #ifdef CONFIG_MCAST_TFTP
aafda38fb   Remy Bohmer   Add error codes/h...
680
  			mcast_cleanup();
53a5c424b   David Updegraff   multicast tftp: R...
681
  #endif
bc0571fc1   Joe Hershberger   net: cosmetic: Fi...
682
  			net_start_again();
aafda38fb   Remy Bohmer   Add error codes/h...
683
684
  			break;
  		}
fe8c2806c   wdenk   Initial revision
685
686
687
  		break;
  	}
  }
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
688
  static void tftp_timeout_handler(void)
fe8c2806c   wdenk   Initial revision
689
  {
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
690
  	if (++timeout_count > timeout_count_max) {
e4cde2f70   Simon Glass   net: tftpput: Fac...
691
  		restart("Retry count exceeded");
fe8c2806c   wdenk   Initial revision
692
  	} else {
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
693
  		puts("T ");
bc0571fc1   Joe Hershberger   net: cosmetic: Fi...
694
  		net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
695
696
  		if (tftp_state != STATE_RECV_WRQ)
  			tftp_send();
fe8c2806c   wdenk   Initial revision
697
698
  	}
  }
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
699
  void tftp_start(enum proto_t protocol)
fe8c2806c   wdenk   Initial revision
700
  {
f5fb73467   Albert ARIBAUD \(3ADEV\)   net: TFTP: variab...
701
  #if CONFIG_NET_TFTP_VARS
ecb0ccd9c   Wolfgang Denk   Allow to force TF...
702
  	char *ep;             /* Environment pointer */
89ba81d10   Alessandro Rubini   tftp: get the tft...
703

c96f86eef   Wolfgang Denk   TFTP: allow for a...
704
705
706
707
  	/*
  	 * 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...
708

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

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

af2ca59e6   Bin Meng   net: Revert "tftp...
717
718
719
  	if (timeout_ms < 1000) {
  		printf("TFTP timeout (%ld ms) too low, set min = 1000 ms
  ",
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
720
  		       timeout_ms);
af2ca59e6   Bin Meng   net: Revert "tftp...
721
  		timeout_ms = 1000;
c96f86eef   Wolfgang Denk   TFTP: allow for a...
722
  	}
00caae6d4   Simon Glass   env: Rename geten...
723
  	ep = env_get("tftptimeoutcountmax");
f5fb73467   Albert ARIBAUD \(3ADEV\)   net: TFTP: variab...
724
725
726
727
728
729
730
731
732
733
  	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...
734
735
  	debug("TFTP blocksize = %i, timeout = %ld ms
  ",
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
736
  	      tftp_block_size_option, timeout_ms);
ecb0ccd9c   Wolfgang Denk   Allow to force TF...
737

049a95a77   Joe Hershberger   net: cosmetic: Ch...
738
  	tftp_remote_ip = net_server_ip;
1411157d8   Joe Hershberger   net: cosmetic: Fi...
739
  	if (net_boot_file_name[0] == '\0') {
ea45cb0ad   Matthias Weisser   net: Make sure IP...
740
  		sprintf(default_filename, "%02X%02X%02X%02X.img",
049a95a77   Joe Hershberger   net: cosmetic: Ch...
741
742
743
744
  			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...
745

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

c718b1439   Luca Ceresoli   net/tftp.c: cosme...
749
750
  		printf("*** Warning: no boot file name; using '%s'
  ",
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
751
  		       tftp_filename);
fe8c2806c   wdenk   Initial revision
752
  	} else {
1411157d8   Joe Hershberger   net: cosmetic: Fi...
753
  		char *p = strchr(net_boot_file_name, ':');
a93907c43   Jean-Christophe PLAGNIOL-VILLARD   TFTP: add host ip...
754
755
  
  		if (p == NULL) {
1411157d8   Joe Hershberger   net: cosmetic: Fi...
756
  			strncpy(tftp_filename, net_boot_file_name, MAX_LEN);
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
757
  			tftp_filename[MAX_LEN - 1] = 0;
a93907c43   Jean-Christophe PLAGNIOL-VILLARD   TFTP: add host ip...
758
  		} else {
1411157d8   Joe Hershberger   net: cosmetic: Fi...
759
  			tftp_remote_ip = string_to_ip(net_boot_file_name);
6a86bb6c2   Peter Tyser   net: Fix TftpStar...
760
  			strncpy(tftp_filename, p + 1, MAX_LEN);
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
761
  			tftp_filename[MAX_LEN - 1] = 0;
a93907c43   Jean-Christophe PLAGNIOL-VILLARD   TFTP: add host ip...
762
  		}
fe8c2806c   wdenk   Initial revision
763
  	}
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
764
765
  	printf("Using %s device
  ", eth_get_name());
1fb7cd498   Simon Glass   net: tftpput: imp...
766
  	printf("TFTP %s server %pI4; our IP address is %pI4",
8c6914f10   Simon Glass   net: Add more #if...
767
768
769
  #ifdef CONFIG_CMD_TFTPPUT
  	       protocol == TFTPPUT ? "to" : "from",
  #else
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
770
  	       "from",
8c6914f10   Simon Glass   net: Add more #if...
771
  #endif
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
772
  	       &tftp_remote_ip, &net_ip);
fe8c2806c   wdenk   Initial revision
773
774
  
  	/* Check if we need to send across this subnet */
049a95a77   Joe Hershberger   net: cosmetic: Ch...
775
776
777
778
779
780
781
782
  	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
783
  	}
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
784
785
  	putc('
  ');
fe8c2806c   wdenk   Initial revision
786

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

1411157d8   Joe Hershberger   net: cosmetic: Fi...
789
790
791
792
  	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
793
  	}
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
794
795
  	putc('
  ');
1fb7cd498   Simon Glass   net: tftpput: imp...
796
  #ifdef CONFIG_CMD_TFTPPUT
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
797
798
  	tftp_put_active = (protocol == TFTPPUT);
  	if (tftp_put_active) {
1fb7cd498   Simon Glass   net: tftpput: imp...
799
800
801
802
  		printf("Save address: 0x%lx
  ", save_addr);
  		printf("Save size:    0x%lx
  ", save_size);
1411157d8   Joe Hershberger   net: cosmetic: Fi...
803
  		net_boot_file_size = save_size;
1fb7cd498   Simon Glass   net: tftpput: imp...
804
  		puts("Saving: *\b");
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
805
  		tftp_state = STATE_SEND_WRQ;
1fb7cd498   Simon Glass   net: tftpput: imp...
806
807
808
809
810
811
812
  		new_transfer();
  	} else
  #endif
  	{
  		printf("Load address: 0x%lx
  ", load_addr);
  		puts("Loading: *\b");
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
813
  		tftp_state = STATE_SEND_RRQ;
64b8d7a67   Jörg Krause   net/tftp: fix bui...
814
  #ifdef CONFIG_CMD_BOOTEFI
0efe1bcf5   Alexander Graf   efi_loader: Add n...
815
  		efi_set_bootdev("Net", "", tftp_filename);
64b8d7a67   Jörg Krause   net/tftp: fix bui...
816
  #endif
1fb7cd498   Simon Glass   net: tftpput: imp...
817
  	}
fe8c2806c   wdenk   Initial revision
818

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

bc0571fc1   Joe Hershberger   net: cosmetic: Fi...
822
  	net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
049a95a77   Joe Hershberger   net: cosmetic: Ch...
823
  	net_set_udp_handler(tftp_handler);
39bccd21d   Simon Glass   net: Hide more co...
824
  #ifdef CONFIG_CMD_TFTPPUT
1fb7cd498   Simon Glass   net: tftpput: imp...
825
  	net_set_icmp_handler(icmp_handler);
39bccd21d   Simon Glass   net: Hide more co...
826
  #endif
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
827
828
  	tftp_remote_port = WELL_KNOWN_PORT;
  	timeout_count = 0;
ecb0ccd9c   Wolfgang Denk   Allow to force TF...
829
  	/* Use a pseudo-random port unless a specific port is set */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
830
  	tftp_our_port = 1024 + (get_timer(0) % 3072);
53a5c424b   David Updegraff   multicast tftp: R...
831

ecb0ccd9c   Wolfgang Denk   Allow to force TF...
832
  #ifdef CONFIG_TFTP_PORT
00caae6d4   Simon Glass   env: Rename geten...
833
  	ep = env_get("tftpdstp");
7bc325a1b   Luca Ceresoli   net/tftp.c: cosme...
834
  	if (ep != NULL)
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
835
  		tftp_remote_port = simple_strtol(ep, NULL, 10);
00caae6d4   Simon Glass   env: Rename geten...
836
  	ep = env_get("tftpsrcp");
7bc325a1b   Luca Ceresoli   net/tftp.c: cosme...
837
  	if (ep != NULL)
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
838
  		tftp_our_port = simple_strtol(ep, NULL, 10);
ecb0ccd9c   Wolfgang Denk   Allow to force TF...
839
  #endif
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
840
  	tftp_cur_block = 0;
fe8c2806c   wdenk   Initial revision
841

73a8b27c5   wdenk   * Add support for...
842
  	/* zero out server ether in case the server ip has changed */
0adb5b761   Joe Hershberger   net: cosmetic: Na...
843
  	memset(net_server_ethaddr, 0, 6);
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
844
845
  	/* Revert tftp_block_size to dflt */
  	tftp_block_size = TFTP_BLOCK_SIZE;
53a5c424b   David Updegraff   multicast tftp: R...
846
  #ifdef CONFIG_MCAST_TFTP
a93907c43   Jean-Christophe PLAGNIOL-VILLARD   TFTP: add host ip...
847
  	mcast_cleanup();
53a5c424b   David Updegraff   multicast tftp: R...
848
  #endif
4fccb818e   Robin Getz   Add Transfer Size...
849
  #ifdef CONFIG_TFTP_TSIZE
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
850
851
  	tftp_tsize = 0;
  	tftp_tsize_num_hash = 0;
4fccb818e   Robin Getz   Add Transfer Size...
852
  #endif
73a8b27c5   wdenk   * Add support for...
853

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
854
  	tftp_send();
fe8c2806c   wdenk   Initial revision
855
  }
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
856
  #ifdef CONFIG_CMD_TFTPSRV
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
857
  void tftp_start_server(void)
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
858
859
  {
  	tftp_filename[0] = 0;
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
860
861
  	printf("Using %s device
  ", eth_get_name());
049a95a77   Joe Hershberger   net: cosmetic: Ch...
862
863
  	printf("Listening for TFTP transfer on %pI4
  ", &net_ip);
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
864
865
866
867
  	printf("Load address: 0x%lx
  ", load_addr);
  
  	puts("Loading: *\b");
f5fb73467   Albert ARIBAUD \(3ADEV\)   net: TFTP: variab...
868
  	timeout_count_max = tftp_timeout_count_max;
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
869
870
  	timeout_count = 0;
  	timeout_ms = TIMEOUT;
bc0571fc1   Joe Hershberger   net: cosmetic: Fi...
871
  	net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
872

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
873
874
875
876
  	/* 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:...
877
878
  
  #ifdef CONFIG_TFTP_TSIZE
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
879
880
  	tftp_tsize = 0;
  	tftp_tsize_num_hash = 0;
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
881
  #endif
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
882
  	tftp_state = STATE_RECV_WRQ;
049a95a77   Joe Hershberger   net: cosmetic: Ch...
883
  	net_set_udp_handler(tftp_handler);
8e52533d1   Andrew Ruder   net: tftpsrv: Get...
884
885
  
  	/* zero out server ether in case the server ip has changed */
0adb5b761   Joe Hershberger   net: cosmetic: Na...
886
  	memset(net_server_ethaddr, 0, 6);
e59e35620   Luca Ceresoli   TFTP: net/tftp.c:...
887
888
  }
  #endif /* CONFIG_CMD_TFTPSRV */
53a5c424b   David Updegraff   multicast tftp: R...
889
  #ifdef CONFIG_MCAST_TFTP
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
890
891
  /*
   * Credits: atftp project.
53a5c424b   David Updegraff   multicast tftp: R...
892
   */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
893
894
  /*
   * Pick up BcastAddr, Port, and whether I am [now] the master-client.
53a5c424b   David Updegraff   multicast tftp: R...
895
896
897
898
899
900
901
902
903
904
905
906
907
   * Frame:
   *    +-------+-----------+---+-------~~-------+---+
   *    |  opc  | multicast | 0 | addr, port, mc | 0 |
   *    +-------+-----------+---+-------~~-------+---+
   * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
   * I am the new master-client so must send ACKs to DataBlocks.  If I am not
   * master-client, I'm a passive client, gathering what DataBlocks I may and
   * making note of which ones I got in my bitmask.
   * In theory, I never go from master->passive..
   * .. this comes in with pkt already pointing just past opc
   */
  static void parse_multicast_oack(char *pkt, int len)
  {
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
908
  	int i;
049a95a77   Joe Hershberger   net: cosmetic: Ch...
909
  	struct in_addr addr;
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
910
911
912
  	char *mc_adr;
  	char *port;
  	char *mc;
53a5c424b   David Updegraff   multicast tftp: R...
913

8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
914
915
916
  	mc_adr = NULL;
  	port = NULL;
  	mc = NULL;
53a5c424b   David Updegraff   multicast tftp: R...
917
918
919
  	/* march along looking for 'multicast\0', which has to start at least
  	 * 14 bytes back from the end.
  	 */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
920
921
  	for (i = 0; i < len - 14; i++)
  		if (strcmp(pkt + i, "multicast") == 0)
53a5c424b   David Updegraff   multicast tftp: R...
922
  			break;
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
923
  	if (i >= (len - 14)) /* non-Multicast OACK, ign. */
53a5c424b   David Updegraff   multicast tftp: R...
924
  		return;
85eb5caf6   Wolfgang Denk   Coding style clea...
925

c718b1439   Luca Ceresoli   net/tftp.c: cosme...
926
  	i += 10; /* strlen multicast */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
927
  	mc_adr = pkt + i;
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
928
  	for (; i < len; i++) {
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
929
930
  		if (*(pkt + i) == ',') {
  			*(pkt + i) = '\0';
53a5c424b   David Updegraff   multicast tftp: R...
931
  			if (port) {
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
932
  				mc = pkt + i + 1;
53a5c424b   David Updegraff   multicast tftp: R...
933
934
  				break;
  			} else {
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
935
  				port = pkt + i + 1;
53a5c424b   David Updegraff   multicast tftp: R...
936
937
938
  			}
  		}
  	}
6d2231e8f   Luca Ceresoli   net/tftp.c: cosme...
939
940
  	if (!port || !mc_adr || !mc)
  		return;
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
941
  	if (tftp_mcast_active && tftp_mcast_master_client) {
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
942
943
  		printf("I got a OACK as master Client, WRONG!
  ");
53a5c424b   David Updegraff   multicast tftp: R...
944
945
946
  		return;
  	}
  	/* ..I now accept packets destined for this MCAST addr, port */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
947
948
  	if (!tftp_mcast_active) {
  		if (tftp_mcast_bitmap) {
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
949
950
  			printf("Internal failure! no mcast.
  ");
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
951
952
953
954
  			free(tftp_mcast_bitmap);
  			tftp_mcast_bitmap = NULL;
  			tftp_mcast_disabled = 1;
  			return;
53a5c424b   David Updegraff   multicast tftp: R...
955
956
  		}
  		/* I malloc instead of pre-declare; so that if the file ends
85eb5caf6   Wolfgang Denk   Coding style clea...
957
958
  		 * up being too big for this bitmap I can retry
  		 */
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
959
960
961
962
963
  		tftp_mcast_bitmap = malloc(tftp_mcast_bitmap_size);
  		if (!tftp_mcast_bitmap) {
  			printf("No bitmap, no multicast. Sorry.
  ");
  			tftp_mcast_disabled = 1;
53a5c424b   David Updegraff   multicast tftp: R...
964
965
  			return;
  		}
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
966
967
968
  		memset(tftp_mcast_bitmap, 0, tftp_mcast_bitmap_size);
  		tftp_mcast_prev_hole = 0;
  		tftp_mcast_active = 1;
53a5c424b   David Updegraff   multicast tftp: R...
969
970
  	}
  	addr = string_to_ip(mc_adr);
049a95a77   Joe Hershberger   net: cosmetic: Ch...
971
972
973
974
975
  	if (net_mcast_addr.s_addr != addr.s_addr) {
  		if (net_mcast_addr.s_addr)
  			eth_mcast_join(net_mcast_addr, 0);
  		net_mcast_addr = addr;
  		if (eth_mcast_join(net_mcast_addr, 1)) {
c718b1439   Luca Ceresoli   net/tftp.c: cosme...
976
977
  			printf("Fail to set mcast, revert to TFTP
  ");
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
978
  			tftp_mcast_disabled = 1;
53a5c424b   David Updegraff   multicast tftp: R...
979
  			mcast_cleanup();
bc0571fc1   Joe Hershberger   net: cosmetic: Fi...
980
  			net_start_again();
53a5c424b   David Updegraff   multicast tftp: R...
981
982
  		}
  	}
8885c5fe9   Joe Hershberger   net: cosmetic: Cl...
983
984
985
986
987
  	tftp_mcast_master_client = simple_strtoul((char *)mc, NULL, 10);
  	tftp_mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
  	printf("Multicast: %s:%d [%d]
  ", mc_adr, tftp_mcast_port,
  	       tftp_mcast_master_client);
53a5c424b   David Updegraff   multicast tftp: R...
988
989
990
991
  	return;
  }
  
  #endif /* Multicast TFTP */