blob: 7e3bb3dc24a5cf1b0f48877adead7566f464b1f0 [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
Simon Glasse4cde2f2011-10-24 18:00:04 +0000193/* Clear our state ready for a new transfer */
194void new_transfer(void)
195{
196 TftpLastBlock = 0;
197 TftpBlockWrap = 0;
198 TftpBlockWrapOffset = 0;
199#ifdef CONFIG_CMD_TFTPPUT
200 TftpFinalBlock = 0;
201#endif
202}
203
Luca Ceresolic718b142011-05-14 05:49:57 +0000204static void TftpSend(void);
205static void TftpTimeout(void);
wdenkfe8c2802002-11-03 00:38:21 +0000206
207/**********************************************************************/
208
Simon Glassf5329bb2011-10-24 18:00:03 +0000209static void show_block_marker(void)
210{
211#ifdef CONFIG_TFTP_TSIZE
212 if (TftpTsize) {
213 ulong pos = TftpBlock * TftpBlkSize + TftpBlockWrapOffset;
214
215 while (TftpNumchars < pos * 50 / TftpTsize) {
216 putc('#');
217 TftpNumchars++;
218 }
219 }
220#endif
221 else {
222 if (((TftpBlock - 1) % 10) == 0)
223 putc('#');
224 else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0)
225 puts("\n\t ");
226 }
227}
228
Simon Glasse4cde2f2011-10-24 18:00:04 +0000229/**
230 * restart the current transfer due to an error
231 *
232 * @param msg Message to print for user
233 */
234static void restart(const char *msg)
235{
236 printf("\n%s; starting again\n", msg);
237#ifdef CONFIG_MCAST_TFTP
238 mcast_cleanup();
239#endif
240 NetStartAgain();
241}
242
243/*
244 * Check if the block number has wrapped, and update progress
245 *
246 * TODO: The egregious use of global variables in this file should be tidied.
247 */
248static void update_block_number(void)
249{
250 /*
251 * RFC1350 specifies that the first data packet will
252 * have sequence number 1. If we receive a sequence
253 * number of 0 this means that there was a wrap
254 * around of the (16 bit) counter.
255 */
256 if (TftpBlock == 0) {
257 TftpBlockWrap++;
258 TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE;
259 TftpTimeoutCount = 0; /* we've done well, reset thhe timeout */
260 } else {
261 show_block_marker();
262 }
263}
264
Simon Glassf5329bb2011-10-24 18:00:03 +0000265/* The TFTP get or put is complete */
266static void tftp_complete(void)
267{
268#ifdef CONFIG_TFTP_TSIZE
269 /* Print hash marks for the last packet received */
270 while (TftpTsize && TftpNumchars < 49) {
271 putc('#');
272 TftpNumchars++;
273 }
274#endif
275 puts("\ndone\n");
276 NetState = NETLOOP_SUCCESS;
277}
278
wdenkfe8c2802002-11-03 00:38:21 +0000279static void
Luca Ceresolic718b142011-05-14 05:49:57 +0000280TftpSend(void)
wdenkfe8c2802002-11-03 00:38:21 +0000281{
Luca Ceresoli2e320252011-05-14 05:49:58 +0000282 volatile uchar *pkt;
283 volatile uchar *xp;
284 int len = 0;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200285 volatile ushort *s;
wdenkfe8c2802002-11-03 00:38:21 +0000286
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200287#ifdef CONFIG_MCAST_TFTP
David Updegraff53a5c422007-06-11 10:41:07 -0500288 /* Multicast TFTP.. non-MasterClients do not ACK data. */
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200289 if (Multicast
290 && (TftpState == STATE_DATA)
291 && (MasterClient == 0))
David Updegraff53a5c422007-06-11 10:41:07 -0500292 return;
293#endif
wdenkfe8c2802002-11-03 00:38:21 +0000294 /*
295 * We will always be sending some sort of packet, so
296 * cobble together the packet headers now.
297 */
wdenka3d991b2004-04-15 21:48:45 +0000298 pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
wdenkfe8c2802002-11-03 00:38:21 +0000299
300 switch (TftpState) {
301
Luca Ceresolie3fb0ab2011-05-17 00:03:38 +0000302 case STATE_SEND_RRQ:
wdenkfe8c2802002-11-03 00:38:21 +0000303 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200304 s = (ushort *)pkt;
305 *s++ = htons(TFTP_RRQ);
306 pkt = (uchar *)s;
Luca Ceresolic718b142011-05-14 05:49:57 +0000307 strcpy((char *)pkt, tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000308 pkt += strlen(tftp_filename) + 1;
Luca Ceresolic718b142011-05-14 05:49:57 +0000309 strcpy((char *)pkt, "octet");
wdenkfe8c2802002-11-03 00:38:21 +0000310 pkt += 5 /*strlen("octet")*/ + 1;
Luca Ceresolic718b142011-05-14 05:49:57 +0000311 strcpy((char *)pkt, "timeout");
wdenkfbe4b5c2003-10-06 21:55:32 +0000312 pkt += 7 /*strlen("timeout")*/ + 1;
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100313 sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000);
Robin Getz0ebf04c2009-07-23 03:01:03 -0400314 debug("send option \"timeout %s\"\n", (char *)pkt);
wdenkfbe4b5c2003-10-06 21:55:32 +0000315 pkt += strlen((char *)pkt) + 1;
Robin Getz4fccb812009-08-20 10:50:20 -0400316#ifdef CONFIG_TFTP_TSIZE
317 memcpy((char *)pkt, "tsize\0000\0", 8);
318 pkt += 8;
319#endif
David Updegraff53a5c422007-06-11 10:41:07 -0500320 /* try for more effic. blk size */
Luca Ceresolic718b142011-05-14 05:49:57 +0000321 pkt += sprintf((char *)pkt, "blksize%c%d%c",
322 0, TftpBlkSizeOption, 0);
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200323#ifdef CONFIG_MCAST_TFTP
David Updegraff53a5c422007-06-11 10:41:07 -0500324 /* Check all preconditions before even trying the option */
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200325 if (!ProhibitMcast
Luca Ceresolic718b142011-05-14 05:49:57 +0000326 && (Bitmap = malloc(Mapsize))
David Updegraff53a5c422007-06-11 10:41:07 -0500327 && eth_get_dev()->mcast) {
328 free(Bitmap);
Luca Ceresolic718b142011-05-14 05:49:57 +0000329 Bitmap = NULL;
330 pkt += sprintf((char *)pkt, "multicast%c%c", 0, 0);
David Updegraff53a5c422007-06-11 10:41:07 -0500331 }
332#endif /* CONFIG_MCAST_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000333 len = pkt - xp;
334 break;
335
wdenkfbe4b5c2003-10-06 21:55:32 +0000336 case STATE_OACK:
David Updegraff53a5c422007-06-11 10:41:07 -0500337#ifdef CONFIG_MCAST_TFTP
338 /* My turn! Start at where I need blocks I missed.*/
339 if (Multicast)
Luca Ceresolic718b142011-05-14 05:49:57 +0000340 TftpBlock = ext2_find_next_zero_bit(Bitmap,
341 (Mapsize*8), 0);
David Updegraff53a5c422007-06-11 10:41:07 -0500342 /*..falling..*/
343#endif
Luca Ceresolie59e3562011-05-17 00:03:39 +0000344
345 case STATE_RECV_WRQ:
David Updegraff53a5c422007-06-11 10:41:07 -0500346 case STATE_DATA:
wdenkfe8c2802002-11-03 00:38:21 +0000347 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200348 s = (ushort *)pkt;
349 *s++ = htons(TFTP_ACK);
350 *s++ = htons(TftpBlock);
351 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000352 len = pkt - xp;
353 break;
354
355 case STATE_TOO_LARGE:
356 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200357 s = (ushort *)pkt;
358 *s++ = htons(TFTP_ERROR);
359 *s++ = htons(3);
360 pkt = (uchar *)s;
Luca Ceresolic718b142011-05-14 05:49:57 +0000361 strcpy((char *)pkt, "File too large");
wdenkfe8c2802002-11-03 00:38:21 +0000362 pkt += 14 /*strlen("File too large")*/ + 1;
363 len = pkt - xp;
364 break;
365
366 case STATE_BAD_MAGIC:
367 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200368 s = (ushort *)pkt;
369 *s++ = htons(TFTP_ERROR);
370 *s++ = htons(2);
371 pkt = (uchar *)s;
Luca Ceresolic718b142011-05-14 05:49:57 +0000372 strcpy((char *)pkt, "File has bad magic");
wdenkfe8c2802002-11-03 00:38:21 +0000373 pkt += 18 /*strlen("File has bad magic")*/ + 1;
374 len = pkt - xp;
375 break;
376 }
377
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000378 NetSendUDPPacket(NetServerEther, TftpRemoteIP, TftpRemotePort,
Luca Ceresoli2f094132011-05-14 05:49:56 +0000379 TftpOurPort, len);
wdenkfe8c2802002-11-03 00:38:21 +0000380}
381
382
383static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000384TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
385 unsigned len)
wdenkfe8c2802002-11-03 00:38:21 +0000386{
387 ushort proto;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200388 ushort *s;
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200389 int i;
wdenkfe8c2802002-11-03 00:38:21 +0000390
391 if (dest != TftpOurPort) {
David Updegraff53a5c422007-06-11 10:41:07 -0500392#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200393 if (Multicast
David Updegraff53a5c422007-06-11 10:41:07 -0500394 && (!Mcast_port || (dest != Mcast_port)))
395#endif
Luca Ceresoli0bdd8ac2011-05-14 05:50:02 +0000396 return;
wdenkfe8c2802002-11-03 00:38:21 +0000397 }
Luca Ceresolie59e3562011-05-17 00:03:39 +0000398 if (TftpState != STATE_SEND_RRQ && src != TftpRemotePort &&
399 TftpState != STATE_RECV_WRQ)
wdenkfe8c2802002-11-03 00:38:21 +0000400 return;
wdenkfe8c2802002-11-03 00:38:21 +0000401
Luca Ceresoli7bc325a2011-05-14 05:50:00 +0000402 if (len < 2)
wdenkfe8c2802002-11-03 00:38:21 +0000403 return;
wdenkfe8c2802002-11-03 00:38:21 +0000404 len -= 2;
405 /* warning: don't use increment (++) in ntohs() macros!! */
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200406 s = (ushort *)pkt;
407 proto = *s++;
408 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000409 switch (ntohs(proto)) {
410
411 case TFTP_RRQ:
wdenkfe8c2802002-11-03 00:38:21 +0000412 case TFTP_ACK:
413 break;
414 default:
415 break;
416
Luca Ceresolie59e3562011-05-17 00:03:39 +0000417#ifdef CONFIG_CMD_TFTPSRV
418 case TFTP_WRQ:
419 debug("Got WRQ\n");
420 TftpRemoteIP = sip;
421 TftpRemotePort = src;
422 TftpOurPort = 1024 + (get_timer(0) % 3072);
Simon Glasse4cde2f2011-10-24 18:00:04 +0000423 new_transfer();
Luca Ceresolie59e3562011-05-17 00:03:39 +0000424 TftpSend(); /* Send ACK(0) */
425 break;
426#endif
427
wdenkfbe4b5c2003-10-06 21:55:32 +0000428 case TFTP_OACK:
Wolfgang Denkd3717082009-08-10 09:59:10 +0200429 debug("Got OACK: %s %s\n",
430 pkt,
431 pkt + strlen((char *)pkt) + 1);
wdenkfbe4b5c2003-10-06 21:55:32 +0000432 TftpState = STATE_OACK;
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000433 TftpRemotePort = src;
Wolfgang Denk60174742007-08-31 10:01:51 +0200434 /*
435 * Check for 'blksize' option.
436 * Careful: "i" is signed, "len" is unsigned, thus
437 * something like "len-8" may give a *huge* number
438 */
Luca Ceresolic718b142011-05-14 05:49:57 +0000439 for (i = 0; i+8 < len; i++) {
Luca Ceresoli2e320252011-05-14 05:49:58 +0000440 if (strcmp((char *)pkt+i, "blksize") == 0) {
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200441 TftpBlkSize = (unsigned short)
Luca Ceresoli2e320252011-05-14 05:49:58 +0000442 simple_strtoul((char *)pkt+i+8, NULL,
Luca Ceresolic718b142011-05-14 05:49:57 +0000443 10);
Robin Getz0ebf04c2009-07-23 03:01:03 -0400444 debug("Blocksize ack: %s, %d\n",
Luca Ceresoli2e320252011-05-14 05:49:58 +0000445 (char *)pkt+i+8, TftpBlkSize);
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200446 }
Robin Getz4fccb812009-08-20 10:50:20 -0400447#ifdef CONFIG_TFTP_TSIZE
Luca Ceresoli2e320252011-05-14 05:49:58 +0000448 if (strcmp((char *)pkt+i, "tsize") == 0) {
449 TftpTsize = simple_strtoul((char *)pkt+i+6,
Luca Ceresoli2f094132011-05-14 05:49:56 +0000450 NULL, 10);
Robin Getz4fccb812009-08-20 10:50:20 -0400451 debug("size = %s, %d\n",
Luca Ceresoli2e320252011-05-14 05:49:58 +0000452 (char *)pkt+i+6, TftpTsize);
Robin Getz4fccb812009-08-20 10:50:20 -0400453 }
454#endif
David Updegraff53a5c422007-06-11 10:41:07 -0500455 }
456#ifdef CONFIG_MCAST_TFTP
Luca Ceresolic718b142011-05-14 05:49:57 +0000457 parse_multicast_oack((char *)pkt, len-1);
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200458 if ((Multicast) && (!MasterClient))
David Updegraff53a5c422007-06-11 10:41:07 -0500459 TftpState = STATE_DATA; /* passive.. */
460 else
461#endif
Luca Ceresolic718b142011-05-14 05:49:57 +0000462 TftpSend(); /* Send ACK */
wdenkfbe4b5c2003-10-06 21:55:32 +0000463 break;
wdenkfe8c2802002-11-03 00:38:21 +0000464 case TFTP_DATA:
465 if (len < 2)
466 return;
467 len -= 2;
468 TftpBlock = ntohs(*(ushort *)pkt);
wdenk3f85ce22004-02-23 16:11:30 +0000469
Simon Glasse4cde2f2011-10-24 18:00:04 +0000470 update_block_number();
wdenkfe8c2802002-11-03 00:38:21 +0000471
Luca Ceresolie3fb0ab2011-05-17 00:03:38 +0000472 if (TftpState == STATE_SEND_RRQ)
Robin Getz0ebf04c2009-07-23 03:01:03 -0400473 debug("Server did not acknowledge timeout option!\n");
wdenkfbe4b5c2003-10-06 21:55:32 +0000474
Luca Ceresolie59e3562011-05-17 00:03:39 +0000475 if (TftpState == STATE_SEND_RRQ || TftpState == STATE_OACK ||
476 TftpState == STATE_RECV_WRQ) {
wdenk3f85ce22004-02-23 16:11:30 +0000477 /* first block received */
wdenkfe8c2802002-11-03 00:38:21 +0000478 TftpState = STATE_DATA;
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000479 TftpRemotePort = src;
Simon Glasse4cde2f2011-10-24 18:00:04 +0000480 new_transfer();
wdenkfe8c2802002-11-03 00:38:21 +0000481
David Updegraff53a5c422007-06-11 10:41:07 -0500482#ifdef CONFIG_MCAST_TFTP
483 if (Multicast) { /* start!=1 common if mcast */
484 TftpLastBlock = TftpBlock - 1;
485 } else
486#endif
wdenkfe8c2802002-11-03 00:38:21 +0000487 if (TftpBlock != 1) { /* Assertion */
Luca Ceresolic718b142011-05-14 05:49:57 +0000488 printf("\nTFTP error: "
489 "First block is not block 1 (%ld)\n"
490 "Starting again\n\n",
wdenkfe8c2802002-11-03 00:38:21 +0000491 TftpBlock);
Luca Ceresolic718b142011-05-14 05:49:57 +0000492 NetStartAgain();
wdenkfe8c2802002-11-03 00:38:21 +0000493 break;
494 }
495 }
496
497 if (TftpBlock == TftpLastBlock) {
498 /*
499 * Same block again; ignore it.
500 */
501 break;
502 }
503
504 TftpLastBlock = TftpBlock;
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200505 TftpTimeoutCountMax = TIMEOUT_COUNT;
Luca Ceresolic718b142011-05-14 05:49:57 +0000506 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
wdenkfe8c2802002-11-03 00:38:21 +0000507
Luca Ceresolic718b142011-05-14 05:49:57 +0000508 store_block(TftpBlock - 1, pkt + 2, len);
wdenkfe8c2802002-11-03 00:38:21 +0000509
510 /*
Luca Ceresoli4d69e982011-05-17 00:03:41 +0000511 * Acknowledge the block just received, which will prompt
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000512 * the remote for the next one.
wdenkfe8c2802002-11-03 00:38:21 +0000513 */
David Updegraff53a5c422007-06-11 10:41:07 -0500514#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200515 /* if I am the MasterClient, actively calculate what my next
516 * needed block is; else I'm passive; not ACKING
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100517 */
David Updegraff53a5c422007-06-11 10:41:07 -0500518 if (Multicast) {
519 if (len < TftpBlkSize) {
520 TftpEndingBlock = TftpBlock;
521 } else if (MasterClient) {
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200522 TftpBlock = PrevBitmapHole =
David Updegraff53a5c422007-06-11 10:41:07 -0500523 ext2_find_next_zero_bit(
524 Bitmap,
525 (Mapsize*8),
526 PrevBitmapHole);
527 if (TftpBlock > ((Mapsize*8) - 1)) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000528 printf("tftpfile too big\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500529 /* try to double it and retry */
Luca Ceresolic718b142011-05-14 05:49:57 +0000530 Mapsize <<= 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500531 mcast_cleanup();
Luca Ceresolic718b142011-05-14 05:49:57 +0000532 NetStartAgain();
David Updegraff53a5c422007-06-11 10:41:07 -0500533 return;
534 }
535 TftpLastBlock = TftpBlock;
536 }
537 }
538#endif
Luca Ceresolic718b142011-05-14 05:49:57 +0000539 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000540
David Updegraff53a5c422007-06-11 10:41:07 -0500541#ifdef CONFIG_MCAST_TFTP
542 if (Multicast) {
543 if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000544 puts("\nMulticast tftp done\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500545 mcast_cleanup();
546 NetState = NETLOOP_SUCCESS;
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200547 }
David Updegraff53a5c422007-06-11 10:41:07 -0500548 }
549 else
550#endif
Simon Glassf5329bb2011-10-24 18:00:03 +0000551 if (len < TftpBlkSize)
552 tftp_complete();
wdenkfe8c2802002-11-03 00:38:21 +0000553 break;
554
555 case TFTP_ERROR:
Luca Ceresolic718b142011-05-14 05:49:57 +0000556 printf("\nTFTP error: '%s' (%d)\n",
557 pkt + 2, ntohs(*(ushort *)pkt));
Remy Bohmeraafda382009-10-28 22:13:40 +0100558
559 switch (ntohs(*(ushort *)pkt)) {
560 case TFTP_ERR_FILE_NOT_FOUND:
561 case TFTP_ERR_ACCESS_DENIED:
562 puts("Not retrying...\n");
563 eth_halt();
564 NetState = NETLOOP_FAIL;
565 break;
566 case TFTP_ERR_UNDEFINED:
567 case TFTP_ERR_DISK_FULL:
568 case TFTP_ERR_UNEXPECTED_OPCODE:
569 case TFTP_ERR_UNKNOWN_TRANSFER_ID:
570 case TFTP_ERR_FILE_ALREADY_EXISTS:
571 default:
572 puts("Starting again\n\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500573#ifdef CONFIG_MCAST_TFTP
Remy Bohmeraafda382009-10-28 22:13:40 +0100574 mcast_cleanup();
David Updegraff53a5c422007-06-11 10:41:07 -0500575#endif
Remy Bohmeraafda382009-10-28 22:13:40 +0100576 NetStartAgain();
577 break;
578 }
wdenkfe8c2802002-11-03 00:38:21 +0000579 break;
580 }
581}
582
583
584static void
Luca Ceresolic718b142011-05-14 05:49:57 +0000585TftpTimeout(void)
wdenkfe8c2802002-11-03 00:38:21 +0000586{
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200587 if (++TftpTimeoutCount > TftpTimeoutCountMax) {
Simon Glasse4cde2f2011-10-24 18:00:04 +0000588 restart("Retry count exceeded");
wdenkfe8c2802002-11-03 00:38:21 +0000589 } else {
Luca Ceresolic718b142011-05-14 05:49:57 +0000590 puts("T ");
591 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
Luca Ceresolie59e3562011-05-17 00:03:39 +0000592 if (TftpState != STATE_RECV_WRQ)
593 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000594 }
595}
596
597
Simon Glass58f317d2011-10-24 18:00:05 +0000598void TftpStart(enum proto_t protocol)
wdenkfe8c2802002-11-03 00:38:21 +0000599{
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200600 char *ep; /* Environment pointer */
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200601
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100602 /*
603 * Allow the user to choose TFTP blocksize and timeout.
604 * TFTP protocol has a minimal timeout of 1 second.
605 */
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000606 ep = getenv("tftpblocksize");
607 if (ep != NULL)
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200608 TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100609
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000610 ep = getenv("tftptimeout");
611 if (ep != NULL)
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100612 TftpTimeoutMSecs = simple_strtol(ep, NULL, 10);
613
614 if (TftpTimeoutMSecs < 1000) {
615 printf("TFTP timeout (%ld ms) too low, "
616 "set minimum = 1000 ms\n",
617 TftpTimeoutMSecs);
618 TftpTimeoutMSecs = 1000;
619 }
620
621 debug("TFTP blocksize = %i, timeout = %ld ms\n",
622 TftpBlkSizeOption, TftpTimeoutMSecs);
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200623
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000624 TftpRemoteIP = NetServerIP;
wdenkfe8c2802002-11-03 00:38:21 +0000625 if (BootFile[0] == '\0') {
wdenkfe8c2802002-11-03 00:38:21 +0000626 sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
Wolfgang Denkc43352c2005-08-04 01:09:44 +0200627 NetOurIP & 0xFF,
628 (NetOurIP >> 8) & 0xFF,
629 (NetOurIP >> 16) & 0xFF,
Luca Ceresolic718b142011-05-14 05:49:57 +0000630 (NetOurIP >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100631
632 strncpy(tftp_filename, default_filename, MAX_LEN);
633 tftp_filename[MAX_LEN-1] = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000634
Luca Ceresolic718b142011-05-14 05:49:57 +0000635 printf("*** Warning: no boot file name; using '%s'\n",
wdenkfe8c2802002-11-03 00:38:21 +0000636 tftp_filename);
637 } else {
Luca Ceresolic718b142011-05-14 05:49:57 +0000638 char *p = strchr(BootFile, ':');
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100639
640 if (p == NULL) {
641 strncpy(tftp_filename, BootFile, MAX_LEN);
642 tftp_filename[MAX_LEN-1] = 0;
643 } else {
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000644 TftpRemoteIP = string_to_ip(BootFile);
Peter Tyser6a86bb62008-12-01 16:29:38 -0600645 strncpy(tftp_filename, p + 1, MAX_LEN);
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100646 tftp_filename[MAX_LEN-1] = 0;
647 }
wdenkfe8c2802002-11-03 00:38:21 +0000648 }
649
Luca Ceresolic718b142011-05-14 05:49:57 +0000650 printf("Using %s device\n", eth_get_name());
Mike Frysingerb6446b62009-02-17 00:00:53 -0500651 printf("TFTP from server %pI4"
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000652 "; our IP address is %pI4", &TftpRemoteIP, &NetOurIP);
wdenkfe8c2802002-11-03 00:38:21 +0000653
654 /* Check if we need to send across this subnet */
655 if (NetOurGatewayIP && NetOurSubnetMask) {
Luca Ceresoli0bdd8ac2011-05-14 05:50:02 +0000656 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000657 IPaddr_t RemoteNet = TftpRemoteIP & NetOurSubnetMask;
wdenkfe8c2802002-11-03 00:38:21 +0000658
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000659 if (OurNet != RemoteNet)
Luca Ceresoli0bdd8ac2011-05-14 05:50:02 +0000660 printf("; sending through gateway %pI4",
661 &NetOurGatewayIP);
wdenkfe8c2802002-11-03 00:38:21 +0000662 }
Luca Ceresolic718b142011-05-14 05:49:57 +0000663 putc('\n');
wdenkfe8c2802002-11-03 00:38:21 +0000664
Luca Ceresolic718b142011-05-14 05:49:57 +0000665 printf("Filename '%s'.", tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000666
667 if (NetBootFileSize) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000668 printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
669 print_size(NetBootFileSize<<9, "");
wdenkfe8c2802002-11-03 00:38:21 +0000670 }
671
Luca Ceresolic718b142011-05-14 05:49:57 +0000672 putc('\n');
wdenkfe8c2802002-11-03 00:38:21 +0000673
Luca Ceresolic718b142011-05-14 05:49:57 +0000674 printf("Load address: 0x%lx\n", load_addr);
wdenkfe8c2802002-11-03 00:38:21 +0000675
Luca Ceresolic718b142011-05-14 05:49:57 +0000676 puts("Loading: *\b");
wdenkfe8c2802002-11-03 00:38:21 +0000677
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200678 TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
679
Luca Ceresolic718b142011-05-14 05:49:57 +0000680 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
681 NetSetHandler(TftpHandler);
wdenkfe8c2802002-11-03 00:38:21 +0000682
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000683 TftpRemotePort = WELL_KNOWN_PORT;
wdenkfe8c2802002-11-03 00:38:21 +0000684 TftpTimeoutCount = 0;
Luca Ceresolie3fb0ab2011-05-17 00:03:38 +0000685 TftpState = STATE_SEND_RRQ;
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200686 /* Use a pseudo-random port unless a specific port is set */
wdenkfe8c2802002-11-03 00:38:21 +0000687 TftpOurPort = 1024 + (get_timer(0) % 3072);
David Updegraff53a5c422007-06-11 10:41:07 -0500688
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200689#ifdef CONFIG_TFTP_PORT
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000690 ep = getenv("tftpdstp");
Luca Ceresoli7bc325a2011-05-14 05:50:00 +0000691 if (ep != NULL)
Luca Ceresoli20478ce2011-05-17 00:03:37 +0000692 TftpRemotePort = simple_strtol(ep, NULL, 10);
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000693 ep = getenv("tftpsrcp");
Luca Ceresoli7bc325a2011-05-14 05:50:00 +0000694 if (ep != NULL)
Luca Ceresolic718b142011-05-14 05:49:57 +0000695 TftpOurPort = simple_strtol(ep, NULL, 10);
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200696#endif
wdenkfbe4b5c2003-10-06 21:55:32 +0000697 TftpBlock = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000698
wdenk73a8b272003-06-05 19:27:42 +0000699 /* zero out server ether in case the server ip has changed */
700 memset(NetServerEther, 0, 6);
David Updegraff53a5c422007-06-11 10:41:07 -0500701 /* Revert TftpBlkSize to dflt */
702 TftpBlkSize = TFTP_BLOCK_SIZE;
703#ifdef CONFIG_MCAST_TFTP
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100704 mcast_cleanup();
David Updegraff53a5c422007-06-11 10:41:07 -0500705#endif
Robin Getz4fccb812009-08-20 10:50:20 -0400706#ifdef CONFIG_TFTP_TSIZE
707 TftpTsize = 0;
708 TftpNumchars = 0;
709#endif
wdenk73a8b272003-06-05 19:27:42 +0000710
Luca Ceresolic718b142011-05-14 05:49:57 +0000711 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000712}
713
Luca Ceresolie59e3562011-05-17 00:03:39 +0000714#ifdef CONFIG_CMD_TFTPSRV
715void
716TftpStartServer(void)
717{
718 tftp_filename[0] = 0;
719
Luca Ceresolie59e3562011-05-17 00:03:39 +0000720 printf("Using %s device\n", eth_get_name());
Luca Ceresolie59e3562011-05-17 00:03:39 +0000721 printf("Listening for TFTP transfer on %pI4\n", &NetOurIP);
722 printf("Load address: 0x%lx\n", load_addr);
723
724 puts("Loading: *\b");
725
726 TftpTimeoutCountMax = TIMEOUT_COUNT;
727 TftpTimeoutCount = 0;
728 TftpTimeoutMSecs = TIMEOUT;
729 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
730
731 /* Revert TftpBlkSize to dflt */
732 TftpBlkSize = TFTP_BLOCK_SIZE;
733 TftpBlock = 0;
734 TftpOurPort = WELL_KNOWN_PORT;
735
736#ifdef CONFIG_TFTP_TSIZE
737 TftpTsize = 0;
738 TftpNumchars = 0;
739#endif
740
741 TftpState = STATE_RECV_WRQ;
742 NetSetHandler(TftpHandler);
743}
744#endif /* CONFIG_CMD_TFTPSRV */
745
David Updegraff53a5c422007-06-11 10:41:07 -0500746#ifdef CONFIG_MCAST_TFTP
747/* Credits: atftp project.
748 */
749
750/* pick up BcastAddr, Port, and whether I am [now] the master-client. *
751 * Frame:
752 * +-------+-----------+---+-------~~-------+---+
753 * | opc | multicast | 0 | addr, port, mc | 0 |
754 * +-------+-----------+---+-------~~-------+---+
755 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
756 * I am the new master-client so must send ACKs to DataBlocks. If I am not
757 * master-client, I'm a passive client, gathering what DataBlocks I may and
758 * making note of which ones I got in my bitmask.
759 * In theory, I never go from master->passive..
760 * .. this comes in with pkt already pointing just past opc
761 */
762static void parse_multicast_oack(char *pkt, int len)
763{
Luca Ceresolic718b142011-05-14 05:49:57 +0000764 int i;
765 IPaddr_t addr;
766 char *mc_adr, *port, *mc;
David Updegraff53a5c422007-06-11 10:41:07 -0500767
Luca Ceresolic718b142011-05-14 05:49:57 +0000768 mc_adr = port = mc = NULL;
David Updegraff53a5c422007-06-11 10:41:07 -0500769 /* march along looking for 'multicast\0', which has to start at least
770 * 14 bytes back from the end.
771 */
Luca Ceresolic718b142011-05-14 05:49:57 +0000772 for (i = 0; i < len-14; i++)
773 if (strcmp(pkt+i, "multicast") == 0)
David Updegraff53a5c422007-06-11 10:41:07 -0500774 break;
775 if (i >= (len-14)) /* non-Multicast OACK, ign. */
776 return;
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200777
Luca Ceresolic718b142011-05-14 05:49:57 +0000778 i += 10; /* strlen multicast */
David Updegraff53a5c422007-06-11 10:41:07 -0500779 mc_adr = pkt+i;
Luca Ceresolic718b142011-05-14 05:49:57 +0000780 for (; i < len; i++) {
David Updegraff53a5c422007-06-11 10:41:07 -0500781 if (*(pkt+i) == ',') {
782 *(pkt+i) = '\0';
783 if (port) {
784 mc = pkt+i+1;
785 break;
786 } else {
787 port = pkt+i+1;
788 }
789 }
790 }
Luca Ceresoli6d2231e2011-05-14 05:50:01 +0000791 if (!port || !mc_adr || !mc)
792 return;
David Updegraff53a5c422007-06-11 10:41:07 -0500793 if (Multicast && MasterClient) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000794 printf("I got a OACK as master Client, WRONG!\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500795 return;
796 }
797 /* ..I now accept packets destined for this MCAST addr, port */
798 if (!Multicast) {
799 if (Bitmap) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000800 printf("Internal failure! no mcast.\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500801 free(Bitmap);
Luca Ceresolic718b142011-05-14 05:49:57 +0000802 Bitmap = NULL;
803 ProhibitMcast = 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500804 return ;
805 }
806 /* I malloc instead of pre-declare; so that if the file ends
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200807 * up being too big for this bitmap I can retry
808 */
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000809 Bitmap = malloc(Mapsize);
810 if (!Bitmap) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000811 printf("No Bitmap, no multicast. Sorry.\n");
812 ProhibitMcast = 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500813 return;
814 }
Luca Ceresolic718b142011-05-14 05:49:57 +0000815 memset(Bitmap, 0, Mapsize);
David Updegraff53a5c422007-06-11 10:41:07 -0500816 PrevBitmapHole = 0;
817 Multicast = 1;
818 }
819 addr = string_to_ip(mc_adr);
820 if (Mcast_addr != addr) {
821 if (Mcast_addr)
822 eth_mcast_join(Mcast_addr, 0);
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000823 Mcast_addr = addr;
824 if (eth_mcast_join(Mcast_addr, 1)) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000825 printf("Fail to set mcast, revert to TFTP\n");
826 ProhibitMcast = 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500827 mcast_cleanup();
828 NetStartAgain();
829 }
830 }
Luca Ceresolic718b142011-05-14 05:49:57 +0000831 MasterClient = (unsigned char)simple_strtoul((char *)mc, NULL, 10);
832 Mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
833 printf("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
David Updegraff53a5c422007-06-11 10:41:07 -0500834 return;
835}
836
837#endif /* Multicast TFTP */