转载:http://blog.chinaunix.net/uid-20737871-id-2124122.html

uboot下的tftp下载功能是非常重要和常见的功能。但是偶尔有些特殊需求的人需要使用uboot的tftp具有上传功能。
默认的uboot没有tftp上传功能,如果需要修改uboot代码。
使用时键入第4个参数,则不同于3个参数的tftp下载功能。
#tftp 50400000 xx.bin 10000
TFTP to server 192.168.0.30; our IP address is 192.168.0.152
Upload Filename 'xx.bin'.
Upload from address: 0x50400000, 0.064 MB to be send ...
Uploading: %#   [ Connected ]

0.064 MB upload ok.
这条命令将板子上0x50400000 开始,长度0x10000的数据上传到远程tftp服务器,命名为xx.bin

这个修改在uboot1.3.4和2008.10版本上测试通过。
1、修改common/cmd_net.c
注释掉

  1. /*
  2. int do_tftpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  3. {
  4. return netboot_common (TFTP, cmdtp, argc, argv);
  5. }
  6. U_BOOT_CMD(
  7. tftpboot, 3, 1, do_tftpb,
  8. "tftpboot- boot image via network using TFTP protocol\n",
  9. "[loadAddress] [[hostIPaddr:]bootfilename]\n"
  10. );
  11. */

可以看出默认uboot执行tftp命令其实调用的是tftpboot,uboot果然是看命名的前面几个字母而不是全名。例如print命令只需要键入pri。
接着添加

  1. int do_tftp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  2. {
  3. return netboot_common (TFTP, cmdtp, argc, argv);
  4. }
  5. U_BOOT_CMD(
  6. tftp, , , do_tftp,
  7. "tftp\t- download or upload image via network using TFTP protocol\n",
  8. "[loadAddress] [bootfilename] <upload_size>\n"
  9. );

然后修改netboot_common成如下代码

  1. static int
  2. netboot_common (proto_t proto, cmd_tbl_t *cmdtp, int argc, char *argv[])
  3. {
  4. extern ulong upload_addr;
  5. extern ulong upload_size;
  6. char *s;
  7. int rcode = ;
  8. int size;
  9. /* pre-set load_addr */
  10. if ((s = getenv("loadaddr")) != NULL) {
  11. load_addr = simple_strtoul(s, NULL, );
  12. }
  13. switch (argc) {
  14. case :
  15. break;
  16. case : /* only one arg - accept two forms:
  17. * just load address, or just boot file name.
  18. * The latter form must be written "filename" here.
  19. */
  20. if (argv[][] == '"') { /* just boot filename */
  21. copy_filename (BootFile, argv[], sizeof(BootFile));
  22. } else { /* load address */
  23. load_addr = simple_strtoul(argv[], NULL, );
  24. }
  25. break;
  26. case : load_addr = simple_strtoul(argv[], NULL, );
  27. copy_filename (BootFile, argv[], sizeof(BootFile));
  28. upload_size = ;
  29.  
  30. break;
  31.  
  32. case :
  33. upload_addr = simple_strtoul(argv[], NULL, );
  34. upload_size = simple_strtoul(argv[], NULL, );
  35. copy_filename (BootFile, argv[], sizeof(BootFile));
  36. break;
  37. default: printf ("Usage:\n%s\n", cmdtp->usage);
  38. show_boot_progress (-);
  39. return ;
  40. }
  41. show_boot_progress ();
  42. if ((size = NetLoop(proto)) < ) {
  43. show_boot_progress (-);
  44. return ;
  45. }
  46. show_boot_progress ();
  47. /* NetLoop ok, update environment */
  48. netboot_update_env();
  49. /* done if no file was loaded (no errors though) */
  50. if (size == ) {
  51. show_boot_progress (-);
  52. return ;
  53. }
  54. /* flush cache */
  55. flush_cache(load_addr, size);
  56. /* Loading ok, check if we should attempt an auto-start */
  57. if (((s = getenv("autostart")) != NULL) && (strcmp(s,"yes") == )) {
  58. char *local_args[];
  59. local_args[] = argv[];
  60. local_args[] = NULL;
  61. printf ("Automatic boot of image at addr 0x%08lX ...\n",
  62. load_addr);
  63. show_boot_progress ();
  64. rcode = do_bootm (cmdtp, , , local_args);
  65. }
  66. #ifdef CONFIG_AUTOSCRIPT
  67. if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == )) {
  68. printf ("Running autoscript at addr 0x%08lX", load_addr);
  69. s = getenv ("autoscript_uname");
  70. if (s)
  71. printf (":%s ...\n", s);
  72. else
  73. puts (" ...\n");
  74. show_boot_progress ();
  75. rcode = autoscript (load_addr, s);
  76. }
  77. #endif
  78. if (rcode < )
  79. show_boot_progress (-);
  80. else
  81. show_boot_progress ();
  82. return rcode;
  83. }

