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