tftp.c 22 KB

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