blob: f25abaab49229bf30de5d33dff7424650ca948ce [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>
Joe Hershbergered191852015-03-22 17:09:08 -050011#include <mapmem.h>
wdenkfe8c2802002-11-03 00:38:21 +000012#include <net.h>
13#include "tftp.h"
14#include "bootp.h"
Joe Hershberger3a29e5a2012-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 Ceresolic42fc272011-05-14 05:49:56 +000019/* Well known TFTP port # */
20#define WELL_KNOWN_PORT 69
21/* Millisecs to timeout for lost pkt */
22#define TIMEOUT 5000UL
wdenkfe8c2802002-11-03 00:38:21 +000023#ifndef CONFIG_NET_RETRY_COUNT
Luca Ceresolic42fc272011-05-14 05:49:56 +000024/* # of timeouts before giving up */
25# define TIMEOUT_COUNT 10
wdenkfe8c2802002-11-03 00:38:21 +000026#else
27# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
28#endif
Luca Ceresolic42fc272011-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
wdenk7182b0f2003-10-06 21:55:32 +000040#define TFTP_OACK 6
wdenkfe8c2802002-11-03 00:38:21 +000041
Bartlomiej Sieka371cde72008-10-01 15:26:29 +020042static ulong TftpTimeoutMSecs = TIMEOUT;
43static int TftpTimeoutCountMax = TIMEOUT_COUNT;
Simon Glasscab9a8d2012-10-11 13:57:36 +000044static ulong time_start; /* Record time we started tftp */
Bartlomiej Sieka371cde72008-10-01 15:26:29 +020045
46/*
47 * These globals govern the timeout behavior when attempting a connection to a
48 * TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to
49 * wait for the server to respond to initial connection. Second global,
50 * TftpRRQTimeoutCountMax, gives the number of such connection retries.
51 * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be
52 * positive. The globals are meant to be set (and restored) by code needing
53 * non-standard timeout behavior when initiating a TFTP transfer.
54 */
55ulong TftpRRQTimeoutMSecs = TIMEOUT;
56int TftpRRQTimeoutCountMax = TIMEOUT_COUNT;
57
Remy Bohmerb46293f2009-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 Hershberger5874dec2015-04-08 01:41:01 -050068static struct in_addr tftp_remote_ip;
Luca Ceresolic42fc272011-05-14 05:49:56 +000069/* The UDP port at their end */
Luca Ceresoli5079c762011-05-17 00:03:37 +000070static int TftpRemotePort;
Luca Ceresolic42fc272011-05-14 05:49:56 +000071/* The UDP port at our end */
72static int TftpOurPort;
wdenkfe8c2802002-11-03 00:38:21 +000073static int TftpTimeoutCount;
Luca Ceresolic42fc272011-05-14 05:49:56 +000074/* packet sequence number */
75static ulong TftpBlock;
76/* last packet sequence number received */
77static ulong TftpLastBlock;
78/* count of sequence number wraparounds */
79static ulong TftpBlockWrap;
80/* memory offset due to wrapping */
81static ulong TftpBlockWrapOffset;
wdenkfe8c2802002-11-03 00:38:21 +000082static int TftpState;
Robin Getzb3b00ed2009-08-20 10:50:20 -040083#ifdef CONFIG_TFTP_TSIZE
Luca Ceresolic42fc272011-05-14 05:49:56 +000084/* The file size reported by the server */
85static int TftpTsize;
86/* The number of hashes we printed */
87static short TftpNumchars;
Robin Getzb3b00ed2009-08-20 10:50:20 -040088#endif
Simon Glass6a398d22011-10-24 18:00:07 +000089#ifdef CONFIG_CMD_TFTPPUT
90static int TftpWriting; /* 1 if writing, else 0 */
91static int TftpFinalBlock; /* 1 if we have sent the last block */
92#else
93#define TftpWriting 0
94#endif
wdenkef893942004-02-23 16:11:30 +000095
Luca Ceresoli562dfe52011-05-17 00:03:38 +000096#define STATE_SEND_RRQ 1
wdenkfe8c2802002-11-03 00:38:21 +000097#define STATE_DATA 2
98#define STATE_TOO_LARGE 3
99#define STATE_BAD_MAGIC 4
wdenk7182b0f2003-10-06 21:55:32 +0000100#define STATE_OACK 5
Luca Ceresolib640ed32011-05-17 00:03:39 +0000101#define STATE_RECV_WRQ 6
Simon Glass6a398d22011-10-24 18:00:07 +0000102#define STATE_SEND_WRQ 7
wdenkfe8c2802002-11-03 00:38:21 +0000103
Luca Ceresolic42fc272011-05-14 05:49:56 +0000104/* default TFTP block size */
105#define TFTP_BLOCK_SIZE 512
106/* sequence number is 16 bit */
107#define TFTP_SEQUENCE_SIZE ((ulong)(1<<16))
wdenkef893942004-02-23 16:11:30 +0000108
wdenkfe8c2802002-11-03 00:38:21 +0000109#define DEFAULT_NAME_LEN (8 + 4 + 1)
110static char default_filename[DEFAULT_NAME_LEN];
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100111
112#ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
113#define MAX_LEN 128
114#else
115#define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
116#endif
117
118static char tftp_filename[MAX_LEN];
wdenkfe8c2802002-11-03 00:38:21 +0000119
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200120/* 512 is poor choice for ethernet, MTU is typically 1500.
121 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
David Updegraff7280da72007-06-11 10:41:07 -0500122 * almost-MTU block sizes. At least try... fall back to 512 if need be.
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200123 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
David Updegraff7280da72007-06-11 10:41:07 -0500124 */
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200125#ifdef CONFIG_TFTP_BLOCKSIZE
126#define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
127#else
David Updegraff7280da72007-06-11 10:41:07 -0500128#define TFTP_MTU_BLOCKSIZE 1468
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200129#endif
130
Luca Ceresolif40538a2011-05-14 05:49:57 +0000131static unsigned short TftpBlkSize = TFTP_BLOCK_SIZE;
132static unsigned short TftpBlkSizeOption = TFTP_MTU_BLOCKSIZE;
David Updegraff7280da72007-06-11 10:41:07 -0500133
134#ifdef CONFIG_MCAST_TFTP
135#include <malloc.h>
136#define MTFTP_BITMAPSIZE 0x1000
137static unsigned *Bitmap;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000138static int PrevBitmapHole, Mapsize = MTFTP_BITMAPSIZE;
Luca Ceresoli4e7a02b2011-05-14 05:50:03 +0000139static uchar ProhibitMcast, MasterClient;
140static uchar Multicast;
David Updegraff7280da72007-06-11 10:41:07 -0500141static int Mcast_port;
142static ulong TftpEndingBlock; /* can get 'last' block before done..*/
143
Luca Ceresolif40538a2011-05-14 05:49:57 +0000144static void parse_multicast_oack(char *pkt, int len);
David Updegraff7280da72007-06-11 10:41:07 -0500145
146static void
147mcast_cleanup(void)
148{
Joe Hershberger5874dec2015-04-08 01:41:01 -0500149 if (net_mcast_addr)
150 eth_mcast_join(net_mcast_addr, 0);
Luca Ceresoli6072fcd2011-05-14 05:50:01 +0000151 if (Bitmap)
152 free(Bitmap);
Luca Ceresolif40538a2011-05-14 05:49:57 +0000153 Bitmap = NULL;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500154 net_mcast_addr.s_addr = 0;
155 Multicast = 0;
156 Mcast_port = 0;
David Updegraff7280da72007-06-11 10:41:07 -0500157 TftpEndingBlock = -1;
158}
159
160#endif /* CONFIG_MCAST_TFTP */
161
Joe Hershberger3a29e5a2012-05-15 08:59:12 +0000162static inline void
Jayachandran Chandrasekharan Naira5d451b2012-07-10 11:48:54 +0530163store_block(int block, uchar *src, unsigned len)
wdenkfe8c2802002-11-03 00:38:21 +0000164{
David Updegraff7280da72007-06-11 10:41:07 -0500165 ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
wdenkef893942004-02-23 16:11:30 +0000166 ulong newsize = offset + len;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200167#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
wdenkfe8c2802002-11-03 00:38:21 +0000168 int i, rc = 0;
169
Luca Ceresolif40538a2011-05-14 05:49:57 +0000170 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenkfe8c2802002-11-03 00:38:21 +0000171 /* start address in flash? */
Jochen Friedrichd31a59b2008-09-02 11:24:59 +0200172 if (flash_info[i].flash_id == FLASH_UNKNOWN)
173 continue;
wdenkfe8c2802002-11-03 00:38:21 +0000174 if (load_addr + offset >= flash_info[i].start[0]) {
175 rc = 1;
176 break;
177 }
178 }
179
180 if (rc) { /* Flash is destination for this packet */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000181 rc = flash_write((char *)src, (ulong)(load_addr+offset), len);
wdenkfe8c2802002-11-03 00:38:21 +0000182 if (rc) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000183 flash_perror(rc);
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000184 net_set_state(NETLOOP_FAIL);
wdenkfe8c2802002-11-03 00:38:21 +0000185 return;
186 }
Joe Hershberger3a29e5a2012-05-15 08:59:12 +0000187 } else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200188#endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000189 {
Joe Hershbergered191852015-03-22 17:09:08 -0500190 void *ptr = map_sysmem(load_addr + offset, len);
191
192 memcpy(ptr, src, len);
193 unmap_sysmem(ptr);
wdenkfe8c2802002-11-03 00:38:21 +0000194 }
David Updegraff7280da72007-06-11 10:41:07 -0500195#ifdef CONFIG_MCAST_TFTP
196 if (Multicast)
197 ext2_set_bit(block, Bitmap);
198#endif
wdenkfe8c2802002-11-03 00:38:21 +0000199
Joe Hershberger290c8992015-04-08 01:41:02 -0500200 if (net_boot_file_size < newsize)
201 net_boot_file_size = newsize;
wdenkfe8c2802002-11-03 00:38:21 +0000202}
203
Simon Glassbd8a0e02011-10-24 18:00:04 +0000204/* Clear our state ready for a new transfer */
Simon Glass84313322011-10-27 06:24:28 +0000205static void new_transfer(void)
Simon Glassbd8a0e02011-10-24 18:00:04 +0000206{
207 TftpLastBlock = 0;
208 TftpBlockWrap = 0;
209 TftpBlockWrapOffset = 0;
210#ifdef CONFIG_CMD_TFTPPUT
211 TftpFinalBlock = 0;
212#endif
213}
Simon Glass6a398d22011-10-24 18:00:07 +0000214
215#ifdef CONFIG_CMD_TFTPPUT
216/**
217 * Load the next block from memory to be sent over tftp.
218 *
219 * @param block Block number to send
220 * @param dst Destination buffer for data
221 * @param len Number of bytes in block (this one and every other)
222 * @return number of bytes loaded
223 */
224static int load_block(unsigned block, uchar *dst, unsigned len)
225{
226 /* We may want to get the final block from the previous set */
227 ulong offset = ((int)block - 1) * len + TftpBlockWrapOffset;
228 ulong tosend = len;
229
Joe Hershberger290c8992015-04-08 01:41:02 -0500230 tosend = min(net_boot_file_size - offset, tosend);
Simon Glass6a398d22011-10-24 18:00:07 +0000231 (void)memcpy(dst, (void *)(save_addr + offset), tosend);
232 debug("%s: block=%d, offset=%ld, len=%d, tosend=%ld\n", __func__,
233 block, offset, len, tosend);
234 return tosend;
235}
236#endif
Simon Glassbd8a0e02011-10-24 18:00:04 +0000237
Luca Ceresolif40538a2011-05-14 05:49:57 +0000238static void TftpSend(void);
239static void TftpTimeout(void);
wdenkfe8c2802002-11-03 00:38:21 +0000240
241/**********************************************************************/
242
Simon Glassb437aad2011-10-24 18:00:03 +0000243static void show_block_marker(void)
244{
245#ifdef CONFIG_TFTP_TSIZE
246 if (TftpTsize) {
247 ulong pos = TftpBlock * TftpBlkSize + TftpBlockWrapOffset;
248
249 while (TftpNumchars < pos * 50 / TftpTsize) {
250 putc('#');
251 TftpNumchars++;
252 }
Simon Glass6a398d22011-10-24 18:00:07 +0000253 } else
Simon Glassb437aad2011-10-24 18:00:03 +0000254#endif
Simon Glass6a398d22011-10-24 18:00:07 +0000255 {
Simon Glassb437aad2011-10-24 18:00:03 +0000256 if (((TftpBlock - 1) % 10) == 0)
257 putc('#');
258 else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0)
259 puts("\n\t ");
260 }
261}
262
Simon Glassbd8a0e02011-10-24 18:00:04 +0000263/**
264 * restart the current transfer due to an error
265 *
266 * @param msg Message to print for user
267 */
268static void restart(const char *msg)
269{
270 printf("\n%s; starting again\n", msg);
271#ifdef CONFIG_MCAST_TFTP
272 mcast_cleanup();
273#endif
274 NetStartAgain();
275}
276
277/*
278 * Check if the block number has wrapped, and update progress
279 *
280 * TODO: The egregious use of global variables in this file should be tidied.
281 */
282static void update_block_number(void)
283{
284 /*
285 * RFC1350 specifies that the first data packet will
286 * have sequence number 1. If we receive a sequence
287 * number of 0 this means that there was a wrap
288 * around of the (16 bit) counter.
289 */
rockly0d757362013-08-03 18:09:05 +0800290 if (TftpBlock == 0 && TftpLastBlock != 0) {
Simon Glassbd8a0e02011-10-24 18:00:04 +0000291 TftpBlockWrap++;
292 TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE;
293 TftpTimeoutCount = 0; /* we've done well, reset thhe timeout */
294 } else {
295 show_block_marker();
296 }
297}
298
Simon Glassb437aad2011-10-24 18:00:03 +0000299/* The TFTP get or put is complete */
300static void tftp_complete(void)
301{
302#ifdef CONFIG_TFTP_TSIZE
303 /* Print hash marks for the last packet received */
304 while (TftpTsize && TftpNumchars < 49) {
305 putc('#');
306 TftpNumchars++;
307 }
Simon Glassc8f0a5c2014-10-10 07:30:21 -0600308 puts(" ");
309 print_size(TftpTsize, "");
Simon Glassb437aad2011-10-24 18:00:03 +0000310#endif
Simon Glasscab9a8d2012-10-11 13:57:36 +0000311 time_start = get_timer(time_start);
312 if (time_start > 0) {
313 puts("\n\t "); /* Line up with "Loading: " */
Joe Hershberger290c8992015-04-08 01:41:02 -0500314 print_size(net_boot_file_size /
Simon Glasscab9a8d2012-10-11 13:57:36 +0000315 time_start * 1000, "/s");
316 }
Simon Glassb437aad2011-10-24 18:00:03 +0000317 puts("\ndone\n");
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000318 net_set_state(NETLOOP_SUCCESS);
Simon Glassb437aad2011-10-24 18:00:03 +0000319}
320
wdenkfe8c2802002-11-03 00:38:21 +0000321static void
Luca Ceresolif40538a2011-05-14 05:49:57 +0000322TftpSend(void)
wdenkfe8c2802002-11-03 00:38:21 +0000323{
Simon Glass6a398d22011-10-24 18:00:07 +0000324 uchar *pkt;
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000325 uchar *xp;
326 int len = 0;
327 ushort *s;
wdenkfe8c2802002-11-03 00:38:21 +0000328
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200329#ifdef CONFIG_MCAST_TFTP
David Updegraff7280da72007-06-11 10:41:07 -0500330 /* Multicast TFTP.. non-MasterClients do not ACK data. */
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200331 if (Multicast
332 && (TftpState == STATE_DATA)
333 && (MasterClient == 0))
David Updegraff7280da72007-06-11 10:41:07 -0500334 return;
335#endif
wdenkfe8c2802002-11-03 00:38:21 +0000336 /*
337 * We will always be sending some sort of packet, so
338 * cobble together the packet headers now.
339 */
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500340 pkt = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
wdenkfe8c2802002-11-03 00:38:21 +0000341
342 switch (TftpState) {
Luca Ceresoli562dfe52011-05-17 00:03:38 +0000343 case STATE_SEND_RRQ:
Simon Glass6a398d22011-10-24 18:00:07 +0000344 case STATE_SEND_WRQ:
wdenkfe8c2802002-11-03 00:38:21 +0000345 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200346 s = (ushort *)pkt;
Simon Glass4abd0e22011-10-27 06:24:29 +0000347#ifdef CONFIG_CMD_TFTPPUT
Simon Glass6a398d22011-10-24 18:00:07 +0000348 *s++ = htons(TftpState == STATE_SEND_RRQ ? TFTP_RRQ :
349 TFTP_WRQ);
Simon Glass4abd0e22011-10-27 06:24:29 +0000350#else
351 *s++ = htons(TFTP_RRQ);
352#endif
Wolfgang Denk3135c542005-08-26 01:36:03 +0200353 pkt = (uchar *)s;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000354 strcpy((char *)pkt, tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000355 pkt += strlen(tftp_filename) + 1;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000356 strcpy((char *)pkt, "octet");
wdenkfe8c2802002-11-03 00:38:21 +0000357 pkt += 5 /*strlen("octet")*/ + 1;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000358 strcpy((char *)pkt, "timeout");
wdenk7182b0f2003-10-06 21:55:32 +0000359 pkt += 7 /*strlen("timeout")*/ + 1;
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100360 sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000);
Robin Getz9e0a4d62009-07-23 03:01:03 -0400361 debug("send option \"timeout %s\"\n", (char *)pkt);
wdenk7182b0f2003-10-06 21:55:32 +0000362 pkt += strlen((char *)pkt) + 1;
Robin Getzb3b00ed2009-08-20 10:50:20 -0400363#ifdef CONFIG_TFTP_TSIZE
Joe Hershberger290c8992015-04-08 01:41:02 -0500364 pkt += sprintf((char *)pkt, "tsize%c%u%c",
365 0, net_boot_file_size, 0);
Robin Getzb3b00ed2009-08-20 10:50:20 -0400366#endif
David Updegraff7280da72007-06-11 10:41:07 -0500367 /* try for more effic. blk size */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000368 pkt += sprintf((char *)pkt, "blksize%c%d%c",
369 0, TftpBlkSizeOption, 0);
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200370#ifdef CONFIG_MCAST_TFTP
David Updegraff7280da72007-06-11 10:41:07 -0500371 /* Check all preconditions before even trying the option */
Joe Hershberger3a29e5a2012-05-15 08:59:12 +0000372 if (!ProhibitMcast) {
373 Bitmap = malloc(Mapsize);
374 if (Bitmap && eth_get_dev()->mcast) {
375 free(Bitmap);
376 Bitmap = NULL;
377 pkt += sprintf((char *)pkt, "multicast%c%c",
378 0, 0);
379 }
David Updegraff7280da72007-06-11 10:41:07 -0500380 }
381#endif /* CONFIG_MCAST_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000382 len = pkt - xp;
383 break;
384
wdenk7182b0f2003-10-06 21:55:32 +0000385 case STATE_OACK:
David Updegraff7280da72007-06-11 10:41:07 -0500386#ifdef CONFIG_MCAST_TFTP
387 /* My turn! Start at where I need blocks I missed.*/
388 if (Multicast)
Luca Ceresolif40538a2011-05-14 05:49:57 +0000389 TftpBlock = ext2_find_next_zero_bit(Bitmap,
390 (Mapsize*8), 0);
David Updegraff7280da72007-06-11 10:41:07 -0500391 /*..falling..*/
392#endif
Luca Ceresolib640ed32011-05-17 00:03:39 +0000393
394 case STATE_RECV_WRQ:
David Updegraff7280da72007-06-11 10:41:07 -0500395 case STATE_DATA:
wdenkfe8c2802002-11-03 00:38:21 +0000396 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200397 s = (ushort *)pkt;
Simon Glass6a398d22011-10-24 18:00:07 +0000398 s[0] = htons(TFTP_ACK);
399 s[1] = htons(TftpBlock);
400 pkt = (uchar *)(s + 2);
401#ifdef CONFIG_CMD_TFTPPUT
402 if (TftpWriting) {
403 int toload = TftpBlkSize;
404 int loaded = load_block(TftpBlock, pkt, toload);
405
406 s[0] = htons(TFTP_DATA);
407 pkt += loaded;
408 TftpFinalBlock = (loaded < toload);
409 }
410#endif
wdenkfe8c2802002-11-03 00:38:21 +0000411 len = pkt - xp;
412 break;
413
414 case STATE_TOO_LARGE:
415 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200416 s = (ushort *)pkt;
417 *s++ = htons(TFTP_ERROR);
Simon Glass6a398d22011-10-24 18:00:07 +0000418 *s++ = htons(3);
419
Wolfgang Denk3135c542005-08-26 01:36:03 +0200420 pkt = (uchar *)s;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000421 strcpy((char *)pkt, "File too large");
wdenkfe8c2802002-11-03 00:38:21 +0000422 pkt += 14 /*strlen("File too large")*/ + 1;
423 len = pkt - xp;
424 break;
425
426 case STATE_BAD_MAGIC:
427 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200428 s = (ushort *)pkt;
429 *s++ = htons(TFTP_ERROR);
430 *s++ = htons(2);
431 pkt = (uchar *)s;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000432 strcpy((char *)pkt, "File has bad magic");
wdenkfe8c2802002-11-03 00:38:21 +0000433 pkt += 18 /*strlen("File has bad magic")*/ + 1;
434 len = pkt - xp;
435 break;
436 }
437
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500438 net_send_udp_packet(net_server_ethaddr, tftp_remote_ip, TftpRemotePort,
439 TftpOurPort, len);
wdenkfe8c2802002-11-03 00:38:21 +0000440}
441
Simon Glass2928cd82011-10-26 14:18:38 +0000442#ifdef CONFIG_CMD_TFTPPUT
Simon Glass6a398d22011-10-24 18:00:07 +0000443static void icmp_handler(unsigned type, unsigned code, unsigned dest,
Joe Hershberger5874dec2015-04-08 01:41:01 -0500444 struct in_addr sip, unsigned src, uchar *pkt,
445 unsigned len)
Simon Glass6a398d22011-10-24 18:00:07 +0000446{
447 if (type == ICMP_NOT_REACH && code == ICMP_NOT_REACH_PORT) {
448 /* Oh dear the other end has gone away */
449 restart("TFTP server died");
450 }
451}
Simon Glass2928cd82011-10-26 14:18:38 +0000452#endif
wdenkfe8c2802002-11-03 00:38:21 +0000453
Joe Hershberger5874dec2015-04-08 01:41:01 -0500454static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
455 unsigned src, unsigned len)
wdenkfe8c2802002-11-03 00:38:21 +0000456{
Kim Phillips878dad02013-01-16 18:09:19 -0600457 __be16 proto;
458 __be16 *s;
Wolfgang Denk83e8e472007-08-30 14:42:15 +0200459 int i;
wdenkfe8c2802002-11-03 00:38:21 +0000460
461 if (dest != TftpOurPort) {
David Updegraff7280da72007-06-11 10:41:07 -0500462#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200463 if (Multicast
David Updegraff7280da72007-06-11 10:41:07 -0500464 && (!Mcast_port || (dest != Mcast_port)))
465#endif
Luca Ceresoli09b18232011-05-14 05:50:02 +0000466 return;
wdenkfe8c2802002-11-03 00:38:21 +0000467 }
Luca Ceresolib640ed32011-05-17 00:03:39 +0000468 if (TftpState != STATE_SEND_RRQ && src != TftpRemotePort &&
Simon Glass6a398d22011-10-24 18:00:07 +0000469 TftpState != STATE_RECV_WRQ && TftpState != STATE_SEND_WRQ)
wdenkfe8c2802002-11-03 00:38:21 +0000470 return;
wdenkfe8c2802002-11-03 00:38:21 +0000471
Luca Ceresoli35dfb182011-05-14 05:50:00 +0000472 if (len < 2)
wdenkfe8c2802002-11-03 00:38:21 +0000473 return;
wdenkfe8c2802002-11-03 00:38:21 +0000474 len -= 2;
475 /* warning: don't use increment (++) in ntohs() macros!! */
Kim Phillips878dad02013-01-16 18:09:19 -0600476 s = (__be16 *)pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200477 proto = *s++;
478 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000479 switch (ntohs(proto)) {
480
481 case TFTP_RRQ:
Simon Glass6a398d22011-10-24 18:00:07 +0000482 break;
483
wdenkfe8c2802002-11-03 00:38:21 +0000484 case TFTP_ACK:
Simon Glass6a398d22011-10-24 18:00:07 +0000485#ifdef CONFIG_CMD_TFTPPUT
486 if (TftpWriting) {
487 if (TftpFinalBlock) {
488 tftp_complete();
489 } else {
490 /*
491 * Move to the next block. We want our block
492 * count to wrap just like the other end!
493 */
494 int block = ntohs(*s);
495 int ack_ok = (TftpBlock == block);
496
497 TftpBlock = (unsigned short)(block + 1);
498 update_block_number();
499 if (ack_ok)
500 TftpSend(); /* Send next data block */
501 }
502 }
503#endif
wdenkfe8c2802002-11-03 00:38:21 +0000504 break;
Simon Glass6a398d22011-10-24 18:00:07 +0000505
wdenkfe8c2802002-11-03 00:38:21 +0000506 default:
507 break;
508
Luca Ceresolib640ed32011-05-17 00:03:39 +0000509#ifdef CONFIG_CMD_TFTPSRV
510 case TFTP_WRQ:
511 debug("Got WRQ\n");
Joe Hershberger5874dec2015-04-08 01:41:01 -0500512 tftp_remote_ip = sip;
Luca Ceresolib640ed32011-05-17 00:03:39 +0000513 TftpRemotePort = src;
514 TftpOurPort = 1024 + (get_timer(0) % 3072);
Simon Glassbd8a0e02011-10-24 18:00:04 +0000515 new_transfer();
Luca Ceresolib640ed32011-05-17 00:03:39 +0000516 TftpSend(); /* Send ACK(0) */
517 break;
518#endif
519
wdenk7182b0f2003-10-06 21:55:32 +0000520 case TFTP_OACK:
Wolfgang Denk12730c72009-08-10 09:59:10 +0200521 debug("Got OACK: %s %s\n",
522 pkt,
523 pkt + strlen((char *)pkt) + 1);
wdenk7182b0f2003-10-06 21:55:32 +0000524 TftpState = STATE_OACK;
Luca Ceresoli5079c762011-05-17 00:03:37 +0000525 TftpRemotePort = src;
Wolfgang Denk8b70f342007-08-31 10:01:51 +0200526 /*
527 * Check for 'blksize' option.
528 * Careful: "i" is signed, "len" is unsigned, thus
529 * something like "len-8" may give a *huge* number
530 */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000531 for (i = 0; i+8 < len; i++) {
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000532 if (strcmp((char *)pkt+i, "blksize") == 0) {
Wolfgang Denk83e8e472007-08-30 14:42:15 +0200533 TftpBlkSize = (unsigned short)
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000534 simple_strtoul((char *)pkt+i+8, NULL,
Luca Ceresolif40538a2011-05-14 05:49:57 +0000535 10);
Robin Getz9e0a4d62009-07-23 03:01:03 -0400536 debug("Blocksize ack: %s, %d\n",
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000537 (char *)pkt+i+8, TftpBlkSize);
Wolfgang Denk83e8e472007-08-30 14:42:15 +0200538 }
Robin Getzb3b00ed2009-08-20 10:50:20 -0400539#ifdef CONFIG_TFTP_TSIZE
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000540 if (strcmp((char *)pkt+i, "tsize") == 0) {
541 TftpTsize = simple_strtoul((char *)pkt+i+6,
Luca Ceresolic42fc272011-05-14 05:49:56 +0000542 NULL, 10);
Robin Getzb3b00ed2009-08-20 10:50:20 -0400543 debug("size = %s, %d\n",
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000544 (char *)pkt+i+6, TftpTsize);
Robin Getzb3b00ed2009-08-20 10:50:20 -0400545 }
546#endif
David Updegraff7280da72007-06-11 10:41:07 -0500547 }
548#ifdef CONFIG_MCAST_TFTP
Luca Ceresolif40538a2011-05-14 05:49:57 +0000549 parse_multicast_oack((char *)pkt, len-1);
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200550 if ((Multicast) && (!MasterClient))
David Updegraff7280da72007-06-11 10:41:07 -0500551 TftpState = STATE_DATA; /* passive.. */
552 else
553#endif
Simon Glass6a398d22011-10-24 18:00:07 +0000554#ifdef CONFIG_CMD_TFTPPUT
555 if (TftpWriting) {
556 /* Get ready to send the first block */
557 TftpState = STATE_DATA;
558 TftpBlock++;
559 }
560#endif
561 TftpSend(); /* Send ACK or first data block */
wdenk7182b0f2003-10-06 21:55:32 +0000562 break;
wdenkfe8c2802002-11-03 00:38:21 +0000563 case TFTP_DATA:
564 if (len < 2)
565 return;
566 len -= 2;
Kim Phillips878dad02013-01-16 18:09:19 -0600567 TftpBlock = ntohs(*(__be16 *)pkt);
wdenkef893942004-02-23 16:11:30 +0000568
Simon Glassbd8a0e02011-10-24 18:00:04 +0000569 update_block_number();
wdenkfe8c2802002-11-03 00:38:21 +0000570
Luca Ceresoli562dfe52011-05-17 00:03:38 +0000571 if (TftpState == STATE_SEND_RRQ)
Robin Getz9e0a4d62009-07-23 03:01:03 -0400572 debug("Server did not acknowledge timeout option!\n");
wdenk7182b0f2003-10-06 21:55:32 +0000573
Luca Ceresolib640ed32011-05-17 00:03:39 +0000574 if (TftpState == STATE_SEND_RRQ || TftpState == STATE_OACK ||
575 TftpState == STATE_RECV_WRQ) {
wdenkef893942004-02-23 16:11:30 +0000576 /* first block received */
wdenkfe8c2802002-11-03 00:38:21 +0000577 TftpState = STATE_DATA;
Luca Ceresoli5079c762011-05-17 00:03:37 +0000578 TftpRemotePort = src;
Simon Glassbd8a0e02011-10-24 18:00:04 +0000579 new_transfer();
wdenkfe8c2802002-11-03 00:38:21 +0000580
David Updegraff7280da72007-06-11 10:41:07 -0500581#ifdef CONFIG_MCAST_TFTP
582 if (Multicast) { /* start!=1 common if mcast */
583 TftpLastBlock = TftpBlock - 1;
584 } else
585#endif
wdenkfe8c2802002-11-03 00:38:21 +0000586 if (TftpBlock != 1) { /* Assertion */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000587 printf("\nTFTP error: "
588 "First block is not block 1 (%ld)\n"
589 "Starting again\n\n",
wdenkfe8c2802002-11-03 00:38:21 +0000590 TftpBlock);
Luca Ceresolif40538a2011-05-14 05:49:57 +0000591 NetStartAgain();
wdenkfe8c2802002-11-03 00:38:21 +0000592 break;
593 }
594 }
595
596 if (TftpBlock == TftpLastBlock) {
597 /*
598 * Same block again; ignore it.
599 */
600 break;
601 }
602
603 TftpLastBlock = TftpBlock;
Bartlomiej Sieka371cde72008-10-01 15:26:29 +0200604 TftpTimeoutCountMax = TIMEOUT_COUNT;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000605 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
wdenkfe8c2802002-11-03 00:38:21 +0000606
Luca Ceresolif40538a2011-05-14 05:49:57 +0000607 store_block(TftpBlock - 1, pkt + 2, len);
wdenkfe8c2802002-11-03 00:38:21 +0000608
609 /*
Luca Ceresoli2f3dc0a2011-05-17 00:03:41 +0000610 * Acknowledge the block just received, which will prompt
Luca Ceresoli5079c762011-05-17 00:03:37 +0000611 * the remote for the next one.
wdenkfe8c2802002-11-03 00:38:21 +0000612 */
David Updegraff7280da72007-06-11 10:41:07 -0500613#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200614 /* if I am the MasterClient, actively calculate what my next
615 * needed block is; else I'm passive; not ACKING
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100616 */
David Updegraff7280da72007-06-11 10:41:07 -0500617 if (Multicast) {
618 if (len < TftpBlkSize) {
619 TftpEndingBlock = TftpBlock;
620 } else if (MasterClient) {
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200621 TftpBlock = PrevBitmapHole =
David Updegraff7280da72007-06-11 10:41:07 -0500622 ext2_find_next_zero_bit(
623 Bitmap,
624 (Mapsize*8),
625 PrevBitmapHole);
626 if (TftpBlock > ((Mapsize*8) - 1)) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000627 printf("tftpfile too big\n");
David Updegraff7280da72007-06-11 10:41:07 -0500628 /* try to double it and retry */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000629 Mapsize <<= 1;
David Updegraff7280da72007-06-11 10:41:07 -0500630 mcast_cleanup();
Luca Ceresolif40538a2011-05-14 05:49:57 +0000631 NetStartAgain();
David Updegraff7280da72007-06-11 10:41:07 -0500632 return;
633 }
634 TftpLastBlock = TftpBlock;
635 }
636 }
637#endif
Luca Ceresolif40538a2011-05-14 05:49:57 +0000638 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000639
David Updegraff7280da72007-06-11 10:41:07 -0500640#ifdef CONFIG_MCAST_TFTP
641 if (Multicast) {
642 if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000643 puts("\nMulticast tftp done\n");
David Updegraff7280da72007-06-11 10:41:07 -0500644 mcast_cleanup();
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000645 net_set_state(NETLOOP_SUCCESS);
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200646 }
Joe Hershberger3a29e5a2012-05-15 08:59:12 +0000647 } else
David Updegraff7280da72007-06-11 10:41:07 -0500648#endif
Simon Glassb437aad2011-10-24 18:00:03 +0000649 if (len < TftpBlkSize)
650 tftp_complete();
wdenkfe8c2802002-11-03 00:38:21 +0000651 break;
652
653 case TFTP_ERROR:
Luca Ceresolif40538a2011-05-14 05:49:57 +0000654 printf("\nTFTP error: '%s' (%d)\n",
Kim Phillips878dad02013-01-16 18:09:19 -0600655 pkt + 2, ntohs(*(__be16 *)pkt));
Remy Bohmerb46293f2009-10-28 22:13:40 +0100656
Kim Phillips878dad02013-01-16 18:09:19 -0600657 switch (ntohs(*(__be16 *)pkt)) {
Remy Bohmerb46293f2009-10-28 22:13:40 +0100658 case TFTP_ERR_FILE_NOT_FOUND:
659 case TFTP_ERR_ACCESS_DENIED:
660 puts("Not retrying...\n");
661 eth_halt();
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000662 net_set_state(NETLOOP_FAIL);
Remy Bohmerb46293f2009-10-28 22:13:40 +0100663 break;
664 case TFTP_ERR_UNDEFINED:
665 case TFTP_ERR_DISK_FULL:
666 case TFTP_ERR_UNEXPECTED_OPCODE:
667 case TFTP_ERR_UNKNOWN_TRANSFER_ID:
668 case TFTP_ERR_FILE_ALREADY_EXISTS:
669 default:
670 puts("Starting again\n\n");
David Updegraff7280da72007-06-11 10:41:07 -0500671#ifdef CONFIG_MCAST_TFTP
Remy Bohmerb46293f2009-10-28 22:13:40 +0100672 mcast_cleanup();
David Updegraff7280da72007-06-11 10:41:07 -0500673#endif
Remy Bohmerb46293f2009-10-28 22:13:40 +0100674 NetStartAgain();
675 break;
676 }
wdenkfe8c2802002-11-03 00:38:21 +0000677 break;
678 }
679}
680
681
682static void
Luca Ceresolif40538a2011-05-14 05:49:57 +0000683TftpTimeout(void)
wdenkfe8c2802002-11-03 00:38:21 +0000684{
Bartlomiej Sieka371cde72008-10-01 15:26:29 +0200685 if (++TftpTimeoutCount > TftpTimeoutCountMax) {
Simon Glassbd8a0e02011-10-24 18:00:04 +0000686 restart("Retry count exceeded");
wdenkfe8c2802002-11-03 00:38:21 +0000687 } else {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000688 puts("T ");
689 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
Luca Ceresolib640ed32011-05-17 00:03:39 +0000690 if (TftpState != STATE_RECV_WRQ)
691 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000692 }
693}
694
695
Simon Glassf59675a2011-10-24 18:00:05 +0000696void TftpStart(enum proto_t protocol)
wdenkfe8c2802002-11-03 00:38:21 +0000697{
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200698 char *ep; /* Environment pointer */
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200699
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100700 /*
701 * Allow the user to choose TFTP blocksize and timeout.
702 * TFTP protocol has a minimal timeout of 1 second.
703 */
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000704 ep = getenv("tftpblocksize");
705 if (ep != NULL)
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200706 TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100707
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000708 ep = getenv("tftptimeout");
709 if (ep != NULL)
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100710 TftpTimeoutMSecs = simple_strtol(ep, NULL, 10);
711
712 if (TftpTimeoutMSecs < 1000) {
713 printf("TFTP timeout (%ld ms) too low, "
714 "set minimum = 1000 ms\n",
715 TftpTimeoutMSecs);
716 TftpTimeoutMSecs = 1000;
717 }
718
719 debug("TFTP blocksize = %i, timeout = %ld ms\n",
720 TftpBlkSizeOption, TftpTimeoutMSecs);
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200721
Joe Hershberger5874dec2015-04-08 01:41:01 -0500722 tftp_remote_ip = net_server_ip;
Joe Hershberger290c8992015-04-08 01:41:02 -0500723 if (net_boot_file_name[0] == '\0') {
Matthias Weisser6cc63262011-12-03 03:29:44 +0000724 sprintf(default_filename, "%02X%02X%02X%02X.img",
Joe Hershberger5874dec2015-04-08 01:41:01 -0500725 net_ip.s_addr & 0xFF,
726 (net_ip.s_addr >> 8) & 0xFF,
727 (net_ip.s_addr >> 16) & 0xFF,
728 (net_ip.s_addr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100729
730 strncpy(tftp_filename, default_filename, MAX_LEN);
731 tftp_filename[MAX_LEN-1] = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000732
Luca Ceresolif40538a2011-05-14 05:49:57 +0000733 printf("*** Warning: no boot file name; using '%s'\n",
wdenkfe8c2802002-11-03 00:38:21 +0000734 tftp_filename);
735 } else {
Joe Hershberger290c8992015-04-08 01:41:02 -0500736 char *p = strchr(net_boot_file_name, ':');
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100737
738 if (p == NULL) {
Joe Hershberger290c8992015-04-08 01:41:02 -0500739 strncpy(tftp_filename, net_boot_file_name, MAX_LEN);
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100740 tftp_filename[MAX_LEN-1] = 0;
741 } else {
Joe Hershberger290c8992015-04-08 01:41:02 -0500742 tftp_remote_ip = string_to_ip(net_boot_file_name);
Peter Tyser5e58efa2008-12-01 16:29:38 -0600743 strncpy(tftp_filename, p + 1, MAX_LEN);
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100744 tftp_filename[MAX_LEN-1] = 0;
745 }
wdenkfe8c2802002-11-03 00:38:21 +0000746 }
747
Luca Ceresolif40538a2011-05-14 05:49:57 +0000748 printf("Using %s device\n", eth_get_name());
Simon Glass6a398d22011-10-24 18:00:07 +0000749 printf("TFTP %s server %pI4; our IP address is %pI4",
Simon Glass4abd0e22011-10-27 06:24:29 +0000750#ifdef CONFIG_CMD_TFTPPUT
751 protocol == TFTPPUT ? "to" : "from",
752#else
753 "from",
754#endif
Joe Hershberger5874dec2015-04-08 01:41:01 -0500755 &tftp_remote_ip, &net_ip);
wdenkfe8c2802002-11-03 00:38:21 +0000756
757 /* Check if we need to send across this subnet */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500758 if (net_gateway.s_addr && net_netmask.s_addr) {
759 struct in_addr our_net;
760 struct in_addr remote_net;
wdenkfe8c2802002-11-03 00:38:21 +0000761
Joe Hershberger5874dec2015-04-08 01:41:01 -0500762 our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
763 remote_net.s_addr = tftp_remote_ip.s_addr & net_netmask.s_addr;
764 if (our_net.s_addr != remote_net.s_addr)
765 printf("; sending through gateway %pI4", &net_gateway);
wdenkfe8c2802002-11-03 00:38:21 +0000766 }
Luca Ceresolif40538a2011-05-14 05:49:57 +0000767 putc('\n');
wdenkfe8c2802002-11-03 00:38:21 +0000768
Luca Ceresolif40538a2011-05-14 05:49:57 +0000769 printf("Filename '%s'.", tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000770
Joe Hershberger290c8992015-04-08 01:41:02 -0500771 if (net_boot_file_expected_size_in_blocks) {
772 printf(" Size is 0x%x Bytes = ",
773 net_boot_file_expected_size_in_blocks << 9);
774 print_size(net_boot_file_expected_size_in_blocks << 9, "");
wdenkfe8c2802002-11-03 00:38:21 +0000775 }
776
Luca Ceresolif40538a2011-05-14 05:49:57 +0000777 putc('\n');
Simon Glass6a398d22011-10-24 18:00:07 +0000778#ifdef CONFIG_CMD_TFTPPUT
779 TftpWriting = (protocol == TFTPPUT);
780 if (TftpWriting) {
781 printf("Save address: 0x%lx\n", save_addr);
782 printf("Save size: 0x%lx\n", save_size);
Joe Hershberger290c8992015-04-08 01:41:02 -0500783 net_boot_file_size = save_size;
Simon Glass6a398d22011-10-24 18:00:07 +0000784 puts("Saving: *\b");
785 TftpState = STATE_SEND_WRQ;
786 new_transfer();
787 } else
788#endif
789 {
790 printf("Load address: 0x%lx\n", load_addr);
791 puts("Loading: *\b");
792 TftpState = STATE_SEND_RRQ;
793 }
wdenkfe8c2802002-11-03 00:38:21 +0000794
Simon Glasscab9a8d2012-10-11 13:57:36 +0000795 time_start = get_timer(0);
Bartlomiej Sieka371cde72008-10-01 15:26:29 +0200796 TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
797
Luca Ceresolif40538a2011-05-14 05:49:57 +0000798 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
Joe Hershberger5874dec2015-04-08 01:41:01 -0500799 net_set_udp_handler(tftp_handler);
Simon Glass2928cd82011-10-26 14:18:38 +0000800#ifdef CONFIG_CMD_TFTPPUT
Simon Glass6a398d22011-10-24 18:00:07 +0000801 net_set_icmp_handler(icmp_handler);
Simon Glass2928cd82011-10-26 14:18:38 +0000802#endif
Luca Ceresoli5079c762011-05-17 00:03:37 +0000803 TftpRemotePort = WELL_KNOWN_PORT;
wdenkfe8c2802002-11-03 00:38:21 +0000804 TftpTimeoutCount = 0;
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200805 /* Use a pseudo-random port unless a specific port is set */
wdenkfe8c2802002-11-03 00:38:21 +0000806 TftpOurPort = 1024 + (get_timer(0) % 3072);
David Updegraff7280da72007-06-11 10:41:07 -0500807
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200808#ifdef CONFIG_TFTP_PORT
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000809 ep = getenv("tftpdstp");
Luca Ceresoli35dfb182011-05-14 05:50:00 +0000810 if (ep != NULL)
Luca Ceresoli5079c762011-05-17 00:03:37 +0000811 TftpRemotePort = simple_strtol(ep, NULL, 10);
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000812 ep = getenv("tftpsrcp");
Luca Ceresoli35dfb182011-05-14 05:50:00 +0000813 if (ep != NULL)
Luca Ceresolif40538a2011-05-14 05:49:57 +0000814 TftpOurPort = simple_strtol(ep, NULL, 10);
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200815#endif
wdenk7182b0f2003-10-06 21:55:32 +0000816 TftpBlock = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000817
wdenke6466f62003-06-05 19:27:42 +0000818 /* zero out server ether in case the server ip has changed */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500819 memset(net_server_ethaddr, 0, 6);
David Updegraff7280da72007-06-11 10:41:07 -0500820 /* Revert TftpBlkSize to dflt */
821 TftpBlkSize = TFTP_BLOCK_SIZE;
822#ifdef CONFIG_MCAST_TFTP
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100823 mcast_cleanup();
David Updegraff7280da72007-06-11 10:41:07 -0500824#endif
Robin Getzb3b00ed2009-08-20 10:50:20 -0400825#ifdef CONFIG_TFTP_TSIZE
826 TftpTsize = 0;
827 TftpNumchars = 0;
828#endif
wdenke6466f62003-06-05 19:27:42 +0000829
Luca Ceresolif40538a2011-05-14 05:49:57 +0000830 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000831}
Luca Ceresolib640ed32011-05-17 00:03:39 +0000832
833#ifdef CONFIG_CMD_TFTPSRV
834void
835TftpStartServer(void)
836{
837 tftp_filename[0] = 0;
838
Luca Ceresolib640ed32011-05-17 00:03:39 +0000839 printf("Using %s device\n", eth_get_name());
Joe Hershberger5874dec2015-04-08 01:41:01 -0500840 printf("Listening for TFTP transfer on %pI4\n", &net_ip);
Luca Ceresolib640ed32011-05-17 00:03:39 +0000841 printf("Load address: 0x%lx\n", load_addr);
842
843 puts("Loading: *\b");
844
845 TftpTimeoutCountMax = TIMEOUT_COUNT;
846 TftpTimeoutCount = 0;
847 TftpTimeoutMSecs = TIMEOUT;
848 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
849
850 /* Revert TftpBlkSize to dflt */
851 TftpBlkSize = TFTP_BLOCK_SIZE;
852 TftpBlock = 0;
853 TftpOurPort = WELL_KNOWN_PORT;
854
855#ifdef CONFIG_TFTP_TSIZE
856 TftpTsize = 0;
857 TftpNumchars = 0;
858#endif
859
860 TftpState = STATE_RECV_WRQ;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500861 net_set_udp_handler(tftp_handler);
Andrew Ruder91393c52013-10-22 19:10:28 -0500862
863 /* zero out server ether in case the server ip has changed */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500864 memset(net_server_ethaddr, 0, 6);
Luca Ceresolib640ed32011-05-17 00:03:39 +0000865}
866#endif /* CONFIG_CMD_TFTPSRV */
wdenkfe8c2802002-11-03 00:38:21 +0000867
David Updegraff7280da72007-06-11 10:41:07 -0500868#ifdef CONFIG_MCAST_TFTP
869/* Credits: atftp project.
870 */
871
872/* pick up BcastAddr, Port, and whether I am [now] the master-client. *
873 * Frame:
874 * +-------+-----------+---+-------~~-------+---+
875 * | opc | multicast | 0 | addr, port, mc | 0 |
876 * +-------+-----------+---+-------~~-------+---+
877 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
878 * I am the new master-client so must send ACKs to DataBlocks. If I am not
879 * master-client, I'm a passive client, gathering what DataBlocks I may and
880 * making note of which ones I got in my bitmask.
881 * In theory, I never go from master->passive..
882 * .. this comes in with pkt already pointing just past opc
883 */
884static void parse_multicast_oack(char *pkt, int len)
885{
Luca Ceresolif40538a2011-05-14 05:49:57 +0000886 int i;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500887 struct in_addr addr;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000888 char *mc_adr, *port, *mc;
David Updegraff7280da72007-06-11 10:41:07 -0500889
Luca Ceresolif40538a2011-05-14 05:49:57 +0000890 mc_adr = port = mc = NULL;
David Updegraff7280da72007-06-11 10:41:07 -0500891 /* march along looking for 'multicast\0', which has to start at least
892 * 14 bytes back from the end.
893 */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000894 for (i = 0; i < len-14; i++)
895 if (strcmp(pkt+i, "multicast") == 0)
David Updegraff7280da72007-06-11 10:41:07 -0500896 break;
897 if (i >= (len-14)) /* non-Multicast OACK, ign. */
898 return;
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200899
Luca Ceresolif40538a2011-05-14 05:49:57 +0000900 i += 10; /* strlen multicast */
David Updegraff7280da72007-06-11 10:41:07 -0500901 mc_adr = pkt+i;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000902 for (; i < len; i++) {
David Updegraff7280da72007-06-11 10:41:07 -0500903 if (*(pkt+i) == ',') {
904 *(pkt+i) = '\0';
905 if (port) {
906 mc = pkt+i+1;
907 break;
908 } else {
909 port = pkt+i+1;
910 }
911 }
912 }
Luca Ceresoli6072fcd2011-05-14 05:50:01 +0000913 if (!port || !mc_adr || !mc)
914 return;
David Updegraff7280da72007-06-11 10:41:07 -0500915 if (Multicast && MasterClient) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000916 printf("I got a OACK as master Client, WRONG!\n");
David Updegraff7280da72007-06-11 10:41:07 -0500917 return;
918 }
919 /* ..I now accept packets destined for this MCAST addr, port */
920 if (!Multicast) {
921 if (Bitmap) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000922 printf("Internal failure! no mcast.\n");
David Updegraff7280da72007-06-11 10:41:07 -0500923 free(Bitmap);
Luca Ceresolif40538a2011-05-14 05:49:57 +0000924 Bitmap = NULL;
925 ProhibitMcast = 1;
David Updegraff7280da72007-06-11 10:41:07 -0500926 return ;
927 }
928 /* I malloc instead of pre-declare; so that if the file ends
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200929 * up being too big for this bitmap I can retry
930 */
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000931 Bitmap = malloc(Mapsize);
932 if (!Bitmap) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000933 printf("No Bitmap, no multicast. Sorry.\n");
934 ProhibitMcast = 1;
David Updegraff7280da72007-06-11 10:41:07 -0500935 return;
936 }
Luca Ceresolif40538a2011-05-14 05:49:57 +0000937 memset(Bitmap, 0, Mapsize);
David Updegraff7280da72007-06-11 10:41:07 -0500938 PrevBitmapHole = 0;
939 Multicast = 1;
940 }
941 addr = string_to_ip(mc_adr);
Joe Hershberger5874dec2015-04-08 01:41:01 -0500942 if (net_mcast_addr.s_addr != addr.s_addr) {
943 if (net_mcast_addr.s_addr)
944 eth_mcast_join(net_mcast_addr, 0);
945 net_mcast_addr = addr;
946 if (eth_mcast_join(net_mcast_addr, 1)) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000947 printf("Fail to set mcast, revert to TFTP\n");
948 ProhibitMcast = 1;
David Updegraff7280da72007-06-11 10:41:07 -0500949 mcast_cleanup();
950 NetStartAgain();
951 }
952 }
Luca Ceresolif40538a2011-05-14 05:49:57 +0000953 MasterClient = (unsigned char)simple_strtoul((char *)mc, NULL, 10);
954 Mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
955 printf("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
David Updegraff7280da72007-06-11 10:41:07 -0500956 return;
957}
958
959#endif /* Multicast TFTP */