2、修改net/tftp.c 为

  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. #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 /* # of timeouts before giving up */
  17. #else
  18. # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * )
  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 = ;
  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=, MasterClient=;
  78. static uchar Multicast=;
  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, );
  87. if (Bitmap) free(Bitmap);
  88. Bitmap=NULL;
  89. Mcast_addr = Multicast = Mcast_port = ;
  90. TftpEndingBlock = -;
  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 = ;
  100. for (i=; 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[]) && (load_addr + offset < flash_info[i].start[] + flash_info[i].size)) {
  105. rc = ;
  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 = ;
  138. int uplen=;
  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 == ))
  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) + ;
  163. strcpy ((char *)pkt, "octet");
  164. pkt += /*strlen("octet")*/ + ;
  165. strcpy ((char *)pkt, "timeout");
  166. pkt += /*strlen("timeout")*/ + ;
  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) + ;
  172. /* try for more effic. blk size */
  173. if(TftpState == STATE_WRQ)
  174. pkt += sprintf((char *)pkt,"blksize%c%d%c",
  175. ,TftpBlkSizeOption,);
  176. else
  177. pkt += sprintf((char *)pkt,"blksize%c%d%c",
  178. ,TftpBlkSizeOption,);
  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",,);
  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*),);
  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();
  212. pkt = (uchar *)s;
  213. strcpy ((char *)pkt, "File too large");
  214. pkt += /*strlen("File too large")*/ + ;
  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();
  222. pkt = (uchar *)s;
  223. strcpy ((char *)pkt, "File has bad magic");
  224. pkt += /*strlen("File has bad magic")*/ + ;
  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+);
  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-) * TftpBlkSize + wrap)&0x0FFFFF)>>, tail)
  248. if( ((block-) & (TBLKS_PER_HASHES-)) == )
  249. putc('#');
  250. if( ((block-) & (TBLKS_PER_HASHES*HASHES_PER_LINE-)) == ) {
  251. if((block-) ==) {
  252. if(wrap==) {
  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 < ) {
  280. return;
  281. }
  282. len -= ;
  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)>>,
  307. ((TftpBlockWrapOffset+upload_size)&0x0FFFFF)>>);
  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)+);
  317. #endif
  318. if(TftpState == STATE_WRQ)
  319. {
  320. TftpState = STATE_ACK;
  321. TftpBlock = ;
  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=; i+<len; i++) {
  334. if (strcmp ((char*)pkt+i,"blksize") == ) {
  335. TftpBlkSize = (unsigned short)
  336. simple_strtoul((char*)pkt+i+,NULL,);
  337. #ifdef ET_DEBUG
  338. printf ("Blocksize ack: %s, %d\n",
  339. (char*)pkt+i+,TftpBlkSize);
  340. #endif
  341. break;
  342. }
  343. }
  344. #ifdef CONFIG_MCAST_TFTP
  345. parse_multicast_oack((char *)pkt,len-);
  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 < )
  354. return;
  355. len -= ;
  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 == ) {
  364. TftpBlockWrap++;
  365. TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE;
  366. printf ("\n\t %lu MB received\n\t ", TftpBlockWrapOffset>>);
  367. } else {
  368. #if 0
  369. if (((TftpBlock - ) % ) == ) {
  370. putc ('#');
  371. } else if ((TftpBlock % ( * HASHES_PER_LINE)) == ) {
  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 = ;
  387. TftpBlockWrap = ;
  388. TftpBlockWrapOffset = ;
  389. #ifdef CONFIG_MCAST_TFTP
  390. if (Multicast) { /* start!=1 common if mcast */
  391. TftpLastBlock = TftpBlock - ;
  392. } else
  393. #endif
  394. if (TftpBlock != ) { /* 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 - , pkt + , 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*),
  429. PrevBitmapHole);
  430. if (TftpBlock > ((Mapsize*) - )) {
  431. printf ("tftpfile too big\n");
  432. /* try to double it and retry */
  433. Mapsize<<=;
  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-)* TftpBlkSize + TftpBlockWrapOffset)>>,
  459. (((TftpBlock-) * TftpBlkSize + TftpBlockWrapOffset)&0x0FFFFF)>>);
  460. puts ("\ndone\n");
  461. NetState = NETLOOP_SUCCESS;
  462. }
  463. break;
  464. case TFTP_ERROR:
  465. printf ("\nTFTP error: '%s' (%d)\n",
  466. pkt + , 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') {
  501. sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
  502. NetOurIP & 0xFF,
  503. (NetOurIP >> ) & 0xFF,
  504. (NetOurIP >> ) & 0xFF,
  505. (NetOurIP >> ) & 0xFF );
  506. strncpy(tftp_filename, default_filename, MAX_LEN);
  507. tftp_filename[MAX_LEN-] = ;
  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-] = ;
  515. } else {
  516. *p++ = '\0';
  517. TftpServerIP = string_to_ip (BootFile);
  518. strncpy(tftp_filename, p, MAX_LEN);
  519. tftp_filename[MAX_LEN-] = ;
  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<<);
  549. print_size (NetBootFileSize<<, "");
  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>>, (upload_size&0x0FFFFF)>>);
  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 = ;
  567. /* Use a pseudo-random port unless a specific port is set */
  568. TftpOurPort = + (get_timer() % );
  569. #ifdef CONFIG_TFTP_PORT
  570. if ((ep = getenv("tftpdstp")) != NULL) {
  571. TftpServerPort = simple_strtol(ep, NULL, );
  572. }
  573. if ((ep = getenv("tftpsrcp")) != NULL) {
  574. TftpOurPort= simple_strtol(ep, NULL, );
  575. }
  576. #endif
  577. TftpBlock = ;
  578. TftpLastBlock = ;
  579. TftpBlockWrap = ;
  580. TftpBlockWrapOffset = ;
  581. /* zero out server ether in case the server ip has changed */
  582. memset(NetServerEther, , );
  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=;i<len-;i++)
  615. if (strcmp (pkt+i,"multicast") == )
  616. break;
  617. if (i >= (len-)) /* non-Multicast OACK, ign. */
  618. return;
  619. i+=; /* 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+;
  626. break;
  627. } else {
  628. port = pkt+i+;
  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=;
  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=;
  652. return;
  653. }
  654. memset (Bitmap,,Mapsize);
  655. PrevBitmapHole = ;
  656. Multicast = ;
  657. }
  658. addr = string_to_ip(mc_adr);
  659. if (Mcast_addr != addr) {
  660. if (Mcast_addr)
  661. eth_mcast_join(Mcast_addr, );
  662. if (eth_mcast_join(Mcast_addr=addr, )) {
  663. printf ("Fail to set mcast, revert to TFTP\n");
  664. ProhibitMcast=;
  665. mcast_cleanup();
  666. NetStartAgain();
  667. }
  668. }
  669. MasterClient = (unsigned char)simple_strtoul((char *)mc,NULL,);
  670. Mcast_port = (unsigned short)simple_strtoul(port,NULL,);
  671. printf ("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
  672. return;
  673. }
  674. #endif /* Multicast TFTP */
  675. #endif

