1
2
3
4
5
6
7
8
9#include <common.h>
10#include <command.h>
11#include <net.h>
12#include "tftp.h"
13#include "bootp.h"
14#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
15#include <flash.h>
16#endif
17
18
19#define WELL_KNOWN_PORT 69
20
21#define TIMEOUT 5000UL
22#ifndef CONFIG_NET_RETRY_COUNT
23
24# define TIMEOUT_COUNT 10
25#else
26# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
27#endif
28
29#define HASHES_PER_LINE 65
30
31
32
33
34#define TFTP_RRQ 1
35#define TFTP_WRQ 2
36#define TFTP_DATA 3
37#define TFTP_ACK 4
38#define TFTP_ERROR 5
39#define TFTP_OACK 6
40
41static ulong TftpTimeoutMSecs = TIMEOUT;
42static int TftpTimeoutCountMax = TIMEOUT_COUNT;
43static ulong time_start;
44
45
46
47
48
49
50
51
52
53
54ulong TftpRRQTimeoutMSecs = TIMEOUT;
55int TftpRRQTimeoutCountMax = TIMEOUT_COUNT;
56
57enum {
58 TFTP_ERR_UNDEFINED = 0,
59 TFTP_ERR_FILE_NOT_FOUND = 1,
60 TFTP_ERR_ACCESS_DENIED = 2,
61 TFTP_ERR_DISK_FULL = 3,
62 TFTP_ERR_UNEXPECTED_OPCODE = 4,
63 TFTP_ERR_UNKNOWN_TRANSFER_ID = 5,
64 TFTP_ERR_FILE_ALREADY_EXISTS = 6,
65};
66
67static IPaddr_t TftpRemoteIP;
68
69static int TftpRemotePort;
70
71static int TftpOurPort;
72static int TftpTimeoutCount;
73
74static ulong TftpBlock;
75
76static ulong TftpLastBlock;
77
78static ulong TftpBlockWrap;
79
80static ulong TftpBlockWrapOffset;
81static int TftpState;
82#ifdef CONFIG_TFTP_TSIZE
83
84static int TftpTsize;
85
86static short TftpNumchars;
87#endif
88#ifdef CONFIG_CMD_TFTPPUT
89static int TftpWriting;
90static int TftpFinalBlock;
91#else
92#define TftpWriting 0
93#endif
94
95#define STATE_SEND_RRQ 1
96#define STATE_DATA 2
97#define STATE_TOO_LARGE 3
98#define STATE_BAD_MAGIC 4
99#define STATE_OACK 5
100#define STATE_RECV_WRQ 6
101#define STATE_SEND_WRQ 7
102
103
104#define TFTP_BLOCK_SIZE 512
105
106#define TFTP_SEQUENCE_SIZE ((ulong)(1<<16))
107
108#define DEFAULT_NAME_LEN (8 + 4 + 1)
109static char default_filename[DEFAULT_NAME_LEN];
110
111#ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
112#define MAX_LEN 128
113#else
114#define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
115#endif
116
117static char tftp_filename[MAX_LEN];
118
119
120
121
122
123
124#ifdef CONFIG_TFTP_BLOCKSIZE
125#define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
126#else
127#define TFTP_MTU_BLOCKSIZE 1468
128#endif
129
130static unsigned short TftpBlkSize = TFTP_BLOCK_SIZE;
131static unsigned short TftpBlkSizeOption = TFTP_MTU_BLOCKSIZE;
132
133#ifdef CONFIG_MCAST_TFTP
134#include <malloc.h>
135#define MTFTP_BITMAPSIZE 0x1000
136static unsigned *Bitmap;
137static int PrevBitmapHole, Mapsize = MTFTP_BITMAPSIZE;
138static uchar ProhibitMcast, MasterClient;
139static uchar Multicast;
140static int Mcast_port;
141static ulong TftpEndingBlock;
142
143static void parse_multicast_oack(char *pkt, int len);
144
145static void
146mcast_cleanup(void)
147{
148 if (Mcast_addr)
149 eth_mcast_join(Mcast_addr, 0);
150 if (Bitmap)
151 free(Bitmap);
152 Bitmap = NULL;
153 Mcast_addr = Multicast = Mcast_port = 0;
154 TftpEndingBlock = -1;
155}
156
157#endif
158
159static inline void
160store_block(int block, uchar *src, unsigned len)
161{
162 ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
163 ulong newsize = offset + len;
164#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
165 int i, rc = 0;
166
167 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
168
169 if (flash_info[i].flash_id == FLASH_UNKNOWN)
170 continue;
171 if (load_addr + offset >= flash_info[i].start[0]) {
172 rc = 1;
173 break;
174 }
175 }
176
177 if (rc) {
178 rc = flash_write((char *)src, (ulong)(load_addr+offset), len);
179 if (rc) {
180 flash_perror(rc);
181 net_set_state(NETLOOP_FAIL);
182 return;
183 }
184 } else
185#endif
186 {
187 (void)memcpy((void *)(load_addr + offset), src, len);
188 }
189#ifdef CONFIG_MCAST_TFTP
190 if (Multicast)
191 ext2_set_bit(block, Bitmap);
192#endif
193
194 if (NetBootFileXferSize < newsize)
195 NetBootFileXferSize = newsize;
196}
197
198
199static void new_transfer(void)
200{
201 TftpLastBlock = 0;
202 TftpBlockWrap = 0;
203 TftpBlockWrapOffset = 0;
204#ifdef CONFIG_CMD_TFTPPUT
205 TftpFinalBlock = 0;
206#endif
207}
208
209#ifdef CONFIG_CMD_TFTPPUT
210
211
212
213
214
215
216
217
218static int load_block(unsigned block, uchar *dst, unsigned len)
219{
220
221 ulong offset = ((int)block - 1) * len + TftpBlockWrapOffset;
222 ulong tosend = len;
223
224 tosend = min(NetBootFileXferSize - offset, tosend);
225 (void)memcpy(dst, (void *)(save_addr + offset), tosend);
226 debug("%s: block=%d, offset=%ld, len=%d, tosend=%ld\n", __func__,
227 block, offset, len, tosend);
228 return tosend;
229}
230#endif
231
232static void TftpSend(void);
233static void TftpTimeout(void);
234
235
236
237static void show_block_marker(void)
238{
239#ifdef CONFIG_TFTP_TSIZE
240 if (TftpTsize) {
241 ulong pos = TftpBlock * TftpBlkSize + TftpBlockWrapOffset;
242
243 while (TftpNumchars < pos * 50 / TftpTsize) {
244 putc('#');
245 TftpNumchars++;
246 }
247 } else
248#endif
249 {
250 if (((TftpBlock - 1) % 10) == 0)
251 putc('#');
252 else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0)
253 puts("\n\t ");
254 }
255}
256
257
258
259
260
261
262static void restart(const char *msg)
263{
264 printf("\n%s; starting again\n", msg);
265#ifdef CONFIG_MCAST_TFTP
266 mcast_cleanup();
267#endif
268 NetStartAgain();
269}
270
271
272
273
274
275
276static void update_block_number(void)
277{
278
279
280
281
282
283
284 if (TftpBlock == 0) {
285 TftpBlockWrap++;
286 TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE;
287 TftpTimeoutCount = 0;
288 } else {
289 show_block_marker();
290 }
291}
292
293
294static void tftp_complete(void)
295{
296#ifdef CONFIG_TFTP_TSIZE
297
298 while (TftpTsize && TftpNumchars < 49) {
299 putc('#');
300 TftpNumchars++;
301 }
302#endif
303 time_start = get_timer(time_start);
304 if (time_start > 0) {
305 puts("\n\t ");
306 print_size(NetBootFileXferSize /
307 time_start * 1000, "/s");
308 }
309 puts("\ndone\n");
310 net_set_state(NETLOOP_SUCCESS);
311}
312
313static void
314TftpSend(void)
315{
316 uchar *pkt;
317 uchar *xp;
318 int len = 0;
319 ushort *s;
320
321#ifdef CONFIG_MCAST_TFTP
322
323 if (Multicast
324 && (TftpState == STATE_DATA)
325 && (MasterClient == 0))
326 return;
327#endif
328
329
330
331
332 pkt = NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
333
334 switch (TftpState) {
335 case STATE_SEND_RRQ:
336 case STATE_SEND_WRQ:
337 xp = pkt;
338 s = (ushort *)pkt;
339#ifdef CONFIG_CMD_TFTPPUT
340 *s++ = htons(TftpState == STATE_SEND_RRQ ? TFTP_RRQ :
341 TFTP_WRQ);
342#else
343 *s++ = htons(TFTP_RRQ);
344#endif
345 pkt = (uchar *)s;
346 strcpy((char *)pkt, tftp_filename);
347 pkt += strlen(tftp_filename) + 1;
348 strcpy((char *)pkt, "octet");
349 pkt += 5 + 1;
350 strcpy((char *)pkt, "timeout");
351 pkt += 7 + 1;
352 sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000);
353 debug("send option \"timeout %s\"\n", (char *)pkt);
354 pkt += strlen((char *)pkt) + 1;
355#ifdef CONFIG_TFTP_TSIZE
356 pkt += sprintf((char *)pkt, "tsize%c%lu%c",
357 0, NetBootFileXferSize, 0);
358#endif
359
360 pkt += sprintf((char *)pkt, "blksize%c%d%c",
361 0, TftpBlkSizeOption, 0);
362#ifdef CONFIG_MCAST_TFTP
363
364 if (!ProhibitMcast) {
365 Bitmap = malloc(Mapsize);
366 if (Bitmap && eth_get_dev()->mcast) {
367 free(Bitmap);
368 Bitmap = NULL;
369 pkt += sprintf((char *)pkt, "multicast%c%c",
370 0, 0);
371 }
372 }
373#endif
374 len = pkt - xp;
375 break;
376
377 case STATE_OACK:
378#ifdef CONFIG_MCAST_TFTP
379
380 if (Multicast)
381 TftpBlock = ext2_find_next_zero_bit(Bitmap,
382 (Mapsize*8), 0);
383
384#endif
385
386 case STATE_RECV_WRQ:
387 case STATE_DATA:
388 xp = pkt;
389 s = (ushort *)pkt;
390 s[0] = htons(TFTP_ACK);
391 s[1] = htons(TftpBlock);
392 pkt = (uchar *)(s + 2);
393#ifdef CONFIG_CMD_TFTPPUT
394 if (TftpWriting) {
395 int toload = TftpBlkSize;
396 int loaded = load_block(TftpBlock, pkt, toload);
397
398 s[0] = htons(TFTP_DATA);
399 pkt += loaded;
400 TftpFinalBlock = (loaded < toload);
401 }
402#endif
403 len = pkt - xp;
404 break;
405
406 case STATE_TOO_LARGE:
407 xp = pkt;
408 s = (ushort *)pkt;
409 *s++ = htons(TFTP_ERROR);
410 *s++ = htons(3);
411
412 pkt = (uchar *)s;
413 strcpy((char *)pkt, "File too large");
414 pkt += 14 + 1;
415 len = pkt - xp;
416 break;
417
418 case STATE_BAD_MAGIC:
419 xp = pkt;
420 s = (ushort *)pkt;
421 *s++ = htons(TFTP_ERROR);
422 *s++ = htons(2);
423 pkt = (uchar *)s;
424 strcpy((char *)pkt, "File has bad magic");
425 pkt += 18 + 1;
426 len = pkt - xp;
427 break;
428 }
429
430 NetSendUDPPacket(NetServerEther, TftpRemoteIP, TftpRemotePort,
431 TftpOurPort, len);
432}
433
434#ifdef CONFIG_CMD_TFTPPUT
435static void icmp_handler(unsigned type, unsigned code, unsigned dest,
436 IPaddr_t sip, unsigned src, uchar *pkt, unsigned len)
437{
438 if (type == ICMP_NOT_REACH && code == ICMP_NOT_REACH_PORT) {
439
440 restart("TFTP server died");
441 }
442}
443#endif
444
445static void
446TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
447 unsigned len)
448{
449 ushort proto;
450 ushort *s;
451 int i;
452
453 if (dest != TftpOurPort) {
454#ifdef CONFIG_MCAST_TFTP
455 if (Multicast
456 && (!Mcast_port || (dest != Mcast_port)))
457#endif
458 return;
459 }
460 if (TftpState != STATE_SEND_RRQ && src != TftpRemotePort &&
461 TftpState != STATE_RECV_WRQ && TftpState != STATE_SEND_WRQ)
462 return;
463
464 if (len < 2)
465 return;
466 len -= 2;
467
468 s = (ushort *)pkt;
469 proto = *s++;
470 pkt = (uchar *)s;
471 switch (ntohs(proto)) {
472
473 case TFTP_RRQ:
474 break;
475
476 case TFTP_ACK:
477#ifdef CONFIG_CMD_TFTPPUT
478 if (TftpWriting) {
479 if (TftpFinalBlock) {
480 tftp_complete();
481 } else {
482
483
484
485
486 int block = ntohs(*s);
487 int ack_ok = (TftpBlock == block);
488
489 TftpBlock = (unsigned short)(block + 1);
490 update_block_number();
491 if (ack_ok)
492 TftpSend();
493 }
494 }
495#endif
496 break;
497
498 default:
499 break;
500
501#ifdef CONFIG_CMD_TFTPSRV
502 case TFTP_WRQ:
503 debug("Got WRQ\n");
504 TftpRemoteIP = sip;
505 TftpRemotePort = src;
506 TftpOurPort = 1024 + (get_timer(0) % 3072);
507 new_transfer();
508 TftpSend();
509 break;
510#endif
511
512 case TFTP_OACK:
513 debug("Got OACK: %s %s\n",
514 pkt,
515 pkt + strlen((char *)pkt) + 1);
516 TftpState = STATE_OACK;
517 TftpRemotePort = src;
518
519
520
521
522
523 for (i = 0; i+8 < len; i++) {
524 if (strcmp((char *)pkt+i, "blksize") == 0) {
525 TftpBlkSize = (unsigned short)
526 simple_strtoul((char *)pkt+i+8, NULL,
527 10);
528 debug("Blocksize ack: %s, %d\n",
529 (char *)pkt+i+8, TftpBlkSize);
530 }
531#ifdef CONFIG_TFTP_TSIZE
532 if (strcmp((char *)pkt+i, "tsize") == 0) {
533 TftpTsize = simple_strtoul((char *)pkt+i+6,
534 NULL, 10);
535 debug("size = %s, %d\n",
536 (char *)pkt+i+6, TftpTsize);
537 }
538#endif
539 }
540#ifdef CONFIG_MCAST_TFTP
541 parse_multicast_oack((char *)pkt, len-1);
542 if ((Multicast) && (!MasterClient))
543 TftpState = STATE_DATA;
544 else
545#endif
546#ifdef CONFIG_CMD_TFTPPUT
547 if (TftpWriting) {
548
549 TftpState = STATE_DATA;
550 TftpBlock++;
551 }
552#endif
553 TftpSend();
554 break;
555 case TFTP_DATA:
556 if (len < 2)
557 return;
558 len -= 2;
559 TftpBlock = ntohs(*(ushort *)pkt);
560
561 update_block_number();
562
563 if (TftpState == STATE_SEND_RRQ)
564 debug("Server did not acknowledge timeout option!\n");
565
566 if (TftpState == STATE_SEND_RRQ || TftpState == STATE_OACK ||
567 TftpState == STATE_RECV_WRQ) {
568
569 TftpState = STATE_DATA;
570 TftpRemotePort = src;
571 new_transfer();
572
573#ifdef CONFIG_MCAST_TFTP
574 if (Multicast) {
575 TftpLastBlock = TftpBlock - 1;
576 } else
577#endif
578 if (TftpBlock != 1) {
579 printf("\nTFTP error: "
580 "First block is not block 1 (%ld)\n"
581 "Starting again\n\n",
582 TftpBlock);
583 NetStartAgain();
584 break;
585 }
586 }
587
588 if (TftpBlock == TftpLastBlock) {
589
590
591
592 break;
593 }
594
595 TftpLastBlock = TftpBlock;
596 TftpTimeoutCountMax = TIMEOUT_COUNT;
597 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
598
599 store_block(TftpBlock - 1, pkt + 2, len);
600
601
602
603
604
605#ifdef CONFIG_MCAST_TFTP
606
607
608
609 if (Multicast) {
610 if (len < TftpBlkSize) {
611 TftpEndingBlock = TftpBlock;
612 } else if (MasterClient) {
613 TftpBlock = PrevBitmapHole =
614 ext2_find_next_zero_bit(
615 Bitmap,
616 (Mapsize*8),
617 PrevBitmapHole);
618 if (TftpBlock > ((Mapsize*8) - 1)) {
619 printf("tftpfile too big\n");
620
621 Mapsize <<= 1;
622 mcast_cleanup();
623 NetStartAgain();
624 return;
625 }
626 TftpLastBlock = TftpBlock;
627 }
628 }
629#endif
630 TftpSend();
631
632#ifdef CONFIG_MCAST_TFTP
633 if (Multicast) {
634 if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
635 puts("\nMulticast tftp done\n");
636 mcast_cleanup();
637 net_set_state(NETLOOP_SUCCESS);
638 }
639 } else
640#endif
641 if (len < TftpBlkSize)
642 tftp_complete();
643 break;
644
645 case TFTP_ERROR:
646 printf("\nTFTP error: '%s' (%d)\n",
647 pkt + 2, ntohs(*(ushort *)pkt));
648
649 switch (ntohs(*(ushort *)pkt)) {
650 case TFTP_ERR_FILE_NOT_FOUND:
651 case TFTP_ERR_ACCESS_DENIED:
652 puts("Not retrying...\n");
653 eth_halt();
654 net_set_state(NETLOOP_FAIL);
655 break;
656 case TFTP_ERR_UNDEFINED:
657 case TFTP_ERR_DISK_FULL:
658 case TFTP_ERR_UNEXPECTED_OPCODE:
659 case TFTP_ERR_UNKNOWN_TRANSFER_ID:
660 case TFTP_ERR_FILE_ALREADY_EXISTS:
661 default:
662 puts("Starting again\n\n");
663#ifdef CONFIG_MCAST_TFTP
664 mcast_cleanup();
665#endif
666 NetStartAgain();
667 break;
668 }
669 break;
670 }
671}
672
673
674static void
675TftpTimeout(void)
676{
677 if (++TftpTimeoutCount > TftpTimeoutCountMax) {
678 restart("Retry count exceeded");
679 } else {
680 puts("T ");
681 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
682 if (TftpState != STATE_RECV_WRQ)
683 TftpSend();
684 }
685}
686
687
688void TftpStart(enum proto_t protocol)
689{
690 char *ep;
691
692
693
694
695
696 ep = getenv("tftpblocksize");
697 if (ep != NULL)
698 TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
699
700 ep = getenv("tftptimeout");
701 if (ep != NULL)
702 TftpTimeoutMSecs = simple_strtol(ep, NULL, 10);
703
704 if (TftpTimeoutMSecs < 1000) {
705 printf("TFTP timeout (%ld ms) too low, "
706 "set minimum = 1000 ms\n",
707 TftpTimeoutMSecs);
708 TftpTimeoutMSecs = 1000;
709 }
710
711 debug("TFTP blocksize = %i, timeout = %ld ms\n",
712 TftpBlkSizeOption, TftpTimeoutMSecs);
713
714 TftpRemoteIP = NetServerIP;
715 if (BootFile[0] == '\0') {
716 sprintf(default_filename, "%02X%02X%02X%02X.img",
717 NetOurIP & 0xFF,
718 (NetOurIP >> 8) & 0xFF,
719 (NetOurIP >> 16) & 0xFF,
720 (NetOurIP >> 24) & 0xFF);
721
722 strncpy(tftp_filename, default_filename, MAX_LEN);
723 tftp_filename[MAX_LEN-1] = 0;
724
725 printf("*** Warning: no boot file name; using '%s'\n",
726 tftp_filename);
727 } else {
728 char *p = strchr(BootFile, ':');
729
730 if (p == NULL) {
731 strncpy(tftp_filename, BootFile, MAX_LEN);
732 tftp_filename[MAX_LEN-1] = 0;
733 } else {
734 TftpRemoteIP = string_to_ip(BootFile);
735 strncpy(tftp_filename, p + 1, MAX_LEN);
736 tftp_filename[MAX_LEN-1] = 0;
737 }
738 }
739
740 printf("Using %s device\n", eth_get_name());
741 printf("TFTP %s server %pI4; our IP address is %pI4",
742#ifdef CONFIG_CMD_TFTPPUT
743 protocol == TFTPPUT ? "to" : "from",
744#else
745 "from",
746#endif
747 &TftpRemoteIP, &NetOurIP);
748
749
750 if (NetOurGatewayIP && NetOurSubnetMask) {
751 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
752 IPaddr_t RemoteNet = TftpRemoteIP & NetOurSubnetMask;
753
754 if (OurNet != RemoteNet)
755 printf("; sending through gateway %pI4",
756 &NetOurGatewayIP);
757 }
758 putc('\n');
759
760 printf("Filename '%s'.", tftp_filename);
761
762 if (NetBootFileSize) {
763 printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
764 print_size(NetBootFileSize<<9, "");
765 }
766
767 putc('\n');
768#ifdef CONFIG_CMD_TFTPPUT
769 TftpWriting = (protocol == TFTPPUT);
770 if (TftpWriting) {
771 printf("Save address: 0x%lx\n", save_addr);
772 printf("Save size: 0x%lx\n", save_size);
773 NetBootFileXferSize = save_size;
774 puts("Saving: *\b");
775 TftpState = STATE_SEND_WRQ;
776 new_transfer();
777 } else
778#endif
779 {
780 printf("Load address: 0x%lx\n", load_addr);
781 puts("Loading: *\b");
782 TftpState = STATE_SEND_RRQ;
783 }
784
785 time_start = get_timer(0);
786 TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
787
788 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
789 net_set_udp_handler(TftpHandler);
790#ifdef CONFIG_CMD_TFTPPUT
791 net_set_icmp_handler(icmp_handler);
792#endif
793 TftpRemotePort = WELL_KNOWN_PORT;
794 TftpTimeoutCount = 0;
795
796 TftpOurPort = 1024 + (get_timer(0) % 3072);
797
798#ifdef CONFIG_TFTP_PORT
799 ep = getenv("tftpdstp");
800 if (ep != NULL)
801 TftpRemotePort = simple_strtol(ep, NULL, 10);
802 ep = getenv("tftpsrcp");
803 if (ep != NULL)
804 TftpOurPort = simple_strtol(ep, NULL, 10);
805#endif
806 TftpBlock = 0;
807
808
809 memset(NetServerEther, 0, 6);
810
811 TftpBlkSize = TFTP_BLOCK_SIZE;
812#ifdef CONFIG_MCAST_TFTP
813 mcast_cleanup();
814#endif
815#ifdef CONFIG_TFTP_TSIZE
816 TftpTsize = 0;
817 TftpNumchars = 0;
818#endif
819
820 TftpSend();
821}
822
823#ifdef CONFIG_CMD_TFTPSRV
824void
825TftpStartServer(void)
826{
827 tftp_filename[0] = 0;
828
829 printf("Using %s device\n", eth_get_name());
830 printf("Listening for TFTP transfer on %pI4\n", &NetOurIP);
831 printf("Load address: 0x%lx\n", load_addr);
832
833 puts("Loading: *\b");
834
835 TftpTimeoutCountMax = TIMEOUT_COUNT;
836 TftpTimeoutCount = 0;
837 TftpTimeoutMSecs = TIMEOUT;
838 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
839
840
841 TftpBlkSize = TFTP_BLOCK_SIZE;
842 TftpBlock = 0;
843 TftpOurPort = WELL_KNOWN_PORT;
844
845#ifdef CONFIG_TFTP_TSIZE
846 TftpTsize = 0;
847 TftpNumchars = 0;
848#endif
849
850 TftpState = STATE_RECV_WRQ;
851 net_set_udp_handler(TftpHandler);
852}
853#endif
854
855#ifdef CONFIG_MCAST_TFTP
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871static void parse_multicast_oack(char *pkt, int len)
872{
873 int i;
874 IPaddr_t addr;
875 char *mc_adr, *port, *mc;
876
877 mc_adr = port = mc = NULL;
878
879
880
881 for (i = 0; i < len-14; i++)
882 if (strcmp(pkt+i, "multicast") == 0)
883 break;
884 if (i >= (len-14))
885 return;
886
887 i += 10;
888 mc_adr = pkt+i;
889 for (; i < len; i++) {
890 if (*(pkt+i) == ',') {
891 *(pkt+i) = '\0';
892 if (port) {
893 mc = pkt+i+1;
894 break;
895 } else {
896 port = pkt+i+1;
897 }
898 }
899 }
900 if (!port || !mc_adr || !mc)
901 return;
902 if (Multicast && MasterClient) {
903 printf("I got a OACK as master Client, WRONG!\n");
904 return;
905 }
906
907 if (!Multicast) {
908 if (Bitmap) {
909 printf("Internal failure! no mcast.\n");
910 free(Bitmap);
911 Bitmap = NULL;
912 ProhibitMcast = 1;
913 return ;
914 }
915
916
917
918 Bitmap = malloc(Mapsize);
919 if (!Bitmap) {
920 printf("No Bitmap, no multicast. Sorry.\n");
921 ProhibitMcast = 1;
922 return;
923 }
924 memset(Bitmap, 0, Mapsize);
925 PrevBitmapHole = 0;
926 Multicast = 1;
927 }
928 addr = string_to_ip(mc_adr);
929 if (Mcast_addr != addr) {
930 if (Mcast_addr)
931 eth_mcast_join(Mcast_addr, 0);
932 Mcast_addr = addr;
933 if (eth_mcast_join(Mcast_addr, 1)) {
934 printf("Fail to set mcast, revert to TFTP\n");
935 ProhibitMcast = 1;
936 mcast_cleanup();
937 NetStartAgain();
938 }
939 }
940 MasterClient = (unsigned char)simple_strtoul((char *)mc, NULL, 10);
941 Mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
942 printf("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
943 return;
944}
945
946#endif
947