blob: 51c67be952222200df6a0ab04ef8a08089641d27 [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
Luca Ceresoli5079c762011-05-17 00:03:37 +000068static IPaddr_t TftpRemoteIP;
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{
Luca Ceresoli6072fcd2011-05-14 05:50:01 +0000149 if (Mcast_addr)
150 eth_mcast_join(Mcast_addr, 0);
151 if (Bitmap)
152 free(Bitmap);
Luca Ceresolif40538a2011-05-14 05:49:57 +0000153 Bitmap = NULL;
David Updegraff7280da72007-06-11 10:41:07 -0500154 Mcast_addr = Multicast = Mcast_port = 0;
155 TftpEndingBlock = -1;
156}
157
158#endif /* CONFIG_MCAST_TFTP */
159
Joe Hershberger3a29e5a2012-05-15 08:59:12 +0000160static inline void
Jayachandran Chandrasekharan Naira5d451b2012-07-10 11:48:54 +0530161store_block(int block, uchar *src, unsigned len)
wdenkfe8c2802002-11-03 00:38:21 +0000162{
David Updegraff7280da72007-06-11 10:41:07 -0500163 ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
wdenkef893942004-02-23 16:11:30 +0000164 ulong newsize = offset + len;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200165#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
wdenkfe8c2802002-11-03 00:38:21 +0000166 int i, rc = 0;
167
Luca Ceresolif40538a2011-05-14 05:49:57 +0000168 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenkfe8c2802002-11-03 00:38:21 +0000169 /* start address in flash? */
Jochen Friedrichd31a59b2008-09-02 11:24:59 +0200170 if (flash_info[i].flash_id == FLASH_UNKNOWN)
171 continue;
wdenkfe8c2802002-11-03 00:38:21 +0000172 if (load_addr + offset >= flash_info[i].start[0]) {
173 rc = 1;
174 break;
175 }
176 }
177
178 if (rc) { /* Flash is destination for this packet */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000179 rc = flash_write((char *)src, (ulong)(load_addr+offset), len);
wdenkfe8c2802002-11-03 00:38:21 +0000180 if (rc) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000181 flash_perror(rc);
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000182 net_set_state(NETLOOP_FAIL);
wdenkfe8c2802002-11-03 00:38:21 +0000183 return;
184 }
Joe Hershberger3a29e5a2012-05-15 08:59:12 +0000185 } else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200186#endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000187 {
Joe Hershbergered191852015-03-22 17:09:08 -0500188 void *ptr = map_sysmem(load_addr + offset, len);
189
190 memcpy(ptr, src, len);
191 unmap_sysmem(ptr);
wdenkfe8c2802002-11-03 00:38:21 +0000192 }
David Updegraff7280da72007-06-11 10:41:07 -0500193#ifdef CONFIG_MCAST_TFTP
194 if (Multicast)
195 ext2_set_bit(block, Bitmap);
196#endif
wdenkfe8c2802002-11-03 00:38:21 +0000197
198 if (NetBootFileXferSize < newsize)
199 NetBootFileXferSize = newsize;
200}
201
Simon Glassbd8a0e02011-10-24 18:00:04 +0000202/* Clear our state ready for a new transfer */
Simon Glass84313322011-10-27 06:24:28 +0000203static void new_transfer(void)
Simon Glassbd8a0e02011-10-24 18:00:04 +0000204{
205 TftpLastBlock = 0;
206 TftpBlockWrap = 0;
207 TftpBlockWrapOffset = 0;
208#ifdef CONFIG_CMD_TFTPPUT
209 TftpFinalBlock = 0;
210#endif
211}
Simon Glass6a398d22011-10-24 18:00:07 +0000212
213#ifdef CONFIG_CMD_TFTPPUT
214/**
215 * Load the next block from memory to be sent over tftp.
216 *
217 * @param block Block number to send
218 * @param dst Destination buffer for data
219 * @param len Number of bytes in block (this one and every other)
220 * @return number of bytes loaded
221 */
222static int load_block(unsigned block, uchar *dst, unsigned len)
223{
224 /* We may want to get the final block from the previous set */
225 ulong offset = ((int)block - 1) * len + TftpBlockWrapOffset;
226 ulong tosend = len;
227
228 tosend = min(NetBootFileXferSize - offset, tosend);
229 (void)memcpy(dst, (void *)(save_addr + offset), tosend);
230 debug("%s: block=%d, offset=%ld, len=%d, tosend=%ld\n", __func__,
231 block, offset, len, tosend);
232 return tosend;
233}
234#endif
Simon Glassbd8a0e02011-10-24 18:00:04 +0000235
Luca Ceresolif40538a2011-05-14 05:49:57 +0000236static void TftpSend(void);
237static void TftpTimeout(void);
wdenkfe8c2802002-11-03 00:38:21 +0000238
239/**********************************************************************/
240
Simon Glassb437aad2011-10-24 18:00:03 +0000241static void show_block_marker(void)
242{
243#ifdef CONFIG_TFTP_TSIZE
244 if (TftpTsize) {
245 ulong pos = TftpBlock * TftpBlkSize + TftpBlockWrapOffset;
246
247 while (TftpNumchars < pos * 50 / TftpTsize) {
248 putc('#');
249 TftpNumchars++;
250 }
Simon Glass6a398d22011-10-24 18:00:07 +0000251 } else
Simon Glassb437aad2011-10-24 18:00:03 +0000252#endif
Simon Glass6a398d22011-10-24 18:00:07 +0000253 {
Simon Glassb437aad2011-10-24 18:00:03 +0000254 if (((TftpBlock - 1) % 10) == 0)
255 putc('#');
256 else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0)
257 puts("\n\t ");
258 }
259}
260
Simon Glassbd8a0e02011-10-24 18:00:04 +0000261/**
262 * restart the current transfer due to an error
263 *
264 * @param msg Message to print for user
265 */
266static void restart(const char *msg)
267{
268 printf("\n%s; starting again\n", msg);
269#ifdef CONFIG_MCAST_TFTP
270 mcast_cleanup();
271#endif
272 NetStartAgain();
273}
274
275/*
276 * Check if the block number has wrapped, and update progress
277 *
278 * TODO: The egregious use of global variables in this file should be tidied.
279 */
280static void update_block_number(void)
281{
282 /*
283 * RFC1350 specifies that the first data packet will
284 * have sequence number 1. If we receive a sequence
285 * number of 0 this means that there was a wrap
286 * around of the (16 bit) counter.
287 */
rockly0d757362013-08-03 18:09:05 +0800288 if (TftpBlock == 0 && TftpLastBlock != 0) {
Simon Glassbd8a0e02011-10-24 18:00:04 +0000289 TftpBlockWrap++;
290 TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE;
291 TftpTimeoutCount = 0; /* we've done well, reset thhe timeout */
292 } else {
293 show_block_marker();
294 }
295}
296
Simon Glassb437aad2011-10-24 18:00:03 +0000297/* The TFTP get or put is complete */
298static void tftp_complete(void)
299{
300#ifdef CONFIG_TFTP_TSIZE
301 /* Print hash marks for the last packet received */
302 while (TftpTsize && TftpNumchars < 49) {
303 putc('#');
304 TftpNumchars++;
305 }
Simon Glassc8f0a5c2014-10-10 07:30:21 -0600306 puts(" ");
307 print_size(TftpTsize, "");
Simon Glassb437aad2011-10-24 18:00:03 +0000308#endif
Simon Glasscab9a8d2012-10-11 13:57:36 +0000309 time_start = get_timer(time_start);
310 if (time_start > 0) {
311 puts("\n\t "); /* Line up with "Loading: " */
312 print_size(NetBootFileXferSize /
313 time_start * 1000, "/s");
314 }
Simon Glassb437aad2011-10-24 18:00:03 +0000315 puts("\ndone\n");
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000316 net_set_state(NETLOOP_SUCCESS);
Simon Glassb437aad2011-10-24 18:00:03 +0000317}
318
wdenkfe8c2802002-11-03 00:38:21 +0000319static void
Luca Ceresolif40538a2011-05-14 05:49:57 +0000320TftpSend(void)
wdenkfe8c2802002-11-03 00:38:21 +0000321{
Simon Glass6a398d22011-10-24 18:00:07 +0000322 uchar *pkt;
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000323 uchar *xp;
324 int len = 0;
325 ushort *s;
wdenkfe8c2802002-11-03 00:38:21 +0000326
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200327#ifdef CONFIG_MCAST_TFTP
David Updegraff7280da72007-06-11 10:41:07 -0500328 /* Multicast TFTP.. non-MasterClients do not ACK data. */
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200329 if (Multicast
330 && (TftpState == STATE_DATA)
331 && (MasterClient == 0))
David Updegraff7280da72007-06-11 10:41:07 -0500332 return;
333#endif
wdenkfe8c2802002-11-03 00:38:21 +0000334 /*
335 * We will always be sending some sort of packet, so
336 * cobble together the packet headers now.
337 */
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000338 pkt = NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
wdenkfe8c2802002-11-03 00:38:21 +0000339
340 switch (TftpState) {
Luca Ceresoli562dfe52011-05-17 00:03:38 +0000341 case STATE_SEND_RRQ:
Simon Glass6a398d22011-10-24 18:00:07 +0000342 case STATE_SEND_WRQ:
wdenkfe8c2802002-11-03 00:38:21 +0000343 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200344 s = (ushort *)pkt;
Simon Glass4abd0e22011-10-27 06:24:29 +0000345#ifdef CONFIG_CMD_TFTPPUT
Simon Glass6a398d22011-10-24 18:00:07 +0000346 *s++ = htons(TftpState == STATE_SEND_RRQ ? TFTP_RRQ :
347 TFTP_WRQ);
Simon Glass4abd0e22011-10-27 06:24:29 +0000348#else
349 *s++ = htons(TFTP_RRQ);
350#endif
Wolfgang Denk3135c542005-08-26 01:36:03 +0200351 pkt = (uchar *)s;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000352 strcpy((char *)pkt, tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000353 pkt += strlen(tftp_filename) + 1;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000354 strcpy((char *)pkt, "octet");
wdenkfe8c2802002-11-03 00:38:21 +0000355 pkt += 5 /*strlen("octet")*/ + 1;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000356 strcpy((char *)pkt, "timeout");
wdenk7182b0f2003-10-06 21:55:32 +0000357 pkt += 7 /*strlen("timeout")*/ + 1;
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100358 sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000);
Robin Getz9e0a4d62009-07-23 03:01:03 -0400359 debug("send option \"timeout %s\"\n", (char *)pkt);
wdenk7182b0f2003-10-06 21:55:32 +0000360 pkt += strlen((char *)pkt) + 1;
Robin Getzb3b00ed2009-08-20 10:50:20 -0400361#ifdef CONFIG_TFTP_TSIZE
Simon Glass6a398d22011-10-24 18:00:07 +0000362 pkt += sprintf((char *)pkt, "tsize%c%lu%c",
363 0, NetBootFileXferSize, 0);
Robin Getzb3b00ed2009-08-20 10:50:20 -0400364#endif
David Updegraff7280da72007-06-11 10:41:07 -0500365 /* try for more effic. blk size */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000366 pkt += sprintf((char *)pkt, "blksize%c%d%c",
367 0, TftpBlkSizeOption, 0);
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200368#ifdef CONFIG_MCAST_TFTP
David Updegraff7280da72007-06-11 10:41:07 -0500369 /* Check all preconditions before even trying the option */
Joe Hershberger3a29e5a2012-05-15 08:59:12 +0000370 if (!ProhibitMcast) {
371 Bitmap = malloc(Mapsize);
372 if (Bitmap && eth_get_dev()->mcast) {
373 free(Bitmap);
374 Bitmap = NULL;
375 pkt += sprintf((char *)pkt, "multicast%c%c",
376 0, 0);
377 }
David Updegraff7280da72007-06-11 10:41:07 -0500378 }
379#endif /* CONFIG_MCAST_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000380 len = pkt - xp;
381 break;
382
wdenk7182b0f2003-10-06 21:55:32 +0000383 case STATE_OACK:
David Updegraff7280da72007-06-11 10:41:07 -0500384#ifdef CONFIG_MCAST_TFTP
385 /* My turn! Start at where I need blocks I missed.*/
386 if (Multicast)
Luca Ceresolif40538a2011-05-14 05:49:57 +0000387 TftpBlock = ext2_find_next_zero_bit(Bitmap,
388 (Mapsize*8), 0);
David Updegraff7280da72007-06-11 10:41:07 -0500389 /*..falling..*/
390#endif
Luca Ceresolib640ed32011-05-17 00:03:39 +0000391
392 case STATE_RECV_WRQ:
David Updegraff7280da72007-06-11 10:41:07 -0500393 case STATE_DATA:
wdenkfe8c2802002-11-03 00:38:21 +0000394 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200395 s = (ushort *)pkt;
Simon Glass6a398d22011-10-24 18:00:07 +0000396 s[0] = htons(TFTP_ACK);
397 s[1] = htons(TftpBlock);
398 pkt = (uchar *)(s + 2);
399#ifdef CONFIG_CMD_TFTPPUT
400 if (TftpWriting) {
401 int toload = TftpBlkSize;
402 int loaded = load_block(TftpBlock, pkt, toload);
403
404 s[0] = htons(TFTP_DATA);
405 pkt += loaded;
406 TftpFinalBlock = (loaded < toload);
407 }
408#endif
wdenkfe8c2802002-11-03 00:38:21 +0000409 len = pkt - xp;
410 break;
411
412 case STATE_TOO_LARGE:
413 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200414 s = (ushort *)pkt;
415 *s++ = htons(TFTP_ERROR);
Simon Glass6a398d22011-10-24 18:00:07 +0000416 *s++ = htons(3);
417
Wolfgang Denk3135c542005-08-26 01:36:03 +0200418 pkt = (uchar *)s;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000419 strcpy((char *)pkt, "File too large");
wdenkfe8c2802002-11-03 00:38:21 +0000420 pkt += 14 /*strlen("File too large")*/ + 1;
421 len = pkt - xp;
422 break;
423
424 case STATE_BAD_MAGIC:
425 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200426 s = (ushort *)pkt;
427 *s++ = htons(TFTP_ERROR);
428 *s++ = htons(2);
429 pkt = (uchar *)s;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000430 strcpy((char *)pkt, "File has bad magic");
wdenkfe8c2802002-11-03 00:38:21 +0000431 pkt += 18 /*strlen("File has bad magic")*/ + 1;
432 len = pkt - xp;
433 break;
434 }
435
Luca Ceresoli5079c762011-05-17 00:03:37 +0000436 NetSendUDPPacket(NetServerEther, TftpRemoteIP, TftpRemotePort,
Luca Ceresolic42fc272011-05-14 05:49:56 +0000437 TftpOurPort, len);
wdenkfe8c2802002-11-03 00:38:21 +0000438}
439
Simon Glass2928cd82011-10-26 14:18:38 +0000440#ifdef CONFIG_CMD_TFTPPUT
Simon Glass6a398d22011-10-24 18:00:07 +0000441static void icmp_handler(unsigned type, unsigned code, unsigned dest,
442 IPaddr_t sip, unsigned src, uchar *pkt, unsigned len)
443{
444 if (type == ICMP_NOT_REACH && code == ICMP_NOT_REACH_PORT) {
445 /* Oh dear the other end has gone away */
446 restart("TFTP server died");
447 }
448}
Simon Glass2928cd82011-10-26 14:18:38 +0000449#endif
wdenkfe8c2802002-11-03 00:38:21 +0000450
451static void
Luca Ceresoli428ab362011-04-18 06:19:50 +0000452TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
453 unsigned len)
wdenkfe8c2802002-11-03 00:38:21 +0000454{
Kim Phillips878dad02013-01-16 18:09:19 -0600455 __be16 proto;
456 __be16 *s;
Wolfgang Denk83e8e472007-08-30 14:42:15 +0200457 int i;
wdenkfe8c2802002-11-03 00:38:21 +0000458
459 if (dest != TftpOurPort) {
David Updegraff7280da72007-06-11 10:41:07 -0500460#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200461 if (Multicast
David Updegraff7280da72007-06-11 10:41:07 -0500462 && (!Mcast_port || (dest != Mcast_port)))
463#endif
Luca Ceresoli09b18232011-05-14 05:50:02 +0000464 return;
wdenkfe8c2802002-11-03 00:38:21 +0000465 }
Luca Ceresolib640ed32011-05-17 00:03:39 +0000466 if (TftpState != STATE_SEND_RRQ && src != TftpRemotePort &&
Simon Glass6a398d22011-10-24 18:00:07 +0000467 TftpState != STATE_RECV_WRQ && TftpState != STATE_SEND_WRQ)
wdenkfe8c2802002-11-03 00:38:21 +0000468 return;
wdenkfe8c2802002-11-03 00:38:21 +0000469
Luca Ceresoli35dfb182011-05-14 05:50:00 +0000470 if (len < 2)
wdenkfe8c2802002-11-03 00:38:21 +0000471 return;
wdenkfe8c2802002-11-03 00:38:21 +0000472 len -= 2;
473 /* warning: don't use increment (++) in ntohs() macros!! */
Kim Phillips878dad02013-01-16 18:09:19 -0600474 s = (__be16 *)pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200475 proto = *s++;
476 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000477 switch (ntohs(proto)) {
478
479 case TFTP_RRQ:
Simon Glass6a398d22011-10-24 18:00:07 +0000480 break;
481
wdenkfe8c2802002-11-03 00:38:21 +0000482 case TFTP_ACK:
Simon Glass6a398d22011-10-24 18:00:07 +0000483#ifdef CONFIG_CMD_TFTPPUT
484 if (TftpWriting) {
485 if (TftpFinalBlock) {
486 tftp_complete();
487 } else {
488 /*
489 * Move to the next block. We want our block
490 * count to wrap just like the other end!
491 */
492 int block = ntohs(*s);
493 int ack_ok = (TftpBlock == block);
494
495 TftpBlock = (unsigned short)(block + 1);
496 update_block_number();
497 if (ack_ok)
498 TftpSend(); /* Send next data block */
499 }
500 }
501#endif
wdenkfe8c2802002-11-03 00:38:21 +0000502 break;
Simon Glass6a398d22011-10-24 18:00:07 +0000503
wdenkfe8c2802002-11-03 00:38:21 +0000504 default:
505 break;
506
Luca Ceresolib640ed32011-05-17 00:03:39 +0000507#ifdef CONFIG_CMD_TFTPSRV
508 case TFTP_WRQ:
509 debug("Got WRQ\n");
510 TftpRemoteIP = sip;
511 TftpRemotePort = src;
512 TftpOurPort = 1024 + (get_timer(0) % 3072);
Simon Glassbd8a0e02011-10-24 18:00:04 +0000513 new_transfer();
Luca Ceresolib640ed32011-05-17 00:03:39 +0000514 TftpSend(); /* Send ACK(0) */
515 break;
516#endif
517
wdenk7182b0f2003-10-06 21:55:32 +0000518 case TFTP_OACK:
Wolfgang Denk12730c72009-08-10 09:59:10 +0200519 debug("Got OACK: %s %s\n",
520 pkt,
521 pkt + strlen((char *)pkt) + 1);
wdenk7182b0f2003-10-06 21:55:32 +0000522 TftpState = STATE_OACK;
Luca Ceresoli5079c762011-05-17 00:03:37 +0000523 TftpRemotePort = src;
Wolfgang Denk8b70f342007-08-31 10:01:51 +0200524 /*
525 * Check for 'blksize' option.
526 * Careful: "i" is signed, "len" is unsigned, thus
527 * something like "len-8" may give a *huge* number
528 */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000529 for (i = 0; i+8 < len; i++) {
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000530 if (strcmp((char *)pkt+i, "blksize") == 0) {
Wolfgang Denk83e8e472007-08-30 14:42:15 +0200531 TftpBlkSize = (unsigned short)
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000532 simple_strtoul((char *)pkt+i+8, NULL,
Luca Ceresolif40538a2011-05-14 05:49:57 +0000533 10);
Robin Getz9e0a4d62009-07-23 03:01:03 -0400534 debug("Blocksize ack: %s, %d\n",
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000535 (char *)pkt+i+8, TftpBlkSize);
Wolfgang Denk83e8e472007-08-30 14:42:15 +0200536 }
Robin Getzb3b00ed2009-08-20 10:50:20 -0400537#ifdef CONFIG_TFTP_TSIZE
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000538 if (strcmp((char *)pkt+i, "tsize") == 0) {
539 TftpTsize = simple_strtoul((char *)pkt+i+6,
Luca Ceresolic42fc272011-05-14 05:49:56 +0000540 NULL, 10);
Robin Getzb3b00ed2009-08-20 10:50:20 -0400541 debug("size = %s, %d\n",
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000542 (char *)pkt+i+6, TftpTsize);
Robin Getzb3b00ed2009-08-20 10:50:20 -0400543 }
544#endif
David Updegraff7280da72007-06-11 10:41:07 -0500545 }
546#ifdef CONFIG_MCAST_TFTP
Luca Ceresolif40538a2011-05-14 05:49:57 +0000547 parse_multicast_oack((char *)pkt, len-1);
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200548 if ((Multicast) && (!MasterClient))
David Updegraff7280da72007-06-11 10:41:07 -0500549 TftpState = STATE_DATA; /* passive.. */
550 else
551#endif
Simon Glass6a398d22011-10-24 18:00:07 +0000552#ifdef CONFIG_CMD_TFTPPUT
553 if (TftpWriting) {
554 /* Get ready to send the first block */
555 TftpState = STATE_DATA;
556 TftpBlock++;
557 }
558#endif
559 TftpSend(); /* Send ACK or first data block */
wdenk7182b0f2003-10-06 21:55:32 +0000560 break;
wdenkfe8c2802002-11-03 00:38:21 +0000561 case TFTP_DATA:
562 if (len < 2)
563 return;
564 len -= 2;
Kim Phillips878dad02013-01-16 18:09:19 -0600565 TftpBlock = ntohs(*(__be16 *)pkt);
wdenkef893942004-02-23 16:11:30 +0000566
Simon Glassbd8a0e02011-10-24 18:00:04 +0000567 update_block_number();
wdenkfe8c2802002-11-03 00:38:21 +0000568
Luca Ceresoli562dfe52011-05-17 00:03:38 +0000569 if (TftpState == STATE_SEND_RRQ)
Robin Getz9e0a4d62009-07-23 03:01:03 -0400570 debug("Server did not acknowledge timeout option!\n");
wdenk7182b0f2003-10-06 21:55:32 +0000571
Luca Ceresolib640ed32011-05-17 00:03:39 +0000572 if (TftpState == STATE_SEND_RRQ || TftpState == STATE_OACK ||
573 TftpState == STATE_RECV_WRQ) {
wdenkef893942004-02-23 16:11:30 +0000574 /* first block received */
wdenkfe8c2802002-11-03 00:38:21 +0000575 TftpState = STATE_DATA;
Luca Ceresoli5079c762011-05-17 00:03:37 +0000576 TftpRemotePort = src;
Simon Glassbd8a0e02011-10-24 18:00:04 +0000577 new_transfer();
wdenkfe8c2802002-11-03 00:38:21 +0000578
David Updegraff7280da72007-06-11 10:41:07 -0500579#ifdef CONFIG_MCAST_TFTP
580 if (Multicast) { /* start!=1 common if mcast */
581 TftpLastBlock = TftpBlock - 1;
582 } else
583#endif
wdenkfe8c2802002-11-03 00:38:21 +0000584 if (TftpBlock != 1) { /* Assertion */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000585 printf("\nTFTP error: "
586 "First block is not block 1 (%ld)\n"
587 "Starting again\n\n",
wdenkfe8c2802002-11-03 00:38:21 +0000588 TftpBlock);
Luca Ceresolif40538a2011-05-14 05:49:57 +0000589 NetStartAgain();
wdenkfe8c2802002-11-03 00:38:21 +0000590 break;
591 }
592 }
593
594 if (TftpBlock == TftpLastBlock) {
595 /*
596 * Same block again; ignore it.
597 */
598 break;
599 }
600
601 TftpLastBlock = TftpBlock;
Bartlomiej Sieka371cde72008-10-01 15:26:29 +0200602 TftpTimeoutCountMax = TIMEOUT_COUNT;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000603 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
wdenkfe8c2802002-11-03 00:38:21 +0000604
Luca Ceresolif40538a2011-05-14 05:49:57 +0000605 store_block(TftpBlock - 1, pkt + 2, len);
wdenkfe8c2802002-11-03 00:38:21 +0000606
607 /*
Luca Ceresoli2f3dc0a2011-05-17 00:03:41 +0000608 * Acknowledge the block just received, which will prompt
Luca Ceresoli5079c762011-05-17 00:03:37 +0000609 * the remote for the next one.
wdenkfe8c2802002-11-03 00:38:21 +0000610 */
David Updegraff7280da72007-06-11 10:41:07 -0500611#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200612 /* if I am the MasterClient, actively calculate what my next
613 * needed block is; else I'm passive; not ACKING
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100614 */
David Updegraff7280da72007-06-11 10:41:07 -0500615 if (Multicast) {
616 if (len < TftpBlkSize) {
617 TftpEndingBlock = TftpBlock;
618 } else if (MasterClient) {
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200619 TftpBlock = PrevBitmapHole =
David Updegraff7280da72007-06-11 10:41:07 -0500620 ext2_find_next_zero_bit(
621 Bitmap,
622 (Mapsize*8),
623 PrevBitmapHole);
624 if (TftpBlock > ((Mapsize*8) - 1)) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000625 printf("tftpfile too big\n");
David Updegraff7280da72007-06-11 10:41:07 -0500626 /* try to double it and retry */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000627 Mapsize <<= 1;
David Updegraff7280da72007-06-11 10:41:07 -0500628 mcast_cleanup();
Luca Ceresolif40538a2011-05-14 05:49:57 +0000629 NetStartAgain();
David Updegraff7280da72007-06-11 10:41:07 -0500630 return;
631 }
632 TftpLastBlock = TftpBlock;
633 }
634 }
635#endif
Luca Ceresolif40538a2011-05-14 05:49:57 +0000636 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000637
David Updegraff7280da72007-06-11 10:41:07 -0500638#ifdef CONFIG_MCAST_TFTP
639 if (Multicast) {
640 if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000641 puts("\nMulticast tftp done\n");
David Updegraff7280da72007-06-11 10:41:07 -0500642 mcast_cleanup();
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000643 net_set_state(NETLOOP_SUCCESS);
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200644 }
Joe Hershberger3a29e5a2012-05-15 08:59:12 +0000645 } else
David Updegraff7280da72007-06-11 10:41:07 -0500646#endif
Simon Glassb437aad2011-10-24 18:00:03 +0000647 if (len < TftpBlkSize)
648 tftp_complete();
wdenkfe8c2802002-11-03 00:38:21 +0000649 break;
650
651 case TFTP_ERROR:
Luca Ceresolif40538a2011-05-14 05:49:57 +0000652 printf("\nTFTP error: '%s' (%d)\n",
Kim Phillips878dad02013-01-16 18:09:19 -0600653 pkt + 2, ntohs(*(__be16 *)pkt));
Remy Bohmerb46293f2009-10-28 22:13:40 +0100654
Kim Phillips878dad02013-01-16 18:09:19 -0600655 switch (ntohs(*(__be16 *)pkt)) {
Remy Bohmerb46293f2009-10-28 22:13:40 +0100656 case TFTP_ERR_FILE_NOT_FOUND:
657 case TFTP_ERR_ACCESS_DENIED:
658 puts("Not retrying...\n");
659 eth_halt();
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000660 net_set_state(NETLOOP_FAIL);
Remy Bohmerb46293f2009-10-28 22:13:40 +0100661 break;
662 case TFTP_ERR_UNDEFINED:
663 case TFTP_ERR_DISK_FULL:
664 case TFTP_ERR_UNEXPECTED_OPCODE:
665 case TFTP_ERR_UNKNOWN_TRANSFER_ID:
666 case TFTP_ERR_FILE_ALREADY_EXISTS:
667 default:
668 puts("Starting again\n\n");
David Updegraff7280da72007-06-11 10:41:07 -0500669#ifdef CONFIG_MCAST_TFTP
Remy Bohmerb46293f2009-10-28 22:13:40 +0100670 mcast_cleanup();
David Updegraff7280da72007-06-11 10:41:07 -0500671#endif
Remy Bohmerb46293f2009-10-28 22:13:40 +0100672 NetStartAgain();
673 break;
674 }
wdenkfe8c2802002-11-03 00:38:21 +0000675 break;
676 }
677}
678
679
680static void
Luca Ceresolif40538a2011-05-14 05:49:57 +0000681TftpTimeout(void)
wdenkfe8c2802002-11-03 00:38:21 +0000682{
Bartlomiej Sieka371cde72008-10-01 15:26:29 +0200683 if (++TftpTimeoutCount > TftpTimeoutCountMax) {
Simon Glassbd8a0e02011-10-24 18:00:04 +0000684 restart("Retry count exceeded");
wdenkfe8c2802002-11-03 00:38:21 +0000685 } else {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000686 puts("T ");
687 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
Luca Ceresolib640ed32011-05-17 00:03:39 +0000688 if (TftpState != STATE_RECV_WRQ)
689 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000690 }
691}
692
693
Simon Glassf59675a2011-10-24 18:00:05 +0000694void TftpStart(enum proto_t protocol)
wdenkfe8c2802002-11-03 00:38:21 +0000695{
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200696 char *ep; /* Environment pointer */
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200697
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100698 /*
699 * Allow the user to choose TFTP blocksize and timeout.
700 * TFTP protocol has a minimal timeout of 1 second.
701 */
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000702 ep = getenv("tftpblocksize");
703 if (ep != NULL)
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200704 TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100705
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000706 ep = getenv("tftptimeout");
707 if (ep != NULL)
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100708 TftpTimeoutMSecs = simple_strtol(ep, NULL, 10);
709
710 if (TftpTimeoutMSecs < 1000) {
711 printf("TFTP timeout (%ld ms) too low, "
712 "set minimum = 1000 ms\n",
713 TftpTimeoutMSecs);
714 TftpTimeoutMSecs = 1000;
715 }
716
717 debug("TFTP blocksize = %i, timeout = %ld ms\n",
718 TftpBlkSizeOption, TftpTimeoutMSecs);
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200719
Luca Ceresoli5079c762011-05-17 00:03:37 +0000720 TftpRemoteIP = NetServerIP;
wdenkfe8c2802002-11-03 00:38:21 +0000721 if (BootFile[0] == '\0') {
Matthias Weisser6cc63262011-12-03 03:29:44 +0000722 sprintf(default_filename, "%02X%02X%02X%02X.img",
Wolfgang Denkdec97322005-08-04 01:09:44 +0200723 NetOurIP & 0xFF,
724 (NetOurIP >> 8) & 0xFF,
725 (NetOurIP >> 16) & 0xFF,
Luca Ceresolif40538a2011-05-14 05:49:57 +0000726 (NetOurIP >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100727
728 strncpy(tftp_filename, default_filename, MAX_LEN);
729 tftp_filename[MAX_LEN-1] = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000730
Luca Ceresolif40538a2011-05-14 05:49:57 +0000731 printf("*** Warning: no boot file name; using '%s'\n",
wdenkfe8c2802002-11-03 00:38:21 +0000732 tftp_filename);
733 } else {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000734 char *p = strchr(BootFile, ':');
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100735
736 if (p == NULL) {
737 strncpy(tftp_filename, BootFile, MAX_LEN);
738 tftp_filename[MAX_LEN-1] = 0;
739 } else {
Luca Ceresoli5079c762011-05-17 00:03:37 +0000740 TftpRemoteIP = string_to_ip(BootFile);
Peter Tyser5e58efa2008-12-01 16:29:38 -0600741 strncpy(tftp_filename, p + 1, MAX_LEN);
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100742 tftp_filename[MAX_LEN-1] = 0;
743 }
wdenkfe8c2802002-11-03 00:38:21 +0000744 }
745
Luca Ceresolif40538a2011-05-14 05:49:57 +0000746 printf("Using %s device\n", eth_get_name());
Simon Glass6a398d22011-10-24 18:00:07 +0000747 printf("TFTP %s server %pI4; our IP address is %pI4",
Simon Glass4abd0e22011-10-27 06:24:29 +0000748#ifdef CONFIG_CMD_TFTPPUT
749 protocol == TFTPPUT ? "to" : "from",
750#else
751 "from",
752#endif
753 &TftpRemoteIP, &NetOurIP);
wdenkfe8c2802002-11-03 00:38:21 +0000754
755 /* Check if we need to send across this subnet */
756 if (NetOurGatewayIP && NetOurSubnetMask) {
Luca Ceresoli09b18232011-05-14 05:50:02 +0000757 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
Luca Ceresoli5079c762011-05-17 00:03:37 +0000758 IPaddr_t RemoteNet = TftpRemoteIP & NetOurSubnetMask;
wdenkfe8c2802002-11-03 00:38:21 +0000759
Luca Ceresoli5079c762011-05-17 00:03:37 +0000760 if (OurNet != RemoteNet)
Luca Ceresoli09b18232011-05-14 05:50:02 +0000761 printf("; sending through gateway %pI4",
762 &NetOurGatewayIP);
wdenkfe8c2802002-11-03 00:38:21 +0000763 }
Luca Ceresolif40538a2011-05-14 05:49:57 +0000764 putc('\n');
wdenkfe8c2802002-11-03 00:38:21 +0000765
Luca Ceresolif40538a2011-05-14 05:49:57 +0000766 printf("Filename '%s'.", tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000767
768 if (NetBootFileSize) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000769 printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
770 print_size(NetBootFileSize<<9, "");
wdenkfe8c2802002-11-03 00:38:21 +0000771 }
772
Luca Ceresolif40538a2011-05-14 05:49:57 +0000773 putc('\n');
Simon Glass6a398d22011-10-24 18:00:07 +0000774#ifdef CONFIG_CMD_TFTPPUT
775 TftpWriting = (protocol == TFTPPUT);
776 if (TftpWriting) {
777 printf("Save address: 0x%lx\n", save_addr);
778 printf("Save size: 0x%lx\n", save_size);
779 NetBootFileXferSize = save_size;
780 puts("Saving: *\b");
781 TftpState = STATE_SEND_WRQ;
782 new_transfer();
783 } else
784#endif
785 {
786 printf("Load address: 0x%lx\n", load_addr);
787 puts("Loading: *\b");
788 TftpState = STATE_SEND_RRQ;
789 }
wdenkfe8c2802002-11-03 00:38:21 +0000790
Simon Glasscab9a8d2012-10-11 13:57:36 +0000791 time_start = get_timer(0);
Bartlomiej Sieka371cde72008-10-01 15:26:29 +0200792 TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
793
Luca Ceresolif40538a2011-05-14 05:49:57 +0000794 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000795 net_set_udp_handler(TftpHandler);
Simon Glass2928cd82011-10-26 14:18:38 +0000796#ifdef CONFIG_CMD_TFTPPUT
Simon Glass6a398d22011-10-24 18:00:07 +0000797 net_set_icmp_handler(icmp_handler);
Simon Glass2928cd82011-10-26 14:18:38 +0000798#endif
Luca Ceresoli5079c762011-05-17 00:03:37 +0000799 TftpRemotePort = WELL_KNOWN_PORT;
wdenkfe8c2802002-11-03 00:38:21 +0000800 TftpTimeoutCount = 0;
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200801 /* Use a pseudo-random port unless a specific port is set */
wdenkfe8c2802002-11-03 00:38:21 +0000802 TftpOurPort = 1024 + (get_timer(0) % 3072);
David Updegraff7280da72007-06-11 10:41:07 -0500803
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200804#ifdef CONFIG_TFTP_PORT
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000805 ep = getenv("tftpdstp");
Luca Ceresoli35dfb182011-05-14 05:50:00 +0000806 if (ep != NULL)
Luca Ceresoli5079c762011-05-17 00:03:37 +0000807 TftpRemotePort = simple_strtol(ep, NULL, 10);
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000808 ep = getenv("tftpsrcp");
Luca Ceresoli35dfb182011-05-14 05:50:00 +0000809 if (ep != NULL)
Luca Ceresolif40538a2011-05-14 05:49:57 +0000810 TftpOurPort = simple_strtol(ep, NULL, 10);
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200811#endif
wdenk7182b0f2003-10-06 21:55:32 +0000812 TftpBlock = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000813
wdenke6466f62003-06-05 19:27:42 +0000814 /* zero out server ether in case the server ip has changed */
815 memset(NetServerEther, 0, 6);
David Updegraff7280da72007-06-11 10:41:07 -0500816 /* Revert TftpBlkSize to dflt */
817 TftpBlkSize = TFTP_BLOCK_SIZE;
818#ifdef CONFIG_MCAST_TFTP
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100819 mcast_cleanup();
David Updegraff7280da72007-06-11 10:41:07 -0500820#endif
Robin Getzb3b00ed2009-08-20 10:50:20 -0400821#ifdef CONFIG_TFTP_TSIZE
822 TftpTsize = 0;
823 TftpNumchars = 0;
824#endif
wdenke6466f62003-06-05 19:27:42 +0000825
Luca Ceresolif40538a2011-05-14 05:49:57 +0000826 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000827}
Luca Ceresolib640ed32011-05-17 00:03:39 +0000828
829#ifdef CONFIG_CMD_TFTPSRV
830void
831TftpStartServer(void)
832{
833 tftp_filename[0] = 0;
834
Luca Ceresolib640ed32011-05-17 00:03:39 +0000835 printf("Using %s device\n", eth_get_name());
Luca Ceresolib640ed32011-05-17 00:03:39 +0000836 printf("Listening for TFTP transfer on %pI4\n", &NetOurIP);
837 printf("Load address: 0x%lx\n", load_addr);
838
839 puts("Loading: *\b");
840
841 TftpTimeoutCountMax = TIMEOUT_COUNT;
842 TftpTimeoutCount = 0;
843 TftpTimeoutMSecs = TIMEOUT;
844 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
845
846 /* Revert TftpBlkSize to dflt */
847 TftpBlkSize = TFTP_BLOCK_SIZE;
848 TftpBlock = 0;
849 TftpOurPort = WELL_KNOWN_PORT;
850
851#ifdef CONFIG_TFTP_TSIZE
852 TftpTsize = 0;
853 TftpNumchars = 0;
854#endif
855
856 TftpState = STATE_RECV_WRQ;
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000857 net_set_udp_handler(TftpHandler);
Andrew Ruder91393c52013-10-22 19:10:28 -0500858
859 /* zero out server ether in case the server ip has changed */
860 memset(NetServerEther, 0, 6);
Luca Ceresolib640ed32011-05-17 00:03:39 +0000861}
862#endif /* CONFIG_CMD_TFTPSRV */
wdenkfe8c2802002-11-03 00:38:21 +0000863
David Updegraff7280da72007-06-11 10:41:07 -0500864#ifdef CONFIG_MCAST_TFTP
865/* Credits: atftp project.
866 */
867
868/* pick up BcastAddr, Port, and whether I am [now] the master-client. *
869 * Frame:
870 * +-------+-----------+---+-------~~-------+---+
871 * | opc | multicast | 0 | addr, port, mc | 0 |
872 * +-------+-----------+---+-------~~-------+---+
873 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
874 * I am the new master-client so must send ACKs to DataBlocks. If I am not
875 * master-client, I'm a passive client, gathering what DataBlocks I may and
876 * making note of which ones I got in my bitmask.
877 * In theory, I never go from master->passive..
878 * .. this comes in with pkt already pointing just past opc
879 */
880static void parse_multicast_oack(char *pkt, int len)
881{
Luca Ceresolif40538a2011-05-14 05:49:57 +0000882 int i;
883 IPaddr_t addr;
884 char *mc_adr, *port, *mc;
David Updegraff7280da72007-06-11 10:41:07 -0500885
Luca Ceresolif40538a2011-05-14 05:49:57 +0000886 mc_adr = port = mc = NULL;
David Updegraff7280da72007-06-11 10:41:07 -0500887 /* march along looking for 'multicast\0', which has to start at least
888 * 14 bytes back from the end.
889 */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000890 for (i = 0; i < len-14; i++)
891 if (strcmp(pkt+i, "multicast") == 0)
David Updegraff7280da72007-06-11 10:41:07 -0500892 break;
893 if (i >= (len-14)) /* non-Multicast OACK, ign. */
894 return;
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200895
Luca Ceresolif40538a2011-05-14 05:49:57 +0000896 i += 10; /* strlen multicast */
David Updegraff7280da72007-06-11 10:41:07 -0500897 mc_adr = pkt+i;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000898 for (; i < len; i++) {
David Updegraff7280da72007-06-11 10:41:07 -0500899 if (*(pkt+i) == ',') {
900 *(pkt+i) = '\0';
901 if (port) {
902 mc = pkt+i+1;
903 break;
904 } else {
905 port = pkt+i+1;
906 }
907 }
908 }
Luca Ceresoli6072fcd2011-05-14 05:50:01 +0000909 if (!port || !mc_adr || !mc)
910 return;
David Updegraff7280da72007-06-11 10:41:07 -0500911 if (Multicast && MasterClient) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000912 printf("I got a OACK as master Client, WRONG!\n");
David Updegraff7280da72007-06-11 10:41:07 -0500913 return;
914 }
915 /* ..I now accept packets destined for this MCAST addr, port */
916 if (!Multicast) {
917 if (Bitmap) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000918 printf("Internal failure! no mcast.\n");
David Updegraff7280da72007-06-11 10:41:07 -0500919 free(Bitmap);
Luca Ceresolif40538a2011-05-14 05:49:57 +0000920 Bitmap = NULL;
921 ProhibitMcast = 1;
David Updegraff7280da72007-06-11 10:41:07 -0500922 return ;
923 }
924 /* I malloc instead of pre-declare; so that if the file ends
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200925 * up being too big for this bitmap I can retry
926 */
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000927 Bitmap = malloc(Mapsize);
928 if (!Bitmap) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000929 printf("No Bitmap, no multicast. Sorry.\n");
930 ProhibitMcast = 1;
David Updegraff7280da72007-06-11 10:41:07 -0500931 return;
932 }
Luca Ceresolif40538a2011-05-14 05:49:57 +0000933 memset(Bitmap, 0, Mapsize);
David Updegraff7280da72007-06-11 10:41:07 -0500934 PrevBitmapHole = 0;
935 Multicast = 1;
936 }
937 addr = string_to_ip(mc_adr);
938 if (Mcast_addr != addr) {
939 if (Mcast_addr)
940 eth_mcast_join(Mcast_addr, 0);
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000941 Mcast_addr = addr;
942 if (eth_mcast_join(Mcast_addr, 1)) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000943 printf("Fail to set mcast, revert to TFTP\n");
944 ProhibitMcast = 1;
David Updegraff7280da72007-06-11 10:41:07 -0500945 mcast_cleanup();
946 NetStartAgain();
947 }
948 }
Luca Ceresolif40538a2011-05-14 05:49:57 +0000949 MasterClient = (unsigned char)simple_strtoul((char *)mc, NULL, 10);
950 Mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
951 printf("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
David Updegraff7280da72007-06-11 10:41:07 -0500952 return;
953}
954
955#endif /* Multicast TFTP */