1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
17 * Copyright (C) 2011 by Broadcom Corporation *
18 * Evan Hunter - ehunter@broadcom.com *
20 * Copyright (C) ST-Ericsson SA 2011 *
21 * michel.jaouen@stericsson.com : smp minimum support *
23 * Copyright (C) 2011 Andreas Fritiofson *
24 * andreas.fritiofson@gmail.com *
26 * This program is free software; you can redistribute it and/or modify *
27 * it under the terms of the GNU General Public License as published by *
28 * the Free Software Foundation; either version 2 of the License, or *
29 * (at your option) any later version. *
31 * This program is distributed in the hope that it will be useful, *
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
34 * GNU General Public License for more details. *
36 * You should have received a copy of the GNU General Public License *
37 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
38 ***************************************************************************/
44 #include <helper/time_support.h>
45 #include <jtag/jtag.h>
46 #include <flash/nor/core.h>
49 #include "target_type.h"
50 #include "target_request.h"
51 #include "breakpoints.h"
55 #include "rtos/rtos.h"
56 #include "transport/transport.h"
58 /* default halt wait timeout (ms) */
59 #define DEFAULT_HALT_TIMEOUT 5000
61 static int target_read_buffer_default(struct target *target, target_addr_t address,
62 uint32_t count, uint8_t *buffer);
63 static int target_write_buffer_default(struct target *target, target_addr_t address,
64 uint32_t count, const uint8_t *buffer);
65 static int target_array2mem(Jim_Interp *interp, struct target *target,
66 int argc, Jim_Obj * const *argv);
67 static int target_mem2array(Jim_Interp *interp, struct target *target,
68 int argc, Jim_Obj * const *argv);
69 static int target_register_user_commands(struct command_context *cmd_ctx);
70 static int target_get_gdb_fileio_info_default(struct target *target,
71 struct gdb_fileio_info *fileio_info);
72 static int target_gdb_fileio_end_default(struct target *target, int retcode,
73 int fileio_errno, bool ctrl_c);
74 static int target_profiling_default(struct target *target, uint32_t *samples,
75 uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds);
78 extern struct target_type arm7tdmi_target;
79 extern struct target_type arm720t_target;
80 extern struct target_type arm9tdmi_target;
81 extern struct target_type arm920t_target;
82 extern struct target_type arm966e_target;
83 extern struct target_type arm946e_target;
84 extern struct target_type arm926ejs_target;
85 extern struct target_type fa526_target;
86 extern struct target_type feroceon_target;
87 extern struct target_type dragonite_target;
88 extern struct target_type xscale_target;
89 extern struct target_type cortexm_target;
90 extern struct target_type cortexa_target;
91 extern struct target_type cortexr4_target;
92 extern struct target_type arm11_target;
93 extern struct target_type ls1_sap_target;
94 extern struct target_type mips_m4k_target;
95 extern struct target_type avr_target;
96 extern struct target_type dsp563xx_target;
97 extern struct target_type dsp5680xx_target;
98 extern struct target_type testee_target;
99 extern struct target_type avr32_ap7k_target;
100 extern struct target_type hla_target;
101 extern struct target_type nds32_v2_target;
102 extern struct target_type nds32_v3_target;
103 extern struct target_type nds32_v3m_target;
104 extern struct target_type or1k_target;
105 extern struct target_type quark_x10xx_target;
106 extern struct target_type quark_d20xx_target;
108 static struct target_type *target_types[] = {
141 struct target *all_targets;
142 static struct target_event_callback *target_event_callbacks;
143 static struct target_timer_callback *target_timer_callbacks;
144 LIST_HEAD(target_reset_callback_list);
145 LIST_HEAD(target_trace_callback_list);
146 static const int polling_interval = 100;
148 static const Jim_Nvp nvp_assert[] = {
149 { .name = "assert", NVP_ASSERT },
150 { .name = "deassert", NVP_DEASSERT },
151 { .name = "T", NVP_ASSERT },
152 { .name = "F", NVP_DEASSERT },
153 { .name = "t", NVP_ASSERT },
154 { .name = "f", NVP_DEASSERT },
155 { .name = NULL, .value = -1 }
158 static const Jim_Nvp nvp_error_target[] = {
159 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
160 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
161 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
162 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
163 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
164 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
165 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
166 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
167 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
168 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
169 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
170 { .value = -1, .name = NULL }
173 static const char *target_strerror_safe(int err)
177 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
184 static const Jim_Nvp nvp_target_event[] = {
186 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
187 { .value = TARGET_EVENT_HALTED, .name = "halted" },
188 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
189 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
190 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
192 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
193 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
195 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
196 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
197 { .value = TARGET_EVENT_RESET_ASSERT, .name = "reset-assert" },
198 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
199 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
200 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
201 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
202 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
203 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
204 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
205 { .value = TARGET_EVENT_RESET_INIT, .name = "reset-init" },
206 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
208 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
209 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
211 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
212 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
214 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
215 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
217 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
218 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
220 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
221 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
223 { .value = TARGET_EVENT_TRACE_CONFIG, .name = "trace-config" },
225 { .name = NULL, .value = -1 }
228 static const Jim_Nvp nvp_target_state[] = {
229 { .name = "unknown", .value = TARGET_UNKNOWN },
230 { .name = "running", .value = TARGET_RUNNING },
231 { .name = "halted", .value = TARGET_HALTED },
232 { .name = "reset", .value = TARGET_RESET },
233 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
234 { .name = NULL, .value = -1 },
237 static const Jim_Nvp nvp_target_debug_reason[] = {
238 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
239 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
240 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
241 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
242 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
243 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
244 { .name = "program-exit" , .value = DBG_REASON_EXIT },
245 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
246 { .name = NULL, .value = -1 },
249 static const Jim_Nvp nvp_target_endian[] = {
250 { .name = "big", .value = TARGET_BIG_ENDIAN },
251 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
252 { .name = "be", .value = TARGET_BIG_ENDIAN },
253 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
254 { .name = NULL, .value = -1 },
257 static const Jim_Nvp nvp_reset_modes[] = {
258 { .name = "unknown", .value = RESET_UNKNOWN },
259 { .name = "run" , .value = RESET_RUN },
260 { .name = "halt" , .value = RESET_HALT },
261 { .name = "init" , .value = RESET_INIT },
262 { .name = NULL , .value = -1 },
265 const char *debug_reason_name(struct target *t)
269 cp = Jim_Nvp_value2name_simple(nvp_target_debug_reason,
270 t->debug_reason)->name;
272 LOG_ERROR("Invalid debug reason: %d", (int)(t->debug_reason));
273 cp = "(*BUG*unknown*BUG*)";
278 const char *target_state_name(struct target *t)
281 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
283 LOG_ERROR("Invalid target state: %d", (int)(t->state));
284 cp = "(*BUG*unknown*BUG*)";
287 if (!target_was_examined(t) && t->defer_examine)
288 cp = "examine deferred";
293 const char *target_event_name(enum target_event event)
296 cp = Jim_Nvp_value2name_simple(nvp_target_event, event)->name;
298 LOG_ERROR("Invalid target event: %d", (int)(event));
299 cp = "(*BUG*unknown*BUG*)";
304 const char *target_reset_mode_name(enum target_reset_mode reset_mode)
307 cp = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode)->name;
309 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode));
310 cp = "(*BUG*unknown*BUG*)";
315 /* determine the number of the new target */
316 static int new_target_number(void)
321 /* number is 0 based */
325 if (x < t->target_number)
326 x = t->target_number;
332 /* read a uint64_t from a buffer in target memory endianness */
333 uint64_t target_buffer_get_u64(struct target *target, const uint8_t *buffer)
335 if (target->endianness == TARGET_LITTLE_ENDIAN)
336 return le_to_h_u64(buffer);
338 return be_to_h_u64(buffer);
341 /* read a uint32_t from a buffer in target memory endianness */
342 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
344 if (target->endianness == TARGET_LITTLE_ENDIAN)
345 return le_to_h_u32(buffer);
347 return be_to_h_u32(buffer);
350 /* read a uint24_t from a buffer in target memory endianness */
351 uint32_t target_buffer_get_u24(struct target *target, const uint8_t *buffer)
353 if (target->endianness == TARGET_LITTLE_ENDIAN)
354 return le_to_h_u24(buffer);
356 return be_to_h_u24(buffer);
359 /* read a uint16_t from a buffer in target memory endianness */
360 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
362 if (target->endianness == TARGET_LITTLE_ENDIAN)
363 return le_to_h_u16(buffer);
365 return be_to_h_u16(buffer);
368 /* read a uint8_t from a buffer in target memory endianness */
369 static uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
371 return *buffer & 0x0ff;
374 /* write a uint64_t to a buffer in target memory endianness */
375 void target_buffer_set_u64(struct target *target, uint8_t *buffer, uint64_t value)
377 if (target->endianness == TARGET_LITTLE_ENDIAN)
378 h_u64_to_le(buffer, value);
380 h_u64_to_be(buffer, value);
383 /* write a uint32_t to a buffer in target memory endianness */
384 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
386 if (target->endianness == TARGET_LITTLE_ENDIAN)
387 h_u32_to_le(buffer, value);
389 h_u32_to_be(buffer, value);
392 /* write a uint24_t to a buffer in target memory endianness */
393 void target_buffer_set_u24(struct target *target, uint8_t *buffer, uint32_t value)
395 if (target->endianness == TARGET_LITTLE_ENDIAN)
396 h_u24_to_le(buffer, value);
398 h_u24_to_be(buffer, value);
401 /* write a uint16_t to a buffer in target memory endianness */
402 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
404 if (target->endianness == TARGET_LITTLE_ENDIAN)
405 h_u16_to_le(buffer, value);
407 h_u16_to_be(buffer, value);
410 /* write a uint8_t to a buffer in target memory endianness */
411 static void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
416 /* write a uint64_t array to a buffer in target memory endianness */
417 void target_buffer_get_u64_array(struct target *target, const uint8_t *buffer, uint32_t count, uint64_t *dstbuf)
420 for (i = 0; i < count; i++)
421 dstbuf[i] = target_buffer_get_u64(target, &buffer[i * 8]);
424 /* write a uint32_t array to a buffer in target memory endianness */
425 void target_buffer_get_u32_array(struct target *target, const uint8_t *buffer, uint32_t count, uint32_t *dstbuf)
428 for (i = 0; i < count; i++)
429 dstbuf[i] = target_buffer_get_u32(target, &buffer[i * 4]);
432 /* write a uint16_t array to a buffer in target memory endianness */
433 void target_buffer_get_u16_array(struct target *target, const uint8_t *buffer, uint32_t count, uint16_t *dstbuf)
436 for (i = 0; i < count; i++)
437 dstbuf[i] = target_buffer_get_u16(target, &buffer[i * 2]);
440 /* write a uint64_t array to a buffer in target memory endianness */
441 void target_buffer_set_u64_array(struct target *target, uint8_t *buffer, uint32_t count, const uint64_t *srcbuf)
444 for (i = 0; i < count; i++)
445 target_buffer_set_u64(target, &buffer[i * 8], srcbuf[i]);
448 /* write a uint32_t array to a buffer in target memory endianness */
449 void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, const uint32_t *srcbuf)
452 for (i = 0; i < count; i++)
453 target_buffer_set_u32(target, &buffer[i * 4], srcbuf[i]);
456 /* write a uint16_t array to a buffer in target memory endianness */
457 void target_buffer_set_u16_array(struct target *target, uint8_t *buffer, uint32_t count, const uint16_t *srcbuf)
460 for (i = 0; i < count; i++)
461 target_buffer_set_u16(target, &buffer[i * 2], srcbuf[i]);
464 /* return a pointer to a configured target; id is name or number */
465 struct target *get_target(const char *id)
467 struct target *target;
469 /* try as tcltarget name */
470 for (target = all_targets; target; target = target->next) {
471 if (target_name(target) == NULL)
473 if (strcmp(id, target_name(target)) == 0)
477 /* It's OK to remove this fallback sometime after August 2010 or so */
479 /* no match, try as number */
481 if (parse_uint(id, &num) != ERROR_OK)
484 for (target = all_targets; target; target = target->next) {
485 if (target->target_number == (int)num) {
486 LOG_WARNING("use '%s' as target identifier, not '%u'",
487 target_name(target), num);
495 /* returns a pointer to the n-th configured target */
496 struct target *get_target_by_num(int num)
498 struct target *target = all_targets;
501 if (target->target_number == num)
503 target = target->next;
509 struct target *get_current_target(struct command_context *cmd_ctx)
511 struct target *target = get_target_by_num(cmd_ctx->current_target);
513 if (target == NULL) {
514 LOG_ERROR("BUG: current_target out of bounds");
521 int target_poll(struct target *target)
525 /* We can't poll until after examine */
526 if (!target_was_examined(target)) {
527 /* Fail silently lest we pollute the log */
531 retval = target->type->poll(target);
532 if (retval != ERROR_OK)
535 if (target->halt_issued) {
536 if (target->state == TARGET_HALTED)
537 target->halt_issued = false;
539 int64_t t = timeval_ms() - target->halt_issued_time;
540 if (t > DEFAULT_HALT_TIMEOUT) {
541 target->halt_issued = false;
542 LOG_INFO("Halt timed out, wake up GDB.");
543 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
551 int target_halt(struct target *target)
554 /* We can't poll until after examine */
555 if (!target_was_examined(target)) {
556 LOG_ERROR("Target not examined yet");
560 retval = target->type->halt(target);
561 if (retval != ERROR_OK)
564 target->halt_issued = true;
565 target->halt_issued_time = timeval_ms();
571 * Make the target (re)start executing using its saved execution
572 * context (possibly with some modifications).
574 * @param target Which target should start executing.
575 * @param current True to use the target's saved program counter instead
576 * of the address parameter
577 * @param address Optionally used as the program counter.
578 * @param handle_breakpoints True iff breakpoints at the resumption PC
579 * should be skipped. (For example, maybe execution was stopped by
580 * such a breakpoint, in which case it would be counterprodutive to
582 * @param debug_execution False if all working areas allocated by OpenOCD
583 * should be released and/or restored to their original contents.
584 * (This would for example be true to run some downloaded "helper"
585 * algorithm code, which resides in one such working buffer and uses
586 * another for data storage.)
588 * @todo Resolve the ambiguity about what the "debug_execution" flag
589 * signifies. For example, Target implementations don't agree on how
590 * it relates to invalidation of the register cache, or to whether
591 * breakpoints and watchpoints should be enabled. (It would seem wrong
592 * to enable breakpoints when running downloaded "helper" algorithms
593 * (debug_execution true), since the breakpoints would be set to match
594 * target firmware being debugged, not the helper algorithm.... and
595 * enabling them could cause such helpers to malfunction (for example,
596 * by overwriting data with a breakpoint instruction. On the other
597 * hand the infrastructure for running such helpers might use this
598 * procedure but rely on hardware breakpoint to detect termination.)
600 int target_resume(struct target *target, int current, target_addr_t address,
601 int handle_breakpoints, int debug_execution)
605 /* We can't poll until after examine */
606 if (!target_was_examined(target)) {
607 LOG_ERROR("Target not examined yet");
611 target_call_event_callbacks(target, TARGET_EVENT_RESUME_START);
613 /* note that resume *must* be asynchronous. The CPU can halt before
614 * we poll. The CPU can even halt at the current PC as a result of
615 * a software breakpoint being inserted by (a bug?) the application.
617 retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution);
618 if (retval != ERROR_OK)
621 target_call_event_callbacks(target, TARGET_EVENT_RESUME_END);
626 static int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
631 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
632 if (n->name == NULL) {
633 LOG_ERROR("invalid reset mode");
637 struct target *target;
638 for (target = all_targets; target; target = target->next)
639 target_call_reset_callbacks(target, reset_mode);
641 /* disable polling during reset to make reset event scripts
642 * more predictable, i.e. dr/irscan & pathmove in events will
643 * not have JTAG operations injected into the middle of a sequence.
645 bool save_poll = jtag_poll_get_enabled();
647 jtag_poll_set_enabled(false);
649 sprintf(buf, "ocd_process_reset %s", n->name);
650 retval = Jim_Eval(cmd_ctx->interp, buf);
652 jtag_poll_set_enabled(save_poll);
654 if (retval != JIM_OK) {
655 Jim_MakeErrorMessage(cmd_ctx->interp);
656 command_print(NULL, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx->interp), NULL));
660 /* We want any events to be processed before the prompt */
661 retval = target_call_timer_callbacks_now();
663 for (target = all_targets; target; target = target->next) {
664 target->type->check_reset(target);
665 target->running_alg = false;
671 static int identity_virt2phys(struct target *target,
672 target_addr_t virtual, target_addr_t *physical)
678 static int no_mmu(struct target *target, int *enabled)
684 static int default_examine(struct target *target)
686 target_set_examined(target);
690 /* no check by default */
691 static int default_check_reset(struct target *target)
696 int target_examine_one(struct target *target)
698 target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_START);
700 int retval = target->type->examine(target);
701 if (retval != ERROR_OK)
704 target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_END);
709 static int jtag_enable_callback(enum jtag_event event, void *priv)
711 struct target *target = priv;
713 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
716 jtag_unregister_event_callback(jtag_enable_callback, target);
718 return target_examine_one(target);
721 /* Targets that correctly implement init + examine, i.e.
722 * no communication with target during init:
726 int target_examine(void)
728 int retval = ERROR_OK;
729 struct target *target;
731 for (target = all_targets; target; target = target->next) {
732 /* defer examination, but don't skip it */
733 if (!target->tap->enabled) {
734 jtag_register_event_callback(jtag_enable_callback,
739 if (target->defer_examine)
742 retval = target_examine_one(target);
743 if (retval != ERROR_OK)
749 const char *target_type_name(struct target *target)
751 return target->type->name;
754 static int target_soft_reset_halt(struct target *target)
756 if (!target_was_examined(target)) {
757 LOG_ERROR("Target not examined yet");
760 if (!target->type->soft_reset_halt) {
761 LOG_ERROR("Target %s does not support soft_reset_halt",
762 target_name(target));
765 return target->type->soft_reset_halt(target);
769 * Downloads a target-specific native code algorithm to the target,
770 * and executes it. * Note that some targets may need to set up, enable,
771 * and tear down a breakpoint (hard or * soft) to detect algorithm
772 * termination, while others may support lower overhead schemes where
773 * soft breakpoints embedded in the algorithm automatically terminate the
776 * @param target used to run the algorithm
777 * @param arch_info target-specific description of the algorithm.
779 int target_run_algorithm(struct target *target,
780 int num_mem_params, struct mem_param *mem_params,
781 int num_reg_params, struct reg_param *reg_param,
782 uint32_t entry_point, uint32_t exit_point,
783 int timeout_ms, void *arch_info)
785 int retval = ERROR_FAIL;
787 if (!target_was_examined(target)) {
788 LOG_ERROR("Target not examined yet");
791 if (!target->type->run_algorithm) {
792 LOG_ERROR("Target type '%s' does not support %s",
793 target_type_name(target), __func__);
797 target->running_alg = true;
798 retval = target->type->run_algorithm(target,
799 num_mem_params, mem_params,
800 num_reg_params, reg_param,
801 entry_point, exit_point, timeout_ms, arch_info);
802 target->running_alg = false;
809 * Downloads a target-specific native code algorithm to the target,
810 * executes and leaves it running.
812 * @param target used to run the algorithm
813 * @param arch_info target-specific description of the algorithm.
815 int target_start_algorithm(struct target *target,
816 int num_mem_params, struct mem_param *mem_params,
817 int num_reg_params, struct reg_param *reg_params,
818 uint32_t entry_point, uint32_t exit_point,
821 int retval = ERROR_FAIL;
823 if (!target_was_examined(target)) {
824 LOG_ERROR("Target not examined yet");
827 if (!target->type->start_algorithm) {
828 LOG_ERROR("Target type '%s' does not support %s",
829 target_type_name(target), __func__);
832 if (target->running_alg) {
833 LOG_ERROR("Target is already running an algorithm");
837 target->running_alg = true;
838 retval = target->type->start_algorithm(target,
839 num_mem_params, mem_params,
840 num_reg_params, reg_params,
841 entry_point, exit_point, arch_info);
848 * Waits for an algorithm started with target_start_algorithm() to complete.
850 * @param target used to run the algorithm
851 * @param arch_info target-specific description of the algorithm.
853 int target_wait_algorithm(struct target *target,
854 int num_mem_params, struct mem_param *mem_params,
855 int num_reg_params, struct reg_param *reg_params,
856 uint32_t exit_point, int timeout_ms,
859 int retval = ERROR_FAIL;
861 if (!target->type->wait_algorithm) {
862 LOG_ERROR("Target type '%s' does not support %s",
863 target_type_name(target), __func__);
866 if (!target->running_alg) {
867 LOG_ERROR("Target is not running an algorithm");
871 retval = target->type->wait_algorithm(target,
872 num_mem_params, mem_params,
873 num_reg_params, reg_params,
874 exit_point, timeout_ms, arch_info);
875 if (retval != ERROR_TARGET_TIMEOUT)
876 target->running_alg = false;
883 * Executes a target-specific native code algorithm in the target.
884 * It differs from target_run_algorithm in that the algorithm is asynchronous.
885 * Because of this it requires an compliant algorithm:
886 * see contrib/loaders/flash/stm32f1x.S for example.
888 * @param target used to run the algorithm
891 int target_run_flash_async_algorithm(struct target *target,
892 const uint8_t *buffer, uint32_t count, int block_size,
893 int num_mem_params, struct mem_param *mem_params,
894 int num_reg_params, struct reg_param *reg_params,
895 uint32_t buffer_start, uint32_t buffer_size,
896 uint32_t entry_point, uint32_t exit_point, void *arch_info)
901 const uint8_t *buffer_orig = buffer;
903 /* Set up working area. First word is write pointer, second word is read pointer,
904 * rest is fifo data area. */
905 uint32_t wp_addr = buffer_start;
906 uint32_t rp_addr = buffer_start + 4;
907 uint32_t fifo_start_addr = buffer_start + 8;
908 uint32_t fifo_end_addr = buffer_start + buffer_size;
910 uint32_t wp = fifo_start_addr;
911 uint32_t rp = fifo_start_addr;
913 /* validate block_size is 2^n */
914 assert(!block_size || !(block_size & (block_size - 1)));
916 retval = target_write_u32(target, wp_addr, wp);
917 if (retval != ERROR_OK)
919 retval = target_write_u32(target, rp_addr, rp);
920 if (retval != ERROR_OK)
923 /* Start up algorithm on target and let it idle while writing the first chunk */
924 retval = target_start_algorithm(target, num_mem_params, mem_params,
925 num_reg_params, reg_params,
930 if (retval != ERROR_OK) {
931 LOG_ERROR("error starting target flash write algorithm");
937 retval = target_read_u32(target, rp_addr, &rp);
938 if (retval != ERROR_OK) {
939 LOG_ERROR("failed to get read pointer");
943 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32 " wp 0x%" PRIx32 " rp 0x%" PRIx32,
944 (size_t) (buffer - buffer_orig), count, wp, rp);
947 LOG_ERROR("flash write algorithm aborted by target");
948 retval = ERROR_FLASH_OPERATION_FAILED;
952 if (((rp - fifo_start_addr) & (block_size - 1)) || rp < fifo_start_addr || rp >= fifo_end_addr) {
953 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32, rp);
957 /* Count the number of bytes available in the fifo without
958 * crossing the wrap around. Make sure to not fill it completely,
959 * because that would make wp == rp and that's the empty condition. */
960 uint32_t thisrun_bytes;
962 thisrun_bytes = rp - wp - block_size;
963 else if (rp > fifo_start_addr)
964 thisrun_bytes = fifo_end_addr - wp;
966 thisrun_bytes = fifo_end_addr - wp - block_size;
968 if (thisrun_bytes == 0) {
969 /* Throttle polling a bit if transfer is (much) faster than flash
970 * programming. The exact delay shouldn't matter as long as it's
971 * less than buffer size / flash speed. This is very unlikely to
972 * run when using high latency connections such as USB. */
975 /* to stop an infinite loop on some targets check and increment a timeout
976 * this issue was observed on a stellaris using the new ICDI interface */
977 if (timeout++ >= 500) {
978 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
979 return ERROR_FLASH_OPERATION_FAILED;
984 /* reset our timeout */
987 /* Limit to the amount of data we actually want to write */
988 if (thisrun_bytes > count * block_size)
989 thisrun_bytes = count * block_size;
991 /* Write data to fifo */
992 retval = target_write_buffer(target, wp, thisrun_bytes, buffer);
993 if (retval != ERROR_OK)
996 /* Update counters and wrap write pointer */
997 buffer += thisrun_bytes;
998 count -= thisrun_bytes / block_size;
1000 if (wp >= fifo_end_addr)
1001 wp = fifo_start_addr;
1003 /* Store updated write pointer to target */
1004 retval = target_write_u32(target, wp_addr, wp);
1005 if (retval != ERROR_OK)
1009 if (retval != ERROR_OK) {
1010 /* abort flash write algorithm on target */
1011 target_write_u32(target, wp_addr, 0);
1014 int retval2 = target_wait_algorithm(target, num_mem_params, mem_params,
1015 num_reg_params, reg_params,
1020 if (retval2 != ERROR_OK) {
1021 LOG_ERROR("error waiting for target flash write algorithm");
1025 if (retval == ERROR_OK) {
1026 /* check if algorithm set rp = 0 after fifo writer loop finished */
1027 retval = target_read_u32(target, rp_addr, &rp);
1028 if (retval == ERROR_OK && rp == 0) {
1029 LOG_ERROR("flash write algorithm aborted by target");
1030 retval = ERROR_FLASH_OPERATION_FAILED;
1037 int target_read_memory(struct target *target,
1038 target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
1040 if (!target_was_examined(target)) {
1041 LOG_ERROR("Target not examined yet");
1044 if (!target->type->read_memory) {
1045 LOG_ERROR("Target %s doesn't support read_memory", target_name(target));
1048 return target->type->read_memory(target, address, size, count, buffer);
1051 int target_read_phys_memory(struct target *target,
1052 target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
1054 if (!target_was_examined(target)) {
1055 LOG_ERROR("Target not examined yet");
1058 if (!target->type->read_phys_memory) {
1059 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target));
1062 return target->type->read_phys_memory(target, address, size, count, buffer);
1065 int target_write_memory(struct target *target,
1066 target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
1068 if (!target_was_examined(target)) {
1069 LOG_ERROR("Target not examined yet");
1072 if (!target->type->write_memory) {
1073 LOG_ERROR("Target %s doesn't support write_memory", target_name(target));
1076 return target->type->write_memory(target, address, size, count, buffer);
1079 int target_write_phys_memory(struct target *target,
1080 target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
1082 if (!target_was_examined(target)) {
1083 LOG_ERROR("Target not examined yet");
1086 if (!target->type->write_phys_memory) {
1087 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target));
1090 return target->type->write_phys_memory(target, address, size, count, buffer);
1093 int target_add_breakpoint(struct target *target,
1094 struct breakpoint *breakpoint)
1096 if ((target->state != TARGET_HALTED) && (breakpoint->type != BKPT_HARD)) {
1097 LOG_WARNING("target %s is not halted", target_name(target));
1098 return ERROR_TARGET_NOT_HALTED;
1100 return target->type->add_breakpoint(target, breakpoint);
1103 int target_add_context_breakpoint(struct target *target,
1104 struct breakpoint *breakpoint)
1106 if (target->state != TARGET_HALTED) {
1107 LOG_WARNING("target %s is not halted", target_name(target));
1108 return ERROR_TARGET_NOT_HALTED;
1110 return target->type->add_context_breakpoint(target, breakpoint);
1113 int target_add_hybrid_breakpoint(struct target *target,
1114 struct breakpoint *breakpoint)
1116 if (target->state != TARGET_HALTED) {
1117 LOG_WARNING("target %s is not halted", target_name(target));
1118 return ERROR_TARGET_NOT_HALTED;
1120 return target->type->add_hybrid_breakpoint(target, breakpoint);
1123 int target_remove_breakpoint(struct target *target,
1124 struct breakpoint *breakpoint)
1126 return target->type->remove_breakpoint(target, breakpoint);
1129 int target_add_watchpoint(struct target *target,
1130 struct watchpoint *watchpoint)
1132 if (target->state != TARGET_HALTED) {
1133 LOG_WARNING("target %s is not halted", target_name(target));
1134 return ERROR_TARGET_NOT_HALTED;
1136 return target->type->add_watchpoint(target, watchpoint);
1138 int target_remove_watchpoint(struct target *target,
1139 struct watchpoint *watchpoint)
1141 return target->type->remove_watchpoint(target, watchpoint);
1143 int target_hit_watchpoint(struct target *target,
1144 struct watchpoint **hit_watchpoint)
1146 if (target->state != TARGET_HALTED) {
1147 LOG_WARNING("target %s is not halted", target->cmd_name);
1148 return ERROR_TARGET_NOT_HALTED;
1151 if (target->type->hit_watchpoint == NULL) {
1152 /* For backward compatible, if hit_watchpoint is not implemented,
1153 * return ERROR_FAIL such that gdb_server will not take the nonsense
1158 return target->type->hit_watchpoint(target, hit_watchpoint);
1161 int target_get_gdb_reg_list(struct target *target,
1162 struct reg **reg_list[], int *reg_list_size,
1163 enum target_register_class reg_class)
1165 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size, reg_class);
1167 int target_step(struct target *target,
1168 int current, target_addr_t address, int handle_breakpoints)
1170 return target->type->step(target, current, address, handle_breakpoints);
1173 int target_get_gdb_fileio_info(struct target *target, struct gdb_fileio_info *fileio_info)
1175 if (target->state != TARGET_HALTED) {
1176 LOG_WARNING("target %s is not halted", target->cmd_name);
1177 return ERROR_TARGET_NOT_HALTED;
1179 return target->type->get_gdb_fileio_info(target, fileio_info);
1182 int target_gdb_fileio_end(struct target *target, int retcode, int fileio_errno, bool ctrl_c)
1184 if (target->state != TARGET_HALTED) {
1185 LOG_WARNING("target %s is not halted", target->cmd_name);
1186 return ERROR_TARGET_NOT_HALTED;
1188 return target->type->gdb_fileio_end(target, retcode, fileio_errno, ctrl_c);
1191 int target_profiling(struct target *target, uint32_t *samples,
1192 uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
1194 if (target->state != TARGET_HALTED) {
1195 LOG_WARNING("target %s is not halted", target->cmd_name);
1196 return ERROR_TARGET_NOT_HALTED;
1198 return target->type->profiling(target, samples, max_num_samples,
1199 num_samples, seconds);
1203 * Reset the @c examined flag for the given target.
1204 * Pure paranoia -- targets are zeroed on allocation.
1206 static void target_reset_examined(struct target *target)
1208 target->examined = false;
1211 static int handle_target(void *priv);
1213 static int target_init_one(struct command_context *cmd_ctx,
1214 struct target *target)
1216 target_reset_examined(target);
1218 struct target_type *type = target->type;
1219 if (type->examine == NULL)
1220 type->examine = default_examine;
1222 if (type->check_reset == NULL)
1223 type->check_reset = default_check_reset;
1225 assert(type->init_target != NULL);
1227 int retval = type->init_target(cmd_ctx, target);
1228 if (ERROR_OK != retval) {
1229 LOG_ERROR("target '%s' init failed", target_name(target));
1233 /* Sanity-check MMU support ... stub in what we must, to help
1234 * implement it in stages, but warn if we need to do so.
1237 if (type->virt2phys == NULL) {
1238 LOG_ERROR("type '%s' is missing virt2phys", type->name);
1239 type->virt2phys = identity_virt2phys;
1242 /* Make sure no-MMU targets all behave the same: make no
1243 * distinction between physical and virtual addresses, and
1244 * ensure that virt2phys() is always an identity mapping.
1246 if (type->write_phys_memory || type->read_phys_memory || type->virt2phys)
1247 LOG_WARNING("type '%s' has bad MMU hooks", type->name);
1250 type->write_phys_memory = type->write_memory;
1251 type->read_phys_memory = type->read_memory;
1252 type->virt2phys = identity_virt2phys;
1255 if (target->type->read_buffer == NULL)
1256 target->type->read_buffer = target_read_buffer_default;
1258 if (target->type->write_buffer == NULL)
1259 target->type->write_buffer = target_write_buffer_default;
1261 if (target->type->get_gdb_fileio_info == NULL)
1262 target->type->get_gdb_fileio_info = target_get_gdb_fileio_info_default;
1264 if (target->type->gdb_fileio_end == NULL)
1265 target->type->gdb_fileio_end = target_gdb_fileio_end_default;
1267 if (target->type->profiling == NULL)
1268 target->type->profiling = target_profiling_default;
1273 static int target_init(struct command_context *cmd_ctx)
1275 struct target *target;
1278 for (target = all_targets; target; target = target->next) {
1279 retval = target_init_one(cmd_ctx, target);
1280 if (ERROR_OK != retval)
1287 retval = target_register_user_commands(cmd_ctx);
1288 if (ERROR_OK != retval)
1291 retval = target_register_timer_callback(&handle_target,
1292 polling_interval, 1, cmd_ctx->interp);
1293 if (ERROR_OK != retval)
1299 COMMAND_HANDLER(handle_target_init_command)
1304 return ERROR_COMMAND_SYNTAX_ERROR;
1306 static bool target_initialized;
1307 if (target_initialized) {
1308 LOG_INFO("'target init' has already been called");
1311 target_initialized = true;
1313 retval = command_run_line(CMD_CTX, "init_targets");
1314 if (ERROR_OK != retval)
1317 retval = command_run_line(CMD_CTX, "init_target_events");
1318 if (ERROR_OK != retval)
1321 retval = command_run_line(CMD_CTX, "init_board");
1322 if (ERROR_OK != retval)
1325 LOG_DEBUG("Initializing targets...");
1326 return target_init(CMD_CTX);
1329 int target_register_event_callback(int (*callback)(struct target *target,
1330 enum target_event event, void *priv), void *priv)
1332 struct target_event_callback **callbacks_p = &target_event_callbacks;
1334 if (callback == NULL)
1335 return ERROR_COMMAND_SYNTAX_ERROR;
1338 while ((*callbacks_p)->next)
1339 callbacks_p = &((*callbacks_p)->next);
1340 callbacks_p = &((*callbacks_p)->next);
1343 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
1344 (*callbacks_p)->callback = callback;
1345 (*callbacks_p)->priv = priv;
1346 (*callbacks_p)->next = NULL;
1351 int target_register_reset_callback(int (*callback)(struct target *target,
1352 enum target_reset_mode reset_mode, void *priv), void *priv)
1354 struct target_reset_callback *entry;
1356 if (callback == NULL)
1357 return ERROR_COMMAND_SYNTAX_ERROR;
1359 entry = malloc(sizeof(struct target_reset_callback));
1360 if (entry == NULL) {
1361 LOG_ERROR("error allocating buffer for reset callback entry");
1362 return ERROR_COMMAND_SYNTAX_ERROR;
1365 entry->callback = callback;
1367 list_add(&entry->list, &target_reset_callback_list);
1373 int target_register_trace_callback(int (*callback)(struct target *target,
1374 size_t len, uint8_t *data, void *priv), void *priv)
1376 struct target_trace_callback *entry;
1378 if (callback == NULL)
1379 return ERROR_COMMAND_SYNTAX_ERROR;
1381 entry = malloc(sizeof(struct target_trace_callback));
1382 if (entry == NULL) {
1383 LOG_ERROR("error allocating buffer for trace callback entry");
1384 return ERROR_COMMAND_SYNTAX_ERROR;
1387 entry->callback = callback;
1389 list_add(&entry->list, &target_trace_callback_list);
1395 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
1397 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
1400 if (callback == NULL)
1401 return ERROR_COMMAND_SYNTAX_ERROR;
1404 while ((*callbacks_p)->next)
1405 callbacks_p = &((*callbacks_p)->next);
1406 callbacks_p = &((*callbacks_p)->next);
1409 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
1410 (*callbacks_p)->callback = callback;
1411 (*callbacks_p)->periodic = periodic;
1412 (*callbacks_p)->time_ms = time_ms;
1413 (*callbacks_p)->removed = false;
1415 gettimeofday(&now, NULL);
1416 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
1417 time_ms -= (time_ms % 1000);
1418 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
1419 if ((*callbacks_p)->when.tv_usec > 1000000) {
1420 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
1421 (*callbacks_p)->when.tv_sec += 1;
1424 (*callbacks_p)->priv = priv;
1425 (*callbacks_p)->next = NULL;
1430 int target_unregister_event_callback(int (*callback)(struct target *target,
1431 enum target_event event, void *priv), void *priv)
1433 struct target_event_callback **p = &target_event_callbacks;
1434 struct target_event_callback *c = target_event_callbacks;
1436 if (callback == NULL)
1437 return ERROR_COMMAND_SYNTAX_ERROR;
1440 struct target_event_callback *next = c->next;
1441 if ((c->callback == callback) && (c->priv == priv)) {
1453 int target_unregister_reset_callback(int (*callback)(struct target *target,
1454 enum target_reset_mode reset_mode, void *priv), void *priv)
1456 struct target_reset_callback *entry;
1458 if (callback == NULL)
1459 return ERROR_COMMAND_SYNTAX_ERROR;
1461 list_for_each_entry(entry, &target_reset_callback_list, list) {
1462 if (entry->callback == callback && entry->priv == priv) {
1463 list_del(&entry->list);
1472 int target_unregister_trace_callback(int (*callback)(struct target *target,
1473 size_t len, uint8_t *data, void *priv), void *priv)
1475 struct target_trace_callback *entry;
1477 if (callback == NULL)
1478 return ERROR_COMMAND_SYNTAX_ERROR;
1480 list_for_each_entry(entry, &target_trace_callback_list, list) {
1481 if (entry->callback == callback && entry->priv == priv) {
1482 list_del(&entry->list);
1491 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
1493 if (callback == NULL)
1494 return ERROR_COMMAND_SYNTAX_ERROR;
1496 for (struct target_timer_callback *c = target_timer_callbacks;
1498 if ((c->callback == callback) && (c->priv == priv)) {
1507 int target_call_event_callbacks(struct target *target, enum target_event event)
1509 struct target_event_callback *callback = target_event_callbacks;
1510 struct target_event_callback *next_callback;
1512 if (event == TARGET_EVENT_HALTED) {
1513 /* execute early halted first */
1514 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1517 LOG_DEBUG("target event %i (%s)", event,
1518 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
1520 target_handle_event(target, event);
1523 next_callback = callback->next;
1524 callback->callback(target, event, callback->priv);
1525 callback = next_callback;
1531 int target_call_reset_callbacks(struct target *target, enum target_reset_mode reset_mode)
1533 struct target_reset_callback *callback;
1535 LOG_DEBUG("target reset %i (%s)", reset_mode,
1536 Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode)->name);
1538 list_for_each_entry(callback, &target_reset_callback_list, list)
1539 callback->callback(target, reset_mode, callback->priv);
1544 int target_call_trace_callbacks(struct target *target, size_t len, uint8_t *data)
1546 struct target_trace_callback *callback;
1548 list_for_each_entry(callback, &target_trace_callback_list, list)
1549 callback->callback(target, len, data, callback->priv);
1554 static int target_timer_callback_periodic_restart(
1555 struct target_timer_callback *cb, struct timeval *now)
1557 int time_ms = cb->time_ms;
1558 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
1559 time_ms -= (time_ms % 1000);
1560 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
1561 if (cb->when.tv_usec > 1000000) {
1562 cb->when.tv_usec = cb->when.tv_usec - 1000000;
1563 cb->when.tv_sec += 1;
1568 static int target_call_timer_callback(struct target_timer_callback *cb,
1569 struct timeval *now)
1571 cb->callback(cb->priv);
1574 return target_timer_callback_periodic_restart(cb, now);
1576 return target_unregister_timer_callback(cb->callback, cb->priv);
1579 static int target_call_timer_callbacks_check_time(int checktime)
1581 static bool callback_processing;
1583 /* Do not allow nesting */
1584 if (callback_processing)
1587 callback_processing = true;
1592 gettimeofday(&now, NULL);
1594 /* Store an address of the place containing a pointer to the
1595 * next item; initially, that's a standalone "root of the
1596 * list" variable. */
1597 struct target_timer_callback **callback = &target_timer_callbacks;
1599 if ((*callback)->removed) {
1600 struct target_timer_callback *p = *callback;
1601 *callback = (*callback)->next;
1606 bool call_it = (*callback)->callback &&
1607 ((!checktime && (*callback)->periodic) ||
1608 now.tv_sec > (*callback)->when.tv_sec ||
1609 (now.tv_sec == (*callback)->when.tv_sec &&
1610 now.tv_usec >= (*callback)->when.tv_usec));
1613 target_call_timer_callback(*callback, &now);
1615 callback = &(*callback)->next;
1618 callback_processing = false;
1622 int target_call_timer_callbacks(void)
1624 return target_call_timer_callbacks_check_time(1);
1627 /* invoke periodic callbacks immediately */
1628 int target_call_timer_callbacks_now(void)
1630 return target_call_timer_callbacks_check_time(0);
1633 /* Prints the working area layout for debug purposes */
1634 static void print_wa_layout(struct target *target)
1636 struct working_area *c = target->working_areas;
1639 LOG_DEBUG("%c%c " TARGET_ADDR_FMT "-" TARGET_ADDR_FMT " (%" PRIu32 " bytes)",
1640 c->backup ? 'b' : ' ', c->free ? ' ' : '*',
1641 c->address, c->address + c->size - 1, c->size);
1646 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1647 static void target_split_working_area(struct working_area *area, uint32_t size)
1649 assert(area->free); /* Shouldn't split an allocated area */
1650 assert(size <= area->size); /* Caller should guarantee this */
1652 /* Split only if not already the right size */
1653 if (size < area->size) {
1654 struct working_area *new_wa = malloc(sizeof(*new_wa));
1659 new_wa->next = area->next;
1660 new_wa->size = area->size - size;
1661 new_wa->address = area->address + size;
1662 new_wa->backup = NULL;
1663 new_wa->user = NULL;
1664 new_wa->free = true;
1666 area->next = new_wa;
1669 /* If backup memory was allocated to this area, it has the wrong size
1670 * now so free it and it will be reallocated if/when needed */
1673 area->backup = NULL;
1678 /* Merge all adjacent free areas into one */
1679 static void target_merge_working_areas(struct target *target)
1681 struct working_area *c = target->working_areas;
1683 while (c && c->next) {
1684 assert(c->next->address == c->address + c->size); /* This is an invariant */
1686 /* Find two adjacent free areas */
1687 if (c->free && c->next->free) {
1688 /* Merge the last into the first */
1689 c->size += c->next->size;
1691 /* Remove the last */
1692 struct working_area *to_be_freed = c->next;
1693 c->next = c->next->next;
1694 if (to_be_freed->backup)
1695 free(to_be_freed->backup);
1698 /* If backup memory was allocated to the remaining area, it's has
1699 * the wrong size now */
1710 int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
1712 /* Reevaluate working area address based on MMU state*/
1713 if (target->working_areas == NULL) {
1717 retval = target->type->mmu(target, &enabled);
1718 if (retval != ERROR_OK)
1722 if (target->working_area_phys_spec) {
1723 LOG_DEBUG("MMU disabled, using physical "
1724 "address for working memory " TARGET_ADDR_FMT,
1725 target->working_area_phys);
1726 target->working_area = target->working_area_phys;
1728 LOG_ERROR("No working memory available. "
1729 "Specify -work-area-phys to target.");
1730 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1733 if (target->working_area_virt_spec) {
1734 LOG_DEBUG("MMU enabled, using virtual "
1735 "address for working memory " TARGET_ADDR_FMT,
1736 target->working_area_virt);
1737 target->working_area = target->working_area_virt;
1739 LOG_ERROR("No working memory available. "
1740 "Specify -work-area-virt to target.");
1741 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1745 /* Set up initial working area on first call */
1746 struct working_area *new_wa = malloc(sizeof(*new_wa));
1748 new_wa->next = NULL;
1749 new_wa->size = target->working_area_size & ~3UL; /* 4-byte align */
1750 new_wa->address = target->working_area;
1751 new_wa->backup = NULL;
1752 new_wa->user = NULL;
1753 new_wa->free = true;
1756 target->working_areas = new_wa;
1759 /* only allocate multiples of 4 byte */
1761 size = (size + 3) & (~3UL);
1763 struct working_area *c = target->working_areas;
1765 /* Find the first large enough working area */
1767 if (c->free && c->size >= size)
1773 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1775 /* Split the working area into the requested size */
1776 target_split_working_area(c, size);
1778 LOG_DEBUG("allocated new working area of %" PRIu32 " bytes at address " TARGET_ADDR_FMT,
1781 if (target->backup_working_area) {
1782 if (c->backup == NULL) {
1783 c->backup = malloc(c->size);
1784 if (c->backup == NULL)
1788 int retval = target_read_memory(target, c->address, 4, c->size / 4, c->backup);
1789 if (retval != ERROR_OK)
1793 /* mark as used, and return the new (reused) area */
1800 print_wa_layout(target);
1805 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1809 retval = target_alloc_working_area_try(target, size, area);
1810 if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
1811 LOG_WARNING("not enough working area available(requested %"PRIu32")", size);
1816 static int target_restore_working_area(struct target *target, struct working_area *area)
1818 int retval = ERROR_OK;
1820 if (target->backup_working_area && area->backup != NULL) {
1821 retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup);
1822 if (retval != ERROR_OK)
1823 LOG_ERROR("failed to restore %" PRIu32 " bytes of working area at address " TARGET_ADDR_FMT,
1824 area->size, area->address);
1830 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1831 static int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1833 int retval = ERROR_OK;
1839 retval = target_restore_working_area(target, area);
1840 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1841 if (retval != ERROR_OK)
1847 LOG_DEBUG("freed %" PRIu32 " bytes of working area at address " TARGET_ADDR_FMT,
1848 area->size, area->address);
1850 /* mark user pointer invalid */
1851 /* TODO: Is this really safe? It points to some previous caller's memory.
1852 * How could we know that the area pointer is still in that place and not
1853 * some other vital data? What's the purpose of this, anyway? */
1857 target_merge_working_areas(target);
1859 print_wa_layout(target);
1864 int target_free_working_area(struct target *target, struct working_area *area)
1866 return target_free_working_area_restore(target, area, 1);
1869 void target_quit(void)
1871 struct target_event_callback *pe = target_event_callbacks;
1873 struct target_event_callback *t = pe->next;
1877 target_event_callbacks = NULL;
1879 struct target_timer_callback *pt = target_timer_callbacks;
1881 struct target_timer_callback *t = pt->next;
1885 target_timer_callbacks = NULL;
1887 for (struct target *target = all_targets;
1888 target; target = target->next) {
1889 if (target->type->deinit_target)
1890 target->type->deinit_target(target);
1894 /* free resources and restore memory, if restoring memory fails,
1895 * free up resources anyway
1897 static void target_free_all_working_areas_restore(struct target *target, int restore)
1899 struct working_area *c = target->working_areas;
1901 LOG_DEBUG("freeing all working areas");
1903 /* Loop through all areas, restoring the allocated ones and marking them as free */
1907 target_restore_working_area(target, c);
1909 *c->user = NULL; /* Same as above */
1915 /* Run a merge pass to combine all areas into one */
1916 target_merge_working_areas(target);
1918 print_wa_layout(target);
1921 void target_free_all_working_areas(struct target *target)
1923 target_free_all_working_areas_restore(target, 1);
1926 /* Find the largest number of bytes that can be allocated */
1927 uint32_t target_get_working_area_avail(struct target *target)
1929 struct working_area *c = target->working_areas;
1930 uint32_t max_size = 0;
1933 return target->working_area_size;
1936 if (c->free && max_size < c->size)
1945 int target_arch_state(struct target *target)
1948 if (target == NULL) {
1949 LOG_WARNING("No target has been configured");
1953 if (target->state != TARGET_HALTED)
1956 retval = target->type->arch_state(target);
1960 static int target_get_gdb_fileio_info_default(struct target *target,
1961 struct gdb_fileio_info *fileio_info)
1963 /* If target does not support semi-hosting function, target
1964 has no need to provide .get_gdb_fileio_info callback.
1965 It just return ERROR_FAIL and gdb_server will return "Txx"
1966 as target halted every time. */
1970 static int target_gdb_fileio_end_default(struct target *target,
1971 int retcode, int fileio_errno, bool ctrl_c)
1976 static int target_profiling_default(struct target *target, uint32_t *samples,
1977 uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
1979 struct timeval timeout, now;
1981 gettimeofday(&timeout, NULL);
1982 timeval_add_time(&timeout, seconds, 0);
1984 LOG_INFO("Starting profiling. Halting and resuming the"
1985 " target as often as we can...");
1987 uint32_t sample_count = 0;
1988 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
1989 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
1991 int retval = ERROR_OK;
1993 target_poll(target);
1994 if (target->state == TARGET_HALTED) {
1995 uint32_t t = buf_get_u32(reg->value, 0, 32);
1996 samples[sample_count++] = t;
1997 /* current pc, addr = 0, do not handle breakpoints, not debugging */
1998 retval = target_resume(target, 1, 0, 0, 0);
1999 target_poll(target);
2000 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2001 } else if (target->state == TARGET_RUNNING) {
2002 /* We want to quickly sample the PC. */
2003 retval = target_halt(target);
2005 LOG_INFO("Target not halted or running");
2010 if (retval != ERROR_OK)
2013 gettimeofday(&now, NULL);
2014 if ((sample_count >= max_num_samples) ||
2015 ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec))) {
2016 LOG_INFO("Profiling completed. %" PRIu32 " samples.", sample_count);
2021 *num_samples = sample_count;
2025 /* Single aligned words are guaranteed to use 16 or 32 bit access
2026 * mode respectively, otherwise data is handled as quickly as
2029 int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
2031 LOG_DEBUG("writing buffer of %" PRIi32 " byte at " TARGET_ADDR_FMT,
2034 if (!target_was_examined(target)) {
2035 LOG_ERROR("Target not examined yet");
2042 if ((address + size - 1) < address) {
2043 /* GDB can request this when e.g. PC is 0xfffffffc */
2044 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT ", 0x%08" PRIx32 ")",
2050 return target->type->write_buffer(target, address, size, buffer);
2053 static int target_write_buffer_default(struct target *target,
2054 target_addr_t address, uint32_t count, const uint8_t *buffer)
2058 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2059 * will have something to do with the size we leave to it. */
2060 for (size = 1; size < 4 && count >= size * 2 + (address & size); size *= 2) {
2061 if (address & size) {
2062 int retval = target_write_memory(target, address, size, 1, buffer);
2063 if (retval != ERROR_OK)
2071 /* Write the data with as large access size as possible. */
2072 for (; size > 0; size /= 2) {
2073 uint32_t aligned = count - count % size;
2075 int retval = target_write_memory(target, address, size, aligned / size, buffer);
2076 if (retval != ERROR_OK)
2087 /* Single aligned words are guaranteed to use 16 or 32 bit access
2088 * mode respectively, otherwise data is handled as quickly as
2091 int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
2093 LOG_DEBUG("reading buffer of %" PRIi32 " byte at " TARGET_ADDR_FMT,
2096 if (!target_was_examined(target)) {
2097 LOG_ERROR("Target not examined yet");
2104 if ((address + size - 1) < address) {
2105 /* GDB can request this when e.g. PC is 0xfffffffc */
2106 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT ", 0x%08" PRIx32 ")",
2112 return target->type->read_buffer(target, address, size, buffer);
2115 static int target_read_buffer_default(struct target *target, target_addr_t address, uint32_t count, uint8_t *buffer)
2119 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2120 * will have something to do with the size we leave to it. */
2121 for (size = 1; size < 4 && count >= size * 2 + (address & size); size *= 2) {
2122 if (address & size) {
2123 int retval = target_read_memory(target, address, size, 1, buffer);
2124 if (retval != ERROR_OK)
2132 /* Read the data with as large access size as possible. */
2133 for (; size > 0; size /= 2) {
2134 uint32_t aligned = count - count % size;
2136 int retval = target_read_memory(target, address, size, aligned / size, buffer);
2137 if (retval != ERROR_OK)
2148 int target_checksum_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t* crc)
2153 uint32_t checksum = 0;
2154 if (!target_was_examined(target)) {
2155 LOG_ERROR("Target not examined yet");
2159 retval = target->type->checksum_memory(target, address, size, &checksum);
2160 if (retval != ERROR_OK) {
2161 buffer = malloc(size);
2162 if (buffer == NULL) {
2163 LOG_ERROR("error allocating buffer for section (%" PRId32 " bytes)", size);
2164 return ERROR_COMMAND_SYNTAX_ERROR;
2166 retval = target_read_buffer(target, address, size, buffer);
2167 if (retval != ERROR_OK) {
2172 /* convert to target endianness */
2173 for (i = 0; i < (size/sizeof(uint32_t)); i++) {
2174 uint32_t target_data;
2175 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
2176 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
2179 retval = image_calculate_checksum(buffer, size, &checksum);
2188 int target_blank_check_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t* blank,
2189 uint8_t erased_value)
2192 if (!target_was_examined(target)) {
2193 LOG_ERROR("Target not examined yet");
2197 if (target->type->blank_check_memory == 0)
2198 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
2200 retval = target->type->blank_check_memory(target, address, size, blank, erased_value);
2205 int target_read_u64(struct target *target, target_addr_t address, uint64_t *value)
2207 uint8_t value_buf[8];
2208 if (!target_was_examined(target)) {
2209 LOG_ERROR("Target not examined yet");
2213 int retval = target_read_memory(target, address, 8, 1, value_buf);
2215 if (retval == ERROR_OK) {
2216 *value = target_buffer_get_u64(target, value_buf);
2217 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64 "",
2222 LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2229 int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
2231 uint8_t value_buf[4];
2232 if (!target_was_examined(target)) {
2233 LOG_ERROR("Target not examined yet");
2237 int retval = target_read_memory(target, address, 4, 1, value_buf);
2239 if (retval == ERROR_OK) {
2240 *value = target_buffer_get_u32(target, value_buf);
2241 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32 "",
2246 LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2253 int target_read_u16(struct target *target, target_addr_t address, uint16_t *value)
2255 uint8_t value_buf[2];
2256 if (!target_was_examined(target)) {
2257 LOG_ERROR("Target not examined yet");
2261 int retval = target_read_memory(target, address, 2, 1, value_buf);
2263 if (retval == ERROR_OK) {
2264 *value = target_buffer_get_u16(target, value_buf);
2265 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%4.4" PRIx16,
2270 LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2277 int target_read_u8(struct target *target, target_addr_t address, uint8_t *value)
2279 if (!target_was_examined(target)) {
2280 LOG_ERROR("Target not examined yet");
2284 int retval = target_read_memory(target, address, 1, 1, value);
2286 if (retval == ERROR_OK) {
2287 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
2292 LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2299 int target_write_u64(struct target *target, target_addr_t address, uint64_t value)
2302 uint8_t value_buf[8];
2303 if (!target_was_examined(target)) {
2304 LOG_ERROR("Target not examined yet");
2308 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64 "",
2312 target_buffer_set_u64(target, value_buf, value);
2313 retval = target_write_memory(target, address, 8, 1, value_buf);
2314 if (retval != ERROR_OK)
2315 LOG_DEBUG("failed: %i", retval);
2320 int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
2323 uint8_t value_buf[4];
2324 if (!target_was_examined(target)) {
2325 LOG_ERROR("Target not examined yet");
2329 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32 "",
2333 target_buffer_set_u32(target, value_buf, value);
2334 retval = target_write_memory(target, address, 4, 1, value_buf);
2335 if (retval != ERROR_OK)
2336 LOG_DEBUG("failed: %i", retval);
2341 int target_write_u16(struct target *target, target_addr_t address, uint16_t value)
2344 uint8_t value_buf[2];
2345 if (!target_was_examined(target)) {
2346 LOG_ERROR("Target not examined yet");
2350 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx16,
2354 target_buffer_set_u16(target, value_buf, value);
2355 retval = target_write_memory(target, address, 2, 1, value_buf);
2356 if (retval != ERROR_OK)
2357 LOG_DEBUG("failed: %i", retval);
2362 int target_write_u8(struct target *target, target_addr_t address, uint8_t value)
2365 if (!target_was_examined(target)) {
2366 LOG_ERROR("Target not examined yet");
2370 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
2373 retval = target_write_memory(target, address, 1, 1, &value);
2374 if (retval != ERROR_OK)
2375 LOG_DEBUG("failed: %i", retval);
2380 int target_write_phys_u64(struct target *target, target_addr_t address, uint64_t value)
2383 uint8_t value_buf[8];
2384 if (!target_was_examined(target)) {
2385 LOG_ERROR("Target not examined yet");
2389 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64 "",
2393 target_buffer_set_u64(target, value_buf, value);
2394 retval = target_write_phys_memory(target, address, 8, 1, value_buf);
2395 if (retval != ERROR_OK)
2396 LOG_DEBUG("failed: %i", retval);
2401 int target_write_phys_u32(struct target *target, target_addr_t address, uint32_t value)
2404 uint8_t value_buf[4];
2405 if (!target_was_examined(target)) {
2406 LOG_ERROR("Target not examined yet");
2410 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32 "",
2414 target_buffer_set_u32(target, value_buf, value);
2415 retval = target_write_phys_memory(target, address, 4, 1, value_buf);
2416 if (retval != ERROR_OK)
2417 LOG_DEBUG("failed: %i", retval);
2422 int target_write_phys_u16(struct target *target, target_addr_t address, uint16_t value)
2425 uint8_t value_buf[2];
2426 if (!target_was_examined(target)) {
2427 LOG_ERROR("Target not examined yet");
2431 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx16,
2435 target_buffer_set_u16(target, value_buf, value);
2436 retval = target_write_phys_memory(target, address, 2, 1, value_buf);
2437 if (retval != ERROR_OK)
2438 LOG_DEBUG("failed: %i", retval);
2443 int target_write_phys_u8(struct target *target, target_addr_t address, uint8_t value)
2446 if (!target_was_examined(target)) {
2447 LOG_ERROR("Target not examined yet");
2451 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
2454 retval = target_write_phys_memory(target, address, 1, 1, &value);
2455 if (retval != ERROR_OK)
2456 LOG_DEBUG("failed: %i", retval);
2461 static int find_target(struct command_context *cmd_ctx, const char *name)
2463 struct target *target = get_target(name);
2464 if (target == NULL) {
2465 LOG_ERROR("Target: %s is unknown, try one of:\n", name);
2468 if (!target->tap->enabled) {
2469 LOG_USER("Target: TAP %s is disabled, "
2470 "can't be the current target\n",
2471 target->tap->dotted_name);
2475 cmd_ctx->current_target = target->target_number;
2480 COMMAND_HANDLER(handle_targets_command)
2482 int retval = ERROR_OK;
2483 if (CMD_ARGC == 1) {
2484 retval = find_target(CMD_CTX, CMD_ARGV[0]);
2485 if (retval == ERROR_OK) {
2491 struct target *target = all_targets;
2492 command_print(CMD_CTX, " TargetName Type Endian TapName State ");
2493 command_print(CMD_CTX, "-- ------------------ ---------- ------ ------------------ ------------");
2498 if (target->tap->enabled)
2499 state = target_state_name(target);
2501 state = "tap-disabled";
2503 if (CMD_CTX->current_target == target->target_number)
2506 /* keep columns lined up to match the headers above */
2507 command_print(CMD_CTX,
2508 "%2d%c %-18s %-10s %-6s %-18s %s",
2509 target->target_number,
2511 target_name(target),
2512 target_type_name(target),
2513 Jim_Nvp_value2name_simple(nvp_target_endian,
2514 target->endianness)->name,
2515 target->tap->dotted_name,
2517 target = target->next;
2523 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2525 static int powerDropout;
2526 static int srstAsserted;
2528 static int runPowerRestore;
2529 static int runPowerDropout;
2530 static int runSrstAsserted;
2531 static int runSrstDeasserted;
2533 static int sense_handler(void)
2535 static int prevSrstAsserted;
2536 static int prevPowerdropout;
2538 int retval = jtag_power_dropout(&powerDropout);
2539 if (retval != ERROR_OK)
2543 powerRestored = prevPowerdropout && !powerDropout;
2545 runPowerRestore = 1;
2547 int64_t current = timeval_ms();
2548 static int64_t lastPower;
2549 bool waitMore = lastPower + 2000 > current;
2550 if (powerDropout && !waitMore) {
2551 runPowerDropout = 1;
2552 lastPower = current;
2555 retval = jtag_srst_asserted(&srstAsserted);
2556 if (retval != ERROR_OK)
2560 srstDeasserted = prevSrstAsserted && !srstAsserted;
2562 static int64_t lastSrst;
2563 waitMore = lastSrst + 2000 > current;
2564 if (srstDeasserted && !waitMore) {
2565 runSrstDeasserted = 1;
2569 if (!prevSrstAsserted && srstAsserted)
2570 runSrstAsserted = 1;
2572 prevSrstAsserted = srstAsserted;
2573 prevPowerdropout = powerDropout;
2575 if (srstDeasserted || powerRestored) {
2576 /* Other than logging the event we can't do anything here.
2577 * Issuing a reset is a particularly bad idea as we might
2578 * be inside a reset already.
2585 /* process target state changes */
2586 static int handle_target(void *priv)
2588 Jim_Interp *interp = (Jim_Interp *)priv;
2589 int retval = ERROR_OK;
2591 if (!is_jtag_poll_safe()) {
2592 /* polling is disabled currently */
2596 /* we do not want to recurse here... */
2597 static int recursive;
2601 /* danger! running these procedures can trigger srst assertions and power dropouts.
2602 * We need to avoid an infinite loop/recursion here and we do that by
2603 * clearing the flags after running these events.
2605 int did_something = 0;
2606 if (runSrstAsserted) {
2607 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2608 Jim_Eval(interp, "srst_asserted");
2611 if (runSrstDeasserted) {
2612 Jim_Eval(interp, "srst_deasserted");
2615 if (runPowerDropout) {
2616 LOG_INFO("Power dropout detected, running power_dropout proc.");
2617 Jim_Eval(interp, "power_dropout");
2620 if (runPowerRestore) {
2621 Jim_Eval(interp, "power_restore");
2625 if (did_something) {
2626 /* clear detect flags */
2630 /* clear action flags */
2632 runSrstAsserted = 0;
2633 runSrstDeasserted = 0;
2634 runPowerRestore = 0;
2635 runPowerDropout = 0;
2640 /* Poll targets for state changes unless that's globally disabled.
2641 * Skip targets that are currently disabled.
2643 for (struct target *target = all_targets;
2644 is_jtag_poll_safe() && target;
2645 target = target->next) {
2647 if (!target_was_examined(target))
2650 if (!target->tap->enabled)
2653 if (target->backoff.times > target->backoff.count) {
2654 /* do not poll this time as we failed previously */
2655 target->backoff.count++;
2658 target->backoff.count = 0;
2660 /* only poll target if we've got power and srst isn't asserted */
2661 if (!powerDropout && !srstAsserted) {
2662 /* polling may fail silently until the target has been examined */
2663 retval = target_poll(target);
2664 if (retval != ERROR_OK) {
2665 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2666 if (target->backoff.times * polling_interval < 5000) {
2667 target->backoff.times *= 2;
2668 target->backoff.times++;
2671 /* Tell GDB to halt the debugger. This allows the user to
2672 * run monitor commands to handle the situation.
2674 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2676 if (target->backoff.times > 0) {
2677 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target));
2678 target_reset_examined(target);
2679 retval = target_examine_one(target);
2680 /* Target examination could have failed due to unstable connection,
2681 * but we set the examined flag anyway to repoll it later */
2682 if (retval != ERROR_OK) {
2683 target->examined = true;
2684 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2685 target->backoff.times * polling_interval);
2690 /* Since we succeeded, we reset backoff count */
2691 target->backoff.times = 0;
2698 COMMAND_HANDLER(handle_reg_command)
2700 struct target *target;
2701 struct reg *reg = NULL;
2707 target = get_current_target(CMD_CTX);
2709 /* list all available registers for the current target */
2710 if (CMD_ARGC == 0) {
2711 struct reg_cache *cache = target->reg_cache;
2717 command_print(CMD_CTX, "===== %s", cache->name);
2719 for (i = 0, reg = cache->reg_list;
2720 i < cache->num_regs;
2721 i++, reg++, count++) {
2722 /* only print cached values if they are valid */
2724 value = buf_to_str(reg->value,
2726 command_print(CMD_CTX,
2727 "(%i) %s (/%" PRIu32 "): 0x%s%s",
2735 command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
2740 cache = cache->next;
2746 /* access a single register by its ordinal number */
2747 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9')) {
2749 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
2751 struct reg_cache *cache = target->reg_cache;
2755 for (i = 0; i < cache->num_regs; i++) {
2756 if (count++ == num) {
2757 reg = &cache->reg_list[i];
2763 cache = cache->next;
2767 command_print(CMD_CTX, "%i is out of bounds, the current target "
2768 "has only %i registers (0 - %i)", num, count, count - 1);
2772 /* access a single register by its name */
2773 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
2776 command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
2781 assert(reg != NULL); /* give clang a hint that we *know* reg is != NULL here */
2783 /* display a register */
2784 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0')
2785 && (CMD_ARGV[1][0] <= '9')))) {
2786 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
2789 if (reg->valid == 0)
2790 reg->type->get(reg);
2791 value = buf_to_str(reg->value, reg->size, 16);
2792 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2797 /* set register value */
2798 if (CMD_ARGC == 2) {
2799 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
2802 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
2804 reg->type->set(reg, buf);
2806 value = buf_to_str(reg->value, reg->size, 16);
2807 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2815 return ERROR_COMMAND_SYNTAX_ERROR;
2818 COMMAND_HANDLER(handle_poll_command)
2820 int retval = ERROR_OK;
2821 struct target *target = get_current_target(CMD_CTX);
2823 if (CMD_ARGC == 0) {
2824 command_print(CMD_CTX, "background polling: %s",
2825 jtag_poll_get_enabled() ? "on" : "off");
2826 command_print(CMD_CTX, "TAP: %s (%s)",
2827 target->tap->dotted_name,
2828 target->tap->enabled ? "enabled" : "disabled");
2829 if (!target->tap->enabled)
2831 retval = target_poll(target);
2832 if (retval != ERROR_OK)
2834 retval = target_arch_state(target);
2835 if (retval != ERROR_OK)
2837 } else if (CMD_ARGC == 1) {
2839 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
2840 jtag_poll_set_enabled(enable);
2842 return ERROR_COMMAND_SYNTAX_ERROR;
2847 COMMAND_HANDLER(handle_wait_halt_command)
2850 return ERROR_COMMAND_SYNTAX_ERROR;
2852 unsigned ms = DEFAULT_HALT_TIMEOUT;
2853 if (1 == CMD_ARGC) {
2854 int retval = parse_uint(CMD_ARGV[0], &ms);
2855 if (ERROR_OK != retval)
2856 return ERROR_COMMAND_SYNTAX_ERROR;
2859 struct target *target = get_current_target(CMD_CTX);
2860 return target_wait_state(target, TARGET_HALTED, ms);
2863 /* wait for target state to change. The trick here is to have a low
2864 * latency for short waits and not to suck up all the CPU time
2867 * After 500ms, keep_alive() is invoked
2869 int target_wait_state(struct target *target, enum target_state state, int ms)
2872 int64_t then = 0, cur;
2876 retval = target_poll(target);
2877 if (retval != ERROR_OK)
2879 if (target->state == state)
2884 then = timeval_ms();
2885 LOG_DEBUG("waiting for target %s...",
2886 Jim_Nvp_value2name_simple(nvp_target_state, state)->name);
2892 if ((cur-then) > ms) {
2893 LOG_ERROR("timed out while waiting for target %s",
2894 Jim_Nvp_value2name_simple(nvp_target_state, state)->name);
2902 COMMAND_HANDLER(handle_halt_command)
2906 struct target *target = get_current_target(CMD_CTX);
2907 int retval = target_halt(target);
2908 if (ERROR_OK != retval)
2911 if (CMD_ARGC == 1) {
2912 unsigned wait_local;
2913 retval = parse_uint(CMD_ARGV[0], &wait_local);
2914 if (ERROR_OK != retval)
2915 return ERROR_COMMAND_SYNTAX_ERROR;
2920 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2923 COMMAND_HANDLER(handle_soft_reset_halt_command)
2925 struct target *target = get_current_target(CMD_CTX);
2927 LOG_USER("requesting target halt and executing a soft reset");
2929 target_soft_reset_halt(target);
2934 COMMAND_HANDLER(handle_reset_command)
2937 return ERROR_COMMAND_SYNTAX_ERROR;
2939 enum target_reset_mode reset_mode = RESET_RUN;
2940 if (CMD_ARGC == 1) {
2942 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
2943 if ((n->name == NULL) || (n->value == RESET_UNKNOWN))
2944 return ERROR_COMMAND_SYNTAX_ERROR;
2945 reset_mode = n->value;
2948 /* reset *all* targets */
2949 return target_process_reset(CMD_CTX, reset_mode);
2953 COMMAND_HANDLER(handle_resume_command)
2957 return ERROR_COMMAND_SYNTAX_ERROR;
2959 struct target *target = get_current_target(CMD_CTX);
2961 /* with no CMD_ARGV, resume from current pc, addr = 0,
2962 * with one arguments, addr = CMD_ARGV[0],
2963 * handle breakpoints, not debugging */
2964 target_addr_t addr = 0;
2965 if (CMD_ARGC == 1) {
2966 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
2970 return target_resume(target, current, addr, 1, 0);
2973 COMMAND_HANDLER(handle_step_command)
2976 return ERROR_COMMAND_SYNTAX_ERROR;
2980 /* with no CMD_ARGV, step from current pc, addr = 0,
2981 * with one argument addr = CMD_ARGV[0],
2982 * handle breakpoints, debugging */
2983 target_addr_t addr = 0;
2985 if (CMD_ARGC == 1) {
2986 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
2990 struct target *target = get_current_target(CMD_CTX);
2992 return target->type->step(target, current_pc, addr, 1);
2995 static void handle_md_output(struct command_context *cmd_ctx,
2996 struct target *target, target_addr_t address, unsigned size,
2997 unsigned count, const uint8_t *buffer)
2999 const unsigned line_bytecnt = 32;
3000 unsigned line_modulo = line_bytecnt / size;
3002 char output[line_bytecnt * 4 + 1];
3003 unsigned output_len = 0;
3005 const char *value_fmt;
3008 value_fmt = "%16.16llx ";
3011 value_fmt = "%8.8x ";
3014 value_fmt = "%4.4x ";
3017 value_fmt = "%2.2x ";
3020 /* "can't happen", caller checked */
3021 LOG_ERROR("invalid memory read size: %u", size);
3025 for (unsigned i = 0; i < count; i++) {
3026 if (i % line_modulo == 0) {
3027 output_len += snprintf(output + output_len,
3028 sizeof(output) - output_len,
3029 TARGET_ADDR_FMT ": ",
3030 (address + (i * size)));
3034 const uint8_t *value_ptr = buffer + i * size;
3037 value = target_buffer_get_u64(target, value_ptr);
3040 value = target_buffer_get_u32(target, value_ptr);
3043 value = target_buffer_get_u16(target, value_ptr);
3048 output_len += snprintf(output + output_len,
3049 sizeof(output) - output_len,
3052 if ((i % line_modulo == line_modulo - 1) || (i == count - 1)) {
3053 command_print(cmd_ctx, "%s", output);
3059 COMMAND_HANDLER(handle_md_command)
3062 return ERROR_COMMAND_SYNTAX_ERROR;
3065 switch (CMD_NAME[2]) {
3079 return ERROR_COMMAND_SYNTAX_ERROR;
3082 bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
3083 int (*fn)(struct target *target,
3084 target_addr_t address, uint32_t size_value, uint32_t count, uint8_t *buffer);
3088 fn = target_read_phys_memory;
3090 fn = target_read_memory;
3091 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
3092 return ERROR_COMMAND_SYNTAX_ERROR;
3094 target_addr_t address;
3095 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], address);
3099 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
3101 uint8_t *buffer = calloc(count, size);
3103 struct target *target = get_current_target(CMD_CTX);
3104 int retval = fn(target, address, size, count, buffer);
3105 if (ERROR_OK == retval)
3106 handle_md_output(CMD_CTX, target, address, size, count, buffer);
3113 typedef int (*target_write_fn)(struct target *target,
3114 target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer);
3116 static int target_fill_mem(struct target *target,
3117 target_addr_t address,
3125 /* We have to write in reasonably large chunks to be able
3126 * to fill large memory areas with any sane speed */
3127 const unsigned chunk_size = 16384;
3128 uint8_t *target_buf = malloc(chunk_size * data_size);
3129 if (target_buf == NULL) {
3130 LOG_ERROR("Out of memory");
3134 for (unsigned i = 0; i < chunk_size; i++) {
3135 switch (data_size) {
3137 target_buffer_set_u64(target, target_buf + i * data_size, b);
3140 target_buffer_set_u32(target, target_buf + i * data_size, b);
3143 target_buffer_set_u16(target, target_buf + i * data_size, b);
3146 target_buffer_set_u8(target, target_buf + i * data_size, b);
3153 int retval = ERROR_OK;
3155 for (unsigned x = 0; x < c; x += chunk_size) {
3158 if (current > chunk_size)
3159 current = chunk_size;
3160 retval = fn(target, address + x * data_size, data_size, current, target_buf);
3161 if (retval != ERROR_OK)
3163 /* avoid GDB timeouts */
3172 COMMAND_HANDLER(handle_mw_command)
3175 return ERROR_COMMAND_SYNTAX_ERROR;
3176 bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
3181 fn = target_write_phys_memory;
3183 fn = target_write_memory;
3184 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
3185 return ERROR_COMMAND_SYNTAX_ERROR;
3187 target_addr_t address;
3188 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], address);
3190 target_addr_t value;
3191 COMMAND_PARSE_ADDRESS(CMD_ARGV[1], value);
3195 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
3197 struct target *target = get_current_target(CMD_CTX);
3199 switch (CMD_NAME[2]) {
3213 return ERROR_COMMAND_SYNTAX_ERROR;
3216 return target_fill_mem(target, address, fn, wordsize, value, count);
3219 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
3220 target_addr_t *min_address, target_addr_t *max_address)
3222 if (CMD_ARGC < 1 || CMD_ARGC > 5)
3223 return ERROR_COMMAND_SYNTAX_ERROR;
3225 /* a base address isn't always necessary,
3226 * default to 0x0 (i.e. don't relocate) */
3227 if (CMD_ARGC >= 2) {
3229 COMMAND_PARSE_ADDRESS(CMD_ARGV[1], addr);
3230 image->base_address = addr;
3231 image->base_address_set = 1;
3233 image->base_address_set = 0;
3235 image->start_address_set = 0;
3238 COMMAND_PARSE_ADDRESS(CMD_ARGV[3], *min_address);
3239 if (CMD_ARGC == 5) {
3240 COMMAND_PARSE_ADDRESS(CMD_ARGV[4], *max_address);
3241 /* use size (given) to find max (required) */
3242 *max_address += *min_address;
3245 if (*min_address > *max_address)
3246 return ERROR_COMMAND_SYNTAX_ERROR;
3251 COMMAND_HANDLER(handle_load_image_command)
3255 uint32_t image_size;
3256 target_addr_t min_address = 0;
3257 target_addr_t max_address = -1;
3261 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
3262 &image, &min_address, &max_address);
3263 if (ERROR_OK != retval)
3266 struct target *target = get_current_target(CMD_CTX);
3268 struct duration bench;
3269 duration_start(&bench);
3271 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
3276 for (i = 0; i < image.num_sections; i++) {
3277 buffer = malloc(image.sections[i].size);
3278 if (buffer == NULL) {
3279 command_print(CMD_CTX,
3280 "error allocating buffer for section (%d bytes)",
3281 (int)(image.sections[i].size));
3282 retval = ERROR_FAIL;
3286 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
3287 if (retval != ERROR_OK) {
3292 uint32_t offset = 0;
3293 uint32_t length = buf_cnt;
3295 /* DANGER!!! beware of unsigned comparision here!!! */
3297 if ((image.sections[i].base_address + buf_cnt >= min_address) &&
3298 (image.sections[i].base_address < max_address)) {
3300 if (image.sections[i].base_address < min_address) {
3301 /* clip addresses below */
3302 offset += min_address-image.sections[i].base_address;
3306 if (image.sections[i].base_address + buf_cnt > max_address)
3307 length -= (image.sections[i].base_address + buf_cnt)-max_address;
3309 retval = target_write_buffer(target,
3310 image.sections[i].base_address + offset, length, buffer + offset);
3311 if (retval != ERROR_OK) {
3315 image_size += length;
3316 command_print(CMD_CTX, "%u bytes written at address " TARGET_ADDR_FMT "",
3317 (unsigned int)length,
3318 image.sections[i].base_address + offset);
3324 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
3325 command_print(CMD_CTX, "downloaded %" PRIu32 " bytes "
3326 "in %fs (%0.3f KiB/s)", image_size,
3327 duration_elapsed(&bench), duration_kbps(&bench, image_size));
3330 image_close(&image);
3336 COMMAND_HANDLER(handle_dump_image_command)
3338 struct fileio *fileio;
3340 int retval, retvaltemp;
3341 target_addr_t address, size;
3342 struct duration bench;
3343 struct target *target = get_current_target(CMD_CTX);
3346 return ERROR_COMMAND_SYNTAX_ERROR;
3348 COMMAND_PARSE_ADDRESS(CMD_ARGV[1], address);
3349 COMMAND_PARSE_ADDRESS(CMD_ARGV[2], size);
3351 uint32_t buf_size = (size > 4096) ? 4096 : size;
3352 buffer = malloc(buf_size);
3356 retval = fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY);
3357 if (retval != ERROR_OK) {
3362 duration_start(&bench);
3365 size_t size_written;
3366 uint32_t this_run_size = (size > buf_size) ? buf_size : size;
3367 retval = target_read_buffer(target, address, this_run_size, buffer);
3368 if (retval != ERROR_OK)
3371 retval = fileio_write(fileio, this_run_size, buffer, &size_written);
3372 if (retval != ERROR_OK)
3375 size -= this_run_size;
3376 address += this_run_size;
3381 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
3383 retval = fileio_size(fileio, &filesize);
3384 if (retval != ERROR_OK)
3386 command_print(CMD_CTX,
3387 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize,
3388 duration_elapsed(&bench), duration_kbps(&bench, filesize));
3391 retvaltemp = fileio_close(fileio);
3392 if (retvaltemp != ERROR_OK)
3401 IMAGE_CHECKSUM_ONLY = 2
3404 static COMMAND_HELPER(handle_verify_image_command_internal, enum verify_mode verify)
3408 uint32_t image_size;
3411 uint32_t checksum = 0;
3412 uint32_t mem_checksum = 0;
3416 struct target *target = get_current_target(CMD_CTX);
3419 return ERROR_COMMAND_SYNTAX_ERROR;
3422 LOG_ERROR("no target selected");
3426 struct duration bench;
3427 duration_start(&bench);
3429 if (CMD_ARGC >= 2) {
3431 COMMAND_PARSE_ADDRESS(CMD_ARGV[1], addr);
3432 image.base_address = addr;
3433 image.base_address_set = 1;
3435 image.base_address_set = 0;
3436 image.base_address = 0x0;
3439 image.start_address_set = 0;
3441 retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL);
3442 if (retval != ERROR_OK)
3448 for (i = 0; i < image.num_sections; i++) {
3449 buffer = malloc(image.sections[i].size);
3450 if (buffer == NULL) {
3451 command_print(CMD_CTX,
3452 "error allocating buffer for section (%d bytes)",
3453 (int)(image.sections[i].size));
3456 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
3457 if (retval != ERROR_OK) {
3462 if (verify >= IMAGE_VERIFY) {
3463 /* calculate checksum of image */
3464 retval = image_calculate_checksum(buffer, buf_cnt, &checksum);
3465 if (retval != ERROR_OK) {
3470 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
3471 if (retval != ERROR_OK) {
3475 if ((checksum != mem_checksum) && (verify == IMAGE_CHECKSUM_ONLY)) {
3476 LOG_ERROR("checksum mismatch");
3478 retval = ERROR_FAIL;
3481 if (checksum != mem_checksum) {
3482 /* failed crc checksum, fall back to a binary compare */
3486 LOG_ERROR("checksum mismatch - attempting binary compare");
3488 data = malloc(buf_cnt);
3490 /* Can we use 32bit word accesses? */
3492 int count = buf_cnt;
3493 if ((count % 4) == 0) {
3497 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
3498 if (retval == ERROR_OK) {
3500 for (t = 0; t < buf_cnt; t++) {
3501 if (data[t] != buffer[t]) {
3502 command_print(CMD_CTX,
3503 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3505 (unsigned)(t + image.sections[i].base_address),
3508 if (diffs++ >= 127) {
3509 command_print(CMD_CTX, "More than 128 errors, the rest are not printed.");
3521 command_print(CMD_CTX, "address " TARGET_ADDR_FMT " length 0x%08zx",
3522 image.sections[i].base_address,
3527 image_size += buf_cnt;
3530 command_print(CMD_CTX, "No more differences found.");
3533 retval = ERROR_FAIL;
3534 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
3535 command_print(CMD_CTX, "verified %" PRIu32 " bytes "
3536 "in %fs (%0.3f KiB/s)", image_size,
3537 duration_elapsed(&bench), duration_kbps(&bench, image_size));
3540 image_close(&image);
3545 COMMAND_HANDLER(handle_verify_image_checksum_command)
3547 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_CHECKSUM_ONLY);
3550 COMMAND_HANDLER(handle_verify_image_command)
3552 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_VERIFY);
3555 COMMAND_HANDLER(handle_test_image_command)
3557 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_TEST);
3560 static int handle_bp_command_list(struct command_context *cmd_ctx)
3562 struct target *target = get_current_target(cmd_ctx);
3563 struct breakpoint *breakpoint = target->breakpoints;
3564 while (breakpoint) {
3565 if (breakpoint->type == BKPT_SOFT) {
3566 char *buf = buf_to_str(breakpoint->orig_instr,
3567 breakpoint->length, 16);
3568 command_print(cmd_ctx, "IVA breakpoint: " TARGET_ADDR_FMT ", 0x%x, %i, 0x%s",
3569 breakpoint->address,
3571 breakpoint->set, buf);
3574 if ((breakpoint->address == 0) && (breakpoint->asid != 0))
3575 command_print(cmd_ctx, "Context breakpoint: 0x%8.8" PRIx32 ", 0x%x, %i",
3577 breakpoint->length, breakpoint->set);
3578 else if ((breakpoint->address != 0) && (breakpoint->asid != 0)) {
3579 command_print(cmd_ctx, "Hybrid breakpoint(IVA): " TARGET_ADDR_FMT ", 0x%x, %i",
3580 breakpoint->address,
3581 breakpoint->length, breakpoint->set);
3582 command_print(cmd_ctx, "\t|--->linked with ContextID: 0x%8.8" PRIx32,
3585 command_print(cmd_ctx, "Breakpoint(IVA): " TARGET_ADDR_FMT ", 0x%x, %i",
3586 breakpoint->address,
3587 breakpoint->length, breakpoint->set);
3590 breakpoint = breakpoint->next;
3595 static int handle_bp_command_set(struct command_context *cmd_ctx,
3596 target_addr_t addr, uint32_t asid, uint32_t length, int hw)
3598 struct target *target = get_current_target(cmd_ctx);
3602 retval = breakpoint_add(target, addr, length, hw);
3603 if (ERROR_OK == retval)
3604 command_print(cmd_ctx, "breakpoint set at " TARGET_ADDR_FMT "", addr);
3606 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3609 } else if (addr == 0) {
3610 if (target->type->add_context_breakpoint == NULL) {
3611 LOG_WARNING("Context breakpoint not available");
3614 retval = context_breakpoint_add(target, asid, length, hw);
3615 if (ERROR_OK == retval)
3616 command_print(cmd_ctx, "Context breakpoint set at 0x%8.8" PRIx32 "", asid);
3618 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3622 if (target->type->add_hybrid_breakpoint == NULL) {
3623 LOG_WARNING("Hybrid breakpoint not available");
3626 retval = hybrid_breakpoint_add(target, addr, asid, length, hw);
3627 if (ERROR_OK == retval)
3628 command_print(cmd_ctx, "Hybrid breakpoint set at 0x%8.8" PRIx32 "", asid);
3630 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3637 COMMAND_HANDLER(handle_bp_command)
3646 return handle_bp_command_list(CMD_CTX);
3650 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
3651 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3652 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3655 if (strcmp(CMD_ARGV[2], "hw") == 0) {
3657 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
3658 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3660 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3661 } else if (strcmp(CMD_ARGV[2], "hw_ctx") == 0) {
3663 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], asid);
3664 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3666 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3671 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
3672 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], asid);
3673 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], length);
3674 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3677 return ERROR_COMMAND_SYNTAX_ERROR;
3681 COMMAND_HANDLER(handle_rbp_command)
3684 return ERROR_COMMAND_SYNTAX_ERROR;
3687 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
3689 struct target *target = get_current_target(CMD_CTX);
3690 breakpoint_remove(target, addr);
3695 COMMAND_HANDLER(handle_wp_command)
3697 struct target *target = get_current_target(CMD_CTX);
3699 if (CMD_ARGC == 0) {
3700 struct watchpoint *watchpoint = target->watchpoints;
3702 while (watchpoint) {
3703 command_print(CMD_CTX, "address: " TARGET_ADDR_FMT
3704 ", len: 0x%8.8" PRIx32
3705 ", r/w/a: %i, value: 0x%8.8" PRIx32
3706 ", mask: 0x%8.8" PRIx32,
3707 watchpoint->address,
3709 (int)watchpoint->rw,
3712 watchpoint = watchpoint->next;
3717 enum watchpoint_rw type = WPT_ACCESS;
3719 uint32_t length = 0;
3720 uint32_t data_value = 0x0;
3721 uint32_t data_mask = 0xffffffff;
3725 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
3728 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
3731 switch (CMD_ARGV[2][0]) {
3742 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
3743 return ERROR_COMMAND_SYNTAX_ERROR;
3747 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3748 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3752 return ERROR_COMMAND_SYNTAX_ERROR;
3755 int retval = watchpoint_add(target, addr, length, type,
3756 data_value, data_mask);
3757 if (ERROR_OK != retval)
3758 LOG_ERROR("Failure setting watchpoints");
3763 COMMAND_HANDLER(handle_rwp_command)
3766 return ERROR_COMMAND_SYNTAX_ERROR;
3769 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3771 struct target *target = get_current_target(CMD_CTX);
3772 watchpoint_remove(target, addr);
3778 * Translate a virtual address to a physical address.
3780 * The low-level target implementation must have logged a detailed error
3781 * which is forwarded to telnet/GDB session.
3783 COMMAND_HANDLER(handle_virt2phys_command)
3786 return ERROR_COMMAND_SYNTAX_ERROR;
3789 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], va);
3792 struct target *target = get_current_target(CMD_CTX);
3793 int retval = target->type->virt2phys(target, va, &pa);
3794 if (retval == ERROR_OK)
3795 command_print(CMD_CTX, "Physical address " TARGET_ADDR_FMT "", pa);
3800 static void writeData(FILE *f, const void *data, size_t len)
3802 size_t written = fwrite(data, 1, len, f);
3804 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
3807 static void writeLong(FILE *f, int l, struct target *target)
3811 target_buffer_set_u32(target, val, l);
3812 writeData(f, val, 4);
3815 static void writeString(FILE *f, char *s)
3817 writeData(f, s, strlen(s));
3820 typedef unsigned char UNIT[2]; /* unit of profiling */
3822 /* Dump a gmon.out histogram file. */
3823 static void write_gmon(uint32_t *samples, uint32_t sampleNum, const char *filename, bool with_range,
3824 uint32_t start_address, uint32_t end_address, struct target *target)
3827 FILE *f = fopen(filename, "w");
3830 writeString(f, "gmon");
3831 writeLong(f, 0x00000001, target); /* Version */
3832 writeLong(f, 0, target); /* padding */
3833 writeLong(f, 0, target); /* padding */
3834 writeLong(f, 0, target); /* padding */
3836 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
3837 writeData(f, &zero, 1);
3839 /* figure out bucket size */
3843 min = start_address;
3848 for (i = 0; i < sampleNum; i++) {
3849 if (min > samples[i])
3851 if (max < samples[i])
3855 /* max should be (largest sample + 1)
3856 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
3860 int addressSpace = max - min;
3861 assert(addressSpace >= 2);
3863 /* FIXME: What is the reasonable number of buckets?
3864 * The profiling result will be more accurate if there are enough buckets. */
3865 static const uint32_t maxBuckets = 128 * 1024; /* maximum buckets. */
3866 uint32_t numBuckets = addressSpace / sizeof(UNIT);
3867 if (numBuckets > maxBuckets)
3868 numBuckets = maxBuckets;
3869 int *buckets = malloc(sizeof(int) * numBuckets);
3870 if (buckets == NULL) {
3874 memset(buckets, 0, sizeof(int) * numBuckets);
3875 for (i = 0; i < sampleNum; i++) {
3876 uint32_t address = samples[i];
3878 if ((address < min) || (max <= address))
3881 long long a = address - min;
3882 long long b = numBuckets;
3883 long long c = addressSpace;
3884 int index_t = (a * b) / c; /* danger!!!! int32 overflows */
3888 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3889 writeLong(f, min, target); /* low_pc */
3890 writeLong(f, max, target); /* high_pc */
3891 writeLong(f, numBuckets, target); /* # of buckets */
3892 writeLong(f, 100, target); /* KLUDGE! We lie, ca. 100Hz best case. */
3893 writeString(f, "seconds");
3894 for (i = 0; i < (15-strlen("seconds")); i++)
3895 writeData(f, &zero, 1);
3896 writeString(f, "s");
3898 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3900 char *data = malloc(2 * numBuckets);
3902 for (i = 0; i < numBuckets; i++) {
3907 data[i * 2] = val&0xff;
3908 data[i * 2 + 1] = (val >> 8) & 0xff;
3911 writeData(f, data, numBuckets * 2);
3919 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3920 * which will be used as a random sampling of PC */
3921 COMMAND_HANDLER(handle_profile_command)
3923 struct target *target = get_current_target(CMD_CTX);
3925 if ((CMD_ARGC != 2) && (CMD_ARGC != 4))
3926 return ERROR_COMMAND_SYNTAX_ERROR;
3928 const uint32_t MAX_PROFILE_SAMPLE_NUM = 10000;
3930 uint32_t num_of_samples;
3931 int retval = ERROR_OK;
3933 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], offset);
3935 uint32_t *samples = malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM);
3936 if (samples == NULL) {
3937 LOG_ERROR("No memory to store samples.");
3942 * Some cores let us sample the PC without the
3943 * annoying halt/resume step; for example, ARMv7 PCSR.
3944 * Provide a way to use that more efficient mechanism.
3946 retval = target_profiling(target, samples, MAX_PROFILE_SAMPLE_NUM,
3947 &num_of_samples, offset);
3948 if (retval != ERROR_OK) {
3953 assert(num_of_samples <= MAX_PROFILE_SAMPLE_NUM);
3955 retval = target_poll(target);
3956 if (retval != ERROR_OK) {
3960 if (target->state == TARGET_RUNNING) {
3961 retval = target_halt(target);
3962 if (retval != ERROR_OK) {
3968 retval = target_poll(target);
3969 if (retval != ERROR_OK) {
3974 uint32_t start_address = 0;
3975 uint32_t end_address = 0;
3976 bool with_range = false;
3977 if (CMD_ARGC == 4) {
3979 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], start_address);
3980 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], end_address);
3983 write_gmon(samples, num_of_samples, CMD_ARGV[1],
3984 with_range, start_address, end_address, target);
3985 command_print(CMD_CTX, "Wrote %s", CMD_ARGV[1]);
3991 static int new_int_array_element(Jim_Interp *interp, const char *varname, int idx, uint32_t val)
3994 Jim_Obj *nameObjPtr, *valObjPtr;
3997 namebuf = alloc_printf("%s(%d)", varname, idx);
4001 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
4002 valObjPtr = Jim_NewIntObj(interp, val);
4003 if (!nameObjPtr || !valObjPtr) {
4008 Jim_IncrRefCount(nameObjPtr);
4009 Jim_IncrRefCount(valObjPtr);
4010 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
4011 Jim_DecrRefCount(interp, nameObjPtr);
4012 Jim_DecrRefCount(interp, valObjPtr);
4014 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
4018 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4020 struct command_context *context;
4021 struct target *target;
4023 context = current_command_context(interp);
4024 assert(context != NULL);
4026 target = get_current_target(context);
4027 if (target == NULL) {
4028 LOG_ERROR("mem2array: no current target");
4032 return target_mem2array(interp, target, argc - 1, argv + 1);
4035 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
4043 const char *varname;
4049 /* argv[1] = name of array to receive the data
4050 * argv[2] = desired width
4051 * argv[3] = memory address
4052 * argv[4] = count of times to read
4054 if (argc < 4 || argc > 5) {
4055 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems [phys]");
4058 varname = Jim_GetString(argv[0], &len);
4059 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4061 e = Jim_GetLong(interp, argv[1], &l);
4066 e = Jim_GetLong(interp, argv[2], &l);
4070 e = Jim_GetLong(interp, argv[3], &l);
4076 phys = Jim_GetString(argv[4], &n);
4077 if (!strncmp(phys, "phys", n))
4093 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4094 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
4098 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4099 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
4102 if ((addr + (len * width)) < addr) {
4103 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4104 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
4107 /* absurd transfer size? */
4109 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4110 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
4115 ((width == 2) && ((addr & 1) == 0)) ||
4116 ((width == 4) && ((addr & 3) == 0))) {
4120 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4121 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
4124 Jim_AppendStrings(interp, Jim_GetResult(interp), buf, NULL);
4133 size_t buffersize = 4096;
4134 uint8_t *buffer = malloc(buffersize);
4141 /* Slurp... in buffer size chunks */
4143 count = len; /* in objects.. */
4144 if (count > (buffersize / width))
4145 count = (buffersize / width);
4148 retval = target_read_phys_memory(target, addr, width, count, buffer);
4150 retval = target_read_memory(target, addr, width, count, buffer);
4151 if (retval != ERROR_OK) {
4153 LOG_ERROR("mem2array: Read @ 0x%08" PRIx32 ", w=%" PRId32 ", cnt=%" PRId32 ", failed",
4157 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4158 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
4162 v = 0; /* shut up gcc */
4163 for (i = 0; i < count ; i++, n++) {
4166 v = target_buffer_get_u32(target, &buffer[i*width]);
4169 v = target_buffer_get_u16(target, &buffer[i*width]);
4172 v = buffer[i] & 0x0ff;
4175 new_int_array_element(interp, varname, n, v);
4178 addr += count * width;
4184 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4189 static int get_int_array_element(Jim_Interp *interp, const char *varname, int idx, uint32_t *val)
4192 Jim_Obj *nameObjPtr, *valObjPtr;
4196 namebuf = alloc_printf("%s(%d)", varname, idx);
4200 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
4206 Jim_IncrRefCount(nameObjPtr);
4207 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
4208 Jim_DecrRefCount(interp, nameObjPtr);
4210 if (valObjPtr == NULL)
4213 result = Jim_GetLong(interp, valObjPtr, &l);
4214 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
4219 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4221 struct command_context *context;
4222 struct target *target;
4224 context = current_command_context(interp);
4225 assert(context != NULL);
4227 target = get_current_target(context);
4228 if (target == NULL) {
4229 LOG_ERROR("array2mem: no current target");
4233 return target_array2mem(interp, target, argc-1, argv + 1);
4236 static int target_array2mem(Jim_Interp *interp, struct target *target,
4237 int argc, Jim_Obj *const *argv)
4245 const char *varname;
4251 /* argv[1] = name of array to get the data
4252 * argv[2] = desired width
4253 * argv[3] = memory address
4254 * argv[4] = count to write
4256 if (argc < 4 || argc > 5) {
4257 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems [phys]");
4260 varname = Jim_GetString(argv[0], &len);
4261 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4263 e = Jim_GetLong(interp, argv[1], &l);
4268 e = Jim_GetLong(interp, argv[2], &l);
4272 e = Jim_GetLong(interp, argv[3], &l);
4278 phys = Jim_GetString(argv[4], &n);
4279 if (!strncmp(phys, "phys", n))
4295 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4296 Jim_AppendStrings(interp, Jim_GetResult(interp),
4297 "Invalid width param, must be 8/16/32", NULL);
4301 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4302 Jim_AppendStrings(interp, Jim_GetResult(interp),
4303 "array2mem: zero width read?", NULL);
4306 if ((addr + (len * width)) < addr) {
4307 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4308 Jim_AppendStrings(interp, Jim_GetResult(interp),
4309 "array2mem: addr + len - wraps to zero?", NULL);
4312 /* absurd transfer size? */
4314 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4315 Jim_AppendStrings(interp, Jim_GetResult(interp),
4316 "array2mem: absurd > 64K item request", NULL);
4321 ((width == 2) && ((addr & 1) == 0)) ||
4322 ((width == 4) && ((addr & 3) == 0))) {
4326 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4327 sprintf(buf, "array2mem address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
4330 Jim_AppendStrings(interp, Jim_GetResult(interp), buf, NULL);
4341 size_t buffersize = 4096;
4342 uint8_t *buffer = malloc(buffersize);
4347 /* Slurp... in buffer size chunks */
4349 count = len; /* in objects.. */
4350 if (count > (buffersize / width))
4351 count = (buffersize / width);
4353 v = 0; /* shut up gcc */
4354 for (i = 0; i < count; i++, n++) {
4355 get_int_array_element(interp, varname, n, &v);
4358 target_buffer_set_u32(target, &buffer[i * width], v);
4361 target_buffer_set_u16(target, &buffer[i * width], v);
4364 buffer[i] = v & 0x0ff;
4371 retval = target_write_phys_memory(target, addr, width, count, buffer);
4373 retval = target_write_memory(target, addr, width, count, buffer);
4374 if (retval != ERROR_OK) {
4376 LOG_ERROR("array2mem: Write @ 0x%08" PRIx32 ", w=%" PRId32 ", cnt=%" PRId32 ", failed",
4380 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4381 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
4385 addr += count * width;
4390 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4395 /* FIX? should we propagate errors here rather than printing them
4398 void target_handle_event(struct target *target, enum target_event e)
4400 struct target_event_action *teap;
4402 for (teap = target->event_action; teap != NULL; teap = teap->next) {
4403 if (teap->event == e) {
4404 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
4405 target->target_number,
4406 target_name(target),
4407 target_type_name(target),
4409 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
4410 Jim_GetString(teap->body, NULL));
4411 if (Jim_EvalObj(teap->interp, teap->body) != JIM_OK) {
4412 Jim_MakeErrorMessage(teap->interp);
4413 command_print(NULL, "%s\n", Jim_GetString(Jim_GetResult(teap->interp), NULL));
4420 * Returns true only if the target has a handler for the specified event.
4422 bool target_has_event_action(struct target *target, enum target_event event)
4424 struct target_event_action *teap;
4426 for (teap = target->event_action; teap != NULL; teap = teap->next) {
4427 if (teap->event == event)
4433 enum target_cfg_param {
4436 TCFG_WORK_AREA_VIRT,
4437 TCFG_WORK_AREA_PHYS,
4438 TCFG_WORK_AREA_SIZE,
4439 TCFG_WORK_AREA_BACKUP,
4442 TCFG_CHAIN_POSITION,
4448 static Jim_Nvp nvp_config_opts[] = {
4449 { .name = "-type", .value = TCFG_TYPE },
4450 { .name = "-event", .value = TCFG_EVENT },
4451 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
4452 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
4453 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
4454 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
4455 { .name = "-endian" , .value = TCFG_ENDIAN },
4456 { .name = "-coreid", .value = TCFG_COREID },
4457 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
4458 { .name = "-dbgbase", .value = TCFG_DBGBASE },
4459 { .name = "-rtos", .value = TCFG_RTOS },
4460 { .name = "-defer-examine", .value = TCFG_DEFER_EXAMINE },
4461 { .name = NULL, .value = -1 }
4464 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
4471 /* parse config or cget options ... */
4472 while (goi->argc > 0) {
4473 Jim_SetEmptyResult(goi->interp);
4474 /* Jim_GetOpt_Debug(goi); */
4476 if (target->type->target_jim_configure) {
4477 /* target defines a configure function */
4478 /* target gets first dibs on parameters */
4479 e = (*(target->type->target_jim_configure))(target, goi);
4488 /* otherwise we 'continue' below */
4490 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
4492 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
4498 if (goi->isconfigure) {
4499 Jim_SetResultFormatted(goi->interp,
4500 "not settable: %s", n->name);
4504 if (goi->argc != 0) {
4505 Jim_WrongNumArgs(goi->interp,
4506 goi->argc, goi->argv,
4511 Jim_SetResultString(goi->interp,
4512 target_type_name(target), -1);
4516 if (goi->argc == 0) {
4517 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
4521 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
4523 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
4527 if (goi->isconfigure) {
4528 if (goi->argc != 1) {
4529 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
4533 if (goi->argc != 0) {
4534 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
4540 struct target_event_action *teap;
4542 teap = target->event_action;
4543 /* replace existing? */
4545 if (teap->event == (enum target_event)n->value)
4550 if (goi->isconfigure) {
4551 bool replace = true;
4554 teap = calloc(1, sizeof(*teap));
4557 teap->event = n->value;
4558 teap->interp = goi->interp;
4559 Jim_GetOpt_Obj(goi, &o);
4561 Jim_DecrRefCount(teap->interp, teap->body);
4562 teap->body = Jim_DuplicateObj(goi->interp, o);
4565 * Tcl/TK - "tk events" have a nice feature.
4566 * See the "BIND" command.
4567 * We should support that here.
4568 * You can specify %X and %Y in the event code.
4569 * The idea is: %T - target name.
4570 * The idea is: %N - target number
4571 * The idea is: %E - event name.
4573 Jim_IncrRefCount(teap->body);
4576 /* add to head of event list */
4577 teap->next = target->event_action;
4578 target->event_action = teap;
4580 Jim_SetEmptyResult(goi->interp);
4584 Jim_SetEmptyResult(goi->interp);
4586 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
4592 case TCFG_WORK_AREA_VIRT:
4593 if (goi->isconfigure) {
4594 target_free_all_working_areas(target);
4595 e = Jim_GetOpt_Wide(goi, &w);
4598 target->working_area_virt = w;
4599 target->working_area_virt_spec = true;
4604 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
4608 case TCFG_WORK_AREA_PHYS:
4609 if (goi->isconfigure) {
4610 target_free_all_working_areas(target);
4611 e = Jim_GetOpt_Wide(goi, &w);
4614 target->working_area_phys = w;
4615 target->working_area_phys_spec = true;
4620 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
4624 case TCFG_WORK_AREA_SIZE:
4625 if (goi->isconfigure) {
4626 target_free_all_working_areas(target);
4627 e = Jim_GetOpt_Wide(goi, &w);
4630 target->working_area_size = w;
4635 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
4639 case TCFG_WORK_AREA_BACKUP:
4640 if (goi->isconfigure) {
4641 target_free_all_working_areas(target);
4642 e = Jim_GetOpt_Wide(goi, &w);
4645 /* make this exactly 1 or 0 */
4646 target->backup_working_area = (!!w);
4651 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
4652 /* loop for more e*/
4657 if (goi->isconfigure) {
4658 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
4660 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
4663 target->endianness = n->value;
4668 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
4669 if (n->name == NULL) {
4670 target->endianness = TARGET_LITTLE_ENDIAN;
4671 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
4673 Jim_SetResultString(goi->interp, n->name, -1);
4678 if (goi->isconfigure) {
4679 e = Jim_GetOpt_Wide(goi, &w);
4682 target->coreid = (int32_t)w;
4687 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
4691 case TCFG_CHAIN_POSITION:
4692 if (goi->isconfigure) {
4694 struct jtag_tap *tap;
4695 target_free_all_working_areas(target);
4696 e = Jim_GetOpt_Obj(goi, &o_t);
4699 tap = jtag_tap_by_jim_obj(goi->interp, o_t);
4702 /* make this exactly 1 or 0 */
4708 Jim_SetResultString(goi->interp, target->tap->dotted_name, -1);
4709 /* loop for more e*/
4712 if (goi->isconfigure) {
4713 e = Jim_GetOpt_Wide(goi, &w);
4716 target->dbgbase = (uint32_t)w;
4717 target->dbgbase_set = true;
4722 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->dbgbase));
4729 int result = rtos_create(goi, target);
4730 if (result != JIM_OK)
4736 case TCFG_DEFER_EXAMINE:
4738 target->defer_examine = true;
4743 } /* while (goi->argc) */
4746 /* done - we return */
4750 static int jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
4754 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4755 goi.isconfigure = !strcmp(Jim_GetString(argv[0], NULL), "configure");
4757 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
4758 "missing: -option ...");
4761 struct target *target = Jim_CmdPrivData(goi.interp);
4762 return target_configure(&goi, target);
4765 static int jim_target_mw(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4767 const char *cmd_name = Jim_GetString(argv[0], NULL);
4770 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4772 if (goi.argc < 2 || goi.argc > 4) {
4773 Jim_SetResultFormatted(goi.interp,
4774 "usage: %s [phys] <address> <data> [<count>]", cmd_name);
4779 fn = target_write_memory;
4782 if (strcmp(Jim_GetString(argv[1], NULL), "phys") == 0) {
4784 struct Jim_Obj *obj;
4785 e = Jim_GetOpt_Obj(&goi, &obj);
4789 fn = target_write_phys_memory;
4793 e = Jim_GetOpt_Wide(&goi, &a);
4798 e = Jim_GetOpt_Wide(&goi, &b);
4803 if (goi.argc == 1) {
4804 e = Jim_GetOpt_Wide(&goi, &c);
4809 /* all args must be consumed */
4813 struct target *target = Jim_CmdPrivData(goi.interp);
4815 if (strcasecmp(cmd_name, "mww") == 0)
4817 else if (strcasecmp(cmd_name, "mwh") == 0)
4819 else if (strcasecmp(cmd_name, "mwb") == 0)
4822 LOG_ERROR("command '%s' unknown: ", cmd_name);
4826 return (target_fill_mem(target, a, fn, data_size, b, c) == ERROR_OK) ? JIM_OK : JIM_ERR;
4830 * @brief Reads an array of words/halfwords/bytes from target memory starting at specified address.
4832 * Usage: mdw [phys] <address> [<count>] - for 32 bit reads
4833 * mdh [phys] <address> [<count>] - for 16 bit reads
4834 * mdb [phys] <address> [<count>] - for 8 bit reads
4836 * Count defaults to 1.
4838 * Calls target_read_memory or target_read_phys_memory depending on
4839 * the presence of the "phys" argument
4840 * Reads the target memory in blocks of max. 32 bytes, and returns an array of ints formatted
4841 * to int representation in base16.
4842 * Also outputs read data in a human readable form using command_print
4844 * @param phys if present target_read_phys_memory will be used instead of target_read_memory
4845 * @param address address where to start the read. May be specified in decimal or hex using the standard "0x" prefix
4846 * @param count optional count parameter to read an array of values. If not specified, defaults to 1.
4847 * @returns: JIM_ERR on error or JIM_OK on success and sets the result string to an array of ascii formatted numbers
4848 * on success, with [<count>] number of elements.
4850 * In case of little endian target:
4851 * Example1: "mdw 0x00000000" returns "10123456"
4852 * Exmaple2: "mdh 0x00000000 1" returns "3456"
4853 * Example3: "mdb 0x00000000" returns "56"
4854 * Example4: "mdh 0x00000000 2" returns "3456 1012"
4855 * Example5: "mdb 0x00000000 3" returns "56 34 12"
4857 static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4859 const char *cmd_name = Jim_GetString(argv[0], NULL);
4862 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4864 if ((goi.argc < 1) || (goi.argc > 3)) {
4865 Jim_SetResultFormatted(goi.interp,
4866 "usage: %s [phys] <address> [<count>]", cmd_name);
4870 int (*fn)(struct target *target,
4871 target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer);
4872 fn = target_read_memory;
4875 if (strcmp(Jim_GetString(argv[1], NULL), "phys") == 0) {
4877 struct Jim_Obj *obj;
4878 e = Jim_GetOpt_Obj(&goi, &obj);
4882 fn = target_read_phys_memory;
4885 /* Read address parameter */
4887 e = Jim_GetOpt_Wide(&goi, &addr);
4891 /* If next parameter exists, read it out as the count parameter, if not, set it to 1 (default) */
4893 if (goi.argc == 1) {
4894 e = Jim_GetOpt_Wide(&goi, &count);
4900 /* all args must be consumed */
4904 jim_wide dwidth = 1; /* shut up gcc */
4905 if (strcasecmp(cmd_name, "mdw") == 0)
4907 else if (strcasecmp(cmd_name, "mdh") == 0)
4909 else if (strcasecmp(cmd_name, "mdb") == 0)
4912 LOG_ERROR("command '%s' unknown: ", cmd_name);
4916 /* convert count to "bytes" */
4917 int bytes = count * dwidth;
4919 struct target *target = Jim_CmdPrivData(goi.interp);
4920 uint8_t target_buf[32];
4923 y = (bytes < 16) ? bytes : 16; /* y = min(bytes, 16); */
4925 /* Try to read out next block */
4926 e = fn(target, addr, dwidth, y / dwidth, target_buf);
4928 if (e != ERROR_OK) {
4929 Jim_SetResultFormatted(interp, "error reading target @ 0x%08lx", (long)addr);
4933 command_print_sameline(NULL, "0x%08x ", (int)(addr));
4936 for (x = 0; x < 16 && x < y; x += 4) {
4937 z = target_buffer_get_u32(target, &(target_buf[x]));
4938 command_print_sameline(NULL, "%08x ", (int)(z));
4940 for (; (x < 16) ; x += 4)
4941 command_print_sameline(NULL, " ");
4944 for (x = 0; x < 16 && x < y; x += 2) {
4945 z = target_buffer_get_u16(target, &(target_buf[x]));
4946 command_print_sameline(NULL, "%04x ", (int)(z));
4948 for (; (x < 16) ; x += 2)
4949 command_print_sameline(NULL, " ");
4953 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4954 z = target_buffer_get_u8(target, &(target_buf[x]));
4955 command_print_sameline(NULL, "%02x ", (int)(z));
4957 for (; (x < 16) ; x += 1)
4958 command_print_sameline(NULL, " ");
4961 /* ascii-ify the bytes */
4962 for (x = 0 ; x < y ; x++) {
4963 if ((target_buf[x] >= 0x20) &&
4964 (target_buf[x] <= 0x7e)) {
4968 target_buf[x] = '.';
4973 target_buf[x] = ' ';
4978 /* print - with a newline */
4979 command_print_sameline(NULL, "%s\n", target_buf);
4987 static int jim_target_mem2array(Jim_Interp *interp,
4988 int argc, Jim_Obj *const *argv)
4990 struct target *target = Jim_CmdPrivData(interp);
4991 return target_mem2array(interp, target, argc - 1, argv + 1);
4994 static int jim_target_array2mem(Jim_Interp *interp,
4995 int argc, Jim_Obj *const *argv)
4997 struct target *target = Jim_CmdPrivData(interp);
4998 return target_array2mem(interp, target, argc - 1, argv + 1);
5001 static int jim_target_tap_disabled(Jim_Interp *interp)
5003 Jim_SetResultFormatted(interp, "[TAP is disabled]");
5007 static int jim_target_examine(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5009 bool allow_defer = false;
5012 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5014 const char *cmd_name = Jim_GetString(argv[0], NULL);
5015 Jim_SetResultFormatted(goi.interp,
5016 "usage: %s ['allow-defer']", cmd_name);
5020 strcmp(Jim_GetString(argv[1], NULL), "allow-defer") == 0) {
5022 struct Jim_Obj *obj;
5023 int e = Jim_GetOpt_Obj(&goi, &obj);
5029 struct target *target = Jim_CmdPrivData(interp);
5030 if (!target->tap->enabled)
5031 return jim_target_tap_disabled(interp);
5033 if (allow_defer && target->defer_examine) {
5034 LOG_INFO("Deferring arp_examine of %s", target_name(target));
5035 LOG_INFO("Use arp_examine command to examine it manually!");
5039 int e = target->type->examine(target);
5045 static int jim_target_was_examined(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
5047 struct target *target = Jim_CmdPrivData(interp);
5049 Jim_SetResultBool(interp, target_was_examined(target));
5053 static int jim_target_examine_deferred(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
5055 struct target *target = Jim_CmdPrivData(interp);
5057 Jim_SetResultBool(interp, target->defer_examine);
5061 static int jim_target_halt_gdb(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5064 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
5067 struct target *target = Jim_CmdPrivData(interp);
5069 if (target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT) != ERROR_OK)
5075 static int jim_target_poll(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5078 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
5081 struct target *target = Jim_CmdPrivData(interp);
5082 if (!target->tap->enabled)
5083 return jim_target_tap_disabled(interp);
5086 if (!(target_was_examined(target)))
5087 e = ERROR_TARGET_NOT_EXAMINED;
5089 e = target->type->poll(target);
5095 static int jim_target_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5098 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5100 if (goi.argc != 2) {
5101 Jim_WrongNumArgs(interp, 0, argv,
5102 "([tT]|[fF]|assert|deassert) BOOL");
5107 int e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
5109 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
5112 /* the halt or not param */
5114 e = Jim_GetOpt_Wide(&goi, &a);
5118 struct target *target = Jim_CmdPrivData(goi.interp);
5119 if (!target->tap->enabled)
5120 return jim_target_tap_disabled(interp);
5122 if (!target->type->assert_reset || !target->type->deassert_reset) {
5123 Jim_SetResultFormatted(interp,
5124 "No target-specific reset for %s",
5125 target_name(target));
5129 if (target->defer_examine)
5130 target_reset_examined(target);
5132 /* determine if we should halt or not. */
5133 target->reset_halt = !!a;
5134 /* When this happens - all workareas are invalid. */
5135 target_free_all_working_areas_restore(target, 0);
5138 if (n->value == NVP_ASSERT)
5139 e = target->type->assert_reset(target);
5141 e = target->type->deassert_reset(target);
5142 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
5145 static int jim_target_halt(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5148 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
5151 struct target *target = Jim_CmdPrivData(interp);
5152 if (!target->tap->enabled)
5153 return jim_target_tap_disabled(interp);
5154 int e = target->type->halt(target);
5155 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
5158 static int jim_target_wait_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5161 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5163 /* params: <name> statename timeoutmsecs */
5164 if (goi.argc != 2) {
5165 const char *cmd_name = Jim_GetString(argv[0], NULL);
5166 Jim_SetResultFormatted(goi.interp,
5167 "%s <state_name> <timeout_in_msec>", cmd_name);
5172 int e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
5174 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state, 1);
5178 e = Jim_GetOpt_Wide(&goi, &a);
5181 struct target *target = Jim_CmdPrivData(interp);
5182 if (!target->tap->enabled)
5183 return jim_target_tap_disabled(interp);
5185 e = target_wait_state(target, n->value, a);
5186 if (e != ERROR_OK) {
5187 Jim_Obj *eObj = Jim_NewIntObj(interp, e);
5188 Jim_SetResultFormatted(goi.interp,
5189 "target: %s wait %s fails (%#s) %s",
5190 target_name(target), n->name,
5191 eObj, target_strerror_safe(e));
5192 Jim_FreeNewObj(interp, eObj);
5197 /* List for human, Events defined for this target.
5198 * scripts/programs should use 'name cget -event NAME'
5200 static int jim_target_event_list(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5202 struct command_context *cmd_ctx = current_command_context(interp);
5203 assert(cmd_ctx != NULL);
5205 struct target *target = Jim_CmdPrivData(interp);
5206 struct target_event_action *teap = target->event_action;
5207 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
5208 target->target_number,
5209 target_name(target));
5210 command_print(cmd_ctx, "%-25s | Body", "Event");
5211 command_print(cmd_ctx, "------------------------- | "
5212 "----------------------------------------");
5214 Jim_Nvp *opt = Jim_Nvp_value2name_simple(nvp_target_event, teap->event);
5215 command_print(cmd_ctx, "%-25s | %s",
5216 opt->name, Jim_GetString(teap->body, NULL));
5219 command_print(cmd_ctx, "***END***");
5222 static int jim_target_current_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5225 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
5228 struct target *target = Jim_CmdPrivData(interp);
5229 Jim_SetResultString(interp, target_state_name(target), -1);
5232 static int jim_target_invoke_event(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5235 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5236 if (goi.argc != 1) {
5237 const char *cmd_name = Jim_GetString(argv[0], NULL);
5238 Jim_SetResultFormatted(goi.interp, "%s <eventname>", cmd_name);
5242 int e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
5244 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
5247 struct target *target = Jim_CmdPrivData(interp);
5248 target_handle_event(target, n->value);
5252 static const struct command_registration target_instance_command_handlers[] = {
5254 .name = "configure",
5255 .mode = COMMAND_CONFIG,
5256 .jim_handler = jim_target_configure,
5257 .help = "configure a new target for use",
5258 .usage = "[target_attribute ...]",
5262 .mode = COMMAND_ANY,
5263 .jim_handler = jim_target_configure,
5264 .help = "returns the specified target attribute",
5265 .usage = "target_attribute",
5269 .mode = COMMAND_EXEC,
5270 .jim_handler = jim_target_mw,
5271 .help = "Write 32-bit word(s) to target memory",
5272 .usage = "address data [count]",
5276 .mode = COMMAND_EXEC,
5277 .jim_handler = jim_target_mw,
5278 .help = "Write 16-bit half-word(s) to target memory",
5279 .usage = "address data [count]",
5283 .mode = COMMAND_EXEC,
5284 .jim_handler = jim_target_mw,
5285 .help = "Write byte(s) to target memory",
5286 .usage = "address data [count]",
5290 .mode = COMMAND_EXEC,
5291 .jim_handler = jim_target_md,
5292 .help = "Display target memory as 32-bit words",
5293 .usage = "address [count]",
5297 .mode = COMMAND_EXEC,
5298 .jim_handler = jim_target_md,
5299 .help = "Display target memory as 16-bit half-words",
5300 .usage = "address [count]",
5304 .mode = COMMAND_EXEC,
5305 .jim_handler = jim_target_md,
5306 .help = "Display target memory as 8-bit bytes",
5307 .usage = "address [count]",
5310 .name = "array2mem",
5311 .mode = COMMAND_EXEC,
5312 .jim_handler = jim_target_array2mem,
5313 .help = "Writes Tcl array of 8/16/32 bit numbers "
5315 .usage = "arrayname bitwidth address count",
5318 .name = "mem2array",
5319 .mode = COMMAND_EXEC,
5320 .jim_handler = jim_target_mem2array,
5321 .help = "Loads Tcl array of 8/16/32 bit numbers "
5322 "from target memory",
5323 .usage = "arrayname bitwidth address count",
5326 .name = "eventlist",
5327 .mode = COMMAND_EXEC,
5328 .jim_handler = jim_target_event_list,
5329 .help = "displays a table of events defined for this target",
5333 .mode = COMMAND_EXEC,
5334 .jim_handler = jim_target_current_state,
5335 .help = "displays the current state of this target",
5338 .name = "arp_examine",
5339 .mode = COMMAND_EXEC,
5340 .jim_handler = jim_target_examine,
5341 .help = "used internally for reset processing",
5342 .usage = "arp_examine ['allow-defer']",
5345 .name = "was_examined",
5346 .mode = COMMAND_EXEC,
5347 .jim_handler = jim_target_was_examined,
5348 .help = "used internally for reset processing",
5349 .usage = "was_examined",
5352 .name = "examine_deferred",
5353 .mode = COMMAND_EXEC,
5354 .jim_handler = jim_target_examine_deferred,
5355 .help = "used internally for reset processing",
5356 .usage = "examine_deferred",
5359 .name = "arp_halt_gdb",
5360 .mode = COMMAND_EXEC,
5361 .jim_handler = jim_target_halt_gdb,
5362 .help = "used internally for reset processing to halt GDB",
5366 .mode = COMMAND_EXEC,
5367 .jim_handler = jim_target_poll,
5368 .help = "used internally for reset processing",
5371 .name = "arp_reset",
5372 .mode = COMMAND_EXEC,
5373 .jim_handler = jim_target_reset,
5374 .help = "used internally for reset processing",
5378 .mode = COMMAND_EXEC,
5379 .jim_handler = jim_target_halt,
5380 .help = "used internally for reset processing",
5383 .name = "arp_waitstate",
5384 .mode = COMMAND_EXEC,
5385 .jim_handler = jim_target_wait_state,
5386 .help = "used internally for reset processing",
5389 .name = "invoke-event",
5390 .mode = COMMAND_EXEC,
5391 .jim_handler = jim_target_invoke_event,
5392 .help = "invoke handler for specified event",
5393 .usage = "event_name",
5395 COMMAND_REGISTRATION_DONE
5398 static int target_create(Jim_GetOptInfo *goi)
5405 struct target *target;
5406 struct command_context *cmd_ctx;
5408 cmd_ctx = current_command_context(goi->interp);
5409 assert(cmd_ctx != NULL);
5411 if (goi->argc < 3) {
5412 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
5417 Jim_GetOpt_Obj(goi, &new_cmd);
5418 /* does this command exist? */
5419 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
5421 cp = Jim_GetString(new_cmd, NULL);
5422 Jim_SetResultFormatted(goi->interp, "Command/target: %s Exists", cp);
5427 e = Jim_GetOpt_String(goi, &cp, NULL);
5430 struct transport *tr = get_current_transport();
5431 if (tr->override_target) {
5432 e = tr->override_target(&cp);
5433 if (e != ERROR_OK) {
5434 LOG_ERROR("The selected transport doesn't support this target");
5437 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
5439 /* now does target type exist */
5440 for (x = 0 ; target_types[x] ; x++) {
5441 if (0 == strcmp(cp, target_types[x]->name)) {
5446 /* check for deprecated name */
5447 if (target_types[x]->deprecated_name) {
5448 if (0 == strcmp(cp, target_types[x]->deprecated_name)) {
5450 LOG_WARNING("target name is deprecated use: \'%s\'", target_types[x]->name);
5455 if (target_types[x] == NULL) {
5456 Jim_SetResultFormatted(goi->interp, "Unknown target type %s, try one of ", cp);
5457 for (x = 0 ; target_types[x] ; x++) {
5458 if (target_types[x + 1]) {
5459 Jim_AppendStrings(goi->interp,
5460 Jim_GetResult(goi->interp),
5461 target_types[x]->name,
5464 Jim_AppendStrings(goi->interp,
5465 Jim_GetResult(goi->interp),
5467 target_types[x]->name, NULL);
5474 target = calloc(1, sizeof(struct target));
5475 /* set target number */
5476 target->target_number = new_target_number();
5477 cmd_ctx->current_target = target->target_number;
5479 /* allocate memory for each unique target type */
5480 target->type = calloc(1, sizeof(struct target_type));
5482 memcpy(target->type, target_types[x], sizeof(struct target_type));
5484 /* will be set by "-endian" */
5485 target->endianness = TARGET_ENDIAN_UNKNOWN;
5487 /* default to first core, override with -coreid */
5490 target->working_area = 0x0;
5491 target->working_area_size = 0x0;
5492 target->working_areas = NULL;
5493 target->backup_working_area = 0;
5495 target->state = TARGET_UNKNOWN;
5496 target->debug_reason = DBG_REASON_UNDEFINED;
5497 target->reg_cache = NULL;
5498 target->breakpoints = NULL;
5499 target->watchpoints = NULL;
5500 target->next = NULL;
5501 target->arch_info = NULL;
5503 target->display = 1;
5505 target->halt_issued = false;
5507 /* initialize trace information */
5508 target->trace_info = calloc(1, sizeof(struct trace));
5510 target->dbgmsg = NULL;
5511 target->dbg_msg_enabled = 0;
5513 target->endianness = TARGET_ENDIAN_UNKNOWN;
5515 target->rtos = NULL;
5516 target->rtos_auto_detect = false;
5518 /* Do the rest as "configure" options */
5519 goi->isconfigure = 1;
5520 e = target_configure(goi, target);
5522 if (target->tap == NULL) {
5523 Jim_SetResultString(goi->interp, "-chain-position required when creating target", -1);
5533 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
5534 /* default endian to little if not specified */
5535 target->endianness = TARGET_LITTLE_ENDIAN;
5538 cp = Jim_GetString(new_cmd, NULL);
5539 target->cmd_name = strdup(cp);
5541 /* create the target specific commands */
5542 if (target->type->commands) {
5543 e = register_commands(cmd_ctx, NULL, target->type->commands);
5545 LOG_ERROR("unable to register '%s' commands", cp);
5547 if (target->type->target_create)
5548 (*(target->type->target_create))(target, goi->interp);
5550 /* append to end of list */
5552 struct target **tpp;
5553 tpp = &(all_targets);
5555 tpp = &((*tpp)->next);
5559 /* now - create the new target name command */
5560 const struct command_registration target_subcommands[] = {
5562 .chain = target_instance_command_handlers,
5565 .chain = target->type->commands,
5567 COMMAND_REGISTRATION_DONE
5569 const struct command_registration target_commands[] = {
5572 .mode = COMMAND_ANY,
5573 .help = "target command group",
5575 .chain = target_subcommands,
5577 COMMAND_REGISTRATION_DONE
5579 e = register_commands(cmd_ctx, NULL, target_commands);
5583 struct command *c = command_find_in_context(cmd_ctx, cp);
5585 command_set_handler_data(c, target);
5587 return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
5590 static int jim_target_current(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5593 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5596 struct command_context *cmd_ctx = current_command_context(interp);
5597 assert(cmd_ctx != NULL);
5599 Jim_SetResultString(interp, target_name(get_current_target(cmd_ctx)), -1);
5603 static int jim_target_types(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5606 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5609 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
5610 for (unsigned x = 0; NULL != target_types[x]; x++) {
5611 Jim_ListAppendElement(interp, Jim_GetResult(interp),
5612 Jim_NewStringObj(interp, target_types[x]->name, -1));
5617 static int jim_target_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5620 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5623 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
5624 struct target *target = all_targets;
5626 Jim_ListAppendElement(interp, Jim_GetResult(interp),
5627 Jim_NewStringObj(interp, target_name(target), -1));
5628 target = target->next;
5633 static int jim_target_smp(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5636 const char *targetname;
5638 struct target *target = (struct target *) NULL;
5639 struct target_list *head, *curr, *new;
5640 curr = (struct target_list *) NULL;
5641 head = (struct target_list *) NULL;
5644 LOG_DEBUG("%d", argc);
5645 /* argv[1] = target to associate in smp
5646 * argv[2] = target to assoicate in smp
5650 for (i = 1; i < argc; i++) {
5652 targetname = Jim_GetString(argv[i], &len);
5653 target = get_target(targetname);
5654 LOG_DEBUG("%s ", targetname);
5656 new = malloc(sizeof(struct target_list));
5657 new->target = target;
5658 new->next = (struct target_list *)NULL;
5659 if (head == (struct target_list *)NULL) {
5668 /* now parse the list of cpu and put the target in smp mode*/
5671 while (curr != (struct target_list *)NULL) {
5672 target = curr->target;
5674 target->head = head;
5678 if (target && target->rtos)
5679 retval = rtos_smp_init(head->target);
5685 static int jim_target_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5688 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5690 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
5691 "<name> <target_type> [<target_options> ...]");
5694 return target_create(&goi);
5697 static const struct command_registration target_subcommand_handlers[] = {
5700 .mode = COMMAND_CONFIG,
5701 .handler = handle_target_init_command,
5702 .help = "initialize targets",
5706 /* REVISIT this should be COMMAND_CONFIG ... */
5707 .mode = COMMAND_ANY,
5708 .jim_handler = jim_target_create,
5709 .usage = "name type '-chain-position' name [options ...]",
5710 .help = "Creates and selects a new target",
5714 .mode = COMMAND_ANY,
5715 .jim_handler = jim_target_current,
5716 .help = "Returns the currently selected target",
5720 .mode = COMMAND_ANY,
5721 .jim_handler = jim_target_types,
5722 .help = "Returns the available target types as "
5723 "a list of strings",
5727 .mode = COMMAND_ANY,
5728 .jim_handler = jim_target_names,
5729 .help = "Returns the names of all targets as a list of strings",
5733 .mode = COMMAND_ANY,
5734 .jim_handler = jim_target_smp,
5735 .usage = "targetname1 targetname2 ...",
5736 .help = "gather several target in a smp list"
5739 COMMAND_REGISTRATION_DONE
5743 target_addr_t address;
5749 static int fastload_num;
5750 static struct FastLoad *fastload;
5752 static void free_fastload(void)
5754 if (fastload != NULL) {
5756 for (i = 0; i < fastload_num; i++) {
5757 if (fastload[i].data)
5758 free(fastload[i].data);
5765 COMMAND_HANDLER(handle_fast_load_image_command)
5769 uint32_t image_size;
5770 target_addr_t min_address = 0;
5771 target_addr_t max_address = -1;
5776 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
5777 &image, &min_address, &max_address);
5778 if (ERROR_OK != retval)
5781 struct duration bench;
5782 duration_start(&bench);
5784 retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL);
5785 if (retval != ERROR_OK)
5790 fastload_num = image.num_sections;
5791 fastload = malloc(sizeof(struct FastLoad)*image.num_sections);
5792 if (fastload == NULL) {
5793 command_print(CMD_CTX, "out of memory");
5794 image_close(&image);
5797 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
5798 for (i = 0; i < image.num_sections; i++) {
5799 buffer = malloc(image.sections[i].size);
5800 if (buffer == NULL) {
5801 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
5802 (int)(image.sections[i].size));
5803 retval = ERROR_FAIL;
5807 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
5808 if (retval != ERROR_OK) {
5813 uint32_t offset = 0;
5814 uint32_t length = buf_cnt;
5816 /* DANGER!!! beware of unsigned comparision here!!! */
5818 if ((image.sections[i].base_address + buf_cnt >= min_address) &&
5819 (image.sections[i].base_address < max_address)) {
5820 if (image.sections[i].base_address < min_address) {
5821 /* clip addresses below */
5822 offset += min_address-image.sections[i].base_address;
5826 if (image.sections[i].base_address + buf_cnt > max_address)
5827 length -= (image.sections[i].base_address + buf_cnt)-max_address;
5829 fastload[i].address = image.sections[i].base_address + offset;
5830 fastload[i].data = malloc(length);
5831 if (fastload[i].data == NULL) {
5833 command_print(CMD_CTX, "error allocating buffer for section (%" PRIu32 " bytes)",
5835 retval = ERROR_FAIL;
5838 memcpy(fastload[i].data, buffer + offset, length);
5839 fastload[i].length = length;
5841 image_size += length;
5842 command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
5843 (unsigned int)length,
5844 ((unsigned int)(image.sections[i].base_address + offset)));
5850 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
5851 command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
5852 "in %fs (%0.3f KiB/s)", image_size,
5853 duration_elapsed(&bench), duration_kbps(&bench, image_size));
5855 command_print(CMD_CTX,
5856 "WARNING: image has not been loaded to target!"
5857 "You can issue a 'fast_load' to finish loading.");
5860 image_close(&image);
5862 if (retval != ERROR_OK)
5868 COMMAND_HANDLER(handle_fast_load_command)
5871 return ERROR_COMMAND_SYNTAX_ERROR;
5872 if (fastload == NULL) {
5873 LOG_ERROR("No image in memory");
5877 int64_t ms = timeval_ms();
5879 int retval = ERROR_OK;
5880 for (i = 0; i < fastload_num; i++) {
5881 struct target *target = get_current_target(CMD_CTX);
5882 command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
5883 (unsigned int)(fastload[i].address),
5884 (unsigned int)(fastload[i].length));
5885 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
5886 if (retval != ERROR_OK)
5888 size += fastload[i].length;
5890 if (retval == ERROR_OK) {
5891 int64_t after = timeval_ms();
5892 command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
5897 static const struct command_registration target_command_handlers[] = {
5900 .handler = handle_targets_command,
5901 .mode = COMMAND_ANY,
5902 .help = "change current default target (one parameter) "
5903 "or prints table of all targets (no parameters)",
5904 .usage = "[target]",
5908 .mode = COMMAND_CONFIG,
5909 .help = "configure target",
5911 .chain = target_subcommand_handlers,
5913 COMMAND_REGISTRATION_DONE
5916 int target_register_commands(struct command_context *cmd_ctx)
5918 return register_commands(cmd_ctx, NULL, target_command_handlers);
5921 static bool target_reset_nag = true;
5923 bool get_target_reset_nag(void)
5925 return target_reset_nag;
5928 COMMAND_HANDLER(handle_target_reset_nag)
5930 return CALL_COMMAND_HANDLER(handle_command_parse_bool,
5931 &target_reset_nag, "Nag after each reset about options to improve "
5935 COMMAND_HANDLER(handle_ps_command)
5937 struct target *target = get_current_target(CMD_CTX);
5939 if (target->state != TARGET_HALTED) {
5940 LOG_INFO("target not halted !!");
5944 if ((target->rtos) && (target->rtos->type)
5945 && (target->rtos->type->ps_command)) {
5946 display = target->rtos->type->ps_command(target);
5947 command_print(CMD_CTX, "%s", display);
5952 return ERROR_TARGET_FAILURE;
5956 static void binprint(struct command_context *cmd_ctx, const char *text, const uint8_t *buf, int size)
5959 command_print_sameline(cmd_ctx, "%s", text);
5960 for (int i = 0; i < size; i++)
5961 command_print_sameline(cmd_ctx, " %02x", buf[i]);
5962 command_print(cmd_ctx, " ");
5965 COMMAND_HANDLER(handle_test_mem_access_command)
5967 struct target *target = get_current_target(CMD_CTX);
5969 int retval = ERROR_OK;
5971 if (target->state != TARGET_HALTED) {
5972 LOG_INFO("target not halted !!");
5977 return ERROR_COMMAND_SYNTAX_ERROR;
5979 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], test_size);
5982 size_t num_bytes = test_size + 4;
5984 struct working_area *wa = NULL;
5985 retval = target_alloc_working_area(target, num_bytes, &wa);
5986 if (retval != ERROR_OK) {
5987 LOG_ERROR("Not enough working area");
5991 uint8_t *test_pattern = malloc(num_bytes);
5993 for (size_t i = 0; i < num_bytes; i++)
5994 test_pattern[i] = rand();
5996 retval = target_write_memory(target, wa->address, 1, num_bytes, test_pattern);
5997 if (retval != ERROR_OK) {
5998 LOG_ERROR("Test pattern write failed");
6002 for (int host_offset = 0; host_offset <= 1; host_offset++) {
6003 for (int size = 1; size <= 4; size *= 2) {
6004 for (int offset = 0; offset < 4; offset++) {
6005 uint32_t count = test_size / size;
6006 size_t host_bufsiz = (count + 2) * size + host_offset;
6007 uint8_t *read_ref = malloc(host_bufsiz);
6008 uint8_t *read_buf = malloc(host_bufsiz);
6010 for (size_t i = 0; i < host_bufsiz; i++) {
6011 read_ref[i] = rand();
6012 read_buf[i] = read_ref[i];
6014 command_print_sameline(CMD_CTX,
6015 "Test read %" PRIu32 " x %d @ %d to %saligned buffer: ", count,
6016 size, offset, host_offset ? "un" : "");
6018 struct duration bench;
6019 duration_start(&bench);
6021 retval = target_read_memory(target, wa->address + offset, size, count,
6022 read_buf + size + host_offset);
6024 duration_measure(&bench);
6026 if (retval == ERROR_TARGET_UNALIGNED_ACCESS) {
6027 command_print(CMD_CTX, "Unsupported alignment");
6029 } else if (retval != ERROR_OK) {
6030 command_print(CMD_CTX, "Memory read failed");
6034 /* replay on host */
6035 memcpy(read_ref + size + host_offset, test_pattern + offset, count * size);
6038 int result = memcmp(read_ref, read_buf, host_bufsiz);
6040 command_print(CMD_CTX, "Pass in %fs (%0.3f KiB/s)",
6041 duration_elapsed(&bench),
6042 duration_kbps(&bench, count * size));
6044 command_print(CMD_CTX, "Compare failed");
6045 binprint(CMD_CTX, "ref:", read_ref, host_bufsiz);
6046 binprint(CMD_CTX, "buf:", read_buf, host_bufsiz);
6059 target_free_working_area(target, wa);
6062 num_bytes = test_size + 4 + 4 + 4;
6064 retval = target_alloc_working_area(target, num_bytes, &wa);
6065 if (retval != ERROR_OK) {
6066 LOG_ERROR("Not enough working area");
6070 test_pattern = malloc(num_bytes);
6072 for (size_t i = 0; i < num_bytes; i++)
6073 test_pattern[i] = rand();
6075 for (int host_offset = 0; host_offset <= 1; host_offset++) {
6076 for (int size = 1; size <= 4; size *= 2) {
6077 for (int offset = 0; offset < 4; offset++) {
6078 uint32_t count = test_size / size;
6079 size_t host_bufsiz = count * size + host_offset;
6080 uint8_t *read_ref = malloc(num_bytes);
6081 uint8_t *read_buf = malloc(num_bytes);
6082 uint8_t *write_buf = malloc(host_bufsiz);
6084 for (size_t i = 0; i < host_bufsiz; i++)
6085 write_buf[i] = rand();
6086 command_print_sameline(CMD_CTX,
6087 "Test write %" PRIu32 " x %d @ %d from %saligned buffer: ", count,
6088 size, offset, host_offset ? "un" : "");
6090 retval = target_write_memory(target, wa->address, 1, num_bytes, test_pattern);
6091 if (retval != ERROR_OK) {
6092 command_print(CMD_CTX, "Test pattern write failed");
6096 /* replay on host */
6097 memcpy(read_ref, test_pattern, num_bytes);
6098 memcpy(read_ref + size + offset, write_buf + host_offset, count * size);
6100 struct duration bench;
6101 duration_start(&bench);
6103 retval = target_write_memory(target, wa->address + size + offset, size, count,
6104 write_buf + host_offset);
6106 duration_measure(&bench);
6108 if (retval == ERROR_TARGET_UNALIGNED_ACCESS) {
6109 command_print(CMD_CTX, "Unsupported alignment");
6111 } else if (retval != ERROR_OK) {
6112 command_print(CMD_CTX, "Memory write failed");
6117 retval = target_read_memory(target, wa->address, 1, num_bytes, read_buf);
6118 if (retval != ERROR_OK) {
6119 command_print(CMD_CTX, "Test pattern write failed");
6124 int result = memcmp(read_ref, read_buf, num_bytes);
6126 command_print(CMD_CTX, "Pass in %fs (%0.3f KiB/s)",
6127 duration_elapsed(&bench),
6128 duration_kbps(&bench, count * size));
6130 command_print(CMD_CTX, "Compare failed");
6131 binprint(CMD_CTX, "ref:", read_ref, num_bytes);
6132 binprint(CMD_CTX, "buf:", read_buf, num_bytes);
6144 target_free_working_area(target, wa);
6148 static const struct command_registration target_exec_command_handlers[] = {
6150 .name = "fast_load_image",
6151 .handler = handle_fast_load_image_command,
6152 .mode = COMMAND_ANY,
6153 .help = "Load image into server memory for later use by "
6154 "fast_load; primarily for profiling",
6155 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
6156 "[min_address [max_length]]",
6159 .name = "fast_load",
6160 .handler = handle_fast_load_command,
6161 .mode = COMMAND_EXEC,
6162 .help = "loads active fast load image to current target "
6163 "- mainly for profiling purposes",
6168 .handler = handle_profile_command,
6169 .mode = COMMAND_EXEC,
6170 .usage = "seconds filename [start end]",
6171 .help = "profiling samples the CPU PC",
6173 /** @todo don't register virt2phys() unless target supports it */
6175 .name = "virt2phys",
6176 .handler = handle_virt2phys_command,
6177 .mode = COMMAND_ANY,
6178 .help = "translate a virtual address into a physical address",
6179 .usage = "virtual_address",
6183 .handler = handle_reg_command,
6184 .mode = COMMAND_EXEC,
6185 .help = "display (reread from target with \"force\") or set a register; "
6186 "with no arguments, displays all registers and their values",
6187 .usage = "[(register_number|register_name) [(value|'force')]]",
6191 .handler = handle_poll_command,
6192 .mode = COMMAND_EXEC,
6193 .help = "poll target state; or reconfigure background polling",
6194 .usage = "['on'|'off']",
6197 .name = "wait_halt",
6198 .handler = handle_wait_halt_command,
6199 .mode = COMMAND_EXEC,
6200 .help = "wait up to the specified number of milliseconds "
6201 "(default 5000) for a previously requested halt",
6202 .usage = "[milliseconds]",
6206 .handler = handle_halt_command,
6207 .mode = COMMAND_EXEC,
6208 .help = "request target to halt, then wait up to the specified"
6209 "number of milliseconds (default 5000) for it to complete",
6210 .usage = "[milliseconds]",
6214 .handler = handle_resume_command,
6215 .mode = COMMAND_EXEC,
6216 .help = "resume target execution from current PC or address",
6217 .usage = "[address]",
6221 .handler = handle_reset_command,
6222 .mode = COMMAND_EXEC,
6223 .usage = "[run|halt|init]",
6224 .help = "Reset all targets into the specified mode."
6225 "Default reset mode is run, if not given.",
6228 .name = "soft_reset_halt",
6229 .handler = handle_soft_reset_halt_command,
6230 .mode = COMMAND_EXEC,
6232 .help = "halt the target and do a soft reset",
6236 .handler = handle_step_command,
6237 .mode = COMMAND_EXEC,
6238 .help = "step one instruction from current PC or address",
6239 .usage = "[address]",
6243 .handler = handle_md_command,
6244 .mode = COMMAND_EXEC,
6245 .help = "display memory words",
6246 .usage = "['phys'] address [count]",
6250 .handler = handle_md_command,
6251 .mode = COMMAND_EXEC,
6252 .help = "display memory words",
6253 .usage = "['phys'] address [count]",
6257 .handler = handle_md_command,
6258 .mode = COMMAND_EXEC,
6259 .help = "display memory half-words",
6260 .usage = "['phys'] address [count]",
6264 .handler = handle_md_command,
6265 .mode = COMMAND_EXEC,
6266 .help = "display memory bytes",
6267 .usage = "['phys'] address [count]",
6271 .handler = handle_mw_command,
6272 .mode = COMMAND_EXEC,
6273 .help = "write memory word",
6274 .usage = "['phys'] address value [count]",
6278 .handler = handle_mw_command,
6279 .mode = COMMAND_EXEC,
6280 .help = "write memory word",
6281 .usage = "['phys'] address value [count]",
6285 .handler = handle_mw_command,
6286 .mode = COMMAND_EXEC,
6287 .help = "write memory half-word",
6288 .usage = "['phys'] address value [count]",
6292 .handler = handle_mw_command,
6293 .mode = COMMAND_EXEC,
6294 .help = "write memory byte",
6295 .usage = "['phys'] address value [count]",
6299 .handler = handle_bp_command,
6300 .mode = COMMAND_EXEC,
6301 .help = "list or set hardware or software breakpoint",
6302 .usage = "<address> [<asid>]<length> ['hw'|'hw_ctx']",
6306 .handler = handle_rbp_command,
6307 .mode = COMMAND_EXEC,
6308 .help = "remove breakpoint",
6313 .handler = handle_wp_command,
6314 .mode = COMMAND_EXEC,
6315 .help = "list (no params) or create watchpoints",
6316 .usage = "[address length [('r'|'w'|'a') value [mask]]]",
6320 .handler = handle_rwp_command,
6321 .mode = COMMAND_EXEC,
6322 .help = "remove watchpoint",
6326 .name = "load_image",
6327 .handler = handle_load_image_command,
6328 .mode = COMMAND_EXEC,
6329 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
6330 "[min_address] [max_length]",
6333 .name = "dump_image",
6334 .handler = handle_dump_image_command,
6335 .mode = COMMAND_EXEC,
6336 .usage = "filename address size",
6339 .name = "verify_image_checksum",
6340 .handler = handle_verify_image_checksum_command,
6341 .mode = COMMAND_EXEC,
6342 .usage = "filename [offset [type]]",
6345 .name = "verify_image",
6346 .handler = handle_verify_image_command,
6347 .mode = COMMAND_EXEC,
6348 .usage = "filename [offset [type]]",
6351 .name = "test_image",
6352 .handler = handle_test_image_command,
6353 .mode = COMMAND_EXEC,
6354 .usage = "filename [offset [type]]",
6357 .name = "mem2array",
6358 .mode = COMMAND_EXEC,
6359 .jim_handler = jim_mem2array,
6360 .help = "read 8/16/32 bit memory and return as a TCL array "
6361 "for script processing",
6362 .usage = "arrayname bitwidth address count",
6365 .name = "array2mem",
6366 .mode = COMMAND_EXEC,
6367 .jim_handler = jim_array2mem,
6368 .help = "convert a TCL array to memory locations "
6369 "and write the 8/16/32 bit values",
6370 .usage = "arrayname bitwidth address count",
6373 .name = "reset_nag",
6374 .handler = handle_target_reset_nag,
6375 .mode = COMMAND_ANY,
6376 .help = "Nag after each reset about options that could have been "
6377 "enabled to improve performance. ",
6378 .usage = "['enable'|'disable']",
6382 .handler = handle_ps_command,
6383 .mode = COMMAND_EXEC,
6384 .help = "list all tasks ",
6388 .name = "test_mem_access",
6389 .handler = handle_test_mem_access_command,
6390 .mode = COMMAND_EXEC,
6391 .help = "Test the target's memory access functions",
6395 COMMAND_REGISTRATION_DONE
6397 static int target_register_user_commands(struct command_context *cmd_ctx)
6399 int retval = ERROR_OK;
6400 retval = target_request_register_commands(cmd_ctx);
6401 if (retval != ERROR_OK)
6404 retval = trace_register_commands(cmd_ctx);
6405 if (retval != ERROR_OK)
6409 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);