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