1. 程式人生 > >關於uboot中tftp上傳記憶體資料到tftp伺服器

關於uboot中tftp上傳記憶體資料到tftp伺服器

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

相關推薦

關於uboottftp記憶體資料到tftp伺服器

/* * Copyright 1994, 1995, 2000 Neil Russell. * (See License) * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, [email protected] */ #i

開發亞馬遜 MWSfeed修改商品資訊 通過GetFeedSubmissionResult來判斷資料是否成功

GetFeedSubmissionResultSample.php 中的方法如下 $config = array ( 'ServiceURL' => $serviceUrl, 'ProxyHost' => null, 'ProxyPort' => -1, 'MaxErrorRetry' =

[Python] socket實現TFTP和下載

一、說明   本文主要基於socket實現TFTP檔案上傳與下載。   測試環境:Win10/Python3.5/tftpd64。   tftpd下載:根據自己的環境選擇下載,地址 :http://tftpd32.jounin.net/tftpd32_download.html   主要內容:TFTP

Linux下tftp檔案失敗的幾條原因

1.  Error code 0: Permission denied 主目錄沒有寫許可權, 可以 chmod  0777  /var/lib/tftpboot 2.Error code 1: File not found 在/etc/xinetd.d/tftp配置檔案中

Spring MVC 檔案/刪除 刪除空資料夾方法 + 下載方法

package com.pqs.common.tools; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOExce

開發板tftp 和下載用法

在開發板上輸入命令 #tftp BusyBox v1.16.1 (2014-08-18 21:57:48 CST) multi-call binary. Usage: tftp [OPTIONS] HOST [PORT] Transfer a file from/to t

【springmvc】springmvc如何文件

doc 問題 ffffff color 遍歷 -c tip int imu 使用到的組件:可自行根據情況選擇版本 <!--上傳依賴包--> <dependency> <groupId

在Windows 2008 R2 Server視頻遇到的問題

mage uri 視頻 打開 技術 stl con 4.5 點擊 在Windows 2008 R2 Server中,上傳視頻不能播放,以及服務器大小限制問題,這裏記錄我的解決方法,以免再次遇到,無所適從。 1、上傳視頻不能播放 打開IIS,找到“MIME類型”,如下:

在Windows Server 2008 R2 Server視頻遇到的問題(二)

content 修改 strong 報錯 con 節點 fail get tle 上一篇 在Windows Server 2008 R2 Server中,上傳視頻遇到的問題(一)中遇到上傳40M視頻報404,然後修改配置文件節點: <httpRuntime tar

如何解決markdown圖片的問題

github上 雲筆記 github ogl markdown weibo hub gif 解決 1.第一種方式(圖床) 1.1 google中的插件-新浪微博圖床 2.第二種方式,操作流程如下 2.1 下載一個有道雲筆記客戶端 2.2 然後把圖片通過有道雲筆記分享出來

小程式圖片

前端小程式:wxml <!--huitianxia/view/attend/attend/attend.wxml--> <import src="/huitianxia/view/common/foot.wxml"/> <view class='wrappe

linux檔案下載

windows篇 linux檔案下載到windows   sz命令   登入到linux伺服器使用 sz log.log 命令,彈出對話方塊選擇下載檔案的目錄,點選確定即可。   windows檔案上傳到linux   rz命令   登入到linux伺服器使用rz命令,彈出檔

Git資料

git上傳的資料夾為空的時候 1,先刪除空的資料夾 參考:https://www.cnblogs.com/wang715100018066/p/9694532.html 2,這個只能說是技巧不能說是方法,原理是在每個空資料夾新建一個.gitignore檔案,然後提交。 $ find . -type d

Springmvcajax檔案出現400錯誤

Springmvc中ajax上傳檔案出現400錯誤 一、配置spring-mvc.xml 需要在spring-mvc.xml中配置檔案上傳解析器 <!-- 定義檔案上傳解析器 --> <bean id="multipartResolver" cl

Android 本地tomcat伺服器接收處理手機資料之案例演示

上一篇:Android 本地tomcat伺服器接收處理手機上傳的資料之環境搭建     本篇基於上一篇搭建的伺服器端環境,具體介紹Android真機上傳資料到tomcat伺服器的互動過程   場景:Android客戶端上傳使用者名稱和密

Spring MVC檔案和下載

檔案上傳 檔案上傳需將表格的提交方式設為"POST",並且將enctype設為"multipart/form-data",以二進位制的方式提交資料。 spring mvc中可通過MultipartResolver監聽每個請求,如有上傳的檔案,則把請求封裝為MultipartH

圖解在Eclipse如何專案到svn

方法/步驟 1.在Eclipse中新建project,如下圖所示:  2.右鍵project --> team --> share project,如下圖所示:  3.選擇repository為svn-----點選n

Jqueryuploadify檔案

var uploadLimit = 1; $(document).ready(function () { $("#uploadify").uploadify({ 'swf': '../Scripts/upload

SpringMVC和下載

一、概述 檔案的上傳和下載,一直以來都是開發中必不可少的功能。在沒有SpringMVC之前對於檔案的上傳和下載的操作,一般都是通過Apache 開源組織提供了一個用來處理表單檔案上傳的一個開源元件( Commons-fileupload )來實現的。現在SpringMVC提供了檔案的

微信小程式實現視訊的開發程式碼

index.wxml <view class="image-plus image-plus-nb" bindtap="chooseVideo"> <view class="image-plus-horizontal"></view> &l