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

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)