ac7c2c68d01256cdedee8a412018bcd2bfc20795
[openocd.git] / src / svf / svf.c
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 ***************************************************************************/
20
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".
24 *
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".
28 */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include <jtag/jtag.h>
35 #include "svf.h"
36 #include <helper/time_support.h>
37
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,
54 };
55
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"
71 };
72
73 enum trst_mode {
74 TRST_ON,
75 TRST_OFF,
76 TRST_Z,
77 TRST_ABSENT
78 };
79
80 static const char *svf_trst_mode_name[4] = {
81 "ON",
82 "OFF",
83 "Z",
84 "ABSENT"
85 };
86
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];
92 };
93
94 /*
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.
99 *
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.
104 *
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.
108 */
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} },
118
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} },
126
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} },
137
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} }
148 };
149
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;
161 };
162
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;
170
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;
177 };
178
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},
201 };
202
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 */
209 };
210
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;
214
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);
219
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);
227
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;
233
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);
237
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;
243
244 /*
245 * macro is used to print the svf hex buffer at desired debug level
246 * DEBUG, INFO, ERROR, USER
247 */
248 #define SVF_BUF_LOG(_lvl, _buf, _nbits, _desc) \
249 svf_hexbuf_print(LOG_LVL_##_lvl , __FILE__, __LINE__, __func__, _buf, _nbits, _desc)
250
251 static void svf_hexbuf_print(int dbg_lvl, const char *file, unsigned line,
252 const char *function, const uint8_t *buf,
253 int bit_len, const char *desc)
254 {
255 int j, len = 0;
256 int byte_len = DIV_ROUND_UP(bit_len, 8);
257 int msbits = bit_len % 8;
258
259 /* allocate 2 bytes per hex digit */
260 char *prbuf = malloc((byte_len * 2) + 1);
261 if (!prbuf)
262 return;
263
264 /* print correct number of bytes, mask excess bits where applicable */
265 uint8_t msb = buf[byte_len - 1] & (msbits ? (1 << msbits) - 1 : 0xff);
266 len = sprintf(prbuf, msbits <= 4 ? "0x%01"PRIx8 : "0x%02"PRIx8, msb);
267 for (j = byte_len - 2; j >= 0; j--)
268 len += sprintf(prbuf + len, "%02"PRIx8, buf[j]);
269
270 log_printf_lf(dbg_lvl, file, line, function, "%8s = %s", desc ? desc : " ", prbuf);
271
272 free(prbuf);
273 }
274
275 static int svf_realloc_buffers(size_t len)
276 {
277 void *ptr;
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 (NULL != para) {
302 if (para->tdi != NULL) {
303 free(para->tdi);
304 para->tdi = NULL;
305 }
306 if (para->tdo != NULL) {
307 free(para->tdo);
308 para->tdo = NULL;
309 }
310 if (para->mask != NULL) {
311 free(para->mask);
312 para->mask = NULL;
313 }
314 if (para->smask != NULL) {
315 free(para->smask);
316 para->smask = NULL;
317 }
318 }
319 }
320
321 int svf_add_statemove(tap_state_t state_to)
322 {
323 tap_state_t state_from = cmd_queue_cur_state;
324 unsigned index_var;
325
326 /* when resetting, be paranoid and ignore current state */
327 if (state_to == TAP_RESET) {
328 if (svf_nil)
329 return ERROR_OK;
330
331 jtag_add_tlr();
332 return ERROR_OK;
333 }
334
335 for (index_var = 0; index_var < ARRAY_SIZE(svf_statemoves); index_var++) {
336 if ((svf_statemoves[index_var].from == state_from)
337 && (svf_statemoves[index_var].to == state_to)) {
338 if (svf_nil)
339 continue;
340 /* recorded path includes current state ... avoid
341 *extra TCKs! */
342 if (svf_statemoves[index_var].num_of_moves > 1)
343 jtag_add_pathmove(svf_statemoves[index_var].num_of_moves - 1,
344 svf_statemoves[index_var].paths + 1);
345 else
346 jtag_add_pathmove(svf_statemoves[index_var].num_of_moves,
347 svf_statemoves[index_var].paths);
348 return ERROR_OK;
349 }
350 }
351 LOG_ERROR("SVF: can not move to %s", tap_state_name(state_to));
352 return ERROR_FAIL;
353 }
354
355 COMMAND_HANDLER(handle_svf_command)
356 {
357 #define SVF_MIN_NUM_OF_OPTIONS 1
358 #define SVF_MAX_NUM_OF_OPTIONS 5
359 int command_num = 0;
360 int ret = ERROR_OK;
361 long long time_measure_ms;
362 int time_measure_s, time_measure_m;
363
364 /* use NULL to indicate a "plain" svf file which accounts for
365 * any additional devices in the scan chain, otherwise the device
366 * that should be affected
367 */
368 struct jtag_tap *tap = NULL;
369
370 if ((CMD_ARGC < SVF_MIN_NUM_OF_OPTIONS) || (CMD_ARGC > SVF_MAX_NUM_OF_OPTIONS))
371 return ERROR_COMMAND_SYNTAX_ERROR;
372
373 /* parse command line */
374 svf_quiet = 0;
375 svf_nil = 0;
376 for (unsigned int i = 0; i < CMD_ARGC; i++) {
377 if (strcmp(CMD_ARGV[i], "-tap") == 0) {
378 tap = jtag_tap_by_string(CMD_ARGV[i+1]);
379 if (!tap) {
380 command_print(CMD_CTX, "Tap: %s unknown", CMD_ARGV[i+1]);
381 return ERROR_FAIL;
382 }
383 i++;
384 } else if ((strcmp(CMD_ARGV[i],
385 "quiet") == 0) || (strcmp(CMD_ARGV[i], "-quiet") == 0))
386 svf_quiet = 1;
387 else if ((strcmp(CMD_ARGV[i], "nil") == 0) || (strcmp(CMD_ARGV[i], "-nil") == 0))
388 svf_nil = 1;
389 else if ((strcmp(CMD_ARGV[i],
390 "progress") == 0) || (strcmp(CMD_ARGV[i], "-progress") == 0))
391 svf_progress_enabled = 1;
392 else {
393 svf_fd = fopen(CMD_ARGV[i], "r");
394 if (svf_fd == NULL) {
395 int err = errno;
396 command_print(CMD_CTX, "open(\"%s\"): %s", CMD_ARGV[i], strerror(err));
397 /* no need to free anything now */
398 return ERROR_COMMAND_SYNTAX_ERROR;
399 } else
400 LOG_USER("svf processing file: \"%s\"", CMD_ARGV[i]);
401 }
402 }
403
404 if (svf_fd == NULL)
405 return ERROR_COMMAND_SYNTAX_ERROR;
406
407 /* get time */
408 time_measure_ms = timeval_ms();
409
410 /* init */
411 svf_line_number = 1;
412 svf_command_buffer_size = 0;
413
414 svf_check_tdo_para_index = 0;
415 svf_check_tdo_para = malloc(sizeof(struct svf_check_tdo_para) * SVF_CHECK_TDO_PARA_SIZE);
416 if (NULL == svf_check_tdo_para) {
417 LOG_ERROR("not enough memory");
418 ret = ERROR_FAIL;
419 goto free_all;
420 }
421
422 svf_buffer_index = 0;
423 /* double the buffer size */
424 /* in case current command cannot be committed, and next command is a bit scan command */
425 /* here is 32K bits for this big scan command, it should be enough */
426 /* buffer will be reallocated if buffer size is not enough */
427 if (svf_realloc_buffers(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT) != ERROR_OK) {
428 ret = ERROR_FAIL;
429 goto free_all;
430 }
431
432 memcpy(&svf_para, &svf_para_init, sizeof(svf_para));
433
434 if (!svf_nil) {
435 /* TAP_RESET */
436 jtag_add_tlr();
437 }
438
439 if (tap) {
440 /* Tap is specified, set header/trailer paddings */
441 int header_ir_len = 0, header_dr_len = 0, trailer_ir_len = 0, trailer_dr_len = 0;
442 struct jtag_tap *check_tap;
443
444 svf_tap_is_specified = 1;
445
446 for (check_tap = jtag_all_taps(); check_tap; check_tap = check_tap->next_tap) {
447 if (check_tap->abs_chain_position < tap->abs_chain_position) {
448 /* Header */
449 header_ir_len += check_tap->ir_length;
450 header_dr_len++;
451 } else if (check_tap->abs_chain_position > tap->abs_chain_position) {
452 /* Trailer */
453 trailer_ir_len += check_tap->ir_length;
454 trailer_dr_len++;
455 }
456 }
457
458 /* HDR %d TDI (0) */
459 if (ERROR_OK != svf_set_padding(&svf_para.hdr_para, header_dr_len, 0)) {
460 LOG_ERROR("failed to set data header");
461 return ERROR_FAIL;
462 }
463
464 /* HIR %d TDI (0xFF) */
465 if (ERROR_OK != svf_set_padding(&svf_para.hir_para, header_ir_len, 0xFF)) {
466 LOG_ERROR("failed to set instruction header");
467 return ERROR_FAIL;
468 }
469
470 /* TDR %d TDI (0) */
471 if (ERROR_OK != svf_set_padding(&svf_para.tdr_para, trailer_dr_len, 0)) {
472 LOG_ERROR("failed to set data trailer");
473 return ERROR_FAIL;
474 }
475
476 /* TIR %d TDI (0xFF) */
477 if (ERROR_OK != svf_set_padding(&svf_para.tir_para, trailer_ir_len, 0xFF)) {
478 LOG_ERROR("failed to set instruction trailer");
479 return ERROR_FAIL;
480 }
481 }
482
483 if (svf_progress_enabled) {
484 /* Count total lines in file. */
485 while (!feof(svf_fd)) {
486 svf_getline(&svf_command_buffer, &svf_command_buffer_size, svf_fd);
487 svf_total_lines++;
488 }
489 rewind(svf_fd);
490 }
491 while (ERROR_OK == svf_read_command_from_file(svf_fd)) {
492 /* Log Output */
493 if (svf_quiet) {
494 if (svf_progress_enabled) {
495 svf_percentage = ((svf_line_number * 20) / svf_total_lines) * 5;
496 if (svf_last_printed_percentage != svf_percentage) {
497 LOG_USER_N("\r%d%% ", svf_percentage);
498 svf_last_printed_percentage = svf_percentage;
499 }
500 }
501 } else {
502 if (svf_progress_enabled) {
503 svf_percentage = ((svf_line_number * 20) / svf_total_lines) * 5;
504 LOG_USER_N("%3d%% %s", svf_percentage, svf_read_line);
505 } else
506 LOG_USER_N("%s", svf_read_line);
507 }
508 /* Run Command */
509 if (ERROR_OK != svf_run_command(CMD_CTX, svf_command_buffer)) {
510 LOG_ERROR("fail to run command at line %d", svf_line_number);
511 ret = ERROR_FAIL;
512 break;
513 }
514 command_num++;
515 }
516
517 if ((!svf_nil) && (ERROR_OK != jtag_execute_queue()))
518 ret = ERROR_FAIL;
519 else if (ERROR_OK != svf_check_tdo())
520 ret = ERROR_FAIL;
521
522 /* print time */
523 time_measure_ms = timeval_ms() - time_measure_ms;
524 time_measure_s = time_measure_ms / 1000;
525 time_measure_ms %= 1000;
526 time_measure_m = time_measure_s / 60;
527 time_measure_s %= 60;
528 if (time_measure_ms < 1000)
529 command_print(CMD_CTX,
530 "\r\nTime used: %dm%ds%lldms ",
531 time_measure_m,
532 time_measure_s,
533 time_measure_ms);
534
535 free_all:
536
537 fclose(svf_fd);
538 svf_fd = 0;
539
540 /* free buffers */
541 if (svf_command_buffer) {
542 free(svf_command_buffer);
543 svf_command_buffer = NULL;
544 svf_command_buffer_size = 0;
545 }
546 if (svf_check_tdo_para) {
547 free(svf_check_tdo_para);
548 svf_check_tdo_para = NULL;
549 svf_check_tdo_para_index = 0;
550 }
551 if (svf_tdi_buffer) {
552 free(svf_tdi_buffer);
553 svf_tdi_buffer = NULL;
554 }
555 if (svf_tdo_buffer) {
556 free(svf_tdo_buffer);
557 svf_tdo_buffer = NULL;
558 }
559 if (svf_mask_buffer) {
560 free(svf_mask_buffer);
561 svf_mask_buffer = NULL;
562 }
563 svf_buffer_index = 0;
564 svf_buffer_size = 0;
565
566 svf_free_xxd_para(&svf_para.hdr_para);
567 svf_free_xxd_para(&svf_para.hir_para);
568 svf_free_xxd_para(&svf_para.tdr_para);
569 svf_free_xxd_para(&svf_para.tir_para);
570 svf_free_xxd_para(&svf_para.sdr_para);
571 svf_free_xxd_para(&svf_para.sir_para);
572
573 if (ERROR_OK == ret)
574 command_print(CMD_CTX,
575 "svf file programmed successfully for %d commands",
576 command_num);
577 else
578 command_print(CMD_CTX, "svf file programmed failed");
579
580 return ret;
581 }
582
583 static int svf_getline(char **lineptr, size_t *n, FILE *stream)
584 {
585 #define MIN_CHUNK 16 /* Buffer is increased by this size each time as required */
586 size_t i = 0;
587
588 if (*lineptr == NULL) {
589 *n = MIN_CHUNK;
590 *lineptr = malloc(*n);
591 if (!*lineptr)
592 return -1;
593 }
594
595 (*lineptr)[0] = fgetc(stream);
596 while ((*lineptr)[i] != '\n') {
597 (*lineptr)[++i] = fgetc(stream);
598 if (feof(stream)) {
599 (*lineptr)[0] = 0;
600 return -1;
601 }
602 if ((i + 2) > *n) {
603 *n += MIN_CHUNK;
604 *lineptr = realloc(*lineptr, *n);
605 }
606 }
607
608 (*lineptr)[++i] = 0;
609
610 return sizeof(*lineptr);
611 }
612
613 #define SVFP_CMD_INC_CNT 1024
614 static int svf_read_command_from_file(FILE *fd)
615 {
616 unsigned char ch;
617 int i = 0;
618 size_t cmd_pos = 0;
619 int cmd_ok = 0, slash = 0;
620
621 if (svf_getline(&svf_read_line, &svf_read_line_size, svf_fd) <= 0)
622 return ERROR_FAIL;
623 svf_line_number++;
624 ch = svf_read_line[0];
625 while (!cmd_ok && (ch != 0)) {
626 switch (ch) {
627 case '!':
628 slash = 0;
629 if (svf_getline(&svf_read_line, &svf_read_line_size, svf_fd) <= 0)
630 return ERROR_FAIL;
631 svf_line_number++;
632 i = -1;
633 break;
634 case '/':
635 if (++slash == 2) {
636 slash = 0;
637 if (svf_getline(&svf_read_line, &svf_read_line_size,
638 svf_fd) <= 0)
639 return ERROR_FAIL;
640 svf_line_number++;
641 i = -1;
642 }
643 break;
644 case ';':
645 slash = 0;
646 cmd_ok = 1;
647 break;
648 case '\n':
649 svf_line_number++;
650 if (svf_getline(&svf_read_line, &svf_read_line_size, svf_fd) <= 0)
651 return ERROR_FAIL;
652 i = -1;
653 case '\r':
654 slash = 0;
655 /* Don't save '\r' and '\n' if no data is parsed */
656 if (!cmd_pos)
657 break;
658 default:
659 /* The parsing code currently expects a space
660 * before parentheses -- "TDI (123)". Also a
661 * space afterwards -- "TDI (123) TDO(456)".
662 * But such spaces are optional... instead of
663 * parser updates, cope with that by adding the
664 * spaces as needed.
665 *
666 * Ensure there are 3 bytes available, for:
667 * - current character
668 * - added space.
669 * - terminating NUL ('\0')
670 */
671 if (cmd_pos + 3 > svf_command_buffer_size) {
672 svf_command_buffer = realloc(svf_command_buffer, cmd_pos + 3);
673 svf_command_buffer_size = cmd_pos + 3;
674 if (svf_command_buffer == NULL) {
675 LOG_ERROR("not enough memory");
676 return ERROR_FAIL;
677 }
678 }
679
680 /* insert a space before '(' */
681 if ('(' == ch)
682 svf_command_buffer[cmd_pos++] = ' ';
683
684 svf_command_buffer[cmd_pos++] = (char)toupper(ch);
685
686 /* insert a space after ')' */
687 if (')' == ch)
688 svf_command_buffer[cmd_pos++] = ' ';
689 break;
690 }
691 ch = svf_read_line[++i];
692 }
693
694 if (cmd_ok) {
695 svf_command_buffer[cmd_pos] = '\0';
696 return ERROR_OK;
697 } else
698 return ERROR_FAIL;
699 }
700
701 static int svf_parse_cmd_string(char *str, int len, char **argus, int *num_of_argu)
702 {
703 int pos = 0, num = 0, space_found = 1, in_bracket = 0;
704
705 while (pos < len) {
706 switch (str[pos]) {
707 case '!':
708 case '/':
709 LOG_ERROR("fail to parse svf command");
710 return ERROR_FAIL;
711 case '(':
712 in_bracket = 1;
713 goto parse_char;
714 case ')':
715 in_bracket = 0;
716 goto parse_char;
717 default:
718 parse_char:
719 if (!in_bracket && isspace((int) str[pos])) {
720 space_found = 1;
721 str[pos] = '\0';
722 } else if (space_found) {
723 argus[num++] = &str[pos];
724 space_found = 0;
725 }
726 break;
727 }
728 pos++;
729 }
730
731 *num_of_argu = num;
732
733 return ERROR_OK;
734 }
735
736 bool svf_tap_state_is_stable(tap_state_t state)
737 {
738 return (TAP_RESET == state) || (TAP_IDLE == state)
739 || (TAP_DRPAUSE == state) || (TAP_IRPAUSE == state);
740 }
741
742 static int svf_find_string_in_array(char *str, char **strs, int num_of_element)
743 {
744 int i;
745
746 for (i = 0; i < num_of_element; i++) {
747 if (!strcmp(str, strs[i]))
748 return i;
749 }
750 return 0xFF;
751 }
752
753 static int svf_adjust_array_length(uint8_t **arr, int orig_bit_len, int new_bit_len)
754 {
755 int new_byte_len = (new_bit_len + 7) >> 3;
756
757 if ((NULL == *arr) || (((orig_bit_len + 7) >> 3) < ((new_bit_len + 7) >> 3))) {
758 if (*arr != NULL) {
759 free(*arr);
760 *arr = NULL;
761 }
762 *arr = malloc(new_byte_len);
763 if (NULL == *arr) {
764 LOG_ERROR("not enough memory");
765 return ERROR_FAIL;
766 }
767 memset(*arr, 0, new_byte_len);
768 }
769 return ERROR_OK;
770 }
771
772 static int svf_set_padding(struct svf_xxr_para *para, int len, unsigned char tdi)
773 {
774 int error = ERROR_OK;
775 error |= svf_adjust_array_length(&para->tdi, para->len, len);
776 memset(para->tdi, tdi, (len + 7) >> 3);
777 error |= svf_adjust_array_length(&para->tdo, para->len, len);
778 error |= svf_adjust_array_length(&para->mask, para->len, len);
779 para->len = len;
780 para->data_mask = XXR_TDI;
781
782 return error;
783 }
784
785 static int svf_copy_hexstring_to_binary(char *str, uint8_t **bin, int orig_bit_len, int bit_len)
786 {
787 int i, str_len = strlen(str), str_hbyte_len = (bit_len + 3) >> 2;
788 uint8_t ch = 0;
789
790 if (ERROR_OK != svf_adjust_array_length(bin, orig_bit_len, bit_len)) {
791 LOG_ERROR("fail to adjust length of array");
792 return ERROR_FAIL;
793 }
794
795 /* fill from LSB (end of str) to MSB (beginning of str) */
796 for (i = 0; i < str_hbyte_len; i++) {
797 ch = 0;
798 while (str_len > 0) {
799 ch = str[--str_len];
800
801 /* Skip whitespace. The SVF specification (rev E) is
802 * deficient in terms of basic lexical issues like
803 * where whitespace is allowed. Long bitstrings may
804 * require line ends for correctness, since there is
805 * a hard limit on line length.
806 */
807 if (!isspace(ch)) {
808 if ((ch >= '0') && (ch <= '9')) {
809 ch = ch - '0';
810 break;
811 } else if ((ch >= 'A') && (ch <= 'F')) {
812 ch = ch - 'A' + 10;
813 break;
814 } else {
815 LOG_ERROR("invalid hex string");
816 return ERROR_FAIL;
817 }
818 }
819
820 ch = 0;
821 }
822
823 /* write bin */
824 if (i % 2) {
825 /* MSB */
826 (*bin)[i / 2] |= ch << 4;
827 } else {
828 /* LSB */
829 (*bin)[i / 2] = 0;
830 (*bin)[i / 2] |= ch;
831 }
832 }
833
834 /* consume optional leading '0' MSBs or whitespace */
835 while (str_len > 0 && ((str[str_len - 1] == '0')
836 || isspace((int) str[str_len - 1])))
837 str_len--;
838
839 /* check validity: we must have consumed everything */
840 if (str_len > 0 || (ch & ~((2 << ((bit_len - 1) % 4)) - 1)) != 0) {
841 LOG_ERROR("value execeeds length");
842 return ERROR_FAIL;
843 }
844
845 return ERROR_OK;
846 }
847
848 static int svf_check_tdo(void)
849 {
850 int i, len, index_var;
851
852 for (i = 0; i < svf_check_tdo_para_index; i++) {
853 index_var = svf_check_tdo_para[i].buffer_offset;
854 len = svf_check_tdo_para[i].bit_len;
855 if ((svf_check_tdo_para[i].enabled)
856 && buf_cmp_mask(&svf_tdi_buffer[index_var], &svf_tdo_buffer[index_var],
857 &svf_mask_buffer[index_var], len)) {
858 LOG_ERROR("tdo check error at line %d",
859 svf_check_tdo_para[i].line_num);
860 SVF_BUF_LOG(ERROR, &svf_tdi_buffer[index_var], len, "READ");
861 SVF_BUF_LOG(ERROR, &svf_tdo_buffer[index_var], len, "WANT");
862 SVF_BUF_LOG(ERROR, &svf_mask_buffer[index_var], len, "MASK");
863 return ERROR_FAIL;
864 }
865 }
866 svf_check_tdo_para_index = 0;
867
868 return ERROR_OK;
869 }
870
871 static int svf_add_check_para(uint8_t enabled, int buffer_offset, int bit_len)
872 {
873 if (svf_check_tdo_para_index >= SVF_CHECK_TDO_PARA_SIZE) {
874 LOG_ERROR("toooooo many operation undone");
875 return ERROR_FAIL;
876 }
877
878 svf_check_tdo_para[svf_check_tdo_para_index].line_num = svf_line_number;
879 svf_check_tdo_para[svf_check_tdo_para_index].bit_len = bit_len;
880 svf_check_tdo_para[svf_check_tdo_para_index].enabled = enabled;
881 svf_check_tdo_para[svf_check_tdo_para_index].buffer_offset = buffer_offset;
882 svf_check_tdo_para_index++;
883
884 return ERROR_OK;
885 }
886
887 static int svf_execute_tap(void)
888 {
889 if ((!svf_nil) && (ERROR_OK != jtag_execute_queue()))
890 return ERROR_FAIL;
891 else if (ERROR_OK != svf_check_tdo())
892 return ERROR_FAIL;
893
894 svf_buffer_index = 0;
895
896 return ERROR_OK;
897 }
898
899 static int svf_run_command(struct command_context *cmd_ctx, char *cmd_str)
900 {
901 char *argus[256], command;
902 int num_of_argu = 0, i;
903
904 /* tmp variable */
905 int i_tmp;
906
907 /* for RUNTEST */
908 int run_count;
909 float min_time;
910 /* for XXR */
911 struct svf_xxr_para *xxr_para_tmp;
912 uint8_t **pbuffer_tmp;
913 struct scan_field field;
914 /* for STATE */
915 tap_state_t *path = NULL, state;
916 /* flag padding commands skipped due to -tap command */
917 int padding_command_skipped = 0;
918
919 if (ERROR_OK != svf_parse_cmd_string(cmd_str, strlen(cmd_str), argus, &num_of_argu))
920 return ERROR_FAIL;
921
922 /* NOTE: we're a bit loose here, because we ignore case in
923 * TAP state names (instead of insisting on uppercase).
924 */
925
926 command = svf_find_string_in_array(argus[0],
927 (char **)svf_command_name, ARRAY_SIZE(svf_command_name));
928 switch (command) {
929 case ENDDR:
930 case ENDIR:
931 if (num_of_argu != 2) {
932 LOG_ERROR("invalid parameter of %s", argus[0]);
933 return ERROR_FAIL;
934 }
935
936 i_tmp = tap_state_by_name(argus[1]);
937
938 if (svf_tap_state_is_stable(i_tmp)) {
939 if (command == ENDIR) {
940 svf_para.ir_end_state = i_tmp;
941 LOG_DEBUG("\tIR end_state = %s",
942 tap_state_name(i_tmp));
943 } else {
944 svf_para.dr_end_state = i_tmp;
945 LOG_DEBUG("\tDR end_state = %s",
946 tap_state_name(i_tmp));
947 }
948 } else {
949 LOG_ERROR("%s: %s is not a stable state",
950 argus[0], argus[1]);
951 return ERROR_FAIL;
952 }
953 break;
954 case FREQUENCY:
955 if ((num_of_argu != 1) && (num_of_argu != 3)) {
956 LOG_ERROR("invalid parameter of %s", argus[0]);
957 return ERROR_FAIL;
958 }
959 if (1 == num_of_argu) {
960 /* TODO: set jtag speed to full speed */
961 svf_para.frequency = 0;
962 } else {
963 if (strcmp(argus[2], "HZ")) {
964 LOG_ERROR("HZ not found in FREQUENCY command");
965 return ERROR_FAIL;
966 }
967 if (ERROR_OK != svf_execute_tap())
968 return ERROR_FAIL;
969 svf_para.frequency = atof(argus[1]);
970 /* TODO: set jtag speed to */
971 if (svf_para.frequency > 0) {
972 command_run_linef(cmd_ctx,
973 "adapter_khz %d",
974 (int)svf_para.frequency / 1000);
975 LOG_DEBUG("\tfrequency = %f", svf_para.frequency);
976 }
977 }
978 break;
979 case HDR:
980 if (svf_tap_is_specified) {
981 padding_command_skipped = 1;
982 break;
983 }
984 xxr_para_tmp = &svf_para.hdr_para;
985 goto XXR_common;
986 case HIR:
987 if (svf_tap_is_specified) {
988 padding_command_skipped = 1;
989 break;
990 }
991 xxr_para_tmp = &svf_para.hir_para;
992 goto XXR_common;
993 case TDR:
994 if (svf_tap_is_specified) {
995 padding_command_skipped = 1;
996 break;
997 }
998 xxr_para_tmp = &svf_para.tdr_para;
999 goto XXR_common;
1000 case TIR:
1001 if (svf_tap_is_specified) {
1002 padding_command_skipped = 1;
1003 break;
1004 }
1005 xxr_para_tmp = &svf_para.tir_para;
1006 goto XXR_common;
1007 case SDR:
1008 xxr_para_tmp = &svf_para.sdr_para;
1009 goto XXR_common;
1010 case SIR:
1011 xxr_para_tmp = &svf_para.sir_para;
1012 goto XXR_common;
1013 XXR_common:
1014 /* XXR length [TDI (tdi)] [TDO (tdo)][MASK (mask)] [SMASK (smask)] */
1015 if ((num_of_argu > 10) || (num_of_argu % 2)) {
1016 LOG_ERROR("invalid parameter of %s", argus[0]);
1017 return ERROR_FAIL;
1018 }
1019 i_tmp = xxr_para_tmp->len;
1020 xxr_para_tmp->len = atoi(argus[1]);
1021 LOG_DEBUG("\tlength = %d", xxr_para_tmp->len);
1022 xxr_para_tmp->data_mask = 0;
1023 for (i = 2; i < num_of_argu; i += 2) {
1024 if ((strlen(argus[i + 1]) < 3) || (argus[i + 1][0] != '(') ||
1025 (argus[i + 1][strlen(argus[i + 1]) - 1] != ')')) {
1026 LOG_ERROR("data section error");
1027 return ERROR_FAIL;
1028 }
1029 argus[i + 1][strlen(argus[i + 1]) - 1] = '\0';
1030 /* TDI, TDO, MASK, SMASK */
1031 if (!strcmp(argus[i], "TDI")) {
1032 /* TDI */
1033 pbuffer_tmp = &xxr_para_tmp->tdi;
1034 xxr_para_tmp->data_mask |= XXR_TDI;
1035 } else if (!strcmp(argus[i], "TDO")) {
1036 /* TDO */
1037 pbuffer_tmp = &xxr_para_tmp->tdo;
1038 xxr_para_tmp->data_mask |= XXR_TDO;
1039 } else if (!strcmp(argus[i], "MASK")) {
1040 /* MASK */
1041 pbuffer_tmp = &xxr_para_tmp->mask;
1042 xxr_para_tmp->data_mask |= XXR_MASK;
1043 } else if (!strcmp(argus[i], "SMASK")) {
1044 /* SMASK */
1045 pbuffer_tmp = &xxr_para_tmp->smask;
1046 xxr_para_tmp->data_mask |= XXR_SMASK;
1047 } else {
1048 LOG_ERROR("unknow parameter: %s", argus[i]);
1049 return ERROR_FAIL;
1050 }
1051 if (ERROR_OK !=
1052 svf_copy_hexstring_to_binary(&argus[i + 1][1], pbuffer_tmp, i_tmp,
1053 xxr_para_tmp->len)) {
1054 LOG_ERROR("fail to parse hex value");
1055 return ERROR_FAIL;
1056 }
1057 SVF_BUF_LOG(DEBUG, *pbuffer_tmp, xxr_para_tmp->len, argus[i]);
1058 }
1059 /* If a command changes the length of the last scan of the same type and the
1060 * MASK parameter is absent, */
1061 /* the mask pattern used is all cares */
1062 if (!(xxr_para_tmp->data_mask & XXR_MASK) && (i_tmp != xxr_para_tmp->len)) {
1063 /* MASK not defined and length changed */
1064 if (ERROR_OK !=
1065 svf_adjust_array_length(&xxr_para_tmp->mask, i_tmp,
1066 xxr_para_tmp->len)) {
1067 LOG_ERROR("fail to adjust length of array");
1068 return ERROR_FAIL;
1069 }
1070 buf_set_ones(xxr_para_tmp->mask, xxr_para_tmp->len);
1071 }
1072 /* If TDO is absent, no comparison is needed, set the mask to 0 */
1073 if (!(xxr_para_tmp->data_mask & XXR_TDO)) {
1074 if (NULL == xxr_para_tmp->tdo) {
1075 if (ERROR_OK !=
1076 svf_adjust_array_length(&xxr_para_tmp->tdo, i_tmp,
1077 xxr_para_tmp->len)) {
1078 LOG_ERROR("fail to adjust length of array");
1079 return ERROR_FAIL;
1080 }
1081 }
1082 if (NULL == xxr_para_tmp->mask) {
1083 if (ERROR_OK !=
1084 svf_adjust_array_length(&xxr_para_tmp->mask, i_tmp,
1085 xxr_para_tmp->len)) {
1086 LOG_ERROR("fail to adjust length of array");
1087 return ERROR_FAIL;
1088 }
1089 }
1090 memset(xxr_para_tmp->mask, 0, (xxr_para_tmp->len + 7) >> 3);
1091 }
1092 /* do scan if necessary */
1093 if (SDR == command) {
1094 /* check buffer size first, reallocate if necessary */
1095 i = svf_para.hdr_para.len + svf_para.sdr_para.len +
1096 svf_para.tdr_para.len;
1097 if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3)) {
1098 /* reallocate buffer */
1099 if (svf_realloc_buffers(svf_buffer_index + ((i + 7) >> 3)) != ERROR_OK) {
1100 LOG_ERROR("not enough memory");
1101 return ERROR_FAIL;
1102 }
1103 }
1104
1105 /* assemble dr data */
1106 i = 0;
1107 buf_set_buf(svf_para.hdr_para.tdi,
1108 0,
1109 &svf_tdi_buffer[svf_buffer_index],
1110 i,
1111 svf_para.hdr_para.len);
1112 i += svf_para.hdr_para.len;
1113 buf_set_buf(svf_para.sdr_para.tdi,
1114 0,
1115 &svf_tdi_buffer[svf_buffer_index],
1116 i,
1117 svf_para.sdr_para.len);
1118 i += svf_para.sdr_para.len;
1119 buf_set_buf(svf_para.tdr_para.tdi,
1120 0,
1121 &svf_tdi_buffer[svf_buffer_index],
1122 i,
1123 svf_para.tdr_para.len);
1124 i += svf_para.tdr_para.len;
1125
1126 /* add check data */
1127 if (svf_para.sdr_para.data_mask & XXR_TDO) {
1128 /* assemble dr mask data */
1129 i = 0;
1130 buf_set_buf(svf_para.hdr_para.mask,
1131 0,
1132 &svf_mask_buffer[svf_buffer_index],
1133 i,
1134 svf_para.hdr_para.len);
1135 i += svf_para.hdr_para.len;
1136 buf_set_buf(svf_para.sdr_para.mask,
1137 0,
1138 &svf_mask_buffer[svf_buffer_index],
1139 i,
1140 svf_para.sdr_para.len);
1141 i += svf_para.sdr_para.len;
1142 buf_set_buf(svf_para.tdr_para.mask,
1143 0,
1144 &svf_mask_buffer[svf_buffer_index],
1145 i,
1146 svf_para.tdr_para.len);
1147
1148 /* assemble dr check data */
1149 i = 0;
1150 buf_set_buf(svf_para.hdr_para.tdo,
1151 0,
1152 &svf_tdo_buffer[svf_buffer_index],
1153 i,
1154 svf_para.hdr_para.len);
1155 i += svf_para.hdr_para.len;
1156 buf_set_buf(svf_para.sdr_para.tdo,
1157 0,
1158 &svf_tdo_buffer[svf_buffer_index],
1159 i,
1160 svf_para.sdr_para.len);
1161 i += svf_para.sdr_para.len;
1162 buf_set_buf(svf_para.tdr_para.tdo,
1163 0,
1164 &svf_tdo_buffer[svf_buffer_index],
1165 i,
1166 svf_para.tdr_para.len);
1167 i += svf_para.tdr_para.len;
1168
1169 svf_add_check_para(1, svf_buffer_index, i);
1170 } else
1171 svf_add_check_para(0, svf_buffer_index, i);
1172 field.num_bits = i;
1173 field.out_value = &svf_tdi_buffer[svf_buffer_index];
1174 field.in_value = &svf_tdi_buffer[svf_buffer_index];
1175 if (!svf_nil) {
1176 /* NOTE: doesn't use SVF-specified state paths */
1177 jtag_add_plain_dr_scan(field.num_bits,
1178 field.out_value,
1179 field.in_value,
1180 svf_para.dr_end_state);
1181 }
1182
1183 svf_buffer_index += (i + 7) >> 3;
1184 } else if (SIR == command) {
1185 /* check buffer size first, reallocate if necessary */
1186 i = svf_para.hir_para.len + svf_para.sir_para.len +
1187 svf_para.tir_para.len;
1188 if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3)) {
1189 if (svf_realloc_buffers(svf_buffer_index + ((i + 7) >> 3)) != ERROR_OK) {
1190 LOG_ERROR("not enough memory");
1191 return ERROR_FAIL;
1192 }
1193 }
1194
1195 /* assemble ir data */
1196 i = 0;
1197 buf_set_buf(svf_para.hir_para.tdi,
1198 0,
1199 &svf_tdi_buffer[svf_buffer_index],
1200 i,
1201 svf_para.hir_para.len);
1202 i += svf_para.hir_para.len;
1203 buf_set_buf(svf_para.sir_para.tdi,
1204 0,
1205 &svf_tdi_buffer[svf_buffer_index],
1206 i,
1207 svf_para.sir_para.len);
1208 i += svf_para.sir_para.len;
1209 buf_set_buf(svf_para.tir_para.tdi,
1210 0,
1211 &svf_tdi_buffer[svf_buffer_index],
1212 i,
1213 svf_para.tir_para.len);
1214 i += svf_para.tir_para.len;
1215
1216 /* add check data */
1217 if (svf_para.sir_para.data_mask & XXR_TDO) {
1218 /* assemble dr mask data */
1219 i = 0;
1220 buf_set_buf(svf_para.hir_para.mask,
1221 0,
1222 &svf_mask_buffer[svf_buffer_index],
1223 i,
1224 svf_para.hir_para.len);
1225 i += svf_para.hir_para.len;
1226 buf_set_buf(svf_para.sir_para.mask,
1227 0,
1228 &svf_mask_buffer[svf_buffer_index],
1229 i,
1230 svf_para.sir_para.len);
1231 i += svf_para.sir_para.len;
1232 buf_set_buf(svf_para.tir_para.mask,
1233 0,
1234 &svf_mask_buffer[svf_buffer_index],
1235 i,
1236 svf_para.tir_para.len);
1237
1238 /* assemble dr check data */
1239 i = 0;
1240 buf_set_buf(svf_para.hir_para.tdo,
1241 0,
1242 &svf_tdo_buffer[svf_buffer_index],
1243 i,
1244 svf_para.hir_para.len);
1245 i += svf_para.hir_para.len;
1246 buf_set_buf(svf_para.sir_para.tdo,
1247 0,
1248 &svf_tdo_buffer[svf_buffer_index],
1249 i,
1250 svf_para.sir_para.len);
1251 i += svf_para.sir_para.len;
1252 buf_set_buf(svf_para.tir_para.tdo,
1253 0,
1254 &svf_tdo_buffer[svf_buffer_index],
1255 i,
1256 svf_para.tir_para.len);
1257 i += svf_para.tir_para.len;
1258
1259 svf_add_check_para(1, svf_buffer_index, i);
1260 } else
1261 svf_add_check_para(0, svf_buffer_index, i);
1262 field.num_bits = i;
1263 field.out_value = &svf_tdi_buffer[svf_buffer_index];
1264 field.in_value = &svf_tdi_buffer[svf_buffer_index];
1265 if (!svf_nil) {
1266 /* NOTE: doesn't use SVF-specified state paths */
1267 jtag_add_plain_ir_scan(field.num_bits,
1268 field.out_value,
1269 field.in_value,
1270 svf_para.ir_end_state);
1271 }
1272
1273 svf_buffer_index += (i + 7) >> 3;
1274 }
1275 break;
1276 case PIO:
1277 case PIOMAP:
1278 LOG_ERROR("PIO and PIOMAP are not supported");
1279 return ERROR_FAIL;
1280 break;
1281 case RUNTEST:
1282 /* RUNTEST [run_state] run_count run_clk [min_time SEC [MAXIMUM max_time
1283 * SEC]] [ENDSTATE end_state] */
1284 /* RUNTEST [run_state] min_time SEC [MAXIMUM max_time SEC] [ENDSTATE
1285 * end_state] */
1286 if ((num_of_argu < 3) && (num_of_argu > 11)) {
1287 LOG_ERROR("invalid parameter of %s", argus[0]);
1288 return ERROR_FAIL;
1289 }
1290 /* init */
1291 run_count = 0;
1292 min_time = 0;
1293 i = 1;
1294
1295 /* run_state */
1296 i_tmp = tap_state_by_name(argus[i]);
1297 if (i_tmp != TAP_INVALID) {
1298 if (svf_tap_state_is_stable(i_tmp)) {
1299 svf_para.runtest_run_state = i_tmp;
1300
1301 /* When a run_state is specified, the new
1302 * run_state becomes the default end_state.
1303 */
1304 svf_para.runtest_end_state = i_tmp;
1305 LOG_DEBUG("\trun_state = %s", tap_state_name(i_tmp));
1306 i++;
1307 } else {
1308 LOG_ERROR("%s: %s is not a stable state", argus[0], tap_state_name(i_tmp));
1309 return ERROR_FAIL;
1310 }
1311 }
1312
1313 /* run_count run_clk */
1314 if (((i + 2) <= num_of_argu) && strcmp(argus[i + 1], "SEC")) {
1315 if (!strcmp(argus[i + 1], "TCK")) {
1316 /* clock source is TCK */
1317 run_count = atoi(argus[i]);
1318 LOG_DEBUG("\trun_count@TCK = %d", run_count);
1319 } else {
1320 LOG_ERROR("%s not supported for clock", argus[i + 1]);
1321 return ERROR_FAIL;
1322 }
1323 i += 2;
1324 }
1325 /* min_time SEC */
1326 if (((i + 2) <= num_of_argu) && !strcmp(argus[i + 1], "SEC")) {
1327 min_time = atof(argus[i]);
1328 LOG_DEBUG("\tmin_time = %fs", min_time);
1329 i += 2;
1330 }
1331 /* MAXIMUM max_time SEC */
1332 if (((i + 3) <= num_of_argu) &&
1333 !strcmp(argus[i], "MAXIMUM") && !strcmp(argus[i + 2], "SEC")) {
1334 float max_time = 0;
1335 max_time = atof(argus[i + 1]);
1336 LOG_DEBUG("\tmax_time = %fs", max_time);
1337 i += 3;
1338 }
1339 /* ENDSTATE end_state */
1340 if (((i + 2) <= num_of_argu) && !strcmp(argus[i], "ENDSTATE")) {
1341 i_tmp = tap_state_by_name(argus[i + 1]);
1342
1343 if (svf_tap_state_is_stable(i_tmp)) {
1344 svf_para.runtest_end_state = i_tmp;
1345 LOG_DEBUG("\tend_state = %s", tap_state_name(i_tmp));
1346 } else {
1347 LOG_ERROR("%s: %s is not a stable state", argus[0], tap_state_name(i_tmp));
1348 return ERROR_FAIL;
1349 }
1350 i += 2;
1351 }
1352
1353 /* all parameter should be parsed */
1354 if (i == num_of_argu) {
1355 #if 1
1356 /* FIXME handle statemove failures */
1357 uint32_t min_usec = 1000000 * min_time;
1358
1359 /* enter into run_state if necessary */
1360 if (cmd_queue_cur_state != svf_para.runtest_run_state)
1361 svf_add_statemove(svf_para.runtest_run_state);
1362
1363 /* add clocks and/or min wait */
1364 if (run_count > 0) {
1365 if (!svf_nil)
1366 jtag_add_clocks(run_count);
1367 }
1368
1369 if (min_usec > 0) {
1370 if (!svf_nil)
1371 jtag_add_sleep(min_usec);
1372 }
1373
1374 /* move to end_state if necessary */
1375 if (svf_para.runtest_end_state != svf_para.runtest_run_state)
1376 svf_add_statemove(svf_para.runtest_end_state);
1377
1378 #else
1379 if (svf_para.runtest_run_state != TAP_IDLE) {
1380 LOG_ERROR("cannot runtest in %s state",
1381 tap_state_name(svf_para.runtest_run_state));
1382 return ERROR_FAIL;
1383 }
1384
1385 if (!svf_nil)
1386 jtag_add_runtest(run_count, svf_para.runtest_end_state);
1387 #endif
1388 } else {
1389 LOG_ERROR("fail to parse parameter of RUNTEST, %d out of %d is parsed",
1390 i,
1391 num_of_argu);
1392 return ERROR_FAIL;
1393 }
1394 break;
1395 case STATE:
1396 /* STATE [pathstate1 [pathstate2 ...[pathstaten]]] stable_state */
1397 if (num_of_argu < 2) {
1398 LOG_ERROR("invalid parameter of %s", argus[0]);
1399 return ERROR_FAIL;
1400 }
1401 if (num_of_argu > 2) {
1402 /* STATE pathstate1 ... stable_state */
1403 path = malloc((num_of_argu - 1) * sizeof(tap_state_t));
1404 if (NULL == path) {
1405 LOG_ERROR("not enough memory");
1406 return ERROR_FAIL;
1407 }
1408 num_of_argu--; /* num of path */
1409 i_tmp = 1; /* path is from parameter 1 */
1410 for (i = 0; i < num_of_argu; i++, i_tmp++) {
1411 path[i] = tap_state_by_name(argus[i_tmp]);
1412 if (path[i] == TAP_INVALID) {
1413 LOG_ERROR("%s: %s is not a valid state", argus[0], argus[i_tmp]);
1414 free(path);
1415 return ERROR_FAIL;
1416 }
1417 /* OpenOCD refuses paths containing TAP_RESET */
1418 if (TAP_RESET == path[i]) {
1419 /* FIXME last state MUST be stable! */
1420 if (i > 0) {
1421 if (!svf_nil)
1422 jtag_add_pathmove(i, path);
1423 }
1424 if (!svf_nil)
1425 jtag_add_tlr();
1426 num_of_argu -= i + 1;
1427 i = -1;
1428 }
1429 }
1430 if (num_of_argu > 0) {
1431 /* execute last path if necessary */
1432 if (svf_tap_state_is_stable(path[num_of_argu - 1])) {
1433 /* last state MUST be stable state */
1434 if (!svf_nil)
1435 jtag_add_pathmove(num_of_argu, path);
1436 LOG_DEBUG("\tmove to %s by path_move",
1437 tap_state_name(path[num_of_argu - 1]));
1438 } else {
1439 LOG_ERROR("%s: %s is not a stable state",
1440 argus[0],
1441 tap_state_name(path[num_of_argu - 1]));
1442 free(path);
1443 return ERROR_FAIL;
1444 }
1445 }
1446
1447 free(path);
1448 path = NULL;
1449 } else {
1450 /* STATE stable_state */
1451 state = tap_state_by_name(argus[1]);
1452 if (svf_tap_state_is_stable(state)) {
1453 LOG_DEBUG("\tmove to %s by svf_add_statemove",
1454 tap_state_name(state));
1455 /* FIXME handle statemove failures */
1456 svf_add_statemove(state);
1457 } else {
1458 LOG_ERROR("%s: %s is not a stable state",
1459 argus[0], tap_state_name(state));
1460 return ERROR_FAIL;
1461 }
1462 }
1463 break;
1464 case TRST:
1465 /* TRST trst_mode */
1466 if (num_of_argu != 2) {
1467 LOG_ERROR("invalid parameter of %s", argus[0]);
1468 return ERROR_FAIL;
1469 }
1470 if (svf_para.trst_mode != TRST_ABSENT) {
1471 if (ERROR_OK != svf_execute_tap())
1472 return ERROR_FAIL;
1473 i_tmp = svf_find_string_in_array(argus[1],
1474 (char **)svf_trst_mode_name,
1475 ARRAY_SIZE(svf_trst_mode_name));
1476 switch (i_tmp) {
1477 case TRST_ON:
1478 if (!svf_nil)
1479 jtag_add_reset(1, 0);
1480 break;
1481 case TRST_Z:
1482 case TRST_OFF:
1483 if (!svf_nil)
1484 jtag_add_reset(0, 0);
1485 break;
1486 case TRST_ABSENT:
1487 break;
1488 default:
1489 LOG_ERROR("unknown TRST mode: %s", argus[1]);
1490 return ERROR_FAIL;
1491 }
1492 svf_para.trst_mode = i_tmp;
1493 LOG_DEBUG("\ttrst_mode = %s", svf_trst_mode_name[svf_para.trst_mode]);
1494 } else {
1495 LOG_ERROR("can not accpet TRST command if trst_mode is ABSENT");
1496 return ERROR_FAIL;
1497 }
1498 break;
1499 default:
1500 LOG_ERROR("invalid svf command: %s", argus[0]);
1501 return ERROR_FAIL;
1502 break;
1503 }
1504
1505 if (!svf_quiet) {
1506 if (padding_command_skipped)
1507 LOG_USER("(Above Padding command skipped, as per -tap argument)");
1508 }
1509
1510 if (debug_level >= LOG_LVL_DEBUG) {
1511 /* for convenient debugging, execute tap if possible */
1512 if ((svf_buffer_index > 0) && \
1513 (((command != STATE) && (command != RUNTEST)) || \
1514 ((command == STATE) && (num_of_argu == 2)))) {
1515 if (ERROR_OK != svf_execute_tap())
1516 return ERROR_FAIL;
1517
1518 /* output debug info */
1519 if ((SIR == command) || (SDR == command)) {
1520 SVF_BUF_LOG(DEBUG, svf_tdi_buffer, svf_check_tdo_para[0].bit_len, "TDO read");
1521 }
1522 }
1523 } else {
1524 /* for fast executing, execute tap if necessary */
1525 /* half of the buffer is for the next command */
1526 if (((svf_buffer_index >= SVF_MAX_BUFFER_SIZE_TO_COMMIT) ||
1527 (svf_check_tdo_para_index >= SVF_CHECK_TDO_PARA_SIZE / 2)) && \
1528 (((command != STATE) && (command != RUNTEST)) || \
1529 ((command == STATE) && (num_of_argu == 2))))
1530 return svf_execute_tap();
1531 }
1532
1533 return ERROR_OK;
1534 }
1535
1536 static const struct command_registration svf_command_handlers[] = {
1537 {
1538 .name = "svf",
1539 .handler = handle_svf_command,
1540 .mode = COMMAND_EXEC,
1541 .help = "Runs a SVF file.",
1542 .usage = "svf [-tap device.tap] <file> [quiet] [nil] [progress]",
1543 },
1544 COMMAND_REGISTRATION_DONE
1545 };
1546
1547 int svf_register_commands(struct command_context *cmd_ctx)
1548 {
1549 return register_commands(cmd_ctx, NULL, svf_command_handlers);
1550 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)