blob: 5eb0f6d8cb3ac6a00617d3bafe4599e577a2da54 [file] [log] [blame]
wdenkfe8c2802002-11-03 00:38:21 +00001/*
Luca Ceresoli2f094132011-05-14 05:49:56 +00002 * Copyright 1994, 1995, 2000 Neil Russell.
3 * (See License)
4 * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
Luca Ceresolie59e3562011-05-17 00:03:39 +00005 * Copyright 2011 Comelit Group SpA,
6 * Luca Ceresoli <luca.ceresoli@comelit.it>
wdenkfe8c2802002-11-03 00:38:21 +00007 */
8
9#include <common.h>
10#include <command.h>
Joe Hershberger55d5fd92015-03-22 17:09:08 -050011#include <mapmem.h>
wdenkfe8c2802002-11-03 00:38:21 +000012#include <net.h>
Lukasz Majewski34696952015-08-24 00:21:43 +020013#include <net/tftp.h>
wdenkfe8c2802002-11-03 00:38:21 +000014#include "bootp.h"
Joe Hershberger13dfe942012-05-15 08:59:12 +000015#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
16#include <flash.h>
17#endif
wdenkfe8c2802002-11-03 00:38:21 +000018
Luca Ceresoli2f094132011-05-14 05:49:56 +000019/* Well known TFTP port # */
20#define WELL_KNOWN_PORT 69
21/* Millisecs to timeout for lost pkt */
Bin Mengaf2ca592015-08-27 22:25:51 -070022#define TIMEOUT 5000UL
wdenkfe8c2802002-11-03 00:38:21 +000023#ifndef CONFIG_NET_RETRY_COUNT
Luca Ceresoli2f094132011-05-14 05:49:56 +000024/* # of timeouts before giving up */
Bin Mengaf2ca592015-08-27 22:25:51 -070025# define TIMEOUT_COUNT 10
wdenkfe8c2802002-11-03 00:38:21 +000026#else
27# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
28#endif
Luca Ceresoli2f094132011-05-14 05:49:56 +000029/* Number of "loading" hashes per line (for checking the image size) */
30#define HASHES_PER_LINE 65
wdenkfe8c2802002-11-03 00:38:21 +000031
32/*
33 * TFTP operations.
34 */
35#define TFTP_RRQ 1
36#define TFTP_WRQ 2
37#define TFTP_DATA 3
38#define TFTP_ACK 4
39#define TFTP_ERROR 5
wdenkfbe4b5c2003-10-06 21:55:32 +000040#define TFTP_OACK 6
wdenkfe8c2802002-11-03 00:38:21 +000041
Joe Hershberger8885c5f2015-04-08 01:41:07 -050042static ulong timeout_ms = TIMEOUT;
43static int timeout_count_max = TIMEOUT_COUNT;
Simon Glass85b19802012-10-11 13:57:36 +000044static ulong time_start; /* Record time we started tftp */
Bartlomiej Siekae83cc062008-10-01 15:26:29 +020045
46/*
47 * These globals govern the timeout behavior when attempting a connection to a
Joe Hershberger8885c5f2015-04-08 01:41:07 -050048 * TFTP server. tftp_timeout_ms specifies the number of milliseconds to
Bartlomiej Siekae83cc062008-10-01 15:26:29 +020049 * wait for the server to respond to initial connection. Second global,
Joe Hershberger8885c5f2015-04-08 01:41:07 -050050 * tftp_timeout_count_max, gives the number of such connection retries.
51 * tftp_timeout_count_max must be non-negative and tftp_timeout_ms must be
Bartlomiej Siekae83cc062008-10-01 15:26:29 +020052 * positive. The globals are meant to be set (and restored) by code needing
53 * non-standard timeout behavior when initiating a TFTP transfer.
54 */
Joe Hershberger8885c5f2015-04-08 01:41:07 -050055ulong tftp_timeout_ms = TIMEOUT;
56int tftp_timeout_count_max = TIMEOUT_COUNT;
Bartlomiej Siekae83cc062008-10-01 15:26:29 +020057
Remy Bohmeraafda382009-10-28 22:13:40 +010058enum {
59 TFTP_ERR_UNDEFINED = 0,
60 TFTP_ERR_FILE_NOT_FOUND = 1,
61 TFTP_ERR_ACCESS_DENIED = 2,
62 TFTP_ERR_DISK_FULL = 3,
63 TFTP_ERR_UNEXPECTED_OPCODE = 4,
64 TFTP_ERR_UNKNOWN_TRANSFER_ID = 5,
65 TFTP_ERR_FILE_ALREADY_EXISTS = 6,
66};
67
Joe Hershberger049a95a2015-04-08 01:41:01 -050068static struct in_addr tftp_remote_ip;
Luca Ceresoli2f094132011-05-14 05:49:56 +000069/* The UDP port at their end */
Joe Hershberger8885c5f2015-04-08 01:41:07 -050070static int tftp_remote_port;
Luca Ceresoli2f094132011-05-14 05:49:56 +000071/* The UDP port at our end */
Joe Hershberger8885c5f2015-04-08 01:41:07 -050072static int tftp_our_port;
73static int timeout_count;
Luca Ceresoli2f094132011-05-14 05:49:56 +000074/* packet sequence number */
Joe Hershberger8885c5f2015-04-08 01:41:07 -050075static ulong tftp_cur_block;
Luca Ceresoli2f094132011-05-14 05:49:56 +000076/* last packet sequence number received */
Joe Hershberger8885c5f2015-04-08 01:41:07 -050077static ulong tftp_prev_block;
Luca Ceresoli2f094132011-05-14 05:49:56 +000078/* count of sequence number wraparounds */
Joe Hershberger8885c5f2015-04-08 01:41:07 -050079static ulong tftp_block_wrap;
Luca Ceresoli2f094132011-05-14 05:49:56 +000080/* memory offset due to wrapping */
Joe Hershberger8885c5f2015-04-08 01:41:07 -050081static ulong tftp_block_wrap_offset;
82static int tftp_state;
Robin Getz4fccb812009-08-20 10:50:20 -040083#ifdef CONFIG_TFTP_TSIZE
Luca Ceresoli2f094132011-05-14 05:49:56 +000084/* The file size reported by the server */
Joe Hershberger8885c5f2015-04-08 01:41:07 -050085static int tftp_tsize;
Luca Ceresoli2f094132011-05-14 05:49:56 +000086/* The number of hashes we printed */
Joe Hershberger8885c5f2015-04-08 01:41:07 -050087static short tftp_tsize_num_hash;
Robin Getz4fccb812009-08-20 10:50:20 -040088#endif
Simon Glass1fb7cd42011-10-24 18:00:07 +000089#ifdef CONFIG_CMD_TFTPPUT
Joe Hershberger8885c5f2015-04-08 01:41:07 -050090/* 1 if writing, else 0 */
91static int tftp_put_active;
92/* 1 if we have sent the last block */
93static int tftp_put_final_block_sent;
Simon Glass1fb7cd42011-10-24 18:00:07 +000094#else
Joe Hershberger8885c5f2015-04-08 01:41:07 -050095#define tftp_put_active 0
Simon Glass1fb7cd42011-10-24 18:00:07 +000096#endif
wdenk3f85ce22004-02-23 16:11:30 +000097
Luca Ceresolie3fb0ab2011-05-17 00:03:38 +000098#define STATE_SEND_RRQ 1
wdenkfe8c2802002-11-03 00:38:21 +000099#define STATE_DATA 2
100#define STATE_TOO_LARGE 3
101#define STATE_BAD_MAGIC 4
wdenkfbe4b5c2003-10-06 21:55:32 +0000102#define STATE_OACK 5
Luca Ceresolie59e3562011-05-17 00:03:39 +0000103#define STATE_RECV_WRQ 6
Simon Glass1fb7cd42011-10-24 18:00:07 +0000104#define STATE_SEND_WRQ 7
wdenkfe8c2802002-11-03 00:38:21 +0000105
Luca Ceresoli2f094132011-05-14 05:49:56 +0000106/* default TFTP block size */
107#define TFTP_BLOCK_SIZE 512
108/* sequence number is 16 bit */
109#define TFTP_SEQUENCE_SIZE ((ulong)(1<<16))
wdenk3f85ce22004-02-23 16:11:30 +0000110
wdenkfe8c2802002-11-03 00:38:21 +0000111#define DEFAULT_NAME_LEN (8 + 4 + 1)
112static char default_filename[DEFAULT_NAME_LEN];
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100113
114#ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
115#define MAX_LEN 128
116#else
117#define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
118#endif
119
120static char tftp_filename[MAX_LEN];
wdenkfe8c2802002-11-03 00:38:21 +0000121
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200122/* 512 is poor choice for ethernet, MTU is typically 1500.
123 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
David Updegraff53a5c422007-06-11 10:41:07 -0500124 * almost-MTU block sizes. At least try... fall back to 512 if need be.
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200125 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
David Updegraff53a5c422007-06-11 10:41:07 -0500126 */
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200127#ifdef CONFIG_TFTP_BLOCKSIZE
128#define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
129#else
David Updegraff53a5c422007-06-11 10:41:07 -0500130#define TFTP_MTU_BLOCKSIZE 1468
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200131#endif
132
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500133static unsigned short tftp_block_size = TFTP_BLOCK_SIZE;
134static unsigned short tftp_block_size_option = TFTP_MTU_BLOCKSIZE;
David Updegraff53a5c422007-06-11 10:41:07 -0500135
136#ifdef CONFIG_MCAST_TFTP
137#include <malloc.h>
138#define MTFTP_BITMAPSIZE 0x1000
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500139static unsigned *tftp_mcast_bitmap;
140static int tftp_mcast_prev_hole;
141static int tftp_mcast_bitmap_size = MTFTP_BITMAPSIZE;
142static int tftp_mcast_disabled;
143static int tftp_mcast_master_client;
144static int tftp_mcast_active;
145static int tftp_mcast_port;
146/* can get 'last' block before done..*/
147static ulong tftp_mcast_ending_block;
David Updegraff53a5c422007-06-11 10:41:07 -0500148
Luca Ceresolic718b142011-05-14 05:49:57 +0000149static void parse_multicast_oack(char *pkt, int len);
David Updegraff53a5c422007-06-11 10:41:07 -0500150
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500151static void mcast_cleanup(void)
David Updegraff53a5c422007-06-11 10:41:07 -0500152{
Joe Hershberger049a95a2015-04-08 01:41:01 -0500153 if (net_mcast_addr)
154 eth_mcast_join(net_mcast_addr, 0);
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500155 if (tftp_mcast_bitmap)
156 free(tftp_mcast_bitmap);
157 tftp_mcast_bitmap = NULL;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500158 net_mcast_addr.s_addr = 0;
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500159 tftp_mcast_active = 0;
160 tftp_mcast_port = 0;
161 tftp_mcast_ending_block = -1;
David Updegraff53a5c422007-06-11 10:41:07 -0500162}
163
164#endif /* CONFIG_MCAST_TFTP */
165
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500166static inline void store_block(int block, uchar *src, unsigned len)
wdenkfe8c2802002-11-03 00:38:21 +0000167{
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500168 ulong offset = block * tftp_block_size + tftp_block_wrap_offset;
wdenk3f85ce22004-02-23 16:11:30 +0000169 ulong newsize = offset + len;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200170#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
wdenkfe8c2802002-11-03 00:38:21 +0000171 int i, rc = 0;
172
Luca Ceresolic718b142011-05-14 05:49:57 +0000173 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenkfe8c2802002-11-03 00:38:21 +0000174 /* start address in flash? */
Jochen Friedrichbe1b0d22008-09-02 11:24:59 +0200175 if (flash_info[i].flash_id == FLASH_UNKNOWN)
176 continue;
wdenkfe8c2802002-11-03 00:38:21 +0000177 if (load_addr + offset >= flash_info[i].start[0]) {
178 rc = 1;
179 break;
180 }
181 }
182
183 if (rc) { /* Flash is destination for this packet */
Luca Ceresolic718b142011-05-14 05:49:57 +0000184 rc = flash_write((char *)src, (ulong)(load_addr+offset), len);
wdenkfe8c2802002-11-03 00:38:21 +0000185 if (rc) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000186 flash_perror(rc);
Joe Hershberger22f6e992012-05-23 07:59:14 +0000187 net_set_state(NETLOOP_FAIL);
wdenkfe8c2802002-11-03 00:38:21 +0000188 return;
189 }
Joe Hershberger13dfe942012-05-15 08:59:12 +0000190 } else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200191#endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000192 {
Joe Hershberger55d5fd92015-03-22 17:09:08 -0500193 void *ptr = map_sysmem(load_addr + offset, len);
194
195 memcpy(ptr, src, len);
196 unmap_sysmem(ptr);
wdenkfe8c2802002-11-03 00:38:21 +0000197 }
David Updegraff53a5c422007-06-11 10:41:07 -0500198#ifdef CONFIG_MCAST_TFTP
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500199 if (tftp_mcast_active)
200 ext2_set_bit(block, tftp_mcast_bitmap);
David Updegraff53a5c422007-06-11 10:41:07 -0500201#endif
wdenkfe8c2802002-11-03 00:38:21 +0000202
Joe Hershberger14111572015-04-08 01:41:02 -0500203 if (net_boot_file_size < newsize)
204 net_boot_file_size = newsize;
wdenkfe8c2802002-11-03 00:38:21 +0000205}
206
Simon Glasse4cde2f2011-10-24 18:00:04 +0000207/* Clear our state ready for a new transfer */
Simon Glass165099e2011-10-27 06:24:28 +0000208static void new_transfer(void)
Simon Glasse4cde2f2011-10-24 18:00:04 +0000209{
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500210 tftp_prev_block = 0;
211 tftp_block_wrap = 0;
212 tftp_block_wrap_offset = 0;
Simon Glasse4cde2f2011-10-24 18:00:04 +0000213#ifdef CONFIG_CMD_TFTPPUT
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500214 tftp_put_final_block_sent = 0;
Simon Glasse4cde2f2011-10-24 18:00:04 +0000215#endif
216}
217
Simon Glass1fb7cd42011-10-24 18:00:07 +0000218#ifdef CONFIG_CMD_TFTPPUT
219/**
220 * Load the next block from memory to be sent over tftp.
221 *
222 * @param block Block number to send
223 * @param dst Destination buffer for data
224 * @param len Number of bytes in block (this one and every other)
225 * @return number of bytes loaded
226 */
227static int load_block(unsigned block, uchar *dst, unsigned len)
228{
229 /* We may want to get the final block from the previous set */
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500230 ulong offset = ((int)block - 1) * len + tftp_block_wrap_offset;
Simon Glass1fb7cd42011-10-24 18:00:07 +0000231 ulong tosend = len;
232
Joe Hershberger14111572015-04-08 01:41:02 -0500233 tosend = min(net_boot_file_size - offset, tosend);
Simon Glass1fb7cd42011-10-24 18:00:07 +0000234 (void)memcpy(dst, (void *)(save_addr + offset), tosend);
235 debug("%s: block=%d, offset=%ld, len=%d, tosend=%ld\n", __func__,
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500236 block, offset, len, tosend);
Simon Glass1fb7cd42011-10-24 18:00:07 +0000237 return tosend;
238}
239#endif
240
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500241static void tftp_send(void);
242static void tftp_timeout_handler(void);
wdenkfe8c2802002-11-03 00:38:21 +0000243
244/**********************************************************************/
245
Simon Glassf5329bb2011-10-24 18:00:03 +0000246static void show_block_marker(void)
247{
248#ifdef CONFIG_TFTP_TSIZE
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500249 if (tftp_tsize) {
250 ulong pos = tftp_cur_block * tftp_block_size +
251 tftp_block_wrap_offset;
Max Krummenacher7628afe2015-08-05 17:17:05 +0200252 if (pos > tftp_tsize)
253 pos = tftp_tsize;
Simon Glassf5329bb2011-10-24 18:00:03 +0000254
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500255 while (tftp_tsize_num_hash < pos * 50 / tftp_tsize) {
Simon Glassf5329bb2011-10-24 18:00:03 +0000256 putc('#');
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500257 tftp_tsize_num_hash++;
Simon Glassf5329bb2011-10-24 18:00:03 +0000258 }
Simon Glass1fb7cd42011-10-24 18:00:07 +0000259 } else
Simon Glassf5329bb2011-10-24 18:00:03 +0000260#endif
Simon Glass1fb7cd42011-10-24 18:00:07 +0000261 {
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500262 if (((tftp_cur_block - 1) % 10) == 0)
Simon Glassf5329bb2011-10-24 18:00:03 +0000263 putc('#');
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500264 else if ((tftp_cur_block % (10 * HASHES_PER_LINE)) == 0)
Simon Glassf5329bb2011-10-24 18:00:03 +0000265 puts("\n\t ");
266 }
267}
268
Simon Glasse4cde2f2011-10-24 18:00:04 +0000269/**
270 * restart the current transfer due to an error
271 *
272 * @param msg Message to print for user
273 */
274static void restart(const char *msg)
275{
276 printf("\n%s; starting again\n", msg);
277#ifdef CONFIG_MCAST_TFTP
278 mcast_cleanup();
279#endif
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500280 net_start_again();
Simon Glasse4cde2f2011-10-24 18:00:04 +0000281}
282
283/*
284 * Check if the block number has wrapped, and update progress
285 *
286 * TODO: The egregious use of global variables in this file should be tidied.
287 */
288static void update_block_number(void)
289{
290 /*
291 * RFC1350 specifies that the first data packet will
292 * have sequence number 1. If we receive a sequence
293 * number of 0 this means that there was a wrap
294 * around of the (16 bit) counter.
295 */
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500296 if (tftp_cur_block == 0 && tftp_prev_block != 0) {
297 tftp_block_wrap++;
298 tftp_block_wrap_offset += tftp_block_size * TFTP_SEQUENCE_SIZE;
299 timeout_count = 0; /* we've done well, reset the timeout */
Simon Glasse4cde2f2011-10-24 18:00:04 +0000300 } else {
301 show_block_marker();
302 }
303}
304
Simon Glassf5329bb2011-10-24 18:00:03 +0000305/* The TFTP get or put is complete */
306static void tftp_complete(void)
307{
308#ifdef CONFIG_TFTP_TSIZE
309 /* Print hash marks for the last packet received */
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500310 while (tftp_tsize && tftp_tsize_num_hash < 49) {
Simon Glassf5329bb2011-10-24 18:00:03 +0000311 putc('#');
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500312 tftp_tsize_num_hash++;
Simon Glassf5329bb2011-10-24 18:00:03 +0000313 }
Simon Glass8104f542014-10-10 07:30:21 -0600314 puts(" ");
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500315 print_size(tftp_tsize, "");
Simon Glassf5329bb2011-10-24 18:00:03 +0000316#endif
Simon Glass85b19802012-10-11 13:57:36 +0000317 time_start = get_timer(time_start);
318 if (time_start > 0) {
319 puts("\n\t "); /* Line up with "Loading: " */
Joe Hershberger14111572015-04-08 01:41:02 -0500320 print_size(net_boot_file_size /
Simon Glass85b19802012-10-11 13:57:36 +0000321 time_start * 1000, "/s");
322 }
Simon Glassf5329bb2011-10-24 18:00:03 +0000323 puts("\ndone\n");
Joe Hershberger22f6e992012-05-23 07:59:14 +0000324 net_set_state(NETLOOP_SUCCESS);
Simon Glassf5329bb2011-10-24 18:00:03 +0000325}
326
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500327static void tftp_send(void)
wdenkfe8c2802002-11-03 00:38:21 +0000328{
Simon Glass1fb7cd42011-10-24 18:00:07 +0000329 uchar *pkt;
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000330 uchar *xp;
331 int len = 0;
332 ushort *s;
wdenkfe8c2802002-11-03 00:38:21 +0000333
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200334#ifdef CONFIG_MCAST_TFTP
David Updegraff53a5c422007-06-11 10:41:07 -0500335 /* Multicast TFTP.. non-MasterClients do not ACK data. */
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500336 if (tftp_mcast_active && tftp_state == STATE_DATA &&
337 tftp_mcast_master_client == 0)
David Updegraff53a5c422007-06-11 10:41:07 -0500338 return;
339#endif
wdenkfe8c2802002-11-03 00:38:21 +0000340 /*
341 * We will always be sending some sort of packet, so
342 * cobble together the packet headers now.
343 */
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500344 pkt = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
wdenkfe8c2802002-11-03 00:38:21 +0000345
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500346 switch (tftp_state) {
Luca Ceresolie3fb0ab2011-05-17 00:03:38 +0000347 case STATE_SEND_RRQ:
Simon Glass1fb7cd42011-10-24 18:00:07 +0000348 case STATE_SEND_WRQ:
wdenkfe8c2802002-11-03 00:38:21 +0000349 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200350 s = (ushort *)pkt;
Simon Glass8c6914f2011-10-27 06:24:29 +0000351#ifdef CONFIG_CMD_TFTPPUT
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500352 *s++ = htons(tftp_state == STATE_SEND_RRQ ? TFTP_RRQ :
Simon Glass1fb7cd42011-10-24 18:00:07 +0000353 TFTP_WRQ);
Simon Glass8c6914f2011-10-27 06:24:29 +0000354#else
355 *s++ = htons(TFTP_RRQ);
356#endif
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200357 pkt = (uchar *)s;
Luca Ceresolic718b142011-05-14 05:49:57 +0000358 strcpy((char *)pkt, tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000359 pkt += strlen(tftp_filename) + 1;
Luca Ceresolic718b142011-05-14 05:49:57 +0000360 strcpy((char *)pkt, "octet");
wdenkfe8c2802002-11-03 00:38:21 +0000361 pkt += 5 /*strlen("octet")*/ + 1;
Luca Ceresolic718b142011-05-14 05:49:57 +0000362 strcpy((char *)pkt, "timeout");
wdenkfbe4b5c2003-10-06 21:55:32 +0000363 pkt += 7 /*strlen("timeout")*/ + 1;
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500364 sprintf((char *)pkt, "%lu", timeout_ms / 1000);
Robin Getz0ebf04c2009-07-23 03:01:03 -0400365 debug("send option \"timeout %s\"\n", (char *)pkt);
wdenkfbe4b5c2003-10-06 21:55:32 +0000366 pkt += strlen((char *)pkt) + 1;
Robin Getz4fccb812009-08-20 10:50:20 -0400367#ifdef CONFIG_TFTP_TSIZE
Joe Hershberger14111572015-04-08 01:41:02 -0500368 pkt += sprintf((char *)pkt, "tsize%c%u%c",
369 0, net_boot_file_size, 0);
Robin Getz4fccb812009-08-20 10:50:20 -0400370#endif
David Updegraff53a5c422007-06-11 10:41:07 -0500371 /* try for more effic. blk size */
Luca Ceresolic718b142011-05-14 05:49:57 +0000372 pkt += sprintf((char *)pkt, "blksize%c%d%c",
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500373 0, tftp_block_size_option, 0);
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200374#ifdef CONFIG_MCAST_TFTP
David Updegraff53a5c422007-06-11 10:41:07 -0500375 /* Check all preconditions before even trying the option */
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500376 if (!tftp_mcast_disabled) {
377 tftp_mcast_bitmap = malloc(tftp_mcast_bitmap_size);
378 if (tftp_mcast_bitmap && eth_get_dev()->mcast) {
379 free(tftp_mcast_bitmap);
380 tftp_mcast_bitmap = NULL;
Joe Hershberger13dfe942012-05-15 08:59:12 +0000381 pkt += sprintf((char *)pkt, "multicast%c%c",
382 0, 0);
383 }
David Updegraff53a5c422007-06-11 10:41:07 -0500384 }
385#endif /* CONFIG_MCAST_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000386 len = pkt - xp;
387 break;
388
wdenkfbe4b5c2003-10-06 21:55:32 +0000389 case STATE_OACK:
David Updegraff53a5c422007-06-11 10:41:07 -0500390#ifdef CONFIG_MCAST_TFTP
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500391 /* My turn! Start at where I need blocks I missed. */
392 if (tftp_mcast_active)
393 tftp_cur_block = ext2_find_next_zero_bit(
394 tftp_mcast_bitmap,
395 tftp_mcast_bitmap_size * 8, 0);
396 /* fall through */
David Updegraff53a5c422007-06-11 10:41:07 -0500397#endif
Luca Ceresolie59e3562011-05-17 00:03:39 +0000398
399 case STATE_RECV_WRQ:
David Updegraff53a5c422007-06-11 10:41:07 -0500400 case STATE_DATA:
wdenkfe8c2802002-11-03 00:38:21 +0000401 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200402 s = (ushort *)pkt;
Simon Glass1fb7cd42011-10-24 18:00:07 +0000403 s[0] = htons(TFTP_ACK);
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500404 s[1] = htons(tftp_cur_block);
Simon Glass1fb7cd42011-10-24 18:00:07 +0000405 pkt = (uchar *)(s + 2);
406#ifdef CONFIG_CMD_TFTPPUT
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500407 if (tftp_put_active) {
408 int toload = tftp_block_size;
409 int loaded = load_block(tftp_cur_block, pkt, toload);
Simon Glass1fb7cd42011-10-24 18:00:07 +0000410
411 s[0] = htons(TFTP_DATA);
412 pkt += loaded;
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500413 tftp_put_final_block_sent = (loaded < toload);
Simon Glass1fb7cd42011-10-24 18:00:07 +0000414 }
415#endif
wdenkfe8c2802002-11-03 00:38:21 +0000416 len = pkt - xp;
417 break;
418
419 case STATE_TOO_LARGE:
420 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200421 s = (ushort *)pkt;
422 *s++ = htons(TFTP_ERROR);
Simon Glass1fb7cd42011-10-24 18:00:07 +0000423 *s++ = htons(3);
424
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200425 pkt = (uchar *)s;
Luca Ceresolic718b142011-05-14 05:49:57 +0000426 strcpy((char *)pkt, "File too large");
wdenkfe8c2802002-11-03 00:38:21 +0000427 pkt += 14 /*strlen("File too large")*/ + 1;
428 len = pkt - xp;
429 break;
430
431 case STATE_BAD_MAGIC:
432 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200433 s = (ushort *)pkt;
434 *s++ = htons(TFTP_ERROR);
435 *s++ = htons(2);
436 pkt = (uchar *)s;
Luca Ceresolic718b142011-05-14 05:49:57 +0000437 strcpy((char *)pkt, "File has bad magic");
wdenkfe8c2802002-11-03 00:38:21 +0000438 pkt += 18 /*strlen("File has bad magic")*/ + 1;
439 len = pkt - xp;
440 break;
441 }
442
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500443 net_send_udp_packet(net_server_ethaddr, tftp_remote_ip,
444 tftp_remote_port, tftp_our_port, len);
wdenkfe8c2802002-11-03 00:38:21 +0000445}
446
Simon Glass39bccd22011-10-26 14:18:38 +0000447#ifdef CONFIG_CMD_TFTPPUT
Simon Glass1fb7cd42011-10-24 18:00:07 +0000448static void icmp_handler(unsigned type, unsigned code, unsigned dest,
Joe Hershberger049a95a2015-04-08 01:41:01 -0500449 struct in_addr sip, unsigned src, uchar *pkt,
450 unsigned len)
Simon Glass1fb7cd42011-10-24 18:00:07 +0000451{
452 if (type == ICMP_NOT_REACH && code == ICMP_NOT_REACH_PORT) {
453 /* Oh dear the other end has gone away */
454 restart("TFTP server died");
455 }
456}
Simon Glass39bccd22011-10-26 14:18:38 +0000457#endif
Simon Glass1fb7cd42011-10-24 18:00:07 +0000458
Joe Hershberger049a95a2015-04-08 01:41:01 -0500459static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
460 unsigned src, unsigned len)
wdenkfe8c2802002-11-03 00:38:21 +0000461{
Kim Phillips61fdd4f2013-01-16 18:09:19 -0600462 __be16 proto;
463 __be16 *s;
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200464 int i;
wdenkfe8c2802002-11-03 00:38:21 +0000465
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500466 if (dest != tftp_our_port) {
David Updegraff53a5c422007-06-11 10:41:07 -0500467#ifdef CONFIG_MCAST_TFTP
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500468 if (tftp_mcast_active &&
469 (!tftp_mcast_port || dest != tftp_mcast_port))
David Updegraff53a5c422007-06-11 10:41:07 -0500470#endif
Luca Ceresoli0bdd8ac2011-05-14 05:50:02 +0000471 return;
wdenkfe8c2802002-11-03 00:38:21 +0000472 }
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500473 if (tftp_state != STATE_SEND_RRQ && src != tftp_remote_port &&
474 tftp_state != STATE_RECV_WRQ && tftp_state != STATE_SEND_WRQ)
wdenkfe8c2802002-11-03 00:38:21 +0000475 return;
wdenkfe8c2802002-11-03 00:38:21 +0000476
Luca Ceresoli7bc325a2011-05-14 05:50:00 +0000477 if (len < 2)
wdenkfe8c2802002-11-03 00:38:21 +0000478 return;
wdenkfe8c2802002-11-03 00:38:21 +0000479 len -= 2;
480 /* warning: don't use increment (++) in ntohs() macros!! */
Kim Phillips61fdd4f2013-01-16 18:09:19 -0600481 s = (__be16 *)pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200482 proto = *s++;
483 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000484 switch (ntohs(proto)) {
wdenkfe8c2802002-11-03 00:38:21 +0000485 case TFTP_RRQ:
wdenkfe8c2802002-11-03 00:38:21 +0000486 break;
Simon Glass1fb7cd42011-10-24 18:00:07 +0000487
488 case TFTP_ACK:
489#ifdef CONFIG_CMD_TFTPPUT
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500490 if (tftp_put_active) {
491 if (tftp_put_final_block_sent) {
Simon Glass1fb7cd42011-10-24 18:00:07 +0000492 tftp_complete();
493 } else {
494 /*
495 * Move to the next block. We want our block
496 * count to wrap just like the other end!
497 */
498 int block = ntohs(*s);
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500499 int ack_ok = (tftp_cur_block == block);
Simon Glass1fb7cd42011-10-24 18:00:07 +0000500
Ajay Kishore3491df22016-09-15 15:29:02 +0530501 tftp_prev_block = tftp_cur_block;
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500502 tftp_cur_block = (unsigned short)(block + 1);
Simon Glass1fb7cd42011-10-24 18:00:07 +0000503 update_block_number();
504 if (ack_ok)
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500505 tftp_send(); /* Send next data block */
Simon Glass1fb7cd42011-10-24 18:00:07 +0000506 }
507 }
508#endif
509 break;
510
wdenkfe8c2802002-11-03 00:38:21 +0000511 default:
512 break;
513
Luca Ceresolie59e3562011-05-17 00:03:39 +0000514#ifdef CONFIG_CMD_TFTPSRV
515 case TFTP_WRQ:
516 debug("Got WRQ\n");
Joe Hershberger049a95a2015-04-08 01:41:01 -0500517 tftp_remote_ip = sip;
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500518 tftp_remote_port = src;
519 tftp_our_port = 1024 + (get_timer(0) % 3072);
Simon Glasse4cde2f2011-10-24 18:00:04 +0000520 new_transfer();
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500521 tftp_send(); /* Send ACK(0) */
Luca Ceresolie59e3562011-05-17 00:03:39 +0000522 break;
523#endif
524
wdenkfbe4b5c2003-10-06 21:55:32 +0000525 case TFTP_OACK:
Wolfgang Denkd3717082009-08-10 09:59:10 +0200526 debug("Got OACK: %s %s\n",
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500527 pkt, pkt + strlen((char *)pkt) + 1);
528 tftp_state = STATE_OACK;
529 tftp_remote_port = src;
Wolfgang Denk60174742007-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 Ceresolic718b142011-05-14 05:49:57 +0000535 for (i = 0; i+8 < len; i++) {
Joe Hershberger8885c5f2015-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 Getz0ebf04c2009-07-23 03:01:03 -0400540 debug("Blocksize ack: %s, %d\n",
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500541 (char *)pkt + i + 8, tftp_block_size);
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200542 }
Robin Getz4fccb812009-08-20 10:50:20 -0400543#ifdef CONFIG_TFTP_TSIZE
Luca Ceresoli2e320252011-05-14 05:49:58 +0000544 if (strcmp((char *)pkt+i, "tsize") == 0) {
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500545 tftp_tsize = simple_strtoul((char *)pkt + i + 6,
Luca Ceresoli2f094132011-05-14 05:49:56 +0000546 NULL, 10);
Robin Getz4fccb812009-08-20 10:50:20 -0400547 debug("size = %s, %d\n",
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500548 (char *)pkt + i + 6, tftp_tsize);
Robin Getz4fccb812009-08-20 10:50:20 -0400549 }
550#endif
David Updegraff53a5c422007-06-11 10:41:07 -0500551 }
552#ifdef CONFIG_MCAST_TFTP
Joe Hershberger8885c5f2015-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 Updegraff53a5c422007-06-11 10:41:07 -0500556 else
557#endif
Simon Glass1fb7cd42011-10-24 18:00:07 +0000558#ifdef CONFIG_CMD_TFTPPUT
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500559 if (tftp_put_active) {
Simon Glass1fb7cd42011-10-24 18:00:07 +0000560 /* Get ready to send the first block */
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500561 tftp_state = STATE_DATA;
562 tftp_cur_block++;
Simon Glass1fb7cd42011-10-24 18:00:07 +0000563 }
564#endif
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500565 tftp_send(); /* Send ACK or first data block */
wdenkfbe4b5c2003-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 Hershberger8885c5f2015-04-08 01:41:07 -0500571 tftp_cur_block = ntohs(*(__be16 *)pkt);
wdenk3f85ce22004-02-23 16:11:30 +0000572
Simon Glasse4cde2f2011-10-24 18:00:04 +0000573 update_block_number();
wdenkfe8c2802002-11-03 00:38:21 +0000574
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500575 if (tftp_state == STATE_SEND_RRQ)
Robin Getz0ebf04c2009-07-23 03:01:03 -0400576 debug("Server did not acknowledge timeout option!\n");
wdenkfbe4b5c2003-10-06 21:55:32 +0000577
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500578 if (tftp_state == STATE_SEND_RRQ || tftp_state == STATE_OACK ||
579 tftp_state == STATE_RECV_WRQ) {
wdenk3f85ce22004-02-23 16:11:30 +0000580 /* first block received */
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500581 tftp_state = STATE_DATA;
582 tftp_remote_port = src;
Simon Glasse4cde2f2011-10-24 18:00:04 +0000583 new_transfer();
wdenkfe8c2802002-11-03 00:38:21 +0000584
David Updegraff53a5c422007-06-11 10:41:07 -0500585#ifdef CONFIG_MCAST_TFTP
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500586 if (tftp_mcast_active) { /* start!=1 common if mcast */
587 tftp_prev_block = tftp_cur_block - 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500588 } else
589#endif
Joe Hershberger8885c5f2015-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 Hershbergerbc0571f2015-04-08 01:41:21 -0500595 net_start_again();
wdenkfe8c2802002-11-03 00:38:21 +0000596 break;
597 }
598 }
599
Joe Hershberger8885c5f2015-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 Hershberger8885c5f2015-04-08 01:41:07 -0500605 tftp_prev_block = tftp_cur_block;
Albert ARIBAUD \(3ADEV\)f5fb7342015-10-12 00:02:57 +0200606 timeout_count_max = tftp_timeout_count_max;
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500607 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
wdenkfe8c2802002-11-03 00:38:21 +0000608
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500609 store_block(tftp_cur_block - 1, pkt + 2, len);
wdenkfe8c2802002-11-03 00:38:21 +0000610
611 /*
Luca Ceresoli4d69e982011-05-17 00:03:41 +0000612 * Acknowledge the block just received, which will prompt
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000613 * the remote for the next one.
wdenkfe8c2802002-11-03 00:38:21 +0000614 */
David Updegraff53a5c422007-06-11 10:41:07 -0500615#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-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-VILLARDa93907c2008-01-18 01:14:03 +0100618 */
Joe Hershberger8885c5f2015-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 Updegraff53a5c422007-06-11 10:41:07 -0500631 /* try to double it and retry */
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500632 tftp_mcast_bitmap_size <<= 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500633 mcast_cleanup();
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500634 net_start_again();
David Updegraff53a5c422007-06-11 10:41:07 -0500635 return;
636 }
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500637 tftp_prev_block = tftp_cur_block;
David Updegraff53a5c422007-06-11 10:41:07 -0500638 }
639 }
640#endif
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500641 tftp_send();
wdenkfe8c2802002-11-03 00:38:21 +0000642
David Updegraff53a5c422007-06-11 10:41:07 -0500643#ifdef CONFIG_MCAST_TFTP
Joe Hershberger8885c5f2015-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 Ceresolic718b142011-05-14 05:49:57 +0000647 puts("\nMulticast tftp done\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500648 mcast_cleanup();
Joe Hershberger22f6e992012-05-23 07:59:14 +0000649 net_set_state(NETLOOP_SUCCESS);
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200650 }
Joe Hershberger13dfe942012-05-15 08:59:12 +0000651 } else
David Updegraff53a5c422007-06-11 10:41:07 -0500652#endif
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500653 if (len < tftp_block_size)
Simon Glassf5329bb2011-10-24 18:00:03 +0000654 tftp_complete();
wdenkfe8c2802002-11-03 00:38:21 +0000655 break;
656
657 case TFTP_ERROR:
Luca Ceresolic718b142011-05-14 05:49:57 +0000658 printf("\nTFTP error: '%s' (%d)\n",
Kim Phillips61fdd4f2013-01-16 18:09:19 -0600659 pkt + 2, ntohs(*(__be16 *)pkt));
Remy Bohmeraafda382009-10-28 22:13:40 +0100660
Kim Phillips61fdd4f2013-01-16 18:09:19 -0600661 switch (ntohs(*(__be16 *)pkt)) {
Remy Bohmeraafda382009-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 Hershberger22f6e992012-05-23 07:59:14 +0000666 net_set_state(NETLOOP_FAIL);
Remy Bohmeraafda382009-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 Updegraff53a5c422007-06-11 10:41:07 -0500675#ifdef CONFIG_MCAST_TFTP
Remy Bohmeraafda382009-10-28 22:13:40 +0100676 mcast_cleanup();
David Updegraff53a5c422007-06-11 10:41:07 -0500677#endif
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500678 net_start_again();
Remy Bohmeraafda382009-10-28 22:13:40 +0100679 break;
680 }
wdenkfe8c2802002-11-03 00:38:21 +0000681 break;
682 }
683}
684
685
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500686static void tftp_timeout_handler(void)
wdenkfe8c2802002-11-03 00:38:21 +0000687{
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500688 if (++timeout_count > timeout_count_max) {
Simon Glasse4cde2f2011-10-24 18:00:04 +0000689 restart("Retry count exceeded");
wdenkfe8c2802002-11-03 00:38:21 +0000690 } else {
Luca Ceresolic718b142011-05-14 05:49:57 +0000691 puts("T ");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500692 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
Joe Hershberger8885c5f2015-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 Hershberger8885c5f2015-04-08 01:41:07 -0500699void tftp_start(enum proto_t protocol)
wdenkfe8c2802002-11-03 00:38:21 +0000700{
Albert ARIBAUD \(3ADEV\)f5fb7342015-10-12 00:02:57 +0200701#if CONFIG_NET_TFTP_VARS
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200702 char *ep; /* Environment pointer */
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200703
Wolfgang Denkc96f86e2010-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\)f5fb7342015-10-12 00:02:57 +0200708
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000709 ep = getenv("tftpblocksize");
710 if (ep != NULL)
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500711 tftp_block_size_option = simple_strtol(ep, NULL, 10);
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100712
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000713 ep = getenv("tftptimeout");
714 if (ep != NULL)
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500715 timeout_ms = simple_strtol(ep, NULL, 10);
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100716
Bin Mengaf2ca592015-08-27 22:25:51 -0700717 if (timeout_ms < 1000) {
718 printf("TFTP timeout (%ld ms) too low, set min = 1000 ms\n",
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500719 timeout_ms);
Bin Mengaf2ca592015-08-27 22:25:51 -0700720 timeout_ms = 1000;
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100721 }
722
Albert ARIBAUD \(3ADEV\)f5fb7342015-10-12 00:02:57 +0200723 ep = getenv("tftptimeoutcountmax");
724 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 Denkc96f86e2010-01-17 23:55:53 +0100734 debug("TFTP blocksize = %i, timeout = %ld ms\n",
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500735 tftp_block_size_option, timeout_ms);
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200736
Joe Hershberger049a95a2015-04-08 01:41:01 -0500737 tftp_remote_ip = net_server_ip;
Joe Hershberger14111572015-04-08 01:41:02 -0500738 if (net_boot_file_name[0] == '\0') {
Matthias Weisserea45cb02011-12-03 03:29:44 +0000739 sprintf(default_filename, "%02X%02X%02X%02X.img",
Joe Hershberger049a95a2015-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-VILLARDa93907c2008-01-18 01:14:03 +0100744
745 strncpy(tftp_filename, default_filename, MAX_LEN);
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500746 tftp_filename[MAX_LEN - 1] = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000747
Luca Ceresolic718b142011-05-14 05:49:57 +0000748 printf("*** Warning: no boot file name; using '%s'\n",
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500749 tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000750 } else {
Joe Hershberger14111572015-04-08 01:41:02 -0500751 char *p = strchr(net_boot_file_name, ':');
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100752
753 if (p == NULL) {
Joe Hershberger14111572015-04-08 01:41:02 -0500754 strncpy(tftp_filename, net_boot_file_name, MAX_LEN);
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500755 tftp_filename[MAX_LEN - 1] = 0;
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100756 } else {
Joe Hershberger14111572015-04-08 01:41:02 -0500757 tftp_remote_ip = string_to_ip(net_boot_file_name);
Peter Tyser6a86bb62008-12-01 16:29:38 -0600758 strncpy(tftp_filename, p + 1, MAX_LEN);
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500759 tftp_filename[MAX_LEN - 1] = 0;
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100760 }
wdenkfe8c2802002-11-03 00:38:21 +0000761 }
762
Luca Ceresolic718b142011-05-14 05:49:57 +0000763 printf("Using %s device\n", eth_get_name());
Simon Glass1fb7cd42011-10-24 18:00:07 +0000764 printf("TFTP %s server %pI4; our IP address is %pI4",
Simon Glass8c6914f2011-10-27 06:24:29 +0000765#ifdef CONFIG_CMD_TFTPPUT
766 protocol == TFTPPUT ? "to" : "from",
767#else
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500768 "from",
Simon Glass8c6914f2011-10-27 06:24:29 +0000769#endif
Joe Hershberger8885c5f2015-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 Hershberger049a95a2015-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 Hershberger049a95a2015-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 Ceresolic718b142011-05-14 05:49:57 +0000782 putc('\n');
wdenkfe8c2802002-11-03 00:38:21 +0000783
Luca Ceresolic718b142011-05-14 05:49:57 +0000784 printf("Filename '%s'.", tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000785
Joe Hershberger14111572015-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 Ceresolic718b142011-05-14 05:49:57 +0000792 putc('\n');
Simon Glass1fb7cd42011-10-24 18:00:07 +0000793#ifdef CONFIG_CMD_TFTPPUT
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500794 tftp_put_active = (protocol == TFTPPUT);
795 if (tftp_put_active) {
Simon Glass1fb7cd42011-10-24 18:00:07 +0000796 printf("Save address: 0x%lx\n", save_addr);
797 printf("Save size: 0x%lx\n", save_size);
Joe Hershberger14111572015-04-08 01:41:02 -0500798 net_boot_file_size = save_size;
Simon Glass1fb7cd42011-10-24 18:00:07 +0000799 puts("Saving: *\b");
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500800 tftp_state = STATE_SEND_WRQ;
Simon Glass1fb7cd42011-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 Hershberger8885c5f2015-04-08 01:41:07 -0500807 tftp_state = STATE_SEND_RRQ;
Simon Glass1fb7cd42011-10-24 18:00:07 +0000808 }
wdenkfe8c2802002-11-03 00:38:21 +0000809
Simon Glass85b19802012-10-11 13:57:36 +0000810 time_start = get_timer(0);
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500811 timeout_count_max = tftp_timeout_count_max;
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200812
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500813 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
Joe Hershberger049a95a2015-04-08 01:41:01 -0500814 net_set_udp_handler(tftp_handler);
Simon Glass39bccd22011-10-26 14:18:38 +0000815#ifdef CONFIG_CMD_TFTPPUT
Simon Glass1fb7cd42011-10-24 18:00:07 +0000816 net_set_icmp_handler(icmp_handler);
Simon Glass39bccd22011-10-26 14:18:38 +0000817#endif
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500818 tftp_remote_port = WELL_KNOWN_PORT;
819 timeout_count = 0;
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200820 /* Use a pseudo-random port unless a specific port is set */
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500821 tftp_our_port = 1024 + (get_timer(0) % 3072);
David Updegraff53a5c422007-06-11 10:41:07 -0500822
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200823#ifdef CONFIG_TFTP_PORT
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000824 ep = getenv("tftpdstp");
Luca Ceresoli7bc325a2011-05-14 05:50:00 +0000825 if (ep != NULL)
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500826 tftp_remote_port = simple_strtol(ep, NULL, 10);
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000827 ep = getenv("tftpsrcp");
Luca Ceresoli7bc325a2011-05-14 05:50:00 +0000828 if (ep != NULL)
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500829 tftp_our_port = simple_strtol(ep, NULL, 10);
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200830#endif
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500831 tftp_cur_block = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000832
wdenk73a8b272003-06-05 19:27:42 +0000833 /* zero out server ether in case the server ip has changed */
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500834 memset(net_server_ethaddr, 0, 6);
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500835 /* Revert tftp_block_size to dflt */
836 tftp_block_size = TFTP_BLOCK_SIZE;
David Updegraff53a5c422007-06-11 10:41:07 -0500837#ifdef CONFIG_MCAST_TFTP
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100838 mcast_cleanup();
David Updegraff53a5c422007-06-11 10:41:07 -0500839#endif
Robin Getz4fccb812009-08-20 10:50:20 -0400840#ifdef CONFIG_TFTP_TSIZE
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500841 tftp_tsize = 0;
842 tftp_tsize_num_hash = 0;
Robin Getz4fccb812009-08-20 10:50:20 -0400843#endif
wdenk73a8b272003-06-05 19:27:42 +0000844
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500845 tftp_send();
wdenkfe8c2802002-11-03 00:38:21 +0000846}
847
Luca Ceresolie59e3562011-05-17 00:03:39 +0000848#ifdef CONFIG_CMD_TFTPSRV
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500849void tftp_start_server(void)
Luca Ceresolie59e3562011-05-17 00:03:39 +0000850{
851 tftp_filename[0] = 0;
852
Luca Ceresolie59e3562011-05-17 00:03:39 +0000853 printf("Using %s device\n", eth_get_name());
Joe Hershberger049a95a2015-04-08 01:41:01 -0500854 printf("Listening for TFTP transfer on %pI4\n", &net_ip);
Luca Ceresolie59e3562011-05-17 00:03:39 +0000855 printf("Load address: 0x%lx\n", load_addr);
856
857 puts("Loading: *\b");
858
Albert ARIBAUD \(3ADEV\)f5fb7342015-10-12 00:02:57 +0200859 timeout_count_max = tftp_timeout_count_max;
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500860 timeout_count = 0;
861 timeout_ms = TIMEOUT;
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500862 net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
Luca Ceresolie59e3562011-05-17 00:03:39 +0000863
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500864 /* Revert tftp_block_size to dflt */
865 tftp_block_size = TFTP_BLOCK_SIZE;
866 tftp_cur_block = 0;
867 tftp_our_port = WELL_KNOWN_PORT;
Luca Ceresolie59e3562011-05-17 00:03:39 +0000868
869#ifdef CONFIG_TFTP_TSIZE
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500870 tftp_tsize = 0;
871 tftp_tsize_num_hash = 0;
Luca Ceresolie59e3562011-05-17 00:03:39 +0000872#endif
873
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500874 tftp_state = STATE_RECV_WRQ;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500875 net_set_udp_handler(tftp_handler);
Andrew Ruder8e525332013-10-22 19:10:28 -0500876
877 /* zero out server ether in case the server ip has changed */
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500878 memset(net_server_ethaddr, 0, 6);
Luca Ceresolie59e3562011-05-17 00:03:39 +0000879}
880#endif /* CONFIG_CMD_TFTPSRV */
881
David Updegraff53a5c422007-06-11 10:41:07 -0500882#ifdef CONFIG_MCAST_TFTP
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500883/*
884 * Credits: atftp project.
David Updegraff53a5c422007-06-11 10:41:07 -0500885 */
886
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500887/*
888 * Pick up BcastAddr, Port, and whether I am [now] the master-client.
David Updegraff53a5c422007-06-11 10:41:07 -0500889 * Frame:
890 * +-------+-----------+---+-------~~-------+---+
891 * | opc | multicast | 0 | addr, port, mc | 0 |
892 * +-------+-----------+---+-------~~-------+---+
893 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
894 * I am the new master-client so must send ACKs to DataBlocks. If I am not
895 * master-client, I'm a passive client, gathering what DataBlocks I may and
896 * making note of which ones I got in my bitmask.
897 * In theory, I never go from master->passive..
898 * .. this comes in with pkt already pointing just past opc
899 */
900static void parse_multicast_oack(char *pkt, int len)
901{
Luca Ceresolic718b142011-05-14 05:49:57 +0000902 int i;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500903 struct in_addr addr;
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500904 char *mc_adr;
905 char *port;
906 char *mc;
David Updegraff53a5c422007-06-11 10:41:07 -0500907
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500908 mc_adr = NULL;
909 port = NULL;
910 mc = NULL;
David Updegraff53a5c422007-06-11 10:41:07 -0500911 /* march along looking for 'multicast\0', which has to start at least
912 * 14 bytes back from the end.
913 */
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500914 for (i = 0; i < len - 14; i++)
915 if (strcmp(pkt + i, "multicast") == 0)
David Updegraff53a5c422007-06-11 10:41:07 -0500916 break;
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500917 if (i >= (len - 14)) /* non-Multicast OACK, ign. */
David Updegraff53a5c422007-06-11 10:41:07 -0500918 return;
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200919
Luca Ceresolic718b142011-05-14 05:49:57 +0000920 i += 10; /* strlen multicast */
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500921 mc_adr = pkt + i;
Luca Ceresolic718b142011-05-14 05:49:57 +0000922 for (; i < len; i++) {
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500923 if (*(pkt + i) == ',') {
924 *(pkt + i) = '\0';
David Updegraff53a5c422007-06-11 10:41:07 -0500925 if (port) {
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500926 mc = pkt + i + 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500927 break;
928 } else {
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500929 port = pkt + i + 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500930 }
931 }
932 }
Luca Ceresoli6d2231e2011-05-14 05:50:01 +0000933 if (!port || !mc_adr || !mc)
934 return;
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500935 if (tftp_mcast_active && tftp_mcast_master_client) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000936 printf("I got a OACK as master Client, WRONG!\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500937 return;
938 }
939 /* ..I now accept packets destined for this MCAST addr, port */
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500940 if (!tftp_mcast_active) {
941 if (tftp_mcast_bitmap) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000942 printf("Internal failure! no mcast.\n");
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500943 free(tftp_mcast_bitmap);
944 tftp_mcast_bitmap = NULL;
945 tftp_mcast_disabled = 1;
946 return;
David Updegraff53a5c422007-06-11 10:41:07 -0500947 }
948 /* I malloc instead of pre-declare; so that if the file ends
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200949 * up being too big for this bitmap I can retry
950 */
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500951 tftp_mcast_bitmap = malloc(tftp_mcast_bitmap_size);
952 if (!tftp_mcast_bitmap) {
953 printf("No bitmap, no multicast. Sorry.\n");
954 tftp_mcast_disabled = 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500955 return;
956 }
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500957 memset(tftp_mcast_bitmap, 0, tftp_mcast_bitmap_size);
958 tftp_mcast_prev_hole = 0;
959 tftp_mcast_active = 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500960 }
961 addr = string_to_ip(mc_adr);
Joe Hershberger049a95a2015-04-08 01:41:01 -0500962 if (net_mcast_addr.s_addr != addr.s_addr) {
963 if (net_mcast_addr.s_addr)
964 eth_mcast_join(net_mcast_addr, 0);
965 net_mcast_addr = addr;
966 if (eth_mcast_join(net_mcast_addr, 1)) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000967 printf("Fail to set mcast, revert to TFTP\n");
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500968 tftp_mcast_disabled = 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500969 mcast_cleanup();
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500970 net_start_again();
David Updegraff53a5c422007-06-11 10:41:07 -0500971 }
972 }
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500973 tftp_mcast_master_client = simple_strtoul((char *)mc, NULL, 10);
974 tftp_mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
975 printf("Multicast: %s:%d [%d]\n", mc_adr, tftp_mcast_port,
976 tftp_mcast_master_client);
David Updegraff53a5c422007-06-11 10:41:07 -0500977 return;
978}
979
980#endif /* Multicast TFTP */