blob: b2e08b4bfb3d3daba64328c24e84ecbe9afc6970 [file] [log] [blame]
wdenkfe8c2802002-11-03 00:38:21 +00001/*
Luca Ceresoli2f094132011-05-14 05:49:56 +00002 * Copyright 1994, 1995, 2000 Neil Russell.
3 * (See License)
4 * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
Luca Ceresolie59e3562011-05-17 00:03:39 +00005 * Copyright 2011 Comelit Group SpA,
6 * Luca Ceresoli <luca.ceresoli@comelit.it>
wdenkfe8c2802002-11-03 00:38:21 +00007 */
8
9#include <common.h>
10#include <command.h>
11#include <net.h>
12#include "tftp.h"
13#include "bootp.h"
Joe Hershberger13dfe942012-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 Ceresoli2f094132011-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 Ceresoli2f094132011-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 Ceresoli2f094132011-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
wdenkfbe4b5c2003-10-06 21:55:32 +000039#define TFTP_OACK 6
wdenkfe8c2802002-11-03 00:38:21 +000040
Bartlomiej Siekae83cc062008-10-01 15:26:29 +020041static ulong TftpTimeoutMSecs = TIMEOUT;
42static int TftpTimeoutCountMax = TIMEOUT_COUNT;
43
44/*
45 * These globals govern the timeout behavior when attempting a connection to a
46 * TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to
47 * wait for the server to respond to initial connection. Second global,
48 * TftpRRQTimeoutCountMax, gives the number of such connection retries.
49 * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be
50 * positive. The globals are meant to be set (and restored) by code needing
51 * non-standard timeout behavior when initiating a TFTP transfer.
52 */
53ulong TftpRRQTimeoutMSecs = TIMEOUT;
54int TftpRRQTimeoutCountMax = TIMEOUT_COUNT;
55
Remy Bohmeraafda382009-10-28 22:13:40 +010056enum {
57 TFTP_ERR_UNDEFINED = 0,
58 TFTP_ERR_FILE_NOT_FOUND = 1,
59 TFTP_ERR_ACCESS_DENIED = 2,
60 TFTP_ERR_DISK_FULL = 3,
61 TFTP_ERR_UNEXPECTED_OPCODE = 4,
62 TFTP_ERR_UNKNOWN_TRANSFER_ID = 5,
63 TFTP_ERR_FILE_ALREADY_EXISTS = 6,
64};
65
Luca Ceresoli20478ce2011-05-17 00:03:37 +000066static IPaddr_t TftpRemoteIP;
Luca Ceresoli2f094132011-05-14 05:49:56 +000067/* The UDP port at their end */
Luca Ceresoli20478ce2011-05-17 00:03:37 +000068static int TftpRemotePort;
Luca Ceresoli2f094132011-05-14 05:49:56 +000069/* The UDP port at our end */
70static int TftpOurPort;
wdenkfe8c2802002-11-03 00:38:21 +000071static int TftpTimeoutCount;
Luca Ceresoli2f094132011-05-14 05:49:56 +000072/* packet sequence number */
73static ulong TftpBlock;
74/* last packet sequence number received */
75static ulong TftpLastBlock;
76/* count of sequence number wraparounds */
77static ulong TftpBlockWrap;
78/* memory offset due to wrapping */
79static ulong TftpBlockWrapOffset;
wdenkfe8c2802002-11-03 00:38:21 +000080static int TftpState;
Robin Getz4fccb812009-08-20 10:50:20 -040081#ifdef CONFIG_TFTP_TSIZE
Luca Ceresoli2f094132011-05-14 05:49:56 +000082/* The file size reported by the server */
83static int TftpTsize;
84/* The number of hashes we printed */
85static short TftpNumchars;
Robin Getz4fccb812009-08-20 10:50:20 -040086#endif
Simon Glass1fb7cd42011-10-24 18:00:07 +000087#ifdef CONFIG_CMD_TFTPPUT
88static int TftpWriting; /* 1 if writing, else 0 */
89static int TftpFinalBlock; /* 1 if we have sent the last block */
90#else
91#define TftpWriting 0
92#endif
wdenk3f85ce22004-02-23 16:11:30 +000093
Luca Ceresolie3fb0ab2011-05-17 00:03:38 +000094#define STATE_SEND_RRQ 1
wdenkfe8c2802002-11-03 00:38:21 +000095#define STATE_DATA 2
96#define STATE_TOO_LARGE 3
97#define STATE_BAD_MAGIC 4
wdenkfbe4b5c2003-10-06 21:55:32 +000098#define STATE_OACK 5
Luca Ceresolie59e3562011-05-17 00:03:39 +000099#define STATE_RECV_WRQ 6
Simon Glass1fb7cd42011-10-24 18:00:07 +0000100#define STATE_SEND_WRQ 7
wdenkfe8c2802002-11-03 00:38:21 +0000101
Luca Ceresoli2f094132011-05-14 05:49:56 +0000102/* default TFTP block size */
103#define TFTP_BLOCK_SIZE 512
104/* sequence number is 16 bit */
105#define TFTP_SEQUENCE_SIZE ((ulong)(1<<16))
wdenk3f85ce22004-02-23 16:11:30 +0000106
wdenkfe8c2802002-11-03 00:38:21 +0000107#define DEFAULT_NAME_LEN (8 + 4 + 1)
108static char default_filename[DEFAULT_NAME_LEN];
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100109
110#ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
111#define MAX_LEN 128
112#else
113#define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
114#endif
115
116static char tftp_filename[MAX_LEN];
wdenkfe8c2802002-11-03 00:38:21 +0000117
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200118/* 512 is poor choice for ethernet, MTU is typically 1500.
119 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
David Updegraff53a5c422007-06-11 10:41:07 -0500120 * almost-MTU block sizes. At least try... fall back to 512 if need be.
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200121 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
David Updegraff53a5c422007-06-11 10:41:07 -0500122 */
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200123#ifdef CONFIG_TFTP_BLOCKSIZE
124#define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
125#else
David Updegraff53a5c422007-06-11 10:41:07 -0500126#define TFTP_MTU_BLOCKSIZE 1468
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200127#endif
128
Luca Ceresolic718b142011-05-14 05:49:57 +0000129static unsigned short TftpBlkSize = TFTP_BLOCK_SIZE;
130static unsigned short TftpBlkSizeOption = TFTP_MTU_BLOCKSIZE;
David Updegraff53a5c422007-06-11 10:41:07 -0500131
132#ifdef CONFIG_MCAST_TFTP
133#include <malloc.h>
134#define MTFTP_BITMAPSIZE 0x1000
135static unsigned *Bitmap;
Luca Ceresolic718b142011-05-14 05:49:57 +0000136static int PrevBitmapHole, Mapsize = MTFTP_BITMAPSIZE;
Luca Ceresoli9bb0a1b2011-05-14 05:50:03 +0000137static uchar ProhibitMcast, MasterClient;
138static uchar Multicast;
David Updegraff53a5c422007-06-11 10:41:07 -0500139static int Mcast_port;
140static ulong TftpEndingBlock; /* can get 'last' block before done..*/
141
Luca Ceresolic718b142011-05-14 05:49:57 +0000142static void parse_multicast_oack(char *pkt, int len);
David Updegraff53a5c422007-06-11 10:41:07 -0500143
144static void
145mcast_cleanup(void)
146{
Luca Ceresoli6d2231e2011-05-14 05:50:01 +0000147 if (Mcast_addr)
148 eth_mcast_join(Mcast_addr, 0);
149 if (Bitmap)
150 free(Bitmap);
Luca Ceresolic718b142011-05-14 05:49:57 +0000151 Bitmap = NULL;
David Updegraff53a5c422007-06-11 10:41:07 -0500152 Mcast_addr = Multicast = Mcast_port = 0;
153 TftpEndingBlock = -1;
154}
155
156#endif /* CONFIG_MCAST_TFTP */
157
Joe Hershberger13dfe942012-05-15 08:59:12 +0000158static inline void
Luca Ceresoli2e320252011-05-14 05:49:58 +0000159store_block(unsigned block, uchar *src, unsigned len)
wdenkfe8c2802002-11-03 00:38:21 +0000160{
David Updegraff53a5c422007-06-11 10:41:07 -0500161 ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
wdenk3f85ce22004-02-23 16:11:30 +0000162 ulong newsize = offset + len;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200163#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
wdenkfe8c2802002-11-03 00:38:21 +0000164 int i, rc = 0;
165
Luca Ceresolic718b142011-05-14 05:49:57 +0000166 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenkfe8c2802002-11-03 00:38:21 +0000167 /* start address in flash? */
Jochen Friedrichbe1b0d22008-09-02 11:24:59 +0200168 if (flash_info[i].flash_id == FLASH_UNKNOWN)
169 continue;
wdenkfe8c2802002-11-03 00:38:21 +0000170 if (load_addr + offset >= flash_info[i].start[0]) {
171 rc = 1;
172 break;
173 }
174 }
175
176 if (rc) { /* Flash is destination for this packet */
Luca Ceresolic718b142011-05-14 05:49:57 +0000177 rc = flash_write((char *)src, (ulong)(load_addr+offset), len);
wdenkfe8c2802002-11-03 00:38:21 +0000178 if (rc) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000179 flash_perror(rc);
Joe Hershberger22f6e992012-05-23 07:59:14 +0000180 net_set_state(NETLOOP_FAIL);
wdenkfe8c2802002-11-03 00:38:21 +0000181 return;
182 }
Joe Hershberger13dfe942012-05-15 08:59:12 +0000183 } else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200184#endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000185 {
186 (void)memcpy((void *)(load_addr + offset), src, len);
187 }
David Updegraff53a5c422007-06-11 10:41:07 -0500188#ifdef CONFIG_MCAST_TFTP
189 if (Multicast)
190 ext2_set_bit(block, Bitmap);
191#endif
wdenkfe8c2802002-11-03 00:38:21 +0000192
193 if (NetBootFileXferSize < newsize)
194 NetBootFileXferSize = newsize;
195}
196
Simon Glasse4cde2f2011-10-24 18:00:04 +0000197/* Clear our state ready for a new transfer */
Simon Glass165099e2011-10-27 06:24:28 +0000198static void new_transfer(void)
Simon Glasse4cde2f2011-10-24 18:00:04 +0000199{
200 TftpLastBlock = 0;
201 TftpBlockWrap = 0;
202 TftpBlockWrapOffset = 0;
203#ifdef CONFIG_CMD_TFTPPUT
204 TftpFinalBlock = 0;
205#endif
206}
207
Simon Glass1fb7cd42011-10-24 18:00:07 +0000208#ifdef CONFIG_CMD_TFTPPUT
209/**
210 * Load the next block from memory to be sent over tftp.
211 *
212 * @param block Block number to send
213 * @param dst Destination buffer for data
214 * @param len Number of bytes in block (this one and every other)
215 * @return number of bytes loaded
216 */
217static int load_block(unsigned block, uchar *dst, unsigned len)
218{
219 /* We may want to get the final block from the previous set */
220 ulong offset = ((int)block - 1) * len + TftpBlockWrapOffset;
221 ulong tosend = len;
222
223 tosend = min(NetBootFileXferSize - offset, tosend);
224 (void)memcpy(dst, (void *)(save_addr + offset), tosend);
225 debug("%s: block=%d, offset=%ld, len=%d, tosend=%ld\n", __func__,
226 block, offset, len, tosend);
227 return tosend;
228}
229#endif
230
Luca Ceresolic718b142011-05-14 05:49:57 +0000231static void TftpSend(void);
232static void TftpTimeout(void);
wdenkfe8c2802002-11-03 00:38:21 +0000233
234/**********************************************************************/
235
Simon Glassf5329bb2011-10-24 18:00:03 +0000236static void show_block_marker(void)
237{
238#ifdef CONFIG_TFTP_TSIZE
239 if (TftpTsize) {
240 ulong pos = TftpBlock * TftpBlkSize + TftpBlockWrapOffset;
241
242 while (TftpNumchars < pos * 50 / TftpTsize) {
243 putc('#');
244 TftpNumchars++;
245 }
Simon Glass1fb7cd42011-10-24 18:00:07 +0000246 } else
Simon Glassf5329bb2011-10-24 18:00:03 +0000247#endif
Simon Glass1fb7cd42011-10-24 18:00:07 +0000248 {
Simon Glassf5329bb2011-10-24 18:00:03 +0000249 if (((TftpBlock - 1) % 10) == 0)
250 putc('#');
251 else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0)
252 puts("\n\t ");
253 }
254}
255
Simon Glasse4cde2f2011-10-24 18:00:04 +0000256/**
257 * restart the current transfer due to an error
258 *
259 * @param msg Message to print for user
260 */
261static void restart(const char *msg)
262{
263 printf("\n%s; starting again\n", msg);
264#ifdef CONFIG_MCAST_TFTP
265 mcast_cleanup();
266#endif
267 NetStartAgain();
268}
269
270/*
271 * Check if the block number has wrapped, and update progress
272 *
273 * TODO: The egregious use of global variables in this file should be tidied.
274 */
275static void update_block_number(void)
276{
277 /*
278 * RFC1350 specifies that the first data packet will
279 * have sequence number 1. If we receive a sequence
280 * number of 0 this means that there was a wrap
281 * around of the (16 bit) counter.
282 */
283 if (TftpBlock == 0) {
284 TftpBlockWrap++;
285 TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE;
286 TftpTimeoutCount = 0; /* we've done well, reset thhe timeout */
287 } else {
288 show_block_marker();
289 }
290}
291
Simon Glassf5329bb2011-10-24 18:00:03 +0000292/* The TFTP get or put is complete */
293static void tftp_complete(void)
294{
295#ifdef CONFIG_TFTP_TSIZE
296 /* Print hash marks for the last packet received */
297 while (TftpTsize && TftpNumchars < 49) {
298 putc('#');
299 TftpNumchars++;
300 }
301#endif
302 puts("\ndone\n");
Joe Hershberger22f6e992012-05-23 07:59:14 +0000303 net_set_state(NETLOOP_SUCCESS);
Simon Glassf5329bb2011-10-24 18:00:03 +0000304}
305
wdenkfe8c2802002-11-03 00:38:21 +0000306static void
Luca Ceresolic718b142011-05-14 05:49:57 +0000307TftpSend(void)
wdenkfe8c2802002-11-03 00:38:21 +0000308{
Simon Glass1fb7cd42011-10-24 18:00:07 +0000309 uchar *pkt;
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000310 uchar *xp;
311 int len = 0;
312 ushort *s;
wdenkfe8c2802002-11-03 00:38:21 +0000313
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200314#ifdef CONFIG_MCAST_TFTP
David Updegraff53a5c422007-06-11 10:41:07 -0500315 /* Multicast TFTP.. non-MasterClients do not ACK data. */
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200316 if (Multicast
317 && (TftpState == STATE_DATA)
318 && (MasterClient == 0))
David Updegraff53a5c422007-06-11 10:41:07 -0500319 return;
320#endif
wdenkfe8c2802002-11-03 00:38:21 +0000321 /*
322 * We will always be sending some sort of packet, so
323 * cobble together the packet headers now.
324 */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000325 pkt = NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
wdenkfe8c2802002-11-03 00:38:21 +0000326
327 switch (TftpState) {
Luca Ceresolie3fb0ab2011-05-17 00:03:38 +0000328 case STATE_SEND_RRQ:
Simon Glass1fb7cd42011-10-24 18:00:07 +0000329 case STATE_SEND_WRQ:
wdenkfe8c2802002-11-03 00:38:21 +0000330 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200331 s = (ushort *)pkt;
Simon Glass8c6914f2011-10-27 06:24:29 +0000332#ifdef CONFIG_CMD_TFTPPUT
Simon Glass1fb7cd42011-10-24 18:00:07 +0000333 *s++ = htons(TftpState == STATE_SEND_RRQ ? TFTP_RRQ :
334 TFTP_WRQ);
Simon Glass8c6914f2011-10-27 06:24:29 +0000335#else
336 *s++ = htons(TFTP_RRQ);
337#endif
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200338 pkt = (uchar *)s;
Luca Ceresolic718b142011-05-14 05:49:57 +0000339 strcpy((char *)pkt, tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000340 pkt += strlen(tftp_filename) + 1;
Luca Ceresolic718b142011-05-14 05:49:57 +0000341 strcpy((char *)pkt, "octet");
wdenkfe8c2802002-11-03 00:38:21 +0000342 pkt += 5 /*strlen("octet")*/ + 1;
Luca Ceresolic718b142011-05-14 05:49:57 +0000343 strcpy((char *)pkt, "timeout");
wdenkfbe4b5c2003-10-06 21:55:32 +0000344 pkt += 7 /*strlen("timeout")*/ + 1;
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100345 sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000);
Robin Getz0ebf04c2009-07-23 03:01:03 -0400346 debug("send option \"timeout %s\"\n", (char *)pkt);
wdenkfbe4b5c2003-10-06 21:55:32 +0000347 pkt += strlen((char *)pkt) + 1;
Robin Getz4fccb812009-08-20 10:50:20 -0400348#ifdef CONFIG_TFTP_TSIZE
Simon Glass1fb7cd42011-10-24 18:00:07 +0000349 pkt += sprintf((char *)pkt, "tsize%c%lu%c",
350 0, NetBootFileXferSize, 0);
Robin Getz4fccb812009-08-20 10:50:20 -0400351#endif
David Updegraff53a5c422007-06-11 10:41:07 -0500352 /* try for more effic. blk size */
Luca Ceresolic718b142011-05-14 05:49:57 +0000353 pkt += sprintf((char *)pkt, "blksize%c%d%c",
354 0, TftpBlkSizeOption, 0);
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200355#ifdef CONFIG_MCAST_TFTP
David Updegraff53a5c422007-06-11 10:41:07 -0500356 /* Check all preconditions before even trying the option */
Joe Hershberger13dfe942012-05-15 08:59:12 +0000357 if (!ProhibitMcast) {
358 Bitmap = malloc(Mapsize);
359 if (Bitmap && eth_get_dev()->mcast) {
360 free(Bitmap);
361 Bitmap = NULL;
362 pkt += sprintf((char *)pkt, "multicast%c%c",
363 0, 0);
364 }
David Updegraff53a5c422007-06-11 10:41:07 -0500365 }
366#endif /* CONFIG_MCAST_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000367 len = pkt - xp;
368 break;
369
wdenkfbe4b5c2003-10-06 21:55:32 +0000370 case STATE_OACK:
David Updegraff53a5c422007-06-11 10:41:07 -0500371#ifdef CONFIG_MCAST_TFTP
372 /* My turn! Start at where I need blocks I missed.*/
373 if (Multicast)
Luca Ceresolic718b142011-05-14 05:49:57 +0000374 TftpBlock = ext2_find_next_zero_bit(Bitmap,
375 (Mapsize*8), 0);
David Updegraff53a5c422007-06-11 10:41:07 -0500376 /*..falling..*/
377#endif
Luca Ceresolie59e3562011-05-17 00:03:39 +0000378
379 case STATE_RECV_WRQ:
David Updegraff53a5c422007-06-11 10:41:07 -0500380 case STATE_DATA:
wdenkfe8c2802002-11-03 00:38:21 +0000381 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200382 s = (ushort *)pkt;
Simon Glass1fb7cd42011-10-24 18:00:07 +0000383 s[0] = htons(TFTP_ACK);
384 s[1] = htons(TftpBlock);
385 pkt = (uchar *)(s + 2);
386#ifdef CONFIG_CMD_TFTPPUT
387 if (TftpWriting) {
388 int toload = TftpBlkSize;
389 int loaded = load_block(TftpBlock, pkt, toload);
390
391 s[0] = htons(TFTP_DATA);
392 pkt += loaded;
393 TftpFinalBlock = (loaded < toload);
394 }
395#endif
wdenkfe8c2802002-11-03 00:38:21 +0000396 len = pkt - xp;
397 break;
398
399 case STATE_TOO_LARGE:
400 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200401 s = (ushort *)pkt;
402 *s++ = htons(TFTP_ERROR);
Simon Glass1fb7cd42011-10-24 18:00:07 +0000403 *s++ = htons(3);
404
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200405 pkt = (uchar *)s;
Luca Ceresolic718b142011-05-14 05:49:57 +0000406 strcpy((char *)pkt, "File too large");
wdenkfe8c2802002-11-03 00:38:21 +0000407 pkt += 14 /*strlen("File too large")*/ + 1;
408 len = pkt - xp;
409 break;
410
411 case STATE_BAD_MAGIC:
412 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200413 s = (ushort *)pkt;
414 *s++ = htons(TFTP_ERROR);
415 *s++ = htons(2);
416 pkt = (uchar *)s;
Luca Ceresolic718b142011-05-14 05:49:57 +0000417 strcpy((char *)pkt, "File has bad magic");
wdenkfe8c2802002-11-03 00:38:21 +0000418 pkt += 18 /*strlen("File has bad magic")*/ + 1;
419 len = pkt - xp;
420 break;
421 }
422
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000423 NetSendUDPPacket(NetServerEther, TftpRemoteIP, TftpRemotePort,
Luca Ceresoli2f094132011-05-14 05:49:56 +0000424 TftpOurPort, len);
wdenkfe8c2802002-11-03 00:38:21 +0000425}
426
Simon Glass39bccd22011-10-26 14:18:38 +0000427#ifdef CONFIG_CMD_TFTPPUT
Simon Glass1fb7cd42011-10-24 18:00:07 +0000428static void icmp_handler(unsigned type, unsigned code, unsigned dest,
429 IPaddr_t sip, unsigned src, uchar *pkt, unsigned len)
430{
431 if (type == ICMP_NOT_REACH && code == ICMP_NOT_REACH_PORT) {
432 /* Oh dear the other end has gone away */
433 restart("TFTP server died");
434 }
435}
Simon Glass39bccd22011-10-26 14:18:38 +0000436#endif
Simon Glass1fb7cd42011-10-24 18:00:07 +0000437
wdenkfe8c2802002-11-03 00:38:21 +0000438static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000439TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
440 unsigned len)
wdenkfe8c2802002-11-03 00:38:21 +0000441{
442 ushort proto;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200443 ushort *s;
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200444 int i;
wdenkfe8c2802002-11-03 00:38:21 +0000445
446 if (dest != TftpOurPort) {
David Updegraff53a5c422007-06-11 10:41:07 -0500447#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200448 if (Multicast
David Updegraff53a5c422007-06-11 10:41:07 -0500449 && (!Mcast_port || (dest != Mcast_port)))
450#endif
Luca Ceresoli0bdd8ac2011-05-14 05:50:02 +0000451 return;
wdenkfe8c2802002-11-03 00:38:21 +0000452 }
Luca Ceresolie59e3562011-05-17 00:03:39 +0000453 if (TftpState != STATE_SEND_RRQ && src != TftpRemotePort &&
Simon Glass1fb7cd42011-10-24 18:00:07 +0000454 TftpState != STATE_RECV_WRQ && TftpState != STATE_SEND_WRQ)
wdenkfe8c2802002-11-03 00:38:21 +0000455 return;
wdenkfe8c2802002-11-03 00:38:21 +0000456
Luca Ceresoli7bc325a2011-05-14 05:50:00 +0000457 if (len < 2)
wdenkfe8c2802002-11-03 00:38:21 +0000458 return;
wdenkfe8c2802002-11-03 00:38:21 +0000459 len -= 2;
460 /* warning: don't use increment (++) in ntohs() macros!! */
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200461 s = (ushort *)pkt;
462 proto = *s++;
463 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000464 switch (ntohs(proto)) {
465
466 case TFTP_RRQ:
wdenkfe8c2802002-11-03 00:38:21 +0000467 break;
Simon Glass1fb7cd42011-10-24 18:00:07 +0000468
469 case TFTP_ACK:
470#ifdef CONFIG_CMD_TFTPPUT
471 if (TftpWriting) {
472 if (TftpFinalBlock) {
473 tftp_complete();
474 } else {
475 /*
476 * Move to the next block. We want our block
477 * count to wrap just like the other end!
478 */
479 int block = ntohs(*s);
480 int ack_ok = (TftpBlock == block);
481
482 TftpBlock = (unsigned short)(block + 1);
483 update_block_number();
484 if (ack_ok)
485 TftpSend(); /* Send next data block */
486 }
487 }
488#endif
489 break;
490
wdenkfe8c2802002-11-03 00:38:21 +0000491 default:
492 break;
493
Luca Ceresolie59e3562011-05-17 00:03:39 +0000494#ifdef CONFIG_CMD_TFTPSRV
495 case TFTP_WRQ:
496 debug("Got WRQ\n");
497 TftpRemoteIP = sip;
498 TftpRemotePort = src;
499 TftpOurPort = 1024 + (get_timer(0) % 3072);
Simon Glasse4cde2f2011-10-24 18:00:04 +0000500 new_transfer();
Luca Ceresolie59e3562011-05-17 00:03:39 +0000501 TftpSend(); /* Send ACK(0) */
502 break;
503#endif
504
wdenkfbe4b5c2003-10-06 21:55:32 +0000505 case TFTP_OACK:
Wolfgang Denkd3717082009-08-10 09:59:10 +0200506 debug("Got OACK: %s %s\n",
507 pkt,
508 pkt + strlen((char *)pkt) + 1);
wdenkfbe4b5c2003-10-06 21:55:32 +0000509 TftpState = STATE_OACK;
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000510 TftpRemotePort = src;
Wolfgang Denk60174742007-08-31 10:01:51 +0200511 /*
512 * Check for 'blksize' option.
513 * Careful: "i" is signed, "len" is unsigned, thus
514 * something like "len-8" may give a *huge* number
515 */
Luca Ceresolic718b142011-05-14 05:49:57 +0000516 for (i = 0; i+8 < len; i++) {
Luca Ceresoli2e320252011-05-14 05:49:58 +0000517 if (strcmp((char *)pkt+i, "blksize") == 0) {
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200518 TftpBlkSize = (unsigned short)
Luca Ceresoli2e320252011-05-14 05:49:58 +0000519 simple_strtoul((char *)pkt+i+8, NULL,
Luca Ceresolic718b142011-05-14 05:49:57 +0000520 10);
Robin Getz0ebf04c2009-07-23 03:01:03 -0400521 debug("Blocksize ack: %s, %d\n",
Luca Ceresoli2e320252011-05-14 05:49:58 +0000522 (char *)pkt+i+8, TftpBlkSize);
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200523 }
Robin Getz4fccb812009-08-20 10:50:20 -0400524#ifdef CONFIG_TFTP_TSIZE
Luca Ceresoli2e320252011-05-14 05:49:58 +0000525 if (strcmp((char *)pkt+i, "tsize") == 0) {
526 TftpTsize = simple_strtoul((char *)pkt+i+6,
Luca Ceresoli2f094132011-05-14 05:49:56 +0000527 NULL, 10);
Robin Getz4fccb812009-08-20 10:50:20 -0400528 debug("size = %s, %d\n",
Luca Ceresoli2e320252011-05-14 05:49:58 +0000529 (char *)pkt+i+6, TftpTsize);
Robin Getz4fccb812009-08-20 10:50:20 -0400530 }
531#endif
David Updegraff53a5c422007-06-11 10:41:07 -0500532 }
533#ifdef CONFIG_MCAST_TFTP
Luca Ceresolic718b142011-05-14 05:49:57 +0000534 parse_multicast_oack((char *)pkt, len-1);
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200535 if ((Multicast) && (!MasterClient))
David Updegraff53a5c422007-06-11 10:41:07 -0500536 TftpState = STATE_DATA; /* passive.. */
537 else
538#endif
Simon Glass1fb7cd42011-10-24 18:00:07 +0000539#ifdef CONFIG_CMD_TFTPPUT
540 if (TftpWriting) {
541 /* Get ready to send the first block */
542 TftpState = STATE_DATA;
543 TftpBlock++;
544 }
545#endif
546 TftpSend(); /* Send ACK or first data block */
wdenkfbe4b5c2003-10-06 21:55:32 +0000547 break;
wdenkfe8c2802002-11-03 00:38:21 +0000548 case TFTP_DATA:
549 if (len < 2)
550 return;
551 len -= 2;
552 TftpBlock = ntohs(*(ushort *)pkt);
wdenk3f85ce22004-02-23 16:11:30 +0000553
Simon Glasse4cde2f2011-10-24 18:00:04 +0000554 update_block_number();
wdenkfe8c2802002-11-03 00:38:21 +0000555
Luca Ceresolie3fb0ab2011-05-17 00:03:38 +0000556 if (TftpState == STATE_SEND_RRQ)
Robin Getz0ebf04c2009-07-23 03:01:03 -0400557 debug("Server did not acknowledge timeout option!\n");
wdenkfbe4b5c2003-10-06 21:55:32 +0000558
Luca Ceresolie59e3562011-05-17 00:03:39 +0000559 if (TftpState == STATE_SEND_RRQ || TftpState == STATE_OACK ||
560 TftpState == STATE_RECV_WRQ) {
wdenk3f85ce22004-02-23 16:11:30 +0000561 /* first block received */
wdenkfe8c2802002-11-03 00:38:21 +0000562 TftpState = STATE_DATA;
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000563 TftpRemotePort = src;
Simon Glasse4cde2f2011-10-24 18:00:04 +0000564 new_transfer();
wdenkfe8c2802002-11-03 00:38:21 +0000565
David Updegraff53a5c422007-06-11 10:41:07 -0500566#ifdef CONFIG_MCAST_TFTP
567 if (Multicast) { /* start!=1 common if mcast */
568 TftpLastBlock = TftpBlock - 1;
569 } else
570#endif
wdenkfe8c2802002-11-03 00:38:21 +0000571 if (TftpBlock != 1) { /* Assertion */
Luca Ceresolic718b142011-05-14 05:49:57 +0000572 printf("\nTFTP error: "
573 "First block is not block 1 (%ld)\n"
574 "Starting again\n\n",
wdenkfe8c2802002-11-03 00:38:21 +0000575 TftpBlock);
Luca Ceresolic718b142011-05-14 05:49:57 +0000576 NetStartAgain();
wdenkfe8c2802002-11-03 00:38:21 +0000577 break;
578 }
579 }
580
581 if (TftpBlock == TftpLastBlock) {
582 /*
583 * Same block again; ignore it.
584 */
585 break;
586 }
587
588 TftpLastBlock = TftpBlock;
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200589 TftpTimeoutCountMax = TIMEOUT_COUNT;
Luca Ceresolic718b142011-05-14 05:49:57 +0000590 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
wdenkfe8c2802002-11-03 00:38:21 +0000591
Luca Ceresolic718b142011-05-14 05:49:57 +0000592 store_block(TftpBlock - 1, pkt + 2, len);
wdenkfe8c2802002-11-03 00:38:21 +0000593
594 /*
Luca Ceresoli4d69e982011-05-17 00:03:41 +0000595 * Acknowledge the block just received, which will prompt
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000596 * the remote for the next one.
wdenkfe8c2802002-11-03 00:38:21 +0000597 */
David Updegraff53a5c422007-06-11 10:41:07 -0500598#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200599 /* if I am the MasterClient, actively calculate what my next
600 * needed block is; else I'm passive; not ACKING
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100601 */
David Updegraff53a5c422007-06-11 10:41:07 -0500602 if (Multicast) {
603 if (len < TftpBlkSize) {
604 TftpEndingBlock = TftpBlock;
605 } else if (MasterClient) {
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200606 TftpBlock = PrevBitmapHole =
David Updegraff53a5c422007-06-11 10:41:07 -0500607 ext2_find_next_zero_bit(
608 Bitmap,
609 (Mapsize*8),
610 PrevBitmapHole);
611 if (TftpBlock > ((Mapsize*8) - 1)) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000612 printf("tftpfile too big\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500613 /* try to double it and retry */
Luca Ceresolic718b142011-05-14 05:49:57 +0000614 Mapsize <<= 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500615 mcast_cleanup();
Luca Ceresolic718b142011-05-14 05:49:57 +0000616 NetStartAgain();
David Updegraff53a5c422007-06-11 10:41:07 -0500617 return;
618 }
619 TftpLastBlock = TftpBlock;
620 }
621 }
622#endif
Luca Ceresolic718b142011-05-14 05:49:57 +0000623 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000624
David Updegraff53a5c422007-06-11 10:41:07 -0500625#ifdef CONFIG_MCAST_TFTP
626 if (Multicast) {
627 if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000628 puts("\nMulticast tftp done\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500629 mcast_cleanup();
Joe Hershberger22f6e992012-05-23 07:59:14 +0000630 net_set_state(NETLOOP_SUCCESS);
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200631 }
Joe Hershberger13dfe942012-05-15 08:59:12 +0000632 } else
David Updegraff53a5c422007-06-11 10:41:07 -0500633#endif
Simon Glassf5329bb2011-10-24 18:00:03 +0000634 if (len < TftpBlkSize)
635 tftp_complete();
wdenkfe8c2802002-11-03 00:38:21 +0000636 break;
637
638 case TFTP_ERROR:
Luca Ceresolic718b142011-05-14 05:49:57 +0000639 printf("\nTFTP error: '%s' (%d)\n",
640 pkt + 2, ntohs(*(ushort *)pkt));
Remy Bohmeraafda382009-10-28 22:13:40 +0100641
642 switch (ntohs(*(ushort *)pkt)) {
643 case TFTP_ERR_FILE_NOT_FOUND:
644 case TFTP_ERR_ACCESS_DENIED:
645 puts("Not retrying...\n");
646 eth_halt();
Joe Hershberger22f6e992012-05-23 07:59:14 +0000647 net_set_state(NETLOOP_FAIL);
Remy Bohmeraafda382009-10-28 22:13:40 +0100648 break;
649 case TFTP_ERR_UNDEFINED:
650 case TFTP_ERR_DISK_FULL:
651 case TFTP_ERR_UNEXPECTED_OPCODE:
652 case TFTP_ERR_UNKNOWN_TRANSFER_ID:
653 case TFTP_ERR_FILE_ALREADY_EXISTS:
654 default:
655 puts("Starting again\n\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500656#ifdef CONFIG_MCAST_TFTP
Remy Bohmeraafda382009-10-28 22:13:40 +0100657 mcast_cleanup();
David Updegraff53a5c422007-06-11 10:41:07 -0500658#endif
Remy Bohmeraafda382009-10-28 22:13:40 +0100659 NetStartAgain();
660 break;
661 }
wdenkfe8c2802002-11-03 00:38:21 +0000662 break;
663 }
664}
665
666
667static void
Luca Ceresolic718b142011-05-14 05:49:57 +0000668TftpTimeout(void)
wdenkfe8c2802002-11-03 00:38:21 +0000669{
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200670 if (++TftpTimeoutCount > TftpTimeoutCountMax) {
Simon Glasse4cde2f2011-10-24 18:00:04 +0000671 restart("Retry count exceeded");
wdenkfe8c2802002-11-03 00:38:21 +0000672 } else {
Luca Ceresolic718b142011-05-14 05:49:57 +0000673 puts("T ");
674 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
Luca Ceresolie59e3562011-05-17 00:03:39 +0000675 if (TftpState != STATE_RECV_WRQ)
676 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000677 }
678}
679
680
Simon Glass58f317d2011-10-24 18:00:05 +0000681void TftpStart(enum proto_t protocol)
wdenkfe8c2802002-11-03 00:38:21 +0000682{
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200683 char *ep; /* Environment pointer */
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200684
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100685 /*
686 * Allow the user to choose TFTP blocksize and timeout.
687 * TFTP protocol has a minimal timeout of 1 second.
688 */
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000689 ep = getenv("tftpblocksize");
690 if (ep != NULL)
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200691 TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100692
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000693 ep = getenv("tftptimeout");
694 if (ep != NULL)
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100695 TftpTimeoutMSecs = simple_strtol(ep, NULL, 10);
696
697 if (TftpTimeoutMSecs < 1000) {
698 printf("TFTP timeout (%ld ms) too low, "
699 "set minimum = 1000 ms\n",
700 TftpTimeoutMSecs);
701 TftpTimeoutMSecs = 1000;
702 }
703
704 debug("TFTP blocksize = %i, timeout = %ld ms\n",
705 TftpBlkSizeOption, TftpTimeoutMSecs);
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200706
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000707 TftpRemoteIP = NetServerIP;
wdenkfe8c2802002-11-03 00:38:21 +0000708 if (BootFile[0] == '\0') {
Matthias Weisserea45cb02011-12-03 03:29:44 +0000709 sprintf(default_filename, "%02X%02X%02X%02X.img",
Wolfgang Denkc43352c2005-08-04 01:09:44 +0200710 NetOurIP & 0xFF,
711 (NetOurIP >> 8) & 0xFF,
712 (NetOurIP >> 16) & 0xFF,
Luca Ceresolic718b142011-05-14 05:49:57 +0000713 (NetOurIP >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100714
715 strncpy(tftp_filename, default_filename, MAX_LEN);
716 tftp_filename[MAX_LEN-1] = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000717
Luca Ceresolic718b142011-05-14 05:49:57 +0000718 printf("*** Warning: no boot file name; using '%s'\n",
wdenkfe8c2802002-11-03 00:38:21 +0000719 tftp_filename);
720 } else {
Luca Ceresolic718b142011-05-14 05:49:57 +0000721 char *p = strchr(BootFile, ':');
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100722
723 if (p == NULL) {
724 strncpy(tftp_filename, BootFile, MAX_LEN);
725 tftp_filename[MAX_LEN-1] = 0;
726 } else {
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000727 TftpRemoteIP = string_to_ip(BootFile);
Peter Tyser6a86bb62008-12-01 16:29:38 -0600728 strncpy(tftp_filename, p + 1, MAX_LEN);
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100729 tftp_filename[MAX_LEN-1] = 0;
730 }
wdenkfe8c2802002-11-03 00:38:21 +0000731 }
732
Luca Ceresolic718b142011-05-14 05:49:57 +0000733 printf("Using %s device\n", eth_get_name());
Simon Glass1fb7cd42011-10-24 18:00:07 +0000734 printf("TFTP %s server %pI4; our IP address is %pI4",
Simon Glass8c6914f2011-10-27 06:24:29 +0000735#ifdef CONFIG_CMD_TFTPPUT
736 protocol == TFTPPUT ? "to" : "from",
737#else
738 "from",
739#endif
740 &TftpRemoteIP, &NetOurIP);
wdenkfe8c2802002-11-03 00:38:21 +0000741
742 /* Check if we need to send across this subnet */
743 if (NetOurGatewayIP && NetOurSubnetMask) {
Luca Ceresoli0bdd8ac2011-05-14 05:50:02 +0000744 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000745 IPaddr_t RemoteNet = TftpRemoteIP & NetOurSubnetMask;
wdenkfe8c2802002-11-03 00:38:21 +0000746
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000747 if (OurNet != RemoteNet)
Luca Ceresoli0bdd8ac2011-05-14 05:50:02 +0000748 printf("; sending through gateway %pI4",
749 &NetOurGatewayIP);
wdenkfe8c2802002-11-03 00:38:21 +0000750 }
Luca Ceresolic718b142011-05-14 05:49:57 +0000751 putc('\n');
wdenkfe8c2802002-11-03 00:38:21 +0000752
Luca Ceresolic718b142011-05-14 05:49:57 +0000753 printf("Filename '%s'.", tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000754
755 if (NetBootFileSize) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000756 printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
757 print_size(NetBootFileSize<<9, "");
wdenkfe8c2802002-11-03 00:38:21 +0000758 }
759
Luca Ceresolic718b142011-05-14 05:49:57 +0000760 putc('\n');
Simon Glass1fb7cd42011-10-24 18:00:07 +0000761#ifdef CONFIG_CMD_TFTPPUT
762 TftpWriting = (protocol == TFTPPUT);
763 if (TftpWriting) {
764 printf("Save address: 0x%lx\n", save_addr);
765 printf("Save size: 0x%lx\n", save_size);
766 NetBootFileXferSize = save_size;
767 puts("Saving: *\b");
768 TftpState = STATE_SEND_WRQ;
769 new_transfer();
770 } else
771#endif
772 {
773 printf("Load address: 0x%lx\n", load_addr);
774 puts("Loading: *\b");
775 TftpState = STATE_SEND_RRQ;
776 }
wdenkfe8c2802002-11-03 00:38:21 +0000777
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200778 TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
779
Luca Ceresolic718b142011-05-14 05:49:57 +0000780 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
Joe Hershbergerece223b2012-05-23 07:59:15 +0000781 net_set_udp_handler(TftpHandler);
Simon Glass39bccd22011-10-26 14:18:38 +0000782#ifdef CONFIG_CMD_TFTPPUT
Simon Glass1fb7cd42011-10-24 18:00:07 +0000783 net_set_icmp_handler(icmp_handler);
Simon Glass39bccd22011-10-26 14:18:38 +0000784#endif
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000785 TftpRemotePort = WELL_KNOWN_PORT;
wdenkfe8c2802002-11-03 00:38:21 +0000786 TftpTimeoutCount = 0;
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200787 /* Use a pseudo-random port unless a specific port is set */
wdenkfe8c2802002-11-03 00:38:21 +0000788 TftpOurPort = 1024 + (get_timer(0) % 3072);
David Updegraff53a5c422007-06-11 10:41:07 -0500789
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200790#ifdef CONFIG_TFTP_PORT
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000791 ep = getenv("tftpdstp");
Luca Ceresoli7bc325a2011-05-14 05:50:00 +0000792 if (ep != NULL)
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000793 TftpRemotePort = simple_strtol(ep, NULL, 10);
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000794 ep = getenv("tftpsrcp");
Luca Ceresoli7bc325a2011-05-14 05:50:00 +0000795 if (ep != NULL)
Luca Ceresolic718b142011-05-14 05:49:57 +0000796 TftpOurPort = simple_strtol(ep, NULL, 10);
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200797#endif
wdenkfbe4b5c2003-10-06 21:55:32 +0000798 TftpBlock = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000799
wdenk73a8b272003-06-05 19:27:42 +0000800 /* zero out server ether in case the server ip has changed */
801 memset(NetServerEther, 0, 6);
David Updegraff53a5c422007-06-11 10:41:07 -0500802 /* Revert TftpBlkSize to dflt */
803 TftpBlkSize = TFTP_BLOCK_SIZE;
804#ifdef CONFIG_MCAST_TFTP
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100805 mcast_cleanup();
David Updegraff53a5c422007-06-11 10:41:07 -0500806#endif
Robin Getz4fccb812009-08-20 10:50:20 -0400807#ifdef CONFIG_TFTP_TSIZE
808 TftpTsize = 0;
809 TftpNumchars = 0;
810#endif
wdenk73a8b272003-06-05 19:27:42 +0000811
Luca Ceresolic718b142011-05-14 05:49:57 +0000812 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000813}
814
Luca Ceresolie59e3562011-05-17 00:03:39 +0000815#ifdef CONFIG_CMD_TFTPSRV
816void
817TftpStartServer(void)
818{
819 tftp_filename[0] = 0;
820
Luca Ceresolie59e3562011-05-17 00:03:39 +0000821 printf("Using %s device\n", eth_get_name());
Luca Ceresolie59e3562011-05-17 00:03:39 +0000822 printf("Listening for TFTP transfer on %pI4\n", &NetOurIP);
823 printf("Load address: 0x%lx\n", load_addr);
824
825 puts("Loading: *\b");
826
827 TftpTimeoutCountMax = TIMEOUT_COUNT;
828 TftpTimeoutCount = 0;
829 TftpTimeoutMSecs = TIMEOUT;
830 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
831
832 /* Revert TftpBlkSize to dflt */
833 TftpBlkSize = TFTP_BLOCK_SIZE;
834 TftpBlock = 0;
835 TftpOurPort = WELL_KNOWN_PORT;
836
837#ifdef CONFIG_TFTP_TSIZE
838 TftpTsize = 0;
839 TftpNumchars = 0;
840#endif
841
842 TftpState = STATE_RECV_WRQ;
Joe Hershbergerece223b2012-05-23 07:59:15 +0000843 net_set_udp_handler(TftpHandler);
Luca Ceresolie59e3562011-05-17 00:03:39 +0000844}
845#endif /* CONFIG_CMD_TFTPSRV */
846
David Updegraff53a5c422007-06-11 10:41:07 -0500847#ifdef CONFIG_MCAST_TFTP
848/* Credits: atftp project.
849 */
850
851/* pick up BcastAddr, Port, and whether I am [now] the master-client. *
852 * Frame:
853 * +-------+-----------+---+-------~~-------+---+
854 * | opc | multicast | 0 | addr, port, mc | 0 |
855 * +-------+-----------+---+-------~~-------+---+
856 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
857 * I am the new master-client so must send ACKs to DataBlocks. If I am not
858 * master-client, I'm a passive client, gathering what DataBlocks I may and
859 * making note of which ones I got in my bitmask.
860 * In theory, I never go from master->passive..
861 * .. this comes in with pkt already pointing just past opc
862 */
863static void parse_multicast_oack(char *pkt, int len)
864{
Luca Ceresolic718b142011-05-14 05:49:57 +0000865 int i;
866 IPaddr_t addr;
867 char *mc_adr, *port, *mc;
David Updegraff53a5c422007-06-11 10:41:07 -0500868
Luca Ceresolic718b142011-05-14 05:49:57 +0000869 mc_adr = port = mc = NULL;
David Updegraff53a5c422007-06-11 10:41:07 -0500870 /* march along looking for 'multicast\0', which has to start at least
871 * 14 bytes back from the end.
872 */
Luca Ceresolic718b142011-05-14 05:49:57 +0000873 for (i = 0; i < len-14; i++)
874 if (strcmp(pkt+i, "multicast") == 0)
David Updegraff53a5c422007-06-11 10:41:07 -0500875 break;
876 if (i >= (len-14)) /* non-Multicast OACK, ign. */
877 return;
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200878
Luca Ceresolic718b142011-05-14 05:49:57 +0000879 i += 10; /* strlen multicast */
David Updegraff53a5c422007-06-11 10:41:07 -0500880 mc_adr = pkt+i;
Luca Ceresolic718b142011-05-14 05:49:57 +0000881 for (; i < len; i++) {
David Updegraff53a5c422007-06-11 10:41:07 -0500882 if (*(pkt+i) == ',') {
883 *(pkt+i) = '\0';
884 if (port) {
885 mc = pkt+i+1;
886 break;
887 } else {
888 port = pkt+i+1;
889 }
890 }
891 }
Luca Ceresoli6d2231e2011-05-14 05:50:01 +0000892 if (!port || !mc_adr || !mc)
893 return;
David Updegraff53a5c422007-06-11 10:41:07 -0500894 if (Multicast && MasterClient) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000895 printf("I got a OACK as master Client, WRONG!\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500896 return;
897 }
898 /* ..I now accept packets destined for this MCAST addr, port */
899 if (!Multicast) {
900 if (Bitmap) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000901 printf("Internal failure! no mcast.\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500902 free(Bitmap);
Luca Ceresolic718b142011-05-14 05:49:57 +0000903 Bitmap = NULL;
904 ProhibitMcast = 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500905 return ;
906 }
907 /* I malloc instead of pre-declare; so that if the file ends
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200908 * up being too big for this bitmap I can retry
909 */
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000910 Bitmap = malloc(Mapsize);
911 if (!Bitmap) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000912 printf("No Bitmap, no multicast. Sorry.\n");
913 ProhibitMcast = 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500914 return;
915 }
Luca Ceresolic718b142011-05-14 05:49:57 +0000916 memset(Bitmap, 0, Mapsize);
David Updegraff53a5c422007-06-11 10:41:07 -0500917 PrevBitmapHole = 0;
918 Multicast = 1;
919 }
920 addr = string_to_ip(mc_adr);
921 if (Mcast_addr != addr) {
922 if (Mcast_addr)
923 eth_mcast_join(Mcast_addr, 0);
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000924 Mcast_addr = addr;
925 if (eth_mcast_join(Mcast_addr, 1)) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000926 printf("Fail to set mcast, revert to TFTP\n");
927 ProhibitMcast = 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500928 mcast_cleanup();
929 NetStartAgain();
930 }
931 }
Luca Ceresolic718b142011-05-14 05:49:57 +0000932 MasterClient = (unsigned char)simple_strtoul((char *)mc, NULL, 10);
933 Mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
934 printf("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
David Updegraff53a5c422007-06-11 10:41:07 -0500935 return;
936}
937
938#endif /* Multicast TFTP */