tftp.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. /*
  2. * Copyright 1994, 1995, 2000 Neil Russell.
  3. * (See License)
  4. * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
  5. * Copyright 2011 Comelit Group SpA,
  6. * Luca Ceresoli <luca.ceresoli@comelit.it>
  7. */
  8. #include <common.h>
  9. #include <command.h>
  10. #include <net.h>
  11. #include "tftp.h"
  12. #include "bootp.h"
  13. #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
  14. #include <flash.h>
  15. #endif
  16. /* Well known TFTP port # */
  17. #define WELL_KNOWN_PORT 69
  18. /* Millisecs to timeout for lost pkt */
  19. #define TIMEOUT 5000UL
  20. #ifndef CONFIG_NET_RETRY_COUNT
  21. /* # of timeouts before giving up */
  22. # define TIMEOUT_COUNT 10
  23. #else
  24. # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
  25. #endif
  26. /* Number of "loading" hashes per line (for checking the image size) */
  27. #define HASHES_PER_LINE 65
  28. /*
  29. * TFTP operations.
  30. */
  31. #define TFTP_RRQ 1
  32. #define TFTP_WRQ 2
  33. #define TFTP_DATA 3
  34. #define TFTP_ACK 4
  35. #define TFTP_ERROR 5
  36. #define TFTP_OACK 6
  37. static ulong TftpTimeoutMSecs = TIMEOUT;
  38. static int TftpTimeoutCountMax = TIMEOUT_COUNT;
  39. static ulong time_start; /* Record time we started tftp */
  40. /*
  41. * These globals govern the timeout behavior when attempting a connection to a
  42. * TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to
  43. * wait for the server to respond to initial connection. Second global,
  44. * TftpRRQTimeoutCountMax, gives the number of such connection retries.
  45. * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be
  46. * positive. The globals are meant to be set (and restored) by code needing
  47. * non-standard timeout behavior when initiating a TFTP transfer.
  48. */
  49. ulong TftpRRQTimeoutMSecs = TIMEOUT;
  50. int TftpRRQTimeoutCountMax = TIMEOUT_COUNT;
  51. enum {
  52. TFTP_ERR_UNDEFINED = 0,
  53. TFTP_ERR_FILE_NOT_FOUND = 1,
  54. TFTP_ERR_ACCESS_DENIED = 2,
  55. TFTP_ERR_DISK_FULL = 3,
  56. TFTP_ERR_UNEXPECTED_OPCODE = 4,
  57. TFTP_ERR_UNKNOWN_TRANSFER_ID = 5,
  58. TFTP_ERR_FILE_ALREADY_EXISTS = 6,
  59. };
  60. static IPaddr_t TftpRemoteIP;
  61. /* The UDP port at their end */
  62. static int TftpRemotePort;
  63. /* The UDP port at our end */
  64. static int TftpOurPort;
  65. static int TftpTimeoutCount;
  66. /* packet sequence number */
  67. static ulong TftpBlock;
  68. /* last packet sequence number received */
  69. static ulong TftpLastBlock;
  70. /* count of sequence number wraparounds */
  71. static ulong TftpBlockWrap;
  72. /* memory offset due to wrapping */
  73. static ulong TftpBlockWrapOffset;
  74. static int TftpState;
  75. #ifdef CONFIG_TFTP_TSIZE
  76. /* The file size reported by the server */
  77. static int TftpTsize;
  78. /* The number of hashes we printed */
  79. static short TftpNumchars;
  80. #endif
  81. #ifdef CONFIG_CMD_TFTPPUT
  82. static int TftpWriting; /* 1 if writing, else 0 */
  83. static int TftpFinalBlock; /* 1 if we have sent the last block */
  84. #else
  85. #define TftpWriting 0
  86. #endif
  87. #define STATE_SEND_RRQ 1
  88. #define STATE_DATA 2
  89. #define STATE_TOO_LARGE 3
  90. #define STATE_BAD_MAGIC 4
  91. #define STATE_OACK 5
  92. #define STATE_RECV_WRQ 6
  93. #define STATE_SEND_WRQ 7
  94. /* default TFTP block size */
  95. #define TFTP_BLOCK_SIZE 512
  96. /* sequence number is 16 bit */
  97. #define TFTP_SEQUENCE_SIZE ((ulong)(1<<16))
  98. #define DEFAULT_NAME_LEN (8 + 4 + 1)
  99. static char default_filename[DEFAULT_NAME_LEN];
  100. #ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
  101. #define MAX_LEN 128
  102. #else
  103. #define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
  104. #endif
  105. static char tftp_filename[MAX_LEN];
  106. /* 512 is poor choice for ethernet, MTU is typically 1500.
  107. * Minus eth.hdrs thats 1468. Can get 2x better throughput with
  108. * almost-MTU block sizes. At least try... fall back to 512 if need be.
  109. * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
  110. */
  111. #ifdef CONFIG_TFTP_BLOCKSIZE
  112. #define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
  113. #else
  114. #define TFTP_MTU_BLOCKSIZE 1468
  115. #endif
  116. static unsigned short TftpBlkSize = TFTP_BLOCK_SIZE;
  117. static unsigned short TftpBlkSizeOption = TFTP_MTU_BLOCKSIZE;
  118. #ifdef CONFIG_MCAST_TFTP
  119. #include <malloc.h>
  120. #define MTFTP_BITMAPSIZE 0x1000
  121. static unsigned *Bitmap;
  122. static int PrevBitmapHole, Mapsize = MTFTP_BITMAPSIZE;
  123. static uchar ProhibitMcast, MasterClient;
  124. static uchar Multicast;
  125. static int Mcast_port;
  126. static ulong TftpEndingBlock; /* can get 'last' block before done..*/
  127. static void parse_multicast_oack(char *pkt, int len);
  128. static void
  129. mcast_cleanup(void)
  130. {
  131. if (Mcast_addr)
  132. eth_mcast_join(Mcast_addr, 0);
  133. if (Bitmap)
  134. free(Bitmap);
  135. Bitmap = NULL;
  136. Mcast_addr = Multicast = Mcast_port = 0;
  137. TftpEndingBlock = -1;
  138. }
  139. #endif /* CONFIG_MCAST_TFTP */
  140. static inline void
  141. store_block(int block, uchar *src, unsigned len)
  142. {
  143. ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
  144. ulong newsize = offset + len;
  145. #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
  146. int i, rc = 0;
  147. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  148. /* start address in flash? */
  149. if (flash_info[i].flash_id == FLASH_UNKNOWN)
  150. continue;
  151. if (load_addr + offset >= flash_info[i].start[0]) {
  152. rc = 1;
  153. break;
  154. }
  155. }
  156. if (rc) { /* Flash is destination for this packet */
  157. rc = flash_write((char *)src, (ulong)(load_addr+offset), len);
  158. if (rc) {
  159. flash_perror(rc);
  160. net_set_state(NETLOOP_FAIL);
  161. return;
  162. }
  163. } else
  164. #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
  165. {
  166. (void)memcpy((void *)(load_addr + offset), src, len);
  167. }
  168. #ifdef CONFIG_MCAST_TFTP
  169. if (Multicast)
  170. ext2_set_bit(block, Bitmap);
  171. #endif
  172. if (NetBootFileXferSize < newsize)
  173. NetBootFileXferSize = newsize;
  174. }
  175. /* Clear our state ready for a new transfer */
  176. static void new_transfer(void)
  177. {
  178. TftpLastBlock = 0;
  179. TftpBlockWrap = 0;
  180. TftpBlockWrapOffset = 0;
  181. #ifdef CONFIG_CMD_TFTPPUT
  182. TftpFinalBlock = 0;
  183. #endif
  184. }
  185. #ifdef CONFIG_CMD_TFTPPUT
  186. /**
  187. * Load the next block from memory to be sent over tftp.
  188. *
  189. * @param block Block number to send
  190. * @param dst Destination buffer for data
  191. * @param len Number of bytes in block (this one and every other)
  192. * @return number of bytes loaded
  193. */
  194. static int load_block(unsigned block, uchar *dst, unsigned len)
  195. {
  196. /* We may want to get the final block from the previous set */
  197. ulong offset = ((int)block - 1) * len + TftpBlockWrapOffset;
  198. ulong tosend = len;
  199. tosend = min(NetBootFileXferSize - offset, tosend);
  200. (void)memcpy(dst, (void *)(save_addr + offset), tosend);
  201. debug("%s: block=%d, offset=%ld, len=%d, tosend=%ld\n", __func__,
  202. block, offset, len, tosend);
  203. return tosend;
  204. }
  205. #endif
  206. static void TftpSend(void);
  207. static void TftpTimeout(void);
  208. /**********************************************************************/
  209. static void show_block_marker(void)
  210. {
  211. #ifdef CONFIG_TFTP_TSIZE
  212. if (TftpTsize) {
  213. ulong pos = TftpBlock * TftpBlkSize + TftpBlockWrapOffset;
  214. while (TftpNumchars < pos * 50 / TftpTsize) {
  215. putc('#');
  216. TftpNumchars++;
  217. }
  218. } else
  219. #endif
  220. {
  221. if (((TftpBlock - 1) % 10) == 0)
  222. putc('#');
  223. else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0)
  224. puts("\n\t ");
  225. }
  226. }
  227. /**
  228. * restart the current transfer due to an error
  229. *
  230. * @param msg Message to print for user
  231. */
  232. static void restart(const char *msg)
  233. {
  234. printf("\n%s; starting again\n", msg);
  235. #ifdef CONFIG_MCAST_TFTP
  236. mcast_cleanup();
  237. #endif
  238. NetStartAgain();
  239. }
  240. /*
  241. * Check if the block number has wrapped, and update progress
  242. *
  243. * TODO: The egregious use of global variables in this file should be tidied.
  244. */
  245. static void update_block_number(void)
  246. {
  247. /*
  248. * RFC1350 specifies that the first data packet will
  249. * have sequence number 1. If we receive a sequence
  250. * number of 0 this means that there was a wrap
  251. * around of the (16 bit) counter.
  252. */
  253. if (TftpBlock == 0 && TftpLastBlock != 0) {
  254. TftpBlockWrap++;
  255. TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE;
  256. TftpTimeoutCount = 0; /* we've done well, reset thhe timeout */
  257. } else {
  258. show_block_marker();
  259. }
  260. }
  261. /* The TFTP get or put is complete */
  262. static void tftp_complete(void)
  263. {
  264. #ifdef CONFIG_TFTP_TSIZE
  265. /* Print hash marks for the last packet received */
  266. while (TftpTsize && TftpNumchars < 49) {
  267. putc('#');
  268. TftpNumchars++;
  269. }
  270. puts(" ");
  271. print_size(TftpTsize, "");
  272. #endif
  273. time_start = get_timer(time_start);
  274. if (time_start > 0) {
  275. puts("\n\t "); /* Line up with "Loading: " */
  276. print_size(NetBootFileXferSize /
  277. time_start * 1000, "/s");
  278. }
  279. puts("\ndone\n");
  280. net_set_state(NETLOOP_SUCCESS);
  281. }
  282. static void
  283. TftpSend(void)
  284. {
  285. uchar *pkt;
  286. uchar *xp;
  287. int len = 0;
  288. ushort *s;
  289. #ifdef CONFIG_MCAST_TFTP
  290. /* Multicast TFTP.. non-MasterClients do not ACK data. */
  291. if (Multicast
  292. && (TftpState == STATE_DATA)
  293. && (MasterClient == 0))
  294. return;
  295. #endif
  296. /*
  297. * We will always be sending some sort of packet, so
  298. * cobble together the packet headers now.
  299. */
  300. pkt = NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
  301. switch (TftpState) {
  302. case STATE_SEND_RRQ:
  303. case STATE_SEND_WRQ:
  304. xp = pkt;
  305. s = (ushort *)pkt;
  306. #ifdef CONFIG_CMD_TFTPPUT
  307. *s++ = htons(TftpState == STATE_SEND_RRQ ? TFTP_RRQ :
  308. TFTP_WRQ);
  309. #else
  310. *s++ = htons(TFTP_RRQ);
  311. #endif
  312. pkt = (uchar *)s;
  313. strcpy((char *)pkt, tftp_filename);
  314. pkt += strlen(tftp_filename) + 1;
  315. strcpy((char *)pkt, "octet");
  316. pkt += 5 /*strlen("octet")*/ + 1;
  317. strcpy((char *)pkt, "timeout");
  318. pkt += 7 /*strlen("timeout")*/ + 1;
  319. sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000);
  320. debug("send option \"timeout %s\"\n", (char *)pkt);
  321. pkt += strlen((char *)pkt) + 1;
  322. #ifdef CONFIG_TFTP_TSIZE
  323. pkt += sprintf((char *)pkt, "tsize%c%lu%c",
  324. 0, NetBootFileXferSize, 0);
  325. #endif
  326. /* try for more effic. blk size */
  327. pkt += sprintf((char *)pkt, "blksize%c%d%c",
  328. 0, TftpBlkSizeOption, 0);
  329. #ifdef CONFIG_MCAST_TFTP
  330. /* Check all preconditions before even trying the option */
  331. if (!ProhibitMcast) {
  332. Bitmap = malloc(Mapsize);
  333. if (Bitmap && eth_get_dev()->mcast) {
  334. free(Bitmap);
  335. Bitmap = NULL;
  336. pkt += sprintf((char *)pkt, "multicast%c%c",
  337. 0, 0);
  338. }
  339. }
  340. #endif /* CONFIG_MCAST_TFTP */
  341. len = pkt - xp;
  342. break;
  343. case STATE_OACK:
  344. #ifdef CONFIG_MCAST_TFTP
  345. /* My turn! Start at where I need blocks I missed.*/
  346. if (Multicast)
  347. TftpBlock = ext2_find_next_zero_bit(Bitmap,
  348. (Mapsize*8), 0);
  349. /*..falling..*/
  350. #endif
  351. case STATE_RECV_WRQ:
  352. case STATE_DATA:
  353. xp = pkt;
  354. s = (ushort *)pkt;
  355. s[0] = htons(TFTP_ACK);
  356. s[1] = htons(TftpBlock);
  357. pkt = (uchar *)(s + 2);
  358. #ifdef CONFIG_CMD_TFTPPUT
  359. if (TftpWriting) {
  360. int toload = TftpBlkSize;
  361. int loaded = load_block(TftpBlock, pkt, toload);
  362. s[0] = htons(TFTP_DATA);
  363. pkt += loaded;
  364. TftpFinalBlock = (loaded < toload);
  365. }
  366. #endif
  367. len = pkt - xp;
  368. break;
  369. case STATE_TOO_LARGE:
  370. xp = pkt;
  371. s = (ushort *)pkt;
  372. *s++ = htons(TFTP_ERROR);
  373. *s++ = htons(3);
  374. pkt = (uchar *)s;
  375. strcpy((char *)pkt, "File too large");
  376. pkt += 14 /*strlen("File too large")*/ + 1;
  377. len = pkt - xp;
  378. break;
  379. case STATE_BAD_MAGIC:
  380. xp = pkt;
  381. s = (ushort *)pkt;
  382. *s++ = htons(TFTP_ERROR);
  383. *s++ = htons(2);
  384. pkt = (uchar *)s;
  385. strcpy((char *)pkt, "File has bad magic");
  386. pkt += 18 /*strlen("File has bad magic")*/ + 1;
  387. len = pkt - xp;
  388. break;
  389. }
  390. NetSendUDPPacket(NetServerEther, TftpRemoteIP, TftpRemotePort,
  391. TftpOurPort, len);
  392. }
  393. #ifdef CONFIG_CMD_TFTPPUT
  394. static void icmp_handler(unsigned type, unsigned code, unsigned dest,
  395. IPaddr_t sip, unsigned src, uchar *pkt, unsigned len)
  396. {
  397. if (type == ICMP_NOT_REACH && code == ICMP_NOT_REACH_PORT) {
  398. /* Oh dear the other end has gone away */
  399. restart("TFTP server died");
  400. }
  401. }
  402. #endif
  403. static void
  404. TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
  405. unsigned len)
  406. {
  407. __be16 proto;
  408. __be16 *s;
  409. int i;
  410. if (dest != TftpOurPort) {
  411. #ifdef CONFIG_MCAST_TFTP
  412. if (Multicast
  413. && (!Mcast_port || (dest != Mcast_port)))
  414. #endif
  415. return;
  416. }
  417. if (TftpState != STATE_SEND_RRQ && src != TftpRemotePort &&
  418. TftpState != STATE_RECV_WRQ && TftpState != STATE_SEND_WRQ)
  419. return;
  420. if (len < 2)
  421. return;
  422. len -= 2;
  423. /* warning: don't use increment (++) in ntohs() macros!! */
  424. s = (__be16 *)pkt;
  425. proto = *s++;
  426. pkt = (uchar *)s;
  427. switch (ntohs(proto)) {
  428. case TFTP_RRQ:
  429. break;
  430. case TFTP_ACK:
  431. #ifdef CONFIG_CMD_TFTPPUT
  432. if (TftpWriting) {
  433. if (TftpFinalBlock) {
  434. tftp_complete();
  435. } else {
  436. /*
  437. * Move to the next block. We want our block
  438. * count to wrap just like the other end!
  439. */
  440. int block = ntohs(*s);
  441. int ack_ok = (TftpBlock == block);
  442. TftpBlock = (unsigned short)(block + 1);
  443. update_block_number();
  444. if (ack_ok)
  445. TftpSend(); /* Send next data block */
  446. }
  447. }
  448. #endif
  449. break;
  450. default:
  451. break;
  452. #ifdef CONFIG_CMD_TFTPSRV
  453. case TFTP_WRQ:
  454. debug("Got WRQ\n");
  455. TftpRemoteIP = sip;
  456. TftpRemotePort = src;
  457. TftpOurPort = 1024 + (get_timer(0) % 3072);
  458. new_transfer();
  459. TftpSend(); /* Send ACK(0) */
  460. break;
  461. #endif
  462. case TFTP_OACK:
  463. debug("Got OACK: %s %s\n",
  464. pkt,
  465. pkt + strlen((char *)pkt) + 1);
  466. TftpState = STATE_OACK;
  467. TftpRemotePort = src;
  468. /*
  469. * Check for 'blksize' option.
  470. * Careful: "i" is signed, "len" is unsigned, thus
  471. * something like "len-8" may give a *huge* number
  472. */
  473. for (i = 0; i+8 < len; i++) {
  474. if (strcmp((char *)pkt+i, "blksize") == 0) {
  475. TftpBlkSize = (unsigned short)
  476. simple_strtoul((char *)pkt+i+8, NULL,
  477. 10);
  478. debug("Blocksize ack: %s, %d\n",
  479. (char *)pkt+i+8, TftpBlkSize);
  480. }
  481. #ifdef CONFIG_TFTP_TSIZE
  482. if (strcmp((char *)pkt+i, "tsize") == 0) {
  483. TftpTsize = simple_strtoul((char *)pkt+i+6,
  484. NULL, 10);
  485. debug("size = %s, %d\n",
  486. (char *)pkt+i+6, TftpTsize);
  487. }
  488. #endif
  489. }
  490. #ifdef CONFIG_MCAST_TFTP
  491. parse_multicast_oack((char *)pkt, len-1);
  492. if ((Multicast) && (!MasterClient))
  493. TftpState = STATE_DATA; /* passive.. */
  494. else
  495. #endif
  496. #ifdef CONFIG_CMD_TFTPPUT
  497. if (TftpWriting) {
  498. /* Get ready to send the first block */
  499. TftpState = STATE_DATA;
  500. TftpBlock++;
  501. }
  502. #endif
  503. TftpSend(); /* Send ACK or first data block */
  504. break;
  505. case TFTP_DATA:
  506. if (len < 2)
  507. return;
  508. len -= 2;
  509. TftpBlock = ntohs(*(__be16 *)pkt);
  510. update_block_number();
  511. if (TftpState == STATE_SEND_RRQ)
  512. debug("Server did not acknowledge timeout option!\n");
  513. if (TftpState == STATE_SEND_RRQ || TftpState == STATE_OACK ||
  514. TftpState == STATE_RECV_WRQ) {
  515. /* first block received */
  516. TftpState = STATE_DATA;
  517. TftpRemotePort = src;
  518. new_transfer();
  519. #ifdef CONFIG_MCAST_TFTP
  520. if (Multicast) { /* start!=1 common if mcast */
  521. TftpLastBlock = TftpBlock - 1;
  522. } else
  523. #endif
  524. if (TftpBlock != 1) { /* Assertion */
  525. printf("\nTFTP error: "
  526. "First block is not block 1 (%ld)\n"
  527. "Starting again\n\n",
  528. TftpBlock);
  529. NetStartAgain();
  530. break;
  531. }
  532. }
  533. if (TftpBlock == TftpLastBlock) {
  534. /*
  535. * Same block again; ignore it.
  536. */
  537. break;
  538. }
  539. TftpLastBlock = TftpBlock;
  540. TftpTimeoutCountMax = TIMEOUT_COUNT;
  541. NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
  542. store_block(TftpBlock - 1, pkt + 2, len);
  543. /*
  544. * Acknowledge the block just received, which will prompt
  545. * the remote for the next one.
  546. */
  547. #ifdef CONFIG_MCAST_TFTP
  548. /* if I am the MasterClient, actively calculate what my next
  549. * needed block is; else I'm passive; not ACKING
  550. */
  551. if (Multicast) {
  552. if (len < TftpBlkSize) {
  553. TftpEndingBlock = TftpBlock;
  554. } else if (MasterClient) {
  555. TftpBlock = PrevBitmapHole =
  556. ext2_find_next_zero_bit(
  557. Bitmap,
  558. (Mapsize*8),
  559. PrevBitmapHole);
  560. if (TftpBlock > ((Mapsize*8) - 1)) {
  561. printf("tftpfile too big\n");
  562. /* try to double it and retry */
  563. Mapsize <<= 1;
  564. mcast_cleanup();
  565. NetStartAgain();
  566. return;
  567. }
  568. TftpLastBlock = TftpBlock;
  569. }
  570. }
  571. #endif
  572. TftpSend();
  573. #ifdef CONFIG_MCAST_TFTP
  574. if (Multicast) {
  575. if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
  576. puts("\nMulticast tftp done\n");
  577. mcast_cleanup();
  578. net_set_state(NETLOOP_SUCCESS);
  579. }
  580. } else
  581. #endif
  582. if (len < TftpBlkSize)
  583. tftp_complete();
  584. break;
  585. case TFTP_ERROR:
  586. printf("\nTFTP error: '%s' (%d)\n",
  587. pkt + 2, ntohs(*(__be16 *)pkt));
  588. switch (ntohs(*(__be16 *)pkt)) {
  589. case TFTP_ERR_FILE_NOT_FOUND:
  590. case TFTP_ERR_ACCESS_DENIED:
  591. puts("Not retrying...\n");
  592. eth_halt();
  593. net_set_state(NETLOOP_FAIL);
  594. break;
  595. case TFTP_ERR_UNDEFINED:
  596. case TFTP_ERR_DISK_FULL:
  597. case TFTP_ERR_UNEXPECTED_OPCODE:
  598. case TFTP_ERR_UNKNOWN_TRANSFER_ID:
  599. case TFTP_ERR_FILE_ALREADY_EXISTS:
  600. default:
  601. puts("Starting again\n\n");
  602. #ifdef CONFIG_MCAST_TFTP
  603. mcast_cleanup();
  604. #endif
  605. NetStartAgain();
  606. break;
  607. }
  608. break;
  609. }
  610. }
  611. static void
  612. TftpTimeout(void)
  613. {
  614. if (++TftpTimeoutCount > TftpTimeoutCountMax) {
  615. restart("Retry count exceeded");
  616. } else {
  617. puts("T ");
  618. NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
  619. if (TftpState != STATE_RECV_WRQ)
  620. TftpSend();
  621. }
  622. }
  623. void TftpStart(enum proto_t protocol)
  624. {
  625. char *ep; /* Environment pointer */
  626. /*
  627. * Allow the user to choose TFTP blocksize and timeout.
  628. * TFTP protocol has a minimal timeout of 1 second.
  629. */
  630. ep = getenv("tftpblocksize");
  631. if (ep != NULL)
  632. TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
  633. ep = getenv("tftptimeout");
  634. if (ep != NULL)
  635. TftpTimeoutMSecs = simple_strtol(ep, NULL, 10);
  636. if (TftpTimeoutMSecs < 1000) {
  637. printf("TFTP timeout (%ld ms) too low, "
  638. "set minimum = 1000 ms\n",
  639. TftpTimeoutMSecs);
  640. TftpTimeoutMSecs = 1000;
  641. }
  642. debug("TFTP blocksize = %i, timeout = %ld ms\n",
  643. TftpBlkSizeOption, TftpTimeoutMSecs);
  644. TftpRemoteIP = NetServerIP;
  645. if (BootFile[0] == '\0') {
  646. sprintf(default_filename, "%02X%02X%02X%02X.img",
  647. NetOurIP & 0xFF,
  648. (NetOurIP >> 8) & 0xFF,
  649. (NetOurIP >> 16) & 0xFF,
  650. (NetOurIP >> 24) & 0xFF);
  651. strncpy(tftp_filename, default_filename, MAX_LEN);
  652. tftp_filename[MAX_LEN-1] = 0;
  653. printf("*** Warning: no boot file name; using '%s'\n",
  654. tftp_filename);
  655. } else {
  656. char *p = strchr(BootFile, ':');
  657. if (p == NULL) {
  658. strncpy(tftp_filename, BootFile, MAX_LEN);
  659. tftp_filename[MAX_LEN-1] = 0;
  660. } else {
  661. TftpRemoteIP = string_to_ip(BootFile);
  662. strncpy(tftp_filename, p + 1, MAX_LEN);
  663. tftp_filename[MAX_LEN-1] = 0;
  664. }
  665. }
  666. printf("Using %s device\n", eth_get_name());
  667. printf("TFTP %s server %pI4; our IP address is %pI4",
  668. #ifdef CONFIG_CMD_TFTPPUT
  669. protocol == TFTPPUT ? "to" : "from",
  670. #else
  671. "from",
  672. #endif
  673. &TftpRemoteIP, &NetOurIP);
  674. /* Check if we need to send across this subnet */
  675. if (NetOurGatewayIP && NetOurSubnetMask) {
  676. IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
  677. IPaddr_t RemoteNet = TftpRemoteIP & NetOurSubnetMask;
  678. if (OurNet != RemoteNet)
  679. printf("; sending through gateway %pI4",
  680. &NetOurGatewayIP);
  681. }
  682. putc('\n');
  683. printf("Filename '%s'.", tftp_filename);
  684. if (NetBootFileSize) {
  685. printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
  686. print_size(NetBootFileSize<<9, "");
  687. }
  688. putc('\n');
  689. #ifdef CONFIG_CMD_TFTPPUT
  690. TftpWriting = (protocol == TFTPPUT);
  691. if (TftpWriting) {
  692. printf("Save address: 0x%lx\n", save_addr);
  693. printf("Save size: 0x%lx\n", save_size);
  694. NetBootFileXferSize = save_size;
  695. puts("Saving: *\b");
  696. TftpState = STATE_SEND_WRQ;
  697. new_transfer();
  698. } else
  699. #endif
  700. {
  701. printf("Load address: 0x%lx\n", load_addr);
  702. puts("Loading: *\b");
  703. TftpState = STATE_SEND_RRQ;
  704. }
  705. time_start = get_timer(0);
  706. TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
  707. NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
  708. net_set_udp_handler(TftpHandler);
  709. #ifdef CONFIG_CMD_TFTPPUT
  710. net_set_icmp_handler(icmp_handler);
  711. #endif
  712. TftpRemotePort = WELL_KNOWN_PORT;
  713. TftpTimeoutCount = 0;
  714. /* Use a pseudo-random port unless a specific port is set */
  715. TftpOurPort = 1024 + (get_timer(0) % 3072);
  716. #ifdef CONFIG_TFTP_PORT
  717. ep = getenv("tftpdstp");
  718. if (ep != NULL)
  719. TftpRemotePort = simple_strtol(ep, NULL, 10);
  720. ep = getenv("tftpsrcp");
  721. if (ep != NULL)
  722. TftpOurPort = simple_strtol(ep, NULL, 10);
  723. #endif
  724. TftpBlock = 0;
  725. /* zero out server ether in case the server ip has changed */
  726. memset(NetServerEther, 0, 6);
  727. /* Revert TftpBlkSize to dflt */
  728. TftpBlkSize = TFTP_BLOCK_SIZE;
  729. #ifdef CONFIG_MCAST_TFTP
  730. mcast_cleanup();
  731. #endif
  732. #ifdef CONFIG_TFTP_TSIZE
  733. TftpTsize = 0;
  734. TftpNumchars = 0;
  735. #endif
  736. TftpSend();
  737. }
  738. #ifdef CONFIG_CMD_TFTPSRV
  739. void
  740. TftpStartServer(void)
  741. {
  742. tftp_filename[0] = 0;
  743. printf("Using %s device\n", eth_get_name());
  744. printf("Listening for TFTP transfer on %pI4\n", &NetOurIP);
  745. printf("Load address: 0x%lx\n", load_addr);
  746. puts("Loading: *\b");
  747. TftpTimeoutCountMax = TIMEOUT_COUNT;
  748. TftpTimeoutCount = 0;
  749. TftpTimeoutMSecs = TIMEOUT;
  750. NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
  751. /* Revert TftpBlkSize to dflt */
  752. TftpBlkSize = TFTP_BLOCK_SIZE;
  753. TftpBlock = 0;
  754. TftpOurPort = WELL_KNOWN_PORT;
  755. #ifdef CONFIG_TFTP_TSIZE
  756. TftpTsize = 0;
  757. TftpNumchars = 0;
  758. #endif
  759. TftpState = STATE_RECV_WRQ;
  760. net_set_udp_handler(TftpHandler);
  761. /* zero out server ether in case the server ip has changed */
  762. memset(NetServerEther, 0, 6);
  763. }
  764. #endif /* CONFIG_CMD_TFTPSRV */
  765. #ifdef CONFIG_MCAST_TFTP
  766. /* Credits: atftp project.
  767. */
  768. /* pick up BcastAddr, Port, and whether I am [now] the master-client. *
  769. * Frame:
  770. * +-------+-----------+---+-------~~-------+---+
  771. * | opc | multicast | 0 | addr, port, mc | 0 |
  772. * +-------+-----------+---+-------~~-------+---+
  773. * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
  774. * I am the new master-client so must send ACKs to DataBlocks. If I am not
  775. * master-client, I'm a passive client, gathering what DataBlocks I may and
  776. * making note of which ones I got in my bitmask.
  777. * In theory, I never go from master->passive..
  778. * .. this comes in with pkt already pointing just past opc
  779. */
  780. static void parse_multicast_oack(char *pkt, int len)
  781. {
  782. int i;
  783. IPaddr_t addr;
  784. char *mc_adr, *port, *mc;
  785. mc_adr = port = mc = NULL;
  786. /* march along looking for 'multicast\0', which has to start at least
  787. * 14 bytes back from the end.
  788. */
  789. for (i = 0; i < len-14; i++)
  790. if (strcmp(pkt+i, "multicast") == 0)
  791. break;
  792. if (i >= (len-14)) /* non-Multicast OACK, ign. */
  793. return;
  794. i += 10; /* strlen multicast */
  795. mc_adr = pkt+i;
  796. for (; i < len; i++) {
  797. if (*(pkt+i) == ',') {
  798. *(pkt+i) = '\0';
  799. if (port) {
  800. mc = pkt+i+1;
  801. break;
  802. } else {
  803. port = pkt+i+1;
  804. }
  805. }
  806. }
  807. if (!port || !mc_adr || !mc)
  808. return;
  809. if (Multicast && MasterClient) {
  810. printf("I got a OACK as master Client, WRONG!\n");
  811. return;
  812. }
  813. /* ..I now accept packets destined for this MCAST addr, port */
  814. if (!Multicast) {
  815. if (Bitmap) {
  816. printf("Internal failure! no mcast.\n");
  817. free(Bitmap);
  818. Bitmap = NULL;
  819. ProhibitMcast = 1;
  820. return ;
  821. }
  822. /* I malloc instead of pre-declare; so that if the file ends
  823. * up being too big for this bitmap I can retry
  824. */
  825. Bitmap = malloc(Mapsize);
  826. if (!Bitmap) {
  827. printf("No Bitmap, no multicast. Sorry.\n");
  828. ProhibitMcast = 1;
  829. return;
  830. }
  831. memset(Bitmap, 0, Mapsize);
  832. PrevBitmapHole = 0;
  833. Multicast = 1;
  834. }
  835. addr = string_to_ip(mc_adr);
  836. if (Mcast_addr != addr) {
  837. if (Mcast_addr)
  838. eth_mcast_join(Mcast_addr, 0);
  839. Mcast_addr = addr;
  840. if (eth_mcast_join(Mcast_addr, 1)) {
  841. printf("Fail to set mcast, revert to TFTP\n");
  842. ProhibitMcast = 1;
  843. mcast_cleanup();
  844. NetStartAgain();
  845. }
  846. }
  847. MasterClient = (unsigned char)simple_strtoul((char *)mc, NULL, 10);
  848. Mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
  849. printf("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
  850. return;
  851. }
  852. #endif /* Multicast TFTP */