1 /***************************************************************************
2 * Copyright (C) 2009 by Simon Qian *
3 * SimonQian@SimonQian.com *
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. *
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. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 /* The specification for SVF is available here:
22 * http://www.asset-intertech.com/support/svf.pdf
23 * Below, this document is refered to as the "SVF spec".
25 * The specification for XSVF is available here:
26 * http://www.xilinx.com/support/documentation/application_notes/xapp503.pdf
27 * Below, this document is refered to as the "XSVF spec".
34 #include <jtag/jtag.h>
36 #include <helper/time_support.h>
56 static const char *svf_command_name[14] = {
80 static const char *svf_trst_mode_name[4] = {
87 struct svf_statemove {
90 uint32_t num_of_moves;
95 * These paths are from the SVF specification for the STATE command, to be
96 * used when the STATE command only includes the final state. The first
97 * element of the path is the "from" (current) state, and the last one is
98 * the "to" (target) state.
100 * All specified paths are the shortest ones in the JTAG spec, and are thus
101 * not (!!) exact matches for the paths used elsewhere in OpenOCD. Note
102 * that PAUSE-to-PAUSE transitions all go through UPDATE and then CAPTURE,
103 * which has specific effects on the various registers; they are not NOPs.
105 * Paths to RESET are disabled here. As elsewhere in OpenOCD, and in XSVF
106 * and many SVF implementations, we don't want to risk missing that state.
107 * To get to RESET, always we ignore the current state.
109 static const struct svf_statemove svf_statemoves[] = {
110 /* from to num_of_moves, paths[8] */
111 /* {TAP_RESET, TAP_RESET, 1, {TAP_RESET}}, */
112 {TAP_RESET, TAP_IDLE, 2, {TAP_RESET, TAP_IDLE} },
113 {TAP_RESET, TAP_DRPAUSE, 6, {TAP_RESET, TAP_IDLE, TAP_DRSELECT,
114 TAP_DRCAPTURE, TAP_DREXIT1, TAP_DRPAUSE} },
115 {TAP_RESET, TAP_IRPAUSE, 7, {TAP_RESET, TAP_IDLE, TAP_DRSELECT,
116 TAP_IRSELECT, TAP_IRCAPTURE,
117 TAP_IREXIT1, TAP_IRPAUSE} },
119 /* {TAP_IDLE, TAP_RESET, 4, {TAP_IDLE,
120 * TAP_DRSELECT, TAP_IRSELECT, TAP_RESET}}, */
121 {TAP_IDLE, TAP_IDLE, 1, {TAP_IDLE} },
122 {TAP_IDLE, TAP_DRPAUSE, 5, {TAP_IDLE, TAP_DRSELECT, TAP_DRCAPTURE,
123 TAP_DREXIT1, TAP_DRPAUSE} },
124 {TAP_IDLE, TAP_IRPAUSE, 6, {TAP_IDLE, TAP_DRSELECT, TAP_IRSELECT,
125 TAP_IRCAPTURE, TAP_IREXIT1, TAP_IRPAUSE} },
127 /* {TAP_DRPAUSE, TAP_RESET, 6, {TAP_DRPAUSE,
128 * TAP_DREXIT2, TAP_DRUPDATE, TAP_DRSELECT, TAP_IRSELECT, TAP_RESET}}, */
129 {TAP_DRPAUSE, TAP_IDLE, 4, {TAP_DRPAUSE, TAP_DREXIT2, TAP_DRUPDATE,
131 {TAP_DRPAUSE, TAP_DRPAUSE, 7, {TAP_DRPAUSE, TAP_DREXIT2, TAP_DRUPDATE,
132 TAP_DRSELECT, TAP_DRCAPTURE,
133 TAP_DREXIT1, TAP_DRPAUSE} },
134 {TAP_DRPAUSE, TAP_IRPAUSE, 8, {TAP_DRPAUSE, TAP_DREXIT2, TAP_DRUPDATE,
135 TAP_DRSELECT, TAP_IRSELECT,
136 TAP_IRCAPTURE, TAP_IREXIT1, TAP_IRPAUSE} },
138 /* {TAP_IRPAUSE, TAP_RESET, 6, {TAP_IRPAUSE,
139 * TAP_IREXIT2, TAP_IRUPDATE, TAP_DRSELECT, TAP_IRSELECT, TAP_RESET}}, */
140 {TAP_IRPAUSE, TAP_IDLE, 4, {TAP_IRPAUSE, TAP_IREXIT2, TAP_IRUPDATE,
142 {TAP_IRPAUSE, TAP_DRPAUSE, 7, {TAP_IRPAUSE, TAP_IREXIT2, TAP_IRUPDATE,
143 TAP_DRSELECT, TAP_DRCAPTURE,
144 TAP_DREXIT1, TAP_DRPAUSE} },
145 {TAP_IRPAUSE, TAP_IRPAUSE, 8, {TAP_IRPAUSE, TAP_IREXIT2, TAP_IRUPDATE,
146 TAP_DRSELECT, TAP_IRSELECT,
147 TAP_IRCAPTURE, TAP_IREXIT1, TAP_IRPAUSE} }
150 #define XXR_TDI (1 << 0)
151 #define XXR_TDO (1 << 1)
152 #define XXR_MASK (1 << 2)
153 #define XXR_SMASK (1 << 3)
154 struct svf_xxr_para {
165 tap_state_t ir_end_state;
166 tap_state_t dr_end_state;
167 tap_state_t runtest_run_state;
168 tap_state_t runtest_end_state;
169 enum trst_mode trst_mode;
171 struct svf_xxr_para hir_para;
172 struct svf_xxr_para hdr_para;
173 struct svf_xxr_para tir_para;
174 struct svf_xxr_para tdr_para;
175 struct svf_xxr_para sir_para;
176 struct svf_xxr_para sdr_para;
179 static struct svf_para svf_para;
180 static const struct svf_para svf_para_init = {
181 /* frequency, ir_end_state, dr_end_state, runtest_run_state, runtest_end_state, trst_mode */
182 0, TAP_IDLE, TAP_IDLE, TAP_IDLE, TAP_IDLE, TRST_Z,
184 /* {len, data_mask, tdi, tdo, mask, smask}, */
185 {0, 0, NULL, NULL, NULL, NULL},
187 /* {len, data_mask, tdi, tdo, mask, smask}, */
188 {0, 0, NULL, NULL, NULL, NULL},
190 /* {len, data_mask, tdi, tdo, mask, smask}, */
191 {0, 0, NULL, NULL, NULL, NULL},
193 /* {len, data_mask, tdi, tdo, mask, smask}, */
194 {0, 0, NULL, NULL, NULL, NULL},
196 /* {len, data_mask, tdi, tdo, mask, smask}, */
197 {0, 0, NULL, NULL, NULL, NULL},
199 /* {len, data_mask, tdi, tdo, mask, smask}, */
200 {0, 0, NULL, NULL, NULL, NULL},
203 struct svf_check_tdo_para {
204 int line_num; /* used to record line number of the check operation */
205 /* so more information could be printed */
206 int enabled; /* check is enabled or not */
207 int buffer_offset; /* buffer_offset to buffers */
208 int bit_len; /* bit length to check */
211 #define SVF_CHECK_TDO_PARA_SIZE 1024
212 static struct svf_check_tdo_para *svf_check_tdo_para;
213 static int svf_check_tdo_para_index;
215 static int svf_read_command_from_file(FILE *fd);
216 static int svf_check_tdo(void);
217 static int svf_add_check_para(uint8_t enabled, int buffer_offset, int bit_len);
218 static int svf_run_command(struct command_context *cmd_ctx, char *cmd_str);
221 static char *svf_read_line;
222 static size_t svf_read_line_size;
223 static char *svf_command_buffer;
224 static size_t svf_command_buffer_size;
225 static int svf_line_number = 1;
226 static int svf_getline(char **lineptr, size_t *n, FILE *stream);
228 #define SVF_MAX_BUFFER_SIZE_TO_COMMIT (1024 * 1024)
229 static uint8_t *svf_tdi_buffer, *svf_tdo_buffer, *svf_mask_buffer;
230 static int svf_buffer_index, svf_buffer_size ;
231 static int svf_quiet;
234 /* Targetting particular tap */
235 static int svf_tap_is_specified;
236 static int svf_set_padding(struct svf_xxr_para *para, int len, unsigned char tdi);
238 /* Progress Indicator */
239 static int svf_progress_enabled;
240 static long svf_total_lines;
241 static int svf_percentage;
242 static int svf_last_printed_percentage = -1;
244 static void svf_free_xxd_para(struct svf_xxr_para *para)
247 if (para->tdi != NULL) {
251 if (para->tdo != NULL) {
255 if (para->mask != NULL) {
259 if (para->smask != NULL) {
266 static unsigned svf_get_mask_u32(int bitlen)
272 else if (bitlen >= 32)
273 bitmask = 0xFFFFFFFF;
275 bitmask = (1 << bitlen) - 1;
280 int svf_add_statemove(tap_state_t state_to)
282 tap_state_t state_from = cmd_queue_cur_state;
285 /* when resetting, be paranoid and ignore current state */
286 if (state_to == TAP_RESET) {
294 for (index_var = 0; index_var < ARRAY_SIZE(svf_statemoves); index_var++) {
295 if ((svf_statemoves[index_var].from == state_from)
296 && (svf_statemoves[index_var].to == state_to)) {
299 /* recorded path includes current state ... avoid
301 if (svf_statemoves[index_var].num_of_moves > 1)
302 jtag_add_pathmove(svf_statemoves[index_var].num_of_moves - 1,
303 svf_statemoves[index_var].paths + 1);
305 jtag_add_pathmove(svf_statemoves[index_var].num_of_moves,
306 svf_statemoves[index_var].paths);
310 LOG_ERROR("SVF: can not move to %s", tap_state_name(state_to));
314 COMMAND_HANDLER(handle_svf_command)
316 #define SVF_MIN_NUM_OF_OPTIONS 1
317 #define SVF_MAX_NUM_OF_OPTIONS 5
320 long long time_measure_ms;
321 int time_measure_s, time_measure_m;
323 /* use NULL to indicate a "plain" svf file which accounts for
324 * any additional devices in the scan chain, otherwise the device
325 * that should be affected
327 struct jtag_tap *tap = NULL;
329 if ((CMD_ARGC < SVF_MIN_NUM_OF_OPTIONS) || (CMD_ARGC > SVF_MAX_NUM_OF_OPTIONS))
330 return ERROR_COMMAND_SYNTAX_ERROR;
332 /* parse command line */
335 for (unsigned int i = 0; i < CMD_ARGC; i++) {
336 if (strcmp(CMD_ARGV[i], "-tap") == 0) {
337 tap = jtag_tap_by_string(CMD_ARGV[i+1]);
339 command_print(CMD_CTX, "Tap: %s unknown", CMD_ARGV[i+1]);
343 } else if ((strcmp(CMD_ARGV[i],
344 "quiet") == 0) || (strcmp(CMD_ARGV[i], "-quiet") == 0))
346 else if ((strcmp(CMD_ARGV[i], "nil") == 0) || (strcmp(CMD_ARGV[i], "-nil") == 0))
348 else if ((strcmp(CMD_ARGV[i],
349 "progress") == 0) || (strcmp(CMD_ARGV[i], "-progress") == 0))
350 svf_progress_enabled = 1;
352 svf_fd = fopen(CMD_ARGV[i], "r");
353 if (svf_fd == NULL) {
355 command_print(CMD_CTX, "open(\"%s\"): %s", CMD_ARGV[i], strerror(err));
356 /* no need to free anything now */
357 return ERROR_COMMAND_SYNTAX_ERROR;
359 LOG_USER("svf processing file: \"%s\"", CMD_ARGV[i]);
364 return ERROR_COMMAND_SYNTAX_ERROR;
367 time_measure_ms = timeval_ms();
371 svf_command_buffer_size = 0;
373 svf_check_tdo_para_index = 0;
374 svf_check_tdo_para = malloc(sizeof(struct svf_check_tdo_para) * SVF_CHECK_TDO_PARA_SIZE);
375 if (NULL == svf_check_tdo_para) {
376 LOG_ERROR("not enough memory");
381 svf_buffer_index = 0;
382 /* double the buffer size */
383 /* in case current command cannot be committed, and next command is a bit scan command */
384 /* here is 32K bits for this big scan command, it should be enough */
385 /* buffer will be reallocated if buffer size is not enough */
386 svf_tdi_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
387 if (NULL == svf_tdi_buffer) {
388 LOG_ERROR("not enough memory");
392 svf_tdo_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
393 if (NULL == svf_tdo_buffer) {
394 LOG_ERROR("not enough memory");
398 svf_mask_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
399 if (NULL == svf_mask_buffer) {
400 LOG_ERROR("not enough memory");
404 svf_buffer_size = 2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT;
406 memcpy(&svf_para, &svf_para_init, sizeof(svf_para));
414 /* Tap is specified, set header/trailer paddings */
415 int header_ir_len = 0, header_dr_len = 0, trailer_ir_len = 0, trailer_dr_len = 0;
416 struct jtag_tap *check_tap;
418 svf_tap_is_specified = 1;
420 for (check_tap = jtag_all_taps(); check_tap; check_tap = check_tap->next_tap) {
421 if (check_tap->abs_chain_position < tap->abs_chain_position) {
423 header_ir_len += check_tap->ir_length;
425 } else if (check_tap->abs_chain_position > tap->abs_chain_position) {
427 trailer_ir_len += check_tap->ir_length;
433 if (ERROR_OK != svf_set_padding(&svf_para.hdr_para, header_dr_len, 0)) {
434 LOG_ERROR("failed to set data header");
438 /* HIR %d TDI (0xFF) */
439 if (ERROR_OK != svf_set_padding(&svf_para.hir_para, header_ir_len, 0xFF)) {
440 LOG_ERROR("failed to set instruction header");
445 if (ERROR_OK != svf_set_padding(&svf_para.tdr_para, trailer_dr_len, 0)) {
446 LOG_ERROR("failed to set data trailer");
450 /* TIR %d TDI (0xFF) */
451 if (ERROR_OK != svf_set_padding(&svf_para.tir_para, trailer_ir_len, 0xFF)) {
452 LOG_ERROR("failed to set instruction trailer");
457 if (svf_progress_enabled) {
458 /* Count total lines in file. */
459 while (!feof(svf_fd)) {
460 svf_getline(&svf_command_buffer, &svf_command_buffer_size, svf_fd);
465 while (ERROR_OK == svf_read_command_from_file(svf_fd)) {
468 if (svf_progress_enabled) {
469 svf_percentage = ((svf_line_number * 20) / svf_total_lines) * 5;
470 if (svf_last_printed_percentage != svf_percentage) {
471 LOG_USER_N("\r%d%% ", svf_percentage);
472 svf_last_printed_percentage = svf_percentage;
476 if (svf_progress_enabled) {
477 svf_percentage = ((svf_line_number * 20) / svf_total_lines) * 5;
478 LOG_USER_N("%3d%% %s", svf_percentage, svf_read_line);
480 LOG_USER_N("%s", svf_read_line);
483 if (ERROR_OK != svf_run_command(CMD_CTX, svf_command_buffer)) {
484 LOG_ERROR("fail to run command at line %d", svf_line_number);
491 if ((!svf_nil) && (ERROR_OK != jtag_execute_queue()))
493 else if (ERROR_OK != svf_check_tdo())
497 time_measure_ms = timeval_ms() - time_measure_ms;
498 time_measure_s = time_measure_ms / 1000;
499 time_measure_ms %= 1000;
500 time_measure_m = time_measure_s / 60;
501 time_measure_s %= 60;
502 if (time_measure_ms < 1000)
503 command_print(CMD_CTX,
504 "\r\nTime used: %dm%ds%lldms ",
515 if (svf_command_buffer) {
516 free(svf_command_buffer);
517 svf_command_buffer = NULL;
518 svf_command_buffer_size = 0;
520 if (svf_check_tdo_para) {
521 free(svf_check_tdo_para);
522 svf_check_tdo_para = NULL;
523 svf_check_tdo_para_index = 0;
525 if (svf_tdi_buffer) {
526 free(svf_tdi_buffer);
527 svf_tdi_buffer = NULL;
529 if (svf_tdo_buffer) {
530 free(svf_tdo_buffer);
531 svf_tdo_buffer = NULL;
533 if (svf_mask_buffer) {
534 free(svf_mask_buffer);
535 svf_mask_buffer = NULL;
537 svf_buffer_index = 0;
540 svf_free_xxd_para(&svf_para.hdr_para);
541 svf_free_xxd_para(&svf_para.hir_para);
542 svf_free_xxd_para(&svf_para.tdr_para);
543 svf_free_xxd_para(&svf_para.tir_para);
544 svf_free_xxd_para(&svf_para.sdr_para);
545 svf_free_xxd_para(&svf_para.sir_para);
548 command_print(CMD_CTX,
549 "svf file programmed successfully for %d commands",
552 command_print(CMD_CTX, "svf file programmed failed");
557 static int svf_getline(char **lineptr, size_t *n, FILE *stream)
559 #define MIN_CHUNK 16 /* Buffer is increased by this size each time as required */
562 if (*lineptr == NULL) {
564 *lineptr = (char *)malloc(*n);
569 (*lineptr)[0] = fgetc(stream);
570 while ((*lineptr)[i] != '\n') {
571 (*lineptr)[++i] = fgetc(stream);
578 *lineptr = realloc(*lineptr, *n);
584 return sizeof(*lineptr);
587 #define SVFP_CMD_INC_CNT 1024
588 static int svf_read_command_from_file(FILE *fd)
593 int cmd_ok = 0, slash = 0;
595 if (svf_getline(&svf_read_line, &svf_read_line_size, svf_fd) <= 0)
598 ch = svf_read_line[0];
599 while (!cmd_ok && (ch != 0)) {
603 if (svf_getline(&svf_read_line, &svf_read_line_size, svf_fd) <= 0)
611 if (svf_getline(&svf_read_line, &svf_read_line_size,
624 if (svf_getline(&svf_read_line, &svf_read_line_size, svf_fd) <= 0)
629 /* Don't save '\r' and '\n' if no data is parsed */
633 /* The parsing code currently expects a space
634 * before parentheses -- "TDI (123)". Also a
635 * space afterwards -- "TDI (123) TDO(456)".
636 * But such spaces are optional... instead of
637 * parser updates, cope with that by adding the
640 * Ensure there are 3 bytes available, for:
641 * - current character
643 * - terminating NUL ('\0')
645 if ((cmd_pos + 2) >= svf_command_buffer_size) {
646 svf_command_buffer = realloc(svf_command_buffer, (cmd_pos + 2));
647 if (svf_command_buffer == NULL) {
648 LOG_ERROR("not enough memory");
653 /* insert a space before '(' */
655 svf_command_buffer[cmd_pos++] = ' ';
657 svf_command_buffer[cmd_pos++] = (char)toupper(ch);
659 /* insert a space after ')' */
661 svf_command_buffer[cmd_pos++] = ' ';
664 ch = svf_read_line[++i];
668 svf_command_buffer[cmd_pos] = '\0';
674 static int svf_parse_cmd_string(char *str, int len, char **argus, int *num_of_argu)
676 int pos = 0, num = 0, space_found = 1, in_bracket = 0;
682 LOG_ERROR("fail to parse svf command");
692 if (!in_bracket && isspace((int) str[pos])) {
695 } else if (space_found) {
696 argus[num++] = &str[pos];
709 bool svf_tap_state_is_stable(tap_state_t state)
711 return (TAP_RESET == state) || (TAP_IDLE == state)
712 || (TAP_DRPAUSE == state) || (TAP_IRPAUSE == state);
715 static int svf_find_string_in_array(char *str, char **strs, int num_of_element)
719 for (i = 0; i < num_of_element; i++) {
720 if (!strcmp(str, strs[i]))
726 static int svf_adjust_array_length(uint8_t **arr, int orig_bit_len, int new_bit_len)
728 int new_byte_len = (new_bit_len + 7) >> 3;
730 if ((NULL == *arr) || (((orig_bit_len + 7) >> 3) < ((new_bit_len + 7) >> 3))) {
735 *arr = (uint8_t *)malloc(new_byte_len);
737 LOG_ERROR("not enough memory");
740 memset(*arr, 0, new_byte_len);
745 static int svf_set_padding(struct svf_xxr_para *para, int len, unsigned char tdi)
747 int error = ERROR_OK;
748 error |= svf_adjust_array_length(¶->tdi, para->len, len);
749 memset(para->tdi, tdi, (len + 7) >> 3);
750 error |= svf_adjust_array_length(¶->tdo, para->len, len);
751 error |= svf_adjust_array_length(¶->mask, para->len, len);
753 para->data_mask = XXR_TDI;
758 static int svf_copy_hexstring_to_binary(char *str, uint8_t **bin, int orig_bit_len, int bit_len)
760 int i, str_len = strlen(str), str_hbyte_len = (bit_len + 3) >> 2;
763 if (ERROR_OK != svf_adjust_array_length(bin, orig_bit_len, bit_len)) {
764 LOG_ERROR("fail to adjust length of array");
768 /* fill from LSB (end of str) to MSB (beginning of str) */
769 for (i = 0; i < str_hbyte_len; i++) {
771 while (str_len > 0) {
774 /* Skip whitespace. The SVF specification (rev E) is
775 * deficient in terms of basic lexical issues like
776 * where whitespace is allowed. Long bitstrings may
777 * require line ends for correctness, since there is
778 * a hard limit on line length.
781 if ((ch >= '0') && (ch <= '9')) {
784 } else if ((ch >= 'A') && (ch <= 'F')) {
788 LOG_ERROR("invalid hex string");
799 (*bin)[i / 2] |= ch << 4;
807 /* consume optional leading '0' MSBs or whitespace */
808 while (str_len > 0 && ((str[str_len - 1] == '0')
809 || isspace((int) str[str_len - 1])))
812 /* check validity: we must have consumed everything */
813 if (str_len > 0 || (ch & ~((2 << ((bit_len - 1) % 4)) - 1)) != 0) {
814 LOG_ERROR("value execeeds length");
821 static int svf_check_tdo(void)
823 int i, len, index_var;
825 for (i = 0; i < svf_check_tdo_para_index; i++) {
826 index_var = svf_check_tdo_para[i].buffer_offset;
827 len = svf_check_tdo_para[i].bit_len;
828 if ((svf_check_tdo_para[i].enabled)
829 && buf_cmp_mask(&svf_tdi_buffer[index_var], &svf_tdo_buffer[index_var],
830 &svf_mask_buffer[index_var], len)) {
832 unsigned received, expected, tapmask;
833 bitmask = svf_get_mask_u32(svf_check_tdo_para[i].bit_len);
835 memcpy(&received, svf_tdi_buffer + index_var, sizeof(unsigned));
836 memcpy(&expected, svf_tdo_buffer + index_var, sizeof(unsigned));
837 memcpy(&tapmask, svf_mask_buffer + index_var, sizeof(unsigned));
838 LOG_ERROR("tdo check error at line %d",
839 svf_check_tdo_para[i].line_num);
840 LOG_ERROR("read = 0x%X, want = 0x%X, mask = 0x%X",
847 svf_check_tdo_para_index = 0;
852 static int svf_add_check_para(uint8_t enabled, int buffer_offset, int bit_len)
854 if (svf_check_tdo_para_index >= SVF_CHECK_TDO_PARA_SIZE) {
855 LOG_ERROR("toooooo many operation undone");
859 svf_check_tdo_para[svf_check_tdo_para_index].line_num = svf_line_number;
860 svf_check_tdo_para[svf_check_tdo_para_index].bit_len = bit_len;
861 svf_check_tdo_para[svf_check_tdo_para_index].enabled = enabled;
862 svf_check_tdo_para[svf_check_tdo_para_index].buffer_offset = buffer_offset;
863 svf_check_tdo_para_index++;
868 static int svf_execute_tap(void)
870 if ((!svf_nil) && (ERROR_OK != jtag_execute_queue()))
872 else if (ERROR_OK != svf_check_tdo())
875 svf_buffer_index = 0;
880 static int svf_run_command(struct command_context *cmd_ctx, char *cmd_str)
882 char *argus[256], command;
883 int num_of_argu = 0, i;
892 struct svf_xxr_para *xxr_para_tmp;
893 uint8_t **pbuffer_tmp;
894 struct scan_field field;
896 tap_state_t *path = NULL, state;
897 /* flag padding commands skipped due to -tap command */
898 int padding_command_skipped = 0;
900 if (ERROR_OK != svf_parse_cmd_string(cmd_str, strlen(cmd_str), argus, &num_of_argu))
903 /* NOTE: we're a bit loose here, because we ignore case in
904 * TAP state names (instead of insisting on uppercase).
907 command = svf_find_string_in_array(argus[0],
908 (char **)svf_command_name, ARRAY_SIZE(svf_command_name));
912 if (num_of_argu != 2) {
913 LOG_ERROR("invalid parameter of %s", argus[0]);
917 i_tmp = tap_state_by_name(argus[1]);
919 if (svf_tap_state_is_stable(i_tmp)) {
920 if (command == ENDIR) {
921 svf_para.ir_end_state = i_tmp;
922 LOG_DEBUG("\tIR end_state = %s",
923 tap_state_name(i_tmp));
925 svf_para.dr_end_state = i_tmp;
926 LOG_DEBUG("\tDR end_state = %s",
927 tap_state_name(i_tmp));
930 LOG_ERROR("%s: %s is not a stable state",
936 if ((num_of_argu != 1) && (num_of_argu != 3)) {
937 LOG_ERROR("invalid parameter of %s", argus[0]);
940 if (1 == num_of_argu) {
941 /* TODO: set jtag speed to full speed */
942 svf_para.frequency = 0;
944 if (strcmp(argus[2], "HZ")) {
945 LOG_ERROR("HZ not found in FREQUENCY command");
948 if (ERROR_OK != svf_execute_tap())
950 svf_para.frequency = atof(argus[1]);
951 /* TODO: set jtag speed to */
952 if (svf_para.frequency > 0) {
953 command_run_linef(cmd_ctx,
955 (int)svf_para.frequency / 1000);
956 LOG_DEBUG("\tfrequency = %f", svf_para.frequency);
961 if (svf_tap_is_specified) {
962 padding_command_skipped = 1;
965 xxr_para_tmp = &svf_para.hdr_para;
968 if (svf_tap_is_specified) {
969 padding_command_skipped = 1;
972 xxr_para_tmp = &svf_para.hir_para;
975 if (svf_tap_is_specified) {
976 padding_command_skipped = 1;
979 xxr_para_tmp = &svf_para.tdr_para;
982 if (svf_tap_is_specified) {
983 padding_command_skipped = 1;
986 xxr_para_tmp = &svf_para.tir_para;
989 xxr_para_tmp = &svf_para.sdr_para;
992 xxr_para_tmp = &svf_para.sir_para;
995 /* XXR length [TDI (tdi)] [TDO (tdo)][MASK (mask)] [SMASK (smask)] */
996 if ((num_of_argu > 10) || (num_of_argu % 2)) {
997 LOG_ERROR("invalid parameter of %s", argus[0]);
1000 i_tmp = xxr_para_tmp->len;
1001 xxr_para_tmp->len = atoi(argus[1]);
1002 LOG_DEBUG("\tlength = %d", xxr_para_tmp->len);
1003 xxr_para_tmp->data_mask = 0;
1004 for (i = 2; i < num_of_argu; i += 2) {
1005 if ((strlen(argus[i + 1]) < 3) || (argus[i + 1][0] != '(') ||
1006 (argus[i + 1][strlen(argus[i + 1]) - 1] != ')')) {
1007 LOG_ERROR("data section error");
1010 argus[i + 1][strlen(argus[i + 1]) - 1] = '\0';
1011 /* TDI, TDO, MASK, SMASK */
1012 if (!strcmp(argus[i], "TDI")) {
1014 pbuffer_tmp = &xxr_para_tmp->tdi;
1015 xxr_para_tmp->data_mask |= XXR_TDI;
1016 } else if (!strcmp(argus[i], "TDO")) {
1018 pbuffer_tmp = &xxr_para_tmp->tdo;
1019 xxr_para_tmp->data_mask |= XXR_TDO;
1020 } else if (!strcmp(argus[i], "MASK")) {
1022 pbuffer_tmp = &xxr_para_tmp->mask;
1023 xxr_para_tmp->data_mask |= XXR_MASK;
1024 } else if (!strcmp(argus[i], "SMASK")) {
1026 pbuffer_tmp = &xxr_para_tmp->smask;
1027 xxr_para_tmp->data_mask |= XXR_SMASK;
1029 LOG_ERROR("unknow parameter: %s", argus[i]);
1033 svf_copy_hexstring_to_binary(&argus[i + 1][1], pbuffer_tmp, i_tmp,
1034 xxr_para_tmp->len)) {
1035 LOG_ERROR("fail to parse hex value");
1038 LOG_DEBUG("\t%s = 0x%X", argus[i],
1039 (**(int **)pbuffer_tmp) & svf_get_mask_u32(xxr_para_tmp->len));
1041 /* If a command changes the length of the last scan of the same type and the
1042 * MASK parameter is absent, */
1043 /* the mask pattern used is all cares */
1044 if (!(xxr_para_tmp->data_mask & XXR_MASK) && (i_tmp != xxr_para_tmp->len)) {
1045 /* MASK not defined and length changed */
1047 svf_adjust_array_length(&xxr_para_tmp->mask, i_tmp,
1048 xxr_para_tmp->len)) {
1049 LOG_ERROR("fail to adjust length of array");
1052 buf_set_ones(xxr_para_tmp->mask, xxr_para_tmp->len);
1054 /* If TDO is absent, no comparison is needed, set the mask to 0 */
1055 if (!(xxr_para_tmp->data_mask & XXR_TDO)) {
1056 if (NULL == xxr_para_tmp->tdo) {
1058 svf_adjust_array_length(&xxr_para_tmp->tdo, i_tmp,
1059 xxr_para_tmp->len)) {
1060 LOG_ERROR("fail to adjust length of array");
1064 if (NULL == xxr_para_tmp->mask) {
1066 svf_adjust_array_length(&xxr_para_tmp->mask, i_tmp,
1067 xxr_para_tmp->len)) {
1068 LOG_ERROR("fail to adjust length of array");
1072 memset(xxr_para_tmp->mask, 0, (xxr_para_tmp->len + 7) >> 3);
1074 /* do scan if necessary */
1075 if (SDR == command) {
1076 /* check buffer size first, reallocate if necessary */
1077 i = svf_para.hdr_para.len + svf_para.sdr_para.len +
1078 svf_para.tdr_para.len;
1079 if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3)) {
1081 /* simply print error message */
1082 LOG_ERROR("buffer is not enough, report to author");
1085 uint8_t *buffer_tmp;
1087 /* reallocate buffer */
1088 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1089 if (NULL == buffer_tmp) {
1090 LOG_ERROR("not enough memory");
1093 memcpy(buffer_tmp, svf_tdi_buffer, svf_buffer_index);
1094 /* svf_tdi_buffer isn't NULL here */
1095 free(svf_tdi_buffer);
1096 svf_tdi_buffer = buffer_tmp;
1098 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1099 if (NULL == buffer_tmp) {
1100 LOG_ERROR("not enough memory");
1103 memcpy(buffer_tmp, svf_tdo_buffer, svf_buffer_index);
1104 /* svf_tdo_buffer isn't NULL here */
1105 free(svf_tdo_buffer);
1106 svf_tdo_buffer = buffer_tmp;
1108 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1109 if (NULL == buffer_tmp) {
1110 LOG_ERROR("not enough memory");
1113 memcpy(buffer_tmp, svf_mask_buffer, svf_buffer_index);
1114 /* svf_mask_buffer isn't NULL here */
1115 free(svf_mask_buffer);
1116 svf_mask_buffer = buffer_tmp;
1119 svf_buffer_size = svf_buffer_index + ((i + 7) >> 3);
1123 /* assemble dr data */
1125 buf_set_buf(svf_para.hdr_para.tdi,
1127 &svf_tdi_buffer[svf_buffer_index],
1129 svf_para.hdr_para.len);
1130 i += svf_para.hdr_para.len;
1131 buf_set_buf(svf_para.sdr_para.tdi,
1133 &svf_tdi_buffer[svf_buffer_index],
1135 svf_para.sdr_para.len);
1136 i += svf_para.sdr_para.len;
1137 buf_set_buf(svf_para.tdr_para.tdi,
1139 &svf_tdi_buffer[svf_buffer_index],
1141 svf_para.tdr_para.len);
1142 i += svf_para.tdr_para.len;
1144 /* add check data */
1145 if (svf_para.sdr_para.data_mask & XXR_TDO) {
1146 /* assemble dr mask data */
1148 buf_set_buf(svf_para.hdr_para.mask,
1150 &svf_mask_buffer[svf_buffer_index],
1152 svf_para.hdr_para.len);
1153 i += svf_para.hdr_para.len;
1154 buf_set_buf(svf_para.sdr_para.mask,
1156 &svf_mask_buffer[svf_buffer_index],
1158 svf_para.sdr_para.len);
1159 i += svf_para.sdr_para.len;
1160 buf_set_buf(svf_para.tdr_para.mask,
1162 &svf_mask_buffer[svf_buffer_index],
1164 svf_para.tdr_para.len);
1166 /* assemble dr check data */
1168 buf_set_buf(svf_para.hdr_para.tdo,
1170 &svf_tdo_buffer[svf_buffer_index],
1172 svf_para.hdr_para.len);
1173 i += svf_para.hdr_para.len;
1174 buf_set_buf(svf_para.sdr_para.tdo,
1176 &svf_tdo_buffer[svf_buffer_index],
1178 svf_para.sdr_para.len);
1179 i += svf_para.sdr_para.len;
1180 buf_set_buf(svf_para.tdr_para.tdo,
1182 &svf_tdo_buffer[svf_buffer_index],
1184 svf_para.tdr_para.len);
1185 i += svf_para.tdr_para.len;
1187 svf_add_check_para(1, svf_buffer_index, i);
1189 svf_add_check_para(0, svf_buffer_index, i);
1191 field.out_value = &svf_tdi_buffer[svf_buffer_index];
1192 field.in_value = &svf_tdi_buffer[svf_buffer_index];
1194 /* NOTE: doesn't use SVF-specified state paths */
1195 jtag_add_plain_dr_scan(field.num_bits,
1198 svf_para.dr_end_state);
1201 svf_buffer_index += (i + 7) >> 3;
1202 } else if (SIR == command) {
1203 /* check buffer size first, reallocate if necessary */
1204 i = svf_para.hir_para.len + svf_para.sir_para.len +
1205 svf_para.tir_para.len;
1206 if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3)) {
1208 /* simply print error message */
1209 LOG_ERROR("buffer is not enough, report to author");
1212 uint8_t *buffer_tmp;
1214 /* reallocate buffer */
1215 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1216 if (NULL == buffer_tmp) {
1217 LOG_ERROR("not enough memory");
1220 memcpy(buffer_tmp, svf_tdi_buffer, svf_buffer_index);
1221 /* svf_tdi_buffer isn't NULL here */
1222 free(svf_tdi_buffer);
1223 svf_tdi_buffer = buffer_tmp;
1225 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1226 if (NULL == buffer_tmp) {
1227 LOG_ERROR("not enough memory");
1230 memcpy(buffer_tmp, svf_tdo_buffer, svf_buffer_index);
1231 /* svf_tdo_buffer isn't NULL here */
1232 free(svf_tdo_buffer);
1233 svf_tdo_buffer = buffer_tmp;
1235 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1236 if (NULL == buffer_tmp) {
1237 LOG_ERROR("not enough memory");
1240 memcpy(buffer_tmp, svf_mask_buffer, svf_buffer_index);
1241 /* svf_mask_buffer isn't NULL here */
1242 free(svf_mask_buffer);
1243 svf_mask_buffer = buffer_tmp;
1246 svf_buffer_size = svf_buffer_index + ((i + 7) >> 3);
1250 /* assemble ir data */
1252 buf_set_buf(svf_para.hir_para.tdi,
1254 &svf_tdi_buffer[svf_buffer_index],
1256 svf_para.hir_para.len);
1257 i += svf_para.hir_para.len;
1258 buf_set_buf(svf_para.sir_para.tdi,
1260 &svf_tdi_buffer[svf_buffer_index],
1262 svf_para.sir_para.len);
1263 i += svf_para.sir_para.len;
1264 buf_set_buf(svf_para.tir_para.tdi,
1266 &svf_tdi_buffer[svf_buffer_index],
1268 svf_para.tir_para.len);
1269 i += svf_para.tir_para.len;
1271 /* add check data */
1272 if (svf_para.sir_para.data_mask & XXR_TDO) {
1273 /* assemble dr mask data */
1275 buf_set_buf(svf_para.hir_para.mask,
1277 &svf_mask_buffer[svf_buffer_index],
1279 svf_para.hir_para.len);
1280 i += svf_para.hir_para.len;
1281 buf_set_buf(svf_para.sir_para.mask,
1283 &svf_mask_buffer[svf_buffer_index],
1285 svf_para.sir_para.len);
1286 i += svf_para.sir_para.len;
1287 buf_set_buf(svf_para.tir_para.mask,
1289 &svf_mask_buffer[svf_buffer_index],
1291 svf_para.tir_para.len);
1293 /* assemble dr check data */
1295 buf_set_buf(svf_para.hir_para.tdo,
1297 &svf_tdo_buffer[svf_buffer_index],
1299 svf_para.hir_para.len);
1300 i += svf_para.hir_para.len;
1301 buf_set_buf(svf_para.sir_para.tdo,
1303 &svf_tdo_buffer[svf_buffer_index],
1305 svf_para.sir_para.len);
1306 i += svf_para.sir_para.len;
1307 buf_set_buf(svf_para.tir_para.tdo,
1309 &svf_tdo_buffer[svf_buffer_index],
1311 svf_para.tir_para.len);
1312 i += svf_para.tir_para.len;
1314 svf_add_check_para(1, svf_buffer_index, i);
1316 svf_add_check_para(0, svf_buffer_index, i);
1318 field.out_value = &svf_tdi_buffer[svf_buffer_index];
1319 field.in_value = &svf_tdi_buffer[svf_buffer_index];
1321 /* NOTE: doesn't use SVF-specified state paths */
1322 jtag_add_plain_ir_scan(field.num_bits,
1325 svf_para.ir_end_state);
1328 svf_buffer_index += (i + 7) >> 3;
1333 LOG_ERROR("PIO and PIOMAP are not supported");
1337 /* RUNTEST [run_state] run_count run_clk [min_time SEC [MAXIMUM max_time
1338 * SEC]] [ENDSTATE end_state] */
1339 /* RUNTEST [run_state] min_time SEC [MAXIMUM max_time SEC] [ENDSTATE
1341 if ((num_of_argu < 3) && (num_of_argu > 11)) {
1342 LOG_ERROR("invalid parameter of %s", argus[0]);
1351 i_tmp = tap_state_by_name(argus[i]);
1352 if (i_tmp != TAP_INVALID) {
1353 if (svf_tap_state_is_stable(i_tmp)) {
1354 svf_para.runtest_run_state = i_tmp;
1356 /* When a run_state is specified, the new
1357 * run_state becomes the default end_state.
1359 svf_para.runtest_end_state = i_tmp;
1360 LOG_DEBUG("\trun_state = %s", tap_state_name(i_tmp));
1363 LOG_ERROR("%s: %s is not a stable state", argus[0], tap_state_name(i_tmp));
1368 /* run_count run_clk */
1369 if (((i + 2) <= num_of_argu) && strcmp(argus[i + 1], "SEC")) {
1370 if (!strcmp(argus[i + 1], "TCK")) {
1371 /* clock source is TCK */
1372 run_count = atoi(argus[i]);
1373 LOG_DEBUG("\trun_count@TCK = %d", run_count);
1375 LOG_ERROR("%s not supported for clock", argus[i + 1]);
1381 if (((i + 2) <= num_of_argu) && !strcmp(argus[i + 1], "SEC")) {
1382 min_time = atof(argus[i]);
1383 LOG_DEBUG("\tmin_time = %fs", min_time);
1386 /* MAXIMUM max_time SEC */
1387 if (((i + 3) <= num_of_argu) &&
1388 !strcmp(argus[i], "MAXIMUM") && !strcmp(argus[i + 2], "SEC")) {
1390 max_time = atof(argus[i + 1]);
1391 LOG_DEBUG("\tmax_time = %fs", max_time);
1394 /* ENDSTATE end_state */
1395 if (((i + 2) <= num_of_argu) && !strcmp(argus[i], "ENDSTATE")) {
1396 i_tmp = tap_state_by_name(argus[i + 1]);
1398 if (svf_tap_state_is_stable(i_tmp)) {
1399 svf_para.runtest_end_state = i_tmp;
1400 LOG_DEBUG("\tend_state = %s", tap_state_name(i_tmp));
1402 LOG_ERROR("%s: %s is not a stable state", argus[0], tap_state_name(i_tmp));
1408 /* all parameter should be parsed */
1409 if (i == num_of_argu) {
1411 /* FIXME handle statemove failures */
1412 uint32_t min_usec = 1000000 * min_time;
1414 /* enter into run_state if necessary */
1415 if (cmd_queue_cur_state != svf_para.runtest_run_state)
1416 svf_add_statemove(svf_para.runtest_run_state);
1418 /* add clocks and/or min wait */
1419 if (run_count > 0) {
1421 jtag_add_clocks(run_count);
1426 jtag_add_sleep(min_usec);
1429 /* move to end_state if necessary */
1430 if (svf_para.runtest_end_state != svf_para.runtest_run_state)
1431 svf_add_statemove(svf_para.runtest_end_state);
1434 if (svf_para.runtest_run_state != TAP_IDLE) {
1435 LOG_ERROR("cannot runtest in %s state",
1436 tap_state_name(svf_para.runtest_run_state));
1441 jtag_add_runtest(run_count, svf_para.runtest_end_state);
1444 LOG_ERROR("fail to parse parameter of RUNTEST, %d out of %d is parsed",
1451 /* STATE [pathstate1 [pathstate2 ...[pathstaten]]] stable_state */
1452 if (num_of_argu < 2) {
1453 LOG_ERROR("invalid parameter of %s", argus[0]);
1456 if (num_of_argu > 2) {
1457 /* STATE pathstate1 ... stable_state */
1458 path = (tap_state_t *)malloc((num_of_argu - 1) * sizeof(tap_state_t));
1460 LOG_ERROR("not enough memory");
1463 num_of_argu--; /* num of path */
1464 i_tmp = 1; /* path is from parameter 1 */
1465 for (i = 0; i < num_of_argu; i++, i_tmp++) {
1466 path[i] = tap_state_by_name(argus[i_tmp]);
1467 if (path[i] == TAP_INVALID) {
1468 LOG_ERROR("%s: %s is not a valid state", argus[0], argus[i_tmp]);
1472 /* OpenOCD refuses paths containing TAP_RESET */
1473 if (TAP_RESET == path[i]) {
1474 /* FIXME last state MUST be stable! */
1477 jtag_add_pathmove(i, path);
1481 num_of_argu -= i + 1;
1485 if (num_of_argu > 0) {
1486 /* execute last path if necessary */
1487 if (svf_tap_state_is_stable(path[num_of_argu - 1])) {
1488 /* last state MUST be stable state */
1490 jtag_add_pathmove(num_of_argu, path);
1491 LOG_DEBUG("\tmove to %s by path_move",
1492 tap_state_name(path[num_of_argu - 1]));
1494 LOG_ERROR("%s: %s is not a stable state",
1496 tap_state_name(path[num_of_argu - 1]));
1505 /* STATE stable_state */
1506 state = tap_state_by_name(argus[1]);
1507 if (svf_tap_state_is_stable(state)) {
1508 LOG_DEBUG("\tmove to %s by svf_add_statemove",
1509 tap_state_name(state));
1510 /* FIXME handle statemove failures */
1511 svf_add_statemove(state);
1513 LOG_ERROR("%s: %s is not a stable state",
1514 argus[0], tap_state_name(state));
1520 /* TRST trst_mode */
1521 if (num_of_argu != 2) {
1522 LOG_ERROR("invalid parameter of %s", argus[0]);
1525 if (svf_para.trst_mode != TRST_ABSENT) {
1526 if (ERROR_OK != svf_execute_tap())
1528 i_tmp = svf_find_string_in_array(argus[1],
1529 (char **)svf_trst_mode_name,
1530 ARRAY_SIZE(svf_trst_mode_name));
1534 jtag_add_reset(1, 0);
1539 jtag_add_reset(0, 0);
1544 LOG_ERROR("unknown TRST mode: %s", argus[1]);
1547 svf_para.trst_mode = i_tmp;
1548 LOG_DEBUG("\ttrst_mode = %s", svf_trst_mode_name[svf_para.trst_mode]);
1550 LOG_ERROR("can not accpet TRST command if trst_mode is ABSENT");
1555 LOG_ERROR("invalid svf command: %s", argus[0]);
1561 if (padding_command_skipped)
1562 LOG_USER("(Above Padding command skipped, as per -tap argument)");
1565 if (debug_level >= LOG_LVL_DEBUG) {
1566 /* for convenient debugging, execute tap if possible */
1567 if ((svf_buffer_index > 0) && \
1568 (((command != STATE) && (command != RUNTEST)) || \
1569 ((command == STATE) && (num_of_argu == 2)))) {
1570 if (ERROR_OK != svf_execute_tap())
1573 /* output debug info */
1574 if ((SIR == command) || (SDR == command)) {
1576 memcpy(&read_value, svf_tdi_buffer, sizeof(int));
1577 /* in debug mode, data is from index 0 */
1578 int read_mask = svf_get_mask_u32(svf_check_tdo_para[0].bit_len);
1579 LOG_DEBUG("\tTDO read = 0x%X", read_value & read_mask);
1583 /* for fast executing, execute tap if necessary */
1584 /* half of the buffer is for the next command */
1585 if (((svf_buffer_index >= SVF_MAX_BUFFER_SIZE_TO_COMMIT) ||
1586 (svf_check_tdo_para_index >= SVF_CHECK_TDO_PARA_SIZE / 2)) && \
1587 (((command != STATE) && (command != RUNTEST)) || \
1588 ((command == STATE) && (num_of_argu == 2))))
1589 return svf_execute_tap();
1595 static const struct command_registration svf_command_handlers[] = {
1598 .handler = handle_svf_command,
1599 .mode = COMMAND_EXEC,
1600 .help = "Runs a SVF file.",
1601 .usage = "svf [-tap device.tap] <file> [quiet] [nil] [progress]",
1603 COMMAND_REGISTRATION_DONE
1606 int svf_register_commands(struct command_context *cmd_ctx)
1608 return register_commands(cmd_ctx, NULL, svf_command_handlers);