Steve Grubb <sgrubb@redhat.com> fix various and sundry leaks
[openocd.git] / src / jtag / tcl.c
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 √ėyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2009 SoftPLC Corporation *
9 * http://softplc.com *
10 * dick@softplc.com *
11 * *
12 * Copyright (C) 2009 Zachary T Welch *
13 * zw@superlucidity.net *
14 * *
15 * This program is free software; you can redistribute it and/or modify *
16 * it under the terms of the GNU General Public License as published by *
17 * the Free Software Foundation; either version 2 of the License, or *
18 * (at your option) any later version. *
19 * *
20 * This program is distributed in the hope that it will be useful, *
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
23 * GNU General Public License for more details. *
24 * *
25 * You should have received a copy of the GNU General Public License *
26 * along with this program; if not, write to the *
27 * Free Software Foundation, Inc., *
28 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
29 ***************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include "jtag.h"
35 #include "minidriver.h"
36 #include "interface.h"
37 #include "interfaces.h"
38
39 #ifdef HAVE_STRINGS_H
40 #include <strings.h>
41 #endif
42
43 static const Jim_Nvp nvp_jtag_tap_event[] = {
44 { .value = JTAG_TAP_EVENT_ENABLE, .name = "tap-enable" },
45 { .value = JTAG_TAP_EVENT_DISABLE, .name = "tap-disable" },
46
47 { .name = NULL, .value = -1 }
48 };
49
50 extern jtag_interface_t *jtag_interface;
51
52 /* jtag commands */
53 static int handle_interface_list_command(struct command_context_s *cmd_ctx,
54 char *cmd, char **args, int argc);
55 static int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
56 static int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
57 static int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
58 static int handle_jtag_rclk_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
59 static int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
60 static int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
61 static int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
62 static int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
63
64 static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
65
66 static int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
67 static int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
68 static int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
69 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
70 static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
71 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args);
72
73 static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
74 static int handle_verify_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
75 static int handle_tms_sequence_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
76
77 extern int jtag_examine_chain(void);
78 extern int jtag_validate_chain(void);
79
80 enum jtag_tap_cfg_param {
81 JCFG_EVENT
82 };
83
84 static Jim_Nvp nvp_config_opts[] = {
85 { .name = "-event", .value = JCFG_EVENT },
86
87 { .name = NULL, .value = -1 }
88 };
89
90 static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, jtag_tap_t * tap)
91 {
92 Jim_Nvp *n;
93 Jim_Obj *o;
94 int e;
95
96 /* parse config or cget options */
97 while (goi->argc > 0) {
98 Jim_SetEmptyResult (goi->interp);
99
100 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
101 if (e != JIM_OK) {
102 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
103 return e;
104 }
105
106 switch (n->value) {
107 case JCFG_EVENT:
108 if (goi->argc == 0) {
109 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
110 return JIM_ERR;
111 }
112
113 e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
114 if (e != JIM_OK) {
115 Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
116 return e;
117 }
118
119 if (goi->isconfigure) {
120 if (goi->argc != 1) {
121 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
122 return JIM_ERR;
123 }
124 } else {
125 if (goi->argc != 0) {
126 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
127 return JIM_ERR;
128 }
129 }
130
131 {
132 jtag_tap_event_action_t *jteap;
133
134 jteap = tap->event_action;
135 /* replace existing? */
136 while (jteap) {
137 if (jteap->event == (enum jtag_event)n->value) {
138 break;
139 }
140 jteap = jteap->next;
141 }
142
143 if (goi->isconfigure) {
144 if (jteap == NULL) {
145 /* create new */
146 jteap = calloc(1, sizeof (*jteap));
147 }
148 jteap->event = n->value;
149 Jim_GetOpt_Obj(goi, &o);
150 if (jteap->body) {
151 Jim_DecrRefCount(interp, jteap->body);
152 }
153 jteap->body = Jim_DuplicateObj(goi->interp, o);
154 Jim_IncrRefCount(jteap->body);
155
156 /* add to head of event list */
157 jteap->next = tap->event_action;
158 tap->event_action = jteap;
159 Jim_SetEmptyResult(goi->interp);
160 } else {
161 /* get */
162 if (jteap == NULL) {
163 Jim_SetEmptyResult(goi->interp);
164 } else {
165 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, jteap->body));
166 }
167 }
168 }
169 /* loop for more */
170 break;
171 }
172 } /* while (goi->argc) */
173
174 return JIM_OK;
175 }
176
177 static int is_bad_irval(int ir_length, jim_wide w)
178 {
179 jim_wide v = 1;
180
181 v <<= ir_length;
182 v -= 1;
183 v = ~v;
184 return (w & v) != 0;
185 }
186
187 static int jim_newtap_cmd(Jim_GetOptInfo *goi)
188 {
189 jtag_tap_t *pTap;
190 jim_wide w;
191 int x;
192 int e;
193 int reqbits;
194 Jim_Nvp *n;
195 char *cp;
196 const Jim_Nvp opts[] = {
197 #define NTAP_OPT_IRLEN 0
198 { .name = "-irlen" , .value = NTAP_OPT_IRLEN },
199 #define NTAP_OPT_IRMASK 1
200 { .name = "-irmask" , .value = NTAP_OPT_IRMASK },
201 #define NTAP_OPT_IRCAPTURE 2
202 { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE },
203 #define NTAP_OPT_ENABLED 3
204 { .name = "-enable" , .value = NTAP_OPT_ENABLED },
205 #define NTAP_OPT_DISABLED 4
206 { .name = "-disable" , .value = NTAP_OPT_DISABLED },
207 #define NTAP_OPT_EXPECTED_ID 5
208 { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID },
209 { .name = NULL , .value = -1 },
210 };
211
212 pTap = calloc(1, sizeof(jtag_tap_t));
213 if (!pTap) {
214 Jim_SetResult_sprintf(goi->interp, "no memory");
215 return JIM_ERR;
216 }
217
218 /*
219 * we expect CHIP + TAP + OPTIONS
220 * */
221 if (goi->argc < 3) {
222 Jim_SetResult_sprintf(goi->interp, "Missing CHIP TAP OPTIONS ....");
223 free(pTap);
224 return JIM_ERR;
225 }
226 Jim_GetOpt_String(goi, &cp, NULL);
227 pTap->chip = strdup(cp);
228
229 Jim_GetOpt_String(goi, &cp, NULL);
230 pTap->tapname = strdup(cp);
231
232 /* name + dot + name + null */
233 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
234 cp = malloc(x);
235 sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
236 pTap->dotted_name = cp;
237
238 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
239 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
240
241 /* deal with options */
242 #define NTREQ_IRLEN 1
243 #define NTREQ_IRCAPTURE 2
244 #define NTREQ_IRMASK 4
245
246 /* clear them as we find them */
247 reqbits = (NTREQ_IRLEN | NTREQ_IRCAPTURE | NTREQ_IRMASK);
248
249 while (goi->argc) {
250 e = Jim_GetOpt_Nvp(goi, opts, &n);
251 if (e != JIM_OK) {
252 Jim_GetOpt_NvpUnknown(goi, opts, 0);
253 free((void *)pTap->dotted_name);
254 free(pTap);
255 return e;
256 }
257 LOG_DEBUG("Processing option: %s", n->name);
258 switch (n->value) {
259 case NTAP_OPT_ENABLED:
260 pTap->disabled_after_reset = false;
261 break;
262 case NTAP_OPT_DISABLED:
263 pTap->disabled_after_reset = true;
264 break;
265 case NTAP_OPT_EXPECTED_ID:
266 {
267 uint32_t *new_expected_ids;
268
269 e = Jim_GetOpt_Wide(goi, &w);
270 if (e != JIM_OK) {
271 Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
272 free((void *)pTap->dotted_name);
273 free(pTap);
274 return e;
275 }
276
277 new_expected_ids = malloc(sizeof(uint32_t) * (pTap->expected_ids_cnt + 1));
278 if (new_expected_ids == NULL) {
279 Jim_SetResult_sprintf(goi->interp, "no memory");
280 free((void *)pTap->dotted_name);
281 free(pTap);
282 return JIM_ERR;
283 }
284
285 memcpy(new_expected_ids, pTap->expected_ids, sizeof(uint32_t) * pTap->expected_ids_cnt);
286
287 new_expected_ids[pTap->expected_ids_cnt] = w;
288
289 free(pTap->expected_ids);
290 pTap->expected_ids = new_expected_ids;
291 pTap->expected_ids_cnt++;
292 break;
293 }
294 case NTAP_OPT_IRLEN:
295 case NTAP_OPT_IRMASK:
296 case NTAP_OPT_IRCAPTURE:
297 e = Jim_GetOpt_Wide(goi, &w);
298 if (e != JIM_OK) {
299 Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
300 free((void *)pTap->dotted_name);
301 free(pTap);
302 return e;
303 }
304 switch (n->value) {
305 case NTAP_OPT_IRLEN:
306 if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value)))
307 LOG_WARNING("huge IR length %d", (int) w);
308 pTap->ir_length = w;
309 reqbits &= (~(NTREQ_IRLEN));
310 break;
311 case NTAP_OPT_IRMASK:
312 if (is_bad_irval(pTap->ir_length, w)) {
313 LOG_ERROR("IR mask %x too big",
314 (int) w);
315 free((void *)pTap->dotted_name);
316 free(pTap);
317 return ERROR_FAIL;
318 }
319 pTap->ir_capture_mask = w;
320 reqbits &= (~(NTREQ_IRMASK));
321 break;
322 case NTAP_OPT_IRCAPTURE:
323 if (is_bad_irval(pTap->ir_length, w)) {
324 LOG_ERROR("IR capture %x too big",
325 (int) w);
326 free((void *)pTap->dotted_name);
327 free(pTap);
328 return ERROR_FAIL;
329 }
330 pTap->ir_capture_value = w;
331 reqbits &= (~(NTREQ_IRCAPTURE));
332 break;
333 }
334 } /* switch (n->value) */
335 } /* while (goi->argc) */
336
337 /* default is enabled-after-reset */
338 pTap->enabled = !pTap->disabled_after_reset;
339
340 /* Did all the required option bits get cleared? */
341 if (0 == reqbits)
342 {
343 jtag_tap_init(pTap);
344 return ERROR_OK;
345 }
346
347 Jim_SetResult_sprintf(goi->interp,
348 "newtap: %s missing required parameters",
349 pTap->dotted_name);
350 jtag_tap_free(pTap);
351 return JIM_ERR;
352 }
353
354 static void jtag_tap_handle_event(jtag_tap_t *tap, enum jtag_event e)
355 {
356 jtag_tap_event_action_t * jteap;
357 int done;
358
359 jteap = tap->event_action;
360
361 done = 0;
362 while (jteap) {
363 if (jteap->event == e) {
364 done = 1;
365 LOG_DEBUG("JTAG tap: %s event: %d (%s) action: %s\n",
366 tap->dotted_name,
367 e,
368 Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e)->name,
369 Jim_GetString(jteap->body, NULL));
370 if (Jim_EvalObj(interp, jteap->body) != JIM_OK) {
371 Jim_PrintErrorMessage(interp);
372 } else {
373 /* NOTE: we currently assume the handlers
374 * can't fail. That presumes later code
375 * will be verifying the scan chains ...
376 */
377 tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
378 }
379 }
380
381 jteap = jteap->next;
382 }
383
384 if (!done) {
385 LOG_DEBUG("event %d %s - no action",
386 e,
387 Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e)->name);
388 }
389 }
390
391
392 static int jim_jtag_command(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
393 {
394 Jim_GetOptInfo goi;
395 int e;
396 Jim_Nvp *n;
397 Jim_Obj *o;
398 struct command_context_s *context;
399
400 enum {
401 JTAG_CMD_INTERFACE,
402 JTAG_CMD_INIT_RESET,
403 JTAG_CMD_NEWTAP,
404 JTAG_CMD_TAPENABLE,
405 JTAG_CMD_TAPDISABLE,
406 JTAG_CMD_TAPISENABLED,
407 JTAG_CMD_CONFIGURE,
408 JTAG_CMD_CGET,
409 JTAG_CMD_NAMES,
410 };
411
412 const Jim_Nvp jtag_cmds[] = {
413 { .name = "interface" , .value = JTAG_CMD_INTERFACE },
414 { .name = "arp_init-reset", .value = JTAG_CMD_INIT_RESET },
415 { .name = "newtap" , .value = JTAG_CMD_NEWTAP },
416 { .name = "tapisenabled" , .value = JTAG_CMD_TAPISENABLED },
417 { .name = "tapenable" , .value = JTAG_CMD_TAPENABLE },
418 { .name = "tapdisable" , .value = JTAG_CMD_TAPDISABLE },
419 { .name = "configure" , .value = JTAG_CMD_CONFIGURE },
420 { .name = "cget" , .value = JTAG_CMD_CGET },
421 { .name = "names" , .value = JTAG_CMD_NAMES },
422
423 { .name = NULL, .value = -1 },
424 };
425
426 context = Jim_GetAssocData(interp, "context");
427 /* go past the command */
428 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
429
430 e = Jim_GetOpt_Nvp(&goi, jtag_cmds, &n);
431 if (e != JIM_OK) {
432 Jim_GetOpt_NvpUnknown(&goi, jtag_cmds, 0);
433 return e;
434 }
435 Jim_SetEmptyResult(goi.interp);
436 switch (n->value) {
437 case JTAG_CMD_INTERFACE:
438 /* return the name of the interface */
439 /* TCL code might need to know the exact type... */
440 /* FUTURE: we allow this as a means to "set" the interface. */
441 if (goi.argc != 0) {
442 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
443 return JIM_ERR;
444 }
445 Jim_SetResultString(goi.interp, jtag_interface->name, -1);
446 return JIM_OK;
447 case JTAG_CMD_INIT_RESET:
448 if (goi.argc != 0) {
449 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
450 return JIM_ERR;
451 }
452 e = jtag_init_reset(context);
453 if (e != ERROR_OK) {
454 Jim_SetResult_sprintf(goi.interp, "error: %d", e);
455 return JIM_ERR;
456 }
457 return JIM_OK;
458 case JTAG_CMD_NEWTAP:
459 return jim_newtap_cmd(&goi);
460 break;
461 case JTAG_CMD_TAPISENABLED:
462 case JTAG_CMD_TAPENABLE:
463 case JTAG_CMD_TAPDISABLE:
464 if (goi.argc != 1) {
465 Jim_SetResultString(goi.interp, "Too many parameters",-1);
466 return JIM_ERR;
467 }
468
469 {
470 jtag_tap_t *t;
471
472 t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
473 if (t == NULL)
474 return JIM_ERR;
475
476 switch (n->value) {
477 case JTAG_CMD_TAPISENABLED:
478 break;
479 case JTAG_CMD_TAPENABLE:
480 if (t->enabled)
481 break;
482 jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
483 if (!t->enabled)
484 break;
485
486 /* FIXME add JTAG sanity checks, w/o TLR
487 * - scan chain length grew by one (this)
488 * - IDs and IR lengths are as expected
489 */
490
491 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
492 break;
493 case JTAG_CMD_TAPDISABLE:
494 if (!t->enabled)
495 break;
496 jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
497 if (t->enabled)
498 break;
499
500 /* FIXME add JTAG sanity checks, w/o TLR
501 * - scan chain length shrank by one (this)
502 * - IDs and IR lengths are as expected
503 */
504
505 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
506 break;
507 }
508 e = t->enabled;
509 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
510 return JIM_OK;
511 }
512 break;
513
514 case JTAG_CMD_CGET:
515 if (goi.argc < 2) {
516 Jim_WrongNumArgs(goi.interp, 0, NULL,
517 "cget tap_name queryparm");
518 return JIM_ERR;
519 }
520
521 {
522 jtag_tap_t *t;
523
524 Jim_GetOpt_Obj(&goi, &o);
525 t = jtag_tap_by_jim_obj(goi.interp, o);
526 if (t == NULL) {
527 return JIM_ERR;
528 }
529
530 goi.isconfigure = 0;
531 return jtag_tap_configure_cmd(&goi, t);
532 }
533 break;
534
535 case JTAG_CMD_CONFIGURE:
536 if (goi.argc < 3) {
537 Jim_WrongNumArgs(goi.interp, 0, NULL,
538 "configure tap_name attribute value ...");
539 return JIM_ERR;
540 }
541
542 {
543 jtag_tap_t *t;
544
545 Jim_GetOpt_Obj(&goi, &o);
546 t = jtag_tap_by_jim_obj(goi.interp, o);
547 if (t == NULL) {
548 return JIM_ERR;
549 }
550
551 goi.isconfigure = 1;
552 return jtag_tap_configure_cmd(&goi, t);
553 }
554 break;
555
556 case JTAG_CMD_NAMES:
557 if (goi.argc != 0) {
558 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
559 return JIM_ERR;
560 }
561 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
562 {
563 jtag_tap_t *tap;
564
565 for (tap = jtag_all_taps(); tap; tap = tap->next_tap) {
566 Jim_ListAppendElement(goi.interp,
567 Jim_GetResult(goi.interp),
568 Jim_NewStringObj(goi.interp,
569 tap->dotted_name, -1));
570 }
571 return JIM_OK;
572 }
573 break;
574
575 }
576
577 return JIM_ERR;
578 }
579
580 int jtag_register_commands(struct command_context_s *cmd_ctx)
581 {
582 register_jim(cmd_ctx, "jtag", jim_jtag_command, "perform jtag tap actions");
583
584 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
585 COMMAND_CONFIG, "try to configure interface");
586 register_command(cmd_ctx, NULL,
587 "interface_list", &handle_interface_list_command,
588 COMMAND_ANY, "list all built-in interfaces");
589 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
590 COMMAND_ANY, "(DEPRECATED) set jtag speed (if supported)");
591 register_command(cmd_ctx, NULL, "jtag_khz", handle_jtag_khz_command,
592 COMMAND_ANY, "set maximum jtag speed (if supported); "
593 "parameter is maximum khz, or 0 for adaptive clocking (RTCK).");
594 register_command(cmd_ctx, NULL, "jtag_rclk", handle_jtag_rclk_command,
595 COMMAND_ANY, "fallback_speed_khz - set JTAG speed to RCLK or use fallback speed");
596 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
597 COMMAND_CONFIG, "(DEPRECATED) jtag_device <ir_length> <ir_expected> <ir_mask>");
598 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
599 COMMAND_ANY,
600 "[none/trst_only/srst_only/trst_and_srst] [srst_pulls_trst/trst_pulls_srst] [combined/separate] [trst_push_pull/trst_open_drain] [srst_push_pull/srst_open_drain]");
601 register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
602 COMMAND_ANY, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
603 register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
604 COMMAND_ANY, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
605
606 register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
607 COMMAND_EXEC, "print current scan chain configuration");
608
609 register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
610 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
611 register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
612 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
613 register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
614 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
615 register_jim(cmd_ctx, "drscan", Jim_Command_drscan, "execute DR scan <device> <num_bits> <value> <num_bits1> <value2> ...");
616 register_jim(cmd_ctx, "flush_count", Jim_Command_flush_count, "returns number of times the JTAG queue has been flushed");
617 register_jim(cmd_ctx, "pathmove", Jim_Command_pathmove, "move JTAG to state1 then to state2, state3, etc. <state1>,<state2>,<stat3>...");
618
619 register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
620 COMMAND_ANY, "verify value captured during Capture-IR <enable | disable>");
621 register_command(cmd_ctx, NULL, "verify_jtag", handle_verify_jtag_command,
622 COMMAND_ANY, "verify value capture <enable | disable>");
623 register_command(cmd_ctx, NULL, "tms_sequence", handle_tms_sequence_command,
624 COMMAND_ANY, "choose short(default) or long tms_sequence <short | long>");
625 return ERROR_OK;
626 }
627
628 static int default_khz(int khz, int *jtag_speed)
629 {
630 LOG_ERROR("Translation from khz to jtag_speed not implemented");
631 return ERROR_FAIL;
632 }
633
634 static int default_speed_div(int speed, int *khz)
635 {
636 LOG_ERROR("Translation from jtag_speed to khz not implemented");
637 return ERROR_FAIL;
638 }
639
640 static int default_power_dropout(int *dropout)
641 {
642 *dropout = 0; /* by default we can't detect power dropout */
643 return ERROR_OK;
644 }
645
646 static int default_srst_asserted(int *srst_asserted)
647 {
648 *srst_asserted = 0; /* by default we can't detect srst asserted */
649 return ERROR_OK;
650 }
651
652 static int handle_interface_command(struct command_context_s *cmd_ctx,
653 char *cmd, char **args, int argc)
654 {
655 /* check whether the interface is already configured */
656 if (jtag_interface)
657 {
658 LOG_WARNING("Interface already configured, ignoring");
659 return ERROR_OK;
660 }
661
662 /* interface name is a mandatory argument */
663 if (argc != 1 || args[0][0] == '\0')
664 return ERROR_COMMAND_SYNTAX_ERROR;
665
666 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
667 {
668 if (strcmp(args[0], jtag_interfaces[i]->name) != 0)
669 continue;
670
671 int retval = jtag_interfaces[i]->register_commands(cmd_ctx);
672 if (ERROR_OK != retval)
673 return retval;
674
675 jtag_interface = jtag_interfaces[i];
676
677 if (jtag_interface->khz == NULL)
678 jtag_interface->khz = default_khz;
679 if (jtag_interface->speed_div == NULL)
680 jtag_interface->speed_div = default_speed_div;
681 if (jtag_interface->power_dropout == NULL)
682 jtag_interface->power_dropout = default_power_dropout;
683 if (jtag_interface->srst_asserted == NULL)
684 jtag_interface->srst_asserted = default_srst_asserted;
685
686 return ERROR_OK;
687 }
688
689 /* no valid interface was found (i.e. the configuration option,
690 * didn't match one of the compiled-in interfaces
691 */
692 LOG_ERROR("The specified JTAG interface was not found (%s)", args[0]);
693 handle_interface_list_command(cmd_ctx, cmd, args, argc);
694 return ERROR_JTAG_INVALID_INTERFACE;
695 }
696
697 static int handle_interface_list_command(struct command_context_s *cmd_ctx,
698 char *cmd, char **args, int argc)
699 {
700 if (strcmp(cmd, "interface_list") == 0 && argc > 0)
701 return ERROR_COMMAND_SYNTAX_ERROR;
702
703 command_print(cmd_ctx, "The following JTAG interfaces are available:");
704 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
705 {
706 const char *name = jtag_interfaces[i]->name;
707 command_print(cmd_ctx, "%u: %s", i + 1, name);
708 }
709
710 return ERROR_OK;
711 }
712
713 static int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
714 {
715 int e;
716 char buf[1024];
717 Jim_Obj *newargs[ 10 ];
718 /*
719 * CONVERT SYNTAX
720 * argv[-1] = command
721 * argv[ 0] = ir length
722 * argv[ 1] = ir capture
723 * argv[ 2] = ir mask
724 * argv[ 3] = not actually used by anything but in the docs
725 */
726
727 if (argc < 4) {
728 command_print(cmd_ctx, "OLD DEPRECATED SYNTAX: Please use the NEW syntax");
729 return ERROR_OK;
730 }
731 command_print(cmd_ctx, "OLD SYNTAX: DEPRECATED - translating to new syntax");
732 command_print(cmd_ctx, "jtag newtap CHIP TAP -irlen %s -ircapture %s -irvalue %s",
733 args[0],
734 args[1],
735 args[2]);
736 command_print(cmd_ctx, "Example: STM32 has 2 taps, the cortexM3(len4) + boundaryscan(len5)");
737 command_print(cmd_ctx, "jtag newtap stm32 cortexm3 ....., thus creating the tap: \"stm32.cortexm3\"");
738 command_print(cmd_ctx, "jtag newtap stm32 boundary ....., and the tap: \"stm32.boundary\"");
739 command_print(cmd_ctx, "And then refer to the taps by the dotted name.");
740
741 newargs[0] = Jim_NewStringObj(interp, "jtag", -1);
742 newargs[1] = Jim_NewStringObj(interp, "newtap", -1);
743 sprintf(buf, "chip%d", jtag_tap_count());
744 newargs[2] = Jim_NewStringObj(interp, buf, -1);
745 sprintf(buf, "tap%d", jtag_tap_count());
746 newargs[3] = Jim_NewStringObj(interp, buf, -1);
747 newargs[4] = Jim_NewStringObj(interp, "-irlen", -1);
748 newargs[5] = Jim_NewStringObj(interp, args[0], -1);
749 newargs[6] = Jim_NewStringObj(interp, "-ircapture", -1);
750 newargs[7] = Jim_NewStringObj(interp, args[1], -1);
751 newargs[8] = Jim_NewStringObj(interp, "-irmask", -1);
752 newargs[9] = Jim_NewStringObj(interp, args[2], -1);
753
754 command_print(cmd_ctx, "NEW COMMAND:");
755 sprintf(buf, "%s %s %s %s %s %s %s %s %s %s",
756 Jim_GetString(newargs[0], NULL),
757 Jim_GetString(newargs[1], NULL),
758 Jim_GetString(newargs[2], NULL),
759 Jim_GetString(newargs[3], NULL),
760 Jim_GetString(newargs[4], NULL),
761 Jim_GetString(newargs[5], NULL),
762 Jim_GetString(newargs[6], NULL),
763 Jim_GetString(newargs[7], NULL),
764 Jim_GetString(newargs[8], NULL),
765 Jim_GetString(newargs[9], NULL));
766
767 e = jim_jtag_command(interp, 10, newargs);
768 if (e != JIM_OK) {
769 command_print(cmd_ctx, "%s", Jim_GetString(Jim_GetResult(interp), NULL));
770 }
771 return e;
772 }
773
774 static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
775 {
776 jtag_tap_t *tap;
777
778 tap = jtag_all_taps();
779 command_print(cmd_ctx, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr ");
780 command_print(cmd_ctx, "---|--------------------|---------|------------|------------|------|------|------|---------");
781
782 while (tap) {
783 uint32_t expected, expected_mask, cur_instr, ii;
784 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
785 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
786 cur_instr = buf_get_u32(tap->cur_instr, 0, tap->ir_length);
787
788 command_print(cmd_ctx,
789 "%2d | %-18s | %c | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x",
790 tap->abs_chain_position,
791 tap->dotted_name,
792 tap->enabled ? 'Y' : 'n',
793 (unsigned int)(tap->idcode),
794 (unsigned int)(tap->expected_ids_cnt > 0 ? tap->expected_ids[0] : 0),
795 (unsigned int)(tap->ir_length),
796 (unsigned int)(expected),
797 (unsigned int)(expected_mask),
798 (unsigned int)(cur_instr));
799
800 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
801 command_print(cmd_ctx, " | | | | 0x%08x | | | | ",
802 (unsigned int)(tap->expected_ids[ii]));
803 }
804
805 tap = tap->next_tap;
806 }
807
808 return ERROR_OK;
809 }
810
811 static int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
812 {
813 int new_cfg = 0;
814 int mask = 0;
815
816 if (argc < 1)
817 return ERROR_COMMAND_SYNTAX_ERROR;
818
819 /* Original versions cared about the order of these tokens:
820 * reset_config signals [combination [trst_type [srst_type]]]
821 * They also clobbered the previous configuration even on error.
822 *
823 * Here we don't care about the order, and only change values
824 * which have been explicitly specified.
825 */
826 for (; argc; argc--, args++) {
827 int tmp = 0;
828 int m;
829
830 /* signals */
831 m = RESET_HAS_TRST | RESET_HAS_SRST;
832 if (strcmp(*args, "none") == 0)
833 tmp = RESET_NONE;
834 else if (strcmp(*args, "trst_only") == 0)
835 tmp = RESET_HAS_TRST;
836 else if (strcmp(*args, "srst_only") == 0)
837 tmp = RESET_HAS_SRST;
838 else if (strcmp(*args, "trst_and_srst") == 0)
839 tmp = RESET_HAS_TRST | RESET_HAS_SRST;
840 else
841 m = 0;
842 if (mask & m) {
843 LOG_ERROR("extra reset_config %s spec (%s)",
844 "signal", *args);
845 return ERROR_INVALID_ARGUMENTS;
846 }
847 if (m)
848 goto next;
849
850 /* combination (options for broken wiring) */
851 m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
852 if (strcmp(*args, "separate") == 0)
853 /* separate reset lines - default */;
854 else if (strcmp(*args, "srst_pulls_trst") == 0)
855 tmp |= RESET_SRST_PULLS_TRST;
856 else if (strcmp(*args, "trst_pulls_srst") == 0)
857 tmp |= RESET_TRST_PULLS_SRST;
858 else if (strcmp(*args, "combined") == 0)
859 tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
860 else
861 m = 0;
862 if (mask & m) {
863 LOG_ERROR("extra reset_config %s spec (%s)",
864 "combination", *args);
865 return ERROR_INVALID_ARGUMENTS;
866 }
867 if (m)
868 goto next;
869
870 /* trst_type (NOP without HAS_TRST) */
871 m = RESET_TRST_OPEN_DRAIN;
872 if (strcmp(*args, "trst_open_drain") == 0)
873 tmp |= RESET_TRST_OPEN_DRAIN;
874 else if (strcmp(*args, "trst_push_pull") == 0)
875 /* push/pull from adapter - default */;
876 else
877 m = 0;
878 if (mask & m) {
879 LOG_ERROR("extra reset_config %s spec (%s)",
880 "trst_type", *args);
881 return ERROR_INVALID_ARGUMENTS;
882 }
883 if (m)
884 goto next;
885
886 /* srst_type (NOP without HAS_SRST) */
887 m |= RESET_SRST_PUSH_PULL;
888 if (strcmp(*args, "srst_push_pull") == 0)
889 tmp |= RESET_SRST_PUSH_PULL;
890 else if (strcmp(*args, "srst_open_drain") == 0)
891 /* open drain from adapter - default */;
892 else
893 m = 0;
894 if (mask & m) {
895 LOG_ERROR("extra reset_config %s spec (%s)",
896 "srst_type", *args);
897 return ERROR_INVALID_ARGUMENTS;
898 }
899 if (m)
900 goto next;
901
902 /* caller provided nonsense; fail */
903 LOG_ERROR("unknown reset_config flag (%s)", *args);
904 return ERROR_INVALID_ARGUMENTS;
905
906 next:
907 /* Remember the bits which were specified (mask)
908 * and their new values (new_cfg).
909 */
910 mask |= m;
911 new_cfg |= tmp;
912 }
913
914 /* clear previous values of those bits, save new values */
915 enum reset_types old_cfg = jtag_get_reset_config();
916 old_cfg &= ~mask;
917 new_cfg |= old_cfg;
918 jtag_set_reset_config(new_cfg);
919
920 return ERROR_OK;
921 }
922
923 static int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx,
924 char *cmd, char **args, int argc)
925 {
926 if (argc > 1)
927 return ERROR_COMMAND_SYNTAX_ERROR;
928 if (argc == 1)
929 {
930 unsigned delay;
931 int retval = parse_uint(args[0], &delay);
932 if (ERROR_OK != retval)
933 return retval;
934 jtag_set_nsrst_delay(delay);
935 }
936 command_print(cmd_ctx, "jtag_nsrst_delay: %u", jtag_get_nsrst_delay());
937 return ERROR_OK;
938 }
939
940 static int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx,
941 char *cmd, char **args, int argc)
942 {
943 if (argc > 1)
944 return ERROR_COMMAND_SYNTAX_ERROR;
945 if (argc == 1)
946 {
947 unsigned delay;
948 int retval = parse_uint(args[0], &delay);
949 if (ERROR_OK != retval)
950 return retval;
951 jtag_set_ntrst_delay(delay);
952 }
953 command_print(cmd_ctx, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
954 return ERROR_OK;
955 }
956
957 static int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
958 {
959 int retval = ERROR_OK;
960
961 command_print(cmd_ctx, "OLD SYNTAX: DEPRECATED - "
962 "use jtag_khz, not jtag_speed");
963
964 if (argc > 1)
965 return ERROR_COMMAND_SYNTAX_ERROR;
966 if (argc == 1)
967 {
968 LOG_DEBUG("handle jtag speed");
969
970 unsigned cur_speed = 0;
971 int retval = parse_uint(args[0], &cur_speed);
972 if (ERROR_OK != retval)
973 return retval;
974 retval = jtag_config_speed(cur_speed);
975
976 }
977 command_print(cmd_ctx, "jtag_speed: %d", jtag_get_speed());
978
979 return retval;
980 }
981
982 static int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
983 {
984 if (argc > 1)
985 return ERROR_COMMAND_SYNTAX_ERROR;
986
987 int retval = ERROR_OK;
988 if (argc == 1)
989 {
990 unsigned khz = 0;
991 int retval = parse_uint(args[0], &khz);
992 if (ERROR_OK != retval)
993 return retval;
994 retval = jtag_config_khz(khz);
995 if (ERROR_OK != retval)
996 return retval;
997 }
998
999 int cur_speed = jtag_get_speed_khz();
1000 retval = jtag_get_speed_readable(&cur_speed);
1001 if (ERROR_OK != retval)
1002 return retval;
1003
1004 if (cur_speed)
1005 command_print(cmd_ctx, "%d kHz", cur_speed);
1006 else
1007 command_print(cmd_ctx, "RCLK - adaptive");
1008
1009 return retval;
1010 }
1011
1012 static int handle_jtag_rclk_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1013 {
1014 if (argc > 1)
1015 return ERROR_COMMAND_SYNTAX_ERROR;
1016
1017 int retval = ERROR_OK;
1018 if (argc == 1)
1019 {
1020 unsigned khz = 0;
1021 int retval = parse_uint(args[0], &khz);
1022 if (ERROR_OK != retval)
1023 return retval;
1024 retval = jtag_config_rclk(khz);
1025 if (ERROR_OK != retval)
1026 return retval;
1027 }
1028
1029 int cur_khz = jtag_get_speed_khz();
1030 retval = jtag_get_speed_readable(&cur_khz);
1031 if (ERROR_OK != retval)
1032 return retval;
1033
1034 if (cur_khz)
1035 command_print(cmd_ctx, "RCLK not supported - fallback to %d kHz", cur_khz);
1036 else
1037 command_print(cmd_ctx, "RCLK - adaptive");
1038
1039 return retval;
1040 }
1041
1042 static int handle_jtag_reset_command(struct command_context_s *cmd_ctx,
1043 char *cmd, char **args, int argc)
1044 {
1045 if (argc != 2)
1046 return ERROR_COMMAND_SYNTAX_ERROR;
1047
1048 int trst = -1;
1049 if (args[0][0] == '1')
1050 trst = 1;
1051 else if (args[0][0] == '0')
1052 trst = 0;
1053 else
1054 return ERROR_COMMAND_SYNTAX_ERROR;
1055
1056 int srst = -1;
1057 if (args[1][0] == '1')
1058 srst = 1;
1059 else if (args[1][0] == '0')
1060 srst = 0;
1061 else
1062 return ERROR_COMMAND_SYNTAX_ERROR;
1063
1064 if (jtag_interface_init(cmd_ctx) != ERROR_OK)
1065 return ERROR_JTAG_INIT_FAILED;
1066
1067 jtag_add_reset(trst, srst);
1068 return jtag_execute_queue();
1069 }
1070
1071 static int handle_runtest_command(struct command_context_s *cmd_ctx,
1072 char *cmd, char **args, int argc)
1073 {
1074 if (argc != 1)
1075 return ERROR_COMMAND_SYNTAX_ERROR;
1076
1077 unsigned num_clocks;
1078 int retval = parse_uint(args[0], &num_clocks);
1079 if (ERROR_OK != retval)
1080 return retval;
1081
1082 jtag_add_runtest(num_clocks, TAP_IDLE);
1083 return jtag_execute_queue();
1084 }
1085
1086 /*
1087 * For "irscan" or "drscan" commands, the "end" (really, "next") state
1088 * should be stable ... and *NOT* a shift state, otherwise free-running
1089 * jtag clocks could change the values latched by the update state.
1090 */
1091 static bool scan_is_safe(tap_state_t state)
1092 {
1093 switch (state)
1094 {
1095 case TAP_RESET:
1096 case TAP_IDLE:
1097 case TAP_DRPAUSE:
1098 case TAP_IRPAUSE:
1099 return true;
1100 default:
1101 return false;
1102 }
1103 }
1104
1105
1106 static int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1107 {
1108 int i;
1109 scan_field_t *fields;
1110 jtag_tap_t *tap;
1111 tap_state_t endstate;
1112
1113 if ((argc < 2) || (argc % 2))
1114 {
1115 return ERROR_COMMAND_SYNTAX_ERROR;
1116 }
1117
1118 /* optional "-endstate" "statename" at the end of the arguments,
1119 * so that e.g. IRPAUSE can let us load the data register before
1120 * entering RUN/IDLE to execute the instruction we load here.
1121 */
1122 endstate = TAP_IDLE;
1123
1124 if (argc >= 4) {
1125 /* have at least one pair of numbers. */
1126 /* is last pair the magic text? */
1127 if (0 == strcmp("-endstate", args[ argc - 2 ])) {
1128 const char *cpA;
1129 const char *cpS;
1130 cpA = args[ argc-1 ];
1131 for (endstate = 0 ; endstate < TAP_NUM_STATES ; endstate++) {
1132 cpS = tap_state_name(endstate);
1133 if (0 == strcmp(cpA, cpS)) {
1134 break;
1135 }
1136 }
1137 if (endstate >= TAP_NUM_STATES) {
1138 return ERROR_COMMAND_SYNTAX_ERROR;
1139 } else {
1140 if (!scan_is_safe(endstate))
1141 LOG_WARNING("irscan with unsafe "
1142 "endstate \"%s\"", cpA);
1143 /* found - remove the last 2 args */
1144 argc -= 2;
1145 }
1146 }
1147 }
1148
1149 int num_fields = argc / 2;
1150 size_t fields_len = sizeof(scan_field_t) * num_fields;
1151 fields = malloc(fields_len);
1152 memset(fields, 0, fields_len);
1153
1154 int retval;
1155 for (i = 0; i < num_fields; i++)
1156 {
1157 tap = jtag_tap_by_string(args[i*2]);
1158 if (tap == NULL)
1159 {
1160 int j;
1161 for (j = 0; j < i; j++)
1162 free(fields[j].out_value);
1163 free(fields);
1164 command_print(cmd_ctx, "Tap: %s unknown", args[i*2]);
1165
1166 return ERROR_FAIL;
1167 }
1168 int field_size = tap->ir_length;
1169 fields[i].tap = tap;
1170 fields[i].num_bits = field_size;
1171 fields[i].out_value = malloc(CEIL(field_size, 8));
1172
1173 uint32_t value;
1174 retval = parse_u32(args[i * 2 + 1], &value);
1175 if (ERROR_OK != retval)
1176 goto error_return;
1177 buf_set_u32(fields[i].out_value, 0, field_size, value);
1178 fields[i].in_value = NULL;
1179 }
1180
1181 /* did we have an endstate? */
1182 jtag_add_ir_scan(num_fields, fields, endstate);
1183
1184 retval = jtag_execute_queue();
1185
1186 error_return:
1187 for (i = 0; i < num_fields; i++)
1188 {
1189 if (NULL != fields[i].out_value)
1190 free(fields[i].out_value);
1191 }
1192
1193 free (fields);
1194
1195 return retval;
1196 }
1197
1198 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
1199 {
1200 int retval;
1201 scan_field_t *fields;
1202 int num_fields;
1203 int field_count = 0;
1204 int i, e;
1205 jtag_tap_t *tap;
1206 tap_state_t endstate;
1207
1208 /* args[1] = device
1209 * args[2] = num_bits
1210 * args[3] = hex string
1211 * ... repeat num bits and hex string ...
1212 *
1213 * .. optionally:
1214 * args[N-2] = "-endstate"
1215 * args[N-1] = statename
1216 */
1217 if ((argc < 4) || ((argc % 2) != 0))
1218 {
1219 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
1220 return JIM_ERR;
1221 }
1222
1223 endstate = TAP_IDLE;
1224
1225 script_debug(interp, "drscan", argc, args);
1226
1227 /* validate arguments as numbers */
1228 e = JIM_OK;
1229 for (i = 2; i < argc; i += 2)
1230 {
1231 long bits;
1232 const char *cp;
1233
1234 e = Jim_GetLong(interp, args[i], &bits);
1235 /* If valid - try next arg */
1236 if (e == JIM_OK) {
1237 continue;
1238 }
1239
1240 /* Not valid.. are we at the end? */
1241 if (((i + 2) != argc)) {
1242 /* nope, then error */
1243 return e;
1244 }
1245
1246 /* it could be: "-endstate FOO"
1247 * e.g. DRPAUSE so we can issue more instructions
1248 * before entering RUN/IDLE and executing them.
1249 */
1250
1251 /* get arg as a string. */
1252 cp = Jim_GetString(args[i], NULL);
1253 /* is it the magic? */
1254 if (0 == strcmp("-endstate", cp)) {
1255 /* is the statename valid? */
1256 cp = Jim_GetString(args[i + 1], NULL);
1257
1258 /* see if it is a valid state name */
1259 endstate = tap_state_by_name(cp);
1260 if (endstate < 0) {
1261 /* update the error message */
1262 Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp);
1263 } else {
1264 if (!scan_is_safe(endstate))
1265 LOG_WARNING("drscan with unsafe "
1266 "endstate \"%s\"", cp);
1267
1268 /* valid - so clear the error */
1269 e = JIM_OK;
1270 /* and remove the last 2 args */
1271 argc -= 2;
1272 }
1273 }
1274
1275 /* Still an error? */
1276 if (e != JIM_OK) {
1277 return e; /* too bad */
1278 }
1279 } /* validate args */
1280
1281 tap = jtag_tap_by_jim_obj(interp, args[1]);
1282 if (tap == NULL) {
1283 return JIM_ERR;
1284 }
1285
1286 num_fields = (argc-2)/2;
1287 fields = malloc(sizeof(scan_field_t) * num_fields);
1288 for (i = 2; i < argc; i += 2)
1289 {
1290 long bits;
1291 int len;
1292 const char *str;
1293
1294 Jim_GetLong(interp, args[i], &bits);
1295 str = Jim_GetString(args[i + 1], &len);
1296
1297 fields[field_count].tap = tap;
1298 fields[field_count].num_bits = bits;
1299 fields[field_count].out_value = malloc(CEIL(bits, 8));
1300 str_to_buf(str, len, fields[field_count].out_value, bits, 0);
1301 fields[field_count].in_value = fields[field_count].out_value;
1302 field_count++;
1303 }
1304
1305 jtag_add_dr_scan(num_fields, fields, endstate);
1306
1307 retval = jtag_execute_queue();
1308 if (retval != ERROR_OK)
1309 {
1310 Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
1311 return JIM_ERR;
1312 }
1313
1314 field_count = 0;
1315 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
1316 for (i = 2; i < argc; i += 2)
1317 {
1318 long bits;
1319 char *str;
1320
1321 Jim_GetLong(interp, args[i], &bits);
1322 str = buf_to_str(fields[field_count].in_value, bits, 16);
1323 free(fields[field_count].out_value);
1324
1325 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
1326 free(str);
1327 field_count++;
1328 }
1329
1330 Jim_SetResult(interp, list);
1331
1332 free(fields);
1333
1334 return JIM_OK;
1335 }
1336
1337
1338 static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *args)
1339 {
1340 tap_state_t states[8];
1341
1342 if ((argc < 2) || ((size_t)argc > (sizeof(states)/sizeof(*states) + 1)))
1343 {
1344 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
1345 return JIM_ERR;
1346 }
1347
1348 script_debug(interp, "pathmove", argc, args);
1349
1350 int i;
1351 for (i = 0; i < argc-1; i++)
1352 {
1353 const char *cp;
1354 cp = Jim_GetString(args[i + 1], NULL);
1355 states[i] = tap_state_by_name(cp);
1356 if (states[i] < 0)
1357 {
1358 /* update the error message */
1359 Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp);
1360 return JIM_ERR;
1361 }
1362 }
1363
1364 if ((jtag_add_statemove(states[0]) != ERROR_OK) || (jtag_execute_queue()!= ERROR_OK))
1365 {
1366 Jim_SetResultString(interp, "pathmove: jtag execute failed",-1);
1367 return JIM_ERR;
1368 }
1369
1370 jtag_add_pathmove(argc-2, states + 1);
1371
1372 if (jtag_execute_queue()!= ERROR_OK)
1373 {
1374 Jim_SetResultString(interp, "pathmove: failed",-1);
1375 return JIM_ERR;
1376 }
1377
1378 return JIM_OK;
1379 }
1380
1381
1382 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
1383 {
1384 script_debug(interp, "flush_count", argc, args);
1385
1386 Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));
1387
1388 return JIM_OK;
1389 }
1390
1391
1392 static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1393 {
1394 if (argc > 1)
1395 return ERROR_COMMAND_SYNTAX_ERROR;
1396
1397 if (argc == 1)
1398 {
1399 if (strcmp(args[0], "enable") == 0)
1400 jtag_set_verify_capture_ir(true);
1401 else if (strcmp(args[0], "disable") == 0)
1402 jtag_set_verify_capture_ir(false);
1403 else
1404 return ERROR_COMMAND_SYNTAX_ERROR;
1405 }
1406
1407 const char *status = jtag_will_verify_capture_ir() ? "enabled": "disabled";
1408 command_print(cmd_ctx, "verify Capture-IR is %s", status);
1409
1410 return ERROR_OK;
1411 }
1412
1413 static int handle_verify_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1414 {
1415 if (argc > 1)
1416 return ERROR_COMMAND_SYNTAX_ERROR;
1417
1418 if (argc == 1)
1419 {
1420 if (strcmp(args[0], "enable") == 0)
1421 jtag_set_verify(true);
1422 else if (strcmp(args[0], "disable") == 0)
1423 jtag_set_verify(false);
1424 else
1425 return ERROR_COMMAND_SYNTAX_ERROR;
1426 }
1427
1428 const char *status = jtag_will_verify() ? "enabled": "disabled";
1429 command_print(cmd_ctx, "verify jtag capture is %s", status);
1430
1431 return ERROR_OK;
1432 }
1433
1434 static int handle_tms_sequence_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1435 {
1436 if (argc > 1)
1437 return ERROR_COMMAND_SYNTAX_ERROR;
1438
1439 if (argc == 1)
1440 {
1441 bool use_new_table;
1442 if (strcmp(args[0], "short") == 0)
1443 use_new_table = true;
1444 else if (strcmp(args[0], "long") == 0)
1445 use_new_table = false;
1446 else
1447 return ERROR_COMMAND_SYNTAX_ERROR;
1448
1449 tap_use_new_tms_table(use_new_table);
1450 }
1451
1452 command_print(cmd_ctx, "tms sequence is %s",
1453 tap_uses_new_tms_table() ? "short": "long");
1454
1455 return ERROR_OK;
1456 }