gdb: cleanup gdb target description support
[openocd.git] / src / svf / svf.c
blob3e7bfbf208eec69f41381badca95dbf4116b7a76
1 /***************************************************************************
2 * Copyright (C) 2009 by Simon Qian *
3 * SimonQian@SimonQian.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, 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".
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
34 #include <jtag/jtag.h>
35 #include "svf.h"
36 #include <helper/time_support.h>
38 /* SVF command */
39 enum svf_command {
40 ENDDR,
41 ENDIR,
42 FREQUENCY,
43 HDR,
44 HIR,
45 PIO,
46 PIOMAP,
47 RUNTEST,
48 SDR,
49 SIR,
50 STATE,
51 TDR,
52 TIR,
53 TRST,
56 static const char *svf_command_name[14] = {
57 "ENDDR",
58 "ENDIR",
59 "FREQUENCY",
60 "HDR",
61 "HIR",
62 "PIO",
63 "PIOMAP",
64 "RUNTEST",
65 "SDR",
66 "SIR",
67 "STATE",
68 "TDR",
69 "TIR",
70 "TRST"
73 enum trst_mode {
74 TRST_ON,
75 TRST_OFF,
76 TRST_Z,
77 TRST_ABSENT
80 static const char *svf_trst_mode_name[4] = {
81 "ON",
82 "OFF",
83 "Z",
84 "ABSENT"
87 struct svf_statemove {
88 tap_state_t from;
89 tap_state_t to;
90 uint32_t num_of_moves;
91 tap_state_t paths[8];
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,
130 TAP_IDLE} },
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,
141 TAP_IDLE} },
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 {
155 int len;
156 int data_mask;
157 uint8_t *tdi;
158 uint8_t *tdo;
159 uint8_t *mask;
160 uint8_t *smask;
163 struct svf_para {
164 float frequency;
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,
183 /* hir_para */
184 /* {len, data_mask, tdi, tdo, mask, smask}, */
185 {0, 0, NULL, NULL, NULL, NULL},
186 /* hdr_para */
187 /* {len, data_mask, tdi, tdo, mask, smask}, */
188 {0, 0, NULL, NULL, NULL, NULL},
189 /* tir_para */
190 /* {len, data_mask, tdi, tdo, mask, smask}, */
191 {0, 0, NULL, NULL, NULL, NULL},
192 /* tdr_para */
193 /* {len, data_mask, tdi, tdo, mask, smask}, */
194 {0, 0, NULL, NULL, NULL, NULL},
195 /* sir_para */
196 /* {len, data_mask, tdi, tdo, mask, smask}, */
197 {0, 0, NULL, NULL, NULL, NULL},
198 /* sdr_para */
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);
220 static FILE *svf_fd;
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;
232 static int svf_nil;
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)
246 if (NULL != para) {
247 if (para->tdi != NULL) {
248 free(para->tdi);
249 para->tdi = NULL;
251 if (para->tdo != NULL) {
252 free(para->tdo);
253 para->tdo = NULL;
255 if (para->mask != NULL) {
256 free(para->mask);
257 para->mask = NULL;
259 if (para->smask != NULL) {
260 free(para->smask);
261 para->smask = NULL;
266 static unsigned svf_get_mask_u32(int bitlen)
268 uint32_t bitmask;
270 if (bitlen < 0)
271 bitmask = 0;
272 else if (bitlen >= 32)
273 bitmask = 0xFFFFFFFF;
274 else
275 bitmask = (1 << bitlen) - 1;
277 return bitmask;
280 int svf_add_statemove(tap_state_t state_to)
282 tap_state_t state_from = cmd_queue_cur_state;
283 unsigned index_var;
285 /* when resetting, be paranoid and ignore current state */
286 if (state_to == TAP_RESET) {
287 if (svf_nil)
288 return ERROR_OK;
290 jtag_add_tlr();
291 return ERROR_OK;
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)) {
297 if (svf_nil)
298 continue;
299 /* recorded path includes current state ... avoid
300 *extra TCKs! */
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);
304 else
305 jtag_add_pathmove(svf_statemoves[index_var].num_of_moves,
306 svf_statemoves[index_var].paths);
307 return ERROR_OK;
310 LOG_ERROR("SVF: can not move to %s", tap_state_name(state_to));
311 return ERROR_FAIL;
314 COMMAND_HANDLER(handle_svf_command)
316 #define SVF_MIN_NUM_OF_OPTIONS 1
317 #define SVF_MAX_NUM_OF_OPTIONS 5
318 int command_num = 0;
319 int ret = ERROR_OK;
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 */
333 svf_quiet = 0;
334 svf_nil = 0;
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]);
338 if (!tap) {
339 command_print(CMD_CTX, "Tap: %s unknown", CMD_ARGV[i+1]);
340 return ERROR_FAIL;
342 i++;
343 } else if ((strcmp(CMD_ARGV[i],
344 "quiet") == 0) || (strcmp(CMD_ARGV[i], "-quiet") == 0))
345 svf_quiet = 1;
346 else if ((strcmp(CMD_ARGV[i], "nil") == 0) || (strcmp(CMD_ARGV[i], "-nil") == 0))
347 svf_nil = 1;
348 else if ((strcmp(CMD_ARGV[i],
349 "progress") == 0) || (strcmp(CMD_ARGV[i], "-progress") == 0))
350 svf_progress_enabled = 1;
351 else {
352 svf_fd = fopen(CMD_ARGV[i], "r");
353 if (svf_fd == NULL) {
354 int err = errno;
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;
358 } else
359 LOG_USER("svf processing file: \"%s\"", CMD_ARGV[i]);
363 if (svf_fd == NULL)
364 return ERROR_COMMAND_SYNTAX_ERROR;
366 /* get time */
367 time_measure_ms = timeval_ms();
369 /* init */
370 svf_line_number = 1;
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");
377 ret = ERROR_FAIL;
378 goto free_all;
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");
389 ret = ERROR_FAIL;
390 goto free_all;
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");
395 ret = ERROR_FAIL;
396 goto free_all;
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");
401 ret = ERROR_FAIL;
402 goto free_all;
404 svf_buffer_size = 2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT;
406 memcpy(&svf_para, &svf_para_init, sizeof(svf_para));
408 if (!svf_nil) {
409 /* TAP_RESET */
410 jtag_add_tlr();
413 if (tap) {
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) {
422 /* Header */
423 header_ir_len += check_tap->ir_length;
424 header_dr_len++;
425 } else if (check_tap->abs_chain_position > tap->abs_chain_position) {
426 /* Trailer */
427 trailer_ir_len += check_tap->ir_length;
428 trailer_dr_len++;
432 /* HDR %d TDI (0) */
433 if (ERROR_OK != svf_set_padding(&svf_para.hdr_para, header_dr_len, 0)) {
434 LOG_ERROR("failed to set data header");
435 return ERROR_FAIL;
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");
441 return ERROR_FAIL;
444 /* TDR %d TDI (0) */
445 if (ERROR_OK != svf_set_padding(&svf_para.tdr_para, trailer_dr_len, 0)) {
446 LOG_ERROR("failed to set data trailer");
447 return ERROR_FAIL;
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");
453 return ERROR_FAIL;
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);
461 svf_total_lines++;
463 rewind(svf_fd);
465 while (ERROR_OK == svf_read_command_from_file(svf_fd)) {
466 /* Log Output */
467 if (svf_quiet) {
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;
475 } else {
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);
479 } else
480 LOG_USER_N("%s", svf_read_line);
482 /* Run Command */
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);
485 ret = ERROR_FAIL;
486 break;
488 command_num++;
491 if ((!svf_nil) && (ERROR_OK != jtag_execute_queue()))
492 ret = ERROR_FAIL;
493 else if (ERROR_OK != svf_check_tdo())
494 ret = ERROR_FAIL;
496 /* print time */
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 ",
505 time_measure_m,
506 time_measure_s,
507 time_measure_ms);
509 free_all:
511 fclose(svf_fd);
512 svf_fd = 0;
514 /* free buffers */
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;
538 svf_buffer_size = 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);
547 if (ERROR_OK == ret)
548 command_print(CMD_CTX,
549 "svf file programmed successfully for %d commands",
550 command_num);
551 else
552 command_print(CMD_CTX, "svf file programmed failed");
554 return ret;
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 */
560 size_t i = 0;
562 if (*lineptr == NULL) {
563 *n = MIN_CHUNK;
564 *lineptr = (char *)malloc(*n);
565 if (!*lineptr)
566 return -1;
569 (*lineptr)[0] = fgetc(stream);
570 while ((*lineptr)[i] != '\n') {
571 (*lineptr)[++i] = fgetc(stream);
572 if (feof(stream)) {
573 (*lineptr)[0] = 0;
574 return -1;
576 if ((i + 2) > *n) {
577 *n += MIN_CHUNK;
578 *lineptr = realloc(*lineptr, *n);
582 (*lineptr)[++i] = 0;
584 return sizeof(*lineptr);
587 #define SVFP_CMD_INC_CNT 1024
588 static int svf_read_command_from_file(FILE *fd)
590 unsigned char ch;
591 int i = 0;
592 size_t cmd_pos = 0;
593 int cmd_ok = 0, slash = 0;
595 if (svf_getline(&svf_read_line, &svf_read_line_size, svf_fd) <= 0)
596 return ERROR_FAIL;
597 svf_line_number++;
598 ch = svf_read_line[0];
599 while (!cmd_ok && (ch != 0)) {
600 switch (ch) {
601 case '!':
602 slash = 0;
603 if (svf_getline(&svf_read_line, &svf_read_line_size, svf_fd) <= 0)
604 return ERROR_FAIL;
605 svf_line_number++;
606 i = -1;
607 break;
608 case '/':
609 if (++slash == 2) {
610 slash = 0;
611 if (svf_getline(&svf_read_line, &svf_read_line_size,
612 svf_fd) <= 0)
613 return ERROR_FAIL;
614 svf_line_number++;
615 i = -1;
617 break;
618 case ';':
619 slash = 0;
620 cmd_ok = 1;
621 break;
622 case '\n':
623 svf_line_number++;
624 if (svf_getline(&svf_read_line, &svf_read_line_size, svf_fd) <= 0)
625 return ERROR_FAIL;
626 i = -1;
627 case '\r':
628 slash = 0;
629 /* Don't save '\r' and '\n' if no data is parsed */
630 if (!cmd_pos)
631 break;
632 default:
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
638 * spaces as needed.
640 * Ensure there are 3 bytes available, for:
641 * - current character
642 * - added space.
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");
650 return ERROR_FAIL;
654 /* insert a space before '(' */
655 if ('(' == ch)
656 svf_command_buffer[cmd_pos++] = ' ';
658 svf_command_buffer[cmd_pos++] = (char)toupper(ch);
660 /* insert a space after ')' */
661 if (')' == ch)
662 svf_command_buffer[cmd_pos++] = ' ';
663 break;
665 ch = svf_read_line[++i];
668 if (cmd_ok) {
669 svf_command_buffer[cmd_pos] = '\0';
670 return ERROR_OK;
671 } else
672 return ERROR_FAIL;
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;
679 while (pos < len) {
680 switch (str[pos]) {
681 case '!':
682 case '/':
683 LOG_ERROR("fail to parse svf command");
684 return ERROR_FAIL;
685 case '(':
686 in_bracket = 1;
687 goto parse_char;
688 case ')':
689 in_bracket = 0;
690 goto parse_char;
691 default:
692 parse_char:
693 if (!in_bracket && isspace((int) str[pos])) {
694 space_found = 1;
695 str[pos] = '\0';
696 } else if (space_found) {
697 argus[num++] = &str[pos];
698 space_found = 0;
700 break;
702 pos++;
705 *num_of_argu = num;
707 return ERROR_OK;
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)
718 int i;
720 for (i = 0; i < num_of_element; i++) {
721 if (!strcmp(str, strs[i]))
722 return i;
724 return 0xFF;
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))) {
732 if (*arr != NULL) {
733 free(*arr);
734 *arr = NULL;
736 *arr = (uint8_t *)malloc(new_byte_len);
737 if (NULL == *arr) {
738 LOG_ERROR("not enough memory");
739 return ERROR_FAIL;
741 memset(*arr, 0, new_byte_len);
743 return ERROR_OK;
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(&para->tdi, para->len, len);
750 memset(para->tdi, tdi, (len + 7) >> 3);
751 error |= svf_adjust_array_length(&para->tdo, para->len, len);
752 error |= svf_adjust_array_length(&para->mask, para->len, len);
753 para->len = len;
754 para->data_mask = XXR_TDI;
756 return error;
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;
762 uint8_t ch = 0;
764 if (ERROR_OK != svf_adjust_array_length(bin, orig_bit_len, bit_len)) {
765 LOG_ERROR("fail to adjust length of array");
766 return ERROR_FAIL;
769 /* fill from LSB (end of str) to MSB (beginning of str) */
770 for (i = 0; i < str_hbyte_len; i++) {
771 ch = 0;
772 while (str_len > 0) {
773 ch = str[--str_len];
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.
781 if (!isspace(ch)) {
782 if ((ch >= '0') && (ch <= '9')) {
783 ch = ch - '0';
784 break;
785 } else if ((ch >= 'A') && (ch <= 'F')) {
786 ch = ch - 'A' + 10;
787 break;
788 } else {
789 LOG_ERROR("invalid hex string");
790 return ERROR_FAIL;
794 ch = 0;
797 /* write bin */
798 if (i % 2) {
799 /* MSB */
800 (*bin)[i / 2] |= ch << 4;
801 } else {
802 /* LSB */
803 (*bin)[i / 2] = 0;
804 (*bin)[i / 2] |= ch;
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])))
811 str_len--;
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");
816 return ERROR_FAIL;
819 return ERROR_OK;
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)) {
832 unsigned bitmask;
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",
842 received & bitmask,
843 expected & bitmask,
844 tapmask & bitmask);
845 return ERROR_FAIL;
848 svf_check_tdo_para_index = 0;
850 return ERROR_OK;
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");
857 return ERROR_FAIL;
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++;
866 return ERROR_OK;
869 static int svf_execute_tap(void)
871 if ((!svf_nil) && (ERROR_OK != jtag_execute_queue()))
872 return ERROR_FAIL;
873 else if (ERROR_OK != svf_check_tdo())
874 return ERROR_FAIL;
876 svf_buffer_index = 0;
878 return ERROR_OK;
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;
886 /* tmp variable */
887 int i_tmp;
889 /* for RUNTEST */
890 int run_count;
891 float min_time;
892 /* for XXR */
893 struct svf_xxr_para *xxr_para_tmp;
894 uint8_t **pbuffer_tmp;
895 struct scan_field field;
896 /* for STATE */
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))
902 return ERROR_FAIL;
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));
910 switch (command) {
911 case ENDDR:
912 case ENDIR:
913 if (num_of_argu != 2) {
914 LOG_ERROR("invalid parameter of %s", argus[0]);
915 return ERROR_FAIL;
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));
925 } else {
926 svf_para.dr_end_state = i_tmp;
927 LOG_DEBUG("\tDR end_state = %s",
928 tap_state_name(i_tmp));
930 } else {
931 LOG_ERROR("%s: %s is not a stable state",
932 argus[0], argus[1]);
933 return ERROR_FAIL;
935 break;
936 case FREQUENCY:
937 if ((num_of_argu != 1) && (num_of_argu != 3)) {
938 LOG_ERROR("invalid parameter of %s", argus[0]);
939 return ERROR_FAIL;
941 if (1 == num_of_argu) {
942 /* TODO: set jtag speed to full speed */
943 svf_para.frequency = 0;
944 } else {
945 if (strcmp(argus[2], "HZ")) {
946 LOG_ERROR("HZ not found in FREQUENCY command");
947 return ERROR_FAIL;
949 if (ERROR_OK != svf_execute_tap())
950 return ERROR_FAIL;
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,
955 "adapter_khz %d",
956 (int)svf_para.frequency / 1000);
957 LOG_DEBUG("\tfrequency = %f", svf_para.frequency);
960 break;
961 case HDR:
962 if (svf_tap_is_specified) {
963 padding_command_skipped = 1;
964 break;
966 xxr_para_tmp = &svf_para.hdr_para;
967 goto XXR_common;
968 case HIR:
969 if (svf_tap_is_specified) {
970 padding_command_skipped = 1;
971 break;
973 xxr_para_tmp = &svf_para.hir_para;
974 goto XXR_common;
975 case TDR:
976 if (svf_tap_is_specified) {
977 padding_command_skipped = 1;
978 break;
980 xxr_para_tmp = &svf_para.tdr_para;
981 goto XXR_common;
982 case TIR:
983 if (svf_tap_is_specified) {
984 padding_command_skipped = 1;
985 break;
987 xxr_para_tmp = &svf_para.tir_para;
988 goto XXR_common;
989 case SDR:
990 xxr_para_tmp = &svf_para.sdr_para;
991 goto XXR_common;
992 case SIR:
993 xxr_para_tmp = &svf_para.sir_para;
994 goto XXR_common;
995 XXR_common:
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]);
999 return ERROR_FAIL;
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");
1009 return ERROR_FAIL;
1011 argus[i + 1][strlen(argus[i + 1]) - 1] = '\0';
1012 /* TDI, TDO, MASK, SMASK */
1013 if (!strcmp(argus[i], "TDI")) {
1014 /* TDI */
1015 pbuffer_tmp = &xxr_para_tmp->tdi;
1016 xxr_para_tmp->data_mask |= XXR_TDI;
1017 } else if (!strcmp(argus[i], "TDO")) {
1018 /* TDO */
1019 pbuffer_tmp = &xxr_para_tmp->tdo;
1020 xxr_para_tmp->data_mask |= XXR_TDO;
1021 } else if (!strcmp(argus[i], "MASK")) {
1022 /* MASK */
1023 pbuffer_tmp = &xxr_para_tmp->mask;
1024 xxr_para_tmp->data_mask |= XXR_MASK;
1025 } else if (!strcmp(argus[i], "SMASK")) {
1026 /* SMASK */
1027 pbuffer_tmp = &xxr_para_tmp->smask;
1028 xxr_para_tmp->data_mask |= XXR_SMASK;
1029 } else {
1030 LOG_ERROR("unknow parameter: %s", argus[i]);
1031 return ERROR_FAIL;
1033 if (ERROR_OK !=
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");
1037 return ERROR_FAIL;
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 */
1047 if (ERROR_OK !=
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");
1051 return ERROR_FAIL;
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) {
1058 if (ERROR_OK !=
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");
1062 return ERROR_FAIL;
1065 if (NULL == xxr_para_tmp->mask) {
1066 if (ERROR_OK !=
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");
1070 return ERROR_FAIL;
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)) {
1081 #if 1
1082 /* simply print error message */
1083 LOG_ERROR("buffer is not enough, report to author");
1084 return ERROR_FAIL;
1085 #else
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");
1092 return ERROR_FAIL;
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");
1102 return ERROR_FAIL;
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");
1112 return ERROR_FAIL;
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;
1119 buffer_tmp = NULL;
1120 svf_buffer_size = svf_buffer_index + ((i + 7) >> 3);
1121 #endif
1124 /* assemble dr data */
1125 i = 0;
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 */
1148 i = 0;
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 */
1168 i = 0;
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);
1189 } else
1190 svf_add_check_para(0, svf_buffer_index, i);
1191 field.num_bits = i;
1192 field.out_value = &svf_tdi_buffer[svf_buffer_index];
1193 field.in_value = &svf_tdi_buffer[svf_buffer_index];
1194 if (!svf_nil) {
1195 /* NOTE: doesn't use SVF-specified state paths */
1196 jtag_add_plain_dr_scan(field.num_bits,
1197 field.out_value,
1198 field.in_value,
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)) {
1208 #if 1
1209 /* simply print error message */
1210 LOG_ERROR("buffer is not enough, report to author");
1211 return ERROR_FAIL;
1212 #else
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");
1219 return ERROR_FAIL;
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");
1229 return ERROR_FAIL;
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");
1239 return ERROR_FAIL;
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;
1246 buffer_tmp = NULL;
1247 svf_buffer_size = svf_buffer_index + ((i + 7) >> 3);
1248 #endif
1251 /* assemble ir data */
1252 i = 0;
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 */
1275 i = 0;
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 */
1295 i = 0;
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);
1316 } else
1317 svf_add_check_para(0, svf_buffer_index, i);
1318 field.num_bits = i;
1319 field.out_value = &svf_tdi_buffer[svf_buffer_index];
1320 field.in_value = &svf_tdi_buffer[svf_buffer_index];
1321 if (!svf_nil) {
1322 /* NOTE: doesn't use SVF-specified state paths */
1323 jtag_add_plain_ir_scan(field.num_bits,
1324 field.out_value,
1325 field.in_value,
1326 svf_para.ir_end_state);
1329 svf_buffer_index += (i + 7) >> 3;
1331 break;
1332 case PIO:
1333 case PIOMAP:
1334 LOG_ERROR("PIO and PIOMAP are not supported");
1335 return ERROR_FAIL;
1336 break;
1337 case RUNTEST:
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
1341 * end_state] */
1342 if ((num_of_argu < 3) && (num_of_argu > 11)) {
1343 LOG_ERROR("invalid parameter of %s", argus[0]);
1344 return ERROR_FAIL;
1346 /* init */
1347 run_count = 0;
1348 min_time = 0;
1349 i = 1;
1351 /* run_state */
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));
1362 i++;
1363 } else {
1364 LOG_ERROR("%s: %s is not a stable state", argus[0], tap_state_name(i_tmp));
1365 return ERROR_FAIL;
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);
1375 } else {
1376 LOG_ERROR("%s not supported for clock", argus[i + 1]);
1377 return ERROR_FAIL;
1379 i += 2;
1381 /* min_time SEC */
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);
1385 i += 2;
1387 /* MAXIMUM max_time SEC */
1388 if (((i + 3) <= num_of_argu) &&
1389 !strcmp(argus[i], "MAXIMUM") && !strcmp(argus[i + 2], "SEC")) {
1390 float max_time = 0;
1391 max_time = atof(argus[i + 1]);
1392 LOG_DEBUG("\tmax_time = %fs", max_time);
1393 i += 3;
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));
1402 } else {
1403 LOG_ERROR("%s: %s is not a stable state", argus[0], tap_state_name(i_tmp));
1404 return ERROR_FAIL;
1406 i += 2;
1409 /* all parameter should be parsed */
1410 if (i == num_of_argu) {
1411 #if 1
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) {
1421 if (!svf_nil)
1422 jtag_add_clocks(run_count);
1425 if (min_usec > 0) {
1426 if (!svf_nil)
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);
1434 #else
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));
1438 return ERROR_FAIL;
1441 if (!svf_nil)
1442 jtag_add_runtest(run_count, svf_para.runtest_end_state);
1443 #endif
1444 } else {
1445 LOG_ERROR("fail to parse parameter of RUNTEST, %d out of %d is parsed",
1447 num_of_argu);
1448 return ERROR_FAIL;
1450 break;
1451 case STATE:
1452 /* STATE [pathstate1 [pathstate2 ...[pathstaten]]] stable_state */
1453 if (num_of_argu < 2) {
1454 LOG_ERROR("invalid parameter of %s", argus[0]);
1455 return ERROR_FAIL;
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));
1460 if (NULL == path) {
1461 LOG_ERROR("not enough memory");
1462 return ERROR_FAIL;
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]);
1470 free(path);
1471 return ERROR_FAIL;
1473 /* OpenOCD refuses paths containing TAP_RESET */
1474 if (TAP_RESET == path[i]) {
1475 /* FIXME last state MUST be stable! */
1476 if (i > 0) {
1477 if (!svf_nil)
1478 jtag_add_pathmove(i, path);
1480 if (!svf_nil)
1481 jtag_add_tlr();
1482 num_of_argu -= i + 1;
1483 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 */
1490 if (!svf_nil)
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]));
1494 } else {
1495 LOG_ERROR("%s: %s is not a stable state",
1496 argus[0],
1497 tap_state_name(path[num_of_argu - 1]));
1498 free(path);
1499 return ERROR_FAIL;
1503 free(path);
1504 path = NULL;
1505 } else {
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);
1513 } else {
1514 LOG_ERROR("%s: %s is not a stable state",
1515 argus[0], tap_state_name(state));
1516 return ERROR_FAIL;
1519 break;
1520 case TRST:
1521 /* TRST trst_mode */
1522 if (num_of_argu != 2) {
1523 LOG_ERROR("invalid parameter of %s", argus[0]);
1524 return ERROR_FAIL;
1526 if (svf_para.trst_mode != TRST_ABSENT) {
1527 if (ERROR_OK != svf_execute_tap())
1528 return ERROR_FAIL;
1529 i_tmp = svf_find_string_in_array(argus[1],
1530 (char **)svf_trst_mode_name,
1531 ARRAY_SIZE(svf_trst_mode_name));
1532 switch (i_tmp) {
1533 case TRST_ON:
1534 if (!svf_nil)
1535 jtag_add_reset(1, 0);
1536 break;
1537 case TRST_Z:
1538 case TRST_OFF:
1539 if (!svf_nil)
1540 jtag_add_reset(0, 0);
1541 break;
1542 case TRST_ABSENT:
1543 break;
1544 default:
1545 LOG_ERROR("unknown TRST mode: %s", argus[1]);
1546 return ERROR_FAIL;
1548 svf_para.trst_mode = i_tmp;
1549 LOG_DEBUG("\ttrst_mode = %s", svf_trst_mode_name[svf_para.trst_mode]);
1550 } else {
1551 LOG_ERROR("can not accpet TRST command if trst_mode is ABSENT");
1552 return ERROR_FAIL;
1554 break;
1555 default:
1556 LOG_ERROR("invalid svf command: %s", argus[0]);
1557 return ERROR_FAIL;
1558 break;
1561 if (!svf_quiet) {
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())
1572 return ERROR_FAIL;
1574 /* output debug info */
1575 if ((SIR == command) || (SDR == command)) {
1576 int read_value;
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);
1583 } else {
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();
1593 return ERROR_OK;
1596 static const struct command_registration svf_command_handlers[] = {
1598 .name = "svf",
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);