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 static void append_to_list_all_targets(struct target
*target
)
303 struct target
**t
= &all_targets
;
310 /* read a uint64_t from a buffer in target memory endianness */
311 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
313 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
314 return le_to_h_u64(buffer
);
316 return be_to_h_u64(buffer
);
319 /* read a uint32_t from a buffer in target memory endianness */
320 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
322 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
323 return le_to_h_u32(buffer
);
325 return be_to_h_u32(buffer
);
328 /* read a uint24_t from a buffer in target memory endianness */
329 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
331 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
332 return le_to_h_u24(buffer
);
334 return be_to_h_u24(buffer
);
337 /* read a uint16_t from a buffer in target memory endianness */
338 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
340 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
341 return le_to_h_u16(buffer
);
343 return be_to_h_u16(buffer
);
346 /* write a uint64_t to a buffer in target memory endianness */
347 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
349 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
350 h_u64_to_le(buffer
, value
);
352 h_u64_to_be(buffer
, value
);
355 /* write a uint32_t to a buffer in target memory endianness */
356 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
358 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
359 h_u32_to_le(buffer
, value
);
361 h_u32_to_be(buffer
, value
);
364 /* write a uint24_t to a buffer in target memory endianness */
365 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
367 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
368 h_u24_to_le(buffer
, value
);
370 h_u24_to_be(buffer
, value
);
373 /* write a uint16_t to a buffer in target memory endianness */
374 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
376 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
377 h_u16_to_le(buffer
, value
);
379 h_u16_to_be(buffer
, value
);
382 /* write a uint8_t to a buffer in target memory endianness */
383 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
388 /* write a uint64_t array to a buffer in target memory endianness */
389 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
392 for (i
= 0; i
< count
; i
++)
393 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
396 /* write a uint32_t array to a buffer in target memory endianness */
397 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
400 for (i
= 0; i
< count
; i
++)
401 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
404 /* write a uint16_t array to a buffer in target memory endianness */
405 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
408 for (i
= 0; i
< count
; i
++)
409 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
412 /* write a uint64_t array to a buffer in target memory endianness */
413 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
416 for (i
= 0; i
< count
; i
++)
417 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
420 /* write a uint32_t array to a buffer in target memory endianness */
421 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
424 for (i
= 0; i
< count
; i
++)
425 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
428 /* write a uint16_t array to a buffer in target memory endianness */
429 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
432 for (i
= 0; i
< count
; i
++)
433 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
436 /* return a pointer to a configured target; id is name or index in all_targets */
437 struct target
*get_target(const char *id
)
439 struct target
*target
;
441 /* try as tcltarget name */
442 for (target
= all_targets
; target
; target
= target
->next
) {
443 if (!target_name(target
))
445 if (strcmp(id
, target_name(target
)) == 0)
450 unsigned int index
, counter
;
451 if (parse_uint(id
, &index
) != ERROR_OK
)
454 for (target
= all_targets
, counter
= index
;
456 target
= target
->next
, --counter
)
462 struct target
*get_current_target(struct command_context
*cmd_ctx
)
464 struct target
*target
= get_current_target_or_null(cmd_ctx
);
467 LOG_ERROR("BUG: current_target out of bounds");
474 struct target
*get_current_target_or_null(struct command_context
*cmd_ctx
)
476 return cmd_ctx
->current_target_override
477 ? cmd_ctx
->current_target_override
478 : cmd_ctx
->current_target
;
481 int target_poll(struct target
*target
)
485 /* We can't poll until after examine */
486 if (!target_was_examined(target
)) {
487 /* Fail silently lest we pollute the log */
491 retval
= target
->type
->poll(target
);
492 if (retval
!= ERROR_OK
)
495 if (target
->halt_issued
) {
496 if (target
->state
== TARGET_HALTED
)
497 target
->halt_issued
= false;
499 int64_t t
= timeval_ms() - target
->halt_issued_time
;
500 if (t
> DEFAULT_HALT_TIMEOUT
) {
501 target
->halt_issued
= false;
502 LOG_INFO("Halt timed out, wake up GDB.");
503 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
511 int target_halt(struct target
*target
)
514 /* We can't poll until after examine */
515 if (!target_was_examined(target
)) {
516 LOG_ERROR("Target not examined yet");
520 retval
= target
->type
->halt(target
);
521 if (retval
!= ERROR_OK
)
524 target
->halt_issued
= true;
525 target
->halt_issued_time
= timeval_ms();
531 * Make the target (re)start executing using its saved execution
532 * context (possibly with some modifications).
534 * @param target Which target should start executing.
535 * @param current True to use the target's saved program counter instead
536 * of the address parameter
537 * @param address Optionally used as the program counter.
538 * @param handle_breakpoints True iff breakpoints at the resumption PC
539 * should be skipped. (For example, maybe execution was stopped by
540 * such a breakpoint, in which case it would be counterproductive to
542 * @param debug_execution False if all working areas allocated by OpenOCD
543 * should be released and/or restored to their original contents.
544 * (This would for example be true to run some downloaded "helper"
545 * algorithm code, which resides in one such working buffer and uses
546 * another for data storage.)
548 * @todo Resolve the ambiguity about what the "debug_execution" flag
549 * signifies. For example, Target implementations don't agree on how
550 * it relates to invalidation of the register cache, or to whether
551 * breakpoints and watchpoints should be enabled. (It would seem wrong
552 * to enable breakpoints when running downloaded "helper" algorithms
553 * (debug_execution true), since the breakpoints would be set to match
554 * target firmware being debugged, not the helper algorithm.... and
555 * enabling them could cause such helpers to malfunction (for example,
556 * by overwriting data with a breakpoint instruction. On the other
557 * hand the infrastructure for running such helpers might use this
558 * procedure but rely on hardware breakpoint to detect termination.)
560 int target_resume(struct target
*target
, int current
, target_addr_t address
,
561 int handle_breakpoints
, int debug_execution
)
565 /* We can't poll until after examine */
566 if (!target_was_examined(target
)) {
567 LOG_ERROR("Target not examined yet");
571 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
573 /* note that resume *must* be asynchronous. The CPU can halt before
574 * we poll. The CPU can even halt at the current PC as a result of
575 * a software breakpoint being inserted by (a bug?) the application.
578 * resume() triggers the event 'resumed'. The execution of TCL commands
579 * in the event handler causes the polling of targets. If the target has
580 * already halted for a breakpoint, polling will run the 'halted' event
581 * handler before the pending 'resumed' handler.
582 * Disable polling during resume() to guarantee the execution of handlers
583 * in the correct order.
585 bool save_poll_mask
= jtag_poll_mask();
586 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
587 jtag_poll_unmask(save_poll_mask
);
589 if (retval
!= ERROR_OK
)
592 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
597 static int target_process_reset(struct command_invocation
*cmd
, enum target_reset_mode reset_mode
)
602 n
= nvp_value2name(nvp_reset_modes
, reset_mode
);
604 LOG_ERROR("invalid reset mode");
608 struct target
*target
;
609 for (target
= all_targets
; target
; target
= target
->next
)
610 target_call_reset_callbacks(target
, reset_mode
);
612 /* disable polling during reset to make reset event scripts
613 * more predictable, i.e. dr/irscan & pathmove in events will
614 * not have JTAG operations injected into the middle of a sequence.
616 bool save_poll_mask
= jtag_poll_mask();
618 sprintf(buf
, "ocd_process_reset %s", n
->name
);
619 retval
= Jim_Eval(cmd
->ctx
->interp
, buf
);
621 jtag_poll_unmask(save_poll_mask
);
623 if (retval
!= JIM_OK
) {
624 Jim_MakeErrorMessage(cmd
->ctx
->interp
);
625 command_print(cmd
, "%s", Jim_GetString(Jim_GetResult(cmd
->ctx
->interp
), NULL
));
629 /* We want any events to be processed before the prompt */
630 retval
= target_call_timer_callbacks_now();
632 for (target
= all_targets
; target
; target
= target
->next
) {
633 target
->type
->check_reset(target
);
634 target
->running_alg
= false;
640 static int identity_virt2phys(struct target
*target
,
641 target_addr_t
virtual, target_addr_t
*physical
)
647 static int no_mmu(struct target
*target
, int *enabled
)
654 * Reset the @c examined flag for the given target.
655 * Pure paranoia -- targets are zeroed on allocation.
657 static inline void target_reset_examined(struct target
*target
)
659 target
->examined
= false;
662 static int default_examine(struct target
*target
)
664 target_set_examined(target
);
668 /* no check by default */
669 static int default_check_reset(struct target
*target
)
674 /* Equivalent Tcl code arp_examine_one is in src/target/startup.tcl
676 int target_examine_one(struct target
*target
)
678 LOG_TARGET_DEBUG(target
, "Examination started");
680 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
682 int retval
= target
->type
->examine(target
);
683 if (retval
!= ERROR_OK
) {
684 LOG_TARGET_ERROR(target
, "Examination failed");
685 LOG_TARGET_DEBUG(target
, "examine() returned error code %d", retval
);
686 target_reset_examined(target
);
687 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_FAIL
);
691 target_set_examined(target
);
692 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
694 LOG_TARGET_INFO(target
, "Examination succeed");
698 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
700 struct target
*target
= priv
;
702 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
705 jtag_unregister_event_callback(jtag_enable_callback
, target
);
707 return target_examine_one(target
);
710 /* Targets that correctly implement init + examine, i.e.
711 * no communication with target during init:
715 int target_examine(void)
717 int retval
= ERROR_OK
;
718 struct target
*target
;
720 for (target
= all_targets
; target
; target
= target
->next
) {
721 /* defer examination, but don't skip it */
722 if (!target
->tap
->enabled
) {
723 jtag_register_event_callback(jtag_enable_callback
,
728 if (target
->defer_examine
)
731 int retval2
= target_examine_one(target
);
732 if (retval2
!= ERROR_OK
) {
733 LOG_WARNING("target %s examination failed", target_name(target
));
740 const char *target_type_name(struct target
*target
)
742 return target
->type
->name
;
745 static int target_soft_reset_halt(struct target
*target
)
747 if (!target_was_examined(target
)) {
748 LOG_ERROR("Target not examined yet");
751 if (!target
->type
->soft_reset_halt
) {
752 LOG_ERROR("Target %s does not support soft_reset_halt",
753 target_name(target
));
756 return target
->type
->soft_reset_halt(target
);
760 * Downloads a target-specific native code algorithm to the target,
761 * and executes it. * Note that some targets may need to set up, enable,
762 * and tear down a breakpoint (hard or * soft) to detect algorithm
763 * termination, while others may support lower overhead schemes where
764 * soft breakpoints embedded in the algorithm automatically terminate the
767 * @param target used to run the algorithm
768 * @param num_mem_params
770 * @param num_reg_params
775 * @param arch_info target-specific description of the algorithm.
777 int target_run_algorithm(struct target
*target
,
778 int num_mem_params
, struct mem_param
*mem_params
,
779 int num_reg_params
, struct reg_param
*reg_param
,
780 target_addr_t entry_point
, target_addr_t exit_point
,
781 unsigned int timeout_ms
, void *arch_info
)
783 int retval
= ERROR_FAIL
;
785 if (!target_was_examined(target
)) {
786 LOG_ERROR("Target not examined yet");
789 if (!target
->type
->run_algorithm
) {
790 LOG_ERROR("Target type '%s' does not support %s",
791 target_type_name(target
), __func__
);
795 target
->running_alg
= true;
796 retval
= target
->type
->run_algorithm(target
,
797 num_mem_params
, mem_params
,
798 num_reg_params
, reg_param
,
799 entry_point
, exit_point
, timeout_ms
, arch_info
);
800 target
->running_alg
= false;
807 * Executes a target-specific native code algorithm and leaves it running.
809 * @param target used to run the algorithm
810 * @param num_mem_params
812 * @param num_reg_params
816 * @param arch_info target-specific description of the algorithm.
818 int target_start_algorithm(struct target
*target
,
819 int num_mem_params
, struct mem_param
*mem_params
,
820 int num_reg_params
, struct reg_param
*reg_params
,
821 target_addr_t entry_point
, target_addr_t exit_point
,
824 int retval
= ERROR_FAIL
;
826 if (!target_was_examined(target
)) {
827 LOG_ERROR("Target not examined yet");
830 if (!target
->type
->start_algorithm
) {
831 LOG_ERROR("Target type '%s' does not support %s",
832 target_type_name(target
), __func__
);
835 if (target
->running_alg
) {
836 LOG_ERROR("Target is already running an algorithm");
840 target
->running_alg
= true;
841 retval
= target
->type
->start_algorithm(target
,
842 num_mem_params
, mem_params
,
843 num_reg_params
, reg_params
,
844 entry_point
, exit_point
, arch_info
);
851 * Waits for an algorithm started with target_start_algorithm() to complete.
853 * @param target used to run the algorithm
854 * @param num_mem_params
856 * @param num_reg_params
860 * @param arch_info target-specific description of the algorithm.
862 int target_wait_algorithm(struct target
*target
,
863 int num_mem_params
, struct mem_param
*mem_params
,
864 int num_reg_params
, struct reg_param
*reg_params
,
865 target_addr_t exit_point
, unsigned int timeout_ms
,
868 int retval
= ERROR_FAIL
;
870 if (!target
->type
->wait_algorithm
) {
871 LOG_ERROR("Target type '%s' does not support %s",
872 target_type_name(target
), __func__
);
875 if (!target
->running_alg
) {
876 LOG_ERROR("Target is not running an algorithm");
880 retval
= target
->type
->wait_algorithm(target
,
881 num_mem_params
, mem_params
,
882 num_reg_params
, reg_params
,
883 exit_point
, timeout_ms
, arch_info
);
884 if (retval
!= ERROR_TARGET_TIMEOUT
)
885 target
->running_alg
= false;
892 * Streams data to a circular buffer on target intended for consumption by code
893 * running asynchronously on target.
895 * This is intended for applications where target-specific native code runs
896 * on the target, receives data from the circular buffer, does something with
897 * it (most likely writing it to a flash memory), and advances the circular
900 * This assumes that the helper algorithm has already been loaded to the target,
901 * but has not been started yet. Given memory and register parameters are passed
904 * The buffer is defined by (buffer_start, buffer_size) arguments and has the
907 * [buffer_start + 0, buffer_start + 4):
908 * Write Pointer address (aka head). Written and updated by this
909 * routine when new data is written to the circular buffer.
910 * [buffer_start + 4, buffer_start + 8):
911 * Read Pointer address (aka tail). Updated by code running on the
912 * target after it consumes data.
913 * [buffer_start + 8, buffer_start + buffer_size):
914 * Circular buffer contents.
916 * See contrib/loaders/flash/stm32f1x.S for an example.
918 * @param target used to run the algorithm
919 * @param buffer address on the host where data to be sent is located
920 * @param count number of blocks to send
921 * @param block_size size in bytes of each block
922 * @param num_mem_params count of memory-based params to pass to algorithm
923 * @param mem_params memory-based params to pass to algorithm
924 * @param num_reg_params count of register-based params to pass to algorithm
925 * @param reg_params memory-based params to pass to algorithm
926 * @param buffer_start address on the target of the circular buffer structure
927 * @param buffer_size size of the circular buffer structure
928 * @param entry_point address on the target to execute to start the algorithm
929 * @param exit_point address at which to set a breakpoint to catch the
930 * end of the algorithm; can be 0 if target triggers a breakpoint itself
934 int target_run_flash_async_algorithm(struct target
*target
,
935 const uint8_t *buffer
, uint32_t count
, int block_size
,
936 int num_mem_params
, struct mem_param
*mem_params
,
937 int num_reg_params
, struct reg_param
*reg_params
,
938 uint32_t buffer_start
, uint32_t buffer_size
,
939 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
944 const uint8_t *buffer_orig
= buffer
;
946 /* Set up working area. First word is write pointer, second word is read pointer,
947 * rest is fifo data area. */
948 uint32_t wp_addr
= buffer_start
;
949 uint32_t rp_addr
= buffer_start
+ 4;
950 uint32_t fifo_start_addr
= buffer_start
+ 8;
951 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
953 uint32_t wp
= fifo_start_addr
;
954 uint32_t rp
= fifo_start_addr
;
956 /* validate block_size is 2^n */
957 assert(IS_PWR_OF_2(block_size
));
959 retval
= target_write_u32(target
, wp_addr
, wp
);
960 if (retval
!= ERROR_OK
)
962 retval
= target_write_u32(target
, rp_addr
, rp
);
963 if (retval
!= ERROR_OK
)
966 /* Start up algorithm on target and let it idle while writing the first chunk */
967 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
968 num_reg_params
, reg_params
,
973 if (retval
!= ERROR_OK
) {
974 LOG_ERROR("error starting target flash write algorithm");
980 retval
= target_read_u32(target
, rp_addr
, &rp
);
981 if (retval
!= ERROR_OK
) {
982 LOG_ERROR("failed to get read pointer");
986 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
987 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
990 LOG_ERROR("flash write algorithm aborted by target");
991 retval
= ERROR_FLASH_OPERATION_FAILED
;
995 if (!IS_ALIGNED(rp
- fifo_start_addr
, block_size
) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
996 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
1000 /* Count the number of bytes available in the fifo without
1001 * crossing the wrap around. Make sure to not fill it completely,
1002 * because that would make wp == rp and that's the empty condition. */
1003 uint32_t thisrun_bytes
;
1005 thisrun_bytes
= rp
- wp
- block_size
;
1006 else if (rp
> fifo_start_addr
)
1007 thisrun_bytes
= fifo_end_addr
- wp
;
1009 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
1011 if (thisrun_bytes
== 0) {
1012 /* Throttle polling a bit if transfer is (much) faster than flash
1013 * programming. The exact delay shouldn't matter as long as it's
1014 * less than buffer size / flash speed. This is very unlikely to
1015 * run when using high latency connections such as USB. */
1018 /* to stop an infinite loop on some targets check and increment a timeout
1019 * this issue was observed on a stellaris using the new ICDI interface */
1020 if (timeout
++ >= 2500) {
1021 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1022 return ERROR_FLASH_OPERATION_FAILED
;
1027 /* reset our timeout */
1030 /* Limit to the amount of data we actually want to write */
1031 if (thisrun_bytes
> count
* block_size
)
1032 thisrun_bytes
= count
* block_size
;
1034 /* Force end of large blocks to be word aligned */
1035 if (thisrun_bytes
>= 16)
1036 thisrun_bytes
-= (rp
+ thisrun_bytes
) & 0x03;
1038 /* Write data to fifo */
1039 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
1040 if (retval
!= ERROR_OK
)
1043 /* Update counters and wrap write pointer */
1044 buffer
+= thisrun_bytes
;
1045 count
-= thisrun_bytes
/ block_size
;
1046 wp
+= thisrun_bytes
;
1047 if (wp
>= fifo_end_addr
)
1048 wp
= fifo_start_addr
;
1050 /* Store updated write pointer to target */
1051 retval
= target_write_u32(target
, wp_addr
, wp
);
1052 if (retval
!= ERROR_OK
)
1055 /* Avoid GDB timeouts */
1059 if (retval
!= ERROR_OK
) {
1060 /* abort flash write algorithm on target */
1061 target_write_u32(target
, wp_addr
, 0);
1064 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1065 num_reg_params
, reg_params
,
1070 if (retval2
!= ERROR_OK
) {
1071 LOG_ERROR("error waiting for target flash write algorithm");
1075 if (retval
== ERROR_OK
) {
1076 /* check if algorithm set rp = 0 after fifo writer loop finished */
1077 retval
= target_read_u32(target
, rp_addr
, &rp
);
1078 if (retval
== ERROR_OK
&& rp
== 0) {
1079 LOG_ERROR("flash write algorithm aborted by target");
1080 retval
= ERROR_FLASH_OPERATION_FAILED
;
1087 int target_run_read_async_algorithm(struct target
*target
,
1088 uint8_t *buffer
, uint32_t count
, int block_size
,
1089 int num_mem_params
, struct mem_param
*mem_params
,
1090 int num_reg_params
, struct reg_param
*reg_params
,
1091 uint32_t buffer_start
, uint32_t buffer_size
,
1092 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
1097 const uint8_t *buffer_orig
= buffer
;
1099 /* Set up working area. First word is write pointer, second word is read pointer,
1100 * rest is fifo data area. */
1101 uint32_t wp_addr
= buffer_start
;
1102 uint32_t rp_addr
= buffer_start
+ 4;
1103 uint32_t fifo_start_addr
= buffer_start
+ 8;
1104 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
1106 uint32_t wp
= fifo_start_addr
;
1107 uint32_t rp
= fifo_start_addr
;
1109 /* validate block_size is 2^n */
1110 assert(IS_PWR_OF_2(block_size
));
1112 retval
= target_write_u32(target
, wp_addr
, wp
);
1113 if (retval
!= ERROR_OK
)
1115 retval
= target_write_u32(target
, rp_addr
, rp
);
1116 if (retval
!= ERROR_OK
)
1119 /* Start up algorithm on target */
1120 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
1121 num_reg_params
, reg_params
,
1126 if (retval
!= ERROR_OK
) {
1127 LOG_ERROR("error starting target flash read algorithm");
1132 retval
= target_read_u32(target
, wp_addr
, &wp
);
1133 if (retval
!= ERROR_OK
) {
1134 LOG_ERROR("failed to get write pointer");
1138 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
1139 (size_t)(buffer
- buffer_orig
), count
, wp
, rp
);
1142 LOG_ERROR("flash read algorithm aborted by target");
1143 retval
= ERROR_FLASH_OPERATION_FAILED
;
1147 if (!IS_ALIGNED(wp
- fifo_start_addr
, block_size
) || wp
< fifo_start_addr
|| wp
>= fifo_end_addr
) {
1148 LOG_ERROR("corrupted fifo write pointer 0x%" PRIx32
, wp
);
1152 /* Count the number of bytes available in the fifo without
1153 * crossing the wrap around. */
1154 uint32_t thisrun_bytes
;
1156 thisrun_bytes
= wp
- rp
;
1158 thisrun_bytes
= fifo_end_addr
- rp
;
1160 if (thisrun_bytes
== 0) {
1161 /* Throttle polling a bit if transfer is (much) faster than flash
1162 * reading. The exact delay shouldn't matter as long as it's
1163 * less than buffer size / flash speed. This is very unlikely to
1164 * run when using high latency connections such as USB. */
1167 /* to stop an infinite loop on some targets check and increment a timeout
1168 * this issue was observed on a stellaris using the new ICDI interface */
1169 if (timeout
++ >= 2500) {
1170 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1171 return ERROR_FLASH_OPERATION_FAILED
;
1176 /* Reset our timeout */
1179 /* Limit to the amount of data we actually want to read */
1180 if (thisrun_bytes
> count
* block_size
)
1181 thisrun_bytes
= count
* block_size
;
1183 /* Force end of large blocks to be word aligned */
1184 if (thisrun_bytes
>= 16)
1185 thisrun_bytes
-= (rp
+ thisrun_bytes
) & 0x03;
1187 /* Read data from fifo */
1188 retval
= target_read_buffer(target
, rp
, thisrun_bytes
, buffer
);
1189 if (retval
!= ERROR_OK
)
1192 /* Update counters and wrap write pointer */
1193 buffer
+= thisrun_bytes
;
1194 count
-= thisrun_bytes
/ block_size
;
1195 rp
+= thisrun_bytes
;
1196 if (rp
>= fifo_end_addr
)
1197 rp
= fifo_start_addr
;
1199 /* Store updated write pointer to target */
1200 retval
= target_write_u32(target
, rp_addr
, rp
);
1201 if (retval
!= ERROR_OK
)
1204 /* Avoid GDB timeouts */
1207 if (openocd_is_shutdown_pending()) {
1208 retval
= ERROR_SERVER_INTERRUPTED
;
1213 if (retval
!= ERROR_OK
) {
1214 /* abort flash write algorithm on target */
1215 target_write_u32(target
, rp_addr
, 0);
1218 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1219 num_reg_params
, reg_params
,
1224 if (retval2
!= ERROR_OK
) {
1225 LOG_ERROR("error waiting for target flash write algorithm");
1229 if (retval
== ERROR_OK
) {
1230 /* check if algorithm set wp = 0 after fifo writer loop finished */
1231 retval
= target_read_u32(target
, wp_addr
, &wp
);
1232 if (retval
== ERROR_OK
&& wp
== 0) {
1233 LOG_ERROR("flash read algorithm aborted by target");
1234 retval
= ERROR_FLASH_OPERATION_FAILED
;
1241 int target_read_memory(struct target
*target
,
1242 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1244 if (!target_was_examined(target
)) {
1245 LOG_ERROR("Target not examined yet");
1248 if (!target
->type
->read_memory
) {
1249 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1252 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1255 int target_read_phys_memory(struct target
*target
,
1256 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1258 if (!target_was_examined(target
)) {
1259 LOG_ERROR("Target not examined yet");
1262 if (!target
->type
->read_phys_memory
) {
1263 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1266 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1269 int target_write_memory(struct target
*target
,
1270 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1272 if (!target_was_examined(target
)) {
1273 LOG_ERROR("Target not examined yet");
1276 if (!target
->type
->write_memory
) {
1277 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1280 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1283 int target_write_phys_memory(struct target
*target
,
1284 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1286 if (!target_was_examined(target
)) {
1287 LOG_ERROR("Target not examined yet");
1290 if (!target
->type
->write_phys_memory
) {
1291 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1294 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1297 int target_add_breakpoint(struct target
*target
,
1298 struct breakpoint
*breakpoint
)
1300 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1301 LOG_TARGET_ERROR(target
, "not halted (add breakpoint)");
1302 return ERROR_TARGET_NOT_HALTED
;
1304 return target
->type
->add_breakpoint(target
, breakpoint
);
1307 int target_add_context_breakpoint(struct target
*target
,
1308 struct breakpoint
*breakpoint
)
1310 if (target
->state
!= TARGET_HALTED
) {
1311 LOG_TARGET_ERROR(target
, "not halted (add context breakpoint)");
1312 return ERROR_TARGET_NOT_HALTED
;
1314 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1317 int target_add_hybrid_breakpoint(struct target
*target
,
1318 struct breakpoint
*breakpoint
)
1320 if (target
->state
!= TARGET_HALTED
) {
1321 LOG_TARGET_ERROR(target
, "not halted (add hybrid breakpoint)");
1322 return ERROR_TARGET_NOT_HALTED
;
1324 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1327 int target_remove_breakpoint(struct target
*target
,
1328 struct breakpoint
*breakpoint
)
1330 return target
->type
->remove_breakpoint(target
, breakpoint
);
1333 int target_add_watchpoint(struct target
*target
,
1334 struct watchpoint
*watchpoint
)
1336 if (target
->state
!= TARGET_HALTED
) {
1337 LOG_TARGET_ERROR(target
, "not halted (add watchpoint)");
1338 return ERROR_TARGET_NOT_HALTED
;
1340 return target
->type
->add_watchpoint(target
, watchpoint
);
1342 int target_remove_watchpoint(struct target
*target
,
1343 struct watchpoint
*watchpoint
)
1345 return target
->type
->remove_watchpoint(target
, watchpoint
);
1347 int target_hit_watchpoint(struct target
*target
,
1348 struct watchpoint
**hit_watchpoint
)
1350 if (target
->state
!= TARGET_HALTED
) {
1351 LOG_TARGET_ERROR(target
, "not halted (hit watchpoint)");
1352 return ERROR_TARGET_NOT_HALTED
;
1355 if (!target
->type
->hit_watchpoint
) {
1356 /* For backward compatible, if hit_watchpoint is not implemented,
1357 * return ERROR_FAIL such that gdb_server will not take the nonsense
1362 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1365 const char *target_get_gdb_arch(struct target
*target
)
1367 if (!target
->type
->get_gdb_arch
)
1369 return target
->type
->get_gdb_arch(target
);
1372 int target_get_gdb_reg_list(struct target
*target
,
1373 struct reg
**reg_list
[], int *reg_list_size
,
1374 enum target_register_class reg_class
)
1376 int result
= ERROR_FAIL
;
1378 if (!target_was_examined(target
)) {
1379 LOG_ERROR("Target not examined yet");
1383 result
= target
->type
->get_gdb_reg_list(target
, reg_list
,
1384 reg_list_size
, reg_class
);
1387 if (result
!= ERROR_OK
) {
1394 int target_get_gdb_reg_list_noread(struct target
*target
,
1395 struct reg
**reg_list
[], int *reg_list_size
,
1396 enum target_register_class reg_class
)
1398 if (target
->type
->get_gdb_reg_list_noread
&&
1399 target
->type
->get_gdb_reg_list_noread(target
, reg_list
,
1400 reg_list_size
, reg_class
) == ERROR_OK
)
1402 return target_get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1405 bool target_supports_gdb_connection(struct target
*target
)
1408 * exclude all the targets that don't provide get_gdb_reg_list
1409 * or that have explicit gdb_max_connection == 0
1411 return !!target
->type
->get_gdb_reg_list
&& !!target
->gdb_max_connections
;
1414 int target_step(struct target
*target
,
1415 int current
, target_addr_t address
, int handle_breakpoints
)
1419 target_call_event_callbacks(target
, TARGET_EVENT_STEP_START
);
1421 retval
= target
->type
->step(target
, current
, address
, handle_breakpoints
);
1422 if (retval
!= ERROR_OK
)
1425 target_call_event_callbacks(target
, TARGET_EVENT_STEP_END
);
1430 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1432 if (target
->state
!= TARGET_HALTED
) {
1433 LOG_TARGET_ERROR(target
, "not halted (gdb fileio)");
1434 return ERROR_TARGET_NOT_HALTED
;
1436 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1439 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1441 if (target
->state
!= TARGET_HALTED
) {
1442 LOG_TARGET_ERROR(target
, "not halted (gdb fileio end)");
1443 return ERROR_TARGET_NOT_HALTED
;
1445 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1448 target_addr_t
target_address_max(struct target
*target
)
1450 unsigned bits
= target_address_bits(target
);
1451 if (sizeof(target_addr_t
) * 8 == bits
)
1452 return (target_addr_t
) -1;
1454 return (((target_addr_t
) 1) << bits
) - 1;
1457 unsigned target_address_bits(struct target
*target
)
1459 if (target
->type
->address_bits
)
1460 return target
->type
->address_bits(target
);
1464 unsigned int target_data_bits(struct target
*target
)
1466 if (target
->type
->data_bits
)
1467 return target
->type
->data_bits(target
);
1471 static int target_profiling(struct target
*target
, uint32_t *samples
,
1472 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1474 return target
->type
->profiling(target
, samples
, max_num_samples
,
1475 num_samples
, seconds
);
1478 static int handle_target(void *priv
);
1480 static int target_init_one(struct command_context
*cmd_ctx
,
1481 struct target
*target
)
1483 target_reset_examined(target
);
1485 struct target_type
*type
= target
->type
;
1487 type
->examine
= default_examine
;
1489 if (!type
->check_reset
)
1490 type
->check_reset
= default_check_reset
;
1492 assert(type
->init_target
);
1494 int retval
= type
->init_target(cmd_ctx
, target
);
1495 if (retval
!= ERROR_OK
) {
1496 LOG_ERROR("target '%s' init failed", target_name(target
));
1500 /* Sanity-check MMU support ... stub in what we must, to help
1501 * implement it in stages, but warn if we need to do so.
1504 if (!type
->virt2phys
) {
1505 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1506 type
->virt2phys
= identity_virt2phys
;
1509 /* Make sure no-MMU targets all behave the same: make no
1510 * distinction between physical and virtual addresses, and
1511 * ensure that virt2phys() is always an identity mapping.
1513 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1514 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1517 type
->write_phys_memory
= type
->write_memory
;
1518 type
->read_phys_memory
= type
->read_memory
;
1519 type
->virt2phys
= identity_virt2phys
;
1522 if (!target
->type
->read_buffer
)
1523 target
->type
->read_buffer
= target_read_buffer_default
;
1525 if (!target
->type
->write_buffer
)
1526 target
->type
->write_buffer
= target_write_buffer_default
;
1528 if (!target
->type
->get_gdb_fileio_info
)
1529 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1531 if (!target
->type
->gdb_fileio_end
)
1532 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1534 if (!target
->type
->profiling
)
1535 target
->type
->profiling
= target_profiling_default
;
1540 static int target_init(struct command_context
*cmd_ctx
)
1542 struct target
*target
;
1545 for (target
= all_targets
; target
; target
= target
->next
) {
1546 retval
= target_init_one(cmd_ctx
, target
);
1547 if (retval
!= ERROR_OK
)
1554 retval
= target_register_user_commands(cmd_ctx
);
1555 if (retval
!= ERROR_OK
)
1558 retval
= target_register_timer_callback(&handle_target
,
1559 polling_interval
, TARGET_TIMER_TYPE_PERIODIC
, cmd_ctx
->interp
);
1560 if (retval
!= ERROR_OK
)
1566 COMMAND_HANDLER(handle_target_init_command
)
1571 return ERROR_COMMAND_SYNTAX_ERROR
;
1573 static bool target_initialized
;
1574 if (target_initialized
) {
1575 LOG_INFO("'target init' has already been called");
1578 target_initialized
= true;
1580 retval
= command_run_line(CMD_CTX
, "init_targets");
1581 if (retval
!= ERROR_OK
)
1584 retval
= command_run_line(CMD_CTX
, "init_target_events");
1585 if (retval
!= ERROR_OK
)
1588 retval
= command_run_line(CMD_CTX
, "init_board");
1589 if (retval
!= ERROR_OK
)
1592 LOG_DEBUG("Initializing targets...");
1593 return target_init(CMD_CTX
);
1596 int target_register_event_callback(int (*callback
)(struct target
*target
,
1597 enum target_event event
, void *priv
), void *priv
)
1599 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1602 return ERROR_COMMAND_SYNTAX_ERROR
;
1605 while ((*callbacks_p
)->next
)
1606 callbacks_p
= &((*callbacks_p
)->next
);
1607 callbacks_p
= &((*callbacks_p
)->next
);
1610 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1611 (*callbacks_p
)->callback
= callback
;
1612 (*callbacks_p
)->priv
= priv
;
1613 (*callbacks_p
)->next
= NULL
;
1618 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1619 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1621 struct target_reset_callback
*entry
;
1624 return ERROR_COMMAND_SYNTAX_ERROR
;
1626 entry
= malloc(sizeof(struct target_reset_callback
));
1628 LOG_ERROR("error allocating buffer for reset callback entry");
1629 return ERROR_COMMAND_SYNTAX_ERROR
;
1632 entry
->callback
= callback
;
1634 list_add(&entry
->list
, &target_reset_callback_list
);
1640 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1641 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1643 struct target_trace_callback
*entry
;
1646 return ERROR_COMMAND_SYNTAX_ERROR
;
1648 entry
= malloc(sizeof(struct target_trace_callback
));
1650 LOG_ERROR("error allocating buffer for trace callback entry");
1651 return ERROR_COMMAND_SYNTAX_ERROR
;
1654 entry
->callback
= callback
;
1656 list_add(&entry
->list
, &target_trace_callback_list
);
1662 int target_register_timer_callback(int (*callback
)(void *priv
),
1663 unsigned int time_ms
, enum target_timer_type type
, void *priv
)
1665 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1668 return ERROR_COMMAND_SYNTAX_ERROR
;
1671 while ((*callbacks_p
)->next
)
1672 callbacks_p
= &((*callbacks_p
)->next
);
1673 callbacks_p
= &((*callbacks_p
)->next
);
1676 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1677 (*callbacks_p
)->callback
= callback
;
1678 (*callbacks_p
)->type
= type
;
1679 (*callbacks_p
)->time_ms
= time_ms
;
1680 (*callbacks_p
)->removed
= false;
1682 (*callbacks_p
)->when
= timeval_ms() + time_ms
;
1683 target_timer_next_event_value
= MIN(target_timer_next_event_value
, (*callbacks_p
)->when
);
1685 (*callbacks_p
)->priv
= priv
;
1686 (*callbacks_p
)->next
= NULL
;
1691 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1692 enum target_event event
, void *priv
), void *priv
)
1694 struct target_event_callback
**p
= &target_event_callbacks
;
1695 struct target_event_callback
*c
= target_event_callbacks
;
1698 return ERROR_COMMAND_SYNTAX_ERROR
;
1701 struct target_event_callback
*next
= c
->next
;
1702 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1714 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1715 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1717 struct target_reset_callback
*entry
;
1720 return ERROR_COMMAND_SYNTAX_ERROR
;
1722 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1723 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1724 list_del(&entry
->list
);
1733 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1734 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1736 struct target_trace_callback
*entry
;
1739 return ERROR_COMMAND_SYNTAX_ERROR
;
1741 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1742 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1743 list_del(&entry
->list
);
1752 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1755 return ERROR_COMMAND_SYNTAX_ERROR
;
1757 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1759 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1768 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1770 struct target_event_callback
*callback
= target_event_callbacks
;
1771 struct target_event_callback
*next_callback
;
1773 if (event
== TARGET_EVENT_HALTED
) {
1774 /* execute early halted first */
1775 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1778 LOG_DEBUG("target event %i (%s) for core %s", event
,
1779 target_event_name(event
),
1780 target_name(target
));
1782 target_handle_event(target
, event
);
1785 next_callback
= callback
->next
;
1786 callback
->callback(target
, event
, callback
->priv
);
1787 callback
= next_callback
;
1793 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1795 struct target_reset_callback
*callback
;
1797 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1798 nvp_value2name(nvp_reset_modes
, reset_mode
)->name
);
1800 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1801 callback
->callback(target
, reset_mode
, callback
->priv
);
1806 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1808 struct target_trace_callback
*callback
;
1810 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1811 callback
->callback(target
, len
, data
, callback
->priv
);
1816 static int target_timer_callback_periodic_restart(
1817 struct target_timer_callback
*cb
, int64_t *now
)
1819 cb
->when
= *now
+ cb
->time_ms
;
1823 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1826 cb
->callback(cb
->priv
);
1828 if (cb
->type
== TARGET_TIMER_TYPE_PERIODIC
)
1829 return target_timer_callback_periodic_restart(cb
, now
);
1831 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1834 static int target_call_timer_callbacks_check_time(int checktime
)
1836 static bool callback_processing
;
1838 /* Do not allow nesting */
1839 if (callback_processing
)
1842 callback_processing
= true;
1846 int64_t now
= timeval_ms();
1848 /* Initialize to a default value that's a ways into the future.
1849 * The loop below will make it closer to now if there are
1850 * callbacks that want to be called sooner. */
1851 target_timer_next_event_value
= now
+ 1000;
1853 /* Store an address of the place containing a pointer to the
1854 * next item; initially, that's a standalone "root of the
1855 * list" variable. */
1856 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1857 while (callback
&& *callback
) {
1858 if ((*callback
)->removed
) {
1859 struct target_timer_callback
*p
= *callback
;
1860 *callback
= (*callback
)->next
;
1865 bool call_it
= (*callback
)->callback
&&
1866 ((!checktime
&& (*callback
)->type
== TARGET_TIMER_TYPE_PERIODIC
) ||
1867 now
>= (*callback
)->when
);
1870 target_call_timer_callback(*callback
, &now
);
1872 if (!(*callback
)->removed
&& (*callback
)->when
< target_timer_next_event_value
)
1873 target_timer_next_event_value
= (*callback
)->when
;
1875 callback
= &(*callback
)->next
;
1878 callback_processing
= false;
1882 int target_call_timer_callbacks(void)
1884 return target_call_timer_callbacks_check_time(1);
1887 /* invoke periodic callbacks immediately */
1888 int target_call_timer_callbacks_now(void)
1890 return target_call_timer_callbacks_check_time(0);
1893 int64_t target_timer_next_event(void)
1895 return target_timer_next_event_value
;
1898 /* Prints the working area layout for debug purposes */
1899 static void print_wa_layout(struct target
*target
)
1901 struct working_area
*c
= target
->working_areas
;
1904 LOG_DEBUG("%c%c " TARGET_ADDR_FMT
"-" TARGET_ADDR_FMT
" (%" PRIu32
" bytes)",
1905 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1906 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1911 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1912 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1914 assert(area
->free
); /* Shouldn't split an allocated area */
1915 assert(size
<= area
->size
); /* Caller should guarantee this */
1917 /* Split only if not already the right size */
1918 if (size
< area
->size
) {
1919 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1924 new_wa
->next
= area
->next
;
1925 new_wa
->size
= area
->size
- size
;
1926 new_wa
->address
= area
->address
+ size
;
1927 new_wa
->backup
= NULL
;
1928 new_wa
->user
= NULL
;
1929 new_wa
->free
= true;
1931 area
->next
= new_wa
;
1934 /* If backup memory was allocated to this area, it has the wrong size
1935 * now so free it and it will be reallocated if/when needed */
1937 area
->backup
= NULL
;
1941 /* Merge all adjacent free areas into one */
1942 static void target_merge_working_areas(struct target
*target
)
1944 struct working_area
*c
= target
->working_areas
;
1946 while (c
&& c
->next
) {
1947 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1949 /* Find two adjacent free areas */
1950 if (c
->free
&& c
->next
->free
) {
1951 /* Merge the last into the first */
1952 c
->size
+= c
->next
->size
;
1954 /* Remove the last */
1955 struct working_area
*to_be_freed
= c
->next
;
1956 c
->next
= c
->next
->next
;
1957 free(to_be_freed
->backup
);
1960 /* If backup memory was allocated to the remaining area, it's has
1961 * the wrong size now */
1970 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1972 /* Reevaluate working area address based on MMU state*/
1973 if (!target
->working_areas
) {
1977 retval
= target
->type
->mmu(target
, &enabled
);
1978 if (retval
!= ERROR_OK
)
1982 if (target
->working_area_phys_spec
) {
1983 LOG_DEBUG("MMU disabled, using physical "
1984 "address for working memory " TARGET_ADDR_FMT
,
1985 target
->working_area_phys
);
1986 target
->working_area
= target
->working_area_phys
;
1988 LOG_ERROR("No working memory available. "
1989 "Specify -work-area-phys to target.");
1990 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1993 if (target
->working_area_virt_spec
) {
1994 LOG_DEBUG("MMU enabled, using virtual "
1995 "address for working memory " TARGET_ADDR_FMT
,
1996 target
->working_area_virt
);
1997 target
->working_area
= target
->working_area_virt
;
1999 LOG_ERROR("No working memory available. "
2000 "Specify -work-area-virt to target.");
2001 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2005 /* Set up initial working area on first call */
2006 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
2008 new_wa
->next
= NULL
;
2009 new_wa
->size
= ALIGN_DOWN(target
->working_area_size
, 4); /* 4-byte align */
2010 new_wa
->address
= target
->working_area
;
2011 new_wa
->backup
= NULL
;
2012 new_wa
->user
= NULL
;
2013 new_wa
->free
= true;
2016 target
->working_areas
= new_wa
;
2019 /* only allocate multiples of 4 byte */
2020 size
= ALIGN_UP(size
, 4);
2022 struct working_area
*c
= target
->working_areas
;
2024 /* Find the first large enough working area */
2026 if (c
->free
&& c
->size
>= size
)
2032 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2034 /* Split the working area into the requested size */
2035 target_split_working_area(c
, size
);
2037 LOG_DEBUG("allocated new working area of %" PRIu32
" bytes at address " TARGET_ADDR_FMT
,
2040 if (target
->backup_working_area
) {
2042 c
->backup
= malloc(c
->size
);
2047 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
2048 if (retval
!= ERROR_OK
)
2052 /* mark as used, and return the new (reused) area */
2059 print_wa_layout(target
);
2064 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
2068 retval
= target_alloc_working_area_try(target
, size
, area
);
2069 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
2070 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
2075 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
2077 int retval
= ERROR_OK
;
2079 if (target
->backup_working_area
&& area
->backup
) {
2080 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
2081 if (retval
!= ERROR_OK
)
2082 LOG_ERROR("failed to restore %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
2083 area
->size
, area
->address
);
2089 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
2090 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
2092 if (!area
|| area
->free
)
2095 int retval
= ERROR_OK
;
2097 retval
= target_restore_working_area(target
, area
);
2098 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
2099 if (retval
!= ERROR_OK
)
2105 LOG_DEBUG("freed %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
2106 area
->size
, area
->address
);
2108 /* mark user pointer invalid */
2109 /* TODO: Is this really safe? It points to some previous caller's memory.
2110 * How could we know that the area pointer is still in that place and not
2111 * some other vital data? What's the purpose of this, anyway? */
2115 target_merge_working_areas(target
);
2117 print_wa_layout(target
);
2122 int target_free_working_area(struct target
*target
, struct working_area
*area
)
2124 return target_free_working_area_restore(target
, area
, 1);
2127 /* free resources and restore memory, if restoring memory fails,
2128 * free up resources anyway
2130 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
2132 struct working_area
*c
= target
->working_areas
;
2134 LOG_DEBUG("freeing all working areas");
2136 /* Loop through all areas, restoring the allocated ones and marking them as free */
2140 target_restore_working_area(target
, c
);
2142 *c
->user
= NULL
; /* Same as above */
2148 /* Run a merge pass to combine all areas into one */
2149 target_merge_working_areas(target
);
2151 print_wa_layout(target
);
2154 void target_free_all_working_areas(struct target
*target
)
2156 target_free_all_working_areas_restore(target
, 1);
2158 /* Now we have none or only one working area marked as free */
2159 if (target
->working_areas
) {
2160 /* Free the last one to allow on-the-fly moving and resizing */
2161 free(target
->working_areas
->backup
);
2162 free(target
->working_areas
);
2163 target
->working_areas
= NULL
;
2167 /* Find the largest number of bytes that can be allocated */
2168 uint32_t target_get_working_area_avail(struct target
*target
)
2170 struct working_area
*c
= target
->working_areas
;
2171 uint32_t max_size
= 0;
2174 return ALIGN_DOWN(target
->working_area_size
, 4);
2177 if (c
->free
&& max_size
< c
->size
)
2186 static void target_destroy(struct target
*target
)
2188 breakpoint_remove_all(target
);
2189 watchpoint_remove_all(target
);
2191 if (target
->type
->deinit_target
)
2192 target
->type
->deinit_target(target
);
2194 if (target
->semihosting
)
2195 free(target
->semihosting
->basedir
);
2196 free(target
->semihosting
);
2198 jtag_unregister_event_callback(jtag_enable_callback
, target
);
2200 struct target_event_action
*teap
= target
->event_action
;
2202 struct target_event_action
*next
= teap
->next
;
2203 Jim_DecrRefCount(teap
->interp
, teap
->body
);
2208 target_free_all_working_areas(target
);
2210 /* release the targets SMP list */
2212 struct target_list
*head
, *tmp
;
2214 list_for_each_entry_safe(head
, tmp
, target
->smp_targets
, lh
) {
2215 list_del(&head
->lh
);
2216 head
->target
->smp
= 0;
2219 if (target
->smp_targets
!= &empty_smp_targets
)
2220 free(target
->smp_targets
);
2224 rtos_destroy(target
);
2226 free(target
->gdb_port_override
);
2228 free(target
->trace_info
);
2229 free(target
->fileio_info
);
2230 free(target
->cmd_name
);
2234 void target_quit(void)
2236 struct target_event_callback
*pe
= target_event_callbacks
;
2238 struct target_event_callback
*t
= pe
->next
;
2242 target_event_callbacks
= NULL
;
2244 struct target_timer_callback
*pt
= target_timer_callbacks
;
2246 struct target_timer_callback
*t
= pt
->next
;
2250 target_timer_callbacks
= NULL
;
2252 for (struct target
*target
= all_targets
; target
;) {
2256 target_destroy(target
);
2263 int target_arch_state(struct target
*target
)
2267 LOG_WARNING("No target has been configured");
2271 if (target
->state
!= TARGET_HALTED
)
2274 retval
= target
->type
->arch_state(target
);
2278 static int target_get_gdb_fileio_info_default(struct target
*target
,
2279 struct gdb_fileio_info
*fileio_info
)
2281 /* If target does not support semi-hosting function, target
2282 has no need to provide .get_gdb_fileio_info callback.
2283 It just return ERROR_FAIL and gdb_server will return "Txx"
2284 as target halted every time. */
2288 static int target_gdb_fileio_end_default(struct target
*target
,
2289 int retcode
, int fileio_errno
, bool ctrl_c
)
2294 int target_profiling_default(struct target
*target
, uint32_t *samples
,
2295 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
2297 struct timeval timeout
, now
;
2299 gettimeofday(&timeout
, NULL
);
2300 timeval_add_time(&timeout
, seconds
, 0);
2302 LOG_INFO("Starting profiling. Halting and resuming the"
2303 " target as often as we can...");
2305 uint32_t sample_count
= 0;
2306 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2307 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", true);
2309 int retval
= ERROR_OK
;
2311 target_poll(target
);
2312 if (target
->state
== TARGET_HALTED
) {
2313 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
2314 samples
[sample_count
++] = t
;
2315 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2316 retval
= target_resume(target
, 1, 0, 0, 0);
2317 target_poll(target
);
2318 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2319 } else if (target
->state
== TARGET_RUNNING
) {
2320 /* We want to quickly sample the PC. */
2321 retval
= target_halt(target
);
2323 LOG_INFO("Target not halted or running");
2328 if (retval
!= ERROR_OK
)
2331 gettimeofday(&now
, NULL
);
2332 if ((sample_count
>= max_num_samples
) || timeval_compare(&now
, &timeout
) >= 0) {
2333 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2338 *num_samples
= sample_count
;
2342 /* Single aligned words are guaranteed to use 16 or 32 bit access
2343 * mode respectively, otherwise data is handled as quickly as
2346 int target_write_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, const uint8_t *buffer
)
2348 LOG_DEBUG("writing buffer of %" PRIu32
" byte at " TARGET_ADDR_FMT
,
2351 if (!target_was_examined(target
)) {
2352 LOG_ERROR("Target not examined yet");
2359 if ((address
+ size
- 1) < address
) {
2360 /* GDB can request this when e.g. PC is 0xfffffffc */
2361 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2367 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2370 static int target_write_buffer_default(struct target
*target
,
2371 target_addr_t address
, uint32_t count
, const uint8_t *buffer
)
2374 unsigned int data_bytes
= target_data_bits(target
) / 8;
2376 /* Align up to maximum bytes. The loop condition makes sure the next pass
2377 * will have something to do with the size we leave to it. */
2379 size
< data_bytes
&& count
>= size
* 2 + (address
& size
);
2381 if (address
& size
) {
2382 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2383 if (retval
!= ERROR_OK
)
2391 /* Write the data with as large access size as possible. */
2392 for (; size
> 0; size
/= 2) {
2393 uint32_t aligned
= count
- count
% size
;
2395 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2396 if (retval
!= ERROR_OK
)
2407 /* Single aligned words are guaranteed to use 16 or 32 bit access
2408 * mode respectively, otherwise data is handled as quickly as
2411 int target_read_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
2413 LOG_DEBUG("reading buffer of %" PRIu32
" byte at " TARGET_ADDR_FMT
,
2416 if (!target_was_examined(target
)) {
2417 LOG_ERROR("Target not examined yet");
2424 if ((address
+ size
- 1) < address
) {
2425 /* GDB can request this when e.g. PC is 0xfffffffc */
2426 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2432 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2435 static int target_read_buffer_default(struct target
*target
, target_addr_t address
, uint32_t count
, uint8_t *buffer
)
2438 unsigned int data_bytes
= target_data_bits(target
) / 8;
2440 /* Align up to maximum bytes. The loop condition makes sure the next pass
2441 * will have something to do with the size we leave to it. */
2443 size
< data_bytes
&& count
>= size
* 2 + (address
& size
);
2445 if (address
& size
) {
2446 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2447 if (retval
!= ERROR_OK
)
2455 /* Read the data with as large access size as possible. */
2456 for (; size
> 0; size
/= 2) {
2457 uint32_t aligned
= count
- count
% size
;
2459 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2460 if (retval
!= ERROR_OK
)
2471 int target_checksum_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t *crc
)
2476 uint32_t checksum
= 0;
2477 if (!target_was_examined(target
)) {
2478 LOG_ERROR("Target not examined yet");
2481 if (!target
->type
->checksum_memory
) {
2482 LOG_ERROR("Target %s doesn't support checksum_memory", target_name(target
));
2486 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2487 if (retval
!= ERROR_OK
) {
2488 buffer
= malloc(size
);
2490 LOG_ERROR("error allocating buffer for section (%" PRIu32
" bytes)", size
);
2491 return ERROR_COMMAND_SYNTAX_ERROR
;
2493 retval
= target_read_buffer(target
, address
, size
, buffer
);
2494 if (retval
!= ERROR_OK
) {
2499 /* convert to target endianness */
2500 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2501 uint32_t target_data
;
2502 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2503 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2506 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2515 int target_blank_check_memory(struct target
*target
,
2516 struct target_memory_check_block
*blocks
, int num_blocks
,
2517 uint8_t erased_value
)
2519 if (!target_was_examined(target
)) {
2520 LOG_ERROR("Target not examined yet");
2524 if (!target
->type
->blank_check_memory
)
2525 return ERROR_NOT_IMPLEMENTED
;
2527 return target
->type
->blank_check_memory(target
, blocks
, num_blocks
, erased_value
);
2530 int target_read_u64(struct target
*target
, target_addr_t address
, uint64_t *value
)
2532 uint8_t value_buf
[8];
2533 if (!target_was_examined(target
)) {
2534 LOG_ERROR("Target not examined yet");
2538 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2540 if (retval
== ERROR_OK
) {
2541 *value
= target_buffer_get_u64(target
, value_buf
);
2542 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2547 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2554 int target_read_u32(struct target
*target
, target_addr_t address
, uint32_t *value
)
2556 uint8_t value_buf
[4];
2557 if (!target_was_examined(target
)) {
2558 LOG_ERROR("Target not examined yet");
2562 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2564 if (retval
== ERROR_OK
) {
2565 *value
= target_buffer_get_u32(target
, value_buf
);
2566 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2571 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2578 int target_read_u16(struct target
*target
, target_addr_t address
, uint16_t *value
)
2580 uint8_t value_buf
[2];
2581 if (!target_was_examined(target
)) {
2582 LOG_ERROR("Target not examined yet");
2586 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2588 if (retval
== ERROR_OK
) {
2589 *value
= target_buffer_get_u16(target
, value_buf
);
2590 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%4.4" PRIx16
,
2595 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2602 int target_read_u8(struct target
*target
, target_addr_t address
, uint8_t *value
)
2604 if (!target_was_examined(target
)) {
2605 LOG_ERROR("Target not examined yet");
2609 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2611 if (retval
== ERROR_OK
) {
2612 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2617 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2624 int target_write_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2627 uint8_t value_buf
[8];
2628 if (!target_was_examined(target
)) {
2629 LOG_ERROR("Target not examined yet");
2633 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2637 target_buffer_set_u64(target
, value_buf
, value
);
2638 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2639 if (retval
!= ERROR_OK
)
2640 LOG_DEBUG("failed: %i", retval
);
2645 int target_write_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2648 uint8_t value_buf
[4];
2649 if (!target_was_examined(target
)) {
2650 LOG_ERROR("Target not examined yet");
2654 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2658 target_buffer_set_u32(target
, value_buf
, value
);
2659 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2660 if (retval
!= ERROR_OK
)
2661 LOG_DEBUG("failed: %i", retval
);
2666 int target_write_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2669 uint8_t value_buf
[2];
2670 if (!target_was_examined(target
)) {
2671 LOG_ERROR("Target not examined yet");
2675 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2679 target_buffer_set_u16(target
, value_buf
, value
);
2680 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2681 if (retval
!= ERROR_OK
)
2682 LOG_DEBUG("failed: %i", retval
);
2687 int target_write_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2690 if (!target_was_examined(target
)) {
2691 LOG_ERROR("Target not examined yet");
2695 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2698 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2699 if (retval
!= ERROR_OK
)
2700 LOG_DEBUG("failed: %i", retval
);
2705 int target_write_phys_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2708 uint8_t value_buf
[8];
2709 if (!target_was_examined(target
)) {
2710 LOG_ERROR("Target not examined yet");
2714 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2718 target_buffer_set_u64(target
, value_buf
, value
);
2719 retval
= target_write_phys_memory(target
, address
, 8, 1, value_buf
);
2720 if (retval
!= ERROR_OK
)
2721 LOG_DEBUG("failed: %i", retval
);
2726 int target_write_phys_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2729 uint8_t value_buf
[4];
2730 if (!target_was_examined(target
)) {
2731 LOG_ERROR("Target not examined yet");
2735 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2739 target_buffer_set_u32(target
, value_buf
, value
);
2740 retval
= target_write_phys_memory(target
, address
, 4, 1, value_buf
);
2741 if (retval
!= ERROR_OK
)
2742 LOG_DEBUG("failed: %i", retval
);
2747 int target_write_phys_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2750 uint8_t value_buf
[2];
2751 if (!target_was_examined(target
)) {
2752 LOG_ERROR("Target not examined yet");
2756 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2760 target_buffer_set_u16(target
, value_buf
, value
);
2761 retval
= target_write_phys_memory(target
, address
, 2, 1, value_buf
);
2762 if (retval
!= ERROR_OK
)
2763 LOG_DEBUG("failed: %i", retval
);
2768 int target_write_phys_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2771 if (!target_was_examined(target
)) {
2772 LOG_ERROR("Target not examined yet");
2776 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2779 retval
= target_write_phys_memory(target
, address
, 1, 1, &value
);
2780 if (retval
!= ERROR_OK
)
2781 LOG_DEBUG("failed: %i", retval
);
2786 static int find_target(struct command_invocation
*cmd
, const char *name
)
2788 struct target
*target
= get_target(name
);
2790 command_print(cmd
, "Target: %s is unknown, try one of:\n", name
);
2793 if (!target
->tap
->enabled
) {
2794 command_print(cmd
, "Target: TAP %s is disabled, "
2795 "can't be the current target\n",
2796 target
->tap
->dotted_name
);
2800 cmd
->ctx
->current_target
= target
;
2801 if (cmd
->ctx
->current_target_override
)
2802 cmd
->ctx
->current_target_override
= target
;
2808 COMMAND_HANDLER(handle_targets_command
)
2810 int retval
= ERROR_OK
;
2811 if (CMD_ARGC
== 1) {
2812 retval
= find_target(CMD
, CMD_ARGV
[0]);
2813 if (retval
== ERROR_OK
) {
2819 unsigned int index
= 0;
2820 command_print(CMD
, " TargetName Type Endian TapName State ");
2821 command_print(CMD
, "-- ------------------ ---------- ------ ------------------ ------------");
2822 for (struct target
*target
= all_targets
; target
; target
= target
->next
, ++index
) {
2826 if (target
->tap
->enabled
)
2827 state
= target_state_name(target
);
2829 state
= "tap-disabled";
2831 if (CMD_CTX
->current_target
== target
)
2834 /* keep columns lined up to match the headers above */
2836 "%2d%c %-18s %-10s %-6s %-18s %s",
2839 target_name(target
),
2840 target_type_name(target
),
2841 jim_nvp_value2name_simple(nvp_target_endian
,
2842 target
->endianness
)->name
,
2843 target
->tap
->dotted_name
,
2850 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2852 static int power_dropout
;
2853 static int srst_asserted
;
2855 static int run_power_restore
;
2856 static int run_power_dropout
;
2857 static int run_srst_asserted
;
2858 static int run_srst_deasserted
;
2860 static int sense_handler(void)
2862 static int prev_srst_asserted
;
2863 static int prev_power_dropout
;
2865 int retval
= jtag_power_dropout(&power_dropout
);
2866 if (retval
!= ERROR_OK
)
2870 power_restored
= prev_power_dropout
&& !power_dropout
;
2872 run_power_restore
= 1;
2874 int64_t current
= timeval_ms();
2875 static int64_t last_power
;
2876 bool wait_more
= last_power
+ 2000 > current
;
2877 if (power_dropout
&& !wait_more
) {
2878 run_power_dropout
= 1;
2879 last_power
= current
;
2882 retval
= jtag_srst_asserted(&srst_asserted
);
2883 if (retval
!= ERROR_OK
)
2886 int srst_deasserted
;
2887 srst_deasserted
= prev_srst_asserted
&& !srst_asserted
;
2889 static int64_t last_srst
;
2890 wait_more
= last_srst
+ 2000 > current
;
2891 if (srst_deasserted
&& !wait_more
) {
2892 run_srst_deasserted
= 1;
2893 last_srst
= current
;
2896 if (!prev_srst_asserted
&& srst_asserted
)
2897 run_srst_asserted
= 1;
2899 prev_srst_asserted
= srst_asserted
;
2900 prev_power_dropout
= power_dropout
;
2902 if (srst_deasserted
|| power_restored
) {
2903 /* Other than logging the event we can't do anything here.
2904 * Issuing a reset is a particularly bad idea as we might
2905 * be inside a reset already.
2912 /* process target state changes */
2913 static int handle_target(void *priv
)
2915 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2916 int retval
= ERROR_OK
;
2918 if (!is_jtag_poll_safe()) {
2919 /* polling is disabled currently */
2923 /* we do not want to recurse here... */
2924 static int recursive
;
2928 /* danger! running these procedures can trigger srst assertions and power dropouts.
2929 * We need to avoid an infinite loop/recursion here and we do that by
2930 * clearing the flags after running these events.
2932 int did_something
= 0;
2933 if (run_srst_asserted
) {
2934 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2935 Jim_Eval(interp
, "srst_asserted");
2938 if (run_srst_deasserted
) {
2939 Jim_Eval(interp
, "srst_deasserted");
2942 if (run_power_dropout
) {
2943 LOG_INFO("Power dropout detected, running power_dropout proc.");
2944 Jim_Eval(interp
, "power_dropout");
2947 if (run_power_restore
) {
2948 Jim_Eval(interp
, "power_restore");
2952 if (did_something
) {
2953 /* clear detect flags */
2957 /* clear action flags */
2959 run_srst_asserted
= 0;
2960 run_srst_deasserted
= 0;
2961 run_power_restore
= 0;
2962 run_power_dropout
= 0;
2967 /* Poll targets for state changes unless that's globally disabled.
2968 * Skip targets that are currently disabled.
2970 for (struct target
*target
= all_targets
;
2971 is_jtag_poll_safe() && target
;
2972 target
= target
->next
) {
2974 if (!target_was_examined(target
))
2977 if (!target
->tap
->enabled
)
2980 if (target
->backoff
.times
> target
->backoff
.count
) {
2981 /* do not poll this time as we failed previously */
2982 target
->backoff
.count
++;
2985 target
->backoff
.count
= 0;
2987 /* only poll target if we've got power and srst isn't asserted */
2988 if (!power_dropout
&& !srst_asserted
) {
2989 /* polling may fail silently until the target has been examined */
2990 retval
= target_poll(target
);
2991 if (retval
!= ERROR_OK
) {
2992 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2993 if (target
->backoff
.times
* polling_interval
< 5000) {
2994 target
->backoff
.times
*= 2;
2995 target
->backoff
.times
++;
2998 /* Tell GDB to halt the debugger. This allows the user to
2999 * run monitor commands to handle the situation.
3001 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
3003 if (target
->backoff
.times
> 0) {
3004 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
3005 target_reset_examined(target
);
3006 retval
= target_examine_one(target
);
3007 /* Target examination could have failed due to unstable connection,
3008 * but we set the examined flag anyway to repoll it later */
3009 if (retval
!= ERROR_OK
) {
3010 target_set_examined(target
);
3011 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
3012 target
->backoff
.times
* polling_interval
);
3017 /* Since we succeeded, we reset backoff count */
3018 target
->backoff
.times
= 0;
3025 COMMAND_HANDLER(handle_reg_command
)
3029 struct target
*target
= get_current_target(CMD_CTX
);
3030 if (!target_was_examined(target
)) {
3031 LOG_ERROR("Target not examined yet");
3032 return ERROR_TARGET_NOT_EXAMINED
;
3034 struct reg
*reg
= NULL
;
3036 /* list all available registers for the current target */
3037 if (CMD_ARGC
== 0) {
3038 struct reg_cache
*cache
= target
->reg_cache
;
3040 unsigned int count
= 0;
3044 command_print(CMD
, "===== %s", cache
->name
);
3046 for (i
= 0, reg
= cache
->reg_list
;
3047 i
< cache
->num_regs
;
3048 i
++, reg
++, count
++) {
3049 if (reg
->exist
== false || reg
->hidden
)
3051 /* only print cached values if they are valid */
3053 char *value
= buf_to_hex_str(reg
->value
,
3056 "(%i) %s (/%" PRIu32
"): 0x%s%s",
3064 command_print(CMD
, "(%i) %s (/%" PRIu32
")",
3069 cache
= cache
->next
;
3075 /* access a single register by its ordinal number */
3076 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
3078 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
3080 struct reg_cache
*cache
= target
->reg_cache
;
3081 unsigned int count
= 0;
3084 for (i
= 0; i
< cache
->num_regs
; i
++) {
3085 if (count
++ == num
) {
3086 reg
= &cache
->reg_list
[i
];
3092 cache
= cache
->next
;
3096 command_print(CMD
, "%i is out of bounds, the current target "
3097 "has only %i registers (0 - %i)", num
, count
, count
- 1);
3101 /* access a single register by its name */
3102 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], true);
3108 assert(reg
); /* give clang a hint that we *know* reg is != NULL here */
3113 /* display a register */
3114 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
3115 && (CMD_ARGV
[1][0] <= '9')))) {
3116 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
3120 int retval
= reg
->type
->get(reg
);
3121 if (retval
!= ERROR_OK
) {
3122 LOG_ERROR("Could not read register '%s'", reg
->name
);
3126 char *value
= buf_to_hex_str(reg
->value
, reg
->size
);
3127 command_print(CMD
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
3132 /* set register value */
3133 if (CMD_ARGC
== 2) {
3134 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
3137 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
3139 int retval
= reg
->type
->set(reg
, buf
);
3140 if (retval
!= ERROR_OK
) {
3141 LOG_ERROR("Could not write to register '%s'", reg
->name
);
3143 char *value
= buf_to_hex_str(reg
->value
, reg
->size
);
3144 command_print(CMD
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
3153 return ERROR_COMMAND_SYNTAX_ERROR
;
3156 command_print(CMD
, "register %s not found in current target", CMD_ARGV
[0]);
3160 COMMAND_HANDLER(handle_poll_command
)
3162 int retval
= ERROR_OK
;
3163 struct target
*target
= get_current_target(CMD_CTX
);
3165 if (CMD_ARGC
== 0) {
3166 command_print(CMD
, "background polling: %s",
3167 jtag_poll_get_enabled() ? "on" : "off");
3168 command_print(CMD
, "TAP: %s (%s)",
3169 target
->tap
->dotted_name
,
3170 target
->tap
->enabled
? "enabled" : "disabled");
3171 if (!target
->tap
->enabled
)
3173 retval
= target_poll(target
);
3174 if (retval
!= ERROR_OK
)
3176 retval
= target_arch_state(target
);
3177 if (retval
!= ERROR_OK
)
3179 } else if (CMD_ARGC
== 1) {
3181 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
3182 jtag_poll_set_enabled(enable
);
3184 return ERROR_COMMAND_SYNTAX_ERROR
;
3189 COMMAND_HANDLER(handle_wait_halt_command
)
3192 return ERROR_COMMAND_SYNTAX_ERROR
;
3194 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
3195 if (1 == CMD_ARGC
) {
3196 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
3197 if (retval
!= ERROR_OK
)
3198 return ERROR_COMMAND_SYNTAX_ERROR
;
3201 struct target
*target
= get_current_target(CMD_CTX
);
3202 return target_wait_state(target
, TARGET_HALTED
, ms
);
3205 /* wait for target state to change. The trick here is to have a low
3206 * latency for short waits and not to suck up all the CPU time
3209 * After 500ms, keep_alive() is invoked
3211 int target_wait_state(struct target
*target
, enum target_state state
, unsigned int ms
)
3214 int64_t then
= 0, cur
;
3218 retval
= target_poll(target
);
3219 if (retval
!= ERROR_OK
)
3221 if (target
->state
== state
)
3226 then
= timeval_ms();
3227 LOG_DEBUG("waiting for target %s...",
3228 nvp_value2name(nvp_target_state
, state
)->name
);
3231 if (cur
- then
> 500) {
3233 if (openocd_is_shutdown_pending())
3234 return ERROR_SERVER_INTERRUPTED
;
3237 if ((cur
-then
) > ms
) {
3238 LOG_ERROR("timed out while waiting for target %s",
3239 nvp_value2name(nvp_target_state
, state
)->name
);
3247 COMMAND_HANDLER(handle_halt_command
)
3251 struct target
*target
= get_current_target(CMD_CTX
);
3253 target
->verbose_halt_msg
= true;
3255 int retval
= target_halt(target
);
3256 if (retval
!= ERROR_OK
)
3259 if (CMD_ARGC
== 1) {
3260 unsigned wait_local
;
3261 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
3262 if (retval
!= ERROR_OK
)
3263 return ERROR_COMMAND_SYNTAX_ERROR
;
3268 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
3271 COMMAND_HANDLER(handle_soft_reset_halt_command
)
3273 struct target
*target
= get_current_target(CMD_CTX
);
3275 LOG_TARGET_INFO(target
, "requesting target halt and executing a soft reset");
3277 target_soft_reset_halt(target
);
3282 COMMAND_HANDLER(handle_reset_command
)
3285 return ERROR_COMMAND_SYNTAX_ERROR
;
3287 enum target_reset_mode reset_mode
= RESET_RUN
;
3288 if (CMD_ARGC
== 1) {
3289 const struct nvp
*n
;
3290 n
= nvp_name2value(nvp_reset_modes
, CMD_ARGV
[0]);
3291 if ((!n
->name
) || (n
->value
== RESET_UNKNOWN
))
3292 return ERROR_COMMAND_SYNTAX_ERROR
;
3293 reset_mode
= n
->value
;
3296 /* reset *all* targets */
3297 return target_process_reset(CMD
, reset_mode
);
3301 COMMAND_HANDLER(handle_resume_command
)
3305 return ERROR_COMMAND_SYNTAX_ERROR
;
3307 struct target
*target
= get_current_target(CMD_CTX
);
3309 /* with no CMD_ARGV, resume from current pc, addr = 0,
3310 * with one arguments, addr = CMD_ARGV[0],
3311 * handle breakpoints, not debugging */
3312 target_addr_t addr
= 0;
3313 if (CMD_ARGC
== 1) {
3314 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3318 return target_resume(target
, current
, addr
, 1, 0);
3321 COMMAND_HANDLER(handle_step_command
)
3324 return ERROR_COMMAND_SYNTAX_ERROR
;
3328 /* with no CMD_ARGV, step from current pc, addr = 0,
3329 * with one argument addr = CMD_ARGV[0],
3330 * handle breakpoints, debugging */
3331 target_addr_t addr
= 0;
3333 if (CMD_ARGC
== 1) {
3334 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3338 struct target
*target
= get_current_target(CMD_CTX
);
3340 return target_step(target
, current_pc
, addr
, 1);
3343 void target_handle_md_output(struct command_invocation
*cmd
,
3344 struct target
*target
, target_addr_t address
, unsigned size
,
3345 unsigned count
, const uint8_t *buffer
)
3347 const unsigned line_bytecnt
= 32;
3348 unsigned line_modulo
= line_bytecnt
/ size
;
3350 char output
[line_bytecnt
* 4 + 1];
3351 unsigned output_len
= 0;
3353 const char *value_fmt
;
3356 value_fmt
= "%16.16"PRIx64
" ";
3359 value_fmt
= "%8.8"PRIx64
" ";
3362 value_fmt
= "%4.4"PRIx64
" ";
3365 value_fmt
= "%2.2"PRIx64
" ";
3368 /* "can't happen", caller checked */
3369 LOG_ERROR("invalid memory read size: %u", size
);
3373 for (unsigned i
= 0; i
< count
; i
++) {
3374 if (i
% line_modulo
== 0) {
3375 output_len
+= snprintf(output
+ output_len
,
3376 sizeof(output
) - output_len
,
3377 TARGET_ADDR_FMT
": ",
3378 (address
+ (i
* size
)));
3382 const uint8_t *value_ptr
= buffer
+ i
* size
;
3385 value
= target_buffer_get_u64(target
, value_ptr
);
3388 value
= target_buffer_get_u32(target
, value_ptr
);
3391 value
= target_buffer_get_u16(target
, value_ptr
);
3396 output_len
+= snprintf(output
+ output_len
,
3397 sizeof(output
) - output_len
,
3400 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
3401 command_print(cmd
, "%s", output
);
3407 COMMAND_HANDLER(handle_md_command
)
3410 return ERROR_COMMAND_SYNTAX_ERROR
;
3413 switch (CMD_NAME
[2]) {
3427 return ERROR_COMMAND_SYNTAX_ERROR
;
3430 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3431 int (*fn
)(struct target
*target
,
3432 target_addr_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
3436 fn
= target_read_phys_memory
;
3438 fn
= target_read_memory
;
3439 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
3440 return ERROR_COMMAND_SYNTAX_ERROR
;
3442 target_addr_t address
;
3443 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3447 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
3449 uint8_t *buffer
= calloc(count
, size
);
3451 LOG_ERROR("Failed to allocate md read buffer");
3455 struct target
*target
= get_current_target(CMD_CTX
);
3456 int retval
= fn(target
, address
, size
, count
, buffer
);
3457 if (retval
== ERROR_OK
)
3458 target_handle_md_output(CMD
, target
, address
, size
, count
, buffer
);
3465 typedef int (*target_write_fn
)(struct target
*target
,
3466 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
3468 static int target_fill_mem(struct target
*target
,
3469 target_addr_t address
,
3477 /* We have to write in reasonably large chunks to be able
3478 * to fill large memory areas with any sane speed */
3479 const unsigned chunk_size
= 16384;
3480 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
3482 LOG_ERROR("Out of memory");
3486 for (unsigned i
= 0; i
< chunk_size
; i
++) {
3487 switch (data_size
) {
3489 target_buffer_set_u64(target
, target_buf
+ i
* data_size
, b
);
3492 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
3495 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
3498 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
3505 int retval
= ERROR_OK
;
3507 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
3510 if (current
> chunk_size
)
3511 current
= chunk_size
;
3512 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
3513 if (retval
!= ERROR_OK
)
3515 /* avoid GDB timeouts */
3518 if (openocd_is_shutdown_pending()) {
3519 retval
= ERROR_SERVER_INTERRUPTED
;
3529 COMMAND_HANDLER(handle_mw_command
)
3532 return ERROR_COMMAND_SYNTAX_ERROR
;
3533 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3538 fn
= target_write_phys_memory
;
3540 fn
= target_write_memory
;
3541 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
3542 return ERROR_COMMAND_SYNTAX_ERROR
;
3544 target_addr_t address
;
3545 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3548 COMMAND_PARSE_NUMBER(u64
, CMD_ARGV
[1], value
);
3552 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
3554 struct target
*target
= get_current_target(CMD_CTX
);
3556 switch (CMD_NAME
[2]) {
3570 return ERROR_COMMAND_SYNTAX_ERROR
;
3573 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
3576 static COMMAND_HELPER(parse_load_image_command
, struct image
*image
,
3577 target_addr_t
*min_address
, target_addr_t
*max_address
)
3579 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
3580 return ERROR_COMMAND_SYNTAX_ERROR
;
3582 /* a base address isn't always necessary,
3583 * default to 0x0 (i.e. don't relocate) */
3584 if (CMD_ARGC
>= 2) {
3586 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3587 image
->base_address
= addr
;
3588 image
->base_address_set
= true;
3590 image
->base_address_set
= false;
3592 image
->start_address_set
= false;
3595 COMMAND_PARSE_ADDRESS(CMD_ARGV
[3], *min_address
);
3596 if (CMD_ARGC
== 5) {
3597 COMMAND_PARSE_ADDRESS(CMD_ARGV
[4], *max_address
);
3598 /* use size (given) to find max (required) */
3599 *max_address
+= *min_address
;
3602 if (*min_address
> *max_address
)
3603 return ERROR_COMMAND_SYNTAX_ERROR
;
3608 COMMAND_HANDLER(handle_load_image_command
)
3612 uint32_t image_size
;
3613 target_addr_t min_address
= 0;
3614 target_addr_t max_address
= -1;
3617 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command
,
3618 &image
, &min_address
, &max_address
);
3619 if (retval
!= ERROR_OK
)
3622 struct target
*target
= get_current_target(CMD_CTX
);
3624 struct duration bench
;
3625 duration_start(&bench
);
3627 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
3632 for (unsigned int i
= 0; i
< image
.num_sections
; i
++) {
3633 buffer
= malloc(image
.sections
[i
].size
);
3636 "error allocating buffer for section (%d bytes)",
3637 (int)(image
.sections
[i
].size
));
3638 retval
= ERROR_FAIL
;
3642 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3643 if (retval
!= ERROR_OK
) {
3648 uint32_t offset
= 0;
3649 uint32_t length
= buf_cnt
;
3651 /* DANGER!!! beware of unsigned comparison here!!! */
3653 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3654 (image
.sections
[i
].base_address
< max_address
)) {
3656 if (image
.sections
[i
].base_address
< min_address
) {
3657 /* clip addresses below */
3658 offset
+= min_address
-image
.sections
[i
].base_address
;
3662 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3663 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3665 retval
= target_write_buffer(target
,
3666 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3667 if (retval
!= ERROR_OK
) {
3671 image_size
+= length
;
3672 command_print(CMD
, "%u bytes written at address " TARGET_ADDR_FMT
"",
3673 (unsigned int)length
,
3674 image
.sections
[i
].base_address
+ offset
);
3680 if ((retval
== ERROR_OK
) && (duration_measure(&bench
) == ERROR_OK
)) {
3681 command_print(CMD
, "downloaded %" PRIu32
" bytes "
3682 "in %fs (%0.3f KiB/s)", image_size
,
3683 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3686 image_close(&image
);
3692 COMMAND_HANDLER(handle_dump_image_command
)
3694 struct fileio
*fileio
;
3696 int retval
, retvaltemp
;
3697 target_addr_t address
, size
;
3698 struct duration bench
;
3699 struct target
*target
= get_current_target(CMD_CTX
);
3702 return ERROR_COMMAND_SYNTAX_ERROR
;
3704 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], address
);
3705 COMMAND_PARSE_ADDRESS(CMD_ARGV
[2], size
);
3707 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
3708 buffer
= malloc(buf_size
);
3712 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3713 if (retval
!= ERROR_OK
) {
3718 duration_start(&bench
);
3721 size_t size_written
;
3722 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
3723 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
3724 if (retval
!= ERROR_OK
)
3727 retval
= fileio_write(fileio
, this_run_size
, buffer
, &size_written
);
3728 if (retval
!= ERROR_OK
)
3731 size
-= this_run_size
;
3732 address
+= this_run_size
;
3737 if ((retval
== ERROR_OK
) && (duration_measure(&bench
) == ERROR_OK
)) {
3739 retval
= fileio_size(fileio
, &filesize
);
3740 if (retval
!= ERROR_OK
)
3743 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize
,
3744 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3747 retvaltemp
= fileio_close(fileio
);
3748 if (retvaltemp
!= ERROR_OK
)
3757 IMAGE_CHECKSUM_ONLY
= 2
3760 static COMMAND_HELPER(handle_verify_image_command_internal
, enum verify_mode verify
)
3764 uint32_t image_size
;
3766 uint32_t checksum
= 0;
3767 uint32_t mem_checksum
= 0;
3771 struct target
*target
= get_current_target(CMD_CTX
);
3774 return ERROR_COMMAND_SYNTAX_ERROR
;
3777 LOG_ERROR("no target selected");
3781 struct duration bench
;
3782 duration_start(&bench
);
3784 if (CMD_ARGC
>= 2) {
3786 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3787 image
.base_address
= addr
;
3788 image
.base_address_set
= true;
3790 image
.base_address_set
= false;
3791 image
.base_address
= 0x0;
3794 image
.start_address_set
= false;
3796 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3797 if (retval
!= ERROR_OK
)
3803 for (unsigned int i
= 0; i
< image
.num_sections
; i
++) {
3804 buffer
= malloc(image
.sections
[i
].size
);
3807 "error allocating buffer for section (%" PRIu32
" bytes)",
3808 image
.sections
[i
].size
);
3811 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3812 if (retval
!= ERROR_OK
) {
3817 if (verify
>= IMAGE_VERIFY
) {
3818 /* calculate checksum of image */
3819 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3820 if (retval
!= ERROR_OK
) {
3825 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3826 if (retval
!= ERROR_OK
) {
3830 if ((checksum
!= mem_checksum
) && (verify
== IMAGE_CHECKSUM_ONLY
)) {
3831 LOG_ERROR("checksum mismatch");
3833 retval
= ERROR_FAIL
;
3836 if (checksum
!= mem_checksum
) {
3837 /* failed crc checksum, fall back to a binary compare */
3841 LOG_ERROR("checksum mismatch - attempting binary compare");
3843 data
= malloc(buf_cnt
);
3845 retval
= target_read_buffer(target
, image
.sections
[i
].base_address
, buf_cnt
, data
);
3846 if (retval
== ERROR_OK
) {
3848 for (t
= 0; t
< buf_cnt
; t
++) {
3849 if (data
[t
] != buffer
[t
]) {
3851 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3853 (unsigned)(t
+ image
.sections
[i
].base_address
),
3856 if (diffs
++ >= 127) {
3857 command_print(CMD
, "More than 128 errors, the rest are not printed.");
3864 if (openocd_is_shutdown_pending()) {
3865 retval
= ERROR_SERVER_INTERRUPTED
;
3875 command_print(CMD
, "address " TARGET_ADDR_FMT
" length 0x%08zx",
3876 image
.sections
[i
].base_address
,
3881 image_size
+= buf_cnt
;
3884 command_print(CMD
, "No more differences found.");
3887 retval
= ERROR_FAIL
;
3888 if ((retval
== ERROR_OK
) && (duration_measure(&bench
) == ERROR_OK
)) {
3889 command_print(CMD
, "verified %" PRIu32
" bytes "
3890 "in %fs (%0.3f KiB/s)", image_size
,
3891 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3894 image_close(&image
);
3899 COMMAND_HANDLER(handle_verify_image_checksum_command
)
3901 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_CHECKSUM_ONLY
);
3904 COMMAND_HANDLER(handle_verify_image_command
)
3906 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_VERIFY
);
3909 COMMAND_HANDLER(handle_test_image_command
)
3911 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_TEST
);
3914 static int handle_bp_command_list(struct command_invocation
*cmd
)
3916 struct target
*target
= get_current_target(cmd
->ctx
);
3917 struct breakpoint
*breakpoint
= target
->breakpoints
;
3918 while (breakpoint
) {
3919 if (breakpoint
->type
== BKPT_SOFT
) {
3920 char *buf
= buf_to_hex_str(breakpoint
->orig_instr
,
3921 breakpoint
->length
);
3922 command_print(cmd
, "Software breakpoint(IVA): addr=" TARGET_ADDR_FMT
", len=0x%x, orig_instr=0x%s",
3923 breakpoint
->address
,
3928 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3929 command_print(cmd
, "Context breakpoint: asid=0x%8.8" PRIx32
", len=0x%x, num=%u",
3931 breakpoint
->length
, breakpoint
->number
);
3932 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3933 command_print(cmd
, "Hybrid breakpoint(IVA): addr=" TARGET_ADDR_FMT
", len=0x%x, num=%u",
3934 breakpoint
->address
,
3935 breakpoint
->length
, breakpoint
->number
);
3936 command_print(cmd
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3939 command_print(cmd
, "Hardware breakpoint(IVA): addr=" TARGET_ADDR_FMT
", len=0x%x, num=%u",
3940 breakpoint
->address
,
3941 breakpoint
->length
, breakpoint
->number
);
3944 breakpoint
= breakpoint
->next
;
3949 static int handle_bp_command_set(struct command_invocation
*cmd
,
3950 target_addr_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3952 struct target
*target
= get_current_target(cmd
->ctx
);
3956 retval
= breakpoint_add(target
, addr
, length
, hw
);
3957 /* error is always logged in breakpoint_add(), do not print it again */
3958 if (retval
== ERROR_OK
)
3959 command_print(cmd
, "breakpoint set at " TARGET_ADDR_FMT
"", addr
);
3961 } else if (addr
== 0) {
3962 if (!target
->type
->add_context_breakpoint
) {
3963 LOG_TARGET_ERROR(target
, "Context breakpoint not available");
3964 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
3966 retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3967 /* error is always logged in context_breakpoint_add(), do not print it again */
3968 if (retval
== ERROR_OK
)
3969 command_print(cmd
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3972 if (!target
->type
->add_hybrid_breakpoint
) {
3973 LOG_TARGET_ERROR(target
, "Hybrid breakpoint not available");
3974 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
3976 retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3977 /* error is always logged in hybrid_breakpoint_add(), do not print it again */
3978 if (retval
== ERROR_OK
)
3979 command_print(cmd
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3984 COMMAND_HANDLER(handle_bp_command
)
3993 return handle_bp_command_list(CMD
);
3997 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3998 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3999 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
4002 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
4004 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4005 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
4007 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
4008 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
4010 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
4011 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
4013 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
4018 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4019 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
4020 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
4021 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
4024 return ERROR_COMMAND_SYNTAX_ERROR
;
4028 COMMAND_HANDLER(handle_rbp_command
)
4033 return ERROR_COMMAND_SYNTAX_ERROR
;
4035 struct target
*target
= get_current_target(CMD_CTX
);
4037 if (!strcmp(CMD_ARGV
[0], "all")) {
4038 retval
= breakpoint_remove_all(target
);
4040 if (retval
!= ERROR_OK
) {
4041 command_print(CMD
, "Error encountered during removal of all breakpoints.");
4042 command_print(CMD
, "Some breakpoints may have remained set.");
4046 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4048 retval
= breakpoint_remove(target
, addr
);
4050 if (retval
!= ERROR_OK
)
4051 command_print(CMD
, "Error during removal of breakpoint at address " TARGET_ADDR_FMT
, addr
);
4057 COMMAND_HANDLER(handle_wp_command
)
4059 struct target
*target
= get_current_target(CMD_CTX
);
4061 if (CMD_ARGC
== 0) {
4062 struct watchpoint
*watchpoint
= target
->watchpoints
;
4064 while (watchpoint
) {
4065 char wp_type
= (watchpoint
->rw
== WPT_READ
? 'r' : (watchpoint
->rw
== WPT_WRITE
? 'w' : 'a'));
4066 command_print(CMD
, "address: " TARGET_ADDR_FMT
4067 ", len: 0x%8.8" PRIx32
4068 ", r/w/a: %c, value: 0x%8.8" PRIx64
4069 ", mask: 0x%8.8" PRIx64
,
4070 watchpoint
->address
,
4075 watchpoint
= watchpoint
->next
;
4080 enum watchpoint_rw type
= WPT_ACCESS
;
4081 target_addr_t addr
= 0;
4082 uint32_t length
= 0;
4083 uint64_t data_value
= 0x0;
4084 uint64_t data_mask
= WATCHPOINT_IGNORE_DATA_VALUE_MASK
;
4085 bool mask_specified
= false;
4089 COMMAND_PARSE_NUMBER(u64
, CMD_ARGV
[4], data_mask
);
4090 mask_specified
= true;
4093 COMMAND_PARSE_NUMBER(u64
, CMD_ARGV
[3], data_value
);
4094 // if user specified only data value without mask - the mask should be 0
4095 if (!mask_specified
)
4099 switch (CMD_ARGV
[2][0]) {
4110 LOG_TARGET_ERROR(target
, "invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
4111 return ERROR_COMMAND_SYNTAX_ERROR
;
4115 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
4116 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4120 return ERROR_COMMAND_SYNTAX_ERROR
;
4123 int retval
= watchpoint_add(target
, addr
, length
, type
,
4124 data_value
, data_mask
);
4125 if (retval
!= ERROR_OK
)
4126 LOG_TARGET_ERROR(target
, "Failure setting watchpoints");
4131 COMMAND_HANDLER(handle_rwp_command
)
4136 return ERROR_COMMAND_SYNTAX_ERROR
;
4138 struct target
*target
= get_current_target(CMD_CTX
);
4139 if (!strcmp(CMD_ARGV
[0], "all")) {
4140 retval
= watchpoint_remove_all(target
);
4142 if (retval
!= ERROR_OK
) {
4143 command_print(CMD
, "Error encountered during removal of all watchpoints.");
4144 command_print(CMD
, "Some watchpoints may have remained set.");
4148 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4150 retval
= watchpoint_remove(target
, addr
);
4152 if (retval
!= ERROR_OK
)
4153 command_print(CMD
, "Error during removal of watchpoint at address " TARGET_ADDR_FMT
, addr
);
4160 * Translate a virtual address to a physical address.
4162 * The low-level target implementation must have logged a detailed error
4163 * which is forwarded to telnet/GDB session.
4165 COMMAND_HANDLER(handle_virt2phys_command
)
4168 return ERROR_COMMAND_SYNTAX_ERROR
;
4171 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], va
);
4174 struct target
*target
= get_current_target(CMD_CTX
);
4175 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
4176 if (retval
== ERROR_OK
)
4177 command_print(CMD
, "Physical address " TARGET_ADDR_FMT
"", pa
);
4182 static void write_data(FILE *f
, const void *data
, size_t len
)
4184 size_t written
= fwrite(data
, 1, len
, f
);
4186 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
4189 static void write_long(FILE *f
, int l
, struct target
*target
)
4193 target_buffer_set_u32(target
, val
, l
);
4194 write_data(f
, val
, 4);
4197 static void write_string(FILE *f
, char *s
)
4199 write_data(f
, s
, strlen(s
));
4202 typedef unsigned char UNIT
[2]; /* unit of profiling */
4204 /* Dump a gmon.out histogram file. */
4205 static void write_gmon(uint32_t *samples
, uint32_t sample_num
, const char *filename
, bool with_range
,
4206 uint32_t start_address
, uint32_t end_address
, struct target
*target
, uint32_t duration_ms
)
4209 FILE *f
= fopen(filename
, "w");
4212 write_string(f
, "gmon");
4213 write_long(f
, 0x00000001, target
); /* Version */
4214 write_long(f
, 0, target
); /* padding */
4215 write_long(f
, 0, target
); /* padding */
4216 write_long(f
, 0, target
); /* padding */
4218 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
4219 write_data(f
, &zero
, 1);
4221 /* figure out bucket size */
4225 min
= start_address
;
4230 for (i
= 0; i
< sample_num
; i
++) {
4231 if (min
> samples
[i
])
4233 if (max
< samples
[i
])
4237 /* max should be (largest sample + 1)
4238 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
4239 if (max
< UINT32_MAX
)
4242 /* gprof requires (max - min) >= 2 */
4243 while ((max
- min
) < 2) {
4244 if (max
< UINT32_MAX
)
4251 uint32_t address_space
= max
- min
;
4253 /* FIXME: What is the reasonable number of buckets?
4254 * The profiling result will be more accurate if there are enough buckets. */
4255 static const uint32_t max_buckets
= 128 * 1024; /* maximum buckets. */
4256 uint32_t num_buckets
= address_space
/ sizeof(UNIT
);
4257 if (num_buckets
> max_buckets
)
4258 num_buckets
= max_buckets
;
4259 int *buckets
= malloc(sizeof(int) * num_buckets
);
4264 memset(buckets
, 0, sizeof(int) * num_buckets
);
4265 for (i
= 0; i
< sample_num
; i
++) {
4266 uint32_t address
= samples
[i
];
4268 if ((address
< min
) || (max
<= address
))
4271 long long a
= address
- min
;
4272 long long b
= num_buckets
;
4273 long long c
= address_space
;
4274 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
4278 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
4279 write_long(f
, min
, target
); /* low_pc */
4280 write_long(f
, max
, target
); /* high_pc */
4281 write_long(f
, num_buckets
, target
); /* # of buckets */
4282 float sample_rate
= sample_num
/ (duration_ms
/ 1000.0);
4283 write_long(f
, sample_rate
, target
);
4284 write_string(f
, "seconds");
4285 for (i
= 0; i
< (15-strlen("seconds")); i
++)
4286 write_data(f
, &zero
, 1);
4287 write_string(f
, "s");
4289 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
4291 char *data
= malloc(2 * num_buckets
);
4293 for (i
= 0; i
< num_buckets
; i
++) {
4298 data
[i
* 2] = val
&0xff;
4299 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
4302 write_data(f
, data
, num_buckets
* 2);
4310 /* profiling samples the CPU PC as quickly as OpenOCD is able,
4311 * which will be used as a random sampling of PC */
4312 COMMAND_HANDLER(handle_profile_command
)
4314 struct target
*target
= get_current_target(CMD_CTX
);
4316 if ((CMD_ARGC
!= 2) && (CMD_ARGC
!= 4))
4317 return ERROR_COMMAND_SYNTAX_ERROR
;
4319 const uint32_t MAX_PROFILE_SAMPLE_NUM
= 1000000;
4321 uint32_t num_of_samples
;
4322 int retval
= ERROR_OK
;
4323 bool halted_before_profiling
= target
->state
== TARGET_HALTED
;
4325 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], offset
);
4327 uint32_t start_address
= 0;
4328 uint32_t end_address
= 0;
4329 bool with_range
= false;
4330 if (CMD_ARGC
== 4) {
4332 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], start_address
);
4333 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], end_address
);
4334 if (start_address
> end_address
|| (end_address
- start_address
) < 2) {
4335 command_print(CMD
, "Error: end - start < 2");
4336 return ERROR_COMMAND_ARGUMENT_INVALID
;
4340 uint32_t *samples
= malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM
);
4342 LOG_ERROR("No memory to store samples.");
4346 uint64_t timestart_ms
= timeval_ms();
4348 * Some cores let us sample the PC without the
4349 * annoying halt/resume step; for example, ARMv7 PCSR.
4350 * Provide a way to use that more efficient mechanism.
4352 retval
= target_profiling(target
, samples
, MAX_PROFILE_SAMPLE_NUM
,
4353 &num_of_samples
, offset
);
4354 if (retval
!= ERROR_OK
) {
4358 uint32_t duration_ms
= timeval_ms() - timestart_ms
;
4360 assert(num_of_samples
<= MAX_PROFILE_SAMPLE_NUM
);
4362 retval
= target_poll(target
);
4363 if (retval
!= ERROR_OK
) {
4368 if (target
->state
== TARGET_RUNNING
&& halted_before_profiling
) {
4369 /* The target was halted before we started and is running now. Halt it,
4370 * for consistency. */
4371 retval
= target_halt(target
);
4372 if (retval
!= ERROR_OK
) {
4376 } else if (target
->state
== TARGET_HALTED
&& !halted_before_profiling
) {
4377 /* The target was running before we started and is halted now. Resume
4378 * it, for consistency. */
4379 retval
= target_resume(target
, 1, 0, 0, 0);
4380 if (retval
!= ERROR_OK
) {
4386 retval
= target_poll(target
);
4387 if (retval
!= ERROR_OK
) {
4392 write_gmon(samples
, num_of_samples
, CMD_ARGV
[1],
4393 with_range
, start_address
, end_address
, target
, duration_ms
);
4394 command_print(CMD
, "Wrote %s", CMD_ARGV
[1]);
4400 static int new_u64_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint64_t val
)
4403 Jim_Obj
*obj_name
, *obj_val
;
4406 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4410 obj_name
= Jim_NewStringObj(interp
, namebuf
, -1);
4411 jim_wide wide_val
= val
;
4412 obj_val
= Jim_NewWideObj(interp
, wide_val
);
4413 if (!obj_name
|| !obj_val
) {
4418 Jim_IncrRefCount(obj_name
);
4419 Jim_IncrRefCount(obj_val
);
4420 result
= Jim_SetVariable(interp
, obj_name
, obj_val
);
4421 Jim_DecrRefCount(interp
, obj_name
);
4422 Jim_DecrRefCount(interp
, obj_val
);
4424 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
4428 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
4432 LOG_WARNING("DEPRECATED! use 'read_memory' not 'mem2array'");
4434 /* argv[0] = name of array to receive the data
4435 * argv[1] = desired element width in bits
4436 * argv[2] = memory address
4437 * argv[3] = count of times to read
4438 * argv[4] = optional "phys"
4440 if (argc
< 4 || argc
> 5) {
4441 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4445 /* Arg 0: Name of the array variable */
4446 const char *varname
= Jim_GetString(argv
[0], NULL
);
4448 /* Arg 1: Bit width of one element */
4450 e
= Jim_GetLong(interp
, argv
[1], &l
);
4453 const unsigned int width_bits
= l
;
4455 if (width_bits
!= 8 &&
4459 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4460 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4461 "Invalid width param. Must be one of: 8, 16, 32 or 64.", NULL
);
4464 const unsigned int width
= width_bits
/ 8;
4466 /* Arg 2: Memory address */
4468 e
= Jim_GetWide(interp
, argv
[2], &wide_addr
);
4471 target_addr_t addr
= (target_addr_t
)wide_addr
;
4473 /* Arg 3: Number of elements to read */
4474 e
= Jim_GetLong(interp
, argv
[3], &l
);
4480 bool is_phys
= false;
4483 const char *phys
= Jim_GetString(argv
[4], &str_len
);
4484 if (!strncmp(phys
, "phys", str_len
))
4490 /* Argument checks */
4492 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4493 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
4496 if ((addr
+ (len
* width
)) < addr
) {
4497 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4498 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
4502 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4503 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4504 "mem2array: too large read request, exceeds 64K items", NULL
);
4509 ((width
== 2) && ((addr
& 1) == 0)) ||
4510 ((width
== 4) && ((addr
& 3) == 0)) ||
4511 ((width
== 8) && ((addr
& 7) == 0))) {
4512 /* alignment correct */
4515 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4516 sprintf(buf
, "mem2array address: " TARGET_ADDR_FMT
" is not aligned for %" PRIu32
" byte reads",
4519 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4528 const size_t buffersize
= 4096;
4529 uint8_t *buffer
= malloc(buffersize
);
4536 /* Slurp... in buffer size chunks */
4537 const unsigned int max_chunk_len
= buffersize
/ width
;
4538 const size_t chunk_len
= MIN(len
, max_chunk_len
); /* in elements.. */
4542 retval
= target_read_phys_memory(target
, addr
, width
, chunk_len
, buffer
);
4544 retval
= target_read_memory(target
, addr
, width
, chunk_len
, buffer
);
4545 if (retval
!= ERROR_OK
) {
4547 LOG_ERROR("mem2array: Read @ " TARGET_ADDR_FMT
", w=%u, cnt=%zu, failed",
4551 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4552 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
4556 for (size_t i
= 0; i
< chunk_len
; i
++, idx
++) {
4560 v
= target_buffer_get_u64(target
, &buffer
[i
*width
]);
4563 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
4566 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
4569 v
= buffer
[i
] & 0x0ff;
4572 new_u64_array_element(interp
, varname
, idx
, v
);
4575 addr
+= chunk_len
* width
;
4581 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4586 COMMAND_HANDLER(handle_target_read_memory
)
4589 * CMD_ARGV[0] = memory address
4590 * CMD_ARGV[1] = desired element width in bits
4591 * CMD_ARGV[2] = number of elements to read
4592 * CMD_ARGV[3] = optional "phys"
4595 if (CMD_ARGC
< 3 || CMD_ARGC
> 4)
4596 return ERROR_COMMAND_SYNTAX_ERROR
;
4598 /* Arg 1: Memory address. */
4600 COMMAND_PARSE_NUMBER(u64
, CMD_ARGV
[0], addr
);
4602 /* Arg 2: Bit width of one element. */
4603 unsigned int width_bits
;
4604 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], width_bits
);
4606 /* Arg 3: Number of elements to read. */
4608 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
4610 /* Arg 4: Optional 'phys'. */
4611 bool is_phys
= false;
4612 if (CMD_ARGC
== 4) {
4613 if (strcmp(CMD_ARGV
[3], "phys")) {
4614 command_print(CMD
, "invalid argument '%s', must be 'phys'", CMD_ARGV
[3]);
4615 return ERROR_COMMAND_ARGUMENT_INVALID
;
4621 switch (width_bits
) {
4628 command_print(CMD
, "invalid width, must be 8, 16, 32 or 64");
4629 return ERROR_COMMAND_ARGUMENT_INVALID
;
4632 const unsigned int width
= width_bits
/ 8;
4634 if ((addr
+ (count
* width
)) < addr
) {
4635 command_print(CMD
, "read_memory: addr + count wraps to zero");
4636 return ERROR_COMMAND_ARGUMENT_INVALID
;
4639 if (count
> 65536) {
4640 command_print(CMD
, "read_memory: too large read request, exceeds 64K elements");
4641 return ERROR_COMMAND_ARGUMENT_INVALID
;
4644 struct target
*target
= get_current_target(CMD_CTX
);
4646 const size_t buffersize
= 4096;
4647 uint8_t *buffer
= malloc(buffersize
);
4650 LOG_ERROR("Failed to allocate memory");
4654 char *separator
= "";
4656 const unsigned int max_chunk_len
= buffersize
/ width
;
4657 const size_t chunk_len
= MIN(count
, max_chunk_len
);
4662 retval
= target_read_phys_memory(target
, addr
, width
, chunk_len
, buffer
);
4664 retval
= target_read_memory(target
, addr
, width
, chunk_len
, buffer
);
4666 if (retval
!= ERROR_OK
) {
4667 LOG_DEBUG("read_memory: read at " TARGET_ADDR_FMT
" with width=%u and count=%zu failed",
4668 addr
, width_bits
, chunk_len
);
4670 * FIXME: we append the errmsg to the list of value already read.
4671 * Add a way to flush and replace old output, but LOG_DEBUG() it
4673 command_print(CMD
, "read_memory: failed to read memory");
4678 for (size_t i
= 0; i
< chunk_len
; i
++) {
4683 v
= target_buffer_get_u64(target
, &buffer
[i
* width
]);
4686 v
= target_buffer_get_u32(target
, &buffer
[i
* width
]);
4689 v
= target_buffer_get_u16(target
, &buffer
[i
* width
]);
4696 command_print_sameline(CMD
, "%s0x%" PRIx64
, separator
, v
);
4701 addr
+= chunk_len
* width
;
4709 static int get_u64_array_element(Jim_Interp
*interp
, const char *varname
, size_t idx
, uint64_t *val
)
4711 char *namebuf
= alloc_printf("%s(%zu)", varname
, idx
);
4715 Jim_Obj
*obj_name
= Jim_NewStringObj(interp
, namebuf
, -1);
4721 Jim_IncrRefCount(obj_name
);
4722 Jim_Obj
*obj_val
= Jim_GetVariable(interp
, obj_name
, JIM_ERRMSG
);
4723 Jim_DecrRefCount(interp
, obj_name
);
4729 int result
= Jim_GetWide(interp
, obj_val
, &wide_val
);
4734 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
4735 int argc
, Jim_Obj
*const *argv
)
4739 LOG_WARNING("DEPRECATED! use 'write_memory' not 'array2mem'");
4741 /* argv[0] = name of array from which to read the data
4742 * argv[1] = desired element width in bits
4743 * argv[2] = memory address
4744 * argv[3] = number of elements to write
4745 * argv[4] = optional "phys"
4747 if (argc
< 4 || argc
> 5) {
4748 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4752 /* Arg 0: Name of the array variable */
4753 const char *varname
= Jim_GetString(argv
[0], NULL
);
4755 /* Arg 1: Bit width of one element */
4757 e
= Jim_GetLong(interp
, argv
[1], &l
);
4760 const unsigned int width_bits
= l
;
4762 if (width_bits
!= 8 &&
4766 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4767 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4768 "Invalid width param. Must be one of: 8, 16, 32 or 64.", NULL
);
4771 const unsigned int width
= width_bits
/ 8;
4773 /* Arg 2: Memory address */
4775 e
= Jim_GetWide(interp
, argv
[2], &wide_addr
);
4778 target_addr_t addr
= (target_addr_t
)wide_addr
;
4780 /* Arg 3: Number of elements to write */
4781 e
= Jim_GetLong(interp
, argv
[3], &l
);
4787 bool is_phys
= false;
4790 const char *phys
= Jim_GetString(argv
[4], &str_len
);
4791 if (!strncmp(phys
, "phys", str_len
))
4797 /* Argument checks */
4799 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4800 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4801 "array2mem: zero width read?", NULL
);
4805 if ((addr
+ (len
* width
)) < addr
) {
4806 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4807 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4808 "array2mem: addr + len - wraps to zero?", NULL
);
4813 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4814 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4815 "array2mem: too large memory write request, exceeds 64K items", NULL
);
4820 ((width
== 2) && ((addr
& 1) == 0)) ||
4821 ((width
== 4) && ((addr
& 3) == 0)) ||
4822 ((width
== 8) && ((addr
& 7) == 0))) {
4823 /* alignment correct */
4826 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4827 sprintf(buf
, "array2mem address: " TARGET_ADDR_FMT
" is not aligned for %" PRIu32
" byte reads",
4830 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4839 const size_t buffersize
= 4096;
4840 uint8_t *buffer
= malloc(buffersize
);
4848 /* Slurp... in buffer size chunks */
4849 const unsigned int max_chunk_len
= buffersize
/ width
;
4851 const size_t chunk_len
= MIN(len
, max_chunk_len
); /* in elements.. */
4853 /* Fill the buffer */
4854 for (size_t i
= 0; i
< chunk_len
; i
++, idx
++) {
4856 if (get_u64_array_element(interp
, varname
, idx
, &v
) != JIM_OK
) {
4862 target_buffer_set_u64(target
, &buffer
[i
* width
], v
);
4865 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
4868 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
4871 buffer
[i
] = v
& 0x0ff;
4877 /* Write the buffer to memory */
4880 retval
= target_write_phys_memory(target
, addr
, width
, chunk_len
, buffer
);
4882 retval
= target_write_memory(target
, addr
, width
, chunk_len
, buffer
);
4883 if (retval
!= ERROR_OK
) {
4885 LOG_ERROR("array2mem: Write @ " TARGET_ADDR_FMT
", w=%u, cnt=%zu, failed",
4889 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4890 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
4894 addr
+= chunk_len
* width
;
4899 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4904 static int target_jim_write_memory(Jim_Interp
*interp
, int argc
,
4905 Jim_Obj
* const *argv
)
4908 * argv[1] = memory address
4909 * argv[2] = desired element width in bits
4910 * argv[3] = list of data to write
4911 * argv[4] = optional "phys"
4914 if (argc
< 4 || argc
> 5) {
4915 Jim_WrongNumArgs(interp
, 1, argv
, "address width data ['phys']");
4919 /* Arg 1: Memory address. */
4922 e
= Jim_GetWide(interp
, argv
[1], &wide_addr
);
4927 target_addr_t addr
= (target_addr_t
)wide_addr
;
4929 /* Arg 2: Bit width of one element. */
4931 e
= Jim_GetLong(interp
, argv
[2], &l
);
4936 const unsigned int width_bits
= l
;
4937 size_t count
= Jim_ListLength(interp
, argv
[3]);
4939 /* Arg 4: Optional 'phys'. */
4940 bool is_phys
= false;
4943 const char *phys
= Jim_GetString(argv
[4], NULL
);
4945 if (strcmp(phys
, "phys")) {
4946 Jim_SetResultFormatted(interp
, "invalid argument '%s', must be 'phys'", phys
);
4953 switch (width_bits
) {
4960 Jim_SetResultString(interp
, "invalid width, must be 8, 16, 32 or 64", -1);
4964 const unsigned int width
= width_bits
/ 8;
4966 if ((addr
+ (count
* width
)) < addr
) {
4967 Jim_SetResultString(interp
, "write_memory: addr + len wraps to zero", -1);
4971 if (count
> 65536) {
4972 Jim_SetResultString(interp
, "write_memory: too large memory write request, exceeds 64K elements", -1);
4976 struct command_context
*cmd_ctx
= current_command_context(interp
);
4977 assert(cmd_ctx
!= NULL
);
4978 struct target
*target
= get_current_target(cmd_ctx
);
4980 const size_t buffersize
= 4096;
4981 uint8_t *buffer
= malloc(buffersize
);
4984 LOG_ERROR("Failed to allocate memory");
4991 const unsigned int max_chunk_len
= buffersize
/ width
;
4992 const size_t chunk_len
= MIN(count
, max_chunk_len
);
4994 for (size_t i
= 0; i
< chunk_len
; i
++, j
++) {
4995 Jim_Obj
*tmp
= Jim_ListGetIndex(interp
, argv
[3], j
);
4996 jim_wide element_wide
;
4997 Jim_GetWide(interp
, tmp
, &element_wide
);
4999 const uint64_t v
= element_wide
;
5003 target_buffer_set_u64(target
, &buffer
[i
* width
], v
);
5006 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
5009 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
5012 buffer
[i
] = v
& 0x0ff;
5022 retval
= target_write_phys_memory(target
, addr
, width
, chunk_len
, buffer
);
5024 retval
= target_write_memory(target
, addr
, width
, chunk_len
, buffer
);
5026 if (retval
!= ERROR_OK
) {
5027 LOG_ERROR("write_memory: write at " TARGET_ADDR_FMT
" with width=%u and count=%zu failed",
5028 addr
, width_bits
, chunk_len
);
5029 Jim_SetResultString(interp
, "write_memory: failed to write memory", -1);
5034 addr
+= chunk_len
* width
;
5042 /* FIX? should we propagate errors here rather than printing them
5045 void target_handle_event(struct target
*target
, enum target_event e
)
5047 struct target_event_action
*teap
;
5050 for (teap
= target
->event_action
; teap
; teap
= teap
->next
) {
5051 if (teap
->event
== e
) {
5052 LOG_DEBUG("target: %s (%s) event: %d (%s) action: %s",
5053 target_name(target
),
5054 target_type_name(target
),
5056 target_event_name(e
),
5057 Jim_GetString(teap
->body
, NULL
));
5059 /* Override current target by the target an event
5060 * is issued from (lot of scripts need it).
5061 * Return back to previous override as soon
5062 * as the handler processing is done */
5063 struct command_context
*cmd_ctx
= current_command_context(teap
->interp
);
5064 struct target
*saved_target_override
= cmd_ctx
->current_target_override
;
5065 cmd_ctx
->current_target_override
= target
;
5067 retval
= Jim_EvalObj(teap
->interp
, teap
->body
);
5069 cmd_ctx
->current_target_override
= saved_target_override
;
5071 if (retval
== ERROR_COMMAND_CLOSE_CONNECTION
)
5074 if (retval
== JIM_RETURN
)
5075 retval
= teap
->interp
->returnCode
;
5077 if (retval
!= JIM_OK
) {
5078 Jim_MakeErrorMessage(teap
->interp
);
5079 LOG_USER("Error executing event %s on target %s:\n%s",
5080 target_event_name(e
),
5081 target_name(target
),
5082 Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
5083 /* clean both error code and stacktrace before return */
5084 Jim_Eval(teap
->interp
, "error \"\" \"\"");
5090 static int target_jim_get_reg(Jim_Interp
*interp
, int argc
,
5091 Jim_Obj
* const *argv
)
5096 const char *option
= Jim_GetString(argv
[1], NULL
);
5098 if (!strcmp(option
, "-force")) {
5103 Jim_SetResultFormatted(interp
, "invalid option '%s'", option
);
5109 Jim_WrongNumArgs(interp
, 1, argv
, "[-force] list");
5113 const int length
= Jim_ListLength(interp
, argv
[1]);
5115 Jim_Obj
*result_dict
= Jim_NewDictObj(interp
, NULL
, 0);
5120 struct command_context
*cmd_ctx
= current_command_context(interp
);
5121 assert(cmd_ctx
!= NULL
);
5122 const struct target
*target
= get_current_target(cmd_ctx
);
5124 for (int i
= 0; i
< length
; i
++) {
5125 Jim_Obj
*elem
= Jim_ListGetIndex(interp
, argv
[1], i
);
5130 const char *reg_name
= Jim_String(elem
);
5132 struct reg
*reg
= register_get_by_name(target
->reg_cache
, reg_name
,
5135 if (!reg
|| !reg
->exist
) {
5136 Jim_SetResultFormatted(interp
, "unknown register '%s'", reg_name
);
5140 if (force
|| !reg
->valid
) {
5141 int retval
= reg
->type
->get(reg
);
5143 if (retval
!= ERROR_OK
) {
5144 Jim_SetResultFormatted(interp
, "failed to read register '%s'",
5150 char *reg_value
= buf_to_hex_str(reg
->value
, reg
->size
);
5153 LOG_ERROR("Failed to allocate memory");
5157 char *tmp
= alloc_printf("0x%s", reg_value
);
5162 LOG_ERROR("Failed to allocate memory");
5166 Jim_DictAddElement(interp
, result_dict
, elem
,
5167 Jim_NewStringObj(interp
, tmp
, -1));
5172 Jim_SetResult(interp
, result_dict
);
5177 static int target_jim_set_reg(Jim_Interp
*interp
, int argc
,
5178 Jim_Obj
* const *argv
)
5181 Jim_WrongNumArgs(interp
, 1, argv
, "dict");
5186 #if JIM_VERSION >= 80
5187 Jim_Obj
**dict
= Jim_DictPairs(interp
, argv
[1], &tmp
);
5193 int ret
= Jim_DictPairs(interp
, argv
[1], &dict
, &tmp
);
5199 const unsigned int length
= tmp
;
5200 struct command_context
*cmd_ctx
= current_command_context(interp
);
5202 const struct target
*target
= get_current_target(cmd_ctx
);
5204 for (unsigned int i
= 0; i
< length
; i
+= 2) {
5205 const char *reg_name
= Jim_String(dict
[i
]);
5206 const char *reg_value
= Jim_String(dict
[i
+ 1]);
5207 struct reg
*reg
= register_get_by_name(target
->reg_cache
, reg_name
,
5210 if (!reg
|| !reg
->exist
) {
5211 Jim_SetResultFormatted(interp
, "unknown register '%s'", reg_name
);
5215 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
5218 LOG_ERROR("Failed to allocate memory");
5222 str_to_buf(reg_value
, strlen(reg_value
), buf
, reg
->size
, 0);
5223 int retval
= reg
->type
->set(reg
, buf
);
5226 if (retval
!= ERROR_OK
) {
5227 Jim_SetResultFormatted(interp
, "failed to set '%s' to register '%s'",
5228 reg_value
, reg_name
);
5237 * Returns true only if the target has a handler for the specified event.
5239 bool target_has_event_action(struct target
*target
, enum target_event event
)
5241 struct target_event_action
*teap
;
5243 for (teap
= target
->event_action
; teap
; teap
= teap
->next
) {
5244 if (teap
->event
== event
)
5250 enum target_cfg_param
{
5253 TCFG_WORK_AREA_VIRT
,
5254 TCFG_WORK_AREA_PHYS
,
5255 TCFG_WORK_AREA_SIZE
,
5256 TCFG_WORK_AREA_BACKUP
,
5259 TCFG_CHAIN_POSITION
,
5264 TCFG_GDB_MAX_CONNECTIONS
,
5267 static struct jim_nvp nvp_config_opts
[] = {
5268 { .name
= "-type", .value
= TCFG_TYPE
},
5269 { .name
= "-event", .value
= TCFG_EVENT
},
5270 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
5271 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
5272 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
5273 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
5274 { .name
= "-endian", .value
= TCFG_ENDIAN
},
5275 { .name
= "-coreid", .value
= TCFG_COREID
},
5276 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
5277 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
5278 { .name
= "-rtos", .value
= TCFG_RTOS
},
5279 { .name
= "-defer-examine", .value
= TCFG_DEFER_EXAMINE
},
5280 { .name
= "-gdb-port", .value
= TCFG_GDB_PORT
},
5281 { .name
= "-gdb-max-connections", .value
= TCFG_GDB_MAX_CONNECTIONS
},
5282 { .name
= NULL
, .value
= -1 }
5285 static int target_configure(struct jim_getopt_info
*goi
, struct target
*target
)
5292 /* parse config or cget options ... */
5293 while (goi
->argc
> 0) {
5294 Jim_SetEmptyResult(goi
->interp
);
5295 /* jim_getopt_debug(goi); */
5297 if (target
->type
->target_jim_configure
) {
5298 /* target defines a configure function */
5299 /* target gets first dibs on parameters */
5300 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
5309 /* otherwise we 'continue' below */
5311 e
= jim_getopt_nvp(goi
, nvp_config_opts
, &n
);
5313 jim_getopt_nvp_unknown(goi
, nvp_config_opts
, 0);
5319 if (goi
->isconfigure
) {
5320 Jim_SetResultFormatted(goi
->interp
,
5321 "not settable: %s", n
->name
);
5325 if (goi
->argc
!= 0) {
5326 Jim_WrongNumArgs(goi
->interp
,
5327 goi
->argc
, goi
->argv
,
5332 Jim_SetResultString(goi
->interp
,
5333 target_type_name(target
), -1);
5337 if (goi
->argc
== 0) {
5338 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
5342 e
= jim_getopt_nvp(goi
, nvp_target_event
, &n
);
5344 jim_getopt_nvp_unknown(goi
, nvp_target_event
, 1);
5348 if (goi
->isconfigure
) {
5349 if (goi
->argc
!= 1) {
5350 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
5354 if (goi
->argc
!= 0) {
5355 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
5361 struct target_event_action
*teap
;
5363 teap
= target
->event_action
;
5364 /* replace existing? */
5366 if (teap
->event
== (enum target_event
)n
->value
)
5371 if (goi
->isconfigure
) {
5372 /* START_DEPRECATED_TPIU */
5373 if (n
->value
== TARGET_EVENT_TRACE_CONFIG
)
5374 LOG_INFO("DEPRECATED target event %s; use TPIU events {pre,post}-{enable,disable}", n
->name
);
5375 /* END_DEPRECATED_TPIU */
5377 bool replace
= true;
5380 teap
= calloc(1, sizeof(*teap
));
5383 teap
->event
= n
->value
;
5384 teap
->interp
= goi
->interp
;
5385 jim_getopt_obj(goi
, &o
);
5387 Jim_DecrRefCount(teap
->interp
, teap
->body
);
5388 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
5391 * Tcl/TK - "tk events" have a nice feature.
5392 * See the "BIND" command.
5393 * We should support that here.
5394 * You can specify %X and %Y in the event code.
5395 * The idea is: %T - target name.
5396 * The idea is: %N - target number
5397 * The idea is: %E - event name.
5399 Jim_IncrRefCount(teap
->body
);
5402 /* add to head of event list */
5403 teap
->next
= target
->event_action
;
5404 target
->event_action
= teap
;
5406 Jim_SetEmptyResult(goi
->interp
);
5410 Jim_SetEmptyResult(goi
->interp
);
5412 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
5418 case TCFG_WORK_AREA_VIRT
:
5419 if (goi
->isconfigure
) {
5420 target_free_all_working_areas(target
);
5421 e
= jim_getopt_wide(goi
, &w
);
5424 target
->working_area_virt
= w
;
5425 target
->working_area_virt_spec
= true;
5430 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
5434 case TCFG_WORK_AREA_PHYS
:
5435 if (goi
->isconfigure
) {
5436 target_free_all_working_areas(target
);
5437 e
= jim_getopt_wide(goi
, &w
);
5440 target
->working_area_phys
= w
;
5441 target
->working_area_phys_spec
= true;
5446 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
5450 case TCFG_WORK_AREA_SIZE
:
5451 if (goi
->isconfigure
) {
5452 target_free_all_working_areas(target
);
5453 e
= jim_getopt_wide(goi
, &w
);
5456 target
->working_area_size
= w
;
5461 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
5465 case TCFG_WORK_AREA_BACKUP
:
5466 if (goi
->isconfigure
) {
5467 target_free_all_working_areas(target
);
5468 e
= jim_getopt_wide(goi
, &w
);
5471 /* make this boolean */
5472 target
->backup_working_area
= (w
!= 0);
5477 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
? 1 : 0));
5478 /* loop for more e*/
5483 if (goi
->isconfigure
) {
5484 e
= jim_getopt_nvp(goi
, nvp_target_endian
, &n
);
5486 jim_getopt_nvp_unknown(goi
, nvp_target_endian
, 1);
5489 target
->endianness
= n
->value
;
5494 n
= jim_nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
5496 target
->endianness
= TARGET_LITTLE_ENDIAN
;
5497 n
= jim_nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
5499 Jim_SetResultString(goi
->interp
, n
->name
, -1);
5504 if (goi
->isconfigure
) {
5505 e
= jim_getopt_wide(goi
, &w
);
5508 target
->coreid
= (int32_t)w
;
5513 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->coreid
));
5517 case TCFG_CHAIN_POSITION
:
5518 if (goi
->isconfigure
) {
5520 struct jtag_tap
*tap
;
5522 if (target
->has_dap
) {
5523 Jim_SetResultString(goi
->interp
,
5524 "target requires -dap parameter instead of -chain-position!", -1);
5528 target_free_all_working_areas(target
);
5529 e
= jim_getopt_obj(goi
, &o_t
);
5532 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
5536 target
->tap_configured
= true;
5541 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
5542 /* loop for more e*/
5545 if (goi
->isconfigure
) {
5546 e
= jim_getopt_wide(goi
, &w
);
5549 target
->dbgbase
= (uint32_t)w
;
5550 target
->dbgbase_set
= true;
5555 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
5561 int result
= rtos_create(goi
, target
);
5562 if (result
!= JIM_OK
)
5568 case TCFG_DEFER_EXAMINE
:
5570 target
->defer_examine
= true;
5575 if (goi
->isconfigure
) {
5576 struct command_context
*cmd_ctx
= current_command_context(goi
->interp
);
5577 if (cmd_ctx
->mode
!= COMMAND_CONFIG
) {
5578 Jim_SetResultString(goi
->interp
, "-gdb-port must be configured before 'init'", -1);
5583 e
= jim_getopt_string(goi
, &s
, NULL
);
5586 free(target
->gdb_port_override
);
5587 target
->gdb_port_override
= strdup(s
);
5592 Jim_SetResultString(goi
->interp
, target
->gdb_port_override
? target
->gdb_port_override
: "undefined", -1);
5596 case TCFG_GDB_MAX_CONNECTIONS
:
5597 if (goi
->isconfigure
) {
5598 struct command_context
*cmd_ctx
= current_command_context(goi
->interp
);
5599 if (cmd_ctx
->mode
!= COMMAND_CONFIG
) {
5600 Jim_SetResultString(goi
->interp
, "-gdb-max-connections must be configured before 'init'", -1);
5604 e
= jim_getopt_wide(goi
, &w
);
5607 target
->gdb_max_connections
= (w
< 0) ? CONNECTION_LIMIT_UNLIMITED
: (int)w
;
5612 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->gdb_max_connections
));
5615 } /* while (goi->argc) */
5618 /* done - we return */
5622 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5624 struct command
*c
= jim_to_command(interp
);
5625 struct jim_getopt_info goi
;
5627 jim_getopt_setup(&goi
, interp
, argc
- 1, argv
+ 1);
5628 goi
.isconfigure
= !strcmp(c
->name
, "configure");
5630 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5631 "missing: -option ...");
5634 struct command_context
*cmd_ctx
= current_command_context(interp
);
5636 struct target
*target
= get_current_target(cmd_ctx
);
5637 return target_configure(&goi
, target
);
5640 static int jim_target_mem2array(Jim_Interp
*interp
,
5641 int argc
, Jim_Obj
*const *argv
)
5643 struct command_context
*cmd_ctx
= current_command_context(interp
);
5645 struct target
*target
= get_current_target(cmd_ctx
);
5646 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
5649 static int jim_target_array2mem(Jim_Interp
*interp
,
5650 int argc
, Jim_Obj
*const *argv
)
5652 struct command_context
*cmd_ctx
= current_command_context(interp
);
5654 struct target
*target
= get_current_target(cmd_ctx
);
5655 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
5658 COMMAND_HANDLER(handle_target_examine
)
5660 bool allow_defer
= false;
5663 return ERROR_COMMAND_SYNTAX_ERROR
;
5665 if (CMD_ARGC
== 1) {
5666 if (strcmp(CMD_ARGV
[0], "allow-defer"))
5667 return ERROR_COMMAND_ARGUMENT_INVALID
;
5671 struct target
*target
= get_current_target(CMD_CTX
);
5672 if (!target
->tap
->enabled
) {
5673 command_print(CMD
, "[TAP is disabled]");
5677 if (allow_defer
&& target
->defer_examine
) {
5678 LOG_INFO("Deferring arp_examine of %s", target_name(target
));
5679 LOG_INFO("Use arp_examine command to examine it manually!");
5683 int retval
= target
->type
->examine(target
);
5684 if (retval
!= ERROR_OK
) {
5685 target_reset_examined(target
);
5689 target_set_examined(target
);
5694 COMMAND_HANDLER(handle_target_was_examined
)
5697 return ERROR_COMMAND_SYNTAX_ERROR
;
5699 struct target
*target
= get_current_target(CMD_CTX
);
5701 command_print(CMD
, "%d", target_was_examined(target
) ? 1 : 0);
5706 COMMAND_HANDLER(handle_target_examine_deferred
)
5709 return ERROR_COMMAND_SYNTAX_ERROR
;
5711 struct target
*target
= get_current_target(CMD_CTX
);
5713 command_print(CMD
, "%d", target
->defer_examine
? 1 : 0);
5718 COMMAND_HANDLER(handle_target_halt_gdb
)
5721 return ERROR_COMMAND_SYNTAX_ERROR
;
5723 struct target
*target
= get_current_target(CMD_CTX
);
5725 return target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
5728 COMMAND_HANDLER(handle_target_poll
)
5731 return ERROR_COMMAND_SYNTAX_ERROR
;
5733 struct target
*target
= get_current_target(CMD_CTX
);
5734 if (!target
->tap
->enabled
) {
5735 command_print(CMD
, "[TAP is disabled]");
5739 if (!(target_was_examined(target
)))
5740 return ERROR_TARGET_NOT_EXAMINED
;
5742 return target
->type
->poll(target
);
5745 COMMAND_HANDLER(handle_target_reset
)
5748 return ERROR_COMMAND_SYNTAX_ERROR
;
5750 const struct nvp
*n
= nvp_name2value(nvp_assert
, CMD_ARGV
[0]);
5752 nvp_unknown_command_print(CMD
, nvp_assert
, NULL
, CMD_ARGV
[0]);
5753 return ERROR_COMMAND_ARGUMENT_INVALID
;
5756 /* the halt or not param */
5758 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[1], a
);
5760 struct target
*target
= get_current_target(CMD_CTX
);
5761 if (!target
->tap
->enabled
) {
5762 command_print(CMD
, "[TAP is disabled]");
5766 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
5767 command_print(CMD
, "No target-specific reset for %s", target_name(target
));
5771 if (target
->defer_examine
)
5772 target_reset_examined(target
);
5774 /* determine if we should halt or not. */
5775 target
->reset_halt
= (a
!= 0);
5776 /* When this happens - all workareas are invalid. */
5777 target_free_all_working_areas_restore(target
, 0);
5780 if (n
->value
== NVP_ASSERT
)
5781 return target
->type
->assert_reset(target
);
5782 return target
->type
->deassert_reset(target
);
5785 COMMAND_HANDLER(handle_target_halt
)
5788 return ERROR_COMMAND_SYNTAX_ERROR
;
5790 struct target
*target
= get_current_target(CMD_CTX
);
5791 if (!target
->tap
->enabled
) {
5792 command_print(CMD
, "[TAP is disabled]");
5796 return target
->type
->halt(target
);
5799 COMMAND_HANDLER(handle_target_wait_state
)
5802 return ERROR_COMMAND_SYNTAX_ERROR
;
5804 const struct nvp
*n
= nvp_name2value(nvp_target_state
, CMD_ARGV
[0]);
5806 nvp_unknown_command_print(CMD
, nvp_target_state
, NULL
, CMD_ARGV
[0]);
5807 return ERROR_COMMAND_ARGUMENT_INVALID
;
5811 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], a
);
5813 struct target
*target
= get_current_target(CMD_CTX
);
5814 if (!target
->tap
->enabled
) {
5815 command_print(CMD
, "[TAP is disabled]");
5819 int retval
= target_wait_state(target
, n
->value
, a
);
5820 if (retval
!= ERROR_OK
) {
5822 "target: %s wait %s fails (%d) %s",
5823 target_name(target
), n
->name
,
5824 retval
, target_strerror_safe(retval
));
5829 /* List for human, Events defined for this target.
5830 * scripts/programs should use 'name cget -event NAME'
5832 COMMAND_HANDLER(handle_target_event_list
)
5834 struct target
*target
= get_current_target(CMD_CTX
);
5835 struct target_event_action
*teap
= target
->event_action
;
5837 command_print(CMD
, "Event actions for target %s\n",
5838 target_name(target
));
5839 command_print(CMD
, "%-25s | Body", "Event");
5840 command_print(CMD
, "------------------------- | "
5841 "----------------------------------------");
5843 command_print(CMD
, "%-25s | %s",
5844 target_event_name(teap
->event
),
5845 Jim_GetString(teap
->body
, NULL
));
5848 command_print(CMD
, "***END***");
5852 COMMAND_HANDLER(handle_target_current_state
)
5855 return ERROR_COMMAND_SYNTAX_ERROR
;
5857 struct target
*target
= get_current_target(CMD_CTX
);
5859 command_print(CMD
, "%s", target_state_name(target
));
5864 COMMAND_HANDLER(handle_target_debug_reason
)
5867 return ERROR_COMMAND_SYNTAX_ERROR
;
5869 struct target
*target
= get_current_target(CMD_CTX
);
5872 const char *debug_reason
= nvp_value2name(nvp_target_debug_reason
,
5873 target
->debug_reason
)->name
;
5875 if (!debug_reason
) {
5876 command_print(CMD
, "bug: invalid debug reason (%d)",
5877 target
->debug_reason
);
5881 command_print(CMD
, "%s", debug_reason
);
5886 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5888 struct jim_getopt_info goi
;
5889 jim_getopt_setup(&goi
, interp
, argc
- 1, argv
+ 1);
5890 if (goi
.argc
!= 1) {
5891 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5892 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
5896 int e
= jim_getopt_nvp(&goi
, nvp_target_event
, &n
);
5898 jim_getopt_nvp_unknown(&goi
, nvp_target_event
, 1);
5901 struct command_context
*cmd_ctx
= current_command_context(interp
);
5903 struct target
*target
= get_current_target(cmd_ctx
);
5904 target_handle_event(target
, n
->value
);
5908 static const struct command_registration target_instance_command_handlers
[] = {
5910 .name
= "configure",
5911 .mode
= COMMAND_ANY
,
5912 .jim_handler
= jim_target_configure
,
5913 .help
= "configure a new target for use",
5914 .usage
= "[target_attribute ...]",
5918 .mode
= COMMAND_ANY
,
5919 .jim_handler
= jim_target_configure
,
5920 .help
= "returns the specified target attribute",
5921 .usage
= "target_attribute",
5925 .handler
= handle_mw_command
,
5926 .mode
= COMMAND_EXEC
,
5927 .help
= "Write 64-bit word(s) to target memory",
5928 .usage
= "address data [count]",
5932 .handler
= handle_mw_command
,
5933 .mode
= COMMAND_EXEC
,
5934 .help
= "Write 32-bit word(s) to target memory",
5935 .usage
= "address data [count]",
5939 .handler
= handle_mw_command
,
5940 .mode
= COMMAND_EXEC
,
5941 .help
= "Write 16-bit half-word(s) to target memory",
5942 .usage
= "address data [count]",
5946 .handler
= handle_mw_command
,
5947 .mode
= COMMAND_EXEC
,
5948 .help
= "Write byte(s) to target memory",
5949 .usage
= "address data [count]",
5953 .handler
= handle_md_command
,
5954 .mode
= COMMAND_EXEC
,
5955 .help
= "Display target memory as 64-bit words",
5956 .usage
= "address [count]",
5960 .handler
= handle_md_command
,
5961 .mode
= COMMAND_EXEC
,
5962 .help
= "Display target memory as 32-bit words",
5963 .usage
= "address [count]",
5967 .handler
= handle_md_command
,
5968 .mode
= COMMAND_EXEC
,
5969 .help
= "Display target memory as 16-bit half-words",
5970 .usage
= "address [count]",
5974 .handler
= handle_md_command
,
5975 .mode
= COMMAND_EXEC
,
5976 .help
= "Display target memory as 8-bit bytes",
5977 .usage
= "address [count]",
5980 .name
= "array2mem",
5981 .mode
= COMMAND_EXEC
,
5982 .jim_handler
= jim_target_array2mem
,
5983 .help
= "Writes Tcl array of 8/16/32 bit numbers "
5985 .usage
= "arrayname bitwidth address count",
5988 .name
= "mem2array",
5989 .mode
= COMMAND_EXEC
,
5990 .jim_handler
= jim_target_mem2array
,
5991 .help
= "Loads Tcl array of 8/16/32 bit numbers "
5992 "from target memory",
5993 .usage
= "arrayname bitwidth address count",
5997 .mode
= COMMAND_EXEC
,
5998 .jim_handler
= target_jim_get_reg
,
5999 .help
= "Get register values from the target",
6004 .mode
= COMMAND_EXEC
,
6005 .jim_handler
= target_jim_set_reg
,
6006 .help
= "Set target register values",
6010 .name
= "read_memory",
6011 .mode
= COMMAND_EXEC
,
6012 .handler
= handle_target_read_memory
,
6013 .help
= "Read Tcl list of 8/16/32/64 bit numbers from target memory",
6014 .usage
= "address width count ['phys']",
6017 .name
= "write_memory",
6018 .mode
= COMMAND_EXEC
,
6019 .jim_handler
= target_jim_write_memory
,
6020 .help
= "Write Tcl list of 8/16/32/64 bit numbers to target memory",
6021 .usage
= "address width data ['phys']",
6024 .name
= "eventlist",
6025 .handler
= handle_target_event_list
,
6026 .mode
= COMMAND_EXEC
,
6027 .help
= "displays a table of events defined for this target",
6032 .mode
= COMMAND_EXEC
,
6033 .handler
= handle_target_current_state
,
6034 .help
= "displays the current state of this target",
6038 .name
= "debug_reason",
6039 .mode
= COMMAND_EXEC
,
6040 .handler
= handle_target_debug_reason
,
6041 .help
= "displays the debug reason of this target",
6045 .name
= "arp_examine",
6046 .mode
= COMMAND_EXEC
,
6047 .handler
= handle_target_examine
,
6048 .help
= "used internally for reset processing",
6049 .usage
= "['allow-defer']",
6052 .name
= "was_examined",
6053 .mode
= COMMAND_EXEC
,
6054 .handler
= handle_target_was_examined
,
6055 .help
= "used internally for reset processing",
6059 .name
= "examine_deferred",
6060 .mode
= COMMAND_EXEC
,
6061 .handler
= handle_target_examine_deferred
,
6062 .help
= "used internally for reset processing",
6066 .name
= "arp_halt_gdb",
6067 .mode
= COMMAND_EXEC
,
6068 .handler
= handle_target_halt_gdb
,
6069 .help
= "used internally for reset processing to halt GDB",
6074 .mode
= COMMAND_EXEC
,
6075 .handler
= handle_target_poll
,
6076 .help
= "used internally for reset processing",
6080 .name
= "arp_reset",
6081 .mode
= COMMAND_EXEC
,
6082 .handler
= handle_target_reset
,
6083 .help
= "used internally for reset processing",
6084 .usage
= "'assert'|'deassert' halt",
6088 .mode
= COMMAND_EXEC
,
6089 .handler
= handle_target_halt
,
6090 .help
= "used internally for reset processing",
6094 .name
= "arp_waitstate",
6095 .mode
= COMMAND_EXEC
,
6096 .handler
= handle_target_wait_state
,
6097 .help
= "used internally for reset processing",
6098 .usage
= "statename timeoutmsecs",
6101 .name
= "invoke-event",
6102 .mode
= COMMAND_EXEC
,
6103 .jim_handler
= jim_target_invoke_event
,
6104 .help
= "invoke handler for specified event",
6105 .usage
= "event_name",
6107 COMMAND_REGISTRATION_DONE
6110 static int target_create(struct jim_getopt_info
*goi
)
6117 struct target
*target
;
6118 struct command_context
*cmd_ctx
;
6120 cmd_ctx
= current_command_context(goi
->interp
);
6123 if (goi
->argc
< 3) {
6124 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
6129 jim_getopt_obj(goi
, &new_cmd
);
6130 /* does this command exist? */
6131 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_NONE
);
6133 cp
= Jim_GetString(new_cmd
, NULL
);
6134 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
6139 e
= jim_getopt_string(goi
, &cp
, NULL
);
6142 struct transport
*tr
= get_current_transport();
6143 if (tr
&& tr
->override_target
) {
6144 e
= tr
->override_target(&cp
);
6145 if (e
!= ERROR_OK
) {
6146 LOG_ERROR("The selected transport doesn't support this target");
6149 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
6151 /* now does target type exist */
6152 for (x
= 0 ; target_types
[x
] ; x
++) {
6153 if (strcmp(cp
, target_types
[x
]->name
) == 0) {
6158 if (!target_types
[x
]) {
6159 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
6160 for (x
= 0 ; target_types
[x
] ; x
++) {
6161 if (target_types
[x
+ 1]) {
6162 Jim_AppendStrings(goi
->interp
,
6163 Jim_GetResult(goi
->interp
),
6164 target_types
[x
]->name
,
6167 Jim_AppendStrings(goi
->interp
,
6168 Jim_GetResult(goi
->interp
),
6170 target_types
[x
]->name
, NULL
);
6177 target
= calloc(1, sizeof(struct target
));
6179 LOG_ERROR("Out of memory");
6183 /* set empty smp cluster */
6184 target
->smp_targets
= &empty_smp_targets
;
6186 /* allocate memory for each unique target type */
6187 target
->type
= malloc(sizeof(struct target_type
));
6188 if (!target
->type
) {
6189 LOG_ERROR("Out of memory");
6194 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
6196 /* default to first core, override with -coreid */
6199 target
->working_area
= 0x0;
6200 target
->working_area_size
= 0x0;
6201 target
->working_areas
= NULL
;
6202 target
->backup_working_area
= false;
6204 target
->state
= TARGET_UNKNOWN
;
6205 target
->debug_reason
= DBG_REASON_UNDEFINED
;
6206 target
->reg_cache
= NULL
;
6207 target
->breakpoints
= NULL
;
6208 target
->watchpoints
= NULL
;
6209 target
->next
= NULL
;
6210 target
->arch_info
= NULL
;
6212 target
->verbose_halt_msg
= true;
6214 target
->halt_issued
= false;
6216 /* initialize trace information */
6217 target
->trace_info
= calloc(1, sizeof(struct trace
));
6218 if (!target
->trace_info
) {
6219 LOG_ERROR("Out of memory");
6225 target
->dbgmsg
= NULL
;
6226 target
->dbg_msg_enabled
= 0;
6228 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
6230 target
->rtos
= NULL
;
6231 target
->rtos_auto_detect
= false;
6233 target
->gdb_port_override
= NULL
;
6234 target
->gdb_max_connections
= 1;
6236 /* Do the rest as "configure" options */
6237 goi
->isconfigure
= 1;
6238 e
= target_configure(goi
, target
);
6241 if (target
->has_dap
) {
6242 if (!target
->dap_configured
) {
6243 Jim_SetResultString(goi
->interp
, "-dap ?name? required when creating target", -1);
6247 if (!target
->tap_configured
) {
6248 Jim_SetResultString(goi
->interp
, "-chain-position ?name? required when creating target", -1);
6252 /* tap must be set after target was configured */
6258 rtos_destroy(target
);
6259 free(target
->gdb_port_override
);
6260 free(target
->trace_info
);
6266 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
6267 /* default endian to little if not specified */
6268 target
->endianness
= TARGET_LITTLE_ENDIAN
;
6271 cp
= Jim_GetString(new_cmd
, NULL
);
6272 target
->cmd_name
= strdup(cp
);
6273 if (!target
->cmd_name
) {
6274 LOG_ERROR("Out of memory");
6275 rtos_destroy(target
);
6276 free(target
->gdb_port_override
);
6277 free(target
->trace_info
);
6283 if (target
->type
->target_create
) {
6284 e
= (*(target
->type
->target_create
))(target
, goi
->interp
);
6285 if (e
!= ERROR_OK
) {
6286 LOG_DEBUG("target_create failed");
6287 free(target
->cmd_name
);
6288 rtos_destroy(target
);
6289 free(target
->gdb_port_override
);
6290 free(target
->trace_info
);
6297 /* create the target specific commands */
6298 if (target
->type
->commands
) {
6299 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
6301 LOG_ERROR("unable to register '%s' commands", cp
);
6304 /* now - create the new target name command */
6305 const struct command_registration target_subcommands
[] = {
6307 .chain
= target_instance_command_handlers
,
6310 .chain
= target
->type
->commands
,
6312 COMMAND_REGISTRATION_DONE
6314 const struct command_registration target_commands
[] = {
6317 .mode
= COMMAND_ANY
,
6318 .help
= "target command group",
6320 .chain
= target_subcommands
,
6322 COMMAND_REGISTRATION_DONE
6324 e
= register_commands_override_target(cmd_ctx
, NULL
, target_commands
, target
);
6325 if (e
!= ERROR_OK
) {
6326 if (target
->type
->deinit_target
)
6327 target
->type
->deinit_target(target
);
6328 free(target
->cmd_name
);
6329 rtos_destroy(target
);
6330 free(target
->gdb_port_override
);
6331 free(target
->trace_info
);
6337 /* append to end of list */
6338 append_to_list_all_targets(target
);
6340 cmd_ctx
->current_target
= target
;
6344 COMMAND_HANDLER(handle_target_current
)
6347 return ERROR_COMMAND_SYNTAX_ERROR
;
6349 struct target
*target
= get_current_target_or_null(CMD_CTX
);
6351 command_print(CMD
, "%s", target_name(target
));
6356 COMMAND_HANDLER(handle_target_types
)
6359 return ERROR_COMMAND_SYNTAX_ERROR
;
6361 for (unsigned int x
= 0; target_types
[x
]; x
++)
6362 command_print(CMD
, "%s", target_types
[x
]->name
);
6367 COMMAND_HANDLER(handle_target_names
)
6370 return ERROR_COMMAND_SYNTAX_ERROR
;
6372 struct target
*target
= all_targets
;
6374 command_print(CMD
, "%s", target_name(target
));
6375 target
= target
->next
;
6381 static struct target_list
*
6382 __attribute__((warn_unused_result
))
6383 create_target_list_node(const char *targetname
)
6385 struct target
*target
= get_target(targetname
);
6386 LOG_DEBUG("%s ", targetname
);
6390 struct target_list
*new = malloc(sizeof(struct target_list
));
6392 LOG_ERROR("Out of memory");
6396 new->target
= target
;
6400 static int get_target_with_common_rtos_type(struct command_invocation
*cmd
,
6401 struct list_head
*lh
, struct target
**result
)
6403 struct target
*target
= NULL
;
6404 struct target_list
*curr
;
6405 foreach_smp_target(curr
, lh
) {
6406 struct rtos
*curr_rtos
= curr
->target
->rtos
;
6408 if (target
&& target
->rtos
&& target
->rtos
->type
!= curr_rtos
->type
) {
6409 command_print(cmd
, "Different rtos types in members of one smp target!");
6412 target
= curr
->target
;
6419 COMMAND_HANDLER(handle_target_smp
)
6421 static int smp_group
= 1;
6423 if (CMD_ARGC
== 0) {
6424 LOG_DEBUG("Empty SMP target");
6427 LOG_DEBUG("%d", CMD_ARGC
);
6428 /* CMD_ARGC[0] = target to associate in smp
6429 * CMD_ARGC[1] = target to associate in smp
6433 struct list_head
*lh
= malloc(sizeof(*lh
));
6435 LOG_ERROR("Out of memory");
6440 for (unsigned int i
= 0; i
< CMD_ARGC
; i
++) {
6441 struct target_list
*new = create_target_list_node(CMD_ARGV
[i
]);
6443 list_add_tail(&new->lh
, lh
);
6445 /* now parse the list of cpu and put the target in smp mode*/
6446 struct target_list
*curr
;
6447 foreach_smp_target(curr
, lh
) {
6448 struct target
*target
= curr
->target
;
6449 target
->smp
= smp_group
;
6450 target
->smp_targets
= lh
;
6454 struct target
*rtos_target
;
6455 int retval
= get_target_with_common_rtos_type(CMD
, lh
, &rtos_target
);
6456 if (retval
== ERROR_OK
&& rtos_target
)
6457 retval
= rtos_smp_init(rtos_target
);
6462 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
6464 struct jim_getopt_info goi
;
6465 jim_getopt_setup(&goi
, interp
, argc
- 1, argv
+ 1);
6467 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
6468 "<name> <target_type> [<target_options> ...]");
6471 return target_create(&goi
);
6474 static const struct command_registration target_subcommand_handlers
[] = {
6477 .mode
= COMMAND_CONFIG
,
6478 .handler
= handle_target_init_command
,
6479 .help
= "initialize targets",
6484 .mode
= COMMAND_CONFIG
,
6485 .jim_handler
= jim_target_create
,
6486 .usage
= "name type '-chain-position' name [options ...]",
6487 .help
= "Creates and selects a new target",
6491 .mode
= COMMAND_ANY
,
6492 .handler
= handle_target_current
,
6493 .help
= "Returns the currently selected target",
6498 .mode
= COMMAND_ANY
,
6499 .handler
= handle_target_types
,
6500 .help
= "Returns the available target types as "
6501 "a list of strings",
6506 .mode
= COMMAND_ANY
,
6507 .handler
= handle_target_names
,
6508 .help
= "Returns the names of all targets as a list of strings",
6513 .mode
= COMMAND_ANY
,
6514 .handler
= handle_target_smp
,
6515 .usage
= "targetname1 targetname2 ...",
6516 .help
= "gather several target in a smp list"
6519 COMMAND_REGISTRATION_DONE
6523 target_addr_t address
;
6529 static int fastload_num
;
6530 static struct fast_load
*fastload
;
6532 static void free_fastload(void)
6535 for (int i
= 0; i
< fastload_num
; i
++)
6536 free(fastload
[i
].data
);
6542 COMMAND_HANDLER(handle_fast_load_image_command
)
6546 uint32_t image_size
;
6547 target_addr_t min_address
= 0;
6548 target_addr_t max_address
= -1;
6552 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command
,
6553 &image
, &min_address
, &max_address
);
6554 if (retval
!= ERROR_OK
)
6557 struct duration bench
;
6558 duration_start(&bench
);
6560 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
6561 if (retval
!= ERROR_OK
)
6566 fastload_num
= image
.num_sections
;
6567 fastload
= malloc(sizeof(struct fast_load
)*image
.num_sections
);
6569 command_print(CMD
, "out of memory");
6570 image_close(&image
);
6573 memset(fastload
, 0, sizeof(struct fast_load
)*image
.num_sections
);
6574 for (unsigned int i
= 0; i
< image
.num_sections
; i
++) {
6575 buffer
= malloc(image
.sections
[i
].size
);
6577 command_print(CMD
, "error allocating buffer for section (%d bytes)",
6578 (int)(image
.sections
[i
].size
));
6579 retval
= ERROR_FAIL
;
6583 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
6584 if (retval
!= ERROR_OK
) {
6589 uint32_t offset
= 0;
6590 uint32_t length
= buf_cnt
;
6592 /* DANGER!!! beware of unsigned comparison here!!! */
6594 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
6595 (image
.sections
[i
].base_address
< max_address
)) {
6596 if (image
.sections
[i
].base_address
< min_address
) {
6597 /* clip addresses below */
6598 offset
+= min_address
-image
.sections
[i
].base_address
;
6602 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
6603 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
6605 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
6606 fastload
[i
].data
= malloc(length
);
6607 if (!fastload
[i
].data
) {
6609 command_print(CMD
, "error allocating buffer for section (%" PRIu32
" bytes)",
6611 retval
= ERROR_FAIL
;
6614 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
6615 fastload
[i
].length
= length
;
6617 image_size
+= length
;
6618 command_print(CMD
, "%u bytes written at address 0x%8.8x",
6619 (unsigned int)length
,
6620 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
6626 if ((retval
== ERROR_OK
) && (duration_measure(&bench
) == ERROR_OK
)) {
6627 command_print(CMD
, "Loaded %" PRIu32
" bytes "
6628 "in %fs (%0.3f KiB/s)", image_size
,
6629 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
6632 "WARNING: image has not been loaded to target!"
6633 "You can issue a 'fast_load' to finish loading.");
6636 image_close(&image
);
6638 if (retval
!= ERROR_OK
)
6644 COMMAND_HANDLER(handle_fast_load_command
)
6647 return ERROR_COMMAND_SYNTAX_ERROR
;
6649 LOG_ERROR("No image in memory");
6653 int64_t ms
= timeval_ms();
6655 int retval
= ERROR_OK
;
6656 for (i
= 0; i
< fastload_num
; i
++) {
6657 struct target
*target
= get_current_target(CMD_CTX
);
6658 command_print(CMD
, "Write to 0x%08x, length 0x%08x",
6659 (unsigned int)(fastload
[i
].address
),
6660 (unsigned int)(fastload
[i
].length
));
6661 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
6662 if (retval
!= ERROR_OK
)
6664 size
+= fastload
[i
].length
;
6666 if (retval
== ERROR_OK
) {
6667 int64_t after
= timeval_ms();
6668 command_print(CMD
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
6673 static const struct command_registration target_command_handlers
[] = {
6676 .handler
= handle_targets_command
,
6677 .mode
= COMMAND_ANY
,
6678 .help
= "change current default target (one parameter) "
6679 "or prints table of all targets (no parameters)",
6680 .usage
= "[target]",
6684 .mode
= COMMAND_CONFIG
,
6685 .help
= "configure target",
6686 .chain
= target_subcommand_handlers
,
6689 COMMAND_REGISTRATION_DONE
6692 int target_register_commands(struct command_context
*cmd_ctx
)
6694 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
6697 static bool target_reset_nag
= true;
6699 bool get_target_reset_nag(void)
6701 return target_reset_nag
;
6704 COMMAND_HANDLER(handle_target_reset_nag
)
6706 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
6707 &target_reset_nag
, "Nag after each reset about options to improve "
6711 COMMAND_HANDLER(handle_ps_command
)
6713 struct target
*target
= get_current_target(CMD_CTX
);
6715 if (target
->state
!= TARGET_HALTED
) {
6716 command_print(CMD
, "Error: [%s] not halted", target_name(target
));
6717 return ERROR_TARGET_NOT_HALTED
;
6720 if ((target
->rtos
) && (target
->rtos
->type
)
6721 && (target
->rtos
->type
->ps_command
)) {
6722 display
= target
->rtos
->type
->ps_command(target
);
6723 command_print(CMD
, "%s", display
);
6728 return ERROR_TARGET_FAILURE
;
6732 static void binprint(struct command_invocation
*cmd
, const char *text
, const uint8_t *buf
, int size
)
6735 command_print_sameline(cmd
, "%s", text
);
6736 for (int i
= 0; i
< size
; i
++)
6737 command_print_sameline(cmd
, " %02x", buf
[i
]);
6738 command_print(cmd
, " ");
6741 COMMAND_HANDLER(handle_test_mem_access_command
)
6743 struct target
*target
= get_current_target(CMD_CTX
);
6745 int retval
= ERROR_OK
;
6747 if (target
->state
!= TARGET_HALTED
) {
6748 command_print(CMD
, "Error: [%s] not halted", target_name(target
));
6749 return ERROR_TARGET_NOT_HALTED
;
6753 return ERROR_COMMAND_SYNTAX_ERROR
;
6755 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], test_size
);
6758 size_t num_bytes
= test_size
+ 4;
6760 struct working_area
*wa
= NULL
;
6761 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6762 if (retval
!= ERROR_OK
) {
6763 LOG_ERROR("Not enough working area");
6767 uint8_t *test_pattern
= malloc(num_bytes
);
6769 for (size_t i
= 0; i
< num_bytes
; i
++)
6770 test_pattern
[i
] = rand();
6772 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6773 if (retval
!= ERROR_OK
) {
6774 LOG_ERROR("Test pattern write failed");
6778 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6779 for (int size
= 1; size
<= 4; size
*= 2) {
6780 for (int offset
= 0; offset
< 4; offset
++) {
6781 uint32_t count
= test_size
/ size
;
6782 size_t host_bufsiz
= (count
+ 2) * size
+ host_offset
;
6783 uint8_t *read_ref
= malloc(host_bufsiz
);
6784 uint8_t *read_buf
= malloc(host_bufsiz
);
6786 for (size_t i
= 0; i
< host_bufsiz
; i
++) {
6787 read_ref
[i
] = rand();
6788 read_buf
[i
] = read_ref
[i
];
6790 command_print_sameline(CMD
,
6791 "Test read %" PRIu32
" x %d @ %d to %saligned buffer: ", count
,
6792 size
, offset
, host_offset
? "un" : "");
6794 struct duration bench
;
6795 duration_start(&bench
);
6797 retval
= target_read_memory(target
, wa
->address
+ offset
, size
, count
,
6798 read_buf
+ size
+ host_offset
);
6800 duration_measure(&bench
);
6802 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6803 command_print(CMD
, "Unsupported alignment");
6805 } else if (retval
!= ERROR_OK
) {
6806 command_print(CMD
, "Memory read failed");
6810 /* replay on host */
6811 memcpy(read_ref
+ size
+ host_offset
, test_pattern
+ offset
, count
* size
);
6814 int result
= memcmp(read_ref
, read_buf
, host_bufsiz
);
6816 command_print(CMD
, "Pass in %fs (%0.3f KiB/s)",
6817 duration_elapsed(&bench
),
6818 duration_kbps(&bench
, count
* size
));
6820 command_print(CMD
, "Compare failed");
6821 binprint(CMD
, "ref:", read_ref
, host_bufsiz
);
6822 binprint(CMD
, "buf:", read_buf
, host_bufsiz
);
6834 target_free_working_area(target
, wa
);
6837 num_bytes
= test_size
+ 4 + 4 + 4;
6839 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6840 if (retval
!= ERROR_OK
) {
6841 LOG_ERROR("Not enough working area");
6845 test_pattern
= malloc(num_bytes
);
6847 for (size_t i
= 0; i
< num_bytes
; i
++)
6848 test_pattern
[i
] = rand();
6850 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6851 for (int size
= 1; size
<= 4; size
*= 2) {
6852 for (int offset
= 0; offset
< 4; offset
++) {
6853 uint32_t count
= test_size
/ size
;
6854 size_t host_bufsiz
= count
* size
+ host_offset
;
6855 uint8_t *read_ref
= malloc(num_bytes
);
6856 uint8_t *read_buf
= malloc(num_bytes
);
6857 uint8_t *write_buf
= malloc(host_bufsiz
);
6859 for (size_t i
= 0; i
< host_bufsiz
; i
++)
6860 write_buf
[i
] = rand();
6861 command_print_sameline(CMD
,
6862 "Test write %" PRIu32
" x %d @ %d from %saligned buffer: ", count
,
6863 size
, offset
, host_offset
? "un" : "");
6865 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6866 if (retval
!= ERROR_OK
) {
6867 command_print(CMD
, "Test pattern write failed");
6871 /* replay on host */
6872 memcpy(read_ref
, test_pattern
, num_bytes
);
6873 memcpy(read_ref
+ size
+ offset
, write_buf
+ host_offset
, count
* size
);
6875 struct duration bench
;
6876 duration_start(&bench
);
6878 retval
= target_write_memory(target
, wa
->address
+ size
+ offset
, size
, count
,
6879 write_buf
+ host_offset
);
6881 duration_measure(&bench
);
6883 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6884 command_print(CMD
, "Unsupported alignment");
6886 } else if (retval
!= ERROR_OK
) {
6887 command_print(CMD
, "Memory write failed");
6892 retval
= target_read_memory(target
, wa
->address
, 1, num_bytes
, read_buf
);
6893 if (retval
!= ERROR_OK
) {
6894 command_print(CMD
, "Test pattern write failed");
6899 int result
= memcmp(read_ref
, read_buf
, num_bytes
);
6901 command_print(CMD
, "Pass in %fs (%0.3f KiB/s)",
6902 duration_elapsed(&bench
),
6903 duration_kbps(&bench
, count
* size
));
6905 command_print(CMD
, "Compare failed");
6906 binprint(CMD
, "ref:", read_ref
, num_bytes
);
6907 binprint(CMD
, "buf:", read_buf
, num_bytes
);
6918 target_free_working_area(target
, wa
);
6922 static const struct command_registration target_exec_command_handlers
[] = {
6924 .name
= "fast_load_image",
6925 .handler
= handle_fast_load_image_command
,
6926 .mode
= COMMAND_ANY
,
6927 .help
= "Load image into server memory for later use by "
6928 "fast_load; primarily for profiling",
6929 .usage
= "filename [address ['bin'|'ihex'|'elf'|'s19' "
6930 "[min_address [max_length]]]]",
6933 .name
= "fast_load",
6934 .handler
= handle_fast_load_command
,
6935 .mode
= COMMAND_EXEC
,
6936 .help
= "loads active fast load image to current target "
6937 "- mainly for profiling purposes",
6942 .handler
= handle_profile_command
,
6943 .mode
= COMMAND_EXEC
,
6944 .usage
= "seconds filename [start end]",
6945 .help
= "profiling samples the CPU PC",
6947 /** @todo don't register virt2phys() unless target supports it */
6949 .name
= "virt2phys",
6950 .handler
= handle_virt2phys_command
,
6951 .mode
= COMMAND_ANY
,
6952 .help
= "translate a virtual address into a physical address",
6953 .usage
= "virtual_address",
6957 .handler
= handle_reg_command
,
6958 .mode
= COMMAND_EXEC
,
6959 .help
= "display (reread from target with \"force\") or set a register; "
6960 "with no arguments, displays all registers and their values",
6961 .usage
= "[(register_number|register_name) [(value|'force')]]",
6965 .handler
= handle_poll_command
,
6966 .mode
= COMMAND_EXEC
,
6967 .help
= "poll target state; or reconfigure background polling",
6968 .usage
= "['on'|'off']",
6971 .name
= "wait_halt",
6972 .handler
= handle_wait_halt_command
,
6973 .mode
= COMMAND_EXEC
,
6974 .help
= "wait up to the specified number of milliseconds "
6975 "(default 5000) for a previously requested halt",
6976 .usage
= "[milliseconds]",
6980 .handler
= handle_halt_command
,
6981 .mode
= COMMAND_EXEC
,
6982 .help
= "request target to halt, then wait up to the specified "
6983 "number of milliseconds (default 5000) for it to complete",
6984 .usage
= "[milliseconds]",
6988 .handler
= handle_resume_command
,
6989 .mode
= COMMAND_EXEC
,
6990 .help
= "resume target execution from current PC or address",
6991 .usage
= "[address]",
6995 .handler
= handle_reset_command
,
6996 .mode
= COMMAND_EXEC
,
6997 .usage
= "[run|halt|init]",
6998 .help
= "Reset all targets into the specified mode. "
6999 "Default reset mode is run, if not given.",
7002 .name
= "soft_reset_halt",
7003 .handler
= handle_soft_reset_halt_command
,
7004 .mode
= COMMAND_EXEC
,
7006 .help
= "halt the target and do a soft reset",
7010 .handler
= handle_step_command
,
7011 .mode
= COMMAND_EXEC
,
7012 .help
= "step one instruction from current PC or address",
7013 .usage
= "[address]",
7017 .handler
= handle_md_command
,
7018 .mode
= COMMAND_EXEC
,
7019 .help
= "display memory double-words",
7020 .usage
= "['phys'] address [count]",
7024 .handler
= handle_md_command
,
7025 .mode
= COMMAND_EXEC
,
7026 .help
= "display memory words",
7027 .usage
= "['phys'] address [count]",
7031 .handler
= handle_md_command
,
7032 .mode
= COMMAND_EXEC
,
7033 .help
= "display memory half-words",
7034 .usage
= "['phys'] address [count]",
7038 .handler
= handle_md_command
,
7039 .mode
= COMMAND_EXEC
,
7040 .help
= "display memory bytes",
7041 .usage
= "['phys'] address [count]",
7045 .handler
= handle_mw_command
,
7046 .mode
= COMMAND_EXEC
,
7047 .help
= "write memory double-word",
7048 .usage
= "['phys'] address value [count]",
7052 .handler
= handle_mw_command
,
7053 .mode
= COMMAND_EXEC
,
7054 .help
= "write memory word",
7055 .usage
= "['phys'] address value [count]",
7059 .handler
= handle_mw_command
,
7060 .mode
= COMMAND_EXEC
,
7061 .help
= "write memory half-word",
7062 .usage
= "['phys'] address value [count]",
7066 .handler
= handle_mw_command
,
7067 .mode
= COMMAND_EXEC
,
7068 .help
= "write memory byte",
7069 .usage
= "['phys'] address value [count]",
7073 .handler
= handle_bp_command
,
7074 .mode
= COMMAND_EXEC
,
7075 .help
= "list or set hardware or software breakpoint",
7076 .usage
= "[<address> [<asid>] <length> ['hw'|'hw_ctx']]",
7080 .handler
= handle_rbp_command
,
7081 .mode
= COMMAND_EXEC
,
7082 .help
= "remove breakpoint",
7083 .usage
= "'all' | address",
7087 .handler
= handle_wp_command
,
7088 .mode
= COMMAND_EXEC
,
7089 .help
= "list (no params) or create watchpoints",
7090 .usage
= "[address length [('r'|'w'|'a') [value [mask]]]]",
7094 .handler
= handle_rwp_command
,
7095 .mode
= COMMAND_EXEC
,
7096 .help
= "remove watchpoint",
7097 .usage
= "'all' | address",
7100 .name
= "load_image",
7101 .handler
= handle_load_image_command
,
7102 .mode
= COMMAND_EXEC
,
7103 .usage
= "filename [address ['bin'|'ihex'|'elf'|'s19' "
7104 "[min_address [max_length]]]]",
7107 .name
= "dump_image",
7108 .handler
= handle_dump_image_command
,
7109 .mode
= COMMAND_EXEC
,
7110 .usage
= "filename address size",
7113 .name
= "verify_image_checksum",
7114 .handler
= handle_verify_image_checksum_command
,
7115 .mode
= COMMAND_EXEC
,
7116 .usage
= "filename [offset [type]]",
7119 .name
= "verify_image",
7120 .handler
= handle_verify_image_command
,
7121 .mode
= COMMAND_EXEC
,
7122 .usage
= "filename [offset [type]]",
7125 .name
= "test_image",
7126 .handler
= handle_test_image_command
,
7127 .mode
= COMMAND_EXEC
,
7128 .usage
= "filename [offset [type]]",
7132 .mode
= COMMAND_EXEC
,
7133 .jim_handler
= target_jim_get_reg
,
7134 .help
= "Get register values from the target",
7139 .mode
= COMMAND_EXEC
,
7140 .jim_handler
= target_jim_set_reg
,
7141 .help
= "Set target register values",
7145 .name
= "read_memory",
7146 .mode
= COMMAND_EXEC
,
7147 .handler
= handle_target_read_memory
,
7148 .help
= "Read Tcl list of 8/16/32/64 bit numbers from target memory",
7149 .usage
= "address width count ['phys']",
7152 .name
= "write_memory",
7153 .mode
= COMMAND_EXEC
,
7154 .jim_handler
= target_jim_write_memory
,
7155 .help
= "Write Tcl list of 8/16/32/64 bit numbers to target memory",
7156 .usage
= "address width data ['phys']",
7159 .name
= "reset_nag",
7160 .handler
= handle_target_reset_nag
,
7161 .mode
= COMMAND_ANY
,
7162 .help
= "Nag after each reset about options that could have been "
7163 "enabled to improve performance.",
7164 .usage
= "['enable'|'disable']",
7168 .handler
= handle_ps_command
,
7169 .mode
= COMMAND_EXEC
,
7170 .help
= "list all tasks",
7174 .name
= "test_mem_access",
7175 .handler
= handle_test_mem_access_command
,
7176 .mode
= COMMAND_EXEC
,
7177 .help
= "Test the target's memory access functions",
7181 COMMAND_REGISTRATION_DONE
7183 static int target_register_user_commands(struct command_context
*cmd_ctx
)
7185 int retval
= ERROR_OK
;
7186 retval
= target_request_register_commands(cmd_ctx
);
7187 if (retval
!= ERROR_OK
)
7190 retval
= trace_register_commands(cmd_ctx
);
7191 if (retval
!= ERROR_OK
)
7195 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);
7198 const char *target_debug_reason_str(enum target_debug_reason reason
)
7201 case DBG_REASON_DBGRQ
:
7203 case DBG_REASON_BREAKPOINT
:
7204 return "BREAKPOINT";
7205 case DBG_REASON_WATCHPOINT
:
7206 return "WATCHPOINT";
7207 case DBG_REASON_WPTANDBKPT
:
7208 return "WPTANDBKPT";
7209 case DBG_REASON_SINGLESTEP
:
7210 return "SINGLESTEP";
7211 case DBG_REASON_NOTHALTED
:
7213 case DBG_REASON_EXIT
:
7215 case DBG_REASON_EXC_CATCH
:
7217 case DBG_REASON_UNDEFINED
: