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

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)