blob: 090aa945ffd2455105cc83e609885c652afb6730 [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
wdenkfe8c2802002-11-03 00:38:21 +000013#define WELL_KNOWN_PORT 69 /* Well known TFTP port # */
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +020014#define TIMEOUT 5000UL /* Millisecs to timeout for lost pkt */
wdenkfe8c2802002-11-03 00:38:21 +000015#ifndef CONFIG_NET_RETRY_COUNT
16# define TIMEOUT_COUNT 10 /* # of timeouts before giving up */
17#else
18# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
19#endif
20 /* (for checking the image size) */
21#define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
22
23/*
24 * TFTP operations.
25 */
26#define TFTP_RRQ 1
27#define TFTP_WRQ 2
28#define TFTP_DATA 3
29#define TFTP_ACK 4
30#define TFTP_ERROR 5
wdenkfbe4b5c2003-10-06 21:55:32 +000031#define TFTP_OACK 6
wdenkfe8c2802002-11-03 00:38:21 +000032
Bartlomiej Siekae83cc062008-10-01 15:26:29 +020033static ulong TftpTimeoutMSecs = TIMEOUT;
34static int TftpTimeoutCountMax = TIMEOUT_COUNT;
35
36/*
37 * These globals govern the timeout behavior when attempting a connection to a
38 * TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to
39 * wait for the server to respond to initial connection. Second global,
40 * TftpRRQTimeoutCountMax, gives the number of such connection retries.
41 * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be
42 * positive. The globals are meant to be set (and restored) by code needing
43 * non-standard timeout behavior when initiating a TFTP transfer.
44 */
45ulong TftpRRQTimeoutMSecs = TIMEOUT;
46int TftpRRQTimeoutCountMax = TIMEOUT_COUNT;
47
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +010048static IPaddr_t TftpServerIP;
wdenkfe8c2802002-11-03 00:38:21 +000049static int TftpServerPort; /* The UDP port at their end */
50static int TftpOurPort; /* The UDP port at our end */
51static int TftpTimeoutCount;
wdenk3f85ce22004-02-23 16:11:30 +000052static ulong TftpBlock; /* packet sequence number */
53static ulong TftpLastBlock; /* last packet sequence number received */
54static ulong TftpBlockWrap; /* count of sequence number wraparounds */
55static ulong TftpBlockWrapOffset; /* memory offset due to wrapping */
wdenkfe8c2802002-11-03 00:38:21 +000056static int TftpState;
Robin Getz4fccb812009-08-20 10:50:20 -040057#ifdef CONFIG_TFTP_TSIZE
58static int TftpTsize; /* The file size reported by the server */
59static short TftpNumchars; /* The number of hashes we printed */
60#endif
wdenk3f85ce22004-02-23 16:11:30 +000061
wdenkfe8c2802002-11-03 00:38:21 +000062#define STATE_RRQ 1
63#define STATE_DATA 2
64#define STATE_TOO_LARGE 3
65#define STATE_BAD_MAGIC 4
wdenkfbe4b5c2003-10-06 21:55:32 +000066#define STATE_OACK 5
wdenkfe8c2802002-11-03 00:38:21 +000067
wdenk3f85ce22004-02-23 16:11:30 +000068#define TFTP_BLOCK_SIZE 512 /* default TFTP block size */
69#define TFTP_SEQUENCE_SIZE ((ulong)(1<<16)) /* sequence number is 16 bit */
70
wdenkfe8c2802002-11-03 00:38:21 +000071#define DEFAULT_NAME_LEN (8 + 4 + 1)
72static char default_filename[DEFAULT_NAME_LEN];
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +010073
74#ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
75#define MAX_LEN 128
76#else
77#define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
78#endif
79
80static char tftp_filename[MAX_LEN];
wdenkfe8c2802002-11-03 00:38:21 +000081
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020082#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
Marian Balakowicze6f2e902005-10-11 19:09:42 +020083extern flash_info_t flash_info[];
wdenkfe8c2802002-11-03 00:38:21 +000084#endif
85
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +020086/* 512 is poor choice for ethernet, MTU is typically 1500.
87 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
David Updegraff53a5c422007-06-11 10:41:07 -050088 * almost-MTU block sizes. At least try... fall back to 512 if need be.
Alessandro Rubini89ba81d2009-08-07 13:59:06 +020089 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
David Updegraff53a5c422007-06-11 10:41:07 -050090 */
Alessandro Rubini89ba81d2009-08-07 13:59:06 +020091#ifdef CONFIG_TFTP_BLOCKSIZE
92#define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
93#else
David Updegraff53a5c422007-06-11 10:41:07 -050094#define TFTP_MTU_BLOCKSIZE 1468
Alessandro Rubini89ba81d2009-08-07 13:59:06 +020095#endif
96
David Updegraff53a5c422007-06-11 10:41:07 -050097static unsigned short TftpBlkSize=TFTP_BLOCK_SIZE;
98static unsigned short TftpBlkSizeOption=TFTP_MTU_BLOCKSIZE;
99
100#ifdef CONFIG_MCAST_TFTP
101#include <malloc.h>
102#define MTFTP_BITMAPSIZE 0x1000
103static unsigned *Bitmap;
104static int PrevBitmapHole,Mapsize=MTFTP_BITMAPSIZE;
105static uchar ProhibitMcast=0, MasterClient=0;
106static uchar Multicast=0;
107extern IPaddr_t Mcast_addr;
108static int Mcast_port;
109static ulong TftpEndingBlock; /* can get 'last' block before done..*/
110
111static void parse_multicast_oack(char *pkt,int len);
112
113static void
114mcast_cleanup(void)
115{
116 if (Mcast_addr) eth_mcast_join(Mcast_addr, 0);
117 if (Bitmap) free(Bitmap);
118 Bitmap=NULL;
119 Mcast_addr = Multicast = Mcast_port = 0;
120 TftpEndingBlock = -1;
121}
122
123#endif /* CONFIG_MCAST_TFTP */
124
wdenkfe8c2802002-11-03 00:38:21 +0000125static __inline__ void
126store_block (unsigned block, uchar * src, unsigned len)
127{
David Updegraff53a5c422007-06-11 10:41:07 -0500128 ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
wdenk3f85ce22004-02-23 16:11:30 +0000129 ulong newsize = offset + len;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200130#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
wdenkfe8c2802002-11-03 00:38:21 +0000131 int i, rc = 0;
132
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200133 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenkfe8c2802002-11-03 00:38:21 +0000134 /* start address in flash? */
Jochen Friedrichbe1b0d22008-09-02 11:24:59 +0200135 if (flash_info[i].flash_id == FLASH_UNKNOWN)
136 continue;
wdenkfe8c2802002-11-03 00:38:21 +0000137 if (load_addr + offset >= flash_info[i].start[0]) {
138 rc = 1;
139 break;
140 }
141 }
142
143 if (rc) { /* Flash is destination for this packet */
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200144 rc = flash_write ((char *)src, (ulong)(load_addr+offset), len);
wdenkfe8c2802002-11-03 00:38:21 +0000145 if (rc) {
146 flash_perror (rc);
147 NetState = NETLOOP_FAIL;
148 return;
149 }
150 }
151 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200152#endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000153 {
154 (void)memcpy((void *)(load_addr + offset), src, len);
155 }
David Updegraff53a5c422007-06-11 10:41:07 -0500156#ifdef CONFIG_MCAST_TFTP
157 if (Multicast)
158 ext2_set_bit(block, Bitmap);
159#endif
wdenkfe8c2802002-11-03 00:38:21 +0000160
161 if (NetBootFileXferSize < newsize)
162 NetBootFileXferSize = newsize;
163}
164
165static void TftpSend (void);
166static void TftpTimeout (void);
167
168/**********************************************************************/
169
170static void
171TftpSend (void)
172{
173 volatile uchar * pkt;
174 volatile uchar * xp;
175 int len = 0;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200176 volatile ushort *s;
wdenkfe8c2802002-11-03 00:38:21 +0000177
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200178#ifdef CONFIG_MCAST_TFTP
David Updegraff53a5c422007-06-11 10:41:07 -0500179 /* Multicast TFTP.. non-MasterClients do not ACK data. */
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200180 if (Multicast
181 && (TftpState == STATE_DATA)
182 && (MasterClient == 0))
David Updegraff53a5c422007-06-11 10:41:07 -0500183 return;
184#endif
wdenkfe8c2802002-11-03 00:38:21 +0000185 /*
186 * We will always be sending some sort of packet, so
187 * cobble together the packet headers now.
188 */
wdenka3d991b2004-04-15 21:48:45 +0000189 pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
wdenkfe8c2802002-11-03 00:38:21 +0000190
191 switch (TftpState) {
192
193 case STATE_RRQ:
194 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200195 s = (ushort *)pkt;
196 *s++ = htons(TFTP_RRQ);
197 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000198 strcpy ((char *)pkt, tftp_filename);
199 pkt += strlen(tftp_filename) + 1;
200 strcpy ((char *)pkt, "octet");
201 pkt += 5 /*strlen("octet")*/ + 1;
wdenkfbe4b5c2003-10-06 21:55:32 +0000202 strcpy ((char *)pkt, "timeout");
203 pkt += 7 /*strlen("timeout")*/ + 1;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200204 sprintf((char *)pkt, "%lu", TIMEOUT / 1000);
Robin Getz0ebf04c2009-07-23 03:01:03 -0400205 debug("send option \"timeout %s\"\n", (char *)pkt);
wdenkfbe4b5c2003-10-06 21:55:32 +0000206 pkt += strlen((char *)pkt) + 1;
Robin Getz4fccb812009-08-20 10:50:20 -0400207#ifdef CONFIG_TFTP_TSIZE
208 memcpy((char *)pkt, "tsize\0000\0", 8);
209 pkt += 8;
210#endif
David Updegraff53a5c422007-06-11 10:41:07 -0500211 /* try for more effic. blk size */
212 pkt += sprintf((char *)pkt,"blksize%c%d%c",
stefano babicef8f2072007-08-21 15:52:33 +0200213 0,TftpBlkSizeOption,0);
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200214#ifdef CONFIG_MCAST_TFTP
David Updegraff53a5c422007-06-11 10:41:07 -0500215 /* Check all preconditions before even trying the option */
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200216 if (!ProhibitMcast
217 && (Bitmap=malloc(Mapsize))
David Updegraff53a5c422007-06-11 10:41:07 -0500218 && eth_get_dev()->mcast) {
219 free(Bitmap);
220 Bitmap=NULL;
221 pkt += sprintf((char *)pkt,"multicast%c%c",0,0);
222 }
223#endif /* CONFIG_MCAST_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000224 len = pkt - xp;
225 break;
226
wdenkfbe4b5c2003-10-06 21:55:32 +0000227 case STATE_OACK:
David Updegraff53a5c422007-06-11 10:41:07 -0500228#ifdef CONFIG_MCAST_TFTP
229 /* My turn! Start at where I need blocks I missed.*/
230 if (Multicast)
231 TftpBlock=ext2_find_next_zero_bit(Bitmap,(Mapsize*8),0);
232 /*..falling..*/
233#endif
234 case STATE_DATA:
wdenkfe8c2802002-11-03 00:38:21 +0000235 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200236 s = (ushort *)pkt;
237 *s++ = htons(TFTP_ACK);
238 *s++ = htons(TftpBlock);
239 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000240 len = pkt - xp;
241 break;
242
243 case STATE_TOO_LARGE:
244 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200245 s = (ushort *)pkt;
246 *s++ = htons(TFTP_ERROR);
247 *s++ = htons(3);
248 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000249 strcpy ((char *)pkt, "File too large");
250 pkt += 14 /*strlen("File too large")*/ + 1;
251 len = pkt - xp;
252 break;
253
254 case STATE_BAD_MAGIC:
255 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200256 s = (ushort *)pkt;
257 *s++ = htons(TFTP_ERROR);
258 *s++ = htons(2);
259 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000260 strcpy ((char *)pkt, "File has bad magic");
261 pkt += 18 /*strlen("File has bad magic")*/ + 1;
262 len = pkt - xp;
263 break;
264 }
265
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100266 NetSendUDPPacket(NetServerEther, TftpServerIP, TftpServerPort, TftpOurPort, len);
wdenkfe8c2802002-11-03 00:38:21 +0000267}
268
269
270static void
271TftpHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
272{
273 ushort proto;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200274 ushort *s;
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200275 int i;
wdenkfe8c2802002-11-03 00:38:21 +0000276
277 if (dest != TftpOurPort) {
David Updegraff53a5c422007-06-11 10:41:07 -0500278#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200279 if (Multicast
David Updegraff53a5c422007-06-11 10:41:07 -0500280 && (!Mcast_port || (dest != Mcast_port)))
281#endif
wdenkfe8c2802002-11-03 00:38:21 +0000282 return;
283 }
284 if (TftpState != STATE_RRQ && src != TftpServerPort) {
285 return;
286 }
287
288 if (len < 2) {
289 return;
290 }
291 len -= 2;
292 /* warning: don't use increment (++) in ntohs() macros!! */
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200293 s = (ushort *)pkt;
294 proto = *s++;
295 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000296 switch (ntohs(proto)) {
297
298 case TFTP_RRQ:
299 case TFTP_WRQ:
300 case TFTP_ACK:
301 break;
302 default:
303 break;
304
wdenkfbe4b5c2003-10-06 21:55:32 +0000305 case TFTP_OACK:
Wolfgang Denkd3717082009-08-10 09:59:10 +0200306 debug("Got OACK: %s %s\n",
307 pkt,
308 pkt + strlen((char *)pkt) + 1);
wdenkfbe4b5c2003-10-06 21:55:32 +0000309 TftpState = STATE_OACK;
310 TftpServerPort = src;
Wolfgang Denk60174742007-08-31 10:01:51 +0200311 /*
312 * Check for 'blksize' option.
313 * Careful: "i" is signed, "len" is unsigned, thus
314 * something like "len-8" may give a *huge* number
315 */
316 for (i=0; i+8<len; i++) {
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200317 if (strcmp ((char*)pkt+i,"blksize") == 0) {
318 TftpBlkSize = (unsigned short)
319 simple_strtoul((char*)pkt+i+8,NULL,10);
Robin Getz0ebf04c2009-07-23 03:01:03 -0400320 debug("Blocksize ack: %s, %d\n",
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200321 (char*)pkt+i+8,TftpBlkSize);
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200322 }
Robin Getz4fccb812009-08-20 10:50:20 -0400323#ifdef CONFIG_TFTP_TSIZE
324 if (strcmp ((char*)pkt+i,"tsize") == 0) {
325 TftpTsize = simple_strtoul((char*)pkt+i+6,NULL,10);
326 debug("size = %s, %d\n",
327 (char*)pkt+i+6, TftpTsize);
328 }
329#endif
David Updegraff53a5c422007-06-11 10:41:07 -0500330 }
331#ifdef CONFIG_MCAST_TFTP
332 parse_multicast_oack((char *)pkt,len-1);
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200333 if ((Multicast) && (!MasterClient))
David Updegraff53a5c422007-06-11 10:41:07 -0500334 TftpState = STATE_DATA; /* passive.. */
335 else
336#endif
wdenkfbe4b5c2003-10-06 21:55:32 +0000337 TftpSend (); /* Send ACK */
338 break;
wdenkfe8c2802002-11-03 00:38:21 +0000339 case TFTP_DATA:
340 if (len < 2)
341 return;
342 len -= 2;
343 TftpBlock = ntohs(*(ushort *)pkt);
wdenk3f85ce22004-02-23 16:11:30 +0000344
345 /*
wdenkcd0a9de2004-02-23 20:48:38 +0000346 * RFC1350 specifies that the first data packet will
347 * have sequence number 1. If we receive a sequence
348 * number of 0 this means that there was a wrap
349 * around of the (16 bit) counter.
wdenk3f85ce22004-02-23 16:11:30 +0000350 */
351 if (TftpBlock == 0) {
352 TftpBlockWrap++;
David Updegraff53a5c422007-06-11 10:41:07 -0500353 TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE;
Wolfgang Denkddd5d9d2006-08-14 22:43:13 +0200354 printf ("\n\t %lu MB received\n\t ", TftpBlockWrapOffset>>20);
Robin Getz4fccb812009-08-20 10:50:20 -0400355 }
356#ifdef CONFIG_TFTP_TSIZE
357 else if (TftpTsize) {
358 while (TftpNumchars < NetBootFileXferSize * 50 / TftpTsize) {
359 putc('#');
360 TftpNumchars++;
361 }
362 }
363#endif
364 else {
wdenk3f85ce22004-02-23 16:11:30 +0000365 if (((TftpBlock - 1) % 10) == 0) {
366 putc ('#');
367 } else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) {
368 puts ("\n\t ");
369 }
wdenkfe8c2802002-11-03 00:38:21 +0000370 }
371
Robin Getz0ebf04c2009-07-23 03:01:03 -0400372 if (TftpState == STATE_RRQ)
373 debug("Server did not acknowledge timeout option!\n");
wdenkfbe4b5c2003-10-06 21:55:32 +0000374
375 if (TftpState == STATE_RRQ || TftpState == STATE_OACK) {
wdenk3f85ce22004-02-23 16:11:30 +0000376 /* first block received */
wdenkfe8c2802002-11-03 00:38:21 +0000377 TftpState = STATE_DATA;
378 TftpServerPort = src;
379 TftpLastBlock = 0;
wdenk3f85ce22004-02-23 16:11:30 +0000380 TftpBlockWrap = 0;
381 TftpBlockWrapOffset = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000382
David Updegraff53a5c422007-06-11 10:41:07 -0500383#ifdef CONFIG_MCAST_TFTP
384 if (Multicast) { /* start!=1 common if mcast */
385 TftpLastBlock = TftpBlock - 1;
386 } else
387#endif
wdenkfe8c2802002-11-03 00:38:21 +0000388 if (TftpBlock != 1) { /* Assertion */
389 printf ("\nTFTP error: "
wdenk3f85ce22004-02-23 16:11:30 +0000390 "First block is not block 1 (%ld)\n"
wdenkfe8c2802002-11-03 00:38:21 +0000391 "Starting again\n\n",
392 TftpBlock);
393 NetStartAgain ();
394 break;
395 }
396 }
397
398 if (TftpBlock == TftpLastBlock) {
399 /*
400 * Same block again; ignore it.
401 */
402 break;
403 }
404
405 TftpLastBlock = TftpBlock;
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200406 TftpTimeoutMSecs = TIMEOUT;
407 TftpTimeoutCountMax = TIMEOUT_COUNT;
408 NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
wdenkfe8c2802002-11-03 00:38:21 +0000409
410 store_block (TftpBlock - 1, pkt + 2, len);
411
412 /*
413 * Acknoledge the block just received, which will prompt
414 * the server for the next one.
415 */
David Updegraff53a5c422007-06-11 10:41:07 -0500416#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200417 /* if I am the MasterClient, actively calculate what my next
418 * needed block is; else I'm passive; not ACKING
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100419 */
David Updegraff53a5c422007-06-11 10:41:07 -0500420 if (Multicast) {
421 if (len < TftpBlkSize) {
422 TftpEndingBlock = TftpBlock;
423 } else if (MasterClient) {
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200424 TftpBlock = PrevBitmapHole =
David Updegraff53a5c422007-06-11 10:41:07 -0500425 ext2_find_next_zero_bit(
426 Bitmap,
427 (Mapsize*8),
428 PrevBitmapHole);
429 if (TftpBlock > ((Mapsize*8) - 1)) {
430 printf ("tftpfile too big\n");
431 /* try to double it and retry */
432 Mapsize<<=1;
433 mcast_cleanup();
434 NetStartAgain ();
435 return;
436 }
437 TftpLastBlock = TftpBlock;
438 }
439 }
440#endif
wdenkfe8c2802002-11-03 00:38:21 +0000441 TftpSend ();
442
David Updegraff53a5c422007-06-11 10:41:07 -0500443#ifdef CONFIG_MCAST_TFTP
444 if (Multicast) {
445 if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
446 puts ("\nMulticast tftp done\n");
447 mcast_cleanup();
448 NetState = NETLOOP_SUCCESS;
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200449 }
David Updegraff53a5c422007-06-11 10:41:07 -0500450 }
451 else
452#endif
453 if (len < TftpBlkSize) {
wdenkfe8c2802002-11-03 00:38:21 +0000454 /*
455 * We received the whole thing. Try to
456 * run it.
457 */
Robin Getz4fccb812009-08-20 10:50:20 -0400458#ifdef CONFIG_TFTP_TSIZE
459 /* Print out the hash marks for the last packet received */
460 while (TftpTsize && TftpNumchars < 49) {
461 putc('#');
462 TftpNumchars++;
463 }
464#endif
wdenkfe8c2802002-11-03 00:38:21 +0000465 puts ("\ndone\n");
466 NetState = NETLOOP_SUCCESS;
467 }
468 break;
469
470 case TFTP_ERROR:
471 printf ("\nTFTP error: '%s' (%d)\n",
472 pkt + 2, ntohs(*(ushort *)pkt));
473 puts ("Starting again\n\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500474#ifdef CONFIG_MCAST_TFTP
475 mcast_cleanup();
476#endif
wdenkfe8c2802002-11-03 00:38:21 +0000477 NetStartAgain ();
478 break;
479 }
480}
481
482
483static void
484TftpTimeout (void)
485{
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200486 if (++TftpTimeoutCount > TftpTimeoutCountMax) {
wdenkfe8c2802002-11-03 00:38:21 +0000487 puts ("\nRetry count exceeded; starting again\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500488#ifdef CONFIG_MCAST_TFTP
489 mcast_cleanup();
490#endif
wdenkfe8c2802002-11-03 00:38:21 +0000491 NetStartAgain ();
492 } else {
493 puts ("T ");
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200494 NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
wdenkfe8c2802002-11-03 00:38:21 +0000495 TftpSend ();
496 }
497}
498
499
500void
501TftpStart (void)
502{
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200503 char *ep; /* Environment pointer */
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200504
505 /* Allow the user to choose tftpblocksize */
506 if ((ep = getenv("tftpblocksize")) != NULL)
507 TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
508 debug("tftp block size is %i\n", TftpBlkSizeOption);
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200509
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100510 TftpServerIP = NetServerIP;
wdenkfe8c2802002-11-03 00:38:21 +0000511 if (BootFile[0] == '\0') {
wdenkfe8c2802002-11-03 00:38:21 +0000512 sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
Wolfgang Denkc43352c2005-08-04 01:09:44 +0200513 NetOurIP & 0xFF,
514 (NetOurIP >> 8) & 0xFF,
515 (NetOurIP >> 16) & 0xFF,
516 (NetOurIP >> 24) & 0xFF );
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100517
518 strncpy(tftp_filename, default_filename, MAX_LEN);
519 tftp_filename[MAX_LEN-1] = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000520
521 printf ("*** Warning: no boot file name; using '%s'\n",
522 tftp_filename);
523 } else {
Jean-Christophe PLAGNIOL-VILLARD38cc09c2008-02-14 08:02:12 +0100524 char *p = strchr (BootFile, ':');
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100525
526 if (p == NULL) {
527 strncpy(tftp_filename, BootFile, MAX_LEN);
528 tftp_filename[MAX_LEN-1] = 0;
529 } else {
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100530 TftpServerIP = string_to_ip (BootFile);
Peter Tyser6a86bb62008-12-01 16:29:38 -0600531 strncpy(tftp_filename, p + 1, MAX_LEN);
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100532 tftp_filename[MAX_LEN-1] = 0;
533 }
wdenkfe8c2802002-11-03 00:38:21 +0000534 }
535
wdenk5bb226e2003-11-17 21:14:37 +0000536#if defined(CONFIG_NET_MULTI)
537 printf ("Using %s device\n", eth_get_name());
538#endif
Mike Frysingerb6446b62009-02-17 00:00:53 -0500539 printf("TFTP from server %pI4"
540 "; our IP address is %pI4", &TftpServerIP, &NetOurIP);
wdenkfe8c2802002-11-03 00:38:21 +0000541
542 /* Check if we need to send across this subnet */
543 if (NetOurGatewayIP && NetOurSubnetMask) {
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100544 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
545 IPaddr_t ServerNet = TftpServerIP & NetOurSubnetMask;
wdenkfe8c2802002-11-03 00:38:21 +0000546
Mike Frysingerb6446b62009-02-17 00:00:53 -0500547 if (OurNet != ServerNet)
548 printf("; sending through gateway %pI4", &NetOurGatewayIP);
wdenkfe8c2802002-11-03 00:38:21 +0000549 }
550 putc ('\n');
551
552 printf ("Filename '%s'.", tftp_filename);
553
554 if (NetBootFileSize) {
555 printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
556 print_size (NetBootFileSize<<9, "");
557 }
558
559 putc ('\n');
560
561 printf ("Load address: 0x%lx\n", load_addr);
562
563 puts ("Loading: *\b");
564
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200565 TftpTimeoutMSecs = TftpRRQTimeoutMSecs;
566 TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
567
568 NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
wdenkfe8c2802002-11-03 00:38:21 +0000569 NetSetHandler (TftpHandler);
570
571 TftpServerPort = WELL_KNOWN_PORT;
572 TftpTimeoutCount = 0;
573 TftpState = STATE_RRQ;
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200574 /* Use a pseudo-random port unless a specific port is set */
wdenkfe8c2802002-11-03 00:38:21 +0000575 TftpOurPort = 1024 + (get_timer(0) % 3072);
David Updegraff53a5c422007-06-11 10:41:07 -0500576
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200577#ifdef CONFIG_TFTP_PORT
Wolfgang Denk28cb9372005-09-24 23:25:46 +0200578 if ((ep = getenv("tftpdstp")) != NULL) {
579 TftpServerPort = simple_strtol(ep, NULL, 10);
580 }
581 if ((ep = getenv("tftpsrcp")) != NULL) {
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200582 TftpOurPort= simple_strtol(ep, NULL, 10);
583 }
584#endif
wdenkfbe4b5c2003-10-06 21:55:32 +0000585 TftpBlock = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000586
wdenk73a8b272003-06-05 19:27:42 +0000587 /* zero out server ether in case the server ip has changed */
588 memset(NetServerEther, 0, 6);
David Updegraff53a5c422007-06-11 10:41:07 -0500589 /* Revert TftpBlkSize to dflt */
590 TftpBlkSize = TFTP_BLOCK_SIZE;
591#ifdef CONFIG_MCAST_TFTP
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100592 mcast_cleanup();
David Updegraff53a5c422007-06-11 10:41:07 -0500593#endif
Robin Getz4fccb812009-08-20 10:50:20 -0400594#ifdef CONFIG_TFTP_TSIZE
595 TftpTsize = 0;
596 TftpNumchars = 0;
597#endif
wdenk73a8b272003-06-05 19:27:42 +0000598
wdenkfe8c2802002-11-03 00:38:21 +0000599 TftpSend ();
600}
601
David Updegraff53a5c422007-06-11 10:41:07 -0500602#ifdef CONFIG_MCAST_TFTP
603/* Credits: atftp project.
604 */
605
606/* pick up BcastAddr, Port, and whether I am [now] the master-client. *
607 * Frame:
608 * +-------+-----------+---+-------~~-------+---+
609 * | opc | multicast | 0 | addr, port, mc | 0 |
610 * +-------+-----------+---+-------~~-------+---+
611 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
612 * I am the new master-client so must send ACKs to DataBlocks. If I am not
613 * master-client, I'm a passive client, gathering what DataBlocks I may and
614 * making note of which ones I got in my bitmask.
615 * In theory, I never go from master->passive..
616 * .. this comes in with pkt already pointing just past opc
617 */
618static void parse_multicast_oack(char *pkt, int len)
619{
620 int i;
621 IPaddr_t addr;
622 char *mc_adr, *port, *mc;
623
624 mc_adr=port=mc=NULL;
625 /* march along looking for 'multicast\0', which has to start at least
626 * 14 bytes back from the end.
627 */
628 for (i=0;i<len-14;i++)
629 if (strcmp (pkt+i,"multicast") == 0)
630 break;
631 if (i >= (len-14)) /* non-Multicast OACK, ign. */
632 return;
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200633
David Updegraff53a5c422007-06-11 10:41:07 -0500634 i+=10; /* strlen multicast */
635 mc_adr = pkt+i;
636 for (;i<len;i++) {
637 if (*(pkt+i) == ',') {
638 *(pkt+i) = '\0';
639 if (port) {
640 mc = pkt+i+1;
641 break;
642 } else {
643 port = pkt+i+1;
644 }
645 }
646 }
647 if (!port || !mc_adr || !mc ) return;
648 if (Multicast && MasterClient) {
649 printf ("I got a OACK as master Client, WRONG!\n");
650 return;
651 }
652 /* ..I now accept packets destined for this MCAST addr, port */
653 if (!Multicast) {
654 if (Bitmap) {
655 printf ("Internal failure! no mcast.\n");
656 free(Bitmap);
657 Bitmap=NULL;
658 ProhibitMcast=1;
659 return ;
660 }
661 /* I malloc instead of pre-declare; so that if the file ends
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200662 * up being too big for this bitmap I can retry
663 */
David Updegraff53a5c422007-06-11 10:41:07 -0500664 if (!(Bitmap = malloc (Mapsize))) {
665 printf ("No Bitmap, no multicast. Sorry.\n");
666 ProhibitMcast=1;
667 return;
668 }
669 memset (Bitmap,0,Mapsize);
670 PrevBitmapHole = 0;
671 Multicast = 1;
672 }
673 addr = string_to_ip(mc_adr);
674 if (Mcast_addr != addr) {
675 if (Mcast_addr)
676 eth_mcast_join(Mcast_addr, 0);
677 if (eth_mcast_join(Mcast_addr=addr, 1)) {
678 printf ("Fail to set mcast, revert to TFTP\n");
679 ProhibitMcast=1;
680 mcast_cleanup();
681 NetStartAgain();
682 }
683 }
684 MasterClient = (unsigned char)simple_strtoul((char *)mc,NULL,10);
685 Mcast_port = (unsigned short)simple_strtoul(port,NULL,10);
686 printf ("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
687 return;
688}
689
690#endif /* Multicast TFTP */