tftp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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. static 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. #ifdef CONFIG_CMD_TFTPPUT
  300. *s++ = htons(TftpState == STATE_SEND_RRQ ? TFTP_RRQ :
  301. TFTP_WRQ);
  302. #else
  303. *s++ = htons(TFTP_RRQ);
  304. #endif
  305. pkt = (uchar *)s;
  306. strcpy((char *)pkt, tftp_filename);
  307. pkt += strlen(tftp_filename) + 1;
  308. strcpy((char *)pkt, "octet");
  309. pkt += 5 /*strlen("octet")*/ + 1;
  310. strcpy((char *)pkt, "timeout");
  311. pkt += 7 /*strlen("timeout")*/ + 1;
  312. sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000);
  313. debug("send option \"timeout %s\"\n", (char *)pkt);
  314. pkt += strlen((char *)pkt) + 1;
  315. #ifdef CONFIG_TFTP_TSIZE
  316. pkt += sprintf((char *)pkt, "tsize%c%lu%c",
  317. 0, NetBootFileXferSize, 0);
  318. #endif
  319. /* try for more effic. blk size */
  320. pkt += sprintf((char *)pkt, "blksize%c%d%c",
  321. 0, TftpBlkSizeOption, 0);
  322. #ifdef CONFIG_MCAST_TFTP
  323. /* Check all preconditions before even trying the option */
  324. if (!ProhibitMcast
  325. && (Bitmap = malloc(Mapsize))
  326. && eth_get_dev()->mcast) {
  327. free(Bitmap);
  328. Bitmap = NULL;
  329. pkt += sprintf((char *)pkt, "multicast%c%c", 0, 0);
  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. NetState = NETLOOP_SUCCESS;
  570. }
  571. }
  572. else
  573. #endif
  574. if (len < TftpBlkSize)
  575. tftp_complete();
  576. break;
  577. case TFTP_ERROR:
  578. printf("\nTFTP error: '%s' (%d)\n",
  579. pkt + 2, ntohs(*(ushort *)pkt));
  580. switch (ntohs(*(ushort *)pkt)) {
  581. case TFTP_ERR_FILE_NOT_FOUND:
  582. case TFTP_ERR_ACCESS_DENIED:
  583. puts("Not retrying...\n");
  584. eth_halt();
  585. NetState = NETLOOP_FAIL;
  586. break;
  587. case TFTP_ERR_UNDEFINED:
  588. case TFTP_ERR_DISK_FULL:
  589. case TFTP_ERR_UNEXPECTED_OPCODE:
  590. case TFTP_ERR_UNKNOWN_TRANSFER_ID:
  591. case TFTP_ERR_FILE_ALREADY_EXISTS:
  592. default:
  593. puts("Starting again\n\n");
  594. #ifdef CONFIG_MCAST_TFTP
  595. mcast_cleanup();
  596. #endif
  597. NetStartAgain();
  598. break;
  599. }
  600. break;
  601. }
  602. }
  603. static void
  604. TftpTimeout(void)
  605. {
  606. if (++TftpTimeoutCount > TftpTimeoutCountMax) {
  607. restart("Retry count exceeded");
  608. } else {
  609. puts("T ");
  610. NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
  611. if (TftpState != STATE_RECV_WRQ)
  612. TftpSend();
  613. }
  614. }
  615. void TftpStart(enum proto_t protocol)
  616. {
  617. char *ep; /* Environment pointer */
  618. /*
  619. * Allow the user to choose TFTP blocksize and timeout.
  620. * TFTP protocol has a minimal timeout of 1 second.
  621. */
  622. ep = getenv("tftpblocksize");
  623. if (ep != NULL)
  624. TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
  625. ep = getenv("tftptimeout");
  626. if (ep != NULL)
  627. TftpTimeoutMSecs = simple_strtol(ep, NULL, 10);
  628. if (TftpTimeoutMSecs < 1000) {
  629. printf("TFTP timeout (%ld ms) too low, "
  630. "set minimum = 1000 ms\n",
  631. TftpTimeoutMSecs);
  632. TftpTimeoutMSecs = 1000;
  633. }
  634. debug("TFTP blocksize = %i, timeout = %ld ms\n",
  635. TftpBlkSizeOption, TftpTimeoutMSecs);
  636. TftpRemoteIP = NetServerIP;
  637. if (BootFile[0] == '\0') {
  638. sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
  639. NetOurIP & 0xFF,
  640. (NetOurIP >> 8) & 0xFF,
  641. (NetOurIP >> 16) & 0xFF,
  642. (NetOurIP >> 24) & 0xFF);
  643. strncpy(tftp_filename, default_filename, MAX_LEN);
  644. tftp_filename[MAX_LEN-1] = 0;
  645. printf("*** Warning: no boot file name; using '%s'\n",
  646. tftp_filename);
  647. } else {
  648. char *p = strchr(BootFile, ':');
  649. if (p == NULL) {
  650. strncpy(tftp_filename, BootFile, MAX_LEN);
  651. tftp_filename[MAX_LEN-1] = 0;
  652. } else {
  653. TftpRemoteIP = string_to_ip(BootFile);
  654. strncpy(tftp_filename, p + 1, MAX_LEN);
  655. tftp_filename[MAX_LEN-1] = 0;
  656. }
  657. }
  658. printf("Using %s device\n", eth_get_name());
  659. printf("TFTP %s server %pI4; our IP address is %pI4",
  660. #ifdef CONFIG_CMD_TFTPPUT
  661. protocol == TFTPPUT ? "to" : "from",
  662. #else
  663. "from",
  664. #endif
  665. &TftpRemoteIP, &NetOurIP);
  666. /* Check if we need to send across this subnet */
  667. if (NetOurGatewayIP && NetOurSubnetMask) {
  668. IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
  669. IPaddr_t RemoteNet = TftpRemoteIP & NetOurSubnetMask;
  670. if (OurNet != RemoteNet)
  671. printf("; sending through gateway %pI4",
  672. &NetOurGatewayIP);
  673. }
  674. putc('\n');
  675. printf("Filename '%s'.", tftp_filename);
  676. if (NetBootFileSize) {
  677. printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
  678. print_size(NetBootFileSize<<9, "");
  679. }
  680. putc('\n');
  681. #ifdef CONFIG_CMD_TFTPPUT
  682. TftpWriting = (protocol == TFTPPUT);
  683. if (TftpWriting) {
  684. printf("Save address: 0x%lx\n", save_addr);
  685. printf("Save size: 0x%lx\n", save_size);
  686. NetBootFileXferSize = save_size;
  687. puts("Saving: *\b");
  688. TftpState = STATE_SEND_WRQ;
  689. new_transfer();
  690. } else
  691. #endif
  692. {
  693. printf("Load address: 0x%lx\n", load_addr);
  694. puts("Loading: *\b");
  695. TftpState = STATE_SEND_RRQ;
  696. }
  697. TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
  698. NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
  699. NetSetHandler(TftpHandler);
  700. #ifdef CONFIG_CMD_TFTPPUT
  701. net_set_icmp_handler(icmp_handler);
  702. #endif
  703. TftpRemotePort = WELL_KNOWN_PORT;
  704. TftpTimeoutCount = 0;
  705. /* Use a pseudo-random port unless a specific port is set */
  706. TftpOurPort = 1024 + (get_timer(0) % 3072);
  707. #ifdef CONFIG_TFTP_PORT
  708. ep = getenv("tftpdstp");
  709. if (ep != NULL)
  710. TftpRemotePort = simple_strtol(ep, NULL, 10);
  711. ep = getenv("tftpsrcp");
  712. if (ep != NULL)
  713. TftpOurPort = simple_strtol(ep, NULL, 10);
  714. #endif
  715. TftpBlock = 0;
  716. /* zero out server ether in case the server ip has changed */
  717. memset(NetServerEther, 0, 6);
  718. /* Revert TftpBlkSize to dflt */
  719. TftpBlkSize = TFTP_BLOCK_SIZE;
  720. #ifdef CONFIG_MCAST_TFTP
  721. mcast_cleanup();
  722. #endif
  723. #ifdef CONFIG_TFTP_TSIZE
  724. TftpTsize = 0;
  725. TftpNumchars = 0;
  726. #endif
  727. TftpSend();
  728. }
  729. #ifdef CONFIG_CMD_TFTPSRV
  730. void
  731. TftpStartServer(void)
  732. {
  733. tftp_filename[0] = 0;
  734. printf("Using %s device\n", eth_get_name());
  735. printf("Listening for TFTP transfer on %pI4\n", &NetOurIP);
  736. printf("Load address: 0x%lx\n", load_addr);
  737. puts("Loading: *\b");
  738. TftpTimeoutCountMax = TIMEOUT_COUNT;
  739. TftpTimeoutCount = 0;
  740. TftpTimeoutMSecs = TIMEOUT;
  741. NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
  742. /* Revert TftpBlkSize to dflt */
  743. TftpBlkSize = TFTP_BLOCK_SIZE;
  744. TftpBlock = 0;
  745. TftpOurPort = WELL_KNOWN_PORT;
  746. #ifdef CONFIG_TFTP_TSIZE
  747. TftpTsize = 0;
  748. TftpNumchars = 0;
  749. #endif
  750. TftpState = STATE_RECV_WRQ;
  751. NetSetHandler(TftpHandler);
  752. }
  753. #endif /* CONFIG_CMD_TFTPSRV */
  754. #ifdef CONFIG_MCAST_TFTP
  755. /* Credits: atftp project.
  756. */
  757. /* pick up BcastAddr, Port, and whether I am [now] the master-client. *
  758. * Frame:
  759. * +-------+-----------+---+-------~~-------+---+
  760. * | opc | multicast | 0 | addr, port, mc | 0 |
  761. * +-------+-----------+---+-------~~-------+---+
  762. * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
  763. * I am the new master-client so must send ACKs to DataBlocks. If I am not
  764. * master-client, I'm a passive client, gathering what DataBlocks I may and
  765. * making note of which ones I got in my bitmask.
  766. * In theory, I never go from master->passive..
  767. * .. this comes in with pkt already pointing just past opc
  768. */
  769. static void parse_multicast_oack(char *pkt, int len)
  770. {
  771. int i;
  772. IPaddr_t addr;
  773. char *mc_adr, *port, *mc;
  774. mc_adr = port = mc = NULL;
  775. /* march along looking for 'multicast\0', which has to start at least
  776. * 14 bytes back from the end.
  777. */
  778. for (i = 0; i < len-14; i++)
  779. if (strcmp(pkt+i, "multicast") == 0)
  780. break;
  781. if (i >= (len-14)) /* non-Multicast OACK, ign. */
  782. return;
  783. i += 10; /* strlen multicast */
  784. mc_adr = pkt+i;
  785. for (; i < len; i++) {
  786. if (*(pkt+i) == ',') {
  787. *(pkt+i) = '\0';
  788. if (port) {
  789. mc = pkt+i+1;
  790. break;
  791. } else {
  792. port = pkt+i+1;
  793. }
  794. }
  795. }
  796. if (!port || !mc_adr || !mc)
  797. return;
  798. if (Multicast && MasterClient) {
  799. printf("I got a OACK as master Client, WRONG!\n");
  800. return;
  801. }
  802. /* ..I now accept packets destined for this MCAST addr, port */
  803. if (!Multicast) {
  804. if (Bitmap) {
  805. printf("Internal failure! no mcast.\n");
  806. free(Bitmap);
  807. Bitmap = NULL;
  808. ProhibitMcast = 1;
  809. return ;
  810. }
  811. /* I malloc instead of pre-declare; so that if the file ends
  812. * up being too big for this bitmap I can retry
  813. */
  814. Bitmap = malloc(Mapsize);
  815. if (!Bitmap) {
  816. printf("No Bitmap, no multicast. Sorry.\n");
  817. ProhibitMcast = 1;
  818. return;
  819. }
  820. memset(Bitmap, 0, Mapsize);
  821. PrevBitmapHole = 0;
  822. Multicast = 1;
  823. }
  824. addr = string_to_ip(mc_adr);
  825. if (Mcast_addr != addr) {
  826. if (Mcast_addr)
  827. eth_mcast_join(Mcast_addr, 0);
  828. Mcast_addr = addr;
  829. if (eth_mcast_join(Mcast_addr, 1)) {
  830. printf("Fail to set mcast, revert to TFTP\n");
  831. ProhibitMcast = 1;
  832. mcast_cleanup();
  833. NetStartAgain();
  834. }
  835. }
  836. MasterClient = (unsigned char)simple_strtoul((char *)mc, NULL, 10);
  837. Mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
  838. printf("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
  839. return;
  840. }
  841. #endif /* Multicast TFTP */