blob: 70daed033c906da94c9bf3fa1ba9f0c8d2e3c17e [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
wdenkef893942004-02-23 16:11:30 +000084
Luca Ceresoli562dfe52011-05-17 00:03:38 +000085#define STATE_SEND_RRQ 1
wdenkfe8c2802002-11-03 00:38:21 +000086#define STATE_DATA 2
87#define STATE_TOO_LARGE 3
88#define STATE_BAD_MAGIC 4
wdenk7182b0f2003-10-06 21:55:32 +000089#define STATE_OACK 5
Luca Ceresolib640ed32011-05-17 00:03:39 +000090#define STATE_RECV_WRQ 6
wdenkfe8c2802002-11-03 00:38:21 +000091
Luca Ceresolic42fc272011-05-14 05:49:56 +000092/* default TFTP block size */
93#define TFTP_BLOCK_SIZE 512
94/* sequence number is 16 bit */
95#define TFTP_SEQUENCE_SIZE ((ulong)(1<<16))
wdenkef893942004-02-23 16:11:30 +000096
wdenkfe8c2802002-11-03 00:38:21 +000097#define DEFAULT_NAME_LEN (8 + 4 + 1)
98static char default_filename[DEFAULT_NAME_LEN];
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +010099
100#ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
101#define MAX_LEN 128
102#else
103#define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
104#endif
105
106static char tftp_filename[MAX_LEN];
wdenkfe8c2802002-11-03 00:38:21 +0000107
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200108#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200109extern flash_info_t flash_info[];
wdenkfe8c2802002-11-03 00:38:21 +0000110#endif
111
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200112/* 512 is poor choice for ethernet, MTU is typically 1500.
113 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
David Updegraff7280da72007-06-11 10:41:07 -0500114 * almost-MTU block sizes. At least try... fall back to 512 if need be.
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200115 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
David Updegraff7280da72007-06-11 10:41:07 -0500116 */
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200117#ifdef CONFIG_TFTP_BLOCKSIZE
118#define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
119#else
David Updegraff7280da72007-06-11 10:41:07 -0500120#define TFTP_MTU_BLOCKSIZE 1468
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200121#endif
122
Luca Ceresolif40538a2011-05-14 05:49:57 +0000123static unsigned short TftpBlkSize = TFTP_BLOCK_SIZE;
124static unsigned short TftpBlkSizeOption = TFTP_MTU_BLOCKSIZE;
David Updegraff7280da72007-06-11 10:41:07 -0500125
126#ifdef CONFIG_MCAST_TFTP
127#include <malloc.h>
128#define MTFTP_BITMAPSIZE 0x1000
129static unsigned *Bitmap;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000130static int PrevBitmapHole, Mapsize = MTFTP_BITMAPSIZE;
Luca Ceresoli4e7a02b2011-05-14 05:50:03 +0000131static uchar ProhibitMcast, MasterClient;
132static uchar Multicast;
David Updegraff7280da72007-06-11 10:41:07 -0500133extern IPaddr_t Mcast_addr;
134static int Mcast_port;
135static ulong TftpEndingBlock; /* can get 'last' block before done..*/
136
Luca Ceresolif40538a2011-05-14 05:49:57 +0000137static void parse_multicast_oack(char *pkt, int len);
David Updegraff7280da72007-06-11 10:41:07 -0500138
139static void
140mcast_cleanup(void)
141{
Luca Ceresoli6072fcd2011-05-14 05:50:01 +0000142 if (Mcast_addr)
143 eth_mcast_join(Mcast_addr, 0);
144 if (Bitmap)
145 free(Bitmap);
Luca Ceresolif40538a2011-05-14 05:49:57 +0000146 Bitmap = NULL;
David Updegraff7280da72007-06-11 10:41:07 -0500147 Mcast_addr = Multicast = Mcast_port = 0;
148 TftpEndingBlock = -1;
149}
150
151#endif /* CONFIG_MCAST_TFTP */
152
wdenkfe8c2802002-11-03 00:38:21 +0000153static __inline__ void
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000154store_block(unsigned block, uchar *src, unsigned len)
wdenkfe8c2802002-11-03 00:38:21 +0000155{
David Updegraff7280da72007-06-11 10:41:07 -0500156 ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
wdenkef893942004-02-23 16:11:30 +0000157 ulong newsize = offset + len;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200158#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
wdenkfe8c2802002-11-03 00:38:21 +0000159 int i, rc = 0;
160
Luca Ceresolif40538a2011-05-14 05:49:57 +0000161 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenkfe8c2802002-11-03 00:38:21 +0000162 /* start address in flash? */
Jochen Friedrichd31a59b2008-09-02 11:24:59 +0200163 if (flash_info[i].flash_id == FLASH_UNKNOWN)
164 continue;
wdenkfe8c2802002-11-03 00:38:21 +0000165 if (load_addr + offset >= flash_info[i].start[0]) {
166 rc = 1;
167 break;
168 }
169 }
170
171 if (rc) { /* Flash is destination for this packet */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000172 rc = flash_write((char *)src, (ulong)(load_addr+offset), len);
wdenkfe8c2802002-11-03 00:38:21 +0000173 if (rc) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000174 flash_perror(rc);
wdenkfe8c2802002-11-03 00:38:21 +0000175 NetState = NETLOOP_FAIL;
176 return;
177 }
178 }
179 else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200180#endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000181 {
182 (void)memcpy((void *)(load_addr + offset), src, len);
183 }
David Updegraff7280da72007-06-11 10:41:07 -0500184#ifdef CONFIG_MCAST_TFTP
185 if (Multicast)
186 ext2_set_bit(block, Bitmap);
187#endif
wdenkfe8c2802002-11-03 00:38:21 +0000188
189 if (NetBootFileXferSize < newsize)
190 NetBootFileXferSize = newsize;
191}
192
Luca Ceresolif40538a2011-05-14 05:49:57 +0000193static void TftpSend(void);
194static void TftpTimeout(void);
wdenkfe8c2802002-11-03 00:38:21 +0000195
196/**********************************************************************/
197
Simon Glassb437aad2011-10-24 18:00:03 +0000198static void show_block_marker(void)
199{
200#ifdef CONFIG_TFTP_TSIZE
201 if (TftpTsize) {
202 ulong pos = TftpBlock * TftpBlkSize + TftpBlockWrapOffset;
203
204 while (TftpNumchars < pos * 50 / TftpTsize) {
205 putc('#');
206 TftpNumchars++;
207 }
208 }
209#endif
210 else {
211 if (((TftpBlock - 1) % 10) == 0)
212 putc('#');
213 else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0)
214 puts("\n\t ");
215 }
216}
217
218/* The TFTP get or put is complete */
219static void tftp_complete(void)
220{
221#ifdef CONFIG_TFTP_TSIZE
222 /* Print hash marks for the last packet received */
223 while (TftpTsize && TftpNumchars < 49) {
224 putc('#');
225 TftpNumchars++;
226 }
227#endif
228 puts("\ndone\n");
229 NetState = NETLOOP_SUCCESS;
230}
231
wdenkfe8c2802002-11-03 00:38:21 +0000232static void
Luca Ceresolif40538a2011-05-14 05:49:57 +0000233TftpSend(void)
wdenkfe8c2802002-11-03 00:38:21 +0000234{
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000235 volatile uchar *pkt;
236 volatile uchar *xp;
237 int len = 0;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200238 volatile ushort *s;
wdenkfe8c2802002-11-03 00:38:21 +0000239
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200240#ifdef CONFIG_MCAST_TFTP
David Updegraff7280da72007-06-11 10:41:07 -0500241 /* Multicast TFTP.. non-MasterClients do not ACK data. */
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200242 if (Multicast
243 && (TftpState == STATE_DATA)
244 && (MasterClient == 0))
David Updegraff7280da72007-06-11 10:41:07 -0500245 return;
246#endif
wdenkfe8c2802002-11-03 00:38:21 +0000247 /*
248 * We will always be sending some sort of packet, so
249 * cobble together the packet headers now.
250 */
wdenk145d2c12004-04-15 21:48:45 +0000251 pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
wdenkfe8c2802002-11-03 00:38:21 +0000252
253 switch (TftpState) {
254
Luca Ceresoli562dfe52011-05-17 00:03:38 +0000255 case STATE_SEND_RRQ:
wdenkfe8c2802002-11-03 00:38:21 +0000256 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200257 s = (ushort *)pkt;
258 *s++ = htons(TFTP_RRQ);
259 pkt = (uchar *)s;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000260 strcpy((char *)pkt, tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000261 pkt += strlen(tftp_filename) + 1;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000262 strcpy((char *)pkt, "octet");
wdenkfe8c2802002-11-03 00:38:21 +0000263 pkt += 5 /*strlen("octet")*/ + 1;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000264 strcpy((char *)pkt, "timeout");
wdenk7182b0f2003-10-06 21:55:32 +0000265 pkt += 7 /*strlen("timeout")*/ + 1;
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100266 sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000);
Robin Getz9e0a4d62009-07-23 03:01:03 -0400267 debug("send option \"timeout %s\"\n", (char *)pkt);
wdenk7182b0f2003-10-06 21:55:32 +0000268 pkt += strlen((char *)pkt) + 1;
Robin Getzb3b00ed2009-08-20 10:50:20 -0400269#ifdef CONFIG_TFTP_TSIZE
270 memcpy((char *)pkt, "tsize\0000\0", 8);
271 pkt += 8;
272#endif
David Updegraff7280da72007-06-11 10:41:07 -0500273 /* try for more effic. blk size */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000274 pkt += sprintf((char *)pkt, "blksize%c%d%c",
275 0, TftpBlkSizeOption, 0);
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200276#ifdef CONFIG_MCAST_TFTP
David Updegraff7280da72007-06-11 10:41:07 -0500277 /* Check all preconditions before even trying the option */
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200278 if (!ProhibitMcast
Luca Ceresolif40538a2011-05-14 05:49:57 +0000279 && (Bitmap = malloc(Mapsize))
David Updegraff7280da72007-06-11 10:41:07 -0500280 && eth_get_dev()->mcast) {
281 free(Bitmap);
Luca Ceresolif40538a2011-05-14 05:49:57 +0000282 Bitmap = NULL;
283 pkt += sprintf((char *)pkt, "multicast%c%c", 0, 0);
David Updegraff7280da72007-06-11 10:41:07 -0500284 }
285#endif /* CONFIG_MCAST_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000286 len = pkt - xp;
287 break;
288
wdenk7182b0f2003-10-06 21:55:32 +0000289 case STATE_OACK:
David Updegraff7280da72007-06-11 10:41:07 -0500290#ifdef CONFIG_MCAST_TFTP
291 /* My turn! Start at where I need blocks I missed.*/
292 if (Multicast)
Luca Ceresolif40538a2011-05-14 05:49:57 +0000293 TftpBlock = ext2_find_next_zero_bit(Bitmap,
294 (Mapsize*8), 0);
David Updegraff7280da72007-06-11 10:41:07 -0500295 /*..falling..*/
296#endif
Luca Ceresolib640ed32011-05-17 00:03:39 +0000297
298 case STATE_RECV_WRQ:
David Updegraff7280da72007-06-11 10:41:07 -0500299 case STATE_DATA:
wdenkfe8c2802002-11-03 00:38:21 +0000300 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200301 s = (ushort *)pkt;
302 *s++ = htons(TFTP_ACK);
303 *s++ = htons(TftpBlock);
304 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000305 len = pkt - xp;
306 break;
307
308 case STATE_TOO_LARGE:
309 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200310 s = (ushort *)pkt;
311 *s++ = htons(TFTP_ERROR);
312 *s++ = htons(3);
313 pkt = (uchar *)s;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000314 strcpy((char *)pkt, "File too large");
wdenkfe8c2802002-11-03 00:38:21 +0000315 pkt += 14 /*strlen("File too large")*/ + 1;
316 len = pkt - xp;
317 break;
318
319 case STATE_BAD_MAGIC:
320 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200321 s = (ushort *)pkt;
322 *s++ = htons(TFTP_ERROR);
323 *s++ = htons(2);
324 pkt = (uchar *)s;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000325 strcpy((char *)pkt, "File has bad magic");
wdenkfe8c2802002-11-03 00:38:21 +0000326 pkt += 18 /*strlen("File has bad magic")*/ + 1;
327 len = pkt - xp;
328 break;
329 }
330
Luca Ceresoli5079c762011-05-17 00:03:37 +0000331 NetSendUDPPacket(NetServerEther, TftpRemoteIP, TftpRemotePort,
Luca Ceresolic42fc272011-05-14 05:49:56 +0000332 TftpOurPort, len);
wdenkfe8c2802002-11-03 00:38:21 +0000333}
334
335
336static void
Luca Ceresoli428ab362011-04-18 06:19:50 +0000337TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
338 unsigned len)
wdenkfe8c2802002-11-03 00:38:21 +0000339{
340 ushort proto;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200341 ushort *s;
Wolfgang Denk83e8e472007-08-30 14:42:15 +0200342 int i;
wdenkfe8c2802002-11-03 00:38:21 +0000343
344 if (dest != TftpOurPort) {
David Updegraff7280da72007-06-11 10:41:07 -0500345#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200346 if (Multicast
David Updegraff7280da72007-06-11 10:41:07 -0500347 && (!Mcast_port || (dest != Mcast_port)))
348#endif
Luca Ceresoli09b18232011-05-14 05:50:02 +0000349 return;
wdenkfe8c2802002-11-03 00:38:21 +0000350 }
Luca Ceresolib640ed32011-05-17 00:03:39 +0000351 if (TftpState != STATE_SEND_RRQ && src != TftpRemotePort &&
352 TftpState != STATE_RECV_WRQ)
wdenkfe8c2802002-11-03 00:38:21 +0000353 return;
wdenkfe8c2802002-11-03 00:38:21 +0000354
Luca Ceresoli35dfb182011-05-14 05:50:00 +0000355 if (len < 2)
wdenkfe8c2802002-11-03 00:38:21 +0000356 return;
wdenkfe8c2802002-11-03 00:38:21 +0000357 len -= 2;
358 /* warning: don't use increment (++) in ntohs() macros!! */
Wolfgang Denk3135c542005-08-26 01:36:03 +0200359 s = (ushort *)pkt;
360 proto = *s++;
361 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000362 switch (ntohs(proto)) {
363
364 case TFTP_RRQ:
wdenkfe8c2802002-11-03 00:38:21 +0000365 case TFTP_ACK:
366 break;
367 default:
368 break;
369
Luca Ceresolib640ed32011-05-17 00:03:39 +0000370#ifdef CONFIG_CMD_TFTPSRV
371 case TFTP_WRQ:
372 debug("Got WRQ\n");
373 TftpRemoteIP = sip;
374 TftpRemotePort = src;
375 TftpOurPort = 1024 + (get_timer(0) % 3072);
376 TftpLastBlock = 0;
377 TftpBlockWrap = 0;
378 TftpBlockWrapOffset = 0;
379 TftpSend(); /* Send ACK(0) */
380 break;
381#endif
382
wdenk7182b0f2003-10-06 21:55:32 +0000383 case TFTP_OACK:
Wolfgang Denk12730c72009-08-10 09:59:10 +0200384 debug("Got OACK: %s %s\n",
385 pkt,
386 pkt + strlen((char *)pkt) + 1);
wdenk7182b0f2003-10-06 21:55:32 +0000387 TftpState = STATE_OACK;
Luca Ceresoli5079c762011-05-17 00:03:37 +0000388 TftpRemotePort = src;
Wolfgang Denk8b70f342007-08-31 10:01:51 +0200389 /*
390 * Check for 'blksize' option.
391 * Careful: "i" is signed, "len" is unsigned, thus
392 * something like "len-8" may give a *huge* number
393 */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000394 for (i = 0; i+8 < len; i++) {
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000395 if (strcmp((char *)pkt+i, "blksize") == 0) {
Wolfgang Denk83e8e472007-08-30 14:42:15 +0200396 TftpBlkSize = (unsigned short)
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000397 simple_strtoul((char *)pkt+i+8, NULL,
Luca Ceresolif40538a2011-05-14 05:49:57 +0000398 10);
Robin Getz9e0a4d62009-07-23 03:01:03 -0400399 debug("Blocksize ack: %s, %d\n",
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000400 (char *)pkt+i+8, TftpBlkSize);
Wolfgang Denk83e8e472007-08-30 14:42:15 +0200401 }
Robin Getzb3b00ed2009-08-20 10:50:20 -0400402#ifdef CONFIG_TFTP_TSIZE
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000403 if (strcmp((char *)pkt+i, "tsize") == 0) {
404 TftpTsize = simple_strtoul((char *)pkt+i+6,
Luca Ceresolic42fc272011-05-14 05:49:56 +0000405 NULL, 10);
Robin Getzb3b00ed2009-08-20 10:50:20 -0400406 debug("size = %s, %d\n",
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000407 (char *)pkt+i+6, TftpTsize);
Robin Getzb3b00ed2009-08-20 10:50:20 -0400408 }
409#endif
David Updegraff7280da72007-06-11 10:41:07 -0500410 }
411#ifdef CONFIG_MCAST_TFTP
Luca Ceresolif40538a2011-05-14 05:49:57 +0000412 parse_multicast_oack((char *)pkt, len-1);
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200413 if ((Multicast) && (!MasterClient))
David Updegraff7280da72007-06-11 10:41:07 -0500414 TftpState = STATE_DATA; /* passive.. */
415 else
416#endif
Luca Ceresolif40538a2011-05-14 05:49:57 +0000417 TftpSend(); /* Send ACK */
wdenk7182b0f2003-10-06 21:55:32 +0000418 break;
wdenkfe8c2802002-11-03 00:38:21 +0000419 case TFTP_DATA:
420 if (len < 2)
421 return;
422 len -= 2;
423 TftpBlock = ntohs(*(ushort *)pkt);
wdenkef893942004-02-23 16:11:30 +0000424
425 /*
wdenkf70cbb22004-02-23 20:48:38 +0000426 * RFC1350 specifies that the first data packet will
427 * have sequence number 1. If we receive a sequence
428 * number of 0 this means that there was a wrap
429 * around of the (16 bit) counter.
wdenkef893942004-02-23 16:11:30 +0000430 */
431 if (TftpBlock == 0) {
432 TftpBlockWrap++;
Luca Ceresolic42fc272011-05-14 05:49:56 +0000433 TftpBlockWrapOffset +=
434 TftpBlkSize * TFTP_SEQUENCE_SIZE;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000435 printf("\n\t %lu MB received\n\t ",
Luca Ceresolic42fc272011-05-14 05:49:56 +0000436 TftpBlockWrapOffset>>20);
Simon Glassb437aad2011-10-24 18:00:03 +0000437 } else {
438 show_block_marker();
wdenkfe8c2802002-11-03 00:38:21 +0000439 }
440
Luca Ceresoli562dfe52011-05-17 00:03:38 +0000441 if (TftpState == STATE_SEND_RRQ)
Robin Getz9e0a4d62009-07-23 03:01:03 -0400442 debug("Server did not acknowledge timeout option!\n");
wdenk7182b0f2003-10-06 21:55:32 +0000443
Luca Ceresolib640ed32011-05-17 00:03:39 +0000444 if (TftpState == STATE_SEND_RRQ || TftpState == STATE_OACK ||
445 TftpState == STATE_RECV_WRQ) {
wdenkef893942004-02-23 16:11:30 +0000446 /* first block received */
wdenkfe8c2802002-11-03 00:38:21 +0000447 TftpState = STATE_DATA;
Luca Ceresoli5079c762011-05-17 00:03:37 +0000448 TftpRemotePort = src;
wdenkfe8c2802002-11-03 00:38:21 +0000449 TftpLastBlock = 0;
wdenkef893942004-02-23 16:11:30 +0000450 TftpBlockWrap = 0;
451 TftpBlockWrapOffset = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000452
David Updegraff7280da72007-06-11 10:41:07 -0500453#ifdef CONFIG_MCAST_TFTP
454 if (Multicast) { /* start!=1 common if mcast */
455 TftpLastBlock = TftpBlock - 1;
456 } else
457#endif
wdenkfe8c2802002-11-03 00:38:21 +0000458 if (TftpBlock != 1) { /* Assertion */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000459 printf("\nTFTP error: "
460 "First block is not block 1 (%ld)\n"
461 "Starting again\n\n",
wdenkfe8c2802002-11-03 00:38:21 +0000462 TftpBlock);
Luca Ceresolif40538a2011-05-14 05:49:57 +0000463 NetStartAgain();
wdenkfe8c2802002-11-03 00:38:21 +0000464 break;
465 }
466 }
467
468 if (TftpBlock == TftpLastBlock) {
469 /*
470 * Same block again; ignore it.
471 */
472 break;
473 }
474
475 TftpLastBlock = TftpBlock;
Bartlomiej Sieka371cde72008-10-01 15:26:29 +0200476 TftpTimeoutCountMax = TIMEOUT_COUNT;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000477 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
wdenkfe8c2802002-11-03 00:38:21 +0000478
Luca Ceresolif40538a2011-05-14 05:49:57 +0000479 store_block(TftpBlock - 1, pkt + 2, len);
wdenkfe8c2802002-11-03 00:38:21 +0000480
481 /*
Luca Ceresoli2f3dc0a2011-05-17 00:03:41 +0000482 * Acknowledge the block just received, which will prompt
Luca Ceresoli5079c762011-05-17 00:03:37 +0000483 * the remote for the next one.
wdenkfe8c2802002-11-03 00:38:21 +0000484 */
David Updegraff7280da72007-06-11 10:41:07 -0500485#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200486 /* if I am the MasterClient, actively calculate what my next
487 * needed block is; else I'm passive; not ACKING
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100488 */
David Updegraff7280da72007-06-11 10:41:07 -0500489 if (Multicast) {
490 if (len < TftpBlkSize) {
491 TftpEndingBlock = TftpBlock;
492 } else if (MasterClient) {
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200493 TftpBlock = PrevBitmapHole =
David Updegraff7280da72007-06-11 10:41:07 -0500494 ext2_find_next_zero_bit(
495 Bitmap,
496 (Mapsize*8),
497 PrevBitmapHole);
498 if (TftpBlock > ((Mapsize*8) - 1)) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000499 printf("tftpfile too big\n");
David Updegraff7280da72007-06-11 10:41:07 -0500500 /* try to double it and retry */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000501 Mapsize <<= 1;
David Updegraff7280da72007-06-11 10:41:07 -0500502 mcast_cleanup();
Luca Ceresolif40538a2011-05-14 05:49:57 +0000503 NetStartAgain();
David Updegraff7280da72007-06-11 10:41:07 -0500504 return;
505 }
506 TftpLastBlock = TftpBlock;
507 }
508 }
509#endif
Luca Ceresolif40538a2011-05-14 05:49:57 +0000510 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000511
David Updegraff7280da72007-06-11 10:41:07 -0500512#ifdef CONFIG_MCAST_TFTP
513 if (Multicast) {
514 if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000515 puts("\nMulticast tftp done\n");
David Updegraff7280da72007-06-11 10:41:07 -0500516 mcast_cleanup();
517 NetState = NETLOOP_SUCCESS;
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200518 }
David Updegraff7280da72007-06-11 10:41:07 -0500519 }
520 else
521#endif
Simon Glassb437aad2011-10-24 18:00:03 +0000522 if (len < TftpBlkSize)
523 tftp_complete();
wdenkfe8c2802002-11-03 00:38:21 +0000524 break;
525
526 case TFTP_ERROR:
Luca Ceresolif40538a2011-05-14 05:49:57 +0000527 printf("\nTFTP error: '%s' (%d)\n",
528 pkt + 2, ntohs(*(ushort *)pkt));
Remy Bohmerb46293f2009-10-28 22:13:40 +0100529
530 switch (ntohs(*(ushort *)pkt)) {
531 case TFTP_ERR_FILE_NOT_FOUND:
532 case TFTP_ERR_ACCESS_DENIED:
533 puts("Not retrying...\n");
534 eth_halt();
535 NetState = NETLOOP_FAIL;
536 break;
537 case TFTP_ERR_UNDEFINED:
538 case TFTP_ERR_DISK_FULL:
539 case TFTP_ERR_UNEXPECTED_OPCODE:
540 case TFTP_ERR_UNKNOWN_TRANSFER_ID:
541 case TFTP_ERR_FILE_ALREADY_EXISTS:
542 default:
543 puts("Starting again\n\n");
David Updegraff7280da72007-06-11 10:41:07 -0500544#ifdef CONFIG_MCAST_TFTP
Remy Bohmerb46293f2009-10-28 22:13:40 +0100545 mcast_cleanup();
David Updegraff7280da72007-06-11 10:41:07 -0500546#endif
Remy Bohmerb46293f2009-10-28 22:13:40 +0100547 NetStartAgain();
548 break;
549 }
wdenkfe8c2802002-11-03 00:38:21 +0000550 break;
551 }
552}
553
554
555static void
Luca Ceresolif40538a2011-05-14 05:49:57 +0000556TftpTimeout(void)
wdenkfe8c2802002-11-03 00:38:21 +0000557{
Bartlomiej Sieka371cde72008-10-01 15:26:29 +0200558 if (++TftpTimeoutCount > TftpTimeoutCountMax) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000559 puts("\nRetry count exceeded; starting again\n");
David Updegraff7280da72007-06-11 10:41:07 -0500560#ifdef CONFIG_MCAST_TFTP
561 mcast_cleanup();
562#endif
Luca Ceresolif40538a2011-05-14 05:49:57 +0000563 NetStartAgain();
wdenkfe8c2802002-11-03 00:38:21 +0000564 } else {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000565 puts("T ");
566 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
Luca Ceresolib640ed32011-05-17 00:03:39 +0000567 if (TftpState != STATE_RECV_WRQ)
568 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000569 }
570}
571
572
573void
Luca Ceresolif40538a2011-05-14 05:49:57 +0000574TftpStart(void)
wdenkfe8c2802002-11-03 00:38:21 +0000575{
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200576 char *ep; /* Environment pointer */
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200577
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100578 /*
579 * Allow the user to choose TFTP blocksize and timeout.
580 * TFTP protocol has a minimal timeout of 1 second.
581 */
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000582 ep = getenv("tftpblocksize");
583 if (ep != NULL)
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200584 TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100585
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000586 ep = getenv("tftptimeout");
587 if (ep != NULL)
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100588 TftpTimeoutMSecs = simple_strtol(ep, NULL, 10);
589
590 if (TftpTimeoutMSecs < 1000) {
591 printf("TFTP timeout (%ld ms) too low, "
592 "set minimum = 1000 ms\n",
593 TftpTimeoutMSecs);
594 TftpTimeoutMSecs = 1000;
595 }
596
597 debug("TFTP blocksize = %i, timeout = %ld ms\n",
598 TftpBlkSizeOption, TftpTimeoutMSecs);
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200599
Luca Ceresoli5079c762011-05-17 00:03:37 +0000600 TftpRemoteIP = NetServerIP;
wdenkfe8c2802002-11-03 00:38:21 +0000601 if (BootFile[0] == '\0') {
wdenkfe8c2802002-11-03 00:38:21 +0000602 sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
Wolfgang Denkdec97322005-08-04 01:09:44 +0200603 NetOurIP & 0xFF,
604 (NetOurIP >> 8) & 0xFF,
605 (NetOurIP >> 16) & 0xFF,
Luca Ceresolif40538a2011-05-14 05:49:57 +0000606 (NetOurIP >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100607
608 strncpy(tftp_filename, default_filename, MAX_LEN);
609 tftp_filename[MAX_LEN-1] = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000610
Luca Ceresolif40538a2011-05-14 05:49:57 +0000611 printf("*** Warning: no boot file name; using '%s'\n",
wdenkfe8c2802002-11-03 00:38:21 +0000612 tftp_filename);
613 } else {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000614 char *p = strchr(BootFile, ':');
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100615
616 if (p == NULL) {
617 strncpy(tftp_filename, BootFile, MAX_LEN);
618 tftp_filename[MAX_LEN-1] = 0;
619 } else {
Luca Ceresoli5079c762011-05-17 00:03:37 +0000620 TftpRemoteIP = string_to_ip(BootFile);
Peter Tyser5e58efa2008-12-01 16:29:38 -0600621 strncpy(tftp_filename, p + 1, MAX_LEN);
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100622 tftp_filename[MAX_LEN-1] = 0;
623 }
wdenkfe8c2802002-11-03 00:38:21 +0000624 }
625
Luca Ceresolif40538a2011-05-14 05:49:57 +0000626 printf("Using %s device\n", eth_get_name());
Mike Frysingerd5695f42009-02-17 00:00:53 -0500627 printf("TFTP from server %pI4"
Luca Ceresoli5079c762011-05-17 00:03:37 +0000628 "; our IP address is %pI4", &TftpRemoteIP, &NetOurIP);
wdenkfe8c2802002-11-03 00:38:21 +0000629
630 /* Check if we need to send across this subnet */
631 if (NetOurGatewayIP && NetOurSubnetMask) {
Luca Ceresoli09b18232011-05-14 05:50:02 +0000632 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
Luca Ceresoli5079c762011-05-17 00:03:37 +0000633 IPaddr_t RemoteNet = TftpRemoteIP & NetOurSubnetMask;
wdenkfe8c2802002-11-03 00:38:21 +0000634
Luca Ceresoli5079c762011-05-17 00:03:37 +0000635 if (OurNet != RemoteNet)
Luca Ceresoli09b18232011-05-14 05:50:02 +0000636 printf("; sending through gateway %pI4",
637 &NetOurGatewayIP);
wdenkfe8c2802002-11-03 00:38:21 +0000638 }
Luca Ceresolif40538a2011-05-14 05:49:57 +0000639 putc('\n');
wdenkfe8c2802002-11-03 00:38:21 +0000640
Luca Ceresolif40538a2011-05-14 05:49:57 +0000641 printf("Filename '%s'.", tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000642
643 if (NetBootFileSize) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000644 printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
645 print_size(NetBootFileSize<<9, "");
wdenkfe8c2802002-11-03 00:38:21 +0000646 }
647
Luca Ceresolif40538a2011-05-14 05:49:57 +0000648 putc('\n');
wdenkfe8c2802002-11-03 00:38:21 +0000649
Luca Ceresolif40538a2011-05-14 05:49:57 +0000650 printf("Load address: 0x%lx\n", load_addr);
wdenkfe8c2802002-11-03 00:38:21 +0000651
Luca Ceresolif40538a2011-05-14 05:49:57 +0000652 puts("Loading: *\b");
wdenkfe8c2802002-11-03 00:38:21 +0000653
Bartlomiej Sieka371cde72008-10-01 15:26:29 +0200654 TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
655
Luca Ceresolif40538a2011-05-14 05:49:57 +0000656 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
657 NetSetHandler(TftpHandler);
wdenkfe8c2802002-11-03 00:38:21 +0000658
Luca Ceresoli5079c762011-05-17 00:03:37 +0000659 TftpRemotePort = WELL_KNOWN_PORT;
wdenkfe8c2802002-11-03 00:38:21 +0000660 TftpTimeoutCount = 0;
Luca Ceresoli562dfe52011-05-17 00:03:38 +0000661 TftpState = STATE_SEND_RRQ;
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200662 /* Use a pseudo-random port unless a specific port is set */
wdenkfe8c2802002-11-03 00:38:21 +0000663 TftpOurPort = 1024 + (get_timer(0) % 3072);
David Updegraff7280da72007-06-11 10:41:07 -0500664
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200665#ifdef CONFIG_TFTP_PORT
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000666 ep = getenv("tftpdstp");
Luca Ceresoli35dfb182011-05-14 05:50:00 +0000667 if (ep != NULL)
Luca Ceresoli5079c762011-05-17 00:03:37 +0000668 TftpRemotePort = simple_strtol(ep, NULL, 10);
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000669 ep = getenv("tftpsrcp");
Luca Ceresoli35dfb182011-05-14 05:50:00 +0000670 if (ep != NULL)
Luca Ceresolif40538a2011-05-14 05:49:57 +0000671 TftpOurPort = simple_strtol(ep, NULL, 10);
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200672#endif
wdenk7182b0f2003-10-06 21:55:32 +0000673 TftpBlock = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000674
wdenke6466f62003-06-05 19:27:42 +0000675 /* zero out server ether in case the server ip has changed */
676 memset(NetServerEther, 0, 6);
David Updegraff7280da72007-06-11 10:41:07 -0500677 /* Revert TftpBlkSize to dflt */
678 TftpBlkSize = TFTP_BLOCK_SIZE;
679#ifdef CONFIG_MCAST_TFTP
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100680 mcast_cleanup();
David Updegraff7280da72007-06-11 10:41:07 -0500681#endif
Robin Getzb3b00ed2009-08-20 10:50:20 -0400682#ifdef CONFIG_TFTP_TSIZE
683 TftpTsize = 0;
684 TftpNumchars = 0;
685#endif
wdenke6466f62003-06-05 19:27:42 +0000686
Luca Ceresolif40538a2011-05-14 05:49:57 +0000687 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000688}
Luca Ceresolib640ed32011-05-17 00:03:39 +0000689
690#ifdef CONFIG_CMD_TFTPSRV
691void
692TftpStartServer(void)
693{
694 tftp_filename[0] = 0;
695
Luca Ceresolib640ed32011-05-17 00:03:39 +0000696 printf("Using %s device\n", eth_get_name());
Luca Ceresolib640ed32011-05-17 00:03:39 +0000697 printf("Listening for TFTP transfer on %pI4\n", &NetOurIP);
698 printf("Load address: 0x%lx\n", load_addr);
699
700 puts("Loading: *\b");
701
702 TftpTimeoutCountMax = TIMEOUT_COUNT;
703 TftpTimeoutCount = 0;
704 TftpTimeoutMSecs = TIMEOUT;
705 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
706
707 /* Revert TftpBlkSize to dflt */
708 TftpBlkSize = TFTP_BLOCK_SIZE;
709 TftpBlock = 0;
710 TftpOurPort = WELL_KNOWN_PORT;
711
712#ifdef CONFIG_TFTP_TSIZE
713 TftpTsize = 0;
714 TftpNumchars = 0;
715#endif
716
717 TftpState = STATE_RECV_WRQ;
718 NetSetHandler(TftpHandler);
719}
720#endif /* CONFIG_CMD_TFTPSRV */
wdenkfe8c2802002-11-03 00:38:21 +0000721
David Updegraff7280da72007-06-11 10:41:07 -0500722#ifdef CONFIG_MCAST_TFTP
723/* Credits: atftp project.
724 */
725
726/* pick up BcastAddr, Port, and whether I am [now] the master-client. *
727 * Frame:
728 * +-------+-----------+---+-------~~-------+---+
729 * | opc | multicast | 0 | addr, port, mc | 0 |
730 * +-------+-----------+---+-------~~-------+---+
731 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
732 * I am the new master-client so must send ACKs to DataBlocks. If I am not
733 * master-client, I'm a passive client, gathering what DataBlocks I may and
734 * making note of which ones I got in my bitmask.
735 * In theory, I never go from master->passive..
736 * .. this comes in with pkt already pointing just past opc
737 */
738static void parse_multicast_oack(char *pkt, int len)
739{
Luca Ceresolif40538a2011-05-14 05:49:57 +0000740 int i;
741 IPaddr_t addr;
742 char *mc_adr, *port, *mc;
David Updegraff7280da72007-06-11 10:41:07 -0500743
Luca Ceresolif40538a2011-05-14 05:49:57 +0000744 mc_adr = port = mc = NULL;
David Updegraff7280da72007-06-11 10:41:07 -0500745 /* march along looking for 'multicast\0', which has to start at least
746 * 14 bytes back from the end.
747 */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000748 for (i = 0; i < len-14; i++)
749 if (strcmp(pkt+i, "multicast") == 0)
David Updegraff7280da72007-06-11 10:41:07 -0500750 break;
751 if (i >= (len-14)) /* non-Multicast OACK, ign. */
752 return;
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200753
Luca Ceresolif40538a2011-05-14 05:49:57 +0000754 i += 10; /* strlen multicast */
David Updegraff7280da72007-06-11 10:41:07 -0500755 mc_adr = pkt+i;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000756 for (; i < len; i++) {
David Updegraff7280da72007-06-11 10:41:07 -0500757 if (*(pkt+i) == ',') {
758 *(pkt+i) = '\0';
759 if (port) {
760 mc = pkt+i+1;
761 break;
762 } else {
763 port = pkt+i+1;
764 }
765 }
766 }
Luca Ceresoli6072fcd2011-05-14 05:50:01 +0000767 if (!port || !mc_adr || !mc)
768 return;
David Updegraff7280da72007-06-11 10:41:07 -0500769 if (Multicast && MasterClient) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000770 printf("I got a OACK as master Client, WRONG!\n");
David Updegraff7280da72007-06-11 10:41:07 -0500771 return;
772 }
773 /* ..I now accept packets destined for this MCAST addr, port */
774 if (!Multicast) {
775 if (Bitmap) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000776 printf("Internal failure! no mcast.\n");
David Updegraff7280da72007-06-11 10:41:07 -0500777 free(Bitmap);
Luca Ceresolif40538a2011-05-14 05:49:57 +0000778 Bitmap = NULL;
779 ProhibitMcast = 1;
David Updegraff7280da72007-06-11 10:41:07 -0500780 return ;
781 }
782 /* I malloc instead of pre-declare; so that if the file ends
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200783 * up being too big for this bitmap I can retry
784 */
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000785 Bitmap = malloc(Mapsize);
786 if (!Bitmap) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000787 printf("No Bitmap, no multicast. Sorry.\n");
788 ProhibitMcast = 1;
David Updegraff7280da72007-06-11 10:41:07 -0500789 return;
790 }
Luca Ceresolif40538a2011-05-14 05:49:57 +0000791 memset(Bitmap, 0, Mapsize);
David Updegraff7280da72007-06-11 10:41:07 -0500792 PrevBitmapHole = 0;
793 Multicast = 1;
794 }
795 addr = string_to_ip(mc_adr);
796 if (Mcast_addr != addr) {
797 if (Mcast_addr)
798 eth_mcast_join(Mcast_addr, 0);
Luca Ceresolic4ffb9f2011-05-14 05:49:59 +0000799 Mcast_addr = addr;
800 if (eth_mcast_join(Mcast_addr, 1)) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000801 printf("Fail to set mcast, revert to TFTP\n");
802 ProhibitMcast = 1;
David Updegraff7280da72007-06-11 10:41:07 -0500803 mcast_cleanup();
804 NetStartAgain();
805 }
806 }
Luca Ceresolif40538a2011-05-14 05:49:57 +0000807 MasterClient = (unsigned char)simple_strtoul((char *)mc, NULL, 10);
808 Mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
809 printf("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
David Updegraff7280da72007-06-11 10:41:07 -0500810 return;
811}
812
813#endif /* Multicast TFTP */