tftp.c 14 KB

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