blob: a5ed8c5d0a3507b5f8dc270aae53194db4c0deb7 [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
Luca Ceresolic42fc272011-05-14 05:49:56 +000020/* Well known TFTP port # */
21#define WELL_KNOWN_PORT 69
22/* Millisecs to timeout for lost pkt */
Bin Menge67eaa82015-08-27 22:25:51 -070023#define TIMEOUT 5000UL
wdenkfe8c2802002-11-03 00:38:21 +000024#ifndef CONFIG_NET_RETRY_COUNT
Luca Ceresolic42fc272011-05-14 05:49:56 +000025/* # of timeouts before giving up */
Bin Menge67eaa82015-08-27 22:25:51 -070026# define TIMEOUT_COUNT 10
wdenkfe8c2802002-11-03 00:38:21 +000027#else
28# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
29#endif
Luca Ceresolic42fc272011-05-14 05:49:56 +000030/* Number of "loading" hashes per line (for checking the image size) */
31#define HASHES_PER_LINE 65
wdenkfe8c2802002-11-03 00:38:21 +000032
33/*
34 * TFTP operations.
35 */
36#define TFTP_RRQ 1
37#define TFTP_WRQ 2
38#define TFTP_DATA 3
39#define TFTP_ACK 4
40#define TFTP_ERROR 5
wdenk7182b0f2003-10-06 21:55:32 +000041#define TFTP_OACK 6
wdenkfe8c2802002-11-03 00:38:21 +000042
Joe Hershberger64701592015-04-08 01:41:07 -050043static ulong timeout_ms = TIMEOUT;
44static int timeout_count_max = TIMEOUT_COUNT;
Simon Glasscab9a8d2012-10-11 13:57:36 +000045static ulong time_start; /* Record time we started tftp */
Bartlomiej Sieka371cde72008-10-01 15:26:29 +020046
47/*
48 * These globals govern the timeout behavior when attempting a connection to a
Joe Hershberger64701592015-04-08 01:41:07 -050049 * TFTP server. tftp_timeout_ms specifies the number of milliseconds to
Bartlomiej Sieka371cde72008-10-01 15:26:29 +020050 * wait for the server to respond to initial connection. Second global,
Joe Hershberger64701592015-04-08 01:41:07 -050051 * tftp_timeout_count_max, gives the number of such connection retries.
52 * tftp_timeout_count_max must be non-negative and tftp_timeout_ms must be
Bartlomiej Sieka371cde72008-10-01 15:26:29 +020053 * positive. The globals are meant to be set (and restored) by code needing
54 * non-standard timeout behavior when initiating a TFTP transfer.
55 */
Joe Hershberger64701592015-04-08 01:41:07 -050056ulong tftp_timeout_ms = TIMEOUT;
57int tftp_timeout_count_max = TIMEOUT_COUNT;
Bartlomiej Sieka371cde72008-10-01 15:26:29 +020058
Remy Bohmerb46293f2009-10-28 22:13:40 +010059enum {
60 TFTP_ERR_UNDEFINED = 0,
61 TFTP_ERR_FILE_NOT_FOUND = 1,
62 TFTP_ERR_ACCESS_DENIED = 2,
63 TFTP_ERR_DISK_FULL = 3,
64 TFTP_ERR_UNEXPECTED_OPCODE = 4,
65 TFTP_ERR_UNKNOWN_TRANSFER_ID = 5,
66 TFTP_ERR_FILE_ALREADY_EXISTS = 6,
67};
68
Joe Hershberger5874dec2015-04-08 01:41:01 -050069static struct in_addr tftp_remote_ip;
Luca Ceresolic42fc272011-05-14 05:49:56 +000070/* The UDP port at their end */
Joe Hershberger64701592015-04-08 01:41:07 -050071static int tftp_remote_port;
Luca Ceresolic42fc272011-05-14 05:49:56 +000072/* The UDP port at our end */
Joe Hershberger64701592015-04-08 01:41:07 -050073static int tftp_our_port;
74static int timeout_count;
Luca Ceresolic42fc272011-05-14 05:49:56 +000075/* packet sequence number */
Joe Hershberger64701592015-04-08 01:41:07 -050076static ulong tftp_cur_block;
Luca Ceresolic42fc272011-05-14 05:49:56 +000077/* last packet sequence number received */
Joe Hershberger64701592015-04-08 01:41:07 -050078static ulong tftp_prev_block;
Luca Ceresolic42fc272011-05-14 05:49:56 +000079/* count of sequence number wraparounds */
Joe Hershberger64701592015-04-08 01:41:07 -050080static ulong tftp_block_wrap;
Luca Ceresolic42fc272011-05-14 05:49:56 +000081/* memory offset due to wrapping */
Joe Hershberger64701592015-04-08 01:41:07 -050082static ulong tftp_block_wrap_offset;
83static int tftp_state;
Robin Getzb3b00ed2009-08-20 10:50:20 -040084#ifdef CONFIG_TFTP_TSIZE
Luca Ceresolic42fc272011-05-14 05:49:56 +000085/* The file size reported by the server */
Joe Hershberger64701592015-04-08 01:41:07 -050086static int tftp_tsize;
Luca Ceresolic42fc272011-05-14 05:49:56 +000087/* The number of hashes we printed */
Joe Hershberger64701592015-04-08 01:41:07 -050088static short tftp_tsize_num_hash;
Robin Getzb3b00ed2009-08-20 10:50:20 -040089#endif
Simon Glass6a398d22011-10-24 18:00:07 +000090#ifdef CONFIG_CMD_TFTPPUT
Joe Hershberger64701592015-04-08 01:41:07 -050091/* 1 if writing, else 0 */
92static int tftp_put_active;
93/* 1 if we have sent the last block */
94static int tftp_put_final_block_sent;
Simon Glass6a398d22011-10-24 18:00:07 +000095#else
Joe Hershberger64701592015-04-08 01:41:07 -050096#define tftp_put_active 0
Simon Glass6a398d22011-10-24 18:00:07 +000097#endif
wdenkef893942004-02-23 16:11:30 +000098
Luca Ceresoli562dfe52011-05-17 00:03:38 +000099#define STATE_SEND_RRQ 1
wdenkfe8c2802002-11-03 00:38:21 +0000100#define STATE_DATA 2
101#define STATE_TOO_LARGE 3
102#define STATE_BAD_MAGIC 4
wdenk7182b0f2003-10-06 21:55:32 +0000103#define STATE_OACK 5
Luca Ceresolib640ed32011-05-17 00:03:39 +0000104#define STATE_RECV_WRQ 6
Simon Glass6a398d22011-10-24 18:00:07 +0000105#define STATE_SEND_WRQ 7
wdenkfe8c2802002-11-03 00:38:21 +0000106
Luca Ceresolic42fc272011-05-14 05:49:56 +0000107/* default TFTP block size */
108#define TFTP_BLOCK_SIZE 512
109/* sequence number is 16 bit */
110#define TFTP_SEQUENCE_SIZE ((ulong)(1<<16))
wdenkef893942004-02-23 16:11:30 +0000111
wdenkfe8c2802002-11-03 00:38:21 +0000112#define DEFAULT_NAME_LEN (8 + 4 + 1)
113static char default_filename[DEFAULT_NAME_LEN];
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100114
115#ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
116#define MAX_LEN 128
117#else
118#define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
119#endif
120
121static char tftp_filename[MAX_LEN];
wdenkfe8c2802002-11-03 00:38:21 +0000122
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200123/* 512 is poor choice for ethernet, MTU is typically 1500.
124 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
David Updegraff7280da72007-06-11 10:41:07 -0500125 * almost-MTU block sizes. At least try... fall back to 512 if need be.
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200126 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
David Updegraff7280da72007-06-11 10:41:07 -0500127 */
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200128#ifdef CONFIG_TFTP_BLOCKSIZE
129#define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
130#else
David Updegraff7280da72007-06-11 10:41:07 -0500131#define TFTP_MTU_BLOCKSIZE 1468
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200132#endif
133
Joe Hershberger64701592015-04-08 01:41:07 -0500134static unsigned short tftp_block_size = TFTP_BLOCK_SIZE;
135static unsigned short tftp_block_size_option = TFTP_MTU_BLOCKSIZE;
David Updegraff7280da72007-06-11 10:41:07 -0500136
137#ifdef CONFIG_MCAST_TFTP
138#include <malloc.h>
139#define MTFTP_BITMAPSIZE 0x1000
Joe Hershberger64701592015-04-08 01:41:07 -0500140static unsigned *tftp_mcast_bitmap;
141static int tftp_mcast_prev_hole;
142static int tftp_mcast_bitmap_size = MTFTP_BITMAPSIZE;
143static int tftp_mcast_disabled;
144static int tftp_mcast_master_client;
145static int tftp_mcast_active;
146static int tftp_mcast_port;
147/* can get 'last' block before done..*/
148static ulong tftp_mcast_ending_block;
David Updegraff7280da72007-06-11 10:41:07 -0500149
Luca Ceresolif40538a2011-05-14 05:49:57 +0000150static void parse_multicast_oack(char *pkt, int len);
David Updegraff7280da72007-06-11 10:41:07 -0500151
Joe Hershberger64701592015-04-08 01:41:07 -0500152static void mcast_cleanup(void)
David Updegraff7280da72007-06-11 10:41:07 -0500153{
Joe Hershberger5874dec2015-04-08 01:41:01 -0500154 if (net_mcast_addr)
155 eth_mcast_join(net_mcast_addr, 0);
Joe Hershberger64701592015-04-08 01:41:07 -0500156 if (tftp_mcast_bitmap)
157 free(tftp_mcast_bitmap);
158 tftp_mcast_bitmap = NULL;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500159 net_mcast_addr.s_addr = 0;
Joe Hershberger64701592015-04-08 01:41:07 -0500160 tftp_mcast_active = 0;
161 tftp_mcast_port = 0;
162 tftp_mcast_ending_block = -1;
David Updegraff7280da72007-06-11 10:41:07 -0500163}
164
165#endif /* CONFIG_MCAST_TFTP */
166
Joe Hershberger64701592015-04-08 01:41:07 -0500167static inline void store_block(int block, uchar *src, unsigned len)
wdenkfe8c2802002-11-03 00:38:21 +0000168{
Joe Hershberger64701592015-04-08 01:41:07 -0500169 ulong offset = block * tftp_block_size + tftp_block_wrap_offset;
wdenkef893942004-02-23 16:11:30 +0000170 ulong newsize = offset + len;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200171#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
wdenkfe8c2802002-11-03 00:38:21 +0000172 int i, rc = 0;
173
Luca Ceresolif40538a2011-05-14 05:49:57 +0000174 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenkfe8c2802002-11-03 00:38:21 +0000175 /* start address in flash? */
Jochen Friedrichd31a59b2008-09-02 11:24:59 +0200176 if (flash_info[i].flash_id == FLASH_UNKNOWN)
177 continue;
wdenkfe8c2802002-11-03 00:38:21 +0000178 if (load_addr + offset >= flash_info[i].start[0]) {
179 rc = 1;
180 break;
181 }
182 }
183
184 if (rc) { /* Flash is destination for this packet */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000185 rc = flash_write((char *)src, (ulong)(load_addr+offset), len);
wdenkfe8c2802002-11-03 00:38:21 +0000186 if (rc) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000187 flash_perror(rc);
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000188 net_set_state(NETLOOP_FAIL);
wdenkfe8c2802002-11-03 00:38:21 +0000189 return;
190 }
Joe Hershberger3a29e5a2012-05-15 08:59:12 +0000191 } else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200192#endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000193 {
Joe Hershbergered191852015-03-22 17:09:08 -0500194 void *ptr = map_sysmem(load_addr + offset, len);
195
196 memcpy(ptr, src, len);
197 unmap_sysmem(ptr);
wdenkfe8c2802002-11-03 00:38:21 +0000198 }
David Updegraff7280da72007-06-11 10:41:07 -0500199#ifdef CONFIG_MCAST_TFTP
Joe Hershberger64701592015-04-08 01:41:07 -0500200 if (tftp_mcast_active)
201 ext2_set_bit(block, tftp_mcast_bitmap);
David Updegraff7280da72007-06-11 10:41:07 -0500202#endif
wdenkfe8c2802002-11-03 00:38:21 +0000203
Joe Hershberger290c8992015-04-08 01:41:02 -0500204 if (net_boot_file_size < newsize)
205 net_boot_file_size = newsize;
wdenkfe8c2802002-11-03 00:38:21 +0000206}
207
Simon Glassbd8a0e02011-10-24 18:00:04 +0000208/* Clear our state ready for a new transfer */
Simon Glass84313322011-10-27 06:24:28 +0000209static void new_transfer(void)
Simon Glassbd8a0e02011-10-24 18:00:04 +0000210{
Joe Hershberger64701592015-04-08 01:41:07 -0500211 tftp_prev_block = 0;
212 tftp_block_wrap = 0;
213 tftp_block_wrap_offset = 0;
Simon Glassbd8a0e02011-10-24 18:00:04 +0000214#ifdef CONFIG_CMD_TFTPPUT
Joe Hershberger64701592015-04-08 01:41:07 -0500215 tftp_put_final_block_sent = 0;
Simon Glassbd8a0e02011-10-24 18:00:04 +0000216#endif
217}
Simon Glass6a398d22011-10-24 18:00:07 +0000218
219#ifdef CONFIG_CMD_TFTPPUT
220/**
221 * Load the next block from memory to be sent over tftp.
222 *
223 * @param block Block number to send
224 * @param dst Destination buffer for data
225 * @param len Number of bytes in block (this one and every other)
226 * @return number of bytes loaded
227 */
228static int load_block(unsigned block, uchar *dst, unsigned len)
229{
230 /* We may want to get the final block from the previous set */
Joe Hershberger64701592015-04-08 01:41:07 -0500231 ulong offset = ((int)block - 1) * len + tftp_block_wrap_offset;
Simon Glass6a398d22011-10-24 18:00:07 +0000232 ulong tosend = len;
233
Joe Hershberger290c8992015-04-08 01:41:02 -0500234 tosend = min(net_boot_file_size - offset, tosend);
Simon Glass6a398d22011-10-24 18:00:07 +0000235 (void)memcpy(dst, (void *)(save_addr + offset), tosend);
236 debug("%s: block=%d, offset=%ld, len=%d, tosend=%ld\n", __func__,
Joe Hershberger64701592015-04-08 01:41:07 -0500237 block, offset, len, tosend);
Simon Glass6a398d22011-10-24 18:00:07 +0000238 return tosend;
239}
240#endif
Simon Glassbd8a0e02011-10-24 18:00:04 +0000241
Joe Hershberger64701592015-04-08 01:41:07 -0500242static void tftp_send(void);
243static void tftp_timeout_handler(void);
wdenkfe8c2802002-11-03 00:38:21 +0000244
245/**********************************************************************/
246
Simon Glassb437aad2011-10-24 18:00:03 +0000247static void show_block_marker(void)
248{
249#ifdef CONFIG_TFTP_TSIZE
Joe Hershberger64701592015-04-08 01:41:07 -0500250 if (tftp_tsize) {
251 ulong pos = tftp_cur_block * tftp_block_size +
252 tftp_block_wrap_offset;
Max Krummenacherdd081cc2015-08-05 17:17:05 +0200253 if (pos > tftp_tsize)
254 pos = tftp_tsize;
Simon Glassb437aad2011-10-24 18:00:03 +0000255
Joe Hershberger64701592015-04-08 01:41:07 -0500256 while (tftp_tsize_num_hash < pos * 50 / tftp_tsize) {
Simon Glassb437aad2011-10-24 18:00:03 +0000257 putc('#');
Joe Hershberger64701592015-04-08 01:41:07 -0500258 tftp_tsize_num_hash++;
Simon Glassb437aad2011-10-24 18:00:03 +0000259 }
Simon Glass6a398d22011-10-24 18:00:07 +0000260 } else
Simon Glassb437aad2011-10-24 18:00:03 +0000261#endif
Simon Glass6a398d22011-10-24 18:00:07 +0000262 {
Joe Hershberger64701592015-04-08 01:41:07 -0500263 if (((tftp_cur_block - 1) % 10) == 0)
Simon Glassb437aad2011-10-24 18:00:03 +0000264 putc('#');
Joe Hershberger64701592015-04-08 01:41:07 -0500265 else if ((tftp_cur_block % (10 * HASHES_PER_LINE)) == 0)
Simon Glassb437aad2011-10-24 18:00:03 +0000266 puts("\n\t ");
267 }
268}
269
Simon Glassbd8a0e02011-10-24 18:00:04 +0000270/**
271 * restart the current transfer due to an error
272 *
273 * @param msg Message to print for user
274 */
275static void restart(const char *msg)
276{
277 printf("\n%s; starting again\n", msg);
278#ifdef CONFIG_MCAST_TFTP
279 mcast_cleanup();
280#endif
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500281 net_start_again();
Simon Glassbd8a0e02011-10-24 18:00:04 +0000282}
283
284/*
285 * Check if the block number has wrapped, and update progress
286 *
287 * TODO: The egregious use of global variables in this file should be tidied.
288 */
289static void update_block_number(void)
290{
291 /*
292 * RFC1350 specifies that the first data packet will
293 * have sequence number 1. If we receive a sequence
294 * number of 0 this means that there was a wrap
295 * around of the (16 bit) counter.
296 */
Joe Hershberger64701592015-04-08 01:41:07 -0500297 if (tftp_cur_block == 0 && tftp_prev_block != 0) {
298 tftp_block_wrap++;
299 tftp_block_wrap_offset += tftp_block_size * TFTP_SEQUENCE_SIZE;
300 timeout_count = 0; /* we've done well, reset the timeout */
Simon Glassbd8a0e02011-10-24 18:00:04 +0000301 } else {
302 show_block_marker();
303 }
304}
305
Simon Glassb437aad2011-10-24 18:00:03 +0000306/* The TFTP get or put is complete */
307static void tftp_complete(void)
308{
309#ifdef CONFIG_TFTP_TSIZE
310 /* Print hash marks for the last packet received */
Joe Hershberger64701592015-04-08 01:41:07 -0500311 while (tftp_tsize && tftp_tsize_num_hash < 49) {
Simon Glassb437aad2011-10-24 18:00:03 +0000312 putc('#');
Joe Hershberger64701592015-04-08 01:41:07 -0500313 tftp_tsize_num_hash++;
Simon Glassb437aad2011-10-24 18:00:03 +0000314 }
Simon Glassc8f0a5c2014-10-10 07:30:21 -0600315 puts(" ");
Joe Hershberger64701592015-04-08 01:41:07 -0500316 print_size(tftp_tsize, "");
Simon Glassb437aad2011-10-24 18:00:03 +0000317#endif
Simon Glasscab9a8d2012-10-11 13:57:36 +0000318 time_start = get_timer(time_start);
319 if (time_start > 0) {
320 puts("\n\t "); /* Line up with "Loading: " */
Joe Hershberger290c8992015-04-08 01:41:02 -0500321 print_size(net_boot_file_size /
Simon Glasscab9a8d2012-10-11 13:57:36 +0000322 time_start * 1000, "/s");
323 }
Simon Glassb437aad2011-10-24 18:00:03 +0000324 puts("\ndone\n");
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000325 net_set_state(NETLOOP_SUCCESS);
Simon Glassb437aad2011-10-24 18:00:03 +0000326}
327
Joe Hershberger64701592015-04-08 01:41:07 -0500328static void tftp_send(void)
wdenkfe8c2802002-11-03 00:38:21 +0000329{
Simon Glass6a398d22011-10-24 18:00:07 +0000330 uchar *pkt;
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000331 uchar *xp;
332 int len = 0;
333 ushort *s;
wdenkfe8c2802002-11-03 00:38:21 +0000334
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200335#ifdef CONFIG_MCAST_TFTP
David Updegraff7280da72007-06-11 10:41:07 -0500336 /* Multicast TFTP.. non-MasterClients do not ACK data. */
Joe Hershberger64701592015-04-08 01:41:07 -0500337 if (tftp_mcast_active && tftp_state == STATE_DATA &&
338 tftp_mcast_master_client == 0)
David Updegraff7280da72007-06-11 10:41:07 -0500339 return;
340#endif
wdenkfe8c2802002-11-03 00:38:21 +0000341 /*
342 * We will always be sending some sort of packet, so
343 * cobble together the packet headers now.
344 */
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500345 pkt = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
wdenkfe8c2802002-11-03 00:38:21 +0000346
Joe Hershberger64701592015-04-08 01:41:07 -0500347 switch (tftp_state) {
Luca Ceresoli562dfe52011-05-17 00:03:38 +0000348 case STATE_SEND_RRQ:
Simon Glass6a398d22011-10-24 18:00:07 +0000349 case STATE_SEND_WRQ:
wdenkfe8c2802002-11-03 00:38:21 +0000350 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200351 s = (ushort *)pkt;
Simon Glass4abd0e22011-10-27 06:24:29 +0000352#ifdef CONFIG_CMD_TFTPPUT
Joe Hershberger64701592015-04-08 01:41:07 -0500353 *s++ = htons(tftp_state == STATE_SEND_RRQ ? TFTP_RRQ :
Simon Glass6a398d22011-10-24 18:00:07 +0000354 TFTP_WRQ);
Simon Glass4abd0e22011-10-27 06:24:29 +0000355#else
356 *s++ = htons(TFTP_RRQ);
357#endif
Wolfgang Denk3135c542005-08-26 01:36:03 +0200358 pkt = (uchar *)s;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000359 strcpy((char *)pkt, tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000360 pkt += strlen(tftp_filename) + 1;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000361 strcpy((char *)pkt, "octet");
wdenkfe8c2802002-11-03 00:38:21 +0000362 pkt += 5 /*strlen("octet")*/ + 1;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000363 strcpy((char *)pkt, "timeout");
wdenk7182b0f2003-10-06 21:55:32 +0000364 pkt += 7 /*strlen("timeout")*/ + 1;
Joe Hershberger64701592015-04-08 01:41:07 -0500365 sprintf((char *)pkt, "%lu", timeout_ms / 1000);
Robin Getz9e0a4d62009-07-23 03:01:03 -0400366 debug("send option \"timeout %s\"\n", (char *)pkt);
wdenk7182b0f2003-10-06 21:55:32 +0000367 pkt += strlen((char *)pkt) + 1;
Robin Getzb3b00ed2009-08-20 10:50:20 -0400368#ifdef CONFIG_TFTP_TSIZE
Joe Hershberger290c8992015-04-08 01:41:02 -0500369 pkt += sprintf((char *)pkt, "tsize%c%u%c",
370 0, net_boot_file_size, 0);
Robin Getzb3b00ed2009-08-20 10:50:20 -0400371#endif
David Updegraff7280da72007-06-11 10:41:07 -0500372 /* try for more effic. blk size */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000373 pkt += sprintf((char *)pkt, "blksize%c%d%c",
Joe Hershberger64701592015-04-08 01:41:07 -0500374 0, tftp_block_size_option, 0);
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200375#ifdef CONFIG_MCAST_TFTP
David Updegraff7280da72007-06-11 10:41:07 -0500376 /* Check all preconditions before even trying the option */
Joe Hershberger64701592015-04-08 01:41:07 -0500377 if (!tftp_mcast_disabled) {
378 tftp_mcast_bitmap = malloc(tftp_mcast_bitmap_size);
379 if (tftp_mcast_bitmap && eth_get_dev()->mcast) {
380 free(tftp_mcast_bitmap);
381 tftp_mcast_bitmap = NULL;
Joe Hershberger3a29e5a2012-05-15 08:59:12 +0000382 pkt += sprintf((char *)pkt, "multicast%c%c",
383 0, 0);
384 }
David Updegraff7280da72007-06-11 10:41:07 -0500385 }
386#endif /* CONFIG_MCAST_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000387 len = pkt - xp;
388 break;
389
wdenk7182b0f2003-10-06 21:55:32 +0000390 case STATE_OACK:
David Updegraff7280da72007-06-11 10:41:07 -0500391#ifdef CONFIG_MCAST_TFTP
Joe Hershberger64701592015-04-08 01:41:07 -0500392 /* My turn! Start at where I need blocks I missed. */
393 if (tftp_mcast_active)
394 tftp_cur_block = ext2_find_next_zero_bit(
395 tftp_mcast_bitmap,
396 tftp_mcast_bitmap_size * 8, 0);
397 /* fall through */
David Updegraff7280da72007-06-11 10:41:07 -0500398#endif
Luca Ceresolib640ed32011-05-17 00:03:39 +0000399
400 case STATE_RECV_WRQ:
David Updegraff7280da72007-06-11 10:41:07 -0500401 case STATE_DATA:
wdenkfe8c2802002-11-03 00:38:21 +0000402 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200403 s = (ushort *)pkt;
Simon Glass6a398d22011-10-24 18:00:07 +0000404 s[0] = htons(TFTP_ACK);
Joe Hershberger64701592015-04-08 01:41:07 -0500405 s[1] = htons(tftp_cur_block);
Simon Glass6a398d22011-10-24 18:00:07 +0000406 pkt = (uchar *)(s + 2);
407#ifdef CONFIG_CMD_TFTPPUT
Joe Hershberger64701592015-04-08 01:41:07 -0500408 if (tftp_put_active) {
409 int toload = tftp_block_size;
410 int loaded = load_block(tftp_cur_block, pkt, toload);
Simon Glass6a398d22011-10-24 18:00:07 +0000411
412 s[0] = htons(TFTP_DATA);
413 pkt += loaded;
Joe Hershberger64701592015-04-08 01:41:07 -0500414 tftp_put_final_block_sent = (loaded < toload);
Simon Glass6a398d22011-10-24 18:00:07 +0000415 }
416#endif
wdenkfe8c2802002-11-03 00:38:21 +0000417 len = pkt - xp;
418 break;
419
420 case STATE_TOO_LARGE:
421 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200422 s = (ushort *)pkt;
423 *s++ = htons(TFTP_ERROR);
Simon Glass6a398d22011-10-24 18:00:07 +0000424 *s++ = htons(3);
425
Wolfgang Denk3135c542005-08-26 01:36:03 +0200426 pkt = (uchar *)s;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000427 strcpy((char *)pkt, "File too large");
wdenkfe8c2802002-11-03 00:38:21 +0000428 pkt += 14 /*strlen("File too large")*/ + 1;
429 len = pkt - xp;
430 break;
431
432 case STATE_BAD_MAGIC:
433 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200434 s = (ushort *)pkt;
435 *s++ = htons(TFTP_ERROR);
436 *s++ = htons(2);
437 pkt = (uchar *)s;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000438 strcpy((char *)pkt, "File has bad magic");
wdenkfe8c2802002-11-03 00:38:21 +0000439 pkt += 18 /*strlen("File has bad magic")*/ + 1;
440 len = pkt - xp;
441 break;
442 }
443
Joe Hershberger64701592015-04-08 01:41:07 -0500444 net_send_udp_packet(net_server_ethaddr, tftp_remote_ip,
445 tftp_remote_port, tftp_our_port, len);
wdenkfe8c2802002-11-03 00:38:21 +0000446}
447
Simon Glass2928cd82011-10-26 14:18:38 +0000448#ifdef CONFIG_CMD_TFTPPUT
Simon Glass6a398d22011-10-24 18:00:07 +0000449static void icmp_handler(unsigned type, unsigned code, unsigned dest,
Joe Hershberger5874dec2015-04-08 01:41:01 -0500450 struct in_addr sip, unsigned src, uchar *pkt,
451 unsigned len)
Simon Glass6a398d22011-10-24 18:00:07 +0000452{
453 if (type == ICMP_NOT_REACH && code == ICMP_NOT_REACH_PORT) {
454 /* Oh dear the other end has gone away */
455 restart("TFTP server died");
456 }
457}
Simon Glass2928cd82011-10-26 14:18:38 +0000458#endif
wdenkfe8c2802002-11-03 00:38:21 +0000459
Joe Hershberger5874dec2015-04-08 01:41:01 -0500460static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
461 unsigned src, unsigned len)
wdenkfe8c2802002-11-03 00:38:21 +0000462{
Kim Phillips878dad02013-01-16 18:09:19 -0600463 __be16 proto;
464 __be16 *s;
Wolfgang Denk83e8e472007-08-30 14:42:15 +0200465 int i;
wdenkfe8c2802002-11-03 00:38:21 +0000466
Joe Hershberger64701592015-04-08 01:41:07 -0500467 if (dest != tftp_our_port) {
David Updegraff7280da72007-06-11 10:41:07 -0500468#ifdef CONFIG_MCAST_TFTP
Joe Hershberger64701592015-04-08 01:41:07 -0500469 if (tftp_mcast_active &&
470 (!tftp_mcast_port || dest != tftp_mcast_port))
David Updegraff7280da72007-06-11 10:41:07 -0500471#endif
Luca Ceresoli09b18232011-05-14 05:50:02 +0000472 return;
wdenkfe8c2802002-11-03 00:38:21 +0000473 }
Joe Hershberger64701592015-04-08 01:41:07 -0500474 if (tftp_state != STATE_SEND_RRQ && src != tftp_remote_port &&
475 tftp_state != STATE_RECV_WRQ && tftp_state != STATE_SEND_WRQ)
wdenkfe8c2802002-11-03 00:38:21 +0000476 return;
wdenkfe8c2802002-11-03 00:38:21 +0000477
Luca Ceresoli35dfb182011-05-14 05:50:00 +0000478 if (len < 2)
wdenkfe8c2802002-11-03 00:38:21 +0000479 return;
wdenkfe8c2802002-11-03 00:38:21 +0000480 len -= 2;
481 /* warning: don't use increment (++) in ntohs() macros!! */
Kim Phillips878dad02013-01-16 18:09:19 -0600482 s = (__be16 *)pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200483 proto = *s++;
484 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000485 switch (ntohs(proto)) {
wdenkfe8c2802002-11-03 00:38:21 +0000486 case TFTP_RRQ:
Simon Glass6a398d22011-10-24 18:00:07 +0000487 break;
488
wdenkfe8c2802002-11-03 00:38:21 +0000489 case TFTP_ACK:
Simon Glass6a398d22011-10-24 18:00:07 +0000490#ifdef CONFIG_CMD_TFTPPUT
Joe Hershberger64701592015-04-08 01:41:07 -0500491 if (tftp_put_active) {
492 if (tftp_put_final_block_sent) {
Simon Glass6a398d22011-10-24 18:00:07 +0000493 tftp_complete();
494 } else {
495 /*
496 * Move to the next block. We want our block
497 * count to wrap just like the other end!
498 */
499 int block = ntohs(*s);
Joe Hershberger64701592015-04-08 01:41:07 -0500500 int ack_ok = (tftp_cur_block == block);
Simon Glass6a398d22011-10-24 18:00:07 +0000501
Joe Hershberger64701592015-04-08 01:41:07 -0500502 tftp_cur_block = (unsigned short)(block + 1);
Simon Glass6a398d22011-10-24 18:00:07 +0000503 update_block_number();
504 if (ack_ok)
Joe Hershberger64701592015-04-08 01:41:07 -0500505 tftp_send(); /* Send next data block */
Simon Glass6a398d22011-10-24 18:00:07 +0000506 }
507 }
508#endif
wdenkfe8c2802002-11-03 00:38:21 +0000509 break;
Simon Glass6a398d22011-10-24 18:00:07 +0000510
wdenkfe8c2802002-11-03 00:38:21 +0000511 default:
512 break;
513
Luca Ceresolib640ed32011-05-17 00:03:39 +0000514#ifdef CONFIG_CMD_TFTPSRV
515 case TFTP_WRQ:
516 debug("Got WRQ\n");
Joe Hershberger5874dec2015-04-08 01:41:01 -0500517 tftp_remote_ip = sip;
Joe Hershberger64701592015-04-08 01:41:07 -0500518 tftp_remote_port = src;
519 tftp_our_port = 1024 + (get_timer(0) % 3072);
Simon Glassbd8a0e02011-10-24 18:00:04 +0000520 new_transfer();
Joe Hershberger64701592015-04-08 01:41:07 -0500521 tftp_send(); /* Send ACK(0) */
Luca Ceresolib640ed32011-05-17 00:03:39 +0000522 break;
523#endif
524
wdenk7182b0f2003-10-06 21:55:32 +0000525 case TFTP_OACK:
Wolfgang Denk12730c72009-08-10 09:59:10 +0200526 debug("Got OACK: %s %s\n",
Joe Hershberger64701592015-04-08 01:41:07 -0500527 pkt, pkt + strlen((char *)pkt) + 1);
528 tftp_state = STATE_OACK;
529 tftp_remote_port = src;
Wolfgang Denk8b70f342007-08-31 10:01:51 +0200530 /*
531 * Check for 'blksize' option.
532 * Careful: "i" is signed, "len" is unsigned, thus
533 * something like "len-8" may give a *huge* number
534 */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000535 for (i = 0; i+8 < len; i++) {
Joe Hershberger64701592015-04-08 01:41:07 -0500536 if (strcmp((char *)pkt + i, "blksize") == 0) {
537 tftp_block_size = (unsigned short)
538 simple_strtoul((char *)pkt + i + 8,
539 NULL, 10);
Robin Getz9e0a4d62009-07-23 03:01:03 -0400540 debug("Blocksize ack: %s, %d\n",
Joe Hershberger64701592015-04-08 01:41:07 -0500541 (char *)pkt + i + 8, tftp_block_size);
Wolfgang Denk83e8e472007-08-30 14:42:15 +0200542 }
Robin Getzb3b00ed2009-08-20 10:50:20 -0400543#ifdef CONFIG_TFTP_TSIZE
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000544 if (strcmp((char *)pkt+i, "tsize") == 0) {
Joe Hershberger64701592015-04-08 01:41:07 -0500545 tftp_tsize = simple_strtoul((char *)pkt + i + 6,
Luca Ceresolic42fc272011-05-14 05:49:56 +0000546 NULL, 10);
Robin Getzb3b00ed2009-08-20 10:50:20 -0400547 debug("size = %s, %d\n",
Joe Hershberger64701592015-04-08 01:41:07 -0500548 (char *)pkt + i + 6, tftp_tsize);
Robin Getzb3b00ed2009-08-20 10:50:20 -0400549 }
550#endif
David Updegraff7280da72007-06-11 10:41:07 -0500551 }
552#ifdef CONFIG_MCAST_TFTP
Joe Hershberger64701592015-04-08 01:41:07 -0500553 parse_multicast_oack((char *)pkt, len - 1);
554 if ((tftp_mcast_active) && (!tftp_mcast_master_client))
555 tftp_state = STATE_DATA; /* passive.. */
David Updegraff7280da72007-06-11 10:41:07 -0500556 else
557#endif
Simon Glass6a398d22011-10-24 18:00:07 +0000558#ifdef CONFIG_CMD_TFTPPUT
Joe Hershberger64701592015-04-08 01:41:07 -0500559 if (tftp_put_active) {
Simon Glass6a398d22011-10-24 18:00:07 +0000560 /* Get ready to send the first block */
Joe Hershberger64701592015-04-08 01:41:07 -0500561 tftp_state = STATE_DATA;
562 tftp_cur_block++;
Simon Glass6a398d22011-10-24 18:00:07 +0000563 }
564#endif
Joe Hershberger64701592015-04-08 01:41:07 -0500565 tftp_send(); /* Send ACK or first data block */
wdenk7182b0f2003-10-06 21:55:32 +0000566 break;
wdenkfe8c2802002-11-03 00:38:21 +0000567 case TFTP_DATA:
568 if (len < 2)
569 return;
570 len -= 2;
Joe Hershberger64701592015-04-08 01:41:07 -0500571 tftp_cur_block = ntohs(*(__be16 *)pkt);
wdenkef893942004-02-23 16:11:30 +0000572
Simon Glassbd8a0e02011-10-24 18:00:04 +0000573 update_block_number();
wdenkfe8c2802002-11-03 00:38:21 +0000574
Joe Hershberger64701592015-04-08 01:41:07 -0500575 if (tftp_state == STATE_SEND_RRQ)
Robin Getz9e0a4d62009-07-23 03:01:03 -0400576 debug("Server did not acknowledge timeout option!\n");
wdenk7182b0f2003-10-06 21:55:32 +0000577
Joe Hershberger64701592015-04-08 01:41:07 -0500578 if (tftp_state == STATE_SEND_RRQ || tftp_state == STATE_OACK ||
579 tftp_state == STATE_RECV_WRQ) {
wdenkef893942004-02-23 16:11:30 +0000580 /* first block received */
Joe Hershberger64701592015-04-08 01:41:07 -0500581 tftp_state = STATE_DATA;
582 tftp_remote_port = src;
Simon Glassbd8a0e02011-10-24 18:00:04 +0000583 new_transfer();
wdenkfe8c2802002-11-03 00:38:21 +0000584
David Updegraff7280da72007-06-11 10:41:07 -0500585#ifdef CONFIG_MCAST_TFTP
Joe Hershberger64701592015-04-08 01:41:07 -0500586 if (tftp_mcast_active) { /* start!=1 common if mcast */
587 tftp_prev_block = tftp_cur_block - 1;
David Updegraff7280da72007-06-11 10:41:07 -0500588 } else
589#endif
Joe Hershberger64701592015-04-08 01:41:07 -0500590 if (tftp_cur_block != 1) { /* Assertion */
591 puts("\nTFTP error: ");
592 printf("First block is not block 1 (%ld)\n",
593 tftp_cur_block);
594 puts("Starting again\n\n");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500595 net_start_again();
wdenkfe8c2802002-11-03 00:38:21 +0000596 break;
597 }
598 }
599
Joe Hershberger64701592015-04-08 01:41:07 -0500600 if (tftp_cur_block == tftp_prev_block) {
601 /* Same block again; ignore it. */
wdenkfe8c2802002-11-03 00:38:21 +0000602 break;
603 }
604
Joe Hershberger64701592015-04-08 01:41:07 -0500605 tftp_prev_block = tftp_cur_block;
Albert ARIBAUD \(3ADEV\)83006852015-10-12 00:02:57 +0200606 timeout_count_max = tftp_timeout_count_max;
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500607 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
wdenkfe8c2802002-11-03 00:38:21 +0000608
Joe Hershberger64701592015-04-08 01:41:07 -0500609 store_block(tftp_cur_block - 1, pkt + 2, len);
wdenkfe8c2802002-11-03 00:38:21 +0000610
611 /*
Luca Ceresoli2f3dc0a2011-05-17 00:03:41 +0000612 * Acknowledge the block just received, which will prompt
Luca Ceresoli5079c762011-05-17 00:03:37 +0000613 * the remote for the next one.
wdenkfe8c2802002-11-03 00:38:21 +0000614 */
David Updegraff7280da72007-06-11 10:41:07 -0500615#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200616 /* if I am the MasterClient, actively calculate what my next
617 * needed block is; else I'm passive; not ACKING
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100618 */
Joe Hershberger64701592015-04-08 01:41:07 -0500619 if (tftp_mcast_active) {
620 if (len < tftp_block_size) {
621 tftp_mcast_ending_block = tftp_cur_block;
622 } else if (tftp_mcast_master_client) {
623 tftp_mcast_prev_hole = ext2_find_next_zero_bit(
624 tftp_mcast_bitmap,
625 tftp_mcast_bitmap_size * 8,
626 tftp_mcast_prev_hole);
627 tftp_cur_block = tftp_mcast_prev_hole;
628 if (tftp_cur_block >
629 ((tftp_mcast_bitmap_size * 8) - 1)) {
630 debug("tftpfile too big\n");
David Updegraff7280da72007-06-11 10:41:07 -0500631 /* try to double it and retry */
Joe Hershberger64701592015-04-08 01:41:07 -0500632 tftp_mcast_bitmap_size <<= 1;
David Updegraff7280da72007-06-11 10:41:07 -0500633 mcast_cleanup();
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500634 net_start_again();
David Updegraff7280da72007-06-11 10:41:07 -0500635 return;
636 }
Joe Hershberger64701592015-04-08 01:41:07 -0500637 tftp_prev_block = tftp_cur_block;
David Updegraff7280da72007-06-11 10:41:07 -0500638 }
639 }
640#endif
Joe Hershberger64701592015-04-08 01:41:07 -0500641 tftp_send();
wdenkfe8c2802002-11-03 00:38:21 +0000642
David Updegraff7280da72007-06-11 10:41:07 -0500643#ifdef CONFIG_MCAST_TFTP
Joe Hershberger64701592015-04-08 01:41:07 -0500644 if (tftp_mcast_active) {
645 if (tftp_mcast_master_client &&
646 (tftp_cur_block >= tftp_mcast_ending_block)) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000647 puts("\nMulticast tftp done\n");
David Updegraff7280da72007-06-11 10:41:07 -0500648 mcast_cleanup();
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000649 net_set_state(NETLOOP_SUCCESS);
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200650 }
Joe Hershberger3a29e5a2012-05-15 08:59:12 +0000651 } else
David Updegraff7280da72007-06-11 10:41:07 -0500652#endif
Joe Hershberger64701592015-04-08 01:41:07 -0500653 if (len < tftp_block_size)
Simon Glassb437aad2011-10-24 18:00:03 +0000654 tftp_complete();
wdenkfe8c2802002-11-03 00:38:21 +0000655 break;
656
657 case TFTP_ERROR:
Luca Ceresolif40538a2011-05-14 05:49:57 +0000658 printf("\nTFTP error: '%s' (%d)\n",
Kim Phillips878dad02013-01-16 18:09:19 -0600659 pkt + 2, ntohs(*(__be16 *)pkt));
Remy Bohmerb46293f2009-10-28 22:13:40 +0100660
Kim Phillips878dad02013-01-16 18:09:19 -0600661 switch (ntohs(*(__be16 *)pkt)) {
Remy Bohmerb46293f2009-10-28 22:13:40 +0100662 case TFTP_ERR_FILE_NOT_FOUND:
663 case TFTP_ERR_ACCESS_DENIED:
664 puts("Not retrying...\n");
665 eth_halt();
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000666 net_set_state(NETLOOP_FAIL);
Remy Bohmerb46293f2009-10-28 22:13:40 +0100667 break;
668 case TFTP_ERR_UNDEFINED:
669 case TFTP_ERR_DISK_FULL:
670 case TFTP_ERR_UNEXPECTED_OPCODE:
671 case TFTP_ERR_UNKNOWN_TRANSFER_ID:
672 case TFTP_ERR_FILE_ALREADY_EXISTS:
673 default:
674 puts("Starting again\n\n");
David Updegraff7280da72007-06-11 10:41:07 -0500675#ifdef CONFIG_MCAST_TFTP
Remy Bohmerb46293f2009-10-28 22:13:40 +0100676 mcast_cleanup();
David Updegraff7280da72007-06-11 10:41:07 -0500677#endif
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500678 net_start_again();
Remy Bohmerb46293f2009-10-28 22:13:40 +0100679 break;
680 }
wdenkfe8c2802002-11-03 00:38:21 +0000681 break;
682 }
683}
684
685
Joe Hershberger64701592015-04-08 01:41:07 -0500686static void tftp_timeout_handler(void)
wdenkfe8c2802002-11-03 00:38:21 +0000687{
Joe Hershberger64701592015-04-08 01:41:07 -0500688 if (++timeout_count > timeout_count_max) {
Simon Glassbd8a0e02011-10-24 18:00:04 +0000689 restart("Retry count exceeded");
wdenkfe8c2802002-11-03 00:38:21 +0000690 } else {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000691 puts("T ");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500692 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
Joe Hershberger64701592015-04-08 01:41:07 -0500693 if (tftp_state != STATE_RECV_WRQ)
694 tftp_send();
wdenkfe8c2802002-11-03 00:38:21 +0000695 }
696}
697
698
Joe Hershberger64701592015-04-08 01:41:07 -0500699void tftp_start(enum proto_t protocol)
wdenkfe8c2802002-11-03 00:38:21 +0000700{
Albert ARIBAUD \(3ADEV\)83006852015-10-12 00:02:57 +0200701#if CONFIG_NET_TFTP_VARS
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200702 char *ep; /* Environment pointer */
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200703
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100704 /*
705 * Allow the user to choose TFTP blocksize and timeout.
706 * TFTP protocol has a minimal timeout of 1 second.
707 */
Albert ARIBAUD \(3ADEV\)83006852015-10-12 00:02:57 +0200708
Simon Glass64b723f2017-08-03 12:22:12 -0600709 ep = env_get("tftpblocksize");
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000710 if (ep != NULL)
Joe Hershberger64701592015-04-08 01:41:07 -0500711 tftp_block_size_option = simple_strtol(ep, NULL, 10);
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100712
Simon Glass64b723f2017-08-03 12:22:12 -0600713 ep = env_get("tftptimeout");
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000714 if (ep != NULL)
Joe Hershberger64701592015-04-08 01:41:07 -0500715 timeout_ms = simple_strtol(ep, NULL, 10);
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100716
Bin Menge67eaa82015-08-27 22:25:51 -0700717 if (timeout_ms < 1000) {
718 printf("TFTP timeout (%ld ms) too low, set min = 1000 ms\n",
Joe Hershberger64701592015-04-08 01:41:07 -0500719 timeout_ms);
Bin Menge67eaa82015-08-27 22:25:51 -0700720 timeout_ms = 1000;
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100721 }
722
Simon Glass64b723f2017-08-03 12:22:12 -0600723 ep = env_get("tftptimeoutcountmax");
Albert ARIBAUD \(3ADEV\)83006852015-10-12 00:02:57 +0200724 if (ep != NULL)
725 tftp_timeout_count_max = simple_strtol(ep, NULL, 10);
726
727 if (tftp_timeout_count_max < 0) {
728 printf("TFTP timeout count max (%d ms) negative, set to 0\n",
729 tftp_timeout_count_max);
730 tftp_timeout_count_max = 0;
731 }
732#endif
733
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100734 debug("TFTP blocksize = %i, timeout = %ld ms\n",
Joe Hershberger64701592015-04-08 01:41:07 -0500735 tftp_block_size_option, timeout_ms);
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200736
Joe Hershberger5874dec2015-04-08 01:41:01 -0500737 tftp_remote_ip = net_server_ip;
Joe Hershberger290c8992015-04-08 01:41:02 -0500738 if (net_boot_file_name[0] == '\0') {
Matthias Weisser6cc63262011-12-03 03:29:44 +0000739 sprintf(default_filename, "%02X%02X%02X%02X.img",
Joe Hershberger5874dec2015-04-08 01:41:01 -0500740 net_ip.s_addr & 0xFF,
741 (net_ip.s_addr >> 8) & 0xFF,
742 (net_ip.s_addr >> 16) & 0xFF,
743 (net_ip.s_addr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100744
Vladimir Zapolskiy04cc2902017-06-28 22:56:07 +0300745 strncpy(tftp_filename, default_filename, DEFAULT_NAME_LEN);
746 tftp_filename[DEFAULT_NAME_LEN - 1] = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000747
Luca Ceresolif40538a2011-05-14 05:49:57 +0000748 printf("*** Warning: no boot file name; using '%s'\n",
Joe Hershberger64701592015-04-08 01:41:07 -0500749 tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000750 } else {
Joe Hershberger290c8992015-04-08 01:41:02 -0500751 char *p = strchr(net_boot_file_name, ':');
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100752
753 if (p == NULL) {
Joe Hershberger290c8992015-04-08 01:41:02 -0500754 strncpy(tftp_filename, net_boot_file_name, MAX_LEN);
Joe Hershberger64701592015-04-08 01:41:07 -0500755 tftp_filename[MAX_LEN - 1] = 0;
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100756 } else {
Joe Hershberger290c8992015-04-08 01:41:02 -0500757 tftp_remote_ip = string_to_ip(net_boot_file_name);
Peter Tyser5e58efa2008-12-01 16:29:38 -0600758 strncpy(tftp_filename, p + 1, MAX_LEN);
Joe Hershberger64701592015-04-08 01:41:07 -0500759 tftp_filename[MAX_LEN - 1] = 0;
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100760 }
wdenkfe8c2802002-11-03 00:38:21 +0000761 }
762
Luca Ceresolif40538a2011-05-14 05:49:57 +0000763 printf("Using %s device\n", eth_get_name());
Simon Glass6a398d22011-10-24 18:00:07 +0000764 printf("TFTP %s server %pI4; our IP address is %pI4",
Simon Glass4abd0e22011-10-27 06:24:29 +0000765#ifdef CONFIG_CMD_TFTPPUT
766 protocol == TFTPPUT ? "to" : "from",
767#else
Joe Hershberger64701592015-04-08 01:41:07 -0500768 "from",
Simon Glass4abd0e22011-10-27 06:24:29 +0000769#endif
Joe Hershberger64701592015-04-08 01:41:07 -0500770 &tftp_remote_ip, &net_ip);
wdenkfe8c2802002-11-03 00:38:21 +0000771
772 /* Check if we need to send across this subnet */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500773 if (net_gateway.s_addr && net_netmask.s_addr) {
774 struct in_addr our_net;
775 struct in_addr remote_net;
wdenkfe8c2802002-11-03 00:38:21 +0000776
Joe Hershberger5874dec2015-04-08 01:41:01 -0500777 our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
778 remote_net.s_addr = tftp_remote_ip.s_addr & net_netmask.s_addr;
779 if (our_net.s_addr != remote_net.s_addr)
780 printf("; sending through gateway %pI4", &net_gateway);
wdenkfe8c2802002-11-03 00:38:21 +0000781 }
Luca Ceresolif40538a2011-05-14 05:49:57 +0000782 putc('\n');
wdenkfe8c2802002-11-03 00:38:21 +0000783
Luca Ceresolif40538a2011-05-14 05:49:57 +0000784 printf("Filename '%s'.", tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000785
Joe Hershberger290c8992015-04-08 01:41:02 -0500786 if (net_boot_file_expected_size_in_blocks) {
787 printf(" Size is 0x%x Bytes = ",
788 net_boot_file_expected_size_in_blocks << 9);
789 print_size(net_boot_file_expected_size_in_blocks << 9, "");
wdenkfe8c2802002-11-03 00:38:21 +0000790 }
791
Luca Ceresolif40538a2011-05-14 05:49:57 +0000792 putc('\n');
Simon Glass6a398d22011-10-24 18:00:07 +0000793#ifdef CONFIG_CMD_TFTPPUT
Joe Hershberger64701592015-04-08 01:41:07 -0500794 tftp_put_active = (protocol == TFTPPUT);
795 if (tftp_put_active) {
Simon Glass6a398d22011-10-24 18:00:07 +0000796 printf("Save address: 0x%lx\n", save_addr);
797 printf("Save size: 0x%lx\n", save_size);
Joe Hershberger290c8992015-04-08 01:41:02 -0500798 net_boot_file_size = save_size;
Simon Glass6a398d22011-10-24 18:00:07 +0000799 puts("Saving: *\b");
Joe Hershberger64701592015-04-08 01:41:07 -0500800 tftp_state = STATE_SEND_WRQ;
Simon Glass6a398d22011-10-24 18:00:07 +0000801 new_transfer();
802 } else
803#endif
804 {
805 printf("Load address: 0x%lx\n", load_addr);
806 puts("Loading: *\b");
Joe Hershberger64701592015-04-08 01:41:07 -0500807 tftp_state = STATE_SEND_RRQ;
Alexander Graf94c4b992016-05-06 21:01:01 +0200808 efi_set_bootdev("Net", "", tftp_filename);
Simon Glass6a398d22011-10-24 18:00:07 +0000809 }
wdenkfe8c2802002-11-03 00:38:21 +0000810
Simon Glasscab9a8d2012-10-11 13:57:36 +0000811 time_start = get_timer(0);
Joe Hershberger64701592015-04-08 01:41:07 -0500812 timeout_count_max = tftp_timeout_count_max;
Bartlomiej Sieka371cde72008-10-01 15:26:29 +0200813
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500814 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
Joe Hershberger5874dec2015-04-08 01:41:01 -0500815 net_set_udp_handler(tftp_handler);
Simon Glass2928cd82011-10-26 14:18:38 +0000816#ifdef CONFIG_CMD_TFTPPUT
Simon Glass6a398d22011-10-24 18:00:07 +0000817 net_set_icmp_handler(icmp_handler);
Simon Glass2928cd82011-10-26 14:18:38 +0000818#endif
Joe Hershberger64701592015-04-08 01:41:07 -0500819 tftp_remote_port = WELL_KNOWN_PORT;
820 timeout_count = 0;
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200821 /* Use a pseudo-random port unless a specific port is set */
Joe Hershberger64701592015-04-08 01:41:07 -0500822 tftp_our_port = 1024 + (get_timer(0) % 3072);
David Updegraff7280da72007-06-11 10:41:07 -0500823
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200824#ifdef CONFIG_TFTP_PORT
Simon Glass64b723f2017-08-03 12:22:12 -0600825 ep = env_get("tftpdstp");
Luca Ceresoli35dfb182011-05-14 05:50:00 +0000826 if (ep != NULL)
Joe Hershberger64701592015-04-08 01:41:07 -0500827 tftp_remote_port = simple_strtol(ep, NULL, 10);
Simon Glass64b723f2017-08-03 12:22:12 -0600828 ep = env_get("tftpsrcp");
Luca Ceresoli35dfb182011-05-14 05:50:00 +0000829 if (ep != NULL)
Joe Hershberger64701592015-04-08 01:41:07 -0500830 tftp_our_port = simple_strtol(ep, NULL, 10);
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200831#endif
Joe Hershberger64701592015-04-08 01:41:07 -0500832 tftp_cur_block = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000833
wdenke6466f62003-06-05 19:27:42 +0000834 /* zero out server ether in case the server ip has changed */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500835 memset(net_server_ethaddr, 0, 6);
Joe Hershberger64701592015-04-08 01:41:07 -0500836 /* Revert tftp_block_size to dflt */
837 tftp_block_size = TFTP_BLOCK_SIZE;
David Updegraff7280da72007-06-11 10:41:07 -0500838#ifdef CONFIG_MCAST_TFTP
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100839 mcast_cleanup();
David Updegraff7280da72007-06-11 10:41:07 -0500840#endif
Robin Getzb3b00ed2009-08-20 10:50:20 -0400841#ifdef CONFIG_TFTP_TSIZE
Joe Hershberger64701592015-04-08 01:41:07 -0500842 tftp_tsize = 0;
843 tftp_tsize_num_hash = 0;
Robin Getzb3b00ed2009-08-20 10:50:20 -0400844#endif
wdenke6466f62003-06-05 19:27:42 +0000845
Joe Hershberger64701592015-04-08 01:41:07 -0500846 tftp_send();
wdenkfe8c2802002-11-03 00:38:21 +0000847}
Luca Ceresolib640ed32011-05-17 00:03:39 +0000848
849#ifdef CONFIG_CMD_TFTPSRV
Joe Hershberger64701592015-04-08 01:41:07 -0500850void tftp_start_server(void)
Luca Ceresolib640ed32011-05-17 00:03:39 +0000851{
852 tftp_filename[0] = 0;
853
Luca Ceresolib640ed32011-05-17 00:03:39 +0000854 printf("Using %s device\n", eth_get_name());
Joe Hershberger5874dec2015-04-08 01:41:01 -0500855 printf("Listening for TFTP transfer on %pI4\n", &net_ip);
Luca Ceresolib640ed32011-05-17 00:03:39 +0000856 printf("Load address: 0x%lx\n", load_addr);
857
858 puts("Loading: *\b");
859
Albert ARIBAUD \(3ADEV\)83006852015-10-12 00:02:57 +0200860 timeout_count_max = tftp_timeout_count_max;
Joe Hershberger64701592015-04-08 01:41:07 -0500861 timeout_count = 0;
862 timeout_ms = TIMEOUT;
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500863 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
Luca Ceresolib640ed32011-05-17 00:03:39 +0000864
Joe Hershberger64701592015-04-08 01:41:07 -0500865 /* Revert tftp_block_size to dflt */
866 tftp_block_size = TFTP_BLOCK_SIZE;
867 tftp_cur_block = 0;
868 tftp_our_port = WELL_KNOWN_PORT;
Luca Ceresolib640ed32011-05-17 00:03:39 +0000869
870#ifdef CONFIG_TFTP_TSIZE
Joe Hershberger64701592015-04-08 01:41:07 -0500871 tftp_tsize = 0;
872 tftp_tsize_num_hash = 0;
Luca Ceresolib640ed32011-05-17 00:03:39 +0000873#endif
874
Joe Hershberger64701592015-04-08 01:41:07 -0500875 tftp_state = STATE_RECV_WRQ;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500876 net_set_udp_handler(tftp_handler);
Andrew Ruder91393c52013-10-22 19:10:28 -0500877
878 /* zero out server ether in case the server ip has changed */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500879 memset(net_server_ethaddr, 0, 6);
Luca Ceresolib640ed32011-05-17 00:03:39 +0000880}
881#endif /* CONFIG_CMD_TFTPSRV */
wdenkfe8c2802002-11-03 00:38:21 +0000882
David Updegraff7280da72007-06-11 10:41:07 -0500883#ifdef CONFIG_MCAST_TFTP
Joe Hershberger64701592015-04-08 01:41:07 -0500884/*
885 * Credits: atftp project.
David Updegraff7280da72007-06-11 10:41:07 -0500886 */
887
Joe Hershberger64701592015-04-08 01:41:07 -0500888/*
889 * Pick up BcastAddr, Port, and whether I am [now] the master-client.
David Updegraff7280da72007-06-11 10:41:07 -0500890 * Frame:
891 * +-------+-----------+---+-------~~-------+---+
892 * | opc | multicast | 0 | addr, port, mc | 0 |
893 * +-------+-----------+---+-------~~-------+---+
894 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
895 * I am the new master-client so must send ACKs to DataBlocks. If I am not
896 * master-client, I'm a passive client, gathering what DataBlocks I may and
897 * making note of which ones I got in my bitmask.
898 * In theory, I never go from master->passive..
899 * .. this comes in with pkt already pointing just past opc
900 */
901static void parse_multicast_oack(char *pkt, int len)
902{
Luca Ceresolif40538a2011-05-14 05:49:57 +0000903 int i;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500904 struct in_addr addr;
Joe Hershberger64701592015-04-08 01:41:07 -0500905 char *mc_adr;
906 char *port;
907 char *mc;
David Updegraff7280da72007-06-11 10:41:07 -0500908
Joe Hershberger64701592015-04-08 01:41:07 -0500909 mc_adr = NULL;
910 port = NULL;
911 mc = NULL;
David Updegraff7280da72007-06-11 10:41:07 -0500912 /* march along looking for 'multicast\0', which has to start at least
913 * 14 bytes back from the end.
914 */
Joe Hershberger64701592015-04-08 01:41:07 -0500915 for (i = 0; i < len - 14; i++)
916 if (strcmp(pkt + i, "multicast") == 0)
David Updegraff7280da72007-06-11 10:41:07 -0500917 break;
Joe Hershberger64701592015-04-08 01:41:07 -0500918 if (i >= (len - 14)) /* non-Multicast OACK, ign. */
David Updegraff7280da72007-06-11 10:41:07 -0500919 return;
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200920
Luca Ceresolif40538a2011-05-14 05:49:57 +0000921 i += 10; /* strlen multicast */
Joe Hershberger64701592015-04-08 01:41:07 -0500922 mc_adr = pkt + i;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000923 for (; i < len; i++) {
Joe Hershberger64701592015-04-08 01:41:07 -0500924 if (*(pkt + i) == ',') {
925 *(pkt + i) = '\0';
David Updegraff7280da72007-06-11 10:41:07 -0500926 if (port) {
Joe Hershberger64701592015-04-08 01:41:07 -0500927 mc = pkt + i + 1;
David Updegraff7280da72007-06-11 10:41:07 -0500928 break;
929 } else {
Joe Hershberger64701592015-04-08 01:41:07 -0500930 port = pkt + i + 1;
David Updegraff7280da72007-06-11 10:41:07 -0500931 }
932 }
933 }
Luca Ceresoli6072fcd2011-05-14 05:50:01 +0000934 if (!port || !mc_adr || !mc)
935 return;
Joe Hershberger64701592015-04-08 01:41:07 -0500936 if (tftp_mcast_active && tftp_mcast_master_client) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000937 printf("I got a OACK as master Client, WRONG!\n");
David Updegraff7280da72007-06-11 10:41:07 -0500938 return;
939 }
940 /* ..I now accept packets destined for this MCAST addr, port */
Joe Hershberger64701592015-04-08 01:41:07 -0500941 if (!tftp_mcast_active) {
942 if (tftp_mcast_bitmap) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000943 printf("Internal failure! no mcast.\n");
Joe Hershberger64701592015-04-08 01:41:07 -0500944 free(tftp_mcast_bitmap);
945 tftp_mcast_bitmap = NULL;
946 tftp_mcast_disabled = 1;
947 return;
David Updegraff7280da72007-06-11 10:41:07 -0500948 }
949 /* I malloc instead of pre-declare; so that if the file ends
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200950 * up being too big for this bitmap I can retry
951 */
Joe Hershberger64701592015-04-08 01:41:07 -0500952 tftp_mcast_bitmap = malloc(tftp_mcast_bitmap_size);
953 if (!tftp_mcast_bitmap) {
954 printf("No bitmap, no multicast. Sorry.\n");
955 tftp_mcast_disabled = 1;
David Updegraff7280da72007-06-11 10:41:07 -0500956 return;
957 }
Joe Hershberger64701592015-04-08 01:41:07 -0500958 memset(tftp_mcast_bitmap, 0, tftp_mcast_bitmap_size);
959 tftp_mcast_prev_hole = 0;
960 tftp_mcast_active = 1;
David Updegraff7280da72007-06-11 10:41:07 -0500961 }
962 addr = string_to_ip(mc_adr);
Joe Hershberger5874dec2015-04-08 01:41:01 -0500963 if (net_mcast_addr.s_addr != addr.s_addr) {
964 if (net_mcast_addr.s_addr)
965 eth_mcast_join(net_mcast_addr, 0);
966 net_mcast_addr = addr;
967 if (eth_mcast_join(net_mcast_addr, 1)) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000968 printf("Fail to set mcast, revert to TFTP\n");
Joe Hershberger64701592015-04-08 01:41:07 -0500969 tftp_mcast_disabled = 1;
David Updegraff7280da72007-06-11 10:41:07 -0500970 mcast_cleanup();
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500971 net_start_again();
David Updegraff7280da72007-06-11 10:41:07 -0500972 }
973 }
Joe Hershberger64701592015-04-08 01:41:07 -0500974 tftp_mcast_master_client = simple_strtoul((char *)mc, NULL, 10);
975 tftp_mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
976 printf("Multicast: %s:%d [%d]\n", mc_adr, tftp_mcast_port,
977 tftp_mcast_master_client);
David Updegraff7280da72007-06-11 10:41:07 -0500978 return;
979}
980
981#endif /* Multicast TFTP */