blob: fb98a346ea6e55fe2fdbcf6d5c0992cc1c56d13b [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.
87 */
88#define TFTP_MTU_BLOCKSIZE 1468
89static unsigned short TftpBlkSize=TFTP_BLOCK_SIZE;
90static unsigned short TftpBlkSizeOption=TFTP_MTU_BLOCKSIZE;
91
92#ifdef CONFIG_MCAST_TFTP
93#include <malloc.h>
94#define MTFTP_BITMAPSIZE 0x1000
95static unsigned *Bitmap;
96static int PrevBitmapHole,Mapsize=MTFTP_BITMAPSIZE;
97static uchar ProhibitMcast=0, MasterClient=0;
98static uchar Multicast=0;
99extern IPaddr_t Mcast_addr;
100static int Mcast_port;
101static ulong TftpEndingBlock; /* can get 'last' block before done..*/
102
103static void parse_multicast_oack(char *pkt,int len);
104
105static void
106mcast_cleanup(void)
107{
108 if (Mcast_addr) eth_mcast_join(Mcast_addr, 0);
109 if (Bitmap) free(Bitmap);
110 Bitmap=NULL;
111 Mcast_addr = Multicast = Mcast_port = 0;
112 TftpEndingBlock = -1;
113}
114
115#endif /* CONFIG_MCAST_TFTP */
116
wdenkfe8c2802002-11-03 00:38:21 +0000117static __inline__ void
118store_block (unsigned block, uchar * src, unsigned len)
119{
David Updegraff53a5c422007-06-11 10:41:07 -0500120 ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
wdenk3f85ce22004-02-23 16:11:30 +0000121 ulong newsize = offset + len;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200122#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
wdenkfe8c2802002-11-03 00:38:21 +0000123 int i, rc = 0;
124
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200125 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenkfe8c2802002-11-03 00:38:21 +0000126 /* start address in flash? */
Jochen Friedrichbe1b0d22008-09-02 11:24:59 +0200127 if (flash_info[i].flash_id == FLASH_UNKNOWN)
128 continue;
wdenkfe8c2802002-11-03 00:38:21 +0000129 if (load_addr + offset >= flash_info[i].start[0]) {
130 rc = 1;
131 break;
132 }
133 }
134
135 if (rc) { /* Flash is destination for this packet */
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200136 rc = flash_write ((char *)src, (ulong)(load_addr+offset), len);
wdenkfe8c2802002-11-03 00:38:21 +0000137 if (rc) {
138 flash_perror (rc);
139 NetState = NETLOOP_FAIL;
140 return;
141 }
142 }
143 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200144#endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000145 {
146 (void)memcpy((void *)(load_addr + offset), src, len);
147 }
David Updegraff53a5c422007-06-11 10:41:07 -0500148#ifdef CONFIG_MCAST_TFTP
149 if (Multicast)
150 ext2_set_bit(block, Bitmap);
151#endif
wdenkfe8c2802002-11-03 00:38:21 +0000152
153 if (NetBootFileXferSize < newsize)
154 NetBootFileXferSize = newsize;
155}
156
157static void TftpSend (void);
158static void TftpTimeout (void);
159
160/**********************************************************************/
161
162static void
163TftpSend (void)
164{
165 volatile uchar * pkt;
166 volatile uchar * xp;
167 int len = 0;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200168 volatile ushort *s;
wdenkfe8c2802002-11-03 00:38:21 +0000169
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200170#ifdef CONFIG_MCAST_TFTP
David Updegraff53a5c422007-06-11 10:41:07 -0500171 /* Multicast TFTP.. non-MasterClients do not ACK data. */
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200172 if (Multicast
173 && (TftpState == STATE_DATA)
174 && (MasterClient == 0))
David Updegraff53a5c422007-06-11 10:41:07 -0500175 return;
176#endif
wdenkfe8c2802002-11-03 00:38:21 +0000177 /*
178 * We will always be sending some sort of packet, so
179 * cobble together the packet headers now.
180 */
wdenka3d991b2004-04-15 21:48:45 +0000181 pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
wdenkfe8c2802002-11-03 00:38:21 +0000182
183 switch (TftpState) {
184
185 case STATE_RRQ:
186 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200187 s = (ushort *)pkt;
188 *s++ = htons(TFTP_RRQ);
189 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000190 strcpy ((char *)pkt, tftp_filename);
191 pkt += strlen(tftp_filename) + 1;
192 strcpy ((char *)pkt, "octet");
193 pkt += 5 /*strlen("octet")*/ + 1;
wdenkfbe4b5c2003-10-06 21:55:32 +0000194 strcpy ((char *)pkt, "timeout");
195 pkt += 7 /*strlen("timeout")*/ + 1;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200196 sprintf((char *)pkt, "%lu", TIMEOUT / 1000);
Robin Getz0ebf04c2009-07-23 03:01:03 -0400197 debug("send option \"timeout %s\"\n", (char *)pkt);
wdenkfbe4b5c2003-10-06 21:55:32 +0000198 pkt += strlen((char *)pkt) + 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500199 /* try for more effic. blk size */
200 pkt += sprintf((char *)pkt,"blksize%c%d%c",
stefano babicef8f2072007-08-21 15:52:33 +0200201 0,TftpBlkSizeOption,0);
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200202#ifdef CONFIG_MCAST_TFTP
David Updegraff53a5c422007-06-11 10:41:07 -0500203 /* Check all preconditions before even trying the option */
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200204 if (!ProhibitMcast
205 && (Bitmap=malloc(Mapsize))
David Updegraff53a5c422007-06-11 10:41:07 -0500206 && eth_get_dev()->mcast) {
207 free(Bitmap);
208 Bitmap=NULL;
209 pkt += sprintf((char *)pkt,"multicast%c%c",0,0);
210 }
211#endif /* CONFIG_MCAST_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000212 len = pkt - xp;
213 break;
214
wdenkfbe4b5c2003-10-06 21:55:32 +0000215 case STATE_OACK:
David Updegraff53a5c422007-06-11 10:41:07 -0500216#ifdef CONFIG_MCAST_TFTP
217 /* My turn! Start at where I need blocks I missed.*/
218 if (Multicast)
219 TftpBlock=ext2_find_next_zero_bit(Bitmap,(Mapsize*8),0);
220 /*..falling..*/
221#endif
222 case STATE_DATA:
wdenkfe8c2802002-11-03 00:38:21 +0000223 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200224 s = (ushort *)pkt;
225 *s++ = htons(TFTP_ACK);
226 *s++ = htons(TftpBlock);
227 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000228 len = pkt - xp;
229 break;
230
231 case STATE_TOO_LARGE:
232 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200233 s = (ushort *)pkt;
234 *s++ = htons(TFTP_ERROR);
235 *s++ = htons(3);
236 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000237 strcpy ((char *)pkt, "File too large");
238 pkt += 14 /*strlen("File too large")*/ + 1;
239 len = pkt - xp;
240 break;
241
242 case STATE_BAD_MAGIC:
243 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200244 s = (ushort *)pkt;
245 *s++ = htons(TFTP_ERROR);
246 *s++ = htons(2);
247 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000248 strcpy ((char *)pkt, "File has bad magic");
249 pkt += 18 /*strlen("File has bad magic")*/ + 1;
250 len = pkt - xp;
251 break;
252 }
253
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100254 NetSendUDPPacket(NetServerEther, TftpServerIP, TftpServerPort, TftpOurPort, len);
wdenkfe8c2802002-11-03 00:38:21 +0000255}
256
257
258static void
259TftpHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
260{
261 ushort proto;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200262 ushort *s;
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200263 int i;
wdenkfe8c2802002-11-03 00:38:21 +0000264
265 if (dest != TftpOurPort) {
David Updegraff53a5c422007-06-11 10:41:07 -0500266#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200267 if (Multicast
David Updegraff53a5c422007-06-11 10:41:07 -0500268 && (!Mcast_port || (dest != Mcast_port)))
269#endif
wdenkfe8c2802002-11-03 00:38:21 +0000270 return;
271 }
272 if (TftpState != STATE_RRQ && src != TftpServerPort) {
273 return;
274 }
275
276 if (len < 2) {
277 return;
278 }
279 len -= 2;
280 /* warning: don't use increment (++) in ntohs() macros!! */
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200281 s = (ushort *)pkt;
282 proto = *s++;
283 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000284 switch (ntohs(proto)) {
285
286 case TFTP_RRQ:
287 case TFTP_WRQ:
288 case TFTP_ACK:
289 break;
290 default:
291 break;
292
wdenkfbe4b5c2003-10-06 21:55:32 +0000293 case TFTP_OACK:
Wolfgang Denkd3717082009-08-10 09:59:10 +0200294 debug("Got OACK: %s %s\n",
295 pkt,
296 pkt + strlen((char *)pkt) + 1);
wdenkfbe4b5c2003-10-06 21:55:32 +0000297 TftpState = STATE_OACK;
298 TftpServerPort = src;
Wolfgang Denk60174742007-08-31 10:01:51 +0200299 /*
300 * Check for 'blksize' option.
301 * Careful: "i" is signed, "len" is unsigned, thus
302 * something like "len-8" may give a *huge* number
303 */
304 for (i=0; i+8<len; i++) {
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200305 if (strcmp ((char*)pkt+i,"blksize") == 0) {
306 TftpBlkSize = (unsigned short)
307 simple_strtoul((char*)pkt+i+8,NULL,10);
Robin Getz0ebf04c2009-07-23 03:01:03 -0400308 debug("Blocksize ack: %s, %d\n",
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200309 (char*)pkt+i+8,TftpBlkSize);
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200310 break;
311 }
David Updegraff53a5c422007-06-11 10:41:07 -0500312 }
313#ifdef CONFIG_MCAST_TFTP
314 parse_multicast_oack((char *)pkt,len-1);
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200315 if ((Multicast) && (!MasterClient))
David Updegraff53a5c422007-06-11 10:41:07 -0500316 TftpState = STATE_DATA; /* passive.. */
317 else
318#endif
wdenkfbe4b5c2003-10-06 21:55:32 +0000319 TftpSend (); /* Send ACK */
320 break;
wdenkfe8c2802002-11-03 00:38:21 +0000321 case TFTP_DATA:
322 if (len < 2)
323 return;
324 len -= 2;
325 TftpBlock = ntohs(*(ushort *)pkt);
wdenk3f85ce22004-02-23 16:11:30 +0000326
327 /*
wdenkcd0a9de2004-02-23 20:48:38 +0000328 * RFC1350 specifies that the first data packet will
329 * have sequence number 1. If we receive a sequence
330 * number of 0 this means that there was a wrap
331 * around of the (16 bit) counter.
wdenk3f85ce22004-02-23 16:11:30 +0000332 */
333 if (TftpBlock == 0) {
334 TftpBlockWrap++;
David Updegraff53a5c422007-06-11 10:41:07 -0500335 TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE;
Wolfgang Denkddd5d9d2006-08-14 22:43:13 +0200336 printf ("\n\t %lu MB received\n\t ", TftpBlockWrapOffset>>20);
wdenk3f85ce22004-02-23 16:11:30 +0000337 } else {
338 if (((TftpBlock - 1) % 10) == 0) {
339 putc ('#');
340 } else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) {
341 puts ("\n\t ");
342 }
wdenkfe8c2802002-11-03 00:38:21 +0000343 }
344
Robin Getz0ebf04c2009-07-23 03:01:03 -0400345 if (TftpState == STATE_RRQ)
346 debug("Server did not acknowledge timeout option!\n");
wdenkfbe4b5c2003-10-06 21:55:32 +0000347
348 if (TftpState == STATE_RRQ || TftpState == STATE_OACK) {
wdenk3f85ce22004-02-23 16:11:30 +0000349 /* first block received */
wdenkfe8c2802002-11-03 00:38:21 +0000350 TftpState = STATE_DATA;
351 TftpServerPort = src;
352 TftpLastBlock = 0;
wdenk3f85ce22004-02-23 16:11:30 +0000353 TftpBlockWrap = 0;
354 TftpBlockWrapOffset = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000355
David Updegraff53a5c422007-06-11 10:41:07 -0500356#ifdef CONFIG_MCAST_TFTP
357 if (Multicast) { /* start!=1 common if mcast */
358 TftpLastBlock = TftpBlock - 1;
359 } else
360#endif
wdenkfe8c2802002-11-03 00:38:21 +0000361 if (TftpBlock != 1) { /* Assertion */
362 printf ("\nTFTP error: "
wdenk3f85ce22004-02-23 16:11:30 +0000363 "First block is not block 1 (%ld)\n"
wdenkfe8c2802002-11-03 00:38:21 +0000364 "Starting again\n\n",
365 TftpBlock);
366 NetStartAgain ();
367 break;
368 }
369 }
370
371 if (TftpBlock == TftpLastBlock) {
372 /*
373 * Same block again; ignore it.
374 */
375 break;
376 }
377
378 TftpLastBlock = TftpBlock;
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200379 TftpTimeoutMSecs = TIMEOUT;
380 TftpTimeoutCountMax = TIMEOUT_COUNT;
381 NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
wdenkfe8c2802002-11-03 00:38:21 +0000382
383 store_block (TftpBlock - 1, pkt + 2, len);
384
385 /*
386 * Acknoledge the block just received, which will prompt
387 * the server for the next one.
388 */
David Updegraff53a5c422007-06-11 10:41:07 -0500389#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200390 /* if I am the MasterClient, actively calculate what my next
391 * needed block is; else I'm passive; not ACKING
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100392 */
David Updegraff53a5c422007-06-11 10:41:07 -0500393 if (Multicast) {
394 if (len < TftpBlkSize) {
395 TftpEndingBlock = TftpBlock;
396 } else if (MasterClient) {
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200397 TftpBlock = PrevBitmapHole =
David Updegraff53a5c422007-06-11 10:41:07 -0500398 ext2_find_next_zero_bit(
399 Bitmap,
400 (Mapsize*8),
401 PrevBitmapHole);
402 if (TftpBlock > ((Mapsize*8) - 1)) {
403 printf ("tftpfile too big\n");
404 /* try to double it and retry */
405 Mapsize<<=1;
406 mcast_cleanup();
407 NetStartAgain ();
408 return;
409 }
410 TftpLastBlock = TftpBlock;
411 }
412 }
413#endif
wdenkfe8c2802002-11-03 00:38:21 +0000414 TftpSend ();
415
David Updegraff53a5c422007-06-11 10:41:07 -0500416#ifdef CONFIG_MCAST_TFTP
417 if (Multicast) {
418 if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
419 puts ("\nMulticast tftp done\n");
420 mcast_cleanup();
421 NetState = NETLOOP_SUCCESS;
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200422 }
David Updegraff53a5c422007-06-11 10:41:07 -0500423 }
424 else
425#endif
426 if (len < TftpBlkSize) {
wdenkfe8c2802002-11-03 00:38:21 +0000427 /*
428 * We received the whole thing. Try to
429 * run it.
430 */
431 puts ("\ndone\n");
432 NetState = NETLOOP_SUCCESS;
433 }
434 break;
435
436 case TFTP_ERROR:
437 printf ("\nTFTP error: '%s' (%d)\n",
438 pkt + 2, ntohs(*(ushort *)pkt));
439 puts ("Starting again\n\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500440#ifdef CONFIG_MCAST_TFTP
441 mcast_cleanup();
442#endif
wdenkfe8c2802002-11-03 00:38:21 +0000443 NetStartAgain ();
444 break;
445 }
446}
447
448
449static void
450TftpTimeout (void)
451{
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200452 if (++TftpTimeoutCount > TftpTimeoutCountMax) {
wdenkfe8c2802002-11-03 00:38:21 +0000453 puts ("\nRetry count exceeded; starting again\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500454#ifdef CONFIG_MCAST_TFTP
455 mcast_cleanup();
456#endif
wdenkfe8c2802002-11-03 00:38:21 +0000457 NetStartAgain ();
458 } else {
459 puts ("T ");
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200460 NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
wdenkfe8c2802002-11-03 00:38:21 +0000461 TftpSend ();
462 }
463}
464
465
466void
467TftpStart (void)
468{
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200469#ifdef CONFIG_TFTP_PORT
470 char *ep; /* Environment pointer */
471#endif
472
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100473 TftpServerIP = NetServerIP;
wdenkfe8c2802002-11-03 00:38:21 +0000474 if (BootFile[0] == '\0') {
wdenkfe8c2802002-11-03 00:38:21 +0000475 sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
Wolfgang Denkc43352c2005-08-04 01:09:44 +0200476 NetOurIP & 0xFF,
477 (NetOurIP >> 8) & 0xFF,
478 (NetOurIP >> 16) & 0xFF,
479 (NetOurIP >> 24) & 0xFF );
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100480
481 strncpy(tftp_filename, default_filename, MAX_LEN);
482 tftp_filename[MAX_LEN-1] = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000483
484 printf ("*** Warning: no boot file name; using '%s'\n",
485 tftp_filename);
486 } else {
Jean-Christophe PLAGNIOL-VILLARD38cc09c2008-02-14 08:02:12 +0100487 char *p = strchr (BootFile, ':');
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100488
489 if (p == NULL) {
490 strncpy(tftp_filename, BootFile, MAX_LEN);
491 tftp_filename[MAX_LEN-1] = 0;
492 } else {
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100493 TftpServerIP = string_to_ip (BootFile);
Peter Tyser6a86bb62008-12-01 16:29:38 -0600494 strncpy(tftp_filename, p + 1, MAX_LEN);
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100495 tftp_filename[MAX_LEN-1] = 0;
496 }
wdenkfe8c2802002-11-03 00:38:21 +0000497 }
498
wdenk5bb226e2003-11-17 21:14:37 +0000499#if defined(CONFIG_NET_MULTI)
500 printf ("Using %s device\n", eth_get_name());
501#endif
Mike Frysingerb6446b62009-02-17 00:00:53 -0500502 printf("TFTP from server %pI4"
503 "; our IP address is %pI4", &TftpServerIP, &NetOurIP);
wdenkfe8c2802002-11-03 00:38:21 +0000504
505 /* Check if we need to send across this subnet */
506 if (NetOurGatewayIP && NetOurSubnetMask) {
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100507 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
508 IPaddr_t ServerNet = TftpServerIP & NetOurSubnetMask;
wdenkfe8c2802002-11-03 00:38:21 +0000509
Mike Frysingerb6446b62009-02-17 00:00:53 -0500510 if (OurNet != ServerNet)
511 printf("; sending through gateway %pI4", &NetOurGatewayIP);
wdenkfe8c2802002-11-03 00:38:21 +0000512 }
513 putc ('\n');
514
515 printf ("Filename '%s'.", tftp_filename);
516
517 if (NetBootFileSize) {
518 printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
519 print_size (NetBootFileSize<<9, "");
520 }
521
522 putc ('\n');
523
524 printf ("Load address: 0x%lx\n", load_addr);
525
526 puts ("Loading: *\b");
527
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200528 TftpTimeoutMSecs = TftpRRQTimeoutMSecs;
529 TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
530
531 NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
wdenkfe8c2802002-11-03 00:38:21 +0000532 NetSetHandler (TftpHandler);
533
534 TftpServerPort = WELL_KNOWN_PORT;
535 TftpTimeoutCount = 0;
536 TftpState = STATE_RRQ;
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200537 /* Use a pseudo-random port unless a specific port is set */
wdenkfe8c2802002-11-03 00:38:21 +0000538 TftpOurPort = 1024 + (get_timer(0) % 3072);
David Updegraff53a5c422007-06-11 10:41:07 -0500539
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200540#ifdef CONFIG_TFTP_PORT
Wolfgang Denk28cb9372005-09-24 23:25:46 +0200541 if ((ep = getenv("tftpdstp")) != NULL) {
542 TftpServerPort = simple_strtol(ep, NULL, 10);
543 }
544 if ((ep = getenv("tftpsrcp")) != NULL) {
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200545 TftpOurPort= simple_strtol(ep, NULL, 10);
546 }
547#endif
wdenkfbe4b5c2003-10-06 21:55:32 +0000548 TftpBlock = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000549
wdenk73a8b272003-06-05 19:27:42 +0000550 /* zero out server ether in case the server ip has changed */
551 memset(NetServerEther, 0, 6);
David Updegraff53a5c422007-06-11 10:41:07 -0500552 /* Revert TftpBlkSize to dflt */
553 TftpBlkSize = TFTP_BLOCK_SIZE;
554#ifdef CONFIG_MCAST_TFTP
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100555 mcast_cleanup();
David Updegraff53a5c422007-06-11 10:41:07 -0500556#endif
wdenk73a8b272003-06-05 19:27:42 +0000557
wdenkfe8c2802002-11-03 00:38:21 +0000558 TftpSend ();
559}
560
David Updegraff53a5c422007-06-11 10:41:07 -0500561#ifdef CONFIG_MCAST_TFTP
562/* Credits: atftp project.
563 */
564
565/* pick up BcastAddr, Port, and whether I am [now] the master-client. *
566 * Frame:
567 * +-------+-----------+---+-------~~-------+---+
568 * | opc | multicast | 0 | addr, port, mc | 0 |
569 * +-------+-----------+---+-------~~-------+---+
570 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
571 * I am the new master-client so must send ACKs to DataBlocks. If I am not
572 * master-client, I'm a passive client, gathering what DataBlocks I may and
573 * making note of which ones I got in my bitmask.
574 * In theory, I never go from master->passive..
575 * .. this comes in with pkt already pointing just past opc
576 */
577static void parse_multicast_oack(char *pkt, int len)
578{
579 int i;
580 IPaddr_t addr;
581 char *mc_adr, *port, *mc;
582
583 mc_adr=port=mc=NULL;
584 /* march along looking for 'multicast\0', which has to start at least
585 * 14 bytes back from the end.
586 */
587 for (i=0;i<len-14;i++)
588 if (strcmp (pkt+i,"multicast") == 0)
589 break;
590 if (i >= (len-14)) /* non-Multicast OACK, ign. */
591 return;
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200592
David Updegraff53a5c422007-06-11 10:41:07 -0500593 i+=10; /* strlen multicast */
594 mc_adr = pkt+i;
595 for (;i<len;i++) {
596 if (*(pkt+i) == ',') {
597 *(pkt+i) = '\0';
598 if (port) {
599 mc = pkt+i+1;
600 break;
601 } else {
602 port = pkt+i+1;
603 }
604 }
605 }
606 if (!port || !mc_adr || !mc ) return;
607 if (Multicast && MasterClient) {
608 printf ("I got a OACK as master Client, WRONG!\n");
609 return;
610 }
611 /* ..I now accept packets destined for this MCAST addr, port */
612 if (!Multicast) {
613 if (Bitmap) {
614 printf ("Internal failure! no mcast.\n");
615 free(Bitmap);
616 Bitmap=NULL;
617 ProhibitMcast=1;
618 return ;
619 }
620 /* I malloc instead of pre-declare; so that if the file ends
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200621 * up being too big for this bitmap I can retry
622 */
David Updegraff53a5c422007-06-11 10:41:07 -0500623 if (!(Bitmap = malloc (Mapsize))) {
624 printf ("No Bitmap, no multicast. Sorry.\n");
625 ProhibitMcast=1;
626 return;
627 }
628 memset (Bitmap,0,Mapsize);
629 PrevBitmapHole = 0;
630 Multicast = 1;
631 }
632 addr = string_to_ip(mc_adr);
633 if (Mcast_addr != addr) {
634 if (Mcast_addr)
635 eth_mcast_join(Mcast_addr, 0);
636 if (eth_mcast_join(Mcast_addr=addr, 1)) {
637 printf ("Fail to set mcast, revert to TFTP\n");
638 ProhibitMcast=1;
639 mcast_cleanup();
640 NetStartAgain();
641 }
642 }
643 MasterClient = (unsigned char)simple_strtoul((char *)mc,NULL,10);
644 Mcast_port = (unsigned short)simple_strtoul(port,NULL,10);
645 printf ("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
646 return;
647}
648
649#endif /* Multicast TFTP */
650
Jon Loeliger30b52df2007-08-15 11:55:35 -0500651#endif