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.
 
 
 
 
 
 

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