blob: 6d44298878857aaecf765879c1291c1d2267d691 [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
wdenk3f85ce22004-02-23 16:11:30 +000084
Luca Ceresolie3fb0ab2011-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
wdenkfbe4b5c2003-10-06 21:55:32 +000089#define STATE_OACK 5
Luca Ceresolie59e3562011-05-17 00:03:39 +000090#define STATE_RECV_WRQ 6
wdenkfe8c2802002-11-03 00:38:21 +000091
Luca Ceresoli2f094132011-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))
wdenk3f85ce22004-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-VILLARDa93907c2008-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-VILLARD6d0f6bc2008-10-16 15:01:15 +0200108#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200109extern flash_info_t flash_info[];
wdenkfe8c2802002-11-03 00:38:21 +0000110#endif
111
Wolfgang Denk85eb5ca2007-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 Updegraff53a5c422007-06-11 10:41:07 -0500114 * almost-MTU block sizes. At least try... fall back to 512 if need be.
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200115 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
David Updegraff53a5c422007-06-11 10:41:07 -0500116 */
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200117#ifdef CONFIG_TFTP_BLOCKSIZE
118#define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
119#else
David Updegraff53a5c422007-06-11 10:41:07 -0500120#define TFTP_MTU_BLOCKSIZE 1468
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200121#endif
122
Luca Ceresolic718b142011-05-14 05:49:57 +0000123static unsigned short TftpBlkSize = TFTP_BLOCK_SIZE;
124static unsigned short TftpBlkSizeOption = TFTP_MTU_BLOCKSIZE;
David Updegraff53a5c422007-06-11 10:41:07 -0500125
126#ifdef CONFIG_MCAST_TFTP
127#include <malloc.h>
128#define MTFTP_BITMAPSIZE 0x1000
129static unsigned *Bitmap;
Luca Ceresolic718b142011-05-14 05:49:57 +0000130static int PrevBitmapHole, Mapsize = MTFTP_BITMAPSIZE;
Luca Ceresoli9bb0a1b2011-05-14 05:50:03 +0000131static uchar ProhibitMcast, MasterClient;
132static uchar Multicast;
David Updegraff53a5c422007-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 Ceresolic718b142011-05-14 05:49:57 +0000137static void parse_multicast_oack(char *pkt, int len);
David Updegraff53a5c422007-06-11 10:41:07 -0500138
139static void
140mcast_cleanup(void)
141{
Luca Ceresoli6d2231e2011-05-14 05:50:01 +0000142 if (Mcast_addr)
143 eth_mcast_join(Mcast_addr, 0);
144 if (Bitmap)
145 free(Bitmap);
Luca Ceresolic718b142011-05-14 05:49:57 +0000146 Bitmap = NULL;
David Updegraff53a5c422007-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 Ceresoli2e320252011-05-14 05:49:58 +0000154store_block(unsigned block, uchar *src, unsigned len)
wdenkfe8c2802002-11-03 00:38:21 +0000155{
David Updegraff53a5c422007-06-11 10:41:07 -0500156 ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
wdenk3f85ce22004-02-23 16:11:30 +0000157 ulong newsize = offset + len;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-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 Ceresolic718b142011-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 Friedrichbe1b0d22008-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 Ceresolic718b142011-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 Ceresolic718b142011-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-VILLARD6d0f6bc2008-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 Updegraff53a5c422007-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 Ceresolic718b142011-05-14 05:49:57 +0000193static void TftpSend(void);
194static void TftpTimeout(void);
wdenkfe8c2802002-11-03 00:38:21 +0000195
196/**********************************************************************/
197
198static void
Luca Ceresolic718b142011-05-14 05:49:57 +0000199TftpSend(void)
wdenkfe8c2802002-11-03 00:38:21 +0000200{
Luca Ceresoli2e320252011-05-14 05:49:58 +0000201 volatile uchar *pkt;
202 volatile uchar *xp;
203 int len = 0;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200204 volatile ushort *s;
wdenkfe8c2802002-11-03 00:38:21 +0000205
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200206#ifdef CONFIG_MCAST_TFTP
David Updegraff53a5c422007-06-11 10:41:07 -0500207 /* Multicast TFTP.. non-MasterClients do not ACK data. */
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200208 if (Multicast
209 && (TftpState == STATE_DATA)
210 && (MasterClient == 0))
David Updegraff53a5c422007-06-11 10:41:07 -0500211 return;
212#endif
wdenkfe8c2802002-11-03 00:38:21 +0000213 /*
214 * We will always be sending some sort of packet, so
215 * cobble together the packet headers now.
216 */
wdenka3d991b2004-04-15 21:48:45 +0000217 pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
wdenkfe8c2802002-11-03 00:38:21 +0000218
219 switch (TftpState) {
220
Luca Ceresolie3fb0ab2011-05-17 00:03:38 +0000221 case STATE_SEND_RRQ:
wdenkfe8c2802002-11-03 00:38:21 +0000222 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200223 s = (ushort *)pkt;
224 *s++ = htons(TFTP_RRQ);
225 pkt = (uchar *)s;
Luca Ceresolic718b142011-05-14 05:49:57 +0000226 strcpy((char *)pkt, tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000227 pkt += strlen(tftp_filename) + 1;
Luca Ceresolic718b142011-05-14 05:49:57 +0000228 strcpy((char *)pkt, "octet");
wdenkfe8c2802002-11-03 00:38:21 +0000229 pkt += 5 /*strlen("octet")*/ + 1;
Luca Ceresolic718b142011-05-14 05:49:57 +0000230 strcpy((char *)pkt, "timeout");
wdenkfbe4b5c2003-10-06 21:55:32 +0000231 pkt += 7 /*strlen("timeout")*/ + 1;
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100232 sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000);
Robin Getz0ebf04c2009-07-23 03:01:03 -0400233 debug("send option \"timeout %s\"\n", (char *)pkt);
wdenkfbe4b5c2003-10-06 21:55:32 +0000234 pkt += strlen((char *)pkt) + 1;
Robin Getz4fccb812009-08-20 10:50:20 -0400235#ifdef CONFIG_TFTP_TSIZE
236 memcpy((char *)pkt, "tsize\0000\0", 8);
237 pkt += 8;
238#endif
David Updegraff53a5c422007-06-11 10:41:07 -0500239 /* try for more effic. blk size */
Luca Ceresolic718b142011-05-14 05:49:57 +0000240 pkt += sprintf((char *)pkt, "blksize%c%d%c",
241 0, TftpBlkSizeOption, 0);
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200242#ifdef CONFIG_MCAST_TFTP
David Updegraff53a5c422007-06-11 10:41:07 -0500243 /* Check all preconditions before even trying the option */
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200244 if (!ProhibitMcast
Luca Ceresolic718b142011-05-14 05:49:57 +0000245 && (Bitmap = malloc(Mapsize))
David Updegraff53a5c422007-06-11 10:41:07 -0500246 && eth_get_dev()->mcast) {
247 free(Bitmap);
Luca Ceresolic718b142011-05-14 05:49:57 +0000248 Bitmap = NULL;
249 pkt += sprintf((char *)pkt, "multicast%c%c", 0, 0);
David Updegraff53a5c422007-06-11 10:41:07 -0500250 }
251#endif /* CONFIG_MCAST_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000252 len = pkt - xp;
253 break;
254
wdenkfbe4b5c2003-10-06 21:55:32 +0000255 case STATE_OACK:
David Updegraff53a5c422007-06-11 10:41:07 -0500256#ifdef CONFIG_MCAST_TFTP
257 /* My turn! Start at where I need blocks I missed.*/
258 if (Multicast)
Luca Ceresolic718b142011-05-14 05:49:57 +0000259 TftpBlock = ext2_find_next_zero_bit(Bitmap,
260 (Mapsize*8), 0);
David Updegraff53a5c422007-06-11 10:41:07 -0500261 /*..falling..*/
262#endif
Luca Ceresolie59e3562011-05-17 00:03:39 +0000263
264 case STATE_RECV_WRQ:
David Updegraff53a5c422007-06-11 10:41:07 -0500265 case STATE_DATA:
wdenkfe8c2802002-11-03 00:38:21 +0000266 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200267 s = (ushort *)pkt;
268 *s++ = htons(TFTP_ACK);
269 *s++ = htons(TftpBlock);
270 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000271 len = pkt - xp;
272 break;
273
274 case STATE_TOO_LARGE:
275 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200276 s = (ushort *)pkt;
277 *s++ = htons(TFTP_ERROR);
278 *s++ = htons(3);
279 pkt = (uchar *)s;
Luca Ceresolic718b142011-05-14 05:49:57 +0000280 strcpy((char *)pkt, "File too large");
wdenkfe8c2802002-11-03 00:38:21 +0000281 pkt += 14 /*strlen("File too large")*/ + 1;
282 len = pkt - xp;
283 break;
284
285 case STATE_BAD_MAGIC:
286 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200287 s = (ushort *)pkt;
288 *s++ = htons(TFTP_ERROR);
289 *s++ = htons(2);
290 pkt = (uchar *)s;
Luca Ceresolic718b142011-05-14 05:49:57 +0000291 strcpy((char *)pkt, "File has bad magic");
wdenkfe8c2802002-11-03 00:38:21 +0000292 pkt += 18 /*strlen("File has bad magic")*/ + 1;
293 len = pkt - xp;
294 break;
295 }
296
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000297 NetSendUDPPacket(NetServerEther, TftpRemoteIP, TftpRemotePort,
Luca Ceresoli2f094132011-05-14 05:49:56 +0000298 TftpOurPort, len);
wdenkfe8c2802002-11-03 00:38:21 +0000299}
300
301
302static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000303TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
304 unsigned len)
wdenkfe8c2802002-11-03 00:38:21 +0000305{
306 ushort proto;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200307 ushort *s;
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200308 int i;
wdenkfe8c2802002-11-03 00:38:21 +0000309
310 if (dest != TftpOurPort) {
David Updegraff53a5c422007-06-11 10:41:07 -0500311#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200312 if (Multicast
David Updegraff53a5c422007-06-11 10:41:07 -0500313 && (!Mcast_port || (dest != Mcast_port)))
314#endif
Luca Ceresoli0bdd8ac2011-05-14 05:50:02 +0000315 return;
wdenkfe8c2802002-11-03 00:38:21 +0000316 }
Luca Ceresolie59e3562011-05-17 00:03:39 +0000317 if (TftpState != STATE_SEND_RRQ && src != TftpRemotePort &&
318 TftpState != STATE_RECV_WRQ)
wdenkfe8c2802002-11-03 00:38:21 +0000319 return;
wdenkfe8c2802002-11-03 00:38:21 +0000320
Luca Ceresoli7bc325a2011-05-14 05:50:00 +0000321 if (len < 2)
wdenkfe8c2802002-11-03 00:38:21 +0000322 return;
wdenkfe8c2802002-11-03 00:38:21 +0000323 len -= 2;
324 /* warning: don't use increment (++) in ntohs() macros!! */
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200325 s = (ushort *)pkt;
326 proto = *s++;
327 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000328 switch (ntohs(proto)) {
329
330 case TFTP_RRQ:
wdenkfe8c2802002-11-03 00:38:21 +0000331 case TFTP_ACK:
332 break;
333 default:
334 break;
335
Luca Ceresolie59e3562011-05-17 00:03:39 +0000336#ifdef CONFIG_CMD_TFTPSRV
337 case TFTP_WRQ:
338 debug("Got WRQ\n");
339 TftpRemoteIP = sip;
340 TftpRemotePort = src;
341 TftpOurPort = 1024 + (get_timer(0) % 3072);
342 TftpLastBlock = 0;
343 TftpBlockWrap = 0;
344 TftpBlockWrapOffset = 0;
345 TftpSend(); /* Send ACK(0) */
346 break;
347#endif
348
wdenkfbe4b5c2003-10-06 21:55:32 +0000349 case TFTP_OACK:
Wolfgang Denkd3717082009-08-10 09:59:10 +0200350 debug("Got OACK: %s %s\n",
351 pkt,
352 pkt + strlen((char *)pkt) + 1);
wdenkfbe4b5c2003-10-06 21:55:32 +0000353 TftpState = STATE_OACK;
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000354 TftpRemotePort = src;
Wolfgang Denk60174742007-08-31 10:01:51 +0200355 /*
356 * Check for 'blksize' option.
357 * Careful: "i" is signed, "len" is unsigned, thus
358 * something like "len-8" may give a *huge* number
359 */
Luca Ceresolic718b142011-05-14 05:49:57 +0000360 for (i = 0; i+8 < len; i++) {
Luca Ceresoli2e320252011-05-14 05:49:58 +0000361 if (strcmp((char *)pkt+i, "blksize") == 0) {
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200362 TftpBlkSize = (unsigned short)
Luca Ceresoli2e320252011-05-14 05:49:58 +0000363 simple_strtoul((char *)pkt+i+8, NULL,
Luca Ceresolic718b142011-05-14 05:49:57 +0000364 10);
Robin Getz0ebf04c2009-07-23 03:01:03 -0400365 debug("Blocksize ack: %s, %d\n",
Luca Ceresoli2e320252011-05-14 05:49:58 +0000366 (char *)pkt+i+8, TftpBlkSize);
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200367 }
Robin Getz4fccb812009-08-20 10:50:20 -0400368#ifdef CONFIG_TFTP_TSIZE
Luca Ceresoli2e320252011-05-14 05:49:58 +0000369 if (strcmp((char *)pkt+i, "tsize") == 0) {
370 TftpTsize = simple_strtoul((char *)pkt+i+6,
Luca Ceresoli2f094132011-05-14 05:49:56 +0000371 NULL, 10);
Robin Getz4fccb812009-08-20 10:50:20 -0400372 debug("size = %s, %d\n",
Luca Ceresoli2e320252011-05-14 05:49:58 +0000373 (char *)pkt+i+6, TftpTsize);
Robin Getz4fccb812009-08-20 10:50:20 -0400374 }
375#endif
David Updegraff53a5c422007-06-11 10:41:07 -0500376 }
377#ifdef CONFIG_MCAST_TFTP
Luca Ceresolic718b142011-05-14 05:49:57 +0000378 parse_multicast_oack((char *)pkt, len-1);
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200379 if ((Multicast) && (!MasterClient))
David Updegraff53a5c422007-06-11 10:41:07 -0500380 TftpState = STATE_DATA; /* passive.. */
381 else
382#endif
Luca Ceresolic718b142011-05-14 05:49:57 +0000383 TftpSend(); /* Send ACK */
wdenkfbe4b5c2003-10-06 21:55:32 +0000384 break;
wdenkfe8c2802002-11-03 00:38:21 +0000385 case TFTP_DATA:
386 if (len < 2)
387 return;
388 len -= 2;
389 TftpBlock = ntohs(*(ushort *)pkt);
wdenk3f85ce22004-02-23 16:11:30 +0000390
391 /*
wdenkcd0a9de2004-02-23 20:48:38 +0000392 * RFC1350 specifies that the first data packet will
393 * have sequence number 1. If we receive a sequence
394 * number of 0 this means that there was a wrap
395 * around of the (16 bit) counter.
wdenk3f85ce22004-02-23 16:11:30 +0000396 */
397 if (TftpBlock == 0) {
398 TftpBlockWrap++;
Luca Ceresoli2f094132011-05-14 05:49:56 +0000399 TftpBlockWrapOffset +=
400 TftpBlkSize * TFTP_SEQUENCE_SIZE;
Luca Ceresolic718b142011-05-14 05:49:57 +0000401 printf("\n\t %lu MB received\n\t ",
Luca Ceresoli2f094132011-05-14 05:49:56 +0000402 TftpBlockWrapOffset>>20);
Robin Getz4fccb812009-08-20 10:50:20 -0400403 }
404#ifdef CONFIG_TFTP_TSIZE
405 else if (TftpTsize) {
Luca Ceresoli2f094132011-05-14 05:49:56 +0000406 while (TftpNumchars <
407 NetBootFileXferSize * 50 / TftpTsize) {
Robin Getz4fccb812009-08-20 10:50:20 -0400408 putc('#');
409 TftpNumchars++;
410 }
411 }
412#endif
413 else {
Luca Ceresoli7bc325a2011-05-14 05:50:00 +0000414 if (((TftpBlock - 1) % 10) == 0)
Luca Ceresolic718b142011-05-14 05:49:57 +0000415 putc('#');
Luca Ceresoli7bc325a2011-05-14 05:50:00 +0000416 else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0)
Luca Ceresolic718b142011-05-14 05:49:57 +0000417 puts("\n\t ");
wdenkfe8c2802002-11-03 00:38:21 +0000418 }
419
Luca Ceresolie3fb0ab2011-05-17 00:03:38 +0000420 if (TftpState == STATE_SEND_RRQ)
Robin Getz0ebf04c2009-07-23 03:01:03 -0400421 debug("Server did not acknowledge timeout option!\n");
wdenkfbe4b5c2003-10-06 21:55:32 +0000422
Luca Ceresolie59e3562011-05-17 00:03:39 +0000423 if (TftpState == STATE_SEND_RRQ || TftpState == STATE_OACK ||
424 TftpState == STATE_RECV_WRQ) {
wdenk3f85ce22004-02-23 16:11:30 +0000425 /* first block received */
wdenkfe8c2802002-11-03 00:38:21 +0000426 TftpState = STATE_DATA;
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000427 TftpRemotePort = src;
wdenkfe8c2802002-11-03 00:38:21 +0000428 TftpLastBlock = 0;
wdenk3f85ce22004-02-23 16:11:30 +0000429 TftpBlockWrap = 0;
430 TftpBlockWrapOffset = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000431
David Updegraff53a5c422007-06-11 10:41:07 -0500432#ifdef CONFIG_MCAST_TFTP
433 if (Multicast) { /* start!=1 common if mcast */
434 TftpLastBlock = TftpBlock - 1;
435 } else
436#endif
wdenkfe8c2802002-11-03 00:38:21 +0000437 if (TftpBlock != 1) { /* Assertion */
Luca Ceresolic718b142011-05-14 05:49:57 +0000438 printf("\nTFTP error: "
439 "First block is not block 1 (%ld)\n"
440 "Starting again\n\n",
wdenkfe8c2802002-11-03 00:38:21 +0000441 TftpBlock);
Luca Ceresolic718b142011-05-14 05:49:57 +0000442 NetStartAgain();
wdenkfe8c2802002-11-03 00:38:21 +0000443 break;
444 }
445 }
446
447 if (TftpBlock == TftpLastBlock) {
448 /*
449 * Same block again; ignore it.
450 */
451 break;
452 }
453
454 TftpLastBlock = TftpBlock;
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200455 TftpTimeoutCountMax = TIMEOUT_COUNT;
Luca Ceresolic718b142011-05-14 05:49:57 +0000456 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
wdenkfe8c2802002-11-03 00:38:21 +0000457
Luca Ceresolic718b142011-05-14 05:49:57 +0000458 store_block(TftpBlock - 1, pkt + 2, len);
wdenkfe8c2802002-11-03 00:38:21 +0000459
460 /*
461 * Acknoledge the block just received, which will prompt
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000462 * the remote for the next one.
wdenkfe8c2802002-11-03 00:38:21 +0000463 */
David Updegraff53a5c422007-06-11 10:41:07 -0500464#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200465 /* if I am the MasterClient, actively calculate what my next
466 * needed block is; else I'm passive; not ACKING
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100467 */
David Updegraff53a5c422007-06-11 10:41:07 -0500468 if (Multicast) {
469 if (len < TftpBlkSize) {
470 TftpEndingBlock = TftpBlock;
471 } else if (MasterClient) {
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200472 TftpBlock = PrevBitmapHole =
David Updegraff53a5c422007-06-11 10:41:07 -0500473 ext2_find_next_zero_bit(
474 Bitmap,
475 (Mapsize*8),
476 PrevBitmapHole);
477 if (TftpBlock > ((Mapsize*8) - 1)) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000478 printf("tftpfile too big\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500479 /* try to double it and retry */
Luca Ceresolic718b142011-05-14 05:49:57 +0000480 Mapsize <<= 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500481 mcast_cleanup();
Luca Ceresolic718b142011-05-14 05:49:57 +0000482 NetStartAgain();
David Updegraff53a5c422007-06-11 10:41:07 -0500483 return;
484 }
485 TftpLastBlock = TftpBlock;
486 }
487 }
488#endif
Luca Ceresolic718b142011-05-14 05:49:57 +0000489 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000490
David Updegraff53a5c422007-06-11 10:41:07 -0500491#ifdef CONFIG_MCAST_TFTP
492 if (Multicast) {
493 if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000494 puts("\nMulticast tftp done\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500495 mcast_cleanup();
496 NetState = NETLOOP_SUCCESS;
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200497 }
David Updegraff53a5c422007-06-11 10:41:07 -0500498 }
499 else
500#endif
501 if (len < TftpBlkSize) {
wdenkfe8c2802002-11-03 00:38:21 +0000502 /*
503 * We received the whole thing. Try to
504 * run it.
505 */
Robin Getz4fccb812009-08-20 10:50:20 -0400506#ifdef CONFIG_TFTP_TSIZE
Luca Ceresoli2f094132011-05-14 05:49:56 +0000507 /* Print hash marks for the last packet received */
Robin Getz4fccb812009-08-20 10:50:20 -0400508 while (TftpTsize && TftpNumchars < 49) {
509 putc('#');
510 TftpNumchars++;
511 }
512#endif
Luca Ceresolic718b142011-05-14 05:49:57 +0000513 puts("\ndone\n");
wdenkfe8c2802002-11-03 00:38:21 +0000514 NetState = NETLOOP_SUCCESS;
515 }
516 break;
517
518 case TFTP_ERROR:
Luca Ceresolic718b142011-05-14 05:49:57 +0000519 printf("\nTFTP error: '%s' (%d)\n",
520 pkt + 2, ntohs(*(ushort *)pkt));
Remy Bohmeraafda382009-10-28 22:13:40 +0100521
522 switch (ntohs(*(ushort *)pkt)) {
523 case TFTP_ERR_FILE_NOT_FOUND:
524 case TFTP_ERR_ACCESS_DENIED:
525 puts("Not retrying...\n");
526 eth_halt();
527 NetState = NETLOOP_FAIL;
528 break;
529 case TFTP_ERR_UNDEFINED:
530 case TFTP_ERR_DISK_FULL:
531 case TFTP_ERR_UNEXPECTED_OPCODE:
532 case TFTP_ERR_UNKNOWN_TRANSFER_ID:
533 case TFTP_ERR_FILE_ALREADY_EXISTS:
534 default:
535 puts("Starting again\n\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500536#ifdef CONFIG_MCAST_TFTP
Remy Bohmeraafda382009-10-28 22:13:40 +0100537 mcast_cleanup();
David Updegraff53a5c422007-06-11 10:41:07 -0500538#endif
Remy Bohmeraafda382009-10-28 22:13:40 +0100539 NetStartAgain();
540 break;
541 }
wdenkfe8c2802002-11-03 00:38:21 +0000542 break;
543 }
544}
545
546
547static void
Luca Ceresolic718b142011-05-14 05:49:57 +0000548TftpTimeout(void)
wdenkfe8c2802002-11-03 00:38:21 +0000549{
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200550 if (++TftpTimeoutCount > TftpTimeoutCountMax) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000551 puts("\nRetry count exceeded; starting again\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500552#ifdef CONFIG_MCAST_TFTP
553 mcast_cleanup();
554#endif
Luca Ceresolic718b142011-05-14 05:49:57 +0000555 NetStartAgain();
wdenkfe8c2802002-11-03 00:38:21 +0000556 } else {
Luca Ceresolic718b142011-05-14 05:49:57 +0000557 puts("T ");
558 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
Luca Ceresolie59e3562011-05-17 00:03:39 +0000559 if (TftpState != STATE_RECV_WRQ)
560 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000561 }
562}
563
564
565void
Luca Ceresolic718b142011-05-14 05:49:57 +0000566TftpStart(void)
wdenkfe8c2802002-11-03 00:38:21 +0000567{
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200568 char *ep; /* Environment pointer */
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200569
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100570 /*
571 * Allow the user to choose TFTP blocksize and timeout.
572 * TFTP protocol has a minimal timeout of 1 second.
573 */
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000574 ep = getenv("tftpblocksize");
575 if (ep != NULL)
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200576 TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100577
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000578 ep = getenv("tftptimeout");
579 if (ep != NULL)
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100580 TftpTimeoutMSecs = simple_strtol(ep, NULL, 10);
581
582 if (TftpTimeoutMSecs < 1000) {
583 printf("TFTP timeout (%ld ms) too low, "
584 "set minimum = 1000 ms\n",
585 TftpTimeoutMSecs);
586 TftpTimeoutMSecs = 1000;
587 }
588
589 debug("TFTP blocksize = %i, timeout = %ld ms\n",
590 TftpBlkSizeOption, TftpTimeoutMSecs);
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200591
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000592 TftpRemoteIP = NetServerIP;
wdenkfe8c2802002-11-03 00:38:21 +0000593 if (BootFile[0] == '\0') {
wdenkfe8c2802002-11-03 00:38:21 +0000594 sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
Wolfgang Denkc43352c2005-08-04 01:09:44 +0200595 NetOurIP & 0xFF,
596 (NetOurIP >> 8) & 0xFF,
597 (NetOurIP >> 16) & 0xFF,
Luca Ceresolic718b142011-05-14 05:49:57 +0000598 (NetOurIP >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100599
600 strncpy(tftp_filename, default_filename, MAX_LEN);
601 tftp_filename[MAX_LEN-1] = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000602
Luca Ceresolic718b142011-05-14 05:49:57 +0000603 printf("*** Warning: no boot file name; using '%s'\n",
wdenkfe8c2802002-11-03 00:38:21 +0000604 tftp_filename);
605 } else {
Luca Ceresolic718b142011-05-14 05:49:57 +0000606 char *p = strchr(BootFile, ':');
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100607
608 if (p == NULL) {
609 strncpy(tftp_filename, BootFile, MAX_LEN);
610 tftp_filename[MAX_LEN-1] = 0;
611 } else {
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000612 TftpRemoteIP = string_to_ip(BootFile);
Peter Tyser6a86bb62008-12-01 16:29:38 -0600613 strncpy(tftp_filename, p + 1, MAX_LEN);
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100614 tftp_filename[MAX_LEN-1] = 0;
615 }
wdenkfe8c2802002-11-03 00:38:21 +0000616 }
617
wdenk5bb226e2003-11-17 21:14:37 +0000618#if defined(CONFIG_NET_MULTI)
Luca Ceresolic718b142011-05-14 05:49:57 +0000619 printf("Using %s device\n", eth_get_name());
wdenk5bb226e2003-11-17 21:14:37 +0000620#endif
Mike Frysingerb6446b62009-02-17 00:00:53 -0500621 printf("TFTP from server %pI4"
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000622 "; our IP address is %pI4", &TftpRemoteIP, &NetOurIP);
wdenkfe8c2802002-11-03 00:38:21 +0000623
624 /* Check if we need to send across this subnet */
625 if (NetOurGatewayIP && NetOurSubnetMask) {
Luca Ceresoli0bdd8ac2011-05-14 05:50:02 +0000626 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000627 IPaddr_t RemoteNet = TftpRemoteIP & NetOurSubnetMask;
wdenkfe8c2802002-11-03 00:38:21 +0000628
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000629 if (OurNet != RemoteNet)
Luca Ceresoli0bdd8ac2011-05-14 05:50:02 +0000630 printf("; sending through gateway %pI4",
631 &NetOurGatewayIP);
wdenkfe8c2802002-11-03 00:38:21 +0000632 }
Luca Ceresolic718b142011-05-14 05:49:57 +0000633 putc('\n');
wdenkfe8c2802002-11-03 00:38:21 +0000634
Luca Ceresolic718b142011-05-14 05:49:57 +0000635 printf("Filename '%s'.", tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000636
637 if (NetBootFileSize) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000638 printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
639 print_size(NetBootFileSize<<9, "");
wdenkfe8c2802002-11-03 00:38:21 +0000640 }
641
Luca Ceresolic718b142011-05-14 05:49:57 +0000642 putc('\n');
wdenkfe8c2802002-11-03 00:38:21 +0000643
Luca Ceresolic718b142011-05-14 05:49:57 +0000644 printf("Load address: 0x%lx\n", load_addr);
wdenkfe8c2802002-11-03 00:38:21 +0000645
Luca Ceresolic718b142011-05-14 05:49:57 +0000646 puts("Loading: *\b");
wdenkfe8c2802002-11-03 00:38:21 +0000647
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200648 TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
649
Luca Ceresolic718b142011-05-14 05:49:57 +0000650 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
651 NetSetHandler(TftpHandler);
wdenkfe8c2802002-11-03 00:38:21 +0000652
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000653 TftpRemotePort = WELL_KNOWN_PORT;
wdenkfe8c2802002-11-03 00:38:21 +0000654 TftpTimeoutCount = 0;
Luca Ceresolie3fb0ab2011-05-17 00:03:38 +0000655 TftpState = STATE_SEND_RRQ;
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200656 /* Use a pseudo-random port unless a specific port is set */
wdenkfe8c2802002-11-03 00:38:21 +0000657 TftpOurPort = 1024 + (get_timer(0) % 3072);
David Updegraff53a5c422007-06-11 10:41:07 -0500658
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200659#ifdef CONFIG_TFTP_PORT
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000660 ep = getenv("tftpdstp");
Luca Ceresoli7bc325a2011-05-14 05:50:00 +0000661 if (ep != NULL)
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000662 TftpRemotePort = simple_strtol(ep, NULL, 10);
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000663 ep = getenv("tftpsrcp");
Luca Ceresoli7bc325a2011-05-14 05:50:00 +0000664 if (ep != NULL)
Luca Ceresolic718b142011-05-14 05:49:57 +0000665 TftpOurPort = simple_strtol(ep, NULL, 10);
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200666#endif
wdenkfbe4b5c2003-10-06 21:55:32 +0000667 TftpBlock = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000668
wdenk73a8b272003-06-05 19:27:42 +0000669 /* zero out server ether in case the server ip has changed */
670 memset(NetServerEther, 0, 6);
David Updegraff53a5c422007-06-11 10:41:07 -0500671 /* Revert TftpBlkSize to dflt */
672 TftpBlkSize = TFTP_BLOCK_SIZE;
673#ifdef CONFIG_MCAST_TFTP
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100674 mcast_cleanup();
David Updegraff53a5c422007-06-11 10:41:07 -0500675#endif
Robin Getz4fccb812009-08-20 10:50:20 -0400676#ifdef CONFIG_TFTP_TSIZE
677 TftpTsize = 0;
678 TftpNumchars = 0;
679#endif
wdenk73a8b272003-06-05 19:27:42 +0000680
Luca Ceresolic718b142011-05-14 05:49:57 +0000681 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000682}
683
Luca Ceresolie59e3562011-05-17 00:03:39 +0000684#ifdef CONFIG_CMD_TFTPSRV
685void
686TftpStartServer(void)
687{
688 tftp_filename[0] = 0;
689
690#if defined(CONFIG_NET_MULTI)
691 printf("Using %s device\n", eth_get_name());
692#endif
693 printf("Listening for TFTP transfer on %pI4\n", &NetOurIP);
694 printf("Load address: 0x%lx\n", load_addr);
695
696 puts("Loading: *\b");
697
698 TftpTimeoutCountMax = TIMEOUT_COUNT;
699 TftpTimeoutCount = 0;
700 TftpTimeoutMSecs = TIMEOUT;
701 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
702
703 /* Revert TftpBlkSize to dflt */
704 TftpBlkSize = TFTP_BLOCK_SIZE;
705 TftpBlock = 0;
706 TftpOurPort = WELL_KNOWN_PORT;
707
708#ifdef CONFIG_TFTP_TSIZE
709 TftpTsize = 0;
710 TftpNumchars = 0;
711#endif
712
713 TftpState = STATE_RECV_WRQ;
714 NetSetHandler(TftpHandler);
715}
716#endif /* CONFIG_CMD_TFTPSRV */
717
David Updegraff53a5c422007-06-11 10:41:07 -0500718#ifdef CONFIG_MCAST_TFTP
719/* Credits: atftp project.
720 */
721
722/* pick up BcastAddr, Port, and whether I am [now] the master-client. *
723 * Frame:
724 * +-------+-----------+---+-------~~-------+---+
725 * | opc | multicast | 0 | addr, port, mc | 0 |
726 * +-------+-----------+---+-------~~-------+---+
727 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
728 * I am the new master-client so must send ACKs to DataBlocks. If I am not
729 * master-client, I'm a passive client, gathering what DataBlocks I may and
730 * making note of which ones I got in my bitmask.
731 * In theory, I never go from master->passive..
732 * .. this comes in with pkt already pointing just past opc
733 */
734static void parse_multicast_oack(char *pkt, int len)
735{
Luca Ceresolic718b142011-05-14 05:49:57 +0000736 int i;
737 IPaddr_t addr;
738 char *mc_adr, *port, *mc;
David Updegraff53a5c422007-06-11 10:41:07 -0500739
Luca Ceresolic718b142011-05-14 05:49:57 +0000740 mc_adr = port = mc = NULL;
David Updegraff53a5c422007-06-11 10:41:07 -0500741 /* march along looking for 'multicast\0', which has to start at least
742 * 14 bytes back from the end.
743 */
Luca Ceresolic718b142011-05-14 05:49:57 +0000744 for (i = 0; i < len-14; i++)
745 if (strcmp(pkt+i, "multicast") == 0)
David Updegraff53a5c422007-06-11 10:41:07 -0500746 break;
747 if (i >= (len-14)) /* non-Multicast OACK, ign. */
748 return;
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200749
Luca Ceresolic718b142011-05-14 05:49:57 +0000750 i += 10; /* strlen multicast */
David Updegraff53a5c422007-06-11 10:41:07 -0500751 mc_adr = pkt+i;
Luca Ceresolic718b142011-05-14 05:49:57 +0000752 for (; i < len; i++) {
David Updegraff53a5c422007-06-11 10:41:07 -0500753 if (*(pkt+i) == ',') {
754 *(pkt+i) = '\0';
755 if (port) {
756 mc = pkt+i+1;
757 break;
758 } else {
759 port = pkt+i+1;
760 }
761 }
762 }
Luca Ceresoli6d2231e2011-05-14 05:50:01 +0000763 if (!port || !mc_adr || !mc)
764 return;
David Updegraff53a5c422007-06-11 10:41:07 -0500765 if (Multicast && MasterClient) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000766 printf("I got a OACK as master Client, WRONG!\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500767 return;
768 }
769 /* ..I now accept packets destined for this MCAST addr, port */
770 if (!Multicast) {
771 if (Bitmap) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000772 printf("Internal failure! no mcast.\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500773 free(Bitmap);
Luca Ceresolic718b142011-05-14 05:49:57 +0000774 Bitmap = NULL;
775 ProhibitMcast = 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500776 return ;
777 }
778 /* I malloc instead of pre-declare; so that if the file ends
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200779 * up being too big for this bitmap I can retry
780 */
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000781 Bitmap = malloc(Mapsize);
782 if (!Bitmap) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000783 printf("No Bitmap, no multicast. Sorry.\n");
784 ProhibitMcast = 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500785 return;
786 }
Luca Ceresolic718b142011-05-14 05:49:57 +0000787 memset(Bitmap, 0, Mapsize);
David Updegraff53a5c422007-06-11 10:41:07 -0500788 PrevBitmapHole = 0;
789 Multicast = 1;
790 }
791 addr = string_to_ip(mc_adr);
792 if (Mcast_addr != addr) {
793 if (Mcast_addr)
794 eth_mcast_join(Mcast_addr, 0);
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000795 Mcast_addr = addr;
796 if (eth_mcast_join(Mcast_addr, 1)) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000797 printf("Fail to set mcast, revert to TFTP\n");
798 ProhibitMcast = 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500799 mcast_cleanup();
800 NetStartAgain();
801 }
802 }
Luca Ceresolic718b142011-05-14 05:49:57 +0000803 MasterClient = (unsigned char)simple_strtoul((char *)mc, NULL, 10);
804 Mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
805 printf("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
David Updegraff53a5c422007-06-11 10:41:07 -0500806 return;
807}
808
809#endif /* Multicast TFTP */