blob: 0a2c53302cab1edacacd88f439b888f4837e7d4a [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>
11#include <net.h>
12#include "tftp.h"
13#include "bootp.h"
Joe Hershberger3a29e5a2012-05-15 08:59:12 +000014#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
15#include <flash.h>
16#endif
wdenkfe8c2802002-11-03 00:38:21 +000017
Luca Ceresolic42fc272011-05-14 05:49:56 +000018/* Well known TFTP port # */
19#define WELL_KNOWN_PORT 69
20/* Millisecs to timeout for lost pkt */
21#define TIMEOUT 5000UL
wdenkfe8c2802002-11-03 00:38:21 +000022#ifndef CONFIG_NET_RETRY_COUNT
Luca Ceresolic42fc272011-05-14 05:49:56 +000023/* # of timeouts before giving up */
24# define TIMEOUT_COUNT 10
wdenkfe8c2802002-11-03 00:38:21 +000025#else
26# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
27#endif
Luca Ceresolic42fc272011-05-14 05:49:56 +000028/* Number of "loading" hashes per line (for checking the image size) */
29#define HASHES_PER_LINE 65
wdenkfe8c2802002-11-03 00:38:21 +000030
31/*
32 * TFTP operations.
33 */
34#define TFTP_RRQ 1
35#define TFTP_WRQ 2
36#define TFTP_DATA 3
37#define TFTP_ACK 4
38#define TFTP_ERROR 5
wdenk7182b0f2003-10-06 21:55:32 +000039#define TFTP_OACK 6
wdenkfe8c2802002-11-03 00:38:21 +000040
Bartlomiej Sieka371cde72008-10-01 15:26:29 +020041static ulong TftpTimeoutMSecs = TIMEOUT;
42static int TftpTimeoutCountMax = TIMEOUT_COUNT;
Simon Glasscab9a8d2012-10-11 13:57:36 +000043static ulong time_start; /* Record time we started tftp */
Bartlomiej Sieka371cde72008-10-01 15:26:29 +020044
45/*
46 * These globals govern the timeout behavior when attempting a connection to a
47 * TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to
48 * wait for the server to respond to initial connection. Second global,
49 * TftpRRQTimeoutCountMax, gives the number of such connection retries.
50 * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be
51 * positive. The globals are meant to be set (and restored) by code needing
52 * non-standard timeout behavior when initiating a TFTP transfer.
53 */
54ulong TftpRRQTimeoutMSecs = TIMEOUT;
55int TftpRRQTimeoutCountMax = TIMEOUT_COUNT;
56
Remy Bohmerb46293f2009-10-28 22:13:40 +010057enum {
58 TFTP_ERR_UNDEFINED = 0,
59 TFTP_ERR_FILE_NOT_FOUND = 1,
60 TFTP_ERR_ACCESS_DENIED = 2,
61 TFTP_ERR_DISK_FULL = 3,
62 TFTP_ERR_UNEXPECTED_OPCODE = 4,
63 TFTP_ERR_UNKNOWN_TRANSFER_ID = 5,
64 TFTP_ERR_FILE_ALREADY_EXISTS = 6,
65};
66
Luca Ceresoli5079c762011-05-17 00:03:37 +000067static IPaddr_t TftpRemoteIP;
Luca Ceresolic42fc272011-05-14 05:49:56 +000068/* The UDP port at their end */
Luca Ceresoli5079c762011-05-17 00:03:37 +000069static int TftpRemotePort;
Luca Ceresolic42fc272011-05-14 05:49:56 +000070/* The UDP port at our end */
71static int TftpOurPort;
wdenkfe8c2802002-11-03 00:38:21 +000072static int TftpTimeoutCount;
Luca Ceresolic42fc272011-05-14 05:49:56 +000073/* packet sequence number */
74static ulong TftpBlock;
75/* last packet sequence number received */
76static ulong TftpLastBlock;
77/* count of sequence number wraparounds */
78static ulong TftpBlockWrap;
79/* memory offset due to wrapping */
80static ulong TftpBlockWrapOffset;
wdenkfe8c2802002-11-03 00:38:21 +000081static int TftpState;
Robin Getzb3b00ed2009-08-20 10:50:20 -040082#ifdef CONFIG_TFTP_TSIZE
Luca Ceresolic42fc272011-05-14 05:49:56 +000083/* The file size reported by the server */
84static int TftpTsize;
85/* The number of hashes we printed */
86static short TftpNumchars;
Robin Getzb3b00ed2009-08-20 10:50:20 -040087#endif
Simon Glass6a398d22011-10-24 18:00:07 +000088#ifdef CONFIG_CMD_TFTPPUT
89static int TftpWriting; /* 1 if writing, else 0 */
90static int TftpFinalBlock; /* 1 if we have sent the last block */
91#else
92#define TftpWriting 0
93#endif
wdenkef893942004-02-23 16:11:30 +000094
Luca Ceresoli562dfe52011-05-17 00:03:38 +000095#define STATE_SEND_RRQ 1
wdenkfe8c2802002-11-03 00:38:21 +000096#define STATE_DATA 2
97#define STATE_TOO_LARGE 3
98#define STATE_BAD_MAGIC 4
wdenk7182b0f2003-10-06 21:55:32 +000099#define STATE_OACK 5
Luca Ceresolib640ed32011-05-17 00:03:39 +0000100#define STATE_RECV_WRQ 6
Simon Glass6a398d22011-10-24 18:00:07 +0000101#define STATE_SEND_WRQ 7
wdenkfe8c2802002-11-03 00:38:21 +0000102
Luca Ceresolic42fc272011-05-14 05:49:56 +0000103/* default TFTP block size */
104#define TFTP_BLOCK_SIZE 512
105/* sequence number is 16 bit */
106#define TFTP_SEQUENCE_SIZE ((ulong)(1<<16))
wdenkef893942004-02-23 16:11:30 +0000107
wdenkfe8c2802002-11-03 00:38:21 +0000108#define DEFAULT_NAME_LEN (8 + 4 + 1)
109static char default_filename[DEFAULT_NAME_LEN];
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100110
111#ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
112#define MAX_LEN 128
113#else
114#define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
115#endif
116
117static char tftp_filename[MAX_LEN];
wdenkfe8c2802002-11-03 00:38:21 +0000118
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200119/* 512 is poor choice for ethernet, MTU is typically 1500.
120 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
David Updegraff7280da72007-06-11 10:41:07 -0500121 * almost-MTU block sizes. At least try... fall back to 512 if need be.
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200122 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
David Updegraff7280da72007-06-11 10:41:07 -0500123 */
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200124#ifdef CONFIG_TFTP_BLOCKSIZE
125#define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
126#else
David Updegraff7280da72007-06-11 10:41:07 -0500127#define TFTP_MTU_BLOCKSIZE 1468
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200128#endif
129
Luca Ceresolif40538a2011-05-14 05:49:57 +0000130static unsigned short TftpBlkSize = TFTP_BLOCK_SIZE;
131static unsigned short TftpBlkSizeOption = TFTP_MTU_BLOCKSIZE;
David Updegraff7280da72007-06-11 10:41:07 -0500132
133#ifdef CONFIG_MCAST_TFTP
134#include <malloc.h>
135#define MTFTP_BITMAPSIZE 0x1000
136static unsigned *Bitmap;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000137static int PrevBitmapHole, Mapsize = MTFTP_BITMAPSIZE;
Luca Ceresoli4e7a02b2011-05-14 05:50:03 +0000138static uchar ProhibitMcast, MasterClient;
139static uchar Multicast;
David Updegraff7280da72007-06-11 10:41:07 -0500140static int Mcast_port;
141static ulong TftpEndingBlock; /* can get 'last' block before done..*/
142
Luca Ceresolif40538a2011-05-14 05:49:57 +0000143static void parse_multicast_oack(char *pkt, int len);
David Updegraff7280da72007-06-11 10:41:07 -0500144
145static void
146mcast_cleanup(void)
147{
Luca Ceresoli6072fcd2011-05-14 05:50:01 +0000148 if (Mcast_addr)
149 eth_mcast_join(Mcast_addr, 0);
150 if (Bitmap)
151 free(Bitmap);
Luca Ceresolif40538a2011-05-14 05:49:57 +0000152 Bitmap = NULL;
David Updegraff7280da72007-06-11 10:41:07 -0500153 Mcast_addr = Multicast = Mcast_port = 0;
154 TftpEndingBlock = -1;
155}
156
157#endif /* CONFIG_MCAST_TFTP */
158
Joe Hershberger3a29e5a2012-05-15 08:59:12 +0000159static inline void
Jayachandran Chandrasekharan Naira5d451b2012-07-10 11:48:54 +0530160store_block(int block, uchar *src, unsigned len)
wdenkfe8c2802002-11-03 00:38:21 +0000161{
David Updegraff7280da72007-06-11 10:41:07 -0500162 ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
wdenkef893942004-02-23 16:11:30 +0000163 ulong newsize = offset + len;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200164#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
wdenkfe8c2802002-11-03 00:38:21 +0000165 int i, rc = 0;
166
Luca Ceresolif40538a2011-05-14 05:49:57 +0000167 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenkfe8c2802002-11-03 00:38:21 +0000168 /* start address in flash? */
Jochen Friedrichd31a59b2008-09-02 11:24:59 +0200169 if (flash_info[i].flash_id == FLASH_UNKNOWN)
170 continue;
wdenkfe8c2802002-11-03 00:38:21 +0000171 if (load_addr + offset >= flash_info[i].start[0]) {
172 rc = 1;
173 break;
174 }
175 }
176
177 if (rc) { /* Flash is destination for this packet */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000178 rc = flash_write((char *)src, (ulong)(load_addr+offset), len);
wdenkfe8c2802002-11-03 00:38:21 +0000179 if (rc) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000180 flash_perror(rc);
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000181 net_set_state(NETLOOP_FAIL);
wdenkfe8c2802002-11-03 00:38:21 +0000182 return;
183 }
Joe Hershberger3a29e5a2012-05-15 08:59:12 +0000184 } else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200185#endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000186 {
187 (void)memcpy((void *)(load_addr + offset), src, len);
188 }
David Updegraff7280da72007-06-11 10:41:07 -0500189#ifdef CONFIG_MCAST_TFTP
190 if (Multicast)
191 ext2_set_bit(block, Bitmap);
192#endif
wdenkfe8c2802002-11-03 00:38:21 +0000193
194 if (NetBootFileXferSize < newsize)
195 NetBootFileXferSize = newsize;
196}
197
Simon Glassbd8a0e02011-10-24 18:00:04 +0000198/* Clear our state ready for a new transfer */
Simon Glass84313322011-10-27 06:24:28 +0000199static void new_transfer(void)
Simon Glassbd8a0e02011-10-24 18:00:04 +0000200{
201 TftpLastBlock = 0;
202 TftpBlockWrap = 0;
203 TftpBlockWrapOffset = 0;
204#ifdef CONFIG_CMD_TFTPPUT
205 TftpFinalBlock = 0;
206#endif
207}
Simon Glass6a398d22011-10-24 18:00:07 +0000208
209#ifdef CONFIG_CMD_TFTPPUT
210/**
211 * Load the next block from memory to be sent over tftp.
212 *
213 * @param block Block number to send
214 * @param dst Destination buffer for data
215 * @param len Number of bytes in block (this one and every other)
216 * @return number of bytes loaded
217 */
218static int load_block(unsigned block, uchar *dst, unsigned len)
219{
220 /* We may want to get the final block from the previous set */
221 ulong offset = ((int)block - 1) * len + TftpBlockWrapOffset;
222 ulong tosend = len;
223
224 tosend = min(NetBootFileXferSize - offset, tosend);
225 (void)memcpy(dst, (void *)(save_addr + offset), tosend);
226 debug("%s: block=%d, offset=%ld, len=%d, tosend=%ld\n", __func__,
227 block, offset, len, tosend);
228 return tosend;
229}
230#endif
Simon Glassbd8a0e02011-10-24 18:00:04 +0000231
Luca Ceresolif40538a2011-05-14 05:49:57 +0000232static void TftpSend(void);
233static void TftpTimeout(void);
wdenkfe8c2802002-11-03 00:38:21 +0000234
235/**********************************************************************/
236
Simon Glassb437aad2011-10-24 18:00:03 +0000237static void show_block_marker(void)
238{
239#ifdef CONFIG_TFTP_TSIZE
240 if (TftpTsize) {
241 ulong pos = TftpBlock * TftpBlkSize + TftpBlockWrapOffset;
242
243 while (TftpNumchars < pos * 50 / TftpTsize) {
244 putc('#');
245 TftpNumchars++;
246 }
Simon Glass6a398d22011-10-24 18:00:07 +0000247 } else
Simon Glassb437aad2011-10-24 18:00:03 +0000248#endif
Simon Glass6a398d22011-10-24 18:00:07 +0000249 {
Simon Glassb437aad2011-10-24 18:00:03 +0000250 if (((TftpBlock - 1) % 10) == 0)
251 putc('#');
252 else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0)
253 puts("\n\t ");
254 }
255}
256
Simon Glassbd8a0e02011-10-24 18:00:04 +0000257/**
258 * restart the current transfer due to an error
259 *
260 * @param msg Message to print for user
261 */
262static void restart(const char *msg)
263{
264 printf("\n%s; starting again\n", msg);
265#ifdef CONFIG_MCAST_TFTP
266 mcast_cleanup();
267#endif
268 NetStartAgain();
269}
270
271/*
272 * Check if the block number has wrapped, and update progress
273 *
274 * TODO: The egregious use of global variables in this file should be tidied.
275 */
276static void update_block_number(void)
277{
278 /*
279 * RFC1350 specifies that the first data packet will
280 * have sequence number 1. If we receive a sequence
281 * number of 0 this means that there was a wrap
282 * around of the (16 bit) counter.
283 */
rockly0d757362013-08-03 18:09:05 +0800284 if (TftpBlock == 0 && TftpLastBlock != 0) {
Simon Glassbd8a0e02011-10-24 18:00:04 +0000285 TftpBlockWrap++;
286 TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE;
287 TftpTimeoutCount = 0; /* we've done well, reset thhe timeout */
288 } else {
289 show_block_marker();
290 }
291}
292
Simon Glassb437aad2011-10-24 18:00:03 +0000293/* The TFTP get or put is complete */
294static void tftp_complete(void)
295{
296#ifdef CONFIG_TFTP_TSIZE
297 /* Print hash marks for the last packet received */
298 while (TftpTsize && TftpNumchars < 49) {
299 putc('#');
300 TftpNumchars++;
301 }
Simon Glassc8f0a5c2014-10-10 07:30:21 -0600302 puts(" ");
303 print_size(TftpTsize, "");
Simon Glassb437aad2011-10-24 18:00:03 +0000304#endif
Simon Glasscab9a8d2012-10-11 13:57:36 +0000305 time_start = get_timer(time_start);
306 if (time_start > 0) {
307 puts("\n\t "); /* Line up with "Loading: " */
308 print_size(NetBootFileXferSize /
309 time_start * 1000, "/s");
310 }
Simon Glassb437aad2011-10-24 18:00:03 +0000311 puts("\ndone\n");
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000312 net_set_state(NETLOOP_SUCCESS);
Simon Glassb437aad2011-10-24 18:00:03 +0000313}
314
wdenkfe8c2802002-11-03 00:38:21 +0000315static void
Luca Ceresolif40538a2011-05-14 05:49:57 +0000316TftpSend(void)
wdenkfe8c2802002-11-03 00:38:21 +0000317{
Simon Glass6a398d22011-10-24 18:00:07 +0000318 uchar *pkt;
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000319 uchar *xp;
320 int len = 0;
321 ushort *s;
wdenkfe8c2802002-11-03 00:38:21 +0000322
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200323#ifdef CONFIG_MCAST_TFTP
David Updegraff7280da72007-06-11 10:41:07 -0500324 /* Multicast TFTP.. non-MasterClients do not ACK data. */
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200325 if (Multicast
326 && (TftpState == STATE_DATA)
327 && (MasterClient == 0))
David Updegraff7280da72007-06-11 10:41:07 -0500328 return;
329#endif
wdenkfe8c2802002-11-03 00:38:21 +0000330 /*
331 * We will always be sending some sort of packet, so
332 * cobble together the packet headers now.
333 */
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000334 pkt = NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
wdenkfe8c2802002-11-03 00:38:21 +0000335
336 switch (TftpState) {
Luca Ceresoli562dfe52011-05-17 00:03:38 +0000337 case STATE_SEND_RRQ:
Simon Glass6a398d22011-10-24 18:00:07 +0000338 case STATE_SEND_WRQ:
wdenkfe8c2802002-11-03 00:38:21 +0000339 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200340 s = (ushort *)pkt;
Simon Glass4abd0e22011-10-27 06:24:29 +0000341#ifdef CONFIG_CMD_TFTPPUT
Simon Glass6a398d22011-10-24 18:00:07 +0000342 *s++ = htons(TftpState == STATE_SEND_RRQ ? TFTP_RRQ :
343 TFTP_WRQ);
Simon Glass4abd0e22011-10-27 06:24:29 +0000344#else
345 *s++ = htons(TFTP_RRQ);
346#endif
Wolfgang Denk3135c542005-08-26 01:36:03 +0200347 pkt = (uchar *)s;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000348 strcpy((char *)pkt, tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000349 pkt += strlen(tftp_filename) + 1;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000350 strcpy((char *)pkt, "octet");
wdenkfe8c2802002-11-03 00:38:21 +0000351 pkt += 5 /*strlen("octet")*/ + 1;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000352 strcpy((char *)pkt, "timeout");
wdenk7182b0f2003-10-06 21:55:32 +0000353 pkt += 7 /*strlen("timeout")*/ + 1;
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100354 sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000);
Robin Getz9e0a4d62009-07-23 03:01:03 -0400355 debug("send option \"timeout %s\"\n", (char *)pkt);
wdenk7182b0f2003-10-06 21:55:32 +0000356 pkt += strlen((char *)pkt) + 1;
Robin Getzb3b00ed2009-08-20 10:50:20 -0400357#ifdef CONFIG_TFTP_TSIZE
Simon Glass6a398d22011-10-24 18:00:07 +0000358 pkt += sprintf((char *)pkt, "tsize%c%lu%c",
359 0, NetBootFileXferSize, 0);
Robin Getzb3b00ed2009-08-20 10:50:20 -0400360#endif
David Updegraff7280da72007-06-11 10:41:07 -0500361 /* try for more effic. blk size */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000362 pkt += sprintf((char *)pkt, "blksize%c%d%c",
363 0, TftpBlkSizeOption, 0);
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200364#ifdef CONFIG_MCAST_TFTP
David Updegraff7280da72007-06-11 10:41:07 -0500365 /* Check all preconditions before even trying the option */
Joe Hershberger3a29e5a2012-05-15 08:59:12 +0000366 if (!ProhibitMcast) {
367 Bitmap = malloc(Mapsize);
368 if (Bitmap && eth_get_dev()->mcast) {
369 free(Bitmap);
370 Bitmap = NULL;
371 pkt += sprintf((char *)pkt, "multicast%c%c",
372 0, 0);
373 }
David Updegraff7280da72007-06-11 10:41:07 -0500374 }
375#endif /* CONFIG_MCAST_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000376 len = pkt - xp;
377 break;
378
wdenk7182b0f2003-10-06 21:55:32 +0000379 case STATE_OACK:
David Updegraff7280da72007-06-11 10:41:07 -0500380#ifdef CONFIG_MCAST_TFTP
381 /* My turn! Start at where I need blocks I missed.*/
382 if (Multicast)
Luca Ceresolif40538a2011-05-14 05:49:57 +0000383 TftpBlock = ext2_find_next_zero_bit(Bitmap,
384 (Mapsize*8), 0);
David Updegraff7280da72007-06-11 10:41:07 -0500385 /*..falling..*/
386#endif
Luca Ceresolib640ed32011-05-17 00:03:39 +0000387
388 case STATE_RECV_WRQ:
David Updegraff7280da72007-06-11 10:41:07 -0500389 case STATE_DATA:
wdenkfe8c2802002-11-03 00:38:21 +0000390 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200391 s = (ushort *)pkt;
Simon Glass6a398d22011-10-24 18:00:07 +0000392 s[0] = htons(TFTP_ACK);
393 s[1] = htons(TftpBlock);
394 pkt = (uchar *)(s + 2);
395#ifdef CONFIG_CMD_TFTPPUT
396 if (TftpWriting) {
397 int toload = TftpBlkSize;
398 int loaded = load_block(TftpBlock, pkt, toload);
399
400 s[0] = htons(TFTP_DATA);
401 pkt += loaded;
402 TftpFinalBlock = (loaded < toload);
403 }
404#endif
wdenkfe8c2802002-11-03 00:38:21 +0000405 len = pkt - xp;
406 break;
407
408 case STATE_TOO_LARGE:
409 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200410 s = (ushort *)pkt;
411 *s++ = htons(TFTP_ERROR);
Simon Glass6a398d22011-10-24 18:00:07 +0000412 *s++ = htons(3);
413
Wolfgang Denk3135c542005-08-26 01:36:03 +0200414 pkt = (uchar *)s;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000415 strcpy((char *)pkt, "File too large");
wdenkfe8c2802002-11-03 00:38:21 +0000416 pkt += 14 /*strlen("File too large")*/ + 1;
417 len = pkt - xp;
418 break;
419
420 case STATE_BAD_MAGIC:
421 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200422 s = (ushort *)pkt;
423 *s++ = htons(TFTP_ERROR);
424 *s++ = htons(2);
425 pkt = (uchar *)s;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000426 strcpy((char *)pkt, "File has bad magic");
wdenkfe8c2802002-11-03 00:38:21 +0000427 pkt += 18 /*strlen("File has bad magic")*/ + 1;
428 len = pkt - xp;
429 break;
430 }
431
Luca Ceresoli5079c762011-05-17 00:03:37 +0000432 NetSendUDPPacket(NetServerEther, TftpRemoteIP, TftpRemotePort,
Luca Ceresolic42fc272011-05-14 05:49:56 +0000433 TftpOurPort, len);
wdenkfe8c2802002-11-03 00:38:21 +0000434}
435
Simon Glass2928cd82011-10-26 14:18:38 +0000436#ifdef CONFIG_CMD_TFTPPUT
Simon Glass6a398d22011-10-24 18:00:07 +0000437static void icmp_handler(unsigned type, unsigned code, unsigned dest,
438 IPaddr_t sip, unsigned src, uchar *pkt, unsigned len)
439{
440 if (type == ICMP_NOT_REACH && code == ICMP_NOT_REACH_PORT) {
441 /* Oh dear the other end has gone away */
442 restart("TFTP server died");
443 }
444}
Simon Glass2928cd82011-10-26 14:18:38 +0000445#endif
wdenkfe8c2802002-11-03 00:38:21 +0000446
447static void
Luca Ceresoli428ab362011-04-18 06:19:50 +0000448TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
449 unsigned len)
wdenkfe8c2802002-11-03 00:38:21 +0000450{
Kim Phillips878dad02013-01-16 18:09:19 -0600451 __be16 proto;
452 __be16 *s;
Wolfgang Denk83e8e472007-08-30 14:42:15 +0200453 int i;
wdenkfe8c2802002-11-03 00:38:21 +0000454
455 if (dest != TftpOurPort) {
David Updegraff7280da72007-06-11 10:41:07 -0500456#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200457 if (Multicast
David Updegraff7280da72007-06-11 10:41:07 -0500458 && (!Mcast_port || (dest != Mcast_port)))
459#endif
Luca Ceresoli09b18232011-05-14 05:50:02 +0000460 return;
wdenkfe8c2802002-11-03 00:38:21 +0000461 }
Luca Ceresolib640ed32011-05-17 00:03:39 +0000462 if (TftpState != STATE_SEND_RRQ && src != TftpRemotePort &&
Simon Glass6a398d22011-10-24 18:00:07 +0000463 TftpState != STATE_RECV_WRQ && TftpState != STATE_SEND_WRQ)
wdenkfe8c2802002-11-03 00:38:21 +0000464 return;
wdenkfe8c2802002-11-03 00:38:21 +0000465
Luca Ceresoli35dfb182011-05-14 05:50:00 +0000466 if (len < 2)
wdenkfe8c2802002-11-03 00:38:21 +0000467 return;
wdenkfe8c2802002-11-03 00:38:21 +0000468 len -= 2;
469 /* warning: don't use increment (++) in ntohs() macros!! */
Kim Phillips878dad02013-01-16 18:09:19 -0600470 s = (__be16 *)pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200471 proto = *s++;
472 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000473 switch (ntohs(proto)) {
474
475 case TFTP_RRQ:
Simon Glass6a398d22011-10-24 18:00:07 +0000476 break;
477
wdenkfe8c2802002-11-03 00:38:21 +0000478 case TFTP_ACK:
Simon Glass6a398d22011-10-24 18:00:07 +0000479#ifdef CONFIG_CMD_TFTPPUT
480 if (TftpWriting) {
481 if (TftpFinalBlock) {
482 tftp_complete();
483 } else {
484 /*
485 * Move to the next block. We want our block
486 * count to wrap just like the other end!
487 */
488 int block = ntohs(*s);
489 int ack_ok = (TftpBlock == block);
490
491 TftpBlock = (unsigned short)(block + 1);
492 update_block_number();
493 if (ack_ok)
494 TftpSend(); /* Send next data block */
495 }
496 }
497#endif
wdenkfe8c2802002-11-03 00:38:21 +0000498 break;
Simon Glass6a398d22011-10-24 18:00:07 +0000499
wdenkfe8c2802002-11-03 00:38:21 +0000500 default:
501 break;
502
Luca Ceresolib640ed32011-05-17 00:03:39 +0000503#ifdef CONFIG_CMD_TFTPSRV
504 case TFTP_WRQ:
505 debug("Got WRQ\n");
506 TftpRemoteIP = sip;
507 TftpRemotePort = src;
508 TftpOurPort = 1024 + (get_timer(0) % 3072);
Simon Glassbd8a0e02011-10-24 18:00:04 +0000509 new_transfer();
Luca Ceresolib640ed32011-05-17 00:03:39 +0000510 TftpSend(); /* Send ACK(0) */
511 break;
512#endif
513
wdenk7182b0f2003-10-06 21:55:32 +0000514 case TFTP_OACK:
Wolfgang Denk12730c72009-08-10 09:59:10 +0200515 debug("Got OACK: %s %s\n",
516 pkt,
517 pkt + strlen((char *)pkt) + 1);
wdenk7182b0f2003-10-06 21:55:32 +0000518 TftpState = STATE_OACK;
Luca Ceresoli5079c762011-05-17 00:03:37 +0000519 TftpRemotePort = src;
Wolfgang Denk8b70f342007-08-31 10:01:51 +0200520 /*
521 * Check for 'blksize' option.
522 * Careful: "i" is signed, "len" is unsigned, thus
523 * something like "len-8" may give a *huge* number
524 */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000525 for (i = 0; i+8 < len; i++) {
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000526 if (strcmp((char *)pkt+i, "blksize") == 0) {
Wolfgang Denk83e8e472007-08-30 14:42:15 +0200527 TftpBlkSize = (unsigned short)
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000528 simple_strtoul((char *)pkt+i+8, NULL,
Luca Ceresolif40538a2011-05-14 05:49:57 +0000529 10);
Robin Getz9e0a4d62009-07-23 03:01:03 -0400530 debug("Blocksize ack: %s, %d\n",
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000531 (char *)pkt+i+8, TftpBlkSize);
Wolfgang Denk83e8e472007-08-30 14:42:15 +0200532 }
Robin Getzb3b00ed2009-08-20 10:50:20 -0400533#ifdef CONFIG_TFTP_TSIZE
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000534 if (strcmp((char *)pkt+i, "tsize") == 0) {
535 TftpTsize = simple_strtoul((char *)pkt+i+6,
Luca Ceresolic42fc272011-05-14 05:49:56 +0000536 NULL, 10);
Robin Getzb3b00ed2009-08-20 10:50:20 -0400537 debug("size = %s, %d\n",
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000538 (char *)pkt+i+6, TftpTsize);
Robin Getzb3b00ed2009-08-20 10:50:20 -0400539 }
540#endif
David Updegraff7280da72007-06-11 10:41:07 -0500541 }
542#ifdef CONFIG_MCAST_TFTP
Luca Ceresolif40538a2011-05-14 05:49:57 +0000543 parse_multicast_oack((char *)pkt, len-1);
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200544 if ((Multicast) && (!MasterClient))
David Updegraff7280da72007-06-11 10:41:07 -0500545 TftpState = STATE_DATA; /* passive.. */
546 else
547#endif
Simon Glass6a398d22011-10-24 18:00:07 +0000548#ifdef CONFIG_CMD_TFTPPUT
549 if (TftpWriting) {
550 /* Get ready to send the first block */
551 TftpState = STATE_DATA;
552 TftpBlock++;
553 }
554#endif
555 TftpSend(); /* Send ACK or first data block */
wdenk7182b0f2003-10-06 21:55:32 +0000556 break;
wdenkfe8c2802002-11-03 00:38:21 +0000557 case TFTP_DATA:
558 if (len < 2)
559 return;
560 len -= 2;
Kim Phillips878dad02013-01-16 18:09:19 -0600561 TftpBlock = ntohs(*(__be16 *)pkt);
wdenkef893942004-02-23 16:11:30 +0000562
Simon Glassbd8a0e02011-10-24 18:00:04 +0000563 update_block_number();
wdenkfe8c2802002-11-03 00:38:21 +0000564
Luca Ceresoli562dfe52011-05-17 00:03:38 +0000565 if (TftpState == STATE_SEND_RRQ)
Robin Getz9e0a4d62009-07-23 03:01:03 -0400566 debug("Server did not acknowledge timeout option!\n");
wdenk7182b0f2003-10-06 21:55:32 +0000567
Luca Ceresolib640ed32011-05-17 00:03:39 +0000568 if (TftpState == STATE_SEND_RRQ || TftpState == STATE_OACK ||
569 TftpState == STATE_RECV_WRQ) {
wdenkef893942004-02-23 16:11:30 +0000570 /* first block received */
wdenkfe8c2802002-11-03 00:38:21 +0000571 TftpState = STATE_DATA;
Luca Ceresoli5079c762011-05-17 00:03:37 +0000572 TftpRemotePort = src;
Simon Glassbd8a0e02011-10-24 18:00:04 +0000573 new_transfer();
wdenkfe8c2802002-11-03 00:38:21 +0000574
David Updegraff7280da72007-06-11 10:41:07 -0500575#ifdef CONFIG_MCAST_TFTP
576 if (Multicast) { /* start!=1 common if mcast */
577 TftpLastBlock = TftpBlock - 1;
578 } else
579#endif
wdenkfe8c2802002-11-03 00:38:21 +0000580 if (TftpBlock != 1) { /* Assertion */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000581 printf("\nTFTP error: "
582 "First block is not block 1 (%ld)\n"
583 "Starting again\n\n",
wdenkfe8c2802002-11-03 00:38:21 +0000584 TftpBlock);
Luca Ceresolif40538a2011-05-14 05:49:57 +0000585 NetStartAgain();
wdenkfe8c2802002-11-03 00:38:21 +0000586 break;
587 }
588 }
589
590 if (TftpBlock == TftpLastBlock) {
591 /*
592 * Same block again; ignore it.
593 */
594 break;
595 }
596
597 TftpLastBlock = TftpBlock;
Bartlomiej Sieka371cde72008-10-01 15:26:29 +0200598 TftpTimeoutCountMax = TIMEOUT_COUNT;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000599 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
wdenkfe8c2802002-11-03 00:38:21 +0000600
Luca Ceresolif40538a2011-05-14 05:49:57 +0000601 store_block(TftpBlock - 1, pkt + 2, len);
wdenkfe8c2802002-11-03 00:38:21 +0000602
603 /*
Luca Ceresoli2f3dc0a2011-05-17 00:03:41 +0000604 * Acknowledge the block just received, which will prompt
Luca Ceresoli5079c762011-05-17 00:03:37 +0000605 * the remote for the next one.
wdenkfe8c2802002-11-03 00:38:21 +0000606 */
David Updegraff7280da72007-06-11 10:41:07 -0500607#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200608 /* if I am the MasterClient, actively calculate what my next
609 * needed block is; else I'm passive; not ACKING
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100610 */
David Updegraff7280da72007-06-11 10:41:07 -0500611 if (Multicast) {
612 if (len < TftpBlkSize) {
613 TftpEndingBlock = TftpBlock;
614 } else if (MasterClient) {
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200615 TftpBlock = PrevBitmapHole =
David Updegraff7280da72007-06-11 10:41:07 -0500616 ext2_find_next_zero_bit(
617 Bitmap,
618 (Mapsize*8),
619 PrevBitmapHole);
620 if (TftpBlock > ((Mapsize*8) - 1)) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000621 printf("tftpfile too big\n");
David Updegraff7280da72007-06-11 10:41:07 -0500622 /* try to double it and retry */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000623 Mapsize <<= 1;
David Updegraff7280da72007-06-11 10:41:07 -0500624 mcast_cleanup();
Luca Ceresolif40538a2011-05-14 05:49:57 +0000625 NetStartAgain();
David Updegraff7280da72007-06-11 10:41:07 -0500626 return;
627 }
628 TftpLastBlock = TftpBlock;
629 }
630 }
631#endif
Luca Ceresolif40538a2011-05-14 05:49:57 +0000632 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000633
David Updegraff7280da72007-06-11 10:41:07 -0500634#ifdef CONFIG_MCAST_TFTP
635 if (Multicast) {
636 if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000637 puts("\nMulticast tftp done\n");
David Updegraff7280da72007-06-11 10:41:07 -0500638 mcast_cleanup();
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000639 net_set_state(NETLOOP_SUCCESS);
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200640 }
Joe Hershberger3a29e5a2012-05-15 08:59:12 +0000641 } else
David Updegraff7280da72007-06-11 10:41:07 -0500642#endif
Simon Glassb437aad2011-10-24 18:00:03 +0000643 if (len < TftpBlkSize)
644 tftp_complete();
wdenkfe8c2802002-11-03 00:38:21 +0000645 break;
646
647 case TFTP_ERROR:
Luca Ceresolif40538a2011-05-14 05:49:57 +0000648 printf("\nTFTP error: '%s' (%d)\n",
Kim Phillips878dad02013-01-16 18:09:19 -0600649 pkt + 2, ntohs(*(__be16 *)pkt));
Remy Bohmerb46293f2009-10-28 22:13:40 +0100650
Kim Phillips878dad02013-01-16 18:09:19 -0600651 switch (ntohs(*(__be16 *)pkt)) {
Remy Bohmerb46293f2009-10-28 22:13:40 +0100652 case TFTP_ERR_FILE_NOT_FOUND:
653 case TFTP_ERR_ACCESS_DENIED:
654 puts("Not retrying...\n");
655 eth_halt();
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000656 net_set_state(NETLOOP_FAIL);
Remy Bohmerb46293f2009-10-28 22:13:40 +0100657 break;
658 case TFTP_ERR_UNDEFINED:
659 case TFTP_ERR_DISK_FULL:
660 case TFTP_ERR_UNEXPECTED_OPCODE:
661 case TFTP_ERR_UNKNOWN_TRANSFER_ID:
662 case TFTP_ERR_FILE_ALREADY_EXISTS:
663 default:
664 puts("Starting again\n\n");
David Updegraff7280da72007-06-11 10:41:07 -0500665#ifdef CONFIG_MCAST_TFTP
Remy Bohmerb46293f2009-10-28 22:13:40 +0100666 mcast_cleanup();
David Updegraff7280da72007-06-11 10:41:07 -0500667#endif
Remy Bohmerb46293f2009-10-28 22:13:40 +0100668 NetStartAgain();
669 break;
670 }
wdenkfe8c2802002-11-03 00:38:21 +0000671 break;
672 }
673}
674
675
676static void
Luca Ceresolif40538a2011-05-14 05:49:57 +0000677TftpTimeout(void)
wdenkfe8c2802002-11-03 00:38:21 +0000678{
Bartlomiej Sieka371cde72008-10-01 15:26:29 +0200679 if (++TftpTimeoutCount > TftpTimeoutCountMax) {
Simon Glassbd8a0e02011-10-24 18:00:04 +0000680 restart("Retry count exceeded");
wdenkfe8c2802002-11-03 00:38:21 +0000681 } else {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000682 puts("T ");
683 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
Luca Ceresolib640ed32011-05-17 00:03:39 +0000684 if (TftpState != STATE_RECV_WRQ)
685 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000686 }
687}
688
689
Simon Glassf59675a2011-10-24 18:00:05 +0000690void TftpStart(enum proto_t protocol)
wdenkfe8c2802002-11-03 00:38:21 +0000691{
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200692 char *ep; /* Environment pointer */
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200693
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100694 /*
695 * Allow the user to choose TFTP blocksize and timeout.
696 * TFTP protocol has a minimal timeout of 1 second.
697 */
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000698 ep = getenv("tftpblocksize");
699 if (ep != NULL)
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200700 TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100701
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000702 ep = getenv("tftptimeout");
703 if (ep != NULL)
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100704 TftpTimeoutMSecs = simple_strtol(ep, NULL, 10);
705
706 if (TftpTimeoutMSecs < 1000) {
707 printf("TFTP timeout (%ld ms) too low, "
708 "set minimum = 1000 ms\n",
709 TftpTimeoutMSecs);
710 TftpTimeoutMSecs = 1000;
711 }
712
713 debug("TFTP blocksize = %i, timeout = %ld ms\n",
714 TftpBlkSizeOption, TftpTimeoutMSecs);
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200715
Luca Ceresoli5079c762011-05-17 00:03:37 +0000716 TftpRemoteIP = NetServerIP;
wdenkfe8c2802002-11-03 00:38:21 +0000717 if (BootFile[0] == '\0') {
Matthias Weisser6cc63262011-12-03 03:29:44 +0000718 sprintf(default_filename, "%02X%02X%02X%02X.img",
Wolfgang Denkdec97322005-08-04 01:09:44 +0200719 NetOurIP & 0xFF,
720 (NetOurIP >> 8) & 0xFF,
721 (NetOurIP >> 16) & 0xFF,
Luca Ceresolif40538a2011-05-14 05:49:57 +0000722 (NetOurIP >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100723
724 strncpy(tftp_filename, default_filename, MAX_LEN);
725 tftp_filename[MAX_LEN-1] = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000726
Luca Ceresolif40538a2011-05-14 05:49:57 +0000727 printf("*** Warning: no boot file name; using '%s'\n",
wdenkfe8c2802002-11-03 00:38:21 +0000728 tftp_filename);
729 } else {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000730 char *p = strchr(BootFile, ':');
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100731
732 if (p == NULL) {
733 strncpy(tftp_filename, BootFile, MAX_LEN);
734 tftp_filename[MAX_LEN-1] = 0;
735 } else {
Luca Ceresoli5079c762011-05-17 00:03:37 +0000736 TftpRemoteIP = string_to_ip(BootFile);
Peter Tyser5e58efa2008-12-01 16:29:38 -0600737 strncpy(tftp_filename, p + 1, MAX_LEN);
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100738 tftp_filename[MAX_LEN-1] = 0;
739 }
wdenkfe8c2802002-11-03 00:38:21 +0000740 }
741
Luca Ceresolif40538a2011-05-14 05:49:57 +0000742 printf("Using %s device\n", eth_get_name());
Simon Glass6a398d22011-10-24 18:00:07 +0000743 printf("TFTP %s server %pI4; our IP address is %pI4",
Simon Glass4abd0e22011-10-27 06:24:29 +0000744#ifdef CONFIG_CMD_TFTPPUT
745 protocol == TFTPPUT ? "to" : "from",
746#else
747 "from",
748#endif
749 &TftpRemoteIP, &NetOurIP);
wdenkfe8c2802002-11-03 00:38:21 +0000750
751 /* Check if we need to send across this subnet */
752 if (NetOurGatewayIP && NetOurSubnetMask) {
Luca Ceresoli09b18232011-05-14 05:50:02 +0000753 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
Luca Ceresoli5079c762011-05-17 00:03:37 +0000754 IPaddr_t RemoteNet = TftpRemoteIP & NetOurSubnetMask;
wdenkfe8c2802002-11-03 00:38:21 +0000755
Luca Ceresoli5079c762011-05-17 00:03:37 +0000756 if (OurNet != RemoteNet)
Luca Ceresoli09b18232011-05-14 05:50:02 +0000757 printf("; sending through gateway %pI4",
758 &NetOurGatewayIP);
wdenkfe8c2802002-11-03 00:38:21 +0000759 }
Luca Ceresolif40538a2011-05-14 05:49:57 +0000760 putc('\n');
wdenkfe8c2802002-11-03 00:38:21 +0000761
Luca Ceresolif40538a2011-05-14 05:49:57 +0000762 printf("Filename '%s'.", tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000763
764 if (NetBootFileSize) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000765 printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
766 print_size(NetBootFileSize<<9, "");
wdenkfe8c2802002-11-03 00:38:21 +0000767 }
768
Luca Ceresolif40538a2011-05-14 05:49:57 +0000769 putc('\n');
Simon Glass6a398d22011-10-24 18:00:07 +0000770#ifdef CONFIG_CMD_TFTPPUT
771 TftpWriting = (protocol == TFTPPUT);
772 if (TftpWriting) {
773 printf("Save address: 0x%lx\n", save_addr);
774 printf("Save size: 0x%lx\n", save_size);
775 NetBootFileXferSize = save_size;
776 puts("Saving: *\b");
777 TftpState = STATE_SEND_WRQ;
778 new_transfer();
779 } else
780#endif
781 {
782 printf("Load address: 0x%lx\n", load_addr);
783 puts("Loading: *\b");
784 TftpState = STATE_SEND_RRQ;
785 }
wdenkfe8c2802002-11-03 00:38:21 +0000786
Simon Glasscab9a8d2012-10-11 13:57:36 +0000787 time_start = get_timer(0);
Bartlomiej Sieka371cde72008-10-01 15:26:29 +0200788 TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
789
Luca Ceresolif40538a2011-05-14 05:49:57 +0000790 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000791 net_set_udp_handler(TftpHandler);
Simon Glass2928cd82011-10-26 14:18:38 +0000792#ifdef CONFIG_CMD_TFTPPUT
Simon Glass6a398d22011-10-24 18:00:07 +0000793 net_set_icmp_handler(icmp_handler);
Simon Glass2928cd82011-10-26 14:18:38 +0000794#endif
Luca Ceresoli5079c762011-05-17 00:03:37 +0000795 TftpRemotePort = WELL_KNOWN_PORT;
wdenkfe8c2802002-11-03 00:38:21 +0000796 TftpTimeoutCount = 0;
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200797 /* Use a pseudo-random port unless a specific port is set */
wdenkfe8c2802002-11-03 00:38:21 +0000798 TftpOurPort = 1024 + (get_timer(0) % 3072);
David Updegraff7280da72007-06-11 10:41:07 -0500799
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200800#ifdef CONFIG_TFTP_PORT
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000801 ep = getenv("tftpdstp");
Luca Ceresoli35dfb182011-05-14 05:50:00 +0000802 if (ep != NULL)
Luca Ceresoli5079c762011-05-17 00:03:37 +0000803 TftpRemotePort = simple_strtol(ep, NULL, 10);
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000804 ep = getenv("tftpsrcp");
Luca Ceresoli35dfb182011-05-14 05:50:00 +0000805 if (ep != NULL)
Luca Ceresolif40538a2011-05-14 05:49:57 +0000806 TftpOurPort = simple_strtol(ep, NULL, 10);
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200807#endif
wdenk7182b0f2003-10-06 21:55:32 +0000808 TftpBlock = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000809
wdenke6466f62003-06-05 19:27:42 +0000810 /* zero out server ether in case the server ip has changed */
811 memset(NetServerEther, 0, 6);
David Updegraff7280da72007-06-11 10:41:07 -0500812 /* Revert TftpBlkSize to dflt */
813 TftpBlkSize = TFTP_BLOCK_SIZE;
814#ifdef CONFIG_MCAST_TFTP
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100815 mcast_cleanup();
David Updegraff7280da72007-06-11 10:41:07 -0500816#endif
Robin Getzb3b00ed2009-08-20 10:50:20 -0400817#ifdef CONFIG_TFTP_TSIZE
818 TftpTsize = 0;
819 TftpNumchars = 0;
820#endif
wdenke6466f62003-06-05 19:27:42 +0000821
Luca Ceresolif40538a2011-05-14 05:49:57 +0000822 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000823}
Luca Ceresolib640ed32011-05-17 00:03:39 +0000824
825#ifdef CONFIG_CMD_TFTPSRV
826void
827TftpStartServer(void)
828{
829 tftp_filename[0] = 0;
830
Luca Ceresolib640ed32011-05-17 00:03:39 +0000831 printf("Using %s device\n", eth_get_name());
Luca Ceresolib640ed32011-05-17 00:03:39 +0000832 printf("Listening for TFTP transfer on %pI4\n", &NetOurIP);
833 printf("Load address: 0x%lx\n", load_addr);
834
835 puts("Loading: *\b");
836
837 TftpTimeoutCountMax = TIMEOUT_COUNT;
838 TftpTimeoutCount = 0;
839 TftpTimeoutMSecs = TIMEOUT;
840 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
841
842 /* Revert TftpBlkSize to dflt */
843 TftpBlkSize = TFTP_BLOCK_SIZE;
844 TftpBlock = 0;
845 TftpOurPort = WELL_KNOWN_PORT;
846
847#ifdef CONFIG_TFTP_TSIZE
848 TftpTsize = 0;
849 TftpNumchars = 0;
850#endif
851
852 TftpState = STATE_RECV_WRQ;
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000853 net_set_udp_handler(TftpHandler);
Andrew Ruder91393c52013-10-22 19:10:28 -0500854
855 /* zero out server ether in case the server ip has changed */
856 memset(NetServerEther, 0, 6);
Luca Ceresolib640ed32011-05-17 00:03:39 +0000857}
858#endif /* CONFIG_CMD_TFTPSRV */
wdenkfe8c2802002-11-03 00:38:21 +0000859
David Updegraff7280da72007-06-11 10:41:07 -0500860#ifdef CONFIG_MCAST_TFTP
861/* Credits: atftp project.
862 */
863
864/* pick up BcastAddr, Port, and whether I am [now] the master-client. *
865 * Frame:
866 * +-------+-----------+---+-------~~-------+---+
867 * | opc | multicast | 0 | addr, port, mc | 0 |
868 * +-------+-----------+---+-------~~-------+---+
869 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
870 * I am the new master-client so must send ACKs to DataBlocks. If I am not
871 * master-client, I'm a passive client, gathering what DataBlocks I may and
872 * making note of which ones I got in my bitmask.
873 * In theory, I never go from master->passive..
874 * .. this comes in with pkt already pointing just past opc
875 */
876static void parse_multicast_oack(char *pkt, int len)
877{
Luca Ceresolif40538a2011-05-14 05:49:57 +0000878 int i;
879 IPaddr_t addr;
880 char *mc_adr, *port, *mc;
David Updegraff7280da72007-06-11 10:41:07 -0500881
Luca Ceresolif40538a2011-05-14 05:49:57 +0000882 mc_adr = port = mc = NULL;
David Updegraff7280da72007-06-11 10:41:07 -0500883 /* march along looking for 'multicast\0', which has to start at least
884 * 14 bytes back from the end.
885 */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000886 for (i = 0; i < len-14; i++)
887 if (strcmp(pkt+i, "multicast") == 0)
David Updegraff7280da72007-06-11 10:41:07 -0500888 break;
889 if (i >= (len-14)) /* non-Multicast OACK, ign. */
890 return;
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200891
Luca Ceresolif40538a2011-05-14 05:49:57 +0000892 i += 10; /* strlen multicast */
David Updegraff7280da72007-06-11 10:41:07 -0500893 mc_adr = pkt+i;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000894 for (; i < len; i++) {
David Updegraff7280da72007-06-11 10:41:07 -0500895 if (*(pkt+i) == ',') {
896 *(pkt+i) = '\0';
897 if (port) {
898 mc = pkt+i+1;
899 break;
900 } else {
901 port = pkt+i+1;
902 }
903 }
904 }
Luca Ceresoli6072fcd2011-05-14 05:50:01 +0000905 if (!port || !mc_adr || !mc)
906 return;
David Updegraff7280da72007-06-11 10:41:07 -0500907 if (Multicast && MasterClient) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000908 printf("I got a OACK as master Client, WRONG!\n");
David Updegraff7280da72007-06-11 10:41:07 -0500909 return;
910 }
911 /* ..I now accept packets destined for this MCAST addr, port */
912 if (!Multicast) {
913 if (Bitmap) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000914 printf("Internal failure! no mcast.\n");
David Updegraff7280da72007-06-11 10:41:07 -0500915 free(Bitmap);
Luca Ceresolif40538a2011-05-14 05:49:57 +0000916 Bitmap = NULL;
917 ProhibitMcast = 1;
David Updegraff7280da72007-06-11 10:41:07 -0500918 return ;
919 }
920 /* I malloc instead of pre-declare; so that if the file ends
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200921 * up being too big for this bitmap I can retry
922 */
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000923 Bitmap = malloc(Mapsize);
924 if (!Bitmap) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000925 printf("No Bitmap, no multicast. Sorry.\n");
926 ProhibitMcast = 1;
David Updegraff7280da72007-06-11 10:41:07 -0500927 return;
928 }
Luca Ceresolif40538a2011-05-14 05:49:57 +0000929 memset(Bitmap, 0, Mapsize);
David Updegraff7280da72007-06-11 10:41:07 -0500930 PrevBitmapHole = 0;
931 Multicast = 1;
932 }
933 addr = string_to_ip(mc_adr);
934 if (Mcast_addr != addr) {
935 if (Mcast_addr)
936 eth_mcast_join(Mcast_addr, 0);
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000937 Mcast_addr = addr;
938 if (eth_mcast_join(Mcast_addr, 1)) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000939 printf("Fail to set mcast, revert to TFTP\n");
940 ProhibitMcast = 1;
David Updegraff7280da72007-06-11 10:41:07 -0500941 mcast_cleanup();
942 NetStartAgain();
943 }
944 }
Luca Ceresolif40538a2011-05-14 05:49:57 +0000945 MasterClient = (unsigned char)simple_strtoul((char *)mc, NULL, 10);
946 Mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
947 printf("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
David Updegraff7280da72007-06-11 10:41:07 -0500948 return;
949}
950
951#endif /* Multicast TFTP */