You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1467 lines
36 KiB

  1. /*
  2. * Copyright (C) 2009 by Simon Qian
  3. * SimonQian@SimonQian.com
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /* The specification for SVF is available here:
  20. * http://www.asset-intertech.com/support/svf.pdf
  21. * Below, this document is refered to as the "SVF spec".
  22. *
  23. * The specification for XSVF is available here:
  24. * http://www.xilinx.com/support/documentation/application_notes/xapp503.pdf
  25. * Below, this document is refered to as the "XSVF spec".
  26. */
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include "jtag.h"
  31. #include "svf.h"
  32. #include "time_support.h"
  33. // SVF command
  34. typedef enum
  35. {
  36. ENDDR,
  37. ENDIR,
  38. FREQUENCY,
  39. HDR,
  40. HIR,
  41. PIO,
  42. PIOMAP,
  43. RUNTEST,
  44. SDR,
  45. SIR,
  46. STATE,
  47. TDR,
  48. TIR,
  49. TRST,
  50. }svf_command_t;
  51. static const char *svf_command_name[14] =
  52. {
  53. "ENDDR",
  54. "ENDIR",
  55. "FREQUENCY",
  56. "HDR",
  57. "HIR",
  58. "PIO",
  59. "PIOMAP",
  60. "RUNTEST",
  61. "SDR",
  62. "SIR",
  63. "STATE",
  64. "TDR",
  65. "TIR",
  66. "TRST"
  67. };
  68. typedef enum
  69. {
  70. TRST_ON,
  71. TRST_OFF,
  72. TRST_Z,
  73. TRST_ABSENT
  74. }trst_mode_t;
  75. static const char *svf_trst_mode_name[4] =
  76. {
  77. "ON",
  78. "OFF",
  79. "Z",
  80. "ABSENT"
  81. };
  82. typedef struct
  83. {
  84. tap_state_t from;
  85. tap_state_t to;
  86. uint32_t num_of_moves;
  87. tap_state_t paths[8];
  88. }svf_statemove_t;
  89. /*
  90. * These paths are from the SVF specification for the STATE command, to be
  91. * used when the STATE command only includes the final state. The first
  92. * element of the path is the "from" (current) state, and the last one is
  93. * the "to" (target) state.
  94. *
  95. * All specified paths are the shortest ones in the JTAG spec, and are thus
  96. * not (!!) exact matches for the paths used elsewhere in OpenOCD. Note
  97. * that PAUSE-to-PAUSE transitions all go through UPDATE and then CAPTURE,
  98. * which has specific effects on the various registers; they are not NOPs.
  99. *
  100. * Paths to RESET are disabled here. As elsewhere in OpenOCD, and in XSVF
  101. * and many SVF implementations, we don't want to risk missing that state.
  102. * To get to RESET, always we ignore the current state.
  103. */
  104. static const svf_statemove_t svf_statemoves[] =
  105. {
  106. // from to num_of_moves, paths[8]
  107. // {TAP_RESET, TAP_RESET, 1, {TAP_RESET}},
  108. {TAP_RESET, TAP_IDLE, 2, {TAP_RESET, TAP_IDLE}},
  109. {TAP_RESET, TAP_DRPAUSE, 6, {TAP_RESET, TAP_IDLE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DREXIT1, TAP_DRPAUSE}},
  110. {TAP_RESET, TAP_IRPAUSE, 7, {TAP_RESET, TAP_IDLE, TAP_DRSELECT, TAP_IRSELECT, TAP_IRCAPTURE, TAP_IREXIT1, TAP_IRPAUSE}},
  111. // {TAP_IDLE, TAP_RESET, 4, {TAP_IDLE, TAP_DRSELECT, TAP_IRSELECT, TAP_RESET}},
  112. {TAP_IDLE, TAP_IDLE, 1, {TAP_IDLE}},
  113. {TAP_IDLE, TAP_DRPAUSE, 5, {TAP_IDLE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DREXIT1, TAP_DRPAUSE}},
  114. {TAP_IDLE, TAP_IRPAUSE, 6, {TAP_IDLE, TAP_DRSELECT, TAP_IRSELECT, TAP_IRCAPTURE, TAP_IREXIT1, TAP_IRPAUSE}},
  115. // {TAP_DRPAUSE, TAP_RESET, 6, {TAP_DRPAUSE, TAP_DREXIT2, TAP_DRUPDATE, TAP_DRSELECT, TAP_IRSELECT, TAP_RESET}},
  116. {TAP_DRPAUSE, TAP_IDLE, 4, {TAP_DRPAUSE, TAP_DREXIT2, TAP_DRUPDATE, TAP_IDLE}},
  117. {TAP_DRPAUSE, TAP_DRPAUSE, 7, {TAP_DRPAUSE, TAP_DREXIT2, TAP_DRUPDATE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DREXIT1, TAP_DRPAUSE}},
  118. {TAP_DRPAUSE, TAP_IRPAUSE, 8, {TAP_DRPAUSE, TAP_DREXIT2, TAP_DRUPDATE, TAP_DRSELECT, TAP_IRSELECT, TAP_IRCAPTURE, TAP_IREXIT1, TAP_IRPAUSE}},
  119. // {TAP_IRPAUSE, TAP_RESET, 6, {TAP_IRPAUSE, TAP_IREXIT2, TAP_IRUPDATE, TAP_DRSELECT, TAP_IRSELECT, TAP_RESET}},
  120. {TAP_IRPAUSE, TAP_IDLE, 4, {TAP_IRPAUSE, TAP_IREXIT2, TAP_IRUPDATE, TAP_IDLE}},
  121. {TAP_IRPAUSE, TAP_DRPAUSE, 7, {TAP_IRPAUSE, TAP_IREXIT2, TAP_IRUPDATE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DREXIT1, TAP_DRPAUSE}},
  122. {TAP_IRPAUSE, TAP_IRPAUSE, 8, {TAP_IRPAUSE, TAP_IREXIT2, TAP_IRUPDATE, TAP_DRSELECT, TAP_IRSELECT, TAP_IRCAPTURE, TAP_IREXIT1, TAP_IRPAUSE}}
  123. };
  124. #define XXR_TDI (1 << 0)
  125. #define XXR_TDO (1 << 1)
  126. #define XXR_MASK (1 << 2)
  127. #define XXR_SMASK (1 << 3)
  128. typedef struct
  129. {
  130. int len;
  131. int data_mask;
  132. uint8_t *tdi;
  133. uint8_t *tdo;
  134. uint8_t *mask;
  135. uint8_t *smask;
  136. }svf_xxr_para_t;
  137. typedef struct
  138. {
  139. float frequency;
  140. tap_state_t ir_end_state;
  141. tap_state_t dr_end_state;
  142. tap_state_t runtest_run_state;
  143. tap_state_t runtest_end_state;
  144. trst_mode_t trst_mode;
  145. svf_xxr_para_t hir_para;
  146. svf_xxr_para_t hdr_para;
  147. svf_xxr_para_t tir_para;
  148. svf_xxr_para_t tdr_para;
  149. svf_xxr_para_t sir_para;
  150. svf_xxr_para_t sdr_para;
  151. }svf_para_t;
  152. static svf_para_t svf_para;
  153. static const svf_para_t svf_para_init =
  154. {
  155. // frequency, ir_end_state, dr_end_state, runtest_run_state, runtest_end_state, trst_mode
  156. 0, TAP_IDLE, TAP_IDLE, TAP_IDLE, TAP_IDLE, TRST_Z,
  157. // hir_para
  158. // {len, data_mask, tdi, tdo, mask, smask},
  159. {0, 0, NULL, NULL, NULL, NULL},
  160. // hdr_para
  161. // {len, data_mask, tdi, tdo, mask, smask},
  162. {0, 0, NULL, NULL, NULL, NULL},
  163. // tir_para
  164. // {len, data_mask, tdi, tdo, mask, smask},
  165. {0, 0, NULL, NULL, NULL, NULL},
  166. // tdr_para
  167. // {len, data_mask, tdi, tdo, mask, smask},
  168. {0, 0, NULL, NULL, NULL, NULL},
  169. // sir_para
  170. // {len, data_mask, tdi, tdo, mask, smask},
  171. {0, 0, NULL, NULL, NULL, NULL},
  172. // sdr_para
  173. // {len, data_mask, tdi, tdo, mask, smask},
  174. {0, 0, NULL, NULL, NULL, NULL},
  175. };
  176. typedef struct
  177. {
  178. int line_num; // used to record line number of the check operation
  179. // so more information could be printed
  180. int enabled; // check is enabled or not
  181. int buffer_offset; // buffer_offset to buffers
  182. int bit_len; // bit length to check
  183. }svf_check_tdo_para_t;
  184. #define SVF_CHECK_TDO_PARA_SIZE 1024
  185. static svf_check_tdo_para_t *svf_check_tdo_para = NULL;
  186. static int svf_check_tdo_para_index = 0;
  187. static int svf_read_command_from_file(int fd);
  188. static int svf_check_tdo(void);
  189. static int svf_add_check_para(uint8_t enabled, int buffer_offset, int bit_len);
  190. static int svf_run_command(struct command_context_s *cmd_ctx, char *cmd_str);
  191. static int handle_svf_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  192. static int svf_fd = 0;
  193. static char *svf_command_buffer = NULL;
  194. static int svf_command_buffer_size = 0;
  195. static int svf_line_number = 1;
  196. static jtag_tap_t *tap = NULL;
  197. #define SVF_MAX_BUFFER_SIZE_TO_COMMIT (4 * 1024)
  198. static uint8_t *svf_tdi_buffer = NULL, *svf_tdo_buffer = NULL, *svf_mask_buffer = NULL;
  199. static int svf_buffer_index = 0, svf_buffer_size = 0;
  200. static int svf_quiet = 0;
  201. int svf_register_commands(struct command_context_s *cmd_ctx)
  202. {
  203. register_command(cmd_ctx, NULL, "svf", handle_svf_command,
  204. COMMAND_EXEC, "run svf <file>");
  205. return ERROR_OK;
  206. }
  207. static void svf_free_xxd_para(svf_xxr_para_t *para)
  208. {
  209. if (NULL != para)
  210. {
  211. if (para->tdi != NULL)
  212. {
  213. free(para->tdi);
  214. para->tdi = NULL;
  215. }
  216. if (para->tdo != NULL)
  217. {
  218. free(para->tdo);
  219. para->tdo = NULL;
  220. }
  221. if (para->mask != NULL)
  222. {
  223. free(para->mask);
  224. para->mask = NULL;
  225. }
  226. if (para->smask != NULL)
  227. {
  228. free(para->smask);
  229. para->smask = NULL;
  230. }
  231. }
  232. }
  233. static unsigned svf_get_mask_u32(int bitlen)
  234. {
  235. uint32_t bitmask;
  236. if (bitlen < 0)
  237. {
  238. bitmask = 0;
  239. }
  240. else if (bitlen >= 32)
  241. {
  242. bitmask = 0xFFFFFFFF;
  243. }
  244. else
  245. {
  246. bitmask = (1 << bitlen) - 1;
  247. }
  248. return bitmask;
  249. }
  250. int svf_add_statemove(tap_state_t state_to)
  251. {
  252. tap_state_t state_from = cmd_queue_cur_state;
  253. uint8_t index;
  254. /* when resetting, be paranoid and ignore current state */
  255. if (state_to == TAP_RESET) {
  256. jtag_add_tlr();
  257. return ERROR_OK;
  258. }
  259. for (index = 0; index < DIM(svf_statemoves); index++)
  260. {
  261. if ((svf_statemoves[index].from == state_from)
  262. && (svf_statemoves[index].to == state_to))
  263. {
  264. /* recorded path includes current state ... avoid extra TCKs! */
  265. if (svf_statemoves[index].num_of_moves > 1)
  266. jtag_add_pathmove(svf_statemoves[index].num_of_moves - 1,
  267. svf_statemoves[index].paths + 1);
  268. else
  269. jtag_add_pathmove(svf_statemoves[index].num_of_moves,
  270. svf_statemoves[index].paths);
  271. return ERROR_OK;
  272. }
  273. }
  274. LOG_ERROR("SVF: can not move to %s", tap_state_name(state_to));
  275. return ERROR_FAIL;
  276. }
  277. static int handle_svf_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  278. {
  279. #define SVF_NUM_OF_OPTIONS 1
  280. int command_num = 0, i;
  281. int ret = ERROR_OK;
  282. long long time_ago;
  283. if ((argc < 1) || (argc > (1 + SVF_NUM_OF_OPTIONS)))
  284. {
  285. command_print(cmd_ctx, "usage: svf <file> [quiet]");
  286. return ERROR_FAIL;
  287. }
  288. // parse variant
  289. svf_quiet = 0;
  290. for (i = 1; i < argc; i++)
  291. {
  292. if (!strcmp(args[i], "quiet"))
  293. {
  294. svf_quiet = 1;
  295. }
  296. else
  297. {
  298. LOG_ERROR("unknown variant for svf: %s", args[i]);
  299. // no need to free anything now
  300. return ERROR_FAIL;
  301. }
  302. }
  303. if ((svf_fd = open(args[0], O_RDONLY)) < 0)
  304. {
  305. command_print(cmd_ctx, "file \"%s\" not found", args[0]);
  306. // no need to free anything now
  307. return ERROR_FAIL;
  308. }
  309. LOG_USER("svf processing file: \"%s\"", args[0]);
  310. // get time
  311. time_ago = timeval_ms();
  312. // init
  313. svf_line_number = 1;
  314. svf_command_buffer_size = 0;
  315. svf_check_tdo_para_index = 0;
  316. svf_check_tdo_para = malloc(sizeof(svf_check_tdo_para_t) * SVF_CHECK_TDO_PARA_SIZE);
  317. if (NULL == svf_check_tdo_para)
  318. {
  319. LOG_ERROR("not enough memory");
  320. ret = ERROR_FAIL;
  321. goto free_all;
  322. }
  323. svf_buffer_index = 0;
  324. // double the buffer size
  325. // in case current command cannot be commited, and next command is a bit scan command
  326. // here is 32K bits for this big scan command, it should be enough
  327. // buffer will be reallocated if buffer size is not enough
  328. svf_tdi_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
  329. if (NULL == svf_tdi_buffer)
  330. {
  331. LOG_ERROR("not enough memory");
  332. ret = ERROR_FAIL;
  333. goto free_all;
  334. }
  335. svf_tdo_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
  336. if (NULL == svf_tdo_buffer)
  337. {
  338. LOG_ERROR("not enough memory");
  339. ret = ERROR_FAIL;
  340. goto free_all;
  341. }
  342. svf_mask_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
  343. if (NULL == svf_mask_buffer)
  344. {
  345. LOG_ERROR("not enough memory");
  346. ret = ERROR_FAIL;
  347. goto free_all;
  348. }
  349. svf_buffer_size = 2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT;
  350. memcpy(&svf_para, &svf_para_init, sizeof(svf_para));
  351. // TAP_RESET
  352. jtag_add_tlr();
  353. while (ERROR_OK == svf_read_command_from_file(svf_fd))
  354. {
  355. if (ERROR_OK != svf_run_command(cmd_ctx, svf_command_buffer))
  356. {
  357. LOG_ERROR("fail to run command at line %d", svf_line_number);
  358. ret = ERROR_FAIL;
  359. break;
  360. }
  361. command_num++;
  362. }
  363. if (ERROR_OK != jtag_execute_queue())
  364. {
  365. ret = ERROR_FAIL;
  366. }
  367. else if (ERROR_OK != svf_check_tdo())
  368. {
  369. ret = ERROR_FAIL;
  370. }
  371. // print time
  372. command_print(cmd_ctx, "%lld ms used", timeval_ms() - time_ago);
  373. free_all:
  374. close(svf_fd);
  375. svf_fd = 0;
  376. // free buffers
  377. if (svf_command_buffer)
  378. {
  379. free(svf_command_buffer);
  380. svf_command_buffer = NULL;
  381. svf_command_buffer_size = 0;
  382. }
  383. if (svf_check_tdo_para)
  384. {
  385. free(svf_check_tdo_para);
  386. svf_check_tdo_para = NULL;
  387. svf_check_tdo_para_index = 0;
  388. }
  389. if (svf_tdi_buffer)
  390. {
  391. free(svf_tdi_buffer);
  392. svf_tdi_buffer = NULL;
  393. }
  394. if (svf_tdo_buffer)
  395. {
  396. free(svf_tdo_buffer);
  397. svf_tdo_buffer = NULL;
  398. }
  399. if (svf_mask_buffer)
  400. {
  401. free(svf_mask_buffer);
  402. svf_mask_buffer = NULL;
  403. }
  404. svf_buffer_index = 0;
  405. svf_buffer_size = 0;
  406. svf_free_xxd_para(&svf_para.hdr_para);
  407. svf_free_xxd_para(&svf_para.hir_para);
  408. svf_free_xxd_para(&svf_para.tdr_para);
  409. svf_free_xxd_para(&svf_para.tir_para);
  410. svf_free_xxd_para(&svf_para.sdr_para);
  411. svf_free_xxd_para(&svf_para.sir_para);
  412. if (ERROR_OK == ret)
  413. {
  414. command_print(cmd_ctx, "svf file programmed successfully for %d commands", command_num);
  415. }
  416. else
  417. {
  418. command_print(cmd_ctx, "svf file programmed failed");
  419. }
  420. return ret;
  421. }
  422. #define SVFP_CMD_INC_CNT 1024
  423. static int svf_read_command_from_file(int fd)
  424. {
  425. char ch, *tmp_buffer = NULL;
  426. int cmd_pos = 0, cmd_ok = 0, slash = 0, comment = 0;
  427. while (!cmd_ok && (read(fd, &ch, 1) > 0))
  428. {
  429. switch (ch)
  430. {
  431. case '!':
  432. slash = 0;
  433. comment = 1;
  434. break;
  435. case '/':
  436. if (++slash == 2)
  437. {
  438. comment = 1;
  439. }
  440. break;
  441. case ';':
  442. slash = 0;
  443. if (!comment)
  444. {
  445. cmd_ok = 1;
  446. }
  447. break;
  448. case '\n':
  449. svf_line_number++;
  450. case '\r':
  451. slash = 0;
  452. comment = 0;
  453. break;
  454. default:
  455. if (!comment)
  456. {
  457. if (cmd_pos >= svf_command_buffer_size - 1)
  458. {
  459. tmp_buffer = (char*)malloc(svf_command_buffer_size + SVFP_CMD_INC_CNT); // 1 more byte for '\0'
  460. if (NULL == tmp_buffer)
  461. {
  462. LOG_ERROR("not enough memory");
  463. return ERROR_FAIL;
  464. }
  465. if (svf_command_buffer_size > 0)
  466. {
  467. memcpy(tmp_buffer, svf_command_buffer, svf_command_buffer_size);
  468. }
  469. if (svf_command_buffer != NULL)
  470. {
  471. free(svf_command_buffer);
  472. }
  473. svf_command_buffer = tmp_buffer;
  474. svf_command_buffer_size += SVFP_CMD_INC_CNT;
  475. tmp_buffer = NULL;
  476. }
  477. svf_command_buffer[cmd_pos++] = (char)toupper(ch);
  478. }
  479. break;
  480. }
  481. }
  482. if (cmd_ok)
  483. {
  484. svf_command_buffer[cmd_pos] = '\0';
  485. return ERROR_OK;
  486. }
  487. else
  488. {
  489. return ERROR_FAIL;
  490. }
  491. }
  492. static int svf_parse_cmd_string(char *str, int len, char **argus, int *num_of_argu)
  493. {
  494. int pos = 0, num = 0, space_found = 1;
  495. while (pos < len)
  496. {
  497. switch (str[pos])
  498. {
  499. case '\n':
  500. case '\r':
  501. case '!':
  502. case '/':
  503. LOG_ERROR("fail to parse svf command");
  504. return ERROR_FAIL;
  505. break;
  506. case ' ':
  507. space_found = 1;
  508. str[pos] = '\0';
  509. break;
  510. default:
  511. if (space_found)
  512. {
  513. argus[num++] = &str[pos];
  514. space_found = 0;
  515. }
  516. break;
  517. }
  518. pos++;
  519. }
  520. *num_of_argu = num;
  521. return ERROR_OK;
  522. }
  523. bool svf_tap_state_is_stable(tap_state_t state)
  524. {
  525. return (TAP_RESET == state) || (TAP_IDLE == state)
  526. || (TAP_DRPAUSE == state) || (TAP_IRPAUSE == state);
  527. }
  528. static int svf_find_string_in_array(char *str, char **strs, int num_of_element)
  529. {
  530. int i;
  531. for (i = 0; i < num_of_element; i++)
  532. {
  533. if (!strcmp(str, strs[i]))
  534. {
  535. return i;
  536. }
  537. }
  538. return 0xFF;
  539. }
  540. static int svf_adjust_array_length(uint8_t **arr, int orig_bit_len, int new_bit_len)
  541. {
  542. int new_byte_len = (new_bit_len + 7) >> 3;
  543. if ((NULL == *arr) || (((orig_bit_len + 7) >> 3) < ((new_bit_len + 7) >> 3)))
  544. {
  545. if (*arr != NULL)
  546. {
  547. free(*arr);
  548. *arr = NULL;
  549. }
  550. *arr = (uint8_t*)malloc(new_byte_len);
  551. if (NULL == *arr)
  552. {
  553. LOG_ERROR("not enough memory");
  554. return ERROR_FAIL;
  555. }
  556. memset(*arr, 0, new_byte_len);
  557. }
  558. return ERROR_OK;
  559. }
  560. static int svf_copy_hexstring_to_binary(char *str, uint8_t **bin, int orig_bit_len, int bit_len)
  561. {
  562. int i, str_len = strlen(str), str_hbyte_len = (bit_len + 3) >> 2;
  563. uint8_t ch = 0;
  564. if (ERROR_OK != svf_adjust_array_length(bin, orig_bit_len, bit_len))
  565. {
  566. LOG_ERROR("fail to adjust length of array");
  567. return ERROR_FAIL;
  568. }
  569. for (i = 0; i < str_hbyte_len; i++)
  570. {
  571. ch = 0;
  572. while (str_len > 0)
  573. {
  574. ch = str[--str_len];
  575. if (!isblank(ch))
  576. {
  577. if ((ch >= '0') && (ch <= '9'))
  578. {
  579. ch = ch - '0';
  580. break;
  581. }
  582. else if ((ch >= 'A') && (ch <= 'F'))
  583. {
  584. ch = ch - 'A' + 10;
  585. break;
  586. }
  587. else
  588. {
  589. LOG_ERROR("invalid hex string");
  590. return ERROR_FAIL;
  591. }
  592. }
  593. ch = 0;
  594. }
  595. // write bin
  596. if (i % 2)
  597. {
  598. // MSB
  599. (*bin)[i / 2] |= ch << 4;
  600. }
  601. else
  602. {
  603. // LSB
  604. (*bin)[i / 2] = 0;
  605. (*bin)[i / 2] |= ch;
  606. }
  607. }
  608. // check valid
  609. if (str_len > 0 || (ch & ~((1 << (4 - (bit_len % 4))) - 1)) != 0)
  610. {
  611. LOG_ERROR("value execede length");
  612. return ERROR_FAIL;
  613. }
  614. return ERROR_OK;
  615. }
  616. static int svf_check_tdo(void)
  617. {
  618. int i, len, index;
  619. for (i = 0; i < svf_check_tdo_para_index; i++)
  620. {
  621. index = svf_check_tdo_para[i].buffer_offset;
  622. len = svf_check_tdo_para[i].bit_len;
  623. if ((svf_check_tdo_para[i].enabled)
  624. && buf_cmp_mask(&svf_tdi_buffer[index], &svf_tdo_buffer[index], &svf_mask_buffer[index], len))
  625. {
  626. unsigned bitmask;
  627. unsigned received, expected, tapmask;
  628. bitmask = svf_get_mask_u32(svf_check_tdo_para[i].bit_len);
  629. memcpy(&received, svf_tdi_buffer + index, sizeof(unsigned));
  630. memcpy(&expected, svf_tdo_buffer + index, sizeof(unsigned));
  631. memcpy(&tapmask, svf_mask_buffer + index, sizeof(unsigned));
  632. LOG_ERROR("tdo check error at line %d",
  633. svf_check_tdo_para[i].line_num);
  634. LOG_ERROR("read = 0x%X, want = 0x%X, mask = 0x%X",
  635. received & bitmask,
  636. expected & bitmask,
  637. tapmask & bitmask);
  638. return ERROR_FAIL;
  639. }
  640. }
  641. svf_check_tdo_para_index = 0;
  642. return ERROR_OK;
  643. }
  644. static int svf_add_check_para(uint8_t enabled, int buffer_offset, int bit_len)
  645. {
  646. if (svf_check_tdo_para_index >= SVF_CHECK_TDO_PARA_SIZE)
  647. {
  648. LOG_ERROR("toooooo many operation undone");
  649. return ERROR_FAIL;
  650. }
  651. svf_check_tdo_para[svf_check_tdo_para_index].line_num = svf_line_number;
  652. svf_check_tdo_para[svf_check_tdo_para_index].bit_len = bit_len;
  653. svf_check_tdo_para[svf_check_tdo_para_index].enabled = enabled;
  654. svf_check_tdo_para[svf_check_tdo_para_index].buffer_offset = buffer_offset;
  655. svf_check_tdo_para_index++;
  656. return ERROR_OK;
  657. }
  658. static int svf_execute_tap(void)
  659. {
  660. if (ERROR_OK != jtag_execute_queue())
  661. {
  662. return ERROR_FAIL;
  663. }
  664. else if (ERROR_OK != svf_check_tdo())
  665. {
  666. return ERROR_FAIL;
  667. }
  668. svf_buffer_index = 0;
  669. return ERROR_OK;
  670. }
  671. static int svf_run_command(struct command_context_s *cmd_ctx, char *cmd_str)
  672. {
  673. char *argus[256], command;
  674. int num_of_argu = 0, i;
  675. // tmp variable
  676. int i_tmp;
  677. // for RUNTEST
  678. int run_count;
  679. float min_time, max_time;
  680. // for XXR
  681. svf_xxr_para_t *xxr_para_tmp;
  682. uint8_t **pbuffer_tmp;
  683. scan_field_t field;
  684. // for STATE
  685. tap_state_t *path = NULL, state;
  686. if (!svf_quiet)
  687. {
  688. LOG_USER("%s", svf_command_buffer);
  689. }
  690. if (ERROR_OK != svf_parse_cmd_string(cmd_str, strlen(cmd_str), argus, &num_of_argu))
  691. {
  692. return ERROR_FAIL;
  693. }
  694. /* NOTE: we're a bit loose here, because we ignore case in
  695. * TAP state names (instead of insisting on uppercase).
  696. */
  697. command = svf_find_string_in_array(argus[0],
  698. (char **)svf_command_name, DIM(svf_command_name));
  699. switch (command)
  700. {
  701. case ENDDR:
  702. case ENDIR:
  703. if (num_of_argu != 2)
  704. {
  705. LOG_ERROR("invalid parameter of %s", argus[0]);
  706. return ERROR_FAIL;
  707. }
  708. i_tmp = tap_state_by_name(argus[1]);
  709. if (svf_tap_state_is_stable(i_tmp))
  710. {
  711. if (command == ENDIR)
  712. {
  713. svf_para.ir_end_state = i_tmp;
  714. LOG_DEBUG("\tIR end_state = %s",
  715. tap_state_name(i_tmp));
  716. }
  717. else
  718. {
  719. svf_para.dr_end_state = i_tmp;
  720. LOG_DEBUG("\tDR end_state = %s",
  721. tap_state_name(i_tmp));
  722. }
  723. }
  724. else
  725. {
  726. LOG_ERROR("%s: %s is not a stable state",
  727. argus[0], argus[1]);
  728. return ERROR_FAIL;
  729. }
  730. break;
  731. case FREQUENCY:
  732. if ((num_of_argu != 1) && (num_of_argu != 3))
  733. {
  734. LOG_ERROR("invalid parameter of %s", argus[0]);
  735. return ERROR_FAIL;
  736. }
  737. if (1 == num_of_argu)
  738. {
  739. // TODO: set jtag speed to full speed
  740. svf_para.frequency = 0;
  741. }
  742. else
  743. {
  744. if (strcmp(argus[2], "HZ"))
  745. {
  746. LOG_ERROR("HZ not found in FREQUENCY command");
  747. return ERROR_FAIL;
  748. }
  749. if (ERROR_OK != svf_execute_tap())
  750. {
  751. return ERROR_FAIL;
  752. }
  753. svf_para.frequency = atof(argus[1]);
  754. // TODO: set jtag speed to
  755. if (svf_para.frequency > 0)
  756. {
  757. command_run_linef(cmd_ctx, "jtag_khz %d", (int)svf_para.frequency / 1000);
  758. LOG_DEBUG("\tfrequency = %f", svf_para.frequency);
  759. }
  760. }
  761. break;
  762. case HDR:
  763. xxr_para_tmp = &svf_para.hdr_para;
  764. goto XXR_common;
  765. case HIR:
  766. xxr_para_tmp = &svf_para.hir_para;
  767. goto XXR_common;
  768. case TDR:
  769. xxr_para_tmp = &svf_para.tdr_para;
  770. goto XXR_common;
  771. case TIR:
  772. xxr_para_tmp = &svf_para.tir_para;
  773. goto XXR_common;
  774. case SDR:
  775. xxr_para_tmp = &svf_para.sdr_para;
  776. goto XXR_common;
  777. case SIR:
  778. xxr_para_tmp = &svf_para.sir_para;
  779. goto XXR_common;
  780. XXR_common:
  781. // XXR length [TDI (tdi)] [TDO (tdo)][MASK (mask)] [SMASK (smask)]
  782. if ((num_of_argu > 10) || (num_of_argu % 2))
  783. {
  784. LOG_ERROR("invalid parameter of %s", argus[0]);
  785. return ERROR_FAIL;
  786. }
  787. i_tmp = xxr_para_tmp->len;
  788. xxr_para_tmp->len = atoi(argus[1]);
  789. LOG_DEBUG("\tlength = %d", xxr_para_tmp->len);
  790. xxr_para_tmp->data_mask = 0;
  791. for (i = 2; i < num_of_argu; i += 2)
  792. {
  793. if ((strlen(argus[i + 1]) < 3) || (argus[i + 1][0] != '(') || (argus[i + 1][strlen(argus[i + 1]) - 1] != ')'))
  794. {
  795. LOG_ERROR("data section error");
  796. return ERROR_FAIL;
  797. }
  798. argus[i + 1][strlen(argus[i + 1]) - 1] = '\0';
  799. // TDI, TDO, MASK, SMASK
  800. if (!strcmp(argus[i], "TDI"))
  801. {
  802. // TDI
  803. pbuffer_tmp = &xxr_para_tmp->tdi;
  804. xxr_para_tmp->data_mask |= XXR_TDI;
  805. }
  806. else if (!strcmp(argus[i], "TDO"))
  807. {
  808. // TDO
  809. pbuffer_tmp = &xxr_para_tmp->tdo;
  810. xxr_para_tmp->data_mask |= XXR_TDO;
  811. }
  812. else if (!strcmp(argus[i], "MASK"))
  813. {
  814. // MASK
  815. pbuffer_tmp = &xxr_para_tmp->mask;
  816. xxr_para_tmp->data_mask |= XXR_MASK;
  817. }
  818. else if (!strcmp(argus[i], "SMASK"))
  819. {
  820. // SMASK
  821. pbuffer_tmp = &xxr_para_tmp->smask;
  822. xxr_para_tmp->data_mask |= XXR_SMASK;
  823. }
  824. else
  825. {
  826. LOG_ERROR("unknow parameter: %s", argus[i]);
  827. return ERROR_FAIL;
  828. }
  829. if (ERROR_OK != svf_copy_hexstring_to_binary(&argus[i + 1][1], pbuffer_tmp, i_tmp, xxr_para_tmp->len))
  830. {
  831. LOG_ERROR("fail to parse hex value");
  832. return ERROR_FAIL;
  833. }
  834. LOG_DEBUG("\t%s = 0x%X", argus[i], (**(int**)pbuffer_tmp) & svf_get_mask_u32(xxr_para_tmp->len));
  835. }
  836. // If a command changes the length of the last scan of the same type and the MASK parameter is absent,
  837. // the mask pattern used is all cares
  838. if (!(xxr_para_tmp->data_mask & XXR_MASK) && (i_tmp != xxr_para_tmp->len))
  839. {
  840. // MASK not defined and length changed
  841. if (ERROR_OK != svf_adjust_array_length(&xxr_para_tmp->mask, i_tmp, xxr_para_tmp->len))
  842. {
  843. LOG_ERROR("fail to adjust length of array");
  844. return ERROR_FAIL;
  845. }
  846. buf_set_ones(xxr_para_tmp->mask, xxr_para_tmp->len);
  847. }
  848. // If TDO is absent, no comparison is needed, set the mask to 0
  849. if (!(xxr_para_tmp->data_mask & XXR_TDO))
  850. {
  851. if (NULL == xxr_para_tmp->tdo)
  852. {
  853. if (ERROR_OK != svf_adjust_array_length(&xxr_para_tmp->tdo, i_tmp, xxr_para_tmp->len))
  854. {
  855. LOG_ERROR("fail to adjust length of array");
  856. return ERROR_FAIL;
  857. }
  858. }
  859. if (NULL == xxr_para_tmp->mask)
  860. {
  861. if (ERROR_OK != svf_adjust_array_length(&xxr_para_tmp->mask, i_tmp, xxr_para_tmp->len))
  862. {
  863. LOG_ERROR("fail to adjust length of array");
  864. return ERROR_FAIL;
  865. }
  866. }
  867. memset(xxr_para_tmp->mask, 0, (xxr_para_tmp->len + 7) >> 3);
  868. }
  869. // do scan if necessary
  870. if (SDR == command)
  871. {
  872. // check buffer size first, reallocate if necessary
  873. i = svf_para.hdr_para.len + svf_para.sdr_para.len + svf_para.tdr_para.len;
  874. if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3))
  875. {
  876. #if 1
  877. // simply print error message
  878. LOG_ERROR("buffer is not enough, report to author");
  879. return ERROR_FAIL;
  880. #else
  881. uint8_t *buffer_tmp;
  882. // reallocate buffer
  883. buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
  884. if (NULL == buffer_tmp)
  885. {
  886. LOG_ERROR("not enough memory");
  887. return ERROR_FAIL;
  888. }
  889. memcpy(buffer_tmp, svf_tdi_buffer, svf_buffer_index);
  890. // svf_tdi_buffer isn't NULL here
  891. free(svf_tdi_buffer);
  892. svf_tdi_buffer = buffer_tmp;
  893. buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
  894. if (NULL == buffer_tmp)
  895. {
  896. LOG_ERROR("not enough memory");
  897. return ERROR_FAIL;
  898. }
  899. memcpy(buffer_tmp, svf_tdo_buffer, svf_buffer_index);
  900. // svf_tdo_buffer isn't NULL here
  901. free(svf_tdo_buffer);
  902. svf_tdo_buffer = buffer_tmp;
  903. buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
  904. if (NULL == buffer_tmp)
  905. {
  906. LOG_ERROR("not enough memory");
  907. return ERROR_FAIL;
  908. }
  909. memcpy(buffer_tmp, svf_mask_buffer, svf_buffer_index);
  910. // svf_mask_buffer isn't NULL here
  911. free(svf_mask_buffer);
  912. svf_mask_buffer = buffer_tmp;
  913. buffer_tmp = NULL;
  914. svf_buffer_size = svf_buffer_index + ((i + 7) >> 3);
  915. #endif
  916. }
  917. // assemble dr data
  918. i = 0;
  919. buf_set_buf(svf_para.hdr_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.hdr_para.len);
  920. i += svf_para.hdr_para.len;
  921. buf_set_buf(svf_para.sdr_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.sdr_para.len);
  922. i += svf_para.sdr_para.len;
  923. buf_set_buf(svf_para.tdr_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.tdr_para.len);
  924. i += svf_para.tdr_para.len;
  925. // add check data
  926. if (svf_para.sdr_para.data_mask & XXR_TDO)
  927. {
  928. // assemble dr mask data
  929. i = 0;
  930. buf_set_buf(svf_para.hdr_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.hdr_para.len);
  931. i += svf_para.hdr_para.len;
  932. buf_set_buf(svf_para.sdr_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.sdr_para.len);
  933. i += svf_para.sdr_para.len;
  934. buf_set_buf(svf_para.tdr_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.tdr_para.len);
  935. i += svf_para.tdr_para.len;
  936. // assemble dr check data
  937. i = 0;
  938. buf_set_buf(svf_para.hdr_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.hdr_para.len);
  939. i += svf_para.hdr_para.len;
  940. buf_set_buf(svf_para.sdr_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.sdr_para.len);
  941. i += svf_para.sdr_para.len;
  942. buf_set_buf(svf_para.tdr_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.tdr_para.len);
  943. i += svf_para.tdr_para.len;
  944. svf_add_check_para(1, svf_buffer_index, i);
  945. }
  946. else
  947. {
  948. svf_add_check_para(0, svf_buffer_index, i);
  949. }
  950. field.tap = tap;
  951. field.num_bits = i;
  952. field.out_value = &svf_tdi_buffer[svf_buffer_index];
  953. field.in_value = &svf_tdi_buffer[svf_buffer_index];
  954. /* NOTE: doesn't use SVF-specified state paths */
  955. jtag_add_plain_dr_scan(1, &field, svf_para.dr_end_state);
  956. svf_buffer_index += (i + 7) >> 3;
  957. }
  958. else if (SIR == command)
  959. {
  960. // check buffer size first, reallocate if necessary
  961. i = svf_para.hir_para.len + svf_para.sir_para.len + svf_para.tir_para.len;
  962. if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3))
  963. {
  964. #if 1
  965. // simply print error message
  966. LOG_ERROR("buffer is not enough, report to author");
  967. return ERROR_FAIL;
  968. #else
  969. uint8_t *buffer_tmp;
  970. // reallocate buffer
  971. buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
  972. if (NULL == buffer_tmp)
  973. {
  974. LOG_ERROR("not enough memory");
  975. return ERROR_FAIL;
  976. }
  977. memcpy(buffer_tmp, svf_tdi_buffer, svf_buffer_index);
  978. // svf_tdi_buffer isn't NULL here
  979. free(svf_tdi_buffer);
  980. svf_tdi_buffer = buffer_tmp;
  981. buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
  982. if (NULL == buffer_tmp)
  983. {
  984. LOG_ERROR("not enough memory");
  985. return ERROR_FAIL;
  986. }
  987. memcpy(buffer_tmp, svf_tdo_buffer, svf_buffer_index);
  988. // svf_tdo_buffer isn't NULL here
  989. free(svf_tdo_buffer);
  990. svf_tdo_buffer = buffer_tmp;
  991. buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
  992. if (NULL == buffer_tmp)
  993. {
  994. LOG_ERROR("not enough memory");
  995. return ERROR_FAIL;
  996. }
  997. memcpy(buffer_tmp, svf_mask_buffer, svf_buffer_index);
  998. // svf_mask_buffer isn't NULL here
  999. free(svf_mask_buffer);
  1000. svf_mask_buffer = buffer_tmp;
  1001. buffer_tmp = NULL;
  1002. svf_buffer_size = svf_buffer_index + ((i + 7) >> 3);
  1003. #endif
  1004. }
  1005. // assemble ir data
  1006. i = 0;
  1007. buf_set_buf(svf_para.hir_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.hir_para.len);
  1008. i += svf_para.hir_para.len;
  1009. buf_set_buf(svf_para.sir_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.sir_para.len);
  1010. i += svf_para.sir_para.len;
  1011. buf_set_buf(svf_para.tir_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.tir_para.len);
  1012. i += svf_para.tir_para.len;
  1013. // add check data
  1014. if (svf_para.sir_para.data_mask & XXR_TDO)
  1015. {
  1016. // assemble dr mask data
  1017. i = 0;
  1018. buf_set_buf(svf_para.hir_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.hir_para.len);
  1019. i += svf_para.hir_para.len;
  1020. buf_set_buf(svf_para.sir_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.sir_para.len);
  1021. i += svf_para.sir_para.len;
  1022. buf_set_buf(svf_para.tir_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.tir_para.len);
  1023. i += svf_para.tir_para.len;
  1024. // assemble dr check data
  1025. i = 0;
  1026. buf_set_buf(svf_para.hir_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.hir_para.len);
  1027. i += svf_para.hir_para.len;
  1028. buf_set_buf(svf_para.sir_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.sir_para.len);
  1029. i += svf_para.sir_para.len;
  1030. buf_set_buf(svf_para.tir_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.tir_para.len);
  1031. i += svf_para.tir_para.len;
  1032. svf_add_check_para(1, svf_buffer_index, i);
  1033. }
  1034. else
  1035. {
  1036. svf_add_check_para(0, svf_buffer_index, i);
  1037. }
  1038. field.tap = tap;
  1039. field.num_bits = i;
  1040. field.out_value = &svf_tdi_buffer[svf_buffer_index];
  1041. field.in_value = &svf_tdi_buffer[svf_buffer_index];
  1042. /* NOTE: doesn't use SVF-specified state paths */
  1043. jtag_add_plain_ir_scan(1, &field, svf_para.ir_end_state);
  1044. svf_buffer_index += (i + 7) >> 3;
  1045. }
  1046. break;
  1047. case PIO:
  1048. case PIOMAP:
  1049. LOG_ERROR("PIO and PIOMAP are not supported");
  1050. return ERROR_FAIL;
  1051. break;
  1052. case RUNTEST:
  1053. // RUNTEST [run_state] run_count run_clk [min_time SEC [MAXIMUM max_time SEC]] [ENDSTATE end_state]
  1054. // RUNTEST [run_state] min_time SEC [MAXIMUM max_time SEC] [ENDSTATE end_state]
  1055. if ((num_of_argu < 3) && (num_of_argu > 11))
  1056. {
  1057. LOG_ERROR("invalid parameter of %s", argus[0]);
  1058. return ERROR_FAIL;
  1059. }
  1060. // init
  1061. run_count = 0;
  1062. min_time = 0;
  1063. max_time = 0;
  1064. i = 1;
  1065. // run_state
  1066. i_tmp = tap_state_by_name(argus[i]);
  1067. if (i_tmp != TAP_INVALID)
  1068. {
  1069. if (svf_tap_state_is_stable(i_tmp))
  1070. {
  1071. svf_para.runtest_run_state = i_tmp;
  1072. /* When a run_state is specified, the new
  1073. * run_state becomes the default end_state.
  1074. */
  1075. svf_para.runtest_end_state = i_tmp;
  1076. LOG_DEBUG("\trun_state = %s",
  1077. tap_state_name(i_tmp));
  1078. i++;
  1079. }
  1080. else
  1081. {
  1082. LOG_ERROR("%s: %s is not a stable state",
  1083. argus[0], tap_state_name(i_tmp));
  1084. return ERROR_FAIL;
  1085. }
  1086. }
  1087. // run_count run_clk
  1088. if (((i + 2) <= num_of_argu) && strcmp(argus[i + 1], "SEC"))
  1089. {
  1090. if (!strcmp(argus[i + 1], "TCK"))
  1091. {
  1092. // clock source is TCK
  1093. run_count = atoi(argus[i]);
  1094. LOG_DEBUG("\trun_count@TCK = %d", run_count);
  1095. }
  1096. else
  1097. {
  1098. LOG_ERROR("%s not supported for clock", argus[i + 1]);
  1099. return ERROR_FAIL;
  1100. }
  1101. i += 2;
  1102. }
  1103. // min_time SEC
  1104. if (((i + 2) <= num_of_argu) && !strcmp(argus[i + 1], "SEC"))
  1105. {
  1106. min_time = atof(argus[i]);
  1107. LOG_DEBUG("\tmin_time = %fs", min_time);
  1108. i += 2;
  1109. }
  1110. // MAXIMUM max_time SEC
  1111. if (((i + 3) <= num_of_argu) && !strcmp(argus[i], "MAXIMUM") && !strcmp(argus[i + 2], "SEC"))
  1112. {
  1113. max_time = atof(argus[i + 1]);
  1114. LOG_DEBUG("\tmax_time = %fs", max_time);
  1115. i += 3;
  1116. }
  1117. // ENDSTATE end_state
  1118. if (((i + 2) <= num_of_argu) && !strcmp(argus[i], "ENDSTATE"))
  1119. {
  1120. i_tmp = tap_state_by_name(argus[i + 1]);
  1121. if (svf_tap_state_is_stable(i_tmp))
  1122. {
  1123. svf_para.runtest_end_state = i_tmp;
  1124. LOG_DEBUG("\tend_state = %s",
  1125. tap_state_name(i_tmp));
  1126. }
  1127. else
  1128. {
  1129. LOG_ERROR("%s: %s is not a stable state",
  1130. argus[0], tap_state_name(i_tmp));
  1131. return ERROR_FAIL;
  1132. }
  1133. i += 2;
  1134. }
  1135. // calculate run_count
  1136. if ((0 == run_count) && (min_time > 0))
  1137. {
  1138. run_count = min_time * svf_para.frequency;
  1139. }
  1140. // all parameter should be parsed
  1141. if (i == num_of_argu)
  1142. {
  1143. if (run_count > 0)
  1144. {
  1145. // run_state and end_state is checked to be stable state
  1146. // TODO: do runtest
  1147. #if 1
  1148. /* FIXME handle statemove failures */
  1149. int retval;
  1150. // enter into run_state if necessary
  1151. if (cmd_queue_cur_state != svf_para.runtest_run_state)
  1152. {
  1153. retval = svf_add_statemove(svf_para.runtest_run_state);
  1154. }
  1155. // call jtag_add_clocks
  1156. jtag_add_clocks(run_count);
  1157. // move to end_state if necessary
  1158. if (svf_para.runtest_end_state != svf_para.runtest_run_state)
  1159. {
  1160. retval = svf_add_statemove(svf_para.runtest_end_state);
  1161. }
  1162. #else
  1163. if (svf_para.runtest_run_state != TAP_IDLE)
  1164. {
  1165. LOG_ERROR("cannot runtest in %s state",
  1166. tap_state_name(svf_para.runtest_run_state));
  1167. return ERROR_FAIL;
  1168. }
  1169. jtag_add_runtest(run_count, svf_para.runtest_end_state);
  1170. #endif
  1171. }
  1172. }
  1173. else
  1174. {
  1175. LOG_ERROR("fail to parse parameter of RUNTEST, %d out of %d is parsed", i, num_of_argu);
  1176. return ERROR_FAIL;
  1177. }
  1178. break;
  1179. case STATE:
  1180. // STATE [pathstate1 [pathstate2 ...[pathstaten]]] stable_state
  1181. if (num_of_argu < 2)
  1182. {
  1183. LOG_ERROR("invalid parameter of %s", argus[0]);
  1184. return ERROR_FAIL;
  1185. }
  1186. if (num_of_argu > 2)
  1187. {
  1188. // STATE pathstate1 ... stable_state
  1189. path = (tap_state_t *)malloc((num_of_argu - 1) * sizeof(tap_state_t));
  1190. if (NULL == path)
  1191. {
  1192. LOG_ERROR("not enough memory");
  1193. return ERROR_FAIL;
  1194. }
  1195. num_of_argu--; // num of path
  1196. i_tmp = 1; /* path is from parameter 1 */
  1197. for (i = 0; i < num_of_argu; i++, i_tmp++)
  1198. {
  1199. path[i] = tap_state_by_name(argus[i_tmp]);
  1200. if (path[i] == TAP_INVALID)
  1201. {
  1202. LOG_ERROR("%s: %s is not a valid state",
  1203. argus[0], argus[i_tmp]);
  1204. free(path);
  1205. return ERROR_FAIL;
  1206. }
  1207. /* OpenOCD refuses paths containing TAP_RESET */
  1208. if (TAP_RESET == path[i])
  1209. {
  1210. /* FIXME last state MUST be stable! */
  1211. if (i > 0)
  1212. {
  1213. jtag_add_pathmove(i, path);
  1214. }
  1215. jtag_add_tlr();
  1216. num_of_argu -= i + 1;
  1217. i = -1;
  1218. }
  1219. }
  1220. if (num_of_argu > 0)
  1221. {
  1222. // execute last path if necessary
  1223. if (svf_tap_state_is_stable(path[num_of_argu - 1]))
  1224. {
  1225. // last state MUST be stable state
  1226. jtag_add_pathmove(num_of_argu, path);
  1227. LOG_DEBUG("\tmove to %s by path_move",
  1228. tap_state_name(path[num_of_argu - 1]));
  1229. }
  1230. else
  1231. {
  1232. LOG_ERROR("%s: %s is not a stable state",
  1233. argus[0],
  1234. tap_state_name(path[num_of_argu - 1]));
  1235. free(path);
  1236. return ERROR_FAIL;
  1237. }
  1238. }
  1239. // no need to keep this memory, in jtag_add_pathmove, path will be duplicated
  1240. if (NULL != path)
  1241. {
  1242. free(path);
  1243. path = NULL;
  1244. }
  1245. }
  1246. else
  1247. {
  1248. // STATE stable_state
  1249. state = tap_state_by_name(argus[1]);
  1250. if (svf_tap_state_is_stable(state))
  1251. {
  1252. LOG_DEBUG("\tmove to %s by svf_add_statemove",
  1253. tap_state_name(state));
  1254. /* FIXME handle statemove failures */
  1255. svf_add_statemove(state);
  1256. }
  1257. else
  1258. {
  1259. LOG_ERROR("%s: %s is not a stable state",
  1260. argus[0], tap_state_name(state));
  1261. return ERROR_FAIL;
  1262. }
  1263. }
  1264. break;
  1265. case TRST:
  1266. // TRST trst_mode
  1267. if (num_of_argu != 2)
  1268. {
  1269. LOG_ERROR("invalid parameter of %s", argus[0]);
  1270. return ERROR_FAIL;
  1271. }
  1272. if (svf_para.trst_mode != TRST_ABSENT)
  1273. {
  1274. if (ERROR_OK != svf_execute_tap())
  1275. {
  1276. return ERROR_FAIL;
  1277. }
  1278. i_tmp = svf_find_string_in_array(argus[1],
  1279. (char **)svf_trst_mode_name,
  1280. DIM(svf_trst_mode_name));
  1281. switch (i_tmp)
  1282. {
  1283. case TRST_ON:
  1284. jtag_add_reset(1, 0);
  1285. break;
  1286. case TRST_Z:
  1287. case TRST_OFF:
  1288. jtag_add_reset(0, 0);
  1289. break;
  1290. case TRST_ABSENT:
  1291. break;
  1292. default:
  1293. LOG_ERROR("unknown TRST mode: %s", argus[1]);
  1294. return ERROR_FAIL;
  1295. }
  1296. svf_para.trst_mode = i_tmp;
  1297. LOG_DEBUG("\ttrst_mode = %s", svf_trst_mode_name[svf_para.trst_mode]);
  1298. }
  1299. else
  1300. {
  1301. LOG_ERROR("can not accpet TRST command if trst_mode is ABSENT");
  1302. return ERROR_FAIL;
  1303. }
  1304. break;
  1305. default:
  1306. LOG_ERROR("invalid svf command: %s", argus[0]);
  1307. return ERROR_FAIL;
  1308. break;
  1309. }
  1310. if (debug_level >= LOG_LVL_DEBUG)
  1311. {
  1312. // for convenient debugging, execute tap if possible
  1313. if ((svf_buffer_index > 0) && \
  1314. (((command != STATE) && (command != RUNTEST)) || \
  1315. ((command == STATE) && (num_of_argu == 2))))
  1316. {
  1317. if (ERROR_OK != svf_execute_tap())
  1318. {
  1319. return ERROR_FAIL;
  1320. }
  1321. // output debug info
  1322. if ((SIR == command) || (SDR == command))
  1323. {
  1324. int read_value;
  1325. memcpy(&read_value, svf_tdi_buffer, sizeof(int));
  1326. // in debug mode, data is from index 0
  1327. int read_mask = svf_get_mask_u32(svf_check_tdo_para[0].bit_len);
  1328. LOG_DEBUG("\tTDO read = 0x%X", read_value & read_mask);
  1329. }
  1330. }
  1331. }
  1332. else
  1333. {
  1334. // for fast executing, execute tap if necessary
  1335. // half of the buffer is for the next command
  1336. if (((svf_buffer_index >= SVF_MAX_BUFFER_SIZE_TO_COMMIT) || (svf_check_tdo_para_index >= SVF_CHECK_TDO_PARA_SIZE / 2)) && \
  1337. (((command != STATE) && (command != RUNTEST)) || \
  1338. ((command == STATE) && (num_of_argu == 2))))
  1339. {
  1340. return svf_execute_tap();
  1341. }
  1342. }
  1343. return ERROR_OK;
  1344. }