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

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)