tftp.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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 TftpServerIP;
  55. /* The UDP port at their end */
  56. static int TftpServerPort;
  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 = 0, MasterClient = 0;
  113. static uchar Multicast = 0;
  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) eth_mcast_join(Mcast_addr, 0);
  122. if (Bitmap) free(Bitmap);
  123. Bitmap = NULL;
  124. Mcast_addr = Multicast = Mcast_port = 0;
  125. TftpEndingBlock = -1;
  126. }
  127. #endif /* CONFIG_MCAST_TFTP */
  128. static __inline__ void
  129. store_block(unsigned block, uchar * src, unsigned len)
  130. {
  131. ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
  132. ulong newsize = offset + len;
  133. #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
  134. int i, rc = 0;
  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. if (rc) { /* Flash is destination for this packet */
  145. rc = flash_write((char *)src, (ulong)(load_addr+offset), len);
  146. if (rc) {
  147. flash_perror(rc);
  148. NetState = NETLOOP_FAIL;
  149. return;
  150. }
  151. }
  152. else
  153. #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
  154. {
  155. (void)memcpy((void *)(load_addr + offset), src, len);
  156. }
  157. #ifdef CONFIG_MCAST_TFTP
  158. if (Multicast)
  159. ext2_set_bit(block, Bitmap);
  160. #endif
  161. if (NetBootFileXferSize < newsize)
  162. NetBootFileXferSize = newsize;
  163. }
  164. static void TftpSend(void);
  165. static void TftpTimeout(void);
  166. /**********************************************************************/
  167. static void
  168. TftpSend(void)
  169. {
  170. volatile uchar * pkt;
  171. volatile uchar * xp;
  172. int len = 0;
  173. volatile ushort *s;
  174. #ifdef CONFIG_MCAST_TFTP
  175. /* Multicast TFTP.. non-MasterClients do not ACK data. */
  176. if (Multicast
  177. && (TftpState == STATE_DATA)
  178. && (MasterClient == 0))
  179. return;
  180. #endif
  181. /*
  182. * We will always be sending some sort of packet, so
  183. * cobble together the packet headers now.
  184. */
  185. pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
  186. switch (TftpState) {
  187. case STATE_RRQ:
  188. xp = pkt;
  189. s = (ushort *)pkt;
  190. *s++ = htons(TFTP_RRQ);
  191. pkt = (uchar *)s;
  192. strcpy((char *)pkt, tftp_filename);
  193. pkt += strlen(tftp_filename) + 1;
  194. strcpy((char *)pkt, "octet");
  195. pkt += 5 /*strlen("octet")*/ + 1;
  196. strcpy((char *)pkt, "timeout");
  197. pkt += 7 /*strlen("timeout")*/ + 1;
  198. sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000);
  199. debug("send option \"timeout %s\"\n", (char *)pkt);
  200. pkt += strlen((char *)pkt) + 1;
  201. #ifdef CONFIG_TFTP_TSIZE
  202. memcpy((char *)pkt, "tsize\0000\0", 8);
  203. pkt += 8;
  204. #endif
  205. /* try for more effic. blk size */
  206. pkt += sprintf((char *)pkt, "blksize%c%d%c",
  207. 0, TftpBlkSizeOption, 0);
  208. #ifdef CONFIG_MCAST_TFTP
  209. /* Check all preconditions before even trying the option */
  210. if (!ProhibitMcast
  211. && (Bitmap = malloc(Mapsize))
  212. && eth_get_dev()->mcast) {
  213. free(Bitmap);
  214. Bitmap = NULL;
  215. pkt += sprintf((char *)pkt, "multicast%c%c", 0, 0);
  216. }
  217. #endif /* CONFIG_MCAST_TFTP */
  218. len = pkt - xp;
  219. break;
  220. case STATE_OACK:
  221. #ifdef CONFIG_MCAST_TFTP
  222. /* My turn! Start at where I need blocks I missed.*/
  223. if (Multicast)
  224. TftpBlock = ext2_find_next_zero_bit(Bitmap,
  225. (Mapsize*8), 0);
  226. /*..falling..*/
  227. #endif
  228. case STATE_DATA:
  229. xp = pkt;
  230. s = (ushort *)pkt;
  231. *s++ = htons(TFTP_ACK);
  232. *s++ = htons(TftpBlock);
  233. pkt = (uchar *)s;
  234. len = pkt - xp;
  235. break;
  236. case STATE_TOO_LARGE:
  237. xp = pkt;
  238. s = (ushort *)pkt;
  239. *s++ = htons(TFTP_ERROR);
  240. *s++ = htons(3);
  241. pkt = (uchar *)s;
  242. strcpy((char *)pkt, "File too large");
  243. pkt += 14 /*strlen("File too large")*/ + 1;
  244. len = pkt - xp;
  245. break;
  246. case STATE_BAD_MAGIC:
  247. xp = pkt;
  248. s = (ushort *)pkt;
  249. *s++ = htons(TFTP_ERROR);
  250. *s++ = htons(2);
  251. pkt = (uchar *)s;
  252. strcpy((char *)pkt, "File has bad magic");
  253. pkt += 18 /*strlen("File has bad magic")*/ + 1;
  254. len = pkt - xp;
  255. break;
  256. }
  257. NetSendUDPPacket(NetServerEther, TftpServerIP, TftpServerPort,
  258. TftpOurPort, len);
  259. }
  260. static void
  261. TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
  262. unsigned len)
  263. {
  264. ushort proto;
  265. ushort *s;
  266. int i;
  267. if (dest != TftpOurPort) {
  268. #ifdef CONFIG_MCAST_TFTP
  269. if (Multicast
  270. && (!Mcast_port || (dest != Mcast_port)))
  271. #endif
  272. return;
  273. }
  274. if (TftpState != STATE_RRQ && src != TftpServerPort) {
  275. return;
  276. }
  277. if (len < 2) {
  278. return;
  279. }
  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. TftpServerPort = 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. }
  362. if (TftpState == STATE_RRQ)
  363. debug("Server did not acknowledge timeout option!\n");
  364. if (TftpState == STATE_RRQ || TftpState == STATE_OACK) {
  365. /* first block received */
  366. TftpState = STATE_DATA;
  367. TftpServerPort = src;
  368. TftpLastBlock = 0;
  369. TftpBlockWrap = 0;
  370. TftpBlockWrapOffset = 0;
  371. #ifdef CONFIG_MCAST_TFTP
  372. if (Multicast) { /* start!=1 common if mcast */
  373. TftpLastBlock = TftpBlock - 1;
  374. } else
  375. #endif
  376. if (TftpBlock != 1) { /* Assertion */
  377. printf("\nTFTP error: "
  378. "First block is not block 1 (%ld)\n"
  379. "Starting again\n\n",
  380. TftpBlock);
  381. NetStartAgain();
  382. break;
  383. }
  384. }
  385. if (TftpBlock == TftpLastBlock) {
  386. /*
  387. * Same block again; ignore it.
  388. */
  389. break;
  390. }
  391. TftpLastBlock = TftpBlock;
  392. TftpTimeoutCountMax = TIMEOUT_COUNT;
  393. NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
  394. store_block(TftpBlock - 1, pkt + 2, len);
  395. /*
  396. * Acknoledge the block just received, which will prompt
  397. * the server for the next one.
  398. */
  399. #ifdef CONFIG_MCAST_TFTP
  400. /* if I am the MasterClient, actively calculate what my next
  401. * needed block is; else I'm passive; not ACKING
  402. */
  403. if (Multicast) {
  404. if (len < TftpBlkSize) {
  405. TftpEndingBlock = TftpBlock;
  406. } else if (MasterClient) {
  407. TftpBlock = PrevBitmapHole =
  408. ext2_find_next_zero_bit(
  409. Bitmap,
  410. (Mapsize*8),
  411. PrevBitmapHole);
  412. if (TftpBlock > ((Mapsize*8) - 1)) {
  413. printf("tftpfile too big\n");
  414. /* try to double it and retry */
  415. Mapsize <<= 1;
  416. mcast_cleanup();
  417. NetStartAgain();
  418. return;
  419. }
  420. TftpLastBlock = TftpBlock;
  421. }
  422. }
  423. #endif
  424. TftpSend();
  425. #ifdef CONFIG_MCAST_TFTP
  426. if (Multicast) {
  427. if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
  428. puts("\nMulticast tftp done\n");
  429. mcast_cleanup();
  430. NetState = NETLOOP_SUCCESS;
  431. }
  432. }
  433. else
  434. #endif
  435. if (len < TftpBlkSize) {
  436. /*
  437. * We received the whole thing. Try to
  438. * run it.
  439. */
  440. #ifdef CONFIG_TFTP_TSIZE
  441. /* Print hash marks for the last packet received */
  442. while (TftpTsize && TftpNumchars < 49) {
  443. putc('#');
  444. TftpNumchars++;
  445. }
  446. #endif
  447. puts("\ndone\n");
  448. NetState = NETLOOP_SUCCESS;
  449. }
  450. break;
  451. case TFTP_ERROR:
  452. printf("\nTFTP error: '%s' (%d)\n",
  453. pkt + 2, ntohs(*(ushort *)pkt));
  454. switch (ntohs(*(ushort *)pkt)) {
  455. case TFTP_ERR_FILE_NOT_FOUND:
  456. case TFTP_ERR_ACCESS_DENIED:
  457. puts("Not retrying...\n");
  458. eth_halt();
  459. NetState = NETLOOP_FAIL;
  460. break;
  461. case TFTP_ERR_UNDEFINED:
  462. case TFTP_ERR_DISK_FULL:
  463. case TFTP_ERR_UNEXPECTED_OPCODE:
  464. case TFTP_ERR_UNKNOWN_TRANSFER_ID:
  465. case TFTP_ERR_FILE_ALREADY_EXISTS:
  466. default:
  467. puts("Starting again\n\n");
  468. #ifdef CONFIG_MCAST_TFTP
  469. mcast_cleanup();
  470. #endif
  471. NetStartAgain();
  472. break;
  473. }
  474. break;
  475. }
  476. }
  477. static void
  478. TftpTimeout(void)
  479. {
  480. if (++TftpTimeoutCount > TftpTimeoutCountMax) {
  481. puts("\nRetry count exceeded; starting again\n");
  482. #ifdef CONFIG_MCAST_TFTP
  483. mcast_cleanup();
  484. #endif
  485. NetStartAgain();
  486. } else {
  487. puts("T ");
  488. NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
  489. TftpSend();
  490. }
  491. }
  492. void
  493. TftpStart(void)
  494. {
  495. char *ep; /* Environment pointer */
  496. /*
  497. * Allow the user to choose TFTP blocksize and timeout.
  498. * TFTP protocol has a minimal timeout of 1 second.
  499. */
  500. if ((ep = getenv("tftpblocksize")) != NULL)
  501. TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
  502. if ((ep = getenv("tftptimeout")) != NULL)
  503. TftpTimeoutMSecs = simple_strtol(ep, NULL, 10);
  504. if (TftpTimeoutMSecs < 1000) {
  505. printf("TFTP timeout (%ld ms) too low, "
  506. "set minimum = 1000 ms\n",
  507. TftpTimeoutMSecs);
  508. TftpTimeoutMSecs = 1000;
  509. }
  510. debug("TFTP blocksize = %i, timeout = %ld ms\n",
  511. TftpBlkSizeOption, TftpTimeoutMSecs);
  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. strncpy(tftp_filename, default_filename, MAX_LEN);
  520. tftp_filename[MAX_LEN-1] = 0;
  521. printf("*** Warning: no boot file name; using '%s'\n",
  522. tftp_filename);
  523. } else {
  524. char *p = strchr(BootFile, ':');
  525. if (p == NULL) {
  526. strncpy(tftp_filename, BootFile, MAX_LEN);
  527. tftp_filename[MAX_LEN-1] = 0;
  528. } else {
  529. TftpServerIP = string_to_ip(BootFile);
  530. strncpy(tftp_filename, p + 1, MAX_LEN);
  531. tftp_filename[MAX_LEN-1] = 0;
  532. }
  533. }
  534. #if defined(CONFIG_NET_MULTI)
  535. printf("Using %s device\n", eth_get_name());
  536. #endif
  537. printf("TFTP from server %pI4"
  538. "; our IP address is %pI4", &TftpServerIP, &NetOurIP);
  539. /* Check if we need to send across this subnet */
  540. if (NetOurGatewayIP && NetOurSubnetMask) {
  541. IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
  542. IPaddr_t ServerNet = TftpServerIP & NetOurSubnetMask;
  543. if (OurNet != ServerNet)
  544. printf("; sending through gateway %pI4", &NetOurGatewayIP);
  545. }
  546. putc('\n');
  547. printf("Filename '%s'.", tftp_filename);
  548. if (NetBootFileSize) {
  549. printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
  550. print_size(NetBootFileSize<<9, "");
  551. }
  552. putc('\n');
  553. printf("Load address: 0x%lx\n", load_addr);
  554. puts("Loading: *\b");
  555. TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
  556. NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
  557. NetSetHandler(TftpHandler);
  558. TftpServerPort = WELL_KNOWN_PORT;
  559. TftpTimeoutCount = 0;
  560. TftpState = STATE_RRQ;
  561. /* Use a pseudo-random port unless a specific port is set */
  562. TftpOurPort = 1024 + (get_timer(0) % 3072);
  563. #ifdef CONFIG_TFTP_PORT
  564. if ((ep = getenv("tftpdstp")) != NULL) {
  565. TftpServerPort = simple_strtol(ep, NULL, 10);
  566. }
  567. if ((ep = getenv("tftpsrcp")) != NULL) {
  568. TftpOurPort = simple_strtol(ep, NULL, 10);
  569. }
  570. #endif
  571. TftpBlock = 0;
  572. /* zero out server ether in case the server ip has changed */
  573. memset(NetServerEther, 0, 6);
  574. /* Revert TftpBlkSize to dflt */
  575. TftpBlkSize = TFTP_BLOCK_SIZE;
  576. #ifdef CONFIG_MCAST_TFTP
  577. mcast_cleanup();
  578. #endif
  579. #ifdef CONFIG_TFTP_TSIZE
  580. TftpTsize = 0;
  581. TftpNumchars = 0;
  582. #endif
  583. TftpSend();
  584. }
  585. #ifdef CONFIG_MCAST_TFTP
  586. /* Credits: atftp project.
  587. */
  588. /* pick up BcastAddr, Port, and whether I am [now] the master-client. *
  589. * Frame:
  590. * +-------+-----------+---+-------~~-------+---+
  591. * | opc | multicast | 0 | addr, port, mc | 0 |
  592. * +-------+-----------+---+-------~~-------+---+
  593. * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
  594. * I am the new master-client so must send ACKs to DataBlocks. If I am not
  595. * master-client, I'm a passive client, gathering what DataBlocks I may and
  596. * making note of which ones I got in my bitmask.
  597. * In theory, I never go from master->passive..
  598. * .. this comes in with pkt already pointing just past opc
  599. */
  600. static void parse_multicast_oack(char *pkt, int len)
  601. {
  602. int i;
  603. IPaddr_t addr;
  604. char *mc_adr, *port, *mc;
  605. mc_adr = port = mc = NULL;
  606. /* march along looking for 'multicast\0', which has to start at least
  607. * 14 bytes back from the end.
  608. */
  609. for (i = 0; i < len-14; i++)
  610. if (strcmp(pkt+i, "multicast") == 0)
  611. break;
  612. if (i >= (len-14)) /* non-Multicast OACK, ign. */
  613. return;
  614. i += 10; /* strlen multicast */
  615. mc_adr = pkt+i;
  616. for (; i < len; i++) {
  617. if (*(pkt+i) == ',') {
  618. *(pkt+i) = '\0';
  619. if (port) {
  620. mc = pkt+i+1;
  621. break;
  622. } else {
  623. port = pkt+i+1;
  624. }
  625. }
  626. }
  627. if (!port || !mc_adr || !mc) return;
  628. if (Multicast && MasterClient) {
  629. printf("I got a OACK as master Client, WRONG!\n");
  630. return;
  631. }
  632. /* ..I now accept packets destined for this MCAST addr, port */
  633. if (!Multicast) {
  634. if (Bitmap) {
  635. printf("Internal failure! no mcast.\n");
  636. free(Bitmap);
  637. Bitmap = NULL;
  638. ProhibitMcast = 1;
  639. return ;
  640. }
  641. /* I malloc instead of pre-declare; so that if the file ends
  642. * up being too big for this bitmap I can retry
  643. */
  644. if (!(Bitmap = malloc(Mapsize))) {
  645. printf("No Bitmap, no multicast. Sorry.\n");
  646. ProhibitMcast = 1;
  647. return;
  648. }
  649. memset(Bitmap, 0, Mapsize);
  650. PrevBitmapHole = 0;
  651. Multicast = 1;
  652. }
  653. addr = string_to_ip(mc_adr);
  654. if (Mcast_addr != addr) {
  655. if (Mcast_addr)
  656. eth_mcast_join(Mcast_addr, 0);
  657. if (eth_mcast_join(Mcast_addr = addr, 1)) {
  658. printf("Fail to set mcast, revert to TFTP\n");
  659. ProhibitMcast = 1;
  660. mcast_cleanup();
  661. NetStartAgain();
  662. }
  663. }
  664. MasterClient = (unsigned char)simple_strtoul((char *)mc, NULL, 10);
  665. Mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
  666. printf("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
  667. return;
  668. }
  669. #endif /* Multicast TFTP */