tftp.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. if (len < 2)
  277. return;
  278. len -= 2;
  279. /* warning: don't use increment (++) in ntohs() macros!! */
  280. s = (ushort *)pkt;
  281. proto = *s++;
  282. pkt = (uchar *)s;
  283. switch (ntohs(proto)) {
  284. case TFTP_RRQ:
  285. case TFTP_WRQ:
  286. case TFTP_ACK:
  287. break;
  288. default:
  289. break;
  290. case TFTP_OACK:
  291. debug("Got OACK: %s %s\n",
  292. pkt,
  293. pkt + strlen((char *)pkt) + 1);
  294. TftpState = STATE_OACK;
  295. TftpServerPort = src;
  296. /*
  297. * Check for 'blksize' option.
  298. * Careful: "i" is signed, "len" is unsigned, thus
  299. * something like "len-8" may give a *huge* number
  300. */
  301. for (i = 0; i+8 < len; i++) {
  302. if (strcmp((char *)pkt+i, "blksize") == 0) {
  303. TftpBlkSize = (unsigned short)
  304. simple_strtoul((char *)pkt+i+8, NULL,
  305. 10);
  306. debug("Blocksize ack: %s, %d\n",
  307. (char *)pkt+i+8, TftpBlkSize);
  308. }
  309. #ifdef CONFIG_TFTP_TSIZE
  310. if (strcmp((char *)pkt+i, "tsize") == 0) {
  311. TftpTsize = simple_strtoul((char *)pkt+i+6,
  312. NULL, 10);
  313. debug("size = %s, %d\n",
  314. (char *)pkt+i+6, TftpTsize);
  315. }
  316. #endif
  317. }
  318. #ifdef CONFIG_MCAST_TFTP
  319. parse_multicast_oack((char *)pkt, len-1);
  320. if ((Multicast) && (!MasterClient))
  321. TftpState = STATE_DATA; /* passive.. */
  322. else
  323. #endif
  324. TftpSend(); /* Send ACK */
  325. break;
  326. case TFTP_DATA:
  327. if (len < 2)
  328. return;
  329. len -= 2;
  330. TftpBlock = ntohs(*(ushort *)pkt);
  331. /*
  332. * RFC1350 specifies that the first data packet will
  333. * have sequence number 1. If we receive a sequence
  334. * number of 0 this means that there was a wrap
  335. * around of the (16 bit) counter.
  336. */
  337. if (TftpBlock == 0) {
  338. TftpBlockWrap++;
  339. TftpBlockWrapOffset +=
  340. TftpBlkSize * TFTP_SEQUENCE_SIZE;
  341. printf("\n\t %lu MB received\n\t ",
  342. TftpBlockWrapOffset>>20);
  343. }
  344. #ifdef CONFIG_TFTP_TSIZE
  345. else if (TftpTsize) {
  346. while (TftpNumchars <
  347. NetBootFileXferSize * 50 / TftpTsize) {
  348. putc('#');
  349. TftpNumchars++;
  350. }
  351. }
  352. #endif
  353. else {
  354. if (((TftpBlock - 1) % 10) == 0)
  355. putc('#');
  356. else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0)
  357. puts("\n\t ");
  358. }
  359. if (TftpState == STATE_RRQ)
  360. debug("Server did not acknowledge timeout option!\n");
  361. if (TftpState == STATE_RRQ || TftpState == STATE_OACK) {
  362. /* first block received */
  363. TftpState = STATE_DATA;
  364. TftpServerPort = src;
  365. TftpLastBlock = 0;
  366. TftpBlockWrap = 0;
  367. TftpBlockWrapOffset = 0;
  368. #ifdef CONFIG_MCAST_TFTP
  369. if (Multicast) { /* start!=1 common if mcast */
  370. TftpLastBlock = TftpBlock - 1;
  371. } else
  372. #endif
  373. if (TftpBlock != 1) { /* Assertion */
  374. printf("\nTFTP error: "
  375. "First block is not block 1 (%ld)\n"
  376. "Starting again\n\n",
  377. TftpBlock);
  378. NetStartAgain();
  379. break;
  380. }
  381. }
  382. if (TftpBlock == TftpLastBlock) {
  383. /*
  384. * Same block again; ignore it.
  385. */
  386. break;
  387. }
  388. TftpLastBlock = TftpBlock;
  389. TftpTimeoutCountMax = TIMEOUT_COUNT;
  390. NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
  391. store_block(TftpBlock - 1, pkt + 2, len);
  392. /*
  393. * Acknoledge the block just received, which will prompt
  394. * the server for the next one.
  395. */
  396. #ifdef CONFIG_MCAST_TFTP
  397. /* if I am the MasterClient, actively calculate what my next
  398. * needed block is; else I'm passive; not ACKING
  399. */
  400. if (Multicast) {
  401. if (len < TftpBlkSize) {
  402. TftpEndingBlock = TftpBlock;
  403. } else if (MasterClient) {
  404. TftpBlock = PrevBitmapHole =
  405. ext2_find_next_zero_bit(
  406. Bitmap,
  407. (Mapsize*8),
  408. PrevBitmapHole);
  409. if (TftpBlock > ((Mapsize*8) - 1)) {
  410. printf("tftpfile too big\n");
  411. /* try to double it and retry */
  412. Mapsize <<= 1;
  413. mcast_cleanup();
  414. NetStartAgain();
  415. return;
  416. }
  417. TftpLastBlock = TftpBlock;
  418. }
  419. }
  420. #endif
  421. TftpSend();
  422. #ifdef CONFIG_MCAST_TFTP
  423. if (Multicast) {
  424. if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
  425. puts("\nMulticast tftp done\n");
  426. mcast_cleanup();
  427. NetState = NETLOOP_SUCCESS;
  428. }
  429. }
  430. else
  431. #endif
  432. if (len < TftpBlkSize) {
  433. /*
  434. * We received the whole thing. Try to
  435. * run it.
  436. */
  437. #ifdef CONFIG_TFTP_TSIZE
  438. /* Print hash marks for the last packet received */
  439. while (TftpTsize && TftpNumchars < 49) {
  440. putc('#');
  441. TftpNumchars++;
  442. }
  443. #endif
  444. puts("\ndone\n");
  445. NetState = NETLOOP_SUCCESS;
  446. }
  447. break;
  448. case TFTP_ERROR:
  449. printf("\nTFTP error: '%s' (%d)\n",
  450. pkt + 2, ntohs(*(ushort *)pkt));
  451. switch (ntohs(*(ushort *)pkt)) {
  452. case TFTP_ERR_FILE_NOT_FOUND:
  453. case TFTP_ERR_ACCESS_DENIED:
  454. puts("Not retrying...\n");
  455. eth_halt();
  456. NetState = NETLOOP_FAIL;
  457. break;
  458. case TFTP_ERR_UNDEFINED:
  459. case TFTP_ERR_DISK_FULL:
  460. case TFTP_ERR_UNEXPECTED_OPCODE:
  461. case TFTP_ERR_UNKNOWN_TRANSFER_ID:
  462. case TFTP_ERR_FILE_ALREADY_EXISTS:
  463. default:
  464. puts("Starting again\n\n");
  465. #ifdef CONFIG_MCAST_TFTP
  466. mcast_cleanup();
  467. #endif
  468. NetStartAgain();
  469. break;
  470. }
  471. break;
  472. }
  473. }
  474. static void
  475. TftpTimeout(void)
  476. {
  477. if (++TftpTimeoutCount > TftpTimeoutCountMax) {
  478. puts("\nRetry count exceeded; starting again\n");
  479. #ifdef CONFIG_MCAST_TFTP
  480. mcast_cleanup();
  481. #endif
  482. NetStartAgain();
  483. } else {
  484. puts("T ");
  485. NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
  486. TftpSend();
  487. }
  488. }
  489. void
  490. TftpStart(void)
  491. {
  492. char *ep; /* Environment pointer */
  493. /*
  494. * Allow the user to choose TFTP blocksize and timeout.
  495. * TFTP protocol has a minimal timeout of 1 second.
  496. */
  497. ep = getenv("tftpblocksize");
  498. if (ep != NULL)
  499. TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
  500. ep = getenv("tftptimeout");
  501. if (ep != NULL)
  502. TftpTimeoutMSecs = simple_strtol(ep, NULL, 10);
  503. if (TftpTimeoutMSecs < 1000) {
  504. printf("TFTP timeout (%ld ms) too low, "
  505. "set minimum = 1000 ms\n",
  506. TftpTimeoutMSecs);
  507. TftpTimeoutMSecs = 1000;
  508. }
  509. debug("TFTP blocksize = %i, timeout = %ld ms\n",
  510. TftpBlkSizeOption, TftpTimeoutMSecs);
  511. TftpServerIP = NetServerIP;
  512. if (BootFile[0] == '\0') {
  513. sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
  514. NetOurIP & 0xFF,
  515. (NetOurIP >> 8) & 0xFF,
  516. (NetOurIP >> 16) & 0xFF,
  517. (NetOurIP >> 24) & 0xFF);
  518. strncpy(tftp_filename, default_filename, MAX_LEN);
  519. tftp_filename[MAX_LEN-1] = 0;
  520. printf("*** Warning: no boot file name; using '%s'\n",
  521. tftp_filename);
  522. } else {
  523. char *p = strchr(BootFile, ':');
  524. if (p == NULL) {
  525. strncpy(tftp_filename, BootFile, MAX_LEN);
  526. tftp_filename[MAX_LEN-1] = 0;
  527. } else {
  528. TftpServerIP = string_to_ip(BootFile);
  529. strncpy(tftp_filename, p + 1, MAX_LEN);
  530. tftp_filename[MAX_LEN-1] = 0;
  531. }
  532. }
  533. #if defined(CONFIG_NET_MULTI)
  534. printf("Using %s device\n", eth_get_name());
  535. #endif
  536. printf("TFTP from server %pI4"
  537. "; our IP address is %pI4", &TftpServerIP, &NetOurIP);
  538. /* Check if we need to send across this subnet */
  539. if (NetOurGatewayIP && NetOurSubnetMask) {
  540. IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
  541. IPaddr_t ServerNet = TftpServerIP & NetOurSubnetMask;
  542. if (OurNet != ServerNet)
  543. printf("; sending through gateway %pI4", &NetOurGatewayIP);
  544. }
  545. putc('\n');
  546. printf("Filename '%s'.", tftp_filename);
  547. if (NetBootFileSize) {
  548. printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
  549. print_size(NetBootFileSize<<9, "");
  550. }
  551. putc('\n');
  552. printf("Load address: 0x%lx\n", load_addr);
  553. puts("Loading: *\b");
  554. TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
  555. NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
  556. NetSetHandler(TftpHandler);
  557. TftpServerPort = WELL_KNOWN_PORT;
  558. TftpTimeoutCount = 0;
  559. TftpState = STATE_RRQ;
  560. /* Use a pseudo-random port unless a specific port is set */
  561. TftpOurPort = 1024 + (get_timer(0) % 3072);
  562. #ifdef CONFIG_TFTP_PORT
  563. ep = getenv("tftpdstp");
  564. if (ep != NULL)
  565. TftpServerPort = simple_strtol(ep, NULL, 10);
  566. ep = getenv("tftpsrcp");
  567. if (ep != NULL)
  568. TftpOurPort = simple_strtol(ep, NULL, 10);
  569. #endif
  570. TftpBlock = 0;
  571. /* zero out server ether in case the server ip has changed */
  572. memset(NetServerEther, 0, 6);
  573. /* Revert TftpBlkSize to dflt */
  574. TftpBlkSize = TFTP_BLOCK_SIZE;
  575. #ifdef CONFIG_MCAST_TFTP
  576. mcast_cleanup();
  577. #endif
  578. #ifdef CONFIG_TFTP_TSIZE
  579. TftpTsize = 0;
  580. TftpNumchars = 0;
  581. #endif
  582. TftpSend();
  583. }
  584. #ifdef CONFIG_MCAST_TFTP
  585. /* Credits: atftp project.
  586. */
  587. /* pick up BcastAddr, Port, and whether I am [now] the master-client. *
  588. * Frame:
  589. * +-------+-----------+---+-------~~-------+---+
  590. * | opc | multicast | 0 | addr, port, mc | 0 |
  591. * +-------+-----------+---+-------~~-------+---+
  592. * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
  593. * I am the new master-client so must send ACKs to DataBlocks. If I am not
  594. * master-client, I'm a passive client, gathering what DataBlocks I may and
  595. * making note of which ones I got in my bitmask.
  596. * In theory, I never go from master->passive..
  597. * .. this comes in with pkt already pointing just past opc
  598. */
  599. static void parse_multicast_oack(char *pkt, int len)
  600. {
  601. int i;
  602. IPaddr_t addr;
  603. char *mc_adr, *port, *mc;
  604. mc_adr = port = mc = NULL;
  605. /* march along looking for 'multicast\0', which has to start at least
  606. * 14 bytes back from the end.
  607. */
  608. for (i = 0; i < len-14; i++)
  609. if (strcmp(pkt+i, "multicast") == 0)
  610. break;
  611. if (i >= (len-14)) /* non-Multicast OACK, ign. */
  612. return;
  613. i += 10; /* strlen multicast */
  614. mc_adr = pkt+i;
  615. for (; i < len; i++) {
  616. if (*(pkt+i) == ',') {
  617. *(pkt+i) = '\0';
  618. if (port) {
  619. mc = pkt+i+1;
  620. break;
  621. } else {
  622. port = pkt+i+1;
  623. }
  624. }
  625. }
  626. if (!port || !mc_adr || !mc) return;
  627. if (Multicast && MasterClient) {
  628. printf("I got a OACK as master Client, WRONG!\n");
  629. return;
  630. }
  631. /* ..I now accept packets destined for this MCAST addr, port */
  632. if (!Multicast) {
  633. if (Bitmap) {
  634. printf("Internal failure! no mcast.\n");
  635. free(Bitmap);
  636. Bitmap = NULL;
  637. ProhibitMcast = 1;
  638. return ;
  639. }
  640. /* I malloc instead of pre-declare; so that if the file ends
  641. * up being too big for this bitmap I can retry
  642. */
  643. Bitmap = malloc(Mapsize);
  644. if (!Bitmap) {
  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. Mcast_addr = addr;
  658. if (eth_mcast_join(Mcast_addr, 1)) {
  659. printf("Fail to set mcast, revert to TFTP\n");
  660. ProhibitMcast = 1;
  661. mcast_cleanup();
  662. NetStartAgain();
  663. }
  664. }
  665. MasterClient = (unsigned char)simple_strtoul((char *)mc, NULL, 10);
  666. Mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
  667. printf("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
  668. return;
  669. }
  670. #endif /* Multicast TFTP */