tftp.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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. #include <common.h>
  7. #include <command.h>
  8. #include <net.h>
  9. #include "tftp.h"
  10. #include "bootp.h"
  11. #if defined(CONFIG_CMD_NET)
  12. #define WELL_KNOWN_PORT 69 /* Well known TFTP port # */
  13. #define TIMEOUT 5000UL /* Millisecs to timeout for lost pkt */
  14. #ifndef CONFIG_NET_RETRY_COUNT
  15. # define TIMEOUT_COUNT 10 /* # of timeouts before giving up */
  16. #else
  17. # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
  18. #endif
  19. /* (for checking the image size) */
  20. #define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
  21. /*
  22. * TFTP operations.
  23. */
  24. #define TFTP_RRQ 1
  25. #define TFTP_WRQ 2
  26. #define TFTP_DATA 3
  27. #define TFTP_ACK 4
  28. #define TFTP_ERROR 5
  29. #define TFTP_OACK 6
  30. static ulong TftpTimeoutMSecs = TIMEOUT;
  31. static int TftpTimeoutCountMax = TIMEOUT_COUNT;
  32. /*
  33. * These globals govern the timeout behavior when attempting a connection to a
  34. * TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to
  35. * wait for the server to respond to initial connection. Second global,
  36. * TftpRRQTimeoutCountMax, gives the number of such connection retries.
  37. * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be
  38. * positive. The globals are meant to be set (and restored) by code needing
  39. * non-standard timeout behavior when initiating a TFTP transfer.
  40. */
  41. ulong TftpRRQTimeoutMSecs = TIMEOUT;
  42. int TftpRRQTimeoutCountMax = TIMEOUT_COUNT;
  43. static IPaddr_t TftpServerIP;
  44. static int TftpServerPort; /* The UDP port at their end */
  45. static int TftpOurPort; /* The UDP port at our end */
  46. static int TftpTimeoutCount;
  47. static ulong TftpBlock; /* packet sequence number */
  48. static ulong TftpLastBlock; /* last packet sequence number received */
  49. static ulong TftpBlockWrap; /* count of sequence number wraparounds */
  50. static ulong TftpBlockWrapOffset; /* memory offset due to wrapping */
  51. static int TftpState;
  52. #define STATE_RRQ 1
  53. #define STATE_DATA 2
  54. #define STATE_TOO_LARGE 3
  55. #define STATE_BAD_MAGIC 4
  56. #define STATE_OACK 5
  57. #define TFTP_BLOCK_SIZE 512 /* default TFTP block size */
  58. #define TFTP_SEQUENCE_SIZE ((ulong)(1<<16)) /* sequence number is 16 bit */
  59. #define DEFAULT_NAME_LEN (8 + 4 + 1)
  60. static char default_filename[DEFAULT_NAME_LEN];
  61. #ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
  62. #define MAX_LEN 128
  63. #else
  64. #define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
  65. #endif
  66. static char tftp_filename[MAX_LEN];
  67. #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
  68. extern flash_info_t flash_info[];
  69. #endif
  70. /* 512 is poor choice for ethernet, MTU is typically 1500.
  71. * Minus eth.hdrs thats 1468. Can get 2x better throughput with
  72. * almost-MTU block sizes. At least try... fall back to 512 if need be.
  73. * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
  74. */
  75. #ifdef CONFIG_TFTP_BLOCKSIZE
  76. #define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
  77. #else
  78. #define TFTP_MTU_BLOCKSIZE 1468
  79. #endif
  80. static unsigned short TftpBlkSize=TFTP_BLOCK_SIZE;
  81. static unsigned short TftpBlkSizeOption=TFTP_MTU_BLOCKSIZE;
  82. #ifdef CONFIG_MCAST_TFTP
  83. #include <malloc.h>
  84. #define MTFTP_BITMAPSIZE 0x1000
  85. static unsigned *Bitmap;
  86. static int PrevBitmapHole,Mapsize=MTFTP_BITMAPSIZE;
  87. static uchar ProhibitMcast=0, MasterClient=0;
  88. static uchar Multicast=0;
  89. extern IPaddr_t Mcast_addr;
  90. static int Mcast_port;
  91. static ulong TftpEndingBlock; /* can get 'last' block before done..*/
  92. static void parse_multicast_oack(char *pkt,int len);
  93. static void
  94. mcast_cleanup(void)
  95. {
  96. if (Mcast_addr) eth_mcast_join(Mcast_addr, 0);
  97. if (Bitmap) free(Bitmap);
  98. Bitmap=NULL;
  99. Mcast_addr = Multicast = Mcast_port = 0;
  100. TftpEndingBlock = -1;
  101. }
  102. #endif /* CONFIG_MCAST_TFTP */
  103. static __inline__ void
  104. store_block (unsigned block, uchar * src, unsigned len)
  105. {
  106. ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
  107. ulong newsize = offset + len;
  108. #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
  109. int i, rc = 0;
  110. for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  111. /* start address in flash? */
  112. if (flash_info[i].flash_id == FLASH_UNKNOWN)
  113. continue;
  114. if (load_addr + offset >= flash_info[i].start[0]) {
  115. rc = 1;
  116. break;
  117. }
  118. }
  119. if (rc) { /* Flash is destination for this packet */
  120. rc = flash_write ((char *)src, (ulong)(load_addr+offset), len);
  121. if (rc) {
  122. flash_perror (rc);
  123. NetState = NETLOOP_FAIL;
  124. return;
  125. }
  126. }
  127. else
  128. #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
  129. {
  130. (void)memcpy((void *)(load_addr + offset), src, len);
  131. }
  132. #ifdef CONFIG_MCAST_TFTP
  133. if (Multicast)
  134. ext2_set_bit(block, Bitmap);
  135. #endif
  136. if (NetBootFileXferSize < newsize)
  137. NetBootFileXferSize = newsize;
  138. }
  139. static void TftpSend (void);
  140. static void TftpTimeout (void);
  141. /**********************************************************************/
  142. static void
  143. TftpSend (void)
  144. {
  145. volatile uchar * pkt;
  146. volatile uchar * xp;
  147. int len = 0;
  148. volatile ushort *s;
  149. #ifdef CONFIG_MCAST_TFTP
  150. /* Multicast TFTP.. non-MasterClients do not ACK data. */
  151. if (Multicast
  152. && (TftpState == STATE_DATA)
  153. && (MasterClient == 0))
  154. return;
  155. #endif
  156. /*
  157. * We will always be sending some sort of packet, so
  158. * cobble together the packet headers now.
  159. */
  160. pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
  161. switch (TftpState) {
  162. case STATE_RRQ:
  163. xp = pkt;
  164. s = (ushort *)pkt;
  165. *s++ = htons(TFTP_RRQ);
  166. pkt = (uchar *)s;
  167. strcpy ((char *)pkt, tftp_filename);
  168. pkt += strlen(tftp_filename) + 1;
  169. strcpy ((char *)pkt, "octet");
  170. pkt += 5 /*strlen("octet")*/ + 1;
  171. strcpy ((char *)pkt, "timeout");
  172. pkt += 7 /*strlen("timeout")*/ + 1;
  173. sprintf((char *)pkt, "%lu", TIMEOUT / 1000);
  174. debug("send option \"timeout %s\"\n", (char *)pkt);
  175. pkt += strlen((char *)pkt) + 1;
  176. /* try for more effic. blk size */
  177. pkt += sprintf((char *)pkt,"blksize%c%d%c",
  178. 0,TftpBlkSizeOption,0);
  179. #ifdef CONFIG_MCAST_TFTP
  180. /* Check all preconditions before even trying the option */
  181. if (!ProhibitMcast
  182. && (Bitmap=malloc(Mapsize))
  183. && eth_get_dev()->mcast) {
  184. free(Bitmap);
  185. Bitmap=NULL;
  186. pkt += sprintf((char *)pkt,"multicast%c%c",0,0);
  187. }
  188. #endif /* CONFIG_MCAST_TFTP */
  189. len = pkt - xp;
  190. break;
  191. case STATE_OACK:
  192. #ifdef CONFIG_MCAST_TFTP
  193. /* My turn! Start at where I need blocks I missed.*/
  194. if (Multicast)
  195. TftpBlock=ext2_find_next_zero_bit(Bitmap,(Mapsize*8),0);
  196. /*..falling..*/
  197. #endif
  198. case STATE_DATA:
  199. xp = pkt;
  200. s = (ushort *)pkt;
  201. *s++ = htons(TFTP_ACK);
  202. *s++ = htons(TftpBlock);
  203. pkt = (uchar *)s;
  204. len = pkt - xp;
  205. break;
  206. case STATE_TOO_LARGE:
  207. xp = pkt;
  208. s = (ushort *)pkt;
  209. *s++ = htons(TFTP_ERROR);
  210. *s++ = htons(3);
  211. pkt = (uchar *)s;
  212. strcpy ((char *)pkt, "File too large");
  213. pkt += 14 /*strlen("File too large")*/ + 1;
  214. len = pkt - xp;
  215. break;
  216. case STATE_BAD_MAGIC:
  217. xp = pkt;
  218. s = (ushort *)pkt;
  219. *s++ = htons(TFTP_ERROR);
  220. *s++ = htons(2);
  221. pkt = (uchar *)s;
  222. strcpy ((char *)pkt, "File has bad magic");
  223. pkt += 18 /*strlen("File has bad magic")*/ + 1;
  224. len = pkt - xp;
  225. break;
  226. }
  227. NetSendUDPPacket(NetServerEther, TftpServerIP, TftpServerPort, TftpOurPort, len);
  228. }
  229. static void
  230. TftpHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
  231. {
  232. ushort proto;
  233. ushort *s;
  234. int i;
  235. if (dest != TftpOurPort) {
  236. #ifdef CONFIG_MCAST_TFTP
  237. if (Multicast
  238. && (!Mcast_port || (dest != Mcast_port)))
  239. #endif
  240. return;
  241. }
  242. if (TftpState != STATE_RRQ && src != TftpServerPort) {
  243. return;
  244. }
  245. if (len < 2) {
  246. return;
  247. }
  248. len -= 2;
  249. /* warning: don't use increment (++) in ntohs() macros!! */
  250. s = (ushort *)pkt;
  251. proto = *s++;
  252. pkt = (uchar *)s;
  253. switch (ntohs(proto)) {
  254. case TFTP_RRQ:
  255. case TFTP_WRQ:
  256. case TFTP_ACK:
  257. break;
  258. default:
  259. break;
  260. case TFTP_OACK:
  261. debug("Got OACK: %s %s\n",
  262. pkt,
  263. pkt + strlen((char *)pkt) + 1);
  264. TftpState = STATE_OACK;
  265. TftpServerPort = src;
  266. /*
  267. * Check for 'blksize' option.
  268. * Careful: "i" is signed, "len" is unsigned, thus
  269. * something like "len-8" may give a *huge* number
  270. */
  271. for (i=0; i+8<len; i++) {
  272. if (strcmp ((char*)pkt+i,"blksize") == 0) {
  273. TftpBlkSize = (unsigned short)
  274. simple_strtoul((char*)pkt+i+8,NULL,10);
  275. debug("Blocksize ack: %s, %d\n",
  276. (char*)pkt+i+8,TftpBlkSize);
  277. break;
  278. }
  279. }
  280. #ifdef CONFIG_MCAST_TFTP
  281. parse_multicast_oack((char *)pkt,len-1);
  282. if ((Multicast) && (!MasterClient))
  283. TftpState = STATE_DATA; /* passive.. */
  284. else
  285. #endif
  286. TftpSend (); /* Send ACK */
  287. break;
  288. case TFTP_DATA:
  289. if (len < 2)
  290. return;
  291. len -= 2;
  292. TftpBlock = ntohs(*(ushort *)pkt);
  293. /*
  294. * RFC1350 specifies that the first data packet will
  295. * have sequence number 1. If we receive a sequence
  296. * number of 0 this means that there was a wrap
  297. * around of the (16 bit) counter.
  298. */
  299. if (TftpBlock == 0) {
  300. TftpBlockWrap++;
  301. TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE;
  302. printf ("\n\t %lu MB received\n\t ", TftpBlockWrapOffset>>20);
  303. } else {
  304. if (((TftpBlock - 1) % 10) == 0) {
  305. putc ('#');
  306. } else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) {
  307. puts ("\n\t ");
  308. }
  309. }
  310. if (TftpState == STATE_RRQ)
  311. debug("Server did not acknowledge timeout option!\n");
  312. if (TftpState == STATE_RRQ || TftpState == STATE_OACK) {
  313. /* first block received */
  314. TftpState = STATE_DATA;
  315. TftpServerPort = src;
  316. TftpLastBlock = 0;
  317. TftpBlockWrap = 0;
  318. TftpBlockWrapOffset = 0;
  319. #ifdef CONFIG_MCAST_TFTP
  320. if (Multicast) { /* start!=1 common if mcast */
  321. TftpLastBlock = TftpBlock - 1;
  322. } else
  323. #endif
  324. if (TftpBlock != 1) { /* Assertion */
  325. printf ("\nTFTP error: "
  326. "First block is not block 1 (%ld)\n"
  327. "Starting again\n\n",
  328. TftpBlock);
  329. NetStartAgain ();
  330. break;
  331. }
  332. }
  333. if (TftpBlock == TftpLastBlock) {
  334. /*
  335. * Same block again; ignore it.
  336. */
  337. break;
  338. }
  339. TftpLastBlock = TftpBlock;
  340. TftpTimeoutMSecs = TIMEOUT;
  341. TftpTimeoutCountMax = TIMEOUT_COUNT;
  342. NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
  343. store_block (TftpBlock - 1, pkt + 2, len);
  344. /*
  345. * Acknoledge the block just received, which will prompt
  346. * the server for the next one.
  347. */
  348. #ifdef CONFIG_MCAST_TFTP
  349. /* if I am the MasterClient, actively calculate what my next
  350. * needed block is; else I'm passive; not ACKING
  351. */
  352. if (Multicast) {
  353. if (len < TftpBlkSize) {
  354. TftpEndingBlock = TftpBlock;
  355. } else if (MasterClient) {
  356. TftpBlock = PrevBitmapHole =
  357. ext2_find_next_zero_bit(
  358. Bitmap,
  359. (Mapsize*8),
  360. PrevBitmapHole);
  361. if (TftpBlock > ((Mapsize*8) - 1)) {
  362. printf ("tftpfile too big\n");
  363. /* try to double it and retry */
  364. Mapsize<<=1;
  365. mcast_cleanup();
  366. NetStartAgain ();
  367. return;
  368. }
  369. TftpLastBlock = TftpBlock;
  370. }
  371. }
  372. #endif
  373. TftpSend ();
  374. #ifdef CONFIG_MCAST_TFTP
  375. if (Multicast) {
  376. if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
  377. puts ("\nMulticast tftp done\n");
  378. mcast_cleanup();
  379. NetState = NETLOOP_SUCCESS;
  380. }
  381. }
  382. else
  383. #endif
  384. if (len < TftpBlkSize) {
  385. /*
  386. * We received the whole thing. Try to
  387. * run it.
  388. */
  389. puts ("\ndone\n");
  390. NetState = NETLOOP_SUCCESS;
  391. }
  392. break;
  393. case TFTP_ERROR:
  394. printf ("\nTFTP error: '%s' (%d)\n",
  395. pkt + 2, ntohs(*(ushort *)pkt));
  396. puts ("Starting again\n\n");
  397. #ifdef CONFIG_MCAST_TFTP
  398. mcast_cleanup();
  399. #endif
  400. NetStartAgain ();
  401. break;
  402. }
  403. }
  404. static void
  405. TftpTimeout (void)
  406. {
  407. if (++TftpTimeoutCount > TftpTimeoutCountMax) {
  408. puts ("\nRetry count exceeded; starting again\n");
  409. #ifdef CONFIG_MCAST_TFTP
  410. mcast_cleanup();
  411. #endif
  412. NetStartAgain ();
  413. } else {
  414. puts ("T ");
  415. NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
  416. TftpSend ();
  417. }
  418. }
  419. void
  420. TftpStart (void)
  421. {
  422. char *ep; /* Environment pointer */
  423. /* Allow the user to choose tftpblocksize */
  424. if ((ep = getenv("tftpblocksize")) != NULL)
  425. TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
  426. debug("tftp block size is %i\n", TftpBlkSizeOption);
  427. TftpServerIP = NetServerIP;
  428. if (BootFile[0] == '\0') {
  429. sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
  430. NetOurIP & 0xFF,
  431. (NetOurIP >> 8) & 0xFF,
  432. (NetOurIP >> 16) & 0xFF,
  433. (NetOurIP >> 24) & 0xFF );
  434. strncpy(tftp_filename, default_filename, MAX_LEN);
  435. tftp_filename[MAX_LEN-1] = 0;
  436. printf ("*** Warning: no boot file name; using '%s'\n",
  437. tftp_filename);
  438. } else {
  439. char *p = strchr (BootFile, ':');
  440. if (p == NULL) {
  441. strncpy(tftp_filename, BootFile, MAX_LEN);
  442. tftp_filename[MAX_LEN-1] = 0;
  443. } else {
  444. TftpServerIP = string_to_ip (BootFile);
  445. strncpy(tftp_filename, p + 1, MAX_LEN);
  446. tftp_filename[MAX_LEN-1] = 0;
  447. }
  448. }
  449. #if defined(CONFIG_NET_MULTI)
  450. printf ("Using %s device\n", eth_get_name());
  451. #endif
  452. printf("TFTP from server %pI4"
  453. "; our IP address is %pI4", &TftpServerIP, &NetOurIP);
  454. /* Check if we need to send across this subnet */
  455. if (NetOurGatewayIP && NetOurSubnetMask) {
  456. IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
  457. IPaddr_t ServerNet = TftpServerIP & NetOurSubnetMask;
  458. if (OurNet != ServerNet)
  459. printf("; sending through gateway %pI4", &NetOurGatewayIP);
  460. }
  461. putc ('\n');
  462. printf ("Filename '%s'.", tftp_filename);
  463. if (NetBootFileSize) {
  464. printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
  465. print_size (NetBootFileSize<<9, "");
  466. }
  467. putc ('\n');
  468. printf ("Load address: 0x%lx\n", load_addr);
  469. puts ("Loading: *\b");
  470. TftpTimeoutMSecs = TftpRRQTimeoutMSecs;
  471. TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
  472. NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
  473. NetSetHandler (TftpHandler);
  474. TftpServerPort = WELL_KNOWN_PORT;
  475. TftpTimeoutCount = 0;
  476. TftpState = STATE_RRQ;
  477. /* Use a pseudo-random port unless a specific port is set */
  478. TftpOurPort = 1024 + (get_timer(0) % 3072);
  479. #ifdef CONFIG_TFTP_PORT
  480. if ((ep = getenv("tftpdstp")) != NULL) {
  481. TftpServerPort = simple_strtol(ep, NULL, 10);
  482. }
  483. if ((ep = getenv("tftpsrcp")) != NULL) {
  484. TftpOurPort= simple_strtol(ep, NULL, 10);
  485. }
  486. #endif
  487. TftpBlock = 0;
  488. /* zero out server ether in case the server ip has changed */
  489. memset(NetServerEther, 0, 6);
  490. /* Revert TftpBlkSize to dflt */
  491. TftpBlkSize = TFTP_BLOCK_SIZE;
  492. #ifdef CONFIG_MCAST_TFTP
  493. mcast_cleanup();
  494. #endif
  495. TftpSend ();
  496. }
  497. #ifdef CONFIG_MCAST_TFTP
  498. /* Credits: atftp project.
  499. */
  500. /* pick up BcastAddr, Port, and whether I am [now] the master-client. *
  501. * Frame:
  502. * +-------+-----------+---+-------~~-------+---+
  503. * | opc | multicast | 0 | addr, port, mc | 0 |
  504. * +-------+-----------+---+-------~~-------+---+
  505. * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
  506. * I am the new master-client so must send ACKs to DataBlocks. If I am not
  507. * master-client, I'm a passive client, gathering what DataBlocks I may and
  508. * making note of which ones I got in my bitmask.
  509. * In theory, I never go from master->passive..
  510. * .. this comes in with pkt already pointing just past opc
  511. */
  512. static void parse_multicast_oack(char *pkt, int len)
  513. {
  514. int i;
  515. IPaddr_t addr;
  516. char *mc_adr, *port, *mc;
  517. mc_adr=port=mc=NULL;
  518. /* march along looking for 'multicast\0', which has to start at least
  519. * 14 bytes back from the end.
  520. */
  521. for (i=0;i<len-14;i++)
  522. if (strcmp (pkt+i,"multicast") == 0)
  523. break;
  524. if (i >= (len-14)) /* non-Multicast OACK, ign. */
  525. return;
  526. i+=10; /* strlen multicast */
  527. mc_adr = pkt+i;
  528. for (;i<len;i++) {
  529. if (*(pkt+i) == ',') {
  530. *(pkt+i) = '\0';
  531. if (port) {
  532. mc = pkt+i+1;
  533. break;
  534. } else {
  535. port = pkt+i+1;
  536. }
  537. }
  538. }
  539. if (!port || !mc_adr || !mc ) return;
  540. if (Multicast && MasterClient) {
  541. printf ("I got a OACK as master Client, WRONG!\n");
  542. return;
  543. }
  544. /* ..I now accept packets destined for this MCAST addr, port */
  545. if (!Multicast) {
  546. if (Bitmap) {
  547. printf ("Internal failure! no mcast.\n");
  548. free(Bitmap);
  549. Bitmap=NULL;
  550. ProhibitMcast=1;
  551. return ;
  552. }
  553. /* I malloc instead of pre-declare; so that if the file ends
  554. * up being too big for this bitmap I can retry
  555. */
  556. if (!(Bitmap = malloc (Mapsize))) {
  557. printf ("No Bitmap, no multicast. Sorry.\n");
  558. ProhibitMcast=1;
  559. return;
  560. }
  561. memset (Bitmap,0,Mapsize);
  562. PrevBitmapHole = 0;
  563. Multicast = 1;
  564. }
  565. addr = string_to_ip(mc_adr);
  566. if (Mcast_addr != addr) {
  567. if (Mcast_addr)
  568. eth_mcast_join(Mcast_addr, 0);
  569. if (eth_mcast_join(Mcast_addr=addr, 1)) {
  570. printf ("Fail to set mcast, revert to TFTP\n");
  571. ProhibitMcast=1;
  572. mcast_cleanup();
  573. NetStartAgain();
  574. }
  575. }
  576. MasterClient = (unsigned char)simple_strtoul((char *)mc,NULL,10);
  577. Mcast_port = (unsigned short)simple_strtoul(port,NULL,10);
  578. printf ("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
  579. return;
  580. }
  581. #endif /* Multicast TFTP */
  582. #endif