blob: 451d73529f2af9285e3c9ae9f9f26d2ab7dbb90f [file] [log] [blame]
wdenkfe8c2802002-11-03 00:38:21 +00001/*
Luca Ceresoli2f094132011-05-14 05:49:56 +00002 * Copyright 1994, 1995, 2000 Neil Russell.
3 * (See License)
4 * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
Luca Ceresolie59e3562011-05-17 00:03:39 +00005 * Copyright 2011 Comelit Group SpA,
6 * Luca Ceresoli <luca.ceresoli@comelit.it>
wdenkfe8c2802002-11-03 00:38:21 +00007 */
wdenkfe8c2802002-11-03 00:38:21 +00008#include <common.h>
9#include <command.h>
Alexander Graf0efe1bc2016-05-06 21:01:01 +020010#include <efi_loader.h>
Simon Glass7b51b572019-08-01 09:46:52 -060011#include <env.h>
Simon Glass8e8ccfe2019-12-28 10:45:03 -070012#include <image.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060013#include <lmb.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Joe Hershberger55d5fd92015-03-22 17:09:08 -050015#include <mapmem.h>
wdenkfe8c2802002-11-03 00:38:21 +000016#include <net.h>
Simon Glass401d1c42020-10-30 21:38:53 -060017#include <asm/global_data.h>
Lukasz Majewski34696952015-08-24 00:21:43 +020018#include <net/tftp.h>
wdenkfe8c2802002-11-03 00:38:21 +000019#include "bootp.h"
20
Simon Goldschmidta156c472019-01-14 22:38:22 +010021DECLARE_GLOBAL_DATA_PTR;
22
Luca Ceresoli2f094132011-05-14 05:49:56 +000023/* Well known TFTP port # */
24#define WELL_KNOWN_PORT 69
25/* Millisecs to timeout for lost pkt */
Bin Mengaf2ca592015-08-27 22:25:51 -070026#define TIMEOUT 5000UL
Luca Ceresoli2f094132011-05-14 05:49:56 +000027/* Number of "loading" hashes per line (for checking the image size) */
28#define HASHES_PER_LINE 65
wdenkfe8c2802002-11-03 00:38:21 +000029
30/*
31 * TFTP operations.
32 */
33#define TFTP_RRQ 1
34#define TFTP_WRQ 2
35#define TFTP_DATA 3
36#define TFTP_ACK 4
37#define TFTP_ERROR 5
wdenkfbe4b5c2003-10-06 21:55:32 +000038#define TFTP_OACK 6
wdenkfe8c2802002-11-03 00:38:21 +000039
Joe Hershberger8885c5f2015-04-08 01:41:07 -050040static ulong timeout_ms = TIMEOUT;
Tom Rini01d1b992022-03-11 09:12:02 -050041static int timeout_count_max = (CONFIG_NET_RETRY_COUNT * 2);
Simon Glass85b19802012-10-11 13:57:36 +000042static ulong time_start; /* Record time we started tftp */
Bartlomiej Siekae83cc062008-10-01 15:26:29 +020043
44/*
45 * These globals govern the timeout behavior when attempting a connection to a
Joe Hershberger8885c5f2015-04-08 01:41:07 -050046 * TFTP server. tftp_timeout_ms specifies the number of milliseconds to
Bartlomiej Siekae83cc062008-10-01 15:26:29 +020047 * wait for the server to respond to initial connection. Second global,
Joe Hershberger8885c5f2015-04-08 01:41:07 -050048 * tftp_timeout_count_max, gives the number of such connection retries.
49 * tftp_timeout_count_max must be non-negative and tftp_timeout_ms must be
Bartlomiej Siekae83cc062008-10-01 15:26:29 +020050 * positive. The globals are meant to be set (and restored) by code needing
51 * non-standard timeout behavior when initiating a TFTP transfer.
52 */
Joe Hershberger8885c5f2015-04-08 01:41:07 -050053ulong tftp_timeout_ms = TIMEOUT;
Tom Rini01d1b992022-03-11 09:12:02 -050054int tftp_timeout_count_max = (CONFIG_NET_RETRY_COUNT * 2);
Bartlomiej Siekae83cc062008-10-01 15:26:29 +020055
Remy Bohmeraafda382009-10-28 22:13:40 +010056enum {
57 TFTP_ERR_UNDEFINED = 0,
58 TFTP_ERR_FILE_NOT_FOUND = 1,
59 TFTP_ERR_ACCESS_DENIED = 2,
60 TFTP_ERR_DISK_FULL = 3,
61 TFTP_ERR_UNEXPECTED_OPCODE = 4,
62 TFTP_ERR_UNKNOWN_TRANSFER_ID = 5,
63 TFTP_ERR_FILE_ALREADY_EXISTS = 6,
Ravik Hasija08139212020-05-18 21:35:43 -070064 TFTP_ERR_OPTION_NEGOTIATION = 8,
Remy Bohmeraafda382009-10-28 22:13:40 +010065};
66
Joe Hershberger049a95a2015-04-08 01:41:01 -050067static struct in_addr tftp_remote_ip;
Luca Ceresoli2f094132011-05-14 05:49:56 +000068/* The UDP port at their end */
Joe Hershberger8885c5f2015-04-08 01:41:07 -050069static int tftp_remote_port;
Luca Ceresoli2f094132011-05-14 05:49:56 +000070/* The UDP port at our end */
Joe Hershberger8885c5f2015-04-08 01:41:07 -050071static int tftp_our_port;
72static int timeout_count;
Luca Ceresoli2f094132011-05-14 05:49:56 +000073/* packet sequence number */
Joe Hershberger8885c5f2015-04-08 01:41:07 -050074static ulong tftp_cur_block;
Luca Ceresoli2f094132011-05-14 05:49:56 +000075/* last packet sequence number received */
Joe Hershberger8885c5f2015-04-08 01:41:07 -050076static ulong tftp_prev_block;
Luca Ceresoli2f094132011-05-14 05:49:56 +000077/* count of sequence number wraparounds */
Joe Hershberger8885c5f2015-04-08 01:41:07 -050078static ulong tftp_block_wrap;
Luca Ceresoli2f094132011-05-14 05:49:56 +000079/* memory offset due to wrapping */
Joe Hershberger8885c5f2015-04-08 01:41:07 -050080static ulong tftp_block_wrap_offset;
81static int tftp_state;
Simon Goldschmidta156c472019-01-14 22:38:22 +010082static ulong tftp_load_addr;
83#ifdef CONFIG_LMB
84static ulong tftp_load_size;
85#endif
Robin Getz4fccb812009-08-20 10:50:20 -040086#ifdef CONFIG_TFTP_TSIZE
Luca Ceresoli2f094132011-05-14 05:49:56 +000087/* The file size reported by the server */
Joe Hershberger8885c5f2015-04-08 01:41:07 -050088static int tftp_tsize;
Luca Ceresoli2f094132011-05-14 05:49:56 +000089/* The number of hashes we printed */
Joe Hershberger8885c5f2015-04-08 01:41:07 -050090static short tftp_tsize_num_hash;
Robin Getz4fccb812009-08-20 10:50:20 -040091#endif
Ramon Friedcc6b87e2020-07-18 23:31:46 +030092/* The window size negotiated */
93static ushort tftp_windowsize;
94/* Next block to send ack to */
95static ushort tftp_next_ack;
96/* Last nack block we send */
97static ushort tftp_last_nack;
Simon Glass1fb7cd42011-10-24 18:00:07 +000098#ifdef CONFIG_CMD_TFTPPUT
Joe Hershberger8885c5f2015-04-08 01:41:07 -050099/* 1 if writing, else 0 */
100static int tftp_put_active;
101/* 1 if we have sent the last block */
102static int tftp_put_final_block_sent;
Simon Glass1fb7cd42011-10-24 18:00:07 +0000103#else
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500104#define tftp_put_active 0
Simon Glass1fb7cd42011-10-24 18:00:07 +0000105#endif
wdenk3f85ce22004-02-23 16:11:30 +0000106
Luca Ceresolie3fb0ab2011-05-17 00:03:38 +0000107#define STATE_SEND_RRQ 1
wdenkfe8c2802002-11-03 00:38:21 +0000108#define STATE_DATA 2
109#define STATE_TOO_LARGE 3
110#define STATE_BAD_MAGIC 4
wdenkfbe4b5c2003-10-06 21:55:32 +0000111#define STATE_OACK 5
Luca Ceresolie59e3562011-05-17 00:03:39 +0000112#define STATE_RECV_WRQ 6
Simon Glass1fb7cd42011-10-24 18:00:07 +0000113#define STATE_SEND_WRQ 7
Ravik Hasija08139212020-05-18 21:35:43 -0700114#define STATE_INVALID_OPTION 8
wdenkfe8c2802002-11-03 00:38:21 +0000115
Luca Ceresoli2f094132011-05-14 05:49:56 +0000116/* default TFTP block size */
117#define TFTP_BLOCK_SIZE 512
118/* sequence number is 16 bit */
119#define TFTP_SEQUENCE_SIZE ((ulong)(1<<16))
wdenk3f85ce22004-02-23 16:11:30 +0000120
wdenkfe8c2802002-11-03 00:38:21 +0000121#define DEFAULT_NAME_LEN (8 + 4 + 1)
122static char default_filename[DEFAULT_NAME_LEN];
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100123
124#ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
125#define MAX_LEN 128
126#else
127#define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
128#endif
129
130static char tftp_filename[MAX_LEN];
wdenkfe8c2802002-11-03 00:38:21 +0000131
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200132/* 512 is poor choice for ethernet, MTU is typically 1500.
133 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
David Updegraff53a5c422007-06-11 10:41:07 -0500134 * almost-MTU block sizes. At least try... fall back to 512 if need be.
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200135 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
David Updegraff53a5c422007-06-11 10:41:07 -0500136 */
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200137
Ramon Friedcc6b87e2020-07-18 23:31:46 +0300138/* When windowsize is defined to 1,
139 * tftp behaves the same way as it was
140 * never declared
141 */
142#ifdef CONFIG_TFTP_WINDOWSIZE
143#define TFTP_WINDOWSIZE CONFIG_TFTP_WINDOWSIZE
144#else
145#define TFTP_WINDOWSIZE 1
146#endif
147
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500148static unsigned short tftp_block_size = TFTP_BLOCK_SIZE;
Patrick Delaunay31d275b2020-04-22 14:18:26 +0200149static unsigned short tftp_block_size_option = CONFIG_TFTP_BLOCKSIZE;
Ramon Friedcc6b87e2020-07-18 23:31:46 +0300150static unsigned short tftp_window_size_option = TFTP_WINDOWSIZE;
David Updegraff53a5c422007-06-11 10:41:07 -0500151
Simon Goldschmidta156c472019-01-14 22:38:22 +0100152static inline int store_block(int block, uchar *src, unsigned int len)
wdenkfe8c2802002-11-03 00:38:21 +0000153{
Ley Foon Tanae0bdf02020-08-25 10:26:36 +0800154 ulong offset = block * tftp_block_size + tftp_block_wrap_offset -
155 tftp_block_size;
wdenk3f85ce22004-02-23 16:11:30 +0000156 ulong newsize = offset + len;
Simon Goldschmidta156c472019-01-14 22:38:22 +0100157 ulong store_addr = tftp_load_addr + offset;
Tom Rini52938fc2022-07-23 13:04:54 -0400158 void *ptr;
Joe Hershberger55d5fd92015-03-22 17:09:08 -0500159
Simon Goldschmidta156c472019-01-14 22:38:22 +0100160#ifdef CONFIG_LMB
Tom Rini52938fc2022-07-23 13:04:54 -0400161 ulong end_addr = tftp_load_addr + tftp_load_size;
Bin Mengca48cb42019-11-15 22:20:13 -0800162
Tom Rini52938fc2022-07-23 13:04:54 -0400163 if (!end_addr)
164 end_addr = ULONG_MAX;
Bin Mengca48cb42019-11-15 22:20:13 -0800165
Tom Rini52938fc2022-07-23 13:04:54 -0400166 if (store_addr < tftp_load_addr ||
167 store_addr + len > end_addr) {
168 puts("\nTFTP error: ");
169 puts("trying to overwrite reserved memory...\n");
170 return -1;
wdenkfe8c2802002-11-03 00:38:21 +0000171 }
Tom Rini52938fc2022-07-23 13:04:54 -0400172#endif
173 ptr = map_sysmem(store_addr, len);
174 memcpy(ptr, src, len);
175 unmap_sysmem(ptr);
wdenkfe8c2802002-11-03 00:38:21 +0000176
Joe Hershberger14111572015-04-08 01:41:02 -0500177 if (net_boot_file_size < newsize)
178 net_boot_file_size = newsize;
Simon Goldschmidta156c472019-01-14 22:38:22 +0100179
180 return 0;
wdenkfe8c2802002-11-03 00:38:21 +0000181}
182
Simon Glasse4cde2f2011-10-24 18:00:04 +0000183/* Clear our state ready for a new transfer */
Simon Glass165099e2011-10-27 06:24:28 +0000184static void new_transfer(void)
Simon Glasse4cde2f2011-10-24 18:00:04 +0000185{
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500186 tftp_prev_block = 0;
187 tftp_block_wrap = 0;
188 tftp_block_wrap_offset = 0;
Simon Glasse4cde2f2011-10-24 18:00:04 +0000189#ifdef CONFIG_CMD_TFTPPUT
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500190 tftp_put_final_block_sent = 0;
Simon Glasse4cde2f2011-10-24 18:00:04 +0000191#endif
192}
193
Simon Glass1fb7cd42011-10-24 18:00:07 +0000194#ifdef CONFIG_CMD_TFTPPUT
195/**
196 * Load the next block from memory to be sent over tftp.
197 *
198 * @param block Block number to send
199 * @param dst Destination buffer for data
200 * @param len Number of bytes in block (this one and every other)
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100201 * Return: number of bytes loaded
Simon Glass1fb7cd42011-10-24 18:00:07 +0000202 */
203static int load_block(unsigned block, uchar *dst, unsigned len)
204{
205 /* We may want to get the final block from the previous set */
Ley Foon Tanf6a158b2020-08-25 10:26:37 +0800206 ulong offset = block * tftp_block_size + tftp_block_wrap_offset -
207 tftp_block_size;
Simon Glass1fb7cd42011-10-24 18:00:07 +0000208 ulong tosend = len;
209
Joe Hershberger14111572015-04-08 01:41:02 -0500210 tosend = min(net_boot_file_size - offset, tosend);
Simon Glassbb872dd2019-12-28 10:45:02 -0700211 (void)memcpy(dst, (void *)(image_save_addr + offset), tosend);
Heinrich Schuchardt2bcc43b2020-02-22 08:43:40 +0100212 debug("%s: block=%u, offset=%lu, len=%u, tosend=%lu\n", __func__,
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500213 block, offset, len, tosend);
Simon Glass1fb7cd42011-10-24 18:00:07 +0000214 return tosend;
215}
216#endif
217
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500218static void tftp_send(void);
219static void tftp_timeout_handler(void);
wdenkfe8c2802002-11-03 00:38:21 +0000220
221/**********************************************************************/
222
Simon Glassf5329bb2011-10-24 18:00:03 +0000223static void show_block_marker(void)
224{
Ravik Hasijade5468e2020-05-07 14:55:32 -0700225 ulong pos;
226
Simon Glassf5329bb2011-10-24 18:00:03 +0000227#ifdef CONFIG_TFTP_TSIZE
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500228 if (tftp_tsize) {
Ravik Hasijade5468e2020-05-07 14:55:32 -0700229 pos = tftp_cur_block * tftp_block_size +
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500230 tftp_block_wrap_offset;
Max Krummenacher7628afe2015-08-05 17:17:05 +0200231 if (pos > tftp_tsize)
232 pos = tftp_tsize;
Simon Glassf5329bb2011-10-24 18:00:03 +0000233
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500234 while (tftp_tsize_num_hash < pos * 50 / tftp_tsize) {
Simon Glassf5329bb2011-10-24 18:00:03 +0000235 putc('#');
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500236 tftp_tsize_num_hash++;
Simon Glassf5329bb2011-10-24 18:00:03 +0000237 }
Simon Glass1fb7cd42011-10-24 18:00:07 +0000238 } else
Simon Glassf5329bb2011-10-24 18:00:03 +0000239#endif
Simon Glass1fb7cd42011-10-24 18:00:07 +0000240 {
Ravik Hasijade5468e2020-05-07 14:55:32 -0700241 pos = (tftp_cur_block - 1) +
242 (tftp_block_wrap * TFTP_SEQUENCE_SIZE);
243 if ((pos % 10) == 0)
Simon Glassf5329bb2011-10-24 18:00:03 +0000244 putc('#');
Ravik Hasijade5468e2020-05-07 14:55:32 -0700245 else if (((pos + 1) % (10 * HASHES_PER_LINE)) == 0)
Simon Glassf5329bb2011-10-24 18:00:03 +0000246 puts("\n\t ");
247 }
248}
249
Simon Glasse4cde2f2011-10-24 18:00:04 +0000250/**
251 * restart the current transfer due to an error
252 *
253 * @param msg Message to print for user
254 */
255static void restart(const char *msg)
256{
257 printf("\n%s; starting again\n", msg);
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500258 net_start_again();
Simon Glasse4cde2f2011-10-24 18:00:04 +0000259}
260
261/*
262 * Check if the block number has wrapped, and update progress
263 *
264 * TODO: The egregious use of global variables in this file should be tidied.
265 */
266static void update_block_number(void)
267{
268 /*
269 * RFC1350 specifies that the first data packet will
270 * have sequence number 1. If we receive a sequence
271 * number of 0 this means that there was a wrap
272 * around of the (16 bit) counter.
273 */
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500274 if (tftp_cur_block == 0 && tftp_prev_block != 0) {
275 tftp_block_wrap++;
276 tftp_block_wrap_offset += tftp_block_size * TFTP_SEQUENCE_SIZE;
277 timeout_count = 0; /* we've done well, reset the timeout */
Simon Glasse4cde2f2011-10-24 18:00:04 +0000278 }
Ravik Hasijade5468e2020-05-07 14:55:32 -0700279 show_block_marker();
Simon Glasse4cde2f2011-10-24 18:00:04 +0000280}
281
Simon Glassf5329bb2011-10-24 18:00:03 +0000282/* The TFTP get or put is complete */
283static void tftp_complete(void)
284{
285#ifdef CONFIG_TFTP_TSIZE
286 /* Print hash marks for the last packet received */
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500287 while (tftp_tsize && tftp_tsize_num_hash < 49) {
Simon Glassf5329bb2011-10-24 18:00:03 +0000288 putc('#');
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500289 tftp_tsize_num_hash++;
Simon Glassf5329bb2011-10-24 18:00:03 +0000290 }
Simon Glass8104f542014-10-10 07:30:21 -0600291 puts(" ");
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500292 print_size(tftp_tsize, "");
Simon Glassf5329bb2011-10-24 18:00:03 +0000293#endif
Simon Glass85b19802012-10-11 13:57:36 +0000294 time_start = get_timer(time_start);
295 if (time_start > 0) {
296 puts("\n\t "); /* Line up with "Loading: " */
Joe Hershberger14111572015-04-08 01:41:02 -0500297 print_size(net_boot_file_size /
Simon Glass85b19802012-10-11 13:57:36 +0000298 time_start * 1000, "/s");
299 }
Simon Glassf5329bb2011-10-24 18:00:03 +0000300 puts("\ndone\n");
Heinrich Schuchardt5f595182021-01-12 12:46:24 +0100301 if (IS_ENABLED(CONFIG_CMD_BOOTEFI)) {
302 if (!tftp_put_active)
303 efi_set_bootdev("Net", "", tftp_filename,
304 map_sysmem(tftp_load_addr, 0),
305 net_boot_file_size);
306 }
Joe Hershberger22f6e992012-05-23 07:59:14 +0000307 net_set_state(NETLOOP_SUCCESS);
Simon Glassf5329bb2011-10-24 18:00:03 +0000308}
309
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500310static void tftp_send(void)
wdenkfe8c2802002-11-03 00:38:21 +0000311{
Simon Glass1fb7cd42011-10-24 18:00:07 +0000312 uchar *pkt;
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000313 uchar *xp;
314 int len = 0;
315 ushort *s;
Ravik Hasija08139212020-05-18 21:35:43 -0700316 bool err_pkt = false;
wdenkfe8c2802002-11-03 00:38:21 +0000317
318 /*
319 * We will always be sending some sort of packet, so
320 * cobble together the packet headers now.
321 */
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500322 pkt = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
wdenkfe8c2802002-11-03 00:38:21 +0000323
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500324 switch (tftp_state) {
Luca Ceresolie3fb0ab2011-05-17 00:03:38 +0000325 case STATE_SEND_RRQ:
Simon Glass1fb7cd42011-10-24 18:00:07 +0000326 case STATE_SEND_WRQ:
wdenkfe8c2802002-11-03 00:38:21 +0000327 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200328 s = (ushort *)pkt;
Simon Glass8c6914f2011-10-27 06:24:29 +0000329#ifdef CONFIG_CMD_TFTPPUT
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500330 *s++ = htons(tftp_state == STATE_SEND_RRQ ? TFTP_RRQ :
Simon Glass1fb7cd42011-10-24 18:00:07 +0000331 TFTP_WRQ);
Simon Glass8c6914f2011-10-27 06:24:29 +0000332#else
333 *s++ = htons(TFTP_RRQ);
334#endif
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200335 pkt = (uchar *)s;
Luca Ceresolic718b142011-05-14 05:49:57 +0000336 strcpy((char *)pkt, tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000337 pkt += strlen(tftp_filename) + 1;
Luca Ceresolic718b142011-05-14 05:49:57 +0000338 strcpy((char *)pkt, "octet");
wdenkfe8c2802002-11-03 00:38:21 +0000339 pkt += 5 /*strlen("octet")*/ + 1;
Luca Ceresolic718b142011-05-14 05:49:57 +0000340 strcpy((char *)pkt, "timeout");
wdenkfbe4b5c2003-10-06 21:55:32 +0000341 pkt += 7 /*strlen("timeout")*/ + 1;
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500342 sprintf((char *)pkt, "%lu", timeout_ms / 1000);
Robin Getz0ebf04c2009-07-23 03:01:03 -0400343 debug("send option \"timeout %s\"\n", (char *)pkt);
wdenkfbe4b5c2003-10-06 21:55:32 +0000344 pkt += strlen((char *)pkt) + 1;
Robin Getz4fccb812009-08-20 10:50:20 -0400345#ifdef CONFIG_TFTP_TSIZE
Joe Hershberger14111572015-04-08 01:41:02 -0500346 pkt += sprintf((char *)pkt, "tsize%c%u%c",
347 0, net_boot_file_size, 0);
Robin Getz4fccb812009-08-20 10:50:20 -0400348#endif
David Updegraff53a5c422007-06-11 10:41:07 -0500349 /* try for more effic. blk size */
Luca Ceresolic718b142011-05-14 05:49:57 +0000350 pkt += sprintf((char *)pkt, "blksize%c%d%c",
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500351 0, tftp_block_size_option, 0);
Ramon Friedcc6b87e2020-07-18 23:31:46 +0300352
353 /* try for more effic. window size.
354 * Implemented only for tftp get.
355 * Don't bother sending if it's 1
356 */
357 if (tftp_state == STATE_SEND_RRQ && tftp_window_size_option > 1)
358 pkt += sprintf((char *)pkt, "windowsize%c%d%c",
359 0, tftp_window_size_option, 0);
wdenkfe8c2802002-11-03 00:38:21 +0000360 len = pkt - xp;
361 break;
362
wdenkfbe4b5c2003-10-06 21:55:32 +0000363 case STATE_OACK:
Luca Ceresolie59e3562011-05-17 00:03:39 +0000364
365 case STATE_RECV_WRQ:
David Updegraff53a5c422007-06-11 10:41:07 -0500366 case STATE_DATA:
wdenkfe8c2802002-11-03 00:38:21 +0000367 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200368 s = (ushort *)pkt;
Simon Glass1fb7cd42011-10-24 18:00:07 +0000369 s[0] = htons(TFTP_ACK);
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500370 s[1] = htons(tftp_cur_block);
Simon Glass1fb7cd42011-10-24 18:00:07 +0000371 pkt = (uchar *)(s + 2);
372#ifdef CONFIG_CMD_TFTPPUT
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500373 if (tftp_put_active) {
374 int toload = tftp_block_size;
375 int loaded = load_block(tftp_cur_block, pkt, toload);
Simon Glass1fb7cd42011-10-24 18:00:07 +0000376
377 s[0] = htons(TFTP_DATA);
378 pkt += loaded;
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500379 tftp_put_final_block_sent = (loaded < toload);
Simon Glass1fb7cd42011-10-24 18:00:07 +0000380 }
381#endif
wdenkfe8c2802002-11-03 00:38:21 +0000382 len = pkt - xp;
383 break;
384
385 case STATE_TOO_LARGE:
386 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200387 s = (ushort *)pkt;
388 *s++ = htons(TFTP_ERROR);
Simon Glass1fb7cd42011-10-24 18:00:07 +0000389 *s++ = htons(3);
390
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200391 pkt = (uchar *)s;
Luca Ceresolic718b142011-05-14 05:49:57 +0000392 strcpy((char *)pkt, "File too large");
wdenkfe8c2802002-11-03 00:38:21 +0000393 pkt += 14 /*strlen("File too large")*/ + 1;
394 len = pkt - xp;
Ravik Hasija08139212020-05-18 21:35:43 -0700395 err_pkt = true;
wdenkfe8c2802002-11-03 00:38:21 +0000396 break;
397
398 case STATE_BAD_MAGIC:
399 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200400 s = (ushort *)pkt;
401 *s++ = htons(TFTP_ERROR);
402 *s++ = htons(2);
403 pkt = (uchar *)s;
Luca Ceresolic718b142011-05-14 05:49:57 +0000404 strcpy((char *)pkt, "File has bad magic");
wdenkfe8c2802002-11-03 00:38:21 +0000405 pkt += 18 /*strlen("File has bad magic")*/ + 1;
406 len = pkt - xp;
Ravik Hasija08139212020-05-18 21:35:43 -0700407 err_pkt = true;
408 break;
409
410 case STATE_INVALID_OPTION:
411 xp = pkt;
412 s = (ushort *)pkt;
413 *s++ = htons(TFTP_ERROR);
414 *s++ = htons(TFTP_ERR_OPTION_NEGOTIATION);
415 pkt = (uchar *)s;
416 strcpy((char *)pkt, "Option Negotiation Failed");
417 /* strlen("Option Negotiation Failed") + NULL*/
418 pkt += 25 + 1;
419 len = pkt - xp;
420 err_pkt = true;
wdenkfe8c2802002-11-03 00:38:21 +0000421 break;
422 }
423
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500424 net_send_udp_packet(net_server_ethaddr, tftp_remote_ip,
425 tftp_remote_port, tftp_our_port, len);
Ravik Hasija08139212020-05-18 21:35:43 -0700426
427 if (err_pkt)
428 net_set_state(NETLOOP_FAIL);
wdenkfe8c2802002-11-03 00:38:21 +0000429}
430
Simon Glass39bccd22011-10-26 14:18:38 +0000431#ifdef CONFIG_CMD_TFTPPUT
Simon Glass1fb7cd42011-10-24 18:00:07 +0000432static void icmp_handler(unsigned type, unsigned code, unsigned dest,
Joe Hershberger049a95a2015-04-08 01:41:01 -0500433 struct in_addr sip, unsigned src, uchar *pkt,
434 unsigned len)
Simon Glass1fb7cd42011-10-24 18:00:07 +0000435{
436 if (type == ICMP_NOT_REACH && code == ICMP_NOT_REACH_PORT) {
437 /* Oh dear the other end has gone away */
438 restart("TFTP server died");
439 }
440}
Simon Glass39bccd22011-10-26 14:18:38 +0000441#endif
Simon Glass1fb7cd42011-10-24 18:00:07 +0000442
Joe Hershberger049a95a2015-04-08 01:41:01 -0500443static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
444 unsigned src, unsigned len)
wdenkfe8c2802002-11-03 00:38:21 +0000445{
Kim Phillips61fdd4f2013-01-16 18:09:19 -0600446 __be16 proto;
447 __be16 *s;
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200448 int i;
Ravik Hasija08139212020-05-18 21:35:43 -0700449 u16 timeout_val_rcvd;
wdenkfe8c2802002-11-03 00:38:21 +0000450
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500451 if (dest != tftp_our_port) {
Luca Ceresoli0bdd8ac2011-05-14 05:50:02 +0000452 return;
wdenkfe8c2802002-11-03 00:38:21 +0000453 }
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500454 if (tftp_state != STATE_SEND_RRQ && src != tftp_remote_port &&
455 tftp_state != STATE_RECV_WRQ && tftp_state != STATE_SEND_WRQ)
wdenkfe8c2802002-11-03 00:38:21 +0000456 return;
wdenkfe8c2802002-11-03 00:38:21 +0000457
Luca Ceresoli7bc325a2011-05-14 05:50:00 +0000458 if (len < 2)
wdenkfe8c2802002-11-03 00:38:21 +0000459 return;
wdenkfe8c2802002-11-03 00:38:21 +0000460 len -= 2;
461 /* warning: don't use increment (++) in ntohs() macros!! */
Kim Phillips61fdd4f2013-01-16 18:09:19 -0600462 s = (__be16 *)pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200463 proto = *s++;
464 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000465 switch (ntohs(proto)) {
wdenkfe8c2802002-11-03 00:38:21 +0000466 case TFTP_RRQ:
wdenkfe8c2802002-11-03 00:38:21 +0000467 break;
Simon Glass1fb7cd42011-10-24 18:00:07 +0000468
469 case TFTP_ACK:
470#ifdef CONFIG_CMD_TFTPPUT
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500471 if (tftp_put_active) {
472 if (tftp_put_final_block_sent) {
Simon Glass1fb7cd42011-10-24 18:00:07 +0000473 tftp_complete();
474 } else {
475 /*
476 * Move to the next block. We want our block
477 * count to wrap just like the other end!
478 */
479 int block = ntohs(*s);
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500480 int ack_ok = (tftp_cur_block == block);
Simon Glass1fb7cd42011-10-24 18:00:07 +0000481
Ley Foon Tan6bf46362020-08-25 10:26:35 +0800482 tftp_prev_block = tftp_cur_block;
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500483 tftp_cur_block = (unsigned short)(block + 1);
Simon Glass1fb7cd42011-10-24 18:00:07 +0000484 update_block_number();
485 if (ack_ok)
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500486 tftp_send(); /* Send next data block */
Simon Glass1fb7cd42011-10-24 18:00:07 +0000487 }
488 }
489#endif
490 break;
491
wdenkfe8c2802002-11-03 00:38:21 +0000492 default:
493 break;
494
Luca Ceresolie59e3562011-05-17 00:03:39 +0000495#ifdef CONFIG_CMD_TFTPSRV
496 case TFTP_WRQ:
497 debug("Got WRQ\n");
Joe Hershberger049a95a2015-04-08 01:41:01 -0500498 tftp_remote_ip = sip;
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500499 tftp_remote_port = src;
500 tftp_our_port = 1024 + (get_timer(0) % 3072);
Simon Glasse4cde2f2011-10-24 18:00:04 +0000501 new_transfer();
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500502 tftp_send(); /* Send ACK(0) */
Luca Ceresolie59e3562011-05-17 00:03:39 +0000503 break;
504#endif
505
wdenkfbe4b5c2003-10-06 21:55:32 +0000506 case TFTP_OACK:
Ravik Hasija08139212020-05-18 21:35:43 -0700507 debug("Got OACK: ");
508 for (i = 0; i < len; i++) {
509 if (pkt[i] == '\0')
510 debug(" ");
511 else
512 debug("%c", pkt[i]);
513 }
514 debug("\n");
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500515 tftp_state = STATE_OACK;
516 tftp_remote_port = src;
Wolfgang Denk60174742007-08-31 10:01:51 +0200517 /*
518 * Check for 'blksize' option.
519 * Careful: "i" is signed, "len" is unsigned, thus
520 * something like "len-8" may give a *huge* number
521 */
Luca Ceresolic718b142011-05-14 05:49:57 +0000522 for (i = 0; i+8 < len; i++) {
Ravik Hasija08139212020-05-18 21:35:43 -0700523 if (strcasecmp((char *)pkt + i, "blksize") == 0) {
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500524 tftp_block_size = (unsigned short)
Simon Glass0b1284e2021-07-24 09:03:30 -0600525 dectoul((char *)pkt + i + 8, NULL);
Ravik Hasija08139212020-05-18 21:35:43 -0700526 debug("Blocksize oack: %s, %d\n",
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500527 (char *)pkt + i + 8, tftp_block_size);
Ravik Hasija08139212020-05-18 21:35:43 -0700528 if (tftp_block_size > tftp_block_size_option) {
529 printf("Invalid blk size(=%d)\n",
530 tftp_block_size);
531 tftp_state = STATE_INVALID_OPTION;
532 }
533 }
534 if (strcasecmp((char *)pkt + i, "timeout") == 0) {
535 timeout_val_rcvd = (unsigned short)
Simon Glass0b1284e2021-07-24 09:03:30 -0600536 dectoul((char *)pkt + i + 8, NULL);
Ravik Hasija08139212020-05-18 21:35:43 -0700537 debug("Timeout oack: %s, %d\n",
538 (char *)pkt + i + 8, timeout_val_rcvd);
539 if (timeout_val_rcvd != (timeout_ms / 1000)) {
540 printf("Invalid timeout val(=%d s)\n",
541 timeout_val_rcvd);
542 tftp_state = STATE_INVALID_OPTION;
543 }
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200544 }
Robin Getz4fccb812009-08-20 10:50:20 -0400545#ifdef CONFIG_TFTP_TSIZE
Ravik Hasija08139212020-05-18 21:35:43 -0700546 if (strcasecmp((char *)pkt + i, "tsize") == 0) {
Simon Glass0b1284e2021-07-24 09:03:30 -0600547 tftp_tsize = dectoul((char *)pkt + i + 6,
548 NULL);
Robin Getz4fccb812009-08-20 10:50:20 -0400549 debug("size = %s, %d\n",
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500550 (char *)pkt + i + 6, tftp_tsize);
Robin Getz4fccb812009-08-20 10:50:20 -0400551 }
552#endif
Ramon Friedcc6b87e2020-07-18 23:31:46 +0300553 if (strcasecmp((char *)pkt + i, "windowsize") == 0) {
554 tftp_windowsize =
Simon Glass0b1284e2021-07-24 09:03:30 -0600555 dectoul((char *)pkt + i + 11, NULL);
Ramon Friedcc6b87e2020-07-18 23:31:46 +0300556 debug("windowsize = %s, %d\n",
557 (char *)pkt + i + 11, tftp_windowsize);
558 }
David Updegraff53a5c422007-06-11 10:41:07 -0500559 }
Ramon Friedcc6b87e2020-07-18 23:31:46 +0300560
561 tftp_next_ack = tftp_windowsize;
562
Simon Glass1fb7cd42011-10-24 18:00:07 +0000563#ifdef CONFIG_CMD_TFTPPUT
Ravik Hasija08139212020-05-18 21:35:43 -0700564 if (tftp_put_active && tftp_state == STATE_OACK) {
Simon Glass1fb7cd42011-10-24 18:00:07 +0000565 /* Get ready to send the first block */
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500566 tftp_state = STATE_DATA;
567 tftp_cur_block++;
Simon Glass1fb7cd42011-10-24 18:00:07 +0000568 }
569#endif
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500570 tftp_send(); /* Send ACK or first data block */
wdenkfbe4b5c2003-10-06 21:55:32 +0000571 break;
wdenkfe8c2802002-11-03 00:38:21 +0000572 case TFTP_DATA:
573 if (len < 2)
574 return;
575 len -= 2;
Ramon Friedcc6b87e2020-07-18 23:31:46 +0300576
577 if (ntohs(*(__be16 *)pkt) != (ushort)(tftp_cur_block + 1)) {
578 debug("Received unexpected block: %d, expected: %d\n",
579 ntohs(*(__be16 *)pkt),
580 (ushort)(tftp_cur_block + 1));
581 /*
582 * If one packet is dropped most likely
583 * all other buffers in the window
584 * that will arrive will cause a sending NACK.
585 * This just overwellms the server, let's just send one.
586 */
587 if (tftp_last_nack != tftp_cur_block) {
588 tftp_send();
589 tftp_last_nack = tftp_cur_block;
590 tftp_next_ack = (ushort)(tftp_cur_block +
591 tftp_windowsize);
592 }
593 break;
594 }
595
596 tftp_cur_block++;
597 tftp_cur_block %= TFTP_SEQUENCE_SIZE;
wdenk3f85ce22004-02-23 16:11:30 +0000598
Harm Berntsenbeb61e12020-11-27 21:45:56 +0000599 if (tftp_state == STATE_SEND_RRQ) {
Ravik Hasija08139212020-05-18 21:35:43 -0700600 debug("Server did not acknowledge any options!\n");
Harm Berntsenbeb61e12020-11-27 21:45:56 +0000601 tftp_next_ack = tftp_windowsize;
602 }
wdenkfbe4b5c2003-10-06 21:55:32 +0000603
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500604 if (tftp_state == STATE_SEND_RRQ || tftp_state == STATE_OACK ||
605 tftp_state == STATE_RECV_WRQ) {
wdenk3f85ce22004-02-23 16:11:30 +0000606 /* first block received */
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500607 tftp_state = STATE_DATA;
608 tftp_remote_port = src;
Simon Glasse4cde2f2011-10-24 18:00:04 +0000609 new_transfer();
wdenkfe8c2802002-11-03 00:38:21 +0000610
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500611 if (tftp_cur_block != 1) { /* Assertion */
612 puts("\nTFTP error: ");
613 printf("First block is not block 1 (%ld)\n",
614 tftp_cur_block);
615 puts("Starting again\n\n");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500616 net_start_again();
wdenkfe8c2802002-11-03 00:38:21 +0000617 break;
618 }
619 }
620
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500621 if (tftp_cur_block == tftp_prev_block) {
622 /* Same block again; ignore it. */
wdenkfe8c2802002-11-03 00:38:21 +0000623 break;
624 }
625
Ravik Hasijade5468e2020-05-07 14:55:32 -0700626 update_block_number();
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500627 tftp_prev_block = tftp_cur_block;
Albert ARIBAUD \(3ADEV\)f5fb7342015-10-12 00:02:57 +0200628 timeout_count_max = tftp_timeout_count_max;
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500629 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
wdenkfe8c2802002-11-03 00:38:21 +0000630
Ley Foon Tanae0bdf02020-08-25 10:26:36 +0800631 if (store_block(tftp_cur_block, pkt + 2, len)) {
Simon Goldschmidta156c472019-01-14 22:38:22 +0100632 eth_halt();
633 net_set_state(NETLOOP_FAIL);
634 break;
635 }
wdenkfe8c2802002-11-03 00:38:21 +0000636
Ramon Fried2dddc1b2021-02-03 21:28:59 +0200637 if (len < tftp_block_size) {
638 tftp_send();
639 tftp_complete();
640 break;
641 }
642
wdenkfe8c2802002-11-03 00:38:21 +0000643 /*
Luca Ceresoli4d69e982011-05-17 00:03:41 +0000644 * Acknowledge the block just received, which will prompt
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000645 * the remote for the next one.
wdenkfe8c2802002-11-03 00:38:21 +0000646 */
Ramon Friedcc6b87e2020-07-18 23:31:46 +0300647 if (tftp_cur_block == tftp_next_ack) {
648 tftp_send();
649 tftp_next_ack += tftp_windowsize;
650 }
wdenkfe8c2802002-11-03 00:38:21 +0000651 break;
652
653 case TFTP_ERROR:
Luca Ceresolic718b142011-05-14 05:49:57 +0000654 printf("\nTFTP error: '%s' (%d)\n",
Kim Phillips61fdd4f2013-01-16 18:09:19 -0600655 pkt + 2, ntohs(*(__be16 *)pkt));
Remy Bohmeraafda382009-10-28 22:13:40 +0100656
Kim Phillips61fdd4f2013-01-16 18:09:19 -0600657 switch (ntohs(*(__be16 *)pkt)) {
Remy Bohmeraafda382009-10-28 22:13:40 +0100658 case TFTP_ERR_FILE_NOT_FOUND:
659 case TFTP_ERR_ACCESS_DENIED:
660 puts("Not retrying...\n");
661 eth_halt();
Joe Hershberger22f6e992012-05-23 07:59:14 +0000662 net_set_state(NETLOOP_FAIL);
Remy Bohmeraafda382009-10-28 22:13:40 +0100663 break;
664 case TFTP_ERR_UNDEFINED:
665 case TFTP_ERR_DISK_FULL:
666 case TFTP_ERR_UNEXPECTED_OPCODE:
667 case TFTP_ERR_UNKNOWN_TRANSFER_ID:
668 case TFTP_ERR_FILE_ALREADY_EXISTS:
669 default:
670 puts("Starting again\n\n");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500671 net_start_again();
Remy Bohmeraafda382009-10-28 22:13:40 +0100672 break;
673 }
wdenkfe8c2802002-11-03 00:38:21 +0000674 break;
675 }
676}
677
678
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500679static void tftp_timeout_handler(void)
wdenkfe8c2802002-11-03 00:38:21 +0000680{
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500681 if (++timeout_count > timeout_count_max) {
Simon Glasse4cde2f2011-10-24 18:00:04 +0000682 restart("Retry count exceeded");
wdenkfe8c2802002-11-03 00:38:21 +0000683 } else {
Luca Ceresolic718b142011-05-14 05:49:57 +0000684 puts("T ");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500685 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500686 if (tftp_state != STATE_RECV_WRQ)
687 tftp_send();
wdenkfe8c2802002-11-03 00:38:21 +0000688 }
689}
690
Simon Glassbb872dd2019-12-28 10:45:02 -0700691/* Initialize tftp_load_addr and tftp_load_size from image_load_addr and lmb */
Simon Goldschmidta156c472019-01-14 22:38:22 +0100692static int tftp_init_load_addr(void)
693{
694#ifdef CONFIG_LMB
695 struct lmb lmb;
696 phys_size_t max_size;
697
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100698 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
Simon Goldschmidta156c472019-01-14 22:38:22 +0100699
Simon Glassbb872dd2019-12-28 10:45:02 -0700700 max_size = lmb_get_free_size(&lmb, image_load_addr);
Simon Goldschmidta156c472019-01-14 22:38:22 +0100701 if (!max_size)
702 return -1;
703
704 tftp_load_size = max_size;
705#endif
Simon Glassbb872dd2019-12-28 10:45:02 -0700706 tftp_load_addr = image_load_addr;
Simon Goldschmidta156c472019-01-14 22:38:22 +0100707 return 0;
708}
wdenkfe8c2802002-11-03 00:38:21 +0000709
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500710void tftp_start(enum proto_t protocol)
wdenkfe8c2802002-11-03 00:38:21 +0000711{
Albert ARIBAUD \(3ADEV\)f5fb7342015-10-12 00:02:57 +0200712#if CONFIG_NET_TFTP_VARS
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200713 char *ep; /* Environment pointer */
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200714
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100715 /*
716 * Allow the user to choose TFTP blocksize and timeout.
717 * TFTP protocol has a minimal timeout of 1 second.
718 */
Albert ARIBAUD \(3ADEV\)f5fb7342015-10-12 00:02:57 +0200719
Simon Glass00caae62017-08-03 12:22:12 -0600720 ep = env_get("tftpblocksize");
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000721 if (ep != NULL)
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500722 tftp_block_size_option = simple_strtol(ep, NULL, 10);
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100723
Ramon Friedcc6b87e2020-07-18 23:31:46 +0300724 ep = env_get("tftpwindowsize");
725 if (ep != NULL)
726 tftp_window_size_option = simple_strtol(ep, NULL, 10);
727
Simon Glass00caae62017-08-03 12:22:12 -0600728 ep = env_get("tftptimeout");
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000729 if (ep != NULL)
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500730 timeout_ms = simple_strtol(ep, NULL, 10);
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100731
Bin Mengaf2ca592015-08-27 22:25:51 -0700732 if (timeout_ms < 1000) {
733 printf("TFTP timeout (%ld ms) too low, set min = 1000 ms\n",
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500734 timeout_ms);
Bin Mengaf2ca592015-08-27 22:25:51 -0700735 timeout_ms = 1000;
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100736 }
737
Simon Glass00caae62017-08-03 12:22:12 -0600738 ep = env_get("tftptimeoutcountmax");
Albert ARIBAUD \(3ADEV\)f5fb7342015-10-12 00:02:57 +0200739 if (ep != NULL)
740 tftp_timeout_count_max = simple_strtol(ep, NULL, 10);
741
742 if (tftp_timeout_count_max < 0) {
743 printf("TFTP timeout count max (%d ms) negative, set to 0\n",
744 tftp_timeout_count_max);
745 tftp_timeout_count_max = 0;
746 }
747#endif
748
Ramon Friedcc6b87e2020-07-18 23:31:46 +0300749 debug("TFTP blocksize = %i, TFTP windowsize = %d timeout = %ld ms\n",
750 tftp_block_size_option, tftp_window_size_option, timeout_ms);
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200751
Joe Hershberger049a95a2015-04-08 01:41:01 -0500752 tftp_remote_ip = net_server_ip;
Joe Hershberger6ab12832018-07-03 19:36:43 -0500753 if (!net_parse_bootfile(&tftp_remote_ip, tftp_filename, MAX_LEN)) {
Matthias Weisserea45cb02011-12-03 03:29:44 +0000754 sprintf(default_filename, "%02X%02X%02X%02X.img",
Joe Hershberger049a95a2015-04-08 01:41:01 -0500755 net_ip.s_addr & 0xFF,
756 (net_ip.s_addr >> 8) & 0xFF,
757 (net_ip.s_addr >> 16) & 0xFF,
758 (net_ip.s_addr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100759
Vladimir Zapolskiy0c17b1b2017-06-28 22:56:07 +0300760 strncpy(tftp_filename, default_filename, DEFAULT_NAME_LEN);
761 tftp_filename[DEFAULT_NAME_LEN - 1] = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000762
Luca Ceresolic718b142011-05-14 05:49:57 +0000763 printf("*** Warning: no boot file name; using '%s'\n",
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500764 tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000765 }
766
Luca Ceresolic718b142011-05-14 05:49:57 +0000767 printf("Using %s device\n", eth_get_name());
Simon Glass1fb7cd42011-10-24 18:00:07 +0000768 printf("TFTP %s server %pI4; our IP address is %pI4",
Simon Glass8c6914f2011-10-27 06:24:29 +0000769#ifdef CONFIG_CMD_TFTPPUT
770 protocol == TFTPPUT ? "to" : "from",
771#else
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500772 "from",
Simon Glass8c6914f2011-10-27 06:24:29 +0000773#endif
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500774 &tftp_remote_ip, &net_ip);
wdenkfe8c2802002-11-03 00:38:21 +0000775
776 /* Check if we need to send across this subnet */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500777 if (net_gateway.s_addr && net_netmask.s_addr) {
778 struct in_addr our_net;
779 struct in_addr remote_net;
wdenkfe8c2802002-11-03 00:38:21 +0000780
Joe Hershberger049a95a2015-04-08 01:41:01 -0500781 our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
782 remote_net.s_addr = tftp_remote_ip.s_addr & net_netmask.s_addr;
783 if (our_net.s_addr != remote_net.s_addr)
784 printf("; sending through gateway %pI4", &net_gateway);
wdenkfe8c2802002-11-03 00:38:21 +0000785 }
Luca Ceresolic718b142011-05-14 05:49:57 +0000786 putc('\n');
wdenkfe8c2802002-11-03 00:38:21 +0000787
Luca Ceresolic718b142011-05-14 05:49:57 +0000788 printf("Filename '%s'.", tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000789
Joe Hershberger14111572015-04-08 01:41:02 -0500790 if (net_boot_file_expected_size_in_blocks) {
791 printf(" Size is 0x%x Bytes = ",
792 net_boot_file_expected_size_in_blocks << 9);
793 print_size(net_boot_file_expected_size_in_blocks << 9, "");
wdenkfe8c2802002-11-03 00:38:21 +0000794 }
795
Luca Ceresolic718b142011-05-14 05:49:57 +0000796 putc('\n');
Simon Glass1fb7cd42011-10-24 18:00:07 +0000797#ifdef CONFIG_CMD_TFTPPUT
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500798 tftp_put_active = (protocol == TFTPPUT);
799 if (tftp_put_active) {
Simon Glassbb872dd2019-12-28 10:45:02 -0700800 printf("Save address: 0x%lx\n", image_save_addr);
801 printf("Save size: 0x%lx\n", image_save_size);
802 net_boot_file_size = image_save_size;
Simon Glass1fb7cd42011-10-24 18:00:07 +0000803 puts("Saving: *\b");
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500804 tftp_state = STATE_SEND_WRQ;
Simon Glass1fb7cd42011-10-24 18:00:07 +0000805 new_transfer();
806 } else
807#endif
808 {
Simon Goldschmidta156c472019-01-14 22:38:22 +0100809 if (tftp_init_load_addr()) {
810 eth_halt();
811 net_set_state(NETLOOP_FAIL);
812 puts("\nTFTP error: ");
813 puts("trying to overwrite reserved memory...\n");
814 return;
815 }
816 printf("Load address: 0x%lx\n", tftp_load_addr);
Simon Glass1fb7cd42011-10-24 18:00:07 +0000817 puts("Loading: *\b");
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500818 tftp_state = STATE_SEND_RRQ;
Simon Glass1fb7cd42011-10-24 18:00:07 +0000819 }
wdenkfe8c2802002-11-03 00:38:21 +0000820
Simon Glass85b19802012-10-11 13:57:36 +0000821 time_start = get_timer(0);
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500822 timeout_count_max = tftp_timeout_count_max;
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200823
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500824 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
Joe Hershberger049a95a2015-04-08 01:41:01 -0500825 net_set_udp_handler(tftp_handler);
Simon Glass39bccd22011-10-26 14:18:38 +0000826#ifdef CONFIG_CMD_TFTPPUT
Simon Glass1fb7cd42011-10-24 18:00:07 +0000827 net_set_icmp_handler(icmp_handler);
Simon Glass39bccd22011-10-26 14:18:38 +0000828#endif
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500829 tftp_remote_port = WELL_KNOWN_PORT;
830 timeout_count = 0;
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200831 /* Use a pseudo-random port unless a specific port is set */
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500832 tftp_our_port = 1024 + (get_timer(0) % 3072);
David Updegraff53a5c422007-06-11 10:41:07 -0500833
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200834#ifdef CONFIG_TFTP_PORT
Simon Glass00caae62017-08-03 12:22:12 -0600835 ep = env_get("tftpdstp");
Luca Ceresoli7bc325a2011-05-14 05:50:00 +0000836 if (ep != NULL)
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500837 tftp_remote_port = simple_strtol(ep, NULL, 10);
Simon Glass00caae62017-08-03 12:22:12 -0600838 ep = env_get("tftpsrcp");
Luca Ceresoli7bc325a2011-05-14 05:50:00 +0000839 if (ep != NULL)
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500840 tftp_our_port = simple_strtol(ep, NULL, 10);
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200841#endif
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500842 tftp_cur_block = 0;
Ramon Friedcc6b87e2020-07-18 23:31:46 +0300843 tftp_windowsize = 1;
844 tftp_last_nack = 0;
wdenk73a8b272003-06-05 19:27:42 +0000845 /* zero out server ether in case the server ip has changed */
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500846 memset(net_server_ethaddr, 0, 6);
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500847 /* Revert tftp_block_size to dflt */
848 tftp_block_size = TFTP_BLOCK_SIZE;
Robin Getz4fccb812009-08-20 10:50:20 -0400849#ifdef CONFIG_TFTP_TSIZE
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500850 tftp_tsize = 0;
851 tftp_tsize_num_hash = 0;
Robin Getz4fccb812009-08-20 10:50:20 -0400852#endif
wdenk73a8b272003-06-05 19:27:42 +0000853
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500854 tftp_send();
wdenkfe8c2802002-11-03 00:38:21 +0000855}
856
Luca Ceresolie59e3562011-05-17 00:03:39 +0000857#ifdef CONFIG_CMD_TFTPSRV
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500858void tftp_start_server(void)
Luca Ceresolie59e3562011-05-17 00:03:39 +0000859{
860 tftp_filename[0] = 0;
861
Simon Goldschmidta156c472019-01-14 22:38:22 +0100862 if (tftp_init_load_addr()) {
863 eth_halt();
864 net_set_state(NETLOOP_FAIL);
865 puts("\nTFTP error: trying to overwrite reserved memory...\n");
866 return;
867 }
Luca Ceresolie59e3562011-05-17 00:03:39 +0000868 printf("Using %s device\n", eth_get_name());
Joe Hershberger049a95a2015-04-08 01:41:01 -0500869 printf("Listening for TFTP transfer on %pI4\n", &net_ip);
Simon Goldschmidta156c472019-01-14 22:38:22 +0100870 printf("Load address: 0x%lx\n", tftp_load_addr);
Luca Ceresolie59e3562011-05-17 00:03:39 +0000871
872 puts("Loading: *\b");
873
Albert ARIBAUD \(3ADEV\)f5fb7342015-10-12 00:02:57 +0200874 timeout_count_max = tftp_timeout_count_max;
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500875 timeout_count = 0;
876 timeout_ms = TIMEOUT;
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500877 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
Luca Ceresolie59e3562011-05-17 00:03:39 +0000878
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500879 /* Revert tftp_block_size to dflt */
880 tftp_block_size = TFTP_BLOCK_SIZE;
881 tftp_cur_block = 0;
882 tftp_our_port = WELL_KNOWN_PORT;
Arjan Minzinga Zijlstra1ffe3662022-03-31 08:03:16 +0000883 tftp_windowsize = 1;
884 tftp_next_ack = tftp_windowsize;
Luca Ceresolie59e3562011-05-17 00:03:39 +0000885
886#ifdef CONFIG_TFTP_TSIZE
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500887 tftp_tsize = 0;
888 tftp_tsize_num_hash = 0;
Luca Ceresolie59e3562011-05-17 00:03:39 +0000889#endif
890
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500891 tftp_state = STATE_RECV_WRQ;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500892 net_set_udp_handler(tftp_handler);
Andrew Ruder8e525332013-10-22 19:10:28 -0500893
894 /* zero out server ether in case the server ip has changed */
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500895 memset(net_server_ethaddr, 0, 6);
Luca Ceresolie59e3562011-05-17 00:03:39 +0000896}
897#endif /* CONFIG_CMD_TFTPSRV */