tftp.c 23 KB

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