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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 + 3 > svf_command_buffer_size) {
646 svf_command_buffer = realloc(svf_command_buffer, cmd_pos + 3);
647 svf_command_buffer_size = cmd_pos + 3;
648 if (svf_command_buffer == NULL) {
649 LOG_ERROR("not enough memory");
654 /* insert a space before '(' */
656 svf_command_buffer[cmd_pos++] = ' ';
658 svf_command_buffer[cmd_pos++] = (char)toupper(ch);
660 /* insert a space after ')' */
662 svf_command_buffer[cmd_pos++] = ' ';
665 ch = svf_read_line[++i];
669 svf_command_buffer[cmd_pos] = '\0';
675 static int svf_parse_cmd_string(char *str, int len, char **argus, int *num_of_argu)
677 int pos = 0, num = 0, space_found = 1, in_bracket = 0;
683 LOG_ERROR("fail to parse svf command");
693 if (!in_bracket && isspace((int) str[pos])) {
696 } else if (space_found) {
697 argus[num++] = &str[pos];
710 bool svf_tap_state_is_stable(tap_state_t state)
712 return (TAP_RESET == state) || (TAP_IDLE == state)
713 || (TAP_DRPAUSE == state) || (TAP_IRPAUSE == state);
716 static int svf_find_string_in_array(char *str, char **strs, int num_of_element)
720 for (i = 0; i < num_of_element; i++) {
721 if (!strcmp(str, strs[i]))
727 static int svf_adjust_array_length(uint8_t **arr, int orig_bit_len, int new_bit_len)
729 int new_byte_len = (new_bit_len + 7) >> 3;
731 if ((NULL == *arr) || (((orig_bit_len + 7) >> 3) < ((new_bit_len + 7) >> 3))) {
736 *arr = (uint8_t *)malloc(new_byte_len);
738 LOG_ERROR("not enough memory");
741 memset(*arr, 0, new_byte_len);
746 static int svf_set_padding(struct svf_xxr_para *para, int len, unsigned char tdi)
748 int error = ERROR_OK;
749 error |= svf_adjust_array_length(¶->tdi, para->len, len);
750 memset(para->tdi, tdi, (len + 7) >> 3);
751 error |= svf_adjust_array_length(¶->tdo, para->len, len);
752 error |= svf_adjust_array_length(¶->mask, para->len, len);
754 para->data_mask = XXR_TDI;
759 static int svf_copy_hexstring_to_binary(char *str, uint8_t **bin, int orig_bit_len, int bit_len)
761 int i, str_len = strlen(str), str_hbyte_len = (bit_len + 3) >> 2;
764 if (ERROR_OK != svf_adjust_array_length(bin, orig_bit_len, bit_len)) {
765 LOG_ERROR("fail to adjust length of array");
769 /* fill from LSB (end of str) to MSB (beginning of str) */
770 for (i = 0; i < str_hbyte_len; i++) {
772 while (str_len > 0) {
775 /* Skip whitespace. The SVF specification (rev E) is
776 * deficient in terms of basic lexical issues like
777 * where whitespace is allowed. Long bitstrings may
778 * require line ends for correctness, since there is
779 * a hard limit on line length.
782 if ((ch >= '0') && (ch <= '9')) {
785 } else if ((ch >= 'A') && (ch <= 'F')) {
789 LOG_ERROR("invalid hex string");
800 (*bin)[i / 2] |= ch << 4;
808 /* consume optional leading '0' MSBs or whitespace */
809 while (str_len > 0 && ((str[str_len - 1] == '0')
810 || isspace((int) str[str_len - 1])))
813 /* check validity: we must have consumed everything */
814 if (str_len > 0 || (ch & ~((2 << ((bit_len - 1) % 4)) - 1)) != 0) {
815 LOG_ERROR("value execeeds length");
822 static int svf_check_tdo(void)
824 int i, len, index_var;
826 for (i = 0; i < svf_check_tdo_para_index; i++) {
827 index_var = svf_check_tdo_para[i].buffer_offset;
828 len = svf_check_tdo_para[i].bit_len;
829 if ((svf_check_tdo_para[i].enabled)
830 && buf_cmp_mask(&svf_tdi_buffer[index_var], &svf_tdo_buffer[index_var],
831 &svf_mask_buffer[index_var], len)) {
833 unsigned received, expected, tapmask;
834 bitmask = svf_get_mask_u32(svf_check_tdo_para[i].bit_len);
836 memcpy(&received, svf_tdi_buffer + index_var, sizeof(unsigned));
837 memcpy(&expected, svf_tdo_buffer + index_var, sizeof(unsigned));
838 memcpy(&tapmask, svf_mask_buffer + index_var, sizeof(unsigned));
839 LOG_ERROR("tdo check error at line %d",
840 svf_check_tdo_para[i].line_num);
841 LOG_ERROR("read = 0x%X, want = 0x%X, mask = 0x%X",
848 svf_check_tdo_para_index = 0;
853 static int svf_add_check_para(uint8_t enabled, int buffer_offset, int bit_len)
855 if (svf_check_tdo_para_index >= SVF_CHECK_TDO_PARA_SIZE) {
856 LOG_ERROR("toooooo many operation undone");
860 svf_check_tdo_para[svf_check_tdo_para_index].line_num = svf_line_number;
861 svf_check_tdo_para[svf_check_tdo_para_index].bit_len = bit_len;
862 svf_check_tdo_para[svf_check_tdo_para_index].enabled = enabled;
863 svf_check_tdo_para[svf_check_tdo_para_index].buffer_offset = buffer_offset;
864 svf_check_tdo_para_index++;
869 static int svf_execute_tap(void)
871 if ((!svf_nil) && (ERROR_OK != jtag_execute_queue()))
873 else if (ERROR_OK != svf_check_tdo())
876 svf_buffer_index = 0;
881 static int svf_run_command(struct command_context *cmd_ctx, char *cmd_str)
883 char *argus[256], command;
884 int num_of_argu = 0, i;
893 struct svf_xxr_para *xxr_para_tmp;
894 uint8_t **pbuffer_tmp;
895 struct scan_field field;
897 tap_state_t *path = NULL, state;
898 /* flag padding commands skipped due to -tap command */
899 int padding_command_skipped = 0;
901 if (ERROR_OK != svf_parse_cmd_string(cmd_str, strlen(cmd_str), argus, &num_of_argu))
904 /* NOTE: we're a bit loose here, because we ignore case in
905 * TAP state names (instead of insisting on uppercase).
908 command = svf_find_string_in_array(argus[0],
909 (char **)svf_command_name, ARRAY_SIZE(svf_command_name));
913 if (num_of_argu != 2) {
914 LOG_ERROR("invalid parameter of %s", argus[0]);
918 i_tmp = tap_state_by_name(argus[1]);
920 if (svf_tap_state_is_stable(i_tmp)) {
921 if (command == ENDIR) {
922 svf_para.ir_end_state = i_tmp;
923 LOG_DEBUG("\tIR end_state = %s",
924 tap_state_name(i_tmp));
926 svf_para.dr_end_state = i_tmp;
927 LOG_DEBUG("\tDR end_state = %s",
928 tap_state_name(i_tmp));
931 LOG_ERROR("%s: %s is not a stable state",
937 if ((num_of_argu != 1) && (num_of_argu != 3)) {
938 LOG_ERROR("invalid parameter of %s", argus[0]);
941 if (1 == num_of_argu) {
942 /* TODO: set jtag speed to full speed */
943 svf_para.frequency = 0;
945 if (strcmp(argus[2], "HZ")) {
946 LOG_ERROR("HZ not found in FREQUENCY command");
949 if (ERROR_OK != svf_execute_tap())
951 svf_para.frequency = atof(argus[1]);
952 /* TODO: set jtag speed to */
953 if (svf_para.frequency > 0) {
954 command_run_linef(cmd_ctx,
956 (int)svf_para.frequency / 1000);
957 LOG_DEBUG("\tfrequency = %f", svf_para.frequency);
962 if (svf_tap_is_specified) {
963 padding_command_skipped = 1;
966 xxr_para_tmp = &svf_para.hdr_para;
969 if (svf_tap_is_specified) {
970 padding_command_skipped = 1;
973 xxr_para_tmp = &svf_para.hir_para;
976 if (svf_tap_is_specified) {
977 padding_command_skipped = 1;
980 xxr_para_tmp = &svf_para.tdr_para;
983 if (svf_tap_is_specified) {
984 padding_command_skipped = 1;
987 xxr_para_tmp = &svf_para.tir_para;
990 xxr_para_tmp = &svf_para.sdr_para;
993 xxr_para_tmp = &svf_para.sir_para;
996 /* XXR length [TDI (tdi)] [TDO (tdo)][MASK (mask)] [SMASK (smask)] */
997 if ((num_of_argu > 10) || (num_of_argu % 2)) {
998 LOG_ERROR("invalid parameter of %s", argus[0]);
1001 i_tmp = xxr_para_tmp->len;
1002 xxr_para_tmp->len = atoi(argus[1]);
1003 LOG_DEBUG("\tlength = %d", xxr_para_tmp->len);
1004 xxr_para_tmp->data_mask = 0;
1005 for (i = 2; i < num_of_argu; i += 2) {
1006 if ((strlen(argus[i + 1]) < 3) || (argus[i + 1][0] != '(') ||
1007 (argus[i + 1][strlen(argus[i + 1]) - 1] != ')')) {
1008 LOG_ERROR("data section error");
1011 argus[i + 1][strlen(argus[i + 1]) - 1] = '\0';
1012 /* TDI, TDO, MASK, SMASK */
1013 if (!strcmp(argus[i], "TDI")) {
1015 pbuffer_tmp = &xxr_para_tmp->tdi;
1016 xxr_para_tmp->data_mask |= XXR_TDI;
1017 } else if (!strcmp(argus[i], "TDO")) {
1019 pbuffer_tmp = &xxr_para_tmp->tdo;
1020 xxr_para_tmp->data_mask |= XXR_TDO;
1021 } else if (!strcmp(argus[i], "MASK")) {
1023 pbuffer_tmp = &xxr_para_tmp->mask;
1024 xxr_para_tmp->data_mask |= XXR_MASK;
1025 } else if (!strcmp(argus[i], "SMASK")) {
1027 pbuffer_tmp = &xxr_para_tmp->smask;
1028 xxr_para_tmp->data_mask |= XXR_SMASK;
1030 LOG_ERROR("unknow parameter: %s", argus[i]);
1034 svf_copy_hexstring_to_binary(&argus[i + 1][1], pbuffer_tmp, i_tmp,
1035 xxr_para_tmp->len)) {
1036 LOG_ERROR("fail to parse hex value");
1039 LOG_DEBUG("\t%s = 0x%X", argus[i],
1040 (**(int **)pbuffer_tmp) & svf_get_mask_u32(xxr_para_tmp->len));
1042 /* If a command changes the length of the last scan of the same type and the
1043 * MASK parameter is absent, */
1044 /* the mask pattern used is all cares */
1045 if (!(xxr_para_tmp->data_mask & XXR_MASK) && (i_tmp != xxr_para_tmp->len)) {
1046 /* MASK not defined and length changed */
1048 svf_adjust_array_length(&xxr_para_tmp->mask, i_tmp,
1049 xxr_para_tmp->len)) {
1050 LOG_ERROR("fail to adjust length of array");
1053 buf_set_ones(xxr_para_tmp->mask, xxr_para_tmp->len);
1055 /* If TDO is absent, no comparison is needed, set the mask to 0 */
1056 if (!(xxr_para_tmp->data_mask & XXR_TDO)) {
1057 if (NULL == xxr_para_tmp->tdo) {
1059 svf_adjust_array_length(&xxr_para_tmp->tdo, i_tmp,
1060 xxr_para_tmp->len)) {
1061 LOG_ERROR("fail to adjust length of array");
1065 if (NULL == xxr_para_tmp->mask) {
1067 svf_adjust_array_length(&xxr_para_tmp->mask, i_tmp,
1068 xxr_para_tmp->len)) {
1069 LOG_ERROR("fail to adjust length of array");
1073 memset(xxr_para_tmp->mask, 0, (xxr_para_tmp->len + 7) >> 3);
1075 /* do scan if necessary */
1076 if (SDR == command) {
1077 /* check buffer size first, reallocate if necessary */
1078 i = svf_para.hdr_para.len + svf_para.sdr_para.len +
1079 svf_para.tdr_para.len;
1080 if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3)) {
1082 /* simply print error message */
1083 LOG_ERROR("buffer is not enough, report to author");
1086 uint8_t *buffer_tmp;
1088 /* reallocate buffer */
1089 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1090 if (NULL == buffer_tmp) {
1091 LOG_ERROR("not enough memory");
1094 memcpy(buffer_tmp, svf_tdi_buffer, svf_buffer_index);
1095 /* svf_tdi_buffer isn't NULL here */
1096 free(svf_tdi_buffer);
1097 svf_tdi_buffer = buffer_tmp;
1099 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1100 if (NULL == buffer_tmp) {
1101 LOG_ERROR("not enough memory");
1104 memcpy(buffer_tmp, svf_tdo_buffer, svf_buffer_index);
1105 /* svf_tdo_buffer isn't NULL here */
1106 free(svf_tdo_buffer);
1107 svf_tdo_buffer = buffer_tmp;
1109 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1110 if (NULL == buffer_tmp) {
1111 LOG_ERROR("not enough memory");
1114 memcpy(buffer_tmp, svf_mask_buffer, svf_buffer_index);
1115 /* svf_mask_buffer isn't NULL here */
1116 free(svf_mask_buffer);
1117 svf_mask_buffer = buffer_tmp;
1120 svf_buffer_size = svf_buffer_index + ((i + 7) >> 3);
1124 /* assemble dr data */
1126 buf_set_buf(svf_para.hdr_para.tdi,
1128 &svf_tdi_buffer[svf_buffer_index],
1130 svf_para.hdr_para.len);
1131 i += svf_para.hdr_para.len;
1132 buf_set_buf(svf_para.sdr_para.tdi,
1134 &svf_tdi_buffer[svf_buffer_index],
1136 svf_para.sdr_para.len);
1137 i += svf_para.sdr_para.len;
1138 buf_set_buf(svf_para.tdr_para.tdi,
1140 &svf_tdi_buffer[svf_buffer_index],
1142 svf_para.tdr_para.len);
1143 i += svf_para.tdr_para.len;
1145 /* add check data */
1146 if (svf_para.sdr_para.data_mask & XXR_TDO) {
1147 /* assemble dr mask data */
1149 buf_set_buf(svf_para.hdr_para.mask,
1151 &svf_mask_buffer[svf_buffer_index],
1153 svf_para.hdr_para.len);
1154 i += svf_para.hdr_para.len;
1155 buf_set_buf(svf_para.sdr_para.mask,
1157 &svf_mask_buffer[svf_buffer_index],
1159 svf_para.sdr_para.len);
1160 i += svf_para.sdr_para.len;
1161 buf_set_buf(svf_para.tdr_para.mask,
1163 &svf_mask_buffer[svf_buffer_index],
1165 svf_para.tdr_para.len);
1167 /* assemble dr check data */
1169 buf_set_buf(svf_para.hdr_para.tdo,
1171 &svf_tdo_buffer[svf_buffer_index],
1173 svf_para.hdr_para.len);
1174 i += svf_para.hdr_para.len;
1175 buf_set_buf(svf_para.sdr_para.tdo,
1177 &svf_tdo_buffer[svf_buffer_index],
1179 svf_para.sdr_para.len);
1180 i += svf_para.sdr_para.len;
1181 buf_set_buf(svf_para.tdr_para.tdo,
1183 &svf_tdo_buffer[svf_buffer_index],
1185 svf_para.tdr_para.len);
1186 i += svf_para.tdr_para.len;
1188 svf_add_check_para(1, svf_buffer_index, i);
1190 svf_add_check_para(0, svf_buffer_index, i);
1192 field.out_value = &svf_tdi_buffer[svf_buffer_index];
1193 field.in_value = &svf_tdi_buffer[svf_buffer_index];
1195 /* NOTE: doesn't use SVF-specified state paths */
1196 jtag_add_plain_dr_scan(field.num_bits,
1199 svf_para.dr_end_state);
1202 svf_buffer_index += (i + 7) >> 3;
1203 } else if (SIR == command) {
1204 /* check buffer size first, reallocate if necessary */
1205 i = svf_para.hir_para.len + svf_para.sir_para.len +
1206 svf_para.tir_para.len;
1207 if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3)) {
1209 /* simply print error message */
1210 LOG_ERROR("buffer is not enough, report to author");
1213 uint8_t *buffer_tmp;
1215 /* reallocate buffer */
1216 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1217 if (NULL == buffer_tmp) {
1218 LOG_ERROR("not enough memory");
1221 memcpy(buffer_tmp, svf_tdi_buffer, svf_buffer_index);
1222 /* svf_tdi_buffer isn't NULL here */
1223 free(svf_tdi_buffer);
1224 svf_tdi_buffer = buffer_tmp;
1226 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1227 if (NULL == buffer_tmp) {
1228 LOG_ERROR("not enough memory");
1231 memcpy(buffer_tmp, svf_tdo_buffer, svf_buffer_index);
1232 /* svf_tdo_buffer isn't NULL here */
1233 free(svf_tdo_buffer);
1234 svf_tdo_buffer = buffer_tmp;
1236 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1237 if (NULL == buffer_tmp) {
1238 LOG_ERROR("not enough memory");
1241 memcpy(buffer_tmp, svf_mask_buffer, svf_buffer_index);
1242 /* svf_mask_buffer isn't NULL here */
1243 free(svf_mask_buffer);
1244 svf_mask_buffer = buffer_tmp;
1247 svf_buffer_size = svf_buffer_index + ((i + 7) >> 3);
1251 /* assemble ir data */
1253 buf_set_buf(svf_para.hir_para.tdi,
1255 &svf_tdi_buffer[svf_buffer_index],
1257 svf_para.hir_para.len);
1258 i += svf_para.hir_para.len;
1259 buf_set_buf(svf_para.sir_para.tdi,
1261 &svf_tdi_buffer[svf_buffer_index],
1263 svf_para.sir_para.len);
1264 i += svf_para.sir_para.len;
1265 buf_set_buf(svf_para.tir_para.tdi,
1267 &svf_tdi_buffer[svf_buffer_index],
1269 svf_para.tir_para.len);
1270 i += svf_para.tir_para.len;
1272 /* add check data */
1273 if (svf_para.sir_para.data_mask & XXR_TDO) {
1274 /* assemble dr mask data */
1276 buf_set_buf(svf_para.hir_para.mask,
1278 &svf_mask_buffer[svf_buffer_index],
1280 svf_para.hir_para.len);
1281 i += svf_para.hir_para.len;
1282 buf_set_buf(svf_para.sir_para.mask,
1284 &svf_mask_buffer[svf_buffer_index],
1286 svf_para.sir_para.len);
1287 i += svf_para.sir_para.len;
1288 buf_set_buf(svf_para.tir_para.mask,
1290 &svf_mask_buffer[svf_buffer_index],
1292 svf_para.tir_para.len);
1294 /* assemble dr check data */
1296 buf_set_buf(svf_para.hir_para.tdo,
1298 &svf_tdo_buffer[svf_buffer_index],
1300 svf_para.hir_para.len);
1301 i += svf_para.hir_para.len;
1302 buf_set_buf(svf_para.sir_para.tdo,
1304 &svf_tdo_buffer[svf_buffer_index],
1306 svf_para.sir_para.len);
1307 i += svf_para.sir_para.len;
1308 buf_set_buf(svf_para.tir_para.tdo,
1310 &svf_tdo_buffer[svf_buffer_index],
1312 svf_para.tir_para.len);
1313 i += svf_para.tir_para.len;
1315 svf_add_check_para(1, svf_buffer_index, i);
1317 svf_add_check_para(0, svf_buffer_index, i);
1319 field.out_value = &svf_tdi_buffer[svf_buffer_index];
1320 field.in_value = &svf_tdi_buffer[svf_buffer_index];
1322 /* NOTE: doesn't use SVF-specified state paths */
1323 jtag_add_plain_ir_scan(field.num_bits,
1326 svf_para.ir_end_state);
1329 svf_buffer_index += (i + 7) >> 3;
1334 LOG_ERROR("PIO and PIOMAP are not supported");
1338 /* RUNTEST [run_state] run_count run_clk [min_time SEC [MAXIMUM max_time
1339 * SEC]] [ENDSTATE end_state] */
1340 /* RUNTEST [run_state] min_time SEC [MAXIMUM max_time SEC] [ENDSTATE
1342 if ((num_of_argu < 3) && (num_of_argu > 11)) {
1343 LOG_ERROR("invalid parameter of %s", argus[0]);
1352 i_tmp = tap_state_by_name(argus[i]);
1353 if (i_tmp != TAP_INVALID) {
1354 if (svf_tap_state_is_stable(i_tmp)) {
1355 svf_para.runtest_run_state = i_tmp;
1357 /* When a run_state is specified, the new
1358 * run_state becomes the default end_state.
1360 svf_para.runtest_end_state = i_tmp;
1361 LOG_DEBUG("\trun_state = %s", tap_state_name(i_tmp));
1364 LOG_ERROR("%s: %s is not a stable state", argus[0], tap_state_name(i_tmp));
1369 /* run_count run_clk */
1370 if (((i + 2) <= num_of_argu) && strcmp(argus[i + 1], "SEC")) {
1371 if (!strcmp(argus[i + 1], "TCK")) {
1372 /* clock source is TCK */
1373 run_count = atoi(argus[i]);
1374 LOG_DEBUG("\trun_count@TCK = %d", run_count);
1376 LOG_ERROR("%s not supported for clock", argus[i + 1]);
1382 if (((i + 2) <= num_of_argu) && !strcmp(argus[i + 1], "SEC")) {
1383 min_time = atof(argus[i]);
1384 LOG_DEBUG("\tmin_time = %fs", min_time);
1387 /* MAXIMUM max_time SEC */
1388 if (((i + 3) <= num_of_argu) &&
1389 !strcmp(argus[i], "MAXIMUM") && !strcmp(argus[i + 2], "SEC")) {
1391 max_time = atof(argus[i + 1]);
1392 LOG_DEBUG("\tmax_time = %fs", max_time);
1395 /* ENDSTATE end_state */
1396 if (((i + 2) <= num_of_argu) && !strcmp(argus[i], "ENDSTATE")) {
1397 i_tmp = tap_state_by_name(argus[i + 1]);
1399 if (svf_tap_state_is_stable(i_tmp)) {
1400 svf_para.runtest_end_state = i_tmp;
1401 LOG_DEBUG("\tend_state = %s", tap_state_name(i_tmp));
1403 LOG_ERROR("%s: %s is not a stable state", argus[0], tap_state_name(i_tmp));
1409 /* all parameter should be parsed */
1410 if (i == num_of_argu) {
1412 /* FIXME handle statemove failures */
1413 uint32_t min_usec = 1000000 * min_time;
1415 /* enter into run_state if necessary */
1416 if (cmd_queue_cur_state != svf_para.runtest_run_state)
1417 svf_add_statemove(svf_para.runtest_run_state);
1419 /* add clocks and/or min wait */
1420 if (run_count > 0) {
1422 jtag_add_clocks(run_count);
1427 jtag_add_sleep(min_usec);
1430 /* move to end_state if necessary */
1431 if (svf_para.runtest_end_state != svf_para.runtest_run_state)
1432 svf_add_statemove(svf_para.runtest_end_state);
1435 if (svf_para.runtest_run_state != TAP_IDLE) {
1436 LOG_ERROR("cannot runtest in %s state",
1437 tap_state_name(svf_para.runtest_run_state));
1442 jtag_add_runtest(run_count, svf_para.runtest_end_state);
1445 LOG_ERROR("fail to parse parameter of RUNTEST, %d out of %d is parsed",
1452 /* STATE [pathstate1 [pathstate2 ...[pathstaten]]] stable_state */
1453 if (num_of_argu < 2) {
1454 LOG_ERROR("invalid parameter of %s", argus[0]);
1457 if (num_of_argu > 2) {
1458 /* STATE pathstate1 ... stable_state */
1459 path = (tap_state_t *)malloc((num_of_argu - 1) * sizeof(tap_state_t));
1461 LOG_ERROR("not enough memory");
1464 num_of_argu--; /* num of path */
1465 i_tmp = 1; /* path is from parameter 1 */
1466 for (i = 0; i < num_of_argu; i++, i_tmp++) {
1467 path[i] = tap_state_by_name(argus[i_tmp]);
1468 if (path[i] == TAP_INVALID) {
1469 LOG_ERROR("%s: %s is not a valid state", argus[0], argus[i_tmp]);
1473 /* OpenOCD refuses paths containing TAP_RESET */
1474 if (TAP_RESET == path[i]) {
1475 /* FIXME last state MUST be stable! */
1478 jtag_add_pathmove(i, path);
1482 num_of_argu -= i + 1;
1486 if (num_of_argu > 0) {
1487 /* execute last path if necessary */
1488 if (svf_tap_state_is_stable(path[num_of_argu - 1])) {
1489 /* last state MUST be stable state */
1491 jtag_add_pathmove(num_of_argu, path);
1492 LOG_DEBUG("\tmove to %s by path_move",
1493 tap_state_name(path[num_of_argu - 1]));
1495 LOG_ERROR("%s: %s is not a stable state",
1497 tap_state_name(path[num_of_argu - 1]));
1506 /* STATE stable_state */
1507 state = tap_state_by_name(argus[1]);
1508 if (svf_tap_state_is_stable(state)) {
1509 LOG_DEBUG("\tmove to %s by svf_add_statemove",
1510 tap_state_name(state));
1511 /* FIXME handle statemove failures */
1512 svf_add_statemove(state);
1514 LOG_ERROR("%s: %s is not a stable state",
1515 argus[0], tap_state_name(state));
1521 /* TRST trst_mode */
1522 if (num_of_argu != 2) {
1523 LOG_ERROR("invalid parameter of %s", argus[0]);
1526 if (svf_para.trst_mode != TRST_ABSENT) {
1527 if (ERROR_OK != svf_execute_tap())
1529 i_tmp = svf_find_string_in_array(argus[1],
1530 (char **)svf_trst_mode_name,
1531 ARRAY_SIZE(svf_trst_mode_name));
1535 jtag_add_reset(1, 0);
1540 jtag_add_reset(0, 0);
1545 LOG_ERROR("unknown TRST mode: %s", argus[1]);
1548 svf_para.trst_mode = i_tmp;
1549 LOG_DEBUG("\ttrst_mode = %s", svf_trst_mode_name[svf_para.trst_mode]);
1551 LOG_ERROR("can not accpet TRST command if trst_mode is ABSENT");
1556 LOG_ERROR("invalid svf command: %s", argus[0]);
1562 if (padding_command_skipped)
1563 LOG_USER("(Above Padding command skipped, as per -tap argument)");
1566 if (debug_level >= LOG_LVL_DEBUG) {
1567 /* for convenient debugging, execute tap if possible */
1568 if ((svf_buffer_index > 0) && \
1569 (((command != STATE) && (command != RUNTEST)) || \
1570 ((command == STATE) && (num_of_argu == 2)))) {
1571 if (ERROR_OK != svf_execute_tap())
1574 /* output debug info */
1575 if ((SIR == command) || (SDR == command)) {
1577 memcpy(&read_value, svf_tdi_buffer, sizeof(int));
1578 /* in debug mode, data is from index 0 */
1579 int read_mask = svf_get_mask_u32(svf_check_tdo_para[0].bit_len);
1580 LOG_DEBUG("\tTDO read = 0x%X", read_value & read_mask);
1584 /* for fast executing, execute tap if necessary */
1585 /* half of the buffer is for the next command */
1586 if (((svf_buffer_index >= SVF_MAX_BUFFER_SIZE_TO_COMMIT) ||
1587 (svf_check_tdo_para_index >= SVF_CHECK_TDO_PARA_SIZE / 2)) && \
1588 (((command != STATE) && (command != RUNTEST)) || \
1589 ((command == STATE) && (num_of_argu == 2))))
1590 return svf_execute_tap();
1596 static const struct command_registration svf_command_handlers[] = {
1599 .handler = handle_svf_command,
1600 .mode = COMMAND_EXEC,
1601 .help = "Runs a SVF file.",
1602 .usage = "svf [-tap device.tap] <file> [quiet] [nil] [progress]",
1604 COMMAND_REGISTRATION_DONE
1607 int svf_register_commands(struct command_context *cmd_ctx)
1609 return register_commands(cmd_ctx, NULL, svf_command_handlers);