blob: e34f20247bcf148133618348fe3c95f824601c03 [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"
14
Luca Ceresoli2f094132011-05-14 05:49:56 +000015/* Well known TFTP port # */
16#define WELL_KNOWN_PORT 69
17/* Millisecs to timeout for lost pkt */
18#define TIMEOUT 5000UL
wdenkfe8c2802002-11-03 00:38:21 +000019#ifndef CONFIG_NET_RETRY_COUNT
Luca Ceresoli2f094132011-05-14 05:49:56 +000020/* # of timeouts before giving up */
21# define TIMEOUT_COUNT 10
wdenkfe8c2802002-11-03 00:38:21 +000022#else
23# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
24#endif
Luca Ceresoli2f094132011-05-14 05:49:56 +000025/* Number of "loading" hashes per line (for checking the image size) */
26#define HASHES_PER_LINE 65
wdenkfe8c2802002-11-03 00:38:21 +000027
28/*
29 * TFTP operations.
30 */
31#define TFTP_RRQ 1
32#define TFTP_WRQ 2
33#define TFTP_DATA 3
34#define TFTP_ACK 4
35#define TFTP_ERROR 5
wdenkfbe4b5c2003-10-06 21:55:32 +000036#define TFTP_OACK 6
wdenkfe8c2802002-11-03 00:38:21 +000037
Bartlomiej Siekae83cc062008-10-01 15:26:29 +020038static ulong TftpTimeoutMSecs = TIMEOUT;
39static int TftpTimeoutCountMax = TIMEOUT_COUNT;
40
41/*
42 * These globals govern the timeout behavior when attempting a connection to a
43 * TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to
44 * wait for the server to respond to initial connection. Second global,
45 * TftpRRQTimeoutCountMax, gives the number of such connection retries.
46 * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be
47 * positive. The globals are meant to be set (and restored) by code needing
48 * non-standard timeout behavior when initiating a TFTP transfer.
49 */
50ulong TftpRRQTimeoutMSecs = TIMEOUT;
51int TftpRRQTimeoutCountMax = TIMEOUT_COUNT;
52
Remy Bohmeraafda382009-10-28 22:13:40 +010053enum {
54 TFTP_ERR_UNDEFINED = 0,
55 TFTP_ERR_FILE_NOT_FOUND = 1,
56 TFTP_ERR_ACCESS_DENIED = 2,
57 TFTP_ERR_DISK_FULL = 3,
58 TFTP_ERR_UNEXPECTED_OPCODE = 4,
59 TFTP_ERR_UNKNOWN_TRANSFER_ID = 5,
60 TFTP_ERR_FILE_ALREADY_EXISTS = 6,
61};
62
Luca Ceresoli20478ce2011-05-17 00:03:37 +000063static IPaddr_t TftpRemoteIP;
Luca Ceresoli2f094132011-05-14 05:49:56 +000064/* The UDP port at their end */
Luca Ceresoli20478ce2011-05-17 00:03:37 +000065static int TftpRemotePort;
Luca Ceresoli2f094132011-05-14 05:49:56 +000066/* The UDP port at our end */
67static int TftpOurPort;
wdenkfe8c2802002-11-03 00:38:21 +000068static int TftpTimeoutCount;
Luca Ceresoli2f094132011-05-14 05:49:56 +000069/* packet sequence number */
70static ulong TftpBlock;
71/* last packet sequence number received */
72static ulong TftpLastBlock;
73/* count of sequence number wraparounds */
74static ulong TftpBlockWrap;
75/* memory offset due to wrapping */
76static ulong TftpBlockWrapOffset;
wdenkfe8c2802002-11-03 00:38:21 +000077static int TftpState;
Robin Getz4fccb812009-08-20 10:50:20 -040078#ifdef CONFIG_TFTP_TSIZE
Luca Ceresoli2f094132011-05-14 05:49:56 +000079/* The file size reported by the server */
80static int TftpTsize;
81/* The number of hashes we printed */
82static short TftpNumchars;
Robin Getz4fccb812009-08-20 10:50:20 -040083#endif
Simon Glass1fb7cd42011-10-24 18:00:07 +000084#ifdef CONFIG_CMD_TFTPPUT
85static int TftpWriting; /* 1 if writing, else 0 */
86static int TftpFinalBlock; /* 1 if we have sent the last block */
87#else
88#define TftpWriting 0
89#endif
wdenk3f85ce22004-02-23 16:11:30 +000090
Luca Ceresolie3fb0ab2011-05-17 00:03:38 +000091#define STATE_SEND_RRQ 1
wdenkfe8c2802002-11-03 00:38:21 +000092#define STATE_DATA 2
93#define STATE_TOO_LARGE 3
94#define STATE_BAD_MAGIC 4
wdenkfbe4b5c2003-10-06 21:55:32 +000095#define STATE_OACK 5
Luca Ceresolie59e3562011-05-17 00:03:39 +000096#define STATE_RECV_WRQ 6
Simon Glass1fb7cd42011-10-24 18:00:07 +000097#define STATE_SEND_WRQ 7
wdenkfe8c2802002-11-03 00:38:21 +000098
Luca Ceresoli2f094132011-05-14 05:49:56 +000099/* default TFTP block size */
100#define TFTP_BLOCK_SIZE 512
101/* sequence number is 16 bit */
102#define TFTP_SEQUENCE_SIZE ((ulong)(1<<16))
wdenk3f85ce22004-02-23 16:11:30 +0000103
wdenkfe8c2802002-11-03 00:38:21 +0000104#define DEFAULT_NAME_LEN (8 + 4 + 1)
105static char default_filename[DEFAULT_NAME_LEN];
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100106
107#ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
108#define MAX_LEN 128
109#else
110#define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
111#endif
112
113static char tftp_filename[MAX_LEN];
wdenkfe8c2802002-11-03 00:38:21 +0000114
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200115#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200116extern flash_info_t flash_info[];
wdenkfe8c2802002-11-03 00:38:21 +0000117#endif
118
Wolfgang Denk85eb5ca2007-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 Updegraff53a5c422007-06-11 10:41:07 -0500121 * almost-MTU block sizes. At least try... fall back to 512 if need be.
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200122 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
David Updegraff53a5c422007-06-11 10:41:07 -0500123 */
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200124#ifdef CONFIG_TFTP_BLOCKSIZE
125#define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
126#else
David Updegraff53a5c422007-06-11 10:41:07 -0500127#define TFTP_MTU_BLOCKSIZE 1468
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200128#endif
129
Luca Ceresolic718b142011-05-14 05:49:57 +0000130static unsigned short TftpBlkSize = TFTP_BLOCK_SIZE;
131static unsigned short TftpBlkSizeOption = TFTP_MTU_BLOCKSIZE;
David Updegraff53a5c422007-06-11 10:41:07 -0500132
133#ifdef CONFIG_MCAST_TFTP
134#include <malloc.h>
135#define MTFTP_BITMAPSIZE 0x1000
136static unsigned *Bitmap;
Luca Ceresolic718b142011-05-14 05:49:57 +0000137static int PrevBitmapHole, Mapsize = MTFTP_BITMAPSIZE;
Luca Ceresoli9bb0a1b2011-05-14 05:50:03 +0000138static uchar ProhibitMcast, MasterClient;
139static uchar Multicast;
David Updegraff53a5c422007-06-11 10:41:07 -0500140extern IPaddr_t Mcast_addr;
141static int Mcast_port;
142static ulong TftpEndingBlock; /* can get 'last' block before done..*/
143
Luca Ceresolic718b142011-05-14 05:49:57 +0000144static void parse_multicast_oack(char *pkt, int len);
David Updegraff53a5c422007-06-11 10:41:07 -0500145
146static void
147mcast_cleanup(void)
148{
Luca Ceresoli6d2231e2011-05-14 05:50:01 +0000149 if (Mcast_addr)
150 eth_mcast_join(Mcast_addr, 0);
151 if (Bitmap)
152 free(Bitmap);
Luca Ceresolic718b142011-05-14 05:49:57 +0000153 Bitmap = NULL;
David Updegraff53a5c422007-06-11 10:41:07 -0500154 Mcast_addr = Multicast = Mcast_port = 0;
155 TftpEndingBlock = -1;
156}
157
158#endif /* CONFIG_MCAST_TFTP */
159
wdenkfe8c2802002-11-03 00:38:21 +0000160static __inline__ void
Luca Ceresoli2e320252011-05-14 05:49:58 +0000161store_block(unsigned block, uchar *src, unsigned len)
wdenkfe8c2802002-11-03 00:38:21 +0000162{
David Updegraff53a5c422007-06-11 10:41:07 -0500163 ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
wdenk3f85ce22004-02-23 16:11:30 +0000164 ulong newsize = offset + len;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-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 Ceresolic718b142011-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 Friedrichbe1b0d22008-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 Ceresolic718b142011-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 Ceresolic718b142011-05-14 05:49:57 +0000181 flash_perror(rc);
wdenkfe8c2802002-11-03 00:38:21 +0000182 NetState = NETLOOP_FAIL;
183 return;
184 }
185 }
186 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200187#endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000188 {
189 (void)memcpy((void *)(load_addr + offset), src, len);
190 }
David Updegraff53a5c422007-06-11 10:41:07 -0500191#ifdef CONFIG_MCAST_TFTP
192 if (Multicast)
193 ext2_set_bit(block, Bitmap);
194#endif
wdenkfe8c2802002-11-03 00:38:21 +0000195
196 if (NetBootFileXferSize < newsize)
197 NetBootFileXferSize = newsize;
198}
199
Simon Glasse4cde2f2011-10-24 18:00:04 +0000200/* Clear our state ready for a new transfer */
201void new_transfer(void)
202{
203 TftpLastBlock = 0;
204 TftpBlockWrap = 0;
205 TftpBlockWrapOffset = 0;
206#ifdef CONFIG_CMD_TFTPPUT
207 TftpFinalBlock = 0;
208#endif
209}
210
Simon Glass1fb7cd42011-10-24 18:00:07 +0000211#ifdef CONFIG_CMD_TFTPPUT
212/**
213 * Load the next block from memory to be sent over tftp.
214 *
215 * @param block Block number to send
216 * @param dst Destination buffer for data
217 * @param len Number of bytes in block (this one and every other)
218 * @return number of bytes loaded
219 */
220static int load_block(unsigned block, uchar *dst, unsigned len)
221{
222 /* We may want to get the final block from the previous set */
223 ulong offset = ((int)block - 1) * len + TftpBlockWrapOffset;
224 ulong tosend = len;
225
226 tosend = min(NetBootFileXferSize - offset, tosend);
227 (void)memcpy(dst, (void *)(save_addr + offset), tosend);
228 debug("%s: block=%d, offset=%ld, len=%d, tosend=%ld\n", __func__,
229 block, offset, len, tosend);
230 return tosend;
231}
232#endif
233
Luca Ceresolic718b142011-05-14 05:49:57 +0000234static void TftpSend(void);
235static void TftpTimeout(void);
wdenkfe8c2802002-11-03 00:38:21 +0000236
237/**********************************************************************/
238
Simon Glassf5329bb2011-10-24 18:00:03 +0000239static void show_block_marker(void)
240{
241#ifdef CONFIG_TFTP_TSIZE
242 if (TftpTsize) {
243 ulong pos = TftpBlock * TftpBlkSize + TftpBlockWrapOffset;
244
245 while (TftpNumchars < pos * 50 / TftpTsize) {
246 putc('#');
247 TftpNumchars++;
248 }
Simon Glass1fb7cd42011-10-24 18:00:07 +0000249 } else
Simon Glassf5329bb2011-10-24 18:00:03 +0000250#endif
Simon Glass1fb7cd42011-10-24 18:00:07 +0000251 {
Simon Glassf5329bb2011-10-24 18:00:03 +0000252 if (((TftpBlock - 1) % 10) == 0)
253 putc('#');
254 else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0)
255 puts("\n\t ");
256 }
257}
258
Simon Glasse4cde2f2011-10-24 18:00:04 +0000259/**
260 * restart the current transfer due to an error
261 *
262 * @param msg Message to print for user
263 */
264static void restart(const char *msg)
265{
266 printf("\n%s; starting again\n", msg);
267#ifdef CONFIG_MCAST_TFTP
268 mcast_cleanup();
269#endif
270 NetStartAgain();
271}
272
273/*
274 * Check if the block number has wrapped, and update progress
275 *
276 * TODO: The egregious use of global variables in this file should be tidied.
277 */
278static void update_block_number(void)
279{
280 /*
281 * RFC1350 specifies that the first data packet will
282 * have sequence number 1. If we receive a sequence
283 * number of 0 this means that there was a wrap
284 * around of the (16 bit) counter.
285 */
286 if (TftpBlock == 0) {
287 TftpBlockWrap++;
288 TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE;
289 TftpTimeoutCount = 0; /* we've done well, reset thhe timeout */
290 } else {
291 show_block_marker();
292 }
293}
294
Simon Glassf5329bb2011-10-24 18:00:03 +0000295/* The TFTP get or put is complete */
296static void tftp_complete(void)
297{
298#ifdef CONFIG_TFTP_TSIZE
299 /* Print hash marks for the last packet received */
300 while (TftpTsize && TftpNumchars < 49) {
301 putc('#');
302 TftpNumchars++;
303 }
304#endif
305 puts("\ndone\n");
306 NetState = NETLOOP_SUCCESS;
307}
308
wdenkfe8c2802002-11-03 00:38:21 +0000309static void
Luca Ceresolic718b142011-05-14 05:49:57 +0000310TftpSend(void)
wdenkfe8c2802002-11-03 00:38:21 +0000311{
Simon Glass1fb7cd42011-10-24 18:00:07 +0000312 uchar *pkt;
Luca Ceresoli2e320252011-05-14 05:49:58 +0000313 volatile uchar *xp;
314 int len = 0;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200315 volatile ushort *s;
wdenkfe8c2802002-11-03 00:38:21 +0000316
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200317#ifdef CONFIG_MCAST_TFTP
David Updegraff53a5c422007-06-11 10:41:07 -0500318 /* Multicast TFTP.. non-MasterClients do not ACK data. */
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200319 if (Multicast
320 && (TftpState == STATE_DATA)
321 && (MasterClient == 0))
David Updegraff53a5c422007-06-11 10:41:07 -0500322 return;
323#endif
wdenkfe8c2802002-11-03 00:38:21 +0000324 /*
325 * We will always be sending some sort of packet, so
326 * cobble together the packet headers now.
327 */
Simon Glass1fb7cd42011-10-24 18:00:07 +0000328 pkt = (uchar *)(NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE);
wdenkfe8c2802002-11-03 00:38:21 +0000329
330 switch (TftpState) {
Luca Ceresolie3fb0ab2011-05-17 00:03:38 +0000331 case STATE_SEND_RRQ:
Simon Glass1fb7cd42011-10-24 18:00:07 +0000332 case STATE_SEND_WRQ:
wdenkfe8c2802002-11-03 00:38:21 +0000333 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200334 s = (ushort *)pkt;
Simon Glass1fb7cd42011-10-24 18:00:07 +0000335 *s++ = htons(TftpState == STATE_SEND_RRQ ? TFTP_RRQ :
336 TFTP_WRQ);
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200337 pkt = (uchar *)s;
Luca Ceresolic718b142011-05-14 05:49:57 +0000338 strcpy((char *)pkt, tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000339 pkt += strlen(tftp_filename) + 1;
Luca Ceresolic718b142011-05-14 05:49:57 +0000340 strcpy((char *)pkt, "octet");
wdenkfe8c2802002-11-03 00:38:21 +0000341 pkt += 5 /*strlen("octet")*/ + 1;
Luca Ceresolic718b142011-05-14 05:49:57 +0000342 strcpy((char *)pkt, "timeout");
wdenkfbe4b5c2003-10-06 21:55:32 +0000343 pkt += 7 /*strlen("timeout")*/ + 1;
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100344 sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000);
Robin Getz0ebf04c2009-07-23 03:01:03 -0400345 debug("send option \"timeout %s\"\n", (char *)pkt);
wdenkfbe4b5c2003-10-06 21:55:32 +0000346 pkt += strlen((char *)pkt) + 1;
Robin Getz4fccb812009-08-20 10:50:20 -0400347#ifdef CONFIG_TFTP_TSIZE
Simon Glass1fb7cd42011-10-24 18:00:07 +0000348 pkt += sprintf((char *)pkt, "tsize%c%lu%c",
349 0, NetBootFileXferSize, 0);
Robin Getz4fccb812009-08-20 10:50:20 -0400350#endif
David Updegraff53a5c422007-06-11 10:41:07 -0500351 /* try for more effic. blk size */
Luca Ceresolic718b142011-05-14 05:49:57 +0000352 pkt += sprintf((char *)pkt, "blksize%c%d%c",
353 0, TftpBlkSizeOption, 0);
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200354#ifdef CONFIG_MCAST_TFTP
David Updegraff53a5c422007-06-11 10:41:07 -0500355 /* Check all preconditions before even trying the option */
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200356 if (!ProhibitMcast
Luca Ceresolic718b142011-05-14 05:49:57 +0000357 && (Bitmap = malloc(Mapsize))
David Updegraff53a5c422007-06-11 10:41:07 -0500358 && eth_get_dev()->mcast) {
359 free(Bitmap);
Luca Ceresolic718b142011-05-14 05:49:57 +0000360 Bitmap = NULL;
361 pkt += sprintf((char *)pkt, "multicast%c%c", 0, 0);
David Updegraff53a5c422007-06-11 10:41:07 -0500362 }
363#endif /* CONFIG_MCAST_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000364 len = pkt - xp;
365 break;
366
wdenkfbe4b5c2003-10-06 21:55:32 +0000367 case STATE_OACK:
David Updegraff53a5c422007-06-11 10:41:07 -0500368#ifdef CONFIG_MCAST_TFTP
369 /* My turn! Start at where I need blocks I missed.*/
370 if (Multicast)
Luca Ceresolic718b142011-05-14 05:49:57 +0000371 TftpBlock = ext2_find_next_zero_bit(Bitmap,
372 (Mapsize*8), 0);
David Updegraff53a5c422007-06-11 10:41:07 -0500373 /*..falling..*/
374#endif
Luca Ceresolie59e3562011-05-17 00:03:39 +0000375
376 case STATE_RECV_WRQ:
David Updegraff53a5c422007-06-11 10:41:07 -0500377 case STATE_DATA:
wdenkfe8c2802002-11-03 00:38:21 +0000378 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200379 s = (ushort *)pkt;
Simon Glass1fb7cd42011-10-24 18:00:07 +0000380 s[0] = htons(TFTP_ACK);
381 s[1] = htons(TftpBlock);
382 pkt = (uchar *)(s + 2);
383#ifdef CONFIG_CMD_TFTPPUT
384 if (TftpWriting) {
385 int toload = TftpBlkSize;
386 int loaded = load_block(TftpBlock, pkt, toload);
387
388 s[0] = htons(TFTP_DATA);
389 pkt += loaded;
390 TftpFinalBlock = (loaded < toload);
391 }
392#endif
wdenkfe8c2802002-11-03 00:38:21 +0000393 len = pkt - xp;
394 break;
395
396 case STATE_TOO_LARGE:
397 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200398 s = (ushort *)pkt;
399 *s++ = htons(TFTP_ERROR);
Simon Glass1fb7cd42011-10-24 18:00:07 +0000400 *s++ = htons(3);
401
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200402 pkt = (uchar *)s;
Luca Ceresolic718b142011-05-14 05:49:57 +0000403 strcpy((char *)pkt, "File too large");
wdenkfe8c2802002-11-03 00:38:21 +0000404 pkt += 14 /*strlen("File too large")*/ + 1;
405 len = pkt - xp;
406 break;
407
408 case STATE_BAD_MAGIC:
409 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200410 s = (ushort *)pkt;
411 *s++ = htons(TFTP_ERROR);
412 *s++ = htons(2);
413 pkt = (uchar *)s;
Luca Ceresolic718b142011-05-14 05:49:57 +0000414 strcpy((char *)pkt, "File has bad magic");
wdenkfe8c2802002-11-03 00:38:21 +0000415 pkt += 18 /*strlen("File has bad magic")*/ + 1;
416 len = pkt - xp;
417 break;
418 }
419
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000420 NetSendUDPPacket(NetServerEther, TftpRemoteIP, TftpRemotePort,
Luca Ceresoli2f094132011-05-14 05:49:56 +0000421 TftpOurPort, len);
wdenkfe8c2802002-11-03 00:38:21 +0000422}
423
Simon Glass39bccd22011-10-26 14:18:38 +0000424#ifdef CONFIG_CMD_TFTPPUT
Simon Glass1fb7cd42011-10-24 18:00:07 +0000425static void icmp_handler(unsigned type, unsigned code, unsigned dest,
426 IPaddr_t sip, unsigned src, uchar *pkt, unsigned len)
427{
428 if (type == ICMP_NOT_REACH && code == ICMP_NOT_REACH_PORT) {
429 /* Oh dear the other end has gone away */
430 restart("TFTP server died");
431 }
432}
Simon Glass39bccd22011-10-26 14:18:38 +0000433#endif
Simon Glass1fb7cd42011-10-24 18:00:07 +0000434
wdenkfe8c2802002-11-03 00:38:21 +0000435static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000436TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
437 unsigned len)
wdenkfe8c2802002-11-03 00:38:21 +0000438{
439 ushort proto;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200440 ushort *s;
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200441 int i;
wdenkfe8c2802002-11-03 00:38:21 +0000442
443 if (dest != TftpOurPort) {
David Updegraff53a5c422007-06-11 10:41:07 -0500444#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200445 if (Multicast
David Updegraff53a5c422007-06-11 10:41:07 -0500446 && (!Mcast_port || (dest != Mcast_port)))
447#endif
Luca Ceresoli0bdd8ac2011-05-14 05:50:02 +0000448 return;
wdenkfe8c2802002-11-03 00:38:21 +0000449 }
Luca Ceresolie59e3562011-05-17 00:03:39 +0000450 if (TftpState != STATE_SEND_RRQ && src != TftpRemotePort &&
Simon Glass1fb7cd42011-10-24 18:00:07 +0000451 TftpState != STATE_RECV_WRQ && TftpState != STATE_SEND_WRQ)
wdenkfe8c2802002-11-03 00:38:21 +0000452 return;
wdenkfe8c2802002-11-03 00:38:21 +0000453
Luca Ceresoli7bc325a2011-05-14 05:50:00 +0000454 if (len < 2)
wdenkfe8c2802002-11-03 00:38:21 +0000455 return;
wdenkfe8c2802002-11-03 00:38:21 +0000456 len -= 2;
457 /* warning: don't use increment (++) in ntohs() macros!! */
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200458 s = (ushort *)pkt;
459 proto = *s++;
460 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000461 switch (ntohs(proto)) {
462
463 case TFTP_RRQ:
wdenkfe8c2802002-11-03 00:38:21 +0000464 break;
Simon Glass1fb7cd42011-10-24 18:00:07 +0000465
466 case TFTP_ACK:
467#ifdef CONFIG_CMD_TFTPPUT
468 if (TftpWriting) {
469 if (TftpFinalBlock) {
470 tftp_complete();
471 } else {
472 /*
473 * Move to the next block. We want our block
474 * count to wrap just like the other end!
475 */
476 int block = ntohs(*s);
477 int ack_ok = (TftpBlock == block);
478
479 TftpBlock = (unsigned short)(block + 1);
480 update_block_number();
481 if (ack_ok)
482 TftpSend(); /* Send next data block */
483 }
484 }
485#endif
486 break;
487
wdenkfe8c2802002-11-03 00:38:21 +0000488 default:
489 break;
490
Luca Ceresolie59e3562011-05-17 00:03:39 +0000491#ifdef CONFIG_CMD_TFTPSRV
492 case TFTP_WRQ:
493 debug("Got WRQ\n");
494 TftpRemoteIP = sip;
495 TftpRemotePort = src;
496 TftpOurPort = 1024 + (get_timer(0) % 3072);
Simon Glasse4cde2f2011-10-24 18:00:04 +0000497 new_transfer();
Luca Ceresolie59e3562011-05-17 00:03:39 +0000498 TftpSend(); /* Send ACK(0) */
499 break;
500#endif
501
wdenkfbe4b5c2003-10-06 21:55:32 +0000502 case TFTP_OACK:
Wolfgang Denkd3717082009-08-10 09:59:10 +0200503 debug("Got OACK: %s %s\n",
504 pkt,
505 pkt + strlen((char *)pkt) + 1);
wdenkfbe4b5c2003-10-06 21:55:32 +0000506 TftpState = STATE_OACK;
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000507 TftpRemotePort = src;
Wolfgang Denk60174742007-08-31 10:01:51 +0200508 /*
509 * Check for 'blksize' option.
510 * Careful: "i" is signed, "len" is unsigned, thus
511 * something like "len-8" may give a *huge* number
512 */
Luca Ceresolic718b142011-05-14 05:49:57 +0000513 for (i = 0; i+8 < len; i++) {
Luca Ceresoli2e320252011-05-14 05:49:58 +0000514 if (strcmp((char *)pkt+i, "blksize") == 0) {
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200515 TftpBlkSize = (unsigned short)
Luca Ceresoli2e320252011-05-14 05:49:58 +0000516 simple_strtoul((char *)pkt+i+8, NULL,
Luca Ceresolic718b142011-05-14 05:49:57 +0000517 10);
Robin Getz0ebf04c2009-07-23 03:01:03 -0400518 debug("Blocksize ack: %s, %d\n",
Luca Ceresoli2e320252011-05-14 05:49:58 +0000519 (char *)pkt+i+8, TftpBlkSize);
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200520 }
Robin Getz4fccb812009-08-20 10:50:20 -0400521#ifdef CONFIG_TFTP_TSIZE
Luca Ceresoli2e320252011-05-14 05:49:58 +0000522 if (strcmp((char *)pkt+i, "tsize") == 0) {
523 TftpTsize = simple_strtoul((char *)pkt+i+6,
Luca Ceresoli2f094132011-05-14 05:49:56 +0000524 NULL, 10);
Robin Getz4fccb812009-08-20 10:50:20 -0400525 debug("size = %s, %d\n",
Luca Ceresoli2e320252011-05-14 05:49:58 +0000526 (char *)pkt+i+6, TftpTsize);
Robin Getz4fccb812009-08-20 10:50:20 -0400527 }
528#endif
David Updegraff53a5c422007-06-11 10:41:07 -0500529 }
530#ifdef CONFIG_MCAST_TFTP
Luca Ceresolic718b142011-05-14 05:49:57 +0000531 parse_multicast_oack((char *)pkt, len-1);
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200532 if ((Multicast) && (!MasterClient))
David Updegraff53a5c422007-06-11 10:41:07 -0500533 TftpState = STATE_DATA; /* passive.. */
534 else
535#endif
Simon Glass1fb7cd42011-10-24 18:00:07 +0000536#ifdef CONFIG_CMD_TFTPPUT
537 if (TftpWriting) {
538 /* Get ready to send the first block */
539 TftpState = STATE_DATA;
540 TftpBlock++;
541 }
542#endif
543 TftpSend(); /* Send ACK or first data block */
wdenkfbe4b5c2003-10-06 21:55:32 +0000544 break;
wdenkfe8c2802002-11-03 00:38:21 +0000545 case TFTP_DATA:
546 if (len < 2)
547 return;
548 len -= 2;
549 TftpBlock = ntohs(*(ushort *)pkt);
wdenk3f85ce22004-02-23 16:11:30 +0000550
Simon Glasse4cde2f2011-10-24 18:00:04 +0000551 update_block_number();
wdenkfe8c2802002-11-03 00:38:21 +0000552
Luca Ceresolie3fb0ab2011-05-17 00:03:38 +0000553 if (TftpState == STATE_SEND_RRQ)
Robin Getz0ebf04c2009-07-23 03:01:03 -0400554 debug("Server did not acknowledge timeout option!\n");
wdenkfbe4b5c2003-10-06 21:55:32 +0000555
Luca Ceresolie59e3562011-05-17 00:03:39 +0000556 if (TftpState == STATE_SEND_RRQ || TftpState == STATE_OACK ||
557 TftpState == STATE_RECV_WRQ) {
wdenk3f85ce22004-02-23 16:11:30 +0000558 /* first block received */
wdenkfe8c2802002-11-03 00:38:21 +0000559 TftpState = STATE_DATA;
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000560 TftpRemotePort = src;
Simon Glasse4cde2f2011-10-24 18:00:04 +0000561 new_transfer();
wdenkfe8c2802002-11-03 00:38:21 +0000562
David Updegraff53a5c422007-06-11 10:41:07 -0500563#ifdef CONFIG_MCAST_TFTP
564 if (Multicast) { /* start!=1 common if mcast */
565 TftpLastBlock = TftpBlock - 1;
566 } else
567#endif
wdenkfe8c2802002-11-03 00:38:21 +0000568 if (TftpBlock != 1) { /* Assertion */
Luca Ceresolic718b142011-05-14 05:49:57 +0000569 printf("\nTFTP error: "
570 "First block is not block 1 (%ld)\n"
571 "Starting again\n\n",
wdenkfe8c2802002-11-03 00:38:21 +0000572 TftpBlock);
Luca Ceresolic718b142011-05-14 05:49:57 +0000573 NetStartAgain();
wdenkfe8c2802002-11-03 00:38:21 +0000574 break;
575 }
576 }
577
578 if (TftpBlock == TftpLastBlock) {
579 /*
580 * Same block again; ignore it.
581 */
582 break;
583 }
584
585 TftpLastBlock = TftpBlock;
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200586 TftpTimeoutCountMax = TIMEOUT_COUNT;
Luca Ceresolic718b142011-05-14 05:49:57 +0000587 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
wdenkfe8c2802002-11-03 00:38:21 +0000588
Luca Ceresolic718b142011-05-14 05:49:57 +0000589 store_block(TftpBlock - 1, pkt + 2, len);
wdenkfe8c2802002-11-03 00:38:21 +0000590
591 /*
Luca Ceresoli4d69e982011-05-17 00:03:41 +0000592 * Acknowledge the block just received, which will prompt
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000593 * the remote for the next one.
wdenkfe8c2802002-11-03 00:38:21 +0000594 */
David Updegraff53a5c422007-06-11 10:41:07 -0500595#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200596 /* if I am the MasterClient, actively calculate what my next
597 * needed block is; else I'm passive; not ACKING
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100598 */
David Updegraff53a5c422007-06-11 10:41:07 -0500599 if (Multicast) {
600 if (len < TftpBlkSize) {
601 TftpEndingBlock = TftpBlock;
602 } else if (MasterClient) {
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200603 TftpBlock = PrevBitmapHole =
David Updegraff53a5c422007-06-11 10:41:07 -0500604 ext2_find_next_zero_bit(
605 Bitmap,
606 (Mapsize*8),
607 PrevBitmapHole);
608 if (TftpBlock > ((Mapsize*8) - 1)) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000609 printf("tftpfile too big\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500610 /* try to double it and retry */
Luca Ceresolic718b142011-05-14 05:49:57 +0000611 Mapsize <<= 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500612 mcast_cleanup();
Luca Ceresolic718b142011-05-14 05:49:57 +0000613 NetStartAgain();
David Updegraff53a5c422007-06-11 10:41:07 -0500614 return;
615 }
616 TftpLastBlock = TftpBlock;
617 }
618 }
619#endif
Luca Ceresolic718b142011-05-14 05:49:57 +0000620 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000621
David Updegraff53a5c422007-06-11 10:41:07 -0500622#ifdef CONFIG_MCAST_TFTP
623 if (Multicast) {
624 if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000625 puts("\nMulticast tftp done\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500626 mcast_cleanup();
627 NetState = NETLOOP_SUCCESS;
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200628 }
David Updegraff53a5c422007-06-11 10:41:07 -0500629 }
630 else
631#endif
Simon Glassf5329bb2011-10-24 18:00:03 +0000632 if (len < TftpBlkSize)
633 tftp_complete();
wdenkfe8c2802002-11-03 00:38:21 +0000634 break;
635
636 case TFTP_ERROR:
Luca Ceresolic718b142011-05-14 05:49:57 +0000637 printf("\nTFTP error: '%s' (%d)\n",
638 pkt + 2, ntohs(*(ushort *)pkt));
Remy Bohmeraafda382009-10-28 22:13:40 +0100639
640 switch (ntohs(*(ushort *)pkt)) {
641 case TFTP_ERR_FILE_NOT_FOUND:
642 case TFTP_ERR_ACCESS_DENIED:
643 puts("Not retrying...\n");
644 eth_halt();
645 NetState = NETLOOP_FAIL;
646 break;
647 case TFTP_ERR_UNDEFINED:
648 case TFTP_ERR_DISK_FULL:
649 case TFTP_ERR_UNEXPECTED_OPCODE:
650 case TFTP_ERR_UNKNOWN_TRANSFER_ID:
651 case TFTP_ERR_FILE_ALREADY_EXISTS:
652 default:
653 puts("Starting again\n\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500654#ifdef CONFIG_MCAST_TFTP
Remy Bohmeraafda382009-10-28 22:13:40 +0100655 mcast_cleanup();
David Updegraff53a5c422007-06-11 10:41:07 -0500656#endif
Remy Bohmeraafda382009-10-28 22:13:40 +0100657 NetStartAgain();
658 break;
659 }
wdenkfe8c2802002-11-03 00:38:21 +0000660 break;
661 }
662}
663
664
665static void
Luca Ceresolic718b142011-05-14 05:49:57 +0000666TftpTimeout(void)
wdenkfe8c2802002-11-03 00:38:21 +0000667{
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200668 if (++TftpTimeoutCount > TftpTimeoutCountMax) {
Simon Glasse4cde2f2011-10-24 18:00:04 +0000669 restart("Retry count exceeded");
wdenkfe8c2802002-11-03 00:38:21 +0000670 } else {
Luca Ceresolic718b142011-05-14 05:49:57 +0000671 puts("T ");
672 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
Luca Ceresolie59e3562011-05-17 00:03:39 +0000673 if (TftpState != STATE_RECV_WRQ)
674 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000675 }
676}
677
678
Simon Glass58f317d2011-10-24 18:00:05 +0000679void TftpStart(enum proto_t protocol)
wdenkfe8c2802002-11-03 00:38:21 +0000680{
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200681 char *ep; /* Environment pointer */
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200682
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100683 /*
684 * Allow the user to choose TFTP blocksize and timeout.
685 * TFTP protocol has a minimal timeout of 1 second.
686 */
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000687 ep = getenv("tftpblocksize");
688 if (ep != NULL)
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200689 TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100690
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000691 ep = getenv("tftptimeout");
692 if (ep != NULL)
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100693 TftpTimeoutMSecs = simple_strtol(ep, NULL, 10);
694
695 if (TftpTimeoutMSecs < 1000) {
696 printf("TFTP timeout (%ld ms) too low, "
697 "set minimum = 1000 ms\n",
698 TftpTimeoutMSecs);
699 TftpTimeoutMSecs = 1000;
700 }
701
702 debug("TFTP blocksize = %i, timeout = %ld ms\n",
703 TftpBlkSizeOption, TftpTimeoutMSecs);
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200704
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000705 TftpRemoteIP = NetServerIP;
wdenkfe8c2802002-11-03 00:38:21 +0000706 if (BootFile[0] == '\0') {
wdenkfe8c2802002-11-03 00:38:21 +0000707 sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
Wolfgang Denkc43352c2005-08-04 01:09:44 +0200708 NetOurIP & 0xFF,
709 (NetOurIP >> 8) & 0xFF,
710 (NetOurIP >> 16) & 0xFF,
Luca Ceresolic718b142011-05-14 05:49:57 +0000711 (NetOurIP >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100712
713 strncpy(tftp_filename, default_filename, MAX_LEN);
714 tftp_filename[MAX_LEN-1] = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000715
Luca Ceresolic718b142011-05-14 05:49:57 +0000716 printf("*** Warning: no boot file name; using '%s'\n",
wdenkfe8c2802002-11-03 00:38:21 +0000717 tftp_filename);
718 } else {
Luca Ceresolic718b142011-05-14 05:49:57 +0000719 char *p = strchr(BootFile, ':');
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100720
721 if (p == NULL) {
722 strncpy(tftp_filename, BootFile, MAX_LEN);
723 tftp_filename[MAX_LEN-1] = 0;
724 } else {
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000725 TftpRemoteIP = string_to_ip(BootFile);
Peter Tyser6a86bb62008-12-01 16:29:38 -0600726 strncpy(tftp_filename, p + 1, MAX_LEN);
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100727 tftp_filename[MAX_LEN-1] = 0;
728 }
wdenkfe8c2802002-11-03 00:38:21 +0000729 }
730
Luca Ceresolic718b142011-05-14 05:49:57 +0000731 printf("Using %s device\n", eth_get_name());
Simon Glass1fb7cd42011-10-24 18:00:07 +0000732 printf("TFTP %s server %pI4; our IP address is %pI4",
733 protocol == TFTPPUT ? "to" : "from", &TftpRemoteIP, &NetOurIP);
wdenkfe8c2802002-11-03 00:38:21 +0000734
735 /* Check if we need to send across this subnet */
736 if (NetOurGatewayIP && NetOurSubnetMask) {
Luca Ceresoli0bdd8ac2011-05-14 05:50:02 +0000737 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000738 IPaddr_t RemoteNet = TftpRemoteIP & NetOurSubnetMask;
wdenkfe8c2802002-11-03 00:38:21 +0000739
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000740 if (OurNet != RemoteNet)
Luca Ceresoli0bdd8ac2011-05-14 05:50:02 +0000741 printf("; sending through gateway %pI4",
742 &NetOurGatewayIP);
wdenkfe8c2802002-11-03 00:38:21 +0000743 }
Luca Ceresolic718b142011-05-14 05:49:57 +0000744 putc('\n');
wdenkfe8c2802002-11-03 00:38:21 +0000745
Luca Ceresolic718b142011-05-14 05:49:57 +0000746 printf("Filename '%s'.", tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000747
748 if (NetBootFileSize) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000749 printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
750 print_size(NetBootFileSize<<9, "");
wdenkfe8c2802002-11-03 00:38:21 +0000751 }
752
Luca Ceresolic718b142011-05-14 05:49:57 +0000753 putc('\n');
Simon Glass1fb7cd42011-10-24 18:00:07 +0000754#ifdef CONFIG_CMD_TFTPPUT
755 TftpWriting = (protocol == TFTPPUT);
756 if (TftpWriting) {
757 printf("Save address: 0x%lx\n", save_addr);
758 printf("Save size: 0x%lx\n", save_size);
759 NetBootFileXferSize = save_size;
760 puts("Saving: *\b");
761 TftpState = STATE_SEND_WRQ;
762 new_transfer();
763 } else
764#endif
765 {
766 printf("Load address: 0x%lx\n", load_addr);
767 puts("Loading: *\b");
768 TftpState = STATE_SEND_RRQ;
769 }
wdenkfe8c2802002-11-03 00:38:21 +0000770
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200771 TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
772
Luca Ceresolic718b142011-05-14 05:49:57 +0000773 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
774 NetSetHandler(TftpHandler);
Simon Glass39bccd22011-10-26 14:18:38 +0000775#ifdef CONFIG_CMD_TFTPPUT
Simon Glass1fb7cd42011-10-24 18:00:07 +0000776 net_set_icmp_handler(icmp_handler);
Simon Glass39bccd22011-10-26 14:18:38 +0000777#endif
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000778 TftpRemotePort = WELL_KNOWN_PORT;
wdenkfe8c2802002-11-03 00:38:21 +0000779 TftpTimeoutCount = 0;
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200780 /* Use a pseudo-random port unless a specific port is set */
wdenkfe8c2802002-11-03 00:38:21 +0000781 TftpOurPort = 1024 + (get_timer(0) % 3072);
David Updegraff53a5c422007-06-11 10:41:07 -0500782
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200783#ifdef CONFIG_TFTP_PORT
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000784 ep = getenv("tftpdstp");
Luca Ceresoli7bc325a2011-05-14 05:50:00 +0000785 if (ep != NULL)
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000786 TftpRemotePort = simple_strtol(ep, NULL, 10);
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000787 ep = getenv("tftpsrcp");
Luca Ceresoli7bc325a2011-05-14 05:50:00 +0000788 if (ep != NULL)
Luca Ceresolic718b142011-05-14 05:49:57 +0000789 TftpOurPort = simple_strtol(ep, NULL, 10);
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200790#endif
wdenkfbe4b5c2003-10-06 21:55:32 +0000791 TftpBlock = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000792
wdenk73a8b272003-06-05 19:27:42 +0000793 /* zero out server ether in case the server ip has changed */
794 memset(NetServerEther, 0, 6);
David Updegraff53a5c422007-06-11 10:41:07 -0500795 /* Revert TftpBlkSize to dflt */
796 TftpBlkSize = TFTP_BLOCK_SIZE;
797#ifdef CONFIG_MCAST_TFTP
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100798 mcast_cleanup();
David Updegraff53a5c422007-06-11 10:41:07 -0500799#endif
Robin Getz4fccb812009-08-20 10:50:20 -0400800#ifdef CONFIG_TFTP_TSIZE
801 TftpTsize = 0;
802 TftpNumchars = 0;
803#endif
wdenk73a8b272003-06-05 19:27:42 +0000804
Luca Ceresolic718b142011-05-14 05:49:57 +0000805 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000806}
807
Luca Ceresolie59e3562011-05-17 00:03:39 +0000808#ifdef CONFIG_CMD_TFTPSRV
809void
810TftpStartServer(void)
811{
812 tftp_filename[0] = 0;
813
Luca Ceresolie59e3562011-05-17 00:03:39 +0000814 printf("Using %s device\n", eth_get_name());
Luca Ceresolie59e3562011-05-17 00:03:39 +0000815 printf("Listening for TFTP transfer on %pI4\n", &NetOurIP);
816 printf("Load address: 0x%lx\n", load_addr);
817
818 puts("Loading: *\b");
819
820 TftpTimeoutCountMax = TIMEOUT_COUNT;
821 TftpTimeoutCount = 0;
822 TftpTimeoutMSecs = TIMEOUT;
823 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
824
825 /* Revert TftpBlkSize to dflt */
826 TftpBlkSize = TFTP_BLOCK_SIZE;
827 TftpBlock = 0;
828 TftpOurPort = WELL_KNOWN_PORT;
829
830#ifdef CONFIG_TFTP_TSIZE
831 TftpTsize = 0;
832 TftpNumchars = 0;
833#endif
834
835 TftpState = STATE_RECV_WRQ;
836 NetSetHandler(TftpHandler);
837}
838#endif /* CONFIG_CMD_TFTPSRV */
839
David Updegraff53a5c422007-06-11 10:41:07 -0500840#ifdef CONFIG_MCAST_TFTP
841/* Credits: atftp project.
842 */
843
844/* pick up BcastAddr, Port, and whether I am [now] the master-client. *
845 * Frame:
846 * +-------+-----------+---+-------~~-------+---+
847 * | opc | multicast | 0 | addr, port, mc | 0 |
848 * +-------+-----------+---+-------~~-------+---+
849 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
850 * I am the new master-client so must send ACKs to DataBlocks. If I am not
851 * master-client, I'm a passive client, gathering what DataBlocks I may and
852 * making note of which ones I got in my bitmask.
853 * In theory, I never go from master->passive..
854 * .. this comes in with pkt already pointing just past opc
855 */
856static void parse_multicast_oack(char *pkt, int len)
857{
Luca Ceresolic718b142011-05-14 05:49:57 +0000858 int i;
859 IPaddr_t addr;
860 char *mc_adr, *port, *mc;
David Updegraff53a5c422007-06-11 10:41:07 -0500861
Luca Ceresolic718b142011-05-14 05:49:57 +0000862 mc_adr = port = mc = NULL;
David Updegraff53a5c422007-06-11 10:41:07 -0500863 /* march along looking for 'multicast\0', which has to start at least
864 * 14 bytes back from the end.
865 */
Luca Ceresolic718b142011-05-14 05:49:57 +0000866 for (i = 0; i < len-14; i++)
867 if (strcmp(pkt+i, "multicast") == 0)
David Updegraff53a5c422007-06-11 10:41:07 -0500868 break;
869 if (i >= (len-14)) /* non-Multicast OACK, ign. */
870 return;
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200871
Luca Ceresolic718b142011-05-14 05:49:57 +0000872 i += 10; /* strlen multicast */
David Updegraff53a5c422007-06-11 10:41:07 -0500873 mc_adr = pkt+i;
Luca Ceresolic718b142011-05-14 05:49:57 +0000874 for (; i < len; i++) {
David Updegraff53a5c422007-06-11 10:41:07 -0500875 if (*(pkt+i) == ',') {
876 *(pkt+i) = '\0';
877 if (port) {
878 mc = pkt+i+1;
879 break;
880 } else {
881 port = pkt+i+1;
882 }
883 }
884 }
Luca Ceresoli6d2231e2011-05-14 05:50:01 +0000885 if (!port || !mc_adr || !mc)
886 return;
David Updegraff53a5c422007-06-11 10:41:07 -0500887 if (Multicast && MasterClient) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000888 printf("I got a OACK as master Client, WRONG!\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500889 return;
890 }
891 /* ..I now accept packets destined for this MCAST addr, port */
892 if (!Multicast) {
893 if (Bitmap) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000894 printf("Internal failure! no mcast.\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500895 free(Bitmap);
Luca Ceresolic718b142011-05-14 05:49:57 +0000896 Bitmap = NULL;
897 ProhibitMcast = 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500898 return ;
899 }
900 /* I malloc instead of pre-declare; so that if the file ends
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200901 * up being too big for this bitmap I can retry
902 */
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000903 Bitmap = malloc(Mapsize);
904 if (!Bitmap) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000905 printf("No Bitmap, no multicast. Sorry.\n");
906 ProhibitMcast = 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500907 return;
908 }
Luca Ceresolic718b142011-05-14 05:49:57 +0000909 memset(Bitmap, 0, Mapsize);
David Updegraff53a5c422007-06-11 10:41:07 -0500910 PrevBitmapHole = 0;
911 Multicast = 1;
912 }
913 addr = string_to_ip(mc_adr);
914 if (Mcast_addr != addr) {
915 if (Mcast_addr)
916 eth_mcast_join(Mcast_addr, 0);
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000917 Mcast_addr = addr;
918 if (eth_mcast_join(Mcast_addr, 1)) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000919 printf("Fail to set mcast, revert to TFTP\n");
920 ProhibitMcast = 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500921 mcast_cleanup();
922 NetStartAgain();
923 }
924 }
Luca Ceresolic718b142011-05-14 05:49:57 +0000925 MasterClient = (unsigned char)simple_strtoul((char *)mc, NULL, 10);
926 Mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
927 printf("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
David Updegraff53a5c422007-06-11 10:41:07 -0500928 return;
929}
930
931#endif /* Multicast TFTP */