blob: 7aa3e23c95d1aba5f79202b7a0f550fd04889754 [file] [log] [blame]
wdenkfe8c2802002-11-03 00:38:21 +00001/*
Luca Ceresolic42fc272011-05-14 05:49:56 +00002 * Copyright 1994, 1995, 2000 Neil Russell.
3 * (See License)
4 * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
Luca Ceresolib640ed32011-05-17 00:03:39 +00005 * Copyright 2011 Comelit Group SpA,
6 * Luca Ceresoli <luca.ceresoli@comelit.it>
wdenkfe8c2802002-11-03 00:38:21 +00007 */
8
9#include <common.h>
10#include <command.h>
11#include <net.h>
12#include "tftp.h"
13#include "bootp.h"
14
Luca Ceresolic42fc272011-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 Ceresolic42fc272011-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 Ceresolic42fc272011-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
wdenk7182b0f2003-10-06 21:55:32 +000036#define TFTP_OACK 6
wdenkfe8c2802002-11-03 00:38:21 +000037
Bartlomiej Sieka371cde72008-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 Bohmerb46293f2009-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 Ceresoli5079c762011-05-17 00:03:37 +000063static IPaddr_t TftpRemoteIP;
Luca Ceresolic42fc272011-05-14 05:49:56 +000064/* The UDP port at their end */
Luca Ceresoli5079c762011-05-17 00:03:37 +000065static int TftpRemotePort;
Luca Ceresolic42fc272011-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 Ceresolic42fc272011-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 Getzb3b00ed2009-08-20 10:50:20 -040078#ifdef CONFIG_TFTP_TSIZE
Luca Ceresolic42fc272011-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 Getzb3b00ed2009-08-20 10:50:20 -040083#endif
Simon Glass6a398d22011-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
wdenkef893942004-02-23 16:11:30 +000090
Luca Ceresoli562dfe52011-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
wdenk7182b0f2003-10-06 21:55:32 +000095#define STATE_OACK 5
Luca Ceresolib640ed32011-05-17 00:03:39 +000096#define STATE_RECV_WRQ 6
Simon Glass6a398d22011-10-24 18:00:07 +000097#define STATE_SEND_WRQ 7
wdenkfe8c2802002-11-03 00:38:21 +000098
Luca Ceresolic42fc272011-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))
wdenkef893942004-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-VILLARD58555412008-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-VILLARD03836942008-10-16 15:01:15 +0200115#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200116extern flash_info_t flash_info[];
wdenkfe8c2802002-11-03 00:38:21 +0000117#endif
118
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200119/* 512 is poor choice for ethernet, MTU is typically 1500.
120 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
David Updegraff7280da72007-06-11 10:41:07 -0500121 * almost-MTU block sizes. At least try... fall back to 512 if need be.
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200122 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
David Updegraff7280da72007-06-11 10:41:07 -0500123 */
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200124#ifdef CONFIG_TFTP_BLOCKSIZE
125#define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
126#else
David Updegraff7280da72007-06-11 10:41:07 -0500127#define TFTP_MTU_BLOCKSIZE 1468
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200128#endif
129
Luca Ceresolif40538a2011-05-14 05:49:57 +0000130static unsigned short TftpBlkSize = TFTP_BLOCK_SIZE;
131static unsigned short TftpBlkSizeOption = TFTP_MTU_BLOCKSIZE;
David Updegraff7280da72007-06-11 10:41:07 -0500132
133#ifdef CONFIG_MCAST_TFTP
134#include <malloc.h>
135#define MTFTP_BITMAPSIZE 0x1000
136static unsigned *Bitmap;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000137static int PrevBitmapHole, Mapsize = MTFTP_BITMAPSIZE;
Luca Ceresoli4e7a02b2011-05-14 05:50:03 +0000138static uchar ProhibitMcast, MasterClient;
139static uchar Multicast;
David Updegraff7280da72007-06-11 10:41:07 -0500140extern IPaddr_t Mcast_addr;
141static 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
wdenkfe8c2802002-11-03 00:38:21 +0000160static __inline__ void
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000161store_block(unsigned 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);
wdenkfe8c2802002-11-03 00:38:21 +0000182 NetState = NETLOOP_FAIL;
183 return;
184 }
185 }
186 else
Jean-Christophe PLAGNIOL-VILLARD03836942008-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 Updegraff7280da72007-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 Glassbd8a0e02011-10-24 18:00:04 +0000200/* Clear our state ready for a new transfer */
Simon Glass84313322011-10-27 06:24:28 +0000201static void new_transfer(void)
Simon Glassbd8a0e02011-10-24 18:00:04 +0000202{
203 TftpLastBlock = 0;
204 TftpBlockWrap = 0;
205 TftpBlockWrapOffset = 0;
206#ifdef CONFIG_CMD_TFTPPUT
207 TftpFinalBlock = 0;
208#endif
209}
Simon Glass6a398d22011-10-24 18:00:07 +0000210
211#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
Simon Glassbd8a0e02011-10-24 18:00:04 +0000233
Luca Ceresolif40538a2011-05-14 05:49:57 +0000234static void TftpSend(void);
235static void TftpTimeout(void);
wdenkfe8c2802002-11-03 00:38:21 +0000236
237/**********************************************************************/
238
Simon Glassb437aad2011-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 Glass6a398d22011-10-24 18:00:07 +0000249 } else
Simon Glassb437aad2011-10-24 18:00:03 +0000250#endif
Simon Glass6a398d22011-10-24 18:00:07 +0000251 {
Simon Glassb437aad2011-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 Glassbd8a0e02011-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 Glassb437aad2011-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 Ceresolif40538a2011-05-14 05:49:57 +0000310TftpSend(void)
wdenkfe8c2802002-11-03 00:38:21 +0000311{
Simon Glass6a398d22011-10-24 18:00:07 +0000312 uchar *pkt;
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000313 volatile uchar *xp;
314 int len = 0;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200315 volatile ushort *s;
wdenkfe8c2802002-11-03 00:38:21 +0000316
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200317#ifdef CONFIG_MCAST_TFTP
David Updegraff7280da72007-06-11 10:41:07 -0500318 /* Multicast TFTP.. non-MasterClients do not ACK data. */
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200319 if (Multicast
320 && (TftpState == STATE_DATA)
321 && (MasterClient == 0))
David Updegraff7280da72007-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 Glass6a398d22011-10-24 18:00:07 +0000328 pkt = (uchar *)(NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE);
wdenkfe8c2802002-11-03 00:38:21 +0000329
330 switch (TftpState) {
Luca Ceresoli562dfe52011-05-17 00:03:38 +0000331 case STATE_SEND_RRQ:
Simon Glass6a398d22011-10-24 18:00:07 +0000332 case STATE_SEND_WRQ:
wdenkfe8c2802002-11-03 00:38:21 +0000333 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200334 s = (ushort *)pkt;
Simon Glass4abd0e22011-10-27 06:24:29 +0000335#ifdef CONFIG_CMD_TFTPPUT
Simon Glass6a398d22011-10-24 18:00:07 +0000336 *s++ = htons(TftpState == STATE_SEND_RRQ ? TFTP_RRQ :
337 TFTP_WRQ);
Simon Glass4abd0e22011-10-27 06:24:29 +0000338#else
339 *s++ = htons(TFTP_RRQ);
340#endif
Wolfgang Denk3135c542005-08-26 01:36:03 +0200341 pkt = (uchar *)s;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000342 strcpy((char *)pkt, tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000343 pkt += strlen(tftp_filename) + 1;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000344 strcpy((char *)pkt, "octet");
wdenkfe8c2802002-11-03 00:38:21 +0000345 pkt += 5 /*strlen("octet")*/ + 1;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000346 strcpy((char *)pkt, "timeout");
wdenk7182b0f2003-10-06 21:55:32 +0000347 pkt += 7 /*strlen("timeout")*/ + 1;
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100348 sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000);
Robin Getz9e0a4d62009-07-23 03:01:03 -0400349 debug("send option \"timeout %s\"\n", (char *)pkt);
wdenk7182b0f2003-10-06 21:55:32 +0000350 pkt += strlen((char *)pkt) + 1;
Robin Getzb3b00ed2009-08-20 10:50:20 -0400351#ifdef CONFIG_TFTP_TSIZE
Simon Glass6a398d22011-10-24 18:00:07 +0000352 pkt += sprintf((char *)pkt, "tsize%c%lu%c",
353 0, NetBootFileXferSize, 0);
Robin Getzb3b00ed2009-08-20 10:50:20 -0400354#endif
David Updegraff7280da72007-06-11 10:41:07 -0500355 /* try for more effic. blk size */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000356 pkt += sprintf((char *)pkt, "blksize%c%d%c",
357 0, TftpBlkSizeOption, 0);
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200358#ifdef CONFIG_MCAST_TFTP
David Updegraff7280da72007-06-11 10:41:07 -0500359 /* Check all preconditions before even trying the option */
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200360 if (!ProhibitMcast
Luca Ceresolif40538a2011-05-14 05:49:57 +0000361 && (Bitmap = malloc(Mapsize))
David Updegraff7280da72007-06-11 10:41:07 -0500362 && eth_get_dev()->mcast) {
363 free(Bitmap);
Luca Ceresolif40538a2011-05-14 05:49:57 +0000364 Bitmap = NULL;
365 pkt += sprintf((char *)pkt, "multicast%c%c", 0, 0);
David Updegraff7280da72007-06-11 10:41:07 -0500366 }
367#endif /* CONFIG_MCAST_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000368 len = pkt - xp;
369 break;
370
wdenk7182b0f2003-10-06 21:55:32 +0000371 case STATE_OACK:
David Updegraff7280da72007-06-11 10:41:07 -0500372#ifdef CONFIG_MCAST_TFTP
373 /* My turn! Start at where I need blocks I missed.*/
374 if (Multicast)
Luca Ceresolif40538a2011-05-14 05:49:57 +0000375 TftpBlock = ext2_find_next_zero_bit(Bitmap,
376 (Mapsize*8), 0);
David Updegraff7280da72007-06-11 10:41:07 -0500377 /*..falling..*/
378#endif
Luca Ceresolib640ed32011-05-17 00:03:39 +0000379
380 case STATE_RECV_WRQ:
David Updegraff7280da72007-06-11 10:41:07 -0500381 case STATE_DATA:
wdenkfe8c2802002-11-03 00:38:21 +0000382 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200383 s = (ushort *)pkt;
Simon Glass6a398d22011-10-24 18:00:07 +0000384 s[0] = htons(TFTP_ACK);
385 s[1] = htons(TftpBlock);
386 pkt = (uchar *)(s + 2);
387#ifdef CONFIG_CMD_TFTPPUT
388 if (TftpWriting) {
389 int toload = TftpBlkSize;
390 int loaded = load_block(TftpBlock, pkt, toload);
391
392 s[0] = htons(TFTP_DATA);
393 pkt += loaded;
394 TftpFinalBlock = (loaded < toload);
395 }
396#endif
wdenkfe8c2802002-11-03 00:38:21 +0000397 len = pkt - xp;
398 break;
399
400 case STATE_TOO_LARGE:
401 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200402 s = (ushort *)pkt;
403 *s++ = htons(TFTP_ERROR);
Simon Glass6a398d22011-10-24 18:00:07 +0000404 *s++ = htons(3);
405
Wolfgang Denk3135c542005-08-26 01:36:03 +0200406 pkt = (uchar *)s;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000407 strcpy((char *)pkt, "File too large");
wdenkfe8c2802002-11-03 00:38:21 +0000408 pkt += 14 /*strlen("File too large")*/ + 1;
409 len = pkt - xp;
410 break;
411
412 case STATE_BAD_MAGIC:
413 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200414 s = (ushort *)pkt;
415 *s++ = htons(TFTP_ERROR);
416 *s++ = htons(2);
417 pkt = (uchar *)s;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000418 strcpy((char *)pkt, "File has bad magic");
wdenkfe8c2802002-11-03 00:38:21 +0000419 pkt += 18 /*strlen("File has bad magic")*/ + 1;
420 len = pkt - xp;
421 break;
422 }
423
Luca Ceresoli5079c762011-05-17 00:03:37 +0000424 NetSendUDPPacket(NetServerEther, TftpRemoteIP, TftpRemotePort,
Luca Ceresolic42fc272011-05-14 05:49:56 +0000425 TftpOurPort, len);
wdenkfe8c2802002-11-03 00:38:21 +0000426}
427
Simon Glass2928cd82011-10-26 14:18:38 +0000428#ifdef CONFIG_CMD_TFTPPUT
Simon Glass6a398d22011-10-24 18:00:07 +0000429static void icmp_handler(unsigned type, unsigned code, unsigned dest,
430 IPaddr_t sip, unsigned src, uchar *pkt, unsigned len)
431{
432 if (type == ICMP_NOT_REACH && code == ICMP_NOT_REACH_PORT) {
433 /* Oh dear the other end has gone away */
434 restart("TFTP server died");
435 }
436}
Simon Glass2928cd82011-10-26 14:18:38 +0000437#endif
wdenkfe8c2802002-11-03 00:38:21 +0000438
439static void
Luca Ceresoli428ab362011-04-18 06:19:50 +0000440TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
441 unsigned len)
wdenkfe8c2802002-11-03 00:38:21 +0000442{
443 ushort proto;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200444 ushort *s;
Wolfgang Denk83e8e472007-08-30 14:42:15 +0200445 int i;
wdenkfe8c2802002-11-03 00:38:21 +0000446
447 if (dest != TftpOurPort) {
David Updegraff7280da72007-06-11 10:41:07 -0500448#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200449 if (Multicast
David Updegraff7280da72007-06-11 10:41:07 -0500450 && (!Mcast_port || (dest != Mcast_port)))
451#endif
Luca Ceresoli09b18232011-05-14 05:50:02 +0000452 return;
wdenkfe8c2802002-11-03 00:38:21 +0000453 }
Luca Ceresolib640ed32011-05-17 00:03:39 +0000454 if (TftpState != STATE_SEND_RRQ && src != TftpRemotePort &&
Simon Glass6a398d22011-10-24 18:00:07 +0000455 TftpState != STATE_RECV_WRQ && TftpState != STATE_SEND_WRQ)
wdenkfe8c2802002-11-03 00:38:21 +0000456 return;
wdenkfe8c2802002-11-03 00:38:21 +0000457
Luca Ceresoli35dfb182011-05-14 05:50:00 +0000458 if (len < 2)
wdenkfe8c2802002-11-03 00:38:21 +0000459 return;
wdenkfe8c2802002-11-03 00:38:21 +0000460 len -= 2;
461 /* warning: don't use increment (++) in ntohs() macros!! */
Wolfgang Denk3135c542005-08-26 01:36:03 +0200462 s = (ushort *)pkt;
463 proto = *s++;
464 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000465 switch (ntohs(proto)) {
466
467 case TFTP_RRQ:
Simon Glass6a398d22011-10-24 18:00:07 +0000468 break;
469
wdenkfe8c2802002-11-03 00:38:21 +0000470 case TFTP_ACK:
Simon Glass6a398d22011-10-24 18:00:07 +0000471#ifdef CONFIG_CMD_TFTPPUT
472 if (TftpWriting) {
473 if (TftpFinalBlock) {
474 tftp_complete();
475 } else {
476 /*
477 * Move to the next block. We want our block
478 * count to wrap just like the other end!
479 */
480 int block = ntohs(*s);
481 int ack_ok = (TftpBlock == block);
482
483 TftpBlock = (unsigned short)(block + 1);
484 update_block_number();
485 if (ack_ok)
486 TftpSend(); /* Send next data block */
487 }
488 }
489#endif
wdenkfe8c2802002-11-03 00:38:21 +0000490 break;
Simon Glass6a398d22011-10-24 18:00:07 +0000491
wdenkfe8c2802002-11-03 00:38:21 +0000492 default:
493 break;
494
Luca Ceresolib640ed32011-05-17 00:03:39 +0000495#ifdef CONFIG_CMD_TFTPSRV
496 case TFTP_WRQ:
497 debug("Got WRQ\n");
498 TftpRemoteIP = sip;
499 TftpRemotePort = src;
500 TftpOurPort = 1024 + (get_timer(0) % 3072);
Simon Glassbd8a0e02011-10-24 18:00:04 +0000501 new_transfer();
Luca Ceresolib640ed32011-05-17 00:03:39 +0000502 TftpSend(); /* Send ACK(0) */
503 break;
504#endif
505
wdenk7182b0f2003-10-06 21:55:32 +0000506 case TFTP_OACK:
Wolfgang Denk12730c72009-08-10 09:59:10 +0200507 debug("Got OACK: %s %s\n",
508 pkt,
509 pkt + strlen((char *)pkt) + 1);
wdenk7182b0f2003-10-06 21:55:32 +0000510 TftpState = STATE_OACK;
Luca Ceresoli5079c762011-05-17 00:03:37 +0000511 TftpRemotePort = src;
Wolfgang Denk8b70f342007-08-31 10:01:51 +0200512 /*
513 * Check for 'blksize' option.
514 * Careful: "i" is signed, "len" is unsigned, thus
515 * something like "len-8" may give a *huge* number
516 */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000517 for (i = 0; i+8 < len; i++) {
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000518 if (strcmp((char *)pkt+i, "blksize") == 0) {
Wolfgang Denk83e8e472007-08-30 14:42:15 +0200519 TftpBlkSize = (unsigned short)
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000520 simple_strtoul((char *)pkt+i+8, NULL,
Luca Ceresolif40538a2011-05-14 05:49:57 +0000521 10);
Robin Getz9e0a4d62009-07-23 03:01:03 -0400522 debug("Blocksize ack: %s, %d\n",
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000523 (char *)pkt+i+8, TftpBlkSize);
Wolfgang Denk83e8e472007-08-30 14:42:15 +0200524 }
Robin Getzb3b00ed2009-08-20 10:50:20 -0400525#ifdef CONFIG_TFTP_TSIZE
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000526 if (strcmp((char *)pkt+i, "tsize") == 0) {
527 TftpTsize = simple_strtoul((char *)pkt+i+6,
Luca Ceresolic42fc272011-05-14 05:49:56 +0000528 NULL, 10);
Robin Getzb3b00ed2009-08-20 10:50:20 -0400529 debug("size = %s, %d\n",
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000530 (char *)pkt+i+6, TftpTsize);
Robin Getzb3b00ed2009-08-20 10:50:20 -0400531 }
532#endif
David Updegraff7280da72007-06-11 10:41:07 -0500533 }
534#ifdef CONFIG_MCAST_TFTP
Luca Ceresolif40538a2011-05-14 05:49:57 +0000535 parse_multicast_oack((char *)pkt, len-1);
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200536 if ((Multicast) && (!MasterClient))
David Updegraff7280da72007-06-11 10:41:07 -0500537 TftpState = STATE_DATA; /* passive.. */
538 else
539#endif
Simon Glass6a398d22011-10-24 18:00:07 +0000540#ifdef CONFIG_CMD_TFTPPUT
541 if (TftpWriting) {
542 /* Get ready to send the first block */
543 TftpState = STATE_DATA;
544 TftpBlock++;
545 }
546#endif
547 TftpSend(); /* Send ACK or first data block */
wdenk7182b0f2003-10-06 21:55:32 +0000548 break;
wdenkfe8c2802002-11-03 00:38:21 +0000549 case TFTP_DATA:
550 if (len < 2)
551 return;
552 len -= 2;
553 TftpBlock = ntohs(*(ushort *)pkt);
wdenkef893942004-02-23 16:11:30 +0000554
Simon Glassbd8a0e02011-10-24 18:00:04 +0000555 update_block_number();
wdenkfe8c2802002-11-03 00:38:21 +0000556
Luca Ceresoli562dfe52011-05-17 00:03:38 +0000557 if (TftpState == STATE_SEND_RRQ)
Robin Getz9e0a4d62009-07-23 03:01:03 -0400558 debug("Server did not acknowledge timeout option!\n");
wdenk7182b0f2003-10-06 21:55:32 +0000559
Luca Ceresolib640ed32011-05-17 00:03:39 +0000560 if (TftpState == STATE_SEND_RRQ || TftpState == STATE_OACK ||
561 TftpState == STATE_RECV_WRQ) {
wdenkef893942004-02-23 16:11:30 +0000562 /* first block received */
wdenkfe8c2802002-11-03 00:38:21 +0000563 TftpState = STATE_DATA;
Luca Ceresoli5079c762011-05-17 00:03:37 +0000564 TftpRemotePort = src;
Simon Glassbd8a0e02011-10-24 18:00:04 +0000565 new_transfer();
wdenkfe8c2802002-11-03 00:38:21 +0000566
David Updegraff7280da72007-06-11 10:41:07 -0500567#ifdef CONFIG_MCAST_TFTP
568 if (Multicast) { /* start!=1 common if mcast */
569 TftpLastBlock = TftpBlock - 1;
570 } else
571#endif
wdenkfe8c2802002-11-03 00:38:21 +0000572 if (TftpBlock != 1) { /* Assertion */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000573 printf("\nTFTP error: "
574 "First block is not block 1 (%ld)\n"
575 "Starting again\n\n",
wdenkfe8c2802002-11-03 00:38:21 +0000576 TftpBlock);
Luca Ceresolif40538a2011-05-14 05:49:57 +0000577 NetStartAgain();
wdenkfe8c2802002-11-03 00:38:21 +0000578 break;
579 }
580 }
581
582 if (TftpBlock == TftpLastBlock) {
583 /*
584 * Same block again; ignore it.
585 */
586 break;
587 }
588
589 TftpLastBlock = TftpBlock;
Bartlomiej Sieka371cde72008-10-01 15:26:29 +0200590 TftpTimeoutCountMax = TIMEOUT_COUNT;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000591 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
wdenkfe8c2802002-11-03 00:38:21 +0000592
Luca Ceresolif40538a2011-05-14 05:49:57 +0000593 store_block(TftpBlock - 1, pkt + 2, len);
wdenkfe8c2802002-11-03 00:38:21 +0000594
595 /*
Luca Ceresoli2f3dc0a2011-05-17 00:03:41 +0000596 * Acknowledge the block just received, which will prompt
Luca Ceresoli5079c762011-05-17 00:03:37 +0000597 * the remote for the next one.
wdenkfe8c2802002-11-03 00:38:21 +0000598 */
David Updegraff7280da72007-06-11 10:41:07 -0500599#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200600 /* if I am the MasterClient, actively calculate what my next
601 * needed block is; else I'm passive; not ACKING
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100602 */
David Updegraff7280da72007-06-11 10:41:07 -0500603 if (Multicast) {
604 if (len < TftpBlkSize) {
605 TftpEndingBlock = TftpBlock;
606 } else if (MasterClient) {
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200607 TftpBlock = PrevBitmapHole =
David Updegraff7280da72007-06-11 10:41:07 -0500608 ext2_find_next_zero_bit(
609 Bitmap,
610 (Mapsize*8),
611 PrevBitmapHole);
612 if (TftpBlock > ((Mapsize*8) - 1)) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000613 printf("tftpfile too big\n");
David Updegraff7280da72007-06-11 10:41:07 -0500614 /* try to double it and retry */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000615 Mapsize <<= 1;
David Updegraff7280da72007-06-11 10:41:07 -0500616 mcast_cleanup();
Luca Ceresolif40538a2011-05-14 05:49:57 +0000617 NetStartAgain();
David Updegraff7280da72007-06-11 10:41:07 -0500618 return;
619 }
620 TftpLastBlock = TftpBlock;
621 }
622 }
623#endif
Luca Ceresolif40538a2011-05-14 05:49:57 +0000624 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000625
David Updegraff7280da72007-06-11 10:41:07 -0500626#ifdef CONFIG_MCAST_TFTP
627 if (Multicast) {
628 if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000629 puts("\nMulticast tftp done\n");
David Updegraff7280da72007-06-11 10:41:07 -0500630 mcast_cleanup();
631 NetState = NETLOOP_SUCCESS;
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200632 }
David Updegraff7280da72007-06-11 10:41:07 -0500633 }
634 else
635#endif
Simon Glassb437aad2011-10-24 18:00:03 +0000636 if (len < TftpBlkSize)
637 tftp_complete();
wdenkfe8c2802002-11-03 00:38:21 +0000638 break;
639
640 case TFTP_ERROR:
Luca Ceresolif40538a2011-05-14 05:49:57 +0000641 printf("\nTFTP error: '%s' (%d)\n",
642 pkt + 2, ntohs(*(ushort *)pkt));
Remy Bohmerb46293f2009-10-28 22:13:40 +0100643
644 switch (ntohs(*(ushort *)pkt)) {
645 case TFTP_ERR_FILE_NOT_FOUND:
646 case TFTP_ERR_ACCESS_DENIED:
647 puts("Not retrying...\n");
648 eth_halt();
649 NetState = NETLOOP_FAIL;
650 break;
651 case TFTP_ERR_UNDEFINED:
652 case TFTP_ERR_DISK_FULL:
653 case TFTP_ERR_UNEXPECTED_OPCODE:
654 case TFTP_ERR_UNKNOWN_TRANSFER_ID:
655 case TFTP_ERR_FILE_ALREADY_EXISTS:
656 default:
657 puts("Starting again\n\n");
David Updegraff7280da72007-06-11 10:41:07 -0500658#ifdef CONFIG_MCAST_TFTP
Remy Bohmerb46293f2009-10-28 22:13:40 +0100659 mcast_cleanup();
David Updegraff7280da72007-06-11 10:41:07 -0500660#endif
Remy Bohmerb46293f2009-10-28 22:13:40 +0100661 NetStartAgain();
662 break;
663 }
wdenkfe8c2802002-11-03 00:38:21 +0000664 break;
665 }
666}
667
668
669static void
Luca Ceresolif40538a2011-05-14 05:49:57 +0000670TftpTimeout(void)
wdenkfe8c2802002-11-03 00:38:21 +0000671{
Bartlomiej Sieka371cde72008-10-01 15:26:29 +0200672 if (++TftpTimeoutCount > TftpTimeoutCountMax) {
Simon Glassbd8a0e02011-10-24 18:00:04 +0000673 restart("Retry count exceeded");
wdenkfe8c2802002-11-03 00:38:21 +0000674 } else {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000675 puts("T ");
676 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
Luca Ceresolib640ed32011-05-17 00:03:39 +0000677 if (TftpState != STATE_RECV_WRQ)
678 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000679 }
680}
681
682
Simon Glassf59675a2011-10-24 18:00:05 +0000683void TftpStart(enum proto_t protocol)
wdenkfe8c2802002-11-03 00:38:21 +0000684{
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200685 char *ep; /* Environment pointer */
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200686
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100687 /*
688 * Allow the user to choose TFTP blocksize and timeout.
689 * TFTP protocol has a minimal timeout of 1 second.
690 */
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000691 ep = getenv("tftpblocksize");
692 if (ep != NULL)
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200693 TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100694
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000695 ep = getenv("tftptimeout");
696 if (ep != NULL)
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100697 TftpTimeoutMSecs = simple_strtol(ep, NULL, 10);
698
699 if (TftpTimeoutMSecs < 1000) {
700 printf("TFTP timeout (%ld ms) too low, "
701 "set minimum = 1000 ms\n",
702 TftpTimeoutMSecs);
703 TftpTimeoutMSecs = 1000;
704 }
705
706 debug("TFTP blocksize = %i, timeout = %ld ms\n",
707 TftpBlkSizeOption, TftpTimeoutMSecs);
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200708
Luca Ceresoli5079c762011-05-17 00:03:37 +0000709 TftpRemoteIP = NetServerIP;
wdenkfe8c2802002-11-03 00:38:21 +0000710 if (BootFile[0] == '\0') {
Matthias Weisser6cc63262011-12-03 03:29:44 +0000711 sprintf(default_filename, "%02X%02X%02X%02X.img",
Wolfgang Denkdec97322005-08-04 01:09:44 +0200712 NetOurIP & 0xFF,
713 (NetOurIP >> 8) & 0xFF,
714 (NetOurIP >> 16) & 0xFF,
Luca Ceresolif40538a2011-05-14 05:49:57 +0000715 (NetOurIP >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100716
717 strncpy(tftp_filename, default_filename, MAX_LEN);
718 tftp_filename[MAX_LEN-1] = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000719
Luca Ceresolif40538a2011-05-14 05:49:57 +0000720 printf("*** Warning: no boot file name; using '%s'\n",
wdenkfe8c2802002-11-03 00:38:21 +0000721 tftp_filename);
722 } else {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000723 char *p = strchr(BootFile, ':');
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100724
725 if (p == NULL) {
726 strncpy(tftp_filename, BootFile, MAX_LEN);
727 tftp_filename[MAX_LEN-1] = 0;
728 } else {
Luca Ceresoli5079c762011-05-17 00:03:37 +0000729 TftpRemoteIP = string_to_ip(BootFile);
Peter Tyser5e58efa2008-12-01 16:29:38 -0600730 strncpy(tftp_filename, p + 1, MAX_LEN);
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100731 tftp_filename[MAX_LEN-1] = 0;
732 }
wdenkfe8c2802002-11-03 00:38:21 +0000733 }
734
Luca Ceresolif40538a2011-05-14 05:49:57 +0000735 printf("Using %s device\n", eth_get_name());
Simon Glass6a398d22011-10-24 18:00:07 +0000736 printf("TFTP %s server %pI4; our IP address is %pI4",
Simon Glass4abd0e22011-10-27 06:24:29 +0000737#ifdef CONFIG_CMD_TFTPPUT
738 protocol == TFTPPUT ? "to" : "from",
739#else
740 "from",
741#endif
742 &TftpRemoteIP, &NetOurIP);
wdenkfe8c2802002-11-03 00:38:21 +0000743
744 /* Check if we need to send across this subnet */
745 if (NetOurGatewayIP && NetOurSubnetMask) {
Luca Ceresoli09b18232011-05-14 05:50:02 +0000746 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
Luca Ceresoli5079c762011-05-17 00:03:37 +0000747 IPaddr_t RemoteNet = TftpRemoteIP & NetOurSubnetMask;
wdenkfe8c2802002-11-03 00:38:21 +0000748
Luca Ceresoli5079c762011-05-17 00:03:37 +0000749 if (OurNet != RemoteNet)
Luca Ceresoli09b18232011-05-14 05:50:02 +0000750 printf("; sending through gateway %pI4",
751 &NetOurGatewayIP);
wdenkfe8c2802002-11-03 00:38:21 +0000752 }
Luca Ceresolif40538a2011-05-14 05:49:57 +0000753 putc('\n');
wdenkfe8c2802002-11-03 00:38:21 +0000754
Luca Ceresolif40538a2011-05-14 05:49:57 +0000755 printf("Filename '%s'.", tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000756
757 if (NetBootFileSize) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000758 printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
759 print_size(NetBootFileSize<<9, "");
wdenkfe8c2802002-11-03 00:38:21 +0000760 }
761
Luca Ceresolif40538a2011-05-14 05:49:57 +0000762 putc('\n');
Simon Glass6a398d22011-10-24 18:00:07 +0000763#ifdef CONFIG_CMD_TFTPPUT
764 TftpWriting = (protocol == TFTPPUT);
765 if (TftpWriting) {
766 printf("Save address: 0x%lx\n", save_addr);
767 printf("Save size: 0x%lx\n", save_size);
768 NetBootFileXferSize = save_size;
769 puts("Saving: *\b");
770 TftpState = STATE_SEND_WRQ;
771 new_transfer();
772 } else
773#endif
774 {
775 printf("Load address: 0x%lx\n", load_addr);
776 puts("Loading: *\b");
777 TftpState = STATE_SEND_RRQ;
778 }
wdenkfe8c2802002-11-03 00:38:21 +0000779
Bartlomiej Sieka371cde72008-10-01 15:26:29 +0200780 TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
781
Luca Ceresolif40538a2011-05-14 05:49:57 +0000782 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
783 NetSetHandler(TftpHandler);
Simon Glass2928cd82011-10-26 14:18:38 +0000784#ifdef CONFIG_CMD_TFTPPUT
Simon Glass6a398d22011-10-24 18:00:07 +0000785 net_set_icmp_handler(icmp_handler);
Simon Glass2928cd82011-10-26 14:18:38 +0000786#endif
Luca Ceresoli5079c762011-05-17 00:03:37 +0000787 TftpRemotePort = WELL_KNOWN_PORT;
wdenkfe8c2802002-11-03 00:38:21 +0000788 TftpTimeoutCount = 0;
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200789 /* Use a pseudo-random port unless a specific port is set */
wdenkfe8c2802002-11-03 00:38:21 +0000790 TftpOurPort = 1024 + (get_timer(0) % 3072);
David Updegraff7280da72007-06-11 10:41:07 -0500791
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200792#ifdef CONFIG_TFTP_PORT
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000793 ep = getenv("tftpdstp");
Luca Ceresoli35dfb182011-05-14 05:50:00 +0000794 if (ep != NULL)
Luca Ceresoli5079c762011-05-17 00:03:37 +0000795 TftpRemotePort = simple_strtol(ep, NULL, 10);
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000796 ep = getenv("tftpsrcp");
Luca Ceresoli35dfb182011-05-14 05:50:00 +0000797 if (ep != NULL)
Luca Ceresolif40538a2011-05-14 05:49:57 +0000798 TftpOurPort = simple_strtol(ep, NULL, 10);
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200799#endif
wdenk7182b0f2003-10-06 21:55:32 +0000800 TftpBlock = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000801
wdenke6466f62003-06-05 19:27:42 +0000802 /* zero out server ether in case the server ip has changed */
803 memset(NetServerEther, 0, 6);
David Updegraff7280da72007-06-11 10:41:07 -0500804 /* Revert TftpBlkSize to dflt */
805 TftpBlkSize = TFTP_BLOCK_SIZE;
806#ifdef CONFIG_MCAST_TFTP
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100807 mcast_cleanup();
David Updegraff7280da72007-06-11 10:41:07 -0500808#endif
Robin Getzb3b00ed2009-08-20 10:50:20 -0400809#ifdef CONFIG_TFTP_TSIZE
810 TftpTsize = 0;
811 TftpNumchars = 0;
812#endif
wdenke6466f62003-06-05 19:27:42 +0000813
Luca Ceresolif40538a2011-05-14 05:49:57 +0000814 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000815}
Luca Ceresolib640ed32011-05-17 00:03:39 +0000816
817#ifdef CONFIG_CMD_TFTPSRV
818void
819TftpStartServer(void)
820{
821 tftp_filename[0] = 0;
822
Luca Ceresolib640ed32011-05-17 00:03:39 +0000823 printf("Using %s device\n", eth_get_name());
Luca Ceresolib640ed32011-05-17 00:03:39 +0000824 printf("Listening for TFTP transfer on %pI4\n", &NetOurIP);
825 printf("Load address: 0x%lx\n", load_addr);
826
827 puts("Loading: *\b");
828
829 TftpTimeoutCountMax = TIMEOUT_COUNT;
830 TftpTimeoutCount = 0;
831 TftpTimeoutMSecs = TIMEOUT;
832 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
833
834 /* Revert TftpBlkSize to dflt */
835 TftpBlkSize = TFTP_BLOCK_SIZE;
836 TftpBlock = 0;
837 TftpOurPort = WELL_KNOWN_PORT;
838
839#ifdef CONFIG_TFTP_TSIZE
840 TftpTsize = 0;
841 TftpNumchars = 0;
842#endif
843
844 TftpState = STATE_RECV_WRQ;
845 NetSetHandler(TftpHandler);
846}
847#endif /* CONFIG_CMD_TFTPSRV */
wdenkfe8c2802002-11-03 00:38:21 +0000848
David Updegraff7280da72007-06-11 10:41:07 -0500849#ifdef CONFIG_MCAST_TFTP
850/* Credits: atftp project.
851 */
852
853/* pick up BcastAddr, Port, and whether I am [now] the master-client. *
854 * Frame:
855 * +-------+-----------+---+-------~~-------+---+
856 * | opc | multicast | 0 | addr, port, mc | 0 |
857 * +-------+-----------+---+-------~~-------+---+
858 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
859 * I am the new master-client so must send ACKs to DataBlocks. If I am not
860 * master-client, I'm a passive client, gathering what DataBlocks I may and
861 * making note of which ones I got in my bitmask.
862 * In theory, I never go from master->passive..
863 * .. this comes in with pkt already pointing just past opc
864 */
865static void parse_multicast_oack(char *pkt, int len)
866{
Luca Ceresolif40538a2011-05-14 05:49:57 +0000867 int i;
868 IPaddr_t addr;
869 char *mc_adr, *port, *mc;
David Updegraff7280da72007-06-11 10:41:07 -0500870
Luca Ceresolif40538a2011-05-14 05:49:57 +0000871 mc_adr = port = mc = NULL;
David Updegraff7280da72007-06-11 10:41:07 -0500872 /* march along looking for 'multicast\0', which has to start at least
873 * 14 bytes back from the end.
874 */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000875 for (i = 0; i < len-14; i++)
876 if (strcmp(pkt+i, "multicast") == 0)
David Updegraff7280da72007-06-11 10:41:07 -0500877 break;
878 if (i >= (len-14)) /* non-Multicast OACK, ign. */
879 return;
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200880
Luca Ceresolif40538a2011-05-14 05:49:57 +0000881 i += 10; /* strlen multicast */
David Updegraff7280da72007-06-11 10:41:07 -0500882 mc_adr = pkt+i;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000883 for (; i < len; i++) {
David Updegraff7280da72007-06-11 10:41:07 -0500884 if (*(pkt+i) == ',') {
885 *(pkt+i) = '\0';
886 if (port) {
887 mc = pkt+i+1;
888 break;
889 } else {
890 port = pkt+i+1;
891 }
892 }
893 }
Luca Ceresoli6072fcd2011-05-14 05:50:01 +0000894 if (!port || !mc_adr || !mc)
895 return;
David Updegraff7280da72007-06-11 10:41:07 -0500896 if (Multicast && MasterClient) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000897 printf("I got a OACK as master Client, WRONG!\n");
David Updegraff7280da72007-06-11 10:41:07 -0500898 return;
899 }
900 /* ..I now accept packets destined for this MCAST addr, port */
901 if (!Multicast) {
902 if (Bitmap) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000903 printf("Internal failure! no mcast.\n");
David Updegraff7280da72007-06-11 10:41:07 -0500904 free(Bitmap);
Luca Ceresolif40538a2011-05-14 05:49:57 +0000905 Bitmap = NULL;
906 ProhibitMcast = 1;
David Updegraff7280da72007-06-11 10:41:07 -0500907 return ;
908 }
909 /* I malloc instead of pre-declare; so that if the file ends
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200910 * up being too big for this bitmap I can retry
911 */
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000912 Bitmap = malloc(Mapsize);
913 if (!Bitmap) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000914 printf("No Bitmap, no multicast. Sorry.\n");
915 ProhibitMcast = 1;
David Updegraff7280da72007-06-11 10:41:07 -0500916 return;
917 }
Luca Ceresolif40538a2011-05-14 05:49:57 +0000918 memset(Bitmap, 0, Mapsize);
David Updegraff7280da72007-06-11 10:41:07 -0500919 PrevBitmapHole = 0;
920 Multicast = 1;
921 }
922 addr = string_to_ip(mc_adr);
923 if (Mcast_addr != addr) {
924 if (Mcast_addr)
925 eth_mcast_join(Mcast_addr, 0);
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000926 Mcast_addr = addr;
927 if (eth_mcast_join(Mcast_addr, 1)) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000928 printf("Fail to set mcast, revert to TFTP\n");
929 ProhibitMcast = 1;
David Updegraff7280da72007-06-11 10:41:07 -0500930 mcast_cleanup();
931 NetStartAgain();
932 }
933 }
Luca Ceresolif40538a2011-05-14 05:49:57 +0000934 MasterClient = (unsigned char)simple_strtoul((char *)mc, NULL, 10);
935 Mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
936 printf("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
David Updegraff7280da72007-06-11 10:41:07 -0500937 return;
938}
939
940#endif /* Multicast TFTP */