tftp.c 18 KB

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