jtag: tcl: show error message when attempting manual "drscan" on a bypassed tap
[openocd.git] / src / jtag / tcl.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2
3 /***************************************************************************
4 * Copyright (C) 2005 by Dominic Rath *
5 * Dominic.Rath@gmx.de *
6 * *
7 * Copyright (C) 2007-2010 Øyvind Harboe *
8 * oyvind.harboe@zylin.com *
9 * *
10 * Copyright (C) 2009 SoftPLC Corporation *
11 * http://softplc.com *
12 * dick@softplc.com *
13 * *
14 * Copyright (C) 2009 Zachary T Welch *
15 * zw@superlucidity.net *
16 ***************************************************************************/
17
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #include "adapter.h"
23 #include "jtag.h"
24 #include "swd.h"
25 #include "minidriver.h"
26 #include "interface.h"
27 #include "interfaces.h"
28 #include "tcl.h"
29
30 #ifdef HAVE_STRINGS_H
31 #include <strings.h>
32 #endif
33
34 #include <helper/time_support.h>
35 #include "transport/transport.h"
36
37 /**
38 * @file
39 * Holds support for accessing JTAG-specific mechanisms from TCl scripts.
40 */
41
42 static const struct jim_nvp nvp_jtag_tap_event[] = {
43 { .value = JTAG_TRST_ASSERTED, .name = "post-reset" },
44 { .value = JTAG_TAP_EVENT_SETUP, .name = "setup" },
45 { .value = JTAG_TAP_EVENT_ENABLE, .name = "tap-enable" },
46 { .value = JTAG_TAP_EVENT_DISABLE, .name = "tap-disable" },
47
48 { .name = NULL, .value = -1 }
49 };
50
51 struct jtag_tap *jtag_tap_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
52 {
53 const char *cp = Jim_GetString(o, NULL);
54 struct jtag_tap *t = cp ? jtag_tap_by_string(cp) : NULL;
55 if (!cp)
56 cp = "(unknown)";
57 if (!t)
58 Jim_SetResultFormatted(interp, "Tap '%s' could not be found", cp);
59 return t;
60 }
61
62 static bool scan_is_safe(tap_state_t state)
63 {
64 switch (state) {
65 case TAP_RESET:
66 case TAP_IDLE:
67 case TAP_DRPAUSE:
68 case TAP_IRPAUSE:
69 return true;
70 default:
71 return false;
72 }
73 }
74
75 static COMMAND_HELPER(handle_jtag_command_drscan_fields, struct scan_field *fields)
76 {
77 unsigned int field_count = 0;
78 for (unsigned int i = 1; i < CMD_ARGC; i += 2) {
79 unsigned int bits;
80 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[i], bits);
81 fields[field_count].num_bits = bits;
82
83 void *t = malloc(DIV_ROUND_UP(bits, 8));
84 if (!t) {
85 LOG_ERROR("Out of memory");
86 return ERROR_FAIL;
87 }
88 fields[field_count].out_value = t;
89 str_to_buf(CMD_ARGV[i + 1], strlen(CMD_ARGV[i + 1]), t, bits, 0);
90 fields[field_count].in_value = t;
91 field_count++;
92 }
93
94 return ERROR_OK;
95 }
96
97 COMMAND_HANDLER(handle_jtag_command_drscan)
98 {
99 /*
100 * CMD_ARGV[0] = device
101 * CMD_ARGV[1] = num_bits
102 * CMD_ARGV[2] = hex string
103 * ... repeat num bits and hex string ...
104 *
105 * ... optionally:
106 * CMD_ARGV[CMD_ARGC-2] = "-endstate"
107 * CMD_ARGV[CMD_ARGC-1] = statename
108 */
109
110 if (CMD_ARGC < 3 || (CMD_ARGC % 2) != 1)
111 return ERROR_COMMAND_SYNTAX_ERROR;
112
113 struct jtag_tap *tap = jtag_tap_by_string(CMD_ARGV[0]);
114 if (!tap) {
115 command_print(CMD, "Tap '%s' could not be found", CMD_ARGV[0]);
116 return ERROR_COMMAND_ARGUMENT_INVALID;
117 }
118
119 if (tap->bypass) {
120 command_print(CMD, "Can't execute as the selected tap is in BYPASS");
121 return ERROR_FAIL;
122 }
123
124 tap_state_t endstate = TAP_IDLE;
125 if (CMD_ARGC > 3 && !strcmp("-endstate", CMD_ARGV[CMD_ARGC - 2])) {
126 const char *state_name = CMD_ARGV[CMD_ARGC - 1];
127 endstate = tap_state_by_name(state_name);
128 if (endstate < 0) {
129 command_print(CMD, "endstate: %s invalid", state_name);
130 return ERROR_COMMAND_ARGUMENT_INVALID;
131 }
132
133 if (!scan_is_safe(endstate))
134 LOG_WARNING("drscan with unsafe endstate \"%s\"", state_name);
135
136 CMD_ARGC -= 2;
137 }
138
139 unsigned int num_fields = (CMD_ARGC - 1) / 2;
140 struct scan_field *fields = calloc(num_fields, sizeof(struct scan_field));
141 if (!fields) {
142 LOG_ERROR("Out of memory");
143 return ERROR_FAIL;
144 }
145
146 int retval = CALL_COMMAND_HANDLER(handle_jtag_command_drscan_fields, fields);
147 if (retval != ERROR_OK)
148 goto fail;
149
150 jtag_add_dr_scan(tap, num_fields, fields, endstate);
151
152 retval = jtag_execute_queue();
153 if (retval != ERROR_OK) {
154 command_print(CMD, "drscan: jtag execute failed");
155 goto fail;
156 }
157
158 for (unsigned int i = 0; i < num_fields; i++) {
159 char *str = buf_to_hex_str(fields[i].in_value, fields[i].num_bits);
160 command_print(CMD, "%s", str);
161 free(str);
162 }
163
164 fail:
165 for (unsigned int i = 0; i < num_fields; i++)
166 free(fields[i].in_value);
167 free(fields);
168
169 return retval;
170 }
171
172 COMMAND_HANDLER(handle_jtag_command_pathmove)
173 {
174 tap_state_t states[8];
175
176 if (CMD_ARGC < 1 || CMD_ARGC > ARRAY_SIZE(states))
177 return ERROR_COMMAND_SYNTAX_ERROR;
178
179 for (unsigned int i = 0; i < CMD_ARGC; i++) {
180 states[i] = tap_state_by_name(CMD_ARGV[i]);
181 if (states[i] < 0) {
182 command_print(CMD, "endstate: %s invalid", CMD_ARGV[i]);
183 return ERROR_COMMAND_ARGUMENT_INVALID;
184 }
185 }
186
187 int retval = jtag_add_statemove(states[0]);
188 if (retval == ERROR_OK)
189 retval = jtag_execute_queue();
190 if (retval != ERROR_OK) {
191 command_print(CMD, "pathmove: jtag execute failed");
192 return retval;
193 }
194
195 jtag_add_pathmove(CMD_ARGC - 1, states + 1);
196 retval = jtag_execute_queue();
197 if (retval != ERROR_OK) {
198 command_print(CMD, "pathmove: failed");
199 return retval;
200 }
201
202 return ERROR_OK;
203 }
204
205 COMMAND_HANDLER(handle_jtag_flush_count)
206 {
207 if (CMD_ARGC != 0)
208 return ERROR_COMMAND_SYNTAX_ERROR;
209
210 int count = jtag_get_flush_queue_count();
211 command_print_sameline(CMD, "%d", count);
212
213 return ERROR_OK;
214 }
215
216 /* REVISIT Just what about these should "move" ... ?
217 * These registrations, into the main JTAG table?
218 *
219 * There's a minor compatibility issue, these all show up twice;
220 * that's not desirable:
221 * - jtag drscan ... NOT DOCUMENTED!
222 * - drscan ...
223 *
224 * The "irscan" command (for example) doesn't show twice.
225 */
226 static const struct command_registration jtag_command_handlers_to_move[] = {
227 {
228 .name = "drscan",
229 .mode = COMMAND_EXEC,
230 .handler = handle_jtag_command_drscan,
231 .help = "Execute Data Register (DR) scan for one TAP. "
232 "Other TAPs must be in BYPASS mode.",
233 .usage = "tap_name (num_bits value)+ ['-endstate' state_name]",
234 },
235 {
236 .name = "flush_count",
237 .mode = COMMAND_EXEC,
238 .handler = handle_jtag_flush_count,
239 .help = "Returns the number of times the JTAG queue "
240 "has been flushed.",
241 .usage = "",
242 },
243 {
244 .name = "pathmove",
245 .mode = COMMAND_EXEC,
246 .handler = handle_jtag_command_pathmove,
247 .usage = "start_state state1 [state2 [state3 ...]]",
248 .help = "Move JTAG state machine from current state "
249 "(start_state) to state1, then state2, state3, etc.",
250 },
251 COMMAND_REGISTRATION_DONE
252 };
253
254
255 enum jtag_tap_cfg_param {
256 JCFG_EVENT,
257 JCFG_IDCODE,
258 };
259
260 static struct jim_nvp nvp_config_opts[] = {
261 { .name = "-event", .value = JCFG_EVENT },
262 { .name = "-idcode", .value = JCFG_IDCODE },
263
264 { .name = NULL, .value = -1 }
265 };
266
267 static int jtag_tap_configure_event(struct jim_getopt_info *goi, struct jtag_tap *tap)
268 {
269 if (goi->argc == 0) {
270 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> ...");
271 return JIM_ERR;
272 }
273
274 struct jim_nvp *n;
275 int e = jim_getopt_nvp(goi, nvp_jtag_tap_event, &n);
276 if (e != JIM_OK) {
277 jim_getopt_nvp_unknown(goi, nvp_jtag_tap_event, 1);
278 return e;
279 }
280
281 if (goi->isconfigure) {
282 if (goi->argc != 1) {
283 Jim_WrongNumArgs(goi->interp,
284 goi->argc,
285 goi->argv,
286 "-event <event-name> <event-body>");
287 return JIM_ERR;
288 }
289 } else {
290 if (goi->argc != 0) {
291 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name>");
292 return JIM_ERR;
293 }
294 }
295
296 struct jtag_tap_event_action *jteap = tap->event_action;
297 /* replace existing event body */
298 bool found = false;
299 while (jteap) {
300 if (jteap->event == (enum jtag_event)n->value) {
301 found = true;
302 break;
303 }
304 jteap = jteap->next;
305 }
306
307 Jim_SetEmptyResult(goi->interp);
308
309 if (goi->isconfigure) {
310 if (!found)
311 jteap = calloc(1, sizeof(*jteap));
312 else if (jteap->body)
313 Jim_DecrRefCount(goi->interp, jteap->body);
314
315 jteap->interp = goi->interp;
316 jteap->event = n->value;
317
318 Jim_Obj *o;
319 jim_getopt_obj(goi, &o);
320 jteap->body = Jim_DuplicateObj(goi->interp, o);
321 Jim_IncrRefCount(jteap->body);
322
323 if (!found) {
324 /* add to head of event list */
325 jteap->next = tap->event_action;
326 tap->event_action = jteap;
327 }
328 } else if (found) {
329 jteap->interp = goi->interp;
330 Jim_SetResult(goi->interp,
331 Jim_DuplicateObj(goi->interp, jteap->body));
332 }
333 return JIM_OK;
334 }
335
336 static int jtag_tap_configure_cmd(struct jim_getopt_info *goi, struct jtag_tap *tap)
337 {
338 /* parse config or cget options */
339 while (goi->argc > 0) {
340 Jim_SetEmptyResult(goi->interp);
341
342 struct jim_nvp *n;
343 int e = jim_getopt_nvp(goi, nvp_config_opts, &n);
344 if (e != JIM_OK) {
345 jim_getopt_nvp_unknown(goi, nvp_config_opts, 0);
346 return e;
347 }
348
349 switch (n->value) {
350 case JCFG_EVENT:
351 e = jtag_tap_configure_event(goi, tap);
352 if (e != JIM_OK)
353 return e;
354 break;
355 case JCFG_IDCODE:
356 if (goi->isconfigure) {
357 Jim_SetResultFormatted(goi->interp,
358 "not settable: %s", n->name);
359 return JIM_ERR;
360 } else {
361 if (goi->argc != 0) {
362 Jim_WrongNumArgs(goi->interp,
363 goi->argc, goi->argv,
364 "NO PARAMS");
365 return JIM_ERR;
366 }
367 }
368 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, tap->idcode));
369 break;
370 default:
371 Jim_SetResultFormatted(goi->interp, "unknown value: %s", n->name);
372 return JIM_ERR;
373 }
374 }
375
376 return JIM_OK;
377 }
378
379 static int is_bad_irval(int ir_length, jim_wide w)
380 {
381 jim_wide v = 1;
382
383 v <<= ir_length;
384 v -= 1;
385 v = ~v;
386 return (w & v) != 0;
387 }
388
389 static int jim_newtap_expected_id(struct jim_nvp *n, struct jim_getopt_info *goi,
390 struct jtag_tap *tap)
391 {
392 jim_wide w;
393 int e = jim_getopt_wide(goi, &w);
394 if (e != JIM_OK) {
395 Jim_SetResultFormatted(goi->interp, "option: %s bad parameter", n->name);
396 return e;
397 }
398
399 uint32_t *p = realloc(tap->expected_ids,
400 (tap->expected_ids_cnt + 1) * sizeof(uint32_t));
401 if (!p) {
402 Jim_SetResultFormatted(goi->interp, "no memory");
403 return JIM_ERR;
404 }
405
406 tap->expected_ids = p;
407 tap->expected_ids[tap->expected_ids_cnt++] = w;
408
409 return JIM_OK;
410 }
411
412 #define NTAP_OPT_IRLEN 0
413 #define NTAP_OPT_IRMASK 1
414 #define NTAP_OPT_IRCAPTURE 2
415 #define NTAP_OPT_ENABLED 3
416 #define NTAP_OPT_DISABLED 4
417 #define NTAP_OPT_EXPECTED_ID 5
418 #define NTAP_OPT_VERSION 6
419 #define NTAP_OPT_BYPASS 7
420
421 static int jim_newtap_ir_param(struct jim_nvp *n, struct jim_getopt_info *goi,
422 struct jtag_tap *tap)
423 {
424 jim_wide w;
425 int e = jim_getopt_wide(goi, &w);
426 if (e != JIM_OK) {
427 Jim_SetResultFormatted(goi->interp,
428 "option: %s bad parameter", n->name);
429 return e;
430 }
431 switch (n->value) {
432 case NTAP_OPT_IRLEN:
433 if (w > (jim_wide) (8 * sizeof(tap->ir_capture_value))) {
434 LOG_WARNING("%s: huge IR length %d",
435 tap->dotted_name, (int) w);
436 }
437 tap->ir_length = w;
438 break;
439 case NTAP_OPT_IRMASK:
440 if (is_bad_irval(tap->ir_length, w)) {
441 LOG_ERROR("%s: IR mask %x too big",
442 tap->dotted_name,
443 (int) w);
444 return JIM_ERR;
445 }
446 if ((w & 3) != 3)
447 LOG_WARNING("%s: nonstandard IR mask", tap->dotted_name);
448 tap->ir_capture_mask = w;
449 break;
450 case NTAP_OPT_IRCAPTURE:
451 if (is_bad_irval(tap->ir_length, w)) {
452 LOG_ERROR("%s: IR capture %x too big",
453 tap->dotted_name, (int) w);
454 return JIM_ERR;
455 }
456 if ((w & 3) != 1)
457 LOG_WARNING("%s: nonstandard IR value",
458 tap->dotted_name);
459 tap->ir_capture_value = w;
460 break;
461 default:
462 return JIM_ERR;
463 }
464 return JIM_OK;
465 }
466
467 static int jim_newtap_cmd(struct jim_getopt_info *goi)
468 {
469 struct jtag_tap *tap;
470 int x;
471 int e;
472 struct jim_nvp *n;
473 char *cp;
474 const struct jim_nvp opts[] = {
475 { .name = "-irlen", .value = NTAP_OPT_IRLEN },
476 { .name = "-irmask", .value = NTAP_OPT_IRMASK },
477 { .name = "-ircapture", .value = NTAP_OPT_IRCAPTURE },
478 { .name = "-enable", .value = NTAP_OPT_ENABLED },
479 { .name = "-disable", .value = NTAP_OPT_DISABLED },
480 { .name = "-expected-id", .value = NTAP_OPT_EXPECTED_ID },
481 { .name = "-ignore-version", .value = NTAP_OPT_VERSION },
482 { .name = "-ignore-bypass", .value = NTAP_OPT_BYPASS },
483 { .name = NULL, .value = -1 },
484 };
485
486 tap = calloc(1, sizeof(struct jtag_tap));
487 if (!tap) {
488 Jim_SetResultFormatted(goi->interp, "no memory");
489 return JIM_ERR;
490 }
491
492 /*
493 * we expect CHIP + TAP + OPTIONS
494 * */
495 if (goi->argc < 3) {
496 Jim_SetResultFormatted(goi->interp, "Missing CHIP TAP OPTIONS ....");
497 free(tap);
498 return JIM_ERR;
499 }
500
501 const char *tmp;
502 jim_getopt_string(goi, &tmp, NULL);
503 tap->chip = strdup(tmp);
504
505 jim_getopt_string(goi, &tmp, NULL);
506 tap->tapname = strdup(tmp);
507
508 /* name + dot + name + null */
509 x = strlen(tap->chip) + 1 + strlen(tap->tapname) + 1;
510 cp = malloc(x);
511 sprintf(cp, "%s.%s", tap->chip, tap->tapname);
512 tap->dotted_name = cp;
513
514 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
515 tap->chip, tap->tapname, tap->dotted_name, goi->argc);
516
517 /* IEEE specifies that the two LSBs of an IR scan are 01, so make
518 * that the default. The "-ircapture" and "-irmask" options are only
519 * needed to cope with nonstandard TAPs, or to specify more bits.
520 */
521 tap->ir_capture_mask = 0x03;
522 tap->ir_capture_value = 0x01;
523
524 while (goi->argc) {
525 e = jim_getopt_nvp(goi, opts, &n);
526 if (e != JIM_OK) {
527 jim_getopt_nvp_unknown(goi, opts, 0);
528 free(cp);
529 free(tap);
530 return e;
531 }
532 LOG_DEBUG("Processing option: %s", n->name);
533 switch (n->value) {
534 case NTAP_OPT_ENABLED:
535 tap->disabled_after_reset = false;
536 break;
537 case NTAP_OPT_DISABLED:
538 tap->disabled_after_reset = true;
539 break;
540 case NTAP_OPT_EXPECTED_ID:
541 e = jim_newtap_expected_id(n, goi, tap);
542 if (e != JIM_OK) {
543 free(cp);
544 free(tap);
545 return e;
546 }
547 break;
548 case NTAP_OPT_IRLEN:
549 case NTAP_OPT_IRMASK:
550 case NTAP_OPT_IRCAPTURE:
551 e = jim_newtap_ir_param(n, goi, tap);
552 if (e != JIM_OK) {
553 free(cp);
554 free(tap);
555 return e;
556 }
557 break;
558 case NTAP_OPT_VERSION:
559 tap->ignore_version = true;
560 break;
561 case NTAP_OPT_BYPASS:
562 tap->ignore_bypass = true;
563 break;
564 } /* switch (n->value) */
565 } /* while (goi->argc) */
566
567 /* default is enabled-after-reset */
568 tap->enabled = !tap->disabled_after_reset;
569
570 /* Did all the required option bits get cleared? */
571 if (!transport_is_jtag() || tap->ir_length != 0) {
572 jtag_tap_init(tap);
573 return JIM_OK;
574 }
575
576 Jim_SetResultFormatted(goi->interp,
577 "newtap: %s missing IR length",
578 tap->dotted_name);
579 jtag_tap_free(tap);
580 return JIM_ERR;
581 }
582
583 static void jtag_tap_handle_event(struct jtag_tap *tap, enum jtag_event e)
584 {
585 struct jtag_tap_event_action *jteap;
586 int retval;
587
588 for (jteap = tap->event_action; jteap; jteap = jteap->next) {
589 if (jteap->event != e)
590 continue;
591
592 struct jim_nvp *nvp = jim_nvp_value2name_simple(nvp_jtag_tap_event, e);
593 LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
594 tap->dotted_name, e, nvp->name,
595 Jim_GetString(jteap->body, NULL));
596
597 retval = Jim_EvalObj(jteap->interp, jteap->body);
598 if (retval == JIM_RETURN)
599 retval = jteap->interp->returnCode;
600
601 if (retval != JIM_OK) {
602 Jim_MakeErrorMessage(jteap->interp);
603 LOG_USER("%s", Jim_GetString(Jim_GetResult(jteap->interp), NULL));
604 continue;
605 }
606
607 switch (e) {
608 case JTAG_TAP_EVENT_ENABLE:
609 case JTAG_TAP_EVENT_DISABLE:
610 /* NOTE: we currently assume the handlers
611 * can't fail. Right here is where we should
612 * really be verifying the scan chains ...
613 */
614 tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
615 LOG_INFO("JTAG tap: %s %s", tap->dotted_name,
616 tap->enabled ? "enabled" : "disabled");
617 break;
618 default:
619 break;
620 }
621 }
622 }
623
624 COMMAND_HANDLER(handle_jtag_arp_init)
625 {
626 if (CMD_ARGC != 0)
627 return ERROR_COMMAND_SYNTAX_ERROR;
628
629 return jtag_init_inner(CMD_CTX);
630 }
631
632 COMMAND_HANDLER(handle_jtag_arp_init_reset)
633 {
634 if (CMD_ARGC != 0)
635 return ERROR_COMMAND_SYNTAX_ERROR;
636
637 if (transport_is_jtag())
638 return jtag_init_reset(CMD_CTX);
639
640 if (transport_is_swd())
641 return swd_init_reset(CMD_CTX);
642
643 return ERROR_OK;
644 }
645
646 int jim_jtag_newtap(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
647 {
648 struct jim_getopt_info goi;
649 jim_getopt_setup(&goi, interp, argc-1, argv + 1);
650 return jim_newtap_cmd(&goi);
651 }
652
653 static bool jtag_tap_enable(struct jtag_tap *t)
654 {
655 if (t->enabled)
656 return false;
657 jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
658 if (!t->enabled)
659 return false;
660
661 /* FIXME add JTAG sanity checks, w/o TLR
662 * - scan chain length grew by one (this)
663 * - IDs and IR lengths are as expected
664 */
665 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
666 return true;
667 }
668 static bool jtag_tap_disable(struct jtag_tap *t)
669 {
670 if (!t->enabled)
671 return false;
672 jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
673 if (t->enabled)
674 return false;
675
676 /* FIXME add JTAG sanity checks, w/o TLR
677 * - scan chain length shrank by one (this)
678 * - IDs and IR lengths are as expected
679 */
680 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
681 return true;
682 }
683
684 int jim_jtag_tap_enabler(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
685 {
686 struct command *c = jim_to_command(interp);
687 const char *cmd_name = c->name;
688 struct jim_getopt_info goi;
689 jim_getopt_setup(&goi, interp, argc-1, argv + 1);
690 if (goi.argc != 1) {
691 Jim_SetResultFormatted(goi.interp, "usage: %s <name>", cmd_name);
692 return JIM_ERR;
693 }
694
695 struct jtag_tap *t;
696
697 t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
698 if (!t)
699 return JIM_ERR;
700
701 if (strcasecmp(cmd_name, "tapisenabled") == 0) {
702 /* do nothing, just return the value */
703 } else if (strcasecmp(cmd_name, "tapenable") == 0) {
704 if (!jtag_tap_enable(t)) {
705 LOG_WARNING("failed to enable tap %s", t->dotted_name);
706 return JIM_ERR;
707 }
708 } else if (strcasecmp(cmd_name, "tapdisable") == 0) {
709 if (!jtag_tap_disable(t)) {
710 LOG_WARNING("failed to disable tap %s", t->dotted_name);
711 return JIM_ERR;
712 }
713 } else {
714 LOG_ERROR("command '%s' unknown", cmd_name);
715 return JIM_ERR;
716 }
717 bool e = t->enabled;
718 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
719 return JIM_OK;
720 }
721
722 int jim_jtag_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
723 {
724 struct command *c = jim_to_command(interp);
725 const char *cmd_name = c->name;
726 struct jim_getopt_info goi;
727 jim_getopt_setup(&goi, interp, argc-1, argv + 1);
728 goi.isconfigure = !strcmp(cmd_name, "configure");
729 if (goi.argc < 2 + goi.isconfigure) {
730 Jim_WrongNumArgs(goi.interp, 0, NULL,
731 "<tap_name> <attribute> ...");
732 return JIM_ERR;
733 }
734
735 struct jtag_tap *t;
736
737 Jim_Obj *o;
738 jim_getopt_obj(&goi, &o);
739 t = jtag_tap_by_jim_obj(goi.interp, o);
740 if (!t)
741 return JIM_ERR;
742
743 return jtag_tap_configure_cmd(&goi, t);
744 }
745
746 COMMAND_HANDLER(handle_jtag_names)
747 {
748 if (CMD_ARGC != 0)
749 return ERROR_COMMAND_SYNTAX_ERROR;
750
751 for (struct jtag_tap *tap = jtag_all_taps(); tap; tap = tap->next_tap)
752 command_print(CMD, "%s", tap->dotted_name);
753
754 return ERROR_OK;
755 }
756
757 COMMAND_HANDLER(handle_jtag_init_command)
758 {
759 if (CMD_ARGC != 0)
760 return ERROR_COMMAND_SYNTAX_ERROR;
761
762 static bool jtag_initialized;
763 if (jtag_initialized) {
764 LOG_INFO("'jtag init' has already been called");
765 return ERROR_OK;
766 }
767 jtag_initialized = true;
768
769 LOG_DEBUG("Initializing jtag devices...");
770 return jtag_init(CMD_CTX);
771 }
772
773 static const struct command_registration jtag_subcommand_handlers[] = {
774 {
775 .name = "init",
776 .mode = COMMAND_ANY,
777 .handler = handle_jtag_init_command,
778 .help = "initialize jtag scan chain",
779 .usage = ""
780 },
781 {
782 .name = "arp_init",
783 .mode = COMMAND_ANY,
784 .handler = handle_jtag_arp_init,
785 .help = "Validates JTAG scan chain against the list of "
786 "declared TAPs using just the four standard JTAG "
787 "signals.",
788 .usage = "",
789 },
790 {
791 .name = "arp_init-reset",
792 .mode = COMMAND_ANY,
793 .handler = handle_jtag_arp_init_reset,
794 .help = "Uses TRST and SRST to try resetting everything on "
795 "the JTAG scan chain, then performs 'jtag arp_init'.",
796 .usage = "",
797 },
798 {
799 .name = "newtap",
800 .mode = COMMAND_CONFIG,
801 .jim_handler = jim_jtag_newtap,
802 .help = "Create a new TAP instance named basename.tap_type, "
803 "and appends it to the scan chain.",
804 .usage = "basename tap_type '-irlen' count "
805 "['-enable'|'-disable'] "
806 "['-expected_id' number] "
807 "['-ignore-version'] "
808 "['-ignore-bypass'] "
809 "['-ircapture' number] "
810 "['-mask' number]",
811 },
812 {
813 .name = "tapisenabled",
814 .mode = COMMAND_EXEC,
815 .jim_handler = jim_jtag_tap_enabler,
816 .help = "Returns a Tcl boolean (0/1) indicating whether "
817 "the TAP is enabled (1) or not (0).",
818 .usage = "tap_name",
819 },
820 {
821 .name = "tapenable",
822 .mode = COMMAND_EXEC,
823 .jim_handler = jim_jtag_tap_enabler,
824 .help = "Try to enable the specified TAP using the "
825 "'tap-enable' TAP event.",
826 .usage = "tap_name",
827 },
828 {
829 .name = "tapdisable",
830 .mode = COMMAND_EXEC,
831 .jim_handler = jim_jtag_tap_enabler,
832 .help = "Try to disable the specified TAP using the "
833 "'tap-disable' TAP event.",
834 .usage = "tap_name",
835 },
836 {
837 .name = "configure",
838 .mode = COMMAND_ANY,
839 .jim_handler = jim_jtag_configure,
840 .help = "Provide a Tcl handler for the specified "
841 "TAP event.",
842 .usage = "tap_name '-event' event_name handler",
843 },
844 {
845 .name = "cget",
846 .mode = COMMAND_EXEC,
847 .jim_handler = jim_jtag_configure,
848 .help = "Return any Tcl handler for the specified "
849 "TAP event.",
850 .usage = "tap_name '-event' event_name",
851 },
852 {
853 .name = "names",
854 .mode = COMMAND_ANY,
855 .handler = handle_jtag_names,
856 .help = "Returns list of all JTAG tap names.",
857 .usage = "",
858 },
859 {
860 .chain = jtag_command_handlers_to_move,
861 },
862 COMMAND_REGISTRATION_DONE
863 };
864
865 void jtag_notify_event(enum jtag_event event)
866 {
867 struct jtag_tap *tap;
868
869 for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
870 jtag_tap_handle_event(tap, event);
871 }
872
873
874 COMMAND_HANDLER(handle_scan_chain_command)
875 {
876 struct jtag_tap *tap;
877 char expected_id[12];
878
879 tap = jtag_all_taps();
880 command_print(CMD,
881 " TapName Enabled IdCode Expected IrLen IrCap IrMask");
882 command_print(CMD,
883 "-- ------------------- -------- ---------- ---------- ----- ----- ------");
884
885 while (tap) {
886 uint32_t expected, expected_mask, ii;
887
888 snprintf(expected_id, sizeof(expected_id), "0x%08x",
889 (unsigned)((tap->expected_ids_cnt > 0)
890 ? tap->expected_ids[0]
891 : 0));
892 if (tap->ignore_version)
893 expected_id[2] = '*';
894
895 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
896 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
897
898 command_print(CMD,
899 "%2d %-18s %c 0x%08x %s %5d 0x%02x 0x%02x",
900 tap->abs_chain_position,
901 tap->dotted_name,
902 tap->enabled ? 'Y' : 'n',
903 (unsigned int)(tap->idcode),
904 expected_id,
905 (unsigned int)(tap->ir_length),
906 (unsigned int)(expected),
907 (unsigned int)(expected_mask));
908
909 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
910 snprintf(expected_id, sizeof(expected_id), "0x%08x",
911 (unsigned) tap->expected_ids[ii]);
912 if (tap->ignore_version)
913 expected_id[2] = '*';
914
915 command_print(CMD,
916 " %s",
917 expected_id);
918 }
919
920 tap = tap->next_tap;
921 }
922
923 return ERROR_OK;
924 }
925
926 COMMAND_HANDLER(handle_jtag_ntrst_delay_command)
927 {
928 if (CMD_ARGC > 1)
929 return ERROR_COMMAND_SYNTAX_ERROR;
930 if (CMD_ARGC == 1) {
931 unsigned delay;
932 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
933
934 jtag_set_ntrst_delay(delay);
935 }
936 command_print(CMD, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
937 return ERROR_OK;
938 }
939
940 COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command)
941 {
942 if (CMD_ARGC > 1)
943 return ERROR_COMMAND_SYNTAX_ERROR;
944 if (CMD_ARGC == 1) {
945 unsigned delay;
946 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
947
948 jtag_set_ntrst_assert_width(delay);
949 }
950 command_print(CMD, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
951 return ERROR_OK;
952 }
953
954 COMMAND_HANDLER(handle_jtag_rclk_command)
955 {
956 if (CMD_ARGC > 1)
957 return ERROR_COMMAND_SYNTAX_ERROR;
958
959 int retval = ERROR_OK;
960 if (CMD_ARGC == 1) {
961 unsigned khz = 0;
962 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
963
964 retval = adapter_config_rclk(khz);
965 if (retval != ERROR_OK)
966 return retval;
967 }
968
969 int cur_khz = adapter_get_speed_khz();
970 retval = adapter_get_speed_readable(&cur_khz);
971 if (retval != ERROR_OK)
972 return retval;
973
974 if (cur_khz)
975 command_print(CMD, "RCLK not supported - fallback to %d kHz", cur_khz);
976 else
977 command_print(CMD, "RCLK - adaptive");
978
979 return retval;
980 }
981
982 COMMAND_HANDLER(handle_runtest_command)
983 {
984 if (CMD_ARGC != 1)
985 return ERROR_COMMAND_SYNTAX_ERROR;
986
987 unsigned num_clocks;
988 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num_clocks);
989
990 jtag_add_runtest(num_clocks, TAP_IDLE);
991 return jtag_execute_queue();
992 }
993
994 /*
995 * For "irscan" or "drscan" commands, the "end" (really, "next") state
996 * should be stable ... and *NOT* a shift state, otherwise free-running
997 * jtag clocks could change the values latched by the update state.
998 * Not surprisingly, this is the same constraint as SVF; the "irscan"
999 * and "drscan" commands are a write-only subset of what SVF provides.
1000 */
1001
1002 COMMAND_HANDLER(handle_irscan_command)
1003 {
1004 int i;
1005 struct scan_field *fields;
1006 struct jtag_tap *tap = NULL;
1007 tap_state_t endstate;
1008
1009 if ((CMD_ARGC < 2) || (CMD_ARGC % 2))
1010 return ERROR_COMMAND_SYNTAX_ERROR;
1011
1012 /* optional "-endstate" "statename" at the end of the arguments,
1013 * so that e.g. IRPAUSE can let us load the data register before
1014 * entering RUN/IDLE to execute the instruction we load here.
1015 */
1016 endstate = TAP_IDLE;
1017
1018 if (CMD_ARGC >= 4) {
1019 /* have at least one pair of numbers.
1020 * is last pair the magic text? */
1021 if (strcmp("-endstate", CMD_ARGV[CMD_ARGC - 2]) == 0) {
1022 endstate = tap_state_by_name(CMD_ARGV[CMD_ARGC - 1]);
1023 if (endstate == TAP_INVALID)
1024 return ERROR_COMMAND_SYNTAX_ERROR;
1025 if (!scan_is_safe(endstate))
1026 LOG_WARNING("unstable irscan endstate \"%s\"",
1027 CMD_ARGV[CMD_ARGC - 1]);
1028 CMD_ARGC -= 2;
1029 }
1030 }
1031
1032 int num_fields = CMD_ARGC / 2;
1033 if (num_fields > 1) {
1034 /* we really should be looking at plain_ir_scan if we want
1035 * anything more fancy.
1036 */
1037 LOG_ERROR("Specify a single value for tap");
1038 return ERROR_COMMAND_SYNTAX_ERROR;
1039 }
1040
1041 fields = calloc(num_fields, sizeof(*fields));
1042
1043 int retval;
1044 for (i = 0; i < num_fields; i++) {
1045 tap = jtag_tap_by_string(CMD_ARGV[i*2]);
1046 if (!tap) {
1047 free(fields);
1048 command_print(CMD, "Tap: %s unknown", CMD_ARGV[i*2]);
1049
1050 return ERROR_FAIL;
1051 }
1052 uint64_t value;
1053 retval = parse_u64(CMD_ARGV[i * 2 + 1], &value);
1054 if (retval != ERROR_OK)
1055 goto error_return;
1056
1057 int field_size = tap->ir_length;
1058 fields[i].num_bits = field_size;
1059 uint8_t *v = calloc(1, DIV_ROUND_UP(field_size, 8));
1060 if (!v) {
1061 LOG_ERROR("Out of memory");
1062 goto error_return;
1063 }
1064
1065 buf_set_u64(v, 0, field_size, value);
1066 fields[i].out_value = v;
1067 fields[i].in_value = NULL;
1068 }
1069
1070 /* did we have an endstate? */
1071 jtag_add_ir_scan(tap, fields, endstate);
1072
1073 retval = jtag_execute_queue();
1074
1075 error_return:
1076 for (i = 0; i < num_fields; i++)
1077 free((void *)fields[i].out_value);
1078
1079 free(fields);
1080
1081 return retval;
1082 }
1083
1084 COMMAND_HANDLER(handle_verify_ircapture_command)
1085 {
1086 if (CMD_ARGC > 1)
1087 return ERROR_COMMAND_SYNTAX_ERROR;
1088
1089 if (CMD_ARGC == 1) {
1090 bool enable;
1091 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1092 jtag_set_verify_capture_ir(enable);
1093 }
1094
1095 const char *status = jtag_will_verify_capture_ir() ? "enabled" : "disabled";
1096 command_print(CMD, "verify Capture-IR is %s", status);
1097
1098 return ERROR_OK;
1099 }
1100
1101 COMMAND_HANDLER(handle_verify_jtag_command)
1102 {
1103 if (CMD_ARGC > 1)
1104 return ERROR_COMMAND_SYNTAX_ERROR;
1105
1106 if (CMD_ARGC == 1) {
1107 bool enable;
1108 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1109 jtag_set_verify(enable);
1110 }
1111
1112 const char *status = jtag_will_verify() ? "enabled" : "disabled";
1113 command_print(CMD, "verify jtag capture is %s", status);
1114
1115 return ERROR_OK;
1116 }
1117
1118 COMMAND_HANDLER(handle_tms_sequence_command)
1119 {
1120 if (CMD_ARGC > 1)
1121 return ERROR_COMMAND_SYNTAX_ERROR;
1122
1123 if (CMD_ARGC == 1) {
1124 bool use_new_table;
1125 if (strcmp(CMD_ARGV[0], "short") == 0)
1126 use_new_table = true;
1127 else if (strcmp(CMD_ARGV[0], "long") == 0)
1128 use_new_table = false;
1129 else
1130 return ERROR_COMMAND_SYNTAX_ERROR;
1131
1132 tap_use_new_tms_table(use_new_table);
1133 }
1134
1135 command_print(CMD, "tms sequence is %s",
1136 tap_uses_new_tms_table() ? "short" : "long");
1137
1138 return ERROR_OK;
1139 }
1140
1141 COMMAND_HANDLER(handle_jtag_flush_queue_sleep)
1142 {
1143 if (CMD_ARGC != 1)
1144 return ERROR_COMMAND_SYNTAX_ERROR;
1145
1146 int sleep_ms;
1147 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], sleep_ms);
1148
1149 jtag_set_flush_queue_sleep(sleep_ms);
1150
1151 return ERROR_OK;
1152 }
1153
1154 COMMAND_HANDLER(handle_wait_srst_deassert)
1155 {
1156 if (CMD_ARGC != 1)
1157 return ERROR_COMMAND_SYNTAX_ERROR;
1158
1159 int timeout_ms;
1160 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], timeout_ms);
1161 if ((timeout_ms <= 0) || (timeout_ms > 100000)) {
1162 LOG_ERROR("Timeout must be an integer between 0 and 100000");
1163 return ERROR_FAIL;
1164 }
1165
1166 LOG_USER("Waiting for srst assert + deassert for at most %dms", timeout_ms);
1167 int asserted_yet;
1168 int64_t then = timeval_ms();
1169 while (jtag_srst_asserted(&asserted_yet) == ERROR_OK) {
1170 if ((timeval_ms() - then) > timeout_ms) {
1171 LOG_ERROR("Timed out");
1172 return ERROR_FAIL;
1173 }
1174 if (asserted_yet)
1175 break;
1176 }
1177 while (jtag_srst_asserted(&asserted_yet) == ERROR_OK) {
1178 if ((timeval_ms() - then) > timeout_ms) {
1179 LOG_ERROR("Timed out");
1180 return ERROR_FAIL;
1181 }
1182 if (!asserted_yet)
1183 break;
1184 }
1185
1186 return ERROR_OK;
1187 }
1188
1189 static const struct command_registration jtag_command_handlers[] = {
1190
1191 {
1192 .name = "jtag_flush_queue_sleep",
1193 .handler = handle_jtag_flush_queue_sleep,
1194 .mode = COMMAND_ANY,
1195 .help = "For debug purposes(simulate long delays of interface) "
1196 "to test performance or change in behavior. Default 0ms.",
1197 .usage = "[sleep in ms]",
1198 },
1199 {
1200 .name = "jtag_rclk",
1201 .handler = handle_jtag_rclk_command,
1202 .mode = COMMAND_ANY,
1203 .help = "With an argument, change to to use adaptive clocking "
1204 "if possible; else to use the fallback speed. "
1205 "With or without argument, display current setting.",
1206 .usage = "[fallback_speed_khz]",
1207 },
1208 {
1209 .name = "jtag_ntrst_delay",
1210 .handler = handle_jtag_ntrst_delay_command,
1211 .mode = COMMAND_ANY,
1212 .help = "delay after deasserting trst in ms",
1213 .usage = "[milliseconds]",
1214 },
1215 {
1216 .name = "jtag_ntrst_assert_width",
1217 .handler = handle_jtag_ntrst_assert_width_command,
1218 .mode = COMMAND_ANY,
1219 .help = "delay after asserting trst in ms",
1220 .usage = "[milliseconds]",
1221 },
1222 {
1223 .name = "scan_chain",
1224 .handler = handle_scan_chain_command,
1225 .mode = COMMAND_ANY,
1226 .help = "print current scan chain configuration",
1227 .usage = ""
1228 },
1229 {
1230 .name = "runtest",
1231 .handler = handle_runtest_command,
1232 .mode = COMMAND_EXEC,
1233 .help = "Move to Run-Test/Idle, and issue TCK for num_cycles.",
1234 .usage = "num_cycles"
1235 },
1236 {
1237 .name = "irscan",
1238 .handler = handle_irscan_command,
1239 .mode = COMMAND_EXEC,
1240 .help = "Execute Instruction Register (IR) scan. The "
1241 "specified opcodes are put into each TAP's IR, "
1242 "and other TAPs are put in BYPASS.",
1243 .usage = "[tap_name instruction]* ['-endstate' state_name]",
1244 },
1245 {
1246 .name = "verify_ircapture",
1247 .handler = handle_verify_ircapture_command,
1248 .mode = COMMAND_ANY,
1249 .help = "Display or assign flag controlling whether to "
1250 "verify values captured during Capture-IR.",
1251 .usage = "['enable'|'disable']",
1252 },
1253 {
1254 .name = "verify_jtag",
1255 .handler = handle_verify_jtag_command,
1256 .mode = COMMAND_ANY,
1257 .help = "Display or assign flag controlling whether to "
1258 "verify values captured during IR and DR scans.",
1259 .usage = "['enable'|'disable']",
1260 },
1261 {
1262 .name = "tms_sequence",
1263 .handler = handle_tms_sequence_command,
1264 .mode = COMMAND_ANY,
1265 .help = "Display or change what style TMS sequences to use "
1266 "for JTAG state transitions: short (default) or "
1267 "long. Only for working around JTAG bugs.",
1268 /* Specifically for working around DRIVER bugs... */
1269 .usage = "['short'|'long']",
1270 },
1271 {
1272 .name = "wait_srst_deassert",
1273 .handler = handle_wait_srst_deassert,
1274 .mode = COMMAND_ANY,
1275 .help = "Wait for an SRST deassert. "
1276 "Useful for cases where you need something to happen within ms "
1277 "of an srst deassert. Timeout in ms",
1278 .usage = "ms",
1279 },
1280 {
1281 .name = "jtag",
1282 .mode = COMMAND_ANY,
1283 .help = "perform jtag tap actions",
1284 .usage = "",
1285
1286 .chain = jtag_subcommand_handlers,
1287 },
1288 {
1289 .chain = jtag_command_handlers_to_move,
1290 },
1291 COMMAND_REGISTRATION_DONE
1292 };
1293
1294 int jtag_register_commands(struct command_context *cmd_ctx)
1295 {
1296 return register_commands(cmd_ctx, NULL, jtag_command_handlers);
1297 }

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)