让uboot的tftp支持上传功能的更多相关文章

  1. MVC5:使用Ajax和HTML5实现文件上传功能

    引言 在实际编程中,经常遇到实现文件上传并显示上传进度的功能,基于此目的,本文就为大家介绍不使用flash 或任何上传文件的插件来实现带有进度显示的文件上传功能. 基本功能:实现带有进度条的文件上传功 ...

  2. OneThink实现多图片批量上传功能

    OneThink原生系统中的图片上传功能是uploadify.swf插件进行上传的,默认是只能上传一张图片的,但是uploadify.swf是支持多图片批量上传的,那么我们稍加改动就可实现OneThi ...

  3. thinkphp达到UploadFile.class.php图片上传功能

    片上传在站点里是非经常常使用的功能.ThinkPHP里也有自带的图片上传类(UploadFile.class.php) 和图片模型类(Image.class.php).方便于我们去实现图片上传功能,以 ...

  4. qt实现头像上传功能

    想必大家都使用过qt的自定义头像功能吧,那么图1应该不会陌生,本片文章我就是要模拟一个这样的功能,虽然没有这么强大的效果,但是能够满足一定的需求. 图1 qq上传图片 首先在讲解功能之前,我先给出一片 ...

  5. PHP语言学习之php做图片上传功能

    本文主要向大家介绍了PHP语言学习之php做图片上传功能,通过具体的内容向大家展示,希望对大家学习php语言有所帮助. 今天来做一个图片上传功能的插件,首先做一个html文件:text.php < ...

  6. JavaWeb:servlet实现下载与上传功能

    本文内容: servlet实现下载功能 servlet实现上传功能 首发日期:2018-07-21 servlet实现下载功能 实现流程 1.首先制作一个jsp页面,主要是用来触发下载的.这里可以根据 ...

  7. JavaScript实现单张图片上传功能

    前台jsp代码 <%@ page language="java" pageEncoding="UTF-8" contentType="text/ ...

  8. SouthidcEditor编辑器如何支持上传png图片

    SouthidcEditor编辑器如何支持上传png图片? asp网站一般都是用的南方数据SouthidcEditor编辑器,可是这个编辑器上传图片功能不能上传png类型的图片,那怎么办?我(红蜘蛛网 ...

  9. spring mvc 3.0 实现文件上传功能

    http://club.jledu.gov.cn/?uid-5282-action-viewspace-itemid-188672 —————————————————————————————————— ...

随机推荐

  1. 子Repeater获取父级Repeater绑定项的值

    原文发布时间为:2010-12-27 -- 来源于本人的百度文章 [由搬家工具导入] 1.子级Repeater中绑定父级的某个字段: <%# DataBinder.Eval((Container ...

  2. 兼容FF和IE的tooltip 鼠标提示框

    原文发布时间为:2009-09-07 -- 来源于本人的百度文章 [由搬家工具导入] http://www.walterzorn.de/tooltip/tooltip.htm 【请见该页面】 Down ...

  3. AVRStudio 6 添加调试功能

    下载这个文件并安装就可以了:http://avr-jungo-usb.software.informer.com/download/ 具体可以看这个贴子:http://electronics.stac ...

  4. derby数据库的一些总结

     本文主要是针对在osgi开发过程中的一些问题进行总结,其中dbcp数据源的配置是在SpringDM下配置的.一,derby数据源的内嵌模式       该模式的主要应用是嵌入式程序,因为其小巧,且不 ...

  5. Sql Server 2005 mdf、ldf文件无法复制问题

    [问题原因]Sql Server服务只要启动就一直占用,故无法进行编辑操作. [解决办法 - 1]: 1)在开始-运行对话框中输入"services.msc”,显示如下界面: 2)关闭如上选 ...

  6. Python 类的重写

    #!/usr/bin/env python # -*- coding:utf-8 -*- # @Time : 2017/11/7 22:46 # @Author : lijunjiang # @Fil ...

  7. ie8实现无刷新文件上传

    ie8由于无法使用FormData,想要无刷新上传文件就显得比较麻烦.这里推荐使用jQuery-File-Upload插件,它能够很方便的解决ie8无刷新文件上传问题.(最低兼容到ie6) jQuer ...

  8. require_once(): Failed opening required '/var/www/config/config.php' (include_path='.:') in /var/www/vendor/forkiss/pharest/src/Pharest/Register/Register.php on line 10

    环境 docker环境 错误 [Tue Jun 18 18:43:26 2019] 127.0.0.1:53980 [500]: /index.php - require_once(): Failed ...

  9. CBIntrospector俗称:内部检查工具

    Download View Introspector   (CBIntrospector)内部检查工具是IOS和IOS模拟器的小工具集,帮助在调试的UIKit类的用户界面,它尤其有用于动态UI布局创建 ...

  10. ios svn学习笔记(一)

    1, 遇到问题 git add in Xcode generates com.apple.dt.IDESourceControlErrorDomain error -70 这个错误发生在要右键选择要c ...