OpenOCD
svf.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2009 by Simon Qian *
5  * SimonQian@SimonQian.com *
6  ***************************************************************************/
7 
8 /* The specification for SVF is available here:
9  * http://www.asset-intertech.com/support/svf.pdf
10  * Below, this document is referred to as the "SVF spec".
11  *
12  * The specification for XSVF is available here:
13  * http://www.xilinx.com/support/documentation/application_notes/xapp503.pdf
14  * Below, this document is referred to as the "XSVF spec".
15  */
16 
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #include <jtag/jtag.h>
22 #include "svf.h"
23 #include "helper/system.h"
24 #include <helper/time_support.h>
25 #include <helper/nvp.h>
26 #include <stdbool.h>
27 
28 /* SVF command */
33  HDR,
34  HIR,
35  PIO,
38  SDR,
39  SIR,
41  TDR,
42  TIR,
44 };
45 
46 static const char *svf_command_name[14] = {
47  "ENDDR",
48  "ENDIR",
49  "FREQUENCY",
50  "HDR",
51  "HIR",
52  "PIO",
53  "PIOMAP",
54  "RUNTEST",
55  "SDR",
56  "SIR",
57  "STATE",
58  "TDR",
59  "TIR",
60  "TRST"
61 };
62 
63 enum trst_mode {
68 };
69 
70 static const char *svf_trst_mode_name[4] = {
71  "ON",
72  "OFF",
73  "Z",
74  "ABSENT"
75 };
76 
77 struct svf_statemove {
80  uint32_t num_of_moves;
82 };
83 
84 /*
85  * These paths are from the SVF specification for the STATE command, to be
86  * used when the STATE command only includes the final state. The first
87  * element of the path is the "from" (current) state, and the last one is
88  * the "to" (target) state.
89  *
90  * All specified paths are the shortest ones in the JTAG spec, and are thus
91  * not (!!) exact matches for the paths used elsewhere in OpenOCD. Note
92  * that PAUSE-to-PAUSE transitions all go through UPDATE and then CAPTURE,
93  * which has specific effects on the various registers; they are not NOPs.
94  *
95  * Paths to RESET are disabled here. As elsewhere in OpenOCD, and in XSVF
96  * and many SVF implementations, we don't want to risk missing that state.
97  * To get to RESET, always we ignore the current state.
98  */
99 static const struct svf_statemove svf_statemoves[] = {
100  /* from to num_of_moves, paths[8] */
101 /* {TAP_RESET, TAP_RESET, 1, {TAP_RESET}}, */
108 
109 /* {TAP_IDLE, TAP_RESET, 4, {TAP_IDLE,
110  * TAP_DRSELECT, TAP_IRSELECT, TAP_RESET}}, */
111  {TAP_IDLE, TAP_IDLE, 1, {TAP_IDLE} },
116 
117 /* {TAP_DRPAUSE, TAP_RESET, 6, {TAP_DRPAUSE,
118  * TAP_DREXIT2, TAP_DRUPDATE, TAP_DRSELECT, TAP_IRSELECT, TAP_RESET}}, */
120  TAP_IDLE} },
127 
128 /* {TAP_IRPAUSE, TAP_RESET, 6, {TAP_IRPAUSE,
129  * TAP_IREXIT2, TAP_IRUPDATE, TAP_DRSELECT, TAP_IRSELECT, TAP_RESET}}, */
131  TAP_IDLE} },
138 };
139 
140 #define XXR_TDI (1 << 0)
141 #define XXR_TDO (1 << 1)
142 #define XXR_MASK (1 << 2)
143 #define XXR_SMASK (1 << 3)
144 
145 #define SVF_MAX_ADDCYCLES 255
146 
147 struct svf_xxr_para {
148  int len;
150  uint8_t *tdi;
151  uint8_t *tdo;
152  uint8_t *mask;
153  uint8_t *smask;
154 };
155 
156 struct svf_para {
157  float frequency;
162  enum trst_mode trst_mode;
163 
164  struct svf_xxr_para hir_para;
165  struct svf_xxr_para hdr_para;
166  struct svf_xxr_para tir_para;
167  struct svf_xxr_para tdr_para;
168  struct svf_xxr_para sir_para;
169  struct svf_xxr_para sdr_para;
170 };
171 
172 static struct svf_para svf_para;
173 static const struct svf_para svf_para_init = {
174 /* frequency, ir_end_state, dr_end_state, runtest_run_state, runtest_end_state, trst_mode */
176 /* hir_para */
177 /* {len, data_mask, tdi, tdo, mask, smask}, */
178  {0, 0, NULL, NULL, NULL, NULL},
179 /* hdr_para */
180 /* {len, data_mask, tdi, tdo, mask, smask}, */
181  {0, 0, NULL, NULL, NULL, NULL},
182 /* tir_para */
183 /* {len, data_mask, tdi, tdo, mask, smask}, */
184  {0, 0, NULL, NULL, NULL, NULL},
185 /* tdr_para */
186 /* {len, data_mask, tdi, tdo, mask, smask}, */
187  {0, 0, NULL, NULL, NULL, NULL},
188 /* sir_para */
189 /* {len, data_mask, tdi, tdo, mask, smask}, */
190  {0, 0, NULL, NULL, NULL, NULL},
191 /* sdr_para */
192 /* {len, data_mask, tdi, tdo, mask, smask}, */
193  {0, 0, NULL, NULL, NULL, NULL},
194 };
195 
197  int line_num; /* used to record line number of the check operation */
198  /* so more information could be printed */
199  int enabled; /* check is enabled or not */
200  int buffer_offset; /* buffer_offset to buffers */
201  int bit_len; /* bit length to check */
202 };
203 
204 #define SVF_CHECK_TDO_PARA_SIZE 1024
207 
208 static int svf_read_command_from_file(FILE *fd);
209 static int svf_check_tdo(void);
210 static int svf_add_check_para(uint8_t enabled, int buffer_offset, int bit_len);
211 static int svf_run_command(struct command_context *cmd_ctx, char *cmd_str);
212 static int svf_execute_tap(void);
213 
214 static FILE *svf_fd;
215 static char *svf_read_line;
216 static size_t svf_read_line_size;
217 static char *svf_command_buffer;
219 static int svf_line_number;
220 static int svf_getline(char **lineptr, size_t *n, FILE *stream);
221 
222 #define SVF_MAX_BUFFER_SIZE_TO_COMMIT (1024 * 1024)
225 static int svf_quiet;
226 static int svf_nil;
227 static int svf_ignore_error;
228 static bool svf_noreset;
229 static int svf_addcycles;
230 
231 /* Targeting particular tap */
233 static int svf_set_padding(struct svf_xxr_para *para, int len, unsigned char tdi);
234 
235 /* Progress Indicator */
237 static long svf_total_lines;
238 static int svf_percentage;
240 
241 /*
242  * macro is used to print the svf hex buffer at desired debug level
243  * DEBUG, INFO, ERROR, USER
244  */
245 #define SVF_BUF_LOG(_lvl, _buf, _nbits, _desc) \
246  svf_hexbuf_print(LOG_LVL_##_lvl, __FILE__, __LINE__, __func__, _buf, _nbits, _desc)
247 
248 static void svf_hexbuf_print(int dbg_lvl, const char *file, unsigned int line,
249  const char *function, const uint8_t *buf,
250  int bit_len, const char *desc)
251 {
252  int j, len = 0;
253  int byte_len = DIV_ROUND_UP(bit_len, 8);
254  int msbits = bit_len % 8;
255 
256  /* allocate 2 bytes per hex digit */
257  char *prbuf = malloc((byte_len * 2) + 2 + 1);
258  if (!prbuf)
259  return;
260 
261  /* print correct number of bytes, mask excess bits where applicable */
262  uint8_t msb = buf[byte_len - 1] & (msbits ? (1 << msbits) - 1 : 0xff);
263  len = sprintf(prbuf, msbits <= 4 ? "0x%01"PRIx8 : "0x%02"PRIx8, msb);
264  for (j = byte_len - 2; j >= 0; j--)
265  len += sprintf(prbuf + len, "%02"PRIx8, buf[j]);
266 
267  log_printf_lf(dbg_lvl, file, line, function, "%8s = %s", desc ? desc : " ", prbuf);
268 
269  free(prbuf);
270 }
271 
272 static int svf_realloc_buffers(size_t len)
273 {
274  void *ptr;
275 
276  if (svf_execute_tap() != ERROR_OK)
277  return ERROR_FAIL;
278 
279  ptr = realloc(svf_tdi_buffer, len);
280  if (!ptr)
281  return ERROR_FAIL;
282  svf_tdi_buffer = ptr;
283 
284  ptr = realloc(svf_tdo_buffer, len);
285  if (!ptr)
286  return ERROR_FAIL;
287  svf_tdo_buffer = ptr;
288 
289  ptr = realloc(svf_mask_buffer, len);
290  if (!ptr)
291  return ERROR_FAIL;
292  svf_mask_buffer = ptr;
293 
294  svf_buffer_size = len;
295 
296  return ERROR_OK;
297 }
298 
299 static void svf_free_xxd_para(struct svf_xxr_para *para)
300 {
301  if (para) {
302  free(para->tdi);
303  para->tdi = NULL;
304 
305  free(para->tdo);
306  para->tdo = NULL;
307 
308  free(para->mask);
309  para->mask = NULL;
310 
311  free(para->smask);
312  para->smask = NULL;
313  }
314 }
315 
317 {
318  tap_state_t state_from = cmd_queue_cur_state;
319  unsigned int index_var;
320 
321  /* when resetting, be paranoid and ignore current state */
322  if (state_to == TAP_RESET) {
323  if (svf_nil)
324  return ERROR_OK;
325 
326  jtag_add_tlr();
327  return ERROR_OK;
328  }
329 
330  for (index_var = 0; index_var < ARRAY_SIZE(svf_statemoves); index_var++) {
331  if ((svf_statemoves[index_var].from == state_from)
332  && (svf_statemoves[index_var].to == state_to)) {
333  if (svf_nil)
334  continue;
335  /* recorded path includes current state ... avoid
336  *extra TCKs! */
337  if (svf_statemoves[index_var].num_of_moves > 1)
338  jtag_add_pathmove(svf_statemoves[index_var].num_of_moves - 1,
339  svf_statemoves[index_var].paths + 1);
340  else
341  jtag_add_pathmove(svf_statemoves[index_var].num_of_moves,
342  svf_statemoves[index_var].paths);
343  return ERROR_OK;
344  }
345  }
346  LOG_ERROR("SVF: can not move to %s", tap_state_name(state_to));
347  return ERROR_FAIL;
348 }
349 
358  /* DEPRECATED */
363 };
364 
365 static const struct nvp svf_cmd_opts[] = {
366  { .name = "-addcycles", .value = OPT_ADDCYCLES },
367  { .name = "-ignore_error", .value = OPT_IGNORE_ERROR },
368  { .name = "-nil", .value = OPT_NIL },
369  { .name = "-noreset", .value = OPT_NORESET },
370  { .name = "-progress", .value = OPT_PROGRESS },
371  { .name = "-quiet", .value = OPT_QUIET },
372  { .name = "-tap", .value = OPT_TAP },
373  /* DEPRECATED */
374  { .name = "ignore_error", .value = DEPRECATED_OPT_IGNORE_ERROR },
375  { .name = "nil", .value = DEPRECATED_OPT_NIL },
376  { .name = "progress", .value = DEPRECATED_OPT_PROGRESS },
377  { .name = "quiet", .value = DEPRECATED_OPT_QUIET },
378  { .name = NULL, .value = -1 }
379 };
380 
381 COMMAND_HANDLER(handle_svf_command)
382 {
383 #define SVF_MIN_NUM_OF_OPTIONS 1
384 #define SVF_MAX_NUM_OF_OPTIONS 8
385  int command_num = 0;
386  int ret = ERROR_OK;
387  int64_t time_measure_ms;
388  int time_measure_s, time_measure_m;
389 
390  /*
391  * use NULL to indicate a "plain" svf file which accounts for
392  * any additional devices in the scan chain, otherwise the device
393  * that should be affected
394  */
395  struct jtag_tap *tap = NULL;
396 
399 
400  /* parse command line */
401  svf_quiet = 0;
402  svf_nil = 0;
404  svf_ignore_error = 0;
405  svf_noreset = false;
406  svf_addcycles = 0;
407 
408  for (unsigned int i = 0; i < CMD_ARGC; i++) {
409  const struct nvp *n = nvp_name2value(svf_cmd_opts, CMD_ARGV[i]);
410  switch (n->value) {
411  case OPT_ADDCYCLES:
412  svf_addcycles = atoi(CMD_ARGV[i + 1]);
414  command_print(CMD, "addcycles: %s out of range", CMD_ARGV[i + 1]);
415  if (svf_fd)
416  fclose(svf_fd);
417  svf_fd = NULL;
419  }
420  i++;
421  break;
422 
423  case OPT_TAP:
424  tap = jtag_tap_by_string(CMD_ARGV[i+1]);
425  if (!tap) {
426  command_print(CMD, "Tap: %s unknown", CMD_ARGV[i+1]);
427  if (svf_fd)
428  fclose(svf_fd);
429  svf_fd = NULL;
431  }
432  i++;
433  break;
434 
436  LOG_INFO("DEPRECATED flag '%s'; use '-%s'", CMD_ARGV[i], CMD_ARGV[i]);
437  /* fallthrough */
438  case OPT_QUIET:
439  svf_quiet = 1;
440  break;
441 
442  case DEPRECATED_OPT_NIL:
443  LOG_INFO("DEPRECATED flag '%s'; use '-%s'", CMD_ARGV[i], CMD_ARGV[i]);
444  /* fallthrough */
445  case OPT_NIL:
446  svf_nil = 1;
447  break;
448 
450  LOG_INFO("DEPRECATED flag '%s'; use '-%s'", CMD_ARGV[i], CMD_ARGV[i]);
451  /* fallthrough */
452  case OPT_PROGRESS:
454  break;
455 
457  LOG_INFO("DEPRECATED flag '%s'; use '-%s'", CMD_ARGV[i], CMD_ARGV[i]);
458  /* fallthrough */
459  case OPT_IGNORE_ERROR:
460  svf_ignore_error = 1;
461  break;
462 
463  case OPT_NORESET:
464  svf_noreset = true;
465  break;
466 
467  default:
468  svf_fd = fopen(CMD_ARGV[i], "r");
469  if (!svf_fd) {
470  int err = errno;
471  command_print(CMD, "open(\"%s\"): %s", CMD_ARGV[i], strerror(err));
472  /* no need to free anything now */
474  }
475  LOG_USER("svf processing file: \"%s\"", CMD_ARGV[i]);
476  break;
477  }
478  }
479 
480  if (!svf_fd)
482 
483  /* get time */
484  time_measure_ms = timeval_ms();
485 
486  /* init */
487  svf_line_number = 0;
489 
492  if (!svf_check_tdo_para) {
493  LOG_ERROR("not enough memory");
494  ret = ERROR_FAIL;
495  goto free_all;
496  }
497 
498  svf_buffer_index = 0;
499  /* double the buffer size */
500  /* in case current command cannot be committed, and next command is a bit scan command */
501  /* here is 32K bits for this big scan command, it should be enough */
502  /* buffer will be reallocated if buffer size is not enough */
504  ret = ERROR_FAIL;
505  goto free_all;
506  }
507 
508  memcpy(&svf_para, &svf_para_init, sizeof(svf_para));
509 
510  if (!svf_nil && !svf_noreset) {
511  /* TAP_RESET */
512  jtag_add_tlr();
513  }
514 
515  if (tap) {
516  /* Tap is specified, set header/trailer paddings */
517  int header_ir_len = 0, header_dr_len = 0, trailer_ir_len = 0, trailer_dr_len = 0;
518  struct jtag_tap *check_tap;
519 
521 
522  for (check_tap = jtag_all_taps(); check_tap; check_tap = check_tap->next_tap) {
523  if (check_tap->abs_chain_position < tap->abs_chain_position) {
524  /* Header */
525  header_ir_len += check_tap->ir_length;
526  header_dr_len++;
527  } else if (check_tap->abs_chain_position > tap->abs_chain_position) {
528  /* Trailer */
529  trailer_ir_len += check_tap->ir_length;
530  trailer_dr_len++;
531  }
532  }
533 
534  /* HDR %d TDI (0) */
535  ret = svf_set_padding(&svf_para.hdr_para, header_dr_len, 0);
536  if (ret != ERROR_OK) {
537  command_print(CMD, "failed to set data header");
538  goto free_all;
539  }
540 
541  /* HIR %d TDI (0xFF) */
542  ret = svf_set_padding(&svf_para.hir_para, header_ir_len, 0xFF);
543  if (ret != ERROR_OK) {
544  command_print(CMD, "failed to set instruction header");
545  goto free_all;
546  }
547 
548  /* TDR %d TDI (0) */
549  ret = svf_set_padding(&svf_para.tdr_para, trailer_dr_len, 0);
550  if (ret != ERROR_OK) {
551  command_print(CMD, "failed to set data trailer");
552  goto free_all;
553  }
554 
555  /* TIR %d TDI (0xFF) */
556  ret = svf_set_padding(&svf_para.tir_para, trailer_ir_len, 0xFF);
557  if (ret != ERROR_OK) {
558  command_print(CMD, "failed to set instruction trailer");
559  goto free_all;
560  }
561  }
562 
563  if (svf_progress_enabled) {
564  /* Count total lines in file. */
565  while (!feof(svf_fd)) {
567  svf_total_lines++;
568  }
569  rewind(svf_fd);
570  }
572  /* Log Output */
573  if (svf_quiet) {
574  if (svf_progress_enabled) {
577  LOG_USER_N("\r%d%% ", svf_percentage);
579  }
580  }
581  } else {
582  if (svf_progress_enabled) {
585  } else
586  LOG_USER_N("%s", svf_read_line);
587  }
588  /* Run Command */
590  LOG_ERROR("fail to run command at line %d", svf_line_number);
591  ret = ERROR_FAIL;
592  break;
593  }
594  command_num++;
595  }
596 
597  if ((!svf_nil) && (jtag_execute_queue() != ERROR_OK))
598  ret = ERROR_FAIL;
599  else if (svf_check_tdo() != ERROR_OK)
600  ret = ERROR_FAIL;
601 
602  /* print time */
603  time_measure_ms = timeval_ms() - time_measure_ms;
604  time_measure_s = time_measure_ms / 1000;
605  time_measure_ms %= 1000;
606  time_measure_m = time_measure_s / 60;
607  time_measure_s %= 60;
608  if (time_measure_ms < 1000)
610  "\r\nTime used: %dm%ds%" PRId64 "ms ",
611  time_measure_m,
612  time_measure_s,
613  time_measure_ms);
614 
615 free_all:
616 
617  fclose(svf_fd);
618  svf_fd = NULL;
619 
620  /* free buffers */
621  free(svf_command_buffer);
624 
625  free(svf_check_tdo_para);
628 
629  free(svf_tdi_buffer);
631 
632  free(svf_tdo_buffer);
634 
635  free(svf_mask_buffer);
637 
638  svf_buffer_index = 0;
639  svf_buffer_size = 0;
640 
647 
648  if (ret == ERROR_OK)
650  "svf file programmed %s for %d commands with %d errors",
651  (svf_ignore_error > 1) ? "unsuccessfully" : "successfully",
652  command_num,
653  (svf_ignore_error > 1) ? (svf_ignore_error - 1) : 0);
654  else
655  command_print(CMD, "svf file programmed failed");
656 
657  svf_ignore_error = 0;
658  return ret;
659 }
660 
661 static int svf_getline(char **lineptr, size_t *n, FILE *stream)
662 {
663 #define MIN_CHUNK 16 /* Buffer is increased by this size each time as required */
664  size_t i = 0;
665 
666  if (!*lineptr) {
667  *n = MIN_CHUNK;
668  *lineptr = malloc(*n);
669  if (!*lineptr)
670  return -1;
671  }
672 
673  (*lineptr)[0] = fgetc(stream);
674  while ((*lineptr)[i] != '\n') {
675  (*lineptr)[++i] = fgetc(stream);
676  if (feof(stream)) {
677  (*lineptr)[0] = 0;
678  return -1;
679  }
680  if ((i + 2) > *n) {
681  *n += MIN_CHUNK;
682  *lineptr = realloc(*lineptr, *n);
683  }
684  }
685 
686  (*lineptr)[++i] = 0;
687 
688  return sizeof(*lineptr);
689 }
690 
691 #define SVFP_CMD_INC_CNT 1024
692 static int svf_read_command_from_file(FILE *fd)
693 {
694  unsigned char ch;
695  int i = 0;
696  size_t cmd_pos = 0;
697  int cmd_ok = 0, slash = 0;
698 
700  return ERROR_FAIL;
701  svf_line_number++;
702  ch = svf_read_line[0];
703  while (!cmd_ok && (ch != 0)) {
704  switch (ch) {
705  case '!':
706  slash = 0;
708  return ERROR_FAIL;
709  svf_line_number++;
710  i = -1;
711  break;
712  case '/':
713  if (++slash == 2) {
714  slash = 0;
716  svf_fd) <= 0)
717  return ERROR_FAIL;
718  svf_line_number++;
719  i = -1;
720  }
721  break;
722  case ';':
723  slash = 0;
724  cmd_ok = 1;
725  break;
726  case '\n':
727  svf_line_number++;
729  return ERROR_FAIL;
730  i = -1;
731  /* fallthrough */
732  case '\r':
733  slash = 0;
734  /* Don't save '\r' and '\n' if no data is parsed */
735  if (!cmd_pos)
736  break;
737  /* fallthrough */
738  default:
739  /* The parsing code currently expects a space
740  * before parentheses -- "TDI (123)". Also a
741  * space afterwards -- "TDI (123) TDO(456)".
742  * But such spaces are optional... instead of
743  * parser updates, cope with that by adding the
744  * spaces as needed.
745  *
746  * Ensure there are 3 bytes available, for:
747  * - current character
748  * - added space.
749  * - terminating NUL ('\0')
750  */
751  if (cmd_pos + 3 > svf_command_buffer_size) {
752  svf_command_buffer = realloc(svf_command_buffer, cmd_pos + 3);
753  svf_command_buffer_size = cmd_pos + 3;
754  if (!svf_command_buffer) {
755  LOG_ERROR("not enough memory");
756  return ERROR_FAIL;
757  }
758  }
759 
760  /* insert a space before '(' */
761  if ('(' == ch)
762  svf_command_buffer[cmd_pos++] = ' ';
763 
764  svf_command_buffer[cmd_pos++] = (char)toupper(ch);
765 
766  /* insert a space after ')' */
767  if (')' == ch)
768  svf_command_buffer[cmd_pos++] = ' ';
769  break;
770  }
771  ch = svf_read_line[++i];
772  }
773 
774  if (cmd_ok) {
775  svf_command_buffer[cmd_pos] = '\0';
776  return ERROR_OK;
777  } else
778  return ERROR_FAIL;
779 }
780 
781 static int svf_parse_cmd_string(char *str, int len, char **argus, int *num_of_argu)
782 {
783  int pos = 0, num = 0, space_found = 1, in_bracket = 0;
784 
785  while (pos < len) {
786  switch (str[pos]) {
787  case '!':
788  case '/':
789  LOG_ERROR("fail to parse svf command");
790  return ERROR_FAIL;
791  case '(':
792  in_bracket = 1;
793  goto parse_char;
794  case ')':
795  in_bracket = 0;
796  goto parse_char;
797  default:
798 parse_char:
799  if (!in_bracket && isspace((int) str[pos])) {
800  space_found = 1;
801  str[pos] = '\0';
802  } else if (space_found) {
803  argus[num++] = &str[pos];
804  space_found = 0;
805  }
806  break;
807  }
808  pos++;
809  }
810 
811  if (num == 0)
812  return ERROR_FAIL;
813 
814  *num_of_argu = num;
815 
816  return ERROR_OK;
817 }
818 
820 {
821  return (state == TAP_RESET) || (state == TAP_IDLE)
822  || (state == TAP_DRPAUSE) || (state == TAP_IRPAUSE);
823 }
824 
825 static int svf_find_string_in_array(char *str, char **strs, int num_of_element)
826 {
827  int i;
828 
829  for (i = 0; i < num_of_element; i++) {
830  if (!strcmp(str, strs[i]))
831  return i;
832  }
833  return 0xFF;
834 }
835 
836 static int svf_adjust_array_length(uint8_t **arr, int orig_bit_len, int new_bit_len)
837 {
838  int new_byte_len = (new_bit_len + 7) >> 3;
839 
840  if ((!*arr) || (((orig_bit_len + 7) >> 3) < ((new_bit_len + 7) >> 3))) {
841  free(*arr);
842  *arr = calloc(1, new_byte_len);
843  if (!*arr) {
844  LOG_ERROR("not enough memory");
845  return ERROR_FAIL;
846  }
847  }
848  return ERROR_OK;
849 }
850 
851 static int svf_set_padding(struct svf_xxr_para *para, int len, unsigned char tdi)
852 {
853  int error = ERROR_OK;
854  error |= svf_adjust_array_length(&para->tdi, para->len, len);
855  memset(para->tdi, tdi, (len + 7) >> 3);
856  error |= svf_adjust_array_length(&para->tdo, para->len, len);
857  error |= svf_adjust_array_length(&para->mask, para->len, len);
858  para->len = len;
859  para->data_mask = XXR_TDI;
860 
861  return error;
862 }
863 
864 static int svf_copy_hexstring_to_binary(char *str, uint8_t **bin, int orig_bit_len, int bit_len)
865 {
866  int i, str_len = strlen(str), str_hbyte_len = (bit_len + 3) >> 2;
867  uint8_t ch = 0;
868 
869  if (svf_adjust_array_length(bin, orig_bit_len, bit_len) != ERROR_OK) {
870  LOG_ERROR("fail to adjust length of array");
871  return ERROR_FAIL;
872  }
873 
874  /* fill from LSB (end of str) to MSB (beginning of str) */
875  for (i = 0; i < str_hbyte_len; i++) {
876  ch = 0;
877  while (str_len > 0) {
878  ch = str[--str_len];
879 
880  /* Skip whitespace. The SVF specification (rev E) is
881  * deficient in terms of basic lexical issues like
882  * where whitespace is allowed. Long bitstrings may
883  * require line ends for correctness, since there is
884  * a hard limit on line length.
885  */
886  if (!isspace(ch)) {
887  if ((ch >= '0') && (ch <= '9')) {
888  ch = ch - '0';
889  break;
890  } else if ((ch >= 'A') && (ch <= 'F')) {
891  ch = ch - 'A' + 10;
892  break;
893  } else {
894  LOG_ERROR("invalid hex string");
895  return ERROR_FAIL;
896  }
897  }
898 
899  ch = 0;
900  }
901 
902  /* write bin */
903  if (i % 2) {
904  /* MSB */
905  (*bin)[i / 2] |= ch << 4;
906  } else {
907  /* LSB */
908  (*bin)[i / 2] = 0;
909  (*bin)[i / 2] |= ch;
910  }
911  }
912 
913  /* consume optional leading '0' MSBs or whitespace */
914  while (str_len > 0 && ((str[str_len - 1] == '0')
915  || isspace((int) str[str_len - 1])))
916  str_len--;
917 
918  /* check validity: we must have consumed everything */
919  if (str_len > 0 || (ch & ~((2 << ((bit_len - 1) % 4)) - 1)) != 0) {
920  LOG_ERROR("value exceeds length");
921  return ERROR_FAIL;
922  }
923 
924  return ERROR_OK;
925 }
926 
927 static int svf_check_tdo(void)
928 {
929  int i, len, index_var;
930 
931  for (i = 0; i < svf_check_tdo_para_index; i++) {
932  index_var = svf_check_tdo_para[i].buffer_offset;
933  len = svf_check_tdo_para[i].bit_len;
934  if ((svf_check_tdo_para[i].enabled)
935  && !buf_eq_mask(&svf_tdi_buffer[index_var], &svf_tdo_buffer[index_var],
936  &svf_mask_buffer[index_var], len)) {
937  LOG_ERROR("tdo check error at line %d",
938  svf_check_tdo_para[i].line_num);
939  SVF_BUF_LOG(ERROR, &svf_tdi_buffer[index_var], len, "READ");
940  SVF_BUF_LOG(ERROR, &svf_tdo_buffer[index_var], len, "WANT");
941  SVF_BUF_LOG(ERROR, &svf_mask_buffer[index_var], len, "MASK");
942 
943  if (svf_ignore_error == 0)
944  return ERROR_FAIL;
945  else
947  }
948  }
950 
951  return ERROR_OK;
952 }
953 
954 static int svf_add_check_para(uint8_t enabled, int buffer_offset, int bit_len)
955 {
957  LOG_ERROR("toooooo many operation undone");
958  return ERROR_FAIL;
959  }
960 
966 
967  return ERROR_OK;
968 }
969 
970 static int svf_execute_tap(void)
971 {
972  if ((!svf_nil) && (jtag_execute_queue() != ERROR_OK))
973  return ERROR_FAIL;
974  else if (svf_check_tdo() != ERROR_OK)
975  return ERROR_FAIL;
976 
977  svf_buffer_index = 0;
978 
979  return ERROR_OK;
980 }
981 
982 static int svf_run_command(struct command_context *cmd_ctx, char *cmd_str)
983 {
984  char *argus[256], command;
985  int num_of_argu = 0, i;
986 
987  /* tmp variable */
988  int i_tmp;
989 
990  /* for RUNTEST */
991  int run_count;
992  float min_time;
993  /* for XXR */
994  struct svf_xxr_para *xxr_para_tmp;
995  uint8_t **pbuffer_tmp;
996  struct scan_field field;
997  /* for STATE */
998  tap_state_t *path = NULL, state;
999  /* flag padding commands skipped due to -tap command */
1000  int padding_command_skipped = 0;
1001 
1002  if (svf_parse_cmd_string(cmd_str, strlen(cmd_str), argus, &num_of_argu) != ERROR_OK)
1003  return ERROR_FAIL;
1004 
1005  /* NOTE: we're a bit loose here, because we ignore case in
1006  * TAP state names (instead of insisting on uppercase).
1007  */
1008 
1009  command = svf_find_string_in_array(argus[0],
1011  switch (command) {
1012  case ENDDR:
1013  case ENDIR:
1014  if (num_of_argu != 2) {
1015  LOG_ERROR("invalid parameter of %s", argus[0]);
1016  return ERROR_FAIL;
1017  }
1018 
1019  i_tmp = tap_state_by_name(argus[1]);
1020 
1021  if (svf_tap_state_is_stable(i_tmp)) {
1022  if (command == ENDIR) {
1023  svf_para.ir_end_state = i_tmp;
1024  LOG_DEBUG("\tIR end_state = %s",
1025  tap_state_name(i_tmp));
1026  } else {
1027  svf_para.dr_end_state = i_tmp;
1028  LOG_DEBUG("\tDR end_state = %s",
1029  tap_state_name(i_tmp));
1030  }
1031  } else {
1032  LOG_ERROR("%s: %s is not a stable state",
1033  argus[0], argus[1]);
1034  return ERROR_FAIL;
1035  }
1036  break;
1037  case FREQUENCY:
1038  if ((num_of_argu != 1) && (num_of_argu != 3)) {
1039  LOG_ERROR("invalid parameter of %s", argus[0]);
1040  return ERROR_FAIL;
1041  }
1042  if (num_of_argu == 1) {
1043  /* TODO: set jtag speed to full speed */
1044  svf_para.frequency = 0;
1045  } else {
1046  if (strcmp(argus[2], "HZ")) {
1047  LOG_ERROR("HZ not found in FREQUENCY command");
1048  return ERROR_FAIL;
1049  }
1050  if (svf_execute_tap() != ERROR_OK)
1051  return ERROR_FAIL;
1052  svf_para.frequency = atof(argus[1]);
1053  /* TODO: set jtag speed to */
1054  if (svf_para.frequency > 0) {
1055  command_run_linef(cmd_ctx,
1056  "adapter speed %d",
1057  (int)svf_para.frequency / 1000);
1058  LOG_DEBUG("\tfrequency = %f", svf_para.frequency);
1059  }
1060  }
1061  break;
1062  case HDR:
1063  if (svf_tap_is_specified) {
1064  padding_command_skipped = 1;
1065  break;
1066  }
1067  xxr_para_tmp = &svf_para.hdr_para;
1068  goto xxr_common;
1069  case HIR:
1070  if (svf_tap_is_specified) {
1071  padding_command_skipped = 1;
1072  break;
1073  }
1074  xxr_para_tmp = &svf_para.hir_para;
1075  goto xxr_common;
1076  case TDR:
1077  if (svf_tap_is_specified) {
1078  padding_command_skipped = 1;
1079  break;
1080  }
1081  xxr_para_tmp = &svf_para.tdr_para;
1082  goto xxr_common;
1083  case TIR:
1084  if (svf_tap_is_specified) {
1085  padding_command_skipped = 1;
1086  break;
1087  }
1088  xxr_para_tmp = &svf_para.tir_para;
1089  goto xxr_common;
1090  case SDR:
1091  xxr_para_tmp = &svf_para.sdr_para;
1092  goto xxr_common;
1093  case SIR:
1094  xxr_para_tmp = &svf_para.sir_para;
1095  goto xxr_common;
1096 xxr_common:
1097  /* XXR length [TDI (tdi)] [TDO (tdo)][MASK (mask)] [SMASK (smask)] */
1098  if ((num_of_argu > 10) || (num_of_argu % 2)) {
1099  LOG_ERROR("invalid parameter of %s", argus[0]);
1100  return ERROR_FAIL;
1101  }
1102  i_tmp = xxr_para_tmp->len;
1103  xxr_para_tmp->len = atoi(argus[1]);
1104  /* If we are to enlarge the buffers, all parts of xxr_para_tmp
1105  * need to be freed */
1106  if (i_tmp < xxr_para_tmp->len) {
1107  free(xxr_para_tmp->tdi);
1108  xxr_para_tmp->tdi = NULL;
1109  free(xxr_para_tmp->tdo);
1110  xxr_para_tmp->tdo = NULL;
1111  free(xxr_para_tmp->mask);
1112  xxr_para_tmp->mask = NULL;
1113  free(xxr_para_tmp->smask);
1114  xxr_para_tmp->smask = NULL;
1115  }
1116 
1117  LOG_DEBUG("\tlength = %d", xxr_para_tmp->len);
1118  xxr_para_tmp->data_mask = 0;
1119  for (i = 2; i < num_of_argu; i += 2) {
1120  if ((strlen(argus[i + 1]) < 3) || (argus[i + 1][0] != '(') ||
1121  (argus[i + 1][strlen(argus[i + 1]) - 1] != ')')) {
1122  LOG_ERROR("data section error");
1123  return ERROR_FAIL;
1124  }
1125  argus[i + 1][strlen(argus[i + 1]) - 1] = '\0';
1126  /* TDI, TDO, MASK, SMASK */
1127  if (!strcmp(argus[i], "TDI")) {
1128  /* TDI */
1129  pbuffer_tmp = &xxr_para_tmp->tdi;
1130  xxr_para_tmp->data_mask |= XXR_TDI;
1131  } else if (!strcmp(argus[i], "TDO")) {
1132  /* TDO */
1133  pbuffer_tmp = &xxr_para_tmp->tdo;
1134  xxr_para_tmp->data_mask |= XXR_TDO;
1135  } else if (!strcmp(argus[i], "MASK")) {
1136  /* MASK */
1137  pbuffer_tmp = &xxr_para_tmp->mask;
1138  xxr_para_tmp->data_mask |= XXR_MASK;
1139  } else if (!strcmp(argus[i], "SMASK")) {
1140  /* SMASK */
1141  pbuffer_tmp = &xxr_para_tmp->smask;
1142  xxr_para_tmp->data_mask |= XXR_SMASK;
1143  } else {
1144  LOG_ERROR("unknown parameter: %s", argus[i]);
1145  return ERROR_FAIL;
1146  }
1147  if (ERROR_OK !=
1148  svf_copy_hexstring_to_binary(&argus[i + 1][1], pbuffer_tmp, i_tmp,
1149  xxr_para_tmp->len)) {
1150  LOG_ERROR("fail to parse hex value");
1151  return ERROR_FAIL;
1152  }
1153  SVF_BUF_LOG(DEBUG, *pbuffer_tmp, xxr_para_tmp->len, argus[i]);
1154  }
1155  /* If a command changes the length of the last scan of the same type and the
1156  * MASK parameter is absent, */
1157  /* the mask pattern used is all cares */
1158  if (!(xxr_para_tmp->data_mask & XXR_MASK) && (i_tmp != xxr_para_tmp->len)) {
1159  /* MASK not defined and length changed */
1160  if (ERROR_OK !=
1161  svf_adjust_array_length(&xxr_para_tmp->mask, i_tmp,
1162  xxr_para_tmp->len)) {
1163  LOG_ERROR("fail to adjust length of array");
1164  return ERROR_FAIL;
1165  }
1166  buf_set_ones(xxr_para_tmp->mask, xxr_para_tmp->len);
1167  }
1168  /* If TDO is absent, no comparison is needed, set the mask to 0 */
1169  if (!(xxr_para_tmp->data_mask & XXR_TDO)) {
1170  if (!xxr_para_tmp->tdo) {
1171  if (ERROR_OK !=
1172  svf_adjust_array_length(&xxr_para_tmp->tdo, i_tmp,
1173  xxr_para_tmp->len)) {
1174  LOG_ERROR("fail to adjust length of array");
1175  return ERROR_FAIL;
1176  }
1177  }
1178  if (!xxr_para_tmp->mask) {
1179  if (ERROR_OK !=
1180  svf_adjust_array_length(&xxr_para_tmp->mask, i_tmp,
1181  xxr_para_tmp->len)) {
1182  LOG_ERROR("fail to adjust length of array");
1183  return ERROR_FAIL;
1184  }
1185  }
1186  memset(xxr_para_tmp->mask, 0, (xxr_para_tmp->len + 7) >> 3);
1187  }
1188  /* do scan if necessary */
1189  if (command == SDR) {
1190  /* check buffer size first, reallocate if necessary */
1193  if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3)) {
1194  /* reallocate buffer */
1195  if (svf_realloc_buffers(svf_buffer_index + ((i + 7) >> 3)) != ERROR_OK) {
1196  LOG_ERROR("not enough memory");
1197  return ERROR_FAIL;
1198  }
1199  }
1200 
1201  /* assemble dr data */
1202  i = 0;
1204  0,
1206  i,
1208  i += svf_para.hdr_para.len;
1210  0,
1212  i,
1214  i += svf_para.sdr_para.len;
1216  0,
1218  i,
1220  i += svf_para.tdr_para.len;
1221 
1222  /* add check data */
1224  /* assemble dr mask data */
1225  i = 0;
1227  0,
1229  i,
1231  i += svf_para.hdr_para.len;
1233  0,
1235  i,
1237  i += svf_para.sdr_para.len;
1239  0,
1241  i,
1243 
1244  /* assemble dr check data */
1245  i = 0;
1247  0,
1249  i,
1251  i += svf_para.hdr_para.len;
1253  0,
1255  i,
1257  i += svf_para.sdr_para.len;
1259  0,
1261  i,
1263  i += svf_para.tdr_para.len;
1264 
1266  } else
1268  field.num_bits = i;
1270  field.in_value = (xxr_para_tmp->data_mask & XXR_TDO) ? &svf_tdi_buffer[svf_buffer_index] : NULL;
1271  if (!svf_nil) {
1272  /* NOTE: doesn't use SVF-specified state paths */
1274  field.out_value,
1275  field.in_value,
1277  }
1278 
1279  if (svf_addcycles)
1281 
1282  svf_buffer_index += (i + 7) >> 3;
1283  } else if (command == SIR) {
1284  /* check buffer size first, reallocate if necessary */
1287  if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3)) {
1288  if (svf_realloc_buffers(svf_buffer_index + ((i + 7) >> 3)) != ERROR_OK) {
1289  LOG_ERROR("not enough memory");
1290  return ERROR_FAIL;
1291  }
1292  }
1293 
1294  /* assemble ir data */
1295  i = 0;
1297  0,
1299  i,
1301  i += svf_para.hir_para.len;
1303  0,
1305  i,
1307  i += svf_para.sir_para.len;
1309  0,
1311  i,
1313  i += svf_para.tir_para.len;
1314 
1315  /* add check data */
1317  /* assemble dr mask data */
1318  i = 0;
1320  0,
1322  i,
1324  i += svf_para.hir_para.len;
1326  0,
1328  i,
1330  i += svf_para.sir_para.len;
1332  0,
1334  i,
1336 
1337  /* assemble dr check data */
1338  i = 0;
1340  0,
1342  i,
1344  i += svf_para.hir_para.len;
1346  0,
1348  i,
1350  i += svf_para.sir_para.len;
1352  0,
1354  i,
1356  i += svf_para.tir_para.len;
1357 
1359  } else
1361  field.num_bits = i;
1363  field.in_value = (xxr_para_tmp->data_mask & XXR_TDO) ? &svf_tdi_buffer[svf_buffer_index] : NULL;
1364  if (!svf_nil) {
1365  /* NOTE: doesn't use SVF-specified state paths */
1367  field.out_value,
1368  field.in_value,
1370  }
1371 
1372  svf_buffer_index += (i + 7) >> 3;
1373  }
1374  break;
1375  case PIO:
1376  case PIOMAP:
1377  LOG_ERROR("PIO and PIOMAP are not supported");
1378  return ERROR_FAIL;
1379  case RUNTEST:
1380  /* RUNTEST [run_state] run_count run_clk [min_time SEC [MAXIMUM max_time
1381  * SEC]] [ENDSTATE end_state] */
1382  /* RUNTEST [run_state] min_time SEC [MAXIMUM max_time SEC] [ENDSTATE
1383  * end_state] */
1384  if ((num_of_argu < 3) || (num_of_argu > 11)) {
1385  LOG_ERROR("invalid parameter of %s", argus[0]);
1386  return ERROR_FAIL;
1387  }
1388  /* init */
1389  run_count = 0;
1390  min_time = 0;
1391  i = 1;
1392 
1393  /* run_state */
1394  i_tmp = tap_state_by_name(argus[i]);
1395  if (i_tmp != TAP_INVALID) {
1396  if (svf_tap_state_is_stable(i_tmp)) {
1397  svf_para.runtest_run_state = i_tmp;
1398 
1399  /* When a run_state is specified, the new
1400  * run_state becomes the default end_state.
1401  */
1402  svf_para.runtest_end_state = i_tmp;
1403  LOG_DEBUG("\trun_state = %s", tap_state_name(i_tmp));
1404  i++;
1405  } else {
1406  LOG_ERROR("%s: %s is not a stable state", argus[0], tap_state_name(i_tmp));
1407  return ERROR_FAIL;
1408  }
1409  }
1410 
1411  /* run_count run_clk */
1412  if (((i + 2) <= num_of_argu) && strcmp(argus[i + 1], "SEC")) {
1413  if (!strcmp(argus[i + 1], "TCK")) {
1414  /* clock source is TCK */
1415  run_count = atoi(argus[i]);
1416  LOG_DEBUG("\trun_count@TCK = %d", run_count);
1417  } else {
1418  LOG_ERROR("%s not supported for clock", argus[i + 1]);
1419  return ERROR_FAIL;
1420  }
1421  i += 2;
1422  }
1423  /* min_time SEC */
1424  if (((i + 2) <= num_of_argu) && !strcmp(argus[i + 1], "SEC")) {
1425  min_time = atof(argus[i]);
1426  LOG_DEBUG("\tmin_time = %fs", min_time);
1427  i += 2;
1428  }
1429  /* MAXIMUM max_time SEC */
1430  if (((i + 3) <= num_of_argu) &&
1431  !strcmp(argus[i], "MAXIMUM") && !strcmp(argus[i + 2], "SEC")) {
1432  float max_time = 0;
1433  max_time = atof(argus[i + 1]);
1434  LOG_DEBUG("\tmax_time = %fs", max_time);
1435  i += 3;
1436  }
1437  /* ENDSTATE end_state */
1438  if (((i + 2) <= num_of_argu) && !strcmp(argus[i], "ENDSTATE")) {
1439  i_tmp = tap_state_by_name(argus[i + 1]);
1440 
1441  if (svf_tap_state_is_stable(i_tmp)) {
1442  svf_para.runtest_end_state = i_tmp;
1443  LOG_DEBUG("\tend_state = %s", tap_state_name(i_tmp));
1444  } else {
1445  LOG_ERROR("%s: %s is not a stable state", argus[0], tap_state_name(i_tmp));
1446  return ERROR_FAIL;
1447  }
1448  i += 2;
1449  }
1450 
1451  /* all parameter should be parsed */
1452  if (i == num_of_argu) {
1453 #if 1
1454  /* FIXME handle statemove failures */
1455  uint32_t min_usec = 1000000 * min_time;
1456 
1457  /* enter into run_state if necessary */
1460 
1461  /* add clocks and/or min wait */
1462  if (run_count > 0) {
1463  if (!svf_nil)
1464  jtag_add_clocks(run_count);
1465  }
1466 
1467  if (min_usec > 0) {
1468  if (!svf_nil)
1469  jtag_add_sleep(min_usec);
1470  }
1471 
1472  /* move to end_state if necessary */
1475 
1476 #else
1478  LOG_ERROR("cannot runtest in %s state",
1480  return ERROR_FAIL;
1481  }
1482 
1483  if (!svf_nil)
1485 #endif
1486  } else {
1487  LOG_ERROR("fail to parse parameter of RUNTEST, %d out of %d is parsed",
1488  i,
1489  num_of_argu);
1490  return ERROR_FAIL;
1491  }
1492  break;
1493  case STATE:
1494  /* STATE [pathstate1 [pathstate2 ...[pathstaten]]] stable_state */
1495  if (num_of_argu < 2) {
1496  LOG_ERROR("invalid parameter of %s", argus[0]);
1497  return ERROR_FAIL;
1498  }
1499  if (num_of_argu > 2) {
1500  /* STATE pathstate1 ... stable_state */
1501  path = malloc((num_of_argu - 1) * sizeof(tap_state_t));
1502  if (!path) {
1503  LOG_ERROR("not enough memory");
1504  return ERROR_FAIL;
1505  }
1506  num_of_argu--; /* num of path */
1507  i_tmp = 1; /* path is from parameter 1 */
1508  for (i = 0; i < num_of_argu; i++, i_tmp++) {
1509  path[i] = tap_state_by_name(argus[i_tmp]);
1510  if (path[i] == TAP_INVALID) {
1511  LOG_ERROR("%s: %s is not a valid state", argus[0], argus[i_tmp]);
1512  free(path);
1513  return ERROR_FAIL;
1514  }
1515  /* OpenOCD refuses paths containing TAP_RESET */
1516  if (path[i] == TAP_RESET) {
1517  /* FIXME last state MUST be stable! */
1518  if (i > 0) {
1519  if (!svf_nil)
1520  jtag_add_pathmove(i, path);
1521  }
1522  if (!svf_nil)
1523  jtag_add_tlr();
1524  num_of_argu -= i + 1;
1525  i = -1;
1526  }
1527  }
1528  if (num_of_argu > 0) {
1529  /* execute last path if necessary */
1530  if (svf_tap_state_is_stable(path[num_of_argu - 1])) {
1531  /* last state MUST be stable state */
1532  if (!svf_nil)
1533  jtag_add_pathmove(num_of_argu, path);
1534  LOG_DEBUG("\tmove to %s by path_move",
1535  tap_state_name(path[num_of_argu - 1]));
1536  } else {
1537  LOG_ERROR("%s: %s is not a stable state",
1538  argus[0],
1539  tap_state_name(path[num_of_argu - 1]));
1540  free(path);
1541  return ERROR_FAIL;
1542  }
1543  }
1544 
1545  free(path);
1546  path = NULL;
1547  } else {
1548  /* STATE stable_state */
1549  state = tap_state_by_name(argus[1]);
1551  LOG_DEBUG("\tmove to %s by svf_add_statemove",
1553  /* FIXME handle statemove failures */
1555  } else {
1556  LOG_ERROR("%s: %s is not a stable state",
1557  argus[0], tap_state_name(state));
1558  return ERROR_FAIL;
1559  }
1560  }
1561  break;
1562  case TRST:
1563  /* TRST trst_mode */
1564  if (num_of_argu != 2) {
1565  LOG_ERROR("invalid parameter of %s", argus[0]);
1566  return ERROR_FAIL;
1567  }
1568  if (svf_para.trst_mode != TRST_ABSENT) {
1569  if (svf_execute_tap() != ERROR_OK)
1570  return ERROR_FAIL;
1571  i_tmp = svf_find_string_in_array(argus[1],
1572  (char **)svf_trst_mode_name,
1574  switch (i_tmp) {
1575  case TRST_ON:
1576  if (!svf_nil)
1577  jtag_add_reset(1, 0);
1578  break;
1579  case TRST_Z:
1580  case TRST_OFF:
1581  if (!svf_nil)
1582  jtag_add_reset(0, 0);
1583  break;
1584  case TRST_ABSENT:
1585  break;
1586  default:
1587  LOG_ERROR("unknown TRST mode: %s", argus[1]);
1588  return ERROR_FAIL;
1589  }
1590  svf_para.trst_mode = i_tmp;
1591  LOG_DEBUG("\ttrst_mode = %s", svf_trst_mode_name[svf_para.trst_mode]);
1592  } else {
1593  LOG_ERROR("can not accept TRST command if trst_mode is ABSENT");
1594  return ERROR_FAIL;
1595  }
1596  break;
1597  default:
1598  LOG_ERROR("invalid svf command: %s", argus[0]);
1599  return ERROR_FAIL;
1600  }
1601 
1602  if (!svf_quiet) {
1603  if (padding_command_skipped)
1604  LOG_USER("(Above Padding command skipped, as per -tap argument)");
1605  }
1606 
1607  if (debug_level >= LOG_LVL_DEBUG) {
1608  /* for convenient debugging, execute tap if possible */
1609  if ((svf_buffer_index > 0) &&
1610  (((command != STATE) && (command != RUNTEST)) ||
1611  ((command == STATE) && (num_of_argu == 2)))) {
1612  if (svf_execute_tap() != ERROR_OK)
1613  return ERROR_FAIL;
1614 
1615  /* output debug info */
1616  if ((command == SIR) || (command == SDR))
1617  SVF_BUF_LOG(DEBUG, svf_tdi_buffer, svf_check_tdo_para[0].bit_len, "TDO read");
1618  }
1619  } else {
1620  /* for fast executing, execute tap if necessary */
1621  /* half of the buffer is for the next command */
1624  (((command != STATE) && (command != RUNTEST)) ||
1625  ((command == STATE) && (num_of_argu == 2))))
1626  return svf_execute_tap();
1627  }
1628 
1629  return ERROR_OK;
1630 }
1631 
1632 static const struct command_registration svf_command_handlers[] = {
1633  {
1634  .name = "svf",
1635  .handler = handle_svf_command,
1636  .mode = COMMAND_EXEC,
1637  .help = "Runs a SVF file.",
1638  .usage = "[-tap device.tap] [-quiet] [-nil] [-progress] [-ignore_error] [-noreset] [-addcycles numcycles] file",
1639  },
1641 };
1642 
1644 {
1645  return register_commands(cmd_ctx, NULL, svf_command_handlers);
1646 }
void * buf_set_buf(const void *_src, unsigned int src_start, void *_dst, unsigned int dst_start, unsigned int len)
Definition: binarybuffer.c:120
void * buf_set_ones(void *_buf, unsigned int size)
Set the contents of buf with count bits, all set to 1.
Definition: binarybuffer.c:105
bool buf_eq_mask(const void *_buf1, const void *_buf2, const void *_mask, unsigned int size)
Definition: binarybuffer.c:87
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:443
int command_run_linef(struct command_context *context, const char *format,...)
Definition: command.c:613
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:141
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:156
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:402
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:151
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:146
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:253
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:404
static int register_commands(struct command_context *cmd_ctx, const char *cmd_prefix, const struct command_registration *cmds)
Register one or more commands in the specified context, as children of parent (or top-level commends,...
Definition: command.h:274
@ COMMAND_EXEC
Definition: command.h:40
const char * tap_state_name(tap_state_t state)
Function tap_state_name Returns a string suitable for display representing the JTAG tap_state.
Definition: interface.c:344
tap_state_t tap_state_by_name(const char *name)
Provides user-friendly name lookup of TAP states.
Definition: interface.c:355
struct jtag_tap * jtag_tap_by_string(const char *s)
Definition: jtag/core.c:237
void jtag_add_runtest(unsigned int num_cycles, tap_state_t state)
Goes to TAP_IDLE (if we're not already there), cycle precisely num_cycles in the TAP_IDLE state,...
Definition: jtag/core.c:592
void jtag_add_pathmove(unsigned int num_states, const tap_state_t *path)
Application code must assume that interfaces will implement transitions between states with different...
Definition: jtag/core.c:517
void jtag_add_plain_dr_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits, tap_state_t state)
Scan out the bits in ir scan mode.
Definition: jtag/core.c:465
void jtag_add_reset(int req_tlr_or_trst, int req_srst)
A reset of the TAP state machine can be requested.
Definition: jtag/core.c:758
void jtag_add_plain_ir_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits, tap_state_t state)
Scan out the bits in ir scan mode.
Definition: jtag/core.c:392
int jtag_execute_queue(void)
For software FIFO implementations, the queued commands can be executed during this call or earlier.
Definition: jtag/core.c:1037
void jtag_add_clocks(unsigned int num_cycles)
Function jtag_add_clocks first checks that the state in which the clocks are to be issued is stable,...
Definition: jtag/core.c:599
struct jtag_tap * jtag_all_taps(void)
Definition: jtag/core.c:184
tap_state_t cmd_queue_cur_state
The current TAP state of the pending JTAG command queue.
Definition: jtag/core.c:88
void jtag_add_tlr(void)
Run a TAP_RESET reset where the end state is TAP_RESET, regardless of the start state.
Definition: jtag/core.c:478
void jtag_add_sleep(uint32_t us)
Definition: jtag/core.c:870
The JTAG interface can be implemented with a software or hardware fifo.
@ TAP_IRCAPTURE
Definition: jtag.h:55
@ TAP_RESET
Definition: jtag.h:56
@ TAP_DRCAPTURE
Definition: jtag.h:47
@ TAP_DRSELECT
Definition: jtag.h:48
@ TAP_DREXIT1
Definition: jtag.h:42
@ TAP_IREXIT1
Definition: jtag.h:50
@ TAP_DRPAUSE
Definition: jtag.h:44
@ TAP_IRSELECT
Definition: jtag.h:45
@ TAP_IRUPDATE
Definition: jtag.h:54
@ TAP_IREXIT2
Definition: jtag.h:49
@ TAP_DREXIT2
Definition: jtag.h:41
@ TAP_IDLE
Definition: jtag.h:53
@ TAP_IRPAUSE
Definition: jtag.h:52
@ TAP_DRUPDATE
Definition: jtag.h:46
@ TAP_INVALID
Definition: jtag.h:38
enum tap_state tap_state_t
Defines JTAG Test Access Port states.
void log_printf_lf(enum log_levels level, const char *file, unsigned int line, const char *function, const char *format,...)
Definition: log.c:183
int debug_level
Definition: log.c:35
#define LOG_USER(expr ...)
Definition: log.h:135
#define ERROR_FAIL
Definition: log.h:170
#define LOG_USER_N(expr ...)
Definition: log.h:138
#define LOG_ERROR(expr ...)
Definition: log.h:132
#define LOG_INFO(expr ...)
Definition: log.h:126
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:164
@ LOG_LVL_DEBUG
Definition: log.h:47
const struct nvp * nvp_name2value(const struct nvp *p, const char *name)
Definition: nvp.c:29
const char * name
Definition: command.h:235
Definition: jtag.h:101
unsigned int ir_length
size of instruction register
Definition: jtag.h:110
unsigned int abs_chain_position
Definition: jtag.h:105
bool enabled
Is this TAP currently enabled?
Definition: jtag.h:109
struct jtag_tap * next_tap
Definition: jtag.h:141
Name Value Pairs, aka: NVP.
Definition: nvp.h:61
int value
Definition: nvp.h:63
const char * name
Definition: nvp.h:62
This structure defines a single scan field in the scan.
Definition: jtag.h:87
uint8_t * in_value
A pointer to a 32-bit memory location for data scanned out.
Definition: jtag.h:93
const uint8_t * out_value
A pointer to value to be scanned into the device.
Definition: jtag.h:91
unsigned int num_bits
The number of bits this field specifies.
Definition: jtag.h:89
int buffer_offset
Definition: svf.c:200
Definition: svf.c:156
struct svf_xxr_para sir_para
Definition: svf.c:168
struct svf_xxr_para tir_para
Definition: svf.c:166
float frequency
Definition: svf.c:157
tap_state_t runtest_run_state
Definition: svf.c:160
struct svf_xxr_para sdr_para
Definition: svf.c:169
struct svf_xxr_para hdr_para
Definition: svf.c:165
tap_state_t dr_end_state
Definition: svf.c:159
struct svf_xxr_para hir_para
Definition: svf.c:164
struct svf_xxr_para tdr_para
Definition: svf.c:167
enum trst_mode trst_mode
Definition: svf.c:162
tap_state_t runtest_end_state
Definition: svf.c:161
tap_state_t ir_end_state
Definition: svf.c:158
uint32_t num_of_moves
Definition: svf.c:80
tap_state_t to
Definition: svf.c:79
tap_state_t paths[8]
Definition: svf.c:81
tap_state_t from
Definition: svf.c:78
uint8_t * mask
Definition: svf.c:152
uint8_t * tdo
Definition: svf.c:151
uint8_t * smask
Definition: svf.c:153
int data_mask
Definition: svf.c:149
int len
Definition: svf.c:148
uint8_t * tdi
Definition: svf.c:150
static size_t svf_read_line_size
Definition: svf.c:216
static int svf_realloc_buffers(size_t len)
Definition: svf.c:272
static int svf_nil
Definition: svf.c:226
bool svf_tap_state_is_stable(tap_state_t state)
svf_tap_state_is_stable() returns true for stable non-SHIFT states
Definition: svf.c:819
static long svf_total_lines
Definition: svf.c:237
#define XXR_SMASK
Definition: svf.c:143
static int svf_check_tdo_para_index
Definition: svf.c:206
static const struct svf_para svf_para_init
Definition: svf.c:173
static int svf_getline(char **lineptr, size_t *n, FILE *stream)
Definition: svf.c:661
static int svf_set_padding(struct svf_xxr_para *para, int len, unsigned char tdi)
Definition: svf.c:851
#define XXR_TDI
Definition: svf.c:140
static int svf_adjust_array_length(uint8_t **arr, int orig_bit_len, int new_bit_len)
Definition: svf.c:836
static size_t svf_command_buffer_size
Definition: svf.c:218
#define SVF_CHECK_TDO_PARA_SIZE
Definition: svf.c:204
static uint8_t * svf_tdi_buffer
Definition: svf.c:223
static int svf_tap_is_specified
Definition: svf.c:232
#define SVF_MAX_BUFFER_SIZE_TO_COMMIT
Definition: svf.c:222
trst_mode
Definition: svf.c:63
@ TRST_ABSENT
Definition: svf.c:67
@ TRST_OFF
Definition: svf.c:65
@ TRST_ON
Definition: svf.c:64
@ TRST_Z
Definition: svf.c:66
COMMAND_HANDLER(handle_svf_command)
Definition: svf.c:381
static uint8_t * svf_tdo_buffer
Definition: svf.c:223
static int svf_copy_hexstring_to_binary(char *str, uint8_t **bin, int orig_bit_len, int bit_len)
Definition: svf.c:864
static int svf_check_tdo(void)
Definition: svf.c:927
#define MIN_CHUNK
#define SVF_MAX_NUM_OF_OPTIONS
int svf_add_statemove(tap_state_t state_to)
svf_add_statemove() moves from the current state to goal_state.
Definition: svf.c:316
static int svf_addcycles
Definition: svf.c:229
static const struct svf_statemove svf_statemoves[]
Definition: svf.c:99
int svf_register_commands(struct command_context *cmd_ctx)
Definition: svf.c:1643
static int svf_percentage
Definition: svf.c:238
#define SVF_MIN_NUM_OF_OPTIONS
static int svf_execute_tap(void)
Definition: svf.c:970
static char * svf_read_line
Definition: svf.c:215
static int svf_buffer_index
Definition: svf.c:224
static int svf_quiet
Definition: svf.c:225
static const char * svf_command_name[14]
Definition: svf.c:46
static FILE * svf_fd
Definition: svf.c:214
static struct svf_check_tdo_para * svf_check_tdo_para
Definition: svf.c:205
svf_command
Definition: svf.c:29
@ FREQUENCY
Definition: svf.c:32
@ ENDIR
Definition: svf.c:31
@ TRST
Definition: svf.c:43
@ SIR
Definition: svf.c:39
@ TDR
Definition: svf.c:41
@ RUNTEST
Definition: svf.c:37
@ HDR
Definition: svf.c:33
@ SDR
Definition: svf.c:38
@ STATE
Definition: svf.c:40
@ HIR
Definition: svf.c:34
@ PIO
Definition: svf.c:35
@ ENDDR
Definition: svf.c:30
@ TIR
Definition: svf.c:42
@ PIOMAP
Definition: svf.c:36
static int svf_run_command(struct command_context *cmd_ctx, char *cmd_str)
Definition: svf.c:982
static int svf_last_printed_percentage
Definition: svf.c:239
static int svf_parse_cmd_string(char *str, int len, char **argus, int *num_of_argu)
Definition: svf.c:781
#define XXR_MASK
Definition: svf.c:142
static const char * svf_trst_mode_name[4]
Definition: svf.c:70
#define SVF_MAX_ADDCYCLES
Definition: svf.c:145
static const struct command_registration svf_command_handlers[]
Definition: svf.c:1632
static const struct nvp svf_cmd_opts[]
Definition: svf.c:365
static int svf_line_number
Definition: svf.c:219
svf_cmd_param
Definition: svf.c:350
@ DEPRECATED_OPT_IGNORE_ERROR
Definition: svf.c:359
@ OPT_TAP
Definition: svf.c:357
@ DEPRECATED_OPT_QUIET
Definition: svf.c:362
@ OPT_QUIET
Definition: svf.c:356
@ OPT_IGNORE_ERROR
Definition: svf.c:352
@ OPT_NIL
Definition: svf.c:353
@ OPT_PROGRESS
Definition: svf.c:355
@ OPT_ADDCYCLES
Definition: svf.c:351
@ DEPRECATED_OPT_PROGRESS
Definition: svf.c:361
@ OPT_NORESET
Definition: svf.c:354
@ DEPRECATED_OPT_NIL
Definition: svf.c:360
static void svf_free_xxd_para(struct svf_xxr_para *para)
Definition: svf.c:299
static bool svf_noreset
Definition: svf.c:228
static int svf_read_command_from_file(FILE *fd)
Definition: svf.c:692
static void svf_hexbuf_print(int dbg_lvl, const char *file, unsigned int line, const char *function, const uint8_t *buf, int bit_len, const char *desc)
Definition: svf.c:248
static int svf_add_check_para(uint8_t enabled, int buffer_offset, int bit_len)
Definition: svf.c:954
static char * svf_command_buffer
Definition: svf.c:217
#define SVF_BUF_LOG(_lvl, _buf, _nbits, _desc)
Definition: svf.c:245
static int svf_find_string_in_array(char *str, char **strs, int num_of_element)
Definition: svf.c:825
static int svf_ignore_error
Definition: svf.c:227
static int svf_progress_enabled
Definition: svf.c:236
static int svf_buffer_size
Definition: svf.c:224
#define XXR_TDO
Definition: svf.c:141
static uint8_t * svf_mask_buffer
Definition: svf.c:223
int64_t timeval_ms(void)
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
Definition: types.h:79
#define NULL
Definition: usb.h:16
uint8_t state[4]
Definition: vdebug.c:21