tftp.c 18 KB

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