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"
59 /* default halt wait timeout (ms) */
60 #define DEFAULT_HALT_TIMEOUT 5000
62 static int target_read_buffer_default(struct target *target, target_addr_t address,
63 uint32_t count, uint8_t *buffer);
64 static int target_write_buffer_default(struct target *target, target_addr_t address,
65 uint32_t count, const uint8_t *buffer);
66 static int target_array2mem(Jim_Interp *interp, struct target *target,
67 int argc, Jim_Obj * const *argv);
68 static int target_mem2array(Jim_Interp *interp, struct target *target,
69 int argc, Jim_Obj * const *argv);
70 static int target_register_user_commands(struct command_context *cmd_ctx);
71 static int target_get_gdb_fileio_info_default(struct target *target,
72 struct gdb_fileio_info *fileio_info);
73 static int target_gdb_fileio_end_default(struct target *target, int retcode,
74 int fileio_errno, bool ctrl_c);
77 extern struct target_type arm7tdmi_target;
78 extern struct target_type arm720t_target;
79 extern struct target_type arm9tdmi_target;
80 extern struct target_type arm920t_target;
81 extern struct target_type arm966e_target;
82 extern struct target_type arm946e_target;
83 extern struct target_type arm926ejs_target;
84 extern struct target_type fa526_target;
85 extern struct target_type feroceon_target;
86 extern struct target_type dragonite_target;
87 extern struct target_type xscale_target;
88 extern struct target_type cortexm_target;
89 extern struct target_type cortexa_target;
90 extern struct target_type aarch64_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 mips_mips64_target;
96 extern struct target_type avr_target;
97 extern struct target_type dsp563xx_target;
98 extern struct target_type dsp5680xx_target;
99 extern struct target_type testee_target;
100 extern struct target_type avr32_ap7k_target;
101 extern struct target_type hla_target;
102 extern struct target_type nds32_v2_target;
103 extern struct target_type nds32_v3_target;
104 extern struct target_type nds32_v3m_target;
105 extern struct target_type or1k_target;
106 extern struct target_type quark_x10xx_target;
107 extern struct target_type quark_d20xx_target;
108 extern struct target_type stm8_target;
109 extern struct target_type riscv_target;
110 extern struct target_type mem_ap_target;
111 extern struct target_type esirisc_target;
112 extern struct target_type arcv2_target;
114 static struct target_type *target_types[] = {
154 struct target *all_targets;
155 static struct target_event_callback *target_event_callbacks;
156 static struct target_timer_callback *target_timer_callbacks;
157 static LIST_HEAD(target_reset_callback_list);
158 static LIST_HEAD(target_trace_callback_list);
159 static const int polling_interval = 100;
161 static const Jim_Nvp nvp_assert[] = {
162 { .name = "assert", NVP_ASSERT },
163 { .name = "deassert", NVP_DEASSERT },
164 { .name = "T", NVP_ASSERT },
165 { .name = "F", NVP_DEASSERT },
166 { .name = "t", NVP_ASSERT },
167 { .name = "f", NVP_DEASSERT },
168 { .name = NULL, .value = -1 }
171 static const Jim_Nvp nvp_error_target[] = {
172 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
173 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
174 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
175 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
176 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
177 { .value = ERROR_TARGET_UNALIGNED_ACCESS, .name = "err-unaligned-access" },
178 { .value = ERROR_TARGET_DATA_ABORT, .name = "err-data-abort" },
179 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE, .name = "err-resource-not-available" },
180 { .value = ERROR_TARGET_TRANSLATION_FAULT, .name = "err-translation-fault" },
181 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
182 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
183 { .value = -1, .name = NULL }
186 static const char *target_strerror_safe(int err)
190 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
197 static const Jim_Nvp nvp_target_event[] = {
199 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
200 { .value = TARGET_EVENT_HALTED, .name = "halted" },
201 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
202 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
203 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
204 { .value = TARGET_EVENT_STEP_START, .name = "step-start" },
205 { .value = TARGET_EVENT_STEP_END, .name = "step-end" },
207 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
208 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
210 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
211 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
212 { .value = TARGET_EVENT_RESET_ASSERT, .name = "reset-assert" },
213 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
214 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
215 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
216 { .value = TARGET_EVENT_RESET_INIT, .name = "reset-init" },
217 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
219 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
220 { .value = TARGET_EVENT_EXAMINE_FAIL, .name = "examine-fail" },
221 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
223 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
224 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
226 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
227 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
229 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
230 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END, .name = "gdb-flash-write-end" },
232 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
233 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END, .name = "gdb-flash-erase-end" },
235 { .value = TARGET_EVENT_TRACE_CONFIG, .name = "trace-config" },
237 { .name = NULL, .value = -1 }
240 static const Jim_Nvp nvp_target_state[] = {
241 { .name = "unknown", .value = TARGET_UNKNOWN },
242 { .name = "running", .value = TARGET_RUNNING },
243 { .name = "halted", .value = TARGET_HALTED },
244 { .name = "reset", .value = TARGET_RESET },
245 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
246 { .name = NULL, .value = -1 },
249 static const Jim_Nvp nvp_target_debug_reason[] = {
250 { .name = "debug-request", .value = DBG_REASON_DBGRQ },
251 { .name = "breakpoint", .value = DBG_REASON_BREAKPOINT },
252 { .name = "watchpoint", .value = DBG_REASON_WATCHPOINT },
253 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
254 { .name = "single-step", .value = DBG_REASON_SINGLESTEP },
255 { .name = "target-not-halted", .value = DBG_REASON_NOTHALTED },
256 { .name = "program-exit", .value = DBG_REASON_EXIT },
257 { .name = "exception-catch", .value = DBG_REASON_EXC_CATCH },
258 { .name = "undefined", .value = DBG_REASON_UNDEFINED },
259 { .name = NULL, .value = -1 },
262 static const Jim_Nvp nvp_target_endian[] = {
263 { .name = "big", .value = TARGET_BIG_ENDIAN },
264 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
265 { .name = "be", .value = TARGET_BIG_ENDIAN },
266 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
267 { .name = NULL, .value = -1 },
270 static const Jim_Nvp nvp_reset_modes[] = {
271 { .name = "unknown", .value = RESET_UNKNOWN },
272 { .name = "run", .value = RESET_RUN },
273 { .name = "halt", .value = RESET_HALT },
274 { .name = "init", .value = RESET_INIT },
275 { .name = NULL, .value = -1 },
278 const char *debug_reason_name(struct target *t)
282 cp = Jim_Nvp_value2name_simple(nvp_target_debug_reason,
283 t->debug_reason)->name;
285 LOG_ERROR("Invalid debug reason: %d", (int)(t->debug_reason));
286 cp = "(*BUG*unknown*BUG*)";
291 const char *target_state_name(struct target *t)
294 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
296 LOG_ERROR("Invalid target state: %d", (int)(t->state));
297 cp = "(*BUG*unknown*BUG*)";
300 if (!target_was_examined(t) && t->defer_examine)
301 cp = "examine deferred";
306 const char *target_event_name(enum target_event event)
309 cp = Jim_Nvp_value2name_simple(nvp_target_event, event)->name;
311 LOG_ERROR("Invalid target event: %d", (int)(event));
312 cp = "(*BUG*unknown*BUG*)";
317 const char *target_reset_mode_name(enum target_reset_mode reset_mode)
320 cp = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode)->name;
322 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode));
323 cp = "(*BUG*unknown*BUG*)";
328 /* determine the number of the new target */
329 static int new_target_number(void)
334 /* number is 0 based */
338 if (x < t->target_number)
339 x = t->target_number;
345 static void append_to_list_all_targets(struct target *target)
347 struct target **t = &all_targets;
354 /* read a uint64_t from a buffer in target memory endianness */
355 uint64_t target_buffer_get_u64(struct target *target, const uint8_t *buffer)
357 if (target->endianness == TARGET_LITTLE_ENDIAN)
358 return le_to_h_u64(buffer);
360 return be_to_h_u64(buffer);
363 /* read a uint32_t from a buffer in target memory endianness */
364 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
366 if (target->endianness == TARGET_LITTLE_ENDIAN)
367 return le_to_h_u32(buffer);
369 return be_to_h_u32(buffer);
372 /* read a uint24_t from a buffer in target memory endianness */
373 uint32_t target_buffer_get_u24(struct target *target, const uint8_t *buffer)
375 if (target->endianness == TARGET_LITTLE_ENDIAN)
376 return le_to_h_u24(buffer);
378 return be_to_h_u24(buffer);
381 /* read a uint16_t from a buffer in target memory endianness */
382 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
384 if (target->endianness == TARGET_LITTLE_ENDIAN)
385 return le_to_h_u16(buffer);
387 return be_to_h_u16(buffer);
390 /* write a uint64_t to a buffer in target memory endianness */
391 void target_buffer_set_u64(struct target *target, uint8_t *buffer, uint64_t value)
393 if (target->endianness == TARGET_LITTLE_ENDIAN)
394 h_u64_to_le(buffer, value);
396 h_u64_to_be(buffer, value);
399 /* write a uint32_t to a buffer in target memory endianness */
400 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
402 if (target->endianness == TARGET_LITTLE_ENDIAN)
403 h_u32_to_le(buffer, value);
405 h_u32_to_be(buffer, value);
408 /* write a uint24_t to a buffer in target memory endianness */
409 void target_buffer_set_u24(struct target *target, uint8_t *buffer, uint32_t value)
411 if (target->endianness == TARGET_LITTLE_ENDIAN)
412 h_u24_to_le(buffer, value);
414 h_u24_to_be(buffer, value);
417 /* write a uint16_t to a buffer in target memory endianness */
418 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
420 if (target->endianness == TARGET_LITTLE_ENDIAN)
421 h_u16_to_le(buffer, value);
423 h_u16_to_be(buffer, value);
426 /* write a uint8_t to a buffer in target memory endianness */
427 static void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
432 /* write a uint64_t array to a buffer in target memory endianness */
433 void target_buffer_get_u64_array(struct target *target, const uint8_t *buffer, uint32_t count, uint64_t *dstbuf)
436 for (i = 0; i < count; i++)
437 dstbuf[i] = target_buffer_get_u64(target, &buffer[i * 8]);
440 /* write a uint32_t array to a buffer in target memory endianness */
441 void target_buffer_get_u32_array(struct target *target, const uint8_t *buffer, uint32_t count, uint32_t *dstbuf)
444 for (i = 0; i < count; i++)
445 dstbuf[i] = target_buffer_get_u32(target, &buffer[i * 4]);
448 /* write a uint16_t array to a buffer in target memory endianness */
449 void target_buffer_get_u16_array(struct target *target, const uint8_t *buffer, uint32_t count, uint16_t *dstbuf)
452 for (i = 0; i < count; i++)
453 dstbuf[i] = target_buffer_get_u16(target, &buffer[i * 2]);
456 /* write a uint64_t array to a buffer in target memory endianness */
457 void target_buffer_set_u64_array(struct target *target, uint8_t *buffer, uint32_t count, const uint64_t *srcbuf)
460 for (i = 0; i < count; i++)
461 target_buffer_set_u64(target, &buffer[i * 8], srcbuf[i]);
464 /* write a uint32_t array to a buffer in target memory endianness */
465 void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, const uint32_t *srcbuf)
468 for (i = 0; i < count; i++)
469 target_buffer_set_u32(target, &buffer[i * 4], srcbuf[i]);
472 /* write a uint16_t array to a buffer in target memory endianness */
473 void target_buffer_set_u16_array(struct target *target, uint8_t *buffer, uint32_t count, const uint16_t *srcbuf)
476 for (i = 0; i < count; i++)
477 target_buffer_set_u16(target, &buffer[i * 2], srcbuf[i]);
480 /* return a pointer to a configured target; id is name or number */
481 struct target *get_target(const char *id)
483 struct target *target;
485 /* try as tcltarget name */
486 for (target = all_targets; target; target = target->next) {
487 if (target_name(target) == NULL)
489 if (strcmp(id, target_name(target)) == 0)
493 /* It's OK to remove this fallback sometime after August 2010 or so */
495 /* no match, try as number */
497 if (parse_uint(id, &num) != ERROR_OK)
500 for (target = all_targets; target; target = target->next) {
501 if (target->target_number == (int)num) {
502 LOG_WARNING("use '%s' as target identifier, not '%u'",
503 target_name(target), num);
511 /* returns a pointer to the n-th configured target */
512 struct target *get_target_by_num(int num)
514 struct target *target = all_targets;
517 if (target->target_number == num)
519 target = target->next;
525 struct target *get_current_target(struct command_context *cmd_ctx)
527 struct target *target = get_current_target_or_null(cmd_ctx);
529 if (target == NULL) {
530 LOG_ERROR("BUG: current_target out of bounds");
537 struct target *get_current_target_or_null(struct command_context *cmd_ctx)
539 return cmd_ctx->current_target_override
540 ? cmd_ctx->current_target_override
541 : cmd_ctx->current_target;
544 int target_poll(struct target *target)
548 /* We can't poll until after examine */
549 if (!target_was_examined(target)) {
550 /* Fail silently lest we pollute the log */
554 retval = target->type->poll(target);
555 if (retval != ERROR_OK)
558 if (target->halt_issued) {
559 if (target->state == TARGET_HALTED)
560 target->halt_issued = false;
562 int64_t t = timeval_ms() - target->halt_issued_time;
563 if (t > DEFAULT_HALT_TIMEOUT) {
564 target->halt_issued = false;
565 LOG_INFO("Halt timed out, wake up GDB.");
566 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
574 int target_halt(struct target *target)
577 /* We can't poll until after examine */
578 if (!target_was_examined(target)) {
579 LOG_ERROR("Target not examined yet");
583 retval = target->type->halt(target);
584 if (retval != ERROR_OK)
587 target->halt_issued = true;
588 target->halt_issued_time = timeval_ms();
594 * Make the target (re)start executing using its saved execution
595 * context (possibly with some modifications).
597 * @param target Which target should start executing.
598 * @param current True to use the target's saved program counter instead
599 * of the address parameter
600 * @param address Optionally used as the program counter.
601 * @param handle_breakpoints True iff breakpoints at the resumption PC
602 * should be skipped. (For example, maybe execution was stopped by
603 * such a breakpoint, in which case it would be counterproductive to
605 * @param debug_execution False if all working areas allocated by OpenOCD
606 * should be released and/or restored to their original contents.
607 * (This would for example be true to run some downloaded "helper"
608 * algorithm code, which resides in one such working buffer and uses
609 * another for data storage.)
611 * @todo Resolve the ambiguity about what the "debug_execution" flag
612 * signifies. For example, Target implementations don't agree on how
613 * it relates to invalidation of the register cache, or to whether
614 * breakpoints and watchpoints should be enabled. (It would seem wrong
615 * to enable breakpoints when running downloaded "helper" algorithms
616 * (debug_execution true), since the breakpoints would be set to match
617 * target firmware being debugged, not the helper algorithm.... and
618 * enabling them could cause such helpers to malfunction (for example,
619 * by overwriting data with a breakpoint instruction. On the other
620 * hand the infrastructure for running such helpers might use this
621 * procedure but rely on hardware breakpoint to detect termination.)
623 int target_resume(struct target *target, int current, target_addr_t address,
624 int handle_breakpoints, int debug_execution)
628 /* We can't poll until after examine */
629 if (!target_was_examined(target)) {
630 LOG_ERROR("Target not examined yet");
634 target_call_event_callbacks(target, TARGET_EVENT_RESUME_START);
636 /* note that resume *must* be asynchronous. The CPU can halt before
637 * we poll. The CPU can even halt at the current PC as a result of
638 * a software breakpoint being inserted by (a bug?) the application.
640 retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution);
641 if (retval != ERROR_OK)
644 target_call_event_callbacks(target, TARGET_EVENT_RESUME_END);
649 static int target_process_reset(struct command_invocation *cmd, enum target_reset_mode reset_mode)
654 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
655 if (n->name == NULL) {
656 LOG_ERROR("invalid reset mode");
660 struct target *target;
661 for (target = all_targets; target; target = target->next)
662 target_call_reset_callbacks(target, reset_mode);
664 /* disable polling during reset to make reset event scripts
665 * more predictable, i.e. dr/irscan & pathmove in events will
666 * not have JTAG operations injected into the middle of a sequence.
668 bool save_poll = jtag_poll_get_enabled();
670 jtag_poll_set_enabled(false);
672 sprintf(buf, "ocd_process_reset %s", n->name);
673 retval = Jim_Eval(cmd->ctx->interp, buf);
675 jtag_poll_set_enabled(save_poll);
677 if (retval != JIM_OK) {
678 Jim_MakeErrorMessage(cmd->ctx->interp);
679 command_print(cmd, "%s", Jim_GetString(Jim_GetResult(cmd->ctx->interp), NULL));
683 /* We want any events to be processed before the prompt */
684 retval = target_call_timer_callbacks_now();
686 for (target = all_targets; target; target = target->next) {
687 target->type->check_reset(target);
688 target->running_alg = false;
694 static int identity_virt2phys(struct target *target,
695 target_addr_t virtual, target_addr_t *physical)
701 static int no_mmu(struct target *target, int *enabled)
707 static int default_examine(struct target *target)
709 target_set_examined(target);
713 /* no check by default */
714 static int default_check_reset(struct target *target)
719 /* Equivalent Tcl code arp_examine_one is in src/target/startup.tcl
721 int target_examine_one(struct target *target)
723 target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_START);
725 int retval = target->type->examine(target);
726 if (retval != ERROR_OK) {
727 target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_FAIL);
731 target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_END);
736 static int jtag_enable_callback(enum jtag_event event, void *priv)
738 struct target *target = priv;
740 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
743 jtag_unregister_event_callback(jtag_enable_callback, target);
745 return target_examine_one(target);
748 /* Targets that correctly implement init + examine, i.e.
749 * no communication with target during init:
753 int target_examine(void)
755 int retval = ERROR_OK;
756 struct target *target;
758 for (target = all_targets; target; target = target->next) {
759 /* defer examination, but don't skip it */
760 if (!target->tap->enabled) {
761 jtag_register_event_callback(jtag_enable_callback,
766 if (target->defer_examine)
769 int retval2 = target_examine_one(target);
770 if (retval2 != ERROR_OK) {
771 LOG_WARNING("target %s examination failed", target_name(target));
778 const char *target_type_name(struct target *target)
780 return target->type->name;
783 static int target_soft_reset_halt(struct target *target)
785 if (!target_was_examined(target)) {
786 LOG_ERROR("Target not examined yet");
789 if (!target->type->soft_reset_halt) {
790 LOG_ERROR("Target %s does not support soft_reset_halt",
791 target_name(target));
794 return target->type->soft_reset_halt(target);
798 * Downloads a target-specific native code algorithm to the target,
799 * and executes it. * Note that some targets may need to set up, enable,
800 * and tear down a breakpoint (hard or * soft) to detect algorithm
801 * termination, while others may support lower overhead schemes where
802 * soft breakpoints embedded in the algorithm automatically terminate the
805 * @param target used to run the algorithm
806 * @param arch_info target-specific description of the algorithm.
808 int target_run_algorithm(struct target *target,
809 int num_mem_params, struct mem_param *mem_params,
810 int num_reg_params, struct reg_param *reg_param,
811 uint32_t entry_point, uint32_t exit_point,
812 int timeout_ms, void *arch_info)
814 int retval = ERROR_FAIL;
816 if (!target_was_examined(target)) {
817 LOG_ERROR("Target not examined yet");
820 if (!target->type->run_algorithm) {
821 LOG_ERROR("Target type '%s' does not support %s",
822 target_type_name(target), __func__);
826 target->running_alg = true;
827 retval = target->type->run_algorithm(target,
828 num_mem_params, mem_params,
829 num_reg_params, reg_param,
830 entry_point, exit_point, timeout_ms, arch_info);
831 target->running_alg = false;
838 * Executes a target-specific native code algorithm and leaves it running.
840 * @param target used to run the algorithm
841 * @param arch_info target-specific description of the algorithm.
843 int target_start_algorithm(struct target *target,
844 int num_mem_params, struct mem_param *mem_params,
845 int num_reg_params, struct reg_param *reg_params,
846 uint32_t entry_point, uint32_t exit_point,
849 int retval = ERROR_FAIL;
851 if (!target_was_examined(target)) {
852 LOG_ERROR("Target not examined yet");
855 if (!target->type->start_algorithm) {
856 LOG_ERROR("Target type '%s' does not support %s",
857 target_type_name(target), __func__);
860 if (target->running_alg) {
861 LOG_ERROR("Target is already running an algorithm");
865 target->running_alg = true;
866 retval = target->type->start_algorithm(target,
867 num_mem_params, mem_params,
868 num_reg_params, reg_params,
869 entry_point, exit_point, arch_info);
876 * Waits for an algorithm started with target_start_algorithm() to complete.
878 * @param target used to run the algorithm
879 * @param arch_info target-specific description of the algorithm.
881 int target_wait_algorithm(struct target *target,
882 int num_mem_params, struct mem_param *mem_params,
883 int num_reg_params, struct reg_param *reg_params,
884 uint32_t exit_point, int timeout_ms,
887 int retval = ERROR_FAIL;
889 if (!target->type->wait_algorithm) {
890 LOG_ERROR("Target type '%s' does not support %s",
891 target_type_name(target), __func__);
894 if (!target->running_alg) {
895 LOG_ERROR("Target is not running an algorithm");
899 retval = target->type->wait_algorithm(target,
900 num_mem_params, mem_params,
901 num_reg_params, reg_params,
902 exit_point, timeout_ms, arch_info);
903 if (retval != ERROR_TARGET_TIMEOUT)
904 target->running_alg = false;
911 * Streams data to a circular buffer on target intended for consumption by code
912 * running asynchronously on target.
914 * This is intended for applications where target-specific native code runs
915 * on the target, receives data from the circular buffer, does something with
916 * it (most likely writing it to a flash memory), and advances the circular
919 * This assumes that the helper algorithm has already been loaded to the target,
920 * but has not been started yet. Given memory and register parameters are passed
923 * The buffer is defined by (buffer_start, buffer_size) arguments and has the
926 * [buffer_start + 0, buffer_start + 4):
927 * Write Pointer address (aka head). Written and updated by this
928 * routine when new data is written to the circular buffer.
929 * [buffer_start + 4, buffer_start + 8):
930 * Read Pointer address (aka tail). Updated by code running on the
931 * target after it consumes data.
932 * [buffer_start + 8, buffer_start + buffer_size):
933 * Circular buffer contents.
935 * See contrib/loaders/flash/stm32f1x.S for an example.
937 * @param target used to run the algorithm
938 * @param buffer address on the host where data to be sent is located
939 * @param count number of blocks to send
940 * @param block_size size in bytes of each block
941 * @param num_mem_params count of memory-based params to pass to algorithm
942 * @param mem_params memory-based params to pass to algorithm
943 * @param num_reg_params count of register-based params to pass to algorithm
944 * @param reg_params memory-based params to pass to algorithm
945 * @param buffer_start address on the target of the circular buffer structure
946 * @param buffer_size size of the circular buffer structure
947 * @param entry_point address on the target to execute to start the algorithm
948 * @param exit_point address at which to set a breakpoint to catch the
949 * end of the algorithm; can be 0 if target triggers a breakpoint itself
952 int target_run_flash_async_algorithm(struct target *target,
953 const uint8_t *buffer, uint32_t count, int block_size,
954 int num_mem_params, struct mem_param *mem_params,
955 int num_reg_params, struct reg_param *reg_params,
956 uint32_t buffer_start, uint32_t buffer_size,
957 uint32_t entry_point, uint32_t exit_point, void *arch_info)
962 const uint8_t *buffer_orig = buffer;
964 /* Set up working area. First word is write pointer, second word is read pointer,
965 * rest is fifo data area. */
966 uint32_t wp_addr = buffer_start;
967 uint32_t rp_addr = buffer_start + 4;
968 uint32_t fifo_start_addr = buffer_start + 8;
969 uint32_t fifo_end_addr = buffer_start + buffer_size;
971 uint32_t wp = fifo_start_addr;
972 uint32_t rp = fifo_start_addr;
974 /* validate block_size is 2^n */
975 assert(!block_size || !(block_size & (block_size - 1)));
977 retval = target_write_u32(target, wp_addr, wp);
978 if (retval != ERROR_OK)
980 retval = target_write_u32(target, rp_addr, rp);
981 if (retval != ERROR_OK)
984 /* Start up algorithm on target and let it idle while writing the first chunk */
985 retval = target_start_algorithm(target, num_mem_params, mem_params,
986 num_reg_params, reg_params,
991 if (retval != ERROR_OK) {
992 LOG_ERROR("error starting target flash write algorithm");
998 retval = target_read_u32(target, rp_addr, &rp);
999 if (retval != ERROR_OK) {
1000 LOG_ERROR("failed to get read pointer");
1004 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32 " wp 0x%" PRIx32 " rp 0x%" PRIx32,
1005 (size_t) (buffer - buffer_orig), count, wp, rp);
1008 LOG_ERROR("flash write algorithm aborted by target");
1009 retval = ERROR_FLASH_OPERATION_FAILED;
1013 if (((rp - fifo_start_addr) & (block_size - 1)) || rp < fifo_start_addr || rp >= fifo_end_addr) {
1014 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32, rp);
1018 /* Count the number of bytes available in the fifo without
1019 * crossing the wrap around. Make sure to not fill it completely,
1020 * because that would make wp == rp and that's the empty condition. */
1021 uint32_t thisrun_bytes;
1023 thisrun_bytes = rp - wp - block_size;
1024 else if (rp > fifo_start_addr)
1025 thisrun_bytes = fifo_end_addr - wp;
1027 thisrun_bytes = fifo_end_addr - wp - block_size;
1029 if (thisrun_bytes == 0) {
1030 /* Throttle polling a bit if transfer is (much) faster than flash
1031 * programming. The exact delay shouldn't matter as long as it's
1032 * less than buffer size / flash speed. This is very unlikely to
1033 * run when using high latency connections such as USB. */
1036 /* to stop an infinite loop on some targets check and increment a timeout
1037 * this issue was observed on a stellaris using the new ICDI interface */
1038 if (timeout++ >= 500) {
1039 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1040 return ERROR_FLASH_OPERATION_FAILED;
1045 /* reset our timeout */
1048 /* Limit to the amount of data we actually want to write */
1049 if (thisrun_bytes > count * block_size)
1050 thisrun_bytes = count * block_size;
1052 /* Write data to fifo */
1053 retval = target_write_buffer(target, wp, thisrun_bytes, buffer);
1054 if (retval != ERROR_OK)
1057 /* Update counters and wrap write pointer */
1058 buffer += thisrun_bytes;
1059 count -= thisrun_bytes / block_size;
1060 wp += thisrun_bytes;
1061 if (wp >= fifo_end_addr)
1062 wp = fifo_start_addr;
1064 /* Store updated write pointer to target */
1065 retval = target_write_u32(target, wp_addr, wp);
1066 if (retval != ERROR_OK)
1069 /* Avoid GDB timeouts */
1073 if (retval != ERROR_OK) {
1074 /* abort flash write algorithm on target */
1075 target_write_u32(target, wp_addr, 0);
1078 int retval2 = target_wait_algorithm(target, num_mem_params, mem_params,
1079 num_reg_params, reg_params,
1084 if (retval2 != ERROR_OK) {
1085 LOG_ERROR("error waiting for target flash write algorithm");
1089 if (retval == ERROR_OK) {
1090 /* check if algorithm set rp = 0 after fifo writer loop finished */
1091 retval = target_read_u32(target, rp_addr, &rp);
1092 if (retval == ERROR_OK && rp == 0) {
1093 LOG_ERROR("flash write algorithm aborted by target");
1094 retval = ERROR_FLASH_OPERATION_FAILED;
1101 int target_read_memory(struct target *target,
1102 target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
1104 if (!target_was_examined(target)) {
1105 LOG_ERROR("Target not examined yet");
1108 if (!target->type->read_memory) {
1109 LOG_ERROR("Target %s doesn't support read_memory", target_name(target));
1112 return target->type->read_memory(target, address, size, count, buffer);
1115 int target_read_phys_memory(struct target *target,
1116 target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
1118 if (!target_was_examined(target)) {
1119 LOG_ERROR("Target not examined yet");
1122 if (!target->type->read_phys_memory) {
1123 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target));
1126 return target->type->read_phys_memory(target, address, size, count, buffer);
1129 int target_write_memory(struct target *target,
1130 target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
1132 if (!target_was_examined(target)) {
1133 LOG_ERROR("Target not examined yet");
1136 if (!target->type->write_memory) {
1137 LOG_ERROR("Target %s doesn't support write_memory", target_name(target));
1140 return target->type->write_memory(target, address, size, count, buffer);
1143 int target_write_phys_memory(struct target *target,
1144 target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
1146 if (!target_was_examined(target)) {
1147 LOG_ERROR("Target not examined yet");
1150 if (!target->type->write_phys_memory) {
1151 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target));
1154 return target->type->write_phys_memory(target, address, size, count, buffer);
1157 int target_add_breakpoint(struct target *target,
1158 struct breakpoint *breakpoint)
1160 if ((target->state != TARGET_HALTED) && (breakpoint->type != BKPT_HARD)) {
1161 LOG_WARNING("target %s is not halted (add breakpoint)", target_name(target));
1162 return ERROR_TARGET_NOT_HALTED;
1164 return target->type->add_breakpoint(target, breakpoint);
1167 int target_add_context_breakpoint(struct target *target,
1168 struct breakpoint *breakpoint)
1170 if (target->state != TARGET_HALTED) {
1171 LOG_WARNING("target %s is not halted (add context breakpoint)", target_name(target));
1172 return ERROR_TARGET_NOT_HALTED;
1174 return target->type->add_context_breakpoint(target, breakpoint);
1177 int target_add_hybrid_breakpoint(struct target *target,
1178 struct breakpoint *breakpoint)
1180 if (target->state != TARGET_HALTED) {
1181 LOG_WARNING("target %s is not halted (add hybrid breakpoint)", target_name(target));
1182 return ERROR_TARGET_NOT_HALTED;
1184 return target->type->add_hybrid_breakpoint(target, breakpoint);
1187 int target_remove_breakpoint(struct target *target,
1188 struct breakpoint *breakpoint)
1190 return target->type->remove_breakpoint(target, breakpoint);
1193 int target_add_watchpoint(struct target *target,
1194 struct watchpoint *watchpoint)
1196 if (target->state != TARGET_HALTED) {
1197 LOG_WARNING("target %s is not halted (add watchpoint)", target_name(target));
1198 return ERROR_TARGET_NOT_HALTED;
1200 return target->type->add_watchpoint(target, watchpoint);
1202 int target_remove_watchpoint(struct target *target,
1203 struct watchpoint *watchpoint)
1205 return target->type->remove_watchpoint(target, watchpoint);
1207 int target_hit_watchpoint(struct target *target,
1208 struct watchpoint **hit_watchpoint)
1210 if (target->state != TARGET_HALTED) {
1211 LOG_WARNING("target %s is not halted (hit watchpoint)", target->cmd_name);
1212 return ERROR_TARGET_NOT_HALTED;
1215 if (target->type->hit_watchpoint == NULL) {
1216 /* For backward compatible, if hit_watchpoint is not implemented,
1217 * return ERROR_FAIL such that gdb_server will not take the nonsense
1222 return target->type->hit_watchpoint(target, hit_watchpoint);
1225 const char *target_get_gdb_arch(struct target *target)
1227 if (target->type->get_gdb_arch == NULL)
1229 return target->type->get_gdb_arch(target);
1232 int target_get_gdb_reg_list(struct target *target,
1233 struct reg **reg_list[], int *reg_list_size,
1234 enum target_register_class reg_class)
1236 int result = ERROR_FAIL;
1238 if (!target_was_examined(target)) {
1239 LOG_ERROR("Target not examined yet");
1243 result = target->type->get_gdb_reg_list(target, reg_list,
1244 reg_list_size, reg_class);
1247 if (result != ERROR_OK) {
1254 int target_get_gdb_reg_list_noread(struct target *target,
1255 struct reg **reg_list[], int *reg_list_size,
1256 enum target_register_class reg_class)
1258 if (target->type->get_gdb_reg_list_noread &&
1259 target->type->get_gdb_reg_list_noread(target, reg_list,
1260 reg_list_size, reg_class) == ERROR_OK)
1262 return target_get_gdb_reg_list(target, reg_list, reg_list_size, reg_class);
1265 bool target_supports_gdb_connection(struct target *target)
1268 * exclude all the targets that don't provide get_gdb_reg_list
1269 * or that have explicit gdb_max_connection == 0
1271 return !!target->type->get_gdb_reg_list && !!target->gdb_max_connections;
1274 int target_step(struct target *target,
1275 int current, target_addr_t address, int handle_breakpoints)
1279 target_call_event_callbacks(target, TARGET_EVENT_STEP_START);
1281 retval = target->type->step(target, current, address, handle_breakpoints);
1282 if (retval != ERROR_OK)
1285 target_call_event_callbacks(target, TARGET_EVENT_STEP_END);
1290 int target_get_gdb_fileio_info(struct target *target, struct gdb_fileio_info *fileio_info)
1292 if (target->state != TARGET_HALTED) {
1293 LOG_WARNING("target %s is not halted (gdb fileio)", target->cmd_name);
1294 return ERROR_TARGET_NOT_HALTED;
1296 return target->type->get_gdb_fileio_info(target, fileio_info);
1299 int target_gdb_fileio_end(struct target *target, int retcode, int fileio_errno, bool ctrl_c)
1301 if (target->state != TARGET_HALTED) {
1302 LOG_WARNING("target %s is not halted (gdb fileio end)", target->cmd_name);
1303 return ERROR_TARGET_NOT_HALTED;
1305 return target->type->gdb_fileio_end(target, retcode, fileio_errno, ctrl_c);
1308 target_addr_t target_address_max(struct target *target)
1310 unsigned bits = target_address_bits(target);
1311 if (sizeof(target_addr_t) * 8 == bits)
1312 return (target_addr_t) -1;
1314 return (((target_addr_t) 1) << bits) - 1;
1317 unsigned target_address_bits(struct target *target)
1319 if (target->type->address_bits)
1320 return target->type->address_bits(target);
1324 static int target_profiling(struct target *target, uint32_t *samples,
1325 uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
1327 return target->type->profiling(target, samples, max_num_samples,
1328 num_samples, seconds);
1332 * Reset the @c examined flag for the given target.
1333 * Pure paranoia -- targets are zeroed on allocation.
1335 static void target_reset_examined(struct target *target)
1337 target->examined = false;
1340 static int handle_target(void *priv);
1342 static int target_init_one(struct command_context *cmd_ctx,
1343 struct target *target)
1345 target_reset_examined(target);
1347 struct target_type *type = target->type;
1348 if (type->examine == NULL)
1349 type->examine = default_examine;
1351 if (type->check_reset == NULL)
1352 type->check_reset = default_check_reset;
1354 assert(type->init_target != NULL);
1356 int retval = type->init_target(cmd_ctx, target);
1357 if (ERROR_OK != retval) {
1358 LOG_ERROR("target '%s' init failed", target_name(target));
1362 /* Sanity-check MMU support ... stub in what we must, to help
1363 * implement it in stages, but warn if we need to do so.
1366 if (type->virt2phys == NULL) {
1367 LOG_ERROR("type '%s' is missing virt2phys", type->name);
1368 type->virt2phys = identity_virt2phys;
1371 /* Make sure no-MMU targets all behave the same: make no
1372 * distinction between physical and virtual addresses, and
1373 * ensure that virt2phys() is always an identity mapping.
1375 if (type->write_phys_memory || type->read_phys_memory || type->virt2phys)
1376 LOG_WARNING("type '%s' has bad MMU hooks", type->name);
1379 type->write_phys_memory = type->write_memory;
1380 type->read_phys_memory = type->read_memory;
1381 type->virt2phys = identity_virt2phys;
1384 if (target->type->read_buffer == NULL)
1385 target->type->read_buffer = target_read_buffer_default;
1387 if (target->type->write_buffer == NULL)
1388 target->type->write_buffer = target_write_buffer_default;
1390 if (target->type->get_gdb_fileio_info == NULL)
1391 target->type->get_gdb_fileio_info = target_get_gdb_fileio_info_default;
1393 if (target->type->gdb_fileio_end == NULL)
1394 target->type->gdb_fileio_end = target_gdb_fileio_end_default;
1396 if (target->type->profiling == NULL)
1397 target->type->profiling = target_profiling_default;
1402 static int target_init(struct command_context *cmd_ctx)
1404 struct target *target;
1407 for (target = all_targets; target; target = target->next) {
1408 retval = target_init_one(cmd_ctx, target);
1409 if (ERROR_OK != retval)
1416 retval = target_register_user_commands(cmd_ctx);
1417 if (ERROR_OK != retval)
1420 retval = target_register_timer_callback(&handle_target,
1421 polling_interval, TARGET_TIMER_TYPE_PERIODIC, cmd_ctx->interp);
1422 if (ERROR_OK != retval)
1428 COMMAND_HANDLER(handle_target_init_command)
1433 return ERROR_COMMAND_SYNTAX_ERROR;
1435 static bool target_initialized;
1436 if (target_initialized) {
1437 LOG_INFO("'target init' has already been called");
1440 target_initialized = true;
1442 retval = command_run_line(CMD_CTX, "init_targets");
1443 if (ERROR_OK != retval)
1446 retval = command_run_line(CMD_CTX, "init_target_events");
1447 if (ERROR_OK != retval)
1450 retval = command_run_line(CMD_CTX, "init_board");
1451 if (ERROR_OK != retval)
1454 LOG_DEBUG("Initializing targets...");
1455 return target_init(CMD_CTX);
1458 int target_register_event_callback(int (*callback)(struct target *target,
1459 enum target_event event, void *priv), void *priv)
1461 struct target_event_callback **callbacks_p = &target_event_callbacks;
1463 if (callback == NULL)
1464 return ERROR_COMMAND_SYNTAX_ERROR;
1467 while ((*callbacks_p)->next)
1468 callbacks_p = &((*callbacks_p)->next);
1469 callbacks_p = &((*callbacks_p)->next);
1472 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
1473 (*callbacks_p)->callback = callback;
1474 (*callbacks_p)->priv = priv;
1475 (*callbacks_p)->next = NULL;
1480 int target_register_reset_callback(int (*callback)(struct target *target,
1481 enum target_reset_mode reset_mode, void *priv), void *priv)
1483 struct target_reset_callback *entry;
1485 if (callback == NULL)
1486 return ERROR_COMMAND_SYNTAX_ERROR;
1488 entry = malloc(sizeof(struct target_reset_callback));
1489 if (entry == NULL) {
1490 LOG_ERROR("error allocating buffer for reset callback entry");
1491 return ERROR_COMMAND_SYNTAX_ERROR;
1494 entry->callback = callback;
1496 list_add(&entry->list, &target_reset_callback_list);
1502 int target_register_trace_callback(int (*callback)(struct target *target,
1503 size_t len, uint8_t *data, void *priv), void *priv)
1505 struct target_trace_callback *entry;
1507 if (callback == NULL)
1508 return ERROR_COMMAND_SYNTAX_ERROR;
1510 entry = malloc(sizeof(struct target_trace_callback));
1511 if (entry == NULL) {
1512 LOG_ERROR("error allocating buffer for trace callback entry");
1513 return ERROR_COMMAND_SYNTAX_ERROR;
1516 entry->callback = callback;
1518 list_add(&entry->list, &target_trace_callback_list);
1524 int target_register_timer_callback(int (*callback)(void *priv),
1525 unsigned int time_ms, enum target_timer_type type, void *priv)
1527 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
1529 if (callback == NULL)
1530 return ERROR_COMMAND_SYNTAX_ERROR;
1533 while ((*callbacks_p)->next)
1534 callbacks_p = &((*callbacks_p)->next);
1535 callbacks_p = &((*callbacks_p)->next);
1538 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
1539 (*callbacks_p)->callback = callback;
1540 (*callbacks_p)->type = type;
1541 (*callbacks_p)->time_ms = time_ms;
1542 (*callbacks_p)->removed = false;
1544 gettimeofday(&(*callbacks_p)->when, NULL);
1545 timeval_add_time(&(*callbacks_p)->when, 0, time_ms * 1000);
1547 (*callbacks_p)->priv = priv;
1548 (*callbacks_p)->next = NULL;
1553 int target_unregister_event_callback(int (*callback)(struct target *target,
1554 enum target_event event, void *priv), void *priv)
1556 struct target_event_callback **p = &target_event_callbacks;
1557 struct target_event_callback *c = target_event_callbacks;
1559 if (callback == NULL)
1560 return ERROR_COMMAND_SYNTAX_ERROR;
1563 struct target_event_callback *next = c->next;
1564 if ((c->callback == callback) && (c->priv == priv)) {
1576 int target_unregister_reset_callback(int (*callback)(struct target *target,
1577 enum target_reset_mode reset_mode, void *priv), void *priv)
1579 struct target_reset_callback *entry;
1581 if (callback == NULL)
1582 return ERROR_COMMAND_SYNTAX_ERROR;
1584 list_for_each_entry(entry, &target_reset_callback_list, list) {
1585 if (entry->callback == callback && entry->priv == priv) {
1586 list_del(&entry->list);
1595 int target_unregister_trace_callback(int (*callback)(struct target *target,
1596 size_t len, uint8_t *data, void *priv), void *priv)
1598 struct target_trace_callback *entry;
1600 if (callback == NULL)
1601 return ERROR_COMMAND_SYNTAX_ERROR;
1603 list_for_each_entry(entry, &target_trace_callback_list, list) {
1604 if (entry->callback == callback && entry->priv == priv) {
1605 list_del(&entry->list);
1614 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
1616 if (callback == NULL)
1617 return ERROR_COMMAND_SYNTAX_ERROR;
1619 for (struct target_timer_callback *c = target_timer_callbacks;
1621 if ((c->callback == callback) && (c->priv == priv)) {
1630 int target_call_event_callbacks(struct target *target, enum target_event event)
1632 struct target_event_callback *callback = target_event_callbacks;
1633 struct target_event_callback *next_callback;
1635 if (event == TARGET_EVENT_HALTED) {
1636 /* execute early halted first */
1637 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1640 LOG_DEBUG("target event %i (%s) for core %s", event,
1641 Jim_Nvp_value2name_simple(nvp_target_event, event)->name,
1642 target_name(target));
1644 target_handle_event(target, event);
1647 next_callback = callback->next;
1648 callback->callback(target, event, callback->priv);
1649 callback = next_callback;
1655 int target_call_reset_callbacks(struct target *target, enum target_reset_mode reset_mode)
1657 struct target_reset_callback *callback;
1659 LOG_DEBUG("target reset %i (%s)", reset_mode,
1660 Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode)->name);
1662 list_for_each_entry(callback, &target_reset_callback_list, list)
1663 callback->callback(target, reset_mode, callback->priv);
1668 int target_call_trace_callbacks(struct target *target, size_t len, uint8_t *data)
1670 struct target_trace_callback *callback;
1672 list_for_each_entry(callback, &target_trace_callback_list, list)
1673 callback->callback(target, len, data, callback->priv);
1678 static int target_timer_callback_periodic_restart(
1679 struct target_timer_callback *cb, struct timeval *now)
1682 timeval_add_time(&cb->when, 0, cb->time_ms * 1000L);
1686 static int target_call_timer_callback(struct target_timer_callback *cb,
1687 struct timeval *now)
1689 cb->callback(cb->priv);
1691 if (cb->type == TARGET_TIMER_TYPE_PERIODIC)
1692 return target_timer_callback_periodic_restart(cb, now);
1694 return target_unregister_timer_callback(cb->callback, cb->priv);
1697 static int target_call_timer_callbacks_check_time(int checktime)
1699 static bool callback_processing;
1701 /* Do not allow nesting */
1702 if (callback_processing)
1705 callback_processing = true;
1710 gettimeofday(&now, NULL);
1712 /* Store an address of the place containing a pointer to the
1713 * next item; initially, that's a standalone "root of the
1714 * list" variable. */
1715 struct target_timer_callback **callback = &target_timer_callbacks;
1716 while (callback && *callback) {
1717 if ((*callback)->removed) {
1718 struct target_timer_callback *p = *callback;
1719 *callback = (*callback)->next;
1724 bool call_it = (*callback)->callback &&
1725 ((!checktime && (*callback)->type == TARGET_TIMER_TYPE_PERIODIC) ||
1726 timeval_compare(&now, &(*callback)->when) >= 0);
1729 target_call_timer_callback(*callback, &now);
1731 callback = &(*callback)->next;
1734 callback_processing = false;
1738 int target_call_timer_callbacks(void)
1740 return target_call_timer_callbacks_check_time(1);
1743 /* invoke periodic callbacks immediately */
1744 int target_call_timer_callbacks_now(void)
1746 return target_call_timer_callbacks_check_time(0);
1749 /* Prints the working area layout for debug purposes */
1750 static void print_wa_layout(struct target *target)
1752 struct working_area *c = target->working_areas;
1755 LOG_DEBUG("%c%c " TARGET_ADDR_FMT "-" TARGET_ADDR_FMT " (%" PRIu32 " bytes)",
1756 c->backup ? 'b' : ' ', c->free ? ' ' : '*',
1757 c->address, c->address + c->size - 1, c->size);
1762 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1763 static void target_split_working_area(struct working_area *area, uint32_t size)
1765 assert(area->free); /* Shouldn't split an allocated area */
1766 assert(size <= area->size); /* Caller should guarantee this */
1768 /* Split only if not already the right size */
1769 if (size < area->size) {
1770 struct working_area *new_wa = malloc(sizeof(*new_wa));
1775 new_wa->next = area->next;
1776 new_wa->size = area->size - size;
1777 new_wa->address = area->address + size;
1778 new_wa->backup = NULL;
1779 new_wa->user = NULL;
1780 new_wa->free = true;
1782 area->next = new_wa;
1785 /* If backup memory was allocated to this area, it has the wrong size
1786 * now so free it and it will be reallocated if/when needed */
1788 area->backup = NULL;
1792 /* Merge all adjacent free areas into one */
1793 static void target_merge_working_areas(struct target *target)
1795 struct working_area *c = target->working_areas;
1797 while (c && c->next) {
1798 assert(c->next->address == c->address + c->size); /* This is an invariant */
1800 /* Find two adjacent free areas */
1801 if (c->free && c->next->free) {
1802 /* Merge the last into the first */
1803 c->size += c->next->size;
1805 /* Remove the last */
1806 struct working_area *to_be_freed = c->next;
1807 c->next = c->next->next;
1808 free(to_be_freed->backup);
1811 /* If backup memory was allocated to the remaining area, it's has
1812 * the wrong size now */
1821 int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
1823 /* Reevaluate working area address based on MMU state*/
1824 if (target->working_areas == NULL) {
1828 retval = target->type->mmu(target, &enabled);
1829 if (retval != ERROR_OK)
1833 if (target->working_area_phys_spec) {
1834 LOG_DEBUG("MMU disabled, using physical "
1835 "address for working memory " TARGET_ADDR_FMT,
1836 target->working_area_phys);
1837 target->working_area = target->working_area_phys;
1839 LOG_ERROR("No working memory available. "
1840 "Specify -work-area-phys to target.");
1841 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1844 if (target->working_area_virt_spec) {
1845 LOG_DEBUG("MMU enabled, using virtual "
1846 "address for working memory " TARGET_ADDR_FMT,
1847 target->working_area_virt);
1848 target->working_area = target->working_area_virt;
1850 LOG_ERROR("No working memory available. "
1851 "Specify -work-area-virt to target.");
1852 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1856 /* Set up initial working area on first call */
1857 struct working_area *new_wa = malloc(sizeof(*new_wa));
1859 new_wa->next = NULL;
1860 new_wa->size = target->working_area_size & ~3UL; /* 4-byte align */
1861 new_wa->address = target->working_area;
1862 new_wa->backup = NULL;
1863 new_wa->user = NULL;
1864 new_wa->free = true;
1867 target->working_areas = new_wa;
1870 /* only allocate multiples of 4 byte */
1872 size = (size + 3) & (~3UL);
1874 struct working_area *c = target->working_areas;
1876 /* Find the first large enough working area */
1878 if (c->free && c->size >= size)
1884 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1886 /* Split the working area into the requested size */
1887 target_split_working_area(c, size);
1889 LOG_DEBUG("allocated new working area of %" PRIu32 " bytes at address " TARGET_ADDR_FMT,
1892 if (target->backup_working_area) {
1893 if (c->backup == NULL) {
1894 c->backup = malloc(c->size);
1895 if (c->backup == NULL)
1899 int retval = target_read_memory(target, c->address, 4, c->size / 4, c->backup);
1900 if (retval != ERROR_OK)
1904 /* mark as used, and return the new (reused) area */
1911 print_wa_layout(target);
1916 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1920 retval = target_alloc_working_area_try(target, size, area);
1921 if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
1922 LOG_WARNING("not enough working area available(requested %"PRIu32")", size);
1927 static int target_restore_working_area(struct target *target, struct working_area *area)
1929 int retval = ERROR_OK;
1931 if (target->backup_working_area && area->backup != NULL) {
1932 retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup);
1933 if (retval != ERROR_OK)
1934 LOG_ERROR("failed to restore %" PRIu32 " bytes of working area at address " TARGET_ADDR_FMT,
1935 area->size, area->address);
1941 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1942 static int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1944 int retval = ERROR_OK;
1950 retval = target_restore_working_area(target, area);
1951 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1952 if (retval != ERROR_OK)
1958 LOG_DEBUG("freed %" PRIu32 " bytes of working area at address " TARGET_ADDR_FMT,
1959 area->size, area->address);
1961 /* mark user pointer invalid */
1962 /* TODO: Is this really safe? It points to some previous caller's memory.
1963 * How could we know that the area pointer is still in that place and not
1964 * some other vital data? What's the purpose of this, anyway? */
1968 target_merge_working_areas(target);
1970 print_wa_layout(target);
1975 int target_free_working_area(struct target *target, struct working_area *area)
1977 return target_free_working_area_restore(target, area, 1);
1980 /* free resources and restore memory, if restoring memory fails,
1981 * free up resources anyway
1983 static void target_free_all_working_areas_restore(struct target *target, int restore)
1985 struct working_area *c = target->working_areas;
1987 LOG_DEBUG("freeing all working areas");
1989 /* Loop through all areas, restoring the allocated ones and marking them as free */
1993 target_restore_working_area(target, c);
1995 *c->user = NULL; /* Same as above */
2001 /* Run a merge pass to combine all areas into one */
2002 target_merge_working_areas(target);
2004 print_wa_layout(target);
2007 void target_free_all_working_areas(struct target *target)
2009 target_free_all_working_areas_restore(target, 1);
2011 /* Now we have none or only one working area marked as free */
2012 if (target->working_areas) {
2013 /* Free the last one to allow on-the-fly moving and resizing */
2014 free(target->working_areas->backup);
2015 free(target->working_areas);
2016 target->working_areas = NULL;
2020 /* Find the largest number of bytes that can be allocated */
2021 uint32_t target_get_working_area_avail(struct target *target)
2023 struct working_area *c = target->working_areas;
2024 uint32_t max_size = 0;
2027 return target->working_area_size;
2030 if (c->free && max_size < c->size)
2039 static void target_destroy(struct target *target)
2041 if (target->type->deinit_target)
2042 target->type->deinit_target(target);
2044 free(target->semihosting);
2046 jtag_unregister_event_callback(jtag_enable_callback, target);
2048 struct target_event_action *teap = target->event_action;
2050 struct target_event_action *next = teap->next;
2051 Jim_DecrRefCount(teap->interp, teap->body);
2056 target_free_all_working_areas(target);
2058 /* release the targets SMP list */
2060 struct target_list *head = target->head;
2061 while (head != NULL) {
2062 struct target_list *pos = head->next;
2063 head->target->smp = 0;
2070 rtos_destroy(target);
2072 free(target->gdb_port_override);
2074 free(target->trace_info);
2075 free(target->fileio_info);
2076 free(target->cmd_name);
2080 void target_quit(void)
2082 struct target_event_callback *pe = target_event_callbacks;
2084 struct target_event_callback *t = pe->next;
2088 target_event_callbacks = NULL;
2090 struct target_timer_callback *pt = target_timer_callbacks;
2092 struct target_timer_callback *t = pt->next;
2096 target_timer_callbacks = NULL;
2098 for (struct target *target = all_targets; target;) {
2102 target_destroy(target);
2109 int target_arch_state(struct target *target)
2112 if (target == NULL) {
2113 LOG_WARNING("No target has been configured");
2117 if (target->state != TARGET_HALTED)
2120 retval = target->type->arch_state(target);
2124 static int target_get_gdb_fileio_info_default(struct target *target,
2125 struct gdb_fileio_info *fileio_info)
2127 /* If target does not support semi-hosting function, target
2128 has no need to provide .get_gdb_fileio_info callback.
2129 It just return ERROR_FAIL and gdb_server will return "Txx"
2130 as target halted every time. */
2134 static int target_gdb_fileio_end_default(struct target *target,
2135 int retcode, int fileio_errno, bool ctrl_c)
2140 int target_profiling_default(struct target *target, uint32_t *samples,
2141 uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
2143 struct timeval timeout, now;
2145 gettimeofday(&timeout, NULL);
2146 timeval_add_time(&timeout, seconds, 0);
2148 LOG_INFO("Starting profiling. Halting and resuming the"
2149 " target as often as we can...");
2151 uint32_t sample_count = 0;
2152 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2153 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
2155 int retval = ERROR_OK;
2157 target_poll(target);
2158 if (target->state == TARGET_HALTED) {
2159 uint32_t t = buf_get_u32(reg->value, 0, 32);
2160 samples[sample_count++] = t;
2161 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2162 retval = target_resume(target, 1, 0, 0, 0);
2163 target_poll(target);
2164 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2165 } else if (target->state == TARGET_RUNNING) {
2166 /* We want to quickly sample the PC. */
2167 retval = target_halt(target);
2169 LOG_INFO("Target not halted or running");
2174 if (retval != ERROR_OK)
2177 gettimeofday(&now, NULL);
2178 if ((sample_count >= max_num_samples) || timeval_compare(&now, &timeout) >= 0) {
2179 LOG_INFO("Profiling completed. %" PRIu32 " samples.", sample_count);
2184 *num_samples = sample_count;
2188 /* Single aligned words are guaranteed to use 16 or 32 bit access
2189 * mode respectively, otherwise data is handled as quickly as
2192 int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
2194 LOG_DEBUG("writing buffer of %" PRIu32 " byte at " TARGET_ADDR_FMT,
2197 if (!target_was_examined(target)) {
2198 LOG_ERROR("Target not examined yet");
2205 if ((address + size - 1) < address) {
2206 /* GDB can request this when e.g. PC is 0xfffffffc */
2207 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT ", 0x%08" PRIx32 ")",
2213 return target->type->write_buffer(target, address, size, buffer);
2216 static int target_write_buffer_default(struct target *target,
2217 target_addr_t address, uint32_t count, const uint8_t *buffer)
2221 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2222 * will have something to do with the size we leave to it. */
2223 for (size = 1; size < 4 && count >= size * 2 + (address & size); size *= 2) {
2224 if (address & size) {
2225 int retval = target_write_memory(target, address, size, 1, buffer);
2226 if (retval != ERROR_OK)
2234 /* Write the data with as large access size as possible. */
2235 for (; size > 0; size /= 2) {
2236 uint32_t aligned = count - count % size;
2238 int retval = target_write_memory(target, address, size, aligned / size, buffer);
2239 if (retval != ERROR_OK)
2250 /* Single aligned words are guaranteed to use 16 or 32 bit access
2251 * mode respectively, otherwise data is handled as quickly as
2254 int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
2256 LOG_DEBUG("reading buffer of %" PRIu32 " byte at " TARGET_ADDR_FMT,
2259 if (!target_was_examined(target)) {
2260 LOG_ERROR("Target not examined yet");
2267 if ((address + size - 1) < address) {
2268 /* GDB can request this when e.g. PC is 0xfffffffc */
2269 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT ", 0x%08" PRIx32 ")",
2275 return target->type->read_buffer(target, address, size, buffer);
2278 static int target_read_buffer_default(struct target *target, target_addr_t address, uint32_t count, uint8_t *buffer)
2282 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2283 * will have something to do with the size we leave to it. */
2284 for (size = 1; size < 4 && count >= size * 2 + (address & size); size *= 2) {
2285 if (address & size) {
2286 int retval = target_read_memory(target, address, size, 1, buffer);
2287 if (retval != ERROR_OK)
2295 /* Read the data with as large access size as possible. */
2296 for (; size > 0; size /= 2) {
2297 uint32_t aligned = count - count % size;
2299 int retval = target_read_memory(target, address, size, aligned / size, buffer);
2300 if (retval != ERROR_OK)
2311 int target_checksum_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t *crc)
2316 uint32_t checksum = 0;
2317 if (!target_was_examined(target)) {
2318 LOG_ERROR("Target not examined yet");
2322 retval = target->type->checksum_memory(target, address, size, &checksum);
2323 if (retval != ERROR_OK) {
2324 buffer = malloc(size);
2325 if (buffer == NULL) {
2326 LOG_ERROR("error allocating buffer for section (%" PRIu32 " bytes)", size);
2327 return ERROR_COMMAND_SYNTAX_ERROR;
2329 retval = target_read_buffer(target, address, size, buffer);
2330 if (retval != ERROR_OK) {
2335 /* convert to target endianness */
2336 for (i = 0; i < (size/sizeof(uint32_t)); i++) {
2337 uint32_t target_data;
2338 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
2339 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
2342 retval = image_calculate_checksum(buffer, size, &checksum);
2351 int target_blank_check_memory(struct target *target,
2352 struct target_memory_check_block *blocks, int num_blocks,
2353 uint8_t erased_value)
2355 if (!target_was_examined(target)) {
2356 LOG_ERROR("Target not examined yet");
2360 if (target->type->blank_check_memory == NULL)
2361 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
2363 return target->type->blank_check_memory(target, blocks, num_blocks, erased_value);
2366 int target_read_u64(struct target *target, target_addr_t address, uint64_t *value)
2368 uint8_t value_buf[8];
2369 if (!target_was_examined(target)) {
2370 LOG_ERROR("Target not examined yet");
2374 int retval = target_read_memory(target, address, 8, 1, value_buf);
2376 if (retval == ERROR_OK) {
2377 *value = target_buffer_get_u64(target, value_buf);
2378 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64 "",
2383 LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2390 int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
2392 uint8_t value_buf[4];
2393 if (!target_was_examined(target)) {
2394 LOG_ERROR("Target not examined yet");
2398 int retval = target_read_memory(target, address, 4, 1, value_buf);
2400 if (retval == ERROR_OK) {
2401 *value = target_buffer_get_u32(target, value_buf);
2402 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32 "",
2407 LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2414 int target_read_u16(struct target *target, target_addr_t address, uint16_t *value)
2416 uint8_t value_buf[2];
2417 if (!target_was_examined(target)) {
2418 LOG_ERROR("Target not examined yet");
2422 int retval = target_read_memory(target, address, 2, 1, value_buf);
2424 if (retval == ERROR_OK) {
2425 *value = target_buffer_get_u16(target, value_buf);
2426 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%4.4" PRIx16,
2431 LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2438 int target_read_u8(struct target *target, target_addr_t address, uint8_t *value)
2440 if (!target_was_examined(target)) {
2441 LOG_ERROR("Target not examined yet");
2445 int retval = target_read_memory(target, address, 1, 1, value);
2447 if (retval == ERROR_OK) {
2448 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
2453 LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2460 int target_write_u64(struct target *target, target_addr_t address, uint64_t value)
2463 uint8_t value_buf[8];
2464 if (!target_was_examined(target)) {
2465 LOG_ERROR("Target not examined yet");
2469 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64 "",
2473 target_buffer_set_u64(target, value_buf, value);
2474 retval = target_write_memory(target, address, 8, 1, value_buf);
2475 if (retval != ERROR_OK)
2476 LOG_DEBUG("failed: %i", retval);
2481 int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
2484 uint8_t value_buf[4];
2485 if (!target_was_examined(target)) {
2486 LOG_ERROR("Target not examined yet");
2490 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32 "",
2494 target_buffer_set_u32(target, value_buf, value);
2495 retval = target_write_memory(target, address, 4, 1, value_buf);
2496 if (retval != ERROR_OK)
2497 LOG_DEBUG("failed: %i", retval);
2502 int target_write_u16(struct target *target, target_addr_t address, uint16_t value)
2505 uint8_t value_buf[2];
2506 if (!target_was_examined(target)) {
2507 LOG_ERROR("Target not examined yet");
2511 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx16,
2515 target_buffer_set_u16(target, value_buf, value);
2516 retval = target_write_memory(target, address, 2, 1, value_buf);
2517 if (retval != ERROR_OK)
2518 LOG_DEBUG("failed: %i", retval);
2523 int target_write_u8(struct target *target, target_addr_t address, uint8_t value)
2526 if (!target_was_examined(target)) {
2527 LOG_ERROR("Target not examined yet");
2531 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
2534 retval = target_write_memory(target, address, 1, 1, &value);
2535 if (retval != ERROR_OK)
2536 LOG_DEBUG("failed: %i", retval);
2541 int target_write_phys_u64(struct target *target, target_addr_t address, uint64_t value)
2544 uint8_t value_buf[8];
2545 if (!target_was_examined(target)) {
2546 LOG_ERROR("Target not examined yet");
2550 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64 "",
2554 target_buffer_set_u64(target, value_buf, value);
2555 retval = target_write_phys_memory(target, address, 8, 1, value_buf);
2556 if (retval != ERROR_OK)
2557 LOG_DEBUG("failed: %i", retval);
2562 int target_write_phys_u32(struct target *target, target_addr_t address, uint32_t value)
2565 uint8_t value_buf[4];
2566 if (!target_was_examined(target)) {
2567 LOG_ERROR("Target not examined yet");
2571 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32 "",
2575 target_buffer_set_u32(target, value_buf, value);
2576 retval = target_write_phys_memory(target, address, 4, 1, value_buf);
2577 if (retval != ERROR_OK)
2578 LOG_DEBUG("failed: %i", retval);
2583 int target_write_phys_u16(struct target *target, target_addr_t address, uint16_t value)
2586 uint8_t value_buf[2];
2587 if (!target_was_examined(target)) {
2588 LOG_ERROR("Target not examined yet");
2592 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx16,
2596 target_buffer_set_u16(target, value_buf, value);
2597 retval = target_write_phys_memory(target, address, 2, 1, value_buf);
2598 if (retval != ERROR_OK)
2599 LOG_DEBUG("failed: %i", retval);
2604 int target_write_phys_u8(struct target *target, target_addr_t address, uint8_t value)
2607 if (!target_was_examined(target)) {
2608 LOG_ERROR("Target not examined yet");
2612 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
2615 retval = target_write_phys_memory(target, address, 1, 1, &value);
2616 if (retval != ERROR_OK)
2617 LOG_DEBUG("failed: %i", retval);
2622 static int find_target(struct command_invocation *cmd, const char *name)
2624 struct target *target = get_target(name);
2625 if (target == NULL) {
2626 command_print(cmd, "Target: %s is unknown, try one of:\n", name);
2629 if (!target->tap->enabled) {
2630 command_print(cmd, "Target: TAP %s is disabled, "
2631 "can't be the current target\n",
2632 target->tap->dotted_name);
2636 cmd->ctx->current_target = target;
2637 if (cmd->ctx->current_target_override)
2638 cmd->ctx->current_target_override = target;
2644 COMMAND_HANDLER(handle_targets_command)
2646 int retval = ERROR_OK;
2647 if (CMD_ARGC == 1) {
2648 retval = find_target(CMD, CMD_ARGV[0]);
2649 if (retval == ERROR_OK) {
2655 struct target *target = all_targets;
2656 command_print(CMD, " TargetName Type Endian TapName State ");
2657 command_print(CMD, "-- ------------------ ---------- ------ ------------------ ------------");
2662 if (target->tap->enabled)
2663 state = target_state_name(target);
2665 state = "tap-disabled";
2667 if (CMD_CTX->current_target == target)
2670 /* keep columns lined up to match the headers above */
2672 "%2d%c %-18s %-10s %-6s %-18s %s",
2673 target->target_number,
2675 target_name(target),
2676 target_type_name(target),
2677 Jim_Nvp_value2name_simple(nvp_target_endian,
2678 target->endianness)->name,
2679 target->tap->dotted_name,
2681 target = target->next;
2687 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2689 static int powerDropout;
2690 static int srstAsserted;
2692 static int runPowerRestore;
2693 static int runPowerDropout;
2694 static int runSrstAsserted;
2695 static int runSrstDeasserted;
2697 static int sense_handler(void)
2699 static int prevSrstAsserted;
2700 static int prevPowerdropout;
2702 int retval = jtag_power_dropout(&powerDropout);
2703 if (retval != ERROR_OK)
2707 powerRestored = prevPowerdropout && !powerDropout;
2709 runPowerRestore = 1;
2711 int64_t current = timeval_ms();
2712 static int64_t lastPower;
2713 bool waitMore = lastPower + 2000 > current;
2714 if (powerDropout && !waitMore) {
2715 runPowerDropout = 1;
2716 lastPower = current;
2719 retval = jtag_srst_asserted(&srstAsserted);
2720 if (retval != ERROR_OK)
2724 srstDeasserted = prevSrstAsserted && !srstAsserted;
2726 static int64_t lastSrst;
2727 waitMore = lastSrst + 2000 > current;
2728 if (srstDeasserted && !waitMore) {
2729 runSrstDeasserted = 1;
2733 if (!prevSrstAsserted && srstAsserted)
2734 runSrstAsserted = 1;
2736 prevSrstAsserted = srstAsserted;
2737 prevPowerdropout = powerDropout;
2739 if (srstDeasserted || powerRestored) {
2740 /* Other than logging the event we can't do anything here.
2741 * Issuing a reset is a particularly bad idea as we might
2742 * be inside a reset already.
2749 /* process target state changes */
2750 static int handle_target(void *priv)
2752 Jim_Interp *interp = (Jim_Interp *)priv;
2753 int retval = ERROR_OK;
2755 if (!is_jtag_poll_safe()) {
2756 /* polling is disabled currently */
2760 /* we do not want to recurse here... */
2761 static int recursive;
2765 /* danger! running these procedures can trigger srst assertions and power dropouts.
2766 * We need to avoid an infinite loop/recursion here and we do that by
2767 * clearing the flags after running these events.
2769 int did_something = 0;
2770 if (runSrstAsserted) {
2771 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2772 Jim_Eval(interp, "srst_asserted");
2775 if (runSrstDeasserted) {
2776 Jim_Eval(interp, "srst_deasserted");
2779 if (runPowerDropout) {
2780 LOG_INFO("Power dropout detected, running power_dropout proc.");
2781 Jim_Eval(interp, "power_dropout");
2784 if (runPowerRestore) {
2785 Jim_Eval(interp, "power_restore");
2789 if (did_something) {
2790 /* clear detect flags */
2794 /* clear action flags */
2796 runSrstAsserted = 0;
2797 runSrstDeasserted = 0;
2798 runPowerRestore = 0;
2799 runPowerDropout = 0;
2804 /* Poll targets for state changes unless that's globally disabled.
2805 * Skip targets that are currently disabled.
2807 for (struct target *target = all_targets;
2808 is_jtag_poll_safe() && target;
2809 target = target->next) {
2811 if (!target_was_examined(target))
2814 if (!target->tap->enabled)
2817 if (target->backoff.times > target->backoff.count) {
2818 /* do not poll this time as we failed previously */
2819 target->backoff.count++;
2822 target->backoff.count = 0;
2824 /* only poll target if we've got power and srst isn't asserted */
2825 if (!powerDropout && !srstAsserted) {
2826 /* polling may fail silently until the target has been examined */
2827 retval = target_poll(target);
2828 if (retval != ERROR_OK) {
2829 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2830 if (target->backoff.times * polling_interval < 5000) {
2831 target->backoff.times *= 2;
2832 target->backoff.times++;
2835 /* Tell GDB to halt the debugger. This allows the user to
2836 * run monitor commands to handle the situation.
2838 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2840 if (target->backoff.times > 0) {
2841 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target));
2842 target_reset_examined(target);
2843 retval = target_examine_one(target);
2844 /* Target examination could have failed due to unstable connection,
2845 * but we set the examined flag anyway to repoll it later */
2846 if (retval != ERROR_OK) {
2847 target->examined = true;
2848 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2849 target->backoff.times * polling_interval);
2854 /* Since we succeeded, we reset backoff count */
2855 target->backoff.times = 0;
2862 COMMAND_HANDLER(handle_reg_command)
2864 struct target *target;
2865 struct reg *reg = NULL;
2871 target = get_current_target(CMD_CTX);
2873 /* list all available registers for the current target */
2874 if (CMD_ARGC == 0) {
2875 struct reg_cache *cache = target->reg_cache;
2881 command_print(CMD, "===== %s", cache->name);
2883 for (i = 0, reg = cache->reg_list;
2884 i < cache->num_regs;
2885 i++, reg++, count++) {
2886 if (reg->exist == false)
2888 /* only print cached values if they are valid */
2890 value = buf_to_hex_str(reg->value,
2893 "(%i) %s (/%" PRIu32 "): 0x%s%s",
2901 command_print(CMD, "(%i) %s (/%" PRIu32 ")",
2906 cache = cache->next;
2912 /* access a single register by its ordinal number */
2913 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9')) {
2915 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
2917 struct reg_cache *cache = target->reg_cache;
2921 for (i = 0; i < cache->num_regs; i++) {
2922 if (count++ == num) {
2923 reg = &cache->reg_list[i];
2929 cache = cache->next;
2933 command_print(CMD, "%i is out of bounds, the current target "
2934 "has only %i registers (0 - %i)", num, count, count - 1);
2938 /* access a single register by its name */
2939 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
2945 assert(reg != NULL); /* give clang a hint that we *know* reg is != NULL here */
2950 /* display a register */
2951 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0')
2952 && (CMD_ARGV[1][0] <= '9')))) {
2953 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
2956 if (reg->valid == 0)
2957 reg->type->get(reg);
2958 value = buf_to_hex_str(reg->value, reg->size);
2959 command_print(CMD, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2964 /* set register value */
2965 if (CMD_ARGC == 2) {
2966 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
2969 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
2971 reg->type->set(reg, buf);
2973 value = buf_to_hex_str(reg->value, reg->size);
2974 command_print(CMD, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2982 return ERROR_COMMAND_SYNTAX_ERROR;
2985 command_print(CMD, "register %s not found in current target", CMD_ARGV[0]);
2989 COMMAND_HANDLER(handle_poll_command)
2991 int retval = ERROR_OK;
2992 struct target *target = get_current_target(CMD_CTX);
2994 if (CMD_ARGC == 0) {
2995 command_print(CMD, "background polling: %s",
2996 jtag_poll_get_enabled() ? "on" : "off");
2997 command_print(CMD, "TAP: %s (%s)",
2998 target->tap->dotted_name,
2999 target->tap->enabled ? "enabled" : "disabled");
3000 if (!target->tap->enabled)
3002 retval = target_poll(target);
3003 if (retval != ERROR_OK)
3005 retval = target_arch_state(target);
3006 if (retval != ERROR_OK)
3008 } else if (CMD_ARGC == 1) {
3010 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
3011 jtag_poll_set_enabled(enable);
3013 return ERROR_COMMAND_SYNTAX_ERROR;
3018 COMMAND_HANDLER(handle_wait_halt_command)
3021 return ERROR_COMMAND_SYNTAX_ERROR;
3023 unsigned ms = DEFAULT_HALT_TIMEOUT;
3024 if (1 == CMD_ARGC) {
3025 int retval = parse_uint(CMD_ARGV[0], &ms);
3026 if (ERROR_OK != retval)
3027 return ERROR_COMMAND_SYNTAX_ERROR;
3030 struct target *target = get_current_target(CMD_CTX);
3031 return target_wait_state(target, TARGET_HALTED, ms);
3034 /* wait for target state to change. The trick here is to have a low
3035 * latency for short waits and not to suck up all the CPU time
3038 * After 500ms, keep_alive() is invoked
3040 int target_wait_state(struct target *target, enum target_state state, int ms)
3043 int64_t then = 0, cur;
3047 retval = target_poll(target);
3048 if (retval != ERROR_OK)
3050 if (target->state == state)
3055 then = timeval_ms();
3056 LOG_DEBUG("waiting for target %s...",
3057 Jim_Nvp_value2name_simple(nvp_target_state, state)->name);
3063 if ((cur-then) > ms) {
3064 LOG_ERROR("timed out while waiting for target %s",
3065 Jim_Nvp_value2name_simple(nvp_target_state, state)->name);
3073 COMMAND_HANDLER(handle_halt_command)
3077 struct target *target = get_current_target(CMD_CTX);
3079 target->verbose_halt_msg = true;
3081 int retval = target_halt(target);
3082 if (ERROR_OK != retval)
3085 if (CMD_ARGC == 1) {
3086 unsigned wait_local;
3087 retval = parse_uint(CMD_ARGV[0], &wait_local);
3088 if (ERROR_OK != retval)
3089 return ERROR_COMMAND_SYNTAX_ERROR;
3094 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
3097 COMMAND_HANDLER(handle_soft_reset_halt_command)
3099 struct target *target = get_current_target(CMD_CTX);
3101 LOG_USER("requesting target halt and executing a soft reset");
3103 target_soft_reset_halt(target);
3108 COMMAND_HANDLER(handle_reset_command)
3111 return ERROR_COMMAND_SYNTAX_ERROR;
3113 enum target_reset_mode reset_mode = RESET_RUN;
3114 if (CMD_ARGC == 1) {
3116 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
3117 if ((n->name == NULL) || (n->value == RESET_UNKNOWN))
3118 return ERROR_COMMAND_SYNTAX_ERROR;
3119 reset_mode = n->value;
3122 /* reset *all* targets */
3123 return target_process_reset(CMD, reset_mode);
3127 COMMAND_HANDLER(handle_resume_command)
3131 return ERROR_COMMAND_SYNTAX_ERROR;
3133 struct target *target = get_current_target(CMD_CTX);
3135 /* with no CMD_ARGV, resume from current pc, addr = 0,
3136 * with one arguments, addr = CMD_ARGV[0],
3137 * handle breakpoints, not debugging */
3138 target_addr_t addr = 0;
3139 if (CMD_ARGC == 1) {
3140 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
3144 return target_resume(target, current, addr, 1, 0);
3147 COMMAND_HANDLER(handle_step_command)
3150 return ERROR_COMMAND_SYNTAX_ERROR;
3154 /* with no CMD_ARGV, step from current pc, addr = 0,
3155 * with one argument addr = CMD_ARGV[0],
3156 * handle breakpoints, debugging */
3157 target_addr_t addr = 0;
3159 if (CMD_ARGC == 1) {
3160 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
3164 struct target *target = get_current_target(CMD_CTX);
3166 return target_step(target, current_pc, addr, 1);
3169 void target_handle_md_output(struct command_invocation *cmd,
3170 struct target *target, target_addr_t address, unsigned size,
3171 unsigned count, const uint8_t *buffer)
3173 const unsigned line_bytecnt = 32;
3174 unsigned line_modulo = line_bytecnt / size;
3176 char output[line_bytecnt * 4 + 1];
3177 unsigned output_len = 0;
3179 const char *value_fmt;
3182 value_fmt = "%16.16"PRIx64" ";
3185 value_fmt = "%8.8"PRIx64" ";
3188 value_fmt = "%4.4"PRIx64" ";
3191 value_fmt = "%2.2"PRIx64" ";
3194 /* "can't happen", caller checked */
3195 LOG_ERROR("invalid memory read size: %u", size);
3199 for (unsigned i = 0; i < count; i++) {
3200 if (i % line_modulo == 0) {
3201 output_len += snprintf(output + output_len,
3202 sizeof(output) - output_len,
3203 TARGET_ADDR_FMT ": ",
3204 (address + (i * size)));
3208 const uint8_t *value_ptr = buffer + i * size;
3211 value = target_buffer_get_u64(target, value_ptr);
3214 value = target_buffer_get_u32(target, value_ptr);
3217 value = target_buffer_get_u16(target, value_ptr);
3222 output_len += snprintf(output + output_len,
3223 sizeof(output) - output_len,
3226 if ((i % line_modulo == line_modulo - 1) || (i == count - 1)) {
3227 command_print(cmd, "%s", output);
3233 COMMAND_HANDLER(handle_md_command)
3236 return ERROR_COMMAND_SYNTAX_ERROR;
3239 switch (CMD_NAME[2]) {
3253 return ERROR_COMMAND_SYNTAX_ERROR;
3256 bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
3257 int (*fn)(struct target *target,
3258 target_addr_t address, uint32_t size_value, uint32_t count, uint8_t *buffer);
3262 fn = target_read_phys_memory;
3264 fn = target_read_memory;
3265 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
3266 return ERROR_COMMAND_SYNTAX_ERROR;
3268 target_addr_t address;
3269 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], address);
3273 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
3275 uint8_t *buffer = calloc(count, size);
3276 if (buffer == NULL) {
3277 LOG_ERROR("Failed to allocate md read buffer");
3281 struct target *target = get_current_target(CMD_CTX);
3282 int retval = fn(target, address, size, count, buffer);
3283 if (ERROR_OK == retval)
3284 target_handle_md_output(CMD, target, address, size, count, buffer);
3291 typedef int (*target_write_fn)(struct target *target,
3292 target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer);
3294 static int target_fill_mem(struct target *target,
3295 target_addr_t address,
3303 /* We have to write in reasonably large chunks to be able
3304 * to fill large memory areas with any sane speed */
3305 const unsigned chunk_size = 16384;
3306 uint8_t *target_buf = malloc(chunk_size * data_size);
3307 if (target_buf == NULL) {
3308 LOG_ERROR("Out of memory");
3312 for (unsigned i = 0; i < chunk_size; i++) {
3313 switch (data_size) {
3315 target_buffer_set_u64(target, target_buf + i * data_size, b);
3318 target_buffer_set_u32(target, target_buf + i * data_size, b);
3321 target_buffer_set_u16(target, target_buf + i * data_size, b);
3324 target_buffer_set_u8(target, target_buf + i * data_size, b);
3331 int retval = ERROR_OK;
3333 for (unsigned x = 0; x < c; x += chunk_size) {
3336 if (current > chunk_size)
3337 current = chunk_size;
3338 retval = fn(target, address + x * data_size, data_size, current, target_buf);
3339 if (retval != ERROR_OK)
3341 /* avoid GDB timeouts */
3350 COMMAND_HANDLER(handle_mw_command)
3353 return ERROR_COMMAND_SYNTAX_ERROR;
3354 bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
3359 fn = target_write_phys_memory;
3361 fn = target_write_memory;
3362 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
3363 return ERROR_COMMAND_SYNTAX_ERROR;
3365 target_addr_t address;
3366 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], address);
3369 COMMAND_PARSE_NUMBER(u64, CMD_ARGV[1], value);
3373 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
3375 struct target *target = get_current_target(CMD_CTX);
3377 switch (CMD_NAME[2]) {
3391 return ERROR_COMMAND_SYNTAX_ERROR;
3394 return target_fill_mem(target, address, fn, wordsize, value, count);
3397 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
3398 target_addr_t *min_address, target_addr_t *max_address)
3400 if (CMD_ARGC < 1 || CMD_ARGC > 5)
3401 return ERROR_COMMAND_SYNTAX_ERROR;
3403 /* a base address isn't always necessary,
3404 * default to 0x0 (i.e. don't relocate) */
3405 if (CMD_ARGC >= 2) {
3407 COMMAND_PARSE_ADDRESS(CMD_ARGV[1], addr);
3408 image->base_address = addr;
3409 image->base_address_set = true;
3411 image->base_address_set = false;
3413 image->start_address_set = false;
3416 COMMAND_PARSE_ADDRESS(CMD_ARGV[3], *min_address);
3417 if (CMD_ARGC == 5) {
3418 COMMAND_PARSE_ADDRESS(CMD_ARGV[4], *max_address);
3419 /* use size (given) to find max (required) */
3420 *max_address += *min_address;
3423 if (*min_address > *max_address)
3424 return ERROR_COMMAND_SYNTAX_ERROR;
3429 COMMAND_HANDLER(handle_load_image_command)
3433 uint32_t image_size;
3434 target_addr_t min_address = 0;
3435 target_addr_t max_address = -1;
3438 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
3439 &image, &min_address, &max_address);
3440 if (ERROR_OK != retval)
3443 struct target *target = get_current_target(CMD_CTX);
3445 struct duration bench;
3446 duration_start(&bench);
3448 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
3453 for (unsigned int i = 0; i < image.num_sections; i++) {
3454 buffer = malloc(image.sections[i].size);
3455 if (buffer == NULL) {
3457 "error allocating buffer for section (%d bytes)",
3458 (int)(image.sections[i].size));
3459 retval = ERROR_FAIL;
3463 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
3464 if (retval != ERROR_OK) {
3469 uint32_t offset = 0;
3470 uint32_t length = buf_cnt;
3472 /* DANGER!!! beware of unsigned comparison here!!! */
3474 if ((image.sections[i].base_address + buf_cnt >= min_address) &&
3475 (image.sections[i].base_address < max_address)) {
3477 if (image.sections[i].base_address < min_address) {
3478 /* clip addresses below */
3479 offset += min_address-image.sections[i].base_address;
3483 if (image.sections[i].base_address + buf_cnt > max_address)
3484 length -= (image.sections[i].base_address + buf_cnt)-max_address;
3486 retval = target_write_buffer(target,
3487 image.sections[i].base_address + offset, length, buffer + offset);
3488 if (retval != ERROR_OK) {
3492 image_size += length;
3493 command_print(CMD, "%u bytes written at address " TARGET_ADDR_FMT "",
3494 (unsigned int)length,
3495 image.sections[i].base_address + offset);
3501 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
3502 command_print(CMD, "downloaded %" PRIu32 " bytes "
3503 "in %fs (%0.3f KiB/s)", image_size,
3504 duration_elapsed(&bench), duration_kbps(&bench, image_size));
3507 image_close(&image);
3513 COMMAND_HANDLER(handle_dump_image_command)
3515 struct fileio *fileio;
3517 int retval, retvaltemp;
3518 target_addr_t address, size;
3519 struct duration bench;
3520 struct target *target = get_current_target(CMD_CTX);
3523 return ERROR_COMMAND_SYNTAX_ERROR;
3525 COMMAND_PARSE_ADDRESS(CMD_ARGV[1], address);
3526 COMMAND_PARSE_ADDRESS(CMD_ARGV[2], size);
3528 uint32_t buf_size = (size > 4096) ? 4096 : size;
3529 buffer = malloc(buf_size);
3533 retval = fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY);
3534 if (retval != ERROR_OK) {
3539 duration_start(&bench);
3542 size_t size_written;
3543 uint32_t this_run_size = (size > buf_size) ? buf_size : size;
3544 retval = target_read_buffer(target, address, this_run_size, buffer);
3545 if (retval != ERROR_OK)
3548 retval = fileio_write(fileio, this_run_size, buffer, &size_written);
3549 if (retval != ERROR_OK)
3552 size -= this_run_size;
3553 address += this_run_size;
3558 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
3560 retval = fileio_size(fileio, &filesize);
3561 if (retval != ERROR_OK)
3564 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize,
3565 duration_elapsed(&bench), duration_kbps(&bench, filesize));
3568 retvaltemp = fileio_close(fileio);
3569 if (retvaltemp != ERROR_OK)
3578 IMAGE_CHECKSUM_ONLY = 2
3581 static COMMAND_HELPER(handle_verify_image_command_internal, enum verify_mode verify)
3585 uint32_t image_size;
3587 uint32_t checksum = 0;
3588 uint32_t mem_checksum = 0;
3592 struct target *target = get_current_target(CMD_CTX);
3595 return ERROR_COMMAND_SYNTAX_ERROR;
3598 LOG_ERROR("no target selected");
3602 struct duration bench;
3603 duration_start(&bench);
3605 if (CMD_ARGC >= 2) {
3607 COMMAND_PARSE_ADDRESS(CMD_ARGV[1], addr);
3608 image.base_address = addr;
3609 image.base_address_set = true;
3611 image.base_address_set = false;
3612 image.base_address = 0x0;
3615 image.start_address_set = false;
3617 retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL);
3618 if (retval != ERROR_OK)
3624 for (unsigned int i = 0; i < image.num_sections; i++) {
3625 buffer = malloc(image.sections[i].size);
3626 if (buffer == NULL) {
3628 "error allocating buffer for section (%" PRIu32 " bytes)",
3629 image.sections[i].size);
3632 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
3633 if (retval != ERROR_OK) {
3638 if (verify >= IMAGE_VERIFY) {
3639 /* calculate checksum of image */
3640 retval = image_calculate_checksum(buffer, buf_cnt, &checksum);
3641 if (retval != ERROR_OK) {
3646 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
3647 if (retval != ERROR_OK) {
3651 if ((checksum != mem_checksum) && (verify == IMAGE_CHECKSUM_ONLY)) {
3652 LOG_ERROR("checksum mismatch");
3654 retval = ERROR_FAIL;
3657 if (checksum != mem_checksum) {
3658 /* failed crc checksum, fall back to a binary compare */
3662 LOG_ERROR("checksum mismatch - attempting binary compare");
3664 data = malloc(buf_cnt);
3666 retval = target_read_buffer(target, image.sections[i].base_address, buf_cnt, data);
3667 if (retval == ERROR_OK) {
3669 for (t = 0; t < buf_cnt; t++) {
3670 if (data[t] != buffer[t]) {
3672 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3674 (unsigned)(t + image.sections[i].base_address),
3677 if (diffs++ >= 127) {
3678 command_print(CMD, "More than 128 errors, the rest are not printed.");
3690 command_print(CMD, "address " TARGET_ADDR_FMT " length 0x%08zx",
3691 image.sections[i].base_address,
3696 image_size += buf_cnt;
3699 command_print(CMD, "No more differences found.");
3702 retval = ERROR_FAIL;
3703 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
3704 command_print(CMD, "verified %" PRIu32 " bytes "
3705 "in %fs (%0.3f KiB/s)", image_size,
3706 duration_elapsed(&bench), duration_kbps(&bench, image_size));
3709 image_close(&image);
3714 COMMAND_HANDLER(handle_verify_image_checksum_command)
3716 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_CHECKSUM_ONLY);
3719 COMMAND_HANDLER(handle_verify_image_command)
3721 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_VERIFY);
3724 COMMAND_HANDLER(handle_test_image_command)
3726 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_TEST);
3729 static int handle_bp_command_list(struct command_invocation *cmd)
3731 struct target *target = get_current_target(cmd->ctx);
3732 struct breakpoint *breakpoint = target->breakpoints;
3733 while (breakpoint) {
3734 if (breakpoint->type == BKPT_SOFT) {
3735 char *buf = buf_to_hex_str(breakpoint->orig_instr,
3736 breakpoint->length);
3737 command_print(cmd, "IVA breakpoint: " TARGET_ADDR_FMT ", 0x%x, %i, 0x%s",
3738 breakpoint->address,
3740 breakpoint->set, buf);
3743 if ((breakpoint->address == 0) && (breakpoint->asid != 0))
3744 command_print(cmd, "Context breakpoint: 0x%8.8" PRIx32 ", 0x%x, %i",
3746 breakpoint->length, breakpoint->set);
3747 else if ((breakpoint->address != 0) && (breakpoint->asid != 0)) {
3748 command_print(cmd, "Hybrid breakpoint(IVA): " TARGET_ADDR_FMT ", 0x%x, %i",
3749 breakpoint->address,
3750 breakpoint->length, breakpoint->set);
3751 command_print(cmd, "\t|--->linked with ContextID: 0x%8.8" PRIx32,
3754 command_print(cmd, "Breakpoint(IVA): " TARGET_ADDR_FMT ", 0x%x, %i",
3755 breakpoint->address,
3756 breakpoint->length, breakpoint->set);
3759 breakpoint = breakpoint->next;
3764 static int handle_bp_command_set(struct command_invocation *cmd,
3765 target_addr_t addr, uint32_t asid, uint32_t length, int hw)
3767 struct target *target = get_current_target(cmd->ctx);
3771 retval = breakpoint_add(target, addr, length, hw);
3772 /* error is always logged in breakpoint_add(), do not print it again */
3773 if (ERROR_OK == retval)
3774 command_print(cmd, "breakpoint set at " TARGET_ADDR_FMT "", addr);
3776 } else if (addr == 0) {
3777 if (target->type->add_context_breakpoint == NULL) {
3778 LOG_ERROR("Context breakpoint not available");
3779 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
3781 retval = context_breakpoint_add(target, asid, length, hw);
3782 /* error is always logged in context_breakpoint_add(), do not print it again */
3783 if (ERROR_OK == retval)
3784 command_print(cmd, "Context breakpoint set at 0x%8.8" PRIx32 "", asid);
3787 if (target->type->add_hybrid_breakpoint == NULL) {
3788 LOG_ERROR("Hybrid breakpoint not available");
3789 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
3791 retval = hybrid_breakpoint_add(target, addr, asid, length, hw);
3792 /* error is always logged in hybrid_breakpoint_add(), do not print it again */
3793 if (ERROR_OK == retval)
3794 command_print(cmd, "Hybrid breakpoint set at 0x%8.8" PRIx32 "", asid);
3799 COMMAND_HANDLER(handle_bp_command)
3808 return handle_bp_command_list(CMD);
3812 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
3813 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3814 return handle_bp_command_set(CMD, addr, asid, length, hw);
3817 if (strcmp(CMD_ARGV[2], "hw") == 0) {
3819 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
3820 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3822 return handle_bp_command_set(CMD, addr, asid, length, hw);
3823 } else if (strcmp(CMD_ARGV[2], "hw_ctx") == 0) {
3825 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], asid);
3826 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3828 return handle_bp_command_set(CMD, addr, asid, length, hw);
3833 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
3834 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], asid);
3835 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], length);
3836 return handle_bp_command_set(CMD, addr, asid, length, hw);
3839 return ERROR_COMMAND_SYNTAX_ERROR;
3843 COMMAND_HANDLER(handle_rbp_command)
3846 return ERROR_COMMAND_SYNTAX_ERROR;
3848 struct target *target = get_current_target(CMD_CTX);
3850 if (!strcmp(CMD_ARGV[0], "all")) {
3851 breakpoint_remove_all(target);
3854 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
3856 breakpoint_remove(target, addr);
3862 COMMAND_HANDLER(handle_wp_command)
3864 struct target *target = get_current_target(CMD_CTX);
3866 if (CMD_ARGC == 0) {
3867 struct watchpoint *watchpoint = target->watchpoints;
3869 while (watchpoint) {
3870 command_print(CMD, "address: " TARGET_ADDR_FMT
3871 ", len: 0x%8.8" PRIx32
3872 ", r/w/a: %i, value: 0x%8.8" PRIx32
3873 ", mask: 0x%8.8" PRIx32,
3874 watchpoint->address,
3876 (int)watchpoint->rw,
3879 watchpoint = watchpoint->next;
3884 enum watchpoint_rw type = WPT_ACCESS;
3886 uint32_t length = 0;
3887 uint32_t data_value = 0x0;
3888 uint32_t data_mask = 0xffffffff;
3892 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
3895 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
3898 switch (CMD_ARGV[2][0]) {
3909 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
3910 return ERROR_COMMAND_SYNTAX_ERROR;
3914 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3915 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3919 return ERROR_COMMAND_SYNTAX_ERROR;
3922 int retval = watchpoint_add(target, addr, length, type,
3923 data_value, data_mask);
3924 if (ERROR_OK != retval)
3925 LOG_ERROR("Failure setting watchpoints");
3930 COMMAND_HANDLER(handle_rwp_command)
3933 return ERROR_COMMAND_SYNTAX_ERROR;
3936 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3938 struct target *target = get_current_target(CMD_CTX);
3939 watchpoint_remove(target, addr);
3945 * Translate a virtual address to a physical address.
3947 * The low-level target implementation must have logged a detailed error
3948 * which is forwarded to telnet/GDB session.
3950 COMMAND_HANDLER(handle_virt2phys_command)
3953 return ERROR_COMMAND_SYNTAX_ERROR;
3956 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], va);
3959 struct target *target = get_current_target(CMD_CTX);
3960 int retval = target->type->virt2phys(target, va, &pa);
3961 if (retval == ERROR_OK)
3962 command_print(CMD, "Physical address " TARGET_ADDR_FMT "", pa);
3967 static void writeData(FILE *f, const void *data, size_t len)
3969 size_t written = fwrite(data, 1, len, f);
3971 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
3974 static void writeLong(FILE *f, int l, struct target *target)
3978 target_buffer_set_u32(target, val, l);
3979 writeData(f, val, 4);
3982 static void writeString(FILE *f, char *s)
3984 writeData(f, s, strlen(s));
3987 typedef unsigned char UNIT[2]; /* unit of profiling */
3989 /* Dump a gmon.out histogram file. */
3990 static void write_gmon(uint32_t *samples, uint32_t sampleNum, const char *filename, bool with_range,
3991 uint32_t start_address, uint32_t end_address, struct target *target, uint32_t duration_ms)
3994 FILE *f = fopen(filename, "w");
3997 writeString(f, "gmon");
3998 writeLong(f, 0x00000001, target); /* Version */
3999 writeLong(f, 0, target); /* padding */
4000 writeLong(f, 0, target); /* padding */
4001 writeLong(f, 0, target); /* padding */
4003 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
4004 writeData(f, &zero, 1);
4006 /* figure out bucket size */
4010 min = start_address;
4015 for (i = 0; i < sampleNum; i++) {
4016 if (min > samples[i])
4018 if (max < samples[i])
4022 /* max should be (largest sample + 1)
4023 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
4027 int addressSpace = max - min;
4028 assert(addressSpace >= 2);
4030 /* FIXME: What is the reasonable number of buckets?
4031 * The profiling result will be more accurate if there are enough buckets. */
4032 static const uint32_t maxBuckets = 128 * 1024; /* maximum buckets. */
4033 uint32_t numBuckets = addressSpace / sizeof(UNIT);
4034 if (numBuckets > maxBuckets)
4035 numBuckets = maxBuckets;
4036 int *buckets = malloc(sizeof(int) * numBuckets);
4037 if (buckets == NULL) {
4041 memset(buckets, 0, sizeof(int) * numBuckets);
4042 for (i = 0; i < sampleNum; i++) {
4043 uint32_t address = samples[i];
4045 if ((address < min) || (max <= address))
4048 long long a = address - min;
4049 long long b = numBuckets;
4050 long long c = addressSpace;
4051 int index_t = (a * b) / c; /* danger!!!! int32 overflows */
4055 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
4056 writeLong(f, min, target); /* low_pc */
4057 writeLong(f, max, target); /* high_pc */
4058 writeLong(f, numBuckets, target); /* # of buckets */
4059 float sample_rate = sampleNum / (duration_ms / 1000.0);
4060 writeLong(f, sample_rate, target);
4061 writeString(f, "seconds");
4062 for (i = 0; i < (15-strlen("seconds")); i++)
4063 writeData(f, &zero, 1);
4064 writeString(f, "s");
4066 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
4068 char *data = malloc(2 * numBuckets);
4070 for (i = 0; i < numBuckets; i++) {
4075 data[i * 2] = val&0xff;
4076 data[i * 2 + 1] = (val >> 8) & 0xff;
4079 writeData(f, data, numBuckets * 2);
4087 /* profiling samples the CPU PC as quickly as OpenOCD is able,
4088 * which will be used as a random sampling of PC */
4089 COMMAND_HANDLER(handle_profile_command)
4091 struct target *target = get_current_target(CMD_CTX);
4093 if ((CMD_ARGC != 2) && (CMD_ARGC != 4))
4094 return ERROR_COMMAND_SYNTAX_ERROR;
4096 const uint32_t MAX_PROFILE_SAMPLE_NUM = 10000;
4098 uint32_t num_of_samples;
4099 int retval = ERROR_OK;
4100 bool halted_before_profiling = target->state == TARGET_HALTED;
4102 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], offset);
4104 uint32_t *samples = malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM);
4105 if (samples == NULL) {
4106 LOG_ERROR("No memory to store samples.");
4110 uint64_t timestart_ms = timeval_ms();
4112 * Some cores let us sample the PC without the
4113 * annoying halt/resume step; for example, ARMv7 PCSR.
4114 * Provide a way to use that more efficient mechanism.
4116 retval = target_profiling(target, samples, MAX_PROFILE_SAMPLE_NUM,
4117 &num_of_samples, offset);
4118 if (retval != ERROR_OK) {
4122 uint32_t duration_ms = timeval_ms() - timestart_ms;
4124 assert(num_of_samples <= MAX_PROFILE_SAMPLE_NUM);
4126 retval = target_poll(target);
4127 if (retval != ERROR_OK) {
4132 if (target->state == TARGET_RUNNING && halted_before_profiling) {
4133 /* The target was halted before we started and is running now. Halt it,
4134 * for consistency. */
4135 retval = target_halt(target);
4136 if (retval != ERROR_OK) {
4140 } else if (target->state == TARGET_HALTED && !halted_before_profiling) {
4141 /* The target was running before we started and is halted now. Resume
4142 * it, for consistency. */
4143 retval = target_resume(target, 1, 0, 0, 0);
4144 if (retval != ERROR_OK) {
4150 retval = target_poll(target);
4151 if (retval != ERROR_OK) {
4156 uint32_t start_address = 0;
4157 uint32_t end_address = 0;
4158 bool with_range = false;
4159 if (CMD_ARGC == 4) {
4161 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], start_address);
4162 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], end_address);
4165 write_gmon(samples, num_of_samples, CMD_ARGV[1],
4166 with_range, start_address, end_address, target, duration_ms);
4167 command_print(CMD, "Wrote %s", CMD_ARGV[1]);
4173 static int new_int_array_element(Jim_Interp *interp, const char *varname, int idx, uint32_t val)
4176 Jim_Obj *nameObjPtr, *valObjPtr;
4179 namebuf = alloc_printf("%s(%d)", varname, idx);
4183 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
4184 valObjPtr = Jim_NewIntObj(interp, val);
4185 if (!nameObjPtr || !valObjPtr) {
4190 Jim_IncrRefCount(nameObjPtr);
4191 Jim_IncrRefCount(valObjPtr);
4192 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
4193 Jim_DecrRefCount(interp, nameObjPtr);
4194 Jim_DecrRefCount(interp, valObjPtr);
4196 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
4200 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4202 struct command_context *context;
4203 struct target *target;
4205 context = current_command_context(interp);
4206 assert(context != NULL);
4208 target = get_current_target(context);
4209 if (target == NULL) {
4210 LOG_ERROR("mem2array: no current target");
4214 return target_mem2array(interp, target, argc - 1, argv + 1);
4217 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
4225 const char *varname;
4231 /* argv[1] = name of array to receive the data
4232 * argv[2] = desired width
4233 * argv[3] = memory address
4234 * argv[4] = count of times to read
4237 if (argc < 4 || argc > 5) {
4238 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems [phys]");
4241 varname = Jim_GetString(argv[0], &len);
4242 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4244 e = Jim_GetLong(interp, argv[1], &l);
4249 e = Jim_GetLong(interp, argv[2], &l);
4253 e = Jim_GetLong(interp, argv[3], &l);
4259 phys = Jim_GetString(argv[4], &n);
4260 if (!strncmp(phys, "phys", n))
4276 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4277 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
4281 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4282 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
4285 if ((addr + (len * width)) < addr) {
4286 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4287 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
4290 /* absurd transfer size? */
4292 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4293 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
4298 ((width == 2) && ((addr & 1) == 0)) ||
4299 ((width == 4) && ((addr & 3) == 0))) {
4303 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4304 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRIu32 " byte reads",
4307 Jim_AppendStrings(interp, Jim_GetResult(interp), buf, NULL);
4316 size_t buffersize = 4096;
4317 uint8_t *buffer = malloc(buffersize);
4324 /* Slurp... in buffer size chunks */
4326 count = len; /* in objects.. */
4327 if (count > (buffersize / width))
4328 count = (buffersize / width);
4331 retval = target_read_phys_memory(target, addr, width, count, buffer);
4333 retval = target_read_memory(target, addr, width, count, buffer);
4334 if (retval != ERROR_OK) {
4336 LOG_ERROR("mem2array: Read @ 0x%08" PRIx32 ", w=%" PRIu32 ", cnt=%" PRIu32 ", failed",
4340 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4341 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
4345 v = 0; /* shut up gcc */
4346 for (i = 0; i < count ; i++, n++) {
4349 v = target_buffer_get_u32(target, &buffer[i*width]);
4352 v = target_buffer_get_u16(target, &buffer[i*width]);
4355 v = buffer[i] & 0x0ff;
4358 new_int_array_element(interp, varname, n, v);
4361 addr += count * width;
4367 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4372 static int get_int_array_element(Jim_Interp *interp, const char *varname, int idx, uint32_t *val)
4375 Jim_Obj *nameObjPtr, *valObjPtr;
4379 namebuf = alloc_printf("%s(%d)", varname, idx);
4383 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
4389 Jim_IncrRefCount(nameObjPtr);
4390 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
4391 Jim_DecrRefCount(interp, nameObjPtr);
4393 if (valObjPtr == NULL)
4396 result = Jim_GetLong(interp, valObjPtr, &l);
4397 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
4402 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4404 struct command_context *context;
4405 struct target *target;
4407 context = current_command_context(interp);
4408 assert(context != NULL);
4410 target = get_current_target(context);
4411 if (target == NULL) {
4412 LOG_ERROR("array2mem: no current target");
4416 return target_array2mem(interp, target, argc-1, argv + 1);
4419 static int target_array2mem(Jim_Interp *interp, struct target *target,
4420 int argc, Jim_Obj *const *argv)
4428 const char *varname;
4434 /* argv[1] = name of array to get the data
4435 * argv[2] = desired width
4436 * argv[3] = memory address
4437 * argv[4] = count to write
4439 if (argc < 4 || argc > 5) {
4440 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems [phys]");
4443 varname = Jim_GetString(argv[0], &len);
4444 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4446 e = Jim_GetLong(interp, argv[1], &l);
4451 e = Jim_GetLong(interp, argv[2], &l);
4455 e = Jim_GetLong(interp, argv[3], &l);
4461 phys = Jim_GetString(argv[4], &n);
4462 if (!strncmp(phys, "phys", n))
4478 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4479 Jim_AppendStrings(interp, Jim_GetResult(interp),
4480 "Invalid width param, must be 8/16/32", NULL);
4484 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4485 Jim_AppendStrings(interp, Jim_GetResult(interp),
4486 "array2mem: zero width read?", NULL);
4489 if ((addr + (len * width)) < addr) {
4490 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4491 Jim_AppendStrings(interp, Jim_GetResult(interp),
4492 "array2mem: addr + len - wraps to zero?", NULL);
4495 /* absurd transfer size? */
4497 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4498 Jim_AppendStrings(interp, Jim_GetResult(interp),
4499 "array2mem: absurd > 64K item request", NULL);
4504 ((width == 2) && ((addr & 1) == 0)) ||
4505 ((width == 4) && ((addr & 3) == 0))) {
4509 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4510 sprintf(buf, "array2mem address: 0x%08" PRIx32 " is not aligned for %" PRIu32 " byte reads",
4513 Jim_AppendStrings(interp, Jim_GetResult(interp), buf, NULL);
4524 size_t buffersize = 4096;
4525 uint8_t *buffer = malloc(buffersize);
4530 /* Slurp... in buffer size chunks */
4532 count = len; /* in objects.. */
4533 if (count > (buffersize / width))
4534 count = (buffersize / width);
4536 v = 0; /* shut up gcc */
4537 for (i = 0; i < count; i++, n++) {
4538 get_int_array_element(interp, varname, n, &v);
4541 target_buffer_set_u32(target, &buffer[i * width], v);
4544 target_buffer_set_u16(target, &buffer[i * width], v);
4547 buffer[i] = v & 0x0ff;
4554 retval = target_write_phys_memory(target, addr, width, count, buffer);
4556 retval = target_write_memory(target, addr, width, count, buffer);
4557 if (retval != ERROR_OK) {
4559 LOG_ERROR("array2mem: Write @ 0x%08" PRIx32 ", w=%" PRIu32 ", cnt=%" PRIu32 ", failed",
4563 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4564 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
4568 addr += count * width;
4573 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4578 /* FIX? should we propagate errors here rather than printing them
4581 void target_handle_event(struct target *target, enum target_event e)
4583 struct target_event_action *teap;
4586 for (teap = target->event_action; teap != NULL; teap = teap->next) {
4587 if (teap->event == e) {
4588 LOG_DEBUG("target(%d): %s (%s) event: %d (%s) action: %s",
4589 target->target_number,
4590 target_name(target),
4591 target_type_name(target),
4593 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
4594 Jim_GetString(teap->body, NULL));
4596 /* Override current target by the target an event
4597 * is issued from (lot of scripts need it).
4598 * Return back to previous override as soon
4599 * as the handler processing is done */
4600 struct command_context *cmd_ctx = current_command_context(teap->interp);
4601 struct target *saved_target_override = cmd_ctx->current_target_override;
4602 cmd_ctx->current_target_override = target;
4604 retval = Jim_EvalObj(teap->interp, teap->body);
4606 cmd_ctx->current_target_override = saved_target_override;
4608 if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
4611 if (retval == JIM_RETURN)
4612 retval = teap->interp->returnCode;
4614 if (retval != JIM_OK) {
4615 Jim_MakeErrorMessage(teap->interp);
4616 LOG_USER("Error executing event %s on target %s:\n%s",
4617 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
4618 target_name(target),
4619 Jim_GetString(Jim_GetResult(teap->interp), NULL));
4620 /* clean both error code and stacktrace before return */
4621 Jim_Eval(teap->interp, "error \"\" \"\"");
4628 * Returns true only if the target has a handler for the specified event.
4630 bool target_has_event_action(struct target *target, enum target_event event)
4632 struct target_event_action *teap;
4634 for (teap = target->event_action; teap != NULL; teap = teap->next) {
4635 if (teap->event == event)
4641 enum target_cfg_param {
4644 TCFG_WORK_AREA_VIRT,
4645 TCFG_WORK_AREA_PHYS,
4646 TCFG_WORK_AREA_SIZE,
4647 TCFG_WORK_AREA_BACKUP,
4650 TCFG_CHAIN_POSITION,
4655 TCFG_GDB_MAX_CONNECTIONS,
4658 static Jim_Nvp nvp_config_opts[] = {
4659 { .name = "-type", .value = TCFG_TYPE },
4660 { .name = "-event", .value = TCFG_EVENT },
4661 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
4662 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
4663 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
4664 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
4665 { .name = "-endian", .value = TCFG_ENDIAN },
4666 { .name = "-coreid", .value = TCFG_COREID },
4667 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
4668 { .name = "-dbgbase", .value = TCFG_DBGBASE },
4669 { .name = "-rtos", .value = TCFG_RTOS },
4670 { .name = "-defer-examine", .value = TCFG_DEFER_EXAMINE },
4671 { .name = "-gdb-port", .value = TCFG_GDB_PORT },
4672 { .name = "-gdb-max-connections", .value = TCFG_GDB_MAX_CONNECTIONS },
4673 { .name = NULL, .value = -1 }
4676 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
4683 /* parse config or cget options ... */
4684 while (goi->argc > 0) {
4685 Jim_SetEmptyResult(goi->interp);
4686 /* Jim_GetOpt_Debug(goi); */
4688 if (target->type->target_jim_configure) {
4689 /* target defines a configure function */
4690 /* target gets first dibs on parameters */
4691 e = (*(target->type->target_jim_configure))(target, goi);
4700 /* otherwise we 'continue' below */
4702 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
4704 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
4710 if (goi->isconfigure) {
4711 Jim_SetResultFormatted(goi->interp,
4712 "not settable: %s", n->name);
4716 if (goi->argc != 0) {
4717 Jim_WrongNumArgs(goi->interp,
4718 goi->argc, goi->argv,
4723 Jim_SetResultString(goi->interp,
4724 target_type_name(target), -1);
4728 if (goi->argc == 0) {
4729 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
4733 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
4735 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
4739 if (goi->isconfigure) {
4740 if (goi->argc != 1) {
4741 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
4745 if (goi->argc != 0) {
4746 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
4752 struct target_event_action *teap;
4754 teap = target->event_action;
4755 /* replace existing? */
4757 if (teap->event == (enum target_event)n->value)
4762 if (goi->isconfigure) {
4763 bool replace = true;
4766 teap = calloc(1, sizeof(*teap));
4769 teap->event = n->value;
4770 teap->interp = goi->interp;
4771 Jim_GetOpt_Obj(goi, &o);
4773 Jim_DecrRefCount(teap->interp, teap->body);
4774 teap->body = Jim_DuplicateObj(goi->interp, o);
4777 * Tcl/TK - "tk events" have a nice feature.
4778 * See the "BIND" command.
4779 * We should support that here.
4780 * You can specify %X and %Y in the event code.
4781 * The idea is: %T - target name.
4782 * The idea is: %N - target number
4783 * The idea is: %E - event name.
4785 Jim_IncrRefCount(teap->body);
4788 /* add to head of event list */
4789 teap->next = target->event_action;
4790 target->event_action = teap;
4792 Jim_SetEmptyResult(goi->interp);
4796 Jim_SetEmptyResult(goi->interp);
4798 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
4804 case TCFG_WORK_AREA_VIRT:
4805 if (goi->isconfigure) {
4806 target_free_all_working_areas(target);
4807 e = Jim_GetOpt_Wide(goi, &w);
4810 target->working_area_virt = w;
4811 target->working_area_virt_spec = true;
4816 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
4820 case TCFG_WORK_AREA_PHYS:
4821 if (goi->isconfigure) {
4822 target_free_all_working_areas(target);
4823 e = Jim_GetOpt_Wide(goi, &w);
4826 target->working_area_phys = w;
4827 target->working_area_phys_spec = true;
4832 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
4836 case TCFG_WORK_AREA_SIZE:
4837 if (goi->isconfigure) {
4838 target_free_all_working_areas(target);
4839 e = Jim_GetOpt_Wide(goi, &w);
4842 target->working_area_size = w;
4847 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
4851 case TCFG_WORK_AREA_BACKUP:
4852 if (goi->isconfigure) {
4853 target_free_all_working_areas(target);
4854 e = Jim_GetOpt_Wide(goi, &w);
4857 /* make this exactly 1 or 0 */
4858 target->backup_working_area = (!!w);
4863 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
4864 /* loop for more e*/
4869 if (goi->isconfigure) {
4870 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
4872 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
4875 target->endianness = n->value;
4880 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
4881 if (n->name == NULL) {
4882 target->endianness = TARGET_LITTLE_ENDIAN;
4883 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
4885 Jim_SetResultString(goi->interp, n->name, -1);
4890 if (goi->isconfigure) {
4891 e = Jim_GetOpt_Wide(goi, &w);
4894 target->coreid = (int32_t)w;
4899 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->coreid));
4903 case TCFG_CHAIN_POSITION:
4904 if (goi->isconfigure) {
4906 struct jtag_tap *tap;
4908 if (target->has_dap) {
4909 Jim_SetResultString(goi->interp,
4910 "target requires -dap parameter instead of -chain-position!", -1);
4914 target_free_all_working_areas(target);
4915 e = Jim_GetOpt_Obj(goi, &o_t);
4918 tap = jtag_tap_by_jim_obj(goi->interp, o_t);
4922 target->tap_configured = true;
4927 Jim_SetResultString(goi->interp, target->tap->dotted_name, -1);
4928 /* loop for more e*/
4931 if (goi->isconfigure) {
4932 e = Jim_GetOpt_Wide(goi, &w);
4935 target->dbgbase = (uint32_t)w;
4936 target->dbgbase_set = true;
4941 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->dbgbase));
4947 int result = rtos_create(goi, target);
4948 if (result != JIM_OK)
4954 case TCFG_DEFER_EXAMINE:
4956 target->defer_examine = true;
4961 if (goi->isconfigure) {
4962 struct command_context *cmd_ctx = current_command_context(goi->interp);
4963 if (cmd_ctx->mode != COMMAND_CONFIG) {
4964 Jim_SetResultString(goi->interp, "-gdb-port must be configured before 'init'", -1);
4969 e = Jim_GetOpt_String(goi, &s, NULL);
4972 target->gdb_port_override = strdup(s);
4977 Jim_SetResultString(goi->interp, target->gdb_port_override ? : "undefined", -1);
4981 case TCFG_GDB_MAX_CONNECTIONS:
4982 if (goi->isconfigure) {
4983 struct command_context *cmd_ctx = current_command_context(goi->interp);
4984 if (cmd_ctx->mode != COMMAND_CONFIG) {
4985 Jim_SetResultString(goi->interp, "-gdb-max-conenctions must be configured before 'init'", -1);
4989 e = Jim_GetOpt_Wide(goi, &w);
4992 target->gdb_max_connections = (w < 0) ? CONNECTION_LIMIT_UNLIMITED : (int)w;
4997 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->gdb_max_connections));
5000 } /* while (goi->argc) */
5003 /* done - we return */
5007 static int jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
5011 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5012 goi.isconfigure = !strcmp(Jim_GetString(argv[0], NULL), "configure");
5014 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
5015 "missing: -option ...");
5018 struct target *target = Jim_CmdPrivData(goi.interp);
5019 return target_configure(&goi, target);
5022 static int jim_target_mem2array(Jim_Interp *interp,
5023 int argc, Jim_Obj *const *argv)
5025 struct target *target = Jim_CmdPrivData(interp);
5026 return target_mem2array(interp, target, argc - 1, argv + 1);
5029 static int jim_target_array2mem(Jim_Interp *interp,
5030 int argc, Jim_Obj *const *argv)
5032 struct target *target = Jim_CmdPrivData(interp);
5033 return target_array2mem(interp, target, argc - 1, argv + 1);
5036 static int jim_target_tap_disabled(Jim_Interp *interp)
5038 Jim_SetResultFormatted(interp, "[TAP is disabled]");
5042 static int jim_target_examine(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5044 bool allow_defer = false;
5047 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5049 const char *cmd_name = Jim_GetString(argv[0], NULL);
5050 Jim_SetResultFormatted(goi.interp,
5051 "usage: %s ['allow-defer']", cmd_name);
5055 strcmp(Jim_GetString(argv[1], NULL), "allow-defer") == 0) {
5058 int e = Jim_GetOpt_Obj(&goi, &obj);
5064 struct target *target = Jim_CmdPrivData(interp);
5065 if (!target->tap->enabled)
5066 return jim_target_tap_disabled(interp);
5068 if (allow_defer && target->defer_examine) {
5069 LOG_INFO("Deferring arp_examine of %s", target_name(target));
5070 LOG_INFO("Use arp_examine command to examine it manually!");
5074 int e = target->type->examine(target);
5080 static int jim_target_was_examined(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
5082 struct target *target = Jim_CmdPrivData(interp);
5084 Jim_SetResultBool(interp, target_was_examined(target));
5088 static int jim_target_examine_deferred(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
5090 struct target *target = Jim_CmdPrivData(interp);
5092 Jim_SetResultBool(interp, target->defer_examine);
5096 static int jim_target_halt_gdb(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5099 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
5102 struct target *target = Jim_CmdPrivData(interp);
5104 if (target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT) != ERROR_OK)
5110 static int jim_target_poll(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5113 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
5116 struct target *target = Jim_CmdPrivData(interp);
5117 if (!target->tap->enabled)
5118 return jim_target_tap_disabled(interp);
5121 if (!(target_was_examined(target)))
5122 e = ERROR_TARGET_NOT_EXAMINED;
5124 e = target->type->poll(target);
5130 static int jim_target_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5133 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5135 if (goi.argc != 2) {
5136 Jim_WrongNumArgs(interp, 0, argv,
5137 "([tT]|[fF]|assert|deassert) BOOL");
5142 int e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
5144 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
5147 /* the halt or not param */
5149 e = Jim_GetOpt_Wide(&goi, &a);
5153 struct target *target = Jim_CmdPrivData(goi.interp);
5154 if (!target->tap->enabled)
5155 return jim_target_tap_disabled(interp);
5157 if (!target->type->assert_reset || !target->type->deassert_reset) {
5158 Jim_SetResultFormatted(interp,
5159 "No target-specific reset for %s",
5160 target_name(target));
5164 if (target->defer_examine)
5165 target_reset_examined(target);
5167 /* determine if we should halt or not. */
5168 target->reset_halt = !!a;
5169 /* When this happens - all workareas are invalid. */
5170 target_free_all_working_areas_restore(target, 0);
5173 if (n->value == NVP_ASSERT)
5174 e = target->type->assert_reset(target);
5176 e = target->type->deassert_reset(target);
5177 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
5180 static int jim_target_halt(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5183 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
5186 struct target *target = Jim_CmdPrivData(interp);
5187 if (!target->tap->enabled)
5188 return jim_target_tap_disabled(interp);
5189 int e = target->type->halt(target);
5190 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
5193 static int jim_target_wait_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5196 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5198 /* params: <name> statename timeoutmsecs */
5199 if (goi.argc != 2) {
5200 const char *cmd_name = Jim_GetString(argv[0], NULL);
5201 Jim_SetResultFormatted(goi.interp,
5202 "%s <state_name> <timeout_in_msec>", cmd_name);
5207 int e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
5209 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state, 1);
5213 e = Jim_GetOpt_Wide(&goi, &a);
5216 struct target *target = Jim_CmdPrivData(interp);
5217 if (!target->tap->enabled)
5218 return jim_target_tap_disabled(interp);
5220 e = target_wait_state(target, n->value, a);
5221 if (e != ERROR_OK) {
5222 Jim_Obj *eObj = Jim_NewIntObj(interp, e);
5223 Jim_SetResultFormatted(goi.interp,
5224 "target: %s wait %s fails (%#s) %s",
5225 target_name(target), n->name,
5226 eObj, target_strerror_safe(e));
5231 /* List for human, Events defined for this target.
5232 * scripts/programs should use 'name cget -event NAME'
5234 COMMAND_HANDLER(handle_target_event_list)
5236 struct target *target = get_current_target(CMD_CTX);
5237 struct target_event_action *teap = target->event_action;
5239 command_print(CMD, "Event actions for target (%d) %s\n",
5240 target->target_number,
5241 target_name(target));
5242 command_print(CMD, "%-25s | Body", "Event");
5243 command_print(CMD, "------------------------- | "
5244 "----------------------------------------");
5246 Jim_Nvp *opt = Jim_Nvp_value2name_simple(nvp_target_event, teap->event);
5247 command_print(CMD, "%-25s | %s",
5248 opt->name, Jim_GetString(teap->body, NULL));
5251 command_print(CMD, "***END***");
5254 static int jim_target_current_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5257 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
5260 struct target *target = Jim_CmdPrivData(interp);
5261 Jim_SetResultString(interp, target_state_name(target), -1);
5264 static int jim_target_invoke_event(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5267 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5268 if (goi.argc != 1) {
5269 const char *cmd_name = Jim_GetString(argv[0], NULL);
5270 Jim_SetResultFormatted(goi.interp, "%s <eventname>", cmd_name);
5274 int e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
5276 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
5279 struct target *target = Jim_CmdPrivData(interp);
5280 target_handle_event(target, n->value);
5284 static const struct command_registration target_instance_command_handlers[] = {
5286 .name = "configure",
5287 .mode = COMMAND_ANY,
5288 .jim_handler = jim_target_configure,
5289 .help = "configure a new target for use",
5290 .usage = "[target_attribute ...]",
5294 .mode = COMMAND_ANY,
5295 .jim_handler = jim_target_configure,
5296 .help = "returns the specified target attribute",
5297 .usage = "target_attribute",
5301 .handler = handle_mw_command,
5302 .mode = COMMAND_EXEC,
5303 .help = "Write 64-bit word(s) to target memory",
5304 .usage = "address data [count]",
5308 .handler = handle_mw_command,
5309 .mode = COMMAND_EXEC,
5310 .help = "Write 32-bit word(s) to target memory",
5311 .usage = "address data [count]",
5315 .handler = handle_mw_command,
5316 .mode = COMMAND_EXEC,
5317 .help = "Write 16-bit half-word(s) to target memory",
5318 .usage = "address data [count]",
5322 .handler = handle_mw_command,
5323 .mode = COMMAND_EXEC,
5324 .help = "Write byte(s) to target memory",
5325 .usage = "address data [count]",
5329 .handler = handle_md_command,
5330 .mode = COMMAND_EXEC,
5331 .help = "Display target memory as 64-bit words",
5332 .usage = "address [count]",
5336 .handler = handle_md_command,
5337 .mode = COMMAND_EXEC,
5338 .help = "Display target memory as 32-bit words",
5339 .usage = "address [count]",
5343 .handler = handle_md_command,
5344 .mode = COMMAND_EXEC,
5345 .help = "Display target memory as 16-bit half-words",
5346 .usage = "address [count]",
5350 .handler = handle_md_command,
5351 .mode = COMMAND_EXEC,
5352 .help = "Display target memory as 8-bit bytes",
5353 .usage = "address [count]",
5356 .name = "array2mem",
5357 .mode = COMMAND_EXEC,
5358 .jim_handler = jim_target_array2mem,
5359 .help = "Writes Tcl array of 8/16/32 bit numbers "
5361 .usage = "arrayname bitwidth address count",
5364 .name = "mem2array",
5365 .mode = COMMAND_EXEC,
5366 .jim_handler = jim_target_mem2array,
5367 .help = "Loads Tcl array of 8/16/32 bit numbers "
5368 "from target memory",
5369 .usage = "arrayname bitwidth address count",
5372 .name = "eventlist",
5373 .handler = handle_target_event_list,
5374 .mode = COMMAND_EXEC,
5375 .help = "displays a table of events defined for this target",
5380 .mode = COMMAND_EXEC,
5381 .jim_handler = jim_target_current_state,
5382 .help = "displays the current state of this target",
5385 .name = "arp_examine",
5386 .mode = COMMAND_EXEC,
5387 .jim_handler = jim_target_examine,
5388 .help = "used internally for reset processing",
5389 .usage = "['allow-defer']",
5392 .name = "was_examined",
5393 .mode = COMMAND_EXEC,
5394 .jim_handler = jim_target_was_examined,
5395 .help = "used internally for reset processing",
5398 .name = "examine_deferred",
5399 .mode = COMMAND_EXEC,
5400 .jim_handler = jim_target_examine_deferred,
5401 .help = "used internally for reset processing",
5404 .name = "arp_halt_gdb",
5405 .mode = COMMAND_EXEC,
5406 .jim_handler = jim_target_halt_gdb,
5407 .help = "used internally for reset processing to halt GDB",
5411 .mode = COMMAND_EXEC,
5412 .jim_handler = jim_target_poll,
5413 .help = "used internally for reset processing",
5416 .name = "arp_reset",
5417 .mode = COMMAND_EXEC,
5418 .jim_handler = jim_target_reset,
5419 .help = "used internally for reset processing",
5423 .mode = COMMAND_EXEC,
5424 .jim_handler = jim_target_halt,
5425 .help = "used internally for reset processing",
5428 .name = "arp_waitstate",
5429 .mode = COMMAND_EXEC,
5430 .jim_handler = jim_target_wait_state,
5431 .help = "used internally for reset processing",
5434 .name = "invoke-event",
5435 .mode = COMMAND_EXEC,
5436 .jim_handler = jim_target_invoke_event,
5437 .help = "invoke handler for specified event",
5438 .usage = "event_name",
5440 COMMAND_REGISTRATION_DONE
5443 static int target_create(Jim_GetOptInfo *goi)
5450 struct target *target;
5451 struct command_context *cmd_ctx;
5453 cmd_ctx = current_command_context(goi->interp);
5454 assert(cmd_ctx != NULL);
5456 if (goi->argc < 3) {
5457 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
5462 Jim_GetOpt_Obj(goi, &new_cmd);
5463 /* does this command exist? */
5464 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
5466 cp = Jim_GetString(new_cmd, NULL);
5467 Jim_SetResultFormatted(goi->interp, "Command/target: %s Exists", cp);
5472 e = Jim_GetOpt_String(goi, &cp, NULL);
5475 struct transport *tr = get_current_transport();
5476 if (tr->override_target) {
5477 e = tr->override_target(&cp);
5478 if (e != ERROR_OK) {
5479 LOG_ERROR("The selected transport doesn't support this target");
5482 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
5484 /* now does target type exist */
5485 for (x = 0 ; target_types[x] ; x++) {
5486 if (0 == strcmp(cp, target_types[x]->name)) {
5491 /* check for deprecated name */
5492 if (target_types[x]->deprecated_name) {
5493 if (0 == strcmp(cp, target_types[x]->deprecated_name)) {
5495 LOG_WARNING("target name is deprecated use: \'%s\'", target_types[x]->name);
5500 if (target_types[x] == NULL) {
5501 Jim_SetResultFormatted(goi->interp, "Unknown target type %s, try one of ", cp);
5502 for (x = 0 ; target_types[x] ; x++) {
5503 if (target_types[x + 1]) {
5504 Jim_AppendStrings(goi->interp,
5505 Jim_GetResult(goi->interp),
5506 target_types[x]->name,
5509 Jim_AppendStrings(goi->interp,
5510 Jim_GetResult(goi->interp),
5512 target_types[x]->name, NULL);
5519 target = calloc(1, sizeof(struct target));
5521 LOG_ERROR("Out of memory");
5525 /* set target number */
5526 target->target_number = new_target_number();
5528 /* allocate memory for each unique target type */
5529 target->type = malloc(sizeof(struct target_type));
5530 if (!target->type) {
5531 LOG_ERROR("Out of memory");
5536 memcpy(target->type, target_types[x], sizeof(struct target_type));
5538 /* will be set by "-endian" */
5539 target->endianness = TARGET_ENDIAN_UNKNOWN;
5541 /* default to first core, override with -coreid */
5544 target->working_area = 0x0;
5545 target->working_area_size = 0x0;
5546 target->working_areas = NULL;
5547 target->backup_working_area = 0;
5549 target->state = TARGET_UNKNOWN;
5550 target->debug_reason = DBG_REASON_UNDEFINED;
5551 target->reg_cache = NULL;
5552 target->breakpoints = NULL;
5553 target->watchpoints = NULL;
5554 target->next = NULL;
5555 target->arch_info = NULL;
5557 target->verbose_halt_msg = true;
5559 target->halt_issued = false;
5561 /* initialize trace information */
5562 target->trace_info = calloc(1, sizeof(struct trace));
5563 if (!target->trace_info) {
5564 LOG_ERROR("Out of memory");
5570 target->dbgmsg = NULL;
5571 target->dbg_msg_enabled = 0;
5573 target->endianness = TARGET_ENDIAN_UNKNOWN;
5575 target->rtos = NULL;
5576 target->rtos_auto_detect = false;
5578 target->gdb_port_override = NULL;
5579 target->gdb_max_connections = 1;
5581 /* Do the rest as "configure" options */
5582 goi->isconfigure = 1;
5583 e = target_configure(goi, target);
5586 if (target->has_dap) {
5587 if (!target->dap_configured) {
5588 Jim_SetResultString(goi->interp, "-dap ?name? required when creating target", -1);
5592 if (!target->tap_configured) {
5593 Jim_SetResultString(goi->interp, "-chain-position ?name? required when creating target", -1);
5597 /* tap must be set after target was configured */
5598 if (target->tap == NULL)
5603 rtos_destroy(target);
5604 free(target->gdb_port_override);
5605 free(target->trace_info);
5611 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
5612 /* default endian to little if not specified */
5613 target->endianness = TARGET_LITTLE_ENDIAN;
5616 cp = Jim_GetString(new_cmd, NULL);
5617 target->cmd_name = strdup(cp);
5618 if (!target->cmd_name) {
5619 LOG_ERROR("Out of memory");
5620 rtos_destroy(target);
5621 free(target->gdb_port_override);
5622 free(target->trace_info);
5628 if (target->type->target_create) {
5629 e = (*(target->type->target_create))(target, goi->interp);
5630 if (e != ERROR_OK) {
5631 LOG_DEBUG("target_create failed");
5632 free(target->cmd_name);
5633 rtos_destroy(target);
5634 free(target->gdb_port_override);
5635 free(target->trace_info);
5642 /* create the target specific commands */
5643 if (target->type->commands) {
5644 e = register_commands(cmd_ctx, NULL, target->type->commands);
5646 LOG_ERROR("unable to register '%s' commands", cp);
5649 /* now - create the new target name command */
5650 const struct command_registration target_subcommands[] = {
5652 .chain = target_instance_command_handlers,
5655 .chain = target->type->commands,
5657 COMMAND_REGISTRATION_DONE
5659 const struct command_registration target_commands[] = {
5662 .mode = COMMAND_ANY,
5663 .help = "target command group",
5665 .chain = target_subcommands,
5667 COMMAND_REGISTRATION_DONE
5669 e = register_commands(cmd_ctx, NULL, target_commands);
5670 if (e != ERROR_OK) {
5671 if (target->type->deinit_target)
5672 target->type->deinit_target(target);
5673 free(target->cmd_name);
5674 rtos_destroy(target);
5675 free(target->gdb_port_override);
5676 free(target->trace_info);
5682 struct command *c = command_find_in_context(cmd_ctx, cp);
5684 command_set_handler_data(c, target);
5686 /* append to end of list */
5687 append_to_list_all_targets(target);
5689 cmd_ctx->current_target = target;
5693 static int jim_target_current(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5696 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5699 struct command_context *cmd_ctx = current_command_context(interp);
5700 assert(cmd_ctx != NULL);
5702 struct target *target = get_current_target_or_null(cmd_ctx);
5704 Jim_SetResultString(interp, target_name(target), -1);
5708 static int jim_target_types(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5711 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5714 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
5715 for (unsigned x = 0; NULL != target_types[x]; x++) {
5716 Jim_ListAppendElement(interp, Jim_GetResult(interp),
5717 Jim_NewStringObj(interp, target_types[x]->name, -1));
5722 static int jim_target_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5725 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5728 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
5729 struct target *target = all_targets;
5731 Jim_ListAppendElement(interp, Jim_GetResult(interp),
5732 Jim_NewStringObj(interp, target_name(target), -1));
5733 target = target->next;
5738 static int jim_target_smp(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5741 const char *targetname;
5743 struct target *target = (struct target *) NULL;
5744 struct target_list *head, *curr, *new;
5745 curr = (struct target_list *) NULL;
5746 head = (struct target_list *) NULL;
5749 LOG_DEBUG("%d", argc);
5750 /* argv[1] = target to associate in smp
5751 * argv[2] = target to associate in smp
5755 for (i = 1; i < argc; i++) {
5757 targetname = Jim_GetString(argv[i], &len);
5758 target = get_target(targetname);
5759 LOG_DEBUG("%s ", targetname);
5761 new = malloc(sizeof(struct target_list));
5762 new->target = target;
5763 new->next = (struct target_list *)NULL;
5764 if (head == (struct target_list *)NULL) {
5773 /* now parse the list of cpu and put the target in smp mode*/
5776 while (curr != (struct target_list *)NULL) {
5777 target = curr->target;
5779 target->head = head;
5783 if (target && target->rtos)
5784 retval = rtos_smp_init(head->target);
5790 static int jim_target_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5793 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5795 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
5796 "<name> <target_type> [<target_options> ...]");
5799 return target_create(&goi);
5802 static const struct command_registration target_subcommand_handlers[] = {
5805 .mode = COMMAND_CONFIG,
5806 .handler = handle_target_init_command,
5807 .help = "initialize targets",
5812 .mode = COMMAND_CONFIG,
5813 .jim_handler = jim_target_create,
5814 .usage = "name type '-chain-position' name [options ...]",
5815 .help = "Creates and selects a new target",
5819 .mode = COMMAND_ANY,
5820 .jim_handler = jim_target_current,
5821 .help = "Returns the currently selected target",
5825 .mode = COMMAND_ANY,
5826 .jim_handler = jim_target_types,
5827 .help = "Returns the available target types as "
5828 "a list of strings",
5832 .mode = COMMAND_ANY,
5833 .jim_handler = jim_target_names,
5834 .help = "Returns the names of all targets as a list of strings",
5838 .mode = COMMAND_ANY,
5839 .jim_handler = jim_target_smp,
5840 .usage = "targetname1 targetname2 ...",
5841 .help = "gather several target in a smp list"
5844 COMMAND_REGISTRATION_DONE
5848 target_addr_t address;
5854 static int fastload_num;
5855 static struct FastLoad *fastload;
5857 static void free_fastload(void)
5859 if (fastload != NULL) {
5860 for (int i = 0; i < fastload_num; i++)
5861 free(fastload[i].data);
5867 COMMAND_HANDLER(handle_fast_load_image_command)
5871 uint32_t image_size;
5872 target_addr_t min_address = 0;
5873 target_addr_t max_address = -1;
5877 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
5878 &image, &min_address, &max_address);
5879 if (ERROR_OK != retval)
5882 struct duration bench;
5883 duration_start(&bench);
5885 retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL);
5886 if (retval != ERROR_OK)
5891 fastload_num = image.num_sections;
5892 fastload = malloc(sizeof(struct FastLoad)*image.num_sections);
5893 if (fastload == NULL) {
5894 command_print(CMD, "out of memory");
5895 image_close(&image);
5898 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
5899 for (unsigned int i = 0; i < image.num_sections; i++) {
5900 buffer = malloc(image.sections[i].size);
5901 if (buffer == NULL) {
5902 command_print(CMD, "error allocating buffer for section (%d bytes)",
5903 (int)(image.sections[i].size));
5904 retval = ERROR_FAIL;
5908 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
5909 if (retval != ERROR_OK) {
5914 uint32_t offset = 0;
5915 uint32_t length = buf_cnt;
5917 /* DANGER!!! beware of unsigned comparison here!!! */
5919 if ((image.sections[i].base_address + buf_cnt >= min_address) &&
5920 (image.sections[i].base_address < max_address)) {
5921 if (image.sections[i].base_address < min_address) {
5922 /* clip addresses below */
5923 offset += min_address-image.sections[i].base_address;
5927 if (image.sections[i].base_address + buf_cnt > max_address)
5928 length -= (image.sections[i].base_address + buf_cnt)-max_address;
5930 fastload[i].address = image.sections[i].base_address + offset;
5931 fastload[i].data = malloc(length);
5932 if (fastload[i].data == NULL) {
5934 command_print(CMD, "error allocating buffer for section (%" PRIu32 " bytes)",
5936 retval = ERROR_FAIL;
5939 memcpy(fastload[i].data, buffer + offset, length);
5940 fastload[i].length = length;
5942 image_size += length;
5943 command_print(CMD, "%u bytes written at address 0x%8.8x",
5944 (unsigned int)length,
5945 ((unsigned int)(image.sections[i].base_address + offset)));
5951 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
5952 command_print(CMD, "Loaded %" PRIu32 " bytes "
5953 "in %fs (%0.3f KiB/s)", image_size,
5954 duration_elapsed(&bench), duration_kbps(&bench, image_size));
5957 "WARNING: image has not been loaded to target!"
5958 "You can issue a 'fast_load' to finish loading.");
5961 image_close(&image);
5963 if (retval != ERROR_OK)
5969 COMMAND_HANDLER(handle_fast_load_command)
5972 return ERROR_COMMAND_SYNTAX_ERROR;
5973 if (fastload == NULL) {
5974 LOG_ERROR("No image in memory");
5978 int64_t ms = timeval_ms();
5980 int retval = ERROR_OK;
5981 for (i = 0; i < fastload_num; i++) {
5982 struct target *target = get_current_target(CMD_CTX);
5983 command_print(CMD, "Write to 0x%08x, length 0x%08x",
5984 (unsigned int)(fastload[i].address),
5985 (unsigned int)(fastload[i].length));
5986 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
5987 if (retval != ERROR_OK)
5989 size += fastload[i].length;
5991 if (retval == ERROR_OK) {
5992 int64_t after = timeval_ms();
5993 command_print(CMD, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
5998 static const struct command_registration target_command_handlers[] = {
6001 .handler = handle_targets_command,
6002 .mode = COMMAND_ANY,
6003 .help = "change current default target (one parameter) "
6004 "or prints table of all targets (no parameters)",
6005 .usage = "[target]",
6009 .mode = COMMAND_CONFIG,
6010 .help = "configure target",
6011 .chain = target_subcommand_handlers,
6014 COMMAND_REGISTRATION_DONE
6017 int target_register_commands(struct command_context *cmd_ctx)
6019 return register_commands(cmd_ctx, NULL, target_command_handlers);
6022 static bool target_reset_nag = true;
6024 bool get_target_reset_nag(void)
6026 return target_reset_nag;
6029 COMMAND_HANDLER(handle_target_reset_nag)
6031 return CALL_COMMAND_HANDLER(handle_command_parse_bool,
6032 &target_reset_nag, "Nag after each reset about options to improve "
6036 COMMAND_HANDLER(handle_ps_command)
6038 struct target *target = get_current_target(CMD_CTX);
6040 if (target->state != TARGET_HALTED) {
6041 LOG_INFO("target not halted !!");
6045 if ((target->rtos) && (target->rtos->type)
6046 && (target->rtos->type->ps_command)) {
6047 display = target->rtos->type->ps_command(target);
6048 command_print(CMD, "%s", display);
6053 return ERROR_TARGET_FAILURE;
6057 static void binprint(struct command_invocation *cmd, const char *text, const uint8_t *buf, int size)
6060 command_print_sameline(cmd, "%s", text);
6061 for (int i = 0; i < size; i++)
6062 command_print_sameline(cmd, " %02x", buf[i]);
6063 command_print(cmd, " ");
6066 COMMAND_HANDLER(handle_test_mem_access_command)
6068 struct target *target = get_current_target(CMD_CTX);
6070 int retval = ERROR_OK;
6072 if (target->state != TARGET_HALTED) {
6073 LOG_INFO("target not halted !!");
6078 return ERROR_COMMAND_SYNTAX_ERROR;
6080 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], test_size);
6083 size_t num_bytes = test_size + 4;
6085 struct working_area *wa = NULL;
6086 retval = target_alloc_working_area(target, num_bytes, &wa);
6087 if (retval != ERROR_OK) {
6088 LOG_ERROR("Not enough working area");
6092 uint8_t *test_pattern = malloc(num_bytes);
6094 for (size_t i = 0; i < num_bytes; i++)
6095 test_pattern[i] = rand();
6097 retval = target_write_memory(target, wa->address, 1, num_bytes, test_pattern);
6098 if (retval != ERROR_OK) {
6099 LOG_ERROR("Test pattern write failed");
6103 for (int host_offset = 0; host_offset <= 1; host_offset++) {
6104 for (int size = 1; size <= 4; size *= 2) {
6105 for (int offset = 0; offset < 4; offset++) {
6106 uint32_t count = test_size / size;
6107 size_t host_bufsiz = (count + 2) * size + host_offset;
6108 uint8_t *read_ref = malloc(host_bufsiz);
6109 uint8_t *read_buf = malloc(host_bufsiz);
6111 for (size_t i = 0; i < host_bufsiz; i++) {
6112 read_ref[i] = rand();
6113 read_buf[i] = read_ref[i];
6115 command_print_sameline(CMD,
6116 "Test read %" PRIu32 " x %d @ %d to %saligned buffer: ", count,
6117 size, offset, host_offset ? "un" : "");
6119 struct duration bench;
6120 duration_start(&bench);
6122 retval = target_read_memory(target, wa->address + offset, size, count,
6123 read_buf + size + host_offset);
6125 duration_measure(&bench);
6127 if (retval == ERROR_TARGET_UNALIGNED_ACCESS) {
6128 command_print(CMD, "Unsupported alignment");
6130 } else if (retval != ERROR_OK) {
6131 command_print(CMD, "Memory read failed");
6135 /* replay on host */
6136 memcpy(read_ref + size + host_offset, test_pattern + offset, count * size);
6139 int result = memcmp(read_ref, read_buf, host_bufsiz);
6141 command_print(CMD, "Pass in %fs (%0.3f KiB/s)",
6142 duration_elapsed(&bench),
6143 duration_kbps(&bench, count * size));
6145 command_print(CMD, "Compare failed");
6146 binprint(CMD, "ref:", read_ref, host_bufsiz);
6147 binprint(CMD, "buf:", read_buf, host_bufsiz);
6160 target_free_working_area(target, wa);
6163 num_bytes = test_size + 4 + 4 + 4;
6165 retval = target_alloc_working_area(target, num_bytes, &wa);
6166 if (retval != ERROR_OK) {
6167 LOG_ERROR("Not enough working area");
6171 test_pattern = malloc(num_bytes);
6173 for (size_t i = 0; i < num_bytes; i++)
6174 test_pattern[i] = rand();
6176 for (int host_offset = 0; host_offset <= 1; host_offset++) {
6177 for (int size = 1; size <= 4; size *= 2) {
6178 for (int offset = 0; offset < 4; offset++) {
6179 uint32_t count = test_size / size;
6180 size_t host_bufsiz = count * size + host_offset;
6181 uint8_t *read_ref = malloc(num_bytes);
6182 uint8_t *read_buf = malloc(num_bytes);
6183 uint8_t *write_buf = malloc(host_bufsiz);
6185 for (size_t i = 0; i < host_bufsiz; i++)
6186 write_buf[i] = rand();
6187 command_print_sameline(CMD,
6188 "Test write %" PRIu32 " x %d @ %d from %saligned buffer: ", count,
6189 size, offset, host_offset ? "un" : "");
6191 retval = target_write_memory(target, wa->address, 1, num_bytes, test_pattern);
6192 if (retval != ERROR_OK) {
6193 command_print(CMD, "Test pattern write failed");
6197 /* replay on host */
6198 memcpy(read_ref, test_pattern, num_bytes);
6199 memcpy(read_ref + size + offset, write_buf + host_offset, count * size);
6201 struct duration bench;
6202 duration_start(&bench);
6204 retval = target_write_memory(target, wa->address + size + offset, size, count,
6205 write_buf + host_offset);
6207 duration_measure(&bench);
6209 if (retval == ERROR_TARGET_UNALIGNED_ACCESS) {
6210 command_print(CMD, "Unsupported alignment");
6212 } else if (retval != ERROR_OK) {
6213 command_print(CMD, "Memory write failed");
6218 retval = target_read_memory(target, wa->address, 1, num_bytes, read_buf);
6219 if (retval != ERROR_OK) {
6220 command_print(CMD, "Test pattern write failed");
6225 int result = memcmp(read_ref, read_buf, num_bytes);
6227 command_print(CMD, "Pass in %fs (%0.3f KiB/s)",
6228 duration_elapsed(&bench),
6229 duration_kbps(&bench, count * size));
6231 command_print(CMD, "Compare failed");
6232 binprint(CMD, "ref:", read_ref, num_bytes);
6233 binprint(CMD, "buf:", read_buf, num_bytes);
6245 target_free_working_area(target, wa);
6249 static const struct command_registration target_exec_command_handlers[] = {
6251 .name = "fast_load_image",
6252 .handler = handle_fast_load_image_command,
6253 .mode = COMMAND_ANY,
6254 .help = "Load image into server memory for later use by "
6255 "fast_load; primarily for profiling",
6256 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
6257 "[min_address [max_length]]",
6260 .name = "fast_load",
6261 .handler = handle_fast_load_command,
6262 .mode = COMMAND_EXEC,
6263 .help = "loads active fast load image to current target "
6264 "- mainly for profiling purposes",
6269 .handler = handle_profile_command,
6270 .mode = COMMAND_EXEC,
6271 .usage = "seconds filename [start end]",
6272 .help = "profiling samples the CPU PC",
6274 /** @todo don't register virt2phys() unless target supports it */
6276 .name = "virt2phys",
6277 .handler = handle_virt2phys_command,
6278 .mode = COMMAND_ANY,
6279 .help = "translate a virtual address into a physical address",
6280 .usage = "virtual_address",
6284 .handler = handle_reg_command,
6285 .mode = COMMAND_EXEC,
6286 .help = "display (reread from target with \"force\") or set a register; "
6287 "with no arguments, displays all registers and their values",
6288 .usage = "[(register_number|register_name) [(value|'force')]]",
6292 .handler = handle_poll_command,
6293 .mode = COMMAND_EXEC,
6294 .help = "poll target state; or reconfigure background polling",
6295 .usage = "['on'|'off']",
6298 .name = "wait_halt",
6299 .handler = handle_wait_halt_command,
6300 .mode = COMMAND_EXEC,
6301 .help = "wait up to the specified number of milliseconds "
6302 "(default 5000) for a previously requested halt",
6303 .usage = "[milliseconds]",
6307 .handler = handle_halt_command,
6308 .mode = COMMAND_EXEC,
6309 .help = "request target to halt, then wait up to the specified "
6310 "number of milliseconds (default 5000) for it to complete",
6311 .usage = "[milliseconds]",
6315 .handler = handle_resume_command,
6316 .mode = COMMAND_EXEC,
6317 .help = "resume target execution from current PC or address",
6318 .usage = "[address]",
6322 .handler = handle_reset_command,
6323 .mode = COMMAND_EXEC,
6324 .usage = "[run|halt|init]",
6325 .help = "Reset all targets into the specified mode. "
6326 "Default reset mode is run, if not given.",
6329 .name = "soft_reset_halt",
6330 .handler = handle_soft_reset_halt_command,
6331 .mode = COMMAND_EXEC,
6333 .help = "halt the target and do a soft reset",
6337 .handler = handle_step_command,
6338 .mode = COMMAND_EXEC,
6339 .help = "step one instruction from current PC or address",
6340 .usage = "[address]",
6344 .handler = handle_md_command,
6345 .mode = COMMAND_EXEC,
6346 .help = "display memory double-words",
6347 .usage = "['phys'] address [count]",
6351 .handler = handle_md_command,
6352 .mode = COMMAND_EXEC,
6353 .help = "display memory words",
6354 .usage = "['phys'] address [count]",
6358 .handler = handle_md_command,
6359 .mode = COMMAND_EXEC,
6360 .help = "display memory half-words",
6361 .usage = "['phys'] address [count]",
6365 .handler = handle_md_command,
6366 .mode = COMMAND_EXEC,
6367 .help = "display memory bytes",
6368 .usage = "['phys'] address [count]",
6372 .handler = handle_mw_command,
6373 .mode = COMMAND_EXEC,
6374 .help = "write memory double-word",
6375 .usage = "['phys'] address value [count]",
6379 .handler = handle_mw_command,
6380 .mode = COMMAND_EXEC,
6381 .help = "write memory word",
6382 .usage = "['phys'] address value [count]",
6386 .handler = handle_mw_command,
6387 .mode = COMMAND_EXEC,
6388 .help = "write memory half-word",
6389 .usage = "['phys'] address value [count]",
6393 .handler = handle_mw_command,
6394 .mode = COMMAND_EXEC,
6395 .help = "write memory byte",
6396 .usage = "['phys'] address value [count]",
6400 .handler = handle_bp_command,
6401 .mode = COMMAND_EXEC,
6402 .help = "list or set hardware or software breakpoint",
6403 .usage = "[<address> [<asid>] <length> ['hw'|'hw_ctx']]",
6407 .handler = handle_rbp_command,
6408 .mode = COMMAND_EXEC,
6409 .help = "remove breakpoint",
6410 .usage = "'all' | address",
6414 .handler = handle_wp_command,
6415 .mode = COMMAND_EXEC,
6416 .help = "list (no params) or create watchpoints",
6417 .usage = "[address length [('r'|'w'|'a') value [mask]]]",
6421 .handler = handle_rwp_command,
6422 .mode = COMMAND_EXEC,
6423 .help = "remove watchpoint",
6427 .name = "load_image",
6428 .handler = handle_load_image_command,
6429 .mode = COMMAND_EXEC,
6430 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
6431 "[min_address] [max_length]",
6434 .name = "dump_image",
6435 .handler = handle_dump_image_command,
6436 .mode = COMMAND_EXEC,
6437 .usage = "filename address size",
6440 .name = "verify_image_checksum",
6441 .handler = handle_verify_image_checksum_command,
6442 .mode = COMMAND_EXEC,
6443 .usage = "filename [offset [type]]",
6446 .name = "verify_image",
6447 .handler = handle_verify_image_command,
6448 .mode = COMMAND_EXEC,
6449 .usage = "filename [offset [type]]",
6452 .name = "test_image",
6453 .handler = handle_test_image_command,
6454 .mode = COMMAND_EXEC,
6455 .usage = "filename [offset [type]]",
6458 .name = "mem2array",
6459 .mode = COMMAND_EXEC,
6460 .jim_handler = jim_mem2array,
6461 .help = "read 8/16/32 bit memory and return as a TCL array "
6462 "for script processing",
6463 .usage = "arrayname bitwidth address count",
6466 .name = "array2mem",
6467 .mode = COMMAND_EXEC,
6468 .jim_handler = jim_array2mem,
6469 .help = "convert a TCL array to memory locations "
6470 "and write the 8/16/32 bit values",
6471 .usage = "arrayname bitwidth address count",
6474 .name = "reset_nag",
6475 .handler = handle_target_reset_nag,
6476 .mode = COMMAND_ANY,
6477 .help = "Nag after each reset about options that could have been "
6478 "enabled to improve performance. ",
6479 .usage = "['enable'|'disable']",
6483 .handler = handle_ps_command,
6484 .mode = COMMAND_EXEC,
6485 .help = "list all tasks ",
6489 .name = "test_mem_access",
6490 .handler = handle_test_mem_access_command,
6491 .mode = COMMAND_EXEC,
6492 .help = "Test the target's memory access functions",
6496 COMMAND_REGISTRATION_DONE
6498 static int target_register_user_commands(struct command_context *cmd_ctx)
6500 int retval = ERROR_OK;
6501 retval = target_request_register_commands(cmd_ctx);
6502 if (retval != ERROR_OK)
6505 retval = trace_register_commands(cmd_ctx);
6506 if (retval != ERROR_OK)
6510 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);