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

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)