]> Git Repo - J-u-boot.git/blame - net/tftp.c
arm64: dts: rockchip: Add mdio and ethernet-phy nodes to rk3308-rock-pi-s
[J-u-boot.git] / net / tftp.c
CommitLineData
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
22DECLARE_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 41static ulong timeout_ms = TIMEOUT;
01d1b99c 42static int timeout_count_max = (CONFIG_NET_RETRY_COUNT * 2);
85b19802 43static ulong time_start; /* Record time we started tftp */
7fbf230d 44static 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 55ulong tftp_timeout_ms = TIMEOUT;
01d1b99c 56int tftp_timeout_count_max = (CONFIG_NET_RETRY_COUNT * 2);
e83cc063 57
aafda38f
RB
58enum {
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 69static struct in_addr tftp_remote_ip;
2f09413f 70/* The UDP port at their end */
8885c5fe 71static int tftp_remote_port;
2f09413f 72/* The UDP port at our end */
8885c5fe
JH
73static int tftp_our_port;
74static int timeout_count;
2f09413f 75/* packet sequence number */
8885c5fe 76static ulong tftp_cur_block;
2f09413f 77/* last packet sequence number received */
8885c5fe 78static ulong tftp_prev_block;
2f09413f 79/* count of sequence number wraparounds */
8885c5fe 80static ulong tftp_block_wrap;
2f09413f 81/* memory offset due to wrapping */
8885c5fe
JH
82static ulong tftp_block_wrap_offset;
83static int tftp_state;
a156c47e
SG
84static ulong tftp_load_addr;
85#ifdef CONFIG_LMB
86static ulong tftp_load_size;
87#endif
4fccb818 88#ifdef CONFIG_TFTP_TSIZE
2f09413f 89/* The file size reported by the server */
8885c5fe 90static int tftp_tsize;
2f09413f 91/* The number of hashes we printed */
8885c5fe 92static short tftp_tsize_num_hash;
4fccb818 93#endif
cc6b87ec
RF
94/* The window size negotiated */
95static ushort tftp_windowsize;
96/* Next block to send ack to */
97static ushort tftp_next_ack;
98/* Last nack block we send */
99static ushort tftp_last_nack;
1fb7cd49 100#ifdef CONFIG_CMD_TFTPPUT
8885c5fe
JH
101/* 1 if writing, else 0 */
102static int tftp_put_active;
103/* 1 if we have sent the last block */
104static 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)
125static 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
133static 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 151static unsigned short tftp_block_size = TFTP_BLOCK_SIZE;
31d275b0 152static unsigned short tftp_block_size_option = CONFIG_TFTP_BLOCKSIZE;
cc6b87ec 153static unsigned short tftp_window_size_option = TFTP_WINDOWSIZE;
53a5c424 154
a156c47e 155static 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 187static 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 */
206static 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
221static void tftp_send(void);
222static void tftp_timeout_handler(void);
fe8c2806
WD
223
224/**********************************************************************/
225
f5329bbc
SG
226static 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 */
258static 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 */
269static 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 */
286static 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 311static 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 443static 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
454static 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 495 update_block_number();
1dd034ec
MK
496 if (ack_ok) {
497 if (block == 0 &&
498 tftp_state == STATE_SEND_WRQ){
499 /* connection's first ACK */
500 tftp_state = STATE_DATA;
501 tftp_remote_port = src;
502 }
8885c5fe 503 tftp_send(); /* Send next data block */
1dd034ec 504 }
1fb7cd49
SG
505 }
506 }
507#endif
fe8c2806 508 break;
1fb7cd49 509
fe8c2806
WD
510 default:
511 break;
512
e59e3562
LC
513#ifdef CONFIG_CMD_TFTPSRV
514 case TFTP_WRQ:
515 debug("Got WRQ\n");
049a95a7 516 tftp_remote_ip = sip;
8885c5fe
JH
517 tftp_remote_port = src;
518 tftp_our_port = 1024 + (get_timer(0) % 3072);
e4cde2f7 519 new_transfer();
8885c5fe 520 tftp_send(); /* Send ACK(0) */
e59e3562
LC
521 break;
522#endif
523
fbe4b5cb 524 case TFTP_OACK:
08139210
RH
525 debug("Got OACK: ");
526 for (i = 0; i < len; i++) {
527 if (pkt[i] == '\0')
528 debug(" ");
529 else
530 debug("%c", pkt[i]);
531 }
532 debug("\n");
8885c5fe
JH
533 tftp_state = STATE_OACK;
534 tftp_remote_port = src;
60174746
WD
535 /*
536 * Check for 'blksize' option.
537 * Careful: "i" is signed, "len" is unsigned, thus
538 * something like "len-8" may give a *huge* number
539 */
c718b143 540 for (i = 0; i+8 < len; i++) {
08139210 541 if (strcasecmp((char *)pkt + i, "blksize") == 0) {
8885c5fe 542 tftp_block_size = (unsigned short)
0b1284eb 543 dectoul((char *)pkt + i + 8, NULL);
08139210 544 debug("Blocksize oack: %s, %d\n",
8885c5fe 545 (char *)pkt + i + 8, tftp_block_size);
08139210
RH
546 if (tftp_block_size > tftp_block_size_option) {
547 printf("Invalid blk size(=%d)\n",
548 tftp_block_size);
549 tftp_state = STATE_INVALID_OPTION;
550 }
551 }
552 if (strcasecmp((char *)pkt + i, "timeout") == 0) {
553 timeout_val_rcvd = (unsigned short)
0b1284eb 554 dectoul((char *)pkt + i + 8, NULL);
08139210
RH
555 debug("Timeout oack: %s, %d\n",
556 (char *)pkt + i + 8, timeout_val_rcvd);
557 if (timeout_val_rcvd != (timeout_ms / 1000)) {
558 printf("Invalid timeout val(=%d s)\n",
559 timeout_val_rcvd);
560 tftp_state = STATE_INVALID_OPTION;
561 }
ff13ac8c 562 }
4fccb818 563#ifdef CONFIG_TFTP_TSIZE
08139210 564 if (strcasecmp((char *)pkt + i, "tsize") == 0) {
0b1284eb
SG
565 tftp_tsize = dectoul((char *)pkt + i + 6,
566 NULL);
4fccb818 567 debug("size = %s, %d\n",
8885c5fe 568 (char *)pkt + i + 6, tftp_tsize);
4fccb818
RG
569 }
570#endif
cc6b87ec
RF
571 if (strcasecmp((char *)pkt + i, "windowsize") == 0) {
572 tftp_windowsize =
0b1284eb 573 dectoul((char *)pkt + i + 11, NULL);
cc6b87ec
RF
574 debug("windowsize = %s, %d\n",
575 (char *)pkt + i + 11, tftp_windowsize);
576 }
53a5c424 577 }
cc6b87ec
RF
578
579 tftp_next_ack = tftp_windowsize;
580
1fb7cd49 581#ifdef CONFIG_CMD_TFTPPUT
08139210 582 if (tftp_put_active && tftp_state == STATE_OACK) {
1fb7cd49 583 /* Get ready to send the first block */
8885c5fe
JH
584 tftp_state = STATE_DATA;
585 tftp_cur_block++;
1fb7cd49
SG
586 }
587#endif
8885c5fe 588 tftp_send(); /* Send ACK or first data block */
fbe4b5cb 589 break;
fe8c2806
WD
590 case TFTP_DATA:
591 if (len < 2)
592 return;
593 len -= 2;
cc6b87ec
RF
594
595 if (ntohs(*(__be16 *)pkt) != (ushort)(tftp_cur_block + 1)) {
596 debug("Received unexpected block: %d, expected: %d\n",
597 ntohs(*(__be16 *)pkt),
598 (ushort)(tftp_cur_block + 1));
21a265c0
SE
599 /*
600 * Only ACK if the block count received is greater than
601 * the expected block count, otherwise skip ACK.
602 * (required to properly handle the server retransmitting
603 * the window)
604 */
605 if ((ushort)(tftp_cur_block + 1) - (short)(ntohs(*(__be16 *)pkt)) > 0)
606 break;
cc6b87ec
RF
607 /*
608 * If one packet is dropped most likely
609 * all other buffers in the window
610 * that will arrive will cause a sending NACK.
611 * This just overwellms the server, let's just send one.
612 */
613 if (tftp_last_nack != tftp_cur_block) {
614 tftp_send();
615 tftp_last_nack = tftp_cur_block;
616 tftp_next_ack = (ushort)(tftp_cur_block +
617 tftp_windowsize);
618 }
619 break;
620 }
621
622 tftp_cur_block++;
623 tftp_cur_block %= TFTP_SEQUENCE_SIZE;
3f85ce27 624
beb61e13 625 if (tftp_state == STATE_SEND_RRQ) {
08139210 626 debug("Server did not acknowledge any options!\n");
beb61e13
HB
627 tftp_next_ack = tftp_windowsize;
628 }
fbe4b5cb 629
8885c5fe
JH
630 if (tftp_state == STATE_SEND_RRQ || tftp_state == STATE_OACK ||
631 tftp_state == STATE_RECV_WRQ) {
3f85ce27 632 /* first block received */
8885c5fe
JH
633 tftp_state = STATE_DATA;
634 tftp_remote_port = src;
e4cde2f7 635 new_transfer();
fe8c2806 636
8885c5fe
JH
637 if (tftp_cur_block != 1) { /* Assertion */
638 puts("\nTFTP error: ");
639 printf("First block is not block 1 (%ld)\n",
640 tftp_cur_block);
641 puts("Starting again\n\n");
bc0571fc 642 net_start_again();
fe8c2806
WD
643 break;
644 }
645 }
646
8885c5fe
JH
647 if (tftp_cur_block == tftp_prev_block) {
648 /* Same block again; ignore it. */
fe8c2806
WD
649 break;
650 }
651
de5468e6 652 update_block_number();
8885c5fe 653 tftp_prev_block = tftp_cur_block;
f5fb7346 654 timeout_count_max = tftp_timeout_count_max;
bc0571fc 655 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
fe8c2806 656
ae0bdf09 657 if (store_block(tftp_cur_block, pkt + 2, len)) {
a156c47e
SG
658 eth_halt();
659 net_set_state(NETLOOP_FAIL);
660 break;
661 }
fe8c2806 662
2dddc1bb
RF
663 if (len < tftp_block_size) {
664 tftp_send();
665 tftp_complete();
666 break;
667 }
668
fe8c2806 669 /*
4d69e98c 670 * Acknowledge the block just received, which will prompt
20478cea 671 * the remote for the next one.
fe8c2806 672 */
cc6b87ec
RF
673 if (tftp_cur_block == tftp_next_ack) {
674 tftp_send();
675 tftp_next_ack += tftp_windowsize;
676 }
fe8c2806
WD
677 break;
678
679 case TFTP_ERROR:
c718b143 680 printf("\nTFTP error: '%s' (%d)\n",
61fdd4f7 681 pkt + 2, ntohs(*(__be16 *)pkt));
aafda38f 682
61fdd4f7 683 switch (ntohs(*(__be16 *)pkt)) {
aafda38f
RB
684 case TFTP_ERR_FILE_NOT_FOUND:
685 case TFTP_ERR_ACCESS_DENIED:
686 puts("Not retrying...\n");
687 eth_halt();
22f6e99d 688 net_set_state(NETLOOP_FAIL);
aafda38f
RB
689 break;
690 case TFTP_ERR_UNDEFINED:
691 case TFTP_ERR_DISK_FULL:
692 case TFTP_ERR_UNEXPECTED_OPCODE:
693 case TFTP_ERR_UNKNOWN_TRANSFER_ID:
694 case TFTP_ERR_FILE_ALREADY_EXISTS:
695 default:
696 puts("Starting again\n\n");
bc0571fc 697 net_start_again();
aafda38f
RB
698 break;
699 }
fe8c2806
WD
700 break;
701 }
702}
703
8885c5fe 704static void tftp_timeout_handler(void)
fe8c2806 705{
8885c5fe 706 if (++timeout_count > timeout_count_max) {
e4cde2f7 707 restart("Retry count exceeded");
fe8c2806 708 } else {
c718b143 709 puts("T ");
bc0571fc 710 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
8885c5fe
JH
711 if (tftp_state != STATE_RECV_WRQ)
712 tftp_send();
fe8c2806
WD
713 }
714}
715
bb872dd9 716/* Initialize tftp_load_addr and tftp_load_size from image_load_addr and lmb */
a156c47e
SG
717static int tftp_init_load_addr(void)
718{
719#ifdef CONFIG_LMB
720 struct lmb lmb;
721 phys_size_t max_size;
722
9cc2323f 723 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
a156c47e 724
bb872dd9 725 max_size = lmb_get_free_size(&lmb, image_load_addr);
a156c47e
SG
726 if (!max_size)
727 return -1;
728
729 tftp_load_size = max_size;
730#endif
bb872dd9 731 tftp_load_addr = image_load_addr;
a156c47e
SG
732 return 0;
733}
fe8c2806 734
087648b5
RV
735static int saved_tftp_block_size_option;
736static void sanitize_tftp_block_size_option(enum proto_t protocol)
737{
738 int cap, max_defrag;
739
740 switch (protocol) {
741 case TFTPGET:
742 max_defrag = config_opt_enabled(CONFIG_IP_DEFRAG, CONFIG_NET_MAXDEFRAG, 0);
743 if (max_defrag) {
744 /* Account for IP, UDP and TFTP headers. */
745 cap = max_defrag - (20 + 8 + 4);
746 /* RFC2348 sets a hard upper limit. */
747 cap = min(cap, 65464);
748 break;
749 }
750 /*
751 * If not CONFIG_IP_DEFRAG, cap at the same value as
752 * for tftp put, namely normal MTU minus protocol
753 * overhead.
754 */
755 fallthrough;
756 case TFTPPUT:
757 default:
758 /*
759 * U-Boot does not support IP fragmentation on TX, so
760 * this must be small enough that it fits normal MTU
761 * (and small enough that it fits net_tx_packet which
762 * has room for PKTSIZE_ALIGN bytes).
763 */
764 cap = 1468;
765 }
766 if (tftp_block_size_option > cap) {
767 printf("Capping tftp block size option to %d (was %d)\n",
768 cap, tftp_block_size_option);
769 saved_tftp_block_size_option = tftp_block_size_option;
770 tftp_block_size_option = cap;
771 }
772}
773
8885c5fe 774void tftp_start(enum proto_t protocol)
fe8c2806 775{
4b8c44e3 776 __maybe_unused char *ep; /* Environment pointer */
087648b5
RV
777
778 if (saved_tftp_block_size_option) {
779 tftp_block_size_option = saved_tftp_block_size_option;
780 saved_tftp_block_size_option = 0;
781 }
782
4b8c44e3 783 if (IS_ENABLED(CONFIG_NET_TFTP_VARS)) {
89ba81d1 784
4b8c44e3
RV
785 /*
786 * Allow the user to choose TFTP blocksize and timeout.
787 * TFTP protocol has a minimal timeout of 1 second.
788 */
f5fb7346 789
4b8c44e3
RV
790 ep = env_get("tftpblocksize");
791 if (ep != NULL)
792 tftp_block_size_option = simple_strtol(ep, NULL, 10);
c96f86ee 793
4b8c44e3
RV
794 ep = env_get("tftpwindowsize");
795 if (ep != NULL)
796 tftp_window_size_option = simple_strtol(ep, NULL, 10);
cc6b87ec 797
4b8c44e3
RV
798 ep = env_get("tftptimeout");
799 if (ep != NULL)
800 timeout_ms = simple_strtol(ep, NULL, 10);
c96f86ee 801
4b8c44e3
RV
802 if (timeout_ms < 1000) {
803 printf("TFTP timeout (%ld ms) too low, set min = 1000 ms\n",
804 timeout_ms);
805 timeout_ms = 1000;
806 }
c96f86ee 807
4b8c44e3
RV
808 ep = env_get("tftptimeoutcountmax");
809 if (ep != NULL)
810 tftp_timeout_count_max = simple_strtol(ep, NULL, 10);
f5fb7346 811
4b8c44e3
RV
812 if (tftp_timeout_count_max < 0) {
813 printf("TFTP timeout count max (%d ms) negative, set to 0\n",
814 tftp_timeout_count_max);
815 tftp_timeout_count_max = 0;
816 }
f5fb7346 817 }
f5fb7346 818
087648b5
RV
819 sanitize_tftp_block_size_option(protocol);
820
cc6b87ec
RF
821 debug("TFTP blocksize = %i, TFTP windowsize = %d timeout = %ld ms\n",
822 tftp_block_size_option, tftp_window_size_option, timeout_ms);
ecb0ccd9 823
7fbf230d
VM
824 if (IS_ENABLED(CONFIG_IPV6))
825 tftp_remote_ip6 = net_server_ip6;
826
049a95a7 827 tftp_remote_ip = net_server_ip;
6ab12830 828 if (!net_parse_bootfile(&tftp_remote_ip, tftp_filename, MAX_LEN)) {
ea45cb0a 829 sprintf(default_filename, "%02X%02X%02X%02X.img",
049a95a7
JH
830 net_ip.s_addr & 0xFF,
831 (net_ip.s_addr >> 8) & 0xFF,
832 (net_ip.s_addr >> 16) & 0xFF,
833 (net_ip.s_addr >> 24) & 0xFF);
a93907c4 834
0c17b1b7
VZ
835 strncpy(tftp_filename, default_filename, DEFAULT_NAME_LEN);
836 tftp_filename[DEFAULT_NAME_LEN - 1] = 0;
fe8c2806 837
c718b143 838 printf("*** Warning: no boot file name; using '%s'\n",
8885c5fe 839 tftp_filename);
fe8c2806
WD
840 }
841
7fbf230d
VM
842 if (IS_ENABLED(CONFIG_IPV6)) {
843 if (use_ip6) {
844 char *s, *e;
845 size_t len;
846
847 s = strchr(net_boot_file_name, '[');
848 e = strchr(net_boot_file_name, ']');
849 len = e - s;
850 if (s && e) {
7db25d99 851 string_to_ip6(s + 1, len - 1, &tftp_remote_ip6);
7fbf230d
VM
852 strlcpy(tftp_filename, e + 2, MAX_LEN);
853 } else {
854 strlcpy(tftp_filename, net_boot_file_name, MAX_LEN);
855 tftp_filename[MAX_LEN - 1] = 0;
856 }
857 }
858 }
859
c718b143 860 printf("Using %s device\n", eth_get_name());
7fbf230d
VM
861
862 if (IS_ENABLED(CONFIG_IPV6) && use_ip6) {
863 printf("TFTP from server %pI6c; our IP address is %pI6c",
864 &tftp_remote_ip6, &net_ip6);
865
866 if (tftp_block_size_option > TFTP_MTU_BLOCKSIZE6)
867 tftp_block_size_option = TFTP_MTU_BLOCKSIZE6;
868 } else {
869 printf("TFTP %s server %pI4; our IP address is %pI4",
8c6914f1
SG
870#ifdef CONFIG_CMD_TFTPPUT
871 protocol == TFTPPUT ? "to" : "from",
872#else
8885c5fe 873 "from",
8c6914f1 874#endif
8885c5fe 875 &tftp_remote_ip, &net_ip);
7fbf230d 876 }
fe8c2806
WD
877
878 /* Check if we need to send across this subnet */
7fbf230d
VM
879 if (IS_ENABLED(CONFIG_IPV6) && use_ip6) {
880 if (!ip6_addr_in_subnet(&net_ip6, &tftp_remote_ip6,
881 net_prefix_length))
882 printf("; sending through gateway %pI6c",
883 &net_gateway6);
884 } else if (net_gateway.s_addr && net_netmask.s_addr) {
049a95a7
JH
885 struct in_addr our_net;
886 struct in_addr remote_net;
887
888 our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
889 remote_net.s_addr = tftp_remote_ip.s_addr & net_netmask.s_addr;
890 if (our_net.s_addr != remote_net.s_addr)
891 printf("; sending through gateway %pI4", &net_gateway);
fe8c2806 892 }
c718b143 893 putc('\n');
fe8c2806 894
c718b143 895 printf("Filename '%s'.", tftp_filename);
fe8c2806 896
1411157d
JH
897 if (net_boot_file_expected_size_in_blocks) {
898 printf(" Size is 0x%x Bytes = ",
899 net_boot_file_expected_size_in_blocks << 9);
900 print_size(net_boot_file_expected_size_in_blocks << 9, "");
fe8c2806
WD
901 }
902
c718b143 903 putc('\n');
1fb7cd49 904#ifdef CONFIG_CMD_TFTPPUT
8885c5fe
JH
905 tftp_put_active = (protocol == TFTPPUT);
906 if (tftp_put_active) {
bb872dd9
SG
907 printf("Save address: 0x%lx\n", image_save_addr);
908 printf("Save size: 0x%lx\n", image_save_size);
909 net_boot_file_size = image_save_size;
1fb7cd49 910 puts("Saving: *\b");
8885c5fe 911 tftp_state = STATE_SEND_WRQ;
1fb7cd49
SG
912 new_transfer();
913 } else
914#endif
915 {
a156c47e
SG
916 if (tftp_init_load_addr()) {
917 eth_halt();
918 net_set_state(NETLOOP_FAIL);
919 puts("\nTFTP error: ");
920 puts("trying to overwrite reserved memory...\n");
921 return;
922 }
923 printf("Load address: 0x%lx\n", tftp_load_addr);
1fb7cd49 924 puts("Loading: *\b");
8885c5fe 925 tftp_state = STATE_SEND_RRQ;
1fb7cd49 926 }
fe8c2806 927
85b19802 928 time_start = get_timer(0);
8885c5fe 929 timeout_count_max = tftp_timeout_count_max;
e83cc063 930
bc0571fc 931 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
049a95a7 932 net_set_udp_handler(tftp_handler);
39bccd21 933#ifdef CONFIG_CMD_TFTPPUT
1fb7cd49 934 net_set_icmp_handler(icmp_handler);
39bccd21 935#endif
8885c5fe
JH
936 tftp_remote_port = WELL_KNOWN_PORT;
937 timeout_count = 0;
ecb0ccd9 938 /* Use a pseudo-random port unless a specific port is set */
8885c5fe 939 tftp_our_port = 1024 + (get_timer(0) % 3072);
53a5c424 940
ecb0ccd9 941#ifdef CONFIG_TFTP_PORT
00caae6d 942 ep = env_get("tftpdstp");
7bc325a1 943 if (ep != NULL)
8885c5fe 944 tftp_remote_port = simple_strtol(ep, NULL, 10);
00caae6d 945 ep = env_get("tftpsrcp");
7bc325a1 946 if (ep != NULL)
8885c5fe 947 tftp_our_port = simple_strtol(ep, NULL, 10);
ecb0ccd9 948#endif
8885c5fe 949 tftp_cur_block = 0;
cc6b87ec
RF
950 tftp_windowsize = 1;
951 tftp_last_nack = 0;
73a8b27c 952 /* zero out server ether in case the server ip has changed */
0adb5b76 953 memset(net_server_ethaddr, 0, 6);
8885c5fe
JH
954 /* Revert tftp_block_size to dflt */
955 tftp_block_size = TFTP_BLOCK_SIZE;
4fccb818 956#ifdef CONFIG_TFTP_TSIZE
8885c5fe
JH
957 tftp_tsize = 0;
958 tftp_tsize_num_hash = 0;
4fccb818 959#endif
73a8b27c 960
8885c5fe 961 tftp_send();
fe8c2806
WD
962}
963
e59e3562 964#ifdef CONFIG_CMD_TFTPSRV
8885c5fe 965void tftp_start_server(void)
e59e3562
LC
966{
967 tftp_filename[0] = 0;
968
a156c47e
SG
969 if (tftp_init_load_addr()) {
970 eth_halt();
971 net_set_state(NETLOOP_FAIL);
972 puts("\nTFTP error: trying to overwrite reserved memory...\n");
973 return;
974 }
e59e3562 975 printf("Using %s device\n", eth_get_name());
049a95a7 976 printf("Listening for TFTP transfer on %pI4\n", &net_ip);
a156c47e 977 printf("Load address: 0x%lx\n", tftp_load_addr);
e59e3562
LC
978
979 puts("Loading: *\b");
980
f5fb7346 981 timeout_count_max = tftp_timeout_count_max;
8885c5fe
JH
982 timeout_count = 0;
983 timeout_ms = TIMEOUT;
bc0571fc 984 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
e59e3562 985
8885c5fe
JH
986 /* Revert tftp_block_size to dflt */
987 tftp_block_size = TFTP_BLOCK_SIZE;
988 tftp_cur_block = 0;
989 tftp_our_port = WELL_KNOWN_PORT;
1ffe3668
AMZ
990 tftp_windowsize = 1;
991 tftp_next_ack = tftp_windowsize;
e59e3562
LC
992
993#ifdef CONFIG_TFTP_TSIZE
8885c5fe
JH
994 tftp_tsize = 0;
995 tftp_tsize_num_hash = 0;
e59e3562
LC
996#endif
997
8885c5fe 998 tftp_state = STATE_RECV_WRQ;
049a95a7 999 net_set_udp_handler(tftp_handler);
8e52533d
AR
1000
1001 /* zero out server ether in case the server ip has changed */
0adb5b76 1002 memset(net_server_ethaddr, 0, 6);
e59e3562
LC
1003}
1004#endif /* CONFIG_CMD_TFTPSRV */
This page took 0.788208 seconds and 4 git commands to generate.