tftp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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 5 /* 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,htons(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. /* Check for 'blksize' option */
  244. for (i=0;i<len-8;i++) {
  245. if (strcmp ((char*)pkt+i,"blksize") == 0) {
  246. TftpBlkSize = (unsigned short)
  247. simple_strtoul((char*)pkt+i+8,NULL,10);
  248. #ifdef ET_DEBUG
  249. printf ("Blocksize ack: %s, %d\n",
  250. (char*)pkt+i+8,TftpBlkSize);
  251. #endif
  252. break;
  253. }
  254. }
  255. #ifdef CONFIG_MCAST_TFTP
  256. parse_multicast_oack((char *)pkt,len-1);
  257. if ((Multicast) && (!MasterClient))
  258. TftpState = STATE_DATA; /* passive.. */
  259. else
  260. #endif
  261. TftpSend (); /* Send ACK */
  262. break;
  263. case TFTP_DATA:
  264. if (len < 2)
  265. return;
  266. len -= 2;
  267. TftpBlock = ntohs(*(ushort *)pkt);
  268. /*
  269. * RFC1350 specifies that the first data packet will
  270. * have sequence number 1. If we receive a sequence
  271. * number of 0 this means that there was a wrap
  272. * around of the (16 bit) counter.
  273. */
  274. if (TftpBlock == 0) {
  275. TftpBlockWrap++;
  276. TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE;
  277. printf ("\n\t %lu MB received\n\t ", TftpBlockWrapOffset>>20);
  278. } else {
  279. if (((TftpBlock - 1) % 10) == 0) {
  280. putc ('#');
  281. } else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) {
  282. puts ("\n\t ");
  283. }
  284. }
  285. #ifdef ET_DEBUG
  286. if (TftpState == STATE_RRQ) {
  287. puts ("Server did not acknowledge timeout option!\n");
  288. }
  289. #endif
  290. if (TftpState == STATE_RRQ || TftpState == STATE_OACK) {
  291. /* first block received */
  292. TftpState = STATE_DATA;
  293. TftpServerPort = src;
  294. TftpLastBlock = 0;
  295. TftpBlockWrap = 0;
  296. TftpBlockWrapOffset = 0;
  297. #ifdef CONFIG_MCAST_TFTP
  298. if (Multicast) { /* start!=1 common if mcast */
  299. TftpLastBlock = TftpBlock - 1;
  300. } else
  301. #endif
  302. if (TftpBlock != 1) { /* Assertion */
  303. printf ("\nTFTP error: "
  304. "First block is not block 1 (%ld)\n"
  305. "Starting again\n\n",
  306. TftpBlock);
  307. NetStartAgain ();
  308. break;
  309. }
  310. }
  311. if (TftpBlock == TftpLastBlock) {
  312. /*
  313. * Same block again; ignore it.
  314. */
  315. break;
  316. }
  317. TftpLastBlock = TftpBlock;
  318. NetSetTimeout (TIMEOUT * CFG_HZ, TftpTimeout);
  319. store_block (TftpBlock - 1, pkt + 2, len);
  320. /*
  321. * Acknoledge the block just received, which will prompt
  322. * the server for the next one.
  323. */
  324. #ifdef CONFIG_MCAST_TFTP
  325. /* if I am the MasterClient, actively calculate what my next
  326. * needed block is; else I'm passive; not ACKING
  327. */
  328. if (Multicast) {
  329. if (len < TftpBlkSize) {
  330. TftpEndingBlock = TftpBlock;
  331. } else if (MasterClient) {
  332. TftpBlock = PrevBitmapHole =
  333. ext2_find_next_zero_bit(
  334. Bitmap,
  335. (Mapsize*8),
  336. PrevBitmapHole);
  337. if (TftpBlock > ((Mapsize*8) - 1)) {
  338. printf ("tftpfile too big\n");
  339. /* try to double it and retry */
  340. Mapsize<<=1;
  341. mcast_cleanup();
  342. NetStartAgain ();
  343. return;
  344. }
  345. TftpLastBlock = TftpBlock;
  346. }
  347. }
  348. #endif
  349. TftpSend ();
  350. #ifdef CONFIG_MCAST_TFTP
  351. if (Multicast) {
  352. if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
  353. puts ("\nMulticast tftp done\n");
  354. mcast_cleanup();
  355. NetState = NETLOOP_SUCCESS;
  356. }
  357. }
  358. else
  359. #endif
  360. if (len < TftpBlkSize) {
  361. /*
  362. * We received the whole thing. Try to
  363. * run it.
  364. */
  365. puts ("\ndone\n");
  366. NetState = NETLOOP_SUCCESS;
  367. }
  368. break;
  369. case TFTP_ERROR:
  370. printf ("\nTFTP error: '%s' (%d)\n",
  371. pkt + 2, ntohs(*(ushort *)pkt));
  372. puts ("Starting again\n\n");
  373. #ifdef CONFIG_MCAST_TFTP
  374. mcast_cleanup();
  375. #endif
  376. NetStartAgain ();
  377. break;
  378. }
  379. }
  380. static void
  381. TftpTimeout (void)
  382. {
  383. if (++TftpTimeoutCount > TIMEOUT_COUNT) {
  384. puts ("\nRetry count exceeded; starting again\n");
  385. #ifdef CONFIG_MCAST_TFTP
  386. mcast_cleanup();
  387. #endif
  388. NetStartAgain ();
  389. } else {
  390. puts ("T ");
  391. NetSetTimeout (TIMEOUT * CFG_HZ, TftpTimeout);
  392. TftpSend ();
  393. }
  394. }
  395. void
  396. TftpStart (void)
  397. {
  398. #ifdef CONFIG_TFTP_PORT
  399. char *ep; /* Environment pointer */
  400. #endif
  401. if (BootFile[0] == '\0') {
  402. sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
  403. NetOurIP & 0xFF,
  404. (NetOurIP >> 8) & 0xFF,
  405. (NetOurIP >> 16) & 0xFF,
  406. (NetOurIP >> 24) & 0xFF );
  407. tftp_filename = default_filename;
  408. printf ("*** Warning: no boot file name; using '%s'\n",
  409. tftp_filename);
  410. } else {
  411. tftp_filename = BootFile;
  412. }
  413. #if defined(CONFIG_NET_MULTI)
  414. printf ("Using %s device\n", eth_get_name());
  415. #endif
  416. puts ("TFTP from server "); print_IPaddr (NetServerIP);
  417. puts ("; our IP address is "); print_IPaddr (NetOurIP);
  418. /* Check if we need to send across this subnet */
  419. if (NetOurGatewayIP && NetOurSubnetMask) {
  420. IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
  421. IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask;
  422. if (OurNet != ServerNet) {
  423. puts ("; sending through gateway ");
  424. print_IPaddr (NetOurGatewayIP) ;
  425. }
  426. }
  427. putc ('\n');
  428. printf ("Filename '%s'.", tftp_filename);
  429. if (NetBootFileSize) {
  430. printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
  431. print_size (NetBootFileSize<<9, "");
  432. }
  433. putc ('\n');
  434. printf ("Load address: 0x%lx\n", load_addr);
  435. puts ("Loading: *\b");
  436. NetSetTimeout (TIMEOUT * CFG_HZ, TftpTimeout);
  437. NetSetHandler (TftpHandler);
  438. TftpServerPort = WELL_KNOWN_PORT;
  439. TftpTimeoutCount = 0;
  440. TftpState = STATE_RRQ;
  441. /* Use a pseudo-random port unless a specific port is set */
  442. TftpOurPort = 1024 + (get_timer(0) % 3072);
  443. #ifdef CONFIG_TFTP_PORT
  444. if ((ep = getenv("tftpdstp")) != NULL) {
  445. TftpServerPort = simple_strtol(ep, NULL, 10);
  446. }
  447. if ((ep = getenv("tftpsrcp")) != NULL) {
  448. TftpOurPort= simple_strtol(ep, NULL, 10);
  449. }
  450. #endif
  451. TftpBlock = 0;
  452. /* zero out server ether in case the server ip has changed */
  453. memset(NetServerEther, 0, 6);
  454. /* Revert TftpBlkSize to dflt */
  455. TftpBlkSize = TFTP_BLOCK_SIZE;
  456. #ifdef CONFIG_MCAST_TFTP
  457. mcast_cleanup();
  458. #endif
  459. TftpSend ();
  460. }
  461. #ifdef CONFIG_MCAST_TFTP
  462. /* Credits: atftp project.
  463. */
  464. /* pick up BcastAddr, Port, and whether I am [now] the master-client. *
  465. * Frame:
  466. * +-------+-----------+---+-------~~-------+---+
  467. * | opc | multicast | 0 | addr, port, mc | 0 |
  468. * +-------+-----------+---+-------~~-------+---+
  469. * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
  470. * I am the new master-client so must send ACKs to DataBlocks. If I am not
  471. * master-client, I'm a passive client, gathering what DataBlocks I may and
  472. * making note of which ones I got in my bitmask.
  473. * In theory, I never go from master->passive..
  474. * .. this comes in with pkt already pointing just past opc
  475. */
  476. static void parse_multicast_oack(char *pkt, int len)
  477. {
  478. int i;
  479. IPaddr_t addr;
  480. char *mc_adr, *port, *mc;
  481. mc_adr=port=mc=NULL;
  482. /* march along looking for 'multicast\0', which has to start at least
  483. * 14 bytes back from the end.
  484. */
  485. for (i=0;i<len-14;i++)
  486. if (strcmp (pkt+i,"multicast") == 0)
  487. break;
  488. if (i >= (len-14)) /* non-Multicast OACK, ign. */
  489. return;
  490. i+=10; /* strlen multicast */
  491. mc_adr = pkt+i;
  492. for (;i<len;i++) {
  493. if (*(pkt+i) == ',') {
  494. *(pkt+i) = '\0';
  495. if (port) {
  496. mc = pkt+i+1;
  497. break;
  498. } else {
  499. port = pkt+i+1;
  500. }
  501. }
  502. }
  503. if (!port || !mc_adr || !mc ) return;
  504. if (Multicast && MasterClient) {
  505. printf ("I got a OACK as master Client, WRONG!\n");
  506. return;
  507. }
  508. /* ..I now accept packets destined for this MCAST addr, port */
  509. if (!Multicast) {
  510. if (Bitmap) {
  511. printf ("Internal failure! no mcast.\n");
  512. free(Bitmap);
  513. Bitmap=NULL;
  514. ProhibitMcast=1;
  515. return ;
  516. }
  517. /* I malloc instead of pre-declare; so that if the file ends
  518. * up being too big for this bitmap I can retry
  519. */
  520. if (!(Bitmap = malloc (Mapsize))) {
  521. printf ("No Bitmap, no multicast. Sorry.\n");
  522. ProhibitMcast=1;
  523. return;
  524. }
  525. memset (Bitmap,0,Mapsize);
  526. PrevBitmapHole = 0;
  527. Multicast = 1;
  528. }
  529. addr = string_to_ip(mc_adr);
  530. if (Mcast_addr != addr) {
  531. if (Mcast_addr)
  532. eth_mcast_join(Mcast_addr, 0);
  533. if (eth_mcast_join(Mcast_addr=addr, 1)) {
  534. printf ("Fail to set mcast, revert to TFTP\n");
  535. ProhibitMcast=1;
  536. mcast_cleanup();
  537. NetStartAgain();
  538. }
  539. }
  540. MasterClient = (unsigned char)simple_strtoul((char *)mc,NULL,10);
  541. Mcast_port = (unsigned short)simple_strtoul(port,NULL,10);
  542. printf ("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
  543. return;
  544. }
  545. #endif /* Multicast TFTP */
  546. #endif /* CFG_CMD_NET */