]>
Commit | Line | Data |
---|---|---|
fe8c2806 | 1 | /* |
2f09413f LC |
2 | * Copyright 1994, 1995, 2000 Neil Russell. |
3 | * (See License) | |
4 | * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, [email protected] | |
e59e3562 LC |
5 | * Copyright 2011 Comelit Group SpA, |
6 | * Luca Ceresoli <[email protected]> | |
fe8c2806 | 7 | */ |
fe8c2806 | 8 | #include <command.h> |
4e4bf944 | 9 | #include <display_options.h> |
0efe1bcf | 10 | #include <efi_loader.h> |
7b51b576 | 11 | #include <env.h> |
8e8ccfe1 | 12 | #include <image.h> |
4d72caa5 | 13 | #include <lmb.h> |
f7ae49fc | 14 | #include <log.h> |
55d5fd9a | 15 | #include <mapmem.h> |
fe8c2806 | 16 | #include <net.h> |
7fbf230d | 17 | #include <net6.h> |
401d1c4f | 18 | #include <asm/global_data.h> |
34696958 | 19 | #include <net/tftp.h> |
fe8c2806 WD |
20 | #include "bootp.h" |
21 | ||
a156c47e SG |
22 | DECLARE_GLOBAL_DATA_PTR; |
23 | ||
2f09413f LC |
24 | /* Well known TFTP port # */ |
25 | #define WELL_KNOWN_PORT 69 | |
26 | /* Millisecs to timeout for lost pkt */ | |
af2ca59e | 27 | #define TIMEOUT 5000UL |
2f09413f LC |
28 | /* Number of "loading" hashes per line (for checking the image size) */ |
29 | #define HASHES_PER_LINE 65 | |
fe8c2806 WD |
30 | |
31 | /* | |
32 | * TFTP operations. | |
33 | */ | |
34 | #define TFTP_RRQ 1 | |
35 | #define TFTP_WRQ 2 | |
36 | #define TFTP_DATA 3 | |
37 | #define TFTP_ACK 4 | |
38 | #define TFTP_ERROR 5 | |
fbe4b5cb | 39 | #define TFTP_OACK 6 |
fe8c2806 | 40 | |
8885c5fe | 41 | static ulong timeout_ms = TIMEOUT; |
01d1b99c | 42 | static int timeout_count_max = (CONFIG_NET_RETRY_COUNT * 2); |
85b19802 | 43 | static ulong time_start; /* Record time we started tftp */ |
7fbf230d | 44 | static struct in6_addr tftp_remote_ip6; |
e83cc063 BS |
45 | |
46 | /* | |
47 | * These globals govern the timeout behavior when attempting a connection to a | |
8885c5fe | 48 | * TFTP server. tftp_timeout_ms specifies the number of milliseconds to |
e83cc063 | 49 | * wait for the server to respond to initial connection. Second global, |
8885c5fe JH |
50 | * tftp_timeout_count_max, gives the number of such connection retries. |
51 | * tftp_timeout_count_max must be non-negative and tftp_timeout_ms must be | |
e83cc063 BS |
52 | * positive. The globals are meant to be set (and restored) by code needing |
53 | * non-standard timeout behavior when initiating a TFTP transfer. | |
54 | */ | |
8885c5fe | 55 | ulong tftp_timeout_ms = TIMEOUT; |
01d1b99c | 56 | int tftp_timeout_count_max = (CONFIG_NET_RETRY_COUNT * 2); |
e83cc063 | 57 | |
aafda38f RB |
58 | enum { |
59 | TFTP_ERR_UNDEFINED = 0, | |
60 | TFTP_ERR_FILE_NOT_FOUND = 1, | |
61 | TFTP_ERR_ACCESS_DENIED = 2, | |
62 | TFTP_ERR_DISK_FULL = 3, | |
63 | TFTP_ERR_UNEXPECTED_OPCODE = 4, | |
64 | TFTP_ERR_UNKNOWN_TRANSFER_ID = 5, | |
65 | TFTP_ERR_FILE_ALREADY_EXISTS = 6, | |
08139210 | 66 | TFTP_ERR_OPTION_NEGOTIATION = 8, |
aafda38f RB |
67 | }; |
68 | ||
049a95a7 | 69 | static struct in_addr tftp_remote_ip; |
2f09413f | 70 | /* The UDP port at their end */ |
8885c5fe | 71 | static int tftp_remote_port; |
2f09413f | 72 | /* The UDP port at our end */ |
8885c5fe JH |
73 | static int tftp_our_port; |
74 | static int timeout_count; | |
2f09413f | 75 | /* packet sequence number */ |
8885c5fe | 76 | static ulong tftp_cur_block; |
2f09413f | 77 | /* last packet sequence number received */ |
8885c5fe | 78 | static ulong tftp_prev_block; |
2f09413f | 79 | /* count of sequence number wraparounds */ |
8885c5fe | 80 | static ulong tftp_block_wrap; |
2f09413f | 81 | /* memory offset due to wrapping */ |
8885c5fe JH |
82 | static ulong tftp_block_wrap_offset; |
83 | static int tftp_state; | |
a156c47e SG |
84 | static ulong tftp_load_addr; |
85 | #ifdef CONFIG_LMB | |
86 | static ulong tftp_load_size; | |
87 | #endif | |
4fccb818 | 88 | #ifdef CONFIG_TFTP_TSIZE |
2f09413f | 89 | /* The file size reported by the server */ |
8885c5fe | 90 | static int tftp_tsize; |
2f09413f | 91 | /* The number of hashes we printed */ |
8885c5fe | 92 | static short tftp_tsize_num_hash; |
4fccb818 | 93 | #endif |
cc6b87ec RF |
94 | /* The window size negotiated */ |
95 | static ushort tftp_windowsize; | |
96 | /* Next block to send ack to */ | |
97 | static ushort tftp_next_ack; | |
98 | /* Last nack block we send */ | |
99 | static ushort tftp_last_nack; | |
1fb7cd49 | 100 | #ifdef CONFIG_CMD_TFTPPUT |
8885c5fe JH |
101 | /* 1 if writing, else 0 */ |
102 | static int tftp_put_active; | |
103 | /* 1 if we have sent the last block */ | |
104 | static int tftp_put_final_block_sent; | |
1fb7cd49 | 105 | #else |
8885c5fe | 106 | #define tftp_put_active 0 |
1fb7cd49 | 107 | #endif |
3f85ce27 | 108 | |
e3fb0abe | 109 | #define STATE_SEND_RRQ 1 |
fe8c2806 WD |
110 | #define STATE_DATA 2 |
111 | #define STATE_TOO_LARGE 3 | |
112 | #define STATE_BAD_MAGIC 4 | |
fbe4b5cb | 113 | #define STATE_OACK 5 |
e59e3562 | 114 | #define STATE_RECV_WRQ 6 |
1fb7cd49 | 115 | #define STATE_SEND_WRQ 7 |
08139210 | 116 | #define STATE_INVALID_OPTION 8 |
fe8c2806 | 117 | |
2f09413f LC |
118 | /* default TFTP block size */ |
119 | #define TFTP_BLOCK_SIZE 512 | |
7fbf230d | 120 | #define TFTP_MTU_BLOCKSIZE6 (CONFIG_TFTP_BLOCKSIZE - 20) |
2f09413f LC |
121 | /* sequence number is 16 bit */ |
122 | #define TFTP_SEQUENCE_SIZE ((ulong)(1<<16)) | |
3f85ce27 | 123 | |
fe8c2806 WD |
124 | #define DEFAULT_NAME_LEN (8 + 4 + 1) |
125 | static char default_filename[DEFAULT_NAME_LEN]; | |
a93907c4 JCPV |
126 | |
127 | #ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN | |
128 | #define MAX_LEN 128 | |
129 | #else | |
130 | #define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN | |
131 | #endif | |
132 | ||
133 | static char tftp_filename[MAX_LEN]; | |
fe8c2806 | 134 | |
85eb5caf WD |
135 | /* 512 is poor choice for ethernet, MTU is typically 1500. |
136 | * Minus eth.hdrs thats 1468. Can get 2x better throughput with | |
53a5c424 | 137 | * almost-MTU block sizes. At least try... fall back to 512 if need be. |
89ba81d1 | 138 | * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file) |
53a5c424 | 139 | */ |
89ba81d1 | 140 | |
cc6b87ec RF |
141 | /* When windowsize is defined to 1, |
142 | * tftp behaves the same way as it was | |
143 | * never declared | |
144 | */ | |
145 | #ifdef CONFIG_TFTP_WINDOWSIZE | |
146 | #define TFTP_WINDOWSIZE CONFIG_TFTP_WINDOWSIZE | |
147 | #else | |
148 | #define TFTP_WINDOWSIZE 1 | |
149 | #endif | |
150 | ||
8885c5fe | 151 | static unsigned short tftp_block_size = TFTP_BLOCK_SIZE; |
31d275b0 | 152 | static unsigned short tftp_block_size_option = CONFIG_TFTP_BLOCKSIZE; |
cc6b87ec | 153 | static unsigned short tftp_window_size_option = TFTP_WINDOWSIZE; |
53a5c424 | 154 | |
a156c47e | 155 | static inline int store_block(int block, uchar *src, unsigned int len) |
fe8c2806 | 156 | { |
ae0bdf09 LFT |
157 | ulong offset = block * tftp_block_size + tftp_block_wrap_offset - |
158 | tftp_block_size; | |
3f85ce27 | 159 | ulong newsize = offset + len; |
a156c47e | 160 | ulong store_addr = tftp_load_addr + offset; |
52938fc4 | 161 | void *ptr; |
a156c47e SG |
162 | |
163 | #ifdef CONFIG_LMB | |
52938fc4 | 164 | ulong end_addr = tftp_load_addr + tftp_load_size; |
ca48cb40 | 165 | |
52938fc4 TR |
166 | if (!end_addr) |
167 | end_addr = ULONG_MAX; | |
ca48cb40 | 168 | |
52938fc4 TR |
169 | if (store_addr < tftp_load_addr || |
170 | store_addr + len > end_addr) { | |
171 | puts("\nTFTP error: "); | |
172 | puts("trying to overwrite reserved memory...\n"); | |
173 | return -1; | |
fe8c2806 | 174 | } |
52938fc4 TR |
175 | #endif |
176 | ptr = map_sysmem(store_addr, len); | |
177 | memcpy(ptr, src, len); | |
178 | unmap_sysmem(ptr); | |
fe8c2806 | 179 | |
1411157d JH |
180 | if (net_boot_file_size < newsize) |
181 | net_boot_file_size = newsize; | |
a156c47e SG |
182 | |
183 | return 0; | |
fe8c2806 WD |
184 | } |
185 | ||
e4cde2f7 | 186 | /* Clear our state ready for a new transfer */ |
165099e7 | 187 | static void new_transfer(void) |
e4cde2f7 | 188 | { |
8885c5fe JH |
189 | tftp_prev_block = 0; |
190 | tftp_block_wrap = 0; | |
191 | tftp_block_wrap_offset = 0; | |
e4cde2f7 | 192 | #ifdef CONFIG_CMD_TFTPPUT |
8885c5fe | 193 | tftp_put_final_block_sent = 0; |
e4cde2f7 SG |
194 | #endif |
195 | } | |
196 | ||
1fb7cd49 SG |
197 | #ifdef CONFIG_CMD_TFTPPUT |
198 | /** | |
199 | * Load the next block from memory to be sent over tftp. | |
200 | * | |
201 | * @param block Block number to send | |
202 | * @param dst Destination buffer for data | |
203 | * @param len Number of bytes in block (this one and every other) | |
185f812c | 204 | * Return: number of bytes loaded |
1fb7cd49 SG |
205 | */ |
206 | static int load_block(unsigned block, uchar *dst, unsigned len) | |
207 | { | |
208 | /* We may want to get the final block from the previous set */ | |
f6a158b9 LFT |
209 | ulong offset = block * tftp_block_size + tftp_block_wrap_offset - |
210 | tftp_block_size; | |
1fb7cd49 SG |
211 | ulong tosend = len; |
212 | ||
1411157d | 213 | tosend = min(net_boot_file_size - offset, tosend); |
bb872dd9 | 214 | (void)memcpy(dst, (void *)(image_save_addr + offset), tosend); |
2bcc43b3 | 215 | debug("%s: block=%u, offset=%lu, len=%u, tosend=%lu\n", __func__, |
8885c5fe | 216 | block, offset, len, tosend); |
1fb7cd49 SG |
217 | return tosend; |
218 | } | |
219 | #endif | |
220 | ||
8885c5fe JH |
221 | static void tftp_send(void); |
222 | static void tftp_timeout_handler(void); | |
fe8c2806 WD |
223 | |
224 | /**********************************************************************/ | |
225 | ||
f5329bbc SG |
226 | static void show_block_marker(void) |
227 | { | |
de5468e6 RH |
228 | ulong pos; |
229 | ||
f5329bbc | 230 | #ifdef CONFIG_TFTP_TSIZE |
8885c5fe | 231 | if (tftp_tsize) { |
de5468e6 | 232 | pos = tftp_cur_block * tftp_block_size + |
8885c5fe | 233 | tftp_block_wrap_offset; |
7628afeb MK |
234 | if (pos > tftp_tsize) |
235 | pos = tftp_tsize; | |
f5329bbc | 236 | |
8885c5fe | 237 | while (tftp_tsize_num_hash < pos * 50 / tftp_tsize) { |
f5329bbc | 238 | putc('#'); |
8885c5fe | 239 | tftp_tsize_num_hash++; |
f5329bbc | 240 | } |
1fb7cd49 | 241 | } else |
f5329bbc | 242 | #endif |
1fb7cd49 | 243 | { |
de5468e6 RH |
244 | pos = (tftp_cur_block - 1) + |
245 | (tftp_block_wrap * TFTP_SEQUENCE_SIZE); | |
246 | if ((pos % 10) == 0) | |
f5329bbc | 247 | putc('#'); |
de5468e6 | 248 | else if (((pos + 1) % (10 * HASHES_PER_LINE)) == 0) |
f5329bbc SG |
249 | puts("\n\t "); |
250 | } | |
251 | } | |
252 | ||
e4cde2f7 SG |
253 | /** |
254 | * restart the current transfer due to an error | |
255 | * | |
256 | * @param msg Message to print for user | |
257 | */ | |
258 | static void restart(const char *msg) | |
259 | { | |
260 | printf("\n%s; starting again\n", msg); | |
bc0571fc | 261 | net_start_again(); |
e4cde2f7 SG |
262 | } |
263 | ||
264 | /* | |
265 | * Check if the block number has wrapped, and update progress | |
266 | * | |
267 | * TODO: The egregious use of global variables in this file should be tidied. | |
268 | */ | |
269 | static void update_block_number(void) | |
270 | { | |
271 | /* | |
272 | * RFC1350 specifies that the first data packet will | |
273 | * have sequence number 1. If we receive a sequence | |
274 | * number of 0 this means that there was a wrap | |
275 | * around of the (16 bit) counter. | |
276 | */ | |
8885c5fe JH |
277 | if (tftp_cur_block == 0 && tftp_prev_block != 0) { |
278 | tftp_block_wrap++; | |
279 | tftp_block_wrap_offset += tftp_block_size * TFTP_SEQUENCE_SIZE; | |
280 | timeout_count = 0; /* we've done well, reset the timeout */ | |
e4cde2f7 | 281 | } |
de5468e6 | 282 | show_block_marker(); |
e4cde2f7 SG |
283 | } |
284 | ||
f5329bbc SG |
285 | /* The TFTP get or put is complete */ |
286 | static void tftp_complete(void) | |
287 | { | |
288 | #ifdef CONFIG_TFTP_TSIZE | |
289 | /* Print hash marks for the last packet received */ | |
8885c5fe | 290 | while (tftp_tsize && tftp_tsize_num_hash < 49) { |
f5329bbc | 291 | putc('#'); |
8885c5fe | 292 | tftp_tsize_num_hash++; |
f5329bbc | 293 | } |
8104f546 | 294 | puts(" "); |
8885c5fe | 295 | print_size(tftp_tsize, ""); |
f5329bbc | 296 | #endif |
85b19802 SG |
297 | time_start = get_timer(time_start); |
298 | if (time_start > 0) { | |
299 | puts("\n\t "); /* Line up with "Loading: " */ | |
1411157d | 300 | print_size(net_boot_file_size / |
85b19802 SG |
301 | time_start * 1000, "/s"); |
302 | } | |
f5329bbc | 303 | puts("\ndone\n"); |
593607c5 AT |
304 | if (!tftp_put_active) |
305 | efi_set_bootdev("Net", "", tftp_filename, | |
306 | map_sysmem(tftp_load_addr, 0), | |
307 | net_boot_file_size); | |
22f6e99d | 308 | net_set_state(NETLOOP_SUCCESS); |
f5329bbc SG |
309 | } |
310 | ||
8885c5fe | 311 | static void tftp_send(void) |
fe8c2806 | 312 | { |
1fb7cd49 | 313 | uchar *pkt; |
db288a96 JH |
314 | uchar *xp; |
315 | int len = 0; | |
316 | ushort *s; | |
08139210 | 317 | bool err_pkt = false; |
fe8c2806 WD |
318 | |
319 | /* | |
320 | * We will always be sending some sort of packet, so | |
321 | * cobble together the packet headers now. | |
322 | */ | |
7fbf230d VM |
323 | if (IS_ENABLED(CONFIG_IPV6) && use_ip6) |
324 | pkt = net_tx_packet + net_eth_hdr_size() + | |
325 | IP6_HDR_SIZE + UDP_HDR_SIZE; | |
326 | else | |
327 | pkt = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE; | |
fe8c2806 | 328 | |
8885c5fe | 329 | switch (tftp_state) { |
e3fb0abe | 330 | case STATE_SEND_RRQ: |
1fb7cd49 | 331 | case STATE_SEND_WRQ: |
fe8c2806 | 332 | xp = pkt; |
7bc5ee07 | 333 | s = (ushort *)pkt; |
8c6914f1 | 334 | #ifdef CONFIG_CMD_TFTPPUT |
8885c5fe | 335 | *s++ = htons(tftp_state == STATE_SEND_RRQ ? TFTP_RRQ : |
1fb7cd49 | 336 | TFTP_WRQ); |
8c6914f1 SG |
337 | #else |
338 | *s++ = htons(TFTP_RRQ); | |
339 | #endif | |
7bc5ee07 | 340 | pkt = (uchar *)s; |
c718b143 | 341 | strcpy((char *)pkt, tftp_filename); |
fe8c2806 | 342 | pkt += strlen(tftp_filename) + 1; |
c718b143 | 343 | strcpy((char *)pkt, "octet"); |
fe8c2806 | 344 | pkt += 5 /*strlen("octet")*/ + 1; |
c718b143 | 345 | strcpy((char *)pkt, "timeout"); |
fbe4b5cb | 346 | pkt += 7 /*strlen("timeout")*/ + 1; |
8885c5fe | 347 | sprintf((char *)pkt, "%lu", timeout_ms / 1000); |
0ebf04c6 | 348 | debug("send option \"timeout %s\"\n", (char *)pkt); |
fbe4b5cb | 349 | pkt += strlen((char *)pkt) + 1; |
4fccb818 | 350 | #ifdef CONFIG_TFTP_TSIZE |
1411157d JH |
351 | pkt += sprintf((char *)pkt, "tsize%c%u%c", |
352 | 0, net_boot_file_size, 0); | |
4fccb818 | 353 | #endif |
53a5c424 | 354 | /* try for more effic. blk size */ |
c718b143 | 355 | pkt += sprintf((char *)pkt, "blksize%c%d%c", |
8885c5fe | 356 | 0, tftp_block_size_option, 0); |
cc6b87ec RF |
357 | |
358 | /* try for more effic. window size. | |
359 | * Implemented only for tftp get. | |
360 | * Don't bother sending if it's 1 | |
361 | */ | |
362 | if (tftp_state == STATE_SEND_RRQ && tftp_window_size_option > 1) | |
363 | pkt += sprintf((char *)pkt, "windowsize%c%d%c", | |
364 | 0, tftp_window_size_option, 0); | |
fe8c2806 WD |
365 | len = pkt - xp; |
366 | break; | |
367 | ||
fbe4b5cb | 368 | case STATE_OACK: |
e59e3562 LC |
369 | |
370 | case STATE_RECV_WRQ: | |
53a5c424 | 371 | case STATE_DATA: |
fe8c2806 | 372 | xp = pkt; |
7bc5ee07 | 373 | s = (ushort *)pkt; |
1fb7cd49 | 374 | s[0] = htons(TFTP_ACK); |
8885c5fe | 375 | s[1] = htons(tftp_cur_block); |
1fb7cd49 SG |
376 | pkt = (uchar *)(s + 2); |
377 | #ifdef CONFIG_CMD_TFTPPUT | |
8885c5fe JH |
378 | if (tftp_put_active) { |
379 | int toload = tftp_block_size; | |
380 | int loaded = load_block(tftp_cur_block, pkt, toload); | |
1fb7cd49 SG |
381 | |
382 | s[0] = htons(TFTP_DATA); | |
383 | pkt += loaded; | |
8885c5fe | 384 | tftp_put_final_block_sent = (loaded < toload); |
1fb7cd49 SG |
385 | } |
386 | #endif | |
fe8c2806 WD |
387 | len = pkt - xp; |
388 | break; | |
389 | ||
390 | case STATE_TOO_LARGE: | |
391 | xp = pkt; | |
7bc5ee07 WD |
392 | s = (ushort *)pkt; |
393 | *s++ = htons(TFTP_ERROR); | |
1fb7cd49 SG |
394 | *s++ = htons(3); |
395 | ||
7bc5ee07 | 396 | pkt = (uchar *)s; |
c718b143 | 397 | strcpy((char *)pkt, "File too large"); |
fe8c2806 WD |
398 | pkt += 14 /*strlen("File too large")*/ + 1; |
399 | len = pkt - xp; | |
08139210 | 400 | err_pkt = true; |
fe8c2806 WD |
401 | break; |
402 | ||
403 | case STATE_BAD_MAGIC: | |
404 | xp = pkt; | |
7bc5ee07 WD |
405 | s = (ushort *)pkt; |
406 | *s++ = htons(TFTP_ERROR); | |
407 | *s++ = htons(2); | |
408 | pkt = (uchar *)s; | |
c718b143 | 409 | strcpy((char *)pkt, "File has bad magic"); |
fe8c2806 WD |
410 | pkt += 18 /*strlen("File has bad magic")*/ + 1; |
411 | len = pkt - xp; | |
08139210 RH |
412 | err_pkt = true; |
413 | break; | |
414 | ||
415 | case STATE_INVALID_OPTION: | |
416 | xp = pkt; | |
417 | s = (ushort *)pkt; | |
418 | *s++ = htons(TFTP_ERROR); | |
419 | *s++ = htons(TFTP_ERR_OPTION_NEGOTIATION); | |
420 | pkt = (uchar *)s; | |
421 | strcpy((char *)pkt, "Option Negotiation Failed"); | |
422 | /* strlen("Option Negotiation Failed") + NULL*/ | |
423 | pkt += 25 + 1; | |
424 | len = pkt - xp; | |
425 | err_pkt = true; | |
fe8c2806 WD |
426 | break; |
427 | } | |
428 | ||
7fbf230d VM |
429 | if (IS_ENABLED(CONFIG_IPV6) && use_ip6) |
430 | net_send_udp_packet6(net_server_ethaddr, | |
431 | &tftp_remote_ip6, | |
432 | tftp_remote_port, | |
433 | tftp_our_port, len); | |
434 | else | |
435 | net_send_udp_packet(net_server_ethaddr, tftp_remote_ip, | |
436 | tftp_remote_port, tftp_our_port, len); | |
08139210 RH |
437 | |
438 | if (err_pkt) | |
439 | net_set_state(NETLOOP_FAIL); | |
fe8c2806 WD |
440 | } |
441 | ||
39bccd21 | 442 | #ifdef CONFIG_CMD_TFTPPUT |
1fb7cd49 | 443 | static void icmp_handler(unsigned type, unsigned code, unsigned dest, |
049a95a7 JH |
444 | struct in_addr sip, unsigned src, uchar *pkt, |
445 | unsigned len) | |
1fb7cd49 SG |
446 | { |
447 | if (type == ICMP_NOT_REACH && code == ICMP_NOT_REACH_PORT) { | |
448 | /* Oh dear the other end has gone away */ | |
449 | restart("TFTP server died"); | |
450 | } | |
451 | } | |
39bccd21 | 452 | #endif |
1fb7cd49 | 453 | |
049a95a7 JH |
454 | static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip, |
455 | unsigned src, unsigned len) | |
fe8c2806 | 456 | { |
61fdd4f7 KP |
457 | __be16 proto; |
458 | __be16 *s; | |
ff13ac8c | 459 | int i; |
08139210 | 460 | u16 timeout_val_rcvd; |
fe8c2806 | 461 | |
8885c5fe | 462 | if (dest != tftp_our_port) { |
0bdd8acc | 463 | return; |
fe8c2806 | 464 | } |
8885c5fe JH |
465 | if (tftp_state != STATE_SEND_RRQ && src != tftp_remote_port && |
466 | tftp_state != STATE_RECV_WRQ && tftp_state != STATE_SEND_WRQ) | |
fe8c2806 | 467 | return; |
fe8c2806 | 468 | |
7bc325a1 | 469 | if (len < 2) |
fe8c2806 | 470 | return; |
fe8c2806 WD |
471 | len -= 2; |
472 | /* warning: don't use increment (++) in ntohs() macros!! */ | |
61fdd4f7 | 473 | s = (__be16 *)pkt; |
7bc5ee07 WD |
474 | proto = *s++; |
475 | pkt = (uchar *)s; | |
fe8c2806 | 476 | switch (ntohs(proto)) { |
fe8c2806 | 477 | case TFTP_RRQ: |
1fb7cd49 SG |
478 | break; |
479 | ||
fe8c2806 | 480 | case TFTP_ACK: |
1fb7cd49 | 481 | #ifdef CONFIG_CMD_TFTPPUT |
8885c5fe JH |
482 | if (tftp_put_active) { |
483 | if (tftp_put_final_block_sent) { | |
1fb7cd49 SG |
484 | tftp_complete(); |
485 | } else { | |
486 | /* | |
487 | * Move to the next block. We want our block | |
488 | * count to wrap just like the other end! | |
489 | */ | |
490 | int block = ntohs(*s); | |
8885c5fe | 491 | int ack_ok = (tftp_cur_block == block); |
1fb7cd49 | 492 | |
6bf46367 | 493 | tftp_prev_block = tftp_cur_block; |
8885c5fe | 494 | tftp_cur_block = (unsigned short)(block + 1); |
1fb7cd49 SG |
495 | update_block_number(); |
496 | if (ack_ok) | |
8885c5fe | 497 | tftp_send(); /* Send next data block */ |
1fb7cd49 SG |
498 | } |
499 | } | |
500 | #endif | |
fe8c2806 | 501 | break; |
1fb7cd49 | 502 | |
fe8c2806 WD |
503 | default: |
504 | break; | |
505 | ||
e59e3562 LC |
506 | #ifdef CONFIG_CMD_TFTPSRV |
507 | case TFTP_WRQ: | |
508 | debug("Got WRQ\n"); | |
049a95a7 | 509 | tftp_remote_ip = sip; |
8885c5fe JH |
510 | tftp_remote_port = src; |
511 | tftp_our_port = 1024 + (get_timer(0) % 3072); | |
e4cde2f7 | 512 | new_transfer(); |
8885c5fe | 513 | tftp_send(); /* Send ACK(0) */ |
e59e3562 LC |
514 | break; |
515 | #endif | |
516 | ||
fbe4b5cb | 517 | case TFTP_OACK: |
08139210 RH |
518 | debug("Got OACK: "); |
519 | for (i = 0; i < len; i++) { | |
520 | if (pkt[i] == '\0') | |
521 | debug(" "); | |
522 | else | |
523 | debug("%c", pkt[i]); | |
524 | } | |
525 | debug("\n"); | |
8885c5fe JH |
526 | tftp_state = STATE_OACK; |
527 | tftp_remote_port = src; | |
60174746 WD |
528 | /* |
529 | * Check for 'blksize' option. | |
530 | * Careful: "i" is signed, "len" is unsigned, thus | |
531 | * something like "len-8" may give a *huge* number | |
532 | */ | |
c718b143 | 533 | for (i = 0; i+8 < len; i++) { |
08139210 | 534 | if (strcasecmp((char *)pkt + i, "blksize") == 0) { |
8885c5fe | 535 | tftp_block_size = (unsigned short) |
0b1284eb | 536 | dectoul((char *)pkt + i + 8, NULL); |
08139210 | 537 | debug("Blocksize oack: %s, %d\n", |
8885c5fe | 538 | (char *)pkt + i + 8, tftp_block_size); |
08139210 RH |
539 | if (tftp_block_size > tftp_block_size_option) { |
540 | printf("Invalid blk size(=%d)\n", | |
541 | tftp_block_size); | |
542 | tftp_state = STATE_INVALID_OPTION; | |
543 | } | |
544 | } | |
545 | if (strcasecmp((char *)pkt + i, "timeout") == 0) { | |
546 | timeout_val_rcvd = (unsigned short) | |
0b1284eb | 547 | dectoul((char *)pkt + i + 8, NULL); |
08139210 RH |
548 | debug("Timeout oack: %s, %d\n", |
549 | (char *)pkt + i + 8, timeout_val_rcvd); | |
550 | if (timeout_val_rcvd != (timeout_ms / 1000)) { | |
551 | printf("Invalid timeout val(=%d s)\n", | |
552 | timeout_val_rcvd); | |
553 | tftp_state = STATE_INVALID_OPTION; | |
554 | } | |
ff13ac8c | 555 | } |
4fccb818 | 556 | #ifdef CONFIG_TFTP_TSIZE |
08139210 | 557 | if (strcasecmp((char *)pkt + i, "tsize") == 0) { |
0b1284eb SG |
558 | tftp_tsize = dectoul((char *)pkt + i + 6, |
559 | NULL); | |
4fccb818 | 560 | debug("size = %s, %d\n", |
8885c5fe | 561 | (char *)pkt + i + 6, tftp_tsize); |
4fccb818 RG |
562 | } |
563 | #endif | |
cc6b87ec RF |
564 | if (strcasecmp((char *)pkt + i, "windowsize") == 0) { |
565 | tftp_windowsize = | |
0b1284eb | 566 | dectoul((char *)pkt + i + 11, NULL); |
cc6b87ec RF |
567 | debug("windowsize = %s, %d\n", |
568 | (char *)pkt + i + 11, tftp_windowsize); | |
569 | } | |
53a5c424 | 570 | } |
cc6b87ec RF |
571 | |
572 | tftp_next_ack = tftp_windowsize; | |
573 | ||
1fb7cd49 | 574 | #ifdef CONFIG_CMD_TFTPPUT |
08139210 | 575 | if (tftp_put_active && tftp_state == STATE_OACK) { |
1fb7cd49 | 576 | /* Get ready to send the first block */ |
8885c5fe JH |
577 | tftp_state = STATE_DATA; |
578 | tftp_cur_block++; | |
1fb7cd49 SG |
579 | } |
580 | #endif | |
8885c5fe | 581 | tftp_send(); /* Send ACK or first data block */ |
fbe4b5cb | 582 | break; |
fe8c2806 WD |
583 | case TFTP_DATA: |
584 | if (len < 2) | |
585 | return; | |
586 | len -= 2; | |
cc6b87ec RF |
587 | |
588 | if (ntohs(*(__be16 *)pkt) != (ushort)(tftp_cur_block + 1)) { | |
589 | debug("Received unexpected block: %d, expected: %d\n", | |
590 | ntohs(*(__be16 *)pkt), | |
591 | (ushort)(tftp_cur_block + 1)); | |
21a265c0 SE |
592 | /* |
593 | * Only ACK if the block count received is greater than | |
594 | * the expected block count, otherwise skip ACK. | |
595 | * (required to properly handle the server retransmitting | |
596 | * the window) | |
597 | */ | |
598 | if ((ushort)(tftp_cur_block + 1) - (short)(ntohs(*(__be16 *)pkt)) > 0) | |
599 | break; | |
cc6b87ec RF |
600 | /* |
601 | * If one packet is dropped most likely | |
602 | * all other buffers in the window | |
603 | * that will arrive will cause a sending NACK. | |
604 | * This just overwellms the server, let's just send one. | |
605 | */ | |
606 | if (tftp_last_nack != tftp_cur_block) { | |
607 | tftp_send(); | |
608 | tftp_last_nack = tftp_cur_block; | |
609 | tftp_next_ack = (ushort)(tftp_cur_block + | |
610 | tftp_windowsize); | |
611 | } | |
612 | break; | |
613 | } | |
614 | ||
615 | tftp_cur_block++; | |
616 | tftp_cur_block %= TFTP_SEQUENCE_SIZE; | |
3f85ce27 | 617 | |
beb61e13 | 618 | if (tftp_state == STATE_SEND_RRQ) { |
08139210 | 619 | debug("Server did not acknowledge any options!\n"); |
beb61e13 HB |
620 | tftp_next_ack = tftp_windowsize; |
621 | } | |
fbe4b5cb | 622 | |
8885c5fe JH |
623 | if (tftp_state == STATE_SEND_RRQ || tftp_state == STATE_OACK || |
624 | tftp_state == STATE_RECV_WRQ) { | |
3f85ce27 | 625 | /* first block received */ |
8885c5fe JH |
626 | tftp_state = STATE_DATA; |
627 | tftp_remote_port = src; | |
e4cde2f7 | 628 | new_transfer(); |
fe8c2806 | 629 | |
8885c5fe JH |
630 | if (tftp_cur_block != 1) { /* Assertion */ |
631 | puts("\nTFTP error: "); | |
632 | printf("First block is not block 1 (%ld)\n", | |
633 | tftp_cur_block); | |
634 | puts("Starting again\n\n"); | |
bc0571fc | 635 | net_start_again(); |
fe8c2806 WD |
636 | break; |
637 | } | |
638 | } | |
639 | ||
8885c5fe JH |
640 | if (tftp_cur_block == tftp_prev_block) { |
641 | /* Same block again; ignore it. */ | |
fe8c2806 WD |
642 | break; |
643 | } | |
644 | ||
de5468e6 | 645 | update_block_number(); |
8885c5fe | 646 | tftp_prev_block = tftp_cur_block; |
f5fb7346 | 647 | timeout_count_max = tftp_timeout_count_max; |
bc0571fc | 648 | net_set_timeout_handler(timeout_ms, tftp_timeout_handler); |
fe8c2806 | 649 | |
ae0bdf09 | 650 | if (store_block(tftp_cur_block, pkt + 2, len)) { |
a156c47e SG |
651 | eth_halt(); |
652 | net_set_state(NETLOOP_FAIL); | |
653 | break; | |
654 | } | |
fe8c2806 | 655 | |
2dddc1bb RF |
656 | if (len < tftp_block_size) { |
657 | tftp_send(); | |
658 | tftp_complete(); | |
659 | break; | |
660 | } | |
661 | ||
fe8c2806 | 662 | /* |
4d69e98c | 663 | * Acknowledge the block just received, which will prompt |
20478cea | 664 | * the remote for the next one. |
fe8c2806 | 665 | */ |
cc6b87ec RF |
666 | if (tftp_cur_block == tftp_next_ack) { |
667 | tftp_send(); | |
668 | tftp_next_ack += tftp_windowsize; | |
669 | } | |
fe8c2806 WD |
670 | break; |
671 | ||
672 | case TFTP_ERROR: | |
c718b143 | 673 | printf("\nTFTP error: '%s' (%d)\n", |
61fdd4f7 | 674 | pkt + 2, ntohs(*(__be16 *)pkt)); |
aafda38f | 675 | |
61fdd4f7 | 676 | switch (ntohs(*(__be16 *)pkt)) { |
aafda38f RB |
677 | case TFTP_ERR_FILE_NOT_FOUND: |
678 | case TFTP_ERR_ACCESS_DENIED: | |
679 | puts("Not retrying...\n"); | |
680 | eth_halt(); | |
22f6e99d | 681 | net_set_state(NETLOOP_FAIL); |
aafda38f RB |
682 | break; |
683 | case TFTP_ERR_UNDEFINED: | |
684 | case TFTP_ERR_DISK_FULL: | |
685 | case TFTP_ERR_UNEXPECTED_OPCODE: | |
686 | case TFTP_ERR_UNKNOWN_TRANSFER_ID: | |
687 | case TFTP_ERR_FILE_ALREADY_EXISTS: | |
688 | default: | |
689 | puts("Starting again\n\n"); | |
bc0571fc | 690 | net_start_again(); |
aafda38f RB |
691 | break; |
692 | } | |
fe8c2806 WD |
693 | break; |
694 | } | |
695 | } | |
696 | ||
697 | ||
8885c5fe | 698 | static void tftp_timeout_handler(void) |
fe8c2806 | 699 | { |
8885c5fe | 700 | if (++timeout_count > timeout_count_max) { |
e4cde2f7 | 701 | restart("Retry count exceeded"); |
fe8c2806 | 702 | } else { |
c718b143 | 703 | puts("T "); |
bc0571fc | 704 | net_set_timeout_handler(timeout_ms, tftp_timeout_handler); |
8885c5fe JH |
705 | if (tftp_state != STATE_RECV_WRQ) |
706 | tftp_send(); | |
fe8c2806 WD |
707 | } |
708 | } | |
709 | ||
bb872dd9 | 710 | /* Initialize tftp_load_addr and tftp_load_size from image_load_addr and lmb */ |
a156c47e SG |
711 | static int tftp_init_load_addr(void) |
712 | { | |
713 | #ifdef CONFIG_LMB | |
714 | struct lmb lmb; | |
715 | phys_size_t max_size; | |
716 | ||
9cc2323f | 717 | lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob); |
a156c47e | 718 | |
bb872dd9 | 719 | max_size = lmb_get_free_size(&lmb, image_load_addr); |
a156c47e SG |
720 | if (!max_size) |
721 | return -1; | |
722 | ||
723 | tftp_load_size = max_size; | |
724 | #endif | |
bb872dd9 | 725 | tftp_load_addr = image_load_addr; |
a156c47e SG |
726 | return 0; |
727 | } | |
fe8c2806 | 728 | |
087648b5 RV |
729 | static int saved_tftp_block_size_option; |
730 | static void sanitize_tftp_block_size_option(enum proto_t protocol) | |
731 | { | |
732 | int cap, max_defrag; | |
733 | ||
734 | switch (protocol) { | |
735 | case TFTPGET: | |
736 | max_defrag = config_opt_enabled(CONFIG_IP_DEFRAG, CONFIG_NET_MAXDEFRAG, 0); | |
737 | if (max_defrag) { | |
738 | /* Account for IP, UDP and TFTP headers. */ | |
739 | cap = max_defrag - (20 + 8 + 4); | |
740 | /* RFC2348 sets a hard upper limit. */ | |
741 | cap = min(cap, 65464); | |
742 | break; | |
743 | } | |
744 | /* | |
745 | * If not CONFIG_IP_DEFRAG, cap at the same value as | |
746 | * for tftp put, namely normal MTU minus protocol | |
747 | * overhead. | |
748 | */ | |
749 | fallthrough; | |
750 | case TFTPPUT: | |
751 | default: | |
752 | /* | |
753 | * U-Boot does not support IP fragmentation on TX, so | |
754 | * this must be small enough that it fits normal MTU | |
755 | * (and small enough that it fits net_tx_packet which | |
756 | * has room for PKTSIZE_ALIGN bytes). | |
757 | */ | |
758 | cap = 1468; | |
759 | } | |
760 | if (tftp_block_size_option > cap) { | |
761 | printf("Capping tftp block size option to %d (was %d)\n", | |
762 | cap, tftp_block_size_option); | |
763 | saved_tftp_block_size_option = tftp_block_size_option; | |
764 | tftp_block_size_option = cap; | |
765 | } | |
766 | } | |
767 | ||
8885c5fe | 768 | void tftp_start(enum proto_t protocol) |
fe8c2806 | 769 | { |
4b8c44e3 | 770 | __maybe_unused char *ep; /* Environment pointer */ |
087648b5 RV |
771 | |
772 | if (saved_tftp_block_size_option) { | |
773 | tftp_block_size_option = saved_tftp_block_size_option; | |
774 | saved_tftp_block_size_option = 0; | |
775 | } | |
776 | ||
4b8c44e3 | 777 | if (IS_ENABLED(CONFIG_NET_TFTP_VARS)) { |
89ba81d1 | 778 | |
4b8c44e3 RV |
779 | /* |
780 | * Allow the user to choose TFTP blocksize and timeout. | |
781 | * TFTP protocol has a minimal timeout of 1 second. | |
782 | */ | |
f5fb7346 | 783 | |
4b8c44e3 RV |
784 | ep = env_get("tftpblocksize"); |
785 | if (ep != NULL) | |
786 | tftp_block_size_option = simple_strtol(ep, NULL, 10); | |
c96f86ee | 787 | |
4b8c44e3 RV |
788 | ep = env_get("tftpwindowsize"); |
789 | if (ep != NULL) | |
790 | tftp_window_size_option = simple_strtol(ep, NULL, 10); | |
cc6b87ec | 791 | |
4b8c44e3 RV |
792 | ep = env_get("tftptimeout"); |
793 | if (ep != NULL) | |
794 | timeout_ms = simple_strtol(ep, NULL, 10); | |
c96f86ee | 795 | |
4b8c44e3 RV |
796 | if (timeout_ms < 1000) { |
797 | printf("TFTP timeout (%ld ms) too low, set min = 1000 ms\n", | |
798 | timeout_ms); | |
799 | timeout_ms = 1000; | |
800 | } | |
c96f86ee | 801 | |
4b8c44e3 RV |
802 | ep = env_get("tftptimeoutcountmax"); |
803 | if (ep != NULL) | |
804 | tftp_timeout_count_max = simple_strtol(ep, NULL, 10); | |
f5fb7346 | 805 | |
4b8c44e3 RV |
806 | if (tftp_timeout_count_max < 0) { |
807 | printf("TFTP timeout count max (%d ms) negative, set to 0\n", | |
808 | tftp_timeout_count_max); | |
809 | tftp_timeout_count_max = 0; | |
810 | } | |
f5fb7346 | 811 | } |
f5fb7346 | 812 | |
087648b5 RV |
813 | sanitize_tftp_block_size_option(protocol); |
814 | ||
cc6b87ec RF |
815 | debug("TFTP blocksize = %i, TFTP windowsize = %d timeout = %ld ms\n", |
816 | tftp_block_size_option, tftp_window_size_option, timeout_ms); | |
ecb0ccd9 | 817 | |
7fbf230d VM |
818 | if (IS_ENABLED(CONFIG_IPV6)) |
819 | tftp_remote_ip6 = net_server_ip6; | |
820 | ||
049a95a7 | 821 | tftp_remote_ip = net_server_ip; |
6ab12830 | 822 | if (!net_parse_bootfile(&tftp_remote_ip, tftp_filename, MAX_LEN)) { |
ea45cb0a | 823 | sprintf(default_filename, "%02X%02X%02X%02X.img", |
049a95a7 JH |
824 | net_ip.s_addr & 0xFF, |
825 | (net_ip.s_addr >> 8) & 0xFF, | |
826 | (net_ip.s_addr >> 16) & 0xFF, | |
827 | (net_ip.s_addr >> 24) & 0xFF); | |
a93907c4 | 828 | |
0c17b1b7 VZ |
829 | strncpy(tftp_filename, default_filename, DEFAULT_NAME_LEN); |
830 | tftp_filename[DEFAULT_NAME_LEN - 1] = 0; | |
fe8c2806 | 831 | |
c718b143 | 832 | printf("*** Warning: no boot file name; using '%s'\n", |
8885c5fe | 833 | tftp_filename); |
fe8c2806 WD |
834 | } |
835 | ||
7fbf230d VM |
836 | if (IS_ENABLED(CONFIG_IPV6)) { |
837 | if (use_ip6) { | |
838 | char *s, *e; | |
839 | size_t len; | |
840 | ||
841 | s = strchr(net_boot_file_name, '['); | |
842 | e = strchr(net_boot_file_name, ']'); | |
843 | len = e - s; | |
844 | if (s && e) { | |
7db25d99 | 845 | string_to_ip6(s + 1, len - 1, &tftp_remote_ip6); |
7fbf230d VM |
846 | strlcpy(tftp_filename, e + 2, MAX_LEN); |
847 | } else { | |
848 | strlcpy(tftp_filename, net_boot_file_name, MAX_LEN); | |
849 | tftp_filename[MAX_LEN - 1] = 0; | |
850 | } | |
851 | } | |
852 | } | |
853 | ||
c718b143 | 854 | printf("Using %s device\n", eth_get_name()); |
7fbf230d VM |
855 | |
856 | if (IS_ENABLED(CONFIG_IPV6) && use_ip6) { | |
857 | printf("TFTP from server %pI6c; our IP address is %pI6c", | |
858 | &tftp_remote_ip6, &net_ip6); | |
859 | ||
860 | if (tftp_block_size_option > TFTP_MTU_BLOCKSIZE6) | |
861 | tftp_block_size_option = TFTP_MTU_BLOCKSIZE6; | |
862 | } else { | |
863 | printf("TFTP %s server %pI4; our IP address is %pI4", | |
8c6914f1 SG |
864 | #ifdef CONFIG_CMD_TFTPPUT |
865 | protocol == TFTPPUT ? "to" : "from", | |
866 | #else | |
8885c5fe | 867 | "from", |
8c6914f1 | 868 | #endif |
8885c5fe | 869 | &tftp_remote_ip, &net_ip); |
7fbf230d | 870 | } |
fe8c2806 WD |
871 | |
872 | /* Check if we need to send across this subnet */ | |
7fbf230d VM |
873 | if (IS_ENABLED(CONFIG_IPV6) && use_ip6) { |
874 | if (!ip6_addr_in_subnet(&net_ip6, &tftp_remote_ip6, | |
875 | net_prefix_length)) | |
876 | printf("; sending through gateway %pI6c", | |
877 | &net_gateway6); | |
878 | } else if (net_gateway.s_addr && net_netmask.s_addr) { | |
049a95a7 JH |
879 | struct in_addr our_net; |
880 | struct in_addr remote_net; | |
881 | ||
882 | our_net.s_addr = net_ip.s_addr & net_netmask.s_addr; | |
883 | remote_net.s_addr = tftp_remote_ip.s_addr & net_netmask.s_addr; | |
884 | if (our_net.s_addr != remote_net.s_addr) | |
885 | printf("; sending through gateway %pI4", &net_gateway); | |
fe8c2806 | 886 | } |
c718b143 | 887 | putc('\n'); |
fe8c2806 | 888 | |
c718b143 | 889 | printf("Filename '%s'.", tftp_filename); |
fe8c2806 | 890 | |
1411157d JH |
891 | if (net_boot_file_expected_size_in_blocks) { |
892 | printf(" Size is 0x%x Bytes = ", | |
893 | net_boot_file_expected_size_in_blocks << 9); | |
894 | print_size(net_boot_file_expected_size_in_blocks << 9, ""); | |
fe8c2806 WD |
895 | } |
896 | ||
c718b143 | 897 | putc('\n'); |
1fb7cd49 | 898 | #ifdef CONFIG_CMD_TFTPPUT |
8885c5fe JH |
899 | tftp_put_active = (protocol == TFTPPUT); |
900 | if (tftp_put_active) { | |
bb872dd9 SG |
901 | printf("Save address: 0x%lx\n", image_save_addr); |
902 | printf("Save size: 0x%lx\n", image_save_size); | |
903 | net_boot_file_size = image_save_size; | |
1fb7cd49 | 904 | puts("Saving: *\b"); |
8885c5fe | 905 | tftp_state = STATE_SEND_WRQ; |
1fb7cd49 SG |
906 | new_transfer(); |
907 | } else | |
908 | #endif | |
909 | { | |
a156c47e SG |
910 | if (tftp_init_load_addr()) { |
911 | eth_halt(); | |
912 | net_set_state(NETLOOP_FAIL); | |
913 | puts("\nTFTP error: "); | |
914 | puts("trying to overwrite reserved memory...\n"); | |
915 | return; | |
916 | } | |
917 | printf("Load address: 0x%lx\n", tftp_load_addr); | |
1fb7cd49 | 918 | puts("Loading: *\b"); |
8885c5fe | 919 | tftp_state = STATE_SEND_RRQ; |
1fb7cd49 | 920 | } |
fe8c2806 | 921 | |
85b19802 | 922 | time_start = get_timer(0); |
8885c5fe | 923 | timeout_count_max = tftp_timeout_count_max; |
e83cc063 | 924 | |
bc0571fc | 925 | net_set_timeout_handler(timeout_ms, tftp_timeout_handler); |
049a95a7 | 926 | net_set_udp_handler(tftp_handler); |
39bccd21 | 927 | #ifdef CONFIG_CMD_TFTPPUT |
1fb7cd49 | 928 | net_set_icmp_handler(icmp_handler); |
39bccd21 | 929 | #endif |
8885c5fe JH |
930 | tftp_remote_port = WELL_KNOWN_PORT; |
931 | timeout_count = 0; | |
ecb0ccd9 | 932 | /* Use a pseudo-random port unless a specific port is set */ |
8885c5fe | 933 | tftp_our_port = 1024 + (get_timer(0) % 3072); |
53a5c424 | 934 | |
ecb0ccd9 | 935 | #ifdef CONFIG_TFTP_PORT |
00caae6d | 936 | ep = env_get("tftpdstp"); |
7bc325a1 | 937 | if (ep != NULL) |
8885c5fe | 938 | tftp_remote_port = simple_strtol(ep, NULL, 10); |
00caae6d | 939 | ep = env_get("tftpsrcp"); |
7bc325a1 | 940 | if (ep != NULL) |
8885c5fe | 941 | tftp_our_port = simple_strtol(ep, NULL, 10); |
ecb0ccd9 | 942 | #endif |
8885c5fe | 943 | tftp_cur_block = 0; |
cc6b87ec RF |
944 | tftp_windowsize = 1; |
945 | tftp_last_nack = 0; | |
73a8b27c | 946 | /* zero out server ether in case the server ip has changed */ |
0adb5b76 | 947 | memset(net_server_ethaddr, 0, 6); |
8885c5fe JH |
948 | /* Revert tftp_block_size to dflt */ |
949 | tftp_block_size = TFTP_BLOCK_SIZE; | |
4fccb818 | 950 | #ifdef CONFIG_TFTP_TSIZE |
8885c5fe JH |
951 | tftp_tsize = 0; |
952 | tftp_tsize_num_hash = 0; | |
4fccb818 | 953 | #endif |
73a8b27c | 954 | |
8885c5fe | 955 | tftp_send(); |
fe8c2806 WD |
956 | } |
957 | ||
e59e3562 | 958 | #ifdef CONFIG_CMD_TFTPSRV |
8885c5fe | 959 | void tftp_start_server(void) |
e59e3562 LC |
960 | { |
961 | tftp_filename[0] = 0; | |
962 | ||
a156c47e SG |
963 | if (tftp_init_load_addr()) { |
964 | eth_halt(); | |
965 | net_set_state(NETLOOP_FAIL); | |
966 | puts("\nTFTP error: trying to overwrite reserved memory...\n"); | |
967 | return; | |
968 | } | |
e59e3562 | 969 | printf("Using %s device\n", eth_get_name()); |
049a95a7 | 970 | printf("Listening for TFTP transfer on %pI4\n", &net_ip); |
a156c47e | 971 | printf("Load address: 0x%lx\n", tftp_load_addr); |
e59e3562 LC |
972 | |
973 | puts("Loading: *\b"); | |
974 | ||
f5fb7346 | 975 | timeout_count_max = tftp_timeout_count_max; |
8885c5fe JH |
976 | timeout_count = 0; |
977 | timeout_ms = TIMEOUT; | |
bc0571fc | 978 | net_set_timeout_handler(timeout_ms, tftp_timeout_handler); |
e59e3562 | 979 | |
8885c5fe JH |
980 | /* Revert tftp_block_size to dflt */ |
981 | tftp_block_size = TFTP_BLOCK_SIZE; | |
982 | tftp_cur_block = 0; | |
983 | tftp_our_port = WELL_KNOWN_PORT; | |
1ffe3668 AMZ |
984 | tftp_windowsize = 1; |
985 | tftp_next_ack = tftp_windowsize; | |
e59e3562 LC |
986 | |
987 | #ifdef CONFIG_TFTP_TSIZE | |
8885c5fe JH |
988 | tftp_tsize = 0; |
989 | tftp_tsize_num_hash = 0; | |
e59e3562 LC |
990 | #endif |
991 | ||
8885c5fe | 992 | tftp_state = STATE_RECV_WRQ; |
049a95a7 | 993 | net_set_udp_handler(tftp_handler); |
8e52533d AR |
994 | |
995 | /* zero out server ether in case the server ip has changed */ | |
0adb5b76 | 996 | memset(net_server_ethaddr, 0, 6); |
e59e3562 LC |
997 | } |
998 | #endif /* CONFIG_CMD_TFTPSRV */ |