Merge branch 'master' of /home/wd/git/u-boot/custodians
[kernel/u-boot.git] / net / tftp.c
1 /*
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 #if defined(CONFIG_CMD_NET)
14
15 #define WELL_KNOWN_PORT 69              /* Well known TFTP port #               */
16 #define TIMEOUT         5000UL          /* Millisecs to timeout for lost pkt */
17 #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
33 #define TFTP_OACK       6
34
35 static ulong TftpTimeoutMSecs = TIMEOUT;
36 static 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  */
47 ulong TftpRRQTimeoutMSecs = TIMEOUT;
48 int TftpRRQTimeoutCountMax = TIMEOUT_COUNT;
49
50 static IPaddr_t TftpServerIP;
51 static int      TftpServerPort;         /* The UDP port at their end            */
52 static int      TftpOurPort;            /* The UDP port at our end              */
53 static int      TftpTimeoutCount;
54 static ulong    TftpBlock;              /* packet sequence number               */
55 static ulong    TftpLastBlock;          /* last packet sequence number received */
56 static ulong    TftpBlockWrap;          /* count of sequence number wraparounds */
57 static ulong    TftpBlockWrapOffset;    /* memory offset due to wrapping        */
58 static int      TftpState;
59 #ifdef CONFIG_TFTP_TSIZE
60 static int      TftpTsize;              /* The file size reported by the server */
61 static short    TftpNumchars;           /* The number of hashes we printed      */
62 #endif
63
64 #define STATE_RRQ       1
65 #define STATE_DATA      2
66 #define STATE_TOO_LARGE 3
67 #define STATE_BAD_MAGIC 4
68 #define STATE_OACK      5
69
70 #define TFTP_BLOCK_SIZE         512                 /* default TFTP block size  */
71 #define TFTP_SEQUENCE_SIZE      ((ulong)(1<<16))    /* sequence number is 16 bit */
72
73 #define DEFAULT_NAME_LEN        (8 + 4 + 1)
74 static char default_filename[DEFAULT_NAME_LEN];
75
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
82 static char tftp_filename[MAX_LEN];
83
84 #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
85 extern flash_info_t flash_info[];
86 #endif
87
88 /* 512 is poor choice for ethernet, MTU is typically 1500.
89  * Minus eth.hdrs thats 1468.  Can get 2x better throughput with
90  * almost-MTU block sizes.  At least try... fall back to 512 if need be.
91  * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
92  */
93 #ifdef CONFIG_TFTP_BLOCKSIZE
94 #define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
95 #else
96 #define TFTP_MTU_BLOCKSIZE 1468
97 #endif
98
99 static unsigned short TftpBlkSize=TFTP_BLOCK_SIZE;
100 static unsigned short TftpBlkSizeOption=TFTP_MTU_BLOCKSIZE;
101
102 #ifdef CONFIG_MCAST_TFTP
103 #include <malloc.h>
104 #define MTFTP_BITMAPSIZE        0x1000
105 static unsigned *Bitmap;
106 static int PrevBitmapHole,Mapsize=MTFTP_BITMAPSIZE;
107 static uchar ProhibitMcast=0, MasterClient=0;
108 static uchar Multicast=0;
109 extern IPaddr_t Mcast_addr;
110 static int Mcast_port;
111 static ulong TftpEndingBlock; /* can get 'last' block before done..*/
112
113 static void parse_multicast_oack(char *pkt,int len);
114
115 static void
116 mcast_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
127 static __inline__ void
128 store_block (unsigned block, uchar * src, unsigned len)
129 {
130         ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
131         ulong newsize = offset + len;
132 #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
133         int i, rc = 0;
134
135         for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; i++) {
136                 /* start address in flash? */
137                 if (flash_info[i].flash_id == FLASH_UNKNOWN)
138                         continue;
139                 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 */
146                 rc = flash_write ((char *)src, (ulong)(load_addr+offset), len);
147                 if (rc) {
148                         flash_perror (rc);
149                         NetState = NETLOOP_FAIL;
150                         return;
151                 }
152         }
153         else
154 #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
155         {
156                 (void)memcpy((void *)(load_addr + offset), src, len);
157         }
158 #ifdef CONFIG_MCAST_TFTP
159         if (Multicast)
160                 ext2_set_bit(block, Bitmap);
161 #endif
162
163         if (NetBootFileXferSize < newsize)
164                 NetBootFileXferSize = newsize;
165 }
166
167 static void TftpSend (void);
168 static void TftpTimeout (void);
169
170 /**********************************************************************/
171
172 static void
173 TftpSend (void)
174 {
175         volatile uchar *        pkt;
176         volatile uchar *        xp;
177         int                     len = 0;
178         volatile ushort *s;
179
180 #ifdef CONFIG_MCAST_TFTP
181         /* Multicast TFTP.. non-MasterClients do not ACK data. */
182         if (Multicast
183          && (TftpState == STATE_DATA)
184          && (MasterClient == 0))
185                 return;
186 #endif
187         /*
188          *      We will always be sending some sort of packet, so
189          *      cobble together the packet headers now.
190          */
191         pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
192
193         switch (TftpState) {
194
195         case STATE_RRQ:
196                 xp = pkt;
197                 s = (ushort *)pkt;
198                 *s++ = htons(TFTP_RRQ);
199                 pkt = (uchar *)s;
200                 strcpy ((char *)pkt, tftp_filename);
201                 pkt += strlen(tftp_filename) + 1;
202                 strcpy ((char *)pkt, "octet");
203                 pkt += 5 /*strlen("octet")*/ + 1;
204                 strcpy ((char *)pkt, "timeout");
205                 pkt += 7 /*strlen("timeout")*/ + 1;
206                 sprintf((char *)pkt, "%lu", TIMEOUT / 1000);
207                 debug("send option \"timeout %s\"\n", (char *)pkt);
208                 pkt += strlen((char *)pkt) + 1;
209 #ifdef CONFIG_TFTP_TSIZE
210                 memcpy((char *)pkt, "tsize\0000\0", 8);
211                 pkt += 8;
212 #endif
213                 /* try for more effic. blk size */
214                 pkt += sprintf((char *)pkt,"blksize%c%d%c",
215                                 0,TftpBlkSizeOption,0);
216 #ifdef CONFIG_MCAST_TFTP
217                 /* Check all preconditions before even trying the option */
218                 if (!ProhibitMcast
219                  && (Bitmap=malloc(Mapsize))
220                  && 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 */
226                 len = pkt - xp;
227                 break;
228
229         case STATE_OACK:
230 #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:
237                 xp = pkt;
238                 s = (ushort *)pkt;
239                 *s++ = htons(TFTP_ACK);
240                 *s++ = htons(TftpBlock);
241                 pkt = (uchar *)s;
242                 len = pkt - xp;
243                 break;
244
245         case STATE_TOO_LARGE:
246                 xp = pkt;
247                 s = (ushort *)pkt;
248                 *s++ = htons(TFTP_ERROR);
249                 *s++ = htons(3);
250                 pkt = (uchar *)s;
251                 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;
258                 s = (ushort *)pkt;
259                 *s++ = htons(TFTP_ERROR);
260                 *s++ = htons(2);
261                 pkt = (uchar *)s;
262                 strcpy ((char *)pkt, "File has bad magic");
263                 pkt += 18 /*strlen("File has bad magic")*/ + 1;
264                 len = pkt - xp;
265                 break;
266         }
267
268         NetSendUDPPacket(NetServerEther, TftpServerIP, TftpServerPort, TftpOurPort, len);
269 }
270
271
272 static void
273 TftpHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
274 {
275         ushort proto;
276         ushort *s;
277         int i;
278
279         if (dest != TftpOurPort) {
280 #ifdef CONFIG_MCAST_TFTP
281                 if (Multicast
282                  && (!Mcast_port || (dest != Mcast_port)))
283 #endif
284                 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!! */
295         s = (ushort *)pkt;
296         proto = *s++;
297         pkt = (uchar *)s;
298         switch (ntohs(proto)) {
299
300         case TFTP_RRQ:
301         case TFTP_WRQ:
302         case TFTP_ACK:
303                 break;
304         default:
305                 break;
306
307         case TFTP_OACK:
308                 debug("Got OACK: %s %s\n",
309                         pkt,
310                         pkt + strlen((char *)pkt) + 1);
311                 TftpState = STATE_OACK;
312                 TftpServerPort = src;
313                 /*
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++) {
319                         if (strcmp ((char*)pkt+i,"blksize") == 0) {
320                                 TftpBlkSize = (unsigned short)
321                                         simple_strtoul((char*)pkt+i+8,NULL,10);
322                                 debug("Blocksize ack: %s, %d\n",
323                                         (char*)pkt+i+8,TftpBlkSize);
324                         }
325 #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
332                 }
333 #ifdef CONFIG_MCAST_TFTP
334                 parse_multicast_oack((char *)pkt,len-1);
335                 if ((Multicast) && (!MasterClient))
336                         TftpState = STATE_DATA; /* passive.. */
337                 else
338 #endif
339                 TftpSend (); /* Send ACK */
340                 break;
341         case TFTP_DATA:
342                 if (len < 2)
343                         return;
344                 len -= 2;
345                 TftpBlock = ntohs(*(ushort *)pkt);
346
347                 /*
348                  * 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.
352                  */
353                 if (TftpBlock == 0) {
354                         TftpBlockWrap++;
355                         TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE;
356                         printf ("\n\t %lu MB received\n\t ", TftpBlockWrapOffset>>20);
357                 }
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 {
367                         if (((TftpBlock - 1) % 10) == 0) {
368                                 putc ('#');
369                         } else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) {
370                                 puts ("\n\t ");
371                         }
372                 }
373
374                 if (TftpState == STATE_RRQ)
375                         debug("Server did not acknowledge timeout option!\n");
376
377                 if (TftpState == STATE_RRQ || TftpState == STATE_OACK) {
378                         /* first block received */
379                         TftpState = STATE_DATA;
380                         TftpServerPort = src;
381                         TftpLastBlock = 0;
382                         TftpBlockWrap = 0;
383                         TftpBlockWrapOffset = 0;
384
385 #ifdef CONFIG_MCAST_TFTP
386                         if (Multicast) { /* start!=1 common if mcast */
387                                 TftpLastBlock = TftpBlock - 1;
388                         } else
389 #endif
390                         if (TftpBlock != 1) {   /* Assertion */
391                                 printf ("\nTFTP error: "
392                                         "First block is not block 1 (%ld)\n"
393                                         "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;
408                 TftpTimeoutMSecs = TIMEOUT;
409                 TftpTimeoutCountMax = TIMEOUT_COUNT;
410                 NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
411
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                  */
418 #ifdef CONFIG_MCAST_TFTP
419                 /* if I am the MasterClient, actively calculate what my next
420                  * needed block is; else I'm passive; not ACKING
421                  */
422                 if (Multicast) {
423                         if (len < TftpBlkSize)  {
424                                 TftpEndingBlock = TftpBlock;
425                         } else if (MasterClient) {
426                                 TftpBlock = PrevBitmapHole =
427                                         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
443                 TftpSend ();
444
445 #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;
451                         }
452                 }
453                 else
454 #endif
455                 if (len < TftpBlkSize) {
456                         /*
457                          *      We received the whole thing.  Try to
458                          *      run it.
459                          */
460 #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
467                         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");
476 #ifdef CONFIG_MCAST_TFTP
477                 mcast_cleanup();
478 #endif
479                 NetStartAgain ();
480                 break;
481         }
482 }
483
484
485 static void
486 TftpTimeout (void)
487 {
488         if (++TftpTimeoutCount > TftpTimeoutCountMax) {
489                 puts ("\nRetry count exceeded; starting again\n");
490 #ifdef CONFIG_MCAST_TFTP
491                 mcast_cleanup();
492 #endif
493                 NetStartAgain ();
494         } else {
495                 puts ("T ");
496                 NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
497                 TftpSend ();
498         }
499 }
500
501
502 void
503 TftpStart (void)
504 {
505         char *ep;             /* Environment pointer */
506
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);
511
512         TftpServerIP = NetServerIP;
513         if (BootFile[0] == '\0') {
514                 sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
515                         NetOurIP & 0xFF,
516                         (NetOurIP >>  8) & 0xFF,
517                         (NetOurIP >> 16) & 0xFF,
518                         (NetOurIP >> 24) & 0xFF );
519
520                 strncpy(tftp_filename, default_filename, MAX_LEN);
521                 tftp_filename[MAX_LEN-1] = 0;
522
523                 printf ("*** Warning: no boot file name; using '%s'\n",
524                         tftp_filename);
525         } else {
526                 char *p = strchr (BootFile, ':');
527
528                 if (p == NULL) {
529                         strncpy(tftp_filename, BootFile, MAX_LEN);
530                         tftp_filename[MAX_LEN-1] = 0;
531                 } else {
532                         TftpServerIP = string_to_ip (BootFile);
533                         strncpy(tftp_filename, p + 1, MAX_LEN);
534                         tftp_filename[MAX_LEN-1] = 0;
535                 }
536         }
537
538 #if defined(CONFIG_NET_MULTI)
539         printf ("Using %s device\n", eth_get_name());
540 #endif
541         printf("TFTP from server %pI4"
542                 "; our IP address is %pI4", &TftpServerIP, &NetOurIP);
543
544         /* Check if we need to send across this subnet */
545         if (NetOurGatewayIP && NetOurSubnetMask) {
546             IPaddr_t OurNet     = NetOurIP    & NetOurSubnetMask;
547             IPaddr_t ServerNet  = TftpServerIP & NetOurSubnetMask;
548
549             if (OurNet != ServerNet)
550                 printf("; sending through gateway %pI4", &NetOurGatewayIP);
551         }
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
567         TftpTimeoutMSecs = TftpRRQTimeoutMSecs;
568         TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
569
570         NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
571         NetSetHandler (TftpHandler);
572
573         TftpServerPort = WELL_KNOWN_PORT;
574         TftpTimeoutCount = 0;
575         TftpState = STATE_RRQ;
576         /* Use a pseudo-random port unless a specific port is set */
577         TftpOurPort = 1024 + (get_timer(0) % 3072);
578
579 #ifdef CONFIG_TFTP_PORT
580         if ((ep = getenv("tftpdstp")) != NULL) {
581                 TftpServerPort = simple_strtol(ep, NULL, 10);
582         }
583         if ((ep = getenv("tftpsrcp")) != NULL) {
584                 TftpOurPort= simple_strtol(ep, NULL, 10);
585         }
586 #endif
587         TftpBlock = 0;
588
589         /* zero out server ether in case the server ip has changed */
590         memset(NetServerEther, 0, 6);
591         /* Revert TftpBlkSize to dflt */
592         TftpBlkSize = TFTP_BLOCK_SIZE;
593 #ifdef CONFIG_MCAST_TFTP
594         mcast_cleanup();
595 #endif
596 #ifdef CONFIG_TFTP_TSIZE
597         TftpTsize = 0;
598         TftpNumchars = 0;
599 #endif
600
601         TftpSend ();
602 }
603
604 #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  */
620 static 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;
635
636         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
664                  * up being too big for this bitmap I can retry
665                  */
666                 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
694 #endif