blob: 5ee7676466925cafcdde9a8eee38f7a4b13282f5 [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
13#undef ET_DEBUG
14
Jon Loeliger643d1ab2007-07-09 17:45:14 -050015#if defined(CONFIG_CMD_NET)
wdenkfe8c2802002-11-03 00:38:21 +000016
17#define WELL_KNOWN_PORT 69 /* Well known TFTP port # */
wdenkfbe4b5c2003-10-06 21:55:32 +000018#define TIMEOUT 5 /* Seconds to timeout for a lost pkt */
wdenkfe8c2802002-11-03 00:38:21 +000019#ifndef CONFIG_NET_RETRY_COUNT
20# define TIMEOUT_COUNT 10 /* # of timeouts before giving up */
21#else
22# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
23#endif
24 /* (for checking the image size) */
25#define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
26
27/*
28 * TFTP operations.
29 */
30#define TFTP_RRQ 1
31#define TFTP_WRQ 2
32#define TFTP_DATA 3
33#define TFTP_ACK 4
34#define TFTP_ERROR 5
wdenkfbe4b5c2003-10-06 21:55:32 +000035#define TFTP_OACK 6
wdenkfe8c2802002-11-03 00:38:21 +000036
37
38static int TftpServerPort; /* The UDP port at their end */
39static int TftpOurPort; /* The UDP port at our end */
40static int TftpTimeoutCount;
wdenk3f85ce22004-02-23 16:11:30 +000041static ulong TftpBlock; /* packet sequence number */
42static ulong TftpLastBlock; /* last packet sequence number received */
43static ulong TftpBlockWrap; /* count of sequence number wraparounds */
44static ulong TftpBlockWrapOffset; /* memory offset due to wrapping */
wdenkfe8c2802002-11-03 00:38:21 +000045static int TftpState;
wdenk3f85ce22004-02-23 16:11:30 +000046
wdenkfe8c2802002-11-03 00:38:21 +000047#define STATE_RRQ 1
48#define STATE_DATA 2
49#define STATE_TOO_LARGE 3
50#define STATE_BAD_MAGIC 4
wdenkfbe4b5c2003-10-06 21:55:32 +000051#define STATE_OACK 5
wdenkfe8c2802002-11-03 00:38:21 +000052
wdenk3f85ce22004-02-23 16:11:30 +000053#define TFTP_BLOCK_SIZE 512 /* default TFTP block size */
54#define TFTP_SEQUENCE_SIZE ((ulong)(1<<16)) /* sequence number is 16 bit */
55
wdenkfe8c2802002-11-03 00:38:21 +000056#define DEFAULT_NAME_LEN (8 + 4 + 1)
57static char default_filename[DEFAULT_NAME_LEN];
58static char *tftp_filename;
59
60#ifdef CFG_DIRECT_FLASH_TFTP
Marian Balakowicze6f2e902005-10-11 19:09:42 +020061extern flash_info_t flash_info[];
wdenkfe8c2802002-11-03 00:38:21 +000062#endif
63
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +020064/* 512 is poor choice for ethernet, MTU is typically 1500.
65 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
David Updegraff53a5c422007-06-11 10:41:07 -050066 * almost-MTU block sizes. At least try... fall back to 512 if need be.
67 */
68#define TFTP_MTU_BLOCKSIZE 1468
69static unsigned short TftpBlkSize=TFTP_BLOCK_SIZE;
70static unsigned short TftpBlkSizeOption=TFTP_MTU_BLOCKSIZE;
71
72#ifdef CONFIG_MCAST_TFTP
73#include <malloc.h>
74#define MTFTP_BITMAPSIZE 0x1000
75static unsigned *Bitmap;
76static int PrevBitmapHole,Mapsize=MTFTP_BITMAPSIZE;
77static uchar ProhibitMcast=0, MasterClient=0;
78static uchar Multicast=0;
79extern IPaddr_t Mcast_addr;
80static int Mcast_port;
81static ulong TftpEndingBlock; /* can get 'last' block before done..*/
82
83static void parse_multicast_oack(char *pkt,int len);
84
85static void
86mcast_cleanup(void)
87{
88 if (Mcast_addr) eth_mcast_join(Mcast_addr, 0);
89 if (Bitmap) free(Bitmap);
90 Bitmap=NULL;
91 Mcast_addr = Multicast = Mcast_port = 0;
92 TftpEndingBlock = -1;
93}
94
95#endif /* CONFIG_MCAST_TFTP */
96
wdenkfe8c2802002-11-03 00:38:21 +000097static __inline__ void
98store_block (unsigned block, uchar * src, unsigned len)
99{
David Updegraff53a5c422007-06-11 10:41:07 -0500100 ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
wdenk3f85ce22004-02-23 16:11:30 +0000101 ulong newsize = offset + len;
wdenkfe8c2802002-11-03 00:38:21 +0000102#ifdef CFG_DIRECT_FLASH_TFTP
103 int i, rc = 0;
104
105 for (i=0; i<CFG_MAX_FLASH_BANKS; i++) {
106 /* start address in flash? */
107 if (load_addr + offset >= flash_info[i].start[0]) {
108 rc = 1;
109 break;
110 }
111 }
112
113 if (rc) { /* Flash is destination for this packet */
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200114 rc = flash_write ((char *)src, (ulong)(load_addr+offset), len);
wdenkfe8c2802002-11-03 00:38:21 +0000115 if (rc) {
116 flash_perror (rc);
117 NetState = NETLOOP_FAIL;
118 return;
119 }
120 }
121 else
122#endif /* CFG_DIRECT_FLASH_TFTP */
123 {
124 (void)memcpy((void *)(load_addr + offset), src, len);
125 }
David Updegraff53a5c422007-06-11 10:41:07 -0500126#ifdef CONFIG_MCAST_TFTP
127 if (Multicast)
128 ext2_set_bit(block, Bitmap);
129#endif
wdenkfe8c2802002-11-03 00:38:21 +0000130
131 if (NetBootFileXferSize < newsize)
132 NetBootFileXferSize = newsize;
133}
134
135static void TftpSend (void);
136static void TftpTimeout (void);
137
138/**********************************************************************/
139
140static void
141TftpSend (void)
142{
143 volatile uchar * pkt;
144 volatile uchar * xp;
145 int len = 0;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200146 volatile ushort *s;
wdenkfe8c2802002-11-03 00:38:21 +0000147
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200148#ifdef CONFIG_MCAST_TFTP
David Updegraff53a5c422007-06-11 10:41:07 -0500149 /* Multicast TFTP.. non-MasterClients do not ACK data. */
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200150 if (Multicast
151 && (TftpState == STATE_DATA)
152 && (MasterClient == 0))
David Updegraff53a5c422007-06-11 10:41:07 -0500153 return;
154#endif
wdenkfe8c2802002-11-03 00:38:21 +0000155 /*
156 * We will always be sending some sort of packet, so
157 * cobble together the packet headers now.
158 */
wdenka3d991b2004-04-15 21:48:45 +0000159 pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
wdenkfe8c2802002-11-03 00:38:21 +0000160
161 switch (TftpState) {
162
163 case STATE_RRQ:
164 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200165 s = (ushort *)pkt;
166 *s++ = htons(TFTP_RRQ);
167 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000168 strcpy ((char *)pkt, tftp_filename);
169 pkt += strlen(tftp_filename) + 1;
170 strcpy ((char *)pkt, "octet");
171 pkt += 5 /*strlen("octet")*/ + 1;
wdenkfbe4b5c2003-10-06 21:55:32 +0000172 strcpy ((char *)pkt, "timeout");
173 pkt += 7 /*strlen("timeout")*/ + 1;
174 sprintf((char *)pkt, "%d", TIMEOUT);
175#ifdef ET_DEBUG
176 printf("send option \"timeout %s\"\n", (char *)pkt);
177#endif
178 pkt += strlen((char *)pkt) + 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500179 /* try for more effic. blk size */
180 pkt += sprintf((char *)pkt,"blksize%c%d%c",
stefano babicef8f2072007-08-21 15:52:33 +0200181 0,TftpBlkSizeOption,0);
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200182#ifdef CONFIG_MCAST_TFTP
David Updegraff53a5c422007-06-11 10:41:07 -0500183 /* Check all preconditions before even trying the option */
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200184 if (!ProhibitMcast
185 && (Bitmap=malloc(Mapsize))
David Updegraff53a5c422007-06-11 10:41:07 -0500186 && eth_get_dev()->mcast) {
187 free(Bitmap);
188 Bitmap=NULL;
189 pkt += sprintf((char *)pkt,"multicast%c%c",0,0);
190 }
191#endif /* CONFIG_MCAST_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000192 len = pkt - xp;
193 break;
194
wdenkfbe4b5c2003-10-06 21:55:32 +0000195 case STATE_OACK:
David Updegraff53a5c422007-06-11 10:41:07 -0500196#ifdef CONFIG_MCAST_TFTP
197 /* My turn! Start at where I need blocks I missed.*/
198 if (Multicast)
199 TftpBlock=ext2_find_next_zero_bit(Bitmap,(Mapsize*8),0);
200 /*..falling..*/
201#endif
202 case STATE_DATA:
wdenkfe8c2802002-11-03 00:38:21 +0000203 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200204 s = (ushort *)pkt;
205 *s++ = htons(TFTP_ACK);
206 *s++ = htons(TftpBlock);
207 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000208 len = pkt - xp;
209 break;
210
211 case STATE_TOO_LARGE:
212 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200213 s = (ushort *)pkt;
214 *s++ = htons(TFTP_ERROR);
215 *s++ = htons(3);
216 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000217 strcpy ((char *)pkt, "File too large");
218 pkt += 14 /*strlen("File too large")*/ + 1;
219 len = pkt - xp;
220 break;
221
222 case STATE_BAD_MAGIC:
223 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200224 s = (ushort *)pkt;
225 *s++ = htons(TFTP_ERROR);
226 *s++ = htons(2);
227 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000228 strcpy ((char *)pkt, "File has bad magic");
229 pkt += 18 /*strlen("File has bad magic")*/ + 1;
230 len = pkt - xp;
231 break;
232 }
233
wdenk73a8b272003-06-05 19:27:42 +0000234 NetSendUDPPacket(NetServerEther, NetServerIP, TftpServerPort, TftpOurPort, len);
wdenkfe8c2802002-11-03 00:38:21 +0000235}
236
237
238static void
239TftpHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
240{
241 ushort proto;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200242 ushort *s;
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200243 int i;
wdenkfe8c2802002-11-03 00:38:21 +0000244
245 if (dest != TftpOurPort) {
David Updegraff53a5c422007-06-11 10:41:07 -0500246#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200247 if (Multicast
David Updegraff53a5c422007-06-11 10:41:07 -0500248 && (!Mcast_port || (dest != Mcast_port)))
249#endif
wdenkfe8c2802002-11-03 00:38:21 +0000250 return;
251 }
252 if (TftpState != STATE_RRQ && src != TftpServerPort) {
253 return;
254 }
255
256 if (len < 2) {
257 return;
258 }
259 len -= 2;
260 /* warning: don't use increment (++) in ntohs() macros!! */
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200261 s = (ushort *)pkt;
262 proto = *s++;
263 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000264 switch (ntohs(proto)) {
265
266 case TFTP_RRQ:
267 case TFTP_WRQ:
268 case TFTP_ACK:
269 break;
270 default:
271 break;
272
wdenkfbe4b5c2003-10-06 21:55:32 +0000273 case TFTP_OACK:
274#ifdef ET_DEBUG
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200275 printf("Got OACK: %s %s\n", pkt, pkt+strlen(pkt)+1);
wdenkfbe4b5c2003-10-06 21:55:32 +0000276#endif
277 TftpState = STATE_OACK;
278 TftpServerPort = src;
Wolfgang Denk60174742007-08-31 10:01:51 +0200279 /*
280 * Check for 'blksize' option.
281 * Careful: "i" is signed, "len" is unsigned, thus
282 * something like "len-8" may give a *huge* number
283 */
284 for (i=0; i+8<len; i++) {
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200285 if (strcmp ((char*)pkt+i,"blksize") == 0) {
286 TftpBlkSize = (unsigned short)
287 simple_strtoul((char*)pkt+i+8,NULL,10);
David Updegraff53a5c422007-06-11 10:41:07 -0500288#ifdef ET_DEBUG
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200289 printf ("Blocksize ack: %s, %d\n",
290 (char*)pkt+i+8,TftpBlkSize);
David Updegraff53a5c422007-06-11 10:41:07 -0500291#endif
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200292 break;
293 }
David Updegraff53a5c422007-06-11 10:41:07 -0500294 }
295#ifdef CONFIG_MCAST_TFTP
296 parse_multicast_oack((char *)pkt,len-1);
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200297 if ((Multicast) && (!MasterClient))
David Updegraff53a5c422007-06-11 10:41:07 -0500298 TftpState = STATE_DATA; /* passive.. */
299 else
300#endif
wdenkfbe4b5c2003-10-06 21:55:32 +0000301 TftpSend (); /* Send ACK */
302 break;
wdenkfe8c2802002-11-03 00:38:21 +0000303 case TFTP_DATA:
304 if (len < 2)
305 return;
306 len -= 2;
307 TftpBlock = ntohs(*(ushort *)pkt);
wdenk3f85ce22004-02-23 16:11:30 +0000308
309 /*
wdenkcd0a9de2004-02-23 20:48:38 +0000310 * RFC1350 specifies that the first data packet will
311 * have sequence number 1. If we receive a sequence
312 * number of 0 this means that there was a wrap
313 * around of the (16 bit) counter.
wdenk3f85ce22004-02-23 16:11:30 +0000314 */
315 if (TftpBlock == 0) {
316 TftpBlockWrap++;
David Updegraff53a5c422007-06-11 10:41:07 -0500317 TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE;
Wolfgang Denkddd5d9d2006-08-14 22:43:13 +0200318 printf ("\n\t %lu MB received\n\t ", TftpBlockWrapOffset>>20);
wdenk3f85ce22004-02-23 16:11:30 +0000319 } else {
320 if (((TftpBlock - 1) % 10) == 0) {
321 putc ('#');
322 } else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) {
323 puts ("\n\t ");
324 }
wdenkfe8c2802002-11-03 00:38:21 +0000325 }
326
wdenkfbe4b5c2003-10-06 21:55:32 +0000327#ifdef ET_DEBUG
wdenkfe8c2802002-11-03 00:38:21 +0000328 if (TftpState == STATE_RRQ) {
wdenk4b9206e2004-03-23 22:14:11 +0000329 puts ("Server did not acknowledge timeout option!\n");
wdenkfbe4b5c2003-10-06 21:55:32 +0000330 }
331#endif
332
333 if (TftpState == STATE_RRQ || TftpState == STATE_OACK) {
wdenk3f85ce22004-02-23 16:11:30 +0000334 /* first block received */
wdenkfe8c2802002-11-03 00:38:21 +0000335 TftpState = STATE_DATA;
336 TftpServerPort = src;
337 TftpLastBlock = 0;
wdenk3f85ce22004-02-23 16:11:30 +0000338 TftpBlockWrap = 0;
339 TftpBlockWrapOffset = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000340
David Updegraff53a5c422007-06-11 10:41:07 -0500341#ifdef CONFIG_MCAST_TFTP
342 if (Multicast) { /* start!=1 common if mcast */
343 TftpLastBlock = TftpBlock - 1;
344 } else
345#endif
wdenkfe8c2802002-11-03 00:38:21 +0000346 if (TftpBlock != 1) { /* Assertion */
347 printf ("\nTFTP error: "
wdenk3f85ce22004-02-23 16:11:30 +0000348 "First block is not block 1 (%ld)\n"
wdenkfe8c2802002-11-03 00:38:21 +0000349 "Starting again\n\n",
350 TftpBlock);
351 NetStartAgain ();
352 break;
353 }
354 }
355
356 if (TftpBlock == TftpLastBlock) {
357 /*
358 * Same block again; ignore it.
359 */
360 break;
361 }
362
363 TftpLastBlock = TftpBlock;
364 NetSetTimeout (TIMEOUT * CFG_HZ, TftpTimeout);
365
366 store_block (TftpBlock - 1, pkt + 2, len);
367
368 /*
369 * Acknoledge the block just received, which will prompt
370 * the server for the next one.
371 */
David Updegraff53a5c422007-06-11 10:41:07 -0500372#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200373 /* if I am the MasterClient, actively calculate what my next
374 * needed block is; else I'm passive; not ACKING
David Updegraff53a5c422007-06-11 10:41:07 -0500375 */
376 if (Multicast) {
377 if (len < TftpBlkSize) {
378 TftpEndingBlock = TftpBlock;
379 } else if (MasterClient) {
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200380 TftpBlock = PrevBitmapHole =
David Updegraff53a5c422007-06-11 10:41:07 -0500381 ext2_find_next_zero_bit(
382 Bitmap,
383 (Mapsize*8),
384 PrevBitmapHole);
385 if (TftpBlock > ((Mapsize*8) - 1)) {
386 printf ("tftpfile too big\n");
387 /* try to double it and retry */
388 Mapsize<<=1;
389 mcast_cleanup();
390 NetStartAgain ();
391 return;
392 }
393 TftpLastBlock = TftpBlock;
394 }
395 }
396#endif
wdenkfe8c2802002-11-03 00:38:21 +0000397 TftpSend ();
398
David Updegraff53a5c422007-06-11 10:41:07 -0500399#ifdef CONFIG_MCAST_TFTP
400 if (Multicast) {
401 if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
402 puts ("\nMulticast tftp done\n");
403 mcast_cleanup();
404 NetState = NETLOOP_SUCCESS;
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200405 }
David Updegraff53a5c422007-06-11 10:41:07 -0500406 }
407 else
408#endif
409 if (len < TftpBlkSize) {
wdenkfe8c2802002-11-03 00:38:21 +0000410 /*
411 * We received the whole thing. Try to
412 * run it.
413 */
414 puts ("\ndone\n");
415 NetState = NETLOOP_SUCCESS;
416 }
417 break;
418
419 case TFTP_ERROR:
420 printf ("\nTFTP error: '%s' (%d)\n",
421 pkt + 2, ntohs(*(ushort *)pkt));
422 puts ("Starting again\n\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500423#ifdef CONFIG_MCAST_TFTP
424 mcast_cleanup();
425#endif
wdenkfe8c2802002-11-03 00:38:21 +0000426 NetStartAgain ();
427 break;
428 }
429}
430
431
432static void
433TftpTimeout (void)
434{
wdenke0ac62d2003-08-17 18:55:18 +0000435 if (++TftpTimeoutCount > TIMEOUT_COUNT) {
wdenkfe8c2802002-11-03 00:38:21 +0000436 puts ("\nRetry count exceeded; starting again\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500437#ifdef CONFIG_MCAST_TFTP
438 mcast_cleanup();
439#endif
wdenkfe8c2802002-11-03 00:38:21 +0000440 NetStartAgain ();
441 } else {
442 puts ("T ");
443 NetSetTimeout (TIMEOUT * CFG_HZ, TftpTimeout);
444 TftpSend ();
445 }
446}
447
448
449void
450TftpStart (void)
451{
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200452#ifdef CONFIG_TFTP_PORT
453 char *ep; /* Environment pointer */
454#endif
455
wdenkfe8c2802002-11-03 00:38:21 +0000456 if (BootFile[0] == '\0') {
wdenkfe8c2802002-11-03 00:38:21 +0000457 sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
Wolfgang Denkc43352c2005-08-04 01:09:44 +0200458 NetOurIP & 0xFF,
459 (NetOurIP >> 8) & 0xFF,
460 (NetOurIP >> 16) & 0xFF,
461 (NetOurIP >> 24) & 0xFF );
wdenkfe8c2802002-11-03 00:38:21 +0000462 tftp_filename = default_filename;
463
464 printf ("*** Warning: no boot file name; using '%s'\n",
465 tftp_filename);
466 } else {
467 tftp_filename = BootFile;
468 }
469
wdenk5bb226e2003-11-17 21:14:37 +0000470#if defined(CONFIG_NET_MULTI)
471 printf ("Using %s device\n", eth_get_name());
472#endif
wdenkfe8c2802002-11-03 00:38:21 +0000473 puts ("TFTP from server "); print_IPaddr (NetServerIP);
474 puts ("; our IP address is "); print_IPaddr (NetOurIP);
475
476 /* Check if we need to send across this subnet */
477 if (NetOurGatewayIP && NetOurSubnetMask) {
478 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
479 IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask;
480
481 if (OurNet != ServerNet) {
482 puts ("; sending through gateway ");
483 print_IPaddr (NetOurGatewayIP) ;
484 }
485 }
486 putc ('\n');
487
488 printf ("Filename '%s'.", tftp_filename);
489
490 if (NetBootFileSize) {
491 printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
492 print_size (NetBootFileSize<<9, "");
493 }
494
495 putc ('\n');
496
497 printf ("Load address: 0x%lx\n", load_addr);
498
499 puts ("Loading: *\b");
500
501 NetSetTimeout (TIMEOUT * CFG_HZ, TftpTimeout);
502 NetSetHandler (TftpHandler);
503
504 TftpServerPort = WELL_KNOWN_PORT;
505 TftpTimeoutCount = 0;
506 TftpState = STATE_RRQ;
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200507 /* Use a pseudo-random port unless a specific port is set */
wdenkfe8c2802002-11-03 00:38:21 +0000508 TftpOurPort = 1024 + (get_timer(0) % 3072);
David Updegraff53a5c422007-06-11 10:41:07 -0500509
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200510#ifdef CONFIG_TFTP_PORT
Wolfgang Denk28cb9372005-09-24 23:25:46 +0200511 if ((ep = getenv("tftpdstp")) != NULL) {
512 TftpServerPort = simple_strtol(ep, NULL, 10);
513 }
514 if ((ep = getenv("tftpsrcp")) != NULL) {
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200515 TftpOurPort= simple_strtol(ep, NULL, 10);
516 }
517#endif
wdenkfbe4b5c2003-10-06 21:55:32 +0000518 TftpBlock = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000519
wdenk73a8b272003-06-05 19:27:42 +0000520 /* zero out server ether in case the server ip has changed */
521 memset(NetServerEther, 0, 6);
David Updegraff53a5c422007-06-11 10:41:07 -0500522 /* Revert TftpBlkSize to dflt */
523 TftpBlkSize = TFTP_BLOCK_SIZE;
524#ifdef CONFIG_MCAST_TFTP
525 mcast_cleanup();
526#endif
wdenk73a8b272003-06-05 19:27:42 +0000527
wdenkfe8c2802002-11-03 00:38:21 +0000528 TftpSend ();
529}
530
David Updegraff53a5c422007-06-11 10:41:07 -0500531#ifdef CONFIG_MCAST_TFTP
532/* Credits: atftp project.
533 */
534
535/* pick up BcastAddr, Port, and whether I am [now] the master-client. *
536 * Frame:
537 * +-------+-----------+---+-------~~-------+---+
538 * | opc | multicast | 0 | addr, port, mc | 0 |
539 * +-------+-----------+---+-------~~-------+---+
540 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
541 * I am the new master-client so must send ACKs to DataBlocks. If I am not
542 * master-client, I'm a passive client, gathering what DataBlocks I may and
543 * making note of which ones I got in my bitmask.
544 * In theory, I never go from master->passive..
545 * .. this comes in with pkt already pointing just past opc
546 */
547static void parse_multicast_oack(char *pkt, int len)
548{
549 int i;
550 IPaddr_t addr;
551 char *mc_adr, *port, *mc;
552
553 mc_adr=port=mc=NULL;
554 /* march along looking for 'multicast\0', which has to start at least
555 * 14 bytes back from the end.
556 */
557 for (i=0;i<len-14;i++)
558 if (strcmp (pkt+i,"multicast") == 0)
559 break;
560 if (i >= (len-14)) /* non-Multicast OACK, ign. */
561 return;
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200562
David Updegraff53a5c422007-06-11 10:41:07 -0500563 i+=10; /* strlen multicast */
564 mc_adr = pkt+i;
565 for (;i<len;i++) {
566 if (*(pkt+i) == ',') {
567 *(pkt+i) = '\0';
568 if (port) {
569 mc = pkt+i+1;
570 break;
571 } else {
572 port = pkt+i+1;
573 }
574 }
575 }
576 if (!port || !mc_adr || !mc ) return;
577 if (Multicast && MasterClient) {
578 printf ("I got a OACK as master Client, WRONG!\n");
579 return;
580 }
581 /* ..I now accept packets destined for this MCAST addr, port */
582 if (!Multicast) {
583 if (Bitmap) {
584 printf ("Internal failure! no mcast.\n");
585 free(Bitmap);
586 Bitmap=NULL;
587 ProhibitMcast=1;
588 return ;
589 }
590 /* I malloc instead of pre-declare; so that if the file ends
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200591 * up being too big for this bitmap I can retry
592 */
David Updegraff53a5c422007-06-11 10:41:07 -0500593 if (!(Bitmap = malloc (Mapsize))) {
594 printf ("No Bitmap, no multicast. Sorry.\n");
595 ProhibitMcast=1;
596 return;
597 }
598 memset (Bitmap,0,Mapsize);
599 PrevBitmapHole = 0;
600 Multicast = 1;
601 }
602 addr = string_to_ip(mc_adr);
603 if (Mcast_addr != addr) {
604 if (Mcast_addr)
605 eth_mcast_join(Mcast_addr, 0);
606 if (eth_mcast_join(Mcast_addr=addr, 1)) {
607 printf ("Fail to set mcast, revert to TFTP\n");
608 ProhibitMcast=1;
609 mcast_cleanup();
610 NetStartAgain();
611 }
612 }
613 MasterClient = (unsigned char)simple_strtoul((char *)mc,NULL,10);
614 Mcast_port = (unsigned short)simple_strtoul(port,NULL,10);
615 printf ("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
616 return;
617}
618
619#endif /* Multicast TFTP */
620
Jon Loeliger30b52df2007-08-15 11:55:35 -0500621#endif