tftp.c 22 KB

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