blob: 0fd6c6560499ea0871c1e2efa45a95d0264756c2 [file] [log] [blame]
wdenkfe8c2802002-11-03 00:38:21 +00001/*
2 * Copyright 1994, 1995, 2000 Neil Russell.
3 * (See License)
4 * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
5 */
6
7#include <common.h>
8#include <command.h>
9#include <net.h>
10#include "tftp.h"
11#include "bootp.h"
12
Jon Loeliger643d1ab2007-07-09 17:45:14 -050013#if defined(CONFIG_CMD_NET)
wdenkfe8c2802002-11-03 00:38:21 +000014
15#define WELL_KNOWN_PORT 69 /* Well known TFTP port # */
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +020016#define TIMEOUT 5000UL /* Millisecs to timeout for lost pkt */
wdenkfe8c2802002-11-03 00:38:21 +000017#ifndef CONFIG_NET_RETRY_COUNT
18# define TIMEOUT_COUNT 10 /* # of timeouts before giving up */
19#else
20# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
21#endif
22 /* (for checking the image size) */
23#define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
24
25/*
26 * TFTP operations.
27 */
28#define TFTP_RRQ 1
29#define TFTP_WRQ 2
30#define TFTP_DATA 3
31#define TFTP_ACK 4
32#define TFTP_ERROR 5
wdenkfbe4b5c2003-10-06 21:55:32 +000033#define TFTP_OACK 6
wdenkfe8c2802002-11-03 00:38:21 +000034
Bartlomiej Siekae83cc062008-10-01 15:26:29 +020035static ulong TftpTimeoutMSecs = TIMEOUT;
36static int TftpTimeoutCountMax = TIMEOUT_COUNT;
37
38/*
39 * These globals govern the timeout behavior when attempting a connection to a
40 * TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to
41 * wait for the server to respond to initial connection. Second global,
42 * TftpRRQTimeoutCountMax, gives the number of such connection retries.
43 * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be
44 * positive. The globals are meant to be set (and restored) by code needing
45 * non-standard timeout behavior when initiating a TFTP transfer.
46 */
47ulong TftpRRQTimeoutMSecs = TIMEOUT;
48int TftpRRQTimeoutCountMax = TIMEOUT_COUNT;
49
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +010050static IPaddr_t TftpServerIP;
wdenkfe8c2802002-11-03 00:38:21 +000051static int TftpServerPort; /* The UDP port at their end */
52static int TftpOurPort; /* The UDP port at our end */
53static int TftpTimeoutCount;
wdenk3f85ce22004-02-23 16:11:30 +000054static ulong TftpBlock; /* packet sequence number */
55static ulong TftpLastBlock; /* last packet sequence number received */
56static ulong TftpBlockWrap; /* count of sequence number wraparounds */
57static ulong TftpBlockWrapOffset; /* memory offset due to wrapping */
wdenkfe8c2802002-11-03 00:38:21 +000058static int TftpState;
wdenk3f85ce22004-02-23 16:11:30 +000059
wdenkfe8c2802002-11-03 00:38:21 +000060#define STATE_RRQ 1
61#define STATE_DATA 2
62#define STATE_TOO_LARGE 3
63#define STATE_BAD_MAGIC 4
wdenkfbe4b5c2003-10-06 21:55:32 +000064#define STATE_OACK 5
wdenkfe8c2802002-11-03 00:38:21 +000065
wdenk3f85ce22004-02-23 16:11:30 +000066#define TFTP_BLOCK_SIZE 512 /* default TFTP block size */
67#define TFTP_SEQUENCE_SIZE ((ulong)(1<<16)) /* sequence number is 16 bit */
68
wdenkfe8c2802002-11-03 00:38:21 +000069#define DEFAULT_NAME_LEN (8 + 4 + 1)
70static char default_filename[DEFAULT_NAME_LEN];
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +010071
72#ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
73#define MAX_LEN 128
74#else
75#define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
76#endif
77
78static char tftp_filename[MAX_LEN];
wdenkfe8c2802002-11-03 00:38:21 +000079
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020080#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
Marian Balakowicze6f2e902005-10-11 19:09:42 +020081extern flash_info_t flash_info[];
wdenkfe8c2802002-11-03 00:38:21 +000082#endif
83
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +020084/* 512 is poor choice for ethernet, MTU is typically 1500.
85 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
David Updegraff53a5c422007-06-11 10:41:07 -050086 * almost-MTU block sizes. At least try... fall back to 512 if need be.
Alessandro Rubini89ba81d2009-08-07 13:59:06 +020087 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
David Updegraff53a5c422007-06-11 10:41:07 -050088 */
Alessandro Rubini89ba81d2009-08-07 13:59:06 +020089#ifdef CONFIG_TFTP_BLOCKSIZE
90#define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
91#else
David Updegraff53a5c422007-06-11 10:41:07 -050092#define TFTP_MTU_BLOCKSIZE 1468
Alessandro Rubini89ba81d2009-08-07 13:59:06 +020093#endif
94
David Updegraff53a5c422007-06-11 10:41:07 -050095static unsigned short TftpBlkSize=TFTP_BLOCK_SIZE;
96static unsigned short TftpBlkSizeOption=TFTP_MTU_BLOCKSIZE;
97
98#ifdef CONFIG_MCAST_TFTP
99#include <malloc.h>
100#define MTFTP_BITMAPSIZE 0x1000
101static unsigned *Bitmap;
102static int PrevBitmapHole,Mapsize=MTFTP_BITMAPSIZE;
103static uchar ProhibitMcast=0, MasterClient=0;
104static uchar Multicast=0;
105extern IPaddr_t Mcast_addr;
106static int Mcast_port;
107static ulong TftpEndingBlock; /* can get 'last' block before done..*/
108
109static void parse_multicast_oack(char *pkt,int len);
110
111static void
112mcast_cleanup(void)
113{
114 if (Mcast_addr) eth_mcast_join(Mcast_addr, 0);
115 if (Bitmap) free(Bitmap);
116 Bitmap=NULL;
117 Mcast_addr = Multicast = Mcast_port = 0;
118 TftpEndingBlock = -1;
119}
120
121#endif /* CONFIG_MCAST_TFTP */
122
wdenkfe8c2802002-11-03 00:38:21 +0000123static __inline__ void
124store_block (unsigned block, uchar * src, unsigned len)
125{
David Updegraff53a5c422007-06-11 10:41:07 -0500126 ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
wdenk3f85ce22004-02-23 16:11:30 +0000127 ulong newsize = offset + len;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200128#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
wdenkfe8c2802002-11-03 00:38:21 +0000129 int i, rc = 0;
130
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200131 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenkfe8c2802002-11-03 00:38:21 +0000132 /* start address in flash? */
Jochen Friedrichbe1b0d22008-09-02 11:24:59 +0200133 if (flash_info[i].flash_id == FLASH_UNKNOWN)
134 continue;
wdenkfe8c2802002-11-03 00:38:21 +0000135 if (load_addr + offset >= flash_info[i].start[0]) {
136 rc = 1;
137 break;
138 }
139 }
140
141 if (rc) { /* Flash is destination for this packet */
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200142 rc = flash_write ((char *)src, (ulong)(load_addr+offset), len);
wdenkfe8c2802002-11-03 00:38:21 +0000143 if (rc) {
144 flash_perror (rc);
145 NetState = NETLOOP_FAIL;
146 return;
147 }
148 }
149 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200150#endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000151 {
152 (void)memcpy((void *)(load_addr + offset), src, len);
153 }
David Updegraff53a5c422007-06-11 10:41:07 -0500154#ifdef CONFIG_MCAST_TFTP
155 if (Multicast)
156 ext2_set_bit(block, Bitmap);
157#endif
wdenkfe8c2802002-11-03 00:38:21 +0000158
159 if (NetBootFileXferSize < newsize)
160 NetBootFileXferSize = newsize;
161}
162
163static void TftpSend (void);
164static void TftpTimeout (void);
165
166/**********************************************************************/
167
168static void
169TftpSend (void)
170{
171 volatile uchar * pkt;
172 volatile uchar * xp;
173 int len = 0;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200174 volatile ushort *s;
wdenkfe8c2802002-11-03 00:38:21 +0000175
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200176#ifdef CONFIG_MCAST_TFTP
David Updegraff53a5c422007-06-11 10:41:07 -0500177 /* Multicast TFTP.. non-MasterClients do not ACK data. */
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200178 if (Multicast
179 && (TftpState == STATE_DATA)
180 && (MasterClient == 0))
David Updegraff53a5c422007-06-11 10:41:07 -0500181 return;
182#endif
wdenkfe8c2802002-11-03 00:38:21 +0000183 /*
184 * We will always be sending some sort of packet, so
185 * cobble together the packet headers now.
186 */
wdenka3d991b2004-04-15 21:48:45 +0000187 pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
wdenkfe8c2802002-11-03 00:38:21 +0000188
189 switch (TftpState) {
190
191 case STATE_RRQ:
192 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200193 s = (ushort *)pkt;
194 *s++ = htons(TFTP_RRQ);
195 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000196 strcpy ((char *)pkt, tftp_filename);
197 pkt += strlen(tftp_filename) + 1;
198 strcpy ((char *)pkt, "octet");
199 pkt += 5 /*strlen("octet")*/ + 1;
wdenkfbe4b5c2003-10-06 21:55:32 +0000200 strcpy ((char *)pkt, "timeout");
201 pkt += 7 /*strlen("timeout")*/ + 1;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200202 sprintf((char *)pkt, "%lu", TIMEOUT / 1000);
Robin Getz0ebf04c2009-07-23 03:01:03 -0400203 debug("send option \"timeout %s\"\n", (char *)pkt);
wdenkfbe4b5c2003-10-06 21:55:32 +0000204 pkt += strlen((char *)pkt) + 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500205 /* try for more effic. blk size */
206 pkt += sprintf((char *)pkt,"blksize%c%d%c",
stefano babicef8f2072007-08-21 15:52:33 +0200207 0,TftpBlkSizeOption,0);
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200208#ifdef CONFIG_MCAST_TFTP
David Updegraff53a5c422007-06-11 10:41:07 -0500209 /* Check all preconditions before even trying the option */
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200210 if (!ProhibitMcast
211 && (Bitmap=malloc(Mapsize))
David Updegraff53a5c422007-06-11 10:41:07 -0500212 && eth_get_dev()->mcast) {
213 free(Bitmap);
214 Bitmap=NULL;
215 pkt += sprintf((char *)pkt,"multicast%c%c",0,0);
216 }
217#endif /* CONFIG_MCAST_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000218 len = pkt - xp;
219 break;
220
wdenkfbe4b5c2003-10-06 21:55:32 +0000221 case STATE_OACK:
David Updegraff53a5c422007-06-11 10:41:07 -0500222#ifdef CONFIG_MCAST_TFTP
223 /* My turn! Start at where I need blocks I missed.*/
224 if (Multicast)
225 TftpBlock=ext2_find_next_zero_bit(Bitmap,(Mapsize*8),0);
226 /*..falling..*/
227#endif
228 case STATE_DATA:
wdenkfe8c2802002-11-03 00:38:21 +0000229 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200230 s = (ushort *)pkt;
231 *s++ = htons(TFTP_ACK);
232 *s++ = htons(TftpBlock);
233 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000234 len = pkt - xp;
235 break;
236
237 case STATE_TOO_LARGE:
238 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200239 s = (ushort *)pkt;
240 *s++ = htons(TFTP_ERROR);
241 *s++ = htons(3);
242 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000243 strcpy ((char *)pkt, "File too large");
244 pkt += 14 /*strlen("File too large")*/ + 1;
245 len = pkt - xp;
246 break;
247
248 case STATE_BAD_MAGIC:
249 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200250 s = (ushort *)pkt;
251 *s++ = htons(TFTP_ERROR);
252 *s++ = htons(2);
253 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000254 strcpy ((char *)pkt, "File has bad magic");
255 pkt += 18 /*strlen("File has bad magic")*/ + 1;
256 len = pkt - xp;
257 break;
258 }
259
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100260 NetSendUDPPacket(NetServerEther, TftpServerIP, TftpServerPort, TftpOurPort, len);
wdenkfe8c2802002-11-03 00:38:21 +0000261}
262
263
264static void
265TftpHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
266{
267 ushort proto;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200268 ushort *s;
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200269 int i;
wdenkfe8c2802002-11-03 00:38:21 +0000270
271 if (dest != TftpOurPort) {
David Updegraff53a5c422007-06-11 10:41:07 -0500272#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200273 if (Multicast
David Updegraff53a5c422007-06-11 10:41:07 -0500274 && (!Mcast_port || (dest != Mcast_port)))
275#endif
wdenkfe8c2802002-11-03 00:38:21 +0000276 return;
277 }
278 if (TftpState != STATE_RRQ && src != TftpServerPort) {
279 return;
280 }
281
282 if (len < 2) {
283 return;
284 }
285 len -= 2;
286 /* warning: don't use increment (++) in ntohs() macros!! */
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200287 s = (ushort *)pkt;
288 proto = *s++;
289 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000290 switch (ntohs(proto)) {
291
292 case TFTP_RRQ:
293 case TFTP_WRQ:
294 case TFTP_ACK:
295 break;
296 default:
297 break;
298
wdenkfbe4b5c2003-10-06 21:55:32 +0000299 case TFTP_OACK:
Wolfgang Denkd3717082009-08-10 09:59:10 +0200300 debug("Got OACK: %s %s\n",
301 pkt,
302 pkt + strlen((char *)pkt) + 1);
wdenkfbe4b5c2003-10-06 21:55:32 +0000303 TftpState = STATE_OACK;
304 TftpServerPort = src;
Wolfgang Denk60174742007-08-31 10:01:51 +0200305 /*
306 * Check for 'blksize' option.
307 * Careful: "i" is signed, "len" is unsigned, thus
308 * something like "len-8" may give a *huge* number
309 */
310 for (i=0; i+8<len; i++) {
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200311 if (strcmp ((char*)pkt+i,"blksize") == 0) {
312 TftpBlkSize = (unsigned short)
313 simple_strtoul((char*)pkt+i+8,NULL,10);
Robin Getz0ebf04c2009-07-23 03:01:03 -0400314 debug("Blocksize ack: %s, %d\n",
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200315 (char*)pkt+i+8,TftpBlkSize);
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200316 break;
317 }
David Updegraff53a5c422007-06-11 10:41:07 -0500318 }
319#ifdef CONFIG_MCAST_TFTP
320 parse_multicast_oack((char *)pkt,len-1);
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200321 if ((Multicast) && (!MasterClient))
David Updegraff53a5c422007-06-11 10:41:07 -0500322 TftpState = STATE_DATA; /* passive.. */
323 else
324#endif
wdenkfbe4b5c2003-10-06 21:55:32 +0000325 TftpSend (); /* Send ACK */
326 break;
wdenkfe8c2802002-11-03 00:38:21 +0000327 case TFTP_DATA:
328 if (len < 2)
329 return;
330 len -= 2;
331 TftpBlock = ntohs(*(ushort *)pkt);
wdenk3f85ce22004-02-23 16:11:30 +0000332
333 /*
wdenkcd0a9de2004-02-23 20:48:38 +0000334 * RFC1350 specifies that the first data packet will
335 * have sequence number 1. If we receive a sequence
336 * number of 0 this means that there was a wrap
337 * around of the (16 bit) counter.
wdenk3f85ce22004-02-23 16:11:30 +0000338 */
339 if (TftpBlock == 0) {
340 TftpBlockWrap++;
David Updegraff53a5c422007-06-11 10:41:07 -0500341 TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE;
Wolfgang Denkddd5d9d2006-08-14 22:43:13 +0200342 printf ("\n\t %lu MB received\n\t ", TftpBlockWrapOffset>>20);
wdenk3f85ce22004-02-23 16:11:30 +0000343 } else {
344 if (((TftpBlock - 1) % 10) == 0) {
345 putc ('#');
346 } else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) {
347 puts ("\n\t ");
348 }
wdenkfe8c2802002-11-03 00:38:21 +0000349 }
350
Robin Getz0ebf04c2009-07-23 03:01:03 -0400351 if (TftpState == STATE_RRQ)
352 debug("Server did not acknowledge timeout option!\n");
wdenkfbe4b5c2003-10-06 21:55:32 +0000353
354 if (TftpState == STATE_RRQ || TftpState == STATE_OACK) {
wdenk3f85ce22004-02-23 16:11:30 +0000355 /* first block received */
wdenkfe8c2802002-11-03 00:38:21 +0000356 TftpState = STATE_DATA;
357 TftpServerPort = src;
358 TftpLastBlock = 0;
wdenk3f85ce22004-02-23 16:11:30 +0000359 TftpBlockWrap = 0;
360 TftpBlockWrapOffset = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000361
David Updegraff53a5c422007-06-11 10:41:07 -0500362#ifdef CONFIG_MCAST_TFTP
363 if (Multicast) { /* start!=1 common if mcast */
364 TftpLastBlock = TftpBlock - 1;
365 } else
366#endif
wdenkfe8c2802002-11-03 00:38:21 +0000367 if (TftpBlock != 1) { /* Assertion */
368 printf ("\nTFTP error: "
wdenk3f85ce22004-02-23 16:11:30 +0000369 "First block is not block 1 (%ld)\n"
wdenkfe8c2802002-11-03 00:38:21 +0000370 "Starting again\n\n",
371 TftpBlock);
372 NetStartAgain ();
373 break;
374 }
375 }
376
377 if (TftpBlock == TftpLastBlock) {
378 /*
379 * Same block again; ignore it.
380 */
381 break;
382 }
383
384 TftpLastBlock = TftpBlock;
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200385 TftpTimeoutMSecs = TIMEOUT;
386 TftpTimeoutCountMax = TIMEOUT_COUNT;
387 NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
wdenkfe8c2802002-11-03 00:38:21 +0000388
389 store_block (TftpBlock - 1, pkt + 2, len);
390
391 /*
392 * Acknoledge the block just received, which will prompt
393 * the server for the next one.
394 */
David Updegraff53a5c422007-06-11 10:41:07 -0500395#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200396 /* if I am the MasterClient, actively calculate what my next
397 * needed block is; else I'm passive; not ACKING
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100398 */
David Updegraff53a5c422007-06-11 10:41:07 -0500399 if (Multicast) {
400 if (len < TftpBlkSize) {
401 TftpEndingBlock = TftpBlock;
402 } else if (MasterClient) {
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200403 TftpBlock = PrevBitmapHole =
David Updegraff53a5c422007-06-11 10:41:07 -0500404 ext2_find_next_zero_bit(
405 Bitmap,
406 (Mapsize*8),
407 PrevBitmapHole);
408 if (TftpBlock > ((Mapsize*8) - 1)) {
409 printf ("tftpfile too big\n");
410 /* try to double it and retry */
411 Mapsize<<=1;
412 mcast_cleanup();
413 NetStartAgain ();
414 return;
415 }
416 TftpLastBlock = TftpBlock;
417 }
418 }
419#endif
wdenkfe8c2802002-11-03 00:38:21 +0000420 TftpSend ();
421
David Updegraff53a5c422007-06-11 10:41:07 -0500422#ifdef CONFIG_MCAST_TFTP
423 if (Multicast) {
424 if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
425 puts ("\nMulticast tftp done\n");
426 mcast_cleanup();
427 NetState = NETLOOP_SUCCESS;
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200428 }
David Updegraff53a5c422007-06-11 10:41:07 -0500429 }
430 else
431#endif
432 if (len < TftpBlkSize) {
wdenkfe8c2802002-11-03 00:38:21 +0000433 /*
434 * We received the whole thing. Try to
435 * run it.
436 */
437 puts ("\ndone\n");
438 NetState = NETLOOP_SUCCESS;
439 }
440 break;
441
442 case TFTP_ERROR:
443 printf ("\nTFTP error: '%s' (%d)\n",
444 pkt + 2, ntohs(*(ushort *)pkt));
445 puts ("Starting again\n\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500446#ifdef CONFIG_MCAST_TFTP
447 mcast_cleanup();
448#endif
wdenkfe8c2802002-11-03 00:38:21 +0000449 NetStartAgain ();
450 break;
451 }
452}
453
454
455static void
456TftpTimeout (void)
457{
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200458 if (++TftpTimeoutCount > TftpTimeoutCountMax) {
wdenkfe8c2802002-11-03 00:38:21 +0000459 puts ("\nRetry count exceeded; starting again\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500460#ifdef CONFIG_MCAST_TFTP
461 mcast_cleanup();
462#endif
wdenkfe8c2802002-11-03 00:38:21 +0000463 NetStartAgain ();
464 } else {
465 puts ("T ");
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200466 NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
wdenkfe8c2802002-11-03 00:38:21 +0000467 TftpSend ();
468 }
469}
470
471
472void
473TftpStart (void)
474{
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200475 char *ep; /* Environment pointer */
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200476
477 /* Allow the user to choose tftpblocksize */
478 if ((ep = getenv("tftpblocksize")) != NULL)
479 TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
480 debug("tftp block size is %i\n", TftpBlkSizeOption);
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200481
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100482 TftpServerIP = NetServerIP;
wdenkfe8c2802002-11-03 00:38:21 +0000483 if (BootFile[0] == '\0') {
wdenkfe8c2802002-11-03 00:38:21 +0000484 sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
Wolfgang Denkc43352c2005-08-04 01:09:44 +0200485 NetOurIP & 0xFF,
486 (NetOurIP >> 8) & 0xFF,
487 (NetOurIP >> 16) & 0xFF,
488 (NetOurIP >> 24) & 0xFF );
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100489
490 strncpy(tftp_filename, default_filename, MAX_LEN);
491 tftp_filename[MAX_LEN-1] = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000492
493 printf ("*** Warning: no boot file name; using '%s'\n",
494 tftp_filename);
495 } else {
Jean-Christophe PLAGNIOL-VILLARD38cc09c2008-02-14 08:02:12 +0100496 char *p = strchr (BootFile, ':');
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100497
498 if (p == NULL) {
499 strncpy(tftp_filename, BootFile, MAX_LEN);
500 tftp_filename[MAX_LEN-1] = 0;
501 } else {
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100502 TftpServerIP = string_to_ip (BootFile);
Peter Tyser6a86bb62008-12-01 16:29:38 -0600503 strncpy(tftp_filename, p + 1, MAX_LEN);
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100504 tftp_filename[MAX_LEN-1] = 0;
505 }
wdenkfe8c2802002-11-03 00:38:21 +0000506 }
507
wdenk5bb226e2003-11-17 21:14:37 +0000508#if defined(CONFIG_NET_MULTI)
509 printf ("Using %s device\n", eth_get_name());
510#endif
Mike Frysingerb6446b62009-02-17 00:00:53 -0500511 printf("TFTP from server %pI4"
512 "; our IP address is %pI4", &TftpServerIP, &NetOurIP);
wdenkfe8c2802002-11-03 00:38:21 +0000513
514 /* Check if we need to send across this subnet */
515 if (NetOurGatewayIP && NetOurSubnetMask) {
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100516 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
517 IPaddr_t ServerNet = TftpServerIP & NetOurSubnetMask;
wdenkfe8c2802002-11-03 00:38:21 +0000518
Mike Frysingerb6446b62009-02-17 00:00:53 -0500519 if (OurNet != ServerNet)
520 printf("; sending through gateway %pI4", &NetOurGatewayIP);
wdenkfe8c2802002-11-03 00:38:21 +0000521 }
522 putc ('\n');
523
524 printf ("Filename '%s'.", tftp_filename);
525
526 if (NetBootFileSize) {
527 printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
528 print_size (NetBootFileSize<<9, "");
529 }
530
531 putc ('\n');
532
533 printf ("Load address: 0x%lx\n", load_addr);
534
535 puts ("Loading: *\b");
536
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200537 TftpTimeoutMSecs = TftpRRQTimeoutMSecs;
538 TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
539
540 NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
wdenkfe8c2802002-11-03 00:38:21 +0000541 NetSetHandler (TftpHandler);
542
543 TftpServerPort = WELL_KNOWN_PORT;
544 TftpTimeoutCount = 0;
545 TftpState = STATE_RRQ;
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200546 /* Use a pseudo-random port unless a specific port is set */
wdenkfe8c2802002-11-03 00:38:21 +0000547 TftpOurPort = 1024 + (get_timer(0) % 3072);
David Updegraff53a5c422007-06-11 10:41:07 -0500548
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200549#ifdef CONFIG_TFTP_PORT
Wolfgang Denk28cb9372005-09-24 23:25:46 +0200550 if ((ep = getenv("tftpdstp")) != NULL) {
551 TftpServerPort = simple_strtol(ep, NULL, 10);
552 }
553 if ((ep = getenv("tftpsrcp")) != NULL) {
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200554 TftpOurPort= simple_strtol(ep, NULL, 10);
555 }
556#endif
wdenkfbe4b5c2003-10-06 21:55:32 +0000557 TftpBlock = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000558
wdenk73a8b272003-06-05 19:27:42 +0000559 /* zero out server ether in case the server ip has changed */
560 memset(NetServerEther, 0, 6);
David Updegraff53a5c422007-06-11 10:41:07 -0500561 /* Revert TftpBlkSize to dflt */
562 TftpBlkSize = TFTP_BLOCK_SIZE;
563#ifdef CONFIG_MCAST_TFTP
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100564 mcast_cleanup();
David Updegraff53a5c422007-06-11 10:41:07 -0500565#endif
wdenk73a8b272003-06-05 19:27:42 +0000566
wdenkfe8c2802002-11-03 00:38:21 +0000567 TftpSend ();
568}
569
David Updegraff53a5c422007-06-11 10:41:07 -0500570#ifdef CONFIG_MCAST_TFTP
571/* Credits: atftp project.
572 */
573
574/* pick up BcastAddr, Port, and whether I am [now] the master-client. *
575 * Frame:
576 * +-------+-----------+---+-------~~-------+---+
577 * | opc | multicast | 0 | addr, port, mc | 0 |
578 * +-------+-----------+---+-------~~-------+---+
579 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
580 * I am the new master-client so must send ACKs to DataBlocks. If I am not
581 * master-client, I'm a passive client, gathering what DataBlocks I may and
582 * making note of which ones I got in my bitmask.
583 * In theory, I never go from master->passive..
584 * .. this comes in with pkt already pointing just past opc
585 */
586static void parse_multicast_oack(char *pkt, int len)
587{
588 int i;
589 IPaddr_t addr;
590 char *mc_adr, *port, *mc;
591
592 mc_adr=port=mc=NULL;
593 /* march along looking for 'multicast\0', which has to start at least
594 * 14 bytes back from the end.
595 */
596 for (i=0;i<len-14;i++)
597 if (strcmp (pkt+i,"multicast") == 0)
598 break;
599 if (i >= (len-14)) /* non-Multicast OACK, ign. */
600 return;
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200601
David Updegraff53a5c422007-06-11 10:41:07 -0500602 i+=10; /* strlen multicast */
603 mc_adr = pkt+i;
604 for (;i<len;i++) {
605 if (*(pkt+i) == ',') {
606 *(pkt+i) = '\0';
607 if (port) {
608 mc = pkt+i+1;
609 break;
610 } else {
611 port = pkt+i+1;
612 }
613 }
614 }
615 if (!port || !mc_adr || !mc ) return;
616 if (Multicast && MasterClient) {
617 printf ("I got a OACK as master Client, WRONG!\n");
618 return;
619 }
620 /* ..I now accept packets destined for this MCAST addr, port */
621 if (!Multicast) {
622 if (Bitmap) {
623 printf ("Internal failure! no mcast.\n");
624 free(Bitmap);
625 Bitmap=NULL;
626 ProhibitMcast=1;
627 return ;
628 }
629 /* I malloc instead of pre-declare; so that if the file ends
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200630 * up being too big for this bitmap I can retry
631 */
David Updegraff53a5c422007-06-11 10:41:07 -0500632 if (!(Bitmap = malloc (Mapsize))) {
633 printf ("No Bitmap, no multicast. Sorry.\n");
634 ProhibitMcast=1;
635 return;
636 }
637 memset (Bitmap,0,Mapsize);
638 PrevBitmapHole = 0;
639 Multicast = 1;
640 }
641 addr = string_to_ip(mc_adr);
642 if (Mcast_addr != addr) {
643 if (Mcast_addr)
644 eth_mcast_join(Mcast_addr, 0);
645 if (eth_mcast_join(Mcast_addr=addr, 1)) {
646 printf ("Fail to set mcast, revert to TFTP\n");
647 ProhibitMcast=1;
648 mcast_cleanup();
649 NetStartAgain();
650 }
651 }
652 MasterClient = (unsigned char)simple_strtoul((char *)mc,NULL,10);
653 Mcast_port = (unsigned short)simple_strtoul(port,NULL,10);
654 printf ("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
655 return;
656}
657
658#endif /* Multicast TFTP */
659
Jon Loeliger30b52df2007-08-15 11:55:35 -0500660#endif