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

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)