blob: cc60a3bd1ca888705b6b0375dfba61aa535291cb [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;
Robin Getz4fccb812009-08-20 10:50:20 -040059#ifdef CONFIG_TFTP_TSIZE
60static int TftpTsize; /* The file size reported by the server */
61static short TftpNumchars; /* The number of hashes we printed */
62#endif
wdenk3f85ce22004-02-23 16:11:30 +000063
wdenkfe8c2802002-11-03 00:38:21 +000064#define STATE_RRQ 1
65#define STATE_DATA 2
66#define STATE_TOO_LARGE 3
67#define STATE_BAD_MAGIC 4
wdenkfbe4b5c2003-10-06 21:55:32 +000068#define STATE_OACK 5
wdenkfe8c2802002-11-03 00:38:21 +000069
wdenk3f85ce22004-02-23 16:11:30 +000070#define TFTP_BLOCK_SIZE 512 /* default TFTP block size */
71#define TFTP_SEQUENCE_SIZE ((ulong)(1<<16)) /* sequence number is 16 bit */
72
wdenkfe8c2802002-11-03 00:38:21 +000073#define DEFAULT_NAME_LEN (8 + 4 + 1)
74static char default_filename[DEFAULT_NAME_LEN];
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +010075
76#ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
77#define MAX_LEN 128
78#else
79#define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
80#endif
81
82static char tftp_filename[MAX_LEN];
wdenkfe8c2802002-11-03 00:38:21 +000083
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020084#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
Marian Balakowicze6f2e902005-10-11 19:09:42 +020085extern flash_info_t flash_info[];
wdenkfe8c2802002-11-03 00:38:21 +000086#endif
87
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +020088/* 512 is poor choice for ethernet, MTU is typically 1500.
89 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
David Updegraff53a5c422007-06-11 10:41:07 -050090 * almost-MTU block sizes. At least try... fall back to 512 if need be.
Alessandro Rubini89ba81d2009-08-07 13:59:06 +020091 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
David Updegraff53a5c422007-06-11 10:41:07 -050092 */
Alessandro Rubini89ba81d2009-08-07 13:59:06 +020093#ifdef CONFIG_TFTP_BLOCKSIZE
94#define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
95#else
David Updegraff53a5c422007-06-11 10:41:07 -050096#define TFTP_MTU_BLOCKSIZE 1468
Alessandro Rubini89ba81d2009-08-07 13:59:06 +020097#endif
98
David Updegraff53a5c422007-06-11 10:41:07 -050099static unsigned short TftpBlkSize=TFTP_BLOCK_SIZE;
100static unsigned short TftpBlkSizeOption=TFTP_MTU_BLOCKSIZE;
101
102#ifdef CONFIG_MCAST_TFTP
103#include <malloc.h>
104#define MTFTP_BITMAPSIZE 0x1000
105static unsigned *Bitmap;
106static int PrevBitmapHole,Mapsize=MTFTP_BITMAPSIZE;
107static uchar ProhibitMcast=0, MasterClient=0;
108static uchar Multicast=0;
109extern IPaddr_t Mcast_addr;
110static int Mcast_port;
111static ulong TftpEndingBlock; /* can get 'last' block before done..*/
112
113static void parse_multicast_oack(char *pkt,int len);
114
115static void
116mcast_cleanup(void)
117{
118 if (Mcast_addr) eth_mcast_join(Mcast_addr, 0);
119 if (Bitmap) free(Bitmap);
120 Bitmap=NULL;
121 Mcast_addr = Multicast = Mcast_port = 0;
122 TftpEndingBlock = -1;
123}
124
125#endif /* CONFIG_MCAST_TFTP */
126
wdenkfe8c2802002-11-03 00:38:21 +0000127static __inline__ void
128store_block (unsigned block, uchar * src, unsigned len)
129{
David Updegraff53a5c422007-06-11 10:41:07 -0500130 ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
wdenk3f85ce22004-02-23 16:11:30 +0000131 ulong newsize = offset + len;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200132#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
wdenkfe8c2802002-11-03 00:38:21 +0000133 int i, rc = 0;
134
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200135 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenkfe8c2802002-11-03 00:38:21 +0000136 /* start address in flash? */
Jochen Friedrichbe1b0d22008-09-02 11:24:59 +0200137 if (flash_info[i].flash_id == FLASH_UNKNOWN)
138 continue;
wdenkfe8c2802002-11-03 00:38:21 +0000139 if (load_addr + offset >= flash_info[i].start[0]) {
140 rc = 1;
141 break;
142 }
143 }
144
145 if (rc) { /* Flash is destination for this packet */
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200146 rc = flash_write ((char *)src, (ulong)(load_addr+offset), len);
wdenkfe8c2802002-11-03 00:38:21 +0000147 if (rc) {
148 flash_perror (rc);
149 NetState = NETLOOP_FAIL;
150 return;
151 }
152 }
153 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200154#endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000155 {
156 (void)memcpy((void *)(load_addr + offset), src, len);
157 }
David Updegraff53a5c422007-06-11 10:41:07 -0500158#ifdef CONFIG_MCAST_TFTP
159 if (Multicast)
160 ext2_set_bit(block, Bitmap);
161#endif
wdenkfe8c2802002-11-03 00:38:21 +0000162
163 if (NetBootFileXferSize < newsize)
164 NetBootFileXferSize = newsize;
165}
166
167static void TftpSend (void);
168static void TftpTimeout (void);
169
170/**********************************************************************/
171
172static void
173TftpSend (void)
174{
175 volatile uchar * pkt;
176 volatile uchar * xp;
177 int len = 0;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200178 volatile ushort *s;
wdenkfe8c2802002-11-03 00:38:21 +0000179
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200180#ifdef CONFIG_MCAST_TFTP
David Updegraff53a5c422007-06-11 10:41:07 -0500181 /* Multicast TFTP.. non-MasterClients do not ACK data. */
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200182 if (Multicast
183 && (TftpState == STATE_DATA)
184 && (MasterClient == 0))
David Updegraff53a5c422007-06-11 10:41:07 -0500185 return;
186#endif
wdenkfe8c2802002-11-03 00:38:21 +0000187 /*
188 * We will always be sending some sort of packet, so
189 * cobble together the packet headers now.
190 */
wdenka3d991b2004-04-15 21:48:45 +0000191 pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
wdenkfe8c2802002-11-03 00:38:21 +0000192
193 switch (TftpState) {
194
195 case STATE_RRQ:
196 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200197 s = (ushort *)pkt;
198 *s++ = htons(TFTP_RRQ);
199 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000200 strcpy ((char *)pkt, tftp_filename);
201 pkt += strlen(tftp_filename) + 1;
202 strcpy ((char *)pkt, "octet");
203 pkt += 5 /*strlen("octet")*/ + 1;
wdenkfbe4b5c2003-10-06 21:55:32 +0000204 strcpy ((char *)pkt, "timeout");
205 pkt += 7 /*strlen("timeout")*/ + 1;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200206 sprintf((char *)pkt, "%lu", TIMEOUT / 1000);
Robin Getz0ebf04c2009-07-23 03:01:03 -0400207 debug("send option \"timeout %s\"\n", (char *)pkt);
wdenkfbe4b5c2003-10-06 21:55:32 +0000208 pkt += strlen((char *)pkt) + 1;
Robin Getz4fccb812009-08-20 10:50:20 -0400209#ifdef CONFIG_TFTP_TSIZE
210 memcpy((char *)pkt, "tsize\0000\0", 8);
211 pkt += 8;
212#endif
David Updegraff53a5c422007-06-11 10:41:07 -0500213 /* try for more effic. blk size */
214 pkt += sprintf((char *)pkt,"blksize%c%d%c",
stefano babicef8f2072007-08-21 15:52:33 +0200215 0,TftpBlkSizeOption,0);
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200216#ifdef CONFIG_MCAST_TFTP
David Updegraff53a5c422007-06-11 10:41:07 -0500217 /* Check all preconditions before even trying the option */
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200218 if (!ProhibitMcast
219 && (Bitmap=malloc(Mapsize))
David Updegraff53a5c422007-06-11 10:41:07 -0500220 && eth_get_dev()->mcast) {
221 free(Bitmap);
222 Bitmap=NULL;
223 pkt += sprintf((char *)pkt,"multicast%c%c",0,0);
224 }
225#endif /* CONFIG_MCAST_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000226 len = pkt - xp;
227 break;
228
wdenkfbe4b5c2003-10-06 21:55:32 +0000229 case STATE_OACK:
David Updegraff53a5c422007-06-11 10:41:07 -0500230#ifdef CONFIG_MCAST_TFTP
231 /* My turn! Start at where I need blocks I missed.*/
232 if (Multicast)
233 TftpBlock=ext2_find_next_zero_bit(Bitmap,(Mapsize*8),0);
234 /*..falling..*/
235#endif
236 case STATE_DATA:
wdenkfe8c2802002-11-03 00:38:21 +0000237 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200238 s = (ushort *)pkt;
239 *s++ = htons(TFTP_ACK);
240 *s++ = htons(TftpBlock);
241 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000242 len = pkt - xp;
243 break;
244
245 case STATE_TOO_LARGE:
246 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200247 s = (ushort *)pkt;
248 *s++ = htons(TFTP_ERROR);
249 *s++ = htons(3);
250 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000251 strcpy ((char *)pkt, "File too large");
252 pkt += 14 /*strlen("File too large")*/ + 1;
253 len = pkt - xp;
254 break;
255
256 case STATE_BAD_MAGIC:
257 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200258 s = (ushort *)pkt;
259 *s++ = htons(TFTP_ERROR);
260 *s++ = htons(2);
261 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000262 strcpy ((char *)pkt, "File has bad magic");
263 pkt += 18 /*strlen("File has bad magic")*/ + 1;
264 len = pkt - xp;
265 break;
266 }
267
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100268 NetSendUDPPacket(NetServerEther, TftpServerIP, TftpServerPort, TftpOurPort, len);
wdenkfe8c2802002-11-03 00:38:21 +0000269}
270
271
272static void
273TftpHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
274{
275 ushort proto;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200276 ushort *s;
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200277 int i;
wdenkfe8c2802002-11-03 00:38:21 +0000278
279 if (dest != TftpOurPort) {
David Updegraff53a5c422007-06-11 10:41:07 -0500280#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200281 if (Multicast
David Updegraff53a5c422007-06-11 10:41:07 -0500282 && (!Mcast_port || (dest != Mcast_port)))
283#endif
wdenkfe8c2802002-11-03 00:38:21 +0000284 return;
285 }
286 if (TftpState != STATE_RRQ && src != TftpServerPort) {
287 return;
288 }
289
290 if (len < 2) {
291 return;
292 }
293 len -= 2;
294 /* warning: don't use increment (++) in ntohs() macros!! */
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200295 s = (ushort *)pkt;
296 proto = *s++;
297 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000298 switch (ntohs(proto)) {
299
300 case TFTP_RRQ:
301 case TFTP_WRQ:
302 case TFTP_ACK:
303 break;
304 default:
305 break;
306
wdenkfbe4b5c2003-10-06 21:55:32 +0000307 case TFTP_OACK:
Wolfgang Denkd3717082009-08-10 09:59:10 +0200308 debug("Got OACK: %s %s\n",
309 pkt,
310 pkt + strlen((char *)pkt) + 1);
wdenkfbe4b5c2003-10-06 21:55:32 +0000311 TftpState = STATE_OACK;
312 TftpServerPort = src;
Wolfgang Denk60174742007-08-31 10:01:51 +0200313 /*
314 * Check for 'blksize' option.
315 * Careful: "i" is signed, "len" is unsigned, thus
316 * something like "len-8" may give a *huge* number
317 */
318 for (i=0; i+8<len; i++) {
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200319 if (strcmp ((char*)pkt+i,"blksize") == 0) {
320 TftpBlkSize = (unsigned short)
321 simple_strtoul((char*)pkt+i+8,NULL,10);
Robin Getz0ebf04c2009-07-23 03:01:03 -0400322 debug("Blocksize ack: %s, %d\n",
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200323 (char*)pkt+i+8,TftpBlkSize);
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200324 }
Robin Getz4fccb812009-08-20 10:50:20 -0400325#ifdef CONFIG_TFTP_TSIZE
326 if (strcmp ((char*)pkt+i,"tsize") == 0) {
327 TftpTsize = simple_strtoul((char*)pkt+i+6,NULL,10);
328 debug("size = %s, %d\n",
329 (char*)pkt+i+6, TftpTsize);
330 }
331#endif
David Updegraff53a5c422007-06-11 10:41:07 -0500332 }
333#ifdef CONFIG_MCAST_TFTP
334 parse_multicast_oack((char *)pkt,len-1);
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200335 if ((Multicast) && (!MasterClient))
David Updegraff53a5c422007-06-11 10:41:07 -0500336 TftpState = STATE_DATA; /* passive.. */
337 else
338#endif
wdenkfbe4b5c2003-10-06 21:55:32 +0000339 TftpSend (); /* Send ACK */
340 break;
wdenkfe8c2802002-11-03 00:38:21 +0000341 case TFTP_DATA:
342 if (len < 2)
343 return;
344 len -= 2;
345 TftpBlock = ntohs(*(ushort *)pkt);
wdenk3f85ce22004-02-23 16:11:30 +0000346
347 /*
wdenkcd0a9de2004-02-23 20:48:38 +0000348 * RFC1350 specifies that the first data packet will
349 * have sequence number 1. If we receive a sequence
350 * number of 0 this means that there was a wrap
351 * around of the (16 bit) counter.
wdenk3f85ce22004-02-23 16:11:30 +0000352 */
353 if (TftpBlock == 0) {
354 TftpBlockWrap++;
David Updegraff53a5c422007-06-11 10:41:07 -0500355 TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE;
Wolfgang Denkddd5d9d2006-08-14 22:43:13 +0200356 printf ("\n\t %lu MB received\n\t ", TftpBlockWrapOffset>>20);
Robin Getz4fccb812009-08-20 10:50:20 -0400357 }
358#ifdef CONFIG_TFTP_TSIZE
359 else if (TftpTsize) {
360 while (TftpNumchars < NetBootFileXferSize * 50 / TftpTsize) {
361 putc('#');
362 TftpNumchars++;
363 }
364 }
365#endif
366 else {
wdenk3f85ce22004-02-23 16:11:30 +0000367 if (((TftpBlock - 1) % 10) == 0) {
368 putc ('#');
369 } else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) {
370 puts ("\n\t ");
371 }
wdenkfe8c2802002-11-03 00:38:21 +0000372 }
373
Robin Getz0ebf04c2009-07-23 03:01:03 -0400374 if (TftpState == STATE_RRQ)
375 debug("Server did not acknowledge timeout option!\n");
wdenkfbe4b5c2003-10-06 21:55:32 +0000376
377 if (TftpState == STATE_RRQ || TftpState == STATE_OACK) {
wdenk3f85ce22004-02-23 16:11:30 +0000378 /* first block received */
wdenkfe8c2802002-11-03 00:38:21 +0000379 TftpState = STATE_DATA;
380 TftpServerPort = src;
381 TftpLastBlock = 0;
wdenk3f85ce22004-02-23 16:11:30 +0000382 TftpBlockWrap = 0;
383 TftpBlockWrapOffset = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000384
David Updegraff53a5c422007-06-11 10:41:07 -0500385#ifdef CONFIG_MCAST_TFTP
386 if (Multicast) { /* start!=1 common if mcast */
387 TftpLastBlock = TftpBlock - 1;
388 } else
389#endif
wdenkfe8c2802002-11-03 00:38:21 +0000390 if (TftpBlock != 1) { /* Assertion */
391 printf ("\nTFTP error: "
wdenk3f85ce22004-02-23 16:11:30 +0000392 "First block is not block 1 (%ld)\n"
wdenkfe8c2802002-11-03 00:38:21 +0000393 "Starting again\n\n",
394 TftpBlock);
395 NetStartAgain ();
396 break;
397 }
398 }
399
400 if (TftpBlock == TftpLastBlock) {
401 /*
402 * Same block again; ignore it.
403 */
404 break;
405 }
406
407 TftpLastBlock = TftpBlock;
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200408 TftpTimeoutMSecs = TIMEOUT;
409 TftpTimeoutCountMax = TIMEOUT_COUNT;
410 NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
wdenkfe8c2802002-11-03 00:38:21 +0000411
412 store_block (TftpBlock - 1, pkt + 2, len);
413
414 /*
415 * Acknoledge the block just received, which will prompt
416 * the server for the next one.
417 */
David Updegraff53a5c422007-06-11 10:41:07 -0500418#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200419 /* if I am the MasterClient, actively calculate what my next
420 * needed block is; else I'm passive; not ACKING
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100421 */
David Updegraff53a5c422007-06-11 10:41:07 -0500422 if (Multicast) {
423 if (len < TftpBlkSize) {
424 TftpEndingBlock = TftpBlock;
425 } else if (MasterClient) {
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200426 TftpBlock = PrevBitmapHole =
David Updegraff53a5c422007-06-11 10:41:07 -0500427 ext2_find_next_zero_bit(
428 Bitmap,
429 (Mapsize*8),
430 PrevBitmapHole);
431 if (TftpBlock > ((Mapsize*8) - 1)) {
432 printf ("tftpfile too big\n");
433 /* try to double it and retry */
434 Mapsize<<=1;
435 mcast_cleanup();
436 NetStartAgain ();
437 return;
438 }
439 TftpLastBlock = TftpBlock;
440 }
441 }
442#endif
wdenkfe8c2802002-11-03 00:38:21 +0000443 TftpSend ();
444
David Updegraff53a5c422007-06-11 10:41:07 -0500445#ifdef CONFIG_MCAST_TFTP
446 if (Multicast) {
447 if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
448 puts ("\nMulticast tftp done\n");
449 mcast_cleanup();
450 NetState = NETLOOP_SUCCESS;
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200451 }
David Updegraff53a5c422007-06-11 10:41:07 -0500452 }
453 else
454#endif
455 if (len < TftpBlkSize) {
wdenkfe8c2802002-11-03 00:38:21 +0000456 /*
457 * We received the whole thing. Try to
458 * run it.
459 */
Robin Getz4fccb812009-08-20 10:50:20 -0400460#ifdef CONFIG_TFTP_TSIZE
461 /* Print out the hash marks for the last packet received */
462 while (TftpTsize && TftpNumchars < 49) {
463 putc('#');
464 TftpNumchars++;
465 }
466#endif
wdenkfe8c2802002-11-03 00:38:21 +0000467 puts ("\ndone\n");
468 NetState = NETLOOP_SUCCESS;
469 }
470 break;
471
472 case TFTP_ERROR:
473 printf ("\nTFTP error: '%s' (%d)\n",
474 pkt + 2, ntohs(*(ushort *)pkt));
475 puts ("Starting again\n\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500476#ifdef CONFIG_MCAST_TFTP
477 mcast_cleanup();
478#endif
wdenkfe8c2802002-11-03 00:38:21 +0000479 NetStartAgain ();
480 break;
481 }
482}
483
484
485static void
486TftpTimeout (void)
487{
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200488 if (++TftpTimeoutCount > TftpTimeoutCountMax) {
wdenkfe8c2802002-11-03 00:38:21 +0000489 puts ("\nRetry count exceeded; starting again\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500490#ifdef CONFIG_MCAST_TFTP
491 mcast_cleanup();
492#endif
wdenkfe8c2802002-11-03 00:38:21 +0000493 NetStartAgain ();
494 } else {
495 puts ("T ");
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200496 NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
wdenkfe8c2802002-11-03 00:38:21 +0000497 TftpSend ();
498 }
499}
500
501
502void
503TftpStart (void)
504{
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200505 char *ep; /* Environment pointer */
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200506
507 /* Allow the user to choose tftpblocksize */
508 if ((ep = getenv("tftpblocksize")) != NULL)
509 TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
510 debug("tftp block size is %i\n", TftpBlkSizeOption);
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200511
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100512 TftpServerIP = NetServerIP;
wdenkfe8c2802002-11-03 00:38:21 +0000513 if (BootFile[0] == '\0') {
wdenkfe8c2802002-11-03 00:38:21 +0000514 sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
Wolfgang Denkc43352c2005-08-04 01:09:44 +0200515 NetOurIP & 0xFF,
516 (NetOurIP >> 8) & 0xFF,
517 (NetOurIP >> 16) & 0xFF,
518 (NetOurIP >> 24) & 0xFF );
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100519
520 strncpy(tftp_filename, default_filename, MAX_LEN);
521 tftp_filename[MAX_LEN-1] = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000522
523 printf ("*** Warning: no boot file name; using '%s'\n",
524 tftp_filename);
525 } else {
Jean-Christophe PLAGNIOL-VILLARD38cc09c2008-02-14 08:02:12 +0100526 char *p = strchr (BootFile, ':');
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100527
528 if (p == NULL) {
529 strncpy(tftp_filename, BootFile, MAX_LEN);
530 tftp_filename[MAX_LEN-1] = 0;
531 } else {
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100532 TftpServerIP = string_to_ip (BootFile);
Peter Tyser6a86bb62008-12-01 16:29:38 -0600533 strncpy(tftp_filename, p + 1, MAX_LEN);
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100534 tftp_filename[MAX_LEN-1] = 0;
535 }
wdenkfe8c2802002-11-03 00:38:21 +0000536 }
537
wdenk5bb226e2003-11-17 21:14:37 +0000538#if defined(CONFIG_NET_MULTI)
539 printf ("Using %s device\n", eth_get_name());
540#endif
Mike Frysingerb6446b62009-02-17 00:00:53 -0500541 printf("TFTP from server %pI4"
542 "; our IP address is %pI4", &TftpServerIP, &NetOurIP);
wdenkfe8c2802002-11-03 00:38:21 +0000543
544 /* Check if we need to send across this subnet */
545 if (NetOurGatewayIP && NetOurSubnetMask) {
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100546 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
547 IPaddr_t ServerNet = TftpServerIP & NetOurSubnetMask;
wdenkfe8c2802002-11-03 00:38:21 +0000548
Mike Frysingerb6446b62009-02-17 00:00:53 -0500549 if (OurNet != ServerNet)
550 printf("; sending through gateway %pI4", &NetOurGatewayIP);
wdenkfe8c2802002-11-03 00:38:21 +0000551 }
552 putc ('\n');
553
554 printf ("Filename '%s'.", tftp_filename);
555
556 if (NetBootFileSize) {
557 printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
558 print_size (NetBootFileSize<<9, "");
559 }
560
561 putc ('\n');
562
563 printf ("Load address: 0x%lx\n", load_addr);
564
565 puts ("Loading: *\b");
566
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200567 TftpTimeoutMSecs = TftpRRQTimeoutMSecs;
568 TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
569
570 NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
wdenkfe8c2802002-11-03 00:38:21 +0000571 NetSetHandler (TftpHandler);
572
573 TftpServerPort = WELL_KNOWN_PORT;
574 TftpTimeoutCount = 0;
575 TftpState = STATE_RRQ;
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200576 /* Use a pseudo-random port unless a specific port is set */
wdenkfe8c2802002-11-03 00:38:21 +0000577 TftpOurPort = 1024 + (get_timer(0) % 3072);
David Updegraff53a5c422007-06-11 10:41:07 -0500578
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200579#ifdef CONFIG_TFTP_PORT
Wolfgang Denk28cb9372005-09-24 23:25:46 +0200580 if ((ep = getenv("tftpdstp")) != NULL) {
581 TftpServerPort = simple_strtol(ep, NULL, 10);
582 }
583 if ((ep = getenv("tftpsrcp")) != NULL) {
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200584 TftpOurPort= simple_strtol(ep, NULL, 10);
585 }
586#endif
wdenkfbe4b5c2003-10-06 21:55:32 +0000587 TftpBlock = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000588
wdenk73a8b272003-06-05 19:27:42 +0000589 /* zero out server ether in case the server ip has changed */
590 memset(NetServerEther, 0, 6);
David Updegraff53a5c422007-06-11 10:41:07 -0500591 /* Revert TftpBlkSize to dflt */
592 TftpBlkSize = TFTP_BLOCK_SIZE;
593#ifdef CONFIG_MCAST_TFTP
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100594 mcast_cleanup();
David Updegraff53a5c422007-06-11 10:41:07 -0500595#endif
Robin Getz4fccb812009-08-20 10:50:20 -0400596#ifdef CONFIG_TFTP_TSIZE
597 TftpTsize = 0;
598 TftpNumchars = 0;
599#endif
wdenk73a8b272003-06-05 19:27:42 +0000600
wdenkfe8c2802002-11-03 00:38:21 +0000601 TftpSend ();
602}
603
David Updegraff53a5c422007-06-11 10:41:07 -0500604#ifdef CONFIG_MCAST_TFTP
605/* Credits: atftp project.
606 */
607
608/* pick up BcastAddr, Port, and whether I am [now] the master-client. *
609 * Frame:
610 * +-------+-----------+---+-------~~-------+---+
611 * | opc | multicast | 0 | addr, port, mc | 0 |
612 * +-------+-----------+---+-------~~-------+---+
613 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
614 * I am the new master-client so must send ACKs to DataBlocks. If I am not
615 * master-client, I'm a passive client, gathering what DataBlocks I may and
616 * making note of which ones I got in my bitmask.
617 * In theory, I never go from master->passive..
618 * .. this comes in with pkt already pointing just past opc
619 */
620static void parse_multicast_oack(char *pkt, int len)
621{
622 int i;
623 IPaddr_t addr;
624 char *mc_adr, *port, *mc;
625
626 mc_adr=port=mc=NULL;
627 /* march along looking for 'multicast\0', which has to start at least
628 * 14 bytes back from the end.
629 */
630 for (i=0;i<len-14;i++)
631 if (strcmp (pkt+i,"multicast") == 0)
632 break;
633 if (i >= (len-14)) /* non-Multicast OACK, ign. */
634 return;
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200635
David Updegraff53a5c422007-06-11 10:41:07 -0500636 i+=10; /* strlen multicast */
637 mc_adr = pkt+i;
638 for (;i<len;i++) {
639 if (*(pkt+i) == ',') {
640 *(pkt+i) = '\0';
641 if (port) {
642 mc = pkt+i+1;
643 break;
644 } else {
645 port = pkt+i+1;
646 }
647 }
648 }
649 if (!port || !mc_adr || !mc ) return;
650 if (Multicast && MasterClient) {
651 printf ("I got a OACK as master Client, WRONG!\n");
652 return;
653 }
654 /* ..I now accept packets destined for this MCAST addr, port */
655 if (!Multicast) {
656 if (Bitmap) {
657 printf ("Internal failure! no mcast.\n");
658 free(Bitmap);
659 Bitmap=NULL;
660 ProhibitMcast=1;
661 return ;
662 }
663 /* I malloc instead of pre-declare; so that if the file ends
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200664 * up being too big for this bitmap I can retry
665 */
David Updegraff53a5c422007-06-11 10:41:07 -0500666 if (!(Bitmap = malloc (Mapsize))) {
667 printf ("No Bitmap, no multicast. Sorry.\n");
668 ProhibitMcast=1;
669 return;
670 }
671 memset (Bitmap,0,Mapsize);
672 PrevBitmapHole = 0;
673 Multicast = 1;
674 }
675 addr = string_to_ip(mc_adr);
676 if (Mcast_addr != addr) {
677 if (Mcast_addr)
678 eth_mcast_join(Mcast_addr, 0);
679 if (eth_mcast_join(Mcast_addr=addr, 1)) {
680 printf ("Fail to set mcast, revert to TFTP\n");
681 ProhibitMcast=1;
682 mcast_cleanup();
683 NetStartAgain();
684 }
685 }
686 MasterClient = (unsigned char)simple_strtoul((char *)mc,NULL,10);
687 Mcast_port = (unsigned short)simple_strtoul(port,NULL,10);
688 printf ("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
689 return;
690}
691
692#endif /* Multicast TFTP */
693
Jon Loeliger30b52df2007-08-15 11:55:35 -0500694#endif