tftp.c 18 KB

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