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

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)