1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 /***************************************************************************
4 * Copyright (C) 2005 by Dominic Rath *
5 * Dominic.Rath@gmx.de *
7 * Copyright (C) 2007-2010 Øyvind Harboe *
8 * oyvind.harboe@zylin.com *
10 * Copyright (C) 2008, Duane Ellis *
11 * openocd@duaneeellis.com *
13 * Copyright (C) 2008 by Spencer Oliver *
14 * spen@spen-soft.co.uk *
16 * Copyright (C) 2008 by Rick Altherr *
17 * kc8apf@kc8apf.net> *
19 * Copyright (C) 2011 by Broadcom Corporation *
20 * Evan Hunter - ehunter@broadcom.com *
22 * Copyright (C) ST-Ericsson SA 2011 *
23 * michel.jaouen@stericsson.com : smp minimum support *
25 * Copyright (C) 2011 Andreas Fritiofson *
26 * andreas.fritiofson@gmail.com *
27 ***************************************************************************/
33 #include <helper/align.h>
34 #include <helper/time_support.h>
35 #include <jtag/jtag.h>
36 #include <flash/nor/core.h>
39 #include "target_type.h"
40 #include "target_request.h"
41 #include "breakpoints.h"
45 #include "rtos/rtos.h"
46 #include "transport/transport.h"
49 #include "semihosting_common.h"
51 /* default halt wait timeout (ms) */
52 #define DEFAULT_HALT_TIMEOUT 5000
54 static int target_read_buffer_default(struct target *target, target_addr_t address,
55 uint32_t count, uint8_t *buffer);
56 static int target_write_buffer_default(struct target *target, target_addr_t address,
57 uint32_t count, const uint8_t *buffer);
58 static int target_array2mem(Jim_Interp *interp, struct target *target,
59 int argc, Jim_Obj * const *argv);
60 static int target_mem2array(Jim_Interp *interp, struct target *target,
61 int argc, Jim_Obj * const *argv);
62 static int target_register_user_commands(struct command_context *cmd_ctx);
63 static int target_get_gdb_fileio_info_default(struct target *target,
64 struct gdb_fileio_info *fileio_info);
65 static int target_gdb_fileio_end_default(struct target *target, int retcode,
66 int fileio_errno, bool ctrl_c);
69 extern struct target_type arm7tdmi_target;
70 extern struct target_type arm720t_target;
71 extern struct target_type arm9tdmi_target;
72 extern struct target_type arm920t_target;
73 extern struct target_type arm966e_target;
74 extern struct target_type arm946e_target;
75 extern struct target_type arm926ejs_target;
76 extern struct target_type fa526_target;
77 extern struct target_type feroceon_target;
78 extern struct target_type dragonite_target;
79 extern struct target_type xscale_target;
80 extern struct target_type cortexm_target;
81 extern struct target_type cortexa_target;
82 extern struct target_type aarch64_target;
83 extern struct target_type cortexr4_target;
84 extern struct target_type arm11_target;
85 extern struct target_type ls1_sap_target;
86 extern struct target_type mips_m4k_target;
87 extern struct target_type mips_mips64_target;
88 extern struct target_type avr_target;
89 extern struct target_type dsp563xx_target;
90 extern struct target_type dsp5680xx_target;
91 extern struct target_type testee_target;
92 extern struct target_type avr32_ap7k_target;
93 extern struct target_type hla_target;
94 extern struct target_type nds32_v2_target;
95 extern struct target_type nds32_v3_target;
96 extern struct target_type nds32_v3m_target;
97 extern struct target_type esp32_target;
98 extern struct target_type esp32s2_target;
99 extern struct target_type esp32s3_target;
100 extern struct target_type or1k_target;
101 extern struct target_type quark_x10xx_target;
102 extern struct target_type quark_d20xx_target;
103 extern struct target_type stm8_target;
104 extern struct target_type riscv_target;
105 extern struct target_type mem_ap_target;
106 extern struct target_type esirisc_target;
107 extern struct target_type arcv2_target;
109 static struct target_type *target_types[] = {
152 struct target *all_targets;
153 static struct target_event_callback *target_event_callbacks;
154 static struct target_timer_callback *target_timer_callbacks;
155 static int64_t target_timer_next_event_value;
156 static LIST_HEAD(target_reset_callback_list);
157 static LIST_HEAD(target_trace_callback_list);
158 static const int polling_interval = TARGET_DEFAULT_POLLING_INTERVAL;
159 static LIST_HEAD(empty_smp_targets);
161 static const struct 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 struct 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)
188 const struct jim_nvp *n;
190 n = jim_nvp_value2name_simple(nvp_error_target, err);
197 static const struct 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 { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0x100, .name = "semihosting-user-cmd-0x100" },
238 { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0x101, .name = "semihosting-user-cmd-0x101" },
239 { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0x102, .name = "semihosting-user-cmd-0x102" },
240 { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0x103, .name = "semihosting-user-cmd-0x103" },
241 { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0x104, .name = "semihosting-user-cmd-0x104" },
242 { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0x105, .name = "semihosting-user-cmd-0x105" },
243 { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0x106, .name = "semihosting-user-cmd-0x106" },
244 { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0x107, .name = "semihosting-user-cmd-0x107" },
246 { .name = NULL, .value = -1 }
249 static const struct jim_nvp nvp_target_state[] = {
250 { .name = "unknown", .value = TARGET_UNKNOWN },
251 { .name = "running", .value = TARGET_RUNNING },
252 { .name = "halted", .value = TARGET_HALTED },
253 { .name = "reset", .value = TARGET_RESET },
254 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
255 { .name = NULL, .value = -1 },
258 static const struct jim_nvp nvp_target_debug_reason[] = {
259 { .name = "debug-request", .value = DBG_REASON_DBGRQ },
260 { .name = "breakpoint", .value = DBG_REASON_BREAKPOINT },
261 { .name = "watchpoint", .value = DBG_REASON_WATCHPOINT },
262 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
263 { .name = "single-step", .value = DBG_REASON_SINGLESTEP },
264 { .name = "target-not-halted", .value = DBG_REASON_NOTHALTED },
265 { .name = "program-exit", .value = DBG_REASON_EXIT },
266 { .name = "exception-catch", .value = DBG_REASON_EXC_CATCH },
267 { .name = "undefined", .value = DBG_REASON_UNDEFINED },
268 { .name = NULL, .value = -1 },
271 static const struct jim_nvp nvp_target_endian[] = {
272 { .name = "big", .value = TARGET_BIG_ENDIAN },
273 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
274 { .name = "be", .value = TARGET_BIG_ENDIAN },
275 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
276 { .name = NULL, .value = -1 },
279 static const struct jim_nvp nvp_reset_modes[] = {
280 { .name = "unknown", .value = RESET_UNKNOWN },
281 { .name = "run", .value = RESET_RUN },
282 { .name = "halt", .value = RESET_HALT },
283 { .name = "init", .value = RESET_INIT },
284 { .name = NULL, .value = -1 },
287 const char *debug_reason_name(struct target *t)
291 cp = jim_nvp_value2name_simple(nvp_target_debug_reason,
292 t->debug_reason)->name;
294 LOG_ERROR("Invalid debug reason: %d", (int)(t->debug_reason));
295 cp = "(*BUG*unknown*BUG*)";
300 const char *target_state_name(struct target *t)
303 cp = jim_nvp_value2name_simple(nvp_target_state, t->state)->name;
305 LOG_ERROR("Invalid target state: %d", (int)(t->state));
306 cp = "(*BUG*unknown*BUG*)";
309 if (!target_was_examined(t) && t->defer_examine)
310 cp = "examine deferred";
315 const char *target_event_name(enum target_event event)
318 cp = jim_nvp_value2name_simple(nvp_target_event, event)->name;
320 LOG_ERROR("Invalid target event: %d", (int)(event));
321 cp = "(*BUG*unknown*BUG*)";
326 const char *target_reset_mode_name(enum target_reset_mode reset_mode)
329 cp = jim_nvp_value2name_simple(nvp_reset_modes, reset_mode)->name;
331 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode));
332 cp = "(*BUG*unknown*BUG*)";
337 /* determine the number of the new target */
338 static int new_target_number(void)
343 /* number is 0 based */
347 if (x < t->target_number)
348 x = t->target_number;
354 static void append_to_list_all_targets(struct target *target)
356 struct target **t = &all_targets;
363 /* read a uint64_t from a buffer in target memory endianness */
364 uint64_t target_buffer_get_u64(struct target *target, const uint8_t *buffer)
366 if (target->endianness == TARGET_LITTLE_ENDIAN)
367 return le_to_h_u64(buffer);
369 return be_to_h_u64(buffer);
372 /* read a uint32_t from a buffer in target memory endianness */
373 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
375 if (target->endianness == TARGET_LITTLE_ENDIAN)
376 return le_to_h_u32(buffer);
378 return be_to_h_u32(buffer);
381 /* read a uint24_t from a buffer in target memory endianness */
382 uint32_t target_buffer_get_u24(struct target *target, const uint8_t *buffer)
384 if (target->endianness == TARGET_LITTLE_ENDIAN)
385 return le_to_h_u24(buffer);
387 return be_to_h_u24(buffer);
390 /* read a uint16_t from a buffer in target memory endianness */
391 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
393 if (target->endianness == TARGET_LITTLE_ENDIAN)
394 return le_to_h_u16(buffer);
396 return be_to_h_u16(buffer);
399 /* write a uint64_t to a buffer in target memory endianness */
400 void target_buffer_set_u64(struct target *target, uint8_t *buffer, uint64_t value)
402 if (target->endianness == TARGET_LITTLE_ENDIAN)
403 h_u64_to_le(buffer, value);
405 h_u64_to_be(buffer, value);
408 /* write a uint32_t to a buffer in target memory endianness */
409 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
411 if (target->endianness == TARGET_LITTLE_ENDIAN)
412 h_u32_to_le(buffer, value);
414 h_u32_to_be(buffer, value);
417 /* write a uint24_t to a buffer in target memory endianness */
418 void target_buffer_set_u24(struct target *target, uint8_t *buffer, uint32_t value)
420 if (target->endianness == TARGET_LITTLE_ENDIAN)
421 h_u24_to_le(buffer, value);
423 h_u24_to_be(buffer, value);
426 /* write a uint16_t to a buffer in target memory endianness */
427 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
429 if (target->endianness == TARGET_LITTLE_ENDIAN)
430 h_u16_to_le(buffer, value);
432 h_u16_to_be(buffer, value);
435 /* write a uint8_t to a buffer in target memory endianness */
436 static void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
441 /* write a uint64_t array to a buffer in target memory endianness */
442 void target_buffer_get_u64_array(struct target *target, const uint8_t *buffer, uint32_t count, uint64_t *dstbuf)
445 for (i = 0; i < count; i++)
446 dstbuf[i] = target_buffer_get_u64(target, &buffer[i * 8]);
449 /* write a uint32_t array to a buffer in target memory endianness */
450 void target_buffer_get_u32_array(struct target *target, const uint8_t *buffer, uint32_t count, uint32_t *dstbuf)
453 for (i = 0; i < count; i++)
454 dstbuf[i] = target_buffer_get_u32(target, &buffer[i * 4]);
457 /* write a uint16_t array to a buffer in target memory endianness */
458 void target_buffer_get_u16_array(struct target *target, const uint8_t *buffer, uint32_t count, uint16_t *dstbuf)
461 for (i = 0; i < count; i++)
462 dstbuf[i] = target_buffer_get_u16(target, &buffer[i * 2]);
465 /* write a uint64_t array to a buffer in target memory endianness */
466 void target_buffer_set_u64_array(struct target *target, uint8_t *buffer, uint32_t count, const uint64_t *srcbuf)
469 for (i = 0; i < count; i++)
470 target_buffer_set_u64(target, &buffer[i * 8], srcbuf[i]);
473 /* write a uint32_t array to a buffer in target memory endianness */
474 void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, const uint32_t *srcbuf)
477 for (i = 0; i < count; i++)
478 target_buffer_set_u32(target, &buffer[i * 4], srcbuf[i]);
481 /* write a uint16_t array to a buffer in target memory endianness */
482 void target_buffer_set_u16_array(struct target *target, uint8_t *buffer, uint32_t count, const uint16_t *srcbuf)
485 for (i = 0; i < count; i++)
486 target_buffer_set_u16(target, &buffer[i * 2], srcbuf[i]);
489 /* return a pointer to a configured target; id is name or number */
490 struct target *get_target(const char *id)
492 struct target *target;
494 /* try as tcltarget name */
495 for (target = all_targets; target; target = target->next) {
496 if (!target_name(target))
498 if (strcmp(id, target_name(target)) == 0)
502 /* It's OK to remove this fallback sometime after August 2010 or so */
504 /* no match, try as number */
506 if (parse_uint(id, &num) != ERROR_OK)
509 for (target = all_targets; target; target = target->next) {
510 if (target->target_number == (int)num) {
511 LOG_WARNING("use '%s' as target identifier, not '%u'",
512 target_name(target), num);
520 /* returns a pointer to the n-th configured target */
521 struct target *get_target_by_num(int num)
523 struct target *target = all_targets;
526 if (target->target_number == num)
528 target = target->next;
534 struct target *get_current_target(struct command_context *cmd_ctx)
536 struct target *target = get_current_target_or_null(cmd_ctx);
539 LOG_ERROR("BUG: current_target out of bounds");
546 struct target *get_current_target_or_null(struct command_context *cmd_ctx)
548 return cmd_ctx->current_target_override
549 ? cmd_ctx->current_target_override
550 : cmd_ctx->current_target;
553 int target_poll(struct target *target)
557 /* We can't poll until after examine */
558 if (!target_was_examined(target)) {
559 /* Fail silently lest we pollute the log */
563 retval = target->type->poll(target);
564 if (retval != ERROR_OK)
567 if (target->halt_issued) {
568 if (target->state == TARGET_HALTED)
569 target->halt_issued = false;
571 int64_t t = timeval_ms() - target->halt_issued_time;
572 if (t > DEFAULT_HALT_TIMEOUT) {
573 target->halt_issued = false;
574 LOG_INFO("Halt timed out, wake up GDB.");
575 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
583 int target_halt(struct target *target)
586 /* We can't poll until after examine */
587 if (!target_was_examined(target)) {
588 LOG_ERROR("Target not examined yet");
592 retval = target->type->halt(target);
593 if (retval != ERROR_OK)
596 target->halt_issued = true;
597 target->halt_issued_time = timeval_ms();
603 * Make the target (re)start executing using its saved execution
604 * context (possibly with some modifications).
606 * @param target Which target should start executing.
607 * @param current True to use the target's saved program counter instead
608 * of the address parameter
609 * @param address Optionally used as the program counter.
610 * @param handle_breakpoints True iff breakpoints at the resumption PC
611 * should be skipped. (For example, maybe execution was stopped by
612 * such a breakpoint, in which case it would be counterproductive to
614 * @param debug_execution False if all working areas allocated by OpenOCD
615 * should be released and/or restored to their original contents.
616 * (This would for example be true to run some downloaded "helper"
617 * algorithm code, which resides in one such working buffer and uses
618 * another for data storage.)
620 * @todo Resolve the ambiguity about what the "debug_execution" flag
621 * signifies. For example, Target implementations don't agree on how
622 * it relates to invalidation of the register cache, or to whether
623 * breakpoints and watchpoints should be enabled. (It would seem wrong
624 * to enable breakpoints when running downloaded "helper" algorithms
625 * (debug_execution true), since the breakpoints would be set to match
626 * target firmware being debugged, not the helper algorithm.... and
627 * enabling them could cause such helpers to malfunction (for example,
628 * by overwriting data with a breakpoint instruction. On the other
629 * hand the infrastructure for running such helpers might use this
630 * procedure but rely on hardware breakpoint to detect termination.)
632 int target_resume(struct target *target, int current, target_addr_t address,
633 int handle_breakpoints, int debug_execution)
637 /* We can't poll until after examine */
638 if (!target_was_examined(target)) {
639 LOG_ERROR("Target not examined yet");
643 target_call_event_callbacks(target, TARGET_EVENT_RESUME_START);
645 /* note that resume *must* be asynchronous. The CPU can halt before
646 * we poll. The CPU can even halt at the current PC as a result of
647 * a software breakpoint being inserted by (a bug?) the application.
650 * resume() triggers the event 'resumed'. The execution of TCL commands
651 * in the event handler causes the polling of targets. If the target has
652 * already halted for a breakpoint, polling will run the 'halted' event
653 * handler before the pending 'resumed' handler.
654 * Disable polling during resume() to guarantee the execution of handlers
655 * in the correct order.
657 bool save_poll = jtag_poll_get_enabled();
658 jtag_poll_set_enabled(false);
659 retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution);
660 jtag_poll_set_enabled(save_poll);
661 if (retval != ERROR_OK)
664 target_call_event_callbacks(target, TARGET_EVENT_RESUME_END);
669 static int target_process_reset(struct command_invocation *cmd, enum target_reset_mode reset_mode)
674 n = jim_nvp_value2name_simple(nvp_reset_modes, reset_mode);
676 LOG_ERROR("invalid reset mode");
680 struct target *target;
681 for (target = all_targets; target; target = target->next)
682 target_call_reset_callbacks(target, reset_mode);
684 /* disable polling during reset to make reset event scripts
685 * more predictable, i.e. dr/irscan & pathmove in events will
686 * not have JTAG operations injected into the middle of a sequence.
688 bool save_poll = jtag_poll_get_enabled();
690 jtag_poll_set_enabled(false);
692 sprintf(buf, "ocd_process_reset %s", n->name);
693 retval = Jim_Eval(cmd->ctx->interp, buf);
695 jtag_poll_set_enabled(save_poll);
697 if (retval != JIM_OK) {
698 Jim_MakeErrorMessage(cmd->ctx->interp);
699 command_print(cmd, "%s", Jim_GetString(Jim_GetResult(cmd->ctx->interp), NULL));
703 /* We want any events to be processed before the prompt */
704 retval = target_call_timer_callbacks_now();
706 for (target = all_targets; target; target = target->next) {
707 target->type->check_reset(target);
708 target->running_alg = false;
714 static int identity_virt2phys(struct target *target,
715 target_addr_t virtual, target_addr_t *physical)
721 static int no_mmu(struct target *target, int *enabled)
728 * Reset the @c examined flag for the given target.
729 * Pure paranoia -- targets are zeroed on allocation.
731 static inline void target_reset_examined(struct target *target)
733 target->examined = false;
736 static int default_examine(struct target *target)
738 target_set_examined(target);
742 /* no check by default */
743 static int default_check_reset(struct target *target)
748 /* Equivalent Tcl code arp_examine_one is in src/target/startup.tcl
750 int target_examine_one(struct target *target)
752 target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_START);
754 int retval = target->type->examine(target);
755 if (retval != ERROR_OK) {
756 target_reset_examined(target);
757 target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_FAIL);
761 target_set_examined(target);
762 target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_END);
767 static int jtag_enable_callback(enum jtag_event event, void *priv)
769 struct target *target = priv;
771 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
774 jtag_unregister_event_callback(jtag_enable_callback, target);
776 return target_examine_one(target);
779 /* Targets that correctly implement init + examine, i.e.
780 * no communication with target during init:
784 int target_examine(void)
786 int retval = ERROR_OK;
787 struct target *target;
789 for (target = all_targets; target; target = target->next) {
790 /* defer examination, but don't skip it */
791 if (!target->tap->enabled) {
792 jtag_register_event_callback(jtag_enable_callback,
797 if (target->defer_examine)
800 int retval2 = target_examine_one(target);
801 if (retval2 != ERROR_OK) {
802 LOG_WARNING("target %s examination failed", target_name(target));
809 const char *target_type_name(struct target *target)
811 return target->type->name;
814 static int target_soft_reset_halt(struct target *target)
816 if (!target_was_examined(target)) {
817 LOG_ERROR("Target not examined yet");
820 if (!target->type->soft_reset_halt) {
821 LOG_ERROR("Target %s does not support soft_reset_halt",
822 target_name(target));
825 return target->type->soft_reset_halt(target);
829 * Downloads a target-specific native code algorithm to the target,
830 * and executes it. * Note that some targets may need to set up, enable,
831 * and tear down a breakpoint (hard or * soft) to detect algorithm
832 * termination, while others may support lower overhead schemes where
833 * soft breakpoints embedded in the algorithm automatically terminate the
836 * @param target used to run the algorithm
837 * @param num_mem_params
839 * @param num_reg_params
844 * @param arch_info target-specific description of the algorithm.
846 int target_run_algorithm(struct target *target,
847 int num_mem_params, struct mem_param *mem_params,
848 int num_reg_params, struct reg_param *reg_param,
849 target_addr_t entry_point, target_addr_t exit_point,
850 int timeout_ms, void *arch_info)
852 int retval = ERROR_FAIL;
854 if (!target_was_examined(target)) {
855 LOG_ERROR("Target not examined yet");
858 if (!target->type->run_algorithm) {
859 LOG_ERROR("Target type '%s' does not support %s",
860 target_type_name(target), __func__);
864 target->running_alg = true;
865 retval = target->type->run_algorithm(target,
866 num_mem_params, mem_params,
867 num_reg_params, reg_param,
868 entry_point, exit_point, timeout_ms, arch_info);
869 target->running_alg = false;
876 * Executes a target-specific native code algorithm and leaves it running.
878 * @param target used to run the algorithm
879 * @param num_mem_params
881 * @param num_reg_params
885 * @param arch_info target-specific description of the algorithm.
887 int target_start_algorithm(struct target *target,
888 int num_mem_params, struct mem_param *mem_params,
889 int num_reg_params, struct reg_param *reg_params,
890 target_addr_t entry_point, target_addr_t exit_point,
893 int retval = ERROR_FAIL;
895 if (!target_was_examined(target)) {
896 LOG_ERROR("Target not examined yet");
899 if (!target->type->start_algorithm) {
900 LOG_ERROR("Target type '%s' does not support %s",
901 target_type_name(target), __func__);
904 if (target->running_alg) {
905 LOG_ERROR("Target is already running an algorithm");
909 target->running_alg = true;
910 retval = target->type->start_algorithm(target,
911 num_mem_params, mem_params,
912 num_reg_params, reg_params,
913 entry_point, exit_point, arch_info);
920 * Waits for an algorithm started with target_start_algorithm() to complete.
922 * @param target used to run the algorithm
923 * @param num_mem_params
925 * @param num_reg_params
929 * @param arch_info target-specific description of the algorithm.
931 int target_wait_algorithm(struct target *target,
932 int num_mem_params, struct mem_param *mem_params,
933 int num_reg_params, struct reg_param *reg_params,
934 target_addr_t exit_point, int timeout_ms,
937 int retval = ERROR_FAIL;
939 if (!target->type->wait_algorithm) {
940 LOG_ERROR("Target type '%s' does not support %s",
941 target_type_name(target), __func__);
944 if (!target->running_alg) {
945 LOG_ERROR("Target is not running an algorithm");
949 retval = target->type->wait_algorithm(target,
950 num_mem_params, mem_params,
951 num_reg_params, reg_params,
952 exit_point, timeout_ms, arch_info);
953 if (retval != ERROR_TARGET_TIMEOUT)
954 target->running_alg = false;
961 * Streams data to a circular buffer on target intended for consumption by code
962 * running asynchronously on target.
964 * This is intended for applications where target-specific native code runs
965 * on the target, receives data from the circular buffer, does something with
966 * it (most likely writing it to a flash memory), and advances the circular
969 * This assumes that the helper algorithm has already been loaded to the target,
970 * but has not been started yet. Given memory and register parameters are passed
973 * The buffer is defined by (buffer_start, buffer_size) arguments and has the
976 * [buffer_start + 0, buffer_start + 4):
977 * Write Pointer address (aka head). Written and updated by this
978 * routine when new data is written to the circular buffer.
979 * [buffer_start + 4, buffer_start + 8):
980 * Read Pointer address (aka tail). Updated by code running on the
981 * target after it consumes data.
982 * [buffer_start + 8, buffer_start + buffer_size):
983 * Circular buffer contents.
985 * See contrib/loaders/flash/stm32f1x.S for an example.
987 * @param target used to run the algorithm
988 * @param buffer address on the host where data to be sent is located
989 * @param count number of blocks to send
990 * @param block_size size in bytes of each block
991 * @param num_mem_params count of memory-based params to pass to algorithm
992 * @param mem_params memory-based params to pass to algorithm
993 * @param num_reg_params count of register-based params to pass to algorithm
994 * @param reg_params memory-based params to pass to algorithm
995 * @param buffer_start address on the target of the circular buffer structure
996 * @param buffer_size size of the circular buffer structure
997 * @param entry_point address on the target to execute to start the algorithm
998 * @param exit_point address at which to set a breakpoint to catch the
999 * end of the algorithm; can be 0 if target triggers a breakpoint itself
1003 int target_run_flash_async_algorithm(struct target *target,
1004 const uint8_t *buffer, uint32_t count, int block_size,
1005 int num_mem_params, struct mem_param *mem_params,
1006 int num_reg_params, struct reg_param *reg_params,
1007 uint32_t buffer_start, uint32_t buffer_size,
1008 uint32_t entry_point, uint32_t exit_point, void *arch_info)
1013 const uint8_t *buffer_orig = buffer;
1015 /* Set up working area. First word is write pointer, second word is read pointer,
1016 * rest is fifo data area. */
1017 uint32_t wp_addr = buffer_start;
1018 uint32_t rp_addr = buffer_start + 4;
1019 uint32_t fifo_start_addr = buffer_start + 8;
1020 uint32_t fifo_end_addr = buffer_start + buffer_size;
1022 uint32_t wp = fifo_start_addr;
1023 uint32_t rp = fifo_start_addr;
1025 /* validate block_size is 2^n */
1026 assert(IS_PWR_OF_2(block_size));
1028 retval = target_write_u32(target, wp_addr, wp);
1029 if (retval != ERROR_OK)
1031 retval = target_write_u32(target, rp_addr, rp);
1032 if (retval != ERROR_OK)
1035 /* Start up algorithm on target and let it idle while writing the first chunk */
1036 retval = target_start_algorithm(target, num_mem_params, mem_params,
1037 num_reg_params, reg_params,
1042 if (retval != ERROR_OK) {
1043 LOG_ERROR("error starting target flash write algorithm");
1049 retval = target_read_u32(target, rp_addr, &rp);
1050 if (retval != ERROR_OK) {
1051 LOG_ERROR("failed to get read pointer");
1055 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32 " wp 0x%" PRIx32 " rp 0x%" PRIx32,
1056 (size_t) (buffer - buffer_orig), count, wp, rp);
1059 LOG_ERROR("flash write algorithm aborted by target");
1060 retval = ERROR_FLASH_OPERATION_FAILED;
1064 if (!IS_ALIGNED(rp - fifo_start_addr, block_size) || rp < fifo_start_addr || rp >= fifo_end_addr) {
1065 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32, rp);
1069 /* Count the number of bytes available in the fifo without
1070 * crossing the wrap around. Make sure to not fill it completely,
1071 * because that would make wp == rp and that's the empty condition. */
1072 uint32_t thisrun_bytes;
1074 thisrun_bytes = rp - wp - block_size;
1075 else if (rp > fifo_start_addr)
1076 thisrun_bytes = fifo_end_addr - wp;
1078 thisrun_bytes = fifo_end_addr - wp - block_size;
1080 if (thisrun_bytes == 0) {
1081 /* Throttle polling a bit if transfer is (much) faster than flash
1082 * programming. The exact delay shouldn't matter as long as it's
1083 * less than buffer size / flash speed. This is very unlikely to
1084 * run when using high latency connections such as USB. */
1087 /* to stop an infinite loop on some targets check and increment a timeout
1088 * this issue was observed on a stellaris using the new ICDI interface */
1089 if (timeout++ >= 2500) {
1090 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1091 return ERROR_FLASH_OPERATION_FAILED;
1096 /* reset our timeout */
1099 /* Limit to the amount of data we actually want to write */
1100 if (thisrun_bytes > count * block_size)
1101 thisrun_bytes = count * block_size;
1103 /* Force end of large blocks to be word aligned */
1104 if (thisrun_bytes >= 16)
1105 thisrun_bytes -= (rp + thisrun_bytes) & 0x03;
1107 /* Write data to fifo */
1108 retval = target_write_buffer(target, wp, thisrun_bytes, buffer);
1109 if (retval != ERROR_OK)
1112 /* Update counters and wrap write pointer */
1113 buffer += thisrun_bytes;
1114 count -= thisrun_bytes / block_size;
1115 wp += thisrun_bytes;
1116 if (wp >= fifo_end_addr)
1117 wp = fifo_start_addr;
1119 /* Store updated write pointer to target */
1120 retval = target_write_u32(target, wp_addr, wp);
1121 if (retval != ERROR_OK)
1124 /* Avoid GDB timeouts */
1128 if (retval != ERROR_OK) {
1129 /* abort flash write algorithm on target */
1130 target_write_u32(target, wp_addr, 0);
1133 int retval2 = target_wait_algorithm(target, num_mem_params, mem_params,
1134 num_reg_params, reg_params,
1139 if (retval2 != ERROR_OK) {
1140 LOG_ERROR("error waiting for target flash write algorithm");
1144 if (retval == ERROR_OK) {
1145 /* check if algorithm set rp = 0 after fifo writer loop finished */
1146 retval = target_read_u32(target, rp_addr, &rp);
1147 if (retval == ERROR_OK && rp == 0) {
1148 LOG_ERROR("flash write algorithm aborted by target");
1149 retval = ERROR_FLASH_OPERATION_FAILED;
1156 int target_run_read_async_algorithm(struct target *target,
1157 uint8_t *buffer, uint32_t count, int block_size,
1158 int num_mem_params, struct mem_param *mem_params,
1159 int num_reg_params, struct reg_param *reg_params,
1160 uint32_t buffer_start, uint32_t buffer_size,
1161 uint32_t entry_point, uint32_t exit_point, void *arch_info)
1166 const uint8_t *buffer_orig = buffer;
1168 /* Set up working area. First word is write pointer, second word is read pointer,
1169 * rest is fifo data area. */
1170 uint32_t wp_addr = buffer_start;
1171 uint32_t rp_addr = buffer_start + 4;
1172 uint32_t fifo_start_addr = buffer_start + 8;
1173 uint32_t fifo_end_addr = buffer_start + buffer_size;
1175 uint32_t wp = fifo_start_addr;
1176 uint32_t rp = fifo_start_addr;
1178 /* validate block_size is 2^n */
1179 assert(IS_PWR_OF_2(block_size));
1181 retval = target_write_u32(target, wp_addr, wp);
1182 if (retval != ERROR_OK)
1184 retval = target_write_u32(target, rp_addr, rp);
1185 if (retval != ERROR_OK)
1188 /* Start up algorithm on target */
1189 retval = target_start_algorithm(target, num_mem_params, mem_params,
1190 num_reg_params, reg_params,
1195 if (retval != ERROR_OK) {
1196 LOG_ERROR("error starting target flash read algorithm");
1201 retval = target_read_u32(target, wp_addr, &wp);
1202 if (retval != ERROR_OK) {
1203 LOG_ERROR("failed to get write pointer");
1207 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32 " wp 0x%" PRIx32 " rp 0x%" PRIx32,
1208 (size_t)(buffer - buffer_orig), count, wp, rp);
1211 LOG_ERROR("flash read algorithm aborted by target");
1212 retval = ERROR_FLASH_OPERATION_FAILED;
1216 if (!IS_ALIGNED(wp - fifo_start_addr, block_size) || wp < fifo_start_addr || wp >= fifo_end_addr) {
1217 LOG_ERROR("corrupted fifo write pointer 0x%" PRIx32, wp);
1221 /* Count the number of bytes available in the fifo without
1222 * crossing the wrap around. */
1223 uint32_t thisrun_bytes;
1225 thisrun_bytes = wp - rp;
1227 thisrun_bytes = fifo_end_addr - rp;
1229 if (thisrun_bytes == 0) {
1230 /* Throttle polling a bit if transfer is (much) faster than flash
1231 * reading. The exact delay shouldn't matter as long as it's
1232 * less than buffer size / flash speed. This is very unlikely to
1233 * run when using high latency connections such as USB. */
1236 /* to stop an infinite loop on some targets check and increment a timeout
1237 * this issue was observed on a stellaris using the new ICDI interface */
1238 if (timeout++ >= 2500) {
1239 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1240 return ERROR_FLASH_OPERATION_FAILED;
1245 /* Reset our timeout */
1248 /* Limit to the amount of data we actually want to read */
1249 if (thisrun_bytes > count * block_size)
1250 thisrun_bytes = count * block_size;
1252 /* Force end of large blocks to be word aligned */
1253 if (thisrun_bytes >= 16)
1254 thisrun_bytes -= (rp + thisrun_bytes) & 0x03;
1256 /* Read data from fifo */
1257 retval = target_read_buffer(target, rp, thisrun_bytes, buffer);
1258 if (retval != ERROR_OK)
1261 /* Update counters and wrap write pointer */
1262 buffer += thisrun_bytes;
1263 count -= thisrun_bytes / block_size;
1264 rp += thisrun_bytes;
1265 if (rp >= fifo_end_addr)
1266 rp = fifo_start_addr;
1268 /* Store updated write pointer to target */
1269 retval = target_write_u32(target, rp_addr, rp);
1270 if (retval != ERROR_OK)
1273 /* Avoid GDB timeouts */
1278 if (retval != ERROR_OK) {
1279 /* abort flash write algorithm on target */
1280 target_write_u32(target, rp_addr, 0);
1283 int retval2 = target_wait_algorithm(target, num_mem_params, mem_params,
1284 num_reg_params, reg_params,
1289 if (retval2 != ERROR_OK) {
1290 LOG_ERROR("error waiting for target flash write algorithm");
1294 if (retval == ERROR_OK) {
1295 /* check if algorithm set wp = 0 after fifo writer loop finished */
1296 retval = target_read_u32(target, wp_addr, &wp);
1297 if (retval == ERROR_OK && wp == 0) {
1298 LOG_ERROR("flash read algorithm aborted by target");
1299 retval = ERROR_FLASH_OPERATION_FAILED;
1306 int target_read_memory(struct target *target,
1307 target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
1309 if (!target_was_examined(target)) {
1310 LOG_ERROR("Target not examined yet");
1313 if (!target->type->read_memory) {
1314 LOG_ERROR("Target %s doesn't support read_memory", target_name(target));
1317 return target->type->read_memory(target, address, size, count, buffer);
1320 int target_read_phys_memory(struct target *target,
1321 target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
1323 if (!target_was_examined(target)) {
1324 LOG_ERROR("Target not examined yet");
1327 if (!target->type->read_phys_memory) {
1328 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target));
1331 return target->type->read_phys_memory(target, address, size, count, buffer);
1334 int target_write_memory(struct target *target,
1335 target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
1337 if (!target_was_examined(target)) {
1338 LOG_ERROR("Target not examined yet");
1341 if (!target->type->write_memory) {
1342 LOG_ERROR("Target %s doesn't support write_memory", target_name(target));
1345 return target->type->write_memory(target, address, size, count, buffer);
1348 int target_write_phys_memory(struct target *target,
1349 target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
1351 if (!target_was_examined(target)) {
1352 LOG_ERROR("Target not examined yet");
1355 if (!target->type->write_phys_memory) {
1356 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target));
1359 return target->type->write_phys_memory(target, address, size, count, buffer);
1362 int target_add_breakpoint(struct target *target,
1363 struct breakpoint *breakpoint)
1365 if ((target->state != TARGET_HALTED) && (breakpoint->type != BKPT_HARD)) {
1366 LOG_WARNING("target %s is not halted (add breakpoint)", target_name(target));
1367 return ERROR_TARGET_NOT_HALTED;
1369 return target->type->add_breakpoint(target, breakpoint);
1372 int target_add_context_breakpoint(struct target *target,
1373 struct breakpoint *breakpoint)
1375 if (target->state != TARGET_HALTED) {
1376 LOG_WARNING("target %s is not halted (add context breakpoint)", target_name(target));
1377 return ERROR_TARGET_NOT_HALTED;
1379 return target->type->add_context_breakpoint(target, breakpoint);
1382 int target_add_hybrid_breakpoint(struct target *target,
1383 struct breakpoint *breakpoint)
1385 if (target->state != TARGET_HALTED) {
1386 LOG_WARNING("target %s is not halted (add hybrid breakpoint)", target_name(target));
1387 return ERROR_TARGET_NOT_HALTED;
1389 return target->type->add_hybrid_breakpoint(target, breakpoint);
1392 int target_remove_breakpoint(struct target *target,
1393 struct breakpoint *breakpoint)
1395 return target->type->remove_breakpoint(target, breakpoint);
1398 int target_add_watchpoint(struct target *target,
1399 struct watchpoint *watchpoint)
1401 if (target->state != TARGET_HALTED) {
1402 LOG_WARNING("target %s is not halted (add watchpoint)", target_name(target));
1403 return ERROR_TARGET_NOT_HALTED;
1405 return target->type->add_watchpoint(target, watchpoint);
1407 int target_remove_watchpoint(struct target *target,
1408 struct watchpoint *watchpoint)
1410 return target->type->remove_watchpoint(target, watchpoint);
1412 int target_hit_watchpoint(struct target *target,
1413 struct watchpoint **hit_watchpoint)
1415 if (target->state != TARGET_HALTED) {
1416 LOG_WARNING("target %s is not halted (hit watchpoint)", target->cmd_name);
1417 return ERROR_TARGET_NOT_HALTED;
1420 if (!target->type->hit_watchpoint) {
1421 /* For backward compatible, if hit_watchpoint is not implemented,
1422 * return ERROR_FAIL such that gdb_server will not take the nonsense
1427 return target->type->hit_watchpoint(target, hit_watchpoint);
1430 const char *target_get_gdb_arch(struct target *target)
1432 if (!target->type->get_gdb_arch)
1434 return target->type->get_gdb_arch(target);
1437 int target_get_gdb_reg_list(struct target *target,
1438 struct reg **reg_list[], int *reg_list_size,
1439 enum target_register_class reg_class)
1441 int result = ERROR_FAIL;
1443 if (!target_was_examined(target)) {
1444 LOG_ERROR("Target not examined yet");
1448 result = target->type->get_gdb_reg_list(target, reg_list,
1449 reg_list_size, reg_class);
1452 if (result != ERROR_OK) {
1459 int target_get_gdb_reg_list_noread(struct target *target,
1460 struct reg **reg_list[], int *reg_list_size,
1461 enum target_register_class reg_class)
1463 if (target->type->get_gdb_reg_list_noread &&
1464 target->type->get_gdb_reg_list_noread(target, reg_list,
1465 reg_list_size, reg_class) == ERROR_OK)
1467 return target_get_gdb_reg_list(target, reg_list, reg_list_size, reg_class);
1470 bool target_supports_gdb_connection(struct target *target)
1473 * exclude all the targets that don't provide get_gdb_reg_list
1474 * or that have explicit gdb_max_connection == 0
1476 return !!target->type->get_gdb_reg_list && !!target->gdb_max_connections;
1479 int target_step(struct target *target,
1480 int current, target_addr_t address, int handle_breakpoints)
1484 target_call_event_callbacks(target, TARGET_EVENT_STEP_START);
1486 retval = target->type->step(target, current, address, handle_breakpoints);
1487 if (retval != ERROR_OK)
1490 target_call_event_callbacks(target, TARGET_EVENT_STEP_END);
1495 int target_get_gdb_fileio_info(struct target *target, struct gdb_fileio_info *fileio_info)
1497 if (target->state != TARGET_HALTED) {
1498 LOG_WARNING("target %s is not halted (gdb fileio)", target->cmd_name);
1499 return ERROR_TARGET_NOT_HALTED;
1501 return target->type->get_gdb_fileio_info(target, fileio_info);
1504 int target_gdb_fileio_end(struct target *target, int retcode, int fileio_errno, bool ctrl_c)
1506 if (target->state != TARGET_HALTED) {
1507 LOG_WARNING("target %s is not halted (gdb fileio end)", target->cmd_name);
1508 return ERROR_TARGET_NOT_HALTED;
1510 return target->type->gdb_fileio_end(target, retcode, fileio_errno, ctrl_c);
1513 target_addr_t target_address_max(struct target *target)
1515 unsigned bits = target_address_bits(target);
1516 if (sizeof(target_addr_t) * 8 == bits)
1517 return (target_addr_t) -1;
1519 return (((target_addr_t) 1) << bits) - 1;
1522 unsigned target_address_bits(struct target *target)
1524 if (target->type->address_bits)
1525 return target->type->address_bits(target);
1529 unsigned int target_data_bits(struct target *target)
1531 if (target->type->data_bits)
1532 return target->type->data_bits(target);
1536 static int target_profiling(struct target *target, uint32_t *samples,
1537 uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
1539 return target->type->profiling(target, samples, max_num_samples,
1540 num_samples, seconds);
1543 static int handle_target(void *priv);
1545 static int target_init_one(struct command_context *cmd_ctx,
1546 struct target *target)
1548 target_reset_examined(target);
1550 struct target_type *type = target->type;
1552 type->examine = default_examine;
1554 if (!type->check_reset)
1555 type->check_reset = default_check_reset;
1557 assert(type->init_target);
1559 int retval = type->init_target(cmd_ctx, target);
1560 if (retval != ERROR_OK) {
1561 LOG_ERROR("target '%s' init failed", target_name(target));
1565 /* Sanity-check MMU support ... stub in what we must, to help
1566 * implement it in stages, but warn if we need to do so.
1569 if (!type->virt2phys) {
1570 LOG_ERROR("type '%s' is missing virt2phys", type->name);
1571 type->virt2phys = identity_virt2phys;
1574 /* Make sure no-MMU targets all behave the same: make no
1575 * distinction between physical and virtual addresses, and
1576 * ensure that virt2phys() is always an identity mapping.
1578 if (type->write_phys_memory || type->read_phys_memory || type->virt2phys)
1579 LOG_WARNING("type '%s' has bad MMU hooks", type->name);
1582 type->write_phys_memory = type->write_memory;
1583 type->read_phys_memory = type->read_memory;
1584 type->virt2phys = identity_virt2phys;
1587 if (!target->type->read_buffer)
1588 target->type->read_buffer = target_read_buffer_default;
1590 if (!target->type->write_buffer)
1591 target->type->write_buffer = target_write_buffer_default;
1593 if (!target->type->get_gdb_fileio_info)
1594 target->type->get_gdb_fileio_info = target_get_gdb_fileio_info_default;
1596 if (!target->type->gdb_fileio_end)
1597 target->type->gdb_fileio_end = target_gdb_fileio_end_default;
1599 if (!target->type->profiling)
1600 target->type->profiling = target_profiling_default;
1605 static int target_init(struct command_context *cmd_ctx)
1607 struct target *target;
1610 for (target = all_targets; target; target = target->next) {
1611 retval = target_init_one(cmd_ctx, target);
1612 if (retval != ERROR_OK)
1619 retval = target_register_user_commands(cmd_ctx);
1620 if (retval != ERROR_OK)
1623 retval = target_register_timer_callback(&handle_target,
1624 polling_interval, TARGET_TIMER_TYPE_PERIODIC, cmd_ctx->interp);
1625 if (retval != ERROR_OK)
1631 COMMAND_HANDLER(handle_target_init_command)
1636 return ERROR_COMMAND_SYNTAX_ERROR;
1638 static bool target_initialized;
1639 if (target_initialized) {
1640 LOG_INFO("'target init' has already been called");
1643 target_initialized = true;
1645 retval = command_run_line(CMD_CTX, "init_targets");
1646 if (retval != ERROR_OK)
1649 retval = command_run_line(CMD_CTX, "init_target_events");
1650 if (retval != ERROR_OK)
1653 retval = command_run_line(CMD_CTX, "init_board");
1654 if (retval != ERROR_OK)
1657 LOG_DEBUG("Initializing targets...");
1658 return target_init(CMD_CTX);
1661 int target_register_event_callback(int (*callback)(struct target *target,
1662 enum target_event event, void *priv), void *priv)
1664 struct target_event_callback **callbacks_p = &target_event_callbacks;
1667 return ERROR_COMMAND_SYNTAX_ERROR;
1670 while ((*callbacks_p)->next)
1671 callbacks_p = &((*callbacks_p)->next);
1672 callbacks_p = &((*callbacks_p)->next);
1675 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
1676 (*callbacks_p)->callback = callback;
1677 (*callbacks_p)->priv = priv;
1678 (*callbacks_p)->next = NULL;
1683 int target_register_reset_callback(int (*callback)(struct target *target,
1684 enum target_reset_mode reset_mode, void *priv), void *priv)
1686 struct target_reset_callback *entry;
1689 return ERROR_COMMAND_SYNTAX_ERROR;
1691 entry = malloc(sizeof(struct target_reset_callback));
1693 LOG_ERROR("error allocating buffer for reset callback entry");
1694 return ERROR_COMMAND_SYNTAX_ERROR;
1697 entry->callback = callback;
1699 list_add(&entry->list, &target_reset_callback_list);
1705 int target_register_trace_callback(int (*callback)(struct target *target,
1706 size_t len, uint8_t *data, void *priv), void *priv)
1708 struct target_trace_callback *entry;
1711 return ERROR_COMMAND_SYNTAX_ERROR;
1713 entry = malloc(sizeof(struct target_trace_callback));
1715 LOG_ERROR("error allocating buffer for trace callback entry");
1716 return ERROR_COMMAND_SYNTAX_ERROR;
1719 entry->callback = callback;
1721 list_add(&entry->list, &target_trace_callback_list);
1727 int target_register_timer_callback(int (*callback)(void *priv),
1728 unsigned int time_ms, enum target_timer_type type, void *priv)
1730 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
1733 return ERROR_COMMAND_SYNTAX_ERROR;
1736 while ((*callbacks_p)->next)
1737 callbacks_p = &((*callbacks_p)->next);
1738 callbacks_p = &((*callbacks_p)->next);
1741 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
1742 (*callbacks_p)->callback = callback;
1743 (*callbacks_p)->type = type;
1744 (*callbacks_p)->time_ms = time_ms;
1745 (*callbacks_p)->removed = false;
1747 (*callbacks_p)->when = timeval_ms() + time_ms;
1748 target_timer_next_event_value = MIN(target_timer_next_event_value, (*callbacks_p)->when);
1750 (*callbacks_p)->priv = priv;
1751 (*callbacks_p)->next = NULL;
1756 int target_unregister_event_callback(int (*callback)(struct target *target,
1757 enum target_event event, void *priv), void *priv)
1759 struct target_event_callback **p = &target_event_callbacks;
1760 struct target_event_callback *c = target_event_callbacks;
1763 return ERROR_COMMAND_SYNTAX_ERROR;
1766 struct target_event_callback *next = c->next;
1767 if ((c->callback == callback) && (c->priv == priv)) {
1779 int target_unregister_reset_callback(int (*callback)(struct target *target,
1780 enum target_reset_mode reset_mode, void *priv), void *priv)
1782 struct target_reset_callback *entry;
1785 return ERROR_COMMAND_SYNTAX_ERROR;
1787 list_for_each_entry(entry, &target_reset_callback_list, list) {
1788 if (entry->callback == callback && entry->priv == priv) {
1789 list_del(&entry->list);
1798 int target_unregister_trace_callback(int (*callback)(struct target *target,
1799 size_t len, uint8_t *data, void *priv), void *priv)
1801 struct target_trace_callback *entry;
1804 return ERROR_COMMAND_SYNTAX_ERROR;
1806 list_for_each_entry(entry, &target_trace_callback_list, list) {
1807 if (entry->callback == callback && entry->priv == priv) {
1808 list_del(&entry->list);
1817 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
1820 return ERROR_COMMAND_SYNTAX_ERROR;
1822 for (struct target_timer_callback *c = target_timer_callbacks;
1824 if ((c->callback == callback) && (c->priv == priv)) {
1833 int target_call_event_callbacks(struct target *target, enum target_event event)
1835 struct target_event_callback *callback = target_event_callbacks;
1836 struct target_event_callback *next_callback;
1838 if (event == TARGET_EVENT_HALTED) {
1839 /* execute early halted first */
1840 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1843 LOG_DEBUG("target event %i (%s) for core %s", event,
1844 target_event_name(event),
1845 target_name(target));
1847 target_handle_event(target, event);
1850 next_callback = callback->next;
1851 callback->callback(target, event, callback->priv);
1852 callback = next_callback;
1858 int target_call_reset_callbacks(struct target *target, enum target_reset_mode reset_mode)
1860 struct target_reset_callback *callback;
1862 LOG_DEBUG("target reset %i (%s)", reset_mode,
1863 jim_nvp_value2name_simple(nvp_reset_modes, reset_mode)->name);
1865 list_for_each_entry(callback, &target_reset_callback_list, list)
1866 callback->callback(target, reset_mode, callback->priv);
1871 int target_call_trace_callbacks(struct target *target, size_t len, uint8_t *data)
1873 struct target_trace_callback *callback;
1875 list_for_each_entry(callback, &target_trace_callback_list, list)
1876 callback->callback(target, len, data, callback->priv);
1881 static int target_timer_callback_periodic_restart(
1882 struct target_timer_callback *cb, int64_t *now)
1884 cb->when = *now + cb->time_ms;
1888 static int target_call_timer_callback(struct target_timer_callback *cb,
1891 cb->callback(cb->priv);
1893 if (cb->type == TARGET_TIMER_TYPE_PERIODIC)
1894 return target_timer_callback_periodic_restart(cb, now);
1896 return target_unregister_timer_callback(cb->callback, cb->priv);
1899 static int target_call_timer_callbacks_check_time(int checktime)
1901 static bool callback_processing;
1903 /* Do not allow nesting */
1904 if (callback_processing)
1907 callback_processing = true;
1911 int64_t now = timeval_ms();
1913 /* Initialize to a default value that's a ways into the future.
1914 * The loop below will make it closer to now if there are
1915 * callbacks that want to be called sooner. */
1916 target_timer_next_event_value = now + 1000;
1918 /* Store an address of the place containing a pointer to the
1919 * next item; initially, that's a standalone "root of the
1920 * list" variable. */
1921 struct target_timer_callback **callback = &target_timer_callbacks;
1922 while (callback && *callback) {
1923 if ((*callback)->removed) {
1924 struct target_timer_callback *p = *callback;
1925 *callback = (*callback)->next;
1930 bool call_it = (*callback)->callback &&
1931 ((!checktime && (*callback)->type == TARGET_TIMER_TYPE_PERIODIC) ||
1932 now >= (*callback)->when);
1935 target_call_timer_callback(*callback, &now);
1937 if (!(*callback)->removed && (*callback)->when < target_timer_next_event_value)
1938 target_timer_next_event_value = (*callback)->when;
1940 callback = &(*callback)->next;
1943 callback_processing = false;
1947 int target_call_timer_callbacks()
1949 return target_call_timer_callbacks_check_time(1);
1952 /* invoke periodic callbacks immediately */
1953 int target_call_timer_callbacks_now()
1955 return target_call_timer_callbacks_check_time(0);
1958 int64_t target_timer_next_event(void)
1960 return target_timer_next_event_value;
1963 /* Prints the working area layout for debug purposes */
1964 static void print_wa_layout(struct target *target)
1966 struct working_area *c = target->working_areas;
1969 LOG_DEBUG("%c%c " TARGET_ADDR_FMT "-" TARGET_ADDR_FMT " (%" PRIu32 " bytes)",
1970 c->backup ? 'b' : ' ', c->free ? ' ' : '*',
1971 c->address, c->address + c->size - 1, c->size);
1976 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1977 static void target_split_working_area(struct working_area *area, uint32_t size)
1979 assert(area->free); /* Shouldn't split an allocated area */
1980 assert(size <= area->size); /* Caller should guarantee this */
1982 /* Split only if not already the right size */
1983 if (size < area->size) {
1984 struct working_area *new_wa = malloc(sizeof(*new_wa));
1989 new_wa->next = area->next;
1990 new_wa->size = area->size - size;
1991 new_wa->address = area->address + size;
1992 new_wa->backup = NULL;
1993 new_wa->user = NULL;
1994 new_wa->free = true;
1996 area->next = new_wa;
1999 /* If backup memory was allocated to this area, it has the wrong size
2000 * now so free it and it will be reallocated if/when needed */
2002 area->backup = NULL;
2006 /* Merge all adjacent free areas into one */
2007 static void target_merge_working_areas(struct target *target)
2009 struct working_area *c = target->working_areas;
2011 while (c && c->next) {
2012 assert(c->next->address == c->address + c->size); /* This is an invariant */
2014 /* Find two adjacent free areas */
2015 if (c->free && c->next->free) {
2016 /* Merge the last into the first */
2017 c->size += c->next->size;
2019 /* Remove the last */
2020 struct working_area *to_be_freed = c->next;
2021 c->next = c->next->next;
2022 free(to_be_freed->backup);
2025 /* If backup memory was allocated to the remaining area, it's has
2026 * the wrong size now */
2035 int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
2037 /* Reevaluate working area address based on MMU state*/
2038 if (!target->working_areas) {
2042 retval = target->type->mmu(target, &enabled);
2043 if (retval != ERROR_OK)
2047 if (target->working_area_phys_spec) {
2048 LOG_DEBUG("MMU disabled, using physical "
2049 "address for working memory " TARGET_ADDR_FMT,
2050 target->working_area_phys);
2051 target->working_area = target->working_area_phys;
2053 LOG_ERROR("No working memory available. "
2054 "Specify -work-area-phys to target.");
2055 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
2058 if (target->working_area_virt_spec) {
2059 LOG_DEBUG("MMU enabled, using virtual "
2060 "address for working memory " TARGET_ADDR_FMT,
2061 target->working_area_virt);
2062 target->working_area = target->working_area_virt;
2064 LOG_ERROR("No working memory available. "
2065 "Specify -work-area-virt to target.");
2066 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
2070 /* Set up initial working area on first call */
2071 struct working_area *new_wa = malloc(sizeof(*new_wa));
2073 new_wa->next = NULL;
2074 new_wa->size = target->working_area_size & ~3UL; /* 4-byte align */
2075 new_wa->address = target->working_area;
2076 new_wa->backup = NULL;
2077 new_wa->user = NULL;
2078 new_wa->free = true;
2081 target->working_areas = new_wa;
2084 /* only allocate multiples of 4 byte */
2086 size = (size + 3) & (~3UL);
2088 struct working_area *c = target->working_areas;
2090 /* Find the first large enough working area */
2092 if (c->free && c->size >= size)
2098 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
2100 /* Split the working area into the requested size */
2101 target_split_working_area(c, size);
2103 LOG_DEBUG("allocated new working area of %" PRIu32 " bytes at address " TARGET_ADDR_FMT,
2106 if (target->backup_working_area) {
2108 c->backup = malloc(c->size);
2113 int retval = target_read_memory(target, c->address, 4, c->size / 4, c->backup);
2114 if (retval != ERROR_OK)
2118 /* mark as used, and return the new (reused) area */
2125 print_wa_layout(target);
2130 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
2134 retval = target_alloc_working_area_try(target, size, area);
2135 if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
2136 LOG_WARNING("not enough working area available(requested %"PRIu32")", size);
2141 static int target_restore_working_area(struct target *target, struct working_area *area)
2143 int retval = ERROR_OK;
2145 if (target->backup_working_area && area->backup) {
2146 retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup);
2147 if (retval != ERROR_OK)
2148 LOG_ERROR("failed to restore %" PRIu32 " bytes of working area at address " TARGET_ADDR_FMT,
2149 area->size, area->address);
2155 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
2156 static int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
2158 if (!area || area->free)
2161 int retval = ERROR_OK;
2163 retval = target_restore_working_area(target, area);
2164 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
2165 if (retval != ERROR_OK)
2171 LOG_DEBUG("freed %" PRIu32 " bytes of working area at address " TARGET_ADDR_FMT,
2172 area->size, area->address);
2174 /* mark user pointer invalid */
2175 /* TODO: Is this really safe? It points to some previous caller's memory.
2176 * How could we know that the area pointer is still in that place and not
2177 * some other vital data? What's the purpose of this, anyway? */
2181 target_merge_working_areas(target);
2183 print_wa_layout(target);
2188 int target_free_working_area(struct target *target, struct working_area *area)
2190 return target_free_working_area_restore(target, area, 1);
2193 /* free resources and restore memory, if restoring memory fails,
2194 * free up resources anyway
2196 static void target_free_all_working_areas_restore(struct target *target, int restore)
2198 struct working_area *c = target->working_areas;
2200 LOG_DEBUG("freeing all working areas");
2202 /* Loop through all areas, restoring the allocated ones and marking them as free */
2206 target_restore_working_area(target, c);
2208 *c->user = NULL; /* Same as above */
2214 /* Run a merge pass to combine all areas into one */
2215 target_merge_working_areas(target);
2217 print_wa_layout(target);
2220 void target_free_all_working_areas(struct target *target)
2222 target_free_all_working_areas_restore(target, 1);
2224 /* Now we have none or only one working area marked as free */
2225 if (target->working_areas) {
2226 /* Free the last one to allow on-the-fly moving and resizing */
2227 free(target->working_areas->backup);
2228 free(target->working_areas);
2229 target->working_areas = NULL;
2233 /* Find the largest number of bytes that can be allocated */
2234 uint32_t target_get_working_area_avail(struct target *target)
2236 struct working_area *c = target->working_areas;
2237 uint32_t max_size = 0;
2240 return target->working_area_size;
2243 if (c->free && max_size < c->size)
2252 static void target_destroy(struct target *target)
2254 if (target->type->deinit_target)
2255 target->type->deinit_target(target);
2257 if (target->semihosting)
2258 free(target->semihosting->basedir);
2259 free(target->semihosting);
2261 jtag_unregister_event_callback(jtag_enable_callback, target);
2263 struct target_event_action *teap = target->event_action;
2265 struct target_event_action *next = teap->next;
2266 Jim_DecrRefCount(teap->interp, teap->body);
2271 target_free_all_working_areas(target);
2273 /* release the targets SMP list */
2275 struct target_list *head, *tmp;
2277 list_for_each_entry_safe(head, tmp, target->smp_targets, lh) {
2278 list_del(&head->lh);
2279 head->target->smp = 0;
2282 if (target->smp_targets != &empty_smp_targets)
2283 free(target->smp_targets);
2287 rtos_destroy(target);
2289 free(target->gdb_port_override);
2291 free(target->trace_info);
2292 free(target->fileio_info);
2293 free(target->cmd_name);
2297 void target_quit(void)
2299 struct target_event_callback *pe = target_event_callbacks;
2301 struct target_event_callback *t = pe->next;
2305 target_event_callbacks = NULL;
2307 struct target_timer_callback *pt = target_timer_callbacks;
2309 struct target_timer_callback *t = pt->next;
2313 target_timer_callbacks = NULL;
2315 for (struct target *target = all_targets; target;) {
2319 target_destroy(target);
2326 int target_arch_state(struct target *target)
2330 LOG_WARNING("No target has been configured");
2334 if (target->state != TARGET_HALTED)
2337 retval = target->type->arch_state(target);
2341 static int target_get_gdb_fileio_info_default(struct target *target,
2342 struct gdb_fileio_info *fileio_info)
2344 /* If target does not support semi-hosting function, target
2345 has no need to provide .get_gdb_fileio_info callback.
2346 It just return ERROR_FAIL and gdb_server will return "Txx"
2347 as target halted every time. */
2351 static int target_gdb_fileio_end_default(struct target *target,
2352 int retcode, int fileio_errno, bool ctrl_c)
2357 int target_profiling_default(struct target *target, uint32_t *samples,
2358 uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
2360 struct timeval timeout, now;
2362 gettimeofday(&timeout, NULL);
2363 timeval_add_time(&timeout, seconds, 0);
2365 LOG_INFO("Starting profiling. Halting and resuming the"
2366 " target as often as we can...");
2368 uint32_t sample_count = 0;
2369 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2370 struct reg *reg = register_get_by_name(target->reg_cache, "pc", true);
2372 int retval = ERROR_OK;
2374 target_poll(target);
2375 if (target->state == TARGET_HALTED) {
2376 uint32_t t = buf_get_u32(reg->value, 0, 32);
2377 samples[sample_count++] = t;
2378 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2379 retval = target_resume(target, 1, 0, 0, 0);
2380 target_poll(target);
2381 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2382 } else if (target->state == TARGET_RUNNING) {
2383 /* We want to quickly sample the PC. */
2384 retval = target_halt(target);
2386 LOG_INFO("Target not halted or running");
2391 if (retval != ERROR_OK)
2394 gettimeofday(&now, NULL);
2395 if ((sample_count >= max_num_samples) || timeval_compare(&now, &timeout) >= 0) {
2396 LOG_INFO("Profiling completed. %" PRIu32 " samples.", sample_count);
2401 *num_samples = sample_count;
2405 /* Single aligned words are guaranteed to use 16 or 32 bit access
2406 * mode respectively, otherwise data is handled as quickly as
2409 int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
2411 LOG_DEBUG("writing buffer of %" PRIu32 " byte at " TARGET_ADDR_FMT,
2414 if (!target_was_examined(target)) {
2415 LOG_ERROR("Target not examined yet");
2422 if ((address + size - 1) < address) {
2423 /* GDB can request this when e.g. PC is 0xfffffffc */
2424 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT ", 0x%08" PRIx32 ")",
2430 return target->type->write_buffer(target, address, size, buffer);
2433 static int target_write_buffer_default(struct target *target,
2434 target_addr_t address, uint32_t count, const uint8_t *buffer)
2437 unsigned int data_bytes = target_data_bits(target) / 8;
2439 /* Align up to maximum bytes. The loop condition makes sure the next pass
2440 * will have something to do with the size we leave to it. */
2442 size < data_bytes && count >= size * 2 + (address & size);
2444 if (address & size) {
2445 int retval = target_write_memory(target, address, size, 1, buffer);
2446 if (retval != ERROR_OK)
2454 /* Write the data with as large access size as possible. */
2455 for (; size > 0; size /= 2) {
2456 uint32_t aligned = count - count % size;
2458 int retval = target_write_memory(target, address, size, aligned / size, buffer);
2459 if (retval != ERROR_OK)
2470 /* Single aligned words are guaranteed to use 16 or 32 bit access
2471 * mode respectively, otherwise data is handled as quickly as
2474 int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
2476 LOG_DEBUG("reading buffer of %" PRIu32 " byte at " TARGET_ADDR_FMT,
2479 if (!target_was_examined(target)) {
2480 LOG_ERROR("Target not examined yet");
2487 if ((address + size - 1) < address) {
2488 /* GDB can request this when e.g. PC is 0xfffffffc */
2489 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT ", 0x%08" PRIx32 ")",
2495 return target->type->read_buffer(target, address, size, buffer);
2498 static int target_read_buffer_default(struct target *target, target_addr_t address, uint32_t count, uint8_t *buffer)
2501 unsigned int data_bytes = target_data_bits(target) / 8;
2503 /* Align up to maximum bytes. The loop condition makes sure the next pass
2504 * will have something to do with the size we leave to it. */
2506 size < data_bytes && count >= size * 2 + (address & size);
2508 if (address & size) {
2509 int retval = target_read_memory(target, address, size, 1, buffer);
2510 if (retval != ERROR_OK)
2518 /* Read the data with as large access size as possible. */
2519 for (; size > 0; size /= 2) {
2520 uint32_t aligned = count - count % size;
2522 int retval = target_read_memory(target, address, size, aligned / size, buffer);
2523 if (retval != ERROR_OK)
2534 int target_checksum_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t *crc)
2539 uint32_t checksum = 0;
2540 if (!target_was_examined(target)) {
2541 LOG_ERROR("Target not examined yet");
2544 if (!target->type->checksum_memory) {
2545 LOG_ERROR("Target %s doesn't support checksum_memory", target_name(target));
2549 retval = target->type->checksum_memory(target, address, size, &checksum);
2550 if (retval != ERROR_OK) {
2551 buffer = malloc(size);
2553 LOG_ERROR("error allocating buffer for section (%" PRIu32 " bytes)", size);
2554 return ERROR_COMMAND_SYNTAX_ERROR;
2556 retval = target_read_buffer(target, address, size, buffer);
2557 if (retval != ERROR_OK) {
2562 /* convert to target endianness */
2563 for (i = 0; i < (size/sizeof(uint32_t)); i++) {
2564 uint32_t target_data;
2565 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
2566 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
2569 retval = image_calculate_checksum(buffer, size, &checksum);
2578 int target_blank_check_memory(struct target *target,
2579 struct target_memory_check_block *blocks, int num_blocks,
2580 uint8_t erased_value)
2582 if (!target_was_examined(target)) {
2583 LOG_ERROR("Target not examined yet");
2587 if (!target->type->blank_check_memory)
2588 return ERROR_NOT_IMPLEMENTED;
2590 return target->type->blank_check_memory(target, blocks, num_blocks, erased_value);
2593 int target_read_u64(struct target *target, target_addr_t address, uint64_t *value)
2595 uint8_t value_buf[8];
2596 if (!target_was_examined(target)) {
2597 LOG_ERROR("Target not examined yet");
2601 int retval = target_read_memory(target, address, 8, 1, value_buf);
2603 if (retval == ERROR_OK) {
2604 *value = target_buffer_get_u64(target, value_buf);
2605 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64 "",
2610 LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2617 int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
2619 uint8_t value_buf[4];
2620 if (!target_was_examined(target)) {
2621 LOG_ERROR("Target not examined yet");
2625 int retval = target_read_memory(target, address, 4, 1, value_buf);
2627 if (retval == ERROR_OK) {
2628 *value = target_buffer_get_u32(target, value_buf);
2629 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32 "",
2634 LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2641 int target_read_u16(struct target *target, target_addr_t address, uint16_t *value)
2643 uint8_t value_buf[2];
2644 if (!target_was_examined(target)) {
2645 LOG_ERROR("Target not examined yet");
2649 int retval = target_read_memory(target, address, 2, 1, value_buf);
2651 if (retval == ERROR_OK) {
2652 *value = target_buffer_get_u16(target, value_buf);
2653 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%4.4" PRIx16,
2658 LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2665 int target_read_u8(struct target *target, target_addr_t address, uint8_t *value)
2667 if (!target_was_examined(target)) {
2668 LOG_ERROR("Target not examined yet");
2672 int retval = target_read_memory(target, address, 1, 1, value);
2674 if (retval == ERROR_OK) {
2675 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
2680 LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2687 int target_write_u64(struct target *target, target_addr_t address, uint64_t value)
2690 uint8_t value_buf[8];
2691 if (!target_was_examined(target)) {
2692 LOG_ERROR("Target not examined yet");
2696 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64 "",
2700 target_buffer_set_u64(target, value_buf, value);
2701 retval = target_write_memory(target, address, 8, 1, value_buf);
2702 if (retval != ERROR_OK)
2703 LOG_DEBUG("failed: %i", retval);
2708 int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
2711 uint8_t value_buf[4];
2712 if (!target_was_examined(target)) {
2713 LOG_ERROR("Target not examined yet");
2717 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32 "",
2721 target_buffer_set_u32(target, value_buf, value);
2722 retval = target_write_memory(target, address, 4, 1, value_buf);
2723 if (retval != ERROR_OK)
2724 LOG_DEBUG("failed: %i", retval);
2729 int target_write_u16(struct target *target, target_addr_t address, uint16_t value)
2732 uint8_t value_buf[2];
2733 if (!target_was_examined(target)) {
2734 LOG_ERROR("Target not examined yet");
2738 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx16,
2742 target_buffer_set_u16(target, value_buf, value);
2743 retval = target_write_memory(target, address, 2, 1, value_buf);
2744 if (retval != ERROR_OK)
2745 LOG_DEBUG("failed: %i", retval);
2750 int target_write_u8(struct target *target, target_addr_t address, uint8_t value)
2753 if (!target_was_examined(target)) {
2754 LOG_ERROR("Target not examined yet");
2758 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
2761 retval = target_write_memory(target, address, 1, 1, &value);
2762 if (retval != ERROR_OK)
2763 LOG_DEBUG("failed: %i", retval);
2768 int target_write_phys_u64(struct target *target, target_addr_t address, uint64_t value)
2771 uint8_t value_buf[8];
2772 if (!target_was_examined(target)) {
2773 LOG_ERROR("Target not examined yet");
2777 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64 "",
2781 target_buffer_set_u64(target, value_buf, value);
2782 retval = target_write_phys_memory(target, address, 8, 1, value_buf);
2783 if (retval != ERROR_OK)
2784 LOG_DEBUG("failed: %i", retval);
2789 int target_write_phys_u32(struct target *target, target_addr_t address, uint32_t value)
2792 uint8_t value_buf[4];
2793 if (!target_was_examined(target)) {
2794 LOG_ERROR("Target not examined yet");
2798 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32 "",
2802 target_buffer_set_u32(target, value_buf, value);
2803 retval = target_write_phys_memory(target, address, 4, 1, value_buf);
2804 if (retval != ERROR_OK)
2805 LOG_DEBUG("failed: %i", retval);
2810 int target_write_phys_u16(struct target *target, target_addr_t address, uint16_t value)
2813 uint8_t value_buf[2];
2814 if (!target_was_examined(target)) {
2815 LOG_ERROR("Target not examined yet");
2819 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx16,
2823 target_buffer_set_u16(target, value_buf, value);
2824 retval = target_write_phys_memory(target, address, 2, 1, value_buf);
2825 if (retval != ERROR_OK)
2826 LOG_DEBUG("failed: %i", retval);
2831 int target_write_phys_u8(struct target *target, target_addr_t address, uint8_t value)
2834 if (!target_was_examined(target)) {
2835 LOG_ERROR("Target not examined yet");
2839 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
2842 retval = target_write_phys_memory(target, address, 1, 1, &value);
2843 if (retval != ERROR_OK)
2844 LOG_DEBUG("failed: %i", retval);
2849 static int find_target(struct command_invocation *cmd, const char *name)
2851 struct target *target = get_target(name);
2853 command_print(cmd, "Target: %s is unknown, try one of:\n", name);
2856 if (!target->tap->enabled) {
2857 command_print(cmd, "Target: TAP %s is disabled, "
2858 "can't be the current target\n",
2859 target->tap->dotted_name);
2863 cmd->ctx->current_target = target;
2864 if (cmd->ctx->current_target_override)
2865 cmd->ctx->current_target_override = target;
2871 COMMAND_HANDLER(handle_targets_command)
2873 int retval = ERROR_OK;
2874 if (CMD_ARGC == 1) {
2875 retval = find_target(CMD, CMD_ARGV[0]);
2876 if (retval == ERROR_OK) {
2882 struct target *target = all_targets;
2883 command_print(CMD, " TargetName Type Endian TapName State ");
2884 command_print(CMD, "-- ------------------ ---------- ------ ------------------ ------------");
2889 if (target->tap->enabled)
2890 state = target_state_name(target);
2892 state = "tap-disabled";
2894 if (CMD_CTX->current_target == target)
2897 /* keep columns lined up to match the headers above */
2899 "%2d%c %-18s %-10s %-6s %-18s %s",
2900 target->target_number,
2902 target_name(target),
2903 target_type_name(target),
2904 jim_nvp_value2name_simple(nvp_target_endian,
2905 target->endianness)->name,
2906 target->tap->dotted_name,
2908 target = target->next;
2914 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2916 static int power_dropout;
2917 static int srst_asserted;
2919 static int run_power_restore;
2920 static int run_power_dropout;
2921 static int run_srst_asserted;
2922 static int run_srst_deasserted;
2924 static int sense_handler(void)
2926 static int prev_srst_asserted;
2927 static int prev_power_dropout;
2929 int retval = jtag_power_dropout(&power_dropout);
2930 if (retval != ERROR_OK)
2934 power_restored = prev_power_dropout && !power_dropout;
2936 run_power_restore = 1;
2938 int64_t current = timeval_ms();
2939 static int64_t last_power;
2940 bool wait_more = last_power + 2000 > current;
2941 if (power_dropout && !wait_more) {
2942 run_power_dropout = 1;
2943 last_power = current;
2946 retval = jtag_srst_asserted(&srst_asserted);
2947 if (retval != ERROR_OK)
2950 int srst_deasserted;
2951 srst_deasserted = prev_srst_asserted && !srst_asserted;
2953 static int64_t last_srst;
2954 wait_more = last_srst + 2000 > current;
2955 if (srst_deasserted && !wait_more) {
2956 run_srst_deasserted = 1;
2957 last_srst = current;
2960 if (!prev_srst_asserted && srst_asserted)
2961 run_srst_asserted = 1;
2963 prev_srst_asserted = srst_asserted;
2964 prev_power_dropout = power_dropout;
2966 if (srst_deasserted || power_restored) {
2967 /* Other than logging the event we can't do anything here.
2968 * Issuing a reset is a particularly bad idea as we might
2969 * be inside a reset already.
2976 /* process target state changes */
2977 static int handle_target(void *priv)
2979 Jim_Interp *interp = (Jim_Interp *)priv;
2980 int retval = ERROR_OK;
2982 if (!is_jtag_poll_safe()) {
2983 /* polling is disabled currently */
2987 /* we do not want to recurse here... */
2988 static int recursive;
2992 /* danger! running these procedures can trigger srst assertions and power dropouts.
2993 * We need to avoid an infinite loop/recursion here and we do that by
2994 * clearing the flags after running these events.
2996 int did_something = 0;
2997 if (run_srst_asserted) {
2998 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2999 Jim_Eval(interp, "srst_asserted");
3002 if (run_srst_deasserted) {
3003 Jim_Eval(interp, "srst_deasserted");
3006 if (run_power_dropout) {
3007 LOG_INFO("Power dropout detected, running power_dropout proc.");
3008 Jim_Eval(interp, "power_dropout");
3011 if (run_power_restore) {
3012 Jim_Eval(interp, "power_restore");
3016 if (did_something) {
3017 /* clear detect flags */
3021 /* clear action flags */
3023 run_srst_asserted = 0;
3024 run_srst_deasserted = 0;
3025 run_power_restore = 0;
3026 run_power_dropout = 0;
3031 /* Poll targets for state changes unless that's globally disabled.
3032 * Skip targets that are currently disabled.
3034 for (struct target *target = all_targets;
3035 is_jtag_poll_safe() && target;
3036 target = target->next) {
3038 if (!target_was_examined(target))
3041 if (!target->tap->enabled)
3044 if (target->backoff.times > target->backoff.count) {
3045 /* do not poll this time as we failed previously */
3046 target->backoff.count++;
3049 target->backoff.count = 0;
3051 /* only poll target if we've got power and srst isn't asserted */
3052 if (!power_dropout && !srst_asserted) {
3053 /* polling may fail silently until the target has been examined */
3054 retval = target_poll(target);
3055 if (retval != ERROR_OK) {
3056 /* 100ms polling interval. Increase interval between polling up to 5000ms */
3057 if (target->backoff.times * polling_interval < 5000) {
3058 target->backoff.times *= 2;
3059 target->backoff.times++;
3062 /* Tell GDB to halt the debugger. This allows the user to
3063 * run monitor commands to handle the situation.
3065 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
3067 if (target->backoff.times > 0) {
3068 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target));
3069 target_reset_examined(target);
3070 retval = target_examine_one(target);
3071 /* Target examination could have failed due to unstable connection,
3072 * but we set the examined flag anyway to repoll it later */
3073 if (retval != ERROR_OK) {
3074 target_set_examined(target);
3075 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
3076 target->backoff.times * polling_interval);
3081 /* Since we succeeded, we reset backoff count */
3082 target->backoff.times = 0;
3089 COMMAND_HANDLER(handle_reg_command)
3093 struct target *target = get_current_target(CMD_CTX);
3094 struct reg *reg = NULL;
3096 /* list all available registers for the current target */
3097 if (CMD_ARGC == 0) {
3098 struct reg_cache *cache = target->reg_cache;
3100 unsigned int count = 0;
3104 command_print(CMD, "===== %s", cache->name);
3106 for (i = 0, reg = cache->reg_list;
3107 i < cache->num_regs;
3108 i++, reg++, count++) {
3109 if (reg->exist == false || reg->hidden)
3111 /* only print cached values if they are valid */
3113 char *value = buf_to_hex_str(reg->value,
3116 "(%i) %s (/%" PRIu32 "): 0x%s%s",
3124 command_print(CMD, "(%i) %s (/%" PRIu32 ")",
3129 cache = cache->next;
3135 /* access a single register by its ordinal number */
3136 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9')) {
3138 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
3140 struct reg_cache *cache = target->reg_cache;
3141 unsigned int count = 0;
3144 for (i = 0; i < cache->num_regs; i++) {
3145 if (count++ == num) {
3146 reg = &cache->reg_list[i];
3152 cache = cache->next;
3156 command_print(CMD, "%i is out of bounds, the current target "
3157 "has only %i registers (0 - %i)", num, count, count - 1);
3161 /* access a single register by its name */
3162 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], true);
3168 assert(reg); /* give clang a hint that we *know* reg is != NULL here */
3173 /* display a register */
3174 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0')
3175 && (CMD_ARGV[1][0] <= '9')))) {
3176 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
3179 if (reg->valid == 0) {
3180 int retval = reg->type->get(reg);
3181 if (retval != ERROR_OK) {
3182 LOG_ERROR("Could not read register '%s'", reg->name);
3186 char *value = buf_to_hex_str(reg->value, reg->size);
3187 command_print(CMD, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
3192 /* set register value */
3193 if (CMD_ARGC == 2) {
3194 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
3197 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
3199 int retval = reg->type->set(reg, buf);
3200 if (retval != ERROR_OK) {
3201 LOG_ERROR("Could not write to register '%s'", reg->name);
3203 char *value = buf_to_hex_str(reg->value, reg->size);
3204 command_print(CMD, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
3213 return ERROR_COMMAND_SYNTAX_ERROR;
3216 command_print(CMD, "register %s not found in current target", CMD_ARGV[0]);
3220 COMMAND_HANDLER(handle_poll_command)
3222 int retval = ERROR_OK;
3223 struct target *target = get_current_target(CMD_CTX);
3225 if (CMD_ARGC == 0) {
3226 command_print(CMD, "background polling: %s",
3227 jtag_poll_get_enabled() ? "on" : "off");
3228 command_print(CMD, "TAP: %s (%s)",
3229 target->tap->dotted_name,
3230 target->tap->enabled ? "enabled" : "disabled");
3231 if (!target->tap->enabled)
3233 retval = target_poll(target);
3234 if (retval != ERROR_OK)
3236 retval = target_arch_state(target);
3237 if (retval != ERROR_OK)
3239 } else if (CMD_ARGC == 1) {
3241 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
3242 jtag_poll_set_enabled(enable);
3244 return ERROR_COMMAND_SYNTAX_ERROR;
3249 COMMAND_HANDLER(handle_wait_halt_command)
3252 return ERROR_COMMAND_SYNTAX_ERROR;
3254 unsigned ms = DEFAULT_HALT_TIMEOUT;
3255 if (1 == CMD_ARGC) {
3256 int retval = parse_uint(CMD_ARGV[0], &ms);
3257 if (retval != ERROR_OK)
3258 return ERROR_COMMAND_SYNTAX_ERROR;
3261 struct target *target = get_current_target(CMD_CTX);
3262 return target_wait_state(target, TARGET_HALTED, ms);
3265 /* wait for target state to change. The trick here is to have a low
3266 * latency for short waits and not to suck up all the CPU time
3269 * After 500ms, keep_alive() is invoked
3271 int target_wait_state(struct target *target, enum target_state state, int ms)
3274 int64_t then = 0, cur;
3278 retval = target_poll(target);
3279 if (retval != ERROR_OK)
3281 if (target->state == state)
3286 then = timeval_ms();
3287 LOG_DEBUG("waiting for target %s...",
3288 jim_nvp_value2name_simple(nvp_target_state, state)->name);
3294 if ((cur-then) > ms) {
3295 LOG_ERROR("timed out while waiting for target %s",
3296 jim_nvp_value2name_simple(nvp_target_state, state)->name);
3304 COMMAND_HANDLER(handle_halt_command)
3308 struct target *target = get_current_target(CMD_CTX);
3310 target->verbose_halt_msg = true;
3312 int retval = target_halt(target);
3313 if (retval != ERROR_OK)
3316 if (CMD_ARGC == 1) {
3317 unsigned wait_local;
3318 retval = parse_uint(CMD_ARGV[0], &wait_local);
3319 if (retval != ERROR_OK)
3320 return ERROR_COMMAND_SYNTAX_ERROR;
3325 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
3328 COMMAND_HANDLER(handle_soft_reset_halt_command)
3330 struct target *target = get_current_target(CMD_CTX);
3332 LOG_TARGET_INFO(target, "requesting target halt and executing a soft reset");
3334 target_soft_reset_halt(target);
3339 COMMAND_HANDLER(handle_reset_command)
3342 return ERROR_COMMAND_SYNTAX_ERROR;
3344 enum target_reset_mode reset_mode = RESET_RUN;
3345 if (CMD_ARGC == 1) {
3346 const struct jim_nvp *n;
3347 n = jim_nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
3348 if ((!n->name) || (n->value == RESET_UNKNOWN))
3349 return ERROR_COMMAND_SYNTAX_ERROR;
3350 reset_mode = n->value;
3353 /* reset *all* targets */
3354 return target_process_reset(CMD, reset_mode);
3358 COMMAND_HANDLER(handle_resume_command)
3362 return ERROR_COMMAND_SYNTAX_ERROR;
3364 struct target *target = get_current_target(CMD_CTX);
3366 /* with no CMD_ARGV, resume from current pc, addr = 0,
3367 * with one arguments, addr = CMD_ARGV[0],
3368 * handle breakpoints, not debugging */
3369 target_addr_t addr = 0;
3370 if (CMD_ARGC == 1) {
3371 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
3375 return target_resume(target, current, addr, 1, 0);
3378 COMMAND_HANDLER(handle_step_command)
3381 return ERROR_COMMAND_SYNTAX_ERROR;
3385 /* with no CMD_ARGV, step from current pc, addr = 0,
3386 * with one argument addr = CMD_ARGV[0],
3387 * handle breakpoints, debugging */
3388 target_addr_t addr = 0;
3390 if (CMD_ARGC == 1) {
3391 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
3395 struct target *target = get_current_target(CMD_CTX);
3397 return target_step(target, current_pc, addr, 1);
3400 void target_handle_md_output(struct command_invocation *cmd,
3401 struct target *target, target_addr_t address, unsigned size,
3402 unsigned count, const uint8_t *buffer)
3404 const unsigned line_bytecnt = 32;
3405 unsigned line_modulo = line_bytecnt / size;
3407 char output[line_bytecnt * 4 + 1];
3408 unsigned output_len = 0;
3410 const char *value_fmt;
3413 value_fmt = "%16.16"PRIx64" ";
3416 value_fmt = "%8.8"PRIx64" ";
3419 value_fmt = "%4.4"PRIx64" ";
3422 value_fmt = "%2.2"PRIx64" ";
3425 /* "can't happen", caller checked */
3426 LOG_ERROR("invalid memory read size: %u", size);
3430 for (unsigned i = 0; i < count; i++) {
3431 if (i % line_modulo == 0) {
3432 output_len += snprintf(output + output_len,
3433 sizeof(output) - output_len,
3434 TARGET_ADDR_FMT ": ",
3435 (address + (i * size)));
3439 const uint8_t *value_ptr = buffer + i * size;
3442 value = target_buffer_get_u64(target, value_ptr);
3445 value = target_buffer_get_u32(target, value_ptr);
3448 value = target_buffer_get_u16(target, value_ptr);
3453 output_len += snprintf(output + output_len,
3454 sizeof(output) - output_len,
3457 if ((i % line_modulo == line_modulo - 1) || (i == count - 1)) {
3458 command_print(cmd, "%s", output);
3464 COMMAND_HANDLER(handle_md_command)
3467 return ERROR_COMMAND_SYNTAX_ERROR;
3470 switch (CMD_NAME[2]) {
3484 return ERROR_COMMAND_SYNTAX_ERROR;
3487 bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
3488 int (*fn)(struct target *target,
3489 target_addr_t address, uint32_t size_value, uint32_t count, uint8_t *buffer);
3493 fn = target_read_phys_memory;
3495 fn = target_read_memory;
3496 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
3497 return ERROR_COMMAND_SYNTAX_ERROR;
3499 target_addr_t address;
3500 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], address);
3504 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
3506 uint8_t *buffer = calloc(count, size);
3508 LOG_ERROR("Failed to allocate md read buffer");
3512 struct target *target = get_current_target(CMD_CTX);
3513 int retval = fn(target, address, size, count, buffer);
3514 if (retval == ERROR_OK)
3515 target_handle_md_output(CMD, target, address, size, count, buffer);
3522 typedef int (*target_write_fn)(struct target *target,
3523 target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer);
3525 static int target_fill_mem(struct target *target,
3526 target_addr_t address,
3534 /* We have to write in reasonably large chunks to be able
3535 * to fill large memory areas with any sane speed */
3536 const unsigned chunk_size = 16384;
3537 uint8_t *target_buf = malloc(chunk_size * data_size);
3539 LOG_ERROR("Out of memory");
3543 for (unsigned i = 0; i < chunk_size; i++) {
3544 switch (data_size) {
3546 target_buffer_set_u64(target, target_buf + i * data_size, b);
3549 target_buffer_set_u32(target, target_buf + i * data_size, b);
3552 target_buffer_set_u16(target, target_buf + i * data_size, b);
3555 target_buffer_set_u8(target, target_buf + i * data_size, b);
3562 int retval = ERROR_OK;
3564 for (unsigned x = 0; x < c; x += chunk_size) {
3567 if (current > chunk_size)
3568 current = chunk_size;
3569 retval = fn(target, address + x * data_size, data_size, current, target_buf);
3570 if (retval != ERROR_OK)
3572 /* avoid GDB timeouts */
3581 COMMAND_HANDLER(handle_mw_command)
3584 return ERROR_COMMAND_SYNTAX_ERROR;
3585 bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
3590 fn = target_write_phys_memory;
3592 fn = target_write_memory;
3593 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
3594 return ERROR_COMMAND_SYNTAX_ERROR;
3596 target_addr_t address;
3597 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], address);
3600 COMMAND_PARSE_NUMBER(u64, CMD_ARGV[1], value);
3604 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
3606 struct target *target = get_current_target(CMD_CTX);
3608 switch (CMD_NAME[2]) {
3622 return ERROR_COMMAND_SYNTAX_ERROR;
3625 return target_fill_mem(target, address, fn, wordsize, value, count);
3628 static COMMAND_HELPER(parse_load_image_command, struct image *image,
3629 target_addr_t *min_address, target_addr_t *max_address)
3631 if (CMD_ARGC < 1 || CMD_ARGC > 5)
3632 return ERROR_COMMAND_SYNTAX_ERROR;
3634 /* a base address isn't always necessary,
3635 * default to 0x0 (i.e. don't relocate) */
3636 if (CMD_ARGC >= 2) {
3638 COMMAND_PARSE_ADDRESS(CMD_ARGV[1], addr);
3639 image->base_address = addr;
3640 image->base_address_set = true;
3642 image->base_address_set = false;
3644 image->start_address_set = false;
3647 COMMAND_PARSE_ADDRESS(CMD_ARGV[3], *min_address);
3648 if (CMD_ARGC == 5) {
3649 COMMAND_PARSE_ADDRESS(CMD_ARGV[4], *max_address);
3650 /* use size (given) to find max (required) */
3651 *max_address += *min_address;
3654 if (*min_address > *max_address)
3655 return ERROR_COMMAND_SYNTAX_ERROR;
3660 COMMAND_HANDLER(handle_load_image_command)
3664 uint32_t image_size;
3665 target_addr_t min_address = 0;
3666 target_addr_t max_address = -1;
3669 int retval = CALL_COMMAND_HANDLER(parse_load_image_command,
3670 &image, &min_address, &max_address);
3671 if (retval != ERROR_OK)
3674 struct target *target = get_current_target(CMD_CTX);
3676 struct duration bench;
3677 duration_start(&bench);
3679 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
3684 for (unsigned int i = 0; i < image.num_sections; i++) {
3685 buffer = malloc(image.sections[i].size);
3688 "error allocating buffer for section (%d bytes)",
3689 (int)(image.sections[i].size));
3690 retval = ERROR_FAIL;
3694 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
3695 if (retval != ERROR_OK) {
3700 uint32_t offset = 0;
3701 uint32_t length = buf_cnt;
3703 /* DANGER!!! beware of unsigned comparison here!!! */
3705 if ((image.sections[i].base_address + buf_cnt >= min_address) &&
3706 (image.sections[i].base_address < max_address)) {
3708 if (image.sections[i].base_address < min_address) {
3709 /* clip addresses below */
3710 offset += min_address-image.sections[i].base_address;
3714 if (image.sections[i].base_address + buf_cnt > max_address)
3715 length -= (image.sections[i].base_address + buf_cnt)-max_address;
3717 retval = target_write_buffer(target,
3718 image.sections[i].base_address + offset, length, buffer + offset);
3719 if (retval != ERROR_OK) {
3723 image_size += length;
3724 command_print(CMD, "%u bytes written at address " TARGET_ADDR_FMT "",
3725 (unsigned int)length,
3726 image.sections[i].base_address + offset);
3732 if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) {
3733 command_print(CMD, "downloaded %" PRIu32 " bytes "
3734 "in %fs (%0.3f KiB/s)", image_size,
3735 duration_elapsed(&bench), duration_kbps(&bench, image_size));
3738 image_close(&image);
3744 COMMAND_HANDLER(handle_dump_image_command)
3746 struct fileio *fileio;
3748 int retval, retvaltemp;
3749 target_addr_t address, size;
3750 struct duration bench;
3751 struct target *target = get_current_target(CMD_CTX);
3754 return ERROR_COMMAND_SYNTAX_ERROR;
3756 COMMAND_PARSE_ADDRESS(CMD_ARGV[1], address);
3757 COMMAND_PARSE_ADDRESS(CMD_ARGV[2], size);
3759 uint32_t buf_size = (size > 4096) ? 4096 : size;
3760 buffer = malloc(buf_size);
3764 retval = fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY);
3765 if (retval != ERROR_OK) {
3770 duration_start(&bench);
3773 size_t size_written;
3774 uint32_t this_run_size = (size > buf_size) ? buf_size : size;
3775 retval = target_read_buffer(target, address, this_run_size, buffer);
3776 if (retval != ERROR_OK)
3779 retval = fileio_write(fileio, this_run_size, buffer, &size_written);
3780 if (retval != ERROR_OK)
3783 size -= this_run_size;
3784 address += this_run_size;
3789 if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) {
3791 retval = fileio_size(fileio, &filesize);
3792 if (retval != ERROR_OK)
3795 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize,
3796 duration_elapsed(&bench), duration_kbps(&bench, filesize));
3799 retvaltemp = fileio_close(fileio);
3800 if (retvaltemp != ERROR_OK)
3809 IMAGE_CHECKSUM_ONLY = 2
3812 static COMMAND_HELPER(handle_verify_image_command_internal, enum verify_mode verify)
3816 uint32_t image_size;
3818 uint32_t checksum = 0;
3819 uint32_t mem_checksum = 0;
3823 struct target *target = get_current_target(CMD_CTX);
3826 return ERROR_COMMAND_SYNTAX_ERROR;
3829 LOG_ERROR("no target selected");
3833 struct duration bench;
3834 duration_start(&bench);
3836 if (CMD_ARGC >= 2) {
3838 COMMAND_PARSE_ADDRESS(CMD_ARGV[1], addr);
3839 image.base_address = addr;
3840 image.base_address_set = true;
3842 image.base_address_set = false;
3843 image.base_address = 0x0;
3846 image.start_address_set = false;
3848 retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL);
3849 if (retval != ERROR_OK)
3855 for (unsigned int i = 0; i < image.num_sections; i++) {
3856 buffer = malloc(image.sections[i].size);
3859 "error allocating buffer for section (%" PRIu32 " bytes)",
3860 image.sections[i].size);
3863 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
3864 if (retval != ERROR_OK) {
3869 if (verify >= IMAGE_VERIFY) {
3870 /* calculate checksum of image */
3871 retval = image_calculate_checksum(buffer, buf_cnt, &checksum);
3872 if (retval != ERROR_OK) {
3877 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
3878 if (retval != ERROR_OK) {
3882 if ((checksum != mem_checksum) && (verify == IMAGE_CHECKSUM_ONLY)) {
3883 LOG_ERROR("checksum mismatch");
3885 retval = ERROR_FAIL;
3888 if (checksum != mem_checksum) {
3889 /* failed crc checksum, fall back to a binary compare */
3893 LOG_ERROR("checksum mismatch - attempting binary compare");
3895 data = malloc(buf_cnt);
3897 retval = target_read_buffer(target, image.sections[i].base_address, buf_cnt, data);
3898 if (retval == ERROR_OK) {
3900 for (t = 0; t < buf_cnt; t++) {
3901 if (data[t] != buffer[t]) {
3903 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3905 (unsigned)(t + image.sections[i].base_address),
3908 if (diffs++ >= 127) {
3909 command_print(CMD, "More than 128 errors, the rest are not printed.");
3921 command_print(CMD, "address " TARGET_ADDR_FMT " length 0x%08zx",
3922 image.sections[i].base_address,
3927 image_size += buf_cnt;
3930 command_print(CMD, "No more differences found.");
3933 retval = ERROR_FAIL;
3934 if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) {
3935 command_print(CMD, "verified %" PRIu32 " bytes "
3936 "in %fs (%0.3f KiB/s)", image_size,
3937 duration_elapsed(&bench), duration_kbps(&bench, image_size));
3940 image_close(&image);
3945 COMMAND_HANDLER(handle_verify_image_checksum_command)
3947 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_CHECKSUM_ONLY);
3950 COMMAND_HANDLER(handle_verify_image_command)
3952 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_VERIFY);
3955 COMMAND_HANDLER(handle_test_image_command)
3957 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_TEST);
3960 static int handle_bp_command_list(struct command_invocation *cmd)
3962 struct target *target = get_current_target(cmd->ctx);
3963 struct breakpoint *breakpoint = target->breakpoints;
3964 while (breakpoint) {
3965 if (breakpoint->type == BKPT_SOFT) {
3966 char *buf = buf_to_hex_str(breakpoint->orig_instr,
3967 breakpoint->length);
3968 command_print(cmd, "IVA breakpoint: " TARGET_ADDR_FMT ", 0x%x, 0x%s",
3969 breakpoint->address,
3974 if ((breakpoint->address == 0) && (breakpoint->asid != 0))
3975 command_print(cmd, "Context breakpoint: 0x%8.8" PRIx32 ", 0x%x, %u",
3977 breakpoint->length, breakpoint->number);
3978 else if ((breakpoint->address != 0) && (breakpoint->asid != 0)) {
3979 command_print(cmd, "Hybrid breakpoint(IVA): " TARGET_ADDR_FMT ", 0x%x, %u",
3980 breakpoint->address,
3981 breakpoint->length, breakpoint->number);
3982 command_print(cmd, "\t|--->linked with ContextID: 0x%8.8" PRIx32,
3985 command_print(cmd, "Breakpoint(IVA): " TARGET_ADDR_FMT ", 0x%x, %u",
3986 breakpoint->address,
3987 breakpoint->length, breakpoint->number);
3990 breakpoint = breakpoint->next;
3995 static int handle_bp_command_set(struct command_invocation *cmd,
3996 target_addr_t addr, uint32_t asid, uint32_t length, int hw)
3998 struct target *target = get_current_target(cmd->ctx);
4002 retval = breakpoint_add(target, addr, length, hw);
4003 /* error is always logged in breakpoint_add(), do not print it again */
4004 if (retval == ERROR_OK)
4005 command_print(cmd, "breakpoint set at " TARGET_ADDR_FMT "", addr);
4007 } else if (addr == 0) {
4008 if (!target->type->add_context_breakpoint) {
4009 LOG_ERROR("Context breakpoint not available");
4010 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
4012 retval = context_breakpoint_add(target, asid, length, hw);
4013 /* error is always logged in context_breakpoint_add(), do not print it again */
4014 if (retval == ERROR_OK)
4015 command_print(cmd, "Context breakpoint set at 0x%8.8" PRIx32 "", asid);
4018 if (!target->type->add_hybrid_breakpoint) {
4019 LOG_ERROR("Hybrid breakpoint not available");
4020 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
4022 retval = hybrid_breakpoint_add(target, addr, asid, length, hw);
4023 /* error is always logged in hybrid_breakpoint_add(), do not print it again */
4024 if (retval == ERROR_OK)
4025 command_print(cmd, "Hybrid breakpoint set at 0x%8.8" PRIx32 "", asid);
4030 COMMAND_HANDLER(handle_bp_command)
4039 return handle_bp_command_list(CMD);
4043 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
4044 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
4045 return handle_bp_command_set(CMD, addr, asid, length, hw);
4048 if (strcmp(CMD_ARGV[2], "hw") == 0) {
4050 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
4051 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
4053 return handle_bp_command_set(CMD, addr, asid, length, hw);
4054 } else if (strcmp(CMD_ARGV[2], "hw_ctx") == 0) {
4056 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], asid);
4057 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
4059 return handle_bp_command_set(CMD, addr, asid, length, hw);
4064 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
4065 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], asid);
4066 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], length);
4067 return handle_bp_command_set(CMD, addr, asid, length, hw);
4070 return ERROR_COMMAND_SYNTAX_ERROR;
4074 COMMAND_HANDLER(handle_rbp_command)
4077 return ERROR_COMMAND_SYNTAX_ERROR;
4079 struct target *target = get_current_target(CMD_CTX);
4081 if (!strcmp(CMD_ARGV[0], "all")) {
4082 breakpoint_remove_all(target);
4085 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
4087 breakpoint_remove(target, addr);
4093 COMMAND_HANDLER(handle_wp_command)
4095 struct target *target = get_current_target(CMD_CTX);
4097 if (CMD_ARGC == 0) {
4098 struct watchpoint *watchpoint = target->watchpoints;
4100 while (watchpoint) {
4101 command_print(CMD, "address: " TARGET_ADDR_FMT
4102 ", len: 0x%8.8" PRIx32
4103 ", r/w/a: %i, value: 0x%8.8" PRIx32
4104 ", mask: 0x%8.8" PRIx32,
4105 watchpoint->address,
4107 (int)watchpoint->rw,
4110 watchpoint = watchpoint->next;
4115 enum watchpoint_rw type = WPT_ACCESS;
4116 target_addr_t addr = 0;
4117 uint32_t length = 0;
4118 uint32_t data_value = 0x0;
4119 uint32_t data_mask = 0xffffffff;
4123 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
4126 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
4129 switch (CMD_ARGV[2][0]) {
4140 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
4141 return ERROR_COMMAND_SYNTAX_ERROR;
4145 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
4146 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
4150 return ERROR_COMMAND_SYNTAX_ERROR;
4153 int retval = watchpoint_add(target, addr, length, type,
4154 data_value, data_mask);
4155 if (retval != ERROR_OK)
4156 LOG_ERROR("Failure setting watchpoints");
4161 COMMAND_HANDLER(handle_rwp_command)
4164 return ERROR_COMMAND_SYNTAX_ERROR;
4167 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
4169 struct target *target = get_current_target(CMD_CTX);
4170 watchpoint_remove(target, addr);
4176 * Translate a virtual address to a physical address.
4178 * The low-level target implementation must have logged a detailed error
4179 * which is forwarded to telnet/GDB session.
4181 COMMAND_HANDLER(handle_virt2phys_command)
4184 return ERROR_COMMAND_SYNTAX_ERROR;
4187 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], va);
4190 struct target *target = get_current_target(CMD_CTX);
4191 int retval = target->type->virt2phys(target, va, &pa);
4192 if (retval == ERROR_OK)
4193 command_print(CMD, "Physical address " TARGET_ADDR_FMT "", pa);
4198 static void write_data(FILE *f, const void *data, size_t len)
4200 size_t written = fwrite(data, 1, len, f);
4202 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
4205 static void write_long(FILE *f, int l, struct target *target)
4209 target_buffer_set_u32(target, val, l);
4210 write_data(f, val, 4);
4213 static void write_string(FILE *f, char *s)
4215 write_data(f, s, strlen(s));
4218 typedef unsigned char UNIT[2]; /* unit of profiling */
4220 /* Dump a gmon.out histogram file. */
4221 static void write_gmon(uint32_t *samples, uint32_t sample_num, const char *filename, bool with_range,
4222 uint32_t start_address, uint32_t end_address, struct target *target, uint32_t duration_ms)
4225 FILE *f = fopen(filename, "w");
4228 write_string(f, "gmon");
4229 write_long(f, 0x00000001, target); /* Version */
4230 write_long(f, 0, target); /* padding */
4231 write_long(f, 0, target); /* padding */
4232 write_long(f, 0, target); /* padding */
4234 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
4235 write_data(f, &zero, 1);
4237 /* figure out bucket size */
4241 min = start_address;
4246 for (i = 0; i < sample_num; i++) {
4247 if (min > samples[i])
4249 if (max < samples[i])
4253 /* max should be (largest sample + 1)
4254 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
4258 int address_space = max - min;
4259 assert(address_space >= 2);
4261 /* FIXME: What is the reasonable number of buckets?
4262 * The profiling result will be more accurate if there are enough buckets. */
4263 static const uint32_t max_buckets = 128 * 1024; /* maximum buckets. */
4264 uint32_t num_buckets = address_space / sizeof(UNIT);
4265 if (num_buckets > max_buckets)
4266 num_buckets = max_buckets;
4267 int *buckets = malloc(sizeof(int) * num_buckets);
4272 memset(buckets, 0, sizeof(int) * num_buckets);
4273 for (i = 0; i < sample_num; i++) {
4274 uint32_t address = samples[i];
4276 if ((address < min) || (max <= address))
4279 long long a = address - min;
4280 long long b = num_buckets;
4281 long long c = address_space;
4282 int index_t = (a * b) / c; /* danger!!!! int32 overflows */
4286 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
4287 write_long(f, min, target); /* low_pc */
4288 write_long(f, max, target); /* high_pc */
4289 write_long(f, num_buckets, target); /* # of buckets */
4290 float sample_rate = sample_num / (duration_ms / 1000.0);
4291 write_long(f, sample_rate, target);
4292 write_string(f, "seconds");
4293 for (i = 0; i < (15-strlen("seconds")); i++)
4294 write_data(f, &zero, 1);
4295 write_string(f, "s");
4297 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
4299 char *data = malloc(2 * num_buckets);
4301 for (i = 0; i < num_buckets; i++) {
4306 data[i * 2] = val&0xff;
4307 data[i * 2 + 1] = (val >> 8) & 0xff;
4310 write_data(f, data, num_buckets * 2);
4318 /* profiling samples the CPU PC as quickly as OpenOCD is able,
4319 * which will be used as a random sampling of PC */
4320 COMMAND_HANDLER(handle_profile_command)
4322 struct target *target = get_current_target(CMD_CTX);
4324 if ((CMD_ARGC != 2) && (CMD_ARGC != 4))
4325 return ERROR_COMMAND_SYNTAX_ERROR;
4327 const uint32_t MAX_PROFILE_SAMPLE_NUM = 10000;
4329 uint32_t num_of_samples;
4330 int retval = ERROR_OK;
4331 bool halted_before_profiling = target->state == TARGET_HALTED;
4333 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], offset);
4335 uint32_t *samples = malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM);
4337 LOG_ERROR("No memory to store samples.");
4341 uint64_t timestart_ms = timeval_ms();
4343 * Some cores let us sample the PC without the
4344 * annoying halt/resume step; for example, ARMv7 PCSR.
4345 * Provide a way to use that more efficient mechanism.
4347 retval = target_profiling(target, samples, MAX_PROFILE_SAMPLE_NUM,
4348 &num_of_samples, offset);
4349 if (retval != ERROR_OK) {
4353 uint32_t duration_ms = timeval_ms() - timestart_ms;
4355 assert(num_of_samples <= MAX_PROFILE_SAMPLE_NUM);
4357 retval = target_poll(target);
4358 if (retval != ERROR_OK) {
4363 if (target->state == TARGET_RUNNING && halted_before_profiling) {
4364 /* The target was halted before we started and is running now. Halt it,
4365 * for consistency. */
4366 retval = target_halt(target);
4367 if (retval != ERROR_OK) {
4371 } else if (target->state == TARGET_HALTED && !halted_before_profiling) {
4372 /* The target was running before we started and is halted now. Resume
4373 * it, for consistency. */
4374 retval = target_resume(target, 1, 0, 0, 0);
4375 if (retval != ERROR_OK) {
4381 retval = target_poll(target);
4382 if (retval != ERROR_OK) {
4387 uint32_t start_address = 0;
4388 uint32_t end_address = 0;
4389 bool with_range = false;
4390 if (CMD_ARGC == 4) {
4392 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], start_address);
4393 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], end_address);
4396 write_gmon(samples, num_of_samples, CMD_ARGV[1],
4397 with_range, start_address, end_address, target, duration_ms);
4398 command_print(CMD, "Wrote %s", CMD_ARGV[1]);
4404 static int new_u64_array_element(Jim_Interp *interp, const char *varname, int idx, uint64_t val)
4407 Jim_Obj *obj_name, *obj_val;
4410 namebuf = alloc_printf("%s(%d)", varname, idx);
4414 obj_name = Jim_NewStringObj(interp, namebuf, -1);
4415 jim_wide wide_val = val;
4416 obj_val = Jim_NewWideObj(interp, wide_val);
4417 if (!obj_name || !obj_val) {
4422 Jim_IncrRefCount(obj_name);
4423 Jim_IncrRefCount(obj_val);
4424 result = Jim_SetVariable(interp, obj_name, obj_val);
4425 Jim_DecrRefCount(interp, obj_name);
4426 Jim_DecrRefCount(interp, obj_val);
4428 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
4432 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
4436 LOG_WARNING("DEPRECATED! use 'read_memory' not 'mem2array'");
4438 /* argv[0] = name of array to receive the data
4439 * argv[1] = desired element width in bits
4440 * argv[2] = memory address
4441 * argv[3] = count of times to read
4442 * argv[4] = optional "phys"
4444 if (argc < 4 || argc > 5) {
4445 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems [phys]");
4449 /* Arg 0: Name of the array variable */
4450 const char *varname = Jim_GetString(argv[0], NULL);
4452 /* Arg 1: Bit width of one element */
4454 e = Jim_GetLong(interp, argv[1], &l);
4457 const unsigned int width_bits = l;
4459 if (width_bits != 8 &&
4463 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4464 Jim_AppendStrings(interp, Jim_GetResult(interp),
4465 "Invalid width param. Must be one of: 8, 16, 32 or 64.", NULL);
4468 const unsigned int width = width_bits / 8;
4470 /* Arg 2: Memory address */
4472 e = Jim_GetWide(interp, argv[2], &wide_addr);
4475 target_addr_t addr = (target_addr_t)wide_addr;
4477 /* Arg 3: Number of elements to read */
4478 e = Jim_GetLong(interp, argv[3], &l);
4484 bool is_phys = false;
4487 const char *phys = Jim_GetString(argv[4], &str_len);
4488 if (!strncmp(phys, "phys", str_len))
4494 /* Argument checks */
4496 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4497 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
4500 if ((addr + (len * width)) < addr) {
4501 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4502 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
4506 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4507 Jim_AppendStrings(interp, Jim_GetResult(interp),
4508 "mem2array: too large read request, exceeds 64K items", NULL);
4513 ((width == 2) && ((addr & 1) == 0)) ||
4514 ((width == 4) && ((addr & 3) == 0)) ||
4515 ((width == 8) && ((addr & 7) == 0))) {
4516 /* alignment correct */
4519 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4520 sprintf(buf, "mem2array address: " TARGET_ADDR_FMT " is not aligned for %" PRIu32 " byte reads",
4523 Jim_AppendStrings(interp, Jim_GetResult(interp), buf, NULL);
4532 const size_t buffersize = 4096;
4533 uint8_t *buffer = malloc(buffersize);
4540 /* Slurp... in buffer size chunks */
4541 const unsigned int max_chunk_len = buffersize / width;
4542 const size_t chunk_len = MIN(len, max_chunk_len); /* in elements.. */
4546 retval = target_read_phys_memory(target, addr, width, chunk_len, buffer);
4548 retval = target_read_memory(target, addr, width, chunk_len, buffer);
4549 if (retval != ERROR_OK) {
4551 LOG_ERROR("mem2array: Read @ " TARGET_ADDR_FMT ", w=%u, cnt=%zu, failed",
4555 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4556 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
4560 for (size_t i = 0; i < chunk_len ; i++, idx++) {
4564 v = target_buffer_get_u64(target, &buffer[i*width]);
4567 v = target_buffer_get_u32(target, &buffer[i*width]);
4570 v = target_buffer_get_u16(target, &buffer[i*width]);
4573 v = buffer[i] & 0x0ff;
4576 new_u64_array_element(interp, varname, idx, v);
4579 addr += chunk_len * width;
4585 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4590 static int target_jim_read_memory(Jim_Interp *interp, int argc,
4591 Jim_Obj * const *argv)
4594 * argv[1] = memory address
4595 * argv[2] = desired element width in bits
4596 * argv[3] = number of elements to read
4597 * argv[4] = optional "phys"
4600 if (argc < 4 || argc > 5) {
4601 Jim_WrongNumArgs(interp, 1, argv, "address width count ['phys']");
4605 /* Arg 1: Memory address. */
4608 e = Jim_GetWide(interp, argv[1], &wide_addr);
4613 target_addr_t addr = (target_addr_t)wide_addr;
4615 /* Arg 2: Bit width of one element. */
4617 e = Jim_GetLong(interp, argv[2], &l);
4622 const unsigned int width_bits = l;
4624 /* Arg 3: Number of elements to read. */
4625 e = Jim_GetLong(interp, argv[3], &l);
4632 /* Arg 4: Optional 'phys'. */
4633 bool is_phys = false;
4636 const char *phys = Jim_GetString(argv[4], NULL);
4638 if (strcmp(phys, "phys")) {
4639 Jim_SetResultFormatted(interp, "invalid argument '%s', must be 'phys'", phys);
4646 switch (width_bits) {
4653 Jim_SetResultString(interp, "invalid width, must be 8, 16, 32 or 64", -1);
4657 const unsigned int width = width_bits / 8;
4659 if ((addr + (count * width)) < addr) {
4660 Jim_SetResultString(interp, "read_memory: addr + count wraps to zero", -1);
4664 if (count > 65536) {
4665 Jim_SetResultString(interp, "read_memory: too large read request, exeeds 64K elements", -1);
4669 struct command_context *cmd_ctx = current_command_context(interp);
4670 assert(cmd_ctx != NULL);
4671 struct target *target = get_current_target(cmd_ctx);
4673 const size_t buffersize = 4096;
4674 uint8_t *buffer = malloc(buffersize);
4677 LOG_ERROR("Failed to allocate memory");
4681 Jim_Obj *result_list = Jim_NewListObj(interp, NULL, 0);
4682 Jim_IncrRefCount(result_list);
4685 const unsigned int max_chunk_len = buffersize / width;
4686 const size_t chunk_len = MIN(count, max_chunk_len);
4691 retval = target_read_phys_memory(target, addr, width, chunk_len, buffer);
4693 retval = target_read_memory(target, addr, width, chunk_len, buffer);
4695 if (retval != ERROR_OK) {
4696 LOG_ERROR("read_memory: read at " TARGET_ADDR_FMT " with width=%u and count=%zu failed",
4697 addr, width_bits, chunk_len);
4698 Jim_SetResultString(interp, "read_memory: failed to read memory", -1);
4703 for (size_t i = 0; i < chunk_len ; i++) {
4708 v = target_buffer_get_u64(target, &buffer[i * width]);
4711 v = target_buffer_get_u32(target, &buffer[i * width]);
4714 v = target_buffer_get_u16(target, &buffer[i * width]);
4722 snprintf(value_buf, sizeof(value_buf), "0x%" PRIx64, v);
4724 Jim_ListAppendElement(interp, result_list,
4725 Jim_NewStringObj(interp, value_buf, -1));
4729 addr += chunk_len * width;
4735 Jim_DecrRefCount(interp, result_list);
4739 Jim_SetResult(interp, result_list);
4740 Jim_DecrRefCount(interp, result_list);
4745 static int get_u64_array_element(Jim_Interp *interp, const char *varname, size_t idx, uint64_t *val)
4747 char *namebuf = alloc_printf("%s(%zu)", varname, idx);
4751 Jim_Obj *obj_name = Jim_NewStringObj(interp, namebuf, -1);
4757 Jim_IncrRefCount(obj_name);
4758 Jim_Obj *obj_val = Jim_GetVariable(interp, obj_name, JIM_ERRMSG);
4759 Jim_DecrRefCount(interp, obj_name);
4765 int result = Jim_GetWide(interp, obj_val, &wide_val);
4770 static int target_array2mem(Jim_Interp *interp, struct target *target,
4771 int argc, Jim_Obj *const *argv)
4775 LOG_WARNING("DEPRECATED! use 'write_memory' not 'array2mem'");
4777 /* argv[0] = name of array from which to read the data
4778 * argv[1] = desired element width in bits
4779 * argv[2] = memory address
4780 * argv[3] = number of elements to write
4781 * argv[4] = optional "phys"
4783 if (argc < 4 || argc > 5) {
4784 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems [phys]");
4788 /* Arg 0: Name of the array variable */
4789 const char *varname = Jim_GetString(argv[0], NULL);
4791 /* Arg 1: Bit width of one element */
4793 e = Jim_GetLong(interp, argv[1], &l);
4796 const unsigned int width_bits = l;
4798 if (width_bits != 8 &&
4802 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4803 Jim_AppendStrings(interp, Jim_GetResult(interp),
4804 "Invalid width param. Must be one of: 8, 16, 32 or 64.", NULL);
4807 const unsigned int width = width_bits / 8;
4809 /* Arg 2: Memory address */
4811 e = Jim_GetWide(interp, argv[2], &wide_addr);
4814 target_addr_t addr = (target_addr_t)wide_addr;
4816 /* Arg 3: Number of elements to write */
4817 e = Jim_GetLong(interp, argv[3], &l);
4823 bool is_phys = false;
4826 const char *phys = Jim_GetString(argv[4], &str_len);
4827 if (!strncmp(phys, "phys", str_len))
4833 /* Argument checks */
4835 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4836 Jim_AppendStrings(interp, Jim_GetResult(interp),
4837 "array2mem: zero width read?", NULL);
4841 if ((addr + (len * width)) < addr) {
4842 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4843 Jim_AppendStrings(interp, Jim_GetResult(interp),
4844 "array2mem: addr + len - wraps to zero?", NULL);
4849 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4850 Jim_AppendStrings(interp, Jim_GetResult(interp),
4851 "array2mem: too large memory write request, exceeds 64K items", NULL);
4856 ((width == 2) && ((addr & 1) == 0)) ||
4857 ((width == 4) && ((addr & 3) == 0)) ||
4858 ((width == 8) && ((addr & 7) == 0))) {
4859 /* alignment correct */
4862 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4863 sprintf(buf, "array2mem address: " TARGET_ADDR_FMT " is not aligned for %" PRIu32 " byte reads",
4866 Jim_AppendStrings(interp, Jim_GetResult(interp), buf, NULL);
4875 const size_t buffersize = 4096;
4876 uint8_t *buffer = malloc(buffersize);
4884 /* Slurp... in buffer size chunks */
4885 const unsigned int max_chunk_len = buffersize / width;
4887 const size_t chunk_len = MIN(len, max_chunk_len); /* in elements.. */
4889 /* Fill the buffer */
4890 for (size_t i = 0; i < chunk_len; i++, idx++) {
4892 if (get_u64_array_element(interp, varname, idx, &v) != JIM_OK) {
4898 target_buffer_set_u64(target, &buffer[i * width], v);
4901 target_buffer_set_u32(target, &buffer[i * width], v);
4904 target_buffer_set_u16(target, &buffer[i * width], v);
4907 buffer[i] = v & 0x0ff;
4913 /* Write the buffer to memory */
4916 retval = target_write_phys_memory(target, addr, width, chunk_len, buffer);
4918 retval = target_write_memory(target, addr, width, chunk_len, buffer);
4919 if (retval != ERROR_OK) {
4921 LOG_ERROR("array2mem: Write @ " TARGET_ADDR_FMT ", w=%u, cnt=%zu, failed",
4925 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4926 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
4930 addr += chunk_len * width;
4935 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4940 static int target_jim_write_memory(Jim_Interp *interp, int argc,
4941 Jim_Obj * const *argv)
4944 * argv[1] = memory address
4945 * argv[2] = desired element width in bits
4946 * argv[3] = list of data to write
4947 * argv[4] = optional "phys"
4950 if (argc < 4 || argc > 5) {
4951 Jim_WrongNumArgs(interp, 1, argv, "address width data ['phys']");
4955 /* Arg 1: Memory address. */
4958 e = Jim_GetWide(interp, argv[1], &wide_addr);
4963 target_addr_t addr = (target_addr_t)wide_addr;
4965 /* Arg 2: Bit width of one element. */
4967 e = Jim_GetLong(interp, argv[2], &l);
4972 const unsigned int width_bits = l;
4973 size_t count = Jim_ListLength(interp, argv[3]);
4975 /* Arg 4: Optional 'phys'. */
4976 bool is_phys = false;
4979 const char *phys = Jim_GetString(argv[4], NULL);
4981 if (strcmp(phys, "phys")) {
4982 Jim_SetResultFormatted(interp, "invalid argument '%s', must be 'phys'", phys);
4989 switch (width_bits) {
4996 Jim_SetResultString(interp, "invalid width, must be 8, 16, 32 or 64", -1);
5000 const unsigned int width = width_bits / 8;
5002 if ((addr + (count * width)) < addr) {
5003 Jim_SetResultString(interp, "write_memory: addr + len wraps to zero", -1);
5007 if (count > 65536) {
5008 Jim_SetResultString(interp, "write_memory: too large memory write request, exceeds 64K elements", -1);
5012 struct command_context *cmd_ctx = current_command_context(interp);
5013 assert(cmd_ctx != NULL);
5014 struct target *target = get_current_target(cmd_ctx);
5016 const size_t buffersize = 4096;
5017 uint8_t *buffer = malloc(buffersize);
5020 LOG_ERROR("Failed to allocate memory");
5027 const unsigned int max_chunk_len = buffersize / width;
5028 const size_t chunk_len = MIN(count, max_chunk_len);
5030 for (size_t i = 0; i < chunk_len; i++, j++) {
5031 Jim_Obj *tmp = Jim_ListGetIndex(interp, argv[3], j);
5032 jim_wide element_wide;
5033 Jim_GetWide(interp, tmp, &element_wide);
5035 const uint64_t v = element_wide;
5039 target_buffer_set_u64(target, &buffer[i * width], v);
5042 target_buffer_set_u32(target, &buffer[i * width], v);
5045 target_buffer_set_u16(target, &buffer[i * width], v);
5048 buffer[i] = v & 0x0ff;
5058 retval = target_write_phys_memory(target, addr, width, chunk_len, buffer);
5060 retval = target_write_memory(target, addr, width, chunk_len, buffer);
5062 if (retval != ERROR_OK) {
5063 LOG_ERROR("write_memory: write at " TARGET_ADDR_FMT " with width=%u and count=%zu failed",
5064 addr, width_bits, chunk_len);
5065 Jim_SetResultString(interp, "write_memory: failed to write memory", -1);
5070 addr += chunk_len * width;
5078 /* FIX? should we propagate errors here rather than printing them
5081 void target_handle_event(struct target *target, enum target_event e)
5083 struct target_event_action *teap;
5086 for (teap = target->event_action; teap; teap = teap->next) {
5087 if (teap->event == e) {
5088 LOG_DEBUG("target(%d): %s (%s) event: %d (%s) action: %s",
5089 target->target_number,
5090 target_name(target),
5091 target_type_name(target),
5093 target_event_name(e),
5094 Jim_GetString(teap->body, NULL));
5096 /* Override current target by the target an event
5097 * is issued from (lot of scripts need it).
5098 * Return back to previous override as soon
5099 * as the handler processing is done */
5100 struct command_context *cmd_ctx = current_command_context(teap->interp);
5101 struct target *saved_target_override = cmd_ctx->current_target_override;
5102 cmd_ctx->current_target_override = target;
5104 retval = Jim_EvalObj(teap->interp, teap->body);
5106 cmd_ctx->current_target_override = saved_target_override;
5108 if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
5111 if (retval == JIM_RETURN)
5112 retval = teap->interp->returnCode;
5114 if (retval != JIM_OK) {
5115 Jim_MakeErrorMessage(teap->interp);
5116 LOG_USER("Error executing event %s on target %s:\n%s",
5117 target_event_name(e),
5118 target_name(target),
5119 Jim_GetString(Jim_GetResult(teap->interp), NULL));
5120 /* clean both error code and stacktrace before return */
5121 Jim_Eval(teap->interp, "error \"\" \"\"");
5127 static int target_jim_get_reg(Jim_Interp *interp, int argc,
5128 Jim_Obj * const *argv)
5133 const char *option = Jim_GetString(argv[1], NULL);
5135 if (!strcmp(option, "-force")) {
5140 Jim_SetResultFormatted(interp, "invalid option '%s'", option);
5146 Jim_WrongNumArgs(interp, 1, argv, "[-force] list");
5150 const int length = Jim_ListLength(interp, argv[1]);
5152 Jim_Obj *result_dict = Jim_NewDictObj(interp, NULL, 0);
5157 struct command_context *cmd_ctx = current_command_context(interp);
5158 assert(cmd_ctx != NULL);
5159 const struct target *target = get_current_target(cmd_ctx);
5161 for (int i = 0; i < length; i++) {
5162 Jim_Obj *elem = Jim_ListGetIndex(interp, argv[1], i);
5167 const char *reg_name = Jim_String(elem);
5169 struct reg *reg = register_get_by_name(target->reg_cache, reg_name,
5172 if (!reg || !reg->exist) {
5173 Jim_SetResultFormatted(interp, "unknown register '%s'", reg_name);
5178 int retval = reg->type->get(reg);
5180 if (retval != ERROR_OK) {
5181 Jim_SetResultFormatted(interp, "failed to read register '%s'",
5187 char *reg_value = buf_to_hex_str(reg->value, reg->size);
5190 LOG_ERROR("Failed to allocate memory");
5194 char *tmp = alloc_printf("0x%s", reg_value);
5199 LOG_ERROR("Failed to allocate memory");
5203 Jim_DictAddElement(interp, result_dict, elem,
5204 Jim_NewStringObj(interp, tmp, -1));
5209 Jim_SetResult(interp, result_dict);
5214 static int target_jim_set_reg(Jim_Interp *interp, int argc,
5215 Jim_Obj * const *argv)
5218 Jim_WrongNumArgs(interp, 1, argv, "dict");
5223 #if JIM_VERSION >= 80
5224 Jim_Obj **dict = Jim_DictPairs(interp, argv[1], &tmp);
5230 int ret = Jim_DictPairs(interp, argv[1], &dict, &tmp);
5236 const unsigned int length = tmp;
5237 struct command_context *cmd_ctx = current_command_context(interp);
5239 const struct target *target = get_current_target(cmd_ctx);
5241 for (unsigned int i = 0; i < length; i += 2) {
5242 const char *reg_name = Jim_String(dict[i]);
5243 const char *reg_value = Jim_String(dict[i + 1]);
5244 struct reg *reg = register_get_by_name(target->reg_cache, reg_name,
5247 if (!reg || !reg->exist) {
5248 Jim_SetResultFormatted(interp, "unknown register '%s'", reg_name);
5252 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
5255 LOG_ERROR("Failed to allocate memory");
5259 str_to_buf(reg_value, strlen(reg_value), buf, reg->size, 0);
5260 int retval = reg->type->set(reg, buf);
5263 if (retval != ERROR_OK) {
5264 Jim_SetResultFormatted(interp, "failed to set '%s' to register '%s'",
5265 reg_value, reg_name);
5274 * Returns true only if the target has a handler for the specified event.
5276 bool target_has_event_action(struct target *target, enum target_event event)
5278 struct target_event_action *teap;
5280 for (teap = target->event_action; teap; teap = teap->next) {
5281 if (teap->event == event)
5287 enum target_cfg_param {
5290 TCFG_WORK_AREA_VIRT,
5291 TCFG_WORK_AREA_PHYS,
5292 TCFG_WORK_AREA_SIZE,
5293 TCFG_WORK_AREA_BACKUP,
5296 TCFG_CHAIN_POSITION,
5301 TCFG_GDB_MAX_CONNECTIONS,
5304 static struct jim_nvp nvp_config_opts[] = {
5305 { .name = "-type", .value = TCFG_TYPE },
5306 { .name = "-event", .value = TCFG_EVENT },
5307 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
5308 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
5309 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
5310 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
5311 { .name = "-endian", .value = TCFG_ENDIAN },
5312 { .name = "-coreid", .value = TCFG_COREID },
5313 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
5314 { .name = "-dbgbase", .value = TCFG_DBGBASE },
5315 { .name = "-rtos", .value = TCFG_RTOS },
5316 { .name = "-defer-examine", .value = TCFG_DEFER_EXAMINE },
5317 { .name = "-gdb-port", .value = TCFG_GDB_PORT },
5318 { .name = "-gdb-max-connections", .value = TCFG_GDB_MAX_CONNECTIONS },
5319 { .name = NULL, .value = -1 }
5322 static int target_configure(struct jim_getopt_info *goi, struct target *target)
5329 /* parse config or cget options ... */
5330 while (goi->argc > 0) {
5331 Jim_SetEmptyResult(goi->interp);
5332 /* jim_getopt_debug(goi); */
5334 if (target->type->target_jim_configure) {
5335 /* target defines a configure function */
5336 /* target gets first dibs on parameters */
5337 e = (*(target->type->target_jim_configure))(target, goi);
5346 /* otherwise we 'continue' below */
5348 e = jim_getopt_nvp(goi, nvp_config_opts, &n);
5350 jim_getopt_nvp_unknown(goi, nvp_config_opts, 0);
5356 if (goi->isconfigure) {
5357 Jim_SetResultFormatted(goi->interp,
5358 "not settable: %s", n->name);
5362 if (goi->argc != 0) {
5363 Jim_WrongNumArgs(goi->interp,
5364 goi->argc, goi->argv,
5369 Jim_SetResultString(goi->interp,
5370 target_type_name(target), -1);
5374 if (goi->argc == 0) {
5375 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
5379 e = jim_getopt_nvp(goi, nvp_target_event, &n);
5381 jim_getopt_nvp_unknown(goi, nvp_target_event, 1);
5385 if (goi->isconfigure) {
5386 if (goi->argc != 1) {
5387 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
5391 if (goi->argc != 0) {
5392 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
5398 struct target_event_action *teap;
5400 teap = target->event_action;
5401 /* replace existing? */
5403 if (teap->event == (enum target_event)n->value)
5408 if (goi->isconfigure) {
5409 /* START_DEPRECATED_TPIU */
5410 if (n->value == TARGET_EVENT_TRACE_CONFIG)
5411 LOG_INFO("DEPRECATED target event %s; use TPIU events {pre,post}-{enable,disable}", n->name);
5412 /* END_DEPRECATED_TPIU */
5414 bool replace = true;
5417 teap = calloc(1, sizeof(*teap));
5420 teap->event = n->value;
5421 teap->interp = goi->interp;
5422 jim_getopt_obj(goi, &o);
5424 Jim_DecrRefCount(teap->interp, teap->body);
5425 teap->body = Jim_DuplicateObj(goi->interp, o);
5428 * Tcl/TK - "tk events" have a nice feature.
5429 * See the "BIND" command.
5430 * We should support that here.
5431 * You can specify %X and %Y in the event code.
5432 * The idea is: %T - target name.
5433 * The idea is: %N - target number
5434 * The idea is: %E - event name.
5436 Jim_IncrRefCount(teap->body);
5439 /* add to head of event list */
5440 teap->next = target->event_action;
5441 target->event_action = teap;
5443 Jim_SetEmptyResult(goi->interp);
5447 Jim_SetEmptyResult(goi->interp);
5449 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
5455 case TCFG_WORK_AREA_VIRT:
5456 if (goi->isconfigure) {
5457 target_free_all_working_areas(target);
5458 e = jim_getopt_wide(goi, &w);
5461 target->working_area_virt = w;
5462 target->working_area_virt_spec = true;
5467 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
5471 case TCFG_WORK_AREA_PHYS:
5472 if (goi->isconfigure) {
5473 target_free_all_working_areas(target);
5474 e = jim_getopt_wide(goi, &w);
5477 target->working_area_phys = w;
5478 target->working_area_phys_spec = true;
5483 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
5487 case TCFG_WORK_AREA_SIZE:
5488 if (goi->isconfigure) {
5489 target_free_all_working_areas(target);
5490 e = jim_getopt_wide(goi, &w);
5493 target->working_area_size = w;
5498 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
5502 case TCFG_WORK_AREA_BACKUP:
5503 if (goi->isconfigure) {
5504 target_free_all_working_areas(target);
5505 e = jim_getopt_wide(goi, &w);
5508 /* make this exactly 1 or 0 */
5509 target->backup_working_area = (!!w);
5514 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
5515 /* loop for more e*/
5520 if (goi->isconfigure) {
5521 e = jim_getopt_nvp(goi, nvp_target_endian, &n);
5523 jim_getopt_nvp_unknown(goi, nvp_target_endian, 1);
5526 target->endianness = n->value;
5531 n = jim_nvp_value2name_simple(nvp_target_endian, target->endianness);
5533 target->endianness = TARGET_LITTLE_ENDIAN;
5534 n = jim_nvp_value2name_simple(nvp_target_endian, target->endianness);
5536 Jim_SetResultString(goi->interp, n->name, -1);
5541 if (goi->isconfigure) {
5542 e = jim_getopt_wide(goi, &w);
5545 target->coreid = (int32_t)w;
5550 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->coreid));
5554 case TCFG_CHAIN_POSITION:
5555 if (goi->isconfigure) {
5557 struct jtag_tap *tap;
5559 if (target->has_dap) {
5560 Jim_SetResultString(goi->interp,
5561 "target requires -dap parameter instead of -chain-position!", -1);
5565 target_free_all_working_areas(target);
5566 e = jim_getopt_obj(goi, &o_t);
5569 tap = jtag_tap_by_jim_obj(goi->interp, o_t);
5573 target->tap_configured = true;
5578 Jim_SetResultString(goi->interp, target->tap->dotted_name, -1);
5579 /* loop for more e*/
5582 if (goi->isconfigure) {
5583 e = jim_getopt_wide(goi, &w);
5586 target->dbgbase = (uint32_t)w;
5587 target->dbgbase_set = true;
5592 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->dbgbase));
5598 int result = rtos_create(goi, target);
5599 if (result != JIM_OK)
5605 case TCFG_DEFER_EXAMINE:
5607 target->defer_examine = true;
5612 if (goi->isconfigure) {
5613 struct command_context *cmd_ctx = current_command_context(goi->interp);
5614 if (cmd_ctx->mode != COMMAND_CONFIG) {
5615 Jim_SetResultString(goi->interp, "-gdb-port must be configured before 'init'", -1);
5620 e = jim_getopt_string(goi, &s, NULL);
5623 free(target->gdb_port_override);
5624 target->gdb_port_override = strdup(s);
5629 Jim_SetResultString(goi->interp, target->gdb_port_override ? target->gdb_port_override : "undefined", -1);
5633 case TCFG_GDB_MAX_CONNECTIONS:
5634 if (goi->isconfigure) {
5635 struct command_context *cmd_ctx = current_command_context(goi->interp);
5636 if (cmd_ctx->mode != COMMAND_CONFIG) {
5637 Jim_SetResultString(goi->interp, "-gdb-max-connections must be configured before 'init'", -1);
5641 e = jim_getopt_wide(goi, &w);
5644 target->gdb_max_connections = (w < 0) ? CONNECTION_LIMIT_UNLIMITED : (int)w;
5649 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->gdb_max_connections));
5652 } /* while (goi->argc) */
5655 /* done - we return */
5659 static int jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
5661 struct command *c = jim_to_command(interp);
5662 struct jim_getopt_info goi;
5664 jim_getopt_setup(&goi, interp, argc - 1, argv + 1);
5665 goi.isconfigure = !strcmp(c->name, "configure");
5667 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
5668 "missing: -option ...");
5671 struct command_context *cmd_ctx = current_command_context(interp);
5673 struct target *target = get_current_target(cmd_ctx);
5674 return target_configure(&goi, target);
5677 static int jim_target_mem2array(Jim_Interp *interp,
5678 int argc, Jim_Obj *const *argv)
5680 struct command_context *cmd_ctx = current_command_context(interp);
5682 struct target *target = get_current_target(cmd_ctx);
5683 return target_mem2array(interp, target, argc - 1, argv + 1);
5686 static int jim_target_array2mem(Jim_Interp *interp,
5687 int argc, Jim_Obj *const *argv)
5689 struct command_context *cmd_ctx = current_command_context(interp);
5691 struct target *target = get_current_target(cmd_ctx);
5692 return target_array2mem(interp, target, argc - 1, argv + 1);
5695 static int jim_target_tap_disabled(Jim_Interp *interp)
5697 Jim_SetResultFormatted(interp, "[TAP is disabled]");
5701 static int jim_target_examine(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5703 bool allow_defer = false;
5705 struct jim_getopt_info goi;
5706 jim_getopt_setup(&goi, interp, argc - 1, argv + 1);
5708 const char *cmd_name = Jim_GetString(argv[0], NULL);
5709 Jim_SetResultFormatted(goi.interp,
5710 "usage: %s ['allow-defer']", cmd_name);
5714 strcmp(Jim_GetString(argv[1], NULL), "allow-defer") == 0) {
5717 int e = jim_getopt_obj(&goi, &obj);
5723 struct command_context *cmd_ctx = current_command_context(interp);
5725 struct target *target = get_current_target(cmd_ctx);
5726 if (!target->tap->enabled)
5727 return jim_target_tap_disabled(interp);
5729 if (allow_defer && target->defer_examine) {
5730 LOG_INFO("Deferring arp_examine of %s", target_name(target));
5731 LOG_INFO("Use arp_examine command to examine it manually!");
5735 int e = target->type->examine(target);
5736 if (e != ERROR_OK) {
5737 target_reset_examined(target);
5741 target_set_examined(target);
5746 static int jim_target_was_examined(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
5748 struct command_context *cmd_ctx = current_command_context(interp);
5750 struct target *target = get_current_target(cmd_ctx);
5752 Jim_SetResultBool(interp, target_was_examined(target));
5756 static int jim_target_examine_deferred(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
5758 struct command_context *cmd_ctx = current_command_context(interp);
5760 struct target *target = get_current_target(cmd_ctx);
5762 Jim_SetResultBool(interp, target->defer_examine);
5766 static int jim_target_halt_gdb(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5769 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
5772 struct command_context *cmd_ctx = current_command_context(interp);
5774 struct target *target = get_current_target(cmd_ctx);
5776 if (target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT) != ERROR_OK)
5782 static int jim_target_poll(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5785 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
5788 struct command_context *cmd_ctx = current_command_context(interp);
5790 struct target *target = get_current_target(cmd_ctx);
5791 if (!target->tap->enabled)
5792 return jim_target_tap_disabled(interp);
5795 if (!(target_was_examined(target)))
5796 e = ERROR_TARGET_NOT_EXAMINED;
5798 e = target->type->poll(target);
5804 static int jim_target_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5806 struct jim_getopt_info goi;
5807 jim_getopt_setup(&goi, interp, argc - 1, argv + 1);
5809 if (goi.argc != 2) {
5810 Jim_WrongNumArgs(interp, 0, argv,
5811 "([tT]|[fF]|assert|deassert) BOOL");
5816 int e = jim_getopt_nvp(&goi, nvp_assert, &n);
5818 jim_getopt_nvp_unknown(&goi, nvp_assert, 1);
5821 /* the halt or not param */
5823 e = jim_getopt_wide(&goi, &a);
5827 struct command_context *cmd_ctx = current_command_context(interp);
5829 struct target *target = get_current_target(cmd_ctx);
5830 if (!target->tap->enabled)
5831 return jim_target_tap_disabled(interp);
5833 if (!target->type->assert_reset || !target->type->deassert_reset) {
5834 Jim_SetResultFormatted(interp,
5835 "No target-specific reset for %s",
5836 target_name(target));
5840 if (target->defer_examine)
5841 target_reset_examined(target);
5843 /* determine if we should halt or not. */
5844 target->reset_halt = (a != 0);
5845 /* When this happens - all workareas are invalid. */
5846 target_free_all_working_areas_restore(target, 0);
5849 if (n->value == NVP_ASSERT)
5850 e = target->type->assert_reset(target);
5852 e = target->type->deassert_reset(target);
5853 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
5856 static int jim_target_halt(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5859 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
5862 struct command_context *cmd_ctx = current_command_context(interp);
5864 struct target *target = get_current_target(cmd_ctx);
5865 if (!target->tap->enabled)
5866 return jim_target_tap_disabled(interp);
5867 int e = target->type->halt(target);
5868 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
5871 static int jim_target_wait_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5873 struct jim_getopt_info goi;
5874 jim_getopt_setup(&goi, interp, argc - 1, argv + 1);
5876 /* params: <name> statename timeoutmsecs */
5877 if (goi.argc != 2) {
5878 const char *cmd_name = Jim_GetString(argv[0], NULL);
5879 Jim_SetResultFormatted(goi.interp,
5880 "%s <state_name> <timeout_in_msec>", cmd_name);
5885 int e = jim_getopt_nvp(&goi, nvp_target_state, &n);
5887 jim_getopt_nvp_unknown(&goi, nvp_target_state, 1);
5891 e = jim_getopt_wide(&goi, &a);
5894 struct command_context *cmd_ctx = current_command_context(interp);
5896 struct target *target = get_current_target(cmd_ctx);
5897 if (!target->tap->enabled)
5898 return jim_target_tap_disabled(interp);
5900 e = target_wait_state(target, n->value, a);
5901 if (e != ERROR_OK) {
5902 Jim_Obj *obj = Jim_NewIntObj(interp, e);
5903 Jim_SetResultFormatted(goi.interp,
5904 "target: %s wait %s fails (%#s) %s",
5905 target_name(target), n->name,
5906 obj, target_strerror_safe(e));
5911 /* List for human, Events defined for this target.
5912 * scripts/programs should use 'name cget -event NAME'
5914 COMMAND_HANDLER(handle_target_event_list)
5916 struct target *target = get_current_target(CMD_CTX);
5917 struct target_event_action *teap = target->event_action;
5919 command_print(CMD, "Event actions for target (%d) %s\n",
5920 target->target_number,
5921 target_name(target));
5922 command_print(CMD, "%-25s | Body", "Event");
5923 command_print(CMD, "------------------------- | "
5924 "----------------------------------------");
5926 command_print(CMD, "%-25s | %s",
5927 target_event_name(teap->event),
5928 Jim_GetString(teap->body, NULL));
5931 command_print(CMD, "***END***");
5934 static int jim_target_current_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5937 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
5940 struct command_context *cmd_ctx = current_command_context(interp);
5942 struct target *target = get_current_target(cmd_ctx);
5943 Jim_SetResultString(interp, target_state_name(target), -1);
5946 static int jim_target_invoke_event(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5948 struct jim_getopt_info goi;
5949 jim_getopt_setup(&goi, interp, argc - 1, argv + 1);
5950 if (goi.argc != 1) {
5951 const char *cmd_name = Jim_GetString(argv[0], NULL);
5952 Jim_SetResultFormatted(goi.interp, "%s <eventname>", cmd_name);
5956 int e = jim_getopt_nvp(&goi, nvp_target_event, &n);
5958 jim_getopt_nvp_unknown(&goi, nvp_target_event, 1);
5961 struct command_context *cmd_ctx = current_command_context(interp);
5963 struct target *target = get_current_target(cmd_ctx);
5964 target_handle_event(target, n->value);
5968 static const struct command_registration target_instance_command_handlers[] = {
5970 .name = "configure",
5971 .mode = COMMAND_ANY,
5972 .jim_handler = jim_target_configure,
5973 .help = "configure a new target for use",
5974 .usage = "[target_attribute ...]",
5978 .mode = COMMAND_ANY,
5979 .jim_handler = jim_target_configure,
5980 .help = "returns the specified target attribute",
5981 .usage = "target_attribute",
5985 .handler = handle_mw_command,
5986 .mode = COMMAND_EXEC,
5987 .help = "Write 64-bit word(s) to target memory",
5988 .usage = "address data [count]",
5992 .handler = handle_mw_command,
5993 .mode = COMMAND_EXEC,
5994 .help = "Write 32-bit word(s) to target memory",
5995 .usage = "address data [count]",
5999 .handler = handle_mw_command,
6000 .mode = COMMAND_EXEC,
6001 .help = "Write 16-bit half-word(s) to target memory",
6002 .usage = "address data [count]",
6006 .handler = handle_mw_command,
6007 .mode = COMMAND_EXEC,
6008 .help = "Write byte(s) to target memory",
6009 .usage = "address data [count]",
6013 .handler = handle_md_command,
6014 .mode = COMMAND_EXEC,
6015 .help = "Display target memory as 64-bit words",
6016 .usage = "address [count]",
6020 .handler = handle_md_command,
6021 .mode = COMMAND_EXEC,
6022 .help = "Display target memory as 32-bit words",
6023 .usage = "address [count]",
6027 .handler = handle_md_command,
6028 .mode = COMMAND_EXEC,
6029 .help = "Display target memory as 16-bit half-words",
6030 .usage = "address [count]",
6034 .handler = handle_md_command,
6035 .mode = COMMAND_EXEC,
6036 .help = "Display target memory as 8-bit bytes",
6037 .usage = "address [count]",
6040 .name = "array2mem",
6041 .mode = COMMAND_EXEC,
6042 .jim_handler = jim_target_array2mem,
6043 .help = "Writes Tcl array of 8/16/32 bit numbers "
6045 .usage = "arrayname bitwidth address count",
6048 .name = "mem2array",
6049 .mode = COMMAND_EXEC,
6050 .jim_handler = jim_target_mem2array,
6051 .help = "Loads Tcl array of 8/16/32 bit numbers "
6052 "from target memory",
6053 .usage = "arrayname bitwidth address count",
6057 .mode = COMMAND_EXEC,
6058 .jim_handler = target_jim_get_reg,
6059 .help = "Get register values from the target",
6064 .mode = COMMAND_EXEC,
6065 .jim_handler = target_jim_set_reg,
6066 .help = "Set target register values",
6070 .name = "read_memory",
6071 .mode = COMMAND_EXEC,
6072 .jim_handler = target_jim_read_memory,
6073 .help = "Read Tcl list of 8/16/32/64 bit numbers from target memory",
6074 .usage = "address width count ['phys']",
6077 .name = "write_memory",
6078 .mode = COMMAND_EXEC,
6079 .jim_handler = target_jim_write_memory,
6080 .help = "Write Tcl list of 8/16/32/64 bit numbers to target memory",
6081 .usage = "address width data ['phys']",
6084 .name = "eventlist",
6085 .handler = handle_target_event_list,
6086 .mode = COMMAND_EXEC,
6087 .help = "displays a table of events defined for this target",
6092 .mode = COMMAND_EXEC,
6093 .jim_handler = jim_target_current_state,
6094 .help = "displays the current state of this target",
6097 .name = "arp_examine",
6098 .mode = COMMAND_EXEC,
6099 .jim_handler = jim_target_examine,
6100 .help = "used internally for reset processing",
6101 .usage = "['allow-defer']",
6104 .name = "was_examined",
6105 .mode = COMMAND_EXEC,
6106 .jim_handler = jim_target_was_examined,
6107 .help = "used internally for reset processing",
6110 .name = "examine_deferred",
6111 .mode = COMMAND_EXEC,
6112 .jim_handler = jim_target_examine_deferred,
6113 .help = "used internally for reset processing",
6116 .name = "arp_halt_gdb",
6117 .mode = COMMAND_EXEC,
6118 .jim_handler = jim_target_halt_gdb,
6119 .help = "used internally for reset processing to halt GDB",
6123 .mode = COMMAND_EXEC,
6124 .jim_handler = jim_target_poll,
6125 .help = "used internally for reset processing",
6128 .name = "arp_reset",
6129 .mode = COMMAND_EXEC,
6130 .jim_handler = jim_target_reset,
6131 .help = "used internally for reset processing",
6135 .mode = COMMAND_EXEC,
6136 .jim_handler = jim_target_halt,
6137 .help = "used internally for reset processing",
6140 .name = "arp_waitstate",
6141 .mode = COMMAND_EXEC,
6142 .jim_handler = jim_target_wait_state,
6143 .help = "used internally for reset processing",
6146 .name = "invoke-event",
6147 .mode = COMMAND_EXEC,
6148 .jim_handler = jim_target_invoke_event,
6149 .help = "invoke handler for specified event",
6150 .usage = "event_name",
6152 COMMAND_REGISTRATION_DONE
6155 static int target_create(struct jim_getopt_info *goi)
6162 struct target *target;
6163 struct command_context *cmd_ctx;
6165 cmd_ctx = current_command_context(goi->interp);
6168 if (goi->argc < 3) {
6169 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
6174 jim_getopt_obj(goi, &new_cmd);
6175 /* does this command exist? */
6176 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_NONE);
6178 cp = Jim_GetString(new_cmd, NULL);
6179 Jim_SetResultFormatted(goi->interp, "Command/target: %s Exists", cp);
6184 e = jim_getopt_string(goi, &cp, NULL);
6187 struct transport *tr = get_current_transport();
6188 if (tr->override_target) {
6189 e = tr->override_target(&cp);
6190 if (e != ERROR_OK) {
6191 LOG_ERROR("The selected transport doesn't support this target");
6194 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
6196 /* now does target type exist */
6197 for (x = 0 ; target_types[x] ; x++) {
6198 if (strcmp(cp, target_types[x]->name) == 0) {
6203 if (!target_types[x]) {
6204 Jim_SetResultFormatted(goi->interp, "Unknown target type %s, try one of ", cp);
6205 for (x = 0 ; target_types[x] ; x++) {
6206 if (target_types[x + 1]) {
6207 Jim_AppendStrings(goi->interp,
6208 Jim_GetResult(goi->interp),
6209 target_types[x]->name,
6212 Jim_AppendStrings(goi->interp,
6213 Jim_GetResult(goi->interp),
6215 target_types[x]->name, NULL);
6222 target = calloc(1, sizeof(struct target));
6224 LOG_ERROR("Out of memory");
6228 /* set empty smp cluster */
6229 target->smp_targets = &empty_smp_targets;
6231 /* set target number */
6232 target->target_number = new_target_number();
6234 /* allocate memory for each unique target type */
6235 target->type = malloc(sizeof(struct target_type));
6236 if (!target->type) {
6237 LOG_ERROR("Out of memory");
6242 memcpy(target->type, target_types[x], sizeof(struct target_type));
6244 /* default to first core, override with -coreid */
6247 target->working_area = 0x0;
6248 target->working_area_size = 0x0;
6249 target->working_areas = NULL;
6250 target->backup_working_area = 0;
6252 target->state = TARGET_UNKNOWN;
6253 target->debug_reason = DBG_REASON_UNDEFINED;
6254 target->reg_cache = NULL;
6255 target->breakpoints = NULL;
6256 target->watchpoints = NULL;
6257 target->next = NULL;
6258 target->arch_info = NULL;
6260 target->verbose_halt_msg = true;
6262 target->halt_issued = false;
6264 /* initialize trace information */
6265 target->trace_info = calloc(1, sizeof(struct trace));
6266 if (!target->trace_info) {
6267 LOG_ERROR("Out of memory");
6273 target->dbgmsg = NULL;
6274 target->dbg_msg_enabled = 0;
6276 target->endianness = TARGET_ENDIAN_UNKNOWN;
6278 target->rtos = NULL;
6279 target->rtos_auto_detect = false;
6281 target->gdb_port_override = NULL;
6282 target->gdb_max_connections = 1;
6284 /* Do the rest as "configure" options */
6285 goi->isconfigure = 1;
6286 e = target_configure(goi, target);
6289 if (target->has_dap) {
6290 if (!target->dap_configured) {
6291 Jim_SetResultString(goi->interp, "-dap ?name? required when creating target", -1);
6295 if (!target->tap_configured) {
6296 Jim_SetResultString(goi->interp, "-chain-position ?name? required when creating target", -1);
6300 /* tap must be set after target was configured */
6306 rtos_destroy(target);
6307 free(target->gdb_port_override);
6308 free(target->trace_info);
6314 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
6315 /* default endian to little if not specified */
6316 target->endianness = TARGET_LITTLE_ENDIAN;
6319 cp = Jim_GetString(new_cmd, NULL);
6320 target->cmd_name = strdup(cp);
6321 if (!target->cmd_name) {
6322 LOG_ERROR("Out of memory");
6323 rtos_destroy(target);
6324 free(target->gdb_port_override);
6325 free(target->trace_info);
6331 if (target->type->target_create) {
6332 e = (*(target->type->target_create))(target, goi->interp);
6333 if (e != ERROR_OK) {
6334 LOG_DEBUG("target_create failed");
6335 free(target->cmd_name);
6336 rtos_destroy(target);
6337 free(target->gdb_port_override);
6338 free(target->trace_info);
6345 /* create the target specific commands */
6346 if (target->type->commands) {
6347 e = register_commands(cmd_ctx, NULL, target->type->commands);
6349 LOG_ERROR("unable to register '%s' commands", cp);
6352 /* now - create the new target name command */
6353 const struct command_registration target_subcommands[] = {
6355 .chain = target_instance_command_handlers,
6358 .chain = target->type->commands,
6360 COMMAND_REGISTRATION_DONE
6362 const struct command_registration target_commands[] = {
6365 .mode = COMMAND_ANY,
6366 .help = "target command group",
6368 .chain = target_subcommands,
6370 COMMAND_REGISTRATION_DONE
6372 e = register_commands_override_target(cmd_ctx, NULL, target_commands, target);
6373 if (e != ERROR_OK) {
6374 if (target->type->deinit_target)
6375 target->type->deinit_target(target);
6376 free(target->cmd_name);
6377 rtos_destroy(target);
6378 free(target->gdb_port_override);
6379 free(target->trace_info);
6385 /* append to end of list */
6386 append_to_list_all_targets(target);
6388 cmd_ctx->current_target = target;
6392 static int jim_target_current(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
6395 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
6398 struct command_context *cmd_ctx = current_command_context(interp);
6401 struct target *target = get_current_target_or_null(cmd_ctx);
6403 Jim_SetResultString(interp, target_name(target), -1);
6407 static int jim_target_types(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
6410 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
6413 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
6414 for (unsigned x = 0; target_types[x]; x++) {
6415 Jim_ListAppendElement(interp, Jim_GetResult(interp),
6416 Jim_NewStringObj(interp, target_types[x]->name, -1));
6421 static int jim_target_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
6424 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
6427 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
6428 struct target *target = all_targets;
6430 Jim_ListAppendElement(interp, Jim_GetResult(interp),
6431 Jim_NewStringObj(interp, target_name(target), -1));
6432 target = target->next;
6437 static int jim_target_smp(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
6440 const char *targetname;
6442 static int smp_group = 1;
6443 struct target *target = NULL;
6444 struct target_list *head, *new;
6447 LOG_DEBUG("%d", argc);
6448 /* argv[1] = target to associate in smp
6449 * argv[2] = target to associate in smp
6453 struct list_head *lh = malloc(sizeof(*lh));
6455 LOG_ERROR("Out of memory");
6460 for (i = 1; i < argc; i++) {
6462 targetname = Jim_GetString(argv[i], &len);
6463 target = get_target(targetname);
6464 LOG_DEBUG("%s ", targetname);
6466 new = malloc(sizeof(struct target_list));
6467 new->target = target;
6468 list_add_tail(&new->lh, lh);
6471 /* now parse the list of cpu and put the target in smp mode*/
6472 foreach_smp_target(head, lh) {
6473 target = head->target;
6474 target->smp = smp_group;
6475 target->smp_targets = lh;
6479 if (target && target->rtos)
6480 retval = rtos_smp_init(target);
6486 static int jim_target_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
6488 struct jim_getopt_info goi;
6489 jim_getopt_setup(&goi, interp, argc - 1, argv + 1);
6491 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
6492 "<name> <target_type> [<target_options> ...]");
6495 return target_create(&goi);
6498 static const struct command_registration target_subcommand_handlers[] = {
6501 .mode = COMMAND_CONFIG,
6502 .handler = handle_target_init_command,
6503 .help = "initialize targets",
6508 .mode = COMMAND_CONFIG,
6509 .jim_handler = jim_target_create,
6510 .usage = "name type '-chain-position' name [options ...]",
6511 .help = "Creates and selects a new target",
6515 .mode = COMMAND_ANY,
6516 .jim_handler = jim_target_current,
6517 .help = "Returns the currently selected target",
6521 .mode = COMMAND_ANY,
6522 .jim_handler = jim_target_types,
6523 .help = "Returns the available target types as "
6524 "a list of strings",
6528 .mode = COMMAND_ANY,
6529 .jim_handler = jim_target_names,
6530 .help = "Returns the names of all targets as a list of strings",
6534 .mode = COMMAND_ANY,
6535 .jim_handler = jim_target_smp,
6536 .usage = "targetname1 targetname2 ...",
6537 .help = "gather several target in a smp list"
6540 COMMAND_REGISTRATION_DONE
6544 target_addr_t address;
6550 static int fastload_num;
6551 static struct fast_load *fastload;
6553 static void free_fastload(void)
6556 for (int i = 0; i < fastload_num; i++)
6557 free(fastload[i].data);
6563 COMMAND_HANDLER(handle_fast_load_image_command)
6567 uint32_t image_size;
6568 target_addr_t min_address = 0;
6569 target_addr_t max_address = -1;
6573 int retval = CALL_COMMAND_HANDLER(parse_load_image_command,
6574 &image, &min_address, &max_address);
6575 if (retval != ERROR_OK)
6578 struct duration bench;
6579 duration_start(&bench);
6581 retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL);
6582 if (retval != ERROR_OK)
6587 fastload_num = image.num_sections;
6588 fastload = malloc(sizeof(struct fast_load)*image.num_sections);
6590 command_print(CMD, "out of memory");
6591 image_close(&image);
6594 memset(fastload, 0, sizeof(struct fast_load)*image.num_sections);
6595 for (unsigned int i = 0; i < image.num_sections; i++) {
6596 buffer = malloc(image.sections[i].size);
6598 command_print(CMD, "error allocating buffer for section (%d bytes)",
6599 (int)(image.sections[i].size));
6600 retval = ERROR_FAIL;
6604 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
6605 if (retval != ERROR_OK) {
6610 uint32_t offset = 0;
6611 uint32_t length = buf_cnt;
6613 /* DANGER!!! beware of unsigned comparison here!!! */
6615 if ((image.sections[i].base_address + buf_cnt >= min_address) &&
6616 (image.sections[i].base_address < max_address)) {
6617 if (image.sections[i].base_address < min_address) {
6618 /* clip addresses below */
6619 offset += min_address-image.sections[i].base_address;
6623 if (image.sections[i].base_address + buf_cnt > max_address)
6624 length -= (image.sections[i].base_address + buf_cnt)-max_address;
6626 fastload[i].address = image.sections[i].base_address + offset;
6627 fastload[i].data = malloc(length);
6628 if (!fastload[i].data) {
6630 command_print(CMD, "error allocating buffer for section (%" PRIu32 " bytes)",
6632 retval = ERROR_FAIL;
6635 memcpy(fastload[i].data, buffer + offset, length);
6636 fastload[i].length = length;
6638 image_size += length;
6639 command_print(CMD, "%u bytes written at address 0x%8.8x",
6640 (unsigned int)length,
6641 ((unsigned int)(image.sections[i].base_address + offset)));
6647 if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) {
6648 command_print(CMD, "Loaded %" PRIu32 " bytes "
6649 "in %fs (%0.3f KiB/s)", image_size,
6650 duration_elapsed(&bench), duration_kbps(&bench, image_size));
6653 "WARNING: image has not been loaded to target!"
6654 "You can issue a 'fast_load' to finish loading.");
6657 image_close(&image);
6659 if (retval != ERROR_OK)
6665 COMMAND_HANDLER(handle_fast_load_command)
6668 return ERROR_COMMAND_SYNTAX_ERROR;
6670 LOG_ERROR("No image in memory");
6674 int64_t ms = timeval_ms();
6676 int retval = ERROR_OK;
6677 for (i = 0; i < fastload_num; i++) {
6678 struct target *target = get_current_target(CMD_CTX);
6679 command_print(CMD, "Write to 0x%08x, length 0x%08x",
6680 (unsigned int)(fastload[i].address),
6681 (unsigned int)(fastload[i].length));
6682 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
6683 if (retval != ERROR_OK)
6685 size += fastload[i].length;
6687 if (retval == ERROR_OK) {
6688 int64_t after = timeval_ms();
6689 command_print(CMD, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
6694 static const struct command_registration target_command_handlers[] = {
6697 .handler = handle_targets_command,
6698 .mode = COMMAND_ANY,
6699 .help = "change current default target (one parameter) "
6700 "or prints table of all targets (no parameters)",
6701 .usage = "[target]",
6705 .mode = COMMAND_CONFIG,
6706 .help = "configure target",
6707 .chain = target_subcommand_handlers,
6710 COMMAND_REGISTRATION_DONE
6713 int target_register_commands(struct command_context *cmd_ctx)
6715 return register_commands(cmd_ctx, NULL, target_command_handlers);
6718 static bool target_reset_nag = true;
6720 bool get_target_reset_nag(void)
6722 return target_reset_nag;
6725 COMMAND_HANDLER(handle_target_reset_nag)
6727 return CALL_COMMAND_HANDLER(handle_command_parse_bool,
6728 &target_reset_nag, "Nag after each reset about options to improve "
6732 COMMAND_HANDLER(handle_ps_command)
6734 struct target *target = get_current_target(CMD_CTX);
6736 if (target->state != TARGET_HALTED) {
6737 LOG_INFO("target not halted !!");
6741 if ((target->rtos) && (target->rtos->type)
6742 && (target->rtos->type->ps_command)) {
6743 display = target->rtos->type->ps_command(target);
6744 command_print(CMD, "%s", display);
6749 return ERROR_TARGET_FAILURE;
6753 static void binprint(struct command_invocation *cmd, const char *text, const uint8_t *buf, int size)
6756 command_print_sameline(cmd, "%s", text);
6757 for (int i = 0; i < size; i++)
6758 command_print_sameline(cmd, " %02x", buf[i]);
6759 command_print(cmd, " ");
6762 COMMAND_HANDLER(handle_test_mem_access_command)
6764 struct target *target = get_current_target(CMD_CTX);
6766 int retval = ERROR_OK;
6768 if (target->state != TARGET_HALTED) {
6769 LOG_INFO("target not halted !!");
6774 return ERROR_COMMAND_SYNTAX_ERROR;
6776 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], test_size);
6779 size_t num_bytes = test_size + 4;
6781 struct working_area *wa = NULL;
6782 retval = target_alloc_working_area(target, num_bytes, &wa);
6783 if (retval != ERROR_OK) {
6784 LOG_ERROR("Not enough working area");
6788 uint8_t *test_pattern = malloc(num_bytes);
6790 for (size_t i = 0; i < num_bytes; i++)
6791 test_pattern[i] = rand();
6793 retval = target_write_memory(target, wa->address, 1, num_bytes, test_pattern);
6794 if (retval != ERROR_OK) {
6795 LOG_ERROR("Test pattern write failed");
6799 for (int host_offset = 0; host_offset <= 1; host_offset++) {
6800 for (int size = 1; size <= 4; size *= 2) {
6801 for (int offset = 0; offset < 4; offset++) {
6802 uint32_t count = test_size / size;
6803 size_t host_bufsiz = (count + 2) * size + host_offset;
6804 uint8_t *read_ref = malloc(host_bufsiz);
6805 uint8_t *read_buf = malloc(host_bufsiz);
6807 for (size_t i = 0; i < host_bufsiz; i++) {
6808 read_ref[i] = rand();
6809 read_buf[i] = read_ref[i];
6811 command_print_sameline(CMD,
6812 "Test read %" PRIu32 " x %d @ %d to %saligned buffer: ", count,
6813 size, offset, host_offset ? "un" : "");
6815 struct duration bench;
6816 duration_start(&bench);
6818 retval = target_read_memory(target, wa->address + offset, size, count,
6819 read_buf + size + host_offset);
6821 duration_measure(&bench);
6823 if (retval == ERROR_TARGET_UNALIGNED_ACCESS) {
6824 command_print(CMD, "Unsupported alignment");
6826 } else if (retval != ERROR_OK) {
6827 command_print(CMD, "Memory read failed");
6831 /* replay on host */
6832 memcpy(read_ref + size + host_offset, test_pattern + offset, count * size);
6835 int result = memcmp(read_ref, read_buf, host_bufsiz);
6837 command_print(CMD, "Pass in %fs (%0.3f KiB/s)",
6838 duration_elapsed(&bench),
6839 duration_kbps(&bench, count * size));
6841 command_print(CMD, "Compare failed");
6842 binprint(CMD, "ref:", read_ref, host_bufsiz);
6843 binprint(CMD, "buf:", read_buf, host_bufsiz);
6855 target_free_working_area(target, wa);
6858 num_bytes = test_size + 4 + 4 + 4;
6860 retval = target_alloc_working_area(target, num_bytes, &wa);
6861 if (retval != ERROR_OK) {
6862 LOG_ERROR("Not enough working area");
6866 test_pattern = malloc(num_bytes);
6868 for (size_t i = 0; i < num_bytes; i++)
6869 test_pattern[i] = rand();
6871 for (int host_offset = 0; host_offset <= 1; host_offset++) {
6872 for (int size = 1; size <= 4; size *= 2) {
6873 for (int offset = 0; offset < 4; offset++) {
6874 uint32_t count = test_size / size;
6875 size_t host_bufsiz = count * size + host_offset;
6876 uint8_t *read_ref = malloc(num_bytes);
6877 uint8_t *read_buf = malloc(num_bytes);
6878 uint8_t *write_buf = malloc(host_bufsiz);
6880 for (size_t i = 0; i < host_bufsiz; i++)
6881 write_buf[i] = rand();
6882 command_print_sameline(CMD,
6883 "Test write %" PRIu32 " x %d @ %d from %saligned buffer: ", count,
6884 size, offset, host_offset ? "un" : "");
6886 retval = target_write_memory(target, wa->address, 1, num_bytes, test_pattern);
6887 if (retval != ERROR_OK) {
6888 command_print(CMD, "Test pattern write failed");
6892 /* replay on host */
6893 memcpy(read_ref, test_pattern, num_bytes);
6894 memcpy(read_ref + size + offset, write_buf + host_offset, count * size);
6896 struct duration bench;
6897 duration_start(&bench);
6899 retval = target_write_memory(target, wa->address + size + offset, size, count,
6900 write_buf + host_offset);
6902 duration_measure(&bench);
6904 if (retval == ERROR_TARGET_UNALIGNED_ACCESS) {
6905 command_print(CMD, "Unsupported alignment");
6907 } else if (retval != ERROR_OK) {
6908 command_print(CMD, "Memory write failed");
6913 retval = target_read_memory(target, wa->address, 1, num_bytes, read_buf);
6914 if (retval != ERROR_OK) {
6915 command_print(CMD, "Test pattern write failed");
6920 int result = memcmp(read_ref, read_buf, num_bytes);
6922 command_print(CMD, "Pass in %fs (%0.3f KiB/s)",
6923 duration_elapsed(&bench),
6924 duration_kbps(&bench, count * size));
6926 command_print(CMD, "Compare failed");
6927 binprint(CMD, "ref:", read_ref, num_bytes);
6928 binprint(CMD, "buf:", read_buf, num_bytes);
6939 target_free_working_area(target, wa);
6943 static const struct command_registration target_exec_command_handlers[] = {
6945 .name = "fast_load_image",
6946 .handler = handle_fast_load_image_command,
6947 .mode = COMMAND_ANY,
6948 .help = "Load image into server memory for later use by "
6949 "fast_load; primarily for profiling",
6950 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
6951 "[min_address [max_length]]",
6954 .name = "fast_load",
6955 .handler = handle_fast_load_command,
6956 .mode = COMMAND_EXEC,
6957 .help = "loads active fast load image to current target "
6958 "- mainly for profiling purposes",
6963 .handler = handle_profile_command,
6964 .mode = COMMAND_EXEC,
6965 .usage = "seconds filename [start end]",
6966 .help = "profiling samples the CPU PC",
6968 /** @todo don't register virt2phys() unless target supports it */
6970 .name = "virt2phys",
6971 .handler = handle_virt2phys_command,
6972 .mode = COMMAND_ANY,
6973 .help = "translate a virtual address into a physical address",
6974 .usage = "virtual_address",
6978 .handler = handle_reg_command,
6979 .mode = COMMAND_EXEC,
6980 .help = "display (reread from target with \"force\") or set a register; "
6981 "with no arguments, displays all registers and their values",
6982 .usage = "[(register_number|register_name) [(value|'force')]]",
6986 .handler = handle_poll_command,
6987 .mode = COMMAND_EXEC,
6988 .help = "poll target state; or reconfigure background polling",
6989 .usage = "['on'|'off']",
6992 .name = "wait_halt",
6993 .handler = handle_wait_halt_command,
6994 .mode = COMMAND_EXEC,
6995 .help = "wait up to the specified number of milliseconds "
6996 "(default 5000) for a previously requested halt",
6997 .usage = "[milliseconds]",
7001 .handler = handle_halt_command,
7002 .mode = COMMAND_EXEC,
7003 .help = "request target to halt, then wait up to the specified "
7004 "number of milliseconds (default 5000) for it to complete",
7005 .usage = "[milliseconds]",
7009 .handler = handle_resume_command,
7010 .mode = COMMAND_EXEC,
7011 .help = "resume target execution from current PC or address",
7012 .usage = "[address]",
7016 .handler = handle_reset_command,
7017 .mode = COMMAND_EXEC,
7018 .usage = "[run|halt|init]",
7019 .help = "Reset all targets into the specified mode. "
7020 "Default reset mode is run, if not given.",
7023 .name = "soft_reset_halt",
7024 .handler = handle_soft_reset_halt_command,
7025 .mode = COMMAND_EXEC,
7027 .help = "halt the target and do a soft reset",
7031 .handler = handle_step_command,
7032 .mode = COMMAND_EXEC,
7033 .help = "step one instruction from current PC or address",
7034 .usage = "[address]",
7038 .handler = handle_md_command,
7039 .mode = COMMAND_EXEC,
7040 .help = "display memory double-words",
7041 .usage = "['phys'] address [count]",
7045 .handler = handle_md_command,
7046 .mode = COMMAND_EXEC,
7047 .help = "display memory words",
7048 .usage = "['phys'] address [count]",
7052 .handler = handle_md_command,
7053 .mode = COMMAND_EXEC,
7054 .help = "display memory half-words",
7055 .usage = "['phys'] address [count]",
7059 .handler = handle_md_command,
7060 .mode = COMMAND_EXEC,
7061 .help = "display memory bytes",
7062 .usage = "['phys'] address [count]",
7066 .handler = handle_mw_command,
7067 .mode = COMMAND_EXEC,
7068 .help = "write memory double-word",
7069 .usage = "['phys'] address value [count]",
7073 .handler = handle_mw_command,
7074 .mode = COMMAND_EXEC,
7075 .help = "write memory word",
7076 .usage = "['phys'] address value [count]",
7080 .handler = handle_mw_command,
7081 .mode = COMMAND_EXEC,
7082 .help = "write memory half-word",
7083 .usage = "['phys'] address value [count]",
7087 .handler = handle_mw_command,
7088 .mode = COMMAND_EXEC,
7089 .help = "write memory byte",
7090 .usage = "['phys'] address value [count]",
7094 .handler = handle_bp_command,
7095 .mode = COMMAND_EXEC,
7096 .help = "list or set hardware or software breakpoint",
7097 .usage = "[<address> [<asid>] <length> ['hw'|'hw_ctx']]",
7101 .handler = handle_rbp_command,
7102 .mode = COMMAND_EXEC,
7103 .help = "remove breakpoint",
7104 .usage = "'all' | address",
7108 .handler = handle_wp_command,
7109 .mode = COMMAND_EXEC,
7110 .help = "list (no params) or create watchpoints",
7111 .usage = "[address length [('r'|'w'|'a') value [mask]]]",
7115 .handler = handle_rwp_command,
7116 .mode = COMMAND_EXEC,
7117 .help = "remove watchpoint",
7121 .name = "load_image",
7122 .handler = handle_load_image_command,
7123 .mode = COMMAND_EXEC,
7124 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
7125 "[min_address] [max_length]",
7128 .name = "dump_image",
7129 .handler = handle_dump_image_command,
7130 .mode = COMMAND_EXEC,
7131 .usage = "filename address size",
7134 .name = "verify_image_checksum",
7135 .handler = handle_verify_image_checksum_command,
7136 .mode = COMMAND_EXEC,
7137 .usage = "filename [offset [type]]",
7140 .name = "verify_image",
7141 .handler = handle_verify_image_command,
7142 .mode = COMMAND_EXEC,
7143 .usage = "filename [offset [type]]",
7146 .name = "test_image",
7147 .handler = handle_test_image_command,
7148 .mode = COMMAND_EXEC,
7149 .usage = "filename [offset [type]]",
7153 .mode = COMMAND_EXEC,
7154 .jim_handler = target_jim_get_reg,
7155 .help = "Get register values from the target",
7160 .mode = COMMAND_EXEC,
7161 .jim_handler = target_jim_set_reg,
7162 .help = "Set target register values",
7166 .name = "read_memory",
7167 .mode = COMMAND_EXEC,
7168 .jim_handler = target_jim_read_memory,
7169 .help = "Read Tcl list of 8/16/32/64 bit numbers from target memory",
7170 .usage = "address width count ['phys']",
7173 .name = "write_memory",
7174 .mode = COMMAND_EXEC,
7175 .jim_handler = target_jim_write_memory,
7176 .help = "Write Tcl list of 8/16/32/64 bit numbers to target memory",
7177 .usage = "address width data ['phys']",
7180 .name = "reset_nag",
7181 .handler = handle_target_reset_nag,
7182 .mode = COMMAND_ANY,
7183 .help = "Nag after each reset about options that could have been "
7184 "enabled to improve performance.",
7185 .usage = "['enable'|'disable']",
7189 .handler = handle_ps_command,
7190 .mode = COMMAND_EXEC,
7191 .help = "list all tasks",
7195 .name = "test_mem_access",
7196 .handler = handle_test_mem_access_command,
7197 .mode = COMMAND_EXEC,
7198 .help = "Test the target's memory access functions",
7202 COMMAND_REGISTRATION_DONE
7204 static int target_register_user_commands(struct command_context *cmd_ctx)
7206 int retval = ERROR_OK;
7207 retval = target_request_register_commands(cmd_ctx);
7208 if (retval != ERROR_OK)
7211 retval = trace_register_commands(cmd_ctx);
7212 if (retval != ERROR_OK)
7216 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);