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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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>
58 static const char *svf_command_name[14] =
84 static const char *svf_trst_mode_name[4] =
96 uint32_t num_of_moves;
101 * These paths are from the SVF specification for the STATE command, to be
102 * used when the STATE command only includes the final state. The first
103 * element of the path is the "from" (current) state, and the last one is
104 * the "to" (target) state.
106 * All specified paths are the shortest ones in the JTAG spec, and are thus
107 * not (!!) exact matches for the paths used elsewhere in OpenOCD. Note
108 * that PAUSE-to-PAUSE transitions all go through UPDATE and then CAPTURE,
109 * which has specific effects on the various registers; they are not NOPs.
111 * Paths to RESET are disabled here. As elsewhere in OpenOCD, and in XSVF
112 * and many SVF implementations, we don't want to risk missing that state.
113 * To get to RESET, always we ignore the current state.
115 static const struct svf_statemove svf_statemoves[] =
117 // from to num_of_moves, paths[8]
118 // {TAP_RESET, TAP_RESET, 1, {TAP_RESET}},
119 {TAP_RESET, TAP_IDLE, 2, {TAP_RESET, TAP_IDLE}},
120 {TAP_RESET, TAP_DRPAUSE, 6, {TAP_RESET, TAP_IDLE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DREXIT1, TAP_DRPAUSE}},
121 {TAP_RESET, TAP_IRPAUSE, 7, {TAP_RESET, TAP_IDLE, TAP_DRSELECT, TAP_IRSELECT, TAP_IRCAPTURE, TAP_IREXIT1, TAP_IRPAUSE}},
123 // {TAP_IDLE, TAP_RESET, 4, {TAP_IDLE, TAP_DRSELECT, TAP_IRSELECT, TAP_RESET}},
124 {TAP_IDLE, TAP_IDLE, 1, {TAP_IDLE}},
125 {TAP_IDLE, TAP_DRPAUSE, 5, {TAP_IDLE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DREXIT1, TAP_DRPAUSE}},
126 {TAP_IDLE, TAP_IRPAUSE, 6, {TAP_IDLE, TAP_DRSELECT, TAP_IRSELECT, TAP_IRCAPTURE, TAP_IREXIT1, TAP_IRPAUSE}},
128 // {TAP_DRPAUSE, TAP_RESET, 6, {TAP_DRPAUSE, TAP_DREXIT2, TAP_DRUPDATE, TAP_DRSELECT, TAP_IRSELECT, TAP_RESET}},
129 {TAP_DRPAUSE, TAP_IDLE, 4, {TAP_DRPAUSE, TAP_DREXIT2, TAP_DRUPDATE, TAP_IDLE}},
130 {TAP_DRPAUSE, TAP_DRPAUSE, 7, {TAP_DRPAUSE, TAP_DREXIT2, TAP_DRUPDATE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DREXIT1, TAP_DRPAUSE}},
131 {TAP_DRPAUSE, TAP_IRPAUSE, 8, {TAP_DRPAUSE, TAP_DREXIT2, TAP_DRUPDATE, TAP_DRSELECT, TAP_IRSELECT, TAP_IRCAPTURE, TAP_IREXIT1, TAP_IRPAUSE}},
133 // {TAP_IRPAUSE, TAP_RESET, 6, {TAP_IRPAUSE, TAP_IREXIT2, TAP_IRUPDATE, TAP_DRSELECT, TAP_IRSELECT, TAP_RESET}},
134 {TAP_IRPAUSE, TAP_IDLE, 4, {TAP_IRPAUSE, TAP_IREXIT2, TAP_IRUPDATE, TAP_IDLE}},
135 {TAP_IRPAUSE, TAP_DRPAUSE, 7, {TAP_IRPAUSE, TAP_IREXIT2, TAP_IRUPDATE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DREXIT1, TAP_DRPAUSE}},
136 {TAP_IRPAUSE, TAP_IRPAUSE, 8, {TAP_IRPAUSE, TAP_IREXIT2, TAP_IRUPDATE, TAP_DRSELECT, TAP_IRSELECT, TAP_IRCAPTURE, TAP_IREXIT1, TAP_IRPAUSE}}
140 #define XXR_TDI (1 << 0)
141 #define XXR_TDO (1 << 1)
142 #define XXR_MASK (1 << 2)
143 #define XXR_SMASK (1 << 3)
157 tap_state_t ir_end_state;
158 tap_state_t dr_end_state;
159 tap_state_t runtest_run_state;
160 tap_state_t runtest_end_state;
161 trst_mode_t trst_mode;
163 struct svf_xxr_para hir_para;
164 struct svf_xxr_para hdr_para;
165 struct svf_xxr_para tir_para;
166 struct svf_xxr_para tdr_para;
167 struct svf_xxr_para sir_para;
168 struct svf_xxr_para sdr_para;
171 static struct svf_para svf_para;
172 static const struct svf_para svf_para_init =
174 // frequency, ir_end_state, dr_end_state, runtest_run_state, runtest_end_state, trst_mode
175 0, TAP_IDLE, TAP_IDLE, TAP_IDLE, TAP_IDLE, TRST_Z,
177 // {len, data_mask, tdi, tdo, mask, smask},
178 {0, 0, NULL, NULL, NULL, NULL},
180 // {len, data_mask, tdi, tdo, mask, smask},
181 {0, 0, NULL, NULL, NULL, NULL},
183 // {len, data_mask, tdi, tdo, mask, smask},
184 {0, 0, NULL, NULL, NULL, NULL},
186 // {len, data_mask, tdi, tdo, mask, smask},
187 {0, 0, NULL, NULL, NULL, NULL},
189 // {len, data_mask, tdi, tdo, mask, smask},
190 {0, 0, NULL, NULL, NULL, NULL},
192 // {len, data_mask, tdi, tdo, mask, smask},
193 {0, 0, NULL, NULL, NULL, NULL},
196 struct svf_check_tdo_para
198 int line_num; // used to record line number of the check operation
199 // so more information could be printed
200 int enabled; // check is enabled or not
201 int buffer_offset; // buffer_offset to buffers
202 int bit_len; // bit length to check
205 #define SVF_CHECK_TDO_PARA_SIZE 1024
206 static struct svf_check_tdo_para *svf_check_tdo_para = NULL;
207 static int svf_check_tdo_para_index = 0;
209 static int svf_read_command_from_file(int fd);
210 static int svf_check_tdo(void);
211 static int svf_add_check_para(uint8_t enabled, int buffer_offset, int bit_len);
212 static int svf_run_command(struct command_context *cmd_ctx, char *cmd_str);
214 static int svf_fd = 0;
215 static char *svf_command_buffer = NULL;
216 static int svf_command_buffer_size = 0;
217 static int svf_line_number = 1;
219 static struct jtag_tap *tap = NULL;
221 #define SVF_MAX_BUFFER_SIZE_TO_COMMIT (4 * 1024)
222 static uint8_t *svf_tdi_buffer = NULL, *svf_tdo_buffer = NULL, *svf_mask_buffer = NULL;
223 static int svf_buffer_index = 0, svf_buffer_size = 0;
224 static int svf_quiet = 0;
227 static void svf_free_xxd_para(struct svf_xxr_para *para)
231 if (para->tdi != NULL)
236 if (para->tdo != NULL)
241 if (para->mask != NULL)
246 if (para->smask != NULL)
254 static unsigned svf_get_mask_u32(int bitlen)
262 else if (bitlen >= 32)
264 bitmask = 0xFFFFFFFF;
268 bitmask = (1 << bitlen) - 1;
274 int svf_add_statemove(tap_state_t state_to)
276 tap_state_t state_from = cmd_queue_cur_state;
279 /* when resetting, be paranoid and ignore current state */
280 if (state_to == TAP_RESET) {
285 for (index = 0; index < ARRAY_SIZE(svf_statemoves); index++)
287 if ((svf_statemoves[index].from == state_from)
288 && (svf_statemoves[index].to == state_to))
290 /* recorded path includes current state ... avoid extra TCKs! */
291 if (svf_statemoves[index].num_of_moves > 1)
292 jtag_add_pathmove(svf_statemoves[index].num_of_moves - 1,
293 svf_statemoves[index].paths + 1);
295 jtag_add_pathmove(svf_statemoves[index].num_of_moves,
296 svf_statemoves[index].paths);
300 LOG_ERROR("SVF: can not move to %s", tap_state_name(state_to));
304 COMMAND_HANDLER(handle_svf_command)
306 #define SVF_NUM_OF_OPTIONS 1
311 if ((CMD_ARGC < 1) || (CMD_ARGC > (1 + SVF_NUM_OF_OPTIONS)))
313 command_print(CMD_CTX, "usage: svf <file> [quiet]");
319 for (unsigned i = 1; i < CMD_ARGC; i++)
321 if (!strcmp(CMD_ARGV[i], "quiet"))
327 LOG_ERROR("unknown variant for svf: %s", CMD_ARGV[i]);
329 // no need to free anything now
334 if ((svf_fd = open(CMD_ARGV[0], O_RDONLY)) < 0)
336 command_print(CMD_CTX, "file \"%s\" not found", CMD_ARGV[0]);
338 // no need to free anything now
342 LOG_USER("svf processing file: \"%s\"", CMD_ARGV[0]);
345 time_ago = timeval_ms();
349 svf_command_buffer_size = 0;
351 svf_check_tdo_para_index = 0;
352 svf_check_tdo_para = malloc(sizeof(struct svf_check_tdo_para) * SVF_CHECK_TDO_PARA_SIZE);
353 if (NULL == svf_check_tdo_para)
355 LOG_ERROR("not enough memory");
360 svf_buffer_index = 0;
361 // double the buffer size
362 // in case current command cannot be commited, and next command is a bit scan command
363 // here is 32K bits for this big scan command, it should be enough
364 // buffer will be reallocated if buffer size is not enough
365 svf_tdi_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
366 if (NULL == svf_tdi_buffer)
368 LOG_ERROR("not enough memory");
372 svf_tdo_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
373 if (NULL == svf_tdo_buffer)
375 LOG_ERROR("not enough memory");
379 svf_mask_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
380 if (NULL == svf_mask_buffer)
382 LOG_ERROR("not enough memory");
386 svf_buffer_size = 2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT;
388 memcpy(&svf_para, &svf_para_init, sizeof(svf_para));
393 while (ERROR_OK == svf_read_command_from_file(svf_fd))
395 if (ERROR_OK != svf_run_command(CMD_CTX, svf_command_buffer))
397 LOG_ERROR("fail to run command at line %d", svf_line_number);
403 if (ERROR_OK != jtag_execute_queue())
407 else if (ERROR_OK != svf_check_tdo())
413 command_print(CMD_CTX, "%lld ms used", timeval_ms() - time_ago);
421 if (svf_command_buffer)
423 free(svf_command_buffer);
424 svf_command_buffer = NULL;
425 svf_command_buffer_size = 0;
427 if (svf_check_tdo_para)
429 free(svf_check_tdo_para);
430 svf_check_tdo_para = NULL;
431 svf_check_tdo_para_index = 0;
435 free(svf_tdi_buffer);
436 svf_tdi_buffer = NULL;
440 free(svf_tdo_buffer);
441 svf_tdo_buffer = NULL;
445 free(svf_mask_buffer);
446 svf_mask_buffer = NULL;
448 svf_buffer_index = 0;
451 svf_free_xxd_para(&svf_para.hdr_para);
452 svf_free_xxd_para(&svf_para.hir_para);
453 svf_free_xxd_para(&svf_para.tdr_para);
454 svf_free_xxd_para(&svf_para.tir_para);
455 svf_free_xxd_para(&svf_para.sdr_para);
456 svf_free_xxd_para(&svf_para.sir_para);
460 command_print(CMD_CTX, "svf file programmed successfully for %d commands", command_num);
464 command_print(CMD_CTX, "svf file programmed failed");
470 #define SVFP_CMD_INC_CNT 1024
471 static int svf_read_command_from_file(int fd)
474 char *tmp_buffer = NULL;
475 int cmd_pos = 0, cmd_ok = 0, slash = 0, comment = 0;
477 while (!cmd_ok && (read(fd, &ch, 1) > 0))
507 /* The parsing code currently expects a space
508 * before parentheses -- "TDI (123)". Also a
509 * space afterwards -- "TDI (123) TDO(456)".
510 * But such spaces are optional... instead of
511 * parser updates, cope with that by adding the
514 * Ensure there are 3 bytes available, for:
515 * - current character
517 * - terminating NUL ('\0')
519 if ((cmd_pos + 2) >= svf_command_buffer_size)
521 /* REVISIT use realloc(); simpler */
523 svf_command_buffer_size
525 if (NULL == tmp_buffer)
527 LOG_ERROR("not enough memory");
530 if (svf_command_buffer_size > 0)
533 svf_command_buffer_size);
534 if (svf_command_buffer != NULL)
535 free(svf_command_buffer);
536 svf_command_buffer = tmp_buffer;
537 svf_command_buffer_size += SVFP_CMD_INC_CNT;
541 /* insert a space before '(' */
543 svf_command_buffer[cmd_pos++] = ' ';
545 svf_command_buffer[cmd_pos++] = (char)toupper(ch);
547 /* insert a space after ')' */
549 svf_command_buffer[cmd_pos++] = ' ';
557 svf_command_buffer[cmd_pos] = '\0';
566 static int svf_parse_cmd_string(char *str, int len, char **argus, int *num_of_argu)
568 int pos = 0, num = 0, space_found = 1;
578 LOG_ERROR("fail to parse svf command");
588 argus[num++] = &str[pos];
601 bool svf_tap_state_is_stable(tap_state_t state)
603 return (TAP_RESET == state) || (TAP_IDLE == state)
604 || (TAP_DRPAUSE == state) || (TAP_IRPAUSE == state);
607 static int svf_find_string_in_array(char *str, char **strs, int num_of_element)
611 for (i = 0; i < num_of_element; i++)
613 if (!strcmp(str, strs[i]))
621 static int svf_adjust_array_length(uint8_t **arr, int orig_bit_len, int new_bit_len)
623 int new_byte_len = (new_bit_len + 7) >> 3;
625 if ((NULL == *arr) || (((orig_bit_len + 7) >> 3) < ((new_bit_len + 7) >> 3)))
632 *arr = (uint8_t*)malloc(new_byte_len);
635 LOG_ERROR("not enough memory");
638 memset(*arr, 0, new_byte_len);
643 static int svf_copy_hexstring_to_binary(char *str, uint8_t **bin, int orig_bit_len, int bit_len)
645 int i, str_len = strlen(str), str_hbyte_len = (bit_len + 3) >> 2;
648 if (ERROR_OK != svf_adjust_array_length(bin, orig_bit_len, bit_len))
650 LOG_ERROR("fail to adjust length of array");
654 for (i = 0; i < str_hbyte_len; i++)
663 if ((ch >= '0') && (ch <= '9'))
668 else if ((ch >= 'A') && (ch <= 'F'))
675 LOG_ERROR("invalid hex string");
687 (*bin)[i / 2] |= ch << 4;
697 // consume optional leading '0' characters
698 while (str_len > 0 && str[str_len - 1] == '0')
702 if (str_len > 0 || (ch & ~((2 << ((bit_len - 1) % 4)) - 1)) != 0)
704 LOG_ERROR("value execeeds length");
711 static int svf_check_tdo(void)
715 for (i = 0; i < svf_check_tdo_para_index; i++)
717 index = svf_check_tdo_para[i].buffer_offset;
718 len = svf_check_tdo_para[i].bit_len;
719 if ((svf_check_tdo_para[i].enabled)
720 && buf_cmp_mask(&svf_tdi_buffer[index], &svf_tdo_buffer[index], &svf_mask_buffer[index], len))
723 unsigned received, expected, tapmask;
724 bitmask = svf_get_mask_u32(svf_check_tdo_para[i].bit_len);
726 memcpy(&received, svf_tdi_buffer + index, sizeof(unsigned));
727 memcpy(&expected, svf_tdo_buffer + index, sizeof(unsigned));
728 memcpy(&tapmask, svf_mask_buffer + index, sizeof(unsigned));
729 LOG_ERROR("tdo check error at line %d",
730 svf_check_tdo_para[i].line_num);
731 LOG_ERROR("read = 0x%X, want = 0x%X, mask = 0x%X",
738 svf_check_tdo_para_index = 0;
743 static int svf_add_check_para(uint8_t enabled, int buffer_offset, int bit_len)
745 if (svf_check_tdo_para_index >= SVF_CHECK_TDO_PARA_SIZE)
747 LOG_ERROR("toooooo many operation undone");
751 svf_check_tdo_para[svf_check_tdo_para_index].line_num = svf_line_number;
752 svf_check_tdo_para[svf_check_tdo_para_index].bit_len = bit_len;
753 svf_check_tdo_para[svf_check_tdo_para_index].enabled = enabled;
754 svf_check_tdo_para[svf_check_tdo_para_index].buffer_offset = buffer_offset;
755 svf_check_tdo_para_index++;
760 static int svf_execute_tap(void)
762 if (ERROR_OK != jtag_execute_queue())
766 else if (ERROR_OK != svf_check_tdo())
771 svf_buffer_index = 0;
776 static int svf_run_command(struct command_context *cmd_ctx, char *cmd_str)
778 char *argus[256], command;
779 int num_of_argu = 0, i;
786 float min_time, max_time;
788 struct svf_xxr_para *xxr_para_tmp;
789 uint8_t **pbuffer_tmp;
790 struct scan_field field;
792 tap_state_t *path = NULL, state;
796 LOG_USER("%s", svf_command_buffer);
799 if (ERROR_OK != svf_parse_cmd_string(cmd_str, strlen(cmd_str), argus, &num_of_argu))
804 /* NOTE: we're a bit loose here, because we ignore case in
805 * TAP state names (instead of insisting on uppercase).
808 command = svf_find_string_in_array(argus[0],
809 (char **)svf_command_name, ARRAY_SIZE(svf_command_name));
814 if (num_of_argu != 2)
816 LOG_ERROR("invalid parameter of %s", argus[0]);
820 i_tmp = tap_state_by_name(argus[1]);
822 if (svf_tap_state_is_stable(i_tmp))
824 if (command == ENDIR)
826 svf_para.ir_end_state = i_tmp;
827 LOG_DEBUG("\tIR end_state = %s",
828 tap_state_name(i_tmp));
832 svf_para.dr_end_state = i_tmp;
833 LOG_DEBUG("\tDR end_state = %s",
834 tap_state_name(i_tmp));
839 LOG_ERROR("%s: %s is not a stable state",
845 if ((num_of_argu != 1) && (num_of_argu != 3))
847 LOG_ERROR("invalid parameter of %s", argus[0]);
850 if (1 == num_of_argu)
852 // TODO: set jtag speed to full speed
853 svf_para.frequency = 0;
857 if (strcmp(argus[2], "HZ"))
859 LOG_ERROR("HZ not found in FREQUENCY command");
862 if (ERROR_OK != svf_execute_tap())
866 svf_para.frequency = atof(argus[1]);
867 // TODO: set jtag speed to
868 if (svf_para.frequency > 0)
870 command_run_linef(cmd_ctx, "jtag_khz %d", (int)svf_para.frequency / 1000);
871 LOG_DEBUG("\tfrequency = %f", svf_para.frequency);
876 xxr_para_tmp = &svf_para.hdr_para;
879 xxr_para_tmp = &svf_para.hir_para;
882 xxr_para_tmp = &svf_para.tdr_para;
885 xxr_para_tmp = &svf_para.tir_para;
888 xxr_para_tmp = &svf_para.sdr_para;
891 xxr_para_tmp = &svf_para.sir_para;
894 // XXR length [TDI (tdi)] [TDO (tdo)][MASK (mask)] [SMASK (smask)]
895 if ((num_of_argu > 10) || (num_of_argu % 2))
897 LOG_ERROR("invalid parameter of %s", argus[0]);
900 i_tmp = xxr_para_tmp->len;
901 xxr_para_tmp->len = atoi(argus[1]);
902 LOG_DEBUG("\tlength = %d", xxr_para_tmp->len);
903 xxr_para_tmp->data_mask = 0;
904 for (i = 2; i < num_of_argu; i += 2)
906 if ((strlen(argus[i + 1]) < 3) || (argus[i + 1][0] != '(') || (argus[i + 1][strlen(argus[i + 1]) - 1] != ')'))
908 LOG_ERROR("data section error");
911 argus[i + 1][strlen(argus[i + 1]) - 1] = '\0';
912 // TDI, TDO, MASK, SMASK
913 if (!strcmp(argus[i], "TDI"))
916 pbuffer_tmp = &xxr_para_tmp->tdi;
917 xxr_para_tmp->data_mask |= XXR_TDI;
919 else if (!strcmp(argus[i], "TDO"))
922 pbuffer_tmp = &xxr_para_tmp->tdo;
923 xxr_para_tmp->data_mask |= XXR_TDO;
925 else if (!strcmp(argus[i], "MASK"))
928 pbuffer_tmp = &xxr_para_tmp->mask;
929 xxr_para_tmp->data_mask |= XXR_MASK;
931 else if (!strcmp(argus[i], "SMASK"))
934 pbuffer_tmp = &xxr_para_tmp->smask;
935 xxr_para_tmp->data_mask |= XXR_SMASK;
939 LOG_ERROR("unknow parameter: %s", argus[i]);
942 if (ERROR_OK != svf_copy_hexstring_to_binary(&argus[i + 1][1], pbuffer_tmp, i_tmp, xxr_para_tmp->len))
944 LOG_ERROR("fail to parse hex value");
947 LOG_DEBUG("\t%s = 0x%X", argus[i], (**(int**)pbuffer_tmp) & svf_get_mask_u32(xxr_para_tmp->len));
949 // If a command changes the length of the last scan of the same type and the MASK parameter is absent,
950 // the mask pattern used is all cares
951 if (!(xxr_para_tmp->data_mask & XXR_MASK) && (i_tmp != xxr_para_tmp->len))
953 // MASK not defined and length changed
954 if (ERROR_OK != svf_adjust_array_length(&xxr_para_tmp->mask, i_tmp, xxr_para_tmp->len))
956 LOG_ERROR("fail to adjust length of array");
959 buf_set_ones(xxr_para_tmp->mask, xxr_para_tmp->len);
961 // If TDO is absent, no comparison is needed, set the mask to 0
962 if (!(xxr_para_tmp->data_mask & XXR_TDO))
964 if (NULL == xxr_para_tmp->tdo)
966 if (ERROR_OK != svf_adjust_array_length(&xxr_para_tmp->tdo, i_tmp, xxr_para_tmp->len))
968 LOG_ERROR("fail to adjust length of array");
972 if (NULL == xxr_para_tmp->mask)
974 if (ERROR_OK != svf_adjust_array_length(&xxr_para_tmp->mask, i_tmp, xxr_para_tmp->len))
976 LOG_ERROR("fail to adjust length of array");
980 memset(xxr_para_tmp->mask, 0, (xxr_para_tmp->len + 7) >> 3);
982 // do scan if necessary
985 // check buffer size first, reallocate if necessary
986 i = svf_para.hdr_para.len + svf_para.sdr_para.len + svf_para.tdr_para.len;
987 if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3))
990 // simply print error message
991 LOG_ERROR("buffer is not enough, report to author");
997 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
998 if (NULL == buffer_tmp)
1000 LOG_ERROR("not enough memory");
1003 memcpy(buffer_tmp, svf_tdi_buffer, svf_buffer_index);
1004 // svf_tdi_buffer isn't NULL here
1005 free(svf_tdi_buffer);
1006 svf_tdi_buffer = buffer_tmp;
1008 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1009 if (NULL == buffer_tmp)
1011 LOG_ERROR("not enough memory");
1014 memcpy(buffer_tmp, svf_tdo_buffer, svf_buffer_index);
1015 // svf_tdo_buffer isn't NULL here
1016 free(svf_tdo_buffer);
1017 svf_tdo_buffer = buffer_tmp;
1019 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1020 if (NULL == buffer_tmp)
1022 LOG_ERROR("not enough memory");
1025 memcpy(buffer_tmp, svf_mask_buffer, svf_buffer_index);
1026 // svf_mask_buffer isn't NULL here
1027 free(svf_mask_buffer);
1028 svf_mask_buffer = buffer_tmp;
1031 svf_buffer_size = svf_buffer_index + ((i + 7) >> 3);
1037 buf_set_buf(svf_para.hdr_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.hdr_para.len);
1038 i += svf_para.hdr_para.len;
1039 buf_set_buf(svf_para.sdr_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.sdr_para.len);
1040 i += svf_para.sdr_para.len;
1041 buf_set_buf(svf_para.tdr_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.tdr_para.len);
1042 i += svf_para.tdr_para.len;
1045 if (svf_para.sdr_para.data_mask & XXR_TDO)
1047 // assemble dr mask data
1049 buf_set_buf(svf_para.hdr_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.hdr_para.len);
1050 i += svf_para.hdr_para.len;
1051 buf_set_buf(svf_para.sdr_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.sdr_para.len);
1052 i += svf_para.sdr_para.len;
1053 buf_set_buf(svf_para.tdr_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.tdr_para.len);
1054 i += svf_para.tdr_para.len;
1055 // assemble dr check data
1057 buf_set_buf(svf_para.hdr_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.hdr_para.len);
1058 i += svf_para.hdr_para.len;
1059 buf_set_buf(svf_para.sdr_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.sdr_para.len);
1060 i += svf_para.sdr_para.len;
1061 buf_set_buf(svf_para.tdr_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.tdr_para.len);
1062 i += svf_para.tdr_para.len;
1064 svf_add_check_para(1, svf_buffer_index, i);
1068 svf_add_check_para(0, svf_buffer_index, i);
1072 field.out_value = &svf_tdi_buffer[svf_buffer_index];
1073 field.in_value = &svf_tdi_buffer[svf_buffer_index];
1074 /* NOTE: doesn't use SVF-specified state paths */
1075 jtag_add_plain_dr_scan(1, &field, svf_para.dr_end_state);
1077 svf_buffer_index += (i + 7) >> 3;
1079 else if (SIR == command)
1081 // check buffer size first, reallocate if necessary
1082 i = svf_para.hir_para.len + svf_para.sir_para.len + svf_para.tir_para.len;
1083 if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3))
1086 // simply print error message
1087 LOG_ERROR("buffer is not enough, report to author");
1090 uint8_t *buffer_tmp;
1092 // reallocate buffer
1093 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1094 if (NULL == buffer_tmp)
1096 LOG_ERROR("not enough memory");
1099 memcpy(buffer_tmp, svf_tdi_buffer, svf_buffer_index);
1100 // svf_tdi_buffer isn't NULL here
1101 free(svf_tdi_buffer);
1102 svf_tdi_buffer = buffer_tmp;
1104 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1105 if (NULL == buffer_tmp)
1107 LOG_ERROR("not enough memory");
1110 memcpy(buffer_tmp, svf_tdo_buffer, svf_buffer_index);
1111 // svf_tdo_buffer isn't NULL here
1112 free(svf_tdo_buffer);
1113 svf_tdo_buffer = buffer_tmp;
1115 buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1116 if (NULL == buffer_tmp)
1118 LOG_ERROR("not enough memory");
1121 memcpy(buffer_tmp, svf_mask_buffer, svf_buffer_index);
1122 // svf_mask_buffer isn't NULL here
1123 free(svf_mask_buffer);
1124 svf_mask_buffer = buffer_tmp;
1127 svf_buffer_size = svf_buffer_index + ((i + 7) >> 3);
1133 buf_set_buf(svf_para.hir_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.hir_para.len);
1134 i += svf_para.hir_para.len;
1135 buf_set_buf(svf_para.sir_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.sir_para.len);
1136 i += svf_para.sir_para.len;
1137 buf_set_buf(svf_para.tir_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.tir_para.len);
1138 i += svf_para.tir_para.len;
1141 if (svf_para.sir_para.data_mask & XXR_TDO)
1143 // assemble dr mask data
1145 buf_set_buf(svf_para.hir_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.hir_para.len);
1146 i += svf_para.hir_para.len;
1147 buf_set_buf(svf_para.sir_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.sir_para.len);
1148 i += svf_para.sir_para.len;
1149 buf_set_buf(svf_para.tir_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.tir_para.len);
1150 i += svf_para.tir_para.len;
1151 // assemble dr check data
1153 buf_set_buf(svf_para.hir_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.hir_para.len);
1154 i += svf_para.hir_para.len;
1155 buf_set_buf(svf_para.sir_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.sir_para.len);
1156 i += svf_para.sir_para.len;
1157 buf_set_buf(svf_para.tir_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.tir_para.len);
1158 i += svf_para.tir_para.len;
1160 svf_add_check_para(1, svf_buffer_index, i);
1164 svf_add_check_para(0, svf_buffer_index, i);
1168 field.out_value = &svf_tdi_buffer[svf_buffer_index];
1169 field.in_value = &svf_tdi_buffer[svf_buffer_index];
1170 /* NOTE: doesn't use SVF-specified state paths */
1171 jtag_add_plain_ir_scan(1, &field, svf_para.ir_end_state);
1173 svf_buffer_index += (i + 7) >> 3;
1178 LOG_ERROR("PIO and PIOMAP are not supported");
1182 // RUNTEST [run_state] run_count run_clk [min_time SEC [MAXIMUM max_time SEC]] [ENDSTATE end_state]
1183 // RUNTEST [run_state] min_time SEC [MAXIMUM max_time SEC] [ENDSTATE end_state]
1184 if ((num_of_argu < 3) && (num_of_argu > 11))
1186 LOG_ERROR("invalid parameter of %s", argus[0]);
1196 i_tmp = tap_state_by_name(argus[i]);
1197 if (i_tmp != TAP_INVALID)
1199 if (svf_tap_state_is_stable(i_tmp))
1201 svf_para.runtest_run_state = i_tmp;
1203 /* When a run_state is specified, the new
1204 * run_state becomes the default end_state.
1206 svf_para.runtest_end_state = i_tmp;
1207 LOG_DEBUG("\trun_state = %s",
1208 tap_state_name(i_tmp));
1213 LOG_ERROR("%s: %s is not a stable state",
1214 argus[0], tap_state_name(i_tmp));
1219 // run_count run_clk
1220 if (((i + 2) <= num_of_argu) && strcmp(argus[i + 1], "SEC"))
1222 if (!strcmp(argus[i + 1], "TCK"))
1224 // clock source is TCK
1225 run_count = atoi(argus[i]);
1226 LOG_DEBUG("\trun_count@TCK = %d", run_count);
1230 LOG_ERROR("%s not supported for clock", argus[i + 1]);
1236 if (((i + 2) <= num_of_argu) && !strcmp(argus[i + 1], "SEC"))
1238 min_time = atof(argus[i]);
1239 LOG_DEBUG("\tmin_time = %fs", min_time);
1242 // MAXIMUM max_time SEC
1243 if (((i + 3) <= num_of_argu) && !strcmp(argus[i], "MAXIMUM") && !strcmp(argus[i + 2], "SEC"))
1245 max_time = atof(argus[i + 1]);
1246 LOG_DEBUG("\tmax_time = %fs", max_time);
1249 // ENDSTATE end_state
1250 if (((i + 2) <= num_of_argu) && !strcmp(argus[i], "ENDSTATE"))
1252 i_tmp = tap_state_by_name(argus[i + 1]);
1254 if (svf_tap_state_is_stable(i_tmp))
1256 svf_para.runtest_end_state = i_tmp;
1257 LOG_DEBUG("\tend_state = %s",
1258 tap_state_name(i_tmp));
1262 LOG_ERROR("%s: %s is not a stable state",
1263 argus[0], tap_state_name(i_tmp));
1268 // calculate run_count
1269 if ((0 == run_count) && (min_time > 0))
1271 run_count = min_time * svf_para.frequency;
1273 // all parameter should be parsed
1274 if (i == num_of_argu)
1278 // run_state and end_state is checked to be stable state
1281 /* FIXME handle statemove failures */
1284 // enter into run_state if necessary
1285 if (cmd_queue_cur_state != svf_para.runtest_run_state)
1287 retval = svf_add_statemove(svf_para.runtest_run_state);
1290 // call jtag_add_clocks
1291 jtag_add_clocks(run_count);
1293 // move to end_state if necessary
1294 if (svf_para.runtest_end_state != svf_para.runtest_run_state)
1296 retval = svf_add_statemove(svf_para.runtest_end_state);
1299 if (svf_para.runtest_run_state != TAP_IDLE)
1301 LOG_ERROR("cannot runtest in %s state",
1302 tap_state_name(svf_para.runtest_run_state));
1306 jtag_add_runtest(run_count, svf_para.runtest_end_state);
1312 LOG_ERROR("fail to parse parameter of RUNTEST, %d out of %d is parsed", i, num_of_argu);
1317 // STATE [pathstate1 [pathstate2 ...[pathstaten]]] stable_state
1318 if (num_of_argu < 2)
1320 LOG_ERROR("invalid parameter of %s", argus[0]);
1323 if (num_of_argu > 2)
1325 // STATE pathstate1 ... stable_state
1326 path = (tap_state_t *)malloc((num_of_argu - 1) * sizeof(tap_state_t));
1329 LOG_ERROR("not enough memory");
1332 num_of_argu--; // num of path
1333 i_tmp = 1; /* path is from parameter 1 */
1334 for (i = 0; i < num_of_argu; i++, i_tmp++)
1336 path[i] = tap_state_by_name(argus[i_tmp]);
1337 if (path[i] == TAP_INVALID)
1339 LOG_ERROR("%s: %s is not a valid state",
1340 argus[0], argus[i_tmp]);
1344 /* OpenOCD refuses paths containing TAP_RESET */
1345 if (TAP_RESET == path[i])
1347 /* FIXME last state MUST be stable! */
1350 jtag_add_pathmove(i, path);
1353 num_of_argu -= i + 1;
1357 if (num_of_argu > 0)
1359 // execute last path if necessary
1360 if (svf_tap_state_is_stable(path[num_of_argu - 1]))
1362 // last state MUST be stable state
1363 jtag_add_pathmove(num_of_argu, path);
1364 LOG_DEBUG("\tmove to %s by path_move",
1365 tap_state_name(path[num_of_argu - 1]));
1369 LOG_ERROR("%s: %s is not a stable state",
1371 tap_state_name(path[num_of_argu - 1]));
1382 // STATE stable_state
1383 state = tap_state_by_name(argus[1]);
1384 if (svf_tap_state_is_stable(state))
1386 LOG_DEBUG("\tmove to %s by svf_add_statemove",
1387 tap_state_name(state));
1388 /* FIXME handle statemove failures */
1389 svf_add_statemove(state);
1393 LOG_ERROR("%s: %s is not a stable state",
1394 argus[0], tap_state_name(state));
1401 if (num_of_argu != 2)
1403 LOG_ERROR("invalid parameter of %s", argus[0]);
1406 if (svf_para.trst_mode != TRST_ABSENT)
1408 if (ERROR_OK != svf_execute_tap())
1412 i_tmp = svf_find_string_in_array(argus[1],
1413 (char **)svf_trst_mode_name,
1414 ARRAY_SIZE(svf_trst_mode_name));
1418 jtag_add_reset(1, 0);
1422 jtag_add_reset(0, 0);
1427 LOG_ERROR("unknown TRST mode: %s", argus[1]);
1430 svf_para.trst_mode = i_tmp;
1431 LOG_DEBUG("\ttrst_mode = %s", svf_trst_mode_name[svf_para.trst_mode]);
1435 LOG_ERROR("can not accpet TRST command if trst_mode is ABSENT");
1440 LOG_ERROR("invalid svf command: %s", argus[0]);
1445 if (debug_level >= LOG_LVL_DEBUG)
1447 // for convenient debugging, execute tap if possible
1448 if ((svf_buffer_index > 0) && \
1449 (((command != STATE) && (command != RUNTEST)) || \
1450 ((command == STATE) && (num_of_argu == 2))))
1452 if (ERROR_OK != svf_execute_tap())
1457 // output debug info
1458 if ((SIR == command) || (SDR == command))
1461 memcpy(&read_value, svf_tdi_buffer, sizeof(int));
1462 // in debug mode, data is from index 0
1463 int read_mask = svf_get_mask_u32(svf_check_tdo_para[0].bit_len);
1464 LOG_DEBUG("\tTDO read = 0x%X", read_value & read_mask);
1470 // for fast executing, execute tap if necessary
1471 // half of the buffer is for the next command
1472 if (((svf_buffer_index >= SVF_MAX_BUFFER_SIZE_TO_COMMIT) || (svf_check_tdo_para_index >= SVF_CHECK_TDO_PARA_SIZE / 2)) && \
1473 (((command != STATE) && (command != RUNTEST)) || \
1474 ((command == STATE) && (num_of_argu == 2))))
1476 return svf_execute_tap();
1483 static const struct command_registration svf_command_handlers[] = {
1486 .handler = handle_svf_command,
1487 .mode = COMMAND_EXEC,
1488 .help = "Runs a SVF file.",
1489 .usage = "filename ['quiet']",
1491 COMMAND_REGISTRATION_DONE
1494 int svf_register_commands(struct command_context *cmd_ctx)
1496 return register_commands(cmd_ctx, NULL, svf_command_handlers);