tftp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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) {
  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. #endif
  271. time_start = get_timer(time_start);
  272. if (time_start > 0) {
  273. puts("\n\t "); /* Line up with "Loading: " */
  274. print_size(NetBootFileXferSize /
  275. time_start * 1000, "/s");
  276. }
  277. puts("\ndone\n");
  278. net_set_state(NETLOOP_SUCCESS);
  279. }
  280. static void
  281. TftpSend(void)
  282. {
  283. uchar *pkt;
  284. uchar *xp;
  285. int len = 0;
  286. ushort *s;
  287. #ifdef CONFIG_MCAST_TFTP
  288. /* Multicast TFTP.. non-MasterClients do not ACK data. */
  289. if (Multicast
  290. && (TftpState == STATE_DATA)
  291. && (MasterClient == 0))
  292. return;
  293. #endif
  294. /*
  295. * We will always be sending some sort of packet, so
  296. * cobble together the packet headers now.
  297. */
  298. pkt = NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
  299. switch (TftpState) {
  300. case STATE_SEND_RRQ:
  301. case STATE_SEND_WRQ:
  302. xp = pkt;
  303. s = (ushort *)pkt;
  304. #ifdef CONFIG_CMD_TFTPPUT
  305. *s++ = htons(TftpState == STATE_SEND_RRQ ? TFTP_RRQ :
  306. TFTP_WRQ);
  307. #else
  308. *s++ = htons(TFTP_RRQ);
  309. #endif
  310. pkt = (uchar *)s;
  311. strcpy((char *)pkt, tftp_filename);
  312. pkt += strlen(tftp_filename) + 1;
  313. strcpy((char *)pkt, "octet");
  314. pkt += 5 /*strlen("octet")*/ + 1;
  315. strcpy((char *)pkt, "timeout");
  316. pkt += 7 /*strlen("timeout")*/ + 1;
  317. sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000);
  318. debug("send option \"timeout %s\"\n", (char *)pkt);
  319. pkt += strlen((char *)pkt) + 1;
  320. #ifdef CONFIG_TFTP_TSIZE
  321. pkt += sprintf((char *)pkt, "tsize%c%lu%c",
  322. 0, NetBootFileXferSize, 0);
  323. #endif
  324. /* try for more effic. blk size */
  325. pkt += sprintf((char *)pkt, "blksize%c%d%c",
  326. 0, TftpBlkSizeOption, 0);
  327. #ifdef CONFIG_MCAST_TFTP
  328. /* Check all preconditions before even trying the option */
  329. if (!ProhibitMcast) {
  330. Bitmap = malloc(Mapsize);
  331. if (Bitmap && eth_get_dev()->mcast) {
  332. free(Bitmap);
  333. Bitmap = NULL;
  334. pkt += sprintf((char *)pkt, "multicast%c%c",
  335. 0, 0);
  336. }
  337. }
  338. #endif /* CONFIG_MCAST_TFTP */
  339. len = pkt - xp;
  340. break;
  341. case STATE_OACK:
  342. #ifdef CONFIG_MCAST_TFTP
  343. /* My turn! Start at where I need blocks I missed.*/
  344. if (Multicast)
  345. TftpBlock = ext2_find_next_zero_bit(Bitmap,
  346. (Mapsize*8), 0);
  347. /*..falling..*/
  348. #endif
  349. case STATE_RECV_WRQ:
  350. case STATE_DATA:
  351. xp = pkt;
  352. s = (ushort *)pkt;
  353. s[0] = htons(TFTP_ACK);
  354. s[1] = htons(TftpBlock);
  355. pkt = (uchar *)(s + 2);
  356. #ifdef CONFIG_CMD_TFTPPUT
  357. if (TftpWriting) {
  358. int toload = TftpBlkSize;
  359. int loaded = load_block(TftpBlock, pkt, toload);
  360. s[0] = htons(TFTP_DATA);
  361. pkt += loaded;
  362. TftpFinalBlock = (loaded < toload);
  363. }
  364. #endif
  365. len = pkt - xp;
  366. break;
  367. case STATE_TOO_LARGE:
  368. xp = pkt;
  369. s = (ushort *)pkt;
  370. *s++ = htons(TFTP_ERROR);
  371. *s++ = htons(3);
  372. pkt = (uchar *)s;
  373. strcpy((char *)pkt, "File too large");
  374. pkt += 14 /*strlen("File too large")*/ + 1;
  375. len = pkt - xp;
  376. break;
  377. case STATE_BAD_MAGIC:
  378. xp = pkt;
  379. s = (ushort *)pkt;
  380. *s++ = htons(TFTP_ERROR);
  381. *s++ = htons(2);
  382. pkt = (uchar *)s;
  383. strcpy((char *)pkt, "File has bad magic");
  384. pkt += 18 /*strlen("File has bad magic")*/ + 1;
  385. len = pkt - xp;
  386. break;
  387. }
  388. NetSendUDPPacket(NetServerEther, TftpRemoteIP, TftpRemotePort,
  389. TftpOurPort, len);
  390. }
  391. #ifdef CONFIG_CMD_TFTPPUT
  392. static void icmp_handler(unsigned type, unsigned code, unsigned dest,
  393. IPaddr_t sip, unsigned src, uchar *pkt, unsigned len)
  394. {
  395. if (type == ICMP_NOT_REACH && code == ICMP_NOT_REACH_PORT) {
  396. /* Oh dear the other end has gone away */
  397. restart("TFTP server died");
  398. }
  399. }
  400. #endif
  401. static void
  402. TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
  403. unsigned len)
  404. {
  405. __be16 proto;
  406. __be16 *s;
  407. int i;
  408. if (dest != TftpOurPort) {
  409. #ifdef CONFIG_MCAST_TFTP
  410. if (Multicast
  411. && (!Mcast_port || (dest != Mcast_port)))
  412. #endif
  413. return;
  414. }
  415. if (TftpState != STATE_SEND_RRQ && src != TftpRemotePort &&
  416. TftpState != STATE_RECV_WRQ && TftpState != STATE_SEND_WRQ)
  417. return;
  418. if (len < 2)
  419. return;
  420. len -= 2;
  421. /* warning: don't use increment (++) in ntohs() macros!! */
  422. s = (__be16 *)pkt;
  423. proto = *s++;
  424. pkt = (uchar *)s;
  425. switch (ntohs(proto)) {
  426. case TFTP_RRQ:
  427. break;
  428. case TFTP_ACK:
  429. #ifdef CONFIG_CMD_TFTPPUT
  430. if (TftpWriting) {
  431. if (TftpFinalBlock) {
  432. tftp_complete();
  433. } else {
  434. /*
  435. * Move to the next block. We want our block
  436. * count to wrap just like the other end!
  437. */
  438. int block = ntohs(*s);
  439. int ack_ok = (TftpBlock == block);
  440. TftpBlock = (unsigned short)(block + 1);
  441. update_block_number();
  442. if (ack_ok)
  443. TftpSend(); /* Send next data block */
  444. }
  445. }
  446. #endif
  447. break;
  448. default:
  449. break;
  450. #ifdef CONFIG_CMD_TFTPSRV
  451. case TFTP_WRQ:
  452. debug("Got WRQ\n");
  453. TftpRemoteIP = sip;
  454. TftpRemotePort = src;
  455. TftpOurPort = 1024 + (get_timer(0) % 3072);
  456. new_transfer();
  457. TftpSend(); /* Send ACK(0) */
  458. break;
  459. #endif
  460. case TFTP_OACK:
  461. debug("Got OACK: %s %s\n",
  462. pkt,
  463. pkt + strlen((char *)pkt) + 1);
  464. TftpState = STATE_OACK;
  465. TftpRemotePort = src;
  466. /*
  467. * Check for 'blksize' option.
  468. * Careful: "i" is signed, "len" is unsigned, thus
  469. * something like "len-8" may give a *huge* number
  470. */
  471. for (i = 0; i+8 < len; i++) {
  472. if (strcmp((char *)pkt+i, "blksize") == 0) {
  473. TftpBlkSize = (unsigned short)
  474. simple_strtoul((char *)pkt+i+8, NULL,
  475. 10);
  476. debug("Blocksize ack: %s, %d\n",
  477. (char *)pkt+i+8, TftpBlkSize);
  478. }
  479. #ifdef CONFIG_TFTP_TSIZE
  480. if (strcmp((char *)pkt+i, "tsize") == 0) {
  481. TftpTsize = simple_strtoul((char *)pkt+i+6,
  482. NULL, 10);
  483. debug("size = %s, %d\n",
  484. (char *)pkt+i+6, TftpTsize);
  485. }
  486. #endif
  487. }
  488. #ifdef CONFIG_MCAST_TFTP
  489. parse_multicast_oack((char *)pkt, len-1);
  490. if ((Multicast) && (!MasterClient))
  491. TftpState = STATE_DATA; /* passive.. */
  492. else
  493. #endif
  494. #ifdef CONFIG_CMD_TFTPPUT
  495. if (TftpWriting) {
  496. /* Get ready to send the first block */
  497. TftpState = STATE_DATA;
  498. TftpBlock++;
  499. }
  500. #endif
  501. TftpSend(); /* Send ACK or first data block */
  502. break;
  503. case TFTP_DATA:
  504. if (len < 2)
  505. return;
  506. len -= 2;
  507. TftpBlock = ntohs(*(__be16 *)pkt);
  508. update_block_number();
  509. if (TftpState == STATE_SEND_RRQ)
  510. debug("Server did not acknowledge timeout option!\n");
  511. if (TftpState == STATE_SEND_RRQ || TftpState == STATE_OACK ||
  512. TftpState == STATE_RECV_WRQ) {
  513. /* first block received */
  514. TftpState = STATE_DATA;
  515. TftpRemotePort = src;
  516. new_transfer();
  517. #ifdef CONFIG_MCAST_TFTP
  518. if (Multicast) { /* start!=1 common if mcast */
  519. TftpLastBlock = TftpBlock - 1;
  520. } else
  521. #endif
  522. if (TftpBlock != 1) { /* Assertion */
  523. printf("\nTFTP error: "
  524. "First block is not block 1 (%ld)\n"
  525. "Starting again\n\n",
  526. TftpBlock);
  527. NetStartAgain();
  528. break;
  529. }
  530. }
  531. if (TftpBlock == TftpLastBlock) {
  532. /*
  533. * Same block again; ignore it.
  534. */
  535. break;
  536. }
  537. TftpLastBlock = TftpBlock;
  538. TftpTimeoutCountMax = TIMEOUT_COUNT;
  539. NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
  540. store_block(TftpBlock - 1, pkt + 2, len);
  541. /*
  542. * Acknowledge the block just received, which will prompt
  543. * the remote for the next one.
  544. */
  545. #ifdef CONFIG_MCAST_TFTP
  546. /* if I am the MasterClient, actively calculate what my next
  547. * needed block is; else I'm passive; not ACKING
  548. */
  549. if (Multicast) {
  550. if (len < TftpBlkSize) {
  551. TftpEndingBlock = TftpBlock;
  552. } else if (MasterClient) {
  553. TftpBlock = PrevBitmapHole =
  554. ext2_find_next_zero_bit(
  555. Bitmap,
  556. (Mapsize*8),
  557. PrevBitmapHole);
  558. if (TftpBlock > ((Mapsize*8) - 1)) {
  559. printf("tftpfile too big\n");
  560. /* try to double it and retry */
  561. Mapsize <<= 1;
  562. mcast_cleanup();
  563. NetStartAgain();
  564. return;
  565. }
  566. TftpLastBlock = TftpBlock;
  567. }
  568. }
  569. #endif
  570. TftpSend();
  571. #ifdef CONFIG_MCAST_TFTP
  572. if (Multicast) {
  573. if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
  574. puts("\nMulticast tftp done\n");
  575. mcast_cleanup();
  576. net_set_state(NETLOOP_SUCCESS);
  577. }
  578. } else
  579. #endif
  580. if (len < TftpBlkSize)
  581. tftp_complete();
  582. break;
  583. case TFTP_ERROR:
  584. printf("\nTFTP error: '%s' (%d)\n",
  585. pkt + 2, ntohs(*(__be16 *)pkt));
  586. switch (ntohs(*(__be16 *)pkt)) {
  587. case TFTP_ERR_FILE_NOT_FOUND:
  588. case TFTP_ERR_ACCESS_DENIED:
  589. puts("Not retrying...\n");
  590. eth_halt();
  591. net_set_state(NETLOOP_FAIL);
  592. break;
  593. case TFTP_ERR_UNDEFINED:
  594. case TFTP_ERR_DISK_FULL:
  595. case TFTP_ERR_UNEXPECTED_OPCODE:
  596. case TFTP_ERR_UNKNOWN_TRANSFER_ID:
  597. case TFTP_ERR_FILE_ALREADY_EXISTS:
  598. default:
  599. puts("Starting again\n\n");
  600. #ifdef CONFIG_MCAST_TFTP
  601. mcast_cleanup();
  602. #endif
  603. NetStartAgain();
  604. break;
  605. }
  606. break;
  607. }
  608. }
  609. static void
  610. TftpTimeout(void)
  611. {
  612. if (++TftpTimeoutCount > TftpTimeoutCountMax) {
  613. restart("Retry count exceeded");
  614. } else {
  615. puts("T ");
  616. NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
  617. if (TftpState != STATE_RECV_WRQ)
  618. TftpSend();
  619. }
  620. }
  621. void TftpStart(enum proto_t protocol)
  622. {
  623. char *ep; /* Environment pointer */
  624. /*
  625. * Allow the user to choose TFTP blocksize and timeout.
  626. * TFTP protocol has a minimal timeout of 1 second.
  627. */
  628. ep = getenv("tftpblocksize");
  629. if (ep != NULL)
  630. TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
  631. ep = getenv("tftptimeout");
  632. if (ep != NULL)
  633. TftpTimeoutMSecs = simple_strtol(ep, NULL, 10);
  634. if (TftpTimeoutMSecs < 1000) {
  635. printf("TFTP timeout (%ld ms) too low, "
  636. "set minimum = 1000 ms\n",
  637. TftpTimeoutMSecs);
  638. TftpTimeoutMSecs = 1000;
  639. }
  640. debug("TFTP blocksize = %i, timeout = %ld ms\n",
  641. TftpBlkSizeOption, TftpTimeoutMSecs);
  642. TftpRemoteIP = NetServerIP;
  643. if (BootFile[0] == '\0') {
  644. sprintf(default_filename, "%02X%02X%02X%02X.img",
  645. NetOurIP & 0xFF,
  646. (NetOurIP >> 8) & 0xFF,
  647. (NetOurIP >> 16) & 0xFF,
  648. (NetOurIP >> 24) & 0xFF);
  649. strncpy(tftp_filename, default_filename, MAX_LEN);
  650. tftp_filename[MAX_LEN-1] = 0;
  651. printf("*** Warning: no boot file name; using '%s'\n",
  652. tftp_filename);
  653. } else {
  654. char *p = strchr(BootFile, ':');
  655. if (p == NULL) {
  656. strncpy(tftp_filename, BootFile, MAX_LEN);
  657. tftp_filename[MAX_LEN-1] = 0;
  658. } else {
  659. TftpRemoteIP = string_to_ip(BootFile);
  660. strncpy(tftp_filename, p + 1, MAX_LEN);
  661. tftp_filename[MAX_LEN-1] = 0;
  662. }
  663. }
  664. printf("Using %s device\n", eth_get_name());
  665. printf("TFTP %s server %pI4; our IP address is %pI4",
  666. #ifdef CONFIG_CMD_TFTPPUT
  667. protocol == TFTPPUT ? "to" : "from",
  668. #else
  669. "from",
  670. #endif
  671. &TftpRemoteIP, &NetOurIP);
  672. /* Check if we need to send across this subnet */
  673. if (NetOurGatewayIP && NetOurSubnetMask) {
  674. IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
  675. IPaddr_t RemoteNet = TftpRemoteIP & NetOurSubnetMask;
  676. if (OurNet != RemoteNet)
  677. printf("; sending through gateway %pI4",
  678. &NetOurGatewayIP);
  679. }
  680. putc('\n');
  681. printf("Filename '%s'.", tftp_filename);
  682. if (NetBootFileSize) {
  683. printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
  684. print_size(NetBootFileSize<<9, "");
  685. }
  686. putc('\n');
  687. #ifdef CONFIG_CMD_TFTPPUT
  688. TftpWriting = (protocol == TFTPPUT);
  689. if (TftpWriting) {
  690. printf("Save address: 0x%lx\n", save_addr);
  691. printf("Save size: 0x%lx\n", save_size);
  692. NetBootFileXferSize = save_size;
  693. puts("Saving: *\b");
  694. TftpState = STATE_SEND_WRQ;
  695. new_transfer();
  696. } else
  697. #endif
  698. {
  699. printf("Load address: 0x%lx\n", load_addr);
  700. puts("Loading: *\b");
  701. TftpState = STATE_SEND_RRQ;
  702. }
  703. time_start = get_timer(0);
  704. TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
  705. NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
  706. net_set_udp_handler(TftpHandler);
  707. #ifdef CONFIG_CMD_TFTPPUT
  708. net_set_icmp_handler(icmp_handler);
  709. #endif
  710. TftpRemotePort = WELL_KNOWN_PORT;
  711. TftpTimeoutCount = 0;
  712. /* Use a pseudo-random port unless a specific port is set */
  713. TftpOurPort = 1024 + (get_timer(0) % 3072);
  714. #ifdef CONFIG_TFTP_PORT
  715. ep = getenv("tftpdstp");
  716. if (ep != NULL)
  717. TftpRemotePort = simple_strtol(ep, NULL, 10);
  718. ep = getenv("tftpsrcp");
  719. if (ep != NULL)
  720. TftpOurPort = simple_strtol(ep, NULL, 10);
  721. #endif
  722. TftpBlock = 0;
  723. /* zero out server ether in case the server ip has changed */
  724. memset(NetServerEther, 0, 6);
  725. /* Revert TftpBlkSize to dflt */
  726. TftpBlkSize = TFTP_BLOCK_SIZE;
  727. #ifdef CONFIG_MCAST_TFTP
  728. mcast_cleanup();
  729. #endif
  730. #ifdef CONFIG_TFTP_TSIZE
  731. TftpTsize = 0;
  732. TftpNumchars = 0;
  733. #endif
  734. TftpSend();
  735. }
  736. #ifdef CONFIG_CMD_TFTPSRV
  737. void
  738. TftpStartServer(void)
  739. {
  740. tftp_filename[0] = 0;
  741. printf("Using %s device\n", eth_get_name());
  742. printf("Listening for TFTP transfer on %pI4\n", &NetOurIP);
  743. printf("Load address: 0x%lx\n", load_addr);
  744. puts("Loading: *\b");
  745. TftpTimeoutCountMax = TIMEOUT_COUNT;
  746. TftpTimeoutCount = 0;
  747. TftpTimeoutMSecs = TIMEOUT;
  748. NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
  749. /* Revert TftpBlkSize to dflt */
  750. TftpBlkSize = TFTP_BLOCK_SIZE;
  751. TftpBlock = 0;
  752. TftpOurPort = WELL_KNOWN_PORT;
  753. #ifdef CONFIG_TFTP_TSIZE
  754. TftpTsize = 0;
  755. TftpNumchars = 0;
  756. #endif
  757. TftpState = STATE_RECV_WRQ;
  758. net_set_udp_handler(TftpHandler);
  759. }
  760. #endif /* CONFIG_CMD_TFTPSRV */
  761. #ifdef CONFIG_MCAST_TFTP
  762. /* Credits: atftp project.
  763. */
  764. /* pick up BcastAddr, Port, and whether I am [now] the master-client. *
  765. * Frame:
  766. * +-------+-----------+---+-------~~-------+---+
  767. * | opc | multicast | 0 | addr, port, mc | 0 |
  768. * +-------+-----------+---+-------~~-------+---+
  769. * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
  770. * I am the new master-client so must send ACKs to DataBlocks. If I am not
  771. * master-client, I'm a passive client, gathering what DataBlocks I may and
  772. * making note of which ones I got in my bitmask.
  773. * In theory, I never go from master->passive..
  774. * .. this comes in with pkt already pointing just past opc
  775. */
  776. static void parse_multicast_oack(char *pkt, int len)
  777. {
  778. int i;
  779. IPaddr_t addr;
  780. char *mc_adr, *port, *mc;
  781. mc_adr = port = mc = NULL;
  782. /* march along looking for 'multicast\0', which has to start at least
  783. * 14 bytes back from the end.
  784. */
  785. for (i = 0; i < len-14; i++)
  786. if (strcmp(pkt+i, "multicast") == 0)
  787. break;
  788. if (i >= (len-14)) /* non-Multicast OACK, ign. */
  789. return;
  790. i += 10; /* strlen multicast */
  791. mc_adr = pkt+i;
  792. for (; i < len; i++) {
  793. if (*(pkt+i) == ',') {
  794. *(pkt+i) = '\0';
  795. if (port) {
  796. mc = pkt+i+1;
  797. break;
  798. } else {
  799. port = pkt+i+1;
  800. }
  801. }
  802. }
  803. if (!port || !mc_adr || !mc)
  804. return;
  805. if (Multicast && MasterClient) {
  806. printf("I got a OACK as master Client, WRONG!\n");
  807. return;
  808. }
  809. /* ..I now accept packets destined for this MCAST addr, port */
  810. if (!Multicast) {
  811. if (Bitmap) {
  812. printf("Internal failure! no mcast.\n");
  813. free(Bitmap);
  814. Bitmap = NULL;
  815. ProhibitMcast = 1;
  816. return ;
  817. }
  818. /* I malloc instead of pre-declare; so that if the file ends
  819. * up being too big for this bitmap I can retry
  820. */
  821. Bitmap = malloc(Mapsize);
  822. if (!Bitmap) {
  823. printf("No Bitmap, no multicast. Sorry.\n");
  824. ProhibitMcast = 1;
  825. return;
  826. }
  827. memset(Bitmap, 0, Mapsize);
  828. PrevBitmapHole = 0;
  829. Multicast = 1;
  830. }
  831. addr = string_to_ip(mc_adr);
  832. if (Mcast_addr != addr) {
  833. if (Mcast_addr)
  834. eth_mcast_join(Mcast_addr, 0);
  835. Mcast_addr = addr;
  836. if (eth_mcast_join(Mcast_addr, 1)) {
  837. printf("Fail to set mcast, revert to TFTP\n");
  838. ProhibitMcast = 1;
  839. mcast_cleanup();
  840. NetStartAgain();
  841. }
  842. }
  843. MasterClient = (unsigned char)simple_strtoul((char *)mc, NULL, 10);
  844. Mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
  845. printf("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
  846. return;
  847. }
  848. #endif /* Multicast TFTP */