tftp.c 22 KB

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