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/nvp.h>
35 #include <helper/time_support.h>
36 #include <jtag/jtag.h>
37 #include <flash/nor/core.h>
40 #include "target_type.h"
41 #include "target_request.h"
42 #include "breakpoints.h"
46 #include "rtos/rtos.h"
47 #include "transport/transport.h"
50 #include "semihosting_common.h"
52 /* default halt wait timeout (ms) */
53 #define DEFAULT_HALT_TIMEOUT 5000
55 static int target_read_buffer_default(struct target
*target
, target_addr_t address
,
56 uint32_t count
, uint8_t *buffer
);
57 static int target_write_buffer_default(struct target
*target
, target_addr_t address
,
58 uint32_t count
, const uint8_t *buffer
);
59 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
60 int argc
, Jim_Obj
* const *argv
);
61 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
62 int argc
, Jim_Obj
* const *argv
);
63 static int target_register_user_commands(struct command_context
*cmd_ctx
);
64 static int target_get_gdb_fileio_info_default(struct target
*target
,
65 struct gdb_fileio_info
*fileio_info
);
66 static int target_gdb_fileio_end_default(struct target
*target
, int retcode
,
67 int fileio_errno
, bool ctrl_c
);
69 static struct target_type
*target_types
[] = {
111 struct target
*all_targets
;
112 static struct target_event_callback
*target_event_callbacks
;
113 static struct target_timer_callback
*target_timer_callbacks
;
114 static int64_t target_timer_next_event_value
;
115 static LIST_HEAD(target_reset_callback_list
);
116 static LIST_HEAD(target_trace_callback_list
);
117 static const int polling_interval
= TARGET_DEFAULT_POLLING_INTERVAL
;
118 static LIST_HEAD(empty_smp_targets
);
125 static const struct nvp nvp_assert
[] = {
126 { .name
= "assert", NVP_ASSERT
},
127 { .name
= "deassert", NVP_DEASSERT
},
128 { .name
= "T", NVP_ASSERT
},
129 { .name
= "F", NVP_DEASSERT
},
130 { .name
= "t", NVP_ASSERT
},
131 { .name
= "f", NVP_DEASSERT
},
132 { .name
= NULL
, .value
= -1 }
135 static const struct nvp nvp_error_target
[] = {
136 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
137 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
138 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
139 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
140 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
141 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
142 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
143 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
144 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
145 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
146 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
147 { .value
= -1, .name
= NULL
}
150 static const char *target_strerror_safe(int err
)
154 n
= nvp_value2name(nvp_error_target
, err
);
161 static const struct jim_nvp nvp_target_event
[] = {
163 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
164 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
165 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
166 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
167 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
168 { .value
= TARGET_EVENT_STEP_START
, .name
= "step-start" },
169 { .value
= TARGET_EVENT_STEP_END
, .name
= "step-end" },
171 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
172 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
174 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
175 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
176 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
177 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
178 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
179 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
180 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
181 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
183 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
184 { .value
= TARGET_EVENT_EXAMINE_FAIL
, .name
= "examine-fail" },
185 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
187 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
188 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
190 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
191 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
193 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
194 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
196 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
197 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
199 { .value
= TARGET_EVENT_TRACE_CONFIG
, .name
= "trace-config" },
201 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0X100
, .name
= "semihosting-user-cmd-0x100" },
202 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0X101
, .name
= "semihosting-user-cmd-0x101" },
203 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0X102
, .name
= "semihosting-user-cmd-0x102" },
204 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0X103
, .name
= "semihosting-user-cmd-0x103" },
205 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0X104
, .name
= "semihosting-user-cmd-0x104" },
206 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0X105
, .name
= "semihosting-user-cmd-0x105" },
207 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0X106
, .name
= "semihosting-user-cmd-0x106" },
208 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0X107
, .name
= "semihosting-user-cmd-0x107" },
210 { .name
= NULL
, .value
= -1 }
213 static const struct nvp nvp_target_state
[] = {
214 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
215 { .name
= "running", .value
= TARGET_RUNNING
},
216 { .name
= "halted", .value
= TARGET_HALTED
},
217 { .name
= "reset", .value
= TARGET_RESET
},
218 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
219 { .name
= NULL
, .value
= -1 },
222 static const struct nvp nvp_target_debug_reason
[] = {
223 { .name
= "debug-request", .value
= DBG_REASON_DBGRQ
},
224 { .name
= "breakpoint", .value
= DBG_REASON_BREAKPOINT
},
225 { .name
= "watchpoint", .value
= DBG_REASON_WATCHPOINT
},
226 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
227 { .name
= "single-step", .value
= DBG_REASON_SINGLESTEP
},
228 { .name
= "target-not-halted", .value
= DBG_REASON_NOTHALTED
},
229 { .name
= "program-exit", .value
= DBG_REASON_EXIT
},
230 { .name
= "exception-catch", .value
= DBG_REASON_EXC_CATCH
},
231 { .name
= "undefined", .value
= DBG_REASON_UNDEFINED
},
232 { .name
= NULL
, .value
= -1 },
235 static const struct jim_nvp nvp_target_endian
[] = {
236 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
237 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
238 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
239 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
240 { .name
= NULL
, .value
= -1 },
243 static const struct nvp nvp_reset_modes
[] = {
244 { .name
= "unknown", .value
= RESET_UNKNOWN
},
245 { .name
= "run", .value
= RESET_RUN
},
246 { .name
= "halt", .value
= RESET_HALT
},
247 { .name
= "init", .value
= RESET_INIT
},
248 { .name
= NULL
, .value
= -1 },
251 const char *debug_reason_name(struct target
*t
)
255 cp
= nvp_value2name(nvp_target_debug_reason
,
256 t
->debug_reason
)->name
;
258 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
259 cp
= "(*BUG*unknown*BUG*)";
264 const char *target_state_name(struct target
*t
)
267 cp
= nvp_value2name(nvp_target_state
, t
->state
)->name
;
269 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
270 cp
= "(*BUG*unknown*BUG*)";
273 if (!target_was_examined(t
) && t
->defer_examine
)
274 cp
= "examine deferred";
279 const char *target_event_name(enum target_event event
)
282 cp
= jim_nvp_value2name_simple(nvp_target_event
, event
)->name
;
284 LOG_ERROR("Invalid target event: %d", (int)(event
));
285 cp
= "(*BUG*unknown*BUG*)";
290 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
293 cp
= nvp_value2name(nvp_reset_modes
, reset_mode
)->name
;
295 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
296 cp
= "(*BUG*unknown*BUG*)";
301 /* determine the number of the new target */
302 static int new_target_number(void)
307 /* number is 0 based */
311 if (x
< t
->target_number
)
312 x
= t
->target_number
;
318 static void append_to_list_all_targets(struct target
*target
)
320 struct target
**t
= &all_targets
;
327 /* read a uint64_t from a buffer in target memory endianness */
328 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
330 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
331 return le_to_h_u64(buffer
);
333 return be_to_h_u64(buffer
);
336 /* read a uint32_t from a buffer in target memory endianness */
337 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
339 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
340 return le_to_h_u32(buffer
);
342 return be_to_h_u32(buffer
);
345 /* read a uint24_t from a buffer in target memory endianness */
346 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
348 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
349 return le_to_h_u24(buffer
);
351 return be_to_h_u24(buffer
);
354 /* read a uint16_t from a buffer in target memory endianness */
355 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
357 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
358 return le_to_h_u16(buffer
);
360 return be_to_h_u16(buffer
);
363 /* write a uint64_t to a buffer in target memory endianness */
364 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
366 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
367 h_u64_to_le(buffer
, value
);
369 h_u64_to_be(buffer
, value
);
372 /* write a uint32_t to a buffer in target memory endianness */
373 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
375 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
376 h_u32_to_le(buffer
, value
);
378 h_u32_to_be(buffer
, value
);
381 /* write a uint24_t to a buffer in target memory endianness */
382 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
384 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
385 h_u24_to_le(buffer
, value
);
387 h_u24_to_be(buffer
, value
);
390 /* write a uint16_t to a buffer in target memory endianness */
391 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
393 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
394 h_u16_to_le(buffer
, value
);
396 h_u16_to_be(buffer
, value
);
399 /* write a uint8_t to a buffer in target memory endianness */
400 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
405 /* write a uint64_t array to a buffer in target memory endianness */
406 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
409 for (i
= 0; i
< count
; i
++)
410 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
413 /* write a uint32_t array to a buffer in target memory endianness */
414 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
417 for (i
= 0; i
< count
; i
++)
418 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
421 /* write a uint16_t array to a buffer in target memory endianness */
422 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
425 for (i
= 0; i
< count
; i
++)
426 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
429 /* write a uint64_t array to a buffer in target memory endianness */
430 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
433 for (i
= 0; i
< count
; i
++)
434 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
437 /* write a uint32_t array to a buffer in target memory endianness */
438 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
441 for (i
= 0; i
< count
; i
++)
442 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
445 /* write a uint16_t array to a buffer in target memory endianness */
446 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
449 for (i
= 0; i
< count
; i
++)
450 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
453 /* return a pointer to a configured target; id is name or number */
454 struct target
*get_target(const char *id
)
456 struct target
*target
;
458 /* try as tcltarget name */
459 for (target
= all_targets
; target
; target
= target
->next
) {
460 if (!target_name(target
))
462 if (strcmp(id
, target_name(target
)) == 0)
466 /* It's OK to remove this fallback sometime after August 2010 or so */
468 /* no match, try as number */
470 if (parse_uint(id
, &num
) != ERROR_OK
)
473 for (target
= all_targets
; target
; target
= target
->next
) {
474 if (target
->target_number
== (int)num
) {
475 LOG_WARNING("use '%s' as target identifier, not '%u'",
476 target_name(target
), num
);
484 /* returns a pointer to the n-th configured target */
485 struct target
*get_target_by_num(int num
)
487 struct target
*target
= all_targets
;
490 if (target
->target_number
== num
)
492 target
= target
->next
;
498 struct target
*get_current_target(struct command_context
*cmd_ctx
)
500 struct target
*target
= get_current_target_or_null(cmd_ctx
);
503 LOG_ERROR("BUG: current_target out of bounds");
510 struct target
*get_current_target_or_null(struct command_context
*cmd_ctx
)
512 return cmd_ctx
->current_target_override
513 ? cmd_ctx
->current_target_override
514 : cmd_ctx
->current_target
;
517 int target_poll(struct target
*target
)
521 /* We can't poll until after examine */
522 if (!target_was_examined(target
)) {
523 /* Fail silently lest we pollute the log */
527 retval
= target
->type
->poll(target
);
528 if (retval
!= ERROR_OK
)
531 if (target
->halt_issued
) {
532 if (target
->state
== TARGET_HALTED
)
533 target
->halt_issued
= false;
535 int64_t t
= timeval_ms() - target
->halt_issued_time
;
536 if (t
> DEFAULT_HALT_TIMEOUT
) {
537 target
->halt_issued
= false;
538 LOG_INFO("Halt timed out, wake up GDB.");
539 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
547 int target_halt(struct target
*target
)
550 /* We can't poll until after examine */
551 if (!target_was_examined(target
)) {
552 LOG_ERROR("Target not examined yet");
556 retval
= target
->type
->halt(target
);
557 if (retval
!= ERROR_OK
)
560 target
->halt_issued
= true;
561 target
->halt_issued_time
= timeval_ms();
567 * Make the target (re)start executing using its saved execution
568 * context (possibly with some modifications).
570 * @param target Which target should start executing.
571 * @param current True to use the target's saved program counter instead
572 * of the address parameter
573 * @param address Optionally used as the program counter.
574 * @param handle_breakpoints True iff breakpoints at the resumption PC
575 * should be skipped. (For example, maybe execution was stopped by
576 * such a breakpoint, in which case it would be counterproductive to
578 * @param debug_execution False if all working areas allocated by OpenOCD
579 * should be released and/or restored to their original contents.
580 * (This would for example be true to run some downloaded "helper"
581 * algorithm code, which resides in one such working buffer and uses
582 * another for data storage.)
584 * @todo Resolve the ambiguity about what the "debug_execution" flag
585 * signifies. For example, Target implementations don't agree on how
586 * it relates to invalidation of the register cache, or to whether
587 * breakpoints and watchpoints should be enabled. (It would seem wrong
588 * to enable breakpoints when running downloaded "helper" algorithms
589 * (debug_execution true), since the breakpoints would be set to match
590 * target firmware being debugged, not the helper algorithm.... and
591 * enabling them could cause such helpers to malfunction (for example,
592 * by overwriting data with a breakpoint instruction. On the other
593 * hand the infrastructure for running such helpers might use this
594 * procedure but rely on hardware breakpoint to detect termination.)
596 int target_resume(struct target
*target
, int current
, target_addr_t address
,
597 int handle_breakpoints
, int debug_execution
)
601 /* We can't poll until after examine */
602 if (!target_was_examined(target
)) {
603 LOG_ERROR("Target not examined yet");
607 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
609 /* note that resume *must* be asynchronous. The CPU can halt before
610 * we poll. The CPU can even halt at the current PC as a result of
611 * a software breakpoint being inserted by (a bug?) the application.
614 * resume() triggers the event 'resumed'. The execution of TCL commands
615 * in the event handler causes the polling of targets. If the target has
616 * already halted for a breakpoint, polling will run the 'halted' event
617 * handler before the pending 'resumed' handler.
618 * Disable polling during resume() to guarantee the execution of handlers
619 * in the correct order.
621 bool save_poll_mask
= jtag_poll_mask();
622 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
623 jtag_poll_unmask(save_poll_mask
);
625 if (retval
!= ERROR_OK
)
628 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
633 static int target_process_reset(struct command_invocation
*cmd
, enum target_reset_mode reset_mode
)
638 n
= nvp_value2name(nvp_reset_modes
, reset_mode
);
640 LOG_ERROR("invalid reset mode");
644 struct target
*target
;
645 for (target
= all_targets
; target
; target
= target
->next
)
646 target_call_reset_callbacks(target
, reset_mode
);
648 /* disable polling during reset to make reset event scripts
649 * more predictable, i.e. dr/irscan & pathmove in events will
650 * not have JTAG operations injected into the middle of a sequence.
652 bool save_poll_mask
= jtag_poll_mask();
654 sprintf(buf
, "ocd_process_reset %s", n
->name
);
655 retval
= Jim_Eval(cmd
->ctx
->interp
, buf
);
657 jtag_poll_unmask(save_poll_mask
);
659 if (retval
!= JIM_OK
) {
660 Jim_MakeErrorMessage(cmd
->ctx
->interp
);
661 command_print(cmd
, "%s", Jim_GetString(Jim_GetResult(cmd
->ctx
->interp
), NULL
));
665 /* We want any events to be processed before the prompt */
666 retval
= target_call_timer_callbacks_now();
668 for (target
= all_targets
; target
; target
= target
->next
) {
669 target
->type
->check_reset(target
);
670 target
->running_alg
= false;
676 static int identity_virt2phys(struct target
*target
,
677 target_addr_t
virtual, target_addr_t
*physical
)
683 static int no_mmu(struct target
*target
, int *enabled
)
690 * Reset the @c examined flag for the given target.
691 * Pure paranoia -- targets are zeroed on allocation.
693 static inline void target_reset_examined(struct target
*target
)
695 target
->examined
= false;
698 static int default_examine(struct target
*target
)
700 target_set_examined(target
);
704 /* no check by default */
705 static int default_check_reset(struct target
*target
)
710 /* Equivalent Tcl code arp_examine_one is in src/target/startup.tcl
712 int target_examine_one(struct target
*target
)
714 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
716 int retval
= target
->type
->examine(target
);
717 if (retval
!= ERROR_OK
) {
718 target_reset_examined(target
);
719 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_FAIL
);
723 target_set_examined(target
);
724 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
729 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
731 struct target
*target
= priv
;
733 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
736 jtag_unregister_event_callback(jtag_enable_callback
, target
);
738 return target_examine_one(target
);
741 /* Targets that correctly implement init + examine, i.e.
742 * no communication with target during init:
746 int target_examine(void)
748 int retval
= ERROR_OK
;
749 struct target
*target
;
751 for (target
= all_targets
; target
; target
= target
->next
) {
752 /* defer examination, but don't skip it */
753 if (!target
->tap
->enabled
) {
754 jtag_register_event_callback(jtag_enable_callback
,
759 if (target
->defer_examine
)
762 int retval2
= target_examine_one(target
);
763 if (retval2
!= ERROR_OK
) {
764 LOG_WARNING("target %s examination failed", target_name(target
));
771 const char *target_type_name(struct target
*target
)
773 return target
->type
->name
;
776 static int target_soft_reset_halt(struct target
*target
)
778 if (!target_was_examined(target
)) {
779 LOG_ERROR("Target not examined yet");
782 if (!target
->type
->soft_reset_halt
) {
783 LOG_ERROR("Target %s does not support soft_reset_halt",
784 target_name(target
));
787 return target
->type
->soft_reset_halt(target
);
791 * Downloads a target-specific native code algorithm to the target,
792 * and executes it. * Note that some targets may need to set up, enable,
793 * and tear down a breakpoint (hard or * soft) to detect algorithm
794 * termination, while others may support lower overhead schemes where
795 * soft breakpoints embedded in the algorithm automatically terminate the
798 * @param target used to run the algorithm
799 * @param num_mem_params
801 * @param num_reg_params
806 * @param arch_info target-specific description of the algorithm.
808 int target_run_algorithm(struct target
*target
,
809 int num_mem_params
, struct mem_param
*mem_params
,
810 int num_reg_params
, struct reg_param
*reg_param
,
811 target_addr_t entry_point
, target_addr_t exit_point
,
812 unsigned int timeout_ms
, void *arch_info
)
814 int retval
= ERROR_FAIL
;
816 if (!target_was_examined(target
)) {
817 LOG_ERROR("Target not examined yet");
820 if (!target
->type
->run_algorithm
) {
821 LOG_ERROR("Target type '%s' does not support %s",
822 target_type_name(target
), __func__
);
826 target
->running_alg
= true;
827 retval
= target
->type
->run_algorithm(target
,
828 num_mem_params
, mem_params
,
829 num_reg_params
, reg_param
,
830 entry_point
, exit_point
, timeout_ms
, arch_info
);
831 target
->running_alg
= false;
838 * Executes a target-specific native code algorithm and leaves it running.
840 * @param target used to run the algorithm
841 * @param num_mem_params
843 * @param num_reg_params
847 * @param arch_info target-specific description of the algorithm.
849 int target_start_algorithm(struct target
*target
,
850 int num_mem_params
, struct mem_param
*mem_params
,
851 int num_reg_params
, struct reg_param
*reg_params
,
852 target_addr_t entry_point
, target_addr_t exit_point
,
855 int retval
= ERROR_FAIL
;
857 if (!target_was_examined(target
)) {
858 LOG_ERROR("Target not examined yet");
861 if (!target
->type
->start_algorithm
) {
862 LOG_ERROR("Target type '%s' does not support %s",
863 target_type_name(target
), __func__
);
866 if (target
->running_alg
) {
867 LOG_ERROR("Target is already running an algorithm");
871 target
->running_alg
= true;
872 retval
= target
->type
->start_algorithm(target
,
873 num_mem_params
, mem_params
,
874 num_reg_params
, reg_params
,
875 entry_point
, exit_point
, arch_info
);
882 * Waits for an algorithm started with target_start_algorithm() to complete.
884 * @param target used to run the algorithm
885 * @param num_mem_params
887 * @param num_reg_params
891 * @param arch_info target-specific description of the algorithm.
893 int target_wait_algorithm(struct target
*target
,
894 int num_mem_params
, struct mem_param
*mem_params
,
895 int num_reg_params
, struct reg_param
*reg_params
,
896 target_addr_t exit_point
, unsigned int timeout_ms
,
899 int retval
= ERROR_FAIL
;
901 if (!target
->type
->wait_algorithm
) {
902 LOG_ERROR("Target type '%s' does not support %s",
903 target_type_name(target
), __func__
);
906 if (!target
->running_alg
) {
907 LOG_ERROR("Target is not running an algorithm");
911 retval
= target
->type
->wait_algorithm(target
,
912 num_mem_params
, mem_params
,
913 num_reg_params
, reg_params
,
914 exit_point
, timeout_ms
, arch_info
);
915 if (retval
!= ERROR_TARGET_TIMEOUT
)
916 target
->running_alg
= false;
923 * Streams data to a circular buffer on target intended for consumption by code
924 * running asynchronously on target.
926 * This is intended for applications where target-specific native code runs
927 * on the target, receives data from the circular buffer, does something with
928 * it (most likely writing it to a flash memory), and advances the circular
931 * This assumes that the helper algorithm has already been loaded to the target,
932 * but has not been started yet. Given memory and register parameters are passed
935 * The buffer is defined by (buffer_start, buffer_size) arguments and has the
938 * [buffer_start + 0, buffer_start + 4):
939 * Write Pointer address (aka head). Written and updated by this
940 * routine when new data is written to the circular buffer.
941 * [buffer_start + 4, buffer_start + 8):
942 * Read Pointer address (aka tail). Updated by code running on the
943 * target after it consumes data.
944 * [buffer_start + 8, buffer_start + buffer_size):
945 * Circular buffer contents.
947 * See contrib/loaders/flash/stm32f1x.S for an example.
949 * @param target used to run the algorithm
950 * @param buffer address on the host where data to be sent is located
951 * @param count number of blocks to send
952 * @param block_size size in bytes of each block
953 * @param num_mem_params count of memory-based params to pass to algorithm
954 * @param mem_params memory-based params to pass to algorithm
955 * @param num_reg_params count of register-based params to pass to algorithm
956 * @param reg_params memory-based params to pass to algorithm
957 * @param buffer_start address on the target of the circular buffer structure
958 * @param buffer_size size of the circular buffer structure
959 * @param entry_point address on the target to execute to start the algorithm
960 * @param exit_point address at which to set a breakpoint to catch the
961 * end of the algorithm; can be 0 if target triggers a breakpoint itself
965 int target_run_flash_async_algorithm(struct target
*target
,
966 const uint8_t *buffer
, uint32_t count
, int block_size
,
967 int num_mem_params
, struct mem_param
*mem_params
,
968 int num_reg_params
, struct reg_param
*reg_params
,
969 uint32_t buffer_start
, uint32_t buffer_size
,
970 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
975 const uint8_t *buffer_orig
= buffer
;
977 /* Set up working area. First word is write pointer, second word is read pointer,
978 * rest is fifo data area. */
979 uint32_t wp_addr
= buffer_start
;
980 uint32_t rp_addr
= buffer_start
+ 4;
981 uint32_t fifo_start_addr
= buffer_start
+ 8;
982 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
984 uint32_t wp
= fifo_start_addr
;
985 uint32_t rp
= fifo_start_addr
;
987 /* validate block_size is 2^n */
988 assert(IS_PWR_OF_2(block_size
));
990 retval
= target_write_u32(target
, wp_addr
, wp
);
991 if (retval
!= ERROR_OK
)
993 retval
= target_write_u32(target
, rp_addr
, rp
);
994 if (retval
!= ERROR_OK
)
997 /* Start up algorithm on target and let it idle while writing the first chunk */
998 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
999 num_reg_params
, reg_params
,
1004 if (retval
!= ERROR_OK
) {
1005 LOG_ERROR("error starting target flash write algorithm");
1011 retval
= target_read_u32(target
, rp_addr
, &rp
);
1012 if (retval
!= ERROR_OK
) {
1013 LOG_ERROR("failed to get read pointer");
1017 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
1018 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
1021 LOG_ERROR("flash write algorithm aborted by target");
1022 retval
= ERROR_FLASH_OPERATION_FAILED
;
1026 if (!IS_ALIGNED(rp
- fifo_start_addr
, block_size
) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
1027 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
1031 /* Count the number of bytes available in the fifo without
1032 * crossing the wrap around. Make sure to not fill it completely,
1033 * because that would make wp == rp and that's the empty condition. */
1034 uint32_t thisrun_bytes
;
1036 thisrun_bytes
= rp
- wp
- block_size
;
1037 else if (rp
> fifo_start_addr
)
1038 thisrun_bytes
= fifo_end_addr
- wp
;
1040 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
1042 if (thisrun_bytes
== 0) {
1043 /* Throttle polling a bit if transfer is (much) faster than flash
1044 * programming. The exact delay shouldn't matter as long as it's
1045 * less than buffer size / flash speed. This is very unlikely to
1046 * run when using high latency connections such as USB. */
1049 /* to stop an infinite loop on some targets check and increment a timeout
1050 * this issue was observed on a stellaris using the new ICDI interface */
1051 if (timeout
++ >= 2500) {
1052 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1053 return ERROR_FLASH_OPERATION_FAILED
;
1058 /* reset our timeout */
1061 /* Limit to the amount of data we actually want to write */
1062 if (thisrun_bytes
> count
* block_size
)
1063 thisrun_bytes
= count
* block_size
;
1065 /* Force end of large blocks to be word aligned */
1066 if (thisrun_bytes
>= 16)
1067 thisrun_bytes
-= (rp
+ thisrun_bytes
) & 0x03;
1069 /* Write data to fifo */
1070 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
1071 if (retval
!= ERROR_OK
)
1074 /* Update counters and wrap write pointer */
1075 buffer
+= thisrun_bytes
;
1076 count
-= thisrun_bytes
/ block_size
;
1077 wp
+= thisrun_bytes
;
1078 if (wp
>= fifo_end_addr
)
1079 wp
= fifo_start_addr
;
1081 /* Store updated write pointer to target */
1082 retval
= target_write_u32(target
, wp_addr
, wp
);
1083 if (retval
!= ERROR_OK
)
1086 /* Avoid GDB timeouts */
1090 if (retval
!= ERROR_OK
) {
1091 /* abort flash write algorithm on target */
1092 target_write_u32(target
, wp_addr
, 0);
1095 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1096 num_reg_params
, reg_params
,
1101 if (retval2
!= ERROR_OK
) {
1102 LOG_ERROR("error waiting for target flash write algorithm");
1106 if (retval
== ERROR_OK
) {
1107 /* check if algorithm set rp = 0 after fifo writer loop finished */
1108 retval
= target_read_u32(target
, rp_addr
, &rp
);
1109 if (retval
== ERROR_OK
&& rp
== 0) {
1110 LOG_ERROR("flash write algorithm aborted by target");
1111 retval
= ERROR_FLASH_OPERATION_FAILED
;
1118 int target_run_read_async_algorithm(struct target
*target
,
1119 uint8_t *buffer
, uint32_t count
, int block_size
,
1120 int num_mem_params
, struct mem_param
*mem_params
,
1121 int num_reg_params
, struct reg_param
*reg_params
,
1122 uint32_t buffer_start
, uint32_t buffer_size
,
1123 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
1128 const uint8_t *buffer_orig
= buffer
;
1130 /* Set up working area. First word is write pointer, second word is read pointer,
1131 * rest is fifo data area. */
1132 uint32_t wp_addr
= buffer_start
;
1133 uint32_t rp_addr
= buffer_start
+ 4;
1134 uint32_t fifo_start_addr
= buffer_start
+ 8;
1135 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
1137 uint32_t wp
= fifo_start_addr
;
1138 uint32_t rp
= fifo_start_addr
;
1140 /* validate block_size is 2^n */
1141 assert(IS_PWR_OF_2(block_size
));
1143 retval
= target_write_u32(target
, wp_addr
, wp
);
1144 if (retval
!= ERROR_OK
)
1146 retval
= target_write_u32(target
, rp_addr
, rp
);
1147 if (retval
!= ERROR_OK
)
1150 /* Start up algorithm on target */
1151 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
1152 num_reg_params
, reg_params
,
1157 if (retval
!= ERROR_OK
) {
1158 LOG_ERROR("error starting target flash read algorithm");
1163 retval
= target_read_u32(target
, wp_addr
, &wp
);
1164 if (retval
!= ERROR_OK
) {
1165 LOG_ERROR("failed to get write pointer");
1169 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
1170 (size_t)(buffer
- buffer_orig
), count
, wp
, rp
);
1173 LOG_ERROR("flash read algorithm aborted by target");
1174 retval
= ERROR_FLASH_OPERATION_FAILED
;
1178 if (!IS_ALIGNED(wp
- fifo_start_addr
, block_size
) || wp
< fifo_start_addr
|| wp
>= fifo_end_addr
) {
1179 LOG_ERROR("corrupted fifo write pointer 0x%" PRIx32
, wp
);
1183 /* Count the number of bytes available in the fifo without
1184 * crossing the wrap around. */
1185 uint32_t thisrun_bytes
;
1187 thisrun_bytes
= wp
- rp
;
1189 thisrun_bytes
= fifo_end_addr
- rp
;
1191 if (thisrun_bytes
== 0) {
1192 /* Throttle polling a bit if transfer is (much) faster than flash
1193 * reading. The exact delay shouldn't matter as long as it's
1194 * less than buffer size / flash speed. This is very unlikely to
1195 * run when using high latency connections such as USB. */
1198 /* to stop an infinite loop on some targets check and increment a timeout
1199 * this issue was observed on a stellaris using the new ICDI interface */
1200 if (timeout
++ >= 2500) {
1201 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1202 return ERROR_FLASH_OPERATION_FAILED
;
1207 /* Reset our timeout */
1210 /* Limit to the amount of data we actually want to read */
1211 if (thisrun_bytes
> count
* block_size
)
1212 thisrun_bytes
= count
* block_size
;
1214 /* Force end of large blocks to be word aligned */
1215 if (thisrun_bytes
>= 16)
1216 thisrun_bytes
-= (rp
+ thisrun_bytes
) & 0x03;
1218 /* Read data from fifo */
1219 retval
= target_read_buffer(target
, rp
, thisrun_bytes
, buffer
);
1220 if (retval
!= ERROR_OK
)
1223 /* Update counters and wrap write pointer */
1224 buffer
+= thisrun_bytes
;
1225 count
-= thisrun_bytes
/ block_size
;
1226 rp
+= thisrun_bytes
;
1227 if (rp
>= fifo_end_addr
)
1228 rp
= fifo_start_addr
;
1230 /* Store updated write pointer to target */
1231 retval
= target_write_u32(target
, rp_addr
, rp
);
1232 if (retval
!= ERROR_OK
)
1235 /* Avoid GDB timeouts */
1240 if (retval
!= ERROR_OK
) {
1241 /* abort flash write algorithm on target */
1242 target_write_u32(target
, rp_addr
, 0);
1245 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1246 num_reg_params
, reg_params
,
1251 if (retval2
!= ERROR_OK
) {
1252 LOG_ERROR("error waiting for target flash write algorithm");
1256 if (retval
== ERROR_OK
) {
1257 /* check if algorithm set wp = 0 after fifo writer loop finished */
1258 retval
= target_read_u32(target
, wp_addr
, &wp
);
1259 if (retval
== ERROR_OK
&& wp
== 0) {
1260 LOG_ERROR("flash read algorithm aborted by target");
1261 retval
= ERROR_FLASH_OPERATION_FAILED
;
1268 int target_read_memory(struct target
*target
,
1269 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1271 if (!target_was_examined(target
)) {
1272 LOG_ERROR("Target not examined yet");
1275 if (!target
->type
->read_memory
) {
1276 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1279 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1282 int target_read_phys_memory(struct target
*target
,
1283 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1285 if (!target_was_examined(target
)) {
1286 LOG_ERROR("Target not examined yet");
1289 if (!target
->type
->read_phys_memory
) {
1290 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1293 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1296 int target_write_memory(struct target
*target
,
1297 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1299 if (!target_was_examined(target
)) {
1300 LOG_ERROR("Target not examined yet");
1303 if (!target
->type
->write_memory
) {
1304 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1307 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1310 int target_write_phys_memory(struct target
*target
,
1311 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1313 if (!target_was_examined(target
)) {
1314 LOG_ERROR("Target not examined yet");
1317 if (!target
->type
->write_phys_memory
) {
1318 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1321 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1324 int target_add_breakpoint(struct target
*target
,
1325 struct breakpoint
*breakpoint
)
1327 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1328 LOG_TARGET_ERROR(target
, "not halted (add breakpoint)");
1329 return ERROR_TARGET_NOT_HALTED
;
1331 return target
->type
->add_breakpoint(target
, breakpoint
);
1334 int target_add_context_breakpoint(struct target
*target
,
1335 struct breakpoint
*breakpoint
)
1337 if (target
->state
!= TARGET_HALTED
) {
1338 LOG_TARGET_ERROR(target
, "not halted (add context breakpoint)");
1339 return ERROR_TARGET_NOT_HALTED
;
1341 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1344 int target_add_hybrid_breakpoint(struct target
*target
,
1345 struct breakpoint
*breakpoint
)
1347 if (target
->state
!= TARGET_HALTED
) {
1348 LOG_TARGET_ERROR(target
, "not halted (add hybrid breakpoint)");
1349 return ERROR_TARGET_NOT_HALTED
;
1351 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1354 int target_remove_breakpoint(struct target
*target
,
1355 struct breakpoint
*breakpoint
)
1357 return target
->type
->remove_breakpoint(target
, breakpoint
);
1360 int target_add_watchpoint(struct target
*target
,
1361 struct watchpoint
*watchpoint
)
1363 if (target
->state
!= TARGET_HALTED
) {
1364 LOG_TARGET_ERROR(target
, "not halted (add watchpoint)");
1365 return ERROR_TARGET_NOT_HALTED
;
1367 return target
->type
->add_watchpoint(target
, watchpoint
);
1369 int target_remove_watchpoint(struct target
*target
,
1370 struct watchpoint
*watchpoint
)
1372 return target
->type
->remove_watchpoint(target
, watchpoint
);
1374 int target_hit_watchpoint(struct target
*target
,
1375 struct watchpoint
**hit_watchpoint
)
1377 if (target
->state
!= TARGET_HALTED
) {
1378 LOG_TARGET_ERROR(target
, "not halted (hit watchpoint)");
1379 return ERROR_TARGET_NOT_HALTED
;
1382 if (!target
->type
->hit_watchpoint
) {
1383 /* For backward compatible, if hit_watchpoint is not implemented,
1384 * return ERROR_FAIL such that gdb_server will not take the nonsense
1389 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1392 const char *target_get_gdb_arch(struct target
*target
)
1394 if (!target
->type
->get_gdb_arch
)
1396 return target
->type
->get_gdb_arch(target
);
1399 int target_get_gdb_reg_list(struct target
*target
,
1400 struct reg
**reg_list
[], int *reg_list_size
,
1401 enum target_register_class reg_class
)
1403 int result
= ERROR_FAIL
;
1405 if (!target_was_examined(target
)) {
1406 LOG_ERROR("Target not examined yet");
1410 result
= target
->type
->get_gdb_reg_list(target
, reg_list
,
1411 reg_list_size
, reg_class
);
1414 if (result
!= ERROR_OK
) {
1421 int target_get_gdb_reg_list_noread(struct target
*target
,
1422 struct reg
**reg_list
[], int *reg_list_size
,
1423 enum target_register_class reg_class
)
1425 if (target
->type
->get_gdb_reg_list_noread
&&
1426 target
->type
->get_gdb_reg_list_noread(target
, reg_list
,
1427 reg_list_size
, reg_class
) == ERROR_OK
)
1429 return target_get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1432 bool target_supports_gdb_connection(struct target
*target
)
1435 * exclude all the targets that don't provide get_gdb_reg_list
1436 * or that have explicit gdb_max_connection == 0
1438 return !!target
->type
->get_gdb_reg_list
&& !!target
->gdb_max_connections
;
1441 int target_step(struct target
*target
,
1442 int current
, target_addr_t address
, int handle_breakpoints
)
1446 target_call_event_callbacks(target
, TARGET_EVENT_STEP_START
);
1448 retval
= target
->type
->step(target
, current
, address
, handle_breakpoints
);
1449 if (retval
!= ERROR_OK
)
1452 target_call_event_callbacks(target
, TARGET_EVENT_STEP_END
);
1457 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1459 if (target
->state
!= TARGET_HALTED
) {
1460 LOG_TARGET_ERROR(target
, "not halted (gdb fileio)");
1461 return ERROR_TARGET_NOT_HALTED
;
1463 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1466 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1468 if (target
->state
!= TARGET_HALTED
) {
1469 LOG_TARGET_ERROR(target
, "not halted (gdb fileio end)");
1470 return ERROR_TARGET_NOT_HALTED
;
1472 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1475 target_addr_t
target_address_max(struct target
*target
)
1477 unsigned bits
= target_address_bits(target
);
1478 if (sizeof(target_addr_t
) * 8 == bits
)
1479 return (target_addr_t
) -1;
1481 return (((target_addr_t
) 1) << bits
) - 1;
1484 unsigned target_address_bits(struct target
*target
)
1486 if (target
->type
->address_bits
)
1487 return target
->type
->address_bits(target
);
1491 unsigned int target_data_bits(struct target
*target
)
1493 if (target
->type
->data_bits
)
1494 return target
->type
->data_bits(target
);
1498 static int target_profiling(struct target
*target
, uint32_t *samples
,
1499 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1501 return target
->type
->profiling(target
, samples
, max_num_samples
,
1502 num_samples
, seconds
);
1505 static int handle_target(void *priv
);
1507 static int target_init_one(struct command_context
*cmd_ctx
,
1508 struct target
*target
)
1510 target_reset_examined(target
);
1512 struct target_type
*type
= target
->type
;
1514 type
->examine
= default_examine
;
1516 if (!type
->check_reset
)
1517 type
->check_reset
= default_check_reset
;
1519 assert(type
->init_target
);
1521 int retval
= type
->init_target(cmd_ctx
, target
);
1522 if (retval
!= ERROR_OK
) {
1523 LOG_ERROR("target '%s' init failed", target_name(target
));
1527 /* Sanity-check MMU support ... stub in what we must, to help
1528 * implement it in stages, but warn if we need to do so.
1531 if (!type
->virt2phys
) {
1532 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1533 type
->virt2phys
= identity_virt2phys
;
1536 /* Make sure no-MMU targets all behave the same: make no
1537 * distinction between physical and virtual addresses, and
1538 * ensure that virt2phys() is always an identity mapping.
1540 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1541 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1544 type
->write_phys_memory
= type
->write_memory
;
1545 type
->read_phys_memory
= type
->read_memory
;
1546 type
->virt2phys
= identity_virt2phys
;
1549 if (!target
->type
->read_buffer
)
1550 target
->type
->read_buffer
= target_read_buffer_default
;
1552 if (!target
->type
->write_buffer
)
1553 target
->type
->write_buffer
= target_write_buffer_default
;
1555 if (!target
->type
->get_gdb_fileio_info
)
1556 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1558 if (!target
->type
->gdb_fileio_end
)
1559 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1561 if (!target
->type
->profiling
)
1562 target
->type
->profiling
= target_profiling_default
;
1567 static int target_init(struct command_context
*cmd_ctx
)
1569 struct target
*target
;
1572 for (target
= all_targets
; target
; target
= target
->next
) {
1573 retval
= target_init_one(cmd_ctx
, target
);
1574 if (retval
!= ERROR_OK
)
1581 retval
= target_register_user_commands(cmd_ctx
);
1582 if (retval
!= ERROR_OK
)
1585 retval
= target_register_timer_callback(&handle_target
,
1586 polling_interval
, TARGET_TIMER_TYPE_PERIODIC
, cmd_ctx
->interp
);
1587 if (retval
!= ERROR_OK
)
1593 COMMAND_HANDLER(handle_target_init_command
)
1598 return ERROR_COMMAND_SYNTAX_ERROR
;
1600 static bool target_initialized
;
1601 if (target_initialized
) {
1602 LOG_INFO("'target init' has already been called");
1605 target_initialized
= true;
1607 retval
= command_run_line(CMD_CTX
, "init_targets");
1608 if (retval
!= ERROR_OK
)
1611 retval
= command_run_line(CMD_CTX
, "init_target_events");
1612 if (retval
!= ERROR_OK
)
1615 retval
= command_run_line(CMD_CTX
, "init_board");
1616 if (retval
!= ERROR_OK
)
1619 LOG_DEBUG("Initializing targets...");
1620 return target_init(CMD_CTX
);
1623 int target_register_event_callback(int (*callback
)(struct target
*target
,
1624 enum target_event event
, void *priv
), void *priv
)
1626 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1629 return ERROR_COMMAND_SYNTAX_ERROR
;
1632 while ((*callbacks_p
)->next
)
1633 callbacks_p
= &((*callbacks_p
)->next
);
1634 callbacks_p
= &((*callbacks_p
)->next
);
1637 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1638 (*callbacks_p
)->callback
= callback
;
1639 (*callbacks_p
)->priv
= priv
;
1640 (*callbacks_p
)->next
= NULL
;
1645 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1646 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1648 struct target_reset_callback
*entry
;
1651 return ERROR_COMMAND_SYNTAX_ERROR
;
1653 entry
= malloc(sizeof(struct target_reset_callback
));
1655 LOG_ERROR("error allocating buffer for reset callback entry");
1656 return ERROR_COMMAND_SYNTAX_ERROR
;
1659 entry
->callback
= callback
;
1661 list_add(&entry
->list
, &target_reset_callback_list
);
1667 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1668 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1670 struct target_trace_callback
*entry
;
1673 return ERROR_COMMAND_SYNTAX_ERROR
;
1675 entry
= malloc(sizeof(struct target_trace_callback
));
1677 LOG_ERROR("error allocating buffer for trace callback entry");
1678 return ERROR_COMMAND_SYNTAX_ERROR
;
1681 entry
->callback
= callback
;
1683 list_add(&entry
->list
, &target_trace_callback_list
);
1689 int target_register_timer_callback(int (*callback
)(void *priv
),
1690 unsigned int time_ms
, enum target_timer_type type
, void *priv
)
1692 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1695 return ERROR_COMMAND_SYNTAX_ERROR
;
1698 while ((*callbacks_p
)->next
)
1699 callbacks_p
= &((*callbacks_p
)->next
);
1700 callbacks_p
= &((*callbacks_p
)->next
);
1703 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1704 (*callbacks_p
)->callback
= callback
;
1705 (*callbacks_p
)->type
= type
;
1706 (*callbacks_p
)->time_ms
= time_ms
;
1707 (*callbacks_p
)->removed
= false;
1709 (*callbacks_p
)->when
= timeval_ms() + time_ms
;
1710 target_timer_next_event_value
= MIN(target_timer_next_event_value
, (*callbacks_p
)->when
);
1712 (*callbacks_p
)->priv
= priv
;
1713 (*callbacks_p
)->next
= NULL
;
1718 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1719 enum target_event event
, void *priv
), void *priv
)
1721 struct target_event_callback
**p
= &target_event_callbacks
;
1722 struct target_event_callback
*c
= target_event_callbacks
;
1725 return ERROR_COMMAND_SYNTAX_ERROR
;
1728 struct target_event_callback
*next
= c
->next
;
1729 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1741 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1742 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1744 struct target_reset_callback
*entry
;
1747 return ERROR_COMMAND_SYNTAX_ERROR
;
1749 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1750 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1751 list_del(&entry
->list
);
1760 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1761 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1763 struct target_trace_callback
*entry
;
1766 return ERROR_COMMAND_SYNTAX_ERROR
;
1768 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1769 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1770 list_del(&entry
->list
);
1779 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1782 return ERROR_COMMAND_SYNTAX_ERROR
;
1784 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1786 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1795 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1797 struct target_event_callback
*callback
= target_event_callbacks
;
1798 struct target_event_callback
*next_callback
;
1800 if (event
== TARGET_EVENT_HALTED
) {
1801 /* execute early halted first */
1802 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1805 LOG_DEBUG("target event %i (%s) for core %s", event
,
1806 target_event_name(event
),
1807 target_name(target
));
1809 target_handle_event(target
, event
);
1812 next_callback
= callback
->next
;
1813 callback
->callback(target
, event
, callback
->priv
);
1814 callback
= next_callback
;
1820 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1822 struct target_reset_callback
*callback
;
1824 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1825 nvp_value2name(nvp_reset_modes
, reset_mode
)->name
);
1827 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1828 callback
->callback(target
, reset_mode
, callback
->priv
);
1833 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1835 struct target_trace_callback
*callback
;
1837 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1838 callback
->callback(target
, len
, data
, callback
->priv
);
1843 static int target_timer_callback_periodic_restart(
1844 struct target_timer_callback
*cb
, int64_t *now
)
1846 cb
->when
= *now
+ cb
->time_ms
;
1850 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1853 cb
->callback(cb
->priv
);
1855 if (cb
->type
== TARGET_TIMER_TYPE_PERIODIC
)
1856 return target_timer_callback_periodic_restart(cb
, now
);
1858 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1861 static int target_call_timer_callbacks_check_time(int checktime
)
1863 static bool callback_processing
;
1865 /* Do not allow nesting */
1866 if (callback_processing
)
1869 callback_processing
= true;
1873 int64_t now
= timeval_ms();
1875 /* Initialize to a default value that's a ways into the future.
1876 * The loop below will make it closer to now if there are
1877 * callbacks that want to be called sooner. */
1878 target_timer_next_event_value
= now
+ 1000;
1880 /* Store an address of the place containing a pointer to the
1881 * next item; initially, that's a standalone "root of the
1882 * list" variable. */
1883 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1884 while (callback
&& *callback
) {
1885 if ((*callback
)->removed
) {
1886 struct target_timer_callback
*p
= *callback
;
1887 *callback
= (*callback
)->next
;
1892 bool call_it
= (*callback
)->callback
&&
1893 ((!checktime
&& (*callback
)->type
== TARGET_TIMER_TYPE_PERIODIC
) ||
1894 now
>= (*callback
)->when
);
1897 target_call_timer_callback(*callback
, &now
);
1899 if (!(*callback
)->removed
&& (*callback
)->when
< target_timer_next_event_value
)
1900 target_timer_next_event_value
= (*callback
)->when
;
1902 callback
= &(*callback
)->next
;
1905 callback_processing
= false;
1909 int target_call_timer_callbacks(void)
1911 return target_call_timer_callbacks_check_time(1);
1914 /* invoke periodic callbacks immediately */
1915 int target_call_timer_callbacks_now(void)
1917 return target_call_timer_callbacks_check_time(0);
1920 int64_t target_timer_next_event(void)
1922 return target_timer_next_event_value
;
1925 /* Prints the working area layout for debug purposes */
1926 static void print_wa_layout(struct target
*target
)
1928 struct working_area
*c
= target
->working_areas
;
1931 LOG_DEBUG("%c%c " TARGET_ADDR_FMT
"-" TARGET_ADDR_FMT
" (%" PRIu32
" bytes)",
1932 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1933 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1938 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1939 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1941 assert(area
->free
); /* Shouldn't split an allocated area */
1942 assert(size
<= area
->size
); /* Caller should guarantee this */
1944 /* Split only if not already the right size */
1945 if (size
< area
->size
) {
1946 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1951 new_wa
->next
= area
->next
;
1952 new_wa
->size
= area
->size
- size
;
1953 new_wa
->address
= area
->address
+ size
;
1954 new_wa
->backup
= NULL
;
1955 new_wa
->user
= NULL
;
1956 new_wa
->free
= true;
1958 area
->next
= new_wa
;
1961 /* If backup memory was allocated to this area, it has the wrong size
1962 * now so free it and it will be reallocated if/when needed */
1964 area
->backup
= NULL
;
1968 /* Merge all adjacent free areas into one */
1969 static void target_merge_working_areas(struct target
*target
)
1971 struct working_area
*c
= target
->working_areas
;
1973 while (c
&& c
->next
) {
1974 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1976 /* Find two adjacent free areas */
1977 if (c
->free
&& c
->next
->free
) {
1978 /* Merge the last into the first */
1979 c
->size
+= c
->next
->size
;
1981 /* Remove the last */
1982 struct working_area
*to_be_freed
= c
->next
;
1983 c
->next
= c
->next
->next
;
1984 free(to_be_freed
->backup
);
1987 /* If backup memory was allocated to the remaining area, it's has
1988 * the wrong size now */
1997 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1999 /* Reevaluate working area address based on MMU state*/
2000 if (!target
->working_areas
) {
2004 retval
= target
->type
->mmu(target
, &enabled
);
2005 if (retval
!= ERROR_OK
)
2009 if (target
->working_area_phys_spec
) {
2010 LOG_DEBUG("MMU disabled, using physical "
2011 "address for working memory " TARGET_ADDR_FMT
,
2012 target
->working_area_phys
);
2013 target
->working_area
= target
->working_area_phys
;
2015 LOG_ERROR("No working memory available. "
2016 "Specify -work-area-phys to target.");
2017 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2020 if (target
->working_area_virt_spec
) {
2021 LOG_DEBUG("MMU enabled, using virtual "
2022 "address for working memory " TARGET_ADDR_FMT
,
2023 target
->working_area_virt
);
2024 target
->working_area
= target
->working_area_virt
;
2026 LOG_ERROR("No working memory available. "
2027 "Specify -work-area-virt to target.");
2028 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2032 /* Set up initial working area on first call */
2033 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
2035 new_wa
->next
= NULL
;
2036 new_wa
->size
= ALIGN_DOWN(target
->working_area_size
, 4); /* 4-byte align */
2037 new_wa
->address
= target
->working_area
;
2038 new_wa
->backup
= NULL
;
2039 new_wa
->user
= NULL
;
2040 new_wa
->free
= true;
2043 target
->working_areas
= new_wa
;
2046 /* only allocate multiples of 4 byte */
2047 size
= ALIGN_UP(size
, 4);
2049 struct working_area
*c
= target
->working_areas
;
2051 /* Find the first large enough working area */
2053 if (c
->free
&& c
->size
>= size
)
2059 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2061 /* Split the working area into the requested size */
2062 target_split_working_area(c
, size
);
2064 LOG_DEBUG("allocated new working area of %" PRIu32
" bytes at address " TARGET_ADDR_FMT
,
2067 if (target
->backup_working_area
) {
2069 c
->backup
= malloc(c
->size
);
2074 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
2075 if (retval
!= ERROR_OK
)
2079 /* mark as used, and return the new (reused) area */
2086 print_wa_layout(target
);
2091 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
2095 retval
= target_alloc_working_area_try(target
, size
, area
);
2096 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
2097 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
2102 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
2104 int retval
= ERROR_OK
;
2106 if (target
->backup_working_area
&& area
->backup
) {
2107 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
2108 if (retval
!= ERROR_OK
)
2109 LOG_ERROR("failed to restore %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
2110 area
->size
, area
->address
);
2116 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
2117 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
2119 if (!area
|| area
->free
)
2122 int retval
= ERROR_OK
;
2124 retval
= target_restore_working_area(target
, area
);
2125 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
2126 if (retval
!= ERROR_OK
)
2132 LOG_DEBUG("freed %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
2133 area
->size
, area
->address
);
2135 /* mark user pointer invalid */
2136 /* TODO: Is this really safe? It points to some previous caller's memory.
2137 * How could we know that the area pointer is still in that place and not
2138 * some other vital data? What's the purpose of this, anyway? */
2142 target_merge_working_areas(target
);
2144 print_wa_layout(target
);
2149 int target_free_working_area(struct target
*target
, struct working_area
*area
)
2151 return target_free_working_area_restore(target
, area
, 1);
2154 /* free resources and restore memory, if restoring memory fails,
2155 * free up resources anyway
2157 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
2159 struct working_area
*c
= target
->working_areas
;
2161 LOG_DEBUG("freeing all working areas");
2163 /* Loop through all areas, restoring the allocated ones and marking them as free */
2167 target_restore_working_area(target
, c
);
2169 *c
->user
= NULL
; /* Same as above */
2175 /* Run a merge pass to combine all areas into one */
2176 target_merge_working_areas(target
);
2178 print_wa_layout(target
);
2181 void target_free_all_working_areas(struct target
*target
)
2183 target_free_all_working_areas_restore(target
, 1);
2185 /* Now we have none or only one working area marked as free */
2186 if (target
->working_areas
) {
2187 /* Free the last one to allow on-the-fly moving and resizing */
2188 free(target
->working_areas
->backup
);
2189 free(target
->working_areas
);
2190 target
->working_areas
= NULL
;
2194 /* Find the largest number of bytes that can be allocated */
2195 uint32_t target_get_working_area_avail(struct target
*target
)
2197 struct working_area
*c
= target
->working_areas
;
2198 uint32_t max_size
= 0;
2201 return ALIGN_DOWN(target
->working_area_size
, 4);
2204 if (c
->free
&& max_size
< c
->size
)
2213 static void target_destroy(struct target
*target
)
2215 if (target
->type
->deinit_target
)
2216 target
->type
->deinit_target(target
);
2218 if (target
->semihosting
)
2219 free(target
->semihosting
->basedir
);
2220 free(target
->semihosting
);
2222 jtag_unregister_event_callback(jtag_enable_callback
, target
);
2224 struct target_event_action
*teap
= target
->event_action
;
2226 struct target_event_action
*next
= teap
->next
;
2227 Jim_DecrRefCount(teap
->interp
, teap
->body
);
2232 target_free_all_working_areas(target
);
2234 /* release the targets SMP list */
2236 struct target_list
*head
, *tmp
;
2238 list_for_each_entry_safe(head
, tmp
, target
->smp_targets
, lh
) {
2239 list_del(&head
->lh
);
2240 head
->target
->smp
= 0;
2243 if (target
->smp_targets
!= &empty_smp_targets
)
2244 free(target
->smp_targets
);
2248 rtos_destroy(target
);
2250 free(target
->gdb_port_override
);
2252 free(target
->trace_info
);
2253 free(target
->fileio_info
);
2254 free(target
->cmd_name
);
2258 void target_quit(void)
2260 struct target_event_callback
*pe
= target_event_callbacks
;
2262 struct target_event_callback
*t
= pe
->next
;
2266 target_event_callbacks
= NULL
;
2268 struct target_timer_callback
*pt
= target_timer_callbacks
;
2270 struct target_timer_callback
*t
= pt
->next
;
2274 target_timer_callbacks
= NULL
;
2276 for (struct target
*target
= all_targets
; target
;) {
2280 target_destroy(target
);
2287 int target_arch_state(struct target
*target
)
2291 LOG_WARNING("No target has been configured");
2295 if (target
->state
!= TARGET_HALTED
)
2298 retval
= target
->type
->arch_state(target
);
2302 static int target_get_gdb_fileio_info_default(struct target
*target
,
2303 struct gdb_fileio_info
*fileio_info
)
2305 /* If target does not support semi-hosting function, target
2306 has no need to provide .get_gdb_fileio_info callback.
2307 It just return ERROR_FAIL and gdb_server will return "Txx"
2308 as target halted every time. */
2312 static int target_gdb_fileio_end_default(struct target
*target
,
2313 int retcode
, int fileio_errno
, bool ctrl_c
)
2318 int target_profiling_default(struct target
*target
, uint32_t *samples
,
2319 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
2321 struct timeval timeout
, now
;
2323 gettimeofday(&timeout
, NULL
);
2324 timeval_add_time(&timeout
, seconds
, 0);
2326 LOG_INFO("Starting profiling. Halting and resuming the"
2327 " target as often as we can...");
2329 uint32_t sample_count
= 0;
2330 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2331 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", true);
2333 int retval
= ERROR_OK
;
2335 target_poll(target
);
2336 if (target
->state
== TARGET_HALTED
) {
2337 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
2338 samples
[sample_count
++] = t
;
2339 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2340 retval
= target_resume(target
, 1, 0, 0, 0);
2341 target_poll(target
);
2342 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2343 } else if (target
->state
== TARGET_RUNNING
) {
2344 /* We want to quickly sample the PC. */
2345 retval
= target_halt(target
);
2347 LOG_INFO("Target not halted or running");
2352 if (retval
!= ERROR_OK
)
2355 gettimeofday(&now
, NULL
);
2356 if ((sample_count
>= max_num_samples
) || timeval_compare(&now
, &timeout
) >= 0) {
2357 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2362 *num_samples
= sample_count
;
2366 /* Single aligned words are guaranteed to use 16 or 32 bit access
2367 * mode respectively, otherwise data is handled as quickly as
2370 int target_write_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, const uint8_t *buffer
)
2372 LOG_DEBUG("writing buffer of %" PRIu32
" byte at " TARGET_ADDR_FMT
,
2375 if (!target_was_examined(target
)) {
2376 LOG_ERROR("Target not examined yet");
2383 if ((address
+ size
- 1) < address
) {
2384 /* GDB can request this when e.g. PC is 0xfffffffc */
2385 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2391 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2394 static int target_write_buffer_default(struct target
*target
,
2395 target_addr_t address
, uint32_t count
, const uint8_t *buffer
)
2398 unsigned int data_bytes
= target_data_bits(target
) / 8;
2400 /* Align up to maximum bytes. The loop condition makes sure the next pass
2401 * will have something to do with the size we leave to it. */
2403 size
< data_bytes
&& count
>= size
* 2 + (address
& size
);
2405 if (address
& size
) {
2406 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2407 if (retval
!= ERROR_OK
)
2415 /* Write the data with as large access size as possible. */
2416 for (; size
> 0; size
/= 2) {
2417 uint32_t aligned
= count
- count
% size
;
2419 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2420 if (retval
!= ERROR_OK
)
2431 /* Single aligned words are guaranteed to use 16 or 32 bit access
2432 * mode respectively, otherwise data is handled as quickly as
2435 int target_read_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
2437 LOG_DEBUG("reading buffer of %" PRIu32
" byte at " TARGET_ADDR_FMT
,
2440 if (!target_was_examined(target
)) {
2441 LOG_ERROR("Target not examined yet");
2448 if ((address
+ size
- 1) < address
) {
2449 /* GDB can request this when e.g. PC is 0xfffffffc */
2450 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2456 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2459 static int target_read_buffer_default(struct target
*target
, target_addr_t address
, uint32_t count
, uint8_t *buffer
)
2462 unsigned int data_bytes
= target_data_bits(target
) / 8;
2464 /* Align up to maximum bytes. The loop condition makes sure the next pass
2465 * will have something to do with the size we leave to it. */
2467 size
< data_bytes
&& count
>= size
* 2 + (address
& size
);
2469 if (address
& size
) {
2470 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2471 if (retval
!= ERROR_OK
)
2479 /* Read the data with as large access size as possible. */
2480 for (; size
> 0; size
/= 2) {
2481 uint32_t aligned
= count
- count
% size
;
2483 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2484 if (retval
!= ERROR_OK
)
2495 int target_checksum_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t *crc
)
2500 uint32_t checksum
= 0;
2501 if (!target_was_examined(target
)) {
2502 LOG_ERROR("Target not examined yet");
2505 if (!target
->type
->checksum_memory
) {
2506 LOG_ERROR("Target %s doesn't support checksum_memory", target_name(target
));
2510 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2511 if (retval
!= ERROR_OK
) {
2512 buffer
= malloc(size
);
2514 LOG_ERROR("error allocating buffer for section (%" PRIu32
" bytes)", size
);
2515 return ERROR_COMMAND_SYNTAX_ERROR
;
2517 retval
= target_read_buffer(target
, address
, size
, buffer
);
2518 if (retval
!= ERROR_OK
) {
2523 /* convert to target endianness */
2524 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2525 uint32_t target_data
;
2526 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2527 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2530 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2539 int target_blank_check_memory(struct target
*target
,
2540 struct target_memory_check_block
*blocks
, int num_blocks
,
2541 uint8_t erased_value
)
2543 if (!target_was_examined(target
)) {
2544 LOG_ERROR("Target not examined yet");
2548 if (!target
->type
->blank_check_memory
)
2549 return ERROR_NOT_IMPLEMENTED
;
2551 return target
->type
->blank_check_memory(target
, blocks
, num_blocks
, erased_value
);
2554 int target_read_u64(struct target
*target
, target_addr_t address
, uint64_t *value
)
2556 uint8_t value_buf
[8];
2557 if (!target_was_examined(target
)) {
2558 LOG_ERROR("Target not examined yet");
2562 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2564 if (retval
== ERROR_OK
) {
2565 *value
= target_buffer_get_u64(target
, value_buf
);
2566 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2571 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2578 int target_read_u32(struct target
*target
, target_addr_t address
, uint32_t *value
)
2580 uint8_t value_buf
[4];
2581 if (!target_was_examined(target
)) {
2582 LOG_ERROR("Target not examined yet");
2586 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2588 if (retval
== ERROR_OK
) {
2589 *value
= target_buffer_get_u32(target
, value_buf
);
2590 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2595 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2602 int target_read_u16(struct target
*target
, target_addr_t address
, uint16_t *value
)
2604 uint8_t value_buf
[2];
2605 if (!target_was_examined(target
)) {
2606 LOG_ERROR("Target not examined yet");
2610 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2612 if (retval
== ERROR_OK
) {
2613 *value
= target_buffer_get_u16(target
, value_buf
);
2614 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%4.4" PRIx16
,
2619 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2626 int target_read_u8(struct target
*target
, target_addr_t address
, uint8_t *value
)
2628 if (!target_was_examined(target
)) {
2629 LOG_ERROR("Target not examined yet");
2633 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2635 if (retval
== ERROR_OK
) {
2636 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2641 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2648 int target_write_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2651 uint8_t value_buf
[8];
2652 if (!target_was_examined(target
)) {
2653 LOG_ERROR("Target not examined yet");
2657 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2661 target_buffer_set_u64(target
, value_buf
, value
);
2662 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2663 if (retval
!= ERROR_OK
)
2664 LOG_DEBUG("failed: %i", retval
);
2669 int target_write_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2672 uint8_t value_buf
[4];
2673 if (!target_was_examined(target
)) {
2674 LOG_ERROR("Target not examined yet");
2678 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2682 target_buffer_set_u32(target
, value_buf
, value
);
2683 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2684 if (retval
!= ERROR_OK
)
2685 LOG_DEBUG("failed: %i", retval
);
2690 int target_write_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2693 uint8_t value_buf
[2];
2694 if (!target_was_examined(target
)) {
2695 LOG_ERROR("Target not examined yet");
2699 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2703 target_buffer_set_u16(target
, value_buf
, value
);
2704 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2705 if (retval
!= ERROR_OK
)
2706 LOG_DEBUG("failed: %i", retval
);
2711 int target_write_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2714 if (!target_was_examined(target
)) {
2715 LOG_ERROR("Target not examined yet");
2719 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2722 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2723 if (retval
!= ERROR_OK
)
2724 LOG_DEBUG("failed: %i", retval
);
2729 int target_write_phys_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2732 uint8_t value_buf
[8];
2733 if (!target_was_examined(target
)) {
2734 LOG_ERROR("Target not examined yet");
2738 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2742 target_buffer_set_u64(target
, value_buf
, value
);
2743 retval
= target_write_phys_memory(target
, address
, 8, 1, value_buf
);
2744 if (retval
!= ERROR_OK
)
2745 LOG_DEBUG("failed: %i", retval
);
2750 int target_write_phys_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2753 uint8_t value_buf
[4];
2754 if (!target_was_examined(target
)) {
2755 LOG_ERROR("Target not examined yet");
2759 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2763 target_buffer_set_u32(target
, value_buf
, value
);
2764 retval
= target_write_phys_memory(target
, address
, 4, 1, value_buf
);
2765 if (retval
!= ERROR_OK
)
2766 LOG_DEBUG("failed: %i", retval
);
2771 int target_write_phys_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2774 uint8_t value_buf
[2];
2775 if (!target_was_examined(target
)) {
2776 LOG_ERROR("Target not examined yet");
2780 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2784 target_buffer_set_u16(target
, value_buf
, value
);
2785 retval
= target_write_phys_memory(target
, address
, 2, 1, value_buf
);
2786 if (retval
!= ERROR_OK
)
2787 LOG_DEBUG("failed: %i", retval
);
2792 int target_write_phys_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2795 if (!target_was_examined(target
)) {
2796 LOG_ERROR("Target not examined yet");
2800 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2803 retval
= target_write_phys_memory(target
, address
, 1, 1, &value
);
2804 if (retval
!= ERROR_OK
)
2805 LOG_DEBUG("failed: %i", retval
);
2810 static int find_target(struct command_invocation
*cmd
, const char *name
)
2812 struct target
*target
= get_target(name
);
2814 command_print(cmd
, "Target: %s is unknown, try one of:\n", name
);
2817 if (!target
->tap
->enabled
) {
2818 command_print(cmd
, "Target: TAP %s is disabled, "
2819 "can't be the current target\n",
2820 target
->tap
->dotted_name
);
2824 cmd
->ctx
->current_target
= target
;
2825 if (cmd
->ctx
->current_target_override
)
2826 cmd
->ctx
->current_target_override
= target
;
2832 COMMAND_HANDLER(handle_targets_command
)
2834 int retval
= ERROR_OK
;
2835 if (CMD_ARGC
== 1) {
2836 retval
= find_target(CMD
, CMD_ARGV
[0]);
2837 if (retval
== ERROR_OK
) {
2843 struct target
*target
= all_targets
;
2844 command_print(CMD
, " TargetName Type Endian TapName State ");
2845 command_print(CMD
, "-- ------------------ ---------- ------ ------------------ ------------");
2850 if (target
->tap
->enabled
)
2851 state
= target_state_name(target
);
2853 state
= "tap-disabled";
2855 if (CMD_CTX
->current_target
== target
)
2858 /* keep columns lined up to match the headers above */
2860 "%2d%c %-18s %-10s %-6s %-18s %s",
2861 target
->target_number
,
2863 target_name(target
),
2864 target_type_name(target
),
2865 jim_nvp_value2name_simple(nvp_target_endian
,
2866 target
->endianness
)->name
,
2867 target
->tap
->dotted_name
,
2869 target
= target
->next
;
2875 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2877 static int power_dropout
;
2878 static int srst_asserted
;
2880 static int run_power_restore
;
2881 static int run_power_dropout
;
2882 static int run_srst_asserted
;
2883 static int run_srst_deasserted
;
2885 static int sense_handler(void)
2887 static int prev_srst_asserted
;
2888 static int prev_power_dropout
;
2890 int retval
= jtag_power_dropout(&power_dropout
);
2891 if (retval
!= ERROR_OK
)
2895 power_restored
= prev_power_dropout
&& !power_dropout
;
2897 run_power_restore
= 1;
2899 int64_t current
= timeval_ms();
2900 static int64_t last_power
;
2901 bool wait_more
= last_power
+ 2000 > current
;
2902 if (power_dropout
&& !wait_more
) {
2903 run_power_dropout
= 1;
2904 last_power
= current
;
2907 retval
= jtag_srst_asserted(&srst_asserted
);
2908 if (retval
!= ERROR_OK
)
2911 int srst_deasserted
;
2912 srst_deasserted
= prev_srst_asserted
&& !srst_asserted
;
2914 static int64_t last_srst
;
2915 wait_more
= last_srst
+ 2000 > current
;
2916 if (srst_deasserted
&& !wait_more
) {
2917 run_srst_deasserted
= 1;
2918 last_srst
= current
;
2921 if (!prev_srst_asserted
&& srst_asserted
)
2922 run_srst_asserted
= 1;
2924 prev_srst_asserted
= srst_asserted
;
2925 prev_power_dropout
= power_dropout
;
2927 if (srst_deasserted
|| power_restored
) {
2928 /* Other than logging the event we can't do anything here.
2929 * Issuing a reset is a particularly bad idea as we might
2930 * be inside a reset already.
2937 /* process target state changes */
2938 static int handle_target(void *priv
)
2940 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2941 int retval
= ERROR_OK
;
2943 if (!is_jtag_poll_safe()) {
2944 /* polling is disabled currently */
2948 /* we do not want to recurse here... */
2949 static int recursive
;
2953 /* danger! running these procedures can trigger srst assertions and power dropouts.
2954 * We need to avoid an infinite loop/recursion here and we do that by
2955 * clearing the flags after running these events.
2957 int did_something
= 0;
2958 if (run_srst_asserted
) {
2959 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2960 Jim_Eval(interp
, "srst_asserted");
2963 if (run_srst_deasserted
) {
2964 Jim_Eval(interp
, "srst_deasserted");
2967 if (run_power_dropout
) {
2968 LOG_INFO("Power dropout detected, running power_dropout proc.");
2969 Jim_Eval(interp
, "power_dropout");
2972 if (run_power_restore
) {
2973 Jim_Eval(interp
, "power_restore");
2977 if (did_something
) {
2978 /* clear detect flags */
2982 /* clear action flags */
2984 run_srst_asserted
= 0;
2985 run_srst_deasserted
= 0;
2986 run_power_restore
= 0;
2987 run_power_dropout
= 0;
2992 /* Poll targets for state changes unless that's globally disabled.
2993 * Skip targets that are currently disabled.
2995 for (struct target
*target
= all_targets
;
2996 is_jtag_poll_safe() && target
;
2997 target
= target
->next
) {
2999 if (!target_was_examined(target
))
3002 if (!target
->tap
->enabled
)
3005 if (target
->backoff
.times
> target
->backoff
.count
) {
3006 /* do not poll this time as we failed previously */
3007 target
->backoff
.count
++;
3010 target
->backoff
.count
= 0;
3012 /* only poll target if we've got power and srst isn't asserted */
3013 if (!power_dropout
&& !srst_asserted
) {
3014 /* polling may fail silently until the target has been examined */
3015 retval
= target_poll(target
);
3016 if (retval
!= ERROR_OK
) {
3017 /* 100ms polling interval. Increase interval between polling up to 5000ms */
3018 if (target
->backoff
.times
* polling_interval
< 5000) {
3019 target
->backoff
.times
*= 2;
3020 target
->backoff
.times
++;
3023 /* Tell GDB to halt the debugger. This allows the user to
3024 * run monitor commands to handle the situation.
3026 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
3028 if (target
->backoff
.times
> 0) {
3029 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
3030 target_reset_examined(target
);
3031 retval
= target_examine_one(target
);
3032 /* Target examination could have failed due to unstable connection,
3033 * but we set the examined flag anyway to repoll it later */
3034 if (retval
!= ERROR_OK
) {
3035 target_set_examined(target
);
3036 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
3037 target
->backoff
.times
* polling_interval
);
3042 /* Since we succeeded, we reset backoff count */
3043 target
->backoff
.times
= 0;
3050 COMMAND_HANDLER(handle_reg_command
)
3054 struct target
*target
= get_current_target(CMD_CTX
);
3055 if (!target_was_examined(target
)) {
3056 LOG_ERROR("Target not examined yet");
3057 return ERROR_TARGET_NOT_EXAMINED
;
3059 struct reg
*reg
= NULL
;
3061 /* list all available registers for the current target */
3062 if (CMD_ARGC
== 0) {
3063 struct reg_cache
*cache
= target
->reg_cache
;
3065 unsigned int count
= 0;
3069 command_print(CMD
, "===== %s", cache
->name
);
3071 for (i
= 0, reg
= cache
->reg_list
;
3072 i
< cache
->num_regs
;
3073 i
++, reg
++, count
++) {
3074 if (reg
->exist
== false || reg
->hidden
)
3076 /* only print cached values if they are valid */
3078 char *value
= buf_to_hex_str(reg
->value
,
3081 "(%i) %s (/%" PRIu32
"): 0x%s%s",
3089 command_print(CMD
, "(%i) %s (/%" PRIu32
")",
3094 cache
= cache
->next
;
3100 /* access a single register by its ordinal number */
3101 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
3103 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
3105 struct reg_cache
*cache
= target
->reg_cache
;
3106 unsigned int count
= 0;
3109 for (i
= 0; i
< cache
->num_regs
; i
++) {
3110 if (count
++ == num
) {
3111 reg
= &cache
->reg_list
[i
];
3117 cache
= cache
->next
;
3121 command_print(CMD
, "%i is out of bounds, the current target "
3122 "has only %i registers (0 - %i)", num
, count
, count
- 1);
3126 /* access a single register by its name */
3127 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], true);
3133 assert(reg
); /* give clang a hint that we *know* reg is != NULL here */
3138 /* display a register */
3139 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
3140 && (CMD_ARGV
[1][0] <= '9')))) {
3141 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
3145 int retval
= reg
->type
->get(reg
);
3146 if (retval
!= ERROR_OK
) {
3147 LOG_ERROR("Could not read register '%s'", reg
->name
);
3151 char *value
= buf_to_hex_str(reg
->value
, reg
->size
);
3152 command_print(CMD
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
3157 /* set register value */
3158 if (CMD_ARGC
== 2) {
3159 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
3162 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
3164 int retval
= reg
->type
->set(reg
, buf
);
3165 if (retval
!= ERROR_OK
) {
3166 LOG_ERROR("Could not write to register '%s'", reg
->name
);
3168 char *value
= buf_to_hex_str(reg
->value
, reg
->size
);
3169 command_print(CMD
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
3178 return ERROR_COMMAND_SYNTAX_ERROR
;
3181 command_print(CMD
, "register %s not found in current target", CMD_ARGV
[0]);
3185 COMMAND_HANDLER(handle_poll_command
)
3187 int retval
= ERROR_OK
;
3188 struct target
*target
= get_current_target(CMD_CTX
);
3190 if (CMD_ARGC
== 0) {
3191 command_print(CMD
, "background polling: %s",
3192 jtag_poll_get_enabled() ? "on" : "off");
3193 command_print(CMD
, "TAP: %s (%s)",
3194 target
->tap
->dotted_name
,
3195 target
->tap
->enabled
? "enabled" : "disabled");
3196 if (!target
->tap
->enabled
)
3198 retval
= target_poll(target
);
3199 if (retval
!= ERROR_OK
)
3201 retval
= target_arch_state(target
);
3202 if (retval
!= ERROR_OK
)
3204 } else if (CMD_ARGC
== 1) {
3206 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
3207 jtag_poll_set_enabled(enable
);
3209 return ERROR_COMMAND_SYNTAX_ERROR
;
3214 COMMAND_HANDLER(handle_wait_halt_command
)
3217 return ERROR_COMMAND_SYNTAX_ERROR
;
3219 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
3220 if (1 == CMD_ARGC
) {
3221 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
3222 if (retval
!= ERROR_OK
)
3223 return ERROR_COMMAND_SYNTAX_ERROR
;
3226 struct target
*target
= get_current_target(CMD_CTX
);
3227 return target_wait_state(target
, TARGET_HALTED
, ms
);
3230 /* wait for target state to change. The trick here is to have a low
3231 * latency for short waits and not to suck up all the CPU time
3234 * After 500ms, keep_alive() is invoked
3236 int target_wait_state(struct target
*target
, enum target_state state
, unsigned int ms
)
3239 int64_t then
= 0, cur
;
3243 retval
= target_poll(target
);
3244 if (retval
!= ERROR_OK
)
3246 if (target
->state
== state
)
3251 then
= timeval_ms();
3252 LOG_DEBUG("waiting for target %s...",
3253 nvp_value2name(nvp_target_state
, state
)->name
);
3259 if ((cur
-then
) > ms
) {
3260 LOG_ERROR("timed out while waiting for target %s",
3261 nvp_value2name(nvp_target_state
, state
)->name
);
3269 COMMAND_HANDLER(handle_halt_command
)
3273 struct target
*target
= get_current_target(CMD_CTX
);
3275 target
->verbose_halt_msg
= true;
3277 int retval
= target_halt(target
);
3278 if (retval
!= ERROR_OK
)
3281 if (CMD_ARGC
== 1) {
3282 unsigned wait_local
;
3283 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
3284 if (retval
!= ERROR_OK
)
3285 return ERROR_COMMAND_SYNTAX_ERROR
;
3290 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
3293 COMMAND_HANDLER(handle_soft_reset_halt_command
)
3295 struct target
*target
= get_current_target(CMD_CTX
);
3297 LOG_TARGET_INFO(target
, "requesting target halt and executing a soft reset");
3299 target_soft_reset_halt(target
);
3304 COMMAND_HANDLER(handle_reset_command
)
3307 return ERROR_COMMAND_SYNTAX_ERROR
;
3309 enum target_reset_mode reset_mode
= RESET_RUN
;
3310 if (CMD_ARGC
== 1) {
3311 const struct nvp
*n
;
3312 n
= nvp_name2value(nvp_reset_modes
, CMD_ARGV
[0]);
3313 if ((!n
->name
) || (n
->value
== RESET_UNKNOWN
))
3314 return ERROR_COMMAND_SYNTAX_ERROR
;
3315 reset_mode
= n
->value
;
3318 /* reset *all* targets */
3319 return target_process_reset(CMD
, reset_mode
);
3323 COMMAND_HANDLER(handle_resume_command
)
3327 return ERROR_COMMAND_SYNTAX_ERROR
;
3329 struct target
*target
= get_current_target(CMD_CTX
);
3331 /* with no CMD_ARGV, resume from current pc, addr = 0,
3332 * with one arguments, addr = CMD_ARGV[0],
3333 * handle breakpoints, not debugging */
3334 target_addr_t addr
= 0;
3335 if (CMD_ARGC
== 1) {
3336 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3340 return target_resume(target
, current
, addr
, 1, 0);
3343 COMMAND_HANDLER(handle_step_command
)
3346 return ERROR_COMMAND_SYNTAX_ERROR
;
3350 /* with no CMD_ARGV, step from current pc, addr = 0,
3351 * with one argument addr = CMD_ARGV[0],
3352 * handle breakpoints, debugging */
3353 target_addr_t addr
= 0;
3355 if (CMD_ARGC
== 1) {
3356 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3360 struct target
*target
= get_current_target(CMD_CTX
);
3362 return target_step(target
, current_pc
, addr
, 1);
3365 void target_handle_md_output(struct command_invocation
*cmd
,
3366 struct target
*target
, target_addr_t address
, unsigned size
,
3367 unsigned count
, const uint8_t *buffer
)
3369 const unsigned line_bytecnt
= 32;
3370 unsigned line_modulo
= line_bytecnt
/ size
;
3372 char output
[line_bytecnt
* 4 + 1];
3373 unsigned output_len
= 0;
3375 const char *value_fmt
;
3378 value_fmt
= "%16.16"PRIx64
" ";
3381 value_fmt
= "%8.8"PRIx64
" ";
3384 value_fmt
= "%4.4"PRIx64
" ";
3387 value_fmt
= "%2.2"PRIx64
" ";
3390 /* "can't happen", caller checked */
3391 LOG_ERROR("invalid memory read size: %u", size
);
3395 for (unsigned i
= 0; i
< count
; i
++) {
3396 if (i
% line_modulo
== 0) {
3397 output_len
+= snprintf(output
+ output_len
,
3398 sizeof(output
) - output_len
,
3399 TARGET_ADDR_FMT
": ",
3400 (address
+ (i
* size
)));
3404 const uint8_t *value_ptr
= buffer
+ i
* size
;
3407 value
= target_buffer_get_u64(target
, value_ptr
);
3410 value
= target_buffer_get_u32(target
, value_ptr
);
3413 value
= target_buffer_get_u16(target
, value_ptr
);
3418 output_len
+= snprintf(output
+ output_len
,
3419 sizeof(output
) - output_len
,
3422 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
3423 command_print(cmd
, "%s", output
);
3429 COMMAND_HANDLER(handle_md_command
)
3432 return ERROR_COMMAND_SYNTAX_ERROR
;
3435 switch (CMD_NAME
[2]) {
3449 return ERROR_COMMAND_SYNTAX_ERROR
;
3452 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3453 int (*fn
)(struct target
*target
,
3454 target_addr_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
3458 fn
= target_read_phys_memory
;
3460 fn
= target_read_memory
;
3461 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
3462 return ERROR_COMMAND_SYNTAX_ERROR
;
3464 target_addr_t address
;
3465 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3469 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
3471 uint8_t *buffer
= calloc(count
, size
);
3473 LOG_ERROR("Failed to allocate md read buffer");
3477 struct target
*target
= get_current_target(CMD_CTX
);
3478 int retval
= fn(target
, address
, size
, count
, buffer
);
3479 if (retval
== ERROR_OK
)
3480 target_handle_md_output(CMD
, target
, address
, size
, count
, buffer
);
3487 typedef int (*target_write_fn
)(struct target
*target
,
3488 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
3490 static int target_fill_mem(struct target
*target
,
3491 target_addr_t address
,
3499 /* We have to write in reasonably large chunks to be able
3500 * to fill large memory areas with any sane speed */
3501 const unsigned chunk_size
= 16384;
3502 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
3504 LOG_ERROR("Out of memory");
3508 for (unsigned i
= 0; i
< chunk_size
; i
++) {
3509 switch (data_size
) {
3511 target_buffer_set_u64(target
, target_buf
+ i
* data_size
, b
);
3514 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
3517 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
3520 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
3527 int retval
= ERROR_OK
;
3529 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
3532 if (current
> chunk_size
)
3533 current
= chunk_size
;
3534 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
3535 if (retval
!= ERROR_OK
)
3537 /* avoid GDB timeouts */
3546 COMMAND_HANDLER(handle_mw_command
)
3549 return ERROR_COMMAND_SYNTAX_ERROR
;
3550 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3555 fn
= target_write_phys_memory
;
3557 fn
= target_write_memory
;
3558 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
3559 return ERROR_COMMAND_SYNTAX_ERROR
;
3561 target_addr_t address
;
3562 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3565 COMMAND_PARSE_NUMBER(u64
, CMD_ARGV
[1], value
);
3569 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
3571 struct target
*target
= get_current_target(CMD_CTX
);
3573 switch (CMD_NAME
[2]) {
3587 return ERROR_COMMAND_SYNTAX_ERROR
;
3590 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
3593 static COMMAND_HELPER(parse_load_image_command
, struct image
*image
,
3594 target_addr_t
*min_address
, target_addr_t
*max_address
)
3596 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
3597 return ERROR_COMMAND_SYNTAX_ERROR
;
3599 /* a base address isn't always necessary,
3600 * default to 0x0 (i.e. don't relocate) */
3601 if (CMD_ARGC
>= 2) {
3603 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3604 image
->base_address
= addr
;
3605 image
->base_address_set
= true;
3607 image
->base_address_set
= false;
3609 image
->start_address_set
= false;
3612 COMMAND_PARSE_ADDRESS(CMD_ARGV
[3], *min_address
);
3613 if (CMD_ARGC
== 5) {
3614 COMMAND_PARSE_ADDRESS(CMD_ARGV
[4], *max_address
);
3615 /* use size (given) to find max (required) */
3616 *max_address
+= *min_address
;
3619 if (*min_address
> *max_address
)
3620 return ERROR_COMMAND_SYNTAX_ERROR
;
3625 COMMAND_HANDLER(handle_load_image_command
)
3629 uint32_t image_size
;
3630 target_addr_t min_address
= 0;
3631 target_addr_t max_address
= -1;
3634 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command
,
3635 &image
, &min_address
, &max_address
);
3636 if (retval
!= ERROR_OK
)
3639 struct target
*target
= get_current_target(CMD_CTX
);
3641 struct duration bench
;
3642 duration_start(&bench
);
3644 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
3649 for (unsigned int i
= 0; i
< image
.num_sections
; i
++) {
3650 buffer
= malloc(image
.sections
[i
].size
);
3653 "error allocating buffer for section (%d bytes)",
3654 (int)(image
.sections
[i
].size
));
3655 retval
= ERROR_FAIL
;
3659 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3660 if (retval
!= ERROR_OK
) {
3665 uint32_t offset
= 0;
3666 uint32_t length
= buf_cnt
;
3668 /* DANGER!!! beware of unsigned comparison here!!! */
3670 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3671 (image
.sections
[i
].base_address
< max_address
)) {
3673 if (image
.sections
[i
].base_address
< min_address
) {
3674 /* clip addresses below */
3675 offset
+= min_address
-image
.sections
[i
].base_address
;
3679 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3680 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3682 retval
= target_write_buffer(target
,
3683 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3684 if (retval
!= ERROR_OK
) {
3688 image_size
+= length
;
3689 command_print(CMD
, "%u bytes written at address " TARGET_ADDR_FMT
"",
3690 (unsigned int)length
,
3691 image
.sections
[i
].base_address
+ offset
);
3697 if ((retval
== ERROR_OK
) && (duration_measure(&bench
) == ERROR_OK
)) {
3698 command_print(CMD
, "downloaded %" PRIu32
" bytes "
3699 "in %fs (%0.3f KiB/s)", image_size
,
3700 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3703 image_close(&image
);
3709 COMMAND_HANDLER(handle_dump_image_command
)
3711 struct fileio
*fileio
;
3713 int retval
, retvaltemp
;
3714 target_addr_t address
, size
;
3715 struct duration bench
;
3716 struct target
*target
= get_current_target(CMD_CTX
);
3719 return ERROR_COMMAND_SYNTAX_ERROR
;
3721 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], address
);
3722 COMMAND_PARSE_ADDRESS(CMD_ARGV
[2], size
);
3724 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
3725 buffer
= malloc(buf_size
);
3729 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3730 if (retval
!= ERROR_OK
) {
3735 duration_start(&bench
);
3738 size_t size_written
;
3739 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
3740 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
3741 if (retval
!= ERROR_OK
)
3744 retval
= fileio_write(fileio
, this_run_size
, buffer
, &size_written
);
3745 if (retval
!= ERROR_OK
)
3748 size
-= this_run_size
;
3749 address
+= this_run_size
;
3754 if ((retval
== ERROR_OK
) && (duration_measure(&bench
) == ERROR_OK
)) {
3756 retval
= fileio_size(fileio
, &filesize
);
3757 if (retval
!= ERROR_OK
)
3760 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize
,
3761 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3764 retvaltemp
= fileio_close(fileio
);
3765 if (retvaltemp
!= ERROR_OK
)
3774 IMAGE_CHECKSUM_ONLY
= 2
3777 static COMMAND_HELPER(handle_verify_image_command_internal
, enum verify_mode verify
)
3781 uint32_t image_size
;
3783 uint32_t checksum
= 0;
3784 uint32_t mem_checksum
= 0;
3788 struct target
*target
= get_current_target(CMD_CTX
);
3791 return ERROR_COMMAND_SYNTAX_ERROR
;
3794 LOG_ERROR("no target selected");
3798 struct duration bench
;
3799 duration_start(&bench
);
3801 if (CMD_ARGC
>= 2) {
3803 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3804 image
.base_address
= addr
;
3805 image
.base_address_set
= true;
3807 image
.base_address_set
= false;
3808 image
.base_address
= 0x0;
3811 image
.start_address_set
= false;
3813 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3814 if (retval
!= ERROR_OK
)
3820 for (unsigned int i
= 0; i
< image
.num_sections
; i
++) {
3821 buffer
= malloc(image
.sections
[i
].size
);
3824 "error allocating buffer for section (%" PRIu32
" bytes)",
3825 image
.sections
[i
].size
);
3828 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3829 if (retval
!= ERROR_OK
) {
3834 if (verify
>= IMAGE_VERIFY
) {
3835 /* calculate checksum of image */
3836 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3837 if (retval
!= ERROR_OK
) {
3842 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3843 if (retval
!= ERROR_OK
) {
3847 if ((checksum
!= mem_checksum
) && (verify
== IMAGE_CHECKSUM_ONLY
)) {
3848 LOG_ERROR("checksum mismatch");
3850 retval
= ERROR_FAIL
;
3853 if (checksum
!= mem_checksum
) {
3854 /* failed crc checksum, fall back to a binary compare */
3858 LOG_ERROR("checksum mismatch - attempting binary compare");
3860 data
= malloc(buf_cnt
);
3862 retval
= target_read_buffer(target
, image
.sections
[i
].base_address
, buf_cnt
, data
);
3863 if (retval
== ERROR_OK
) {
3865 for (t
= 0; t
< buf_cnt
; t
++) {
3866 if (data
[t
] != buffer
[t
]) {
3868 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3870 (unsigned)(t
+ image
.sections
[i
].base_address
),
3873 if (diffs
++ >= 127) {
3874 command_print(CMD
, "More than 128 errors, the rest are not printed.");
3886 command_print(CMD
, "address " TARGET_ADDR_FMT
" length 0x%08zx",
3887 image
.sections
[i
].base_address
,
3892 image_size
+= buf_cnt
;
3895 command_print(CMD
, "No more differences found.");
3898 retval
= ERROR_FAIL
;
3899 if ((retval
== ERROR_OK
) && (duration_measure(&bench
) == ERROR_OK
)) {
3900 command_print(CMD
, "verified %" PRIu32
" bytes "
3901 "in %fs (%0.3f KiB/s)", image_size
,
3902 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3905 image_close(&image
);
3910 COMMAND_HANDLER(handle_verify_image_checksum_command
)
3912 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_CHECKSUM_ONLY
);
3915 COMMAND_HANDLER(handle_verify_image_command
)
3917 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_VERIFY
);
3920 COMMAND_HANDLER(handle_test_image_command
)
3922 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_TEST
);
3925 static int handle_bp_command_list(struct command_invocation
*cmd
)
3927 struct target
*target
= get_current_target(cmd
->ctx
);
3928 struct breakpoint
*breakpoint
= target
->breakpoints
;
3929 while (breakpoint
) {
3930 if (breakpoint
->type
== BKPT_SOFT
) {
3931 char *buf
= buf_to_hex_str(breakpoint
->orig_instr
,
3932 breakpoint
->length
);
3933 command_print(cmd
, "Software breakpoint(IVA): addr=" TARGET_ADDR_FMT
", len=0x%x, orig_instr=0x%s",
3934 breakpoint
->address
,
3939 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3940 command_print(cmd
, "Context breakpoint: asid=0x%8.8" PRIx32
", len=0x%x, num=%u",
3942 breakpoint
->length
, breakpoint
->number
);
3943 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3944 command_print(cmd
, "Hybrid breakpoint(IVA): addr=" TARGET_ADDR_FMT
", len=0x%x, num=%u",
3945 breakpoint
->address
,
3946 breakpoint
->length
, breakpoint
->number
);
3947 command_print(cmd
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3950 command_print(cmd
, "Hardware breakpoint(IVA): addr=" TARGET_ADDR_FMT
", len=0x%x, num=%u",
3951 breakpoint
->address
,
3952 breakpoint
->length
, breakpoint
->number
);
3955 breakpoint
= breakpoint
->next
;
3960 static int handle_bp_command_set(struct command_invocation
*cmd
,
3961 target_addr_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3963 struct target
*target
= get_current_target(cmd
->ctx
);
3967 retval
= breakpoint_add(target
, addr
, length
, hw
);
3968 /* error is always logged in breakpoint_add(), do not print it again */
3969 if (retval
== ERROR_OK
)
3970 command_print(cmd
, "breakpoint set at " TARGET_ADDR_FMT
"", addr
);
3972 } else if (addr
== 0) {
3973 if (!target
->type
->add_context_breakpoint
) {
3974 LOG_ERROR("Context breakpoint not available");
3975 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
3977 retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3978 /* error is always logged in context_breakpoint_add(), do not print it again */
3979 if (retval
== ERROR_OK
)
3980 command_print(cmd
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3983 if (!target
->type
->add_hybrid_breakpoint
) {
3984 LOG_ERROR("Hybrid breakpoint not available");
3985 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
3987 retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3988 /* error is always logged in hybrid_breakpoint_add(), do not print it again */
3989 if (retval
== ERROR_OK
)
3990 command_print(cmd
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3995 COMMAND_HANDLER(handle_bp_command
)
4004 return handle_bp_command_list(CMD
);
4008 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4009 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
4010 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
4013 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
4015 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4016 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
4018 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
4019 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
4021 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
4022 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
4024 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
4029 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4030 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
4031 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
4032 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
4035 return ERROR_COMMAND_SYNTAX_ERROR
;
4039 COMMAND_HANDLER(handle_rbp_command
)
4044 return ERROR_COMMAND_SYNTAX_ERROR
;
4046 struct target
*target
= get_current_target(CMD_CTX
);
4048 if (!strcmp(CMD_ARGV
[0], "all")) {
4049 retval
= breakpoint_remove_all(target
);
4051 if (retval
!= ERROR_OK
) {
4052 command_print(CMD
, "Error encountered during removal of all breakpoints.");
4053 command_print(CMD
, "Some breakpoints may have remained set.");
4057 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4059 retval
= breakpoint_remove(target
, addr
);
4061 if (retval
!= ERROR_OK
)
4062 command_print(CMD
, "Error during removal of breakpoint at address " TARGET_ADDR_FMT
, addr
);
4068 COMMAND_HANDLER(handle_wp_command
)
4070 struct target
*target
= get_current_target(CMD_CTX
);
4072 if (CMD_ARGC
== 0) {
4073 struct watchpoint
*watchpoint
= target
->watchpoints
;
4075 while (watchpoint
) {
4076 char wp_type
= (watchpoint
->rw
== WPT_READ
? 'r' : (watchpoint
->rw
== WPT_WRITE
? 'w' : 'a'));
4077 command_print(CMD
, "address: " TARGET_ADDR_FMT
4078 ", len: 0x%8.8" PRIx32
4079 ", r/w/a: %c, value: 0x%8.8" PRIx64
4080 ", mask: 0x%8.8" PRIx64
,
4081 watchpoint
->address
,
4086 watchpoint
= watchpoint
->next
;
4091 enum watchpoint_rw type
= WPT_ACCESS
;
4092 target_addr_t addr
= 0;
4093 uint32_t length
= 0;
4094 uint64_t data_value
= 0x0;
4095 uint64_t data_mask
= WATCHPOINT_IGNORE_DATA_VALUE_MASK
;
4096 bool mask_specified
= false;
4100 COMMAND_PARSE_NUMBER(u64
, CMD_ARGV
[4], data_mask
);
4101 mask_specified
= true;
4104 COMMAND_PARSE_NUMBER(u64
, CMD_ARGV
[3], data_value
);
4105 // if user specified only data value without mask - the mask should be 0
4106 if (!mask_specified
)
4110 switch (CMD_ARGV
[2][0]) {
4121 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
4122 return ERROR_COMMAND_SYNTAX_ERROR
;
4126 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
4127 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4131 return ERROR_COMMAND_SYNTAX_ERROR
;
4134 int retval
= watchpoint_add(target
, addr
, length
, type
,
4135 data_value
, data_mask
);
4136 if (retval
!= ERROR_OK
)
4137 LOG_ERROR("Failure setting watchpoints");
4142 COMMAND_HANDLER(handle_rwp_command
)
4147 return ERROR_COMMAND_SYNTAX_ERROR
;
4149 struct target
*target
= get_current_target(CMD_CTX
);
4150 if (!strcmp(CMD_ARGV
[0], "all")) {
4151 retval
= watchpoint_remove_all(target
);
4153 if (retval
!= ERROR_OK
) {
4154 command_print(CMD
, "Error encountered during removal of all watchpoints.");
4155 command_print(CMD
, "Some watchpoints may have remained set.");
4159 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4161 retval
= watchpoint_remove(target
, addr
);
4163 if (retval
!= ERROR_OK
)
4164 command_print(CMD
, "Error during removal of watchpoint at address " TARGET_ADDR_FMT
, addr
);
4171 * Translate a virtual address to a physical address.
4173 * The low-level target implementation must have logged a detailed error
4174 * which is forwarded to telnet/GDB session.
4176 COMMAND_HANDLER(handle_virt2phys_command
)
4179 return ERROR_COMMAND_SYNTAX_ERROR
;
4182 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], va
);
4185 struct target
*target
= get_current_target(CMD_CTX
);
4186 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
4187 if (retval
== ERROR_OK
)
4188 command_print(CMD
, "Physical address " TARGET_ADDR_FMT
"", pa
);
4193 static void write_data(FILE *f
, const void *data
, size_t len
)
4195 size_t written
= fwrite(data
, 1, len
, f
);
4197 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
4200 static void write_long(FILE *f
, int l
, struct target
*target
)
4204 target_buffer_set_u32(target
, val
, l
);
4205 write_data(f
, val
, 4);
4208 static void write_string(FILE *f
, char *s
)
4210 write_data(f
, s
, strlen(s
));
4213 typedef unsigned char UNIT
[2]; /* unit of profiling */
4215 /* Dump a gmon.out histogram file. */
4216 static void write_gmon(uint32_t *samples
, uint32_t sample_num
, const char *filename
, bool with_range
,
4217 uint32_t start_address
, uint32_t end_address
, struct target
*target
, uint32_t duration_ms
)
4220 FILE *f
= fopen(filename
, "w");
4223 write_string(f
, "gmon");
4224 write_long(f
, 0x00000001, target
); /* Version */
4225 write_long(f
, 0, target
); /* padding */
4226 write_long(f
, 0, target
); /* padding */
4227 write_long(f
, 0, target
); /* padding */
4229 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
4230 write_data(f
, &zero
, 1);
4232 /* figure out bucket size */
4236 min
= start_address
;
4241 for (i
= 0; i
< sample_num
; i
++) {
4242 if (min
> samples
[i
])
4244 if (max
< samples
[i
])
4248 /* max should be (largest sample + 1)
4249 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
4250 if (max
< UINT32_MAX
)
4253 /* gprof requires (max - min) >= 2 */
4254 while ((max
- min
) < 2) {
4255 if (max
< UINT32_MAX
)
4262 uint32_t address_space
= max
- min
;
4264 /* FIXME: What is the reasonable number of buckets?
4265 * The profiling result will be more accurate if there are enough buckets. */
4266 static const uint32_t max_buckets
= 128 * 1024; /* maximum buckets. */
4267 uint32_t num_buckets
= address_space
/ sizeof(UNIT
);
4268 if (num_buckets
> max_buckets
)
4269 num_buckets
= max_buckets
;
4270 int *buckets
= malloc(sizeof(int) * num_buckets
);
4275 memset(buckets
, 0, sizeof(int) * num_buckets
);
4276 for (i
= 0; i
< sample_num
; i
++) {
4277 uint32_t address
= samples
[i
];
4279 if ((address
< min
) || (max
<= address
))
4282 long long a
= address
- min
;
4283 long long b
= num_buckets
;
4284 long long c
= address_space
;
4285 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
4289 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
4290 write_long(f
, min
, target
); /* low_pc */
4291 write_long(f
, max
, target
); /* high_pc */
4292 write_long(f
, num_buckets
, target
); /* # of buckets */
4293 float sample_rate
= sample_num
/ (duration_ms
/ 1000.0);
4294 write_long(f
, sample_rate
, target
);
4295 write_string(f
, "seconds");
4296 for (i
= 0; i
< (15-strlen("seconds")); i
++)
4297 write_data(f
, &zero
, 1);
4298 write_string(f
, "s");
4300 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
4302 char *data
= malloc(2 * num_buckets
);
4304 for (i
= 0; i
< num_buckets
; i
++) {
4309 data
[i
* 2] = val
&0xff;
4310 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
4313 write_data(f
, data
, num_buckets
* 2);
4321 /* profiling samples the CPU PC as quickly as OpenOCD is able,
4322 * which will be used as a random sampling of PC */
4323 COMMAND_HANDLER(handle_profile_command
)
4325 struct target
*target
= get_current_target(CMD_CTX
);
4327 if ((CMD_ARGC
!= 2) && (CMD_ARGC
!= 4))
4328 return ERROR_COMMAND_SYNTAX_ERROR
;
4330 const uint32_t MAX_PROFILE_SAMPLE_NUM
= 10000;
4332 uint32_t num_of_samples
;
4333 int retval
= ERROR_OK
;
4334 bool halted_before_profiling
= target
->state
== TARGET_HALTED
;
4336 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], offset
);
4338 uint32_t start_address
= 0;
4339 uint32_t end_address
= 0;
4340 bool with_range
= false;
4341 if (CMD_ARGC
== 4) {
4343 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], start_address
);
4344 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], end_address
);
4345 if (start_address
> end_address
|| (end_address
- start_address
) < 2) {
4346 command_print(CMD
, "Error: end - start < 2");
4347 return ERROR_COMMAND_ARGUMENT_INVALID
;
4351 uint32_t *samples
= malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM
);
4353 LOG_ERROR("No memory to store samples.");
4357 uint64_t timestart_ms
= timeval_ms();
4359 * Some cores let us sample the PC without the
4360 * annoying halt/resume step; for example, ARMv7 PCSR.
4361 * Provide a way to use that more efficient mechanism.
4363 retval
= target_profiling(target
, samples
, MAX_PROFILE_SAMPLE_NUM
,
4364 &num_of_samples
, offset
);
4365 if (retval
!= ERROR_OK
) {
4369 uint32_t duration_ms
= timeval_ms() - timestart_ms
;
4371 assert(num_of_samples
<= MAX_PROFILE_SAMPLE_NUM
);
4373 retval
= target_poll(target
);
4374 if (retval
!= ERROR_OK
) {
4379 if (target
->state
== TARGET_RUNNING
&& halted_before_profiling
) {
4380 /* The target was halted before we started and is running now. Halt it,
4381 * for consistency. */
4382 retval
= target_halt(target
);
4383 if (retval
!= ERROR_OK
) {
4387 } else if (target
->state
== TARGET_HALTED
&& !halted_before_profiling
) {
4388 /* The target was running before we started and is halted now. Resume
4389 * it, for consistency. */
4390 retval
= target_resume(target
, 1, 0, 0, 0);
4391 if (retval
!= ERROR_OK
) {
4397 retval
= target_poll(target
);
4398 if (retval
!= ERROR_OK
) {
4403 write_gmon(samples
, num_of_samples
, CMD_ARGV
[1],
4404 with_range
, start_address
, end_address
, target
, duration_ms
);
4405 command_print(CMD
, "Wrote %s", CMD_ARGV
[1]);
4411 static int new_u64_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint64_t val
)
4414 Jim_Obj
*obj_name
, *obj_val
;
4417 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4421 obj_name
= Jim_NewStringObj(interp
, namebuf
, -1);
4422 jim_wide wide_val
= val
;
4423 obj_val
= Jim_NewWideObj(interp
, wide_val
);
4424 if (!obj_name
|| !obj_val
) {
4429 Jim_IncrRefCount(obj_name
);
4430 Jim_IncrRefCount(obj_val
);
4431 result
= Jim_SetVariable(interp
, obj_name
, obj_val
);
4432 Jim_DecrRefCount(interp
, obj_name
);
4433 Jim_DecrRefCount(interp
, obj_val
);
4435 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
4439 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
4443 LOG_WARNING("DEPRECATED! use 'read_memory' not 'mem2array'");
4445 /* argv[0] = name of array to receive the data
4446 * argv[1] = desired element width in bits
4447 * argv[2] = memory address
4448 * argv[3] = count of times to read
4449 * argv[4] = optional "phys"
4451 if (argc
< 4 || argc
> 5) {
4452 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4456 /* Arg 0: Name of the array variable */
4457 const char *varname
= Jim_GetString(argv
[0], NULL
);
4459 /* Arg 1: Bit width of one element */
4461 e
= Jim_GetLong(interp
, argv
[1], &l
);
4464 const unsigned int width_bits
= l
;
4466 if (width_bits
!= 8 &&
4470 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4471 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4472 "Invalid width param. Must be one of: 8, 16, 32 or 64.", NULL
);
4475 const unsigned int width
= width_bits
/ 8;
4477 /* Arg 2: Memory address */
4479 e
= Jim_GetWide(interp
, argv
[2], &wide_addr
);
4482 target_addr_t addr
= (target_addr_t
)wide_addr
;
4484 /* Arg 3: Number of elements to read */
4485 e
= Jim_GetLong(interp
, argv
[3], &l
);
4491 bool is_phys
= false;
4494 const char *phys
= Jim_GetString(argv
[4], &str_len
);
4495 if (!strncmp(phys
, "phys", str_len
))
4501 /* Argument checks */
4503 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4504 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
4507 if ((addr
+ (len
* width
)) < addr
) {
4508 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4509 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
4513 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4514 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4515 "mem2array: too large read request, exceeds 64K items", NULL
);
4520 ((width
== 2) && ((addr
& 1) == 0)) ||
4521 ((width
== 4) && ((addr
& 3) == 0)) ||
4522 ((width
== 8) && ((addr
& 7) == 0))) {
4523 /* alignment correct */
4526 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4527 sprintf(buf
, "mem2array address: " TARGET_ADDR_FMT
" is not aligned for %" PRIu32
" byte reads",
4530 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4539 const size_t buffersize
= 4096;
4540 uint8_t *buffer
= malloc(buffersize
);
4547 /* Slurp... in buffer size chunks */
4548 const unsigned int max_chunk_len
= buffersize
/ width
;
4549 const size_t chunk_len
= MIN(len
, max_chunk_len
); /* in elements.. */
4553 retval
= target_read_phys_memory(target
, addr
, width
, chunk_len
, buffer
);
4555 retval
= target_read_memory(target
, addr
, width
, chunk_len
, buffer
);
4556 if (retval
!= ERROR_OK
) {
4558 LOG_ERROR("mem2array: Read @ " TARGET_ADDR_FMT
", w=%u, cnt=%zu, failed",
4562 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4563 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
4567 for (size_t i
= 0; i
< chunk_len
; i
++, idx
++) {
4571 v
= target_buffer_get_u64(target
, &buffer
[i
*width
]);
4574 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
4577 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
4580 v
= buffer
[i
] & 0x0ff;
4583 new_u64_array_element(interp
, varname
, idx
, v
);
4586 addr
+= chunk_len
* width
;
4592 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4597 COMMAND_HANDLER(handle_target_read_memory
)
4600 * CMD_ARGV[0] = memory address
4601 * CMD_ARGV[1] = desired element width in bits
4602 * CMD_ARGV[2] = number of elements to read
4603 * CMD_ARGV[3] = optional "phys"
4606 if (CMD_ARGC
< 3 || CMD_ARGC
> 4)
4607 return ERROR_COMMAND_SYNTAX_ERROR
;
4609 /* Arg 1: Memory address. */
4611 COMMAND_PARSE_NUMBER(u64
, CMD_ARGV
[0], addr
);
4613 /* Arg 2: Bit width of one element. */
4614 unsigned int width_bits
;
4615 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], width_bits
);
4617 /* Arg 3: Number of elements to read. */
4619 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
4621 /* Arg 4: Optional 'phys'. */
4622 bool is_phys
= false;
4623 if (CMD_ARGC
== 4) {
4624 if (strcmp(CMD_ARGV
[3], "phys")) {
4625 command_print(CMD
, "invalid argument '%s', must be 'phys'", CMD_ARGV
[3]);
4626 return ERROR_COMMAND_ARGUMENT_INVALID
;
4632 switch (width_bits
) {
4639 command_print(CMD
, "invalid width, must be 8, 16, 32 or 64");
4640 return ERROR_COMMAND_ARGUMENT_INVALID
;
4643 const unsigned int width
= width_bits
/ 8;
4645 if ((addr
+ (count
* width
)) < addr
) {
4646 command_print(CMD
, "read_memory: addr + count wraps to zero");
4647 return ERROR_COMMAND_ARGUMENT_INVALID
;
4650 if (count
> 65536) {
4651 command_print(CMD
, "read_memory: too large read request, exceeds 64K elements");
4652 return ERROR_COMMAND_ARGUMENT_INVALID
;
4655 struct target
*target
= get_current_target(CMD_CTX
);
4657 const size_t buffersize
= 4096;
4658 uint8_t *buffer
= malloc(buffersize
);
4661 LOG_ERROR("Failed to allocate memory");
4665 char *separator
= "";
4667 const unsigned int max_chunk_len
= buffersize
/ width
;
4668 const size_t chunk_len
= MIN(count
, max_chunk_len
);
4673 retval
= target_read_phys_memory(target
, addr
, width
, chunk_len
, buffer
);
4675 retval
= target_read_memory(target
, addr
, width
, chunk_len
, buffer
);
4677 if (retval
!= ERROR_OK
) {
4678 LOG_DEBUG("read_memory: read at " TARGET_ADDR_FMT
" with width=%u and count=%zu failed",
4679 addr
, width_bits
, chunk_len
);
4681 * FIXME: we append the errmsg to the list of value already read.
4682 * Add a way to flush and replace old output, but LOG_DEBUG() it
4684 command_print(CMD
, "read_memory: failed to read memory");
4689 for (size_t i
= 0; i
< chunk_len
; i
++) {
4694 v
= target_buffer_get_u64(target
, &buffer
[i
* width
]);
4697 v
= target_buffer_get_u32(target
, &buffer
[i
* width
]);
4700 v
= target_buffer_get_u16(target
, &buffer
[i
* width
]);
4707 command_print_sameline(CMD
, "%s0x%" PRIx64
, separator
, v
);
4712 addr
+= chunk_len
* width
;
4720 static int get_u64_array_element(Jim_Interp
*interp
, const char *varname
, size_t idx
, uint64_t *val
)
4722 char *namebuf
= alloc_printf("%s(%zu)", varname
, idx
);
4726 Jim_Obj
*obj_name
= Jim_NewStringObj(interp
, namebuf
, -1);
4732 Jim_IncrRefCount(obj_name
);
4733 Jim_Obj
*obj_val
= Jim_GetVariable(interp
, obj_name
, JIM_ERRMSG
);
4734 Jim_DecrRefCount(interp
, obj_name
);
4740 int result
= Jim_GetWide(interp
, obj_val
, &wide_val
);
4745 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
4746 int argc
, Jim_Obj
*const *argv
)
4750 LOG_WARNING("DEPRECATED! use 'write_memory' not 'array2mem'");
4752 /* argv[0] = name of array from which to read the data
4753 * argv[1] = desired element width in bits
4754 * argv[2] = memory address
4755 * argv[3] = number of elements to write
4756 * argv[4] = optional "phys"
4758 if (argc
< 4 || argc
> 5) {
4759 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4763 /* Arg 0: Name of the array variable */
4764 const char *varname
= Jim_GetString(argv
[0], NULL
);
4766 /* Arg 1: Bit width of one element */
4768 e
= Jim_GetLong(interp
, argv
[1], &l
);
4771 const unsigned int width_bits
= l
;
4773 if (width_bits
!= 8 &&
4777 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4778 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4779 "Invalid width param. Must be one of: 8, 16, 32 or 64.", NULL
);
4782 const unsigned int width
= width_bits
/ 8;
4784 /* Arg 2: Memory address */
4786 e
= Jim_GetWide(interp
, argv
[2], &wide_addr
);
4789 target_addr_t addr
= (target_addr_t
)wide_addr
;
4791 /* Arg 3: Number of elements to write */
4792 e
= Jim_GetLong(interp
, argv
[3], &l
);
4798 bool is_phys
= false;
4801 const char *phys
= Jim_GetString(argv
[4], &str_len
);
4802 if (!strncmp(phys
, "phys", str_len
))
4808 /* Argument checks */
4810 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4811 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4812 "array2mem: zero width read?", NULL
);
4816 if ((addr
+ (len
* width
)) < addr
) {
4817 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4818 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4819 "array2mem: addr + len - wraps to zero?", NULL
);
4824 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4825 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4826 "array2mem: too large memory write request, exceeds 64K items", NULL
);
4831 ((width
== 2) && ((addr
& 1) == 0)) ||
4832 ((width
== 4) && ((addr
& 3) == 0)) ||
4833 ((width
== 8) && ((addr
& 7) == 0))) {
4834 /* alignment correct */
4837 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4838 sprintf(buf
, "array2mem address: " TARGET_ADDR_FMT
" is not aligned for %" PRIu32
" byte reads",
4841 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4850 const size_t buffersize
= 4096;
4851 uint8_t *buffer
= malloc(buffersize
);
4859 /* Slurp... in buffer size chunks */
4860 const unsigned int max_chunk_len
= buffersize
/ width
;
4862 const size_t chunk_len
= MIN(len
, max_chunk_len
); /* in elements.. */
4864 /* Fill the buffer */
4865 for (size_t i
= 0; i
< chunk_len
; i
++, idx
++) {
4867 if (get_u64_array_element(interp
, varname
, idx
, &v
) != JIM_OK
) {
4873 target_buffer_set_u64(target
, &buffer
[i
* width
], v
);
4876 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
4879 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
4882 buffer
[i
] = v
& 0x0ff;
4888 /* Write the buffer to memory */
4891 retval
= target_write_phys_memory(target
, addr
, width
, chunk_len
, buffer
);
4893 retval
= target_write_memory(target
, addr
, width
, chunk_len
, buffer
);
4894 if (retval
!= ERROR_OK
) {
4896 LOG_ERROR("array2mem: Write @ " TARGET_ADDR_FMT
", w=%u, cnt=%zu, failed",
4900 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4901 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
4905 addr
+= chunk_len
* width
;
4910 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4915 static int target_jim_write_memory(Jim_Interp
*interp
, int argc
,
4916 Jim_Obj
* const *argv
)
4919 * argv[1] = memory address
4920 * argv[2] = desired element width in bits
4921 * argv[3] = list of data to write
4922 * argv[4] = optional "phys"
4925 if (argc
< 4 || argc
> 5) {
4926 Jim_WrongNumArgs(interp
, 1, argv
, "address width data ['phys']");
4930 /* Arg 1: Memory address. */
4933 e
= Jim_GetWide(interp
, argv
[1], &wide_addr
);
4938 target_addr_t addr
= (target_addr_t
)wide_addr
;
4940 /* Arg 2: Bit width of one element. */
4942 e
= Jim_GetLong(interp
, argv
[2], &l
);
4947 const unsigned int width_bits
= l
;
4948 size_t count
= Jim_ListLength(interp
, argv
[3]);
4950 /* Arg 4: Optional 'phys'. */
4951 bool is_phys
= false;
4954 const char *phys
= Jim_GetString(argv
[4], NULL
);
4956 if (strcmp(phys
, "phys")) {
4957 Jim_SetResultFormatted(interp
, "invalid argument '%s', must be 'phys'", phys
);
4964 switch (width_bits
) {
4971 Jim_SetResultString(interp
, "invalid width, must be 8, 16, 32 or 64", -1);
4975 const unsigned int width
= width_bits
/ 8;
4977 if ((addr
+ (count
* width
)) < addr
) {
4978 Jim_SetResultString(interp
, "write_memory: addr + len wraps to zero", -1);
4982 if (count
> 65536) {
4983 Jim_SetResultString(interp
, "write_memory: too large memory write request, exceeds 64K elements", -1);
4987 struct command_context
*cmd_ctx
= current_command_context(interp
);
4988 assert(cmd_ctx
!= NULL
);
4989 struct target
*target
= get_current_target(cmd_ctx
);
4991 const size_t buffersize
= 4096;
4992 uint8_t *buffer
= malloc(buffersize
);
4995 LOG_ERROR("Failed to allocate memory");
5002 const unsigned int max_chunk_len
= buffersize
/ width
;
5003 const size_t chunk_len
= MIN(count
, max_chunk_len
);
5005 for (size_t i
= 0; i
< chunk_len
; i
++, j
++) {
5006 Jim_Obj
*tmp
= Jim_ListGetIndex(interp
, argv
[3], j
);
5007 jim_wide element_wide
;
5008 Jim_GetWide(interp
, tmp
, &element_wide
);
5010 const uint64_t v
= element_wide
;
5014 target_buffer_set_u64(target
, &buffer
[i
* width
], v
);
5017 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
5020 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
5023 buffer
[i
] = v
& 0x0ff;
5033 retval
= target_write_phys_memory(target
, addr
, width
, chunk_len
, buffer
);
5035 retval
= target_write_memory(target
, addr
, width
, chunk_len
, buffer
);
5037 if (retval
!= ERROR_OK
) {
5038 LOG_ERROR("write_memory: write at " TARGET_ADDR_FMT
" with width=%u and count=%zu failed",
5039 addr
, width_bits
, chunk_len
);
5040 Jim_SetResultString(interp
, "write_memory: failed to write memory", -1);
5045 addr
+= chunk_len
* width
;
5053 /* FIX? should we propagate errors here rather than printing them
5056 void target_handle_event(struct target
*target
, enum target_event e
)
5058 struct target_event_action
*teap
;
5061 for (teap
= target
->event_action
; teap
; teap
= teap
->next
) {
5062 if (teap
->event
== e
) {
5063 LOG_DEBUG("target(%d): %s (%s) event: %d (%s) action: %s",
5064 target
->target_number
,
5065 target_name(target
),
5066 target_type_name(target
),
5068 target_event_name(e
),
5069 Jim_GetString(teap
->body
, NULL
));
5071 /* Override current target by the target an event
5072 * is issued from (lot of scripts need it).
5073 * Return back to previous override as soon
5074 * as the handler processing is done */
5075 struct command_context
*cmd_ctx
= current_command_context(teap
->interp
);
5076 struct target
*saved_target_override
= cmd_ctx
->current_target_override
;
5077 cmd_ctx
->current_target_override
= target
;
5079 retval
= Jim_EvalObj(teap
->interp
, teap
->body
);
5081 cmd_ctx
->current_target_override
= saved_target_override
;
5083 if (retval
== ERROR_COMMAND_CLOSE_CONNECTION
)
5086 if (retval
== JIM_RETURN
)
5087 retval
= teap
->interp
->returnCode
;
5089 if (retval
!= JIM_OK
) {
5090 Jim_MakeErrorMessage(teap
->interp
);
5091 LOG_USER("Error executing event %s on target %s:\n%s",
5092 target_event_name(e
),
5093 target_name(target
),
5094 Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
5095 /* clean both error code and stacktrace before return */
5096 Jim_Eval(teap
->interp
, "error \"\" \"\"");
5102 static int target_jim_get_reg(Jim_Interp
*interp
, int argc
,
5103 Jim_Obj
* const *argv
)
5108 const char *option
= Jim_GetString(argv
[1], NULL
);
5110 if (!strcmp(option
, "-force")) {
5115 Jim_SetResultFormatted(interp
, "invalid option '%s'", option
);
5121 Jim_WrongNumArgs(interp
, 1, argv
, "[-force] list");
5125 const int length
= Jim_ListLength(interp
, argv
[1]);
5127 Jim_Obj
*result_dict
= Jim_NewDictObj(interp
, NULL
, 0);
5132 struct command_context
*cmd_ctx
= current_command_context(interp
);
5133 assert(cmd_ctx
!= NULL
);
5134 const struct target
*target
= get_current_target(cmd_ctx
);
5136 for (int i
= 0; i
< length
; i
++) {
5137 Jim_Obj
*elem
= Jim_ListGetIndex(interp
, argv
[1], i
);
5142 const char *reg_name
= Jim_String(elem
);
5144 struct reg
*reg
= register_get_by_name(target
->reg_cache
, reg_name
,
5147 if (!reg
|| !reg
->exist
) {
5148 Jim_SetResultFormatted(interp
, "unknown register '%s'", reg_name
);
5153 int retval
= reg
->type
->get(reg
);
5155 if (retval
!= ERROR_OK
) {
5156 Jim_SetResultFormatted(interp
, "failed to read register '%s'",
5162 char *reg_value
= buf_to_hex_str(reg
->value
, reg
->size
);
5165 LOG_ERROR("Failed to allocate memory");
5169 char *tmp
= alloc_printf("0x%s", reg_value
);
5174 LOG_ERROR("Failed to allocate memory");
5178 Jim_DictAddElement(interp
, result_dict
, elem
,
5179 Jim_NewStringObj(interp
, tmp
, -1));
5184 Jim_SetResult(interp
, result_dict
);
5189 static int target_jim_set_reg(Jim_Interp
*interp
, int argc
,
5190 Jim_Obj
* const *argv
)
5193 Jim_WrongNumArgs(interp
, 1, argv
, "dict");
5198 #if JIM_VERSION >= 80
5199 Jim_Obj
**dict
= Jim_DictPairs(interp
, argv
[1], &tmp
);
5205 int ret
= Jim_DictPairs(interp
, argv
[1], &dict
, &tmp
);
5211 const unsigned int length
= tmp
;
5212 struct command_context
*cmd_ctx
= current_command_context(interp
);
5214 const struct target
*target
= get_current_target(cmd_ctx
);
5216 for (unsigned int i
= 0; i
< length
; i
+= 2) {
5217 const char *reg_name
= Jim_String(dict
[i
]);
5218 const char *reg_value
= Jim_String(dict
[i
+ 1]);
5219 struct reg
*reg
= register_get_by_name(target
->reg_cache
, reg_name
,
5222 if (!reg
|| !reg
->exist
) {
5223 Jim_SetResultFormatted(interp
, "unknown register '%s'", reg_name
);
5227 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
5230 LOG_ERROR("Failed to allocate memory");
5234 str_to_buf(reg_value
, strlen(reg_value
), buf
, reg
->size
, 0);
5235 int retval
= reg
->type
->set(reg
, buf
);
5238 if (retval
!= ERROR_OK
) {
5239 Jim_SetResultFormatted(interp
, "failed to set '%s' to register '%s'",
5240 reg_value
, reg_name
);
5249 * Returns true only if the target has a handler for the specified event.
5251 bool target_has_event_action(struct target
*target
, enum target_event event
)
5253 struct target_event_action
*teap
;
5255 for (teap
= target
->event_action
; teap
; teap
= teap
->next
) {
5256 if (teap
->event
== event
)
5262 enum target_cfg_param
{
5265 TCFG_WORK_AREA_VIRT
,
5266 TCFG_WORK_AREA_PHYS
,
5267 TCFG_WORK_AREA_SIZE
,
5268 TCFG_WORK_AREA_BACKUP
,
5271 TCFG_CHAIN_POSITION
,
5276 TCFG_GDB_MAX_CONNECTIONS
,
5279 static struct jim_nvp nvp_config_opts
[] = {
5280 { .name
= "-type", .value
= TCFG_TYPE
},
5281 { .name
= "-event", .value
= TCFG_EVENT
},
5282 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
5283 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
5284 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
5285 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
5286 { .name
= "-endian", .value
= TCFG_ENDIAN
},
5287 { .name
= "-coreid", .value
= TCFG_COREID
},
5288 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
5289 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
5290 { .name
= "-rtos", .value
= TCFG_RTOS
},
5291 { .name
= "-defer-examine", .value
= TCFG_DEFER_EXAMINE
},
5292 { .name
= "-gdb-port", .value
= TCFG_GDB_PORT
},
5293 { .name
= "-gdb-max-connections", .value
= TCFG_GDB_MAX_CONNECTIONS
},
5294 { .name
= NULL
, .value
= -1 }
5297 static int target_configure(struct jim_getopt_info
*goi
, struct target
*target
)
5304 /* parse config or cget options ... */
5305 while (goi
->argc
> 0) {
5306 Jim_SetEmptyResult(goi
->interp
);
5307 /* jim_getopt_debug(goi); */
5309 if (target
->type
->target_jim_configure
) {
5310 /* target defines a configure function */
5311 /* target gets first dibs on parameters */
5312 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
5321 /* otherwise we 'continue' below */
5323 e
= jim_getopt_nvp(goi
, nvp_config_opts
, &n
);
5325 jim_getopt_nvp_unknown(goi
, nvp_config_opts
, 0);
5331 if (goi
->isconfigure
) {
5332 Jim_SetResultFormatted(goi
->interp
,
5333 "not settable: %s", n
->name
);
5337 if (goi
->argc
!= 0) {
5338 Jim_WrongNumArgs(goi
->interp
,
5339 goi
->argc
, goi
->argv
,
5344 Jim_SetResultString(goi
->interp
,
5345 target_type_name(target
), -1);
5349 if (goi
->argc
== 0) {
5350 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
5354 e
= jim_getopt_nvp(goi
, nvp_target_event
, &n
);
5356 jim_getopt_nvp_unknown(goi
, nvp_target_event
, 1);
5360 if (goi
->isconfigure
) {
5361 if (goi
->argc
!= 1) {
5362 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
5366 if (goi
->argc
!= 0) {
5367 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
5373 struct target_event_action
*teap
;
5375 teap
= target
->event_action
;
5376 /* replace existing? */
5378 if (teap
->event
== (enum target_event
)n
->value
)
5383 if (goi
->isconfigure
) {
5384 /* START_DEPRECATED_TPIU */
5385 if (n
->value
== TARGET_EVENT_TRACE_CONFIG
)
5386 LOG_INFO("DEPRECATED target event %s; use TPIU events {pre,post}-{enable,disable}", n
->name
);
5387 /* END_DEPRECATED_TPIU */
5389 bool replace
= true;
5392 teap
= calloc(1, sizeof(*teap
));
5395 teap
->event
= n
->value
;
5396 teap
->interp
= goi
->interp
;
5397 jim_getopt_obj(goi
, &o
);
5399 Jim_DecrRefCount(teap
->interp
, teap
->body
);
5400 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
5403 * Tcl/TK - "tk events" have a nice feature.
5404 * See the "BIND" command.
5405 * We should support that here.
5406 * You can specify %X and %Y in the event code.
5407 * The idea is: %T - target name.
5408 * The idea is: %N - target number
5409 * The idea is: %E - event name.
5411 Jim_IncrRefCount(teap
->body
);
5414 /* add to head of event list */
5415 teap
->next
= target
->event_action
;
5416 target
->event_action
= teap
;
5418 Jim_SetEmptyResult(goi
->interp
);
5422 Jim_SetEmptyResult(goi
->interp
);
5424 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
5430 case TCFG_WORK_AREA_VIRT
:
5431 if (goi
->isconfigure
) {
5432 target_free_all_working_areas(target
);
5433 e
= jim_getopt_wide(goi
, &w
);
5436 target
->working_area_virt
= w
;
5437 target
->working_area_virt_spec
= true;
5442 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
5446 case TCFG_WORK_AREA_PHYS
:
5447 if (goi
->isconfigure
) {
5448 target_free_all_working_areas(target
);
5449 e
= jim_getopt_wide(goi
, &w
);
5452 target
->working_area_phys
= w
;
5453 target
->working_area_phys_spec
= true;
5458 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
5462 case TCFG_WORK_AREA_SIZE
:
5463 if (goi
->isconfigure
) {
5464 target_free_all_working_areas(target
);
5465 e
= jim_getopt_wide(goi
, &w
);
5468 target
->working_area_size
= w
;
5473 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
5477 case TCFG_WORK_AREA_BACKUP
:
5478 if (goi
->isconfigure
) {
5479 target_free_all_working_areas(target
);
5480 e
= jim_getopt_wide(goi
, &w
);
5483 /* make this exactly 1 or 0 */
5484 target
->backup_working_area
= (!!w
);
5489 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
5490 /* loop for more e*/
5495 if (goi
->isconfigure
) {
5496 e
= jim_getopt_nvp(goi
, nvp_target_endian
, &n
);
5498 jim_getopt_nvp_unknown(goi
, nvp_target_endian
, 1);
5501 target
->endianness
= n
->value
;
5506 n
= jim_nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
5508 target
->endianness
= TARGET_LITTLE_ENDIAN
;
5509 n
= jim_nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
5511 Jim_SetResultString(goi
->interp
, n
->name
, -1);
5516 if (goi
->isconfigure
) {
5517 e
= jim_getopt_wide(goi
, &w
);
5520 target
->coreid
= (int32_t)w
;
5525 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->coreid
));
5529 case TCFG_CHAIN_POSITION
:
5530 if (goi
->isconfigure
) {
5532 struct jtag_tap
*tap
;
5534 if (target
->has_dap
) {
5535 Jim_SetResultString(goi
->interp
,
5536 "target requires -dap parameter instead of -chain-position!", -1);
5540 target_free_all_working_areas(target
);
5541 e
= jim_getopt_obj(goi
, &o_t
);
5544 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
5548 target
->tap_configured
= true;
5553 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
5554 /* loop for more e*/
5557 if (goi
->isconfigure
) {
5558 e
= jim_getopt_wide(goi
, &w
);
5561 target
->dbgbase
= (uint32_t)w
;
5562 target
->dbgbase_set
= true;
5567 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
5573 int result
= rtos_create(goi
, target
);
5574 if (result
!= JIM_OK
)
5580 case TCFG_DEFER_EXAMINE
:
5582 target
->defer_examine
= true;
5587 if (goi
->isconfigure
) {
5588 struct command_context
*cmd_ctx
= current_command_context(goi
->interp
);
5589 if (cmd_ctx
->mode
!= COMMAND_CONFIG
) {
5590 Jim_SetResultString(goi
->interp
, "-gdb-port must be configured before 'init'", -1);
5595 e
= jim_getopt_string(goi
, &s
, NULL
);
5598 free(target
->gdb_port_override
);
5599 target
->gdb_port_override
= strdup(s
);
5604 Jim_SetResultString(goi
->interp
, target
->gdb_port_override
? target
->gdb_port_override
: "undefined", -1);
5608 case TCFG_GDB_MAX_CONNECTIONS
:
5609 if (goi
->isconfigure
) {
5610 struct command_context
*cmd_ctx
= current_command_context(goi
->interp
);
5611 if (cmd_ctx
->mode
!= COMMAND_CONFIG
) {
5612 Jim_SetResultString(goi
->interp
, "-gdb-max-connections must be configured before 'init'", -1);
5616 e
= jim_getopt_wide(goi
, &w
);
5619 target
->gdb_max_connections
= (w
< 0) ? CONNECTION_LIMIT_UNLIMITED
: (int)w
;
5624 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->gdb_max_connections
));
5627 } /* while (goi->argc) */
5630 /* done - we return */
5634 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5636 struct command
*c
= jim_to_command(interp
);
5637 struct jim_getopt_info goi
;
5639 jim_getopt_setup(&goi
, interp
, argc
- 1, argv
+ 1);
5640 goi
.isconfigure
= !strcmp(c
->name
, "configure");
5642 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5643 "missing: -option ...");
5646 struct command_context
*cmd_ctx
= current_command_context(interp
);
5648 struct target
*target
= get_current_target(cmd_ctx
);
5649 return target_configure(&goi
, target
);
5652 static int jim_target_mem2array(Jim_Interp
*interp
,
5653 int argc
, Jim_Obj
*const *argv
)
5655 struct command_context
*cmd_ctx
= current_command_context(interp
);
5657 struct target
*target
= get_current_target(cmd_ctx
);
5658 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
5661 static int jim_target_array2mem(Jim_Interp
*interp
,
5662 int argc
, Jim_Obj
*const *argv
)
5664 struct command_context
*cmd_ctx
= current_command_context(interp
);
5666 struct target
*target
= get_current_target(cmd_ctx
);
5667 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
5670 COMMAND_HANDLER(handle_target_examine
)
5672 bool allow_defer
= false;
5675 return ERROR_COMMAND_SYNTAX_ERROR
;
5677 if (CMD_ARGC
== 1) {
5678 if (strcmp(CMD_ARGV
[0], "allow-defer"))
5679 return ERROR_COMMAND_ARGUMENT_INVALID
;
5683 struct target
*target
= get_current_target(CMD_CTX
);
5684 if (!target
->tap
->enabled
) {
5685 command_print(CMD
, "[TAP is disabled]");
5689 if (allow_defer
&& target
->defer_examine
) {
5690 LOG_INFO("Deferring arp_examine of %s", target_name(target
));
5691 LOG_INFO("Use arp_examine command to examine it manually!");
5695 int retval
= target
->type
->examine(target
);
5696 if (retval
!= ERROR_OK
) {
5697 target_reset_examined(target
);
5701 target_set_examined(target
);
5706 COMMAND_HANDLER(handle_target_was_examined
)
5709 return ERROR_COMMAND_SYNTAX_ERROR
;
5711 struct target
*target
= get_current_target(CMD_CTX
);
5713 command_print(CMD
, "%d", target_was_examined(target
) ? 1 : 0);
5718 COMMAND_HANDLER(handle_target_examine_deferred
)
5721 return ERROR_COMMAND_SYNTAX_ERROR
;
5723 struct target
*target
= get_current_target(CMD_CTX
);
5725 command_print(CMD
, "%d", target
->defer_examine
? 1 : 0);
5730 COMMAND_HANDLER(handle_target_halt_gdb
)
5733 return ERROR_COMMAND_SYNTAX_ERROR
;
5735 struct target
*target
= get_current_target(CMD_CTX
);
5737 return target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
5740 COMMAND_HANDLER(handle_target_poll
)
5743 return ERROR_COMMAND_SYNTAX_ERROR
;
5745 struct target
*target
= get_current_target(CMD_CTX
);
5746 if (!target
->tap
->enabled
) {
5747 command_print(CMD
, "[TAP is disabled]");
5751 if (!(target_was_examined(target
)))
5752 return ERROR_TARGET_NOT_EXAMINED
;
5754 return target
->type
->poll(target
);
5757 COMMAND_HANDLER(handle_target_reset
)
5760 return ERROR_COMMAND_SYNTAX_ERROR
;
5762 const struct nvp
*n
= nvp_name2value(nvp_assert
, CMD_ARGV
[0]);
5764 nvp_unknown_command_print(CMD
, nvp_assert
, NULL
, CMD_ARGV
[0]);
5765 return ERROR_COMMAND_ARGUMENT_INVALID
;
5768 /* the halt or not param */
5770 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[1], a
);
5772 struct target
*target
= get_current_target(CMD_CTX
);
5773 if (!target
->tap
->enabled
) {
5774 command_print(CMD
, "[TAP is disabled]");
5778 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
5779 command_print(CMD
, "No target-specific reset for %s", target_name(target
));
5783 if (target
->defer_examine
)
5784 target_reset_examined(target
);
5786 /* determine if we should halt or not. */
5787 target
->reset_halt
= (a
!= 0);
5788 /* When this happens - all workareas are invalid. */
5789 target_free_all_working_areas_restore(target
, 0);
5792 if (n
->value
== NVP_ASSERT
)
5793 return target
->type
->assert_reset(target
);
5794 return target
->type
->deassert_reset(target
);
5797 COMMAND_HANDLER(handle_target_halt
)
5800 return ERROR_COMMAND_SYNTAX_ERROR
;
5802 struct target
*target
= get_current_target(CMD_CTX
);
5803 if (!target
->tap
->enabled
) {
5804 command_print(CMD
, "[TAP is disabled]");
5808 return target
->type
->halt(target
);
5811 COMMAND_HANDLER(handle_target_wait_state
)
5814 return ERROR_COMMAND_SYNTAX_ERROR
;
5816 const struct nvp
*n
= nvp_name2value(nvp_target_state
, CMD_ARGV
[0]);
5818 nvp_unknown_command_print(CMD
, nvp_target_state
, NULL
, CMD_ARGV
[0]);
5819 return ERROR_COMMAND_ARGUMENT_INVALID
;
5823 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], a
);
5825 struct target
*target
= get_current_target(CMD_CTX
);
5826 if (!target
->tap
->enabled
) {
5827 command_print(CMD
, "[TAP is disabled]");
5831 int retval
= target_wait_state(target
, n
->value
, a
);
5832 if (retval
!= ERROR_OK
) {
5834 "target: %s wait %s fails (%d) %s",
5835 target_name(target
), n
->name
,
5836 retval
, target_strerror_safe(retval
));
5841 /* List for human, Events defined for this target.
5842 * scripts/programs should use 'name cget -event NAME'
5844 COMMAND_HANDLER(handle_target_event_list
)
5846 struct target
*target
= get_current_target(CMD_CTX
);
5847 struct target_event_action
*teap
= target
->event_action
;
5849 command_print(CMD
, "Event actions for target (%d) %s\n",
5850 target
->target_number
,
5851 target_name(target
));
5852 command_print(CMD
, "%-25s | Body", "Event");
5853 command_print(CMD
, "------------------------- | "
5854 "----------------------------------------");
5856 command_print(CMD
, "%-25s | %s",
5857 target_event_name(teap
->event
),
5858 Jim_GetString(teap
->body
, NULL
));
5861 command_print(CMD
, "***END***");
5865 COMMAND_HANDLER(handle_target_current_state
)
5868 return ERROR_COMMAND_SYNTAX_ERROR
;
5870 struct target
*target
= get_current_target(CMD_CTX
);
5872 command_print(CMD
, "%s", target_state_name(target
));
5877 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5879 struct jim_getopt_info goi
;
5880 jim_getopt_setup(&goi
, interp
, argc
- 1, argv
+ 1);
5881 if (goi
.argc
!= 1) {
5882 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5883 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
5887 int e
= jim_getopt_nvp(&goi
, nvp_target_event
, &n
);
5889 jim_getopt_nvp_unknown(&goi
, nvp_target_event
, 1);
5892 struct command_context
*cmd_ctx
= current_command_context(interp
);
5894 struct target
*target
= get_current_target(cmd_ctx
);
5895 target_handle_event(target
, n
->value
);
5899 static const struct command_registration target_instance_command_handlers
[] = {
5901 .name
= "configure",
5902 .mode
= COMMAND_ANY
,
5903 .jim_handler
= jim_target_configure
,
5904 .help
= "configure a new target for use",
5905 .usage
= "[target_attribute ...]",
5909 .mode
= COMMAND_ANY
,
5910 .jim_handler
= jim_target_configure
,
5911 .help
= "returns the specified target attribute",
5912 .usage
= "target_attribute",
5916 .handler
= handle_mw_command
,
5917 .mode
= COMMAND_EXEC
,
5918 .help
= "Write 64-bit word(s) to target memory",
5919 .usage
= "address data [count]",
5923 .handler
= handle_mw_command
,
5924 .mode
= COMMAND_EXEC
,
5925 .help
= "Write 32-bit word(s) to target memory",
5926 .usage
= "address data [count]",
5930 .handler
= handle_mw_command
,
5931 .mode
= COMMAND_EXEC
,
5932 .help
= "Write 16-bit half-word(s) to target memory",
5933 .usage
= "address data [count]",
5937 .handler
= handle_mw_command
,
5938 .mode
= COMMAND_EXEC
,
5939 .help
= "Write byte(s) to target memory",
5940 .usage
= "address data [count]",
5944 .handler
= handle_md_command
,
5945 .mode
= COMMAND_EXEC
,
5946 .help
= "Display target memory as 64-bit words",
5947 .usage
= "address [count]",
5951 .handler
= handle_md_command
,
5952 .mode
= COMMAND_EXEC
,
5953 .help
= "Display target memory as 32-bit words",
5954 .usage
= "address [count]",
5958 .handler
= handle_md_command
,
5959 .mode
= COMMAND_EXEC
,
5960 .help
= "Display target memory as 16-bit half-words",
5961 .usage
= "address [count]",
5965 .handler
= handle_md_command
,
5966 .mode
= COMMAND_EXEC
,
5967 .help
= "Display target memory as 8-bit bytes",
5968 .usage
= "address [count]",
5971 .name
= "array2mem",
5972 .mode
= COMMAND_EXEC
,
5973 .jim_handler
= jim_target_array2mem
,
5974 .help
= "Writes Tcl array of 8/16/32 bit numbers "
5976 .usage
= "arrayname bitwidth address count",
5979 .name
= "mem2array",
5980 .mode
= COMMAND_EXEC
,
5981 .jim_handler
= jim_target_mem2array
,
5982 .help
= "Loads Tcl array of 8/16/32 bit numbers "
5983 "from target memory",
5984 .usage
= "arrayname bitwidth address count",
5988 .mode
= COMMAND_EXEC
,
5989 .jim_handler
= target_jim_get_reg
,
5990 .help
= "Get register values from the target",
5995 .mode
= COMMAND_EXEC
,
5996 .jim_handler
= target_jim_set_reg
,
5997 .help
= "Set target register values",
6001 .name
= "read_memory",
6002 .mode
= COMMAND_EXEC
,
6003 .handler
= handle_target_read_memory
,
6004 .help
= "Read Tcl list of 8/16/32/64 bit numbers from target memory",
6005 .usage
= "address width count ['phys']",
6008 .name
= "write_memory",
6009 .mode
= COMMAND_EXEC
,
6010 .jim_handler
= target_jim_write_memory
,
6011 .help
= "Write Tcl list of 8/16/32/64 bit numbers to target memory",
6012 .usage
= "address width data ['phys']",
6015 .name
= "eventlist",
6016 .handler
= handle_target_event_list
,
6017 .mode
= COMMAND_EXEC
,
6018 .help
= "displays a table of events defined for this target",
6023 .mode
= COMMAND_EXEC
,
6024 .handler
= handle_target_current_state
,
6025 .help
= "displays the current state of this target",
6029 .name
= "arp_examine",
6030 .mode
= COMMAND_EXEC
,
6031 .handler
= handle_target_examine
,
6032 .help
= "used internally for reset processing",
6033 .usage
= "['allow-defer']",
6036 .name
= "was_examined",
6037 .mode
= COMMAND_EXEC
,
6038 .handler
= handle_target_was_examined
,
6039 .help
= "used internally for reset processing",
6043 .name
= "examine_deferred",
6044 .mode
= COMMAND_EXEC
,
6045 .handler
= handle_target_examine_deferred
,
6046 .help
= "used internally for reset processing",
6050 .name
= "arp_halt_gdb",
6051 .mode
= COMMAND_EXEC
,
6052 .handler
= handle_target_halt_gdb
,
6053 .help
= "used internally for reset processing to halt GDB",
6058 .mode
= COMMAND_EXEC
,
6059 .handler
= handle_target_poll
,
6060 .help
= "used internally for reset processing",
6064 .name
= "arp_reset",
6065 .mode
= COMMAND_EXEC
,
6066 .handler
= handle_target_reset
,
6067 .help
= "used internally for reset processing",
6068 .usage
= "'assert'|'deassert' halt",
6072 .mode
= COMMAND_EXEC
,
6073 .handler
= handle_target_halt
,
6074 .help
= "used internally for reset processing",
6078 .name
= "arp_waitstate",
6079 .mode
= COMMAND_EXEC
,
6080 .handler
= handle_target_wait_state
,
6081 .help
= "used internally for reset processing",
6082 .usage
= "statename timeoutmsecs",
6085 .name
= "invoke-event",
6086 .mode
= COMMAND_EXEC
,
6087 .jim_handler
= jim_target_invoke_event
,
6088 .help
= "invoke handler for specified event",
6089 .usage
= "event_name",
6091 COMMAND_REGISTRATION_DONE
6094 static int target_create(struct jim_getopt_info
*goi
)
6101 struct target
*target
;
6102 struct command_context
*cmd_ctx
;
6104 cmd_ctx
= current_command_context(goi
->interp
);
6107 if (goi
->argc
< 3) {
6108 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
6113 jim_getopt_obj(goi
, &new_cmd
);
6114 /* does this command exist? */
6115 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_NONE
);
6117 cp
= Jim_GetString(new_cmd
, NULL
);
6118 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
6123 e
= jim_getopt_string(goi
, &cp
, NULL
);
6126 struct transport
*tr
= get_current_transport();
6127 if (tr
->override_target
) {
6128 e
= tr
->override_target(&cp
);
6129 if (e
!= ERROR_OK
) {
6130 LOG_ERROR("The selected transport doesn't support this target");
6133 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
6135 /* now does target type exist */
6136 for (x
= 0 ; target_types
[x
] ; x
++) {
6137 if (strcmp(cp
, target_types
[x
]->name
) == 0) {
6142 if (!target_types
[x
]) {
6143 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
6144 for (x
= 0 ; target_types
[x
] ; x
++) {
6145 if (target_types
[x
+ 1]) {
6146 Jim_AppendStrings(goi
->interp
,
6147 Jim_GetResult(goi
->interp
),
6148 target_types
[x
]->name
,
6151 Jim_AppendStrings(goi
->interp
,
6152 Jim_GetResult(goi
->interp
),
6154 target_types
[x
]->name
, NULL
);
6161 target
= calloc(1, sizeof(struct target
));
6163 LOG_ERROR("Out of memory");
6167 /* set empty smp cluster */
6168 target
->smp_targets
= &empty_smp_targets
;
6170 /* set target number */
6171 target
->target_number
= new_target_number();
6173 /* allocate memory for each unique target type */
6174 target
->type
= malloc(sizeof(struct target_type
));
6175 if (!target
->type
) {
6176 LOG_ERROR("Out of memory");
6181 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
6183 /* default to first core, override with -coreid */
6186 target
->working_area
= 0x0;
6187 target
->working_area_size
= 0x0;
6188 target
->working_areas
= NULL
;
6189 target
->backup_working_area
= 0;
6191 target
->state
= TARGET_UNKNOWN
;
6192 target
->debug_reason
= DBG_REASON_UNDEFINED
;
6193 target
->reg_cache
= NULL
;
6194 target
->breakpoints
= NULL
;
6195 target
->watchpoints
= NULL
;
6196 target
->next
= NULL
;
6197 target
->arch_info
= NULL
;
6199 target
->verbose_halt_msg
= true;
6201 target
->halt_issued
= false;
6203 /* initialize trace information */
6204 target
->trace_info
= calloc(1, sizeof(struct trace
));
6205 if (!target
->trace_info
) {
6206 LOG_ERROR("Out of memory");
6212 target
->dbgmsg
= NULL
;
6213 target
->dbg_msg_enabled
= 0;
6215 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
6217 target
->rtos
= NULL
;
6218 target
->rtos_auto_detect
= false;
6220 target
->gdb_port_override
= NULL
;
6221 target
->gdb_max_connections
= 1;
6223 /* Do the rest as "configure" options */
6224 goi
->isconfigure
= 1;
6225 e
= target_configure(goi
, target
);
6228 if (target
->has_dap
) {
6229 if (!target
->dap_configured
) {
6230 Jim_SetResultString(goi
->interp
, "-dap ?name? required when creating target", -1);
6234 if (!target
->tap_configured
) {
6235 Jim_SetResultString(goi
->interp
, "-chain-position ?name? required when creating target", -1);
6239 /* tap must be set after target was configured */
6245 rtos_destroy(target
);
6246 free(target
->gdb_port_override
);
6247 free(target
->trace_info
);
6253 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
6254 /* default endian to little if not specified */
6255 target
->endianness
= TARGET_LITTLE_ENDIAN
;
6258 cp
= Jim_GetString(new_cmd
, NULL
);
6259 target
->cmd_name
= strdup(cp
);
6260 if (!target
->cmd_name
) {
6261 LOG_ERROR("Out of memory");
6262 rtos_destroy(target
);
6263 free(target
->gdb_port_override
);
6264 free(target
->trace_info
);
6270 if (target
->type
->target_create
) {
6271 e
= (*(target
->type
->target_create
))(target
, goi
->interp
);
6272 if (e
!= ERROR_OK
) {
6273 LOG_DEBUG("target_create failed");
6274 free(target
->cmd_name
);
6275 rtos_destroy(target
);
6276 free(target
->gdb_port_override
);
6277 free(target
->trace_info
);
6284 /* create the target specific commands */
6285 if (target
->type
->commands
) {
6286 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
6288 LOG_ERROR("unable to register '%s' commands", cp
);
6291 /* now - create the new target name command */
6292 const struct command_registration target_subcommands
[] = {
6294 .chain
= target_instance_command_handlers
,
6297 .chain
= target
->type
->commands
,
6299 COMMAND_REGISTRATION_DONE
6301 const struct command_registration target_commands
[] = {
6304 .mode
= COMMAND_ANY
,
6305 .help
= "target command group",
6307 .chain
= target_subcommands
,
6309 COMMAND_REGISTRATION_DONE
6311 e
= register_commands_override_target(cmd_ctx
, NULL
, target_commands
, target
);
6312 if (e
!= ERROR_OK
) {
6313 if (target
->type
->deinit_target
)
6314 target
->type
->deinit_target(target
);
6315 free(target
->cmd_name
);
6316 rtos_destroy(target
);
6317 free(target
->gdb_port_override
);
6318 free(target
->trace_info
);
6324 /* append to end of list */
6325 append_to_list_all_targets(target
);
6327 cmd_ctx
->current_target
= target
;
6331 COMMAND_HANDLER(handle_target_current
)
6334 return ERROR_COMMAND_SYNTAX_ERROR
;
6336 struct target
*target
= get_current_target_or_null(CMD_CTX
);
6338 command_print(CMD
, "%s", target_name(target
));
6343 COMMAND_HANDLER(handle_target_types
)
6346 return ERROR_COMMAND_SYNTAX_ERROR
;
6348 for (unsigned int x
= 0; target_types
[x
]; x
++)
6349 command_print(CMD
, "%s", target_types
[x
]->name
);
6354 COMMAND_HANDLER(handle_target_names
)
6357 return ERROR_COMMAND_SYNTAX_ERROR
;
6359 struct target
*target
= all_targets
;
6361 command_print(CMD
, "%s", target_name(target
));
6362 target
= target
->next
;
6368 static struct target_list
*
6369 __attribute__((warn_unused_result
))
6370 create_target_list_node(const char *targetname
)
6372 struct target
*target
= get_target(targetname
);
6373 LOG_DEBUG("%s ", targetname
);
6377 struct target_list
*new = malloc(sizeof(struct target_list
));
6379 LOG_ERROR("Out of memory");
6383 new->target
= target
;
6387 static int get_target_with_common_rtos_type(struct command_invocation
*cmd
,
6388 struct list_head
*lh
, struct target
**result
)
6390 struct target
*target
= NULL
;
6391 struct target_list
*curr
;
6392 foreach_smp_target(curr
, lh
) {
6393 struct rtos
*curr_rtos
= curr
->target
->rtos
;
6395 if (target
&& target
->rtos
&& target
->rtos
->type
!= curr_rtos
->type
) {
6396 command_print(cmd
, "Different rtos types in members of one smp target!");
6399 target
= curr
->target
;
6406 COMMAND_HANDLER(handle_target_smp
)
6408 static int smp_group
= 1;
6410 if (CMD_ARGC
== 0) {
6411 LOG_DEBUG("Empty SMP target");
6414 LOG_DEBUG("%d", CMD_ARGC
);
6415 /* CMD_ARGC[0] = target to associate in smp
6416 * CMD_ARGC[1] = target to associate in smp
6420 struct list_head
*lh
= malloc(sizeof(*lh
));
6422 LOG_ERROR("Out of memory");
6427 for (unsigned int i
= 0; i
< CMD_ARGC
; i
++) {
6428 struct target_list
*new = create_target_list_node(CMD_ARGV
[i
]);
6430 list_add_tail(&new->lh
, lh
);
6432 /* now parse the list of cpu and put the target in smp mode*/
6433 struct target_list
*curr
;
6434 foreach_smp_target(curr
, lh
) {
6435 struct target
*target
= curr
->target
;
6436 target
->smp
= smp_group
;
6437 target
->smp_targets
= lh
;
6441 struct target
*rtos_target
;
6442 int retval
= get_target_with_common_rtos_type(CMD
, lh
, &rtos_target
);
6443 if (retval
== ERROR_OK
&& rtos_target
)
6444 retval
= rtos_smp_init(rtos_target
);
6449 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
6451 struct jim_getopt_info goi
;
6452 jim_getopt_setup(&goi
, interp
, argc
- 1, argv
+ 1);
6454 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
6455 "<name> <target_type> [<target_options> ...]");
6458 return target_create(&goi
);
6461 static const struct command_registration target_subcommand_handlers
[] = {
6464 .mode
= COMMAND_CONFIG
,
6465 .handler
= handle_target_init_command
,
6466 .help
= "initialize targets",
6471 .mode
= COMMAND_CONFIG
,
6472 .jim_handler
= jim_target_create
,
6473 .usage
= "name type '-chain-position' name [options ...]",
6474 .help
= "Creates and selects a new target",
6478 .mode
= COMMAND_ANY
,
6479 .handler
= handle_target_current
,
6480 .help
= "Returns the currently selected target",
6485 .mode
= COMMAND_ANY
,
6486 .handler
= handle_target_types
,
6487 .help
= "Returns the available target types as "
6488 "a list of strings",
6493 .mode
= COMMAND_ANY
,
6494 .handler
= handle_target_names
,
6495 .help
= "Returns the names of all targets as a list of strings",
6500 .mode
= COMMAND_ANY
,
6501 .handler
= handle_target_smp
,
6502 .usage
= "targetname1 targetname2 ...",
6503 .help
= "gather several target in a smp list"
6506 COMMAND_REGISTRATION_DONE
6510 target_addr_t address
;
6516 static int fastload_num
;
6517 static struct fast_load
*fastload
;
6519 static void free_fastload(void)
6522 for (int i
= 0; i
< fastload_num
; i
++)
6523 free(fastload
[i
].data
);
6529 COMMAND_HANDLER(handle_fast_load_image_command
)
6533 uint32_t image_size
;
6534 target_addr_t min_address
= 0;
6535 target_addr_t max_address
= -1;
6539 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command
,
6540 &image
, &min_address
, &max_address
);
6541 if (retval
!= ERROR_OK
)
6544 struct duration bench
;
6545 duration_start(&bench
);
6547 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
6548 if (retval
!= ERROR_OK
)
6553 fastload_num
= image
.num_sections
;
6554 fastload
= malloc(sizeof(struct fast_load
)*image
.num_sections
);
6556 command_print(CMD
, "out of memory");
6557 image_close(&image
);
6560 memset(fastload
, 0, sizeof(struct fast_load
)*image
.num_sections
);
6561 for (unsigned int i
= 0; i
< image
.num_sections
; i
++) {
6562 buffer
= malloc(image
.sections
[i
].size
);
6564 command_print(CMD
, "error allocating buffer for section (%d bytes)",
6565 (int)(image
.sections
[i
].size
));
6566 retval
= ERROR_FAIL
;
6570 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
6571 if (retval
!= ERROR_OK
) {
6576 uint32_t offset
= 0;
6577 uint32_t length
= buf_cnt
;
6579 /* DANGER!!! beware of unsigned comparison here!!! */
6581 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
6582 (image
.sections
[i
].base_address
< max_address
)) {
6583 if (image
.sections
[i
].base_address
< min_address
) {
6584 /* clip addresses below */
6585 offset
+= min_address
-image
.sections
[i
].base_address
;
6589 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
6590 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
6592 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
6593 fastload
[i
].data
= malloc(length
);
6594 if (!fastload
[i
].data
) {
6596 command_print(CMD
, "error allocating buffer for section (%" PRIu32
" bytes)",
6598 retval
= ERROR_FAIL
;
6601 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
6602 fastload
[i
].length
= length
;
6604 image_size
+= length
;
6605 command_print(CMD
, "%u bytes written at address 0x%8.8x",
6606 (unsigned int)length
,
6607 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
6613 if ((retval
== ERROR_OK
) && (duration_measure(&bench
) == ERROR_OK
)) {
6614 command_print(CMD
, "Loaded %" PRIu32
" bytes "
6615 "in %fs (%0.3f KiB/s)", image_size
,
6616 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
6619 "WARNING: image has not been loaded to target!"
6620 "You can issue a 'fast_load' to finish loading.");
6623 image_close(&image
);
6625 if (retval
!= ERROR_OK
)
6631 COMMAND_HANDLER(handle_fast_load_command
)
6634 return ERROR_COMMAND_SYNTAX_ERROR
;
6636 LOG_ERROR("No image in memory");
6640 int64_t ms
= timeval_ms();
6642 int retval
= ERROR_OK
;
6643 for (i
= 0; i
< fastload_num
; i
++) {
6644 struct target
*target
= get_current_target(CMD_CTX
);
6645 command_print(CMD
, "Write to 0x%08x, length 0x%08x",
6646 (unsigned int)(fastload
[i
].address
),
6647 (unsigned int)(fastload
[i
].length
));
6648 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
6649 if (retval
!= ERROR_OK
)
6651 size
+= fastload
[i
].length
;
6653 if (retval
== ERROR_OK
) {
6654 int64_t after
= timeval_ms();
6655 command_print(CMD
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
6660 static const struct command_registration target_command_handlers
[] = {
6663 .handler
= handle_targets_command
,
6664 .mode
= COMMAND_ANY
,
6665 .help
= "change current default target (one parameter) "
6666 "or prints table of all targets (no parameters)",
6667 .usage
= "[target]",
6671 .mode
= COMMAND_CONFIG
,
6672 .help
= "configure target",
6673 .chain
= target_subcommand_handlers
,
6676 COMMAND_REGISTRATION_DONE
6679 int target_register_commands(struct command_context
*cmd_ctx
)
6681 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
6684 static bool target_reset_nag
= true;
6686 bool get_target_reset_nag(void)
6688 return target_reset_nag
;
6691 COMMAND_HANDLER(handle_target_reset_nag
)
6693 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
6694 &target_reset_nag
, "Nag after each reset about options to improve "
6698 COMMAND_HANDLER(handle_ps_command
)
6700 struct target
*target
= get_current_target(CMD_CTX
);
6702 if (target
->state
!= TARGET_HALTED
) {
6703 command_print(CMD
, "Error: [%s] not halted", target_name(target
));
6704 return ERROR_TARGET_NOT_HALTED
;
6707 if ((target
->rtos
) && (target
->rtos
->type
)
6708 && (target
->rtos
->type
->ps_command
)) {
6709 display
= target
->rtos
->type
->ps_command(target
);
6710 command_print(CMD
, "%s", display
);
6715 return ERROR_TARGET_FAILURE
;
6719 static void binprint(struct command_invocation
*cmd
, const char *text
, const uint8_t *buf
, int size
)
6722 command_print_sameline(cmd
, "%s", text
);
6723 for (int i
= 0; i
< size
; i
++)
6724 command_print_sameline(cmd
, " %02x", buf
[i
]);
6725 command_print(cmd
, " ");
6728 COMMAND_HANDLER(handle_test_mem_access_command
)
6730 struct target
*target
= get_current_target(CMD_CTX
);
6732 int retval
= ERROR_OK
;
6734 if (target
->state
!= TARGET_HALTED
) {
6735 command_print(CMD
, "Error: [%s] not halted", target_name(target
));
6736 return ERROR_TARGET_NOT_HALTED
;
6740 return ERROR_COMMAND_SYNTAX_ERROR
;
6742 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], test_size
);
6745 size_t num_bytes
= test_size
+ 4;
6747 struct working_area
*wa
= NULL
;
6748 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6749 if (retval
!= ERROR_OK
) {
6750 LOG_ERROR("Not enough working area");
6754 uint8_t *test_pattern
= malloc(num_bytes
);
6756 for (size_t i
= 0; i
< num_bytes
; i
++)
6757 test_pattern
[i
] = rand();
6759 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6760 if (retval
!= ERROR_OK
) {
6761 LOG_ERROR("Test pattern write failed");
6765 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6766 for (int size
= 1; size
<= 4; size
*= 2) {
6767 for (int offset
= 0; offset
< 4; offset
++) {
6768 uint32_t count
= test_size
/ size
;
6769 size_t host_bufsiz
= (count
+ 2) * size
+ host_offset
;
6770 uint8_t *read_ref
= malloc(host_bufsiz
);
6771 uint8_t *read_buf
= malloc(host_bufsiz
);
6773 for (size_t i
= 0; i
< host_bufsiz
; i
++) {
6774 read_ref
[i
] = rand();
6775 read_buf
[i
] = read_ref
[i
];
6777 command_print_sameline(CMD
,
6778 "Test read %" PRIu32
" x %d @ %d to %saligned buffer: ", count
,
6779 size
, offset
, host_offset
? "un" : "");
6781 struct duration bench
;
6782 duration_start(&bench
);
6784 retval
= target_read_memory(target
, wa
->address
+ offset
, size
, count
,
6785 read_buf
+ size
+ host_offset
);
6787 duration_measure(&bench
);
6789 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6790 command_print(CMD
, "Unsupported alignment");
6792 } else if (retval
!= ERROR_OK
) {
6793 command_print(CMD
, "Memory read failed");
6797 /* replay on host */
6798 memcpy(read_ref
+ size
+ host_offset
, test_pattern
+ offset
, count
* size
);
6801 int result
= memcmp(read_ref
, read_buf
, host_bufsiz
);
6803 command_print(CMD
, "Pass in %fs (%0.3f KiB/s)",
6804 duration_elapsed(&bench
),
6805 duration_kbps(&bench
, count
* size
));
6807 command_print(CMD
, "Compare failed");
6808 binprint(CMD
, "ref:", read_ref
, host_bufsiz
);
6809 binprint(CMD
, "buf:", read_buf
, host_bufsiz
);
6821 target_free_working_area(target
, wa
);
6824 num_bytes
= test_size
+ 4 + 4 + 4;
6826 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6827 if (retval
!= ERROR_OK
) {
6828 LOG_ERROR("Not enough working area");
6832 test_pattern
= malloc(num_bytes
);
6834 for (size_t i
= 0; i
< num_bytes
; i
++)
6835 test_pattern
[i
] = rand();
6837 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6838 for (int size
= 1; size
<= 4; size
*= 2) {
6839 for (int offset
= 0; offset
< 4; offset
++) {
6840 uint32_t count
= test_size
/ size
;
6841 size_t host_bufsiz
= count
* size
+ host_offset
;
6842 uint8_t *read_ref
= malloc(num_bytes
);
6843 uint8_t *read_buf
= malloc(num_bytes
);
6844 uint8_t *write_buf
= malloc(host_bufsiz
);
6846 for (size_t i
= 0; i
< host_bufsiz
; i
++)
6847 write_buf
[i
] = rand();
6848 command_print_sameline(CMD
,
6849 "Test write %" PRIu32
" x %d @ %d from %saligned buffer: ", count
,
6850 size
, offset
, host_offset
? "un" : "");
6852 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6853 if (retval
!= ERROR_OK
) {
6854 command_print(CMD
, "Test pattern write failed");
6858 /* replay on host */
6859 memcpy(read_ref
, test_pattern
, num_bytes
);
6860 memcpy(read_ref
+ size
+ offset
, write_buf
+ host_offset
, count
* size
);
6862 struct duration bench
;
6863 duration_start(&bench
);
6865 retval
= target_write_memory(target
, wa
->address
+ size
+ offset
, size
, count
,
6866 write_buf
+ host_offset
);
6868 duration_measure(&bench
);
6870 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6871 command_print(CMD
, "Unsupported alignment");
6873 } else if (retval
!= ERROR_OK
) {
6874 command_print(CMD
, "Memory write failed");
6879 retval
= target_read_memory(target
, wa
->address
, 1, num_bytes
, read_buf
);
6880 if (retval
!= ERROR_OK
) {
6881 command_print(CMD
, "Test pattern write failed");
6886 int result
= memcmp(read_ref
, read_buf
, num_bytes
);
6888 command_print(CMD
, "Pass in %fs (%0.3f KiB/s)",
6889 duration_elapsed(&bench
),
6890 duration_kbps(&bench
, count
* size
));
6892 command_print(CMD
, "Compare failed");
6893 binprint(CMD
, "ref:", read_ref
, num_bytes
);
6894 binprint(CMD
, "buf:", read_buf
, num_bytes
);
6905 target_free_working_area(target
, wa
);
6909 static const struct command_registration target_exec_command_handlers
[] = {
6911 .name
= "fast_load_image",
6912 .handler
= handle_fast_load_image_command
,
6913 .mode
= COMMAND_ANY
,
6914 .help
= "Load image into server memory for later use by "
6915 "fast_load; primarily for profiling",
6916 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6917 "[min_address [max_length]]",
6920 .name
= "fast_load",
6921 .handler
= handle_fast_load_command
,
6922 .mode
= COMMAND_EXEC
,
6923 .help
= "loads active fast load image to current target "
6924 "- mainly for profiling purposes",
6929 .handler
= handle_profile_command
,
6930 .mode
= COMMAND_EXEC
,
6931 .usage
= "seconds filename [start end]",
6932 .help
= "profiling samples the CPU PC",
6934 /** @todo don't register virt2phys() unless target supports it */
6936 .name
= "virt2phys",
6937 .handler
= handle_virt2phys_command
,
6938 .mode
= COMMAND_ANY
,
6939 .help
= "translate a virtual address into a physical address",
6940 .usage
= "virtual_address",
6944 .handler
= handle_reg_command
,
6945 .mode
= COMMAND_EXEC
,
6946 .help
= "display (reread from target with \"force\") or set a register; "
6947 "with no arguments, displays all registers and their values",
6948 .usage
= "[(register_number|register_name) [(value|'force')]]",
6952 .handler
= handle_poll_command
,
6953 .mode
= COMMAND_EXEC
,
6954 .help
= "poll target state; or reconfigure background polling",
6955 .usage
= "['on'|'off']",
6958 .name
= "wait_halt",
6959 .handler
= handle_wait_halt_command
,
6960 .mode
= COMMAND_EXEC
,
6961 .help
= "wait up to the specified number of milliseconds "
6962 "(default 5000) for a previously requested halt",
6963 .usage
= "[milliseconds]",
6967 .handler
= handle_halt_command
,
6968 .mode
= COMMAND_EXEC
,
6969 .help
= "request target to halt, then wait up to the specified "
6970 "number of milliseconds (default 5000) for it to complete",
6971 .usage
= "[milliseconds]",
6975 .handler
= handle_resume_command
,
6976 .mode
= COMMAND_EXEC
,
6977 .help
= "resume target execution from current PC or address",
6978 .usage
= "[address]",
6982 .handler
= handle_reset_command
,
6983 .mode
= COMMAND_EXEC
,
6984 .usage
= "[run|halt|init]",
6985 .help
= "Reset all targets into the specified mode. "
6986 "Default reset mode is run, if not given.",
6989 .name
= "soft_reset_halt",
6990 .handler
= handle_soft_reset_halt_command
,
6991 .mode
= COMMAND_EXEC
,
6993 .help
= "halt the target and do a soft reset",
6997 .handler
= handle_step_command
,
6998 .mode
= COMMAND_EXEC
,
6999 .help
= "step one instruction from current PC or address",
7000 .usage
= "[address]",
7004 .handler
= handle_md_command
,
7005 .mode
= COMMAND_EXEC
,
7006 .help
= "display memory double-words",
7007 .usage
= "['phys'] address [count]",
7011 .handler
= handle_md_command
,
7012 .mode
= COMMAND_EXEC
,
7013 .help
= "display memory words",
7014 .usage
= "['phys'] address [count]",
7018 .handler
= handle_md_command
,
7019 .mode
= COMMAND_EXEC
,
7020 .help
= "display memory half-words",
7021 .usage
= "['phys'] address [count]",
7025 .handler
= handle_md_command
,
7026 .mode
= COMMAND_EXEC
,
7027 .help
= "display memory bytes",
7028 .usage
= "['phys'] address [count]",
7032 .handler
= handle_mw_command
,
7033 .mode
= COMMAND_EXEC
,
7034 .help
= "write memory double-word",
7035 .usage
= "['phys'] address value [count]",
7039 .handler
= handle_mw_command
,
7040 .mode
= COMMAND_EXEC
,
7041 .help
= "write memory word",
7042 .usage
= "['phys'] address value [count]",
7046 .handler
= handle_mw_command
,
7047 .mode
= COMMAND_EXEC
,
7048 .help
= "write memory half-word",
7049 .usage
= "['phys'] address value [count]",
7053 .handler
= handle_mw_command
,
7054 .mode
= COMMAND_EXEC
,
7055 .help
= "write memory byte",
7056 .usage
= "['phys'] address value [count]",
7060 .handler
= handle_bp_command
,
7061 .mode
= COMMAND_EXEC
,
7062 .help
= "list or set hardware or software breakpoint",
7063 .usage
= "[<address> [<asid>] <length> ['hw'|'hw_ctx']]",
7067 .handler
= handle_rbp_command
,
7068 .mode
= COMMAND_EXEC
,
7069 .help
= "remove breakpoint",
7070 .usage
= "'all' | address",
7074 .handler
= handle_wp_command
,
7075 .mode
= COMMAND_EXEC
,
7076 .help
= "list (no params) or create watchpoints",
7077 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
7081 .handler
= handle_rwp_command
,
7082 .mode
= COMMAND_EXEC
,
7083 .help
= "remove watchpoint",
7084 .usage
= "'all' | address",
7087 .name
= "load_image",
7088 .handler
= handle_load_image_command
,
7089 .mode
= COMMAND_EXEC
,
7090 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
7091 "[min_address] [max_length]",
7094 .name
= "dump_image",
7095 .handler
= handle_dump_image_command
,
7096 .mode
= COMMAND_EXEC
,
7097 .usage
= "filename address size",
7100 .name
= "verify_image_checksum",
7101 .handler
= handle_verify_image_checksum_command
,
7102 .mode
= COMMAND_EXEC
,
7103 .usage
= "filename [offset [type]]",
7106 .name
= "verify_image",
7107 .handler
= handle_verify_image_command
,
7108 .mode
= COMMAND_EXEC
,
7109 .usage
= "filename [offset [type]]",
7112 .name
= "test_image",
7113 .handler
= handle_test_image_command
,
7114 .mode
= COMMAND_EXEC
,
7115 .usage
= "filename [offset [type]]",
7119 .mode
= COMMAND_EXEC
,
7120 .jim_handler
= target_jim_get_reg
,
7121 .help
= "Get register values from the target",
7126 .mode
= COMMAND_EXEC
,
7127 .jim_handler
= target_jim_set_reg
,
7128 .help
= "Set target register values",
7132 .name
= "read_memory",
7133 .mode
= COMMAND_EXEC
,
7134 .handler
= handle_target_read_memory
,
7135 .help
= "Read Tcl list of 8/16/32/64 bit numbers from target memory",
7136 .usage
= "address width count ['phys']",
7139 .name
= "write_memory",
7140 .mode
= COMMAND_EXEC
,
7141 .jim_handler
= target_jim_write_memory
,
7142 .help
= "Write Tcl list of 8/16/32/64 bit numbers to target memory",
7143 .usage
= "address width data ['phys']",
7146 .name
= "reset_nag",
7147 .handler
= handle_target_reset_nag
,
7148 .mode
= COMMAND_ANY
,
7149 .help
= "Nag after each reset about options that could have been "
7150 "enabled to improve performance.",
7151 .usage
= "['enable'|'disable']",
7155 .handler
= handle_ps_command
,
7156 .mode
= COMMAND_EXEC
,
7157 .help
= "list all tasks",
7161 .name
= "test_mem_access",
7162 .handler
= handle_test_mem_access_command
,
7163 .mode
= COMMAND_EXEC
,
7164 .help
= "Test the target's memory access functions",
7168 COMMAND_REGISTRATION_DONE
7170 static int target_register_user_commands(struct command_context
*cmd_ctx
)
7172 int retval
= ERROR_OK
;
7173 retval
= target_request_register_commands(cmd_ctx
);
7174 if (retval
!= ERROR_OK
)
7177 retval
= trace_register_commands(cmd_ctx
);
7178 if (retval
!= ERROR_OK
)
7182 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);
7185 const char *target_debug_reason_str(enum target_debug_reason reason
)
7188 case DBG_REASON_DBGRQ
:
7190 case DBG_REASON_BREAKPOINT
:
7191 return "BREAKPOINT";
7192 case DBG_REASON_WATCHPOINT
:
7193 return "WATCHPOINT";
7194 case DBG_REASON_WPTANDBKPT
:
7195 return "WPTANDBKPT";
7196 case DBG_REASON_SINGLESTEP
:
7197 return "SINGLESTEP";
7198 case DBG_REASON_NOTHALTED
:
7200 case DBG_REASON_EXIT
:
7202 case DBG_REASON_EXC_CATCH
:
7204 case DBG_REASON_UNDEFINED
: