tftp.c 18 KB

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