TCL/SPEAr: move DDR activation in common code
[openocd/dsp568013.git] / src / target / target.c
blob4708a1d626bb271a4a09826dfdc7cfcc29c21acc
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
10 * *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
13 * *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
16 * *
17 * Copyright (C) 2011 by Broadcom Corporation *
18 * Evan Hunter - ehunter@broadcom.com *
19 * *
20 * Copyright (C) ST-Ericsson SA 2011 *
21 * michel.jaouen@stericsson.com : smp minimum support *
22 * *
23 * This program is free software; you can redistribute it and/or modify *
24 * it under the terms of the GNU General Public License as published by *
25 * the Free Software Foundation; either version 2 of the License, or *
26 * (at your option) any later version. *
27 * *
28 * This program is distributed in the hope that it will be useful, *
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
31 * GNU General Public License for more details. *
32 * *
33 * You should have received a copy of the GNU General Public License *
34 * along with this program; if not, write to the *
35 * Free Software Foundation, Inc., *
36 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
37 ***************************************************************************/
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
42 #include <helper/time_support.h>
43 #include <jtag/jtag.h>
44 #include <flash/nor/core.h>
46 #include "target.h"
47 #include "target_type.h"
48 #include "target_request.h"
49 #include "breakpoints.h"
50 #include "register.h"
51 #include "trace.h"
52 #include "image.h"
53 #include "rtos/rtos.h"
56 static int target_read_buffer_default(struct target *target, uint32_t address,
57 uint32_t size, uint8_t *buffer);
58 static int target_write_buffer_default(struct target *target, uint32_t address,
59 uint32_t size, const uint8_t *buffer);
60 static int target_array2mem(Jim_Interp *interp, struct target *target,
61 int argc, Jim_Obj *const *argv);
62 static int target_mem2array(Jim_Interp *interp, struct target *target,
63 int argc, Jim_Obj *const *argv);
64 static int target_register_user_commands(struct command_context *cmd_ctx);
66 /* targets */
67 extern struct target_type arm7tdmi_target;
68 extern struct target_type arm720t_target;
69 extern struct target_type arm9tdmi_target;
70 extern struct target_type arm920t_target;
71 extern struct target_type arm966e_target;
72 extern struct target_type arm946e_target;
73 extern struct target_type arm926ejs_target;
74 extern struct target_type fa526_target;
75 extern struct target_type feroceon_target;
76 extern struct target_type dragonite_target;
77 extern struct target_type xscale_target;
78 extern struct target_type cortexm3_target;
79 extern struct target_type cortexa8_target;
80 extern struct target_type arm11_target;
81 extern struct target_type mips_m4k_target;
82 extern struct target_type avr_target;
83 extern struct target_type dsp563xx_target;
84 extern struct target_type dsp5680xx_target;
85 extern struct target_type testee_target;
86 extern struct target_type avr32_ap7k_target;
88 static struct target_type *target_types[] =
90 &arm7tdmi_target,
91 &arm9tdmi_target,
92 &arm920t_target,
93 &arm720t_target,
94 &arm966e_target,
95 &arm946e_target,
96 &arm926ejs_target,
97 &fa526_target,
98 &feroceon_target,
99 &dragonite_target,
100 &xscale_target,
101 &cortexm3_target,
102 &cortexa8_target,
103 &arm11_target,
104 &mips_m4k_target,
105 &avr_target,
106 &dsp563xx_target,
107 &dsp5680xx_target,
108 &testee_target,
109 &avr32_ap7k_target,
110 NULL,
113 struct target *all_targets = NULL;
114 static struct target_event_callback *target_event_callbacks = NULL;
115 static struct target_timer_callback *target_timer_callbacks = NULL;
116 static const int polling_interval = 100;
118 static const Jim_Nvp nvp_assert[] = {
119 { .name = "assert", NVP_ASSERT },
120 { .name = "deassert", NVP_DEASSERT },
121 { .name = "T", NVP_ASSERT },
122 { .name = "F", NVP_DEASSERT },
123 { .name = "t", NVP_ASSERT },
124 { .name = "f", NVP_DEASSERT },
125 { .name = NULL, .value = -1 }
128 static const Jim_Nvp nvp_error_target[] = {
129 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
130 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
131 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
132 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
133 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
134 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
135 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
136 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
137 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
138 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
139 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
140 { .value = -1, .name = NULL }
143 static const char *target_strerror_safe(int err)
145 const Jim_Nvp *n;
147 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
148 if (n->name == NULL) {
149 return "unknown";
150 } else {
151 return n->name;
155 static const Jim_Nvp nvp_target_event[] = {
156 { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
157 { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" },
159 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
160 { .value = TARGET_EVENT_HALTED, .name = "halted" },
161 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
162 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
163 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
165 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
166 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
168 /* historical name */
170 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
172 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
173 { .value = TARGET_EVENT_RESET_ASSERT, .name = "reset-assert" },
174 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
175 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
176 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
177 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
178 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
179 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
180 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
181 { .value = TARGET_EVENT_RESET_INIT, .name = "reset-init" },
182 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
184 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
185 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
187 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
188 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
190 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
191 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
193 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
194 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
196 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
197 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
199 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
200 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
201 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
203 { .name = NULL, .value = -1 }
206 static const Jim_Nvp nvp_target_state[] = {
207 { .name = "unknown", .value = TARGET_UNKNOWN },
208 { .name = "running", .value = TARGET_RUNNING },
209 { .name = "halted", .value = TARGET_HALTED },
210 { .name = "reset", .value = TARGET_RESET },
211 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
212 { .name = NULL, .value = -1 },
215 static const Jim_Nvp nvp_target_debug_reason [] = {
216 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
217 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
218 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
219 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
220 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
221 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
222 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
223 { .name = NULL, .value = -1 },
226 static const Jim_Nvp nvp_target_endian[] = {
227 { .name = "big", .value = TARGET_BIG_ENDIAN },
228 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
229 { .name = "be", .value = TARGET_BIG_ENDIAN },
230 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
231 { .name = NULL, .value = -1 },
234 static const Jim_Nvp nvp_reset_modes[] = {
235 { .name = "unknown", .value = RESET_UNKNOWN },
236 { .name = "run" , .value = RESET_RUN },
237 { .name = "halt" , .value = RESET_HALT },
238 { .name = "init" , .value = RESET_INIT },
239 { .name = NULL , .value = -1 },
242 const char *debug_reason_name(struct target *t)
244 const char *cp;
246 cp = Jim_Nvp_value2name_simple(nvp_target_debug_reason,
247 t->debug_reason)->name;
248 if (!cp) {
249 LOG_ERROR("Invalid debug reason: %d", (int)(t->debug_reason));
250 cp = "(*BUG*unknown*BUG*)";
252 return cp;
255 const char *
256 target_state_name( struct target *t )
258 const char *cp;
259 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
260 if( !cp ){
261 LOG_ERROR("Invalid target state: %d", (int)(t->state));
262 cp = "(*BUG*unknown*BUG*)";
264 return cp;
267 /* determine the number of the new target */
268 static int new_target_number(void)
270 struct target *t;
271 int x;
273 /* number is 0 based */
274 x = -1;
275 t = all_targets;
276 while (t) {
277 if (x < t->target_number) {
278 x = t->target_number;
280 t = t->next;
282 return x + 1;
285 /* read a uint32_t from a buffer in target memory endianness */
286 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
288 if (target->endianness == TARGET_LITTLE_ENDIAN)
289 return le_to_h_u32(buffer);
290 else
291 return be_to_h_u32(buffer);
294 /* read a uint24_t from a buffer in target memory endianness */
295 uint32_t target_buffer_get_u24(struct target *target, const uint8_t *buffer)
297 if (target->endianness == TARGET_LITTLE_ENDIAN)
298 return le_to_h_u24(buffer);
299 else
300 return be_to_h_u24(buffer);
303 /* read a uint16_t from a buffer in target memory endianness */
304 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
306 if (target->endianness == TARGET_LITTLE_ENDIAN)
307 return le_to_h_u16(buffer);
308 else
309 return be_to_h_u16(buffer);
312 /* read a uint8_t from a buffer in target memory endianness */
313 static uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
315 return *buffer & 0x0ff;
318 /* write a uint32_t to a buffer in target memory endianness */
319 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
321 if (target->endianness == TARGET_LITTLE_ENDIAN)
322 h_u32_to_le(buffer, value);
323 else
324 h_u32_to_be(buffer, value);
327 /* write a uint24_t to a buffer in target memory endianness */
328 void target_buffer_set_u24(struct target *target, uint8_t *buffer, uint32_t value)
330 if (target->endianness == TARGET_LITTLE_ENDIAN)
331 h_u24_to_le(buffer, value);
332 else
333 h_u24_to_be(buffer, value);
336 /* write a uint16_t to a buffer in target memory endianness */
337 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
339 if (target->endianness == TARGET_LITTLE_ENDIAN)
340 h_u16_to_le(buffer, value);
341 else
342 h_u16_to_be(buffer, value);
345 /* write a uint8_t to a buffer in target memory endianness */
346 static void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
348 *buffer = value;
351 /* write a uint32_t array to a buffer in target memory endianness */
352 void target_buffer_get_u32_array(struct target *target, const uint8_t *buffer, uint32_t count, uint32_t *dstbuf)
354 uint32_t i;
355 for(i = 0; i < count; i ++)
356 dstbuf[i] = target_buffer_get_u32(target,&buffer[i*4]);
359 /* write a uint16_t array to a buffer in target memory endianness */
360 void target_buffer_get_u16_array(struct target *target, const uint8_t *buffer, uint32_t count, uint16_t *dstbuf)
362 uint32_t i;
363 for(i = 0; i < count; i ++)
364 dstbuf[i] = target_buffer_get_u16(target,&buffer[i*2]);
367 /* write a uint32_t array to a buffer in target memory endianness */
368 void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, uint32_t *srcbuf)
370 uint32_t i;
371 for(i = 0; i < count; i ++)
372 target_buffer_set_u32(target,&buffer[i*4],srcbuf[i]);
375 /* write a uint16_t array to a buffer in target memory endianness */
376 void target_buffer_set_u16_array(struct target *target, uint8_t *buffer, uint32_t count, uint16_t *srcbuf)
378 uint32_t i;
379 for(i = 0; i < count; i ++)
380 target_buffer_set_u16(target,&buffer[i*2],srcbuf[i]);
383 /* return a pointer to a configured target; id is name or number */
384 struct target *get_target(const char *id)
386 struct target *target;
388 /* try as tcltarget name */
389 for (target = all_targets; target; target = target->next) {
390 if (target->cmd_name == NULL)
391 continue;
392 if (strcmp(id, target->cmd_name) == 0)
393 return target;
396 /* It's OK to remove this fallback sometime after August 2010 or so */
398 /* no match, try as number */
399 unsigned num;
400 if (parse_uint(id, &num) != ERROR_OK)
401 return NULL;
403 for (target = all_targets; target; target = target->next) {
404 if (target->target_number == (int)num) {
405 LOG_WARNING("use '%s' as target identifier, not '%u'",
406 target->cmd_name, num);
407 return target;
411 return NULL;
414 /* returns a pointer to the n-th configured target */
415 static struct target *get_target_by_num(int num)
417 struct target *target = all_targets;
419 while (target) {
420 if (target->target_number == num) {
421 return target;
423 target = target->next;
426 return NULL;
429 struct target* get_current_target(struct command_context *cmd_ctx)
431 struct target *target = get_target_by_num(cmd_ctx->current_target);
433 if (target == NULL)
435 LOG_ERROR("BUG: current_target out of bounds");
436 exit(-1);
439 return target;
442 int target_poll(struct target *target)
444 int retval;
446 /* We can't poll until after examine */
447 if (!target_was_examined(target))
449 /* Fail silently lest we pollute the log */
450 return ERROR_FAIL;
453 retval = target->type->poll(target);
454 if (retval != ERROR_OK)
455 return retval;
457 if (target->halt_issued)
459 if (target->state == TARGET_HALTED)
461 target->halt_issued = false;
462 } else
464 long long t = timeval_ms() - target->halt_issued_time;
465 if (t>1000)
467 target->halt_issued = false;
468 LOG_INFO("Halt timed out, wake up GDB.");
469 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
474 return ERROR_OK;
477 int target_halt(struct target *target)
479 int retval;
480 /* We can't poll until after examine */
481 if (!target_was_examined(target))
483 LOG_ERROR("Target not examined yet");
484 return ERROR_FAIL;
487 retval = target->type->halt(target);
488 if (retval != ERROR_OK)
489 return retval;
491 target->halt_issued = true;
492 target->halt_issued_time = timeval_ms();
494 return ERROR_OK;
498 * Make the target (re)start executing using its saved execution
499 * context (possibly with some modifications).
501 * @param target Which target should start executing.
502 * @param current True to use the target's saved program counter instead
503 * of the address parameter
504 * @param address Optionally used as the program counter.
505 * @param handle_breakpoints True iff breakpoints at the resumption PC
506 * should be skipped. (For example, maybe execution was stopped by
507 * such a breakpoint, in which case it would be counterprodutive to
508 * let it re-trigger.
509 * @param debug_execution False if all working areas allocated by OpenOCD
510 * should be released and/or restored to their original contents.
511 * (This would for example be true to run some downloaded "helper"
512 * algorithm code, which resides in one such working buffer and uses
513 * another for data storage.)
515 * @todo Resolve the ambiguity about what the "debug_execution" flag
516 * signifies. For example, Target implementations don't agree on how
517 * it relates to invalidation of the register cache, or to whether
518 * breakpoints and watchpoints should be enabled. (It would seem wrong
519 * to enable breakpoints when running downloaded "helper" algorithms
520 * (debug_execution true), since the breakpoints would be set to match
521 * target firmware being debugged, not the helper algorithm.... and
522 * enabling them could cause such helpers to malfunction (for example,
523 * by overwriting data with a breakpoint instruction. On the other
524 * hand the infrastructure for running such helpers might use this
525 * procedure but rely on hardware breakpoint to detect termination.)
527 int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
529 int retval;
531 /* We can't poll until after examine */
532 if (!target_was_examined(target))
534 LOG_ERROR("Target not examined yet");
535 return ERROR_FAIL;
538 /* note that resume *must* be asynchronous. The CPU can halt before
539 * we poll. The CPU can even halt at the current PC as a result of
540 * a software breakpoint being inserted by (a bug?) the application.
542 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
543 return retval;
545 return retval;
548 static int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
550 char buf[100];
551 int retval;
552 Jim_Nvp *n;
553 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
554 if (n->name == NULL) {
555 LOG_ERROR("invalid reset mode");
556 return ERROR_FAIL;
559 /* disable polling during reset to make reset event scripts
560 * more predictable, i.e. dr/irscan & pathmove in events will
561 * not have JTAG operations injected into the middle of a sequence.
563 bool save_poll = jtag_poll_get_enabled();
565 jtag_poll_set_enabled(false);
567 sprintf(buf, "ocd_process_reset %s", n->name);
568 retval = Jim_Eval(cmd_ctx->interp, buf);
570 jtag_poll_set_enabled(save_poll);
572 if (retval != JIM_OK) {
573 Jim_MakeErrorMessage(cmd_ctx->interp);
574 command_print(NULL,"%s\n", Jim_GetString(Jim_GetResult(cmd_ctx->interp), NULL));
575 return ERROR_FAIL;
578 /* We want any events to be processed before the prompt */
579 retval = target_call_timer_callbacks_now();
581 struct target *target;
582 for (target = all_targets; target; target = target->next) {
583 target->type->check_reset(target);
586 return retval;
589 static int identity_virt2phys(struct target *target,
590 uint32_t virtual, uint32_t *physical)
592 *physical = virtual;
593 return ERROR_OK;
596 static int no_mmu(struct target *target, int *enabled)
598 *enabled = 0;
599 return ERROR_OK;
602 static int default_examine(struct target *target)
604 target_set_examined(target);
605 return ERROR_OK;
608 /* no check by default */
609 static int default_check_reset(struct target *target)
611 return ERROR_OK;
614 int target_examine_one(struct target *target)
616 return target->type->examine(target);
619 static int jtag_enable_callback(enum jtag_event event, void *priv)
621 struct target *target = priv;
623 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
624 return ERROR_OK;
626 jtag_unregister_event_callback(jtag_enable_callback, target);
627 return target_examine_one(target);
631 /* Targets that correctly implement init + examine, i.e.
632 * no communication with target during init:
634 * XScale
636 int target_examine(void)
638 int retval = ERROR_OK;
639 struct target *target;
641 for (target = all_targets; target; target = target->next)
643 /* defer examination, but don't skip it */
644 if (!target->tap->enabled) {
645 jtag_register_event_callback(jtag_enable_callback,
646 target);
647 continue;
649 if ((retval = target_examine_one(target)) != ERROR_OK)
650 return retval;
652 return retval;
654 const char *target_type_name(struct target *target)
656 return target->type->name;
659 static int target_write_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
661 if (!target_was_examined(target))
663 LOG_ERROR("Target not examined yet");
664 return ERROR_FAIL;
666 return target->type->write_memory_imp(target, address, size, count, buffer);
669 static int target_read_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
671 if (!target_was_examined(target))
673 LOG_ERROR("Target not examined yet");
674 return ERROR_FAIL;
676 return target->type->read_memory_imp(target, address, size, count, buffer);
679 static int target_soft_reset_halt_imp(struct target *target)
681 if (!target_was_examined(target))
683 LOG_ERROR("Target not examined yet");
684 return ERROR_FAIL;
686 if (!target->type->soft_reset_halt_imp) {
687 LOG_ERROR("Target %s does not support soft_reset_halt",
688 target_name(target));
689 return ERROR_FAIL;
691 return target->type->soft_reset_halt_imp(target);
695 * Downloads a target-specific native code algorithm to the target,
696 * and executes it. * Note that some targets may need to set up, enable,
697 * and tear down a breakpoint (hard or * soft) to detect algorithm
698 * termination, while others may support lower overhead schemes where
699 * soft breakpoints embedded in the algorithm automatically terminate the
700 * algorithm.
702 * @param target used to run the algorithm
703 * @param arch_info target-specific description of the algorithm.
705 int target_run_algorithm(struct target *target,
706 int num_mem_params, struct mem_param *mem_params,
707 int num_reg_params, struct reg_param *reg_param,
708 uint32_t entry_point, uint32_t exit_point,
709 int timeout_ms, void *arch_info)
711 int retval = ERROR_FAIL;
713 if (!target_was_examined(target))
715 LOG_ERROR("Target not examined yet");
716 goto done;
718 if (!target->type->run_algorithm) {
719 LOG_ERROR("Target type '%s' does not support %s",
720 target_type_name(target), __func__);
721 goto done;
724 target->running_alg = true;
725 retval = target->type->run_algorithm(target,
726 num_mem_params, mem_params,
727 num_reg_params, reg_param,
728 entry_point, exit_point, timeout_ms, arch_info);
729 target->running_alg = false;
731 done:
732 return retval;
736 * Downloads a target-specific native code algorithm to the target,
737 * executes and leaves it running.
739 * @param target used to run the algorithm
740 * @param arch_info target-specific description of the algorithm.
742 int target_start_algorithm(struct target *target,
743 int num_mem_params, struct mem_param *mem_params,
744 int num_reg_params, struct reg_param *reg_params,
745 uint32_t entry_point, uint32_t exit_point,
746 void *arch_info)
748 int retval = ERROR_FAIL;
750 if (!target_was_examined(target))
752 LOG_ERROR("Target not examined yet");
753 goto done;
755 if (!target->type->start_algorithm) {
756 LOG_ERROR("Target type '%s' does not support %s",
757 target_type_name(target), __func__);
758 goto done;
760 if (target->running_alg) {
761 LOG_ERROR("Target is already running an algorithm");
762 goto done;
765 target->running_alg = true;
766 retval = target->type->start_algorithm(target,
767 num_mem_params, mem_params,
768 num_reg_params, reg_params,
769 entry_point, exit_point, arch_info);
771 done:
772 return retval;
776 * Waits for an algorithm started with target_start_algorithm() to complete.
778 * @param target used to run the algorithm
779 * @param arch_info target-specific description of the algorithm.
781 int target_wait_algorithm(struct target *target,
782 int num_mem_params, struct mem_param *mem_params,
783 int num_reg_params, struct reg_param *reg_params,
784 uint32_t exit_point, int timeout_ms,
785 void *arch_info)
787 int retval = ERROR_FAIL;
789 if (!target->type->wait_algorithm) {
790 LOG_ERROR("Target type '%s' does not support %s",
791 target_type_name(target), __func__);
792 goto done;
794 if (!target->running_alg) {
795 LOG_ERROR("Target is not running an algorithm");
796 goto done;
799 retval = target->type->wait_algorithm(target,
800 num_mem_params, mem_params,
801 num_reg_params, reg_params,
802 exit_point, timeout_ms, arch_info);
803 if (retval != ERROR_TARGET_TIMEOUT)
804 target->running_alg = false;
806 done:
807 return retval;
811 int target_read_memory(struct target *target,
812 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
814 return target->type->read_memory(target, address, size, count, buffer);
817 static int target_read_phys_memory(struct target *target,
818 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
820 return target->type->read_phys_memory(target, address, size, count, buffer);
823 int target_write_memory(struct target *target,
824 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
826 return target->type->write_memory(target, address, size, count, buffer);
829 static int target_write_phys_memory(struct target *target,
830 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
832 return target->type->write_phys_memory(target, address, size, count, buffer);
835 int target_bulk_write_memory(struct target *target,
836 uint32_t address, uint32_t count, const uint8_t *buffer)
838 return target->type->bulk_write_memory(target, address, count, buffer);
841 int target_add_breakpoint(struct target *target,
842 struct breakpoint *breakpoint)
844 if ((target->state != TARGET_HALTED)&&(breakpoint->type!=BKPT_HARD)) {
845 LOG_WARNING("target %s is not halted", target->cmd_name);
846 return ERROR_TARGET_NOT_HALTED;
848 return target->type->add_breakpoint(target, breakpoint);
851 int target_add_context_breakpoint(struct target *target,
852 struct breakpoint *breakpoint)
854 if (target->state != TARGET_HALTED) {
855 LOG_WARNING("target %s is not halted", target->cmd_name);
856 return ERROR_TARGET_NOT_HALTED;
858 return target->type->add_context_breakpoint(target, breakpoint);
861 int target_add_hybrid_breakpoint(struct target *target,
862 struct breakpoint *breakpoint)
864 if (target->state != TARGET_HALTED) {
865 LOG_WARNING("target %s is not halted", target->cmd_name);
866 return ERROR_TARGET_NOT_HALTED;
868 return target->type->add_hybrid_breakpoint(target, breakpoint);
871 int target_remove_breakpoint(struct target *target,
872 struct breakpoint *breakpoint)
874 return target->type->remove_breakpoint(target, breakpoint);
877 int target_add_watchpoint(struct target *target,
878 struct watchpoint *watchpoint)
880 if (target->state != TARGET_HALTED) {
881 LOG_WARNING("target %s is not halted", target->cmd_name);
882 return ERROR_TARGET_NOT_HALTED;
884 return target->type->add_watchpoint(target, watchpoint);
886 int target_remove_watchpoint(struct target *target,
887 struct watchpoint *watchpoint)
889 return target->type->remove_watchpoint(target, watchpoint);
892 int target_get_gdb_reg_list(struct target *target,
893 struct reg **reg_list[], int *reg_list_size)
895 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
897 int target_step(struct target *target,
898 int current, uint32_t address, int handle_breakpoints)
900 return target->type->step(target, current, address, handle_breakpoints);
905 * Reset the @c examined flag for the given target.
906 * Pure paranoia -- targets are zeroed on allocation.
908 static void target_reset_examined(struct target *target)
910 target->examined = false;
913 static int
914 err_read_phys_memory(struct target *target, uint32_t address,
915 uint32_t size, uint32_t count, uint8_t *buffer)
917 LOG_ERROR("Not implemented: %s", __func__);
918 return ERROR_FAIL;
921 static int
922 err_write_phys_memory(struct target *target, uint32_t address,
923 uint32_t size, uint32_t count, const uint8_t *buffer)
925 LOG_ERROR("Not implemented: %s", __func__);
926 return ERROR_FAIL;
929 static int handle_target(void *priv);
931 static int target_init_one(struct command_context *cmd_ctx,
932 struct target *target)
934 target_reset_examined(target);
936 struct target_type *type = target->type;
937 if (type->examine == NULL)
938 type->examine = default_examine;
940 if (type->check_reset== NULL)
941 type->check_reset = default_check_reset;
943 int retval = type->init_target(cmd_ctx, target);
944 if (ERROR_OK != retval)
946 LOG_ERROR("target '%s' init failed", target_name(target));
947 return retval;
951 * @todo get rid of those *memory_imp() methods, now that all
952 * callers are using target_*_memory() accessors ... and make
953 * sure the "physical" paths handle the same issues.
955 /* a non-invasive way(in terms of patches) to add some code that
956 * runs before the type->write/read_memory implementation
958 type->write_memory_imp = target->type->write_memory;
959 type->write_memory = target_write_memory_imp;
961 type->read_memory_imp = target->type->read_memory;
962 type->read_memory = target_read_memory_imp;
964 type->soft_reset_halt_imp = target->type->soft_reset_halt;
965 type->soft_reset_halt = target_soft_reset_halt_imp;
967 /* Sanity-check MMU support ... stub in what we must, to help
968 * implement it in stages, but warn if we need to do so.
970 if (type->mmu)
972 if (type->write_phys_memory == NULL)
974 LOG_ERROR("type '%s' is missing write_phys_memory",
975 type->name);
976 type->write_phys_memory = err_write_phys_memory;
978 if (type->read_phys_memory == NULL)
980 LOG_ERROR("type '%s' is missing read_phys_memory",
981 type->name);
982 type->read_phys_memory = err_read_phys_memory;
984 if (type->virt2phys == NULL)
986 LOG_ERROR("type '%s' is missing virt2phys", type->name);
987 type->virt2phys = identity_virt2phys;
990 else
992 /* Make sure no-MMU targets all behave the same: make no
993 * distinction between physical and virtual addresses, and
994 * ensure that virt2phys() is always an identity mapping.
996 if (type->write_phys_memory || type->read_phys_memory
997 || type->virt2phys)
999 LOG_WARNING("type '%s' has bad MMU hooks", type->name);
1002 type->mmu = no_mmu;
1003 type->write_phys_memory = type->write_memory;
1004 type->read_phys_memory = type->read_memory;
1005 type->virt2phys = identity_virt2phys;
1008 if (target->type->read_buffer == NULL)
1009 target->type->read_buffer = target_read_buffer_default;
1011 if (target->type->write_buffer == NULL)
1012 target->type->write_buffer = target_write_buffer_default;
1014 return ERROR_OK;
1017 static int target_init(struct command_context *cmd_ctx)
1019 struct target *target;
1020 int retval;
1022 for (target = all_targets; target; target = target->next)
1024 retval = target_init_one(cmd_ctx, target);
1025 if (ERROR_OK != retval)
1026 return retval;
1029 if (!all_targets)
1030 return ERROR_OK;
1032 retval = target_register_user_commands(cmd_ctx);
1033 if (ERROR_OK != retval)
1034 return retval;
1036 retval = target_register_timer_callback(&handle_target,
1037 polling_interval, 1, cmd_ctx->interp);
1038 if (ERROR_OK != retval)
1039 return retval;
1041 return ERROR_OK;
1044 COMMAND_HANDLER(handle_target_init_command)
1046 if (CMD_ARGC != 0)
1047 return ERROR_COMMAND_SYNTAX_ERROR;
1049 static bool target_initialized = false;
1050 if (target_initialized)
1052 LOG_INFO("'target init' has already been called");
1053 return ERROR_OK;
1055 target_initialized = true;
1057 LOG_DEBUG("Initializing targets...");
1058 return target_init(CMD_CTX);
1061 int target_register_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
1063 struct target_event_callback **callbacks_p = &target_event_callbacks;
1065 if (callback == NULL)
1067 return ERROR_INVALID_ARGUMENTS;
1070 if (*callbacks_p)
1072 while ((*callbacks_p)->next)
1073 callbacks_p = &((*callbacks_p)->next);
1074 callbacks_p = &((*callbacks_p)->next);
1077 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
1078 (*callbacks_p)->callback = callback;
1079 (*callbacks_p)->priv = priv;
1080 (*callbacks_p)->next = NULL;
1082 return ERROR_OK;
1085 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
1087 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
1088 struct timeval now;
1090 if (callback == NULL)
1092 return ERROR_INVALID_ARGUMENTS;
1095 if (*callbacks_p)
1097 while ((*callbacks_p)->next)
1098 callbacks_p = &((*callbacks_p)->next);
1099 callbacks_p = &((*callbacks_p)->next);
1102 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
1103 (*callbacks_p)->callback = callback;
1104 (*callbacks_p)->periodic = periodic;
1105 (*callbacks_p)->time_ms = time_ms;
1107 gettimeofday(&now, NULL);
1108 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
1109 time_ms -= (time_ms % 1000);
1110 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
1111 if ((*callbacks_p)->when.tv_usec > 1000000)
1113 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
1114 (*callbacks_p)->when.tv_sec += 1;
1117 (*callbacks_p)->priv = priv;
1118 (*callbacks_p)->next = NULL;
1120 return ERROR_OK;
1123 int target_unregister_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
1125 struct target_event_callback **p = &target_event_callbacks;
1126 struct target_event_callback *c = target_event_callbacks;
1128 if (callback == NULL)
1130 return ERROR_INVALID_ARGUMENTS;
1133 while (c)
1135 struct target_event_callback *next = c->next;
1136 if ((c->callback == callback) && (c->priv == priv))
1138 *p = next;
1139 free(c);
1140 return ERROR_OK;
1142 else
1143 p = &(c->next);
1144 c = next;
1147 return ERROR_OK;
1150 static int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
1152 struct target_timer_callback **p = &target_timer_callbacks;
1153 struct target_timer_callback *c = target_timer_callbacks;
1155 if (callback == NULL)
1157 return ERROR_INVALID_ARGUMENTS;
1160 while (c)
1162 struct target_timer_callback *next = c->next;
1163 if ((c->callback == callback) && (c->priv == priv))
1165 *p = next;
1166 free(c);
1167 return ERROR_OK;
1169 else
1170 p = &(c->next);
1171 c = next;
1174 return ERROR_OK;
1177 int target_call_event_callbacks(struct target *target, enum target_event event)
1179 struct target_event_callback *callback = target_event_callbacks;
1180 struct target_event_callback *next_callback;
1182 if (event == TARGET_EVENT_HALTED)
1184 /* execute early halted first */
1185 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1188 LOG_DEBUG("target event %i (%s)",
1189 event,
1190 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
1192 target_handle_event(target, event);
1194 while (callback)
1196 next_callback = callback->next;
1197 callback->callback(target, event, callback->priv);
1198 callback = next_callback;
1201 return ERROR_OK;
1204 static int target_timer_callback_periodic_restart(
1205 struct target_timer_callback *cb, struct timeval *now)
1207 int time_ms = cb->time_ms;
1208 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
1209 time_ms -= (time_ms % 1000);
1210 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
1211 if (cb->when.tv_usec > 1000000)
1213 cb->when.tv_usec = cb->when.tv_usec - 1000000;
1214 cb->when.tv_sec += 1;
1216 return ERROR_OK;
1219 static int target_call_timer_callback(struct target_timer_callback *cb,
1220 struct timeval *now)
1222 cb->callback(cb->priv);
1224 if (cb->periodic)
1225 return target_timer_callback_periodic_restart(cb, now);
1227 return target_unregister_timer_callback(cb->callback, cb->priv);
1230 static int target_call_timer_callbacks_check_time(int checktime)
1232 keep_alive();
1234 struct timeval now;
1235 gettimeofday(&now, NULL);
1237 struct target_timer_callback *callback = target_timer_callbacks;
1238 while (callback)
1240 // cleaning up may unregister and free this callback
1241 struct target_timer_callback *next_callback = callback->next;
1243 bool call_it = callback->callback &&
1244 ((!checktime && callback->periodic) ||
1245 now.tv_sec > callback->when.tv_sec ||
1246 (now.tv_sec == callback->when.tv_sec &&
1247 now.tv_usec >= callback->when.tv_usec));
1249 if (call_it)
1251 int retval = target_call_timer_callback(callback, &now);
1252 if (retval != ERROR_OK)
1253 return retval;
1256 callback = next_callback;
1259 return ERROR_OK;
1262 int target_call_timer_callbacks(void)
1264 return target_call_timer_callbacks_check_time(1);
1267 /* invoke periodic callbacks immediately */
1268 int target_call_timer_callbacks_now(void)
1270 return target_call_timer_callbacks_check_time(0);
1273 int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
1275 struct working_area *c = target->working_areas;
1276 struct working_area *new_wa = NULL;
1278 /* Reevaluate working area address based on MMU state*/
1279 if (target->working_areas == NULL)
1281 int retval;
1282 int enabled;
1284 retval = target->type->mmu(target, &enabled);
1285 if (retval != ERROR_OK)
1287 return retval;
1290 if (!enabled) {
1291 if (target->working_area_phys_spec) {
1292 LOG_DEBUG("MMU disabled, using physical "
1293 "address for working memory 0x%08x",
1294 (unsigned)target->working_area_phys);
1295 target->working_area = target->working_area_phys;
1296 } else {
1297 LOG_ERROR("No working memory available. "
1298 "Specify -work-area-phys to target.");
1299 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1301 } else {
1302 if (target->working_area_virt_spec) {
1303 LOG_DEBUG("MMU enabled, using virtual "
1304 "address for working memory 0x%08x",
1305 (unsigned)target->working_area_virt);
1306 target->working_area = target->working_area_virt;
1307 } else {
1308 LOG_ERROR("No working memory available. "
1309 "Specify -work-area-virt to target.");
1310 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1315 /* only allocate multiples of 4 byte */
1316 if (size % 4)
1318 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
1319 size = (size + 3) & (~3);
1322 /* see if there's already a matching working area */
1323 while (c)
1325 if ((c->free) && (c->size == size))
1327 new_wa = c;
1328 break;
1330 c = c->next;
1333 /* if not, allocate a new one */
1334 if (!new_wa)
1336 struct working_area **p = &target->working_areas;
1337 uint32_t first_free = target->working_area;
1338 uint32_t free_size = target->working_area_size;
1340 c = target->working_areas;
1341 while (c)
1343 first_free += c->size;
1344 free_size -= c->size;
1345 p = &c->next;
1346 c = c->next;
1349 if (free_size < size)
1351 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1354 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free);
1356 new_wa = malloc(sizeof(struct working_area));
1357 new_wa->next = NULL;
1358 new_wa->size = size;
1359 new_wa->address = first_free;
1361 if (target->backup_working_area)
1363 int retval;
1364 new_wa->backup = malloc(new_wa->size);
1365 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1367 free(new_wa->backup);
1368 free(new_wa);
1369 return retval;
1372 else
1374 new_wa->backup = NULL;
1377 /* put new entry in list */
1378 *p = new_wa;
1381 /* mark as used, and return the new (reused) area */
1382 new_wa->free = false;
1383 *area = new_wa;
1385 /* user pointer */
1386 new_wa->user = area;
1388 return ERROR_OK;
1391 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1393 int retval;
1395 retval = target_alloc_working_area_try(target, size, area);
1396 if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
1398 LOG_WARNING("not enough working area available(requested %u)", (unsigned)(size));
1400 return retval;
1404 static int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1406 if (area->free)
1407 return ERROR_OK;
1409 if (restore && target->backup_working_area)
1411 int retval;
1412 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1413 return retval;
1416 area->free = true;
1418 /* mark user pointer invalid */
1419 *area->user = NULL;
1420 area->user = NULL;
1422 return ERROR_OK;
1425 int target_free_working_area(struct target *target, struct working_area *area)
1427 return target_free_working_area_restore(target, area, 1);
1430 /* free resources and restore memory, if restoring memory fails,
1431 * free up resources anyway
1433 static void target_free_all_working_areas_restore(struct target *target, int restore)
1435 struct working_area *c = target->working_areas;
1437 while (c)
1439 struct working_area *next = c->next;
1440 target_free_working_area_restore(target, c, restore);
1442 if (c->backup)
1443 free(c->backup);
1445 free(c);
1447 c = next;
1450 target->working_areas = NULL;
1453 void target_free_all_working_areas(struct target *target)
1455 target_free_all_working_areas_restore(target, 1);
1458 int target_arch_state(struct target *target)
1460 int retval;
1461 if (target == NULL)
1463 LOG_USER("No target has been configured");
1464 return ERROR_OK;
1467 LOG_USER("target state: %s", target_state_name( target ));
1469 if (target->state != TARGET_HALTED)
1470 return ERROR_OK;
1472 retval = target->type->arch_state(target);
1473 return retval;
1476 /* Single aligned words are guaranteed to use 16 or 32 bit access
1477 * mode respectively, otherwise data is handled as quickly as
1478 * possible
1480 int target_write_buffer(struct target *target, uint32_t address, uint32_t size, const uint8_t *buffer)
1482 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1483 (int)size, (unsigned)address);
1485 if (!target_was_examined(target))
1487 LOG_ERROR("Target not examined yet");
1488 return ERROR_FAIL;
1491 if (size == 0) {
1492 return ERROR_OK;
1495 if ((address + size - 1) < address)
1497 /* GDB can request this when e.g. PC is 0xfffffffc*/
1498 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1499 (unsigned)address,
1500 (unsigned)size);
1501 return ERROR_FAIL;
1504 return target->type->write_buffer(target, address, size, buffer);
1507 static int target_write_buffer_default(struct target *target, uint32_t address, uint32_t size, const uint8_t *buffer)
1509 int retval = ERROR_OK;
1511 if (((address % 2) == 0) && (size == 2))
1513 return target_write_memory(target, address, 2, 1, buffer);
1516 /* handle unaligned head bytes */
1517 if (address % 4)
1519 uint32_t unaligned = 4 - (address % 4);
1521 if (unaligned > size)
1522 unaligned = size;
1524 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1525 return retval;
1527 buffer += unaligned;
1528 address += unaligned;
1529 size -= unaligned;
1532 /* handle aligned words */
1533 if (size >= 4)
1535 int aligned = size - (size % 4);
1537 /* use bulk writes above a certain limit. This may have to be changed */
1538 if (aligned > 128)
1540 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1541 return retval;
1543 else
1545 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1546 return retval;
1549 buffer += aligned;
1550 address += aligned;
1551 size -= aligned;
1554 /* handle tail writes of less than 4 bytes */
1555 if (size > 0)
1557 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1558 return retval;
1561 return retval;
1564 /* Single aligned words are guaranteed to use 16 or 32 bit access
1565 * mode respectively, otherwise data is handled as quickly as
1566 * possible
1568 int target_read_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1570 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1571 (int)size, (unsigned)address);
1573 if (!target_was_examined(target))
1575 LOG_ERROR("Target not examined yet");
1576 return ERROR_FAIL;
1579 if (size == 0) {
1580 return ERROR_OK;
1583 if ((address + size - 1) < address)
1585 /* GDB can request this when e.g. PC is 0xfffffffc*/
1586 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1587 address,
1588 size);
1589 return ERROR_FAIL;
1592 return target->type->read_buffer(target, address, size, buffer);
1595 static int target_read_buffer_default(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1597 int retval = ERROR_OK;
1599 if (((address % 2) == 0) && (size == 2))
1601 return target_read_memory(target, address, 2, 1, buffer);
1604 /* handle unaligned head bytes */
1605 if (address % 4)
1607 uint32_t unaligned = 4 - (address % 4);
1609 if (unaligned > size)
1610 unaligned = size;
1612 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1613 return retval;
1615 buffer += unaligned;
1616 address += unaligned;
1617 size -= unaligned;
1620 /* handle aligned words */
1621 if (size >= 4)
1623 int aligned = size - (size % 4);
1625 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1626 return retval;
1628 buffer += aligned;
1629 address += aligned;
1630 size -= aligned;
1633 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1634 if(size >=2)
1636 int aligned = size - (size%2);
1637 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1638 if (retval != ERROR_OK)
1639 return retval;
1641 buffer += aligned;
1642 address += aligned;
1643 size -= aligned;
1645 /* handle tail writes of less than 4 bytes */
1646 if (size > 0)
1648 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1649 return retval;
1652 return ERROR_OK;
1655 int target_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* crc)
1657 uint8_t *buffer;
1658 int retval;
1659 uint32_t i;
1660 uint32_t checksum = 0;
1661 if (!target_was_examined(target))
1663 LOG_ERROR("Target not examined yet");
1664 return ERROR_FAIL;
1667 if ((retval = target->type->checksum_memory(target, address,
1668 size, &checksum)) != ERROR_OK)
1670 buffer = malloc(size);
1671 if (buffer == NULL)
1673 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1674 return ERROR_INVALID_ARGUMENTS;
1676 retval = target_read_buffer(target, address, size, buffer);
1677 if (retval != ERROR_OK)
1679 free(buffer);
1680 return retval;
1683 /* convert to target endianness */
1684 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1686 uint32_t target_data;
1687 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1688 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1691 retval = image_calculate_checksum(buffer, size, &checksum);
1692 free(buffer);
1695 *crc = checksum;
1697 return retval;
1700 int target_blank_check_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* blank)
1702 int retval;
1703 if (!target_was_examined(target))
1705 LOG_ERROR("Target not examined yet");
1706 return ERROR_FAIL;
1709 if (target->type->blank_check_memory == 0)
1710 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1712 retval = target->type->blank_check_memory(target, address, size, blank);
1714 return retval;
1717 int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
1719 uint8_t value_buf[4];
1720 if (!target_was_examined(target))
1722 LOG_ERROR("Target not examined yet");
1723 return ERROR_FAIL;
1726 int retval = target_read_memory(target, address, 4, 1, value_buf);
1728 if (retval == ERROR_OK)
1730 *value = target_buffer_get_u32(target, value_buf);
1731 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1732 address,
1733 *value);
1735 else
1737 *value = 0x0;
1738 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1739 address);
1742 return retval;
1745 int target_read_u16(struct target *target, uint32_t address, uint16_t *value)
1747 uint8_t value_buf[2];
1748 if (!target_was_examined(target))
1750 LOG_ERROR("Target not examined yet");
1751 return ERROR_FAIL;
1754 int retval = target_read_memory(target, address, 2, 1, value_buf);
1756 if (retval == ERROR_OK)
1758 *value = target_buffer_get_u16(target, value_buf);
1759 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1760 address,
1761 *value);
1763 else
1765 *value = 0x0;
1766 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1767 address);
1770 return retval;
1773 int target_read_u8(struct target *target, uint32_t address, uint8_t *value)
1775 int retval = target_read_memory(target, address, 1, 1, value);
1776 if (!target_was_examined(target))
1778 LOG_ERROR("Target not examined yet");
1779 return ERROR_FAIL;
1782 if (retval == ERROR_OK)
1784 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1785 address,
1786 *value);
1788 else
1790 *value = 0x0;
1791 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1792 address);
1795 return retval;
1798 int target_write_u32(struct target *target, uint32_t address, uint32_t value)
1800 int retval;
1801 uint8_t value_buf[4];
1802 if (!target_was_examined(target))
1804 LOG_ERROR("Target not examined yet");
1805 return ERROR_FAIL;
1808 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1809 address,
1810 value);
1812 target_buffer_set_u32(target, value_buf, value);
1813 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1815 LOG_DEBUG("failed: %i", retval);
1818 return retval;
1821 int target_write_u16(struct target *target, uint32_t address, uint16_t value)
1823 int retval;
1824 uint8_t value_buf[2];
1825 if (!target_was_examined(target))
1827 LOG_ERROR("Target not examined yet");
1828 return ERROR_FAIL;
1831 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1832 address,
1833 value);
1835 target_buffer_set_u16(target, value_buf, value);
1836 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1838 LOG_DEBUG("failed: %i", retval);
1841 return retval;
1844 int target_write_u8(struct target *target, uint32_t address, uint8_t value)
1846 int retval;
1847 if (!target_was_examined(target))
1849 LOG_ERROR("Target not examined yet");
1850 return ERROR_FAIL;
1853 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1854 address, value);
1856 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1858 LOG_DEBUG("failed: %i", retval);
1861 return retval;
1864 static int find_target(struct command_context *cmd_ctx, const char *name)
1866 struct target *target = get_target(name);
1867 if (target == NULL) {
1868 LOG_ERROR("Target: %s is unknown, try one of:\n", name);
1869 return ERROR_FAIL;
1871 if (!target->tap->enabled) {
1872 LOG_USER("Target: TAP %s is disabled, "
1873 "can't be the current target\n",
1874 target->tap->dotted_name);
1875 return ERROR_FAIL;
1878 cmd_ctx->current_target = target->target_number;
1879 return ERROR_OK;
1883 COMMAND_HANDLER(handle_targets_command)
1885 int retval = ERROR_OK;
1886 if (CMD_ARGC == 1)
1888 retval = find_target(CMD_CTX, CMD_ARGV[0]);
1889 if (retval == ERROR_OK) {
1890 /* we're done! */
1891 return retval;
1895 struct target *target = all_targets;
1896 command_print(CMD_CTX, " TargetName Type Endian TapName State ");
1897 command_print(CMD_CTX, "-- ------------------ ---------- ------ ------------------ ------------");
1898 while (target)
1900 const char *state;
1901 char marker = ' ';
1903 if (target->tap->enabled)
1904 state = target_state_name( target );
1905 else
1906 state = "tap-disabled";
1908 if (CMD_CTX->current_target == target->target_number)
1909 marker = '*';
1911 /* keep columns lined up to match the headers above */
1912 command_print(CMD_CTX,
1913 "%2d%c %-18s %-10s %-6s %-18s %s",
1914 target->target_number,
1915 marker,
1916 target_name(target),
1917 target_type_name(target),
1918 Jim_Nvp_value2name_simple(nvp_target_endian,
1919 target->endianness)->name,
1920 target->tap->dotted_name,
1921 state);
1922 target = target->next;
1925 return retval;
1928 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1930 static int powerDropout;
1931 static int srstAsserted;
1933 static int runPowerRestore;
1934 static int runPowerDropout;
1935 static int runSrstAsserted;
1936 static int runSrstDeasserted;
1938 static int sense_handler(void)
1940 static int prevSrstAsserted = 0;
1941 static int prevPowerdropout = 0;
1943 int retval;
1944 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1945 return retval;
1947 int powerRestored;
1948 powerRestored = prevPowerdropout && !powerDropout;
1949 if (powerRestored)
1951 runPowerRestore = 1;
1954 long long current = timeval_ms();
1955 static long long lastPower = 0;
1956 int waitMore = lastPower + 2000 > current;
1957 if (powerDropout && !waitMore)
1959 runPowerDropout = 1;
1960 lastPower = current;
1963 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1964 return retval;
1966 int srstDeasserted;
1967 srstDeasserted = prevSrstAsserted && !srstAsserted;
1969 static long long lastSrst = 0;
1970 waitMore = lastSrst + 2000 > current;
1971 if (srstDeasserted && !waitMore)
1973 runSrstDeasserted = 1;
1974 lastSrst = current;
1977 if (!prevSrstAsserted && srstAsserted)
1979 runSrstAsserted = 1;
1982 prevSrstAsserted = srstAsserted;
1983 prevPowerdropout = powerDropout;
1985 if (srstDeasserted || powerRestored)
1987 /* Other than logging the event we can't do anything here.
1988 * Issuing a reset is a particularly bad idea as we might
1989 * be inside a reset already.
1993 return ERROR_OK;
1996 static int backoff_times = 0;
1997 static int backoff_count = 0;
1999 /* process target state changes */
2000 static int handle_target(void *priv)
2002 Jim_Interp *interp = (Jim_Interp *)priv;
2003 int retval = ERROR_OK;
2005 if (!is_jtag_poll_safe())
2007 /* polling is disabled currently */
2008 return ERROR_OK;
2011 /* we do not want to recurse here... */
2012 static int recursive = 0;
2013 if (! recursive)
2015 recursive = 1;
2016 sense_handler();
2017 /* danger! running these procedures can trigger srst assertions and power dropouts.
2018 * We need to avoid an infinite loop/recursion here and we do that by
2019 * clearing the flags after running these events.
2021 int did_something = 0;
2022 if (runSrstAsserted)
2024 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2025 Jim_Eval(interp, "srst_asserted");
2026 did_something = 1;
2028 if (runSrstDeasserted)
2030 Jim_Eval(interp, "srst_deasserted");
2031 did_something = 1;
2033 if (runPowerDropout)
2035 LOG_INFO("Power dropout detected, running power_dropout proc.");
2036 Jim_Eval(interp, "power_dropout");
2037 did_something = 1;
2039 if (runPowerRestore)
2041 Jim_Eval(interp, "power_restore");
2042 did_something = 1;
2045 if (did_something)
2047 /* clear detect flags */
2048 sense_handler();
2051 /* clear action flags */
2053 runSrstAsserted = 0;
2054 runSrstDeasserted = 0;
2055 runPowerRestore = 0;
2056 runPowerDropout = 0;
2058 recursive = 0;
2061 if (backoff_times > backoff_count)
2063 /* do not poll this time as we failed previously */
2064 backoff_count++;
2065 return ERROR_OK;
2067 backoff_count = 0;
2069 /* Poll targets for state changes unless that's globally disabled.
2070 * Skip targets that are currently disabled.
2072 for (struct target *target = all_targets;
2073 is_jtag_poll_safe() && target;
2074 target = target->next)
2076 if (!target->tap->enabled)
2077 continue;
2079 /* only poll target if we've got power and srst isn't asserted */
2080 if (!powerDropout && !srstAsserted)
2082 /* polling may fail silently until the target has been examined */
2083 if ((retval = target_poll(target)) != ERROR_OK)
2085 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2086 if (backoff_times * polling_interval < 5000)
2088 backoff_times *= 2;
2089 backoff_times++;
2091 LOG_USER("Polling target failed, GDB will be halted. Polling again in %dms", backoff_times * polling_interval);
2093 /* Tell GDB to halt the debugger. This allows the user to
2094 * run monitor commands to handle the situation.
2096 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2097 return retval;
2099 /* Since we succeeded, we reset backoff count */
2100 if (backoff_times > 0)
2102 LOG_USER("Polling succeeded again");
2104 backoff_times = 0;
2108 return retval;
2111 COMMAND_HANDLER(handle_reg_command)
2113 struct target *target;
2114 struct reg *reg = NULL;
2115 unsigned count = 0;
2116 char *value;
2118 LOG_DEBUG("-");
2120 target = get_current_target(CMD_CTX);
2122 /* list all available registers for the current target */
2123 if (CMD_ARGC == 0)
2125 struct reg_cache *cache = target->reg_cache;
2127 count = 0;
2128 while (cache)
2130 unsigned i;
2132 command_print(CMD_CTX, "===== %s", cache->name);
2134 for (i = 0, reg = cache->reg_list;
2135 i < cache->num_regs;
2136 i++, reg++, count++)
2138 /* only print cached values if they are valid */
2139 if (reg->valid) {
2140 value = buf_to_str(reg->value,
2141 reg->size, 16);
2142 command_print(CMD_CTX,
2143 "(%i) %s (/%" PRIu32 "): 0x%s%s",
2144 count, reg->name,
2145 reg->size, value,
2146 reg->dirty
2147 ? " (dirty)"
2148 : "");
2149 free(value);
2150 } else {
2151 command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
2152 count, reg->name,
2153 reg->size) ;
2156 cache = cache->next;
2159 return ERROR_OK;
2162 /* access a single register by its ordinal number */
2163 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9'))
2165 unsigned num;
2166 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
2168 struct reg_cache *cache = target->reg_cache;
2169 count = 0;
2170 while (cache)
2172 unsigned i;
2173 for (i = 0; i < cache->num_regs; i++)
2175 if (count++ == num)
2177 reg = &cache->reg_list[i];
2178 break;
2181 if (reg)
2182 break;
2183 cache = cache->next;
2186 if (!reg)
2188 command_print(CMD_CTX, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
2189 return ERROR_OK;
2191 } else /* access a single register by its name */
2193 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
2195 if (!reg)
2197 command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
2198 return ERROR_OK;
2202 assert(reg != NULL); /* give clang a hint that we *know* reg is != NULL here */
2204 /* display a register */
2205 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0') && (CMD_ARGV[1][0] <= '9'))))
2207 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
2208 reg->valid = 0;
2210 if (reg->valid == 0)
2212 reg->type->get(reg);
2214 value = buf_to_str(reg->value, reg->size, 16);
2215 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2216 free(value);
2217 return ERROR_OK;
2220 /* set register value */
2221 if (CMD_ARGC == 2)
2223 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
2224 if (buf == NULL)
2225 return ERROR_FAIL;
2226 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
2228 reg->type->set(reg, buf);
2230 value = buf_to_str(reg->value, reg->size, 16);
2231 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2232 free(value);
2234 free(buf);
2236 return ERROR_OK;
2239 command_print(CMD_CTX, "usage: reg <#|name> [value]");
2241 return ERROR_OK;
2244 COMMAND_HANDLER(handle_poll_command)
2246 int retval = ERROR_OK;
2247 struct target *target = get_current_target(CMD_CTX);
2249 if (CMD_ARGC == 0)
2251 command_print(CMD_CTX, "background polling: %s",
2252 jtag_poll_get_enabled() ? "on" : "off");
2253 command_print(CMD_CTX, "TAP: %s (%s)",
2254 target->tap->dotted_name,
2255 target->tap->enabled ? "enabled" : "disabled");
2256 if (!target->tap->enabled)
2257 return ERROR_OK;
2258 if ((retval = target_poll(target)) != ERROR_OK)
2259 return retval;
2260 if ((retval = target_arch_state(target)) != ERROR_OK)
2261 return retval;
2263 else if (CMD_ARGC == 1)
2265 bool enable;
2266 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
2267 jtag_poll_set_enabled(enable);
2269 else
2271 return ERROR_COMMAND_SYNTAX_ERROR;
2274 return retval;
2277 COMMAND_HANDLER(handle_wait_halt_command)
2279 if (CMD_ARGC > 1)
2280 return ERROR_COMMAND_SYNTAX_ERROR;
2282 unsigned ms = 5000;
2283 if (1 == CMD_ARGC)
2285 int retval = parse_uint(CMD_ARGV[0], &ms);
2286 if (ERROR_OK != retval)
2288 command_print(CMD_CTX, "usage: %s [seconds]", CMD_NAME);
2289 return ERROR_COMMAND_SYNTAX_ERROR;
2291 // convert seconds (given) to milliseconds (needed)
2292 ms *= 1000;
2295 struct target *target = get_current_target(CMD_CTX);
2296 return target_wait_state(target, TARGET_HALTED, ms);
2299 /* wait for target state to change. The trick here is to have a low
2300 * latency for short waits and not to suck up all the CPU time
2301 * on longer waits.
2303 * After 500ms, keep_alive() is invoked
2305 int target_wait_state(struct target *target, enum target_state state, int ms)
2307 int retval;
2308 long long then = 0, cur;
2309 int once = 1;
2311 for (;;)
2313 if ((retval = target_poll(target)) != ERROR_OK)
2314 return retval;
2315 if (target->state == state)
2317 break;
2319 cur = timeval_ms();
2320 if (once)
2322 once = 0;
2323 then = timeval_ms();
2324 LOG_DEBUG("waiting for target %s...",
2325 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2328 if (cur-then > 500)
2330 keep_alive();
2333 if ((cur-then) > ms)
2335 LOG_ERROR("timed out while waiting for target %s",
2336 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2337 return ERROR_FAIL;
2341 return ERROR_OK;
2344 COMMAND_HANDLER(handle_halt_command)
2346 LOG_DEBUG("-");
2348 struct target *target = get_current_target(CMD_CTX);
2349 int retval = target_halt(target);
2350 if (ERROR_OK != retval)
2351 return retval;
2353 if (CMD_ARGC == 1)
2355 unsigned wait_local;
2356 retval = parse_uint(CMD_ARGV[0], &wait_local);
2357 if (ERROR_OK != retval)
2358 return ERROR_COMMAND_SYNTAX_ERROR;
2359 if (!wait_local)
2360 return ERROR_OK;
2363 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2366 COMMAND_HANDLER(handle_soft_reset_halt_command)
2368 struct target *target = get_current_target(CMD_CTX);
2370 LOG_USER("requesting target halt and executing a soft reset");
2372 target->type->soft_reset_halt(target);
2374 return ERROR_OK;
2377 COMMAND_HANDLER(handle_reset_command)
2379 if (CMD_ARGC > 1)
2380 return ERROR_COMMAND_SYNTAX_ERROR;
2382 enum target_reset_mode reset_mode = RESET_RUN;
2383 if (CMD_ARGC == 1)
2385 const Jim_Nvp *n;
2386 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
2387 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2388 return ERROR_COMMAND_SYNTAX_ERROR;
2390 reset_mode = n->value;
2393 /* reset *all* targets */
2394 return target_process_reset(CMD_CTX, reset_mode);
2398 COMMAND_HANDLER(handle_resume_command)
2400 int current = 1;
2401 if (CMD_ARGC > 1)
2402 return ERROR_COMMAND_SYNTAX_ERROR;
2404 struct target *target = get_current_target(CMD_CTX);
2405 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2407 /* with no CMD_ARGV, resume from current pc, addr = 0,
2408 * with one arguments, addr = CMD_ARGV[0],
2409 * handle breakpoints, not debugging */
2410 uint32_t addr = 0;
2411 if (CMD_ARGC == 1)
2413 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2414 current = 0;
2417 return target_resume(target, current, addr, 1, 0);
2420 COMMAND_HANDLER(handle_step_command)
2422 if (CMD_ARGC > 1)
2423 return ERROR_COMMAND_SYNTAX_ERROR;
2425 LOG_DEBUG("-");
2427 /* with no CMD_ARGV, step from current pc, addr = 0,
2428 * with one argument addr = CMD_ARGV[0],
2429 * handle breakpoints, debugging */
2430 uint32_t addr = 0;
2431 int current_pc = 1;
2432 if (CMD_ARGC == 1)
2434 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2435 current_pc = 0;
2438 struct target *target = get_current_target(CMD_CTX);
2440 return target->type->step(target, current_pc, addr, 1);
2443 static void handle_md_output(struct command_context *cmd_ctx,
2444 struct target *target, uint32_t address, unsigned size,
2445 unsigned count, const uint8_t *buffer)
2447 const unsigned line_bytecnt = 32;
2448 unsigned line_modulo = line_bytecnt / size;
2450 char output[line_bytecnt * 4 + 1];
2451 unsigned output_len = 0;
2453 const char *value_fmt;
2454 switch (size) {
2455 case 4: value_fmt = "%8.8x "; break;
2456 case 2: value_fmt = "%4.4x "; break;
2457 case 1: value_fmt = "%2.2x "; break;
2458 default:
2459 /* "can't happen", caller checked */
2460 LOG_ERROR("invalid memory read size: %u", size);
2461 return;
2464 for (unsigned i = 0; i < count; i++)
2466 if (i % line_modulo == 0)
2468 output_len += snprintf(output + output_len,
2469 sizeof(output) - output_len,
2470 "0x%8.8x: ",
2471 (unsigned)(address + (i*size)));
2474 uint32_t value = 0;
2475 const uint8_t *value_ptr = buffer + i * size;
2476 switch (size) {
2477 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2478 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2479 case 1: value = *value_ptr;
2481 output_len += snprintf(output + output_len,
2482 sizeof(output) - output_len,
2483 value_fmt, value);
2485 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2487 command_print(cmd_ctx, "%s", output);
2488 output_len = 0;
2493 COMMAND_HANDLER(handle_md_command)
2495 if (CMD_ARGC < 1)
2496 return ERROR_COMMAND_SYNTAX_ERROR;
2498 unsigned size = 0;
2499 switch (CMD_NAME[2]) {
2500 case 'w': size = 4; break;
2501 case 'h': size = 2; break;
2502 case 'b': size = 1; break;
2503 default: return ERROR_COMMAND_SYNTAX_ERROR;
2506 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2507 int (*fn)(struct target *target,
2508 uint32_t address, uint32_t size_value, uint32_t count, uint8_t *buffer);
2509 if (physical)
2511 CMD_ARGC--;
2512 CMD_ARGV++;
2513 fn=target_read_phys_memory;
2514 } else
2516 fn=target_read_memory;
2518 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
2520 return ERROR_COMMAND_SYNTAX_ERROR;
2523 uint32_t address;
2524 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2526 unsigned count = 1;
2527 if (CMD_ARGC == 2)
2528 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
2530 uint8_t *buffer = calloc(count, size);
2532 struct target *target = get_current_target(CMD_CTX);
2533 int retval = fn(target, address, size, count, buffer);
2534 if (ERROR_OK == retval)
2535 handle_md_output(CMD_CTX, target, address, size, count, buffer);
2537 free(buffer);
2539 return retval;
2542 typedef int (*target_write_fn)(struct target *target,
2543 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer);
2545 static int target_write_memory_fast(struct target *target,
2546 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
2548 return target_write_buffer(target, address, size * count, buffer);
2551 static int target_fill_mem(struct target *target,
2552 uint32_t address,
2553 target_write_fn fn,
2554 unsigned data_size,
2555 /* value */
2556 uint32_t b,
2557 /* count */
2558 unsigned c)
2560 /* We have to write in reasonably large chunks to be able
2561 * to fill large memory areas with any sane speed */
2562 const unsigned chunk_size = 16384;
2563 uint8_t *target_buf = malloc(chunk_size * data_size);
2564 if (target_buf == NULL)
2566 LOG_ERROR("Out of memory");
2567 return ERROR_FAIL;
2570 for (unsigned i = 0; i < chunk_size; i ++)
2572 switch (data_size)
2574 case 4:
2575 target_buffer_set_u32(target, target_buf + i*data_size, b);
2576 break;
2577 case 2:
2578 target_buffer_set_u16(target, target_buf + i*data_size, b);
2579 break;
2580 case 1:
2581 target_buffer_set_u8(target, target_buf + i*data_size, b);
2582 break;
2583 default:
2584 exit(-1);
2588 int retval = ERROR_OK;
2590 for (unsigned x = 0; x < c; x += chunk_size)
2592 unsigned current;
2593 current = c - x;
2594 if (current > chunk_size)
2596 current = chunk_size;
2598 retval = fn(target, address + x * data_size, data_size, current, target_buf);
2599 if (retval != ERROR_OK)
2601 break;
2603 /* avoid GDB timeouts */
2604 keep_alive();
2606 free(target_buf);
2608 return retval;
2612 COMMAND_HANDLER(handle_mw_command)
2614 if (CMD_ARGC < 2)
2616 return ERROR_COMMAND_SYNTAX_ERROR;
2618 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2619 target_write_fn fn;
2620 if (physical)
2622 CMD_ARGC--;
2623 CMD_ARGV++;
2624 fn=target_write_phys_memory;
2625 } else
2627 fn = target_write_memory_fast;
2629 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
2630 return ERROR_COMMAND_SYNTAX_ERROR;
2632 uint32_t address;
2633 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2635 uint32_t value;
2636 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2638 unsigned count = 1;
2639 if (CMD_ARGC == 3)
2640 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
2642 struct target *target = get_current_target(CMD_CTX);
2643 unsigned wordsize;
2644 switch (CMD_NAME[2])
2646 case 'w':
2647 wordsize = 4;
2648 break;
2649 case 'h':
2650 wordsize = 2;
2651 break;
2652 case 'b':
2653 wordsize = 1;
2654 break;
2655 default:
2656 return ERROR_COMMAND_SYNTAX_ERROR;
2659 return target_fill_mem(target, address, fn, wordsize, value, count);
2662 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
2663 uint32_t *min_address, uint32_t *max_address)
2665 if (CMD_ARGC < 1 || CMD_ARGC > 5)
2666 return ERROR_COMMAND_SYNTAX_ERROR;
2668 /* a base address isn't always necessary,
2669 * default to 0x0 (i.e. don't relocate) */
2670 if (CMD_ARGC >= 2)
2672 uint32_t addr;
2673 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2674 image->base_address = addr;
2675 image->base_address_set = 1;
2677 else
2678 image->base_address_set = 0;
2680 image->start_address_set = 0;
2682 if (CMD_ARGC >= 4)
2684 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], *min_address);
2686 if (CMD_ARGC == 5)
2688 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], *max_address);
2689 // use size (given) to find max (required)
2690 *max_address += *min_address;
2693 if (*min_address > *max_address)
2694 return ERROR_COMMAND_SYNTAX_ERROR;
2696 return ERROR_OK;
2699 COMMAND_HANDLER(handle_load_image_command)
2701 uint8_t *buffer;
2702 size_t buf_cnt;
2703 uint32_t image_size;
2704 uint32_t min_address = 0;
2705 uint32_t max_address = 0xffffffff;
2706 int i;
2707 struct image image;
2709 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
2710 &image, &min_address, &max_address);
2711 if (ERROR_OK != retval)
2712 return retval;
2714 struct target *target = get_current_target(CMD_CTX);
2716 struct duration bench;
2717 duration_start(&bench);
2719 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
2721 return ERROR_OK;
2724 image_size = 0x0;
2725 retval = ERROR_OK;
2726 for (i = 0; i < image.num_sections; i++)
2728 buffer = malloc(image.sections[i].size);
2729 if (buffer == NULL)
2731 command_print(CMD_CTX,
2732 "error allocating buffer for section (%d bytes)",
2733 (int)(image.sections[i].size));
2734 break;
2737 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2739 free(buffer);
2740 break;
2743 uint32_t offset = 0;
2744 uint32_t length = buf_cnt;
2746 /* DANGER!!! beware of unsigned comparision here!!! */
2748 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2749 (image.sections[i].base_address < max_address))
2751 if (image.sections[i].base_address < min_address)
2753 /* clip addresses below */
2754 offset += min_address-image.sections[i].base_address;
2755 length -= offset;
2758 if (image.sections[i].base_address + buf_cnt > max_address)
2760 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2763 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2765 free(buffer);
2766 break;
2768 image_size += length;
2769 command_print(CMD_CTX, "%u bytes written at address 0x%8.8" PRIx32 "",
2770 (unsigned int)length,
2771 image.sections[i].base_address + offset);
2774 free(buffer);
2777 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2779 command_print(CMD_CTX, "downloaded %" PRIu32 " bytes "
2780 "in %fs (%0.3f KiB/s)", image_size,
2781 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2784 image_close(&image);
2786 return retval;
2790 COMMAND_HANDLER(handle_dump_image_command)
2792 struct fileio fileio;
2793 uint8_t buffer[560];
2794 int retval, retvaltemp;
2795 uint32_t address, size;
2796 struct duration bench;
2797 struct target *target = get_current_target(CMD_CTX);
2799 if (CMD_ARGC != 3)
2800 return ERROR_COMMAND_SYNTAX_ERROR;
2802 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], address);
2803 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], size);
2805 retval = fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY);
2806 if (retval != ERROR_OK)
2807 return retval;
2809 duration_start(&bench);
2811 retval = ERROR_OK;
2812 while (size > 0)
2814 size_t size_written;
2815 uint32_t this_run_size = (size > 560) ? 560 : size;
2816 retval = target_read_buffer(target, address, this_run_size, buffer);
2817 if (retval != ERROR_OK)
2819 break;
2822 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2823 if (retval != ERROR_OK)
2825 break;
2828 size -= this_run_size;
2829 address += this_run_size;
2832 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2834 int filesize;
2835 retval = fileio_size(&fileio, &filesize);
2836 if (retval != ERROR_OK)
2837 return retval;
2838 command_print(CMD_CTX,
2839 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize,
2840 duration_elapsed(&bench), duration_kbps(&bench, filesize));
2843 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2844 return retvaltemp;
2846 return retval;
2849 static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
2851 uint8_t *buffer;
2852 size_t buf_cnt;
2853 uint32_t image_size;
2854 int i;
2855 int retval;
2856 uint32_t checksum = 0;
2857 uint32_t mem_checksum = 0;
2859 struct image image;
2861 struct target *target = get_current_target(CMD_CTX);
2863 if (CMD_ARGC < 1)
2865 return ERROR_COMMAND_SYNTAX_ERROR;
2868 if (!target)
2870 LOG_ERROR("no target selected");
2871 return ERROR_FAIL;
2874 struct duration bench;
2875 duration_start(&bench);
2877 if (CMD_ARGC >= 2)
2879 uint32_t addr;
2880 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2881 image.base_address = addr;
2882 image.base_address_set = 1;
2884 else
2886 image.base_address_set = 0;
2887 image.base_address = 0x0;
2890 image.start_address_set = 0;
2892 if ((retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL)) != ERROR_OK)
2894 return retval;
2897 image_size = 0x0;
2898 int diffs = 0;
2899 retval = ERROR_OK;
2900 for (i = 0; i < image.num_sections; i++)
2902 buffer = malloc(image.sections[i].size);
2903 if (buffer == NULL)
2905 command_print(CMD_CTX,
2906 "error allocating buffer for section (%d bytes)",
2907 (int)(image.sections[i].size));
2908 break;
2910 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2912 free(buffer);
2913 break;
2916 if (verify)
2918 /* calculate checksum of image */
2919 retval = image_calculate_checksum(buffer, buf_cnt, &checksum);
2920 if (retval != ERROR_OK)
2922 free(buffer);
2923 break;
2926 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2927 if (retval != ERROR_OK)
2929 free(buffer);
2930 break;
2933 if (checksum != mem_checksum)
2935 /* failed crc checksum, fall back to a binary compare */
2936 uint8_t *data;
2938 if (diffs == 0)
2940 LOG_ERROR("checksum mismatch - attempting binary compare");
2943 data = (uint8_t*)malloc(buf_cnt);
2945 /* Can we use 32bit word accesses? */
2946 int size = 1;
2947 int count = buf_cnt;
2948 if ((count % 4) == 0)
2950 size *= 4;
2951 count /= 4;
2953 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2954 if (retval == ERROR_OK)
2956 uint32_t t;
2957 for (t = 0; t < buf_cnt; t++)
2959 if (data[t] != buffer[t])
2961 command_print(CMD_CTX,
2962 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
2963 diffs,
2964 (unsigned)(t + image.sections[i].base_address),
2965 data[t],
2966 buffer[t]);
2967 if (diffs++ >= 127)
2969 command_print(CMD_CTX, "More than 128 errors, the rest are not printed.");
2970 free(data);
2971 free(buffer);
2972 goto done;
2975 keep_alive();
2978 free(data);
2980 } else
2982 command_print(CMD_CTX, "address 0x%08" PRIx32 " length 0x%08zx",
2983 image.sections[i].base_address,
2984 buf_cnt);
2987 free(buffer);
2988 image_size += buf_cnt;
2990 if (diffs > 0)
2992 command_print(CMD_CTX, "No more differences found.");
2994 done:
2995 if (diffs > 0)
2997 retval = ERROR_FAIL;
2999 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
3001 command_print(CMD_CTX, "verified %" PRIu32 " bytes "
3002 "in %fs (%0.3f KiB/s)", image_size,
3003 duration_elapsed(&bench), duration_kbps(&bench, image_size));
3006 image_close(&image);
3008 return retval;
3011 COMMAND_HANDLER(handle_verify_image_command)
3013 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 1);
3016 COMMAND_HANDLER(handle_test_image_command)
3018 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 0);
3021 static int handle_bp_command_list(struct command_context *cmd_ctx)
3023 struct target *target = get_current_target(cmd_ctx);
3024 struct breakpoint *breakpoint = target->breakpoints;
3025 while (breakpoint)
3027 if (breakpoint->type == BKPT_SOFT)
3029 char* buf = buf_to_str(breakpoint->orig_instr,
3030 breakpoint->length, 16);
3031 command_print(cmd_ctx, "IVA breakpoint: 0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
3032 breakpoint->address,
3033 breakpoint->length,
3034 breakpoint->set, buf);
3035 free(buf);
3037 else
3039 if ((breakpoint->address == 0) && (breakpoint->asid != 0))
3040 command_print(cmd_ctx, "Context breakpoint: 0x%8.8" PRIx32 ", 0x%x, %i",
3041 breakpoint->asid,
3042 breakpoint->length, breakpoint->set);
3043 else if ((breakpoint->address != 0) && (breakpoint->asid != 0))
3045 command_print(cmd_ctx, "Hybrid breakpoint(IVA): 0x%8.8" PRIx32 ", 0x%x, %i",
3046 breakpoint->address,
3047 breakpoint->length, breakpoint->set);
3048 command_print(cmd_ctx, "\t|--->linked with ContextID: 0x%8.8" PRIx32,
3049 breakpoint->asid);
3051 else
3052 command_print(cmd_ctx, "Breakpoint(IVA): 0x%8.8" PRIx32 ", 0x%x, %i",
3053 breakpoint->address,
3054 breakpoint->length, breakpoint->set);
3057 breakpoint = breakpoint->next;
3059 return ERROR_OK;
3062 static int handle_bp_command_set(struct command_context *cmd_ctx,
3063 uint32_t addr, uint32_t asid, uint32_t length, int hw)
3065 struct target *target = get_current_target(cmd_ctx);
3067 if (asid == 0)
3069 int retval = breakpoint_add(target, addr, length, hw);
3070 if (ERROR_OK == retval)
3071 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
3072 else
3074 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3075 return retval;
3078 else if (addr == 0)
3080 int retval = context_breakpoint_add(target, asid, length, hw);
3081 if (ERROR_OK == retval)
3082 command_print(cmd_ctx, "Context breakpoint set at 0x%8.8" PRIx32 "", asid);
3083 else
3085 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3086 return retval;
3089 else
3091 int retval = hybrid_breakpoint_add(target, addr, asid, length, hw);
3092 if(ERROR_OK == retval)
3093 command_print(cmd_ctx, "Hybrid breakpoint set at 0x%8.8" PRIx32 "", asid);
3094 else
3096 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3097 return retval;
3100 return ERROR_OK;
3103 COMMAND_HANDLER(handle_bp_command)
3105 uint32_t addr;
3106 uint32_t asid;
3107 uint32_t length;
3108 int hw = BKPT_SOFT;
3109 switch(CMD_ARGC)
3111 case 0:
3112 return handle_bp_command_list(CMD_CTX);
3114 case 2:
3115 asid = 0;
3116 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3117 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3118 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3120 case 3:
3121 if(strcmp(CMD_ARGV[2], "hw") == 0)
3123 hw = BKPT_HARD;
3124 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3126 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3128 asid = 0;
3129 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3131 else if(strcmp(CMD_ARGV[2], "hw_ctx") == 0)
3133 hw = BKPT_HARD;
3134 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], asid);
3135 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3136 addr = 0;
3137 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3140 case 4:
3141 hw = BKPT_HARD;
3142 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3143 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], asid);
3144 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], length);
3145 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3147 default:
3148 command_print(CMD_CTX, "usage: bp <address> [<asid>]<length> ['hw'|'hw_ctx']");
3149 return ERROR_COMMAND_SYNTAX_ERROR;
3153 COMMAND_HANDLER(handle_rbp_command)
3155 if (CMD_ARGC != 1)
3156 return ERROR_COMMAND_SYNTAX_ERROR;
3158 uint32_t addr;
3159 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3161 struct target *target = get_current_target(CMD_CTX);
3162 breakpoint_remove(target, addr);
3164 return ERROR_OK;
3167 COMMAND_HANDLER(handle_wp_command)
3169 struct target *target = get_current_target(CMD_CTX);
3171 if (CMD_ARGC == 0)
3173 struct watchpoint *watchpoint = target->watchpoints;
3175 while (watchpoint)
3177 command_print(CMD_CTX, "address: 0x%8.8" PRIx32
3178 ", len: 0x%8.8" PRIx32
3179 ", r/w/a: %i, value: 0x%8.8" PRIx32
3180 ", mask: 0x%8.8" PRIx32,
3181 watchpoint->address,
3182 watchpoint->length,
3183 (int)watchpoint->rw,
3184 watchpoint->value,
3185 watchpoint->mask);
3186 watchpoint = watchpoint->next;
3188 return ERROR_OK;
3191 enum watchpoint_rw type = WPT_ACCESS;
3192 uint32_t addr = 0;
3193 uint32_t length = 0;
3194 uint32_t data_value = 0x0;
3195 uint32_t data_mask = 0xffffffff;
3197 switch (CMD_ARGC)
3199 case 5:
3200 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
3201 // fall through
3202 case 4:
3203 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
3204 // fall through
3205 case 3:
3206 switch (CMD_ARGV[2][0])
3208 case 'r':
3209 type = WPT_READ;
3210 break;
3211 case 'w':
3212 type = WPT_WRITE;
3213 break;
3214 case 'a':
3215 type = WPT_ACCESS;
3216 break;
3217 default:
3218 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
3219 return ERROR_COMMAND_SYNTAX_ERROR;
3221 // fall through
3222 case 2:
3223 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3224 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3225 break;
3227 default:
3228 command_print(CMD_CTX, "usage: wp [address length "
3229 "[(r|w|a) [value [mask]]]]");
3230 return ERROR_COMMAND_SYNTAX_ERROR;
3233 int retval = watchpoint_add(target, addr, length, type,
3234 data_value, data_mask);
3235 if (ERROR_OK != retval)
3236 LOG_ERROR("Failure setting watchpoints");
3238 return retval;
3241 COMMAND_HANDLER(handle_rwp_command)
3243 if (CMD_ARGC != 1)
3244 return ERROR_COMMAND_SYNTAX_ERROR;
3246 uint32_t addr;
3247 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3249 struct target *target = get_current_target(CMD_CTX);
3250 watchpoint_remove(target, addr);
3252 return ERROR_OK;
3257 * Translate a virtual address to a physical address.
3259 * The low-level target implementation must have logged a detailed error
3260 * which is forwarded to telnet/GDB session.
3262 COMMAND_HANDLER(handle_virt2phys_command)
3264 if (CMD_ARGC != 1)
3265 return ERROR_COMMAND_SYNTAX_ERROR;
3267 uint32_t va;
3268 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], va);
3269 uint32_t pa;
3271 struct target *target = get_current_target(CMD_CTX);
3272 int retval = target->type->virt2phys(target, va, &pa);
3273 if (retval == ERROR_OK)
3274 command_print(CMD_CTX, "Physical address 0x%08" PRIx32 "", pa);
3276 return retval;
3279 static void writeData(FILE *f, const void *data, size_t len)
3281 size_t written = fwrite(data, 1, len, f);
3282 if (written != len)
3283 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
3286 static void writeLong(FILE *f, int l)
3288 int i;
3289 for (i = 0; i < 4; i++)
3291 char c = (l >> (i*8))&0xff;
3292 writeData(f, &c, 1);
3297 static void writeString(FILE *f, char *s)
3299 writeData(f, s, strlen(s));
3302 /* Dump a gmon.out histogram file. */
3303 static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filename)
3305 uint32_t i;
3306 FILE *f = fopen(filename, "w");
3307 if (f == NULL)
3308 return;
3309 writeString(f, "gmon");
3310 writeLong(f, 0x00000001); /* Version */
3311 writeLong(f, 0); /* padding */
3312 writeLong(f, 0); /* padding */
3313 writeLong(f, 0); /* padding */
3315 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
3316 writeData(f, &zero, 1);
3318 /* figure out bucket size */
3319 uint32_t min = samples[0];
3320 uint32_t max = samples[0];
3321 for (i = 0; i < sampleNum; i++)
3323 if (min > samples[i])
3325 min = samples[i];
3327 if (max < samples[i])
3329 max = samples[i];
3333 int addressSpace = (max - min + 1);
3334 assert(addressSpace >= 2);
3336 static const uint32_t maxBuckets = 16 * 1024; /* maximum buckets. */
3337 uint32_t length = addressSpace;
3338 if (length > maxBuckets)
3340 length = maxBuckets;
3342 int *buckets = malloc(sizeof(int)*length);
3343 if (buckets == NULL)
3345 fclose(f);
3346 return;
3348 memset(buckets, 0, sizeof(int)*length);
3349 for (i = 0; i < sampleNum;i++)
3351 uint32_t address = samples[i];
3352 long long a = address-min;
3353 long long b = length-1;
3354 long long c = addressSpace-1;
3355 int index_t = (a*b)/c; /* danger!!!! int32 overflows */
3356 buckets[index_t]++;
3359 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3360 writeLong(f, min); /* low_pc */
3361 writeLong(f, max); /* high_pc */
3362 writeLong(f, length); /* # of samples */
3363 writeLong(f, 100); /* KLUDGE! We lie, ca. 100Hz best case. */
3364 writeString(f, "seconds");
3365 for (i = 0; i < (15-strlen("seconds")); i++)
3366 writeData(f, &zero, 1);
3367 writeString(f, "s");
3369 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3371 char *data = malloc(2*length);
3372 if (data != NULL)
3374 for (i = 0; i < length;i++)
3376 int val;
3377 val = buckets[i];
3378 if (val > 65535)
3380 val = 65535;
3382 data[i*2]=val&0xff;
3383 data[i*2 + 1]=(val >> 8)&0xff;
3385 free(buckets);
3386 writeData(f, data, length * 2);
3387 free(data);
3388 } else
3390 free(buckets);
3393 fclose(f);
3396 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3397 * which will be used as a random sampling of PC */
3398 COMMAND_HANDLER(handle_profile_command)
3400 struct target *target = get_current_target(CMD_CTX);
3401 struct timeval timeout, now;
3403 gettimeofday(&timeout, NULL);
3404 if (CMD_ARGC != 2)
3406 return ERROR_COMMAND_SYNTAX_ERROR;
3408 unsigned offset;
3409 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], offset);
3411 timeval_add_time(&timeout, offset, 0);
3414 * @todo: Some cores let us sample the PC without the
3415 * annoying halt/resume step; for example, ARMv7 PCSR.
3416 * Provide a way to use that more efficient mechanism.
3419 command_print(CMD_CTX, "Starting profiling. Halting and resuming the target as often as we can...");
3421 static const int maxSample = 10000;
3422 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
3423 if (samples == NULL)
3424 return ERROR_OK;
3426 int numSamples = 0;
3427 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3428 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
3430 int retval = ERROR_OK;
3431 for (;;)
3433 target_poll(target);
3434 if (target->state == TARGET_HALTED)
3436 uint32_t t=*((uint32_t *)reg->value);
3437 samples[numSamples++]=t;
3438 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3439 target_poll(target);
3440 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3441 } else if (target->state == TARGET_RUNNING)
3443 /* We want to quickly sample the PC. */
3444 if ((retval = target_halt(target)) != ERROR_OK)
3446 free(samples);
3447 return retval;
3449 } else
3451 command_print(CMD_CTX, "Target not halted or running");
3452 retval = ERROR_OK;
3453 break;
3455 if (retval != ERROR_OK)
3457 break;
3460 gettimeofday(&now, NULL);
3461 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3463 command_print(CMD_CTX, "Profiling completed. %d samples.", numSamples);
3464 if ((retval = target_poll(target)) != ERROR_OK)
3466 free(samples);
3467 return retval;
3469 if (target->state == TARGET_HALTED)
3471 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3473 if ((retval = target_poll(target)) != ERROR_OK)
3475 free(samples);
3476 return retval;
3478 writeGmon(samples, numSamples, CMD_ARGV[1]);
3479 command_print(CMD_CTX, "Wrote %s", CMD_ARGV[1]);
3480 break;
3483 free(samples);
3485 return retval;
3488 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3490 char *namebuf;
3491 Jim_Obj *nameObjPtr, *valObjPtr;
3492 int result;
3494 namebuf = alloc_printf("%s(%d)", varname, idx);
3495 if (!namebuf)
3496 return JIM_ERR;
3498 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3499 valObjPtr = Jim_NewIntObj(interp, val);
3500 if (!nameObjPtr || !valObjPtr)
3502 free(namebuf);
3503 return JIM_ERR;
3506 Jim_IncrRefCount(nameObjPtr);
3507 Jim_IncrRefCount(valObjPtr);
3508 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3509 Jim_DecrRefCount(interp, nameObjPtr);
3510 Jim_DecrRefCount(interp, valObjPtr);
3511 free(namebuf);
3512 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3513 return result;
3516 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3518 struct command_context *context;
3519 struct target *target;
3521 context = current_command_context(interp);
3522 assert (context != NULL);
3524 target = get_current_target(context);
3525 if (target == NULL)
3527 LOG_ERROR("mem2array: no current target");
3528 return JIM_ERR;
3531 return target_mem2array(interp, target, argc-1, argv + 1);
3534 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3536 long l;
3537 uint32_t width;
3538 int len;
3539 uint32_t addr;
3540 uint32_t count;
3541 uint32_t v;
3542 const char *varname;
3543 int n, e, retval;
3544 uint32_t i;
3546 /* argv[1] = name of array to receive the data
3547 * argv[2] = desired width
3548 * argv[3] = memory address
3549 * argv[4] = count of times to read
3551 if (argc != 4) {
3552 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3553 return JIM_ERR;
3555 varname = Jim_GetString(argv[0], &len);
3556 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3558 e = Jim_GetLong(interp, argv[1], &l);
3559 width = l;
3560 if (e != JIM_OK) {
3561 return e;
3564 e = Jim_GetLong(interp, argv[2], &l);
3565 addr = l;
3566 if (e != JIM_OK) {
3567 return e;
3569 e = Jim_GetLong(interp, argv[3], &l);
3570 len = l;
3571 if (e != JIM_OK) {
3572 return e;
3574 switch (width) {
3575 case 8:
3576 width = 1;
3577 break;
3578 case 16:
3579 width = 2;
3580 break;
3581 case 32:
3582 width = 4;
3583 break;
3584 default:
3585 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3586 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3587 return JIM_ERR;
3589 if (len == 0) {
3590 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3591 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3592 return JIM_ERR;
3594 if ((addr + (len * width)) < addr) {
3595 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3596 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3597 return JIM_ERR;
3599 /* absurd transfer size? */
3600 if (len > 65536) {
3601 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3602 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3603 return JIM_ERR;
3606 if ((width == 1) ||
3607 ((width == 2) && ((addr & 1) == 0)) ||
3608 ((width == 4) && ((addr & 3) == 0))) {
3609 /* all is well */
3610 } else {
3611 char buf[100];
3612 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3613 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3614 addr,
3615 width);
3616 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3617 return JIM_ERR;
3620 /* Transfer loop */
3622 /* index counter */
3623 n = 0;
3625 size_t buffersize = 4096;
3626 uint8_t *buffer = malloc(buffersize);
3627 if (buffer == NULL)
3628 return JIM_ERR;
3630 /* assume ok */
3631 e = JIM_OK;
3632 while (len) {
3633 /* Slurp... in buffer size chunks */
3635 count = len; /* in objects.. */
3636 if (count > (buffersize/width)) {
3637 count = (buffersize/width);
3640 retval = target_read_memory(target, addr, width, count, buffer);
3641 if (retval != ERROR_OK) {
3642 /* BOO !*/
3643 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3644 (unsigned int)addr,
3645 (int)width,
3646 (int)count);
3647 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3648 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3649 e = JIM_ERR;
3650 break;
3651 } else {
3652 v = 0; /* shut up gcc */
3653 for (i = 0 ;i < count ;i++, n++) {
3654 switch (width) {
3655 case 4:
3656 v = target_buffer_get_u32(target, &buffer[i*width]);
3657 break;
3658 case 2:
3659 v = target_buffer_get_u16(target, &buffer[i*width]);
3660 break;
3661 case 1:
3662 v = buffer[i] & 0x0ff;
3663 break;
3665 new_int_array_element(interp, varname, n, v);
3667 len -= count;
3671 free(buffer);
3673 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3675 return e;
3678 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3680 char *namebuf;
3681 Jim_Obj *nameObjPtr, *valObjPtr;
3682 int result;
3683 long l;
3685 namebuf = alloc_printf("%s(%d)", varname, idx);
3686 if (!namebuf)
3687 return JIM_ERR;
3689 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3690 if (!nameObjPtr)
3692 free(namebuf);
3693 return JIM_ERR;
3696 Jim_IncrRefCount(nameObjPtr);
3697 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3698 Jim_DecrRefCount(interp, nameObjPtr);
3699 free(namebuf);
3700 if (valObjPtr == NULL)
3701 return JIM_ERR;
3703 result = Jim_GetLong(interp, valObjPtr, &l);
3704 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3705 *val = l;
3706 return result;
3709 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3711 struct command_context *context;
3712 struct target *target;
3714 context = current_command_context(interp);
3715 assert (context != NULL);
3717 target = get_current_target(context);
3718 if (target == NULL) {
3719 LOG_ERROR("array2mem: no current target");
3720 return JIM_ERR;
3723 return target_array2mem(interp,target, argc-1, argv + 1);
3726 static int target_array2mem(Jim_Interp *interp, struct target *target,
3727 int argc, Jim_Obj *const *argv)
3729 long l;
3730 uint32_t width;
3731 int len;
3732 uint32_t addr;
3733 uint32_t count;
3734 uint32_t v;
3735 const char *varname;
3736 int n, e, retval;
3737 uint32_t i;
3739 /* argv[1] = name of array to get the data
3740 * argv[2] = desired width
3741 * argv[3] = memory address
3742 * argv[4] = count to write
3744 if (argc != 4) {
3745 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems");
3746 return JIM_ERR;
3748 varname = Jim_GetString(argv[0], &len);
3749 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3751 e = Jim_GetLong(interp, argv[1], &l);
3752 width = l;
3753 if (e != JIM_OK) {
3754 return e;
3757 e = Jim_GetLong(interp, argv[2], &l);
3758 addr = l;
3759 if (e != JIM_OK) {
3760 return e;
3762 e = Jim_GetLong(interp, argv[3], &l);
3763 len = l;
3764 if (e != JIM_OK) {
3765 return e;
3767 switch (width) {
3768 case 8:
3769 width = 1;
3770 break;
3771 case 16:
3772 width = 2;
3773 break;
3774 case 32:
3775 width = 4;
3776 break;
3777 default:
3778 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3779 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3780 return JIM_ERR;
3782 if (len == 0) {
3783 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3784 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3785 return JIM_ERR;
3787 if ((addr + (len * width)) < addr) {
3788 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3789 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3790 return JIM_ERR;
3792 /* absurd transfer size? */
3793 if (len > 65536) {
3794 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3795 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3796 return JIM_ERR;
3799 if ((width == 1) ||
3800 ((width == 2) && ((addr & 1) == 0)) ||
3801 ((width == 4) && ((addr & 3) == 0))) {
3802 /* all is well */
3803 } else {
3804 char buf[100];
3805 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3806 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3807 (unsigned int)addr,
3808 (int)width);
3809 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3810 return JIM_ERR;
3813 /* Transfer loop */
3815 /* index counter */
3816 n = 0;
3817 /* assume ok */
3818 e = JIM_OK;
3820 size_t buffersize = 4096;
3821 uint8_t *buffer = malloc(buffersize);
3822 if (buffer == NULL)
3823 return JIM_ERR;
3825 while (len) {
3826 /* Slurp... in buffer size chunks */
3828 count = len; /* in objects.. */
3829 if (count > (buffersize/width)) {
3830 count = (buffersize/width);
3833 v = 0; /* shut up gcc */
3834 for (i = 0 ;i < count ;i++, n++) {
3835 get_int_array_element(interp, varname, n, &v);
3836 switch (width) {
3837 case 4:
3838 target_buffer_set_u32(target, &buffer[i*width], v);
3839 break;
3840 case 2:
3841 target_buffer_set_u16(target, &buffer[i*width], v);
3842 break;
3843 case 1:
3844 buffer[i] = v & 0x0ff;
3845 break;
3848 len -= count;
3850 retval = target_write_memory(target, addr, width, count, buffer);
3851 if (retval != ERROR_OK) {
3852 /* BOO !*/
3853 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3854 (unsigned int)addr,
3855 (int)width,
3856 (int)count);
3857 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3858 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3859 e = JIM_ERR;
3860 break;
3864 free(buffer);
3866 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3868 return e;
3871 /* FIX? should we propagate errors here rather than printing them
3872 * and continuing?
3874 void target_handle_event(struct target *target, enum target_event e)
3876 struct target_event_action *teap;
3878 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3879 if (teap->event == e) {
3880 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3881 target->target_number,
3882 target_name(target),
3883 target_type_name(target),
3885 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3886 Jim_GetString(teap->body, NULL));
3887 if (Jim_EvalObj(teap->interp, teap->body) != JIM_OK)
3889 Jim_MakeErrorMessage(teap->interp);
3890 command_print(NULL,"%s\n", Jim_GetString(Jim_GetResult(teap->interp), NULL));
3897 * Returns true only if the target has a handler for the specified event.
3899 bool target_has_event_action(struct target *target, enum target_event event)
3901 struct target_event_action *teap;
3903 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3904 if (teap->event == event)
3905 return true;
3907 return false;
3910 enum target_cfg_param {
3911 TCFG_TYPE,
3912 TCFG_EVENT,
3913 TCFG_WORK_AREA_VIRT,
3914 TCFG_WORK_AREA_PHYS,
3915 TCFG_WORK_AREA_SIZE,
3916 TCFG_WORK_AREA_BACKUP,
3917 TCFG_ENDIAN,
3918 TCFG_VARIANT,
3919 TCFG_COREID,
3920 TCFG_CHAIN_POSITION,
3921 TCFG_DBGBASE,
3922 TCFG_RTOS,
3925 static Jim_Nvp nvp_config_opts[] = {
3926 { .name = "-type", .value = TCFG_TYPE },
3927 { .name = "-event", .value = TCFG_EVENT },
3928 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3929 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3930 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3931 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3932 { .name = "-endian" , .value = TCFG_ENDIAN },
3933 { .name = "-variant", .value = TCFG_VARIANT },
3934 { .name = "-coreid", .value = TCFG_COREID },
3935 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3936 { .name = "-dbgbase", .value = TCFG_DBGBASE },
3937 { .name = "-rtos", .value = TCFG_RTOS },
3938 { .name = NULL, .value = -1 }
3941 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
3943 Jim_Nvp *n;
3944 Jim_Obj *o;
3945 jim_wide w;
3946 char *cp;
3947 int e;
3949 /* parse config or cget options ... */
3950 while (goi->argc > 0) {
3951 Jim_SetEmptyResult(goi->interp);
3952 /* Jim_GetOpt_Debug(goi); */
3954 if (target->type->target_jim_configure) {
3955 /* target defines a configure function */
3956 /* target gets first dibs on parameters */
3957 e = (*(target->type->target_jim_configure))(target, goi);
3958 if (e == JIM_OK) {
3959 /* more? */
3960 continue;
3962 if (e == JIM_ERR) {
3963 /* An error */
3964 return e;
3966 /* otherwise we 'continue' below */
3968 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3969 if (e != JIM_OK) {
3970 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3971 return e;
3973 switch (n->value) {
3974 case TCFG_TYPE:
3975 /* not setable */
3976 if (goi->isconfigure) {
3977 Jim_SetResultFormatted(goi->interp,
3978 "not settable: %s", n->name);
3979 return JIM_ERR;
3980 } else {
3981 no_params:
3982 if (goi->argc != 0) {
3983 Jim_WrongNumArgs(goi->interp,
3984 goi->argc, goi->argv,
3985 "NO PARAMS");
3986 return JIM_ERR;
3989 Jim_SetResultString(goi->interp,
3990 target_type_name(target), -1);
3991 /* loop for more */
3992 break;
3993 case TCFG_EVENT:
3994 if (goi->argc == 0) {
3995 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3996 return JIM_ERR;
3999 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
4000 if (e != JIM_OK) {
4001 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
4002 return e;
4005 if (goi->isconfigure) {
4006 if (goi->argc != 1) {
4007 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
4008 return JIM_ERR;
4010 } else {
4011 if (goi->argc != 0) {
4012 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
4013 return JIM_ERR;
4018 struct target_event_action *teap;
4020 teap = target->event_action;
4021 /* replace existing? */
4022 while (teap) {
4023 if (teap->event == (enum target_event)n->value) {
4024 break;
4026 teap = teap->next;
4029 if (goi->isconfigure) {
4030 bool replace = true;
4031 if (teap == NULL) {
4032 /* create new */
4033 teap = calloc(1, sizeof(*teap));
4034 replace = false;
4036 teap->event = n->value;
4037 teap->interp = goi->interp;
4038 Jim_GetOpt_Obj(goi, &o);
4039 if (teap->body) {
4040 Jim_DecrRefCount(teap->interp, teap->body);
4042 teap->body = Jim_DuplicateObj(goi->interp, o);
4044 * FIXME:
4045 * Tcl/TK - "tk events" have a nice feature.
4046 * See the "BIND" command.
4047 * We should support that here.
4048 * You can specify %X and %Y in the event code.
4049 * The idea is: %T - target name.
4050 * The idea is: %N - target number
4051 * The idea is: %E - event name.
4053 Jim_IncrRefCount(teap->body);
4055 if (!replace)
4057 /* add to head of event list */
4058 teap->next = target->event_action;
4059 target->event_action = teap;
4061 Jim_SetEmptyResult(goi->interp);
4062 } else {
4063 /* get */
4064 if (teap == NULL) {
4065 Jim_SetEmptyResult(goi->interp);
4066 } else {
4067 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
4071 /* loop for more */
4072 break;
4074 case TCFG_WORK_AREA_VIRT:
4075 if (goi->isconfigure) {
4076 target_free_all_working_areas(target);
4077 e = Jim_GetOpt_Wide(goi, &w);
4078 if (e != JIM_OK) {
4079 return e;
4081 target->working_area_virt = w;
4082 target->working_area_virt_spec = true;
4083 } else {
4084 if (goi->argc != 0) {
4085 goto no_params;
4088 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
4089 /* loop for more */
4090 break;
4092 case TCFG_WORK_AREA_PHYS:
4093 if (goi->isconfigure) {
4094 target_free_all_working_areas(target);
4095 e = Jim_GetOpt_Wide(goi, &w);
4096 if (e != JIM_OK) {
4097 return e;
4099 target->working_area_phys = w;
4100 target->working_area_phys_spec = true;
4101 } else {
4102 if (goi->argc != 0) {
4103 goto no_params;
4106 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
4107 /* loop for more */
4108 break;
4110 case TCFG_WORK_AREA_SIZE:
4111 if (goi->isconfigure) {
4112 target_free_all_working_areas(target);
4113 e = Jim_GetOpt_Wide(goi, &w);
4114 if (e != JIM_OK) {
4115 return e;
4117 target->working_area_size = w;
4118 } else {
4119 if (goi->argc != 0) {
4120 goto no_params;
4123 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
4124 /* loop for more */
4125 break;
4127 case TCFG_WORK_AREA_BACKUP:
4128 if (goi->isconfigure) {
4129 target_free_all_working_areas(target);
4130 e = Jim_GetOpt_Wide(goi, &w);
4131 if (e != JIM_OK) {
4132 return e;
4134 /* make this exactly 1 or 0 */
4135 target->backup_working_area = (!!w);
4136 } else {
4137 if (goi->argc != 0) {
4138 goto no_params;
4141 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
4142 /* loop for more e*/
4143 break;
4146 case TCFG_ENDIAN:
4147 if (goi->isconfigure) {
4148 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
4149 if (e != JIM_OK) {
4150 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
4151 return e;
4153 target->endianness = n->value;
4154 } else {
4155 if (goi->argc != 0) {
4156 goto no_params;
4159 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
4160 if (n->name == NULL) {
4161 target->endianness = TARGET_LITTLE_ENDIAN;
4162 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
4164 Jim_SetResultString(goi->interp, n->name, -1);
4165 /* loop for more */
4166 break;
4168 case TCFG_VARIANT:
4169 if (goi->isconfigure) {
4170 if (goi->argc < 1) {
4171 Jim_SetResultFormatted(goi->interp,
4172 "%s ?STRING?",
4173 n->name);
4174 return JIM_ERR;
4176 if (target->variant) {
4177 free((void *)(target->variant));
4179 e = Jim_GetOpt_String(goi, &cp, NULL);
4180 if (e != JIM_OK)
4181 return e;
4182 target->variant = strdup(cp);
4183 } else {
4184 if (goi->argc != 0) {
4185 goto no_params;
4188 Jim_SetResultString(goi->interp, target->variant,-1);
4189 /* loop for more */
4190 break;
4192 case TCFG_COREID:
4193 if (goi->isconfigure) {
4194 e = Jim_GetOpt_Wide(goi, &w);
4195 if (e != JIM_OK) {
4196 return e;
4198 target->coreid = (int32_t)w;
4199 } else {
4200 if (goi->argc != 0) {
4201 goto no_params;
4204 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
4205 /* loop for more */
4206 break;
4208 case TCFG_CHAIN_POSITION:
4209 if (goi->isconfigure) {
4210 Jim_Obj *o_t;
4211 struct jtag_tap *tap;
4212 target_free_all_working_areas(target);
4213 e = Jim_GetOpt_Obj(goi, &o_t);
4214 if (e != JIM_OK) {
4215 return e;
4217 tap = jtag_tap_by_jim_obj(goi->interp, o_t);
4218 if (tap == NULL) {
4219 return JIM_ERR;
4221 /* make this exactly 1 or 0 */
4222 target->tap = tap;
4223 } else {
4224 if (goi->argc != 0) {
4225 goto no_params;
4228 Jim_SetResultString(goi->interp, target->tap->dotted_name, -1);
4229 /* loop for more e*/
4230 break;
4231 case TCFG_DBGBASE:
4232 if (goi->isconfigure) {
4233 e = Jim_GetOpt_Wide(goi, &w);
4234 if (e != JIM_OK) {
4235 return e;
4237 target->dbgbase = (uint32_t)w;
4238 target->dbgbase_set = true;
4239 } else {
4240 if (goi->argc != 0) {
4241 goto no_params;
4244 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->dbgbase));
4245 /* loop for more */
4246 break;
4248 case TCFG_RTOS:
4249 /* RTOS */
4251 int result = rtos_create( goi, target );
4252 if ( result != JIM_OK )
4254 return result;
4257 /* loop for more */
4258 break;
4260 } /* while (goi->argc) */
4263 /* done - we return */
4264 return JIM_OK;
4267 static int
4268 jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4270 Jim_GetOptInfo goi;
4272 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4273 goi.isconfigure = !strcmp(Jim_GetString(argv[0], NULL), "configure");
4274 int need_args = 1 + goi.isconfigure;
4275 if (goi.argc < need_args)
4277 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
4278 goi.isconfigure
4279 ? "missing: -option VALUE ..."
4280 : "missing: -option ...");
4281 return JIM_ERR;
4283 struct target *target = Jim_CmdPrivData(goi.interp);
4284 return target_configure(&goi, target);
4287 static int jim_target_mw(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4289 const char *cmd_name = Jim_GetString(argv[0], NULL);
4291 Jim_GetOptInfo goi;
4292 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4294 if (goi.argc < 2 || goi.argc > 4)
4296 Jim_SetResultFormatted(goi.interp,
4297 "usage: %s [phys] <address> <data> [<count>]", cmd_name);
4298 return JIM_ERR;
4301 target_write_fn fn;
4302 fn = target_write_memory_fast;
4304 int e;
4305 if (strcmp(Jim_GetString(argv[1], NULL), "phys") == 0)
4307 /* consume it */
4308 struct Jim_Obj *obj;
4309 e = Jim_GetOpt_Obj(&goi, &obj);
4310 if (e != JIM_OK)
4311 return e;
4313 fn = target_write_phys_memory;
4316 jim_wide a;
4317 e = Jim_GetOpt_Wide(&goi, &a);
4318 if (e != JIM_OK)
4319 return e;
4321 jim_wide b;
4322 e = Jim_GetOpt_Wide(&goi, &b);
4323 if (e != JIM_OK)
4324 return e;
4326 jim_wide c = 1;
4327 if (goi.argc == 1)
4329 e = Jim_GetOpt_Wide(&goi, &c);
4330 if (e != JIM_OK)
4331 return e;
4334 /* all args must be consumed */
4335 if (goi.argc != 0)
4337 return JIM_ERR;
4340 struct target *target = Jim_CmdPrivData(goi.interp);
4341 unsigned data_size;
4342 if (strcasecmp(cmd_name, "mww") == 0) {
4343 data_size = 4;
4345 else if (strcasecmp(cmd_name, "mwh") == 0) {
4346 data_size = 2;
4348 else if (strcasecmp(cmd_name, "mwb") == 0) {
4349 data_size = 1;
4350 } else {
4351 LOG_ERROR("command '%s' unknown: ", cmd_name);
4352 return JIM_ERR;
4355 return (target_fill_mem(target, a, fn, data_size, b, c) == ERROR_OK) ? JIM_OK : JIM_ERR;
4358 static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4360 const char *cmd_name = Jim_GetString(argv[0], NULL);
4362 Jim_GetOptInfo goi;
4363 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4365 if ((goi.argc < 1) || (goi.argc > 3))
4367 Jim_SetResultFormatted(goi.interp,
4368 "usage: %s [phys] <address> [<count>]", cmd_name);
4369 return JIM_ERR;
4372 int (*fn)(struct target *target,
4373 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
4374 fn=target_read_memory;
4376 int e;
4377 if (strcmp(Jim_GetString(argv[1], NULL), "phys") == 0)
4379 /* consume it */
4380 struct Jim_Obj *obj;
4381 e = Jim_GetOpt_Obj(&goi, &obj);
4382 if (e != JIM_OK)
4383 return e;
4385 fn=target_read_phys_memory;
4388 jim_wide a;
4389 e = Jim_GetOpt_Wide(&goi, &a);
4390 if (e != JIM_OK) {
4391 return JIM_ERR;
4393 jim_wide c;
4394 if (goi.argc == 1) {
4395 e = Jim_GetOpt_Wide(&goi, &c);
4396 if (e != JIM_OK) {
4397 return JIM_ERR;
4399 } else {
4400 c = 1;
4403 /* all args must be consumed */
4404 if (goi.argc != 0)
4406 return JIM_ERR;
4409 jim_wide b = 1; /* shut up gcc */
4410 if (strcasecmp(cmd_name, "mdw") == 0)
4411 b = 4;
4412 else if (strcasecmp(cmd_name, "mdh") == 0)
4413 b = 2;
4414 else if (strcasecmp(cmd_name, "mdb") == 0)
4415 b = 1;
4416 else {
4417 LOG_ERROR("command '%s' unknown: ", cmd_name);
4418 return JIM_ERR;
4421 /* convert count to "bytes" */
4422 c = c * b;
4424 struct target *target = Jim_CmdPrivData(goi.interp);
4425 uint8_t target_buf[32];
4426 jim_wide x, y, z;
4427 while (c > 0) {
4428 y = c;
4429 if (y > 16) {
4430 y = 16;
4432 e = fn(target, a, b, y / b, target_buf);
4433 if (e != ERROR_OK) {
4434 char tmp[10];
4435 snprintf(tmp, sizeof(tmp), "%08lx", (long)a);
4436 Jim_SetResultFormatted(interp, "error reading target @ 0x%s", tmp);
4437 return JIM_ERR;
4440 command_print(NULL, "0x%08x ", (int)(a));
4441 switch (b) {
4442 case 4:
4443 for (x = 0; x < 16 && x < y; x += 4)
4445 z = target_buffer_get_u32(target, &(target_buf[ x ]));
4446 command_print(NULL, "%08x ", (int)(z));
4448 for (; (x < 16) ; x += 4) {
4449 command_print(NULL, " ");
4451 break;
4452 case 2:
4453 for (x = 0; x < 16 && x < y; x += 2)
4455 z = target_buffer_get_u16(target, &(target_buf[ x ]));
4456 command_print(NULL, "%04x ", (int)(z));
4458 for (; (x < 16) ; x += 2) {
4459 command_print(NULL, " ");
4461 break;
4462 case 1:
4463 default:
4464 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4465 z = target_buffer_get_u8(target, &(target_buf[ x ]));
4466 command_print(NULL, "%02x ", (int)(z));
4468 for (; (x < 16) ; x += 1) {
4469 command_print(NULL, " ");
4471 break;
4473 /* ascii-ify the bytes */
4474 for (x = 0 ; x < y ; x++) {
4475 if ((target_buf[x] >= 0x20) &&
4476 (target_buf[x] <= 0x7e)) {
4477 /* good */
4478 } else {
4479 /* smack it */
4480 target_buf[x] = '.';
4483 /* space pad */
4484 while (x < 16) {
4485 target_buf[x] = ' ';
4486 x++;
4488 /* terminate */
4489 target_buf[16] = 0;
4490 /* print - with a newline */
4491 command_print(NULL, "%s\n", target_buf);
4492 /* NEXT... */
4493 c -= 16;
4494 a += 16;
4496 return JIM_OK;
4499 static int jim_target_mem2array(Jim_Interp *interp,
4500 int argc, Jim_Obj *const *argv)
4502 struct target *target = Jim_CmdPrivData(interp);
4503 return target_mem2array(interp, target, argc - 1, argv + 1);
4506 static int jim_target_array2mem(Jim_Interp *interp,
4507 int argc, Jim_Obj *const *argv)
4509 struct target *target = Jim_CmdPrivData(interp);
4510 return target_array2mem(interp, target, argc - 1, argv + 1);
4513 static int jim_target_tap_disabled(Jim_Interp *interp)
4515 Jim_SetResultFormatted(interp, "[TAP is disabled]");
4516 return JIM_ERR;
4519 static int jim_target_examine(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4521 if (argc != 1)
4523 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4524 return JIM_ERR;
4526 struct target *target = Jim_CmdPrivData(interp);
4527 if (!target->tap->enabled)
4528 return jim_target_tap_disabled(interp);
4530 int e = target->type->examine(target);
4531 if (e != ERROR_OK)
4533 return JIM_ERR;
4535 return JIM_OK;
4538 static int jim_target_halt_gdb(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4540 if (argc != 1)
4542 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4543 return JIM_ERR;
4545 struct target *target = Jim_CmdPrivData(interp);
4547 if (target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT) != ERROR_OK)
4548 return JIM_ERR;
4550 return JIM_OK;
4553 static int jim_target_poll(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4555 if (argc != 1)
4557 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4558 return JIM_ERR;
4560 struct target *target = Jim_CmdPrivData(interp);
4561 if (!target->tap->enabled)
4562 return jim_target_tap_disabled(interp);
4564 int e;
4565 if (!(target_was_examined(target))) {
4566 e = ERROR_TARGET_NOT_EXAMINED;
4567 } else {
4568 e = target->type->poll(target);
4570 if (e != ERROR_OK)
4572 return JIM_ERR;
4574 return JIM_OK;
4577 static int jim_target_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4579 Jim_GetOptInfo goi;
4580 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4582 if (goi.argc != 2)
4584 Jim_WrongNumArgs(interp, 0, argv,
4585 "([tT]|[fF]|assert|deassert) BOOL");
4586 return JIM_ERR;
4589 Jim_Nvp *n;
4590 int e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4591 if (e != JIM_OK)
4593 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4594 return e;
4596 /* the halt or not param */
4597 jim_wide a;
4598 e = Jim_GetOpt_Wide(&goi, &a);
4599 if (e != JIM_OK)
4600 return e;
4602 struct target *target = Jim_CmdPrivData(goi.interp);
4603 if (!target->tap->enabled)
4604 return jim_target_tap_disabled(interp);
4605 if (!(target_was_examined(target)))
4607 LOG_ERROR("Target not examined yet");
4608 return ERROR_TARGET_NOT_EXAMINED;
4610 if (!target->type->assert_reset || !target->type->deassert_reset)
4612 Jim_SetResultFormatted(interp,
4613 "No target-specific reset for %s",
4614 target_name(target));
4615 return JIM_ERR;
4617 /* determine if we should halt or not. */
4618 target->reset_halt = !!a;
4619 /* When this happens - all workareas are invalid. */
4620 target_free_all_working_areas_restore(target, 0);
4622 /* do the assert */
4623 if (n->value == NVP_ASSERT) {
4624 e = target->type->assert_reset(target);
4625 } else {
4626 e = target->type->deassert_reset(target);
4628 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4631 static int jim_target_halt(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4633 if (argc != 1) {
4634 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4635 return JIM_ERR;
4637 struct target *target = Jim_CmdPrivData(interp);
4638 if (!target->tap->enabled)
4639 return jim_target_tap_disabled(interp);
4640 int e = target->type->halt(target);
4641 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4644 static int jim_target_wait_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4646 Jim_GetOptInfo goi;
4647 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4649 /* params: <name> statename timeoutmsecs */
4650 if (goi.argc != 2)
4652 const char *cmd_name = Jim_GetString(argv[0], NULL);
4653 Jim_SetResultFormatted(goi.interp,
4654 "%s <state_name> <timeout_in_msec>", cmd_name);
4655 return JIM_ERR;
4658 Jim_Nvp *n;
4659 int e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4660 if (e != JIM_OK) {
4661 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4662 return e;
4664 jim_wide a;
4665 e = Jim_GetOpt_Wide(&goi, &a);
4666 if (e != JIM_OK) {
4667 return e;
4669 struct target *target = Jim_CmdPrivData(interp);
4670 if (!target->tap->enabled)
4671 return jim_target_tap_disabled(interp);
4673 e = target_wait_state(target, n->value, a);
4674 if (e != ERROR_OK)
4676 Jim_Obj *eObj = Jim_NewIntObj(interp, e);
4677 Jim_SetResultFormatted(goi.interp,
4678 "target: %s wait %s fails (%#s) %s",
4679 target_name(target), n->name,
4680 eObj, target_strerror_safe(e));
4681 Jim_FreeNewObj(interp, eObj);
4682 return JIM_ERR;
4684 return JIM_OK;
4686 /* List for human, Events defined for this target.
4687 * scripts/programs should use 'name cget -event NAME'
4689 static int jim_target_event_list(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4691 struct command_context *cmd_ctx = current_command_context(interp);
4692 assert (cmd_ctx != NULL);
4694 struct target *target = Jim_CmdPrivData(interp);
4695 struct target_event_action *teap = target->event_action;
4696 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4697 target->target_number,
4698 target_name(target));
4699 command_print(cmd_ctx, "%-25s | Body", "Event");
4700 command_print(cmd_ctx, "------------------------- | "
4701 "----------------------------------------");
4702 while (teap)
4704 Jim_Nvp *opt = Jim_Nvp_value2name_simple(nvp_target_event, teap->event);
4705 command_print(cmd_ctx, "%-25s | %s",
4706 opt->name, Jim_GetString(teap->body, NULL));
4707 teap = teap->next;
4709 command_print(cmd_ctx, "***END***");
4710 return JIM_OK;
4712 static int jim_target_current_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4714 if (argc != 1)
4716 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4717 return JIM_ERR;
4719 struct target *target = Jim_CmdPrivData(interp);
4720 Jim_SetResultString(interp, target_state_name(target), -1);
4721 return JIM_OK;
4723 static int jim_target_invoke_event(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4725 Jim_GetOptInfo goi;
4726 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4727 if (goi.argc != 1)
4729 const char *cmd_name = Jim_GetString(argv[0], NULL);
4730 Jim_SetResultFormatted(goi.interp, "%s <eventname>", cmd_name);
4731 return JIM_ERR;
4733 Jim_Nvp *n;
4734 int e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4735 if (e != JIM_OK)
4737 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4738 return e;
4740 struct target *target = Jim_CmdPrivData(interp);
4741 target_handle_event(target, n->value);
4742 return JIM_OK;
4745 static const struct command_registration target_instance_command_handlers[] = {
4747 .name = "configure",
4748 .mode = COMMAND_CONFIG,
4749 .jim_handler = jim_target_configure,
4750 .help = "configure a new target for use",
4751 .usage = "[target_attribute ...]",
4754 .name = "cget",
4755 .mode = COMMAND_ANY,
4756 .jim_handler = jim_target_configure,
4757 .help = "returns the specified target attribute",
4758 .usage = "target_attribute",
4761 .name = "mww",
4762 .mode = COMMAND_EXEC,
4763 .jim_handler = jim_target_mw,
4764 .help = "Write 32-bit word(s) to target memory",
4765 .usage = "address data [count]",
4768 .name = "mwh",
4769 .mode = COMMAND_EXEC,
4770 .jim_handler = jim_target_mw,
4771 .help = "Write 16-bit half-word(s) to target memory",
4772 .usage = "address data [count]",
4775 .name = "mwb",
4776 .mode = COMMAND_EXEC,
4777 .jim_handler = jim_target_mw,
4778 .help = "Write byte(s) to target memory",
4779 .usage = "address data [count]",
4782 .name = "mdw",
4783 .mode = COMMAND_EXEC,
4784 .jim_handler = jim_target_md,
4785 .help = "Display target memory as 32-bit words",
4786 .usage = "address [count]",
4789 .name = "mdh",
4790 .mode = COMMAND_EXEC,
4791 .jim_handler = jim_target_md,
4792 .help = "Display target memory as 16-bit half-words",
4793 .usage = "address [count]",
4796 .name = "mdb",
4797 .mode = COMMAND_EXEC,
4798 .jim_handler = jim_target_md,
4799 .help = "Display target memory as 8-bit bytes",
4800 .usage = "address [count]",
4803 .name = "array2mem",
4804 .mode = COMMAND_EXEC,
4805 .jim_handler = jim_target_array2mem,
4806 .help = "Writes Tcl array of 8/16/32 bit numbers "
4807 "to target memory",
4808 .usage = "arrayname bitwidth address count",
4811 .name = "mem2array",
4812 .mode = COMMAND_EXEC,
4813 .jim_handler = jim_target_mem2array,
4814 .help = "Loads Tcl array of 8/16/32 bit numbers "
4815 "from target memory",
4816 .usage = "arrayname bitwidth address count",
4819 .name = "eventlist",
4820 .mode = COMMAND_EXEC,
4821 .jim_handler = jim_target_event_list,
4822 .help = "displays a table of events defined for this target",
4825 .name = "curstate",
4826 .mode = COMMAND_EXEC,
4827 .jim_handler = jim_target_current_state,
4828 .help = "displays the current state of this target",
4831 .name = "arp_examine",
4832 .mode = COMMAND_EXEC,
4833 .jim_handler = jim_target_examine,
4834 .help = "used internally for reset processing",
4837 .name = "arp_halt_gdb",
4838 .mode = COMMAND_EXEC,
4839 .jim_handler = jim_target_halt_gdb,
4840 .help = "used internally for reset processing to halt GDB",
4843 .name = "arp_poll",
4844 .mode = COMMAND_EXEC,
4845 .jim_handler = jim_target_poll,
4846 .help = "used internally for reset processing",
4849 .name = "arp_reset",
4850 .mode = COMMAND_EXEC,
4851 .jim_handler = jim_target_reset,
4852 .help = "used internally for reset processing",
4855 .name = "arp_halt",
4856 .mode = COMMAND_EXEC,
4857 .jim_handler = jim_target_halt,
4858 .help = "used internally for reset processing",
4861 .name = "arp_waitstate",
4862 .mode = COMMAND_EXEC,
4863 .jim_handler = jim_target_wait_state,
4864 .help = "used internally for reset processing",
4867 .name = "invoke-event",
4868 .mode = COMMAND_EXEC,
4869 .jim_handler = jim_target_invoke_event,
4870 .help = "invoke handler for specified event",
4871 .usage = "event_name",
4873 COMMAND_REGISTRATION_DONE
4876 static int target_create(Jim_GetOptInfo *goi)
4878 Jim_Obj *new_cmd;
4879 Jim_Cmd *cmd;
4880 const char *cp;
4881 char *cp2;
4882 int e;
4883 int x;
4884 struct target *target;
4885 struct command_context *cmd_ctx;
4887 cmd_ctx = current_command_context(goi->interp);
4888 assert (cmd_ctx != NULL);
4890 if (goi->argc < 3) {
4891 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4892 return JIM_ERR;
4895 /* COMMAND */
4896 Jim_GetOpt_Obj(goi, &new_cmd);
4897 /* does this command exist? */
4898 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4899 if (cmd) {
4900 cp = Jim_GetString(new_cmd, NULL);
4901 Jim_SetResultFormatted(goi->interp, "Command/target: %s Exists", cp);
4902 return JIM_ERR;
4905 /* TYPE */
4906 e = Jim_GetOpt_String(goi, &cp2, NULL);
4907 if (e != JIM_OK)
4908 return e;
4909 cp = cp2;
4910 /* now does target type exist */
4911 for (x = 0 ; target_types[x] ; x++) {
4912 if (0 == strcmp(cp, target_types[x]->name)) {
4913 /* found */
4914 break;
4917 if (target_types[x] == NULL) {
4918 Jim_SetResultFormatted(goi->interp, "Unknown target type %s, try one of ", cp);
4919 for (x = 0 ; target_types[x] ; x++) {
4920 if (target_types[x + 1]) {
4921 Jim_AppendStrings(goi->interp,
4922 Jim_GetResult(goi->interp),
4923 target_types[x]->name,
4924 ", ", NULL);
4925 } else {
4926 Jim_AppendStrings(goi->interp,
4927 Jim_GetResult(goi->interp),
4928 " or ",
4929 target_types[x]->name,NULL);
4932 return JIM_ERR;
4935 /* Create it */
4936 target = calloc(1,sizeof(struct target));
4937 /* set target number */
4938 target->target_number = new_target_number();
4940 /* allocate memory for each unique target type */
4941 target->type = (struct target_type*)calloc(1,sizeof(struct target_type));
4943 memcpy(target->type, target_types[x], sizeof(struct target_type));
4945 /* will be set by "-endian" */
4946 target->endianness = TARGET_ENDIAN_UNKNOWN;
4948 /* default to first core, override with -coreid */
4949 target->coreid = 0;
4951 target->working_area = 0x0;
4952 target->working_area_size = 0x0;
4953 target->working_areas = NULL;
4954 target->backup_working_area = 0;
4956 target->state = TARGET_UNKNOWN;
4957 target->debug_reason = DBG_REASON_UNDEFINED;
4958 target->reg_cache = NULL;
4959 target->breakpoints = NULL;
4960 target->watchpoints = NULL;
4961 target->next = NULL;
4962 target->arch_info = NULL;
4964 target->display = 1;
4966 target->halt_issued = false;
4968 /* initialize trace information */
4969 target->trace_info = malloc(sizeof(struct trace));
4970 target->trace_info->num_trace_points = 0;
4971 target->trace_info->trace_points_size = 0;
4972 target->trace_info->trace_points = NULL;
4973 target->trace_info->trace_history_size = 0;
4974 target->trace_info->trace_history = NULL;
4975 target->trace_info->trace_history_pos = 0;
4976 target->trace_info->trace_history_overflowed = 0;
4978 target->dbgmsg = NULL;
4979 target->dbg_msg_enabled = 0;
4981 target->endianness = TARGET_ENDIAN_UNKNOWN;
4983 target->rtos = NULL;
4984 target->rtos_auto_detect = false;
4986 /* Do the rest as "configure" options */
4987 goi->isconfigure = 1;
4988 e = target_configure(goi, target);
4990 if (target->tap == NULL)
4992 Jim_SetResultString(goi->interp, "-chain-position required when creating target", -1);
4993 e = JIM_ERR;
4996 if (e != JIM_OK) {
4997 free(target->type);
4998 free(target);
4999 return e;
5002 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
5003 /* default endian to little if not specified */
5004 target->endianness = TARGET_LITTLE_ENDIAN;
5007 /* incase variant is not set */
5008 if (!target->variant)
5009 target->variant = strdup("");
5011 cp = Jim_GetString(new_cmd, NULL);
5012 target->cmd_name = strdup(cp);
5014 /* create the target specific commands */
5015 if (target->type->commands) {
5016 e = register_commands(cmd_ctx, NULL, target->type->commands);
5017 if (ERROR_OK != e)
5018 LOG_ERROR("unable to register '%s' commands", cp);
5020 if (target->type->target_create) {
5021 (*(target->type->target_create))(target, goi->interp);
5024 /* append to end of list */
5026 struct target **tpp;
5027 tpp = &(all_targets);
5028 while (*tpp) {
5029 tpp = &((*tpp)->next);
5031 *tpp = target;
5034 /* now - create the new target name command */
5035 const const struct command_registration target_subcommands[] = {
5037 .chain = target_instance_command_handlers,
5040 .chain = target->type->commands,
5042 COMMAND_REGISTRATION_DONE
5044 const const struct command_registration target_commands[] = {
5046 .name = cp,
5047 .mode = COMMAND_ANY,
5048 .help = "target command group",
5049 .chain = target_subcommands,
5051 COMMAND_REGISTRATION_DONE
5053 e = register_commands(cmd_ctx, NULL, target_commands);
5054 if (ERROR_OK != e)
5055 return JIM_ERR;
5057 struct command *c = command_find_in_context(cmd_ctx, cp);
5058 assert(c);
5059 command_set_handler_data(c, target);
5061 return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
5064 static int jim_target_current(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5066 if (argc != 1)
5068 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5069 return JIM_ERR;
5071 struct command_context *cmd_ctx = current_command_context(interp);
5072 assert (cmd_ctx != NULL);
5074 Jim_SetResultString(interp, get_current_target(cmd_ctx)->cmd_name, -1);
5075 return JIM_OK;
5078 static int jim_target_types(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5080 if (argc != 1)
5082 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5083 return JIM_ERR;
5085 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
5086 for (unsigned x = 0; NULL != target_types[x]; x++)
5088 Jim_ListAppendElement(interp, Jim_GetResult(interp),
5089 Jim_NewStringObj(interp, target_types[x]->name, -1));
5091 return JIM_OK;
5094 static int jim_target_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5096 if (argc != 1)
5098 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5099 return JIM_ERR;
5101 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
5102 struct target *target = all_targets;
5103 while (target)
5105 Jim_ListAppendElement(interp, Jim_GetResult(interp),
5106 Jim_NewStringObj(interp, target_name(target), -1));
5107 target = target->next;
5109 return JIM_OK;
5112 static int jim_target_smp(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5114 int i;
5115 const char *targetname;
5116 int retval,len;
5117 struct target *target;
5118 struct target_list *head, *curr;
5119 curr = (struct target_list*) NULL;
5120 head = (struct target_list*) NULL;
5122 retval = 0;
5123 LOG_DEBUG("%d",argc);
5124 /* argv[1] = target to associate in smp
5125 * argv[2] = target to assoicate in smp
5126 * argv[3] ...
5129 for(i=1;i<argc;i++)
5132 targetname = Jim_GetString(argv[i], &len);
5133 target = get_target(targetname);
5134 LOG_DEBUG("%s ",targetname);
5135 if (target)
5137 struct target_list *new;
5138 new=malloc(sizeof(struct target_list));
5139 new->target = target;
5140 new->next = (struct target_list*)NULL;
5141 if (head == (struct target_list*)NULL)
5143 head = new;
5144 curr = head;
5146 else
5148 curr->next = new;
5149 curr = new;
5153 /* now parse the list of cpu and put the target in smp mode*/
5154 curr=head;
5156 while(curr!=(struct target_list *)NULL)
5158 target=curr->target;
5159 target->smp = 1;
5160 target->head = head;
5161 curr=curr->next;
5163 return retval;
5167 static int jim_target_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5169 Jim_GetOptInfo goi;
5170 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5171 if (goi.argc < 3)
5173 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
5174 "<name> <target_type> [<target_options> ...]");
5175 return JIM_ERR;
5177 return target_create(&goi);
5180 static int jim_target_number(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5182 Jim_GetOptInfo goi;
5183 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5185 /* It's OK to remove this mechanism sometime after August 2010 or so */
5186 LOG_WARNING("don't use numbers as target identifiers; use names");
5187 if (goi.argc != 1)
5189 Jim_SetResultFormatted(goi.interp, "usage: target number <number>");
5190 return JIM_ERR;
5192 jim_wide w;
5193 int e = Jim_GetOpt_Wide(&goi, &w);
5194 if (e != JIM_OK)
5195 return JIM_ERR;
5197 struct target *target;
5198 for (target = all_targets; NULL != target; target = target->next)
5200 if (target->target_number != w)
5201 continue;
5203 Jim_SetResultString(goi.interp, target_name(target), -1);
5204 return JIM_OK;
5207 Jim_Obj *wObj = Jim_NewIntObj(goi.interp, w);
5208 Jim_SetResultFormatted(goi.interp,
5209 "Target: number %#s does not exist", wObj);
5210 Jim_FreeNewObj(interp, wObj);
5212 return JIM_ERR;
5215 static int jim_target_count(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5217 if (argc != 1)
5219 Jim_WrongNumArgs(interp, 1, argv, "<no parameters>");
5220 return JIM_ERR;
5222 unsigned count = 0;
5223 struct target *target = all_targets;
5224 while (NULL != target)
5226 target = target->next;
5227 count++;
5229 Jim_SetResult(interp, Jim_NewIntObj(interp, count));
5230 return JIM_OK;
5233 static const struct command_registration target_subcommand_handlers[] = {
5235 .name = "init",
5236 .mode = COMMAND_CONFIG,
5237 .handler = handle_target_init_command,
5238 .help = "initialize targets",
5241 .name = "create",
5242 /* REVISIT this should be COMMAND_CONFIG ... */
5243 .mode = COMMAND_ANY,
5244 .jim_handler = jim_target_create,
5245 .usage = "name type '-chain-position' name [options ...]",
5246 .help = "Creates and selects a new target",
5249 .name = "current",
5250 .mode = COMMAND_ANY,
5251 .jim_handler = jim_target_current,
5252 .help = "Returns the currently selected target",
5255 .name = "types",
5256 .mode = COMMAND_ANY,
5257 .jim_handler = jim_target_types,
5258 .help = "Returns the available target types as "
5259 "a list of strings",
5262 .name = "names",
5263 .mode = COMMAND_ANY,
5264 .jim_handler = jim_target_names,
5265 .help = "Returns the names of all targets as a list of strings",
5268 .name = "number",
5269 .mode = COMMAND_ANY,
5270 .jim_handler = jim_target_number,
5271 .usage = "number",
5272 .help = "Returns the name of the numbered target "
5273 "(DEPRECATED)",
5276 .name = "count",
5277 .mode = COMMAND_ANY,
5278 .jim_handler = jim_target_count,
5279 .help = "Returns the number of targets as an integer "
5280 "(DEPRECATED)",
5283 .name = "smp",
5284 .mode = COMMAND_ANY,
5285 .jim_handler = jim_target_smp,
5286 .usage = "targetname1 targetname2 ...",
5287 .help = "gather several target in a smp list"
5290 COMMAND_REGISTRATION_DONE
5293 struct FastLoad
5295 uint32_t address;
5296 uint8_t *data;
5297 int length;
5301 static int fastload_num;
5302 static struct FastLoad *fastload;
5304 static void free_fastload(void)
5306 if (fastload != NULL)
5308 int i;
5309 for (i = 0; i < fastload_num; i++)
5311 if (fastload[i].data)
5312 free(fastload[i].data);
5314 free(fastload);
5315 fastload = NULL;
5322 COMMAND_HANDLER(handle_fast_load_image_command)
5324 uint8_t *buffer;
5325 size_t buf_cnt;
5326 uint32_t image_size;
5327 uint32_t min_address = 0;
5328 uint32_t max_address = 0xffffffff;
5329 int i;
5331 struct image image;
5333 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
5334 &image, &min_address, &max_address);
5335 if (ERROR_OK != retval)
5336 return retval;
5338 struct duration bench;
5339 duration_start(&bench);
5341 retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL);
5342 if (retval != ERROR_OK)
5344 return retval;
5347 image_size = 0x0;
5348 retval = ERROR_OK;
5349 fastload_num = image.num_sections;
5350 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
5351 if (fastload == NULL)
5353 command_print(CMD_CTX, "out of memory");
5354 image_close(&image);
5355 return ERROR_FAIL;
5357 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
5358 for (i = 0; i < image.num_sections; i++)
5360 buffer = malloc(image.sections[i].size);
5361 if (buffer == NULL)
5363 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
5364 (int)(image.sections[i].size));
5365 retval = ERROR_FAIL;
5366 break;
5369 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
5371 free(buffer);
5372 break;
5375 uint32_t offset = 0;
5376 uint32_t length = buf_cnt;
5379 /* DANGER!!! beware of unsigned comparision here!!! */
5381 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
5382 (image.sections[i].base_address < max_address))
5384 if (image.sections[i].base_address < min_address)
5386 /* clip addresses below */
5387 offset += min_address-image.sections[i].base_address;
5388 length -= offset;
5391 if (image.sections[i].base_address + buf_cnt > max_address)
5393 length -= (image.sections[i].base_address + buf_cnt)-max_address;
5396 fastload[i].address = image.sections[i].base_address + offset;
5397 fastload[i].data = malloc(length);
5398 if (fastload[i].data == NULL)
5400 free(buffer);
5401 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
5402 length);
5403 retval = ERROR_FAIL;
5404 break;
5406 memcpy(fastload[i].data, buffer + offset, length);
5407 fastload[i].length = length;
5409 image_size += length;
5410 command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
5411 (unsigned int)length,
5412 ((unsigned int)(image.sections[i].base_address + offset)));
5415 free(buffer);
5418 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
5420 command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
5421 "in %fs (%0.3f KiB/s)", image_size,
5422 duration_elapsed(&bench), duration_kbps(&bench, image_size));
5424 command_print(CMD_CTX,
5425 "WARNING: image has not been loaded to target!"
5426 "You can issue a 'fast_load' to finish loading.");
5429 image_close(&image);
5431 if (retval != ERROR_OK)
5433 free_fastload();
5436 return retval;
5439 COMMAND_HANDLER(handle_fast_load_command)
5441 if (CMD_ARGC > 0)
5442 return ERROR_COMMAND_SYNTAX_ERROR;
5443 if (fastload == NULL)
5445 LOG_ERROR("No image in memory");
5446 return ERROR_FAIL;
5448 int i;
5449 int ms = timeval_ms();
5450 int size = 0;
5451 int retval = ERROR_OK;
5452 for (i = 0; i < fastload_num;i++)
5454 struct target *target = get_current_target(CMD_CTX);
5455 command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
5456 (unsigned int)(fastload[i].address),
5457 (unsigned int)(fastload[i].length));
5458 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
5459 if (retval != ERROR_OK)
5461 break;
5463 size += fastload[i].length;
5465 if (retval == ERROR_OK)
5467 int after = timeval_ms();
5468 command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
5470 return retval;
5473 static const struct command_registration target_command_handlers[] = {
5475 .name = "targets",
5476 .handler = handle_targets_command,
5477 .mode = COMMAND_ANY,
5478 .help = "change current default target (one parameter) "
5479 "or prints table of all targets (no parameters)",
5480 .usage = "[target]",
5483 .name = "target",
5484 .mode = COMMAND_CONFIG,
5485 .help = "configure target",
5487 .chain = target_subcommand_handlers,
5489 COMMAND_REGISTRATION_DONE
5492 int target_register_commands(struct command_context *cmd_ctx)
5494 return register_commands(cmd_ctx, NULL, target_command_handlers);
5497 static bool target_reset_nag = true;
5499 bool get_target_reset_nag(void)
5501 return target_reset_nag;
5504 COMMAND_HANDLER(handle_target_reset_nag)
5506 return CALL_COMMAND_HANDLER(handle_command_parse_bool,
5507 &target_reset_nag, "Nag after each reset about options to improve "
5508 "performance");
5511 static const struct command_registration target_exec_command_handlers[] = {
5513 .name = "fast_load_image",
5514 .handler = handle_fast_load_image_command,
5515 .mode = COMMAND_ANY,
5516 .help = "Load image into server memory for later use by "
5517 "fast_load; primarily for profiling",
5518 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
5519 "[min_address [max_length]]",
5522 .name = "fast_load",
5523 .handler = handle_fast_load_command,
5524 .mode = COMMAND_EXEC,
5525 .help = "loads active fast load image to current target "
5526 "- mainly for profiling purposes",
5529 .name = "profile",
5530 .handler = handle_profile_command,
5531 .mode = COMMAND_EXEC,
5532 .help = "profiling samples the CPU PC",
5534 /** @todo don't register virt2phys() unless target supports it */
5536 .name = "virt2phys",
5537 .handler = handle_virt2phys_command,
5538 .mode = COMMAND_ANY,
5539 .help = "translate a virtual address into a physical address",
5540 .usage = "virtual_address",
5543 .name = "reg",
5544 .handler = handle_reg_command,
5545 .mode = COMMAND_EXEC,
5546 .help = "display or set a register; with no arguments, "
5547 "displays all registers and their values",
5548 .usage = "[(register_name|register_number) [value]]",
5551 .name = "poll",
5552 .handler = handle_poll_command,
5553 .mode = COMMAND_EXEC,
5554 .help = "poll target state; or reconfigure background polling",
5555 .usage = "['on'|'off']",
5558 .name = "wait_halt",
5559 .handler = handle_wait_halt_command,
5560 .mode = COMMAND_EXEC,
5561 .help = "wait up to the specified number of milliseconds "
5562 "(default 5) for a previously requested halt",
5563 .usage = "[milliseconds]",
5566 .name = "halt",
5567 .handler = handle_halt_command,
5568 .mode = COMMAND_EXEC,
5569 .help = "request target to halt, then wait up to the specified"
5570 "number of milliseconds (default 5) for it to complete",
5571 .usage = "[milliseconds]",
5574 .name = "resume",
5575 .handler = handle_resume_command,
5576 .mode = COMMAND_EXEC,
5577 .help = "resume target execution from current PC or address",
5578 .usage = "[address]",
5581 .name = "reset",
5582 .handler = handle_reset_command,
5583 .mode = COMMAND_EXEC,
5584 .usage = "[run|halt|init]",
5585 .help = "Reset all targets into the specified mode."
5586 "Default reset mode is run, if not given.",
5589 .name = "soft_reset_halt",
5590 .handler = handle_soft_reset_halt_command,
5591 .mode = COMMAND_EXEC,
5592 .help = "halt the target and do a soft reset",
5595 .name = "step",
5596 .handler = handle_step_command,
5597 .mode = COMMAND_EXEC,
5598 .help = "step one instruction from current PC or address",
5599 .usage = "[address]",
5602 .name = "mdw",
5603 .handler = handle_md_command,
5604 .mode = COMMAND_EXEC,
5605 .help = "display memory words",
5606 .usage = "['phys'] address [count]",
5609 .name = "mdh",
5610 .handler = handle_md_command,
5611 .mode = COMMAND_EXEC,
5612 .help = "display memory half-words",
5613 .usage = "['phys'] address [count]",
5616 .name = "mdb",
5617 .handler = handle_md_command,
5618 .mode = COMMAND_EXEC,
5619 .help = "display memory bytes",
5620 .usage = "['phys'] address [count]",
5623 .name = "mww",
5624 .handler = handle_mw_command,
5625 .mode = COMMAND_EXEC,
5626 .help = "write memory word",
5627 .usage = "['phys'] address value [count]",
5630 .name = "mwh",
5631 .handler = handle_mw_command,
5632 .mode = COMMAND_EXEC,
5633 .help = "write memory half-word",
5634 .usage = "['phys'] address value [count]",
5637 .name = "mwb",
5638 .handler = handle_mw_command,
5639 .mode = COMMAND_EXEC,
5640 .help = "write memory byte",
5641 .usage = "['phys'] address value [count]",
5644 .name = "bp",
5645 .handler = handle_bp_command,
5646 .mode = COMMAND_EXEC,
5647 .help = "list or set hardware or software breakpoint",
5648 .usage = "usage: bp <address> [<asid>]<length> ['hw'|'hw_ctx']",
5651 .name = "rbp",
5652 .handler = handle_rbp_command,
5653 .mode = COMMAND_EXEC,
5654 .help = "remove breakpoint",
5655 .usage = "address",
5658 .name = "wp",
5659 .handler = handle_wp_command,
5660 .mode = COMMAND_EXEC,
5661 .help = "list (no params) or create watchpoints",
5662 .usage = "[address length [('r'|'w'|'a') value [mask]]]",
5665 .name = "rwp",
5666 .handler = handle_rwp_command,
5667 .mode = COMMAND_EXEC,
5668 .help = "remove watchpoint",
5669 .usage = "address",
5672 .name = "load_image",
5673 .handler = handle_load_image_command,
5674 .mode = COMMAND_EXEC,
5675 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
5676 "[min_address] [max_length]",
5679 .name = "dump_image",
5680 .handler = handle_dump_image_command,
5681 .mode = COMMAND_EXEC,
5682 .usage = "filename address size",
5685 .name = "verify_image",
5686 .handler = handle_verify_image_command,
5687 .mode = COMMAND_EXEC,
5688 .usage = "filename [offset [type]]",
5691 .name = "test_image",
5692 .handler = handle_test_image_command,
5693 .mode = COMMAND_EXEC,
5694 .usage = "filename [offset [type]]",
5697 .name = "mem2array",
5698 .mode = COMMAND_EXEC,
5699 .jim_handler = jim_mem2array,
5700 .help = "read 8/16/32 bit memory and return as a TCL array "
5701 "for script processing",
5702 .usage = "arrayname bitwidth address count",
5705 .name = "array2mem",
5706 .mode = COMMAND_EXEC,
5707 .jim_handler = jim_array2mem,
5708 .help = "convert a TCL array to memory locations "
5709 "and write the 8/16/32 bit values",
5710 .usage = "arrayname bitwidth address count",
5713 .name = "reset_nag",
5714 .handler = handle_target_reset_nag,
5715 .mode = COMMAND_ANY,
5716 .help = "Nag after each reset about options that could have been "
5717 "enabled to improve performance. ",
5718 .usage = "['enable'|'disable']",
5720 COMMAND_REGISTRATION_DONE
5722 static int target_register_user_commands(struct command_context *cmd_ctx)
5724 int retval = ERROR_OK;
5725 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
5726 return retval;
5728 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
5729 return retval;
5732 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);