blob: a9335b1b7e0b2a20ccb0757e5c3b81edc7e1a2c2 [file] [log] [blame]
wdenkfe8c2802002-11-03 00:38:21 +00001/*
Luca Ceresolic42fc272011-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 Ceresolib640ed32011-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 */
8
9#include <common.h>
10#include <command.h>
Alexander Graf94c4b992016-05-06 21:01:01 +020011#include <efi_loader.h>
Joe Hershbergered191852015-03-22 17:09:08 -050012#include <mapmem.h>
wdenkfe8c2802002-11-03 00:38:21 +000013#include <net.h>
Lukasz Majewski21185922015-08-24 00:21:43 +020014#include <net/tftp.h>
wdenkfe8c2802002-11-03 00:38:21 +000015#include "bootp.h"
Joe Hershberger3a29e5a2012-05-15 08:59:12 +000016#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
17#include <flash.h>
18#endif
wdenkfe8c2802002-11-03 00:38:21 +000019
Simon Goldschmidta96cf8f2019-01-14 22:38:22 +010020DECLARE_GLOBAL_DATA_PTR;
21
Luca Ceresolic42fc272011-05-14 05:49:56 +000022/* Well known TFTP port # */
23#define WELL_KNOWN_PORT 69
24/* Millisecs to timeout for lost pkt */
Bin Menge67eaa82015-08-27 22:25:51 -070025#define TIMEOUT 5000UL
wdenkfe8c2802002-11-03 00:38:21 +000026#ifndef CONFIG_NET_RETRY_COUNT
Luca Ceresolic42fc272011-05-14 05:49:56 +000027/* # of timeouts before giving up */
Bin Menge67eaa82015-08-27 22:25:51 -070028# define TIMEOUT_COUNT 10
wdenkfe8c2802002-11-03 00:38:21 +000029#else
30# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
31#endif
Luca Ceresolic42fc272011-05-14 05:49:56 +000032/* Number of "loading" hashes per line (for checking the image size) */
33#define HASHES_PER_LINE 65
wdenkfe8c2802002-11-03 00:38:21 +000034
35/*
36 * TFTP operations.
37 */
38#define TFTP_RRQ 1
39#define TFTP_WRQ 2
40#define TFTP_DATA 3
41#define TFTP_ACK 4
42#define TFTP_ERROR 5
wdenk7182b0f2003-10-06 21:55:32 +000043#define TFTP_OACK 6
wdenkfe8c2802002-11-03 00:38:21 +000044
Joe Hershberger64701592015-04-08 01:41:07 -050045static ulong timeout_ms = TIMEOUT;
46static int timeout_count_max = TIMEOUT_COUNT;
Simon Glasscab9a8d2012-10-11 13:57:36 +000047static ulong time_start; /* Record time we started tftp */
Bartlomiej Sieka371cde72008-10-01 15:26:29 +020048
49/*
50 * These globals govern the timeout behavior when attempting a connection to a
Joe Hershberger64701592015-04-08 01:41:07 -050051 * TFTP server. tftp_timeout_ms specifies the number of milliseconds to
Bartlomiej Sieka371cde72008-10-01 15:26:29 +020052 * wait for the server to respond to initial connection. Second global,
Joe Hershberger64701592015-04-08 01:41:07 -050053 * tftp_timeout_count_max, gives the number of such connection retries.
54 * tftp_timeout_count_max must be non-negative and tftp_timeout_ms must be
Bartlomiej Sieka371cde72008-10-01 15:26:29 +020055 * positive. The globals are meant to be set (and restored) by code needing
56 * non-standard timeout behavior when initiating a TFTP transfer.
57 */
Joe Hershberger64701592015-04-08 01:41:07 -050058ulong tftp_timeout_ms = TIMEOUT;
59int tftp_timeout_count_max = TIMEOUT_COUNT;
Bartlomiej Sieka371cde72008-10-01 15:26:29 +020060
Remy Bohmerb46293f2009-10-28 22:13:40 +010061enum {
62 TFTP_ERR_UNDEFINED = 0,
63 TFTP_ERR_FILE_NOT_FOUND = 1,
64 TFTP_ERR_ACCESS_DENIED = 2,
65 TFTP_ERR_DISK_FULL = 3,
66 TFTP_ERR_UNEXPECTED_OPCODE = 4,
67 TFTP_ERR_UNKNOWN_TRANSFER_ID = 5,
68 TFTP_ERR_FILE_ALREADY_EXISTS = 6,
69};
70
Joe Hershberger5874dec2015-04-08 01:41:01 -050071static struct in_addr tftp_remote_ip;
Luca Ceresolic42fc272011-05-14 05:49:56 +000072/* The UDP port at their end */
Joe Hershberger64701592015-04-08 01:41:07 -050073static int tftp_remote_port;
Luca Ceresolic42fc272011-05-14 05:49:56 +000074/* The UDP port at our end */
Joe Hershberger64701592015-04-08 01:41:07 -050075static int tftp_our_port;
76static int timeout_count;
Luca Ceresolic42fc272011-05-14 05:49:56 +000077/* packet sequence number */
Joe Hershberger64701592015-04-08 01:41:07 -050078static ulong tftp_cur_block;
Luca Ceresolic42fc272011-05-14 05:49:56 +000079/* last packet sequence number received */
Joe Hershberger64701592015-04-08 01:41:07 -050080static ulong tftp_prev_block;
Luca Ceresolic42fc272011-05-14 05:49:56 +000081/* count of sequence number wraparounds */
Joe Hershberger64701592015-04-08 01:41:07 -050082static ulong tftp_block_wrap;
Luca Ceresolic42fc272011-05-14 05:49:56 +000083/* memory offset due to wrapping */
Joe Hershberger64701592015-04-08 01:41:07 -050084static ulong tftp_block_wrap_offset;
85static int tftp_state;
Simon Goldschmidta96cf8f2019-01-14 22:38:22 +010086static ulong tftp_load_addr;
87#ifdef CONFIG_LMB
88static ulong tftp_load_size;
89#endif
Robin Getzb3b00ed2009-08-20 10:50:20 -040090#ifdef CONFIG_TFTP_TSIZE
Luca Ceresolic42fc272011-05-14 05:49:56 +000091/* The file size reported by the server */
Joe Hershberger64701592015-04-08 01:41:07 -050092static int tftp_tsize;
Luca Ceresolic42fc272011-05-14 05:49:56 +000093/* The number of hashes we printed */
Joe Hershberger64701592015-04-08 01:41:07 -050094static short tftp_tsize_num_hash;
Robin Getzb3b00ed2009-08-20 10:50:20 -040095#endif
Simon Glass6a398d22011-10-24 18:00:07 +000096#ifdef CONFIG_CMD_TFTPPUT
Joe Hershberger64701592015-04-08 01:41:07 -050097/* 1 if writing, else 0 */
98static int tftp_put_active;
99/* 1 if we have sent the last block */
100static int tftp_put_final_block_sent;
Simon Glass6a398d22011-10-24 18:00:07 +0000101#else
Joe Hershberger64701592015-04-08 01:41:07 -0500102#define tftp_put_active 0
Simon Glass6a398d22011-10-24 18:00:07 +0000103#endif
wdenkef893942004-02-23 16:11:30 +0000104
Luca Ceresoli562dfe52011-05-17 00:03:38 +0000105#define STATE_SEND_RRQ 1
wdenkfe8c2802002-11-03 00:38:21 +0000106#define STATE_DATA 2
107#define STATE_TOO_LARGE 3
108#define STATE_BAD_MAGIC 4
wdenk7182b0f2003-10-06 21:55:32 +0000109#define STATE_OACK 5
Luca Ceresolib640ed32011-05-17 00:03:39 +0000110#define STATE_RECV_WRQ 6
Simon Glass6a398d22011-10-24 18:00:07 +0000111#define STATE_SEND_WRQ 7
wdenkfe8c2802002-11-03 00:38:21 +0000112
Luca Ceresolic42fc272011-05-14 05:49:56 +0000113/* default TFTP block size */
114#define TFTP_BLOCK_SIZE 512
115/* sequence number is 16 bit */
116#define TFTP_SEQUENCE_SIZE ((ulong)(1<<16))
wdenkef893942004-02-23 16:11:30 +0000117
wdenkfe8c2802002-11-03 00:38:21 +0000118#define DEFAULT_NAME_LEN (8 + 4 + 1)
119static char default_filename[DEFAULT_NAME_LEN];
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100120
121#ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
122#define MAX_LEN 128
123#else
124#define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
125#endif
126
127static char tftp_filename[MAX_LEN];
wdenkfe8c2802002-11-03 00:38:21 +0000128
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200129/* 512 is poor choice for ethernet, MTU is typically 1500.
130 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
David Updegraff7280da72007-06-11 10:41:07 -0500131 * almost-MTU block sizes. At least try... fall back to 512 if need be.
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200132 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
David Updegraff7280da72007-06-11 10:41:07 -0500133 */
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200134#ifdef CONFIG_TFTP_BLOCKSIZE
135#define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
136#else
David Updegraff7280da72007-06-11 10:41:07 -0500137#define TFTP_MTU_BLOCKSIZE 1468
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200138#endif
139
Joe Hershberger64701592015-04-08 01:41:07 -0500140static unsigned short tftp_block_size = TFTP_BLOCK_SIZE;
141static unsigned short tftp_block_size_option = TFTP_MTU_BLOCKSIZE;
David Updegraff7280da72007-06-11 10:41:07 -0500142
143#ifdef CONFIG_MCAST_TFTP
144#include <malloc.h>
145#define MTFTP_BITMAPSIZE 0x1000
Joe Hershberger64701592015-04-08 01:41:07 -0500146static unsigned *tftp_mcast_bitmap;
147static int tftp_mcast_prev_hole;
148static int tftp_mcast_bitmap_size = MTFTP_BITMAPSIZE;
149static int tftp_mcast_disabled;
150static int tftp_mcast_master_client;
151static int tftp_mcast_active;
152static int tftp_mcast_port;
153/* can get 'last' block before done..*/
154static ulong tftp_mcast_ending_block;
David Updegraff7280da72007-06-11 10:41:07 -0500155
Luca Ceresolif40538a2011-05-14 05:49:57 +0000156static void parse_multicast_oack(char *pkt, int len);
David Updegraff7280da72007-06-11 10:41:07 -0500157
Joe Hershberger64701592015-04-08 01:41:07 -0500158static void mcast_cleanup(void)
David Updegraff7280da72007-06-11 10:41:07 -0500159{
Joe Hershberger5874dec2015-04-08 01:41:01 -0500160 if (net_mcast_addr)
161 eth_mcast_join(net_mcast_addr, 0);
Joe Hershberger64701592015-04-08 01:41:07 -0500162 if (tftp_mcast_bitmap)
163 free(tftp_mcast_bitmap);
164 tftp_mcast_bitmap = NULL;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500165 net_mcast_addr.s_addr = 0;
Joe Hershberger64701592015-04-08 01:41:07 -0500166 tftp_mcast_active = 0;
167 tftp_mcast_port = 0;
168 tftp_mcast_ending_block = -1;
David Updegraff7280da72007-06-11 10:41:07 -0500169}
170
171#endif /* CONFIG_MCAST_TFTP */
172
Simon Goldschmidta96cf8f2019-01-14 22:38:22 +0100173static inline int store_block(int block, uchar *src, unsigned int len)
wdenkfe8c2802002-11-03 00:38:21 +0000174{
Joe Hershberger64701592015-04-08 01:41:07 -0500175 ulong offset = block * tftp_block_size + tftp_block_wrap_offset;
wdenkef893942004-02-23 16:11:30 +0000176 ulong newsize = offset + len;
Simon Goldschmidta96cf8f2019-01-14 22:38:22 +0100177 ulong store_addr = tftp_load_addr + offset;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200178#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
wdenkfe8c2802002-11-03 00:38:21 +0000179 int i, rc = 0;
180
Luca Ceresolif40538a2011-05-14 05:49:57 +0000181 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenkfe8c2802002-11-03 00:38:21 +0000182 /* start address in flash? */
Jochen Friedrichd31a59b2008-09-02 11:24:59 +0200183 if (flash_info[i].flash_id == FLASH_UNKNOWN)
184 continue;
Simon Goldschmidta96cf8f2019-01-14 22:38:22 +0100185 if (store_addr >= flash_info[i].start[0]) {
wdenkfe8c2802002-11-03 00:38:21 +0000186 rc = 1;
187 break;
188 }
189 }
190
191 if (rc) { /* Flash is destination for this packet */
Simon Goldschmidta96cf8f2019-01-14 22:38:22 +0100192 rc = flash_write((char *)src, store_addr, len);
wdenkfe8c2802002-11-03 00:38:21 +0000193 if (rc) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000194 flash_perror(rc);
Simon Goldschmidta96cf8f2019-01-14 22:38:22 +0100195 return rc;
wdenkfe8c2802002-11-03 00:38:21 +0000196 }
Joe Hershberger3a29e5a2012-05-15 08:59:12 +0000197 } else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200198#endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000199 {
Simon Goldschmidta96cf8f2019-01-14 22:38:22 +0100200 void *ptr;
Joe Hershbergered191852015-03-22 17:09:08 -0500201
Simon Goldschmidta96cf8f2019-01-14 22:38:22 +0100202#ifdef CONFIG_LMB
203 if (store_addr < tftp_load_addr ||
204 store_addr + len > tftp_load_addr + tftp_load_size) {
205 puts("\nTFTP error: ");
206 puts("trying to overwrite reserved memory...\n");
207 return -1;
208 }
209#endif
210 ptr = map_sysmem(store_addr, len);
Joe Hershbergered191852015-03-22 17:09:08 -0500211 memcpy(ptr, src, len);
212 unmap_sysmem(ptr);
wdenkfe8c2802002-11-03 00:38:21 +0000213 }
David Updegraff7280da72007-06-11 10:41:07 -0500214#ifdef CONFIG_MCAST_TFTP
Joe Hershberger64701592015-04-08 01:41:07 -0500215 if (tftp_mcast_active)
216 ext2_set_bit(block, tftp_mcast_bitmap);
David Updegraff7280da72007-06-11 10:41:07 -0500217#endif
wdenkfe8c2802002-11-03 00:38:21 +0000218
Joe Hershberger290c8992015-04-08 01:41:02 -0500219 if (net_boot_file_size < newsize)
220 net_boot_file_size = newsize;
Simon Goldschmidta96cf8f2019-01-14 22:38:22 +0100221
222 return 0;
wdenkfe8c2802002-11-03 00:38:21 +0000223}
224
Simon Glassbd8a0e02011-10-24 18:00:04 +0000225/* Clear our state ready for a new transfer */
Simon Glass84313322011-10-27 06:24:28 +0000226static void new_transfer(void)
Simon Glassbd8a0e02011-10-24 18:00:04 +0000227{
Joe Hershberger64701592015-04-08 01:41:07 -0500228 tftp_prev_block = 0;
229 tftp_block_wrap = 0;
230 tftp_block_wrap_offset = 0;
Simon Glassbd8a0e02011-10-24 18:00:04 +0000231#ifdef CONFIG_CMD_TFTPPUT
Joe Hershberger64701592015-04-08 01:41:07 -0500232 tftp_put_final_block_sent = 0;
Simon Glassbd8a0e02011-10-24 18:00:04 +0000233#endif
234}
Simon Glass6a398d22011-10-24 18:00:07 +0000235
236#ifdef CONFIG_CMD_TFTPPUT
237/**
238 * Load the next block from memory to be sent over tftp.
239 *
240 * @param block Block number to send
241 * @param dst Destination buffer for data
242 * @param len Number of bytes in block (this one and every other)
243 * @return number of bytes loaded
244 */
245static int load_block(unsigned block, uchar *dst, unsigned len)
246{
247 /* We may want to get the final block from the previous set */
Joe Hershberger64701592015-04-08 01:41:07 -0500248 ulong offset = ((int)block - 1) * len + tftp_block_wrap_offset;
Simon Glass6a398d22011-10-24 18:00:07 +0000249 ulong tosend = len;
250
Joe Hershberger290c8992015-04-08 01:41:02 -0500251 tosend = min(net_boot_file_size - offset, tosend);
Simon Glass6a398d22011-10-24 18:00:07 +0000252 (void)memcpy(dst, (void *)(save_addr + offset), tosend);
253 debug("%s: block=%d, offset=%ld, len=%d, tosend=%ld\n", __func__,
Joe Hershberger64701592015-04-08 01:41:07 -0500254 block, offset, len, tosend);
Simon Glass6a398d22011-10-24 18:00:07 +0000255 return tosend;
256}
257#endif
Simon Glassbd8a0e02011-10-24 18:00:04 +0000258
Joe Hershberger64701592015-04-08 01:41:07 -0500259static void tftp_send(void);
260static void tftp_timeout_handler(void);
wdenkfe8c2802002-11-03 00:38:21 +0000261
262/**********************************************************************/
263
Simon Glassb437aad2011-10-24 18:00:03 +0000264static void show_block_marker(void)
265{
266#ifdef CONFIG_TFTP_TSIZE
Joe Hershberger64701592015-04-08 01:41:07 -0500267 if (tftp_tsize) {
268 ulong pos = tftp_cur_block * tftp_block_size +
269 tftp_block_wrap_offset;
Max Krummenacherdd081cc2015-08-05 17:17:05 +0200270 if (pos > tftp_tsize)
271 pos = tftp_tsize;
Simon Glassb437aad2011-10-24 18:00:03 +0000272
Joe Hershberger64701592015-04-08 01:41:07 -0500273 while (tftp_tsize_num_hash < pos * 50 / tftp_tsize) {
Simon Glassb437aad2011-10-24 18:00:03 +0000274 putc('#');
Joe Hershberger64701592015-04-08 01:41:07 -0500275 tftp_tsize_num_hash++;
Simon Glassb437aad2011-10-24 18:00:03 +0000276 }
Simon Glass6a398d22011-10-24 18:00:07 +0000277 } else
Simon Glassb437aad2011-10-24 18:00:03 +0000278#endif
Simon Glass6a398d22011-10-24 18:00:07 +0000279 {
Joe Hershberger64701592015-04-08 01:41:07 -0500280 if (((tftp_cur_block - 1) % 10) == 0)
Simon Glassb437aad2011-10-24 18:00:03 +0000281 putc('#');
Joe Hershberger64701592015-04-08 01:41:07 -0500282 else if ((tftp_cur_block % (10 * HASHES_PER_LINE)) == 0)
Simon Glassb437aad2011-10-24 18:00:03 +0000283 puts("\n\t ");
284 }
285}
286
Simon Glassbd8a0e02011-10-24 18:00:04 +0000287/**
288 * restart the current transfer due to an error
289 *
290 * @param msg Message to print for user
291 */
292static void restart(const char *msg)
293{
294 printf("\n%s; starting again\n", msg);
295#ifdef CONFIG_MCAST_TFTP
296 mcast_cleanup();
297#endif
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500298 net_start_again();
Simon Glassbd8a0e02011-10-24 18:00:04 +0000299}
300
301/*
302 * Check if the block number has wrapped, and update progress
303 *
304 * TODO: The egregious use of global variables in this file should be tidied.
305 */
306static void update_block_number(void)
307{
308 /*
309 * RFC1350 specifies that the first data packet will
310 * have sequence number 1. If we receive a sequence
311 * number of 0 this means that there was a wrap
312 * around of the (16 bit) counter.
313 */
Joe Hershberger64701592015-04-08 01:41:07 -0500314 if (tftp_cur_block == 0 && tftp_prev_block != 0) {
315 tftp_block_wrap++;
316 tftp_block_wrap_offset += tftp_block_size * TFTP_SEQUENCE_SIZE;
317 timeout_count = 0; /* we've done well, reset the timeout */
Simon Glassbd8a0e02011-10-24 18:00:04 +0000318 } else {
319 show_block_marker();
320 }
321}
322
Simon Glassb437aad2011-10-24 18:00:03 +0000323/* The TFTP get or put is complete */
324static void tftp_complete(void)
325{
326#ifdef CONFIG_TFTP_TSIZE
327 /* Print hash marks for the last packet received */
Joe Hershberger64701592015-04-08 01:41:07 -0500328 while (tftp_tsize && tftp_tsize_num_hash < 49) {
Simon Glassb437aad2011-10-24 18:00:03 +0000329 putc('#');
Joe Hershberger64701592015-04-08 01:41:07 -0500330 tftp_tsize_num_hash++;
Simon Glassb437aad2011-10-24 18:00:03 +0000331 }
Simon Glassc8f0a5c2014-10-10 07:30:21 -0600332 puts(" ");
Joe Hershberger64701592015-04-08 01:41:07 -0500333 print_size(tftp_tsize, "");
Simon Glassb437aad2011-10-24 18:00:03 +0000334#endif
Simon Glasscab9a8d2012-10-11 13:57:36 +0000335 time_start = get_timer(time_start);
336 if (time_start > 0) {
337 puts("\n\t "); /* Line up with "Loading: " */
Joe Hershberger290c8992015-04-08 01:41:02 -0500338 print_size(net_boot_file_size /
Simon Glasscab9a8d2012-10-11 13:57:36 +0000339 time_start * 1000, "/s");
340 }
Simon Glassb437aad2011-10-24 18:00:03 +0000341 puts("\ndone\n");
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000342 net_set_state(NETLOOP_SUCCESS);
Simon Glassb437aad2011-10-24 18:00:03 +0000343}
344
Joe Hershberger64701592015-04-08 01:41:07 -0500345static void tftp_send(void)
wdenkfe8c2802002-11-03 00:38:21 +0000346{
Simon Glass6a398d22011-10-24 18:00:07 +0000347 uchar *pkt;
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000348 uchar *xp;
349 int len = 0;
350 ushort *s;
wdenkfe8c2802002-11-03 00:38:21 +0000351
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200352#ifdef CONFIG_MCAST_TFTP
David Updegraff7280da72007-06-11 10:41:07 -0500353 /* Multicast TFTP.. non-MasterClients do not ACK data. */
Joe Hershberger64701592015-04-08 01:41:07 -0500354 if (tftp_mcast_active && tftp_state == STATE_DATA &&
355 tftp_mcast_master_client == 0)
David Updegraff7280da72007-06-11 10:41:07 -0500356 return;
357#endif
wdenkfe8c2802002-11-03 00:38:21 +0000358 /*
359 * We will always be sending some sort of packet, so
360 * cobble together the packet headers now.
361 */
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500362 pkt = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
wdenkfe8c2802002-11-03 00:38:21 +0000363
Joe Hershberger64701592015-04-08 01:41:07 -0500364 switch (tftp_state) {
Luca Ceresoli562dfe52011-05-17 00:03:38 +0000365 case STATE_SEND_RRQ:
Simon Glass6a398d22011-10-24 18:00:07 +0000366 case STATE_SEND_WRQ:
wdenkfe8c2802002-11-03 00:38:21 +0000367 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200368 s = (ushort *)pkt;
Simon Glass4abd0e22011-10-27 06:24:29 +0000369#ifdef CONFIG_CMD_TFTPPUT
Joe Hershberger64701592015-04-08 01:41:07 -0500370 *s++ = htons(tftp_state == STATE_SEND_RRQ ? TFTP_RRQ :
Simon Glass6a398d22011-10-24 18:00:07 +0000371 TFTP_WRQ);
Simon Glass4abd0e22011-10-27 06:24:29 +0000372#else
373 *s++ = htons(TFTP_RRQ);
374#endif
Wolfgang Denk3135c542005-08-26 01:36:03 +0200375 pkt = (uchar *)s;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000376 strcpy((char *)pkt, tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000377 pkt += strlen(tftp_filename) + 1;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000378 strcpy((char *)pkt, "octet");
wdenkfe8c2802002-11-03 00:38:21 +0000379 pkt += 5 /*strlen("octet")*/ + 1;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000380 strcpy((char *)pkt, "timeout");
wdenk7182b0f2003-10-06 21:55:32 +0000381 pkt += 7 /*strlen("timeout")*/ + 1;
Joe Hershberger64701592015-04-08 01:41:07 -0500382 sprintf((char *)pkt, "%lu", timeout_ms / 1000);
Robin Getz9e0a4d62009-07-23 03:01:03 -0400383 debug("send option \"timeout %s\"\n", (char *)pkt);
wdenk7182b0f2003-10-06 21:55:32 +0000384 pkt += strlen((char *)pkt) + 1;
Robin Getzb3b00ed2009-08-20 10:50:20 -0400385#ifdef CONFIG_TFTP_TSIZE
Joe Hershberger290c8992015-04-08 01:41:02 -0500386 pkt += sprintf((char *)pkt, "tsize%c%u%c",
387 0, net_boot_file_size, 0);
Robin Getzb3b00ed2009-08-20 10:50:20 -0400388#endif
David Updegraff7280da72007-06-11 10:41:07 -0500389 /* try for more effic. blk size */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000390 pkt += sprintf((char *)pkt, "blksize%c%d%c",
Joe Hershberger64701592015-04-08 01:41:07 -0500391 0, tftp_block_size_option, 0);
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200392#ifdef CONFIG_MCAST_TFTP
David Updegraff7280da72007-06-11 10:41:07 -0500393 /* Check all preconditions before even trying the option */
Joe Hershberger64701592015-04-08 01:41:07 -0500394 if (!tftp_mcast_disabled) {
395 tftp_mcast_bitmap = malloc(tftp_mcast_bitmap_size);
396 if (tftp_mcast_bitmap && eth_get_dev()->mcast) {
397 free(tftp_mcast_bitmap);
398 tftp_mcast_bitmap = NULL;
Joe Hershberger3a29e5a2012-05-15 08:59:12 +0000399 pkt += sprintf((char *)pkt, "multicast%c%c",
400 0, 0);
401 }
David Updegraff7280da72007-06-11 10:41:07 -0500402 }
403#endif /* CONFIG_MCAST_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000404 len = pkt - xp;
405 break;
406
wdenk7182b0f2003-10-06 21:55:32 +0000407 case STATE_OACK:
David Updegraff7280da72007-06-11 10:41:07 -0500408#ifdef CONFIG_MCAST_TFTP
Joe Hershberger64701592015-04-08 01:41:07 -0500409 /* My turn! Start at where I need blocks I missed. */
410 if (tftp_mcast_active)
411 tftp_cur_block = ext2_find_next_zero_bit(
412 tftp_mcast_bitmap,
413 tftp_mcast_bitmap_size * 8, 0);
414 /* fall through */
David Updegraff7280da72007-06-11 10:41:07 -0500415#endif
Luca Ceresolib640ed32011-05-17 00:03:39 +0000416
417 case STATE_RECV_WRQ:
David Updegraff7280da72007-06-11 10:41:07 -0500418 case STATE_DATA:
wdenkfe8c2802002-11-03 00:38:21 +0000419 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200420 s = (ushort *)pkt;
Simon Glass6a398d22011-10-24 18:00:07 +0000421 s[0] = htons(TFTP_ACK);
Joe Hershberger64701592015-04-08 01:41:07 -0500422 s[1] = htons(tftp_cur_block);
Simon Glass6a398d22011-10-24 18:00:07 +0000423 pkt = (uchar *)(s + 2);
424#ifdef CONFIG_CMD_TFTPPUT
Joe Hershberger64701592015-04-08 01:41:07 -0500425 if (tftp_put_active) {
426 int toload = tftp_block_size;
427 int loaded = load_block(tftp_cur_block, pkt, toload);
Simon Glass6a398d22011-10-24 18:00:07 +0000428
429 s[0] = htons(TFTP_DATA);
430 pkt += loaded;
Joe Hershberger64701592015-04-08 01:41:07 -0500431 tftp_put_final_block_sent = (loaded < toload);
Simon Glass6a398d22011-10-24 18:00:07 +0000432 }
433#endif
wdenkfe8c2802002-11-03 00:38:21 +0000434 len = pkt - xp;
435 break;
436
437 case STATE_TOO_LARGE:
438 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200439 s = (ushort *)pkt;
440 *s++ = htons(TFTP_ERROR);
Simon Glass6a398d22011-10-24 18:00:07 +0000441 *s++ = htons(3);
442
Wolfgang Denk3135c542005-08-26 01:36:03 +0200443 pkt = (uchar *)s;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000444 strcpy((char *)pkt, "File too large");
wdenkfe8c2802002-11-03 00:38:21 +0000445 pkt += 14 /*strlen("File too large")*/ + 1;
446 len = pkt - xp;
447 break;
448
449 case STATE_BAD_MAGIC:
450 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200451 s = (ushort *)pkt;
452 *s++ = htons(TFTP_ERROR);
453 *s++ = htons(2);
454 pkt = (uchar *)s;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000455 strcpy((char *)pkt, "File has bad magic");
wdenkfe8c2802002-11-03 00:38:21 +0000456 pkt += 18 /*strlen("File has bad magic")*/ + 1;
457 len = pkt - xp;
458 break;
459 }
460
Joe Hershberger64701592015-04-08 01:41:07 -0500461 net_send_udp_packet(net_server_ethaddr, tftp_remote_ip,
462 tftp_remote_port, tftp_our_port, len);
wdenkfe8c2802002-11-03 00:38:21 +0000463}
464
Simon Glass2928cd82011-10-26 14:18:38 +0000465#ifdef CONFIG_CMD_TFTPPUT
Simon Glass6a398d22011-10-24 18:00:07 +0000466static void icmp_handler(unsigned type, unsigned code, unsigned dest,
Joe Hershberger5874dec2015-04-08 01:41:01 -0500467 struct in_addr sip, unsigned src, uchar *pkt,
468 unsigned len)
Simon Glass6a398d22011-10-24 18:00:07 +0000469{
470 if (type == ICMP_NOT_REACH && code == ICMP_NOT_REACH_PORT) {
471 /* Oh dear the other end has gone away */
472 restart("TFTP server died");
473 }
474}
Simon Glass2928cd82011-10-26 14:18:38 +0000475#endif
wdenkfe8c2802002-11-03 00:38:21 +0000476
Joe Hershberger5874dec2015-04-08 01:41:01 -0500477static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
478 unsigned src, unsigned len)
wdenkfe8c2802002-11-03 00:38:21 +0000479{
Kim Phillips878dad02013-01-16 18:09:19 -0600480 __be16 proto;
481 __be16 *s;
Wolfgang Denk83e8e472007-08-30 14:42:15 +0200482 int i;
wdenkfe8c2802002-11-03 00:38:21 +0000483
Joe Hershberger64701592015-04-08 01:41:07 -0500484 if (dest != tftp_our_port) {
David Updegraff7280da72007-06-11 10:41:07 -0500485#ifdef CONFIG_MCAST_TFTP
Joe Hershberger64701592015-04-08 01:41:07 -0500486 if (tftp_mcast_active &&
487 (!tftp_mcast_port || dest != tftp_mcast_port))
David Updegraff7280da72007-06-11 10:41:07 -0500488#endif
Luca Ceresoli09b18232011-05-14 05:50:02 +0000489 return;
wdenkfe8c2802002-11-03 00:38:21 +0000490 }
Joe Hershberger64701592015-04-08 01:41:07 -0500491 if (tftp_state != STATE_SEND_RRQ && src != tftp_remote_port &&
492 tftp_state != STATE_RECV_WRQ && tftp_state != STATE_SEND_WRQ)
wdenkfe8c2802002-11-03 00:38:21 +0000493 return;
wdenkfe8c2802002-11-03 00:38:21 +0000494
Luca Ceresoli35dfb182011-05-14 05:50:00 +0000495 if (len < 2)
wdenkfe8c2802002-11-03 00:38:21 +0000496 return;
wdenkfe8c2802002-11-03 00:38:21 +0000497 len -= 2;
498 /* warning: don't use increment (++) in ntohs() macros!! */
Kim Phillips878dad02013-01-16 18:09:19 -0600499 s = (__be16 *)pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200500 proto = *s++;
501 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000502 switch (ntohs(proto)) {
wdenkfe8c2802002-11-03 00:38:21 +0000503 case TFTP_RRQ:
Simon Glass6a398d22011-10-24 18:00:07 +0000504 break;
505
wdenkfe8c2802002-11-03 00:38:21 +0000506 case TFTP_ACK:
Simon Glass6a398d22011-10-24 18:00:07 +0000507#ifdef CONFIG_CMD_TFTPPUT
Joe Hershberger64701592015-04-08 01:41:07 -0500508 if (tftp_put_active) {
509 if (tftp_put_final_block_sent) {
Simon Glass6a398d22011-10-24 18:00:07 +0000510 tftp_complete();
511 } else {
512 /*
513 * Move to the next block. We want our block
514 * count to wrap just like the other end!
515 */
516 int block = ntohs(*s);
Joe Hershberger64701592015-04-08 01:41:07 -0500517 int ack_ok = (tftp_cur_block == block);
Simon Glass6a398d22011-10-24 18:00:07 +0000518
Joe Hershberger64701592015-04-08 01:41:07 -0500519 tftp_cur_block = (unsigned short)(block + 1);
Simon Glass6a398d22011-10-24 18:00:07 +0000520 update_block_number();
521 if (ack_ok)
Joe Hershberger64701592015-04-08 01:41:07 -0500522 tftp_send(); /* Send next data block */
Simon Glass6a398d22011-10-24 18:00:07 +0000523 }
524 }
525#endif
wdenkfe8c2802002-11-03 00:38:21 +0000526 break;
Simon Glass6a398d22011-10-24 18:00:07 +0000527
wdenkfe8c2802002-11-03 00:38:21 +0000528 default:
529 break;
530
Luca Ceresolib640ed32011-05-17 00:03:39 +0000531#ifdef CONFIG_CMD_TFTPSRV
532 case TFTP_WRQ:
533 debug("Got WRQ\n");
Joe Hershberger5874dec2015-04-08 01:41:01 -0500534 tftp_remote_ip = sip;
Joe Hershberger64701592015-04-08 01:41:07 -0500535 tftp_remote_port = src;
536 tftp_our_port = 1024 + (get_timer(0) % 3072);
Simon Glassbd8a0e02011-10-24 18:00:04 +0000537 new_transfer();
Joe Hershberger64701592015-04-08 01:41:07 -0500538 tftp_send(); /* Send ACK(0) */
Luca Ceresolib640ed32011-05-17 00:03:39 +0000539 break;
540#endif
541
wdenk7182b0f2003-10-06 21:55:32 +0000542 case TFTP_OACK:
Wolfgang Denk12730c72009-08-10 09:59:10 +0200543 debug("Got OACK: %s %s\n",
Joe Hershberger64701592015-04-08 01:41:07 -0500544 pkt, pkt + strlen((char *)pkt) + 1);
545 tftp_state = STATE_OACK;
546 tftp_remote_port = src;
Wolfgang Denk8b70f342007-08-31 10:01:51 +0200547 /*
548 * Check for 'blksize' option.
549 * Careful: "i" is signed, "len" is unsigned, thus
550 * something like "len-8" may give a *huge* number
551 */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000552 for (i = 0; i+8 < len; i++) {
Joe Hershberger64701592015-04-08 01:41:07 -0500553 if (strcmp((char *)pkt + i, "blksize") == 0) {
554 tftp_block_size = (unsigned short)
555 simple_strtoul((char *)pkt + i + 8,
556 NULL, 10);
Robin Getz9e0a4d62009-07-23 03:01:03 -0400557 debug("Blocksize ack: %s, %d\n",
Joe Hershberger64701592015-04-08 01:41:07 -0500558 (char *)pkt + i + 8, tftp_block_size);
Wolfgang Denk83e8e472007-08-30 14:42:15 +0200559 }
Robin Getzb3b00ed2009-08-20 10:50:20 -0400560#ifdef CONFIG_TFTP_TSIZE
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000561 if (strcmp((char *)pkt+i, "tsize") == 0) {
Joe Hershberger64701592015-04-08 01:41:07 -0500562 tftp_tsize = simple_strtoul((char *)pkt + i + 6,
Luca Ceresolic42fc272011-05-14 05:49:56 +0000563 NULL, 10);
Robin Getzb3b00ed2009-08-20 10:50:20 -0400564 debug("size = %s, %d\n",
Joe Hershberger64701592015-04-08 01:41:07 -0500565 (char *)pkt + i + 6, tftp_tsize);
Robin Getzb3b00ed2009-08-20 10:50:20 -0400566 }
567#endif
David Updegraff7280da72007-06-11 10:41:07 -0500568 }
569#ifdef CONFIG_MCAST_TFTP
Joe Hershberger64701592015-04-08 01:41:07 -0500570 parse_multicast_oack((char *)pkt, len - 1);
571 if ((tftp_mcast_active) && (!tftp_mcast_master_client))
572 tftp_state = STATE_DATA; /* passive.. */
David Updegraff7280da72007-06-11 10:41:07 -0500573 else
574#endif
Simon Glass6a398d22011-10-24 18:00:07 +0000575#ifdef CONFIG_CMD_TFTPPUT
Joe Hershberger64701592015-04-08 01:41:07 -0500576 if (tftp_put_active) {
Simon Glass6a398d22011-10-24 18:00:07 +0000577 /* Get ready to send the first block */
Joe Hershberger64701592015-04-08 01:41:07 -0500578 tftp_state = STATE_DATA;
579 tftp_cur_block++;
Simon Glass6a398d22011-10-24 18:00:07 +0000580 }
581#endif
Joe Hershberger64701592015-04-08 01:41:07 -0500582 tftp_send(); /* Send ACK or first data block */
wdenk7182b0f2003-10-06 21:55:32 +0000583 break;
wdenkfe8c2802002-11-03 00:38:21 +0000584 case TFTP_DATA:
585 if (len < 2)
586 return;
587 len -= 2;
Joe Hershberger64701592015-04-08 01:41:07 -0500588 tftp_cur_block = ntohs(*(__be16 *)pkt);
wdenkef893942004-02-23 16:11:30 +0000589
Simon Glassbd8a0e02011-10-24 18:00:04 +0000590 update_block_number();
wdenkfe8c2802002-11-03 00:38:21 +0000591
Joe Hershberger64701592015-04-08 01:41:07 -0500592 if (tftp_state == STATE_SEND_RRQ)
Robin Getz9e0a4d62009-07-23 03:01:03 -0400593 debug("Server did not acknowledge timeout option!\n");
wdenk7182b0f2003-10-06 21:55:32 +0000594
Joe Hershberger64701592015-04-08 01:41:07 -0500595 if (tftp_state == STATE_SEND_RRQ || tftp_state == STATE_OACK ||
596 tftp_state == STATE_RECV_WRQ) {
wdenkef893942004-02-23 16:11:30 +0000597 /* first block received */
Joe Hershberger64701592015-04-08 01:41:07 -0500598 tftp_state = STATE_DATA;
599 tftp_remote_port = src;
Simon Glassbd8a0e02011-10-24 18:00:04 +0000600 new_transfer();
wdenkfe8c2802002-11-03 00:38:21 +0000601
David Updegraff7280da72007-06-11 10:41:07 -0500602#ifdef CONFIG_MCAST_TFTP
Joe Hershberger64701592015-04-08 01:41:07 -0500603 if (tftp_mcast_active) { /* start!=1 common if mcast */
604 tftp_prev_block = tftp_cur_block - 1;
David Updegraff7280da72007-06-11 10:41:07 -0500605 } else
606#endif
Joe Hershberger64701592015-04-08 01:41:07 -0500607 if (tftp_cur_block != 1) { /* Assertion */
608 puts("\nTFTP error: ");
609 printf("First block is not block 1 (%ld)\n",
610 tftp_cur_block);
611 puts("Starting again\n\n");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500612 net_start_again();
wdenkfe8c2802002-11-03 00:38:21 +0000613 break;
614 }
615 }
616
Joe Hershberger64701592015-04-08 01:41:07 -0500617 if (tftp_cur_block == tftp_prev_block) {
618 /* Same block again; ignore it. */
wdenkfe8c2802002-11-03 00:38:21 +0000619 break;
620 }
621
Joe Hershberger64701592015-04-08 01:41:07 -0500622 tftp_prev_block = tftp_cur_block;
Albert ARIBAUD \(3ADEV\)83006852015-10-12 00:02:57 +0200623 timeout_count_max = tftp_timeout_count_max;
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500624 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
wdenkfe8c2802002-11-03 00:38:21 +0000625
Simon Goldschmidta96cf8f2019-01-14 22:38:22 +0100626 if (store_block(tftp_cur_block - 1, pkt + 2, len)) {
627 eth_halt();
628 net_set_state(NETLOOP_FAIL);
629 break;
630 }
wdenkfe8c2802002-11-03 00:38:21 +0000631
632 /*
Luca Ceresoli2f3dc0a2011-05-17 00:03:41 +0000633 * Acknowledge the block just received, which will prompt
Luca Ceresoli5079c762011-05-17 00:03:37 +0000634 * the remote for the next one.
wdenkfe8c2802002-11-03 00:38:21 +0000635 */
David Updegraff7280da72007-06-11 10:41:07 -0500636#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200637 /* if I am the MasterClient, actively calculate what my next
638 * needed block is; else I'm passive; not ACKING
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100639 */
Joe Hershberger64701592015-04-08 01:41:07 -0500640 if (tftp_mcast_active) {
641 if (len < tftp_block_size) {
642 tftp_mcast_ending_block = tftp_cur_block;
643 } else if (tftp_mcast_master_client) {
644 tftp_mcast_prev_hole = ext2_find_next_zero_bit(
645 tftp_mcast_bitmap,
646 tftp_mcast_bitmap_size * 8,
647 tftp_mcast_prev_hole);
648 tftp_cur_block = tftp_mcast_prev_hole;
649 if (tftp_cur_block >
650 ((tftp_mcast_bitmap_size * 8) - 1)) {
651 debug("tftpfile too big\n");
David Updegraff7280da72007-06-11 10:41:07 -0500652 /* try to double it and retry */
Joe Hershberger64701592015-04-08 01:41:07 -0500653 tftp_mcast_bitmap_size <<= 1;
David Updegraff7280da72007-06-11 10:41:07 -0500654 mcast_cleanup();
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500655 net_start_again();
David Updegraff7280da72007-06-11 10:41:07 -0500656 return;
657 }
Joe Hershberger64701592015-04-08 01:41:07 -0500658 tftp_prev_block = tftp_cur_block;
David Updegraff7280da72007-06-11 10:41:07 -0500659 }
660 }
661#endif
Joe Hershberger64701592015-04-08 01:41:07 -0500662 tftp_send();
wdenkfe8c2802002-11-03 00:38:21 +0000663
David Updegraff7280da72007-06-11 10:41:07 -0500664#ifdef CONFIG_MCAST_TFTP
Joe Hershberger64701592015-04-08 01:41:07 -0500665 if (tftp_mcast_active) {
666 if (tftp_mcast_master_client &&
667 (tftp_cur_block >= tftp_mcast_ending_block)) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000668 puts("\nMulticast tftp done\n");
David Updegraff7280da72007-06-11 10:41:07 -0500669 mcast_cleanup();
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000670 net_set_state(NETLOOP_SUCCESS);
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200671 }
Joe Hershberger3a29e5a2012-05-15 08:59:12 +0000672 } else
David Updegraff7280da72007-06-11 10:41:07 -0500673#endif
Joe Hershberger64701592015-04-08 01:41:07 -0500674 if (len < tftp_block_size)
Simon Glassb437aad2011-10-24 18:00:03 +0000675 tftp_complete();
wdenkfe8c2802002-11-03 00:38:21 +0000676 break;
677
678 case TFTP_ERROR:
Luca Ceresolif40538a2011-05-14 05:49:57 +0000679 printf("\nTFTP error: '%s' (%d)\n",
Kim Phillips878dad02013-01-16 18:09:19 -0600680 pkt + 2, ntohs(*(__be16 *)pkt));
Remy Bohmerb46293f2009-10-28 22:13:40 +0100681
Kim Phillips878dad02013-01-16 18:09:19 -0600682 switch (ntohs(*(__be16 *)pkt)) {
Remy Bohmerb46293f2009-10-28 22:13:40 +0100683 case TFTP_ERR_FILE_NOT_FOUND:
684 case TFTP_ERR_ACCESS_DENIED:
685 puts("Not retrying...\n");
686 eth_halt();
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000687 net_set_state(NETLOOP_FAIL);
Remy Bohmerb46293f2009-10-28 22:13:40 +0100688 break;
689 case TFTP_ERR_UNDEFINED:
690 case TFTP_ERR_DISK_FULL:
691 case TFTP_ERR_UNEXPECTED_OPCODE:
692 case TFTP_ERR_UNKNOWN_TRANSFER_ID:
693 case TFTP_ERR_FILE_ALREADY_EXISTS:
694 default:
695 puts("Starting again\n\n");
David Updegraff7280da72007-06-11 10:41:07 -0500696#ifdef CONFIG_MCAST_TFTP
Remy Bohmerb46293f2009-10-28 22:13:40 +0100697 mcast_cleanup();
David Updegraff7280da72007-06-11 10:41:07 -0500698#endif
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500699 net_start_again();
Remy Bohmerb46293f2009-10-28 22:13:40 +0100700 break;
701 }
wdenkfe8c2802002-11-03 00:38:21 +0000702 break;
703 }
704}
705
706
Joe Hershberger64701592015-04-08 01:41:07 -0500707static void tftp_timeout_handler(void)
wdenkfe8c2802002-11-03 00:38:21 +0000708{
Joe Hershberger64701592015-04-08 01:41:07 -0500709 if (++timeout_count > timeout_count_max) {
Simon Glassbd8a0e02011-10-24 18:00:04 +0000710 restart("Retry count exceeded");
wdenkfe8c2802002-11-03 00:38:21 +0000711 } else {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000712 puts("T ");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500713 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
Joe Hershberger64701592015-04-08 01:41:07 -0500714 if (tftp_state != STATE_RECV_WRQ)
715 tftp_send();
wdenkfe8c2802002-11-03 00:38:21 +0000716 }
717}
718
Simon Goldschmidta96cf8f2019-01-14 22:38:22 +0100719/* Initialize tftp_load_addr and tftp_load_size from load_addr and lmb */
720static int tftp_init_load_addr(void)
721{
722#ifdef CONFIG_LMB
723 struct lmb lmb;
724 phys_size_t max_size;
725
726 lmb_init_and_reserve(&lmb, gd->bd->bi_dram[0].start,
727 gd->bd->bi_dram[0].size, (void *)gd->fdt_blob);
728
729 max_size = lmb_get_unreserved_size(&lmb, load_addr);
730 if (!max_size)
731 return -1;
732
733 tftp_load_size = max_size;
734#endif
735 tftp_load_addr = load_addr;
736 return 0;
737}
wdenkfe8c2802002-11-03 00:38:21 +0000738
Joe Hershberger64701592015-04-08 01:41:07 -0500739void tftp_start(enum proto_t protocol)
wdenkfe8c2802002-11-03 00:38:21 +0000740{
Albert ARIBAUD \(3ADEV\)83006852015-10-12 00:02:57 +0200741#if CONFIG_NET_TFTP_VARS
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200742 char *ep; /* Environment pointer */
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200743
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100744 /*
745 * Allow the user to choose TFTP blocksize and timeout.
746 * TFTP protocol has a minimal timeout of 1 second.
747 */
Albert ARIBAUD \(3ADEV\)83006852015-10-12 00:02:57 +0200748
Simon Glass64b723f2017-08-03 12:22:12 -0600749 ep = env_get("tftpblocksize");
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000750 if (ep != NULL)
Joe Hershberger64701592015-04-08 01:41:07 -0500751 tftp_block_size_option = simple_strtol(ep, NULL, 10);
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100752
Simon Glass64b723f2017-08-03 12:22:12 -0600753 ep = env_get("tftptimeout");
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000754 if (ep != NULL)
Joe Hershberger64701592015-04-08 01:41:07 -0500755 timeout_ms = simple_strtol(ep, NULL, 10);
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100756
Bin Menge67eaa82015-08-27 22:25:51 -0700757 if (timeout_ms < 1000) {
758 printf("TFTP timeout (%ld ms) too low, set min = 1000 ms\n",
Joe Hershberger64701592015-04-08 01:41:07 -0500759 timeout_ms);
Bin Menge67eaa82015-08-27 22:25:51 -0700760 timeout_ms = 1000;
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100761 }
762
Simon Glass64b723f2017-08-03 12:22:12 -0600763 ep = env_get("tftptimeoutcountmax");
Albert ARIBAUD \(3ADEV\)83006852015-10-12 00:02:57 +0200764 if (ep != NULL)
765 tftp_timeout_count_max = simple_strtol(ep, NULL, 10);
766
767 if (tftp_timeout_count_max < 0) {
768 printf("TFTP timeout count max (%d ms) negative, set to 0\n",
769 tftp_timeout_count_max);
770 tftp_timeout_count_max = 0;
771 }
772#endif
773
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100774 debug("TFTP blocksize = %i, timeout = %ld ms\n",
Joe Hershberger64701592015-04-08 01:41:07 -0500775 tftp_block_size_option, timeout_ms);
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200776
Joe Hershberger5874dec2015-04-08 01:41:01 -0500777 tftp_remote_ip = net_server_ip;
Joe Hershberger9cb7b022018-07-03 19:36:43 -0500778 if (!net_parse_bootfile(&tftp_remote_ip, tftp_filename, MAX_LEN)) {
Matthias Weisser6cc63262011-12-03 03:29:44 +0000779 sprintf(default_filename, "%02X%02X%02X%02X.img",
Joe Hershberger5874dec2015-04-08 01:41:01 -0500780 net_ip.s_addr & 0xFF,
781 (net_ip.s_addr >> 8) & 0xFF,
782 (net_ip.s_addr >> 16) & 0xFF,
783 (net_ip.s_addr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100784
Vladimir Zapolskiy04cc2902017-06-28 22:56:07 +0300785 strncpy(tftp_filename, default_filename, DEFAULT_NAME_LEN);
786 tftp_filename[DEFAULT_NAME_LEN - 1] = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000787
Luca Ceresolif40538a2011-05-14 05:49:57 +0000788 printf("*** Warning: no boot file name; using '%s'\n",
Joe Hershberger64701592015-04-08 01:41:07 -0500789 tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000790 }
791
Luca Ceresolif40538a2011-05-14 05:49:57 +0000792 printf("Using %s device\n", eth_get_name());
Simon Glass6a398d22011-10-24 18:00:07 +0000793 printf("TFTP %s server %pI4; our IP address is %pI4",
Simon Glass4abd0e22011-10-27 06:24:29 +0000794#ifdef CONFIG_CMD_TFTPPUT
795 protocol == TFTPPUT ? "to" : "from",
796#else
Joe Hershberger64701592015-04-08 01:41:07 -0500797 "from",
Simon Glass4abd0e22011-10-27 06:24:29 +0000798#endif
Joe Hershberger64701592015-04-08 01:41:07 -0500799 &tftp_remote_ip, &net_ip);
wdenkfe8c2802002-11-03 00:38:21 +0000800
801 /* Check if we need to send across this subnet */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500802 if (net_gateway.s_addr && net_netmask.s_addr) {
803 struct in_addr our_net;
804 struct in_addr remote_net;
wdenkfe8c2802002-11-03 00:38:21 +0000805
Joe Hershberger5874dec2015-04-08 01:41:01 -0500806 our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
807 remote_net.s_addr = tftp_remote_ip.s_addr & net_netmask.s_addr;
808 if (our_net.s_addr != remote_net.s_addr)
809 printf("; sending through gateway %pI4", &net_gateway);
wdenkfe8c2802002-11-03 00:38:21 +0000810 }
Luca Ceresolif40538a2011-05-14 05:49:57 +0000811 putc('\n');
wdenkfe8c2802002-11-03 00:38:21 +0000812
Luca Ceresolif40538a2011-05-14 05:49:57 +0000813 printf("Filename '%s'.", tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000814
Joe Hershberger290c8992015-04-08 01:41:02 -0500815 if (net_boot_file_expected_size_in_blocks) {
816 printf(" Size is 0x%x Bytes = ",
817 net_boot_file_expected_size_in_blocks << 9);
818 print_size(net_boot_file_expected_size_in_blocks << 9, "");
wdenkfe8c2802002-11-03 00:38:21 +0000819 }
820
Luca Ceresolif40538a2011-05-14 05:49:57 +0000821 putc('\n');
Simon Glass6a398d22011-10-24 18:00:07 +0000822#ifdef CONFIG_CMD_TFTPPUT
Joe Hershberger64701592015-04-08 01:41:07 -0500823 tftp_put_active = (protocol == TFTPPUT);
824 if (tftp_put_active) {
Simon Glass6a398d22011-10-24 18:00:07 +0000825 printf("Save address: 0x%lx\n", save_addr);
826 printf("Save size: 0x%lx\n", save_size);
Joe Hershberger290c8992015-04-08 01:41:02 -0500827 net_boot_file_size = save_size;
Simon Glass6a398d22011-10-24 18:00:07 +0000828 puts("Saving: *\b");
Joe Hershberger64701592015-04-08 01:41:07 -0500829 tftp_state = STATE_SEND_WRQ;
Simon Glass6a398d22011-10-24 18:00:07 +0000830 new_transfer();
831 } else
832#endif
833 {
Simon Goldschmidta96cf8f2019-01-14 22:38:22 +0100834 if (tftp_init_load_addr()) {
835 eth_halt();
836 net_set_state(NETLOOP_FAIL);
837 puts("\nTFTP error: ");
838 puts("trying to overwrite reserved memory...\n");
839 return;
840 }
841 printf("Load address: 0x%lx\n", tftp_load_addr);
Simon Glass6a398d22011-10-24 18:00:07 +0000842 puts("Loading: *\b");
Joe Hershberger64701592015-04-08 01:41:07 -0500843 tftp_state = STATE_SEND_RRQ;
Jörg Krause6f341f52017-09-15 22:16:48 +0200844#ifdef CONFIG_CMD_BOOTEFI
Alexander Graf94c4b992016-05-06 21:01:01 +0200845 efi_set_bootdev("Net", "", tftp_filename);
Jörg Krause6f341f52017-09-15 22:16:48 +0200846#endif
Simon Glass6a398d22011-10-24 18:00:07 +0000847 }
wdenkfe8c2802002-11-03 00:38:21 +0000848
Simon Glasscab9a8d2012-10-11 13:57:36 +0000849 time_start = get_timer(0);
Joe Hershberger64701592015-04-08 01:41:07 -0500850 timeout_count_max = tftp_timeout_count_max;
Bartlomiej Sieka371cde72008-10-01 15:26:29 +0200851
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500852 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
Joe Hershberger5874dec2015-04-08 01:41:01 -0500853 net_set_udp_handler(tftp_handler);
Simon Glass2928cd82011-10-26 14:18:38 +0000854#ifdef CONFIG_CMD_TFTPPUT
Simon Glass6a398d22011-10-24 18:00:07 +0000855 net_set_icmp_handler(icmp_handler);
Simon Glass2928cd82011-10-26 14:18:38 +0000856#endif
Joe Hershberger64701592015-04-08 01:41:07 -0500857 tftp_remote_port = WELL_KNOWN_PORT;
858 timeout_count = 0;
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200859 /* Use a pseudo-random port unless a specific port is set */
Joe Hershberger64701592015-04-08 01:41:07 -0500860 tftp_our_port = 1024 + (get_timer(0) % 3072);
David Updegraff7280da72007-06-11 10:41:07 -0500861
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200862#ifdef CONFIG_TFTP_PORT
Simon Glass64b723f2017-08-03 12:22:12 -0600863 ep = env_get("tftpdstp");
Luca Ceresoli35dfb182011-05-14 05:50:00 +0000864 if (ep != NULL)
Joe Hershberger64701592015-04-08 01:41:07 -0500865 tftp_remote_port = simple_strtol(ep, NULL, 10);
Simon Glass64b723f2017-08-03 12:22:12 -0600866 ep = env_get("tftpsrcp");
Luca Ceresoli35dfb182011-05-14 05:50:00 +0000867 if (ep != NULL)
Joe Hershberger64701592015-04-08 01:41:07 -0500868 tftp_our_port = simple_strtol(ep, NULL, 10);
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200869#endif
Joe Hershberger64701592015-04-08 01:41:07 -0500870 tftp_cur_block = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000871
wdenke6466f62003-06-05 19:27:42 +0000872 /* zero out server ether in case the server ip has changed */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500873 memset(net_server_ethaddr, 0, 6);
Joe Hershberger64701592015-04-08 01:41:07 -0500874 /* Revert tftp_block_size to dflt */
875 tftp_block_size = TFTP_BLOCK_SIZE;
David Updegraff7280da72007-06-11 10:41:07 -0500876#ifdef CONFIG_MCAST_TFTP
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100877 mcast_cleanup();
David Updegraff7280da72007-06-11 10:41:07 -0500878#endif
Robin Getzb3b00ed2009-08-20 10:50:20 -0400879#ifdef CONFIG_TFTP_TSIZE
Joe Hershberger64701592015-04-08 01:41:07 -0500880 tftp_tsize = 0;
881 tftp_tsize_num_hash = 0;
Robin Getzb3b00ed2009-08-20 10:50:20 -0400882#endif
wdenke6466f62003-06-05 19:27:42 +0000883
Joe Hershberger64701592015-04-08 01:41:07 -0500884 tftp_send();
wdenkfe8c2802002-11-03 00:38:21 +0000885}
Luca Ceresolib640ed32011-05-17 00:03:39 +0000886
887#ifdef CONFIG_CMD_TFTPSRV
Joe Hershberger64701592015-04-08 01:41:07 -0500888void tftp_start_server(void)
Luca Ceresolib640ed32011-05-17 00:03:39 +0000889{
890 tftp_filename[0] = 0;
891
Simon Goldschmidta96cf8f2019-01-14 22:38:22 +0100892 if (tftp_init_load_addr()) {
893 eth_halt();
894 net_set_state(NETLOOP_FAIL);
895 puts("\nTFTP error: trying to overwrite reserved memory...\n");
896 return;
897 }
Luca Ceresolib640ed32011-05-17 00:03:39 +0000898 printf("Using %s device\n", eth_get_name());
Joe Hershberger5874dec2015-04-08 01:41:01 -0500899 printf("Listening for TFTP transfer on %pI4\n", &net_ip);
Simon Goldschmidta96cf8f2019-01-14 22:38:22 +0100900 printf("Load address: 0x%lx\n", tftp_load_addr);
Luca Ceresolib640ed32011-05-17 00:03:39 +0000901
902 puts("Loading: *\b");
903
Albert ARIBAUD \(3ADEV\)83006852015-10-12 00:02:57 +0200904 timeout_count_max = tftp_timeout_count_max;
Joe Hershberger64701592015-04-08 01:41:07 -0500905 timeout_count = 0;
906 timeout_ms = TIMEOUT;
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500907 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
Luca Ceresolib640ed32011-05-17 00:03:39 +0000908
Joe Hershberger64701592015-04-08 01:41:07 -0500909 /* Revert tftp_block_size to dflt */
910 tftp_block_size = TFTP_BLOCK_SIZE;
911 tftp_cur_block = 0;
912 tftp_our_port = WELL_KNOWN_PORT;
Luca Ceresolib640ed32011-05-17 00:03:39 +0000913
914#ifdef CONFIG_TFTP_TSIZE
Joe Hershberger64701592015-04-08 01:41:07 -0500915 tftp_tsize = 0;
916 tftp_tsize_num_hash = 0;
Luca Ceresolib640ed32011-05-17 00:03:39 +0000917#endif
918
Joe Hershberger64701592015-04-08 01:41:07 -0500919 tftp_state = STATE_RECV_WRQ;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500920 net_set_udp_handler(tftp_handler);
Andrew Ruder91393c52013-10-22 19:10:28 -0500921
922 /* zero out server ether in case the server ip has changed */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500923 memset(net_server_ethaddr, 0, 6);
Luca Ceresolib640ed32011-05-17 00:03:39 +0000924}
925#endif /* CONFIG_CMD_TFTPSRV */
wdenkfe8c2802002-11-03 00:38:21 +0000926
David Updegraff7280da72007-06-11 10:41:07 -0500927#ifdef CONFIG_MCAST_TFTP
Joe Hershberger64701592015-04-08 01:41:07 -0500928/*
929 * Credits: atftp project.
David Updegraff7280da72007-06-11 10:41:07 -0500930 */
931
Joe Hershberger64701592015-04-08 01:41:07 -0500932/*
933 * Pick up BcastAddr, Port, and whether I am [now] the master-client.
David Updegraff7280da72007-06-11 10:41:07 -0500934 * Frame:
935 * +-------+-----------+---+-------~~-------+---+
936 * | opc | multicast | 0 | addr, port, mc | 0 |
937 * +-------+-----------+---+-------~~-------+---+
938 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
939 * I am the new master-client so must send ACKs to DataBlocks. If I am not
940 * master-client, I'm a passive client, gathering what DataBlocks I may and
941 * making note of which ones I got in my bitmask.
942 * In theory, I never go from master->passive..
943 * .. this comes in with pkt already pointing just past opc
944 */
945static void parse_multicast_oack(char *pkt, int len)
946{
Luca Ceresolif40538a2011-05-14 05:49:57 +0000947 int i;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500948 struct in_addr addr;
Joe Hershberger64701592015-04-08 01:41:07 -0500949 char *mc_adr;
950 char *port;
951 char *mc;
David Updegraff7280da72007-06-11 10:41:07 -0500952
Joe Hershberger64701592015-04-08 01:41:07 -0500953 mc_adr = NULL;
954 port = NULL;
955 mc = NULL;
David Updegraff7280da72007-06-11 10:41:07 -0500956 /* march along looking for 'multicast\0', which has to start at least
957 * 14 bytes back from the end.
958 */
Joe Hershberger64701592015-04-08 01:41:07 -0500959 for (i = 0; i < len - 14; i++)
960 if (strcmp(pkt + i, "multicast") == 0)
David Updegraff7280da72007-06-11 10:41:07 -0500961 break;
Joe Hershberger64701592015-04-08 01:41:07 -0500962 if (i >= (len - 14)) /* non-Multicast OACK, ign. */
David Updegraff7280da72007-06-11 10:41:07 -0500963 return;
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200964
Luca Ceresolif40538a2011-05-14 05:49:57 +0000965 i += 10; /* strlen multicast */
Joe Hershberger64701592015-04-08 01:41:07 -0500966 mc_adr = pkt + i;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000967 for (; i < len; i++) {
Joe Hershberger64701592015-04-08 01:41:07 -0500968 if (*(pkt + i) == ',') {
969 *(pkt + i) = '\0';
David Updegraff7280da72007-06-11 10:41:07 -0500970 if (port) {
Joe Hershberger64701592015-04-08 01:41:07 -0500971 mc = pkt + i + 1;
David Updegraff7280da72007-06-11 10:41:07 -0500972 break;
973 } else {
Joe Hershberger64701592015-04-08 01:41:07 -0500974 port = pkt + i + 1;
David Updegraff7280da72007-06-11 10:41:07 -0500975 }
976 }
977 }
Luca Ceresoli6072fcd2011-05-14 05:50:01 +0000978 if (!port || !mc_adr || !mc)
979 return;
Joe Hershberger64701592015-04-08 01:41:07 -0500980 if (tftp_mcast_active && tftp_mcast_master_client) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000981 printf("I got a OACK as master Client, WRONG!\n");
David Updegraff7280da72007-06-11 10:41:07 -0500982 return;
983 }
984 /* ..I now accept packets destined for this MCAST addr, port */
Joe Hershberger64701592015-04-08 01:41:07 -0500985 if (!tftp_mcast_active) {
986 if (tftp_mcast_bitmap) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000987 printf("Internal failure! no mcast.\n");
Joe Hershberger64701592015-04-08 01:41:07 -0500988 free(tftp_mcast_bitmap);
989 tftp_mcast_bitmap = NULL;
990 tftp_mcast_disabled = 1;
991 return;
David Updegraff7280da72007-06-11 10:41:07 -0500992 }
993 /* I malloc instead of pre-declare; so that if the file ends
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200994 * up being too big for this bitmap I can retry
995 */
Joe Hershberger64701592015-04-08 01:41:07 -0500996 tftp_mcast_bitmap = malloc(tftp_mcast_bitmap_size);
997 if (!tftp_mcast_bitmap) {
998 printf("No bitmap, no multicast. Sorry.\n");
999 tftp_mcast_disabled = 1;
David Updegraff7280da72007-06-11 10:41:07 -05001000 return;
1001 }
Joe Hershberger64701592015-04-08 01:41:07 -05001002 memset(tftp_mcast_bitmap, 0, tftp_mcast_bitmap_size);
1003 tftp_mcast_prev_hole = 0;
1004 tftp_mcast_active = 1;
David Updegraff7280da72007-06-11 10:41:07 -05001005 }
1006 addr = string_to_ip(mc_adr);
Joe Hershberger5874dec2015-04-08 01:41:01 -05001007 if (net_mcast_addr.s_addr != addr.s_addr) {
1008 if (net_mcast_addr.s_addr)
1009 eth_mcast_join(net_mcast_addr, 0);
1010 net_mcast_addr = addr;
1011 if (eth_mcast_join(net_mcast_addr, 1)) {
Luca Ceresolif40538a2011-05-14 05:49:57 +00001012 printf("Fail to set mcast, revert to TFTP\n");
Joe Hershberger64701592015-04-08 01:41:07 -05001013 tftp_mcast_disabled = 1;
David Updegraff7280da72007-06-11 10:41:07 -05001014 mcast_cleanup();
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001015 net_start_again();
David Updegraff7280da72007-06-11 10:41:07 -05001016 }
1017 }
Joe Hershberger64701592015-04-08 01:41:07 -05001018 tftp_mcast_master_client = simple_strtoul((char *)mc, NULL, 10);
1019 tftp_mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
1020 printf("Multicast: %s:%d [%d]\n", mc_adr, tftp_mcast_port,
1021 tftp_mcast_master_client);
David Updegraff7280da72007-06-11 10:41:07 -05001022 return;
1023}
1024
1025#endif /* Multicast TFTP */