1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2005 by Dominic Rath *
5 * Dominic.Rath@gmx.de *
7 * Copyright (C) 2007-2010 Øyvind Harboe *
8 * oyvind.harboe@zylin.com *
10 * Copyright (C) 2008, Duane Ellis *
11 * openocd@duaneeellis.com *
13 * Copyright (C) 2008 by Spencer Oliver *
14 * spen@spen-soft.co.uk *
16 * Copyright (C) 2008 by Rick Altherr *
17 * kc8apf@kc8apf.net> *
19 * Copyright (C) 2011 by Broadcom Corporation *
20 * Evan Hunter - ehunter@broadcom.com *
22 * Copyright (C) ST-Ericsson SA 2011 *
23 * michel.jaouen@stericsson.com : smp minimum support *
25 * Copyright (C) 2011 Andreas Fritiofson *
26 * andreas.fritiofson@gmail.com *
27 ***************************************************************************/
33 #include <helper/align.h>
34 #include <helper/time_support.h>
35 #include <jtag/jtag.h>
36 #include <flash/nor/core.h>
39 #include "target_type.h"
40 #include "target_request.h"
41 #include "breakpoints.h"
45 #include "rtos/rtos.h"
46 #include "transport/transport.h"
49 #include "semihosting_common.h"
51 /* default halt wait timeout (ms) */
52 #define DEFAULT_HALT_TIMEOUT 5000
54 static int target_read_buffer_default(struct target
*target
, target_addr_t address
,
55 uint32_t count
, uint8_t *buffer
);
56 static int target_write_buffer_default(struct target
*target
, target_addr_t address
,
57 uint32_t count
, const uint8_t *buffer
);
58 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
59 int argc
, Jim_Obj
* const *argv
);
60 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
61 int argc
, Jim_Obj
* const *argv
);
62 static int target_register_user_commands(struct command_context
*cmd_ctx
);
63 static int target_get_gdb_fileio_info_default(struct target
*target
,
64 struct gdb_fileio_info
*fileio_info
);
65 static int target_gdb_fileio_end_default(struct target
*target
, int retcode
,
66 int fileio_errno
, bool ctrl_c
);
69 extern struct target_type arm7tdmi_target
;
70 extern struct target_type arm720t_target
;
71 extern struct target_type arm9tdmi_target
;
72 extern struct target_type arm920t_target
;
73 extern struct target_type arm966e_target
;
74 extern struct target_type arm946e_target
;
75 extern struct target_type arm926ejs_target
;
76 extern struct target_type fa526_target
;
77 extern struct target_type feroceon_target
;
78 extern struct target_type dragonite_target
;
79 extern struct target_type xscale_target
;
80 extern struct target_type xtensa_chip_target
;
81 extern struct target_type cortexm_target
;
82 extern struct target_type cortexa_target
;
83 extern struct target_type aarch64_target
;
84 extern struct target_type cortexr4_target
;
85 extern struct target_type arm11_target
;
86 extern struct target_type ls1_sap_target
;
87 extern struct target_type mips_m4k_target
;
88 extern struct target_type mips_mips64_target
;
89 extern struct target_type avr_target
;
90 extern struct target_type dsp563xx_target
;
91 extern struct target_type dsp5680xx_target
;
92 extern struct target_type testee_target
;
93 extern struct target_type avr32_ap7k_target
;
94 extern struct target_type hla_target
;
95 extern struct target_type esp32_target
;
96 extern struct target_type esp32s2_target
;
97 extern struct target_type esp32s3_target
;
98 extern struct target_type or1k_target
;
99 extern struct target_type quark_x10xx_target
;
100 extern struct target_type quark_d20xx_target
;
101 extern struct target_type stm8_target
;
102 extern struct target_type riscv_target
;
103 extern struct target_type mem_ap_target
;
104 extern struct target_type esirisc_target
;
105 extern struct target_type arcv2_target
;
107 static struct target_type
*target_types
[] = {
148 struct target
*all_targets
;
149 static struct target_event_callback
*target_event_callbacks
;
150 static struct target_timer_callback
*target_timer_callbacks
;
151 static int64_t target_timer_next_event_value
;
152 static LIST_HEAD(target_reset_callback_list
);
153 static LIST_HEAD(target_trace_callback_list
);
154 static const int polling_interval
= TARGET_DEFAULT_POLLING_INTERVAL
;
155 static LIST_HEAD(empty_smp_targets
);
157 static const struct jim_nvp nvp_assert
[] = {
158 { .name
= "assert", NVP_ASSERT
},
159 { .name
= "deassert", NVP_DEASSERT
},
160 { .name
= "T", NVP_ASSERT
},
161 { .name
= "F", NVP_DEASSERT
},
162 { .name
= "t", NVP_ASSERT
},
163 { .name
= "f", NVP_DEASSERT
},
164 { .name
= NULL
, .value
= -1 }
167 static const struct jim_nvp nvp_error_target
[] = {
168 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
169 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
170 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
171 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
172 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
173 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
174 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
175 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
176 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
177 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
178 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
179 { .value
= -1, .name
= NULL
}
182 static const char *target_strerror_safe(int err
)
184 const struct jim_nvp
*n
;
186 n
= jim_nvp_value2name_simple(nvp_error_target
, err
);
193 static const struct jim_nvp nvp_target_event
[] = {
195 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
196 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
197 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
198 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
199 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
200 { .value
= TARGET_EVENT_STEP_START
, .name
= "step-start" },
201 { .value
= TARGET_EVENT_STEP_END
, .name
= "step-end" },
203 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
204 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
206 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
207 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
208 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
209 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
210 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
211 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
212 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
213 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
215 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
216 { .value
= TARGET_EVENT_EXAMINE_FAIL
, .name
= "examine-fail" },
217 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
219 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
220 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
222 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
223 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
225 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
226 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
228 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
229 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
231 { .value
= TARGET_EVENT_TRACE_CONFIG
, .name
= "trace-config" },
233 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0X100
, .name
= "semihosting-user-cmd-0x100" },
234 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0X101
, .name
= "semihosting-user-cmd-0x101" },
235 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0X102
, .name
= "semihosting-user-cmd-0x102" },
236 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0X103
, .name
= "semihosting-user-cmd-0x103" },
237 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0X104
, .name
= "semihosting-user-cmd-0x104" },
238 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0X105
, .name
= "semihosting-user-cmd-0x105" },
239 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0X106
, .name
= "semihosting-user-cmd-0x106" },
240 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0X107
, .name
= "semihosting-user-cmd-0x107" },
242 { .name
= NULL
, .value
= -1 }
245 static const struct jim_nvp nvp_target_state
[] = {
246 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
247 { .name
= "running", .value
= TARGET_RUNNING
},
248 { .name
= "halted", .value
= TARGET_HALTED
},
249 { .name
= "reset", .value
= TARGET_RESET
},
250 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
251 { .name
= NULL
, .value
= -1 },
254 static const struct jim_nvp nvp_target_debug_reason
[] = {
255 { .name
= "debug-request", .value
= DBG_REASON_DBGRQ
},
256 { .name
= "breakpoint", .value
= DBG_REASON_BREAKPOINT
},
257 { .name
= "watchpoint", .value
= DBG_REASON_WATCHPOINT
},
258 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
259 { .name
= "single-step", .value
= DBG_REASON_SINGLESTEP
},
260 { .name
= "target-not-halted", .value
= DBG_REASON_NOTHALTED
},
261 { .name
= "program-exit", .value
= DBG_REASON_EXIT
},
262 { .name
= "exception-catch", .value
= DBG_REASON_EXC_CATCH
},
263 { .name
= "undefined", .value
= DBG_REASON_UNDEFINED
},
264 { .name
= NULL
, .value
= -1 },
267 static const struct jim_nvp nvp_target_endian
[] = {
268 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
269 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
270 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
271 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
272 { .name
= NULL
, .value
= -1 },
275 static const struct jim_nvp nvp_reset_modes
[] = {
276 { .name
= "unknown", .value
= RESET_UNKNOWN
},
277 { .name
= "run", .value
= RESET_RUN
},
278 { .name
= "halt", .value
= RESET_HALT
},
279 { .name
= "init", .value
= RESET_INIT
},
280 { .name
= NULL
, .value
= -1 },
283 const char *debug_reason_name(struct target
*t
)
287 cp
= jim_nvp_value2name_simple(nvp_target_debug_reason
,
288 t
->debug_reason
)->name
;
290 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
291 cp
= "(*BUG*unknown*BUG*)";
296 const char *target_state_name(struct target
*t
)
299 cp
= jim_nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
301 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
302 cp
= "(*BUG*unknown*BUG*)";
305 if (!target_was_examined(t
) && t
->defer_examine
)
306 cp
= "examine deferred";
311 const char *target_event_name(enum target_event event
)
314 cp
= jim_nvp_value2name_simple(nvp_target_event
, event
)->name
;
316 LOG_ERROR("Invalid target event: %d", (int)(event
));
317 cp
= "(*BUG*unknown*BUG*)";
322 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
325 cp
= jim_nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
;
327 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
328 cp
= "(*BUG*unknown*BUG*)";
333 /* determine the number of the new target */
334 static int new_target_number(void)
339 /* number is 0 based */
343 if (x
< t
->target_number
)
344 x
= t
->target_number
;
350 static void append_to_list_all_targets(struct target
*target
)
352 struct target
**t
= &all_targets
;
359 /* read a uint64_t from a buffer in target memory endianness */
360 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
362 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
363 return le_to_h_u64(buffer
);
365 return be_to_h_u64(buffer
);
368 /* read a uint32_t from a buffer in target memory endianness */
369 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
371 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
372 return le_to_h_u32(buffer
);
374 return be_to_h_u32(buffer
);
377 /* read a uint24_t from a buffer in target memory endianness */
378 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
380 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
381 return le_to_h_u24(buffer
);
383 return be_to_h_u24(buffer
);
386 /* read a uint16_t from a buffer in target memory endianness */
387 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
389 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
390 return le_to_h_u16(buffer
);
392 return be_to_h_u16(buffer
);
395 /* write a uint64_t to a buffer in target memory endianness */
396 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
398 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
399 h_u64_to_le(buffer
, value
);
401 h_u64_to_be(buffer
, value
);
404 /* write a uint32_t to a buffer in target memory endianness */
405 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
407 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
408 h_u32_to_le(buffer
, value
);
410 h_u32_to_be(buffer
, value
);
413 /* write a uint24_t to a buffer in target memory endianness */
414 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
416 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
417 h_u24_to_le(buffer
, value
);
419 h_u24_to_be(buffer
, value
);
422 /* write a uint16_t to a buffer in target memory endianness */
423 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
425 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
426 h_u16_to_le(buffer
, value
);
428 h_u16_to_be(buffer
, value
);
431 /* write a uint8_t to a buffer in target memory endianness */
432 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
437 /* write a uint64_t array to a buffer in target memory endianness */
438 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
441 for (i
= 0; i
< count
; i
++)
442 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
445 /* write a uint32_t array to a buffer in target memory endianness */
446 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
449 for (i
= 0; i
< count
; i
++)
450 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
453 /* write a uint16_t array to a buffer in target memory endianness */
454 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
457 for (i
= 0; i
< count
; i
++)
458 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
461 /* write a uint64_t array to a buffer in target memory endianness */
462 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
465 for (i
= 0; i
< count
; i
++)
466 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
469 /* write a uint32_t array to a buffer in target memory endianness */
470 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
473 for (i
= 0; i
< count
; i
++)
474 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
477 /* write a uint16_t array to a buffer in target memory endianness */
478 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
481 for (i
= 0; i
< count
; i
++)
482 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
485 /* return a pointer to a configured target; id is name or number */
486 struct target
*get_target(const char *id
)
488 struct target
*target
;
490 /* try as tcltarget name */
491 for (target
= all_targets
; target
; target
= target
->next
) {
492 if (!target_name(target
))
494 if (strcmp(id
, target_name(target
)) == 0)
498 /* It's OK to remove this fallback sometime after August 2010 or so */
500 /* no match, try as number */
502 if (parse_uint(id
, &num
) != ERROR_OK
)
505 for (target
= all_targets
; target
; target
= target
->next
) {
506 if (target
->target_number
== (int)num
) {
507 LOG_WARNING("use '%s' as target identifier, not '%u'",
508 target_name(target
), num
);
516 /* returns a pointer to the n-th configured target */
517 struct target
*get_target_by_num(int num
)
519 struct target
*target
= all_targets
;
522 if (target
->target_number
== num
)
524 target
= target
->next
;
530 struct target
*get_current_target(struct command_context
*cmd_ctx
)
532 struct target
*target
= get_current_target_or_null(cmd_ctx
);
535 LOG_ERROR("BUG: current_target out of bounds");
542 struct target
*get_current_target_or_null(struct command_context
*cmd_ctx
)
544 return cmd_ctx
->current_target_override
545 ? cmd_ctx
->current_target_override
546 : cmd_ctx
->current_target
;
549 int target_poll(struct target
*target
)
553 /* We can't poll until after examine */
554 if (!target_was_examined(target
)) {
555 /* Fail silently lest we pollute the log */
559 retval
= target
->type
->poll(target
);
560 if (retval
!= ERROR_OK
)
563 if (target
->halt_issued
) {
564 if (target
->state
== TARGET_HALTED
)
565 target
->halt_issued
= false;
567 int64_t t
= timeval_ms() - target
->halt_issued_time
;
568 if (t
> DEFAULT_HALT_TIMEOUT
) {
569 target
->halt_issued
= false;
570 LOG_INFO("Halt timed out, wake up GDB.");
571 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
579 int target_halt(struct target
*target
)
582 /* We can't poll until after examine */
583 if (!target_was_examined(target
)) {
584 LOG_ERROR("Target not examined yet");
588 retval
= target
->type
->halt(target
);
589 if (retval
!= ERROR_OK
)
592 target
->halt_issued
= true;
593 target
->halt_issued_time
= timeval_ms();
599 * Make the target (re)start executing using its saved execution
600 * context (possibly with some modifications).
602 * @param target Which target should start executing.
603 * @param current True to use the target's saved program counter instead
604 * of the address parameter
605 * @param address Optionally used as the program counter.
606 * @param handle_breakpoints True iff breakpoints at the resumption PC
607 * should be skipped. (For example, maybe execution was stopped by
608 * such a breakpoint, in which case it would be counterproductive to
610 * @param debug_execution False if all working areas allocated by OpenOCD
611 * should be released and/or restored to their original contents.
612 * (This would for example be true to run some downloaded "helper"
613 * algorithm code, which resides in one such working buffer and uses
614 * another for data storage.)
616 * @todo Resolve the ambiguity about what the "debug_execution" flag
617 * signifies. For example, Target implementations don't agree on how
618 * it relates to invalidation of the register cache, or to whether
619 * breakpoints and watchpoints should be enabled. (It would seem wrong
620 * to enable breakpoints when running downloaded "helper" algorithms
621 * (debug_execution true), since the breakpoints would be set to match
622 * target firmware being debugged, not the helper algorithm.... and
623 * enabling them could cause such helpers to malfunction (for example,
624 * by overwriting data with a breakpoint instruction. On the other
625 * hand the infrastructure for running such helpers might use this
626 * procedure but rely on hardware breakpoint to detect termination.)
628 int target_resume(struct target
*target
, int current
, target_addr_t address
,
629 int handle_breakpoints
, int debug_execution
)
633 /* We can't poll until after examine */
634 if (!target_was_examined(target
)) {
635 LOG_ERROR("Target not examined yet");
639 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
641 /* note that resume *must* be asynchronous. The CPU can halt before
642 * we poll. The CPU can even halt at the current PC as a result of
643 * a software breakpoint being inserted by (a bug?) the application.
646 * resume() triggers the event 'resumed'. The execution of TCL commands
647 * in the event handler causes the polling of targets. If the target has
648 * already halted for a breakpoint, polling will run the 'halted' event
649 * handler before the pending 'resumed' handler.
650 * Disable polling during resume() to guarantee the execution of handlers
651 * in the correct order.
653 bool save_poll_mask
= jtag_poll_mask();
654 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
655 jtag_poll_unmask(save_poll_mask
);
657 if (retval
!= ERROR_OK
)
660 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
665 static int target_process_reset(struct command_invocation
*cmd
, enum target_reset_mode reset_mode
)
670 n
= jim_nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
672 LOG_ERROR("invalid reset mode");
676 struct target
*target
;
677 for (target
= all_targets
; target
; target
= target
->next
)
678 target_call_reset_callbacks(target
, reset_mode
);
680 /* disable polling during reset to make reset event scripts
681 * more predictable, i.e. dr/irscan & pathmove in events will
682 * not have JTAG operations injected into the middle of a sequence.
684 bool save_poll_mask
= jtag_poll_mask();
686 sprintf(buf
, "ocd_process_reset %s", n
->name
);
687 retval
= Jim_Eval(cmd
->ctx
->interp
, buf
);
689 jtag_poll_unmask(save_poll_mask
);
691 if (retval
!= JIM_OK
) {
692 Jim_MakeErrorMessage(cmd
->ctx
->interp
);
693 command_print(cmd
, "%s", Jim_GetString(Jim_GetResult(cmd
->ctx
->interp
), NULL
));
697 /* We want any events to be processed before the prompt */
698 retval
= target_call_timer_callbacks_now();
700 for (target
= all_targets
; target
; target
= target
->next
) {
701 target
->type
->check_reset(target
);
702 target
->running_alg
= false;
708 static int identity_virt2phys(struct target
*target
,
709 target_addr_t
virtual, target_addr_t
*physical
)
715 static int no_mmu(struct target
*target
, int *enabled
)
722 * Reset the @c examined flag for the given target.
723 * Pure paranoia -- targets are zeroed on allocation.
725 static inline void target_reset_examined(struct target
*target
)
727 target
->examined
= false;
730 static int default_examine(struct target
*target
)
732 target_set_examined(target
);
736 /* no check by default */
737 static int default_check_reset(struct target
*target
)
742 /* Equivalent Tcl code arp_examine_one is in src/target/startup.tcl
744 int target_examine_one(struct target
*target
)
746 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
748 int retval
= target
->type
->examine(target
);
749 if (retval
!= ERROR_OK
) {
750 target_reset_examined(target
);
751 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_FAIL
);
755 target_set_examined(target
);
756 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
761 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
763 struct target
*target
= priv
;
765 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
768 jtag_unregister_event_callback(jtag_enable_callback
, target
);
770 return target_examine_one(target
);
773 /* Targets that correctly implement init + examine, i.e.
774 * no communication with target during init:
778 int target_examine(void)
780 int retval
= ERROR_OK
;
781 struct target
*target
;
783 for (target
= all_targets
; target
; target
= target
->next
) {
784 /* defer examination, but don't skip it */
785 if (!target
->tap
->enabled
) {
786 jtag_register_event_callback(jtag_enable_callback
,
791 if (target
->defer_examine
)
794 int retval2
= target_examine_one(target
);
795 if (retval2
!= ERROR_OK
) {
796 LOG_WARNING("target %s examination failed", target_name(target
));
803 const char *target_type_name(struct target
*target
)
805 return target
->type
->name
;
808 static int target_soft_reset_halt(struct target
*target
)
810 if (!target_was_examined(target
)) {
811 LOG_ERROR("Target not examined yet");
814 if (!target
->type
->soft_reset_halt
) {
815 LOG_ERROR("Target %s does not support soft_reset_halt",
816 target_name(target
));
819 return target
->type
->soft_reset_halt(target
);
823 * Downloads a target-specific native code algorithm to the target,
824 * and executes it. * Note that some targets may need to set up, enable,
825 * and tear down a breakpoint (hard or * soft) to detect algorithm
826 * termination, while others may support lower overhead schemes where
827 * soft breakpoints embedded in the algorithm automatically terminate the
830 * @param target used to run the algorithm
831 * @param num_mem_params
833 * @param num_reg_params
838 * @param arch_info target-specific description of the algorithm.
840 int target_run_algorithm(struct target
*target
,
841 int num_mem_params
, struct mem_param
*mem_params
,
842 int num_reg_params
, struct reg_param
*reg_param
,
843 target_addr_t entry_point
, target_addr_t exit_point
,
844 int timeout_ms
, void *arch_info
)
846 int retval
= ERROR_FAIL
;
848 if (!target_was_examined(target
)) {
849 LOG_ERROR("Target not examined yet");
852 if (!target
->type
->run_algorithm
) {
853 LOG_ERROR("Target type '%s' does not support %s",
854 target_type_name(target
), __func__
);
858 target
->running_alg
= true;
859 retval
= target
->type
->run_algorithm(target
,
860 num_mem_params
, mem_params
,
861 num_reg_params
, reg_param
,
862 entry_point
, exit_point
, timeout_ms
, arch_info
);
863 target
->running_alg
= false;
870 * Executes a target-specific native code algorithm and leaves it running.
872 * @param target used to run the algorithm
873 * @param num_mem_params
875 * @param num_reg_params
879 * @param arch_info target-specific description of the algorithm.
881 int target_start_algorithm(struct target
*target
,
882 int num_mem_params
, struct mem_param
*mem_params
,
883 int num_reg_params
, struct reg_param
*reg_params
,
884 target_addr_t entry_point
, target_addr_t exit_point
,
887 int retval
= ERROR_FAIL
;
889 if (!target_was_examined(target
)) {
890 LOG_ERROR("Target not examined yet");
893 if (!target
->type
->start_algorithm
) {
894 LOG_ERROR("Target type '%s' does not support %s",
895 target_type_name(target
), __func__
);
898 if (target
->running_alg
) {
899 LOG_ERROR("Target is already running an algorithm");
903 target
->running_alg
= true;
904 retval
= target
->type
->start_algorithm(target
,
905 num_mem_params
, mem_params
,
906 num_reg_params
, reg_params
,
907 entry_point
, exit_point
, arch_info
);
914 * Waits for an algorithm started with target_start_algorithm() to complete.
916 * @param target used to run the algorithm
917 * @param num_mem_params
919 * @param num_reg_params
923 * @param arch_info target-specific description of the algorithm.
925 int target_wait_algorithm(struct target
*target
,
926 int num_mem_params
, struct mem_param
*mem_params
,
927 int num_reg_params
, struct reg_param
*reg_params
,
928 target_addr_t exit_point
, int timeout_ms
,
931 int retval
= ERROR_FAIL
;
933 if (!target
->type
->wait_algorithm
) {
934 LOG_ERROR("Target type '%s' does not support %s",
935 target_type_name(target
), __func__
);
938 if (!target
->running_alg
) {
939 LOG_ERROR("Target is not running an algorithm");
943 retval
= target
->type
->wait_algorithm(target
,
944 num_mem_params
, mem_params
,
945 num_reg_params
, reg_params
,
946 exit_point
, timeout_ms
, arch_info
);
947 if (retval
!= ERROR_TARGET_TIMEOUT
)
948 target
->running_alg
= false;
955 * Streams data to a circular buffer on target intended for consumption by code
956 * running asynchronously on target.
958 * This is intended for applications where target-specific native code runs
959 * on the target, receives data from the circular buffer, does something with
960 * it (most likely writing it to a flash memory), and advances the circular
963 * This assumes that the helper algorithm has already been loaded to the target,
964 * but has not been started yet. Given memory and register parameters are passed
967 * The buffer is defined by (buffer_start, buffer_size) arguments and has the
970 * [buffer_start + 0, buffer_start + 4):
971 * Write Pointer address (aka head). Written and updated by this
972 * routine when new data is written to the circular buffer.
973 * [buffer_start + 4, buffer_start + 8):
974 * Read Pointer address (aka tail). Updated by code running on the
975 * target after it consumes data.
976 * [buffer_start + 8, buffer_start + buffer_size):
977 * Circular buffer contents.
979 * See contrib/loaders/flash/stm32f1x.S for an example.
981 * @param target used to run the algorithm
982 * @param buffer address on the host where data to be sent is located
983 * @param count number of blocks to send
984 * @param block_size size in bytes of each block
985 * @param num_mem_params count of memory-based params to pass to algorithm
986 * @param mem_params memory-based params to pass to algorithm
987 * @param num_reg_params count of register-based params to pass to algorithm
988 * @param reg_params memory-based params to pass to algorithm
989 * @param buffer_start address on the target of the circular buffer structure
990 * @param buffer_size size of the circular buffer structure
991 * @param entry_point address on the target to execute to start the algorithm
992 * @param exit_point address at which to set a breakpoint to catch the
993 * end of the algorithm; can be 0 if target triggers a breakpoint itself
997 int target_run_flash_async_algorithm(struct target
*target
,
998 const uint8_t *buffer
, uint32_t count
, int block_size
,
999 int num_mem_params
, struct mem_param
*mem_params
,
1000 int num_reg_params
, struct reg_param
*reg_params
,
1001 uint32_t buffer_start
, uint32_t buffer_size
,
1002 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
1007 const uint8_t *buffer_orig
= buffer
;
1009 /* Set up working area. First word is write pointer, second word is read pointer,
1010 * rest is fifo data area. */
1011 uint32_t wp_addr
= buffer_start
;
1012 uint32_t rp_addr
= buffer_start
+ 4;
1013 uint32_t fifo_start_addr
= buffer_start
+ 8;
1014 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
1016 uint32_t wp
= fifo_start_addr
;
1017 uint32_t rp
= fifo_start_addr
;
1019 /* validate block_size is 2^n */
1020 assert(IS_PWR_OF_2(block_size
));
1022 retval
= target_write_u32(target
, wp_addr
, wp
);
1023 if (retval
!= ERROR_OK
)
1025 retval
= target_write_u32(target
, rp_addr
, rp
);
1026 if (retval
!= ERROR_OK
)
1029 /* Start up algorithm on target and let it idle while writing the first chunk */
1030 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
1031 num_reg_params
, reg_params
,
1036 if (retval
!= ERROR_OK
) {
1037 LOG_ERROR("error starting target flash write algorithm");
1043 retval
= target_read_u32(target
, rp_addr
, &rp
);
1044 if (retval
!= ERROR_OK
) {
1045 LOG_ERROR("failed to get read pointer");
1049 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
1050 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
1053 LOG_ERROR("flash write algorithm aborted by target");
1054 retval
= ERROR_FLASH_OPERATION_FAILED
;
1058 if (!IS_ALIGNED(rp
- fifo_start_addr
, block_size
) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
1059 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
1063 /* Count the number of bytes available in the fifo without
1064 * crossing the wrap around. Make sure to not fill it completely,
1065 * because that would make wp == rp and that's the empty condition. */
1066 uint32_t thisrun_bytes
;
1068 thisrun_bytes
= rp
- wp
- block_size
;
1069 else if (rp
> fifo_start_addr
)
1070 thisrun_bytes
= fifo_end_addr
- wp
;
1072 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
1074 if (thisrun_bytes
== 0) {
1075 /* Throttle polling a bit if transfer is (much) faster than flash
1076 * programming. The exact delay shouldn't matter as long as it's
1077 * less than buffer size / flash speed. This is very unlikely to
1078 * run when using high latency connections such as USB. */
1081 /* to stop an infinite loop on some targets check and increment a timeout
1082 * this issue was observed on a stellaris using the new ICDI interface */
1083 if (timeout
++ >= 2500) {
1084 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1085 return ERROR_FLASH_OPERATION_FAILED
;
1090 /* reset our timeout */
1093 /* Limit to the amount of data we actually want to write */
1094 if (thisrun_bytes
> count
* block_size
)
1095 thisrun_bytes
= count
* block_size
;
1097 /* Force end of large blocks to be word aligned */
1098 if (thisrun_bytes
>= 16)
1099 thisrun_bytes
-= (rp
+ thisrun_bytes
) & 0x03;
1101 /* Write data to fifo */
1102 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
1103 if (retval
!= ERROR_OK
)
1106 /* Update counters and wrap write pointer */
1107 buffer
+= thisrun_bytes
;
1108 count
-= thisrun_bytes
/ block_size
;
1109 wp
+= thisrun_bytes
;
1110 if (wp
>= fifo_end_addr
)
1111 wp
= fifo_start_addr
;
1113 /* Store updated write pointer to target */
1114 retval
= target_write_u32(target
, wp_addr
, wp
);
1115 if (retval
!= ERROR_OK
)
1118 /* Avoid GDB timeouts */
1122 if (retval
!= ERROR_OK
) {
1123 /* abort flash write algorithm on target */
1124 target_write_u32(target
, wp_addr
, 0);
1127 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1128 num_reg_params
, reg_params
,
1133 if (retval2
!= ERROR_OK
) {
1134 LOG_ERROR("error waiting for target flash write algorithm");
1138 if (retval
== ERROR_OK
) {
1139 /* check if algorithm set rp = 0 after fifo writer loop finished */
1140 retval
= target_read_u32(target
, rp_addr
, &rp
);
1141 if (retval
== ERROR_OK
&& rp
== 0) {
1142 LOG_ERROR("flash write algorithm aborted by target");
1143 retval
= ERROR_FLASH_OPERATION_FAILED
;
1150 int target_run_read_async_algorithm(struct target
*target
,
1151 uint8_t *buffer
, uint32_t count
, int block_size
,
1152 int num_mem_params
, struct mem_param
*mem_params
,
1153 int num_reg_params
, struct reg_param
*reg_params
,
1154 uint32_t buffer_start
, uint32_t buffer_size
,
1155 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
1160 const uint8_t *buffer_orig
= buffer
;
1162 /* Set up working area. First word is write pointer, second word is read pointer,
1163 * rest is fifo data area. */
1164 uint32_t wp_addr
= buffer_start
;
1165 uint32_t rp_addr
= buffer_start
+ 4;
1166 uint32_t fifo_start_addr
= buffer_start
+ 8;
1167 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
1169 uint32_t wp
= fifo_start_addr
;
1170 uint32_t rp
= fifo_start_addr
;
1172 /* validate block_size is 2^n */
1173 assert(IS_PWR_OF_2(block_size
));
1175 retval
= target_write_u32(target
, wp_addr
, wp
);
1176 if (retval
!= ERROR_OK
)
1178 retval
= target_write_u32(target
, rp_addr
, rp
);
1179 if (retval
!= ERROR_OK
)
1182 /* Start up algorithm on target */
1183 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
1184 num_reg_params
, reg_params
,
1189 if (retval
!= ERROR_OK
) {
1190 LOG_ERROR("error starting target flash read algorithm");
1195 retval
= target_read_u32(target
, wp_addr
, &wp
);
1196 if (retval
!= ERROR_OK
) {
1197 LOG_ERROR("failed to get write pointer");
1201 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
1202 (size_t)(buffer
- buffer_orig
), count
, wp
, rp
);
1205 LOG_ERROR("flash read algorithm aborted by target");
1206 retval
= ERROR_FLASH_OPERATION_FAILED
;
1210 if (!IS_ALIGNED(wp
- fifo_start_addr
, block_size
) || wp
< fifo_start_addr
|| wp
>= fifo_end_addr
) {
1211 LOG_ERROR("corrupted fifo write pointer 0x%" PRIx32
, wp
);
1215 /* Count the number of bytes available in the fifo without
1216 * crossing the wrap around. */
1217 uint32_t thisrun_bytes
;
1219 thisrun_bytes
= wp
- rp
;
1221 thisrun_bytes
= fifo_end_addr
- rp
;
1223 if (thisrun_bytes
== 0) {
1224 /* Throttle polling a bit if transfer is (much) faster than flash
1225 * reading. The exact delay shouldn't matter as long as it's
1226 * less than buffer size / flash speed. This is very unlikely to
1227 * run when using high latency connections such as USB. */
1230 /* to stop an infinite loop on some targets check and increment a timeout
1231 * this issue was observed on a stellaris using the new ICDI interface */
1232 if (timeout
++ >= 2500) {
1233 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1234 return ERROR_FLASH_OPERATION_FAILED
;
1239 /* Reset our timeout */
1242 /* Limit to the amount of data we actually want to read */
1243 if (thisrun_bytes
> count
* block_size
)
1244 thisrun_bytes
= count
* block_size
;
1246 /* Force end of large blocks to be word aligned */
1247 if (thisrun_bytes
>= 16)
1248 thisrun_bytes
-= (rp
+ thisrun_bytes
) & 0x03;
1250 /* Read data from fifo */
1251 retval
= target_read_buffer(target
, rp
, thisrun_bytes
, buffer
);
1252 if (retval
!= ERROR_OK
)
1255 /* Update counters and wrap write pointer */
1256 buffer
+= thisrun_bytes
;
1257 count
-= thisrun_bytes
/ block_size
;
1258 rp
+= thisrun_bytes
;
1259 if (rp
>= fifo_end_addr
)
1260 rp
= fifo_start_addr
;
1262 /* Store updated write pointer to target */
1263 retval
= target_write_u32(target
, rp_addr
, rp
);
1264 if (retval
!= ERROR_OK
)
1267 /* Avoid GDB timeouts */
1272 if (retval
!= ERROR_OK
) {
1273 /* abort flash write algorithm on target */
1274 target_write_u32(target
, rp_addr
, 0);
1277 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1278 num_reg_params
, reg_params
,
1283 if (retval2
!= ERROR_OK
) {
1284 LOG_ERROR("error waiting for target flash write algorithm");
1288 if (retval
== ERROR_OK
) {
1289 /* check if algorithm set wp = 0 after fifo writer loop finished */
1290 retval
= target_read_u32(target
, wp_addr
, &wp
);
1291 if (retval
== ERROR_OK
&& wp
== 0) {
1292 LOG_ERROR("flash read algorithm aborted by target");
1293 retval
= ERROR_FLASH_OPERATION_FAILED
;
1300 int target_read_memory(struct target
*target
,
1301 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1303 if (!target_was_examined(target
)) {
1304 LOG_ERROR("Target not examined yet");
1307 if (!target
->type
->read_memory
) {
1308 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1311 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1314 int target_read_phys_memory(struct target
*target
,
1315 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1317 if (!target_was_examined(target
)) {
1318 LOG_ERROR("Target not examined yet");
1321 if (!target
->type
->read_phys_memory
) {
1322 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1325 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1328 int target_write_memory(struct target
*target
,
1329 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1331 if (!target_was_examined(target
)) {
1332 LOG_ERROR("Target not examined yet");
1335 if (!target
->type
->write_memory
) {
1336 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1339 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1342 int target_write_phys_memory(struct target
*target
,
1343 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1345 if (!target_was_examined(target
)) {
1346 LOG_ERROR("Target not examined yet");
1349 if (!target
->type
->write_phys_memory
) {
1350 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1353 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1356 int target_add_breakpoint(struct target
*target
,
1357 struct breakpoint
*breakpoint
)
1359 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1360 LOG_WARNING("target %s is not halted (add breakpoint)", target_name(target
));
1361 return ERROR_TARGET_NOT_HALTED
;
1363 return target
->type
->add_breakpoint(target
, breakpoint
);
1366 int target_add_context_breakpoint(struct target
*target
,
1367 struct breakpoint
*breakpoint
)
1369 if (target
->state
!= TARGET_HALTED
) {
1370 LOG_WARNING("target %s is not halted (add context breakpoint)", target_name(target
));
1371 return ERROR_TARGET_NOT_HALTED
;
1373 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1376 int target_add_hybrid_breakpoint(struct target
*target
,
1377 struct breakpoint
*breakpoint
)
1379 if (target
->state
!= TARGET_HALTED
) {
1380 LOG_WARNING("target %s is not halted (add hybrid breakpoint)", target_name(target
));
1381 return ERROR_TARGET_NOT_HALTED
;
1383 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1386 int target_remove_breakpoint(struct target
*target
,
1387 struct breakpoint
*breakpoint
)
1389 return target
->type
->remove_breakpoint(target
, breakpoint
);
1392 int target_add_watchpoint(struct target
*target
,
1393 struct watchpoint
*watchpoint
)
1395 if (target
->state
!= TARGET_HALTED
) {
1396 LOG_WARNING("target %s is not halted (add watchpoint)", target_name(target
));
1397 return ERROR_TARGET_NOT_HALTED
;
1399 return target
->type
->add_watchpoint(target
, watchpoint
);
1401 int target_remove_watchpoint(struct target
*target
,
1402 struct watchpoint
*watchpoint
)
1404 return target
->type
->remove_watchpoint(target
, watchpoint
);
1406 int target_hit_watchpoint(struct target
*target
,
1407 struct watchpoint
**hit_watchpoint
)
1409 if (target
->state
!= TARGET_HALTED
) {
1410 LOG_WARNING("target %s is not halted (hit watchpoint)", target
->cmd_name
);
1411 return ERROR_TARGET_NOT_HALTED
;
1414 if (!target
->type
->hit_watchpoint
) {
1415 /* For backward compatible, if hit_watchpoint is not implemented,
1416 * return ERROR_FAIL such that gdb_server will not take the nonsense
1421 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1424 const char *target_get_gdb_arch(struct target
*target
)
1426 if (!target
->type
->get_gdb_arch
)
1428 return target
->type
->get_gdb_arch(target
);
1431 int target_get_gdb_reg_list(struct target
*target
,
1432 struct reg
**reg_list
[], int *reg_list_size
,
1433 enum target_register_class reg_class
)
1435 int result
= ERROR_FAIL
;
1437 if (!target_was_examined(target
)) {
1438 LOG_ERROR("Target not examined yet");
1442 result
= target
->type
->get_gdb_reg_list(target
, reg_list
,
1443 reg_list_size
, reg_class
);
1446 if (result
!= ERROR_OK
) {
1453 int target_get_gdb_reg_list_noread(struct target
*target
,
1454 struct reg
**reg_list
[], int *reg_list_size
,
1455 enum target_register_class reg_class
)
1457 if (target
->type
->get_gdb_reg_list_noread
&&
1458 target
->type
->get_gdb_reg_list_noread(target
, reg_list
,
1459 reg_list_size
, reg_class
) == ERROR_OK
)
1461 return target_get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1464 bool target_supports_gdb_connection(struct target
*target
)
1467 * exclude all the targets that don't provide get_gdb_reg_list
1468 * or that have explicit gdb_max_connection == 0
1470 return !!target
->type
->get_gdb_reg_list
&& !!target
->gdb_max_connections
;
1473 int target_step(struct target
*target
,
1474 int current
, target_addr_t address
, int handle_breakpoints
)
1478 target_call_event_callbacks(target
, TARGET_EVENT_STEP_START
);
1480 retval
= target
->type
->step(target
, current
, address
, handle_breakpoints
);
1481 if (retval
!= ERROR_OK
)
1484 target_call_event_callbacks(target
, TARGET_EVENT_STEP_END
);
1489 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1491 if (target
->state
!= TARGET_HALTED
) {
1492 LOG_WARNING("target %s is not halted (gdb fileio)", target
->cmd_name
);
1493 return ERROR_TARGET_NOT_HALTED
;
1495 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1498 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1500 if (target
->state
!= TARGET_HALTED
) {
1501 LOG_WARNING("target %s is not halted (gdb fileio end)", target
->cmd_name
);
1502 return ERROR_TARGET_NOT_HALTED
;
1504 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1507 target_addr_t
target_address_max(struct target
*target
)
1509 unsigned bits
= target_address_bits(target
);
1510 if (sizeof(target_addr_t
) * 8 == bits
)
1511 return (target_addr_t
) -1;
1513 return (((target_addr_t
) 1) << bits
) - 1;
1516 unsigned target_address_bits(struct target
*target
)
1518 if (target
->type
->address_bits
)
1519 return target
->type
->address_bits(target
);
1523 unsigned int target_data_bits(struct target
*target
)
1525 if (target
->type
->data_bits
)
1526 return target
->type
->data_bits(target
);
1530 static int target_profiling(struct target
*target
, uint32_t *samples
,
1531 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1533 return target
->type
->profiling(target
, samples
, max_num_samples
,
1534 num_samples
, seconds
);
1537 static int handle_target(void *priv
);
1539 static int target_init_one(struct command_context
*cmd_ctx
,
1540 struct target
*target
)
1542 target_reset_examined(target
);
1544 struct target_type
*type
= target
->type
;
1546 type
->examine
= default_examine
;
1548 if (!type
->check_reset
)
1549 type
->check_reset
= default_check_reset
;
1551 assert(type
->init_target
);
1553 int retval
= type
->init_target(cmd_ctx
, target
);
1554 if (retval
!= ERROR_OK
) {
1555 LOG_ERROR("target '%s' init failed", target_name(target
));
1559 /* Sanity-check MMU support ... stub in what we must, to help
1560 * implement it in stages, but warn if we need to do so.
1563 if (!type
->virt2phys
) {
1564 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1565 type
->virt2phys
= identity_virt2phys
;
1568 /* Make sure no-MMU targets all behave the same: make no
1569 * distinction between physical and virtual addresses, and
1570 * ensure that virt2phys() is always an identity mapping.
1572 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1573 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1576 type
->write_phys_memory
= type
->write_memory
;
1577 type
->read_phys_memory
= type
->read_memory
;
1578 type
->virt2phys
= identity_virt2phys
;
1581 if (!target
->type
->read_buffer
)
1582 target
->type
->read_buffer
= target_read_buffer_default
;
1584 if (!target
->type
->write_buffer
)
1585 target
->type
->write_buffer
= target_write_buffer_default
;
1587 if (!target
->type
->get_gdb_fileio_info
)
1588 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1590 if (!target
->type
->gdb_fileio_end
)
1591 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1593 if (!target
->type
->profiling
)
1594 target
->type
->profiling
= target_profiling_default
;
1599 static int target_init(struct command_context
*cmd_ctx
)
1601 struct target
*target
;
1604 for (target
= all_targets
; target
; target
= target
->next
) {
1605 retval
= target_init_one(cmd_ctx
, target
);
1606 if (retval
!= ERROR_OK
)
1613 retval
= target_register_user_commands(cmd_ctx
);
1614 if (retval
!= ERROR_OK
)
1617 retval
= target_register_timer_callback(&handle_target
,
1618 polling_interval
, TARGET_TIMER_TYPE_PERIODIC
, cmd_ctx
->interp
);
1619 if (retval
!= ERROR_OK
)
1625 COMMAND_HANDLER(handle_target_init_command
)
1630 return ERROR_COMMAND_SYNTAX_ERROR
;
1632 static bool target_initialized
;
1633 if (target_initialized
) {
1634 LOG_INFO("'target init' has already been called");
1637 target_initialized
= true;
1639 retval
= command_run_line(CMD_CTX
, "init_targets");
1640 if (retval
!= ERROR_OK
)
1643 retval
= command_run_line(CMD_CTX
, "init_target_events");
1644 if (retval
!= ERROR_OK
)
1647 retval
= command_run_line(CMD_CTX
, "init_board");
1648 if (retval
!= ERROR_OK
)
1651 LOG_DEBUG("Initializing targets...");
1652 return target_init(CMD_CTX
);
1655 int target_register_event_callback(int (*callback
)(struct target
*target
,
1656 enum target_event event
, void *priv
), void *priv
)
1658 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1661 return ERROR_COMMAND_SYNTAX_ERROR
;
1664 while ((*callbacks_p
)->next
)
1665 callbacks_p
= &((*callbacks_p
)->next
);
1666 callbacks_p
= &((*callbacks_p
)->next
);
1669 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1670 (*callbacks_p
)->callback
= callback
;
1671 (*callbacks_p
)->priv
= priv
;
1672 (*callbacks_p
)->next
= NULL
;
1677 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1678 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1680 struct target_reset_callback
*entry
;
1683 return ERROR_COMMAND_SYNTAX_ERROR
;
1685 entry
= malloc(sizeof(struct target_reset_callback
));
1687 LOG_ERROR("error allocating buffer for reset callback entry");
1688 return ERROR_COMMAND_SYNTAX_ERROR
;
1691 entry
->callback
= callback
;
1693 list_add(&entry
->list
, &target_reset_callback_list
);
1699 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1700 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1702 struct target_trace_callback
*entry
;
1705 return ERROR_COMMAND_SYNTAX_ERROR
;
1707 entry
= malloc(sizeof(struct target_trace_callback
));
1709 LOG_ERROR("error allocating buffer for trace callback entry");
1710 return ERROR_COMMAND_SYNTAX_ERROR
;
1713 entry
->callback
= callback
;
1715 list_add(&entry
->list
, &target_trace_callback_list
);
1721 int target_register_timer_callback(int (*callback
)(void *priv
),
1722 unsigned int time_ms
, enum target_timer_type type
, void *priv
)
1724 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1727 return ERROR_COMMAND_SYNTAX_ERROR
;
1730 while ((*callbacks_p
)->next
)
1731 callbacks_p
= &((*callbacks_p
)->next
);
1732 callbacks_p
= &((*callbacks_p
)->next
);
1735 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1736 (*callbacks_p
)->callback
= callback
;
1737 (*callbacks_p
)->type
= type
;
1738 (*callbacks_p
)->time_ms
= time_ms
;
1739 (*callbacks_p
)->removed
= false;
1741 (*callbacks_p
)->when
= timeval_ms() + time_ms
;
1742 target_timer_next_event_value
= MIN(target_timer_next_event_value
, (*callbacks_p
)->when
);
1744 (*callbacks_p
)->priv
= priv
;
1745 (*callbacks_p
)->next
= NULL
;
1750 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1751 enum target_event event
, void *priv
), void *priv
)
1753 struct target_event_callback
**p
= &target_event_callbacks
;
1754 struct target_event_callback
*c
= target_event_callbacks
;
1757 return ERROR_COMMAND_SYNTAX_ERROR
;
1760 struct target_event_callback
*next
= c
->next
;
1761 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1773 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1774 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1776 struct target_reset_callback
*entry
;
1779 return ERROR_COMMAND_SYNTAX_ERROR
;
1781 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1782 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1783 list_del(&entry
->list
);
1792 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1793 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1795 struct target_trace_callback
*entry
;
1798 return ERROR_COMMAND_SYNTAX_ERROR
;
1800 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1801 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1802 list_del(&entry
->list
);
1811 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1814 return ERROR_COMMAND_SYNTAX_ERROR
;
1816 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1818 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1827 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1829 struct target_event_callback
*callback
= target_event_callbacks
;
1830 struct target_event_callback
*next_callback
;
1832 if (event
== TARGET_EVENT_HALTED
) {
1833 /* execute early halted first */
1834 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1837 LOG_DEBUG("target event %i (%s) for core %s", event
,
1838 target_event_name(event
),
1839 target_name(target
));
1841 target_handle_event(target
, event
);
1844 next_callback
= callback
->next
;
1845 callback
->callback(target
, event
, callback
->priv
);
1846 callback
= next_callback
;
1852 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1854 struct target_reset_callback
*callback
;
1856 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1857 jim_nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1859 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1860 callback
->callback(target
, reset_mode
, callback
->priv
);
1865 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1867 struct target_trace_callback
*callback
;
1869 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1870 callback
->callback(target
, len
, data
, callback
->priv
);
1875 static int target_timer_callback_periodic_restart(
1876 struct target_timer_callback
*cb
, int64_t *now
)
1878 cb
->when
= *now
+ cb
->time_ms
;
1882 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1885 cb
->callback(cb
->priv
);
1887 if (cb
->type
== TARGET_TIMER_TYPE_PERIODIC
)
1888 return target_timer_callback_periodic_restart(cb
, now
);
1890 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1893 static int target_call_timer_callbacks_check_time(int checktime
)
1895 static bool callback_processing
;
1897 /* Do not allow nesting */
1898 if (callback_processing
)
1901 callback_processing
= true;
1905 int64_t now
= timeval_ms();
1907 /* Initialize to a default value that's a ways into the future.
1908 * The loop below will make it closer to now if there are
1909 * callbacks that want to be called sooner. */
1910 target_timer_next_event_value
= now
+ 1000;
1912 /* Store an address of the place containing a pointer to the
1913 * next item; initially, that's a standalone "root of the
1914 * list" variable. */
1915 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1916 while (callback
&& *callback
) {
1917 if ((*callback
)->removed
) {
1918 struct target_timer_callback
*p
= *callback
;
1919 *callback
= (*callback
)->next
;
1924 bool call_it
= (*callback
)->callback
&&
1925 ((!checktime
&& (*callback
)->type
== TARGET_TIMER_TYPE_PERIODIC
) ||
1926 now
>= (*callback
)->when
);
1929 target_call_timer_callback(*callback
, &now
);
1931 if (!(*callback
)->removed
&& (*callback
)->when
< target_timer_next_event_value
)
1932 target_timer_next_event_value
= (*callback
)->when
;
1934 callback
= &(*callback
)->next
;
1937 callback_processing
= false;
1941 int target_call_timer_callbacks()
1943 return target_call_timer_callbacks_check_time(1);
1946 /* invoke periodic callbacks immediately */
1947 int target_call_timer_callbacks_now()
1949 return target_call_timer_callbacks_check_time(0);
1952 int64_t target_timer_next_event(void)
1954 return target_timer_next_event_value
;
1957 /* Prints the working area layout for debug purposes */
1958 static void print_wa_layout(struct target
*target
)
1960 struct working_area
*c
= target
->working_areas
;
1963 LOG_DEBUG("%c%c " TARGET_ADDR_FMT
"-" TARGET_ADDR_FMT
" (%" PRIu32
" bytes)",
1964 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1965 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1970 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1971 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1973 assert(area
->free
); /* Shouldn't split an allocated area */
1974 assert(size
<= area
->size
); /* Caller should guarantee this */
1976 /* Split only if not already the right size */
1977 if (size
< area
->size
) {
1978 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1983 new_wa
->next
= area
->next
;
1984 new_wa
->size
= area
->size
- size
;
1985 new_wa
->address
= area
->address
+ size
;
1986 new_wa
->backup
= NULL
;
1987 new_wa
->user
= NULL
;
1988 new_wa
->free
= true;
1990 area
->next
= new_wa
;
1993 /* If backup memory was allocated to this area, it has the wrong size
1994 * now so free it and it will be reallocated if/when needed */
1996 area
->backup
= NULL
;
2000 /* Merge all adjacent free areas into one */
2001 static void target_merge_working_areas(struct target
*target
)
2003 struct working_area
*c
= target
->working_areas
;
2005 while (c
&& c
->next
) {
2006 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
2008 /* Find two adjacent free areas */
2009 if (c
->free
&& c
->next
->free
) {
2010 /* Merge the last into the first */
2011 c
->size
+= c
->next
->size
;
2013 /* Remove the last */
2014 struct working_area
*to_be_freed
= c
->next
;
2015 c
->next
= c
->next
->next
;
2016 free(to_be_freed
->backup
);
2019 /* If backup memory was allocated to the remaining area, it's has
2020 * the wrong size now */
2029 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
2031 /* Reevaluate working area address based on MMU state*/
2032 if (!target
->working_areas
) {
2036 retval
= target
->type
->mmu(target
, &enabled
);
2037 if (retval
!= ERROR_OK
)
2041 if (target
->working_area_phys_spec
) {
2042 LOG_DEBUG("MMU disabled, using physical "
2043 "address for working memory " TARGET_ADDR_FMT
,
2044 target
->working_area_phys
);
2045 target
->working_area
= target
->working_area_phys
;
2047 LOG_ERROR("No working memory available. "
2048 "Specify -work-area-phys to target.");
2049 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2052 if (target
->working_area_virt_spec
) {
2053 LOG_DEBUG("MMU enabled, using virtual "
2054 "address for working memory " TARGET_ADDR_FMT
,
2055 target
->working_area_virt
);
2056 target
->working_area
= target
->working_area_virt
;
2058 LOG_ERROR("No working memory available. "
2059 "Specify -work-area-virt to target.");
2060 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2064 /* Set up initial working area on first call */
2065 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
2067 new_wa
->next
= NULL
;
2068 new_wa
->size
= ALIGN_DOWN(target
->working_area_size
, 4); /* 4-byte align */
2069 new_wa
->address
= target
->working_area
;
2070 new_wa
->backup
= NULL
;
2071 new_wa
->user
= NULL
;
2072 new_wa
->free
= true;
2075 target
->working_areas
= new_wa
;
2078 /* only allocate multiples of 4 byte */
2079 size
= ALIGN_UP(size
, 4);
2081 struct working_area
*c
= target
->working_areas
;
2083 /* Find the first large enough working area */
2085 if (c
->free
&& c
->size
>= size
)
2091 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2093 /* Split the working area into the requested size */
2094 target_split_working_area(c
, size
);
2096 LOG_DEBUG("allocated new working area of %" PRIu32
" bytes at address " TARGET_ADDR_FMT
,
2099 if (target
->backup_working_area
) {
2101 c
->backup
= malloc(c
->size
);
2106 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
2107 if (retval
!= ERROR_OK
)
2111 /* mark as used, and return the new (reused) area */
2118 print_wa_layout(target
);
2123 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
2127 retval
= target_alloc_working_area_try(target
, size
, area
);
2128 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
2129 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
2134 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
2136 int retval
= ERROR_OK
;
2138 if (target
->backup_working_area
&& area
->backup
) {
2139 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
2140 if (retval
!= ERROR_OK
)
2141 LOG_ERROR("failed to restore %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
2142 area
->size
, area
->address
);
2148 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
2149 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
2151 if (!area
|| area
->free
)
2154 int retval
= ERROR_OK
;
2156 retval
= target_restore_working_area(target
, area
);
2157 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
2158 if (retval
!= ERROR_OK
)
2164 LOG_DEBUG("freed %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
2165 area
->size
, area
->address
);
2167 /* mark user pointer invalid */
2168 /* TODO: Is this really safe? It points to some previous caller's memory.
2169 * How could we know that the area pointer is still in that place and not
2170 * some other vital data? What's the purpose of this, anyway? */
2174 target_merge_working_areas(target
);
2176 print_wa_layout(target
);
2181 int target_free_working_area(struct target
*target
, struct working_area
*area
)
2183 return target_free_working_area_restore(target
, area
, 1);
2186 /* free resources and restore memory, if restoring memory fails,
2187 * free up resources anyway
2189 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
2191 struct working_area
*c
= target
->working_areas
;
2193 LOG_DEBUG("freeing all working areas");
2195 /* Loop through all areas, restoring the allocated ones and marking them as free */
2199 target_restore_working_area(target
, c
);
2201 *c
->user
= NULL
; /* Same as above */
2207 /* Run a merge pass to combine all areas into one */
2208 target_merge_working_areas(target
);
2210 print_wa_layout(target
);
2213 void target_free_all_working_areas(struct target
*target
)
2215 target_free_all_working_areas_restore(target
, 1);
2217 /* Now we have none or only one working area marked as free */
2218 if (target
->working_areas
) {
2219 /* Free the last one to allow on-the-fly moving and resizing */
2220 free(target
->working_areas
->backup
);
2221 free(target
->working_areas
);
2222 target
->working_areas
= NULL
;
2226 /* Find the largest number of bytes that can be allocated */
2227 uint32_t target_get_working_area_avail(struct target
*target
)
2229 struct working_area
*c
= target
->working_areas
;
2230 uint32_t max_size
= 0;
2233 return ALIGN_DOWN(target
->working_area_size
, 4);
2236 if (c
->free
&& max_size
< c
->size
)
2245 static void target_destroy(struct target
*target
)
2247 if (target
->type
->deinit_target
)
2248 target
->type
->deinit_target(target
);
2250 if (target
->semihosting
)
2251 free(target
->semihosting
->basedir
);
2252 free(target
->semihosting
);
2254 jtag_unregister_event_callback(jtag_enable_callback
, target
);
2256 struct target_event_action
*teap
= target
->event_action
;
2258 struct target_event_action
*next
= teap
->next
;
2259 Jim_DecrRefCount(teap
->interp
, teap
->body
);
2264 target_free_all_working_areas(target
);
2266 /* release the targets SMP list */
2268 struct target_list
*head
, *tmp
;
2270 list_for_each_entry_safe(head
, tmp
, target
->smp_targets
, lh
) {
2271 list_del(&head
->lh
);
2272 head
->target
->smp
= 0;
2275 if (target
->smp_targets
!= &empty_smp_targets
)
2276 free(target
->smp_targets
);
2280 rtos_destroy(target
);
2282 free(target
->gdb_port_override
);
2284 free(target
->trace_info
);
2285 free(target
->fileio_info
);
2286 free(target
->cmd_name
);
2290 void target_quit(void)
2292 struct target_event_callback
*pe
= target_event_callbacks
;
2294 struct target_event_callback
*t
= pe
->next
;
2298 target_event_callbacks
= NULL
;
2300 struct target_timer_callback
*pt
= target_timer_callbacks
;
2302 struct target_timer_callback
*t
= pt
->next
;
2306 target_timer_callbacks
= NULL
;
2308 for (struct target
*target
= all_targets
; target
;) {
2312 target_destroy(target
);
2319 int target_arch_state(struct target
*target
)
2323 LOG_WARNING("No target has been configured");
2327 if (target
->state
!= TARGET_HALTED
)
2330 retval
= target
->type
->arch_state(target
);
2334 static int target_get_gdb_fileio_info_default(struct target
*target
,
2335 struct gdb_fileio_info
*fileio_info
)
2337 /* If target does not support semi-hosting function, target
2338 has no need to provide .get_gdb_fileio_info callback.
2339 It just return ERROR_FAIL and gdb_server will return "Txx"
2340 as target halted every time. */
2344 static int target_gdb_fileio_end_default(struct target
*target
,
2345 int retcode
, int fileio_errno
, bool ctrl_c
)
2350 int target_profiling_default(struct target
*target
, uint32_t *samples
,
2351 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
2353 struct timeval timeout
, now
;
2355 gettimeofday(&timeout
, NULL
);
2356 timeval_add_time(&timeout
, seconds
, 0);
2358 LOG_INFO("Starting profiling. Halting and resuming the"
2359 " target as often as we can...");
2361 uint32_t sample_count
= 0;
2362 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2363 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", true);
2365 int retval
= ERROR_OK
;
2367 target_poll(target
);
2368 if (target
->state
== TARGET_HALTED
) {
2369 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
2370 samples
[sample_count
++] = t
;
2371 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2372 retval
= target_resume(target
, 1, 0, 0, 0);
2373 target_poll(target
);
2374 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2375 } else if (target
->state
== TARGET_RUNNING
) {
2376 /* We want to quickly sample the PC. */
2377 retval
= target_halt(target
);
2379 LOG_INFO("Target not halted or running");
2384 if (retval
!= ERROR_OK
)
2387 gettimeofday(&now
, NULL
);
2388 if ((sample_count
>= max_num_samples
) || timeval_compare(&now
, &timeout
) >= 0) {
2389 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2394 *num_samples
= sample_count
;
2398 /* Single aligned words are guaranteed to use 16 or 32 bit access
2399 * mode respectively, otherwise data is handled as quickly as
2402 int target_write_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, const uint8_t *buffer
)
2404 LOG_DEBUG("writing buffer of %" PRIu32
" byte at " TARGET_ADDR_FMT
,
2407 if (!target_was_examined(target
)) {
2408 LOG_ERROR("Target not examined yet");
2415 if ((address
+ size
- 1) < address
) {
2416 /* GDB can request this when e.g. PC is 0xfffffffc */
2417 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2423 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2426 static int target_write_buffer_default(struct target
*target
,
2427 target_addr_t address
, uint32_t count
, const uint8_t *buffer
)
2430 unsigned int data_bytes
= target_data_bits(target
) / 8;
2432 /* Align up to maximum bytes. The loop condition makes sure the next pass
2433 * will have something to do with the size we leave to it. */
2435 size
< data_bytes
&& count
>= size
* 2 + (address
& size
);
2437 if (address
& size
) {
2438 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2439 if (retval
!= ERROR_OK
)
2447 /* Write the data with as large access size as possible. */
2448 for (; size
> 0; size
/= 2) {
2449 uint32_t aligned
= count
- count
% size
;
2451 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2452 if (retval
!= ERROR_OK
)
2463 /* Single aligned words are guaranteed to use 16 or 32 bit access
2464 * mode respectively, otherwise data is handled as quickly as
2467 int target_read_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
2469 LOG_DEBUG("reading buffer of %" PRIu32
" byte at " TARGET_ADDR_FMT
,
2472 if (!target_was_examined(target
)) {
2473 LOG_ERROR("Target not examined yet");
2480 if ((address
+ size
- 1) < address
) {
2481 /* GDB can request this when e.g. PC is 0xfffffffc */
2482 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2488 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2491 static int target_read_buffer_default(struct target
*target
, target_addr_t address
, uint32_t count
, uint8_t *buffer
)
2494 unsigned int data_bytes
= target_data_bits(target
) / 8;
2496 /* Align up to maximum bytes. The loop condition makes sure the next pass
2497 * will have something to do with the size we leave to it. */
2499 size
< data_bytes
&& count
>= size
* 2 + (address
& size
);
2501 if (address
& size
) {
2502 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2503 if (retval
!= ERROR_OK
)
2511 /* Read the data with as large access size as possible. */
2512 for (; size
> 0; size
/= 2) {
2513 uint32_t aligned
= count
- count
% size
;
2515 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2516 if (retval
!= ERROR_OK
)
2527 int target_checksum_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t *crc
)
2532 uint32_t checksum
= 0;
2533 if (!target_was_examined(target
)) {
2534 LOG_ERROR("Target not examined yet");
2537 if (!target
->type
->checksum_memory
) {
2538 LOG_ERROR("Target %s doesn't support checksum_memory", target_name(target
));
2542 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2543 if (retval
!= ERROR_OK
) {
2544 buffer
= malloc(size
);
2546 LOG_ERROR("error allocating buffer for section (%" PRIu32
" bytes)", size
);
2547 return ERROR_COMMAND_SYNTAX_ERROR
;
2549 retval
= target_read_buffer(target
, address
, size
, buffer
);
2550 if (retval
!= ERROR_OK
) {
2555 /* convert to target endianness */
2556 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2557 uint32_t target_data
;
2558 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2559 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2562 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2571 int target_blank_check_memory(struct target
*target
,
2572 struct target_memory_check_block
*blocks
, int num_blocks
,
2573 uint8_t erased_value
)
2575 if (!target_was_examined(target
)) {
2576 LOG_ERROR("Target not examined yet");
2580 if (!target
->type
->blank_check_memory
)
2581 return ERROR_NOT_IMPLEMENTED
;
2583 return target
->type
->blank_check_memory(target
, blocks
, num_blocks
, erased_value
);
2586 int target_read_u64(struct target
*target
, target_addr_t address
, uint64_t *value
)
2588 uint8_t value_buf
[8];
2589 if (!target_was_examined(target
)) {
2590 LOG_ERROR("Target not examined yet");
2594 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2596 if (retval
== ERROR_OK
) {
2597 *value
= target_buffer_get_u64(target
, value_buf
);
2598 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2603 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2610 int target_read_u32(struct target
*target
, target_addr_t address
, uint32_t *value
)
2612 uint8_t value_buf
[4];
2613 if (!target_was_examined(target
)) {
2614 LOG_ERROR("Target not examined yet");
2618 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2620 if (retval
== ERROR_OK
) {
2621 *value
= target_buffer_get_u32(target
, value_buf
);
2622 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2627 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2634 int target_read_u16(struct target
*target
, target_addr_t address
, uint16_t *value
)
2636 uint8_t value_buf
[2];
2637 if (!target_was_examined(target
)) {
2638 LOG_ERROR("Target not examined yet");
2642 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2644 if (retval
== ERROR_OK
) {
2645 *value
= target_buffer_get_u16(target
, value_buf
);
2646 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%4.4" PRIx16
,
2651 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2658 int target_read_u8(struct target
*target
, target_addr_t address
, uint8_t *value
)
2660 if (!target_was_examined(target
)) {
2661 LOG_ERROR("Target not examined yet");
2665 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2667 if (retval
== ERROR_OK
) {
2668 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2673 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2680 int target_write_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2683 uint8_t value_buf
[8];
2684 if (!target_was_examined(target
)) {
2685 LOG_ERROR("Target not examined yet");
2689 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2693 target_buffer_set_u64(target
, value_buf
, value
);
2694 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2695 if (retval
!= ERROR_OK
)
2696 LOG_DEBUG("failed: %i", retval
);
2701 int target_write_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2704 uint8_t value_buf
[4];
2705 if (!target_was_examined(target
)) {
2706 LOG_ERROR("Target not examined yet");
2710 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2714 target_buffer_set_u32(target
, value_buf
, value
);
2715 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2716 if (retval
!= ERROR_OK
)
2717 LOG_DEBUG("failed: %i", retval
);
2722 int target_write_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2725 uint8_t value_buf
[2];
2726 if (!target_was_examined(target
)) {
2727 LOG_ERROR("Target not examined yet");
2731 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2735 target_buffer_set_u16(target
, value_buf
, value
);
2736 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2737 if (retval
!= ERROR_OK
)
2738 LOG_DEBUG("failed: %i", retval
);
2743 int target_write_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2746 if (!target_was_examined(target
)) {
2747 LOG_ERROR("Target not examined yet");
2751 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2754 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2755 if (retval
!= ERROR_OK
)
2756 LOG_DEBUG("failed: %i", retval
);
2761 int target_write_phys_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2764 uint8_t value_buf
[8];
2765 if (!target_was_examined(target
)) {
2766 LOG_ERROR("Target not examined yet");
2770 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2774 target_buffer_set_u64(target
, value_buf
, value
);
2775 retval
= target_write_phys_memory(target
, address
, 8, 1, value_buf
);
2776 if (retval
!= ERROR_OK
)
2777 LOG_DEBUG("failed: %i", retval
);
2782 int target_write_phys_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2785 uint8_t value_buf
[4];
2786 if (!target_was_examined(target
)) {
2787 LOG_ERROR("Target not examined yet");
2791 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2795 target_buffer_set_u32(target
, value_buf
, value
);
2796 retval
= target_write_phys_memory(target
, address
, 4, 1, value_buf
);
2797 if (retval
!= ERROR_OK
)
2798 LOG_DEBUG("failed: %i", retval
);
2803 int target_write_phys_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2806 uint8_t value_buf
[2];
2807 if (!target_was_examined(target
)) {
2808 LOG_ERROR("Target not examined yet");
2812 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2816 target_buffer_set_u16(target
, value_buf
, value
);
2817 retval
= target_write_phys_memory(target
, address
, 2, 1, value_buf
);
2818 if (retval
!= ERROR_OK
)
2819 LOG_DEBUG("failed: %i", retval
);
2824 int target_write_phys_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2827 if (!target_was_examined(target
)) {
2828 LOG_ERROR("Target not examined yet");
2832 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2835 retval
= target_write_phys_memory(target
, address
, 1, 1, &value
);
2836 if (retval
!= ERROR_OK
)
2837 LOG_DEBUG("failed: %i", retval
);
2842 static int find_target(struct command_invocation
*cmd
, const char *name
)
2844 struct target
*target
= get_target(name
);
2846 command_print(cmd
, "Target: %s is unknown, try one of:\n", name
);
2849 if (!target
->tap
->enabled
) {
2850 command_print(cmd
, "Target: TAP %s is disabled, "
2851 "can't be the current target\n",
2852 target
->tap
->dotted_name
);
2856 cmd
->ctx
->current_target
= target
;
2857 if (cmd
->ctx
->current_target_override
)
2858 cmd
->ctx
->current_target_override
= target
;
2864 COMMAND_HANDLER(handle_targets_command
)
2866 int retval
= ERROR_OK
;
2867 if (CMD_ARGC
== 1) {
2868 retval
= find_target(CMD
, CMD_ARGV
[0]);
2869 if (retval
== ERROR_OK
) {
2875 struct target
*target
= all_targets
;
2876 command_print(CMD
, " TargetName Type Endian TapName State ");
2877 command_print(CMD
, "-- ------------------ ---------- ------ ------------------ ------------");
2882 if (target
->tap
->enabled
)
2883 state
= target_state_name(target
);
2885 state
= "tap-disabled";
2887 if (CMD_CTX
->current_target
== target
)
2890 /* keep columns lined up to match the headers above */
2892 "%2d%c %-18s %-10s %-6s %-18s %s",
2893 target
->target_number
,
2895 target_name(target
),
2896 target_type_name(target
),
2897 jim_nvp_value2name_simple(nvp_target_endian
,
2898 target
->endianness
)->name
,
2899 target
->tap
->dotted_name
,
2901 target
= target
->next
;
2907 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2909 static int power_dropout
;
2910 static int srst_asserted
;
2912 static int run_power_restore
;
2913 static int run_power_dropout
;
2914 static int run_srst_asserted
;
2915 static int run_srst_deasserted
;
2917 static int sense_handler(void)
2919 static int prev_srst_asserted
;
2920 static int prev_power_dropout
;
2922 int retval
= jtag_power_dropout(&power_dropout
);
2923 if (retval
!= ERROR_OK
)
2927 power_restored
= prev_power_dropout
&& !power_dropout
;
2929 run_power_restore
= 1;
2931 int64_t current
= timeval_ms();
2932 static int64_t last_power
;
2933 bool wait_more
= last_power
+ 2000 > current
;
2934 if (power_dropout
&& !wait_more
) {
2935 run_power_dropout
= 1;
2936 last_power
= current
;
2939 retval
= jtag_srst_asserted(&srst_asserted
);
2940 if (retval
!= ERROR_OK
)
2943 int srst_deasserted
;
2944 srst_deasserted
= prev_srst_asserted
&& !srst_asserted
;
2946 static int64_t last_srst
;
2947 wait_more
= last_srst
+ 2000 > current
;
2948 if (srst_deasserted
&& !wait_more
) {
2949 run_srst_deasserted
= 1;
2950 last_srst
= current
;
2953 if (!prev_srst_asserted
&& srst_asserted
)
2954 run_srst_asserted
= 1;
2956 prev_srst_asserted
= srst_asserted
;
2957 prev_power_dropout
= power_dropout
;
2959 if (srst_deasserted
|| power_restored
) {
2960 /* Other than logging the event we can't do anything here.
2961 * Issuing a reset is a particularly bad idea as we might
2962 * be inside a reset already.
2969 /* process target state changes */
2970 static int handle_target(void *priv
)
2972 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2973 int retval
= ERROR_OK
;
2975 if (!is_jtag_poll_safe()) {
2976 /* polling is disabled currently */
2980 /* we do not want to recurse here... */
2981 static int recursive
;
2985 /* danger! running these procedures can trigger srst assertions and power dropouts.
2986 * We need to avoid an infinite loop/recursion here and we do that by
2987 * clearing the flags after running these events.
2989 int did_something
= 0;
2990 if (run_srst_asserted
) {
2991 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2992 Jim_Eval(interp
, "srst_asserted");
2995 if (run_srst_deasserted
) {
2996 Jim_Eval(interp
, "srst_deasserted");
2999 if (run_power_dropout
) {
3000 LOG_INFO("Power dropout detected, running power_dropout proc.");
3001 Jim_Eval(interp
, "power_dropout");
3004 if (run_power_restore
) {
3005 Jim_Eval(interp
, "power_restore");
3009 if (did_something
) {
3010 /* clear detect flags */
3014 /* clear action flags */
3016 run_srst_asserted
= 0;
3017 run_srst_deasserted
= 0;
3018 run_power_restore
= 0;
3019 run_power_dropout
= 0;
3024 /* Poll targets for state changes unless that's globally disabled.
3025 * Skip targets that are currently disabled.
3027 for (struct target
*target
= all_targets
;
3028 is_jtag_poll_safe() && target
;
3029 target
= target
->next
) {
3031 if (!target_was_examined(target
))
3034 if (!target
->tap
->enabled
)
3037 if (target
->backoff
.times
> target
->backoff
.count
) {
3038 /* do not poll this time as we failed previously */
3039 target
->backoff
.count
++;
3042 target
->backoff
.count
= 0;
3044 /* only poll target if we've got power and srst isn't asserted */
3045 if (!power_dropout
&& !srst_asserted
) {
3046 /* polling may fail silently until the target has been examined */
3047 retval
= target_poll(target
);
3048 if (retval
!= ERROR_OK
) {
3049 /* 100ms polling interval. Increase interval between polling up to 5000ms */
3050 if (target
->backoff
.times
* polling_interval
< 5000) {
3051 target
->backoff
.times
*= 2;
3052 target
->backoff
.times
++;
3055 /* Tell GDB to halt the debugger. This allows the user to
3056 * run monitor commands to handle the situation.
3058 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
3060 if (target
->backoff
.times
> 0) {
3061 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
3062 target_reset_examined(target
);
3063 retval
= target_examine_one(target
);
3064 /* Target examination could have failed due to unstable connection,
3065 * but we set the examined flag anyway to repoll it later */
3066 if (retval
!= ERROR_OK
) {
3067 target_set_examined(target
);
3068 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
3069 target
->backoff
.times
* polling_interval
);
3074 /* Since we succeeded, we reset backoff count */
3075 target
->backoff
.times
= 0;
3082 COMMAND_HANDLER(handle_reg_command
)
3086 struct target
*target
= get_current_target(CMD_CTX
);
3087 struct reg
*reg
= NULL
;
3089 /* list all available registers for the current target */
3090 if (CMD_ARGC
== 0) {
3091 struct reg_cache
*cache
= target
->reg_cache
;
3093 unsigned int count
= 0;
3097 command_print(CMD
, "===== %s", cache
->name
);
3099 for (i
= 0, reg
= cache
->reg_list
;
3100 i
< cache
->num_regs
;
3101 i
++, reg
++, count
++) {
3102 if (reg
->exist
== false || reg
->hidden
)
3104 /* only print cached values if they are valid */
3106 char *value
= buf_to_hex_str(reg
->value
,
3109 "(%i) %s (/%" PRIu32
"): 0x%s%s",
3117 command_print(CMD
, "(%i) %s (/%" PRIu32
")",
3122 cache
= cache
->next
;
3128 /* access a single register by its ordinal number */
3129 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
3131 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
3133 struct reg_cache
*cache
= target
->reg_cache
;
3134 unsigned int count
= 0;
3137 for (i
= 0; i
< cache
->num_regs
; i
++) {
3138 if (count
++ == num
) {
3139 reg
= &cache
->reg_list
[i
];
3145 cache
= cache
->next
;
3149 command_print(CMD
, "%i is out of bounds, the current target "
3150 "has only %i registers (0 - %i)", num
, count
, count
- 1);
3154 /* access a single register by its name */
3155 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], true);
3161 assert(reg
); /* give clang a hint that we *know* reg is != NULL here */
3166 /* display a register */
3167 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
3168 && (CMD_ARGV
[1][0] <= '9')))) {
3169 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
3172 if (reg
->valid
== 0) {
3173 int retval
= reg
->type
->get(reg
);
3174 if (retval
!= ERROR_OK
) {
3175 LOG_ERROR("Could not read register '%s'", reg
->name
);
3179 char *value
= buf_to_hex_str(reg
->value
, reg
->size
);
3180 command_print(CMD
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
3185 /* set register value */
3186 if (CMD_ARGC
== 2) {
3187 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
3190 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
3192 int retval
= reg
->type
->set(reg
, buf
);
3193 if (retval
!= ERROR_OK
) {
3194 LOG_ERROR("Could not write to register '%s'", reg
->name
);
3196 char *value
= buf_to_hex_str(reg
->value
, reg
->size
);
3197 command_print(CMD
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
3206 return ERROR_COMMAND_SYNTAX_ERROR
;
3209 command_print(CMD
, "register %s not found in current target", CMD_ARGV
[0]);
3213 COMMAND_HANDLER(handle_poll_command
)
3215 int retval
= ERROR_OK
;
3216 struct target
*target
= get_current_target(CMD_CTX
);
3218 if (CMD_ARGC
== 0) {
3219 command_print(CMD
, "background polling: %s",
3220 jtag_poll_get_enabled() ? "on" : "off");
3221 command_print(CMD
, "TAP: %s (%s)",
3222 target
->tap
->dotted_name
,
3223 target
->tap
->enabled
? "enabled" : "disabled");
3224 if (!target
->tap
->enabled
)
3226 retval
= target_poll(target
);
3227 if (retval
!= ERROR_OK
)
3229 retval
= target_arch_state(target
);
3230 if (retval
!= ERROR_OK
)
3232 } else if (CMD_ARGC
== 1) {
3234 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
3235 jtag_poll_set_enabled(enable
);
3237 return ERROR_COMMAND_SYNTAX_ERROR
;
3242 COMMAND_HANDLER(handle_wait_halt_command
)
3245 return ERROR_COMMAND_SYNTAX_ERROR
;
3247 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
3248 if (1 == CMD_ARGC
) {
3249 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
3250 if (retval
!= ERROR_OK
)
3251 return ERROR_COMMAND_SYNTAX_ERROR
;
3254 struct target
*target
= get_current_target(CMD_CTX
);
3255 return target_wait_state(target
, TARGET_HALTED
, ms
);
3258 /* wait for target state to change. The trick here is to have a low
3259 * latency for short waits and not to suck up all the CPU time
3262 * After 500ms, keep_alive() is invoked
3264 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
3267 int64_t then
= 0, cur
;
3271 retval
= target_poll(target
);
3272 if (retval
!= ERROR_OK
)
3274 if (target
->state
== state
)
3279 then
= timeval_ms();
3280 LOG_DEBUG("waiting for target %s...",
3281 jim_nvp_value2name_simple(nvp_target_state
, state
)->name
);
3287 if ((cur
-then
) > ms
) {
3288 LOG_ERROR("timed out while waiting for target %s",
3289 jim_nvp_value2name_simple(nvp_target_state
, state
)->name
);
3297 COMMAND_HANDLER(handle_halt_command
)
3301 struct target
*target
= get_current_target(CMD_CTX
);
3303 target
->verbose_halt_msg
= true;
3305 int retval
= target_halt(target
);
3306 if (retval
!= ERROR_OK
)
3309 if (CMD_ARGC
== 1) {
3310 unsigned wait_local
;
3311 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
3312 if (retval
!= ERROR_OK
)
3313 return ERROR_COMMAND_SYNTAX_ERROR
;
3318 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
3321 COMMAND_HANDLER(handle_soft_reset_halt_command
)
3323 struct target
*target
= get_current_target(CMD_CTX
);
3325 LOG_TARGET_INFO(target
, "requesting target halt and executing a soft reset");
3327 target_soft_reset_halt(target
);
3332 COMMAND_HANDLER(handle_reset_command
)
3335 return ERROR_COMMAND_SYNTAX_ERROR
;
3337 enum target_reset_mode reset_mode
= RESET_RUN
;
3338 if (CMD_ARGC
== 1) {
3339 const struct jim_nvp
*n
;
3340 n
= jim_nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
3341 if ((!n
->name
) || (n
->value
== RESET_UNKNOWN
))
3342 return ERROR_COMMAND_SYNTAX_ERROR
;
3343 reset_mode
= n
->value
;
3346 /* reset *all* targets */
3347 return target_process_reset(CMD
, reset_mode
);
3351 COMMAND_HANDLER(handle_resume_command
)
3355 return ERROR_COMMAND_SYNTAX_ERROR
;
3357 struct target
*target
= get_current_target(CMD_CTX
);
3359 /* with no CMD_ARGV, resume from current pc, addr = 0,
3360 * with one arguments, addr = CMD_ARGV[0],
3361 * handle breakpoints, not debugging */
3362 target_addr_t addr
= 0;
3363 if (CMD_ARGC
== 1) {
3364 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3368 return target_resume(target
, current
, addr
, 1, 0);
3371 COMMAND_HANDLER(handle_step_command
)
3374 return ERROR_COMMAND_SYNTAX_ERROR
;
3378 /* with no CMD_ARGV, step from current pc, addr = 0,
3379 * with one argument addr = CMD_ARGV[0],
3380 * handle breakpoints, debugging */
3381 target_addr_t addr
= 0;
3383 if (CMD_ARGC
== 1) {
3384 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3388 struct target
*target
= get_current_target(CMD_CTX
);
3390 return target_step(target
, current_pc
, addr
, 1);
3393 void target_handle_md_output(struct command_invocation
*cmd
,
3394 struct target
*target
, target_addr_t address
, unsigned size
,
3395 unsigned count
, const uint8_t *buffer
)
3397 const unsigned line_bytecnt
= 32;
3398 unsigned line_modulo
= line_bytecnt
/ size
;
3400 char output
[line_bytecnt
* 4 + 1];
3401 unsigned output_len
= 0;
3403 const char *value_fmt
;
3406 value_fmt
= "%16.16"PRIx64
" ";
3409 value_fmt
= "%8.8"PRIx64
" ";
3412 value_fmt
= "%4.4"PRIx64
" ";
3415 value_fmt
= "%2.2"PRIx64
" ";
3418 /* "can't happen", caller checked */
3419 LOG_ERROR("invalid memory read size: %u", size
);
3423 for (unsigned i
= 0; i
< count
; i
++) {
3424 if (i
% line_modulo
== 0) {
3425 output_len
+= snprintf(output
+ output_len
,
3426 sizeof(output
) - output_len
,
3427 TARGET_ADDR_FMT
": ",
3428 (address
+ (i
* size
)));
3432 const uint8_t *value_ptr
= buffer
+ i
* size
;
3435 value
= target_buffer_get_u64(target
, value_ptr
);
3438 value
= target_buffer_get_u32(target
, value_ptr
);
3441 value
= target_buffer_get_u16(target
, value_ptr
);
3446 output_len
+= snprintf(output
+ output_len
,
3447 sizeof(output
) - output_len
,
3450 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
3451 command_print(cmd
, "%s", output
);
3457 COMMAND_HANDLER(handle_md_command
)
3460 return ERROR_COMMAND_SYNTAX_ERROR
;
3463 switch (CMD_NAME
[2]) {
3477 return ERROR_COMMAND_SYNTAX_ERROR
;
3480 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3481 int (*fn
)(struct target
*target
,
3482 target_addr_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
3486 fn
= target_read_phys_memory
;
3488 fn
= target_read_memory
;
3489 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
3490 return ERROR_COMMAND_SYNTAX_ERROR
;
3492 target_addr_t address
;
3493 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3497 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
3499 uint8_t *buffer
= calloc(count
, size
);
3501 LOG_ERROR("Failed to allocate md read buffer");
3505 struct target
*target
= get_current_target(CMD_CTX
);
3506 int retval
= fn(target
, address
, size
, count
, buffer
);
3507 if (retval
== ERROR_OK
)
3508 target_handle_md_output(CMD
, target
, address
, size
, count
, buffer
);
3515 typedef int (*target_write_fn
)(struct target
*target
,
3516 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
3518 static int target_fill_mem(struct target
*target
,
3519 target_addr_t address
,
3527 /* We have to write in reasonably large chunks to be able
3528 * to fill large memory areas with any sane speed */
3529 const unsigned chunk_size
= 16384;
3530 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
3532 LOG_ERROR("Out of memory");
3536 for (unsigned i
= 0; i
< chunk_size
; i
++) {
3537 switch (data_size
) {
3539 target_buffer_set_u64(target
, target_buf
+ i
* data_size
, b
);
3542 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
3545 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
3548 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
3555 int retval
= ERROR_OK
;
3557 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
3560 if (current
> chunk_size
)
3561 current
= chunk_size
;
3562 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
3563 if (retval
!= ERROR_OK
)
3565 /* avoid GDB timeouts */
3574 COMMAND_HANDLER(handle_mw_command
)
3577 return ERROR_COMMAND_SYNTAX_ERROR
;
3578 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3583 fn
= target_write_phys_memory
;
3585 fn
= target_write_memory
;
3586 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
3587 return ERROR_COMMAND_SYNTAX_ERROR
;
3589 target_addr_t address
;
3590 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3593 COMMAND_PARSE_NUMBER(u64
, CMD_ARGV
[1], value
);
3597 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
3599 struct target
*target
= get_current_target(CMD_CTX
);
3601 switch (CMD_NAME
[2]) {
3615 return ERROR_COMMAND_SYNTAX_ERROR
;
3618 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
3621 static COMMAND_HELPER(parse_load_image_command
, struct image
*image
,
3622 target_addr_t
*min_address
, target_addr_t
*max_address
)
3624 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
3625 return ERROR_COMMAND_SYNTAX_ERROR
;
3627 /* a base address isn't always necessary,
3628 * default to 0x0 (i.e. don't relocate) */
3629 if (CMD_ARGC
>= 2) {
3631 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3632 image
->base_address
= addr
;
3633 image
->base_address_set
= true;
3635 image
->base_address_set
= false;
3637 image
->start_address_set
= false;
3640 COMMAND_PARSE_ADDRESS(CMD_ARGV
[3], *min_address
);
3641 if (CMD_ARGC
== 5) {
3642 COMMAND_PARSE_ADDRESS(CMD_ARGV
[4], *max_address
);
3643 /* use size (given) to find max (required) */
3644 *max_address
+= *min_address
;
3647 if (*min_address
> *max_address
)
3648 return ERROR_COMMAND_SYNTAX_ERROR
;
3653 COMMAND_HANDLER(handle_load_image_command
)
3657 uint32_t image_size
;
3658 target_addr_t min_address
= 0;
3659 target_addr_t max_address
= -1;
3662 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command
,
3663 &image
, &min_address
, &max_address
);
3664 if (retval
!= ERROR_OK
)
3667 struct target
*target
= get_current_target(CMD_CTX
);
3669 struct duration bench
;
3670 duration_start(&bench
);
3672 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
3677 for (unsigned int i
= 0; i
< image
.num_sections
; i
++) {
3678 buffer
= malloc(image
.sections
[i
].size
);
3681 "error allocating buffer for section (%d bytes)",
3682 (int)(image
.sections
[i
].size
));
3683 retval
= ERROR_FAIL
;
3687 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3688 if (retval
!= ERROR_OK
) {
3693 uint32_t offset
= 0;
3694 uint32_t length
= buf_cnt
;
3696 /* DANGER!!! beware of unsigned comparison here!!! */
3698 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3699 (image
.sections
[i
].base_address
< max_address
)) {
3701 if (image
.sections
[i
].base_address
< min_address
) {
3702 /* clip addresses below */
3703 offset
+= min_address
-image
.sections
[i
].base_address
;
3707 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3708 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3710 retval
= target_write_buffer(target
,
3711 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3712 if (retval
!= ERROR_OK
) {
3716 image_size
+= length
;
3717 command_print(CMD
, "%u bytes written at address " TARGET_ADDR_FMT
"",
3718 (unsigned int)length
,
3719 image
.sections
[i
].base_address
+ offset
);
3725 if ((retval
== ERROR_OK
) && (duration_measure(&bench
) == ERROR_OK
)) {
3726 command_print(CMD
, "downloaded %" PRIu32
" bytes "
3727 "in %fs (%0.3f KiB/s)", image_size
,
3728 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3731 image_close(&image
);
3737 COMMAND_HANDLER(handle_dump_image_command
)
3739 struct fileio
*fileio
;
3741 int retval
, retvaltemp
;
3742 target_addr_t address
, size
;
3743 struct duration bench
;
3744 struct target
*target
= get_current_target(CMD_CTX
);
3747 return ERROR_COMMAND_SYNTAX_ERROR
;
3749 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], address
);
3750 COMMAND_PARSE_ADDRESS(CMD_ARGV
[2], size
);
3752 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
3753 buffer
= malloc(buf_size
);
3757 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3758 if (retval
!= ERROR_OK
) {
3763 duration_start(&bench
);
3766 size_t size_written
;
3767 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
3768 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
3769 if (retval
!= ERROR_OK
)
3772 retval
= fileio_write(fileio
, this_run_size
, buffer
, &size_written
);
3773 if (retval
!= ERROR_OK
)
3776 size
-= this_run_size
;
3777 address
+= this_run_size
;
3782 if ((retval
== ERROR_OK
) && (duration_measure(&bench
) == ERROR_OK
)) {
3784 retval
= fileio_size(fileio
, &filesize
);
3785 if (retval
!= ERROR_OK
)
3788 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize
,
3789 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3792 retvaltemp
= fileio_close(fileio
);
3793 if (retvaltemp
!= ERROR_OK
)
3802 IMAGE_CHECKSUM_ONLY
= 2
3805 static COMMAND_HELPER(handle_verify_image_command_internal
, enum verify_mode verify
)
3809 uint32_t image_size
;
3811 uint32_t checksum
= 0;
3812 uint32_t mem_checksum
= 0;
3816 struct target
*target
= get_current_target(CMD_CTX
);
3819 return ERROR_COMMAND_SYNTAX_ERROR
;
3822 LOG_ERROR("no target selected");
3826 struct duration bench
;
3827 duration_start(&bench
);
3829 if (CMD_ARGC
>= 2) {
3831 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3832 image
.base_address
= addr
;
3833 image
.base_address_set
= true;
3835 image
.base_address_set
= false;
3836 image
.base_address
= 0x0;
3839 image
.start_address_set
= false;
3841 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3842 if (retval
!= ERROR_OK
)
3848 for (unsigned int i
= 0; i
< image
.num_sections
; i
++) {
3849 buffer
= malloc(image
.sections
[i
].size
);
3852 "error allocating buffer for section (%" PRIu32
" bytes)",
3853 image
.sections
[i
].size
);
3856 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3857 if (retval
!= ERROR_OK
) {
3862 if (verify
>= IMAGE_VERIFY
) {
3863 /* calculate checksum of image */
3864 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3865 if (retval
!= ERROR_OK
) {
3870 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3871 if (retval
!= ERROR_OK
) {
3875 if ((checksum
!= mem_checksum
) && (verify
== IMAGE_CHECKSUM_ONLY
)) {
3876 LOG_ERROR("checksum mismatch");
3878 retval
= ERROR_FAIL
;
3881 if (checksum
!= mem_checksum
) {
3882 /* failed crc checksum, fall back to a binary compare */
3886 LOG_ERROR("checksum mismatch - attempting binary compare");
3888 data
= malloc(buf_cnt
);
3890 retval
= target_read_buffer(target
, image
.sections
[i
].base_address
, buf_cnt
, data
);
3891 if (retval
== ERROR_OK
) {
3893 for (t
= 0; t
< buf_cnt
; t
++) {
3894 if (data
[t
] != buffer
[t
]) {
3896 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3898 (unsigned)(t
+ image
.sections
[i
].base_address
),
3901 if (diffs
++ >= 127) {
3902 command_print(CMD
, "More than 128 errors, the rest are not printed.");
3914 command_print(CMD
, "address " TARGET_ADDR_FMT
" length 0x%08zx",
3915 image
.sections
[i
].base_address
,
3920 image_size
+= buf_cnt
;
3923 command_print(CMD
, "No more differences found.");
3926 retval
= ERROR_FAIL
;
3927 if ((retval
== ERROR_OK
) && (duration_measure(&bench
) == ERROR_OK
)) {
3928 command_print(CMD
, "verified %" PRIu32
" bytes "
3929 "in %fs (%0.3f KiB/s)", image_size
,
3930 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3933 image_close(&image
);
3938 COMMAND_HANDLER(handle_verify_image_checksum_command
)
3940 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_CHECKSUM_ONLY
);
3943 COMMAND_HANDLER(handle_verify_image_command
)
3945 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_VERIFY
);
3948 COMMAND_HANDLER(handle_test_image_command
)
3950 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_TEST
);
3953 static int handle_bp_command_list(struct command_invocation
*cmd
)
3955 struct target
*target
= get_current_target(cmd
->ctx
);
3956 struct breakpoint
*breakpoint
= target
->breakpoints
;
3957 while (breakpoint
) {
3958 if (breakpoint
->type
== BKPT_SOFT
) {
3959 char *buf
= buf_to_hex_str(breakpoint
->orig_instr
,
3960 breakpoint
->length
);
3961 command_print(cmd
, "IVA breakpoint: " TARGET_ADDR_FMT
", 0x%x, 0x%s",
3962 breakpoint
->address
,
3967 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3968 command_print(cmd
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %u",
3970 breakpoint
->length
, breakpoint
->number
);
3971 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3972 command_print(cmd
, "Hybrid breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %u",
3973 breakpoint
->address
,
3974 breakpoint
->length
, breakpoint
->number
);
3975 command_print(cmd
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3978 command_print(cmd
, "Breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %u",
3979 breakpoint
->address
,
3980 breakpoint
->length
, breakpoint
->number
);
3983 breakpoint
= breakpoint
->next
;
3988 static int handle_bp_command_set(struct command_invocation
*cmd
,
3989 target_addr_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3991 struct target
*target
= get_current_target(cmd
->ctx
);
3995 retval
= breakpoint_add(target
, addr
, length
, hw
);
3996 /* error is always logged in breakpoint_add(), do not print it again */
3997 if (retval
== ERROR_OK
)
3998 command_print(cmd
, "breakpoint set at " TARGET_ADDR_FMT
"", addr
);
4000 } else if (addr
== 0) {
4001 if (!target
->type
->add_context_breakpoint
) {
4002 LOG_ERROR("Context breakpoint not available");
4003 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
4005 retval
= context_breakpoint_add(target
, asid
, length
, hw
);
4006 /* error is always logged in context_breakpoint_add(), do not print it again */
4007 if (retval
== ERROR_OK
)
4008 command_print(cmd
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
4011 if (!target
->type
->add_hybrid_breakpoint
) {
4012 LOG_ERROR("Hybrid breakpoint not available");
4013 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
4015 retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
4016 /* error is always logged in hybrid_breakpoint_add(), do not print it again */
4017 if (retval
== ERROR_OK
)
4018 command_print(cmd
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
4023 COMMAND_HANDLER(handle_bp_command
)
4032 return handle_bp_command_list(CMD
);
4036 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4037 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
4038 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
4041 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
4043 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4044 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
4046 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
4047 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
4049 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
4050 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
4052 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
4057 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4058 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
4059 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
4060 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
4063 return ERROR_COMMAND_SYNTAX_ERROR
;
4067 COMMAND_HANDLER(handle_rbp_command
)
4070 return ERROR_COMMAND_SYNTAX_ERROR
;
4072 struct target
*target
= get_current_target(CMD_CTX
);
4074 if (!strcmp(CMD_ARGV
[0], "all")) {
4075 breakpoint_remove_all(target
);
4078 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4080 breakpoint_remove(target
, addr
);
4086 COMMAND_HANDLER(handle_wp_command
)
4088 struct target
*target
= get_current_target(CMD_CTX
);
4090 if (CMD_ARGC
== 0) {
4091 struct watchpoint
*watchpoint
= target
->watchpoints
;
4093 while (watchpoint
) {
4094 command_print(CMD
, "address: " TARGET_ADDR_FMT
4095 ", len: 0x%8.8" PRIx32
4096 ", r/w/a: %i, value: 0x%8.8" PRIx32
4097 ", mask: 0x%8.8" PRIx32
,
4098 watchpoint
->address
,
4100 (int)watchpoint
->rw
,
4103 watchpoint
= watchpoint
->next
;
4108 enum watchpoint_rw type
= WPT_ACCESS
;
4109 target_addr_t addr
= 0;
4110 uint32_t length
= 0;
4111 uint32_t data_value
= 0x0;
4112 uint32_t data_mask
= 0xffffffff;
4116 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
4119 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
4122 switch (CMD_ARGV
[2][0]) {
4133 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
4134 return ERROR_COMMAND_SYNTAX_ERROR
;
4138 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
4139 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4143 return ERROR_COMMAND_SYNTAX_ERROR
;
4146 int retval
= watchpoint_add(target
, addr
, length
, type
,
4147 data_value
, data_mask
);
4148 if (retval
!= ERROR_OK
)
4149 LOG_ERROR("Failure setting watchpoints");
4154 COMMAND_HANDLER(handle_rwp_command
)
4157 return ERROR_COMMAND_SYNTAX_ERROR
;
4160 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4162 struct target
*target
= get_current_target(CMD_CTX
);
4163 watchpoint_remove(target
, addr
);
4169 * Translate a virtual address to a physical address.
4171 * The low-level target implementation must have logged a detailed error
4172 * which is forwarded to telnet/GDB session.
4174 COMMAND_HANDLER(handle_virt2phys_command
)
4177 return ERROR_COMMAND_SYNTAX_ERROR
;
4180 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], va
);
4183 struct target
*target
= get_current_target(CMD_CTX
);
4184 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
4185 if (retval
== ERROR_OK
)
4186 command_print(CMD
, "Physical address " TARGET_ADDR_FMT
"", pa
);
4191 static void write_data(FILE *f
, const void *data
, size_t len
)
4193 size_t written
= fwrite(data
, 1, len
, f
);
4195 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
4198 static void write_long(FILE *f
, int l
, struct target
*target
)
4202 target_buffer_set_u32(target
, val
, l
);
4203 write_data(f
, val
, 4);
4206 static void write_string(FILE *f
, char *s
)
4208 write_data(f
, s
, strlen(s
));
4211 typedef unsigned char UNIT
[2]; /* unit of profiling */
4213 /* Dump a gmon.out histogram file. */
4214 static void write_gmon(uint32_t *samples
, uint32_t sample_num
, const char *filename
, bool with_range
,
4215 uint32_t start_address
, uint32_t end_address
, struct target
*target
, uint32_t duration_ms
)
4218 FILE *f
= fopen(filename
, "w");
4221 write_string(f
, "gmon");
4222 write_long(f
, 0x00000001, target
); /* Version */
4223 write_long(f
, 0, target
); /* padding */
4224 write_long(f
, 0, target
); /* padding */
4225 write_long(f
, 0, target
); /* padding */
4227 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
4228 write_data(f
, &zero
, 1);
4230 /* figure out bucket size */
4234 min
= start_address
;
4239 for (i
= 0; i
< sample_num
; i
++) {
4240 if (min
> samples
[i
])
4242 if (max
< samples
[i
])
4246 /* max should be (largest sample + 1)
4247 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
4248 if (max
< UINT32_MAX
)
4251 /* gprof requires (max - min) >= 2 */
4252 while ((max
- min
) < 2) {
4253 if (max
< UINT32_MAX
)
4260 uint32_t address_space
= max
- min
;
4262 /* FIXME: What is the reasonable number of buckets?
4263 * The profiling result will be more accurate if there are enough buckets. */
4264 static const uint32_t max_buckets
= 128 * 1024; /* maximum buckets. */
4265 uint32_t num_buckets
= address_space
/ sizeof(UNIT
);
4266 if (num_buckets
> max_buckets
)
4267 num_buckets
= max_buckets
;
4268 int *buckets
= malloc(sizeof(int) * num_buckets
);
4273 memset(buckets
, 0, sizeof(int) * num_buckets
);
4274 for (i
= 0; i
< sample_num
; i
++) {
4275 uint32_t address
= samples
[i
];
4277 if ((address
< min
) || (max
<= address
))
4280 long long a
= address
- min
;
4281 long long b
= num_buckets
;
4282 long long c
= address_space
;
4283 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
4287 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
4288 write_long(f
, min
, target
); /* low_pc */
4289 write_long(f
, max
, target
); /* high_pc */
4290 write_long(f
, num_buckets
, target
); /* # of buckets */
4291 float sample_rate
= sample_num
/ (duration_ms
/ 1000.0);
4292 write_long(f
, sample_rate
, target
);
4293 write_string(f
, "seconds");
4294 for (i
= 0; i
< (15-strlen("seconds")); i
++)
4295 write_data(f
, &zero
, 1);
4296 write_string(f
, "s");
4298 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
4300 char *data
= malloc(2 * num_buckets
);
4302 for (i
= 0; i
< num_buckets
; i
++) {
4307 data
[i
* 2] = val
&0xff;
4308 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
4311 write_data(f
, data
, num_buckets
* 2);
4319 /* profiling samples the CPU PC as quickly as OpenOCD is able,
4320 * which will be used as a random sampling of PC */
4321 COMMAND_HANDLER(handle_profile_command
)
4323 struct target
*target
= get_current_target(CMD_CTX
);
4325 if ((CMD_ARGC
!= 2) && (CMD_ARGC
!= 4))
4326 return ERROR_COMMAND_SYNTAX_ERROR
;
4328 const uint32_t MAX_PROFILE_SAMPLE_NUM
= 10000;
4330 uint32_t num_of_samples
;
4331 int retval
= ERROR_OK
;
4332 bool halted_before_profiling
= target
->state
== TARGET_HALTED
;
4334 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], offset
);
4336 uint32_t start_address
= 0;
4337 uint32_t end_address
= 0;
4338 bool with_range
= false;
4339 if (CMD_ARGC
== 4) {
4341 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], start_address
);
4342 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], end_address
);
4343 if (start_address
> end_address
|| (end_address
- start_address
) < 2) {
4344 command_print(CMD
, "Error: end - start < 2");
4345 return ERROR_COMMAND_ARGUMENT_INVALID
;
4349 uint32_t *samples
= malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM
);
4351 LOG_ERROR("No memory to store samples.");
4355 uint64_t timestart_ms
= timeval_ms();
4357 * Some cores let us sample the PC without the
4358 * annoying halt/resume step; for example, ARMv7 PCSR.
4359 * Provide a way to use that more efficient mechanism.
4361 retval
= target_profiling(target
, samples
, MAX_PROFILE_SAMPLE_NUM
,
4362 &num_of_samples
, offset
);
4363 if (retval
!= ERROR_OK
) {
4367 uint32_t duration_ms
= timeval_ms() - timestart_ms
;
4369 assert(num_of_samples
<= MAX_PROFILE_SAMPLE_NUM
);
4371 retval
= target_poll(target
);
4372 if (retval
!= ERROR_OK
) {
4377 if (target
->state
== TARGET_RUNNING
&& halted_before_profiling
) {
4378 /* The target was halted before we started and is running now. Halt it,
4379 * for consistency. */
4380 retval
= target_halt(target
);
4381 if (retval
!= ERROR_OK
) {
4385 } else if (target
->state
== TARGET_HALTED
&& !halted_before_profiling
) {
4386 /* The target was running before we started and is halted now. Resume
4387 * it, for consistency. */
4388 retval
= target_resume(target
, 1, 0, 0, 0);
4389 if (retval
!= ERROR_OK
) {
4395 retval
= target_poll(target
);
4396 if (retval
!= ERROR_OK
) {
4401 write_gmon(samples
, num_of_samples
, CMD_ARGV
[1],
4402 with_range
, start_address
, end_address
, target
, duration_ms
);
4403 command_print(CMD
, "Wrote %s", CMD_ARGV
[1]);
4409 static int new_u64_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint64_t val
)
4412 Jim_Obj
*obj_name
, *obj_val
;
4415 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4419 obj_name
= Jim_NewStringObj(interp
, namebuf
, -1);
4420 jim_wide wide_val
= val
;
4421 obj_val
= Jim_NewWideObj(interp
, wide_val
);
4422 if (!obj_name
|| !obj_val
) {
4427 Jim_IncrRefCount(obj_name
);
4428 Jim_IncrRefCount(obj_val
);
4429 result
= Jim_SetVariable(interp
, obj_name
, obj_val
);
4430 Jim_DecrRefCount(interp
, obj_name
);
4431 Jim_DecrRefCount(interp
, obj_val
);
4433 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
4437 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
4441 LOG_WARNING("DEPRECATED! use 'read_memory' not 'mem2array'");
4443 /* argv[0] = name of array to receive the data
4444 * argv[1] = desired element width in bits
4445 * argv[2] = memory address
4446 * argv[3] = count of times to read
4447 * argv[4] = optional "phys"
4449 if (argc
< 4 || argc
> 5) {
4450 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4454 /* Arg 0: Name of the array variable */
4455 const char *varname
= Jim_GetString(argv
[0], NULL
);
4457 /* Arg 1: Bit width of one element */
4459 e
= Jim_GetLong(interp
, argv
[1], &l
);
4462 const unsigned int width_bits
= l
;
4464 if (width_bits
!= 8 &&
4468 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4469 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4470 "Invalid width param. Must be one of: 8, 16, 32 or 64.", NULL
);
4473 const unsigned int width
= width_bits
/ 8;
4475 /* Arg 2: Memory address */
4477 e
= Jim_GetWide(interp
, argv
[2], &wide_addr
);
4480 target_addr_t addr
= (target_addr_t
)wide_addr
;
4482 /* Arg 3: Number of elements to read */
4483 e
= Jim_GetLong(interp
, argv
[3], &l
);
4489 bool is_phys
= false;
4492 const char *phys
= Jim_GetString(argv
[4], &str_len
);
4493 if (!strncmp(phys
, "phys", str_len
))
4499 /* Argument checks */
4501 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4502 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
4505 if ((addr
+ (len
* width
)) < addr
) {
4506 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4507 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
4511 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4512 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4513 "mem2array: too large read request, exceeds 64K items", NULL
);
4518 ((width
== 2) && ((addr
& 1) == 0)) ||
4519 ((width
== 4) && ((addr
& 3) == 0)) ||
4520 ((width
== 8) && ((addr
& 7) == 0))) {
4521 /* alignment correct */
4524 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4525 sprintf(buf
, "mem2array address: " TARGET_ADDR_FMT
" is not aligned for %" PRIu32
" byte reads",
4528 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4537 const size_t buffersize
= 4096;
4538 uint8_t *buffer
= malloc(buffersize
);
4545 /* Slurp... in buffer size chunks */
4546 const unsigned int max_chunk_len
= buffersize
/ width
;
4547 const size_t chunk_len
= MIN(len
, max_chunk_len
); /* in elements.. */
4551 retval
= target_read_phys_memory(target
, addr
, width
, chunk_len
, buffer
);
4553 retval
= target_read_memory(target
, addr
, width
, chunk_len
, buffer
);
4554 if (retval
!= ERROR_OK
) {
4556 LOG_ERROR("mem2array: Read @ " TARGET_ADDR_FMT
", w=%u, cnt=%zu, failed",
4560 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4561 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
4565 for (size_t i
= 0; i
< chunk_len
; i
++, idx
++) {
4569 v
= target_buffer_get_u64(target
, &buffer
[i
*width
]);
4572 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
4575 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
4578 v
= buffer
[i
] & 0x0ff;
4581 new_u64_array_element(interp
, varname
, idx
, v
);
4584 addr
+= chunk_len
* width
;
4590 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4595 COMMAND_HANDLER(handle_target_read_memory
)
4598 * CMD_ARGV[0] = memory address
4599 * CMD_ARGV[1] = desired element width in bits
4600 * CMD_ARGV[2] = number of elements to read
4601 * CMD_ARGV[3] = optional "phys"
4604 if (CMD_ARGC
< 3 || CMD_ARGC
> 4)
4605 return ERROR_COMMAND_SYNTAX_ERROR
;
4607 /* Arg 1: Memory address. */
4609 COMMAND_PARSE_NUMBER(u64
, CMD_ARGV
[0], addr
);
4611 /* Arg 2: Bit width of one element. */
4612 unsigned int width_bits
;
4613 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], width_bits
);
4615 /* Arg 3: Number of elements to read. */
4617 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
4619 /* Arg 4: Optional 'phys'. */
4620 bool is_phys
= false;
4621 if (CMD_ARGC
== 4) {
4622 if (strcmp(CMD_ARGV
[3], "phys")) {
4623 command_print(CMD
, "invalid argument '%s', must be 'phys'", CMD_ARGV
[3]);
4624 return ERROR_COMMAND_ARGUMENT_INVALID
;
4630 switch (width_bits
) {
4637 command_print(CMD
, "invalid width, must be 8, 16, 32 or 64");
4638 return ERROR_COMMAND_ARGUMENT_INVALID
;
4641 const unsigned int width
= width_bits
/ 8;
4643 if ((addr
+ (count
* width
)) < addr
) {
4644 command_print(CMD
, "read_memory: addr + count wraps to zero");
4645 return ERROR_COMMAND_ARGUMENT_INVALID
;
4648 if (count
> 65536) {
4649 command_print(CMD
, "read_memory: too large read request, exceeds 64K elements");
4650 return ERROR_COMMAND_ARGUMENT_INVALID
;
4653 struct target
*target
= get_current_target(CMD_CTX
);
4655 const size_t buffersize
= 4096;
4656 uint8_t *buffer
= malloc(buffersize
);
4659 LOG_ERROR("Failed to allocate memory");
4663 char *separator
= "";
4665 const unsigned int max_chunk_len
= buffersize
/ width
;
4666 const size_t chunk_len
= MIN(count
, max_chunk_len
);
4671 retval
= target_read_phys_memory(target
, addr
, width
, chunk_len
, buffer
);
4673 retval
= target_read_memory(target
, addr
, width
, chunk_len
, buffer
);
4675 if (retval
!= ERROR_OK
) {
4676 LOG_DEBUG("read_memory: read at " TARGET_ADDR_FMT
" with width=%u and count=%zu failed",
4677 addr
, width_bits
, chunk_len
);
4679 * FIXME: we append the errmsg to the list of value already read.
4680 * Add a way to flush and replace old output, but LOG_DEBUG() it
4682 command_print(CMD
, "read_memory: failed to read memory");
4687 for (size_t i
= 0; i
< chunk_len
; i
++) {
4692 v
= target_buffer_get_u64(target
, &buffer
[i
* width
]);
4695 v
= target_buffer_get_u32(target
, &buffer
[i
* width
]);
4698 v
= target_buffer_get_u16(target
, &buffer
[i
* width
]);
4705 command_print_sameline(CMD
, "%s0x%" PRIx64
, separator
, v
);
4710 addr
+= chunk_len
* width
;
4718 static int get_u64_array_element(Jim_Interp
*interp
, const char *varname
, size_t idx
, uint64_t *val
)
4720 char *namebuf
= alloc_printf("%s(%zu)", varname
, idx
);
4724 Jim_Obj
*obj_name
= Jim_NewStringObj(interp
, namebuf
, -1);
4730 Jim_IncrRefCount(obj_name
);
4731 Jim_Obj
*obj_val
= Jim_GetVariable(interp
, obj_name
, JIM_ERRMSG
);
4732 Jim_DecrRefCount(interp
, obj_name
);
4738 int result
= Jim_GetWide(interp
, obj_val
, &wide_val
);
4743 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
4744 int argc
, Jim_Obj
*const *argv
)
4748 LOG_WARNING("DEPRECATED! use 'write_memory' not 'array2mem'");
4750 /* argv[0] = name of array from which to read the data
4751 * argv[1] = desired element width in bits
4752 * argv[2] = memory address
4753 * argv[3] = number of elements to write
4754 * argv[4] = optional "phys"
4756 if (argc
< 4 || argc
> 5) {
4757 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4761 /* Arg 0: Name of the array variable */
4762 const char *varname
= Jim_GetString(argv
[0], NULL
);
4764 /* Arg 1: Bit width of one element */
4766 e
= Jim_GetLong(interp
, argv
[1], &l
);
4769 const unsigned int width_bits
= l
;
4771 if (width_bits
!= 8 &&
4775 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4776 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4777 "Invalid width param. Must be one of: 8, 16, 32 or 64.", NULL
);
4780 const unsigned int width
= width_bits
/ 8;
4782 /* Arg 2: Memory address */
4784 e
= Jim_GetWide(interp
, argv
[2], &wide_addr
);
4787 target_addr_t addr
= (target_addr_t
)wide_addr
;
4789 /* Arg 3: Number of elements to write */
4790 e
= Jim_GetLong(interp
, argv
[3], &l
);
4796 bool is_phys
= false;
4799 const char *phys
= Jim_GetString(argv
[4], &str_len
);
4800 if (!strncmp(phys
, "phys", str_len
))
4806 /* Argument checks */
4808 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4809 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4810 "array2mem: zero width read?", NULL
);
4814 if ((addr
+ (len
* width
)) < addr
) {
4815 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4816 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4817 "array2mem: addr + len - wraps to zero?", NULL
);
4822 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4823 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4824 "array2mem: too large memory write request, exceeds 64K items", NULL
);
4829 ((width
== 2) && ((addr
& 1) == 0)) ||
4830 ((width
== 4) && ((addr
& 3) == 0)) ||
4831 ((width
== 8) && ((addr
& 7) == 0))) {
4832 /* alignment correct */
4835 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4836 sprintf(buf
, "array2mem address: " TARGET_ADDR_FMT
" is not aligned for %" PRIu32
" byte reads",
4839 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4848 const size_t buffersize
= 4096;
4849 uint8_t *buffer
= malloc(buffersize
);
4857 /* Slurp... in buffer size chunks */
4858 const unsigned int max_chunk_len
= buffersize
/ width
;
4860 const size_t chunk_len
= MIN(len
, max_chunk_len
); /* in elements.. */
4862 /* Fill the buffer */
4863 for (size_t i
= 0; i
< chunk_len
; i
++, idx
++) {
4865 if (get_u64_array_element(interp
, varname
, idx
, &v
) != JIM_OK
) {
4871 target_buffer_set_u64(target
, &buffer
[i
* width
], v
);
4874 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
4877 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
4880 buffer
[i
] = v
& 0x0ff;
4886 /* Write the buffer to memory */
4889 retval
= target_write_phys_memory(target
, addr
, width
, chunk_len
, buffer
);
4891 retval
= target_write_memory(target
, addr
, width
, chunk_len
, buffer
);
4892 if (retval
!= ERROR_OK
) {
4894 LOG_ERROR("array2mem: Write @ " TARGET_ADDR_FMT
", w=%u, cnt=%zu, failed",
4898 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4899 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
4903 addr
+= chunk_len
* width
;
4908 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4913 static int target_jim_write_memory(Jim_Interp
*interp
, int argc
,
4914 Jim_Obj
* const *argv
)
4917 * argv[1] = memory address
4918 * argv[2] = desired element width in bits
4919 * argv[3] = list of data to write
4920 * argv[4] = optional "phys"
4923 if (argc
< 4 || argc
> 5) {
4924 Jim_WrongNumArgs(interp
, 1, argv
, "address width data ['phys']");
4928 /* Arg 1: Memory address. */
4931 e
= Jim_GetWide(interp
, argv
[1], &wide_addr
);
4936 target_addr_t addr
= (target_addr_t
)wide_addr
;
4938 /* Arg 2: Bit width of one element. */
4940 e
= Jim_GetLong(interp
, argv
[2], &l
);
4945 const unsigned int width_bits
= l
;
4946 size_t count
= Jim_ListLength(interp
, argv
[3]);
4948 /* Arg 4: Optional 'phys'. */
4949 bool is_phys
= false;
4952 const char *phys
= Jim_GetString(argv
[4], NULL
);
4954 if (strcmp(phys
, "phys")) {
4955 Jim_SetResultFormatted(interp
, "invalid argument '%s', must be 'phys'", phys
);
4962 switch (width_bits
) {
4969 Jim_SetResultString(interp
, "invalid width, must be 8, 16, 32 or 64", -1);
4973 const unsigned int width
= width_bits
/ 8;
4975 if ((addr
+ (count
* width
)) < addr
) {
4976 Jim_SetResultString(interp
, "write_memory: addr + len wraps to zero", -1);
4980 if (count
> 65536) {
4981 Jim_SetResultString(interp
, "write_memory: too large memory write request, exceeds 64K elements", -1);
4985 struct command_context
*cmd_ctx
= current_command_context(interp
);
4986 assert(cmd_ctx
!= NULL
);
4987 struct target
*target
= get_current_target(cmd_ctx
);
4989 const size_t buffersize
= 4096;
4990 uint8_t *buffer
= malloc(buffersize
);
4993 LOG_ERROR("Failed to allocate memory");
5000 const unsigned int max_chunk_len
= buffersize
/ width
;
5001 const size_t chunk_len
= MIN(count
, max_chunk_len
);
5003 for (size_t i
= 0; i
< chunk_len
; i
++, j
++) {
5004 Jim_Obj
*tmp
= Jim_ListGetIndex(interp
, argv
[3], j
);
5005 jim_wide element_wide
;
5006 Jim_GetWide(interp
, tmp
, &element_wide
);
5008 const uint64_t v
= element_wide
;
5012 target_buffer_set_u64(target
, &buffer
[i
* width
], v
);
5015 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
5018 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
5021 buffer
[i
] = v
& 0x0ff;
5031 retval
= target_write_phys_memory(target
, addr
, width
, chunk_len
, buffer
);
5033 retval
= target_write_memory(target
, addr
, width
, chunk_len
, buffer
);
5035 if (retval
!= ERROR_OK
) {
5036 LOG_ERROR("write_memory: write at " TARGET_ADDR_FMT
" with width=%u and count=%zu failed",
5037 addr
, width_bits
, chunk_len
);
5038 Jim_SetResultString(interp
, "write_memory: failed to write memory", -1);
5043 addr
+= chunk_len
* width
;
5051 /* FIX? should we propagate errors here rather than printing them
5054 void target_handle_event(struct target
*target
, enum target_event e
)
5056 struct target_event_action
*teap
;
5059 for (teap
= target
->event_action
; teap
; teap
= teap
->next
) {
5060 if (teap
->event
== e
) {
5061 LOG_DEBUG("target(%d): %s (%s) event: %d (%s) action: %s",
5062 target
->target_number
,
5063 target_name(target
),
5064 target_type_name(target
),
5066 target_event_name(e
),
5067 Jim_GetString(teap
->body
, NULL
));
5069 /* Override current target by the target an event
5070 * is issued from (lot of scripts need it).
5071 * Return back to previous override as soon
5072 * as the handler processing is done */
5073 struct command_context
*cmd_ctx
= current_command_context(teap
->interp
);
5074 struct target
*saved_target_override
= cmd_ctx
->current_target_override
;
5075 cmd_ctx
->current_target_override
= target
;
5077 retval
= Jim_EvalObj(teap
->interp
, teap
->body
);
5079 cmd_ctx
->current_target_override
= saved_target_override
;
5081 if (retval
== ERROR_COMMAND_CLOSE_CONNECTION
)
5084 if (retval
== JIM_RETURN
)
5085 retval
= teap
->interp
->returnCode
;
5087 if (retval
!= JIM_OK
) {
5088 Jim_MakeErrorMessage(teap
->interp
);
5089 LOG_USER("Error executing event %s on target %s:\n%s",
5090 target_event_name(e
),
5091 target_name(target
),
5092 Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
5093 /* clean both error code and stacktrace before return */
5094 Jim_Eval(teap
->interp
, "error \"\" \"\"");
5100 static int target_jim_get_reg(Jim_Interp
*interp
, int argc
,
5101 Jim_Obj
* const *argv
)
5106 const char *option
= Jim_GetString(argv
[1], NULL
);
5108 if (!strcmp(option
, "-force")) {
5113 Jim_SetResultFormatted(interp
, "invalid option '%s'", option
);
5119 Jim_WrongNumArgs(interp
, 1, argv
, "[-force] list");
5123 const int length
= Jim_ListLength(interp
, argv
[1]);
5125 Jim_Obj
*result_dict
= Jim_NewDictObj(interp
, NULL
, 0);
5130 struct command_context
*cmd_ctx
= current_command_context(interp
);
5131 assert(cmd_ctx
!= NULL
);
5132 const struct target
*target
= get_current_target(cmd_ctx
);
5134 for (int i
= 0; i
< length
; i
++) {
5135 Jim_Obj
*elem
= Jim_ListGetIndex(interp
, argv
[1], i
);
5140 const char *reg_name
= Jim_String(elem
);
5142 struct reg
*reg
= register_get_by_name(target
->reg_cache
, reg_name
,
5145 if (!reg
|| !reg
->exist
) {
5146 Jim_SetResultFormatted(interp
, "unknown register '%s'", reg_name
);
5151 int retval
= reg
->type
->get(reg
);
5153 if (retval
!= ERROR_OK
) {
5154 Jim_SetResultFormatted(interp
, "failed to read register '%s'",
5160 char *reg_value
= buf_to_hex_str(reg
->value
, reg
->size
);
5163 LOG_ERROR("Failed to allocate memory");
5167 char *tmp
= alloc_printf("0x%s", reg_value
);
5172 LOG_ERROR("Failed to allocate memory");
5176 Jim_DictAddElement(interp
, result_dict
, elem
,
5177 Jim_NewStringObj(interp
, tmp
, -1));
5182 Jim_SetResult(interp
, result_dict
);
5187 static int target_jim_set_reg(Jim_Interp
*interp
, int argc
,
5188 Jim_Obj
* const *argv
)
5191 Jim_WrongNumArgs(interp
, 1, argv
, "dict");
5196 #if JIM_VERSION >= 80
5197 Jim_Obj
**dict
= Jim_DictPairs(interp
, argv
[1], &tmp
);
5203 int ret
= Jim_DictPairs(interp
, argv
[1], &dict
, &tmp
);
5209 const unsigned int length
= tmp
;
5210 struct command_context
*cmd_ctx
= current_command_context(interp
);
5212 const struct target
*target
= get_current_target(cmd_ctx
);
5214 for (unsigned int i
= 0; i
< length
; i
+= 2) {
5215 const char *reg_name
= Jim_String(dict
[i
]);
5216 const char *reg_value
= Jim_String(dict
[i
+ 1]);
5217 struct reg
*reg
= register_get_by_name(target
->reg_cache
, reg_name
,
5220 if (!reg
|| !reg
->exist
) {
5221 Jim_SetResultFormatted(interp
, "unknown register '%s'", reg_name
);
5225 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
5228 LOG_ERROR("Failed to allocate memory");
5232 str_to_buf(reg_value
, strlen(reg_value
), buf
, reg
->size
, 0);
5233 int retval
= reg
->type
->set(reg
, buf
);
5236 if (retval
!= ERROR_OK
) {
5237 Jim_SetResultFormatted(interp
, "failed to set '%s' to register '%s'",
5238 reg_value
, reg_name
);
5247 * Returns true only if the target has a handler for the specified event.
5249 bool target_has_event_action(struct target
*target
, enum target_event event
)
5251 struct target_event_action
*teap
;
5253 for (teap
= target
->event_action
; teap
; teap
= teap
->next
) {
5254 if (teap
->event
== event
)
5260 enum target_cfg_param
{
5263 TCFG_WORK_AREA_VIRT
,
5264 TCFG_WORK_AREA_PHYS
,
5265 TCFG_WORK_AREA_SIZE
,
5266 TCFG_WORK_AREA_BACKUP
,
5269 TCFG_CHAIN_POSITION
,
5274 TCFG_GDB_MAX_CONNECTIONS
,
5277 static struct jim_nvp nvp_config_opts
[] = {
5278 { .name
= "-type", .value
= TCFG_TYPE
},
5279 { .name
= "-event", .value
= TCFG_EVENT
},
5280 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
5281 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
5282 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
5283 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
5284 { .name
= "-endian", .value
= TCFG_ENDIAN
},
5285 { .name
= "-coreid", .value
= TCFG_COREID
},
5286 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
5287 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
5288 { .name
= "-rtos", .value
= TCFG_RTOS
},
5289 { .name
= "-defer-examine", .value
= TCFG_DEFER_EXAMINE
},
5290 { .name
= "-gdb-port", .value
= TCFG_GDB_PORT
},
5291 { .name
= "-gdb-max-connections", .value
= TCFG_GDB_MAX_CONNECTIONS
},
5292 { .name
= NULL
, .value
= -1 }
5295 static int target_configure(struct jim_getopt_info
*goi
, struct target
*target
)
5302 /* parse config or cget options ... */
5303 while (goi
->argc
> 0) {
5304 Jim_SetEmptyResult(goi
->interp
);
5305 /* jim_getopt_debug(goi); */
5307 if (target
->type
->target_jim_configure
) {
5308 /* target defines a configure function */
5309 /* target gets first dibs on parameters */
5310 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
5319 /* otherwise we 'continue' below */
5321 e
= jim_getopt_nvp(goi
, nvp_config_opts
, &n
);
5323 jim_getopt_nvp_unknown(goi
, nvp_config_opts
, 0);
5329 if (goi
->isconfigure
) {
5330 Jim_SetResultFormatted(goi
->interp
,
5331 "not settable: %s", n
->name
);
5335 if (goi
->argc
!= 0) {
5336 Jim_WrongNumArgs(goi
->interp
,
5337 goi
->argc
, goi
->argv
,
5342 Jim_SetResultString(goi
->interp
,
5343 target_type_name(target
), -1);
5347 if (goi
->argc
== 0) {
5348 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
5352 e
= jim_getopt_nvp(goi
, nvp_target_event
, &n
);
5354 jim_getopt_nvp_unknown(goi
, nvp_target_event
, 1);
5358 if (goi
->isconfigure
) {
5359 if (goi
->argc
!= 1) {
5360 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
5364 if (goi
->argc
!= 0) {
5365 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
5371 struct target_event_action
*teap
;
5373 teap
= target
->event_action
;
5374 /* replace existing? */
5376 if (teap
->event
== (enum target_event
)n
->value
)
5381 if (goi
->isconfigure
) {
5382 /* START_DEPRECATED_TPIU */
5383 if (n
->value
== TARGET_EVENT_TRACE_CONFIG
)
5384 LOG_INFO("DEPRECATED target event %s; use TPIU events {pre,post}-{enable,disable}", n
->name
);
5385 /* END_DEPRECATED_TPIU */
5387 bool replace
= true;
5390 teap
= calloc(1, sizeof(*teap
));
5393 teap
->event
= n
->value
;
5394 teap
->interp
= goi
->interp
;
5395 jim_getopt_obj(goi
, &o
);
5397 Jim_DecrRefCount(teap
->interp
, teap
->body
);
5398 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
5401 * Tcl/TK - "tk events" have a nice feature.
5402 * See the "BIND" command.
5403 * We should support that here.
5404 * You can specify %X and %Y in the event code.
5405 * The idea is: %T - target name.
5406 * The idea is: %N - target number
5407 * The idea is: %E - event name.
5409 Jim_IncrRefCount(teap
->body
);
5412 /* add to head of event list */
5413 teap
->next
= target
->event_action
;
5414 target
->event_action
= teap
;
5416 Jim_SetEmptyResult(goi
->interp
);
5420 Jim_SetEmptyResult(goi
->interp
);
5422 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
5428 case TCFG_WORK_AREA_VIRT
:
5429 if (goi
->isconfigure
) {
5430 target_free_all_working_areas(target
);
5431 e
= jim_getopt_wide(goi
, &w
);
5434 target
->working_area_virt
= w
;
5435 target
->working_area_virt_spec
= true;
5440 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
5444 case TCFG_WORK_AREA_PHYS
:
5445 if (goi
->isconfigure
) {
5446 target_free_all_working_areas(target
);
5447 e
= jim_getopt_wide(goi
, &w
);
5450 target
->working_area_phys
= w
;
5451 target
->working_area_phys_spec
= true;
5456 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
5460 case TCFG_WORK_AREA_SIZE
:
5461 if (goi
->isconfigure
) {
5462 target_free_all_working_areas(target
);
5463 e
= jim_getopt_wide(goi
, &w
);
5466 target
->working_area_size
= w
;
5471 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
5475 case TCFG_WORK_AREA_BACKUP
:
5476 if (goi
->isconfigure
) {
5477 target_free_all_working_areas(target
);
5478 e
= jim_getopt_wide(goi
, &w
);
5481 /* make this exactly 1 or 0 */
5482 target
->backup_working_area
= (!!w
);
5487 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
5488 /* loop for more e*/
5493 if (goi
->isconfigure
) {
5494 e
= jim_getopt_nvp(goi
, nvp_target_endian
, &n
);
5496 jim_getopt_nvp_unknown(goi
, nvp_target_endian
, 1);
5499 target
->endianness
= n
->value
;
5504 n
= jim_nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
5506 target
->endianness
= TARGET_LITTLE_ENDIAN
;
5507 n
= jim_nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
5509 Jim_SetResultString(goi
->interp
, n
->name
, -1);
5514 if (goi
->isconfigure
) {
5515 e
= jim_getopt_wide(goi
, &w
);
5518 target
->coreid
= (int32_t)w
;
5523 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->coreid
));
5527 case TCFG_CHAIN_POSITION
:
5528 if (goi
->isconfigure
) {
5530 struct jtag_tap
*tap
;
5532 if (target
->has_dap
) {
5533 Jim_SetResultString(goi
->interp
,
5534 "target requires -dap parameter instead of -chain-position!", -1);
5538 target_free_all_working_areas(target
);
5539 e
= jim_getopt_obj(goi
, &o_t
);
5542 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
5546 target
->tap_configured
= true;
5551 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
5552 /* loop for more e*/
5555 if (goi
->isconfigure
) {
5556 e
= jim_getopt_wide(goi
, &w
);
5559 target
->dbgbase
= (uint32_t)w
;
5560 target
->dbgbase_set
= true;
5565 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
5571 int result
= rtos_create(goi
, target
);
5572 if (result
!= JIM_OK
)
5578 case TCFG_DEFER_EXAMINE
:
5580 target
->defer_examine
= true;
5585 if (goi
->isconfigure
) {
5586 struct command_context
*cmd_ctx
= current_command_context(goi
->interp
);
5587 if (cmd_ctx
->mode
!= COMMAND_CONFIG
) {
5588 Jim_SetResultString(goi
->interp
, "-gdb-port must be configured before 'init'", -1);
5593 e
= jim_getopt_string(goi
, &s
, NULL
);
5596 free(target
->gdb_port_override
);
5597 target
->gdb_port_override
= strdup(s
);
5602 Jim_SetResultString(goi
->interp
, target
->gdb_port_override
? target
->gdb_port_override
: "undefined", -1);
5606 case TCFG_GDB_MAX_CONNECTIONS
:
5607 if (goi
->isconfigure
) {
5608 struct command_context
*cmd_ctx
= current_command_context(goi
->interp
);
5609 if (cmd_ctx
->mode
!= COMMAND_CONFIG
) {
5610 Jim_SetResultString(goi
->interp
, "-gdb-max-connections must be configured before 'init'", -1);
5614 e
= jim_getopt_wide(goi
, &w
);
5617 target
->gdb_max_connections
= (w
< 0) ? CONNECTION_LIMIT_UNLIMITED
: (int)w
;
5622 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->gdb_max_connections
));
5625 } /* while (goi->argc) */
5628 /* done - we return */
5632 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5634 struct command
*c
= jim_to_command(interp
);
5635 struct jim_getopt_info goi
;
5637 jim_getopt_setup(&goi
, interp
, argc
- 1, argv
+ 1);
5638 goi
.isconfigure
= !strcmp(c
->name
, "configure");
5640 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5641 "missing: -option ...");
5644 struct command_context
*cmd_ctx
= current_command_context(interp
);
5646 struct target
*target
= get_current_target(cmd_ctx
);
5647 return target_configure(&goi
, target
);
5650 static int jim_target_mem2array(Jim_Interp
*interp
,
5651 int argc
, Jim_Obj
*const *argv
)
5653 struct command_context
*cmd_ctx
= current_command_context(interp
);
5655 struct target
*target
= get_current_target(cmd_ctx
);
5656 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
5659 static int jim_target_array2mem(Jim_Interp
*interp
,
5660 int argc
, Jim_Obj
*const *argv
)
5662 struct command_context
*cmd_ctx
= current_command_context(interp
);
5664 struct target
*target
= get_current_target(cmd_ctx
);
5665 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
5668 static int jim_target_tap_disabled(Jim_Interp
*interp
)
5670 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
5674 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5676 bool allow_defer
= false;
5678 struct jim_getopt_info goi
;
5679 jim_getopt_setup(&goi
, interp
, argc
- 1, argv
+ 1);
5681 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5682 Jim_SetResultFormatted(goi
.interp
,
5683 "usage: %s ['allow-defer']", cmd_name
);
5687 strcmp(Jim_GetString(argv
[1], NULL
), "allow-defer") == 0) {
5690 int e
= jim_getopt_obj(&goi
, &obj
);
5696 struct command_context
*cmd_ctx
= current_command_context(interp
);
5698 struct target
*target
= get_current_target(cmd_ctx
);
5699 if (!target
->tap
->enabled
)
5700 return jim_target_tap_disabled(interp
);
5702 if (allow_defer
&& target
->defer_examine
) {
5703 LOG_INFO("Deferring arp_examine of %s", target_name(target
));
5704 LOG_INFO("Use arp_examine command to examine it manually!");
5708 int e
= target
->type
->examine(target
);
5709 if (e
!= ERROR_OK
) {
5710 target_reset_examined(target
);
5714 target_set_examined(target
);
5719 COMMAND_HANDLER(handle_target_was_examined
)
5722 return ERROR_COMMAND_SYNTAX_ERROR
;
5724 struct target
*target
= get_current_target(CMD_CTX
);
5726 command_print(CMD
, "%d", target_was_examined(target
) ? 1 : 0);
5731 COMMAND_HANDLER(handle_target_examine_deferred
)
5734 return ERROR_COMMAND_SYNTAX_ERROR
;
5736 struct target
*target
= get_current_target(CMD_CTX
);
5738 command_print(CMD
, "%d", target
->defer_examine
? 1 : 0);
5743 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5746 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5749 struct command_context
*cmd_ctx
= current_command_context(interp
);
5751 struct target
*target
= get_current_target(cmd_ctx
);
5753 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
5759 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5762 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5765 struct command_context
*cmd_ctx
= current_command_context(interp
);
5767 struct target
*target
= get_current_target(cmd_ctx
);
5768 if (!target
->tap
->enabled
)
5769 return jim_target_tap_disabled(interp
);
5772 if (!(target_was_examined(target
)))
5773 e
= ERROR_TARGET_NOT_EXAMINED
;
5775 e
= target
->type
->poll(target
);
5781 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5783 struct jim_getopt_info goi
;
5784 jim_getopt_setup(&goi
, interp
, argc
- 1, argv
+ 1);
5786 if (goi
.argc
!= 2) {
5787 Jim_WrongNumArgs(interp
, 0, argv
,
5788 "([tT]|[fF]|assert|deassert) BOOL");
5793 int e
= jim_getopt_nvp(&goi
, nvp_assert
, &n
);
5795 jim_getopt_nvp_unknown(&goi
, nvp_assert
, 1);
5798 /* the halt or not param */
5800 e
= jim_getopt_wide(&goi
, &a
);
5804 struct command_context
*cmd_ctx
= current_command_context(interp
);
5806 struct target
*target
= get_current_target(cmd_ctx
);
5807 if (!target
->tap
->enabled
)
5808 return jim_target_tap_disabled(interp
);
5810 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
5811 Jim_SetResultFormatted(interp
,
5812 "No target-specific reset for %s",
5813 target_name(target
));
5817 if (target
->defer_examine
)
5818 target_reset_examined(target
);
5820 /* determine if we should halt or not. */
5821 target
->reset_halt
= (a
!= 0);
5822 /* When this happens - all workareas are invalid. */
5823 target_free_all_working_areas_restore(target
, 0);
5826 if (n
->value
== NVP_ASSERT
)
5827 e
= target
->type
->assert_reset(target
);
5829 e
= target
->type
->deassert_reset(target
);
5830 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5833 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5836 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5839 struct command_context
*cmd_ctx
= current_command_context(interp
);
5841 struct target
*target
= get_current_target(cmd_ctx
);
5842 if (!target
->tap
->enabled
)
5843 return jim_target_tap_disabled(interp
);
5844 int e
= target
->type
->halt(target
);
5845 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5848 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5850 struct jim_getopt_info goi
;
5851 jim_getopt_setup(&goi
, interp
, argc
- 1, argv
+ 1);
5853 /* params: <name> statename timeoutmsecs */
5854 if (goi
.argc
!= 2) {
5855 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5856 Jim_SetResultFormatted(goi
.interp
,
5857 "%s <state_name> <timeout_in_msec>", cmd_name
);
5862 int e
= jim_getopt_nvp(&goi
, nvp_target_state
, &n
);
5864 jim_getopt_nvp_unknown(&goi
, nvp_target_state
, 1);
5868 e
= jim_getopt_wide(&goi
, &a
);
5871 struct command_context
*cmd_ctx
= current_command_context(interp
);
5873 struct target
*target
= get_current_target(cmd_ctx
);
5874 if (!target
->tap
->enabled
)
5875 return jim_target_tap_disabled(interp
);
5877 e
= target_wait_state(target
, n
->value
, a
);
5878 if (e
!= ERROR_OK
) {
5879 Jim_Obj
*obj
= Jim_NewIntObj(interp
, e
);
5880 Jim_SetResultFormatted(goi
.interp
,
5881 "target: %s wait %s fails (%#s) %s",
5882 target_name(target
), n
->name
,
5883 obj
, target_strerror_safe(e
));
5888 /* List for human, Events defined for this target.
5889 * scripts/programs should use 'name cget -event NAME'
5891 COMMAND_HANDLER(handle_target_event_list
)
5893 struct target
*target
= get_current_target(CMD_CTX
);
5894 struct target_event_action
*teap
= target
->event_action
;
5896 command_print(CMD
, "Event actions for target (%d) %s\n",
5897 target
->target_number
,
5898 target_name(target
));
5899 command_print(CMD
, "%-25s | Body", "Event");
5900 command_print(CMD
, "------------------------- | "
5901 "----------------------------------------");
5903 command_print(CMD
, "%-25s | %s",
5904 target_event_name(teap
->event
),
5905 Jim_GetString(teap
->body
, NULL
));
5908 command_print(CMD
, "***END***");
5912 COMMAND_HANDLER(handle_target_current_state
)
5915 return ERROR_COMMAND_SYNTAX_ERROR
;
5917 struct target
*target
= get_current_target(CMD_CTX
);
5919 command_print(CMD
, "%s", target_state_name(target
));
5924 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5926 struct jim_getopt_info goi
;
5927 jim_getopt_setup(&goi
, interp
, argc
- 1, argv
+ 1);
5928 if (goi
.argc
!= 1) {
5929 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5930 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
5934 int e
= jim_getopt_nvp(&goi
, nvp_target_event
, &n
);
5936 jim_getopt_nvp_unknown(&goi
, nvp_target_event
, 1);
5939 struct command_context
*cmd_ctx
= current_command_context(interp
);
5941 struct target
*target
= get_current_target(cmd_ctx
);
5942 target_handle_event(target
, n
->value
);
5946 static const struct command_registration target_instance_command_handlers
[] = {
5948 .name
= "configure",
5949 .mode
= COMMAND_ANY
,
5950 .jim_handler
= jim_target_configure
,
5951 .help
= "configure a new target for use",
5952 .usage
= "[target_attribute ...]",
5956 .mode
= COMMAND_ANY
,
5957 .jim_handler
= jim_target_configure
,
5958 .help
= "returns the specified target attribute",
5959 .usage
= "target_attribute",
5963 .handler
= handle_mw_command
,
5964 .mode
= COMMAND_EXEC
,
5965 .help
= "Write 64-bit word(s) to target memory",
5966 .usage
= "address data [count]",
5970 .handler
= handle_mw_command
,
5971 .mode
= COMMAND_EXEC
,
5972 .help
= "Write 32-bit word(s) to target memory",
5973 .usage
= "address data [count]",
5977 .handler
= handle_mw_command
,
5978 .mode
= COMMAND_EXEC
,
5979 .help
= "Write 16-bit half-word(s) to target memory",
5980 .usage
= "address data [count]",
5984 .handler
= handle_mw_command
,
5985 .mode
= COMMAND_EXEC
,
5986 .help
= "Write byte(s) to target memory",
5987 .usage
= "address data [count]",
5991 .handler
= handle_md_command
,
5992 .mode
= COMMAND_EXEC
,
5993 .help
= "Display target memory as 64-bit words",
5994 .usage
= "address [count]",
5998 .handler
= handle_md_command
,
5999 .mode
= COMMAND_EXEC
,
6000 .help
= "Display target memory as 32-bit words",
6001 .usage
= "address [count]",
6005 .handler
= handle_md_command
,
6006 .mode
= COMMAND_EXEC
,
6007 .help
= "Display target memory as 16-bit half-words",
6008 .usage
= "address [count]",
6012 .handler
= handle_md_command
,
6013 .mode
= COMMAND_EXEC
,
6014 .help
= "Display target memory as 8-bit bytes",
6015 .usage
= "address [count]",
6018 .name
= "array2mem",
6019 .mode
= COMMAND_EXEC
,
6020 .jim_handler
= jim_target_array2mem
,
6021 .help
= "Writes Tcl array of 8/16/32 bit numbers "
6023 .usage
= "arrayname bitwidth address count",
6026 .name
= "mem2array",
6027 .mode
= COMMAND_EXEC
,
6028 .jim_handler
= jim_target_mem2array
,
6029 .help
= "Loads Tcl array of 8/16/32 bit numbers "
6030 "from target memory",
6031 .usage
= "arrayname bitwidth address count",
6035 .mode
= COMMAND_EXEC
,
6036 .jim_handler
= target_jim_get_reg
,
6037 .help
= "Get register values from the target",
6042 .mode
= COMMAND_EXEC
,
6043 .jim_handler
= target_jim_set_reg
,
6044 .help
= "Set target register values",
6048 .name
= "read_memory",
6049 .mode
= COMMAND_EXEC
,
6050 .handler
= handle_target_read_memory
,
6051 .help
= "Read Tcl list of 8/16/32/64 bit numbers from target memory",
6052 .usage
= "address width count ['phys']",
6055 .name
= "write_memory",
6056 .mode
= COMMAND_EXEC
,
6057 .jim_handler
= target_jim_write_memory
,
6058 .help
= "Write Tcl list of 8/16/32/64 bit numbers to target memory",
6059 .usage
= "address width data ['phys']",
6062 .name
= "eventlist",
6063 .handler
= handle_target_event_list
,
6064 .mode
= COMMAND_EXEC
,
6065 .help
= "displays a table of events defined for this target",
6070 .mode
= COMMAND_EXEC
,
6071 .handler
= handle_target_current_state
,
6072 .help
= "displays the current state of this target",
6076 .name
= "arp_examine",
6077 .mode
= COMMAND_EXEC
,
6078 .jim_handler
= jim_target_examine
,
6079 .help
= "used internally for reset processing",
6080 .usage
= "['allow-defer']",
6083 .name
= "was_examined",
6084 .mode
= COMMAND_EXEC
,
6085 .handler
= handle_target_was_examined
,
6086 .help
= "used internally for reset processing",
6090 .name
= "examine_deferred",
6091 .mode
= COMMAND_EXEC
,
6092 .handler
= handle_target_examine_deferred
,
6093 .help
= "used internally for reset processing",
6097 .name
= "arp_halt_gdb",
6098 .mode
= COMMAND_EXEC
,
6099 .jim_handler
= jim_target_halt_gdb
,
6100 .help
= "used internally for reset processing to halt GDB",
6104 .mode
= COMMAND_EXEC
,
6105 .jim_handler
= jim_target_poll
,
6106 .help
= "used internally for reset processing",
6109 .name
= "arp_reset",
6110 .mode
= COMMAND_EXEC
,
6111 .jim_handler
= jim_target_reset
,
6112 .help
= "used internally for reset processing",
6116 .mode
= COMMAND_EXEC
,
6117 .jim_handler
= jim_target_halt
,
6118 .help
= "used internally for reset processing",
6121 .name
= "arp_waitstate",
6122 .mode
= COMMAND_EXEC
,
6123 .jim_handler
= jim_target_wait_state
,
6124 .help
= "used internally for reset processing",
6127 .name
= "invoke-event",
6128 .mode
= COMMAND_EXEC
,
6129 .jim_handler
= jim_target_invoke_event
,
6130 .help
= "invoke handler for specified event",
6131 .usage
= "event_name",
6133 COMMAND_REGISTRATION_DONE
6136 static int target_create(struct jim_getopt_info
*goi
)
6143 struct target
*target
;
6144 struct command_context
*cmd_ctx
;
6146 cmd_ctx
= current_command_context(goi
->interp
);
6149 if (goi
->argc
< 3) {
6150 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
6155 jim_getopt_obj(goi
, &new_cmd
);
6156 /* does this command exist? */
6157 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_NONE
);
6159 cp
= Jim_GetString(new_cmd
, NULL
);
6160 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
6165 e
= jim_getopt_string(goi
, &cp
, NULL
);
6168 struct transport
*tr
= get_current_transport();
6169 if (tr
->override_target
) {
6170 e
= tr
->override_target(&cp
);
6171 if (e
!= ERROR_OK
) {
6172 LOG_ERROR("The selected transport doesn't support this target");
6175 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
6177 /* now does target type exist */
6178 for (x
= 0 ; target_types
[x
] ; x
++) {
6179 if (strcmp(cp
, target_types
[x
]->name
) == 0) {
6184 if (!target_types
[x
]) {
6185 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
6186 for (x
= 0 ; target_types
[x
] ; x
++) {
6187 if (target_types
[x
+ 1]) {
6188 Jim_AppendStrings(goi
->interp
,
6189 Jim_GetResult(goi
->interp
),
6190 target_types
[x
]->name
,
6193 Jim_AppendStrings(goi
->interp
,
6194 Jim_GetResult(goi
->interp
),
6196 target_types
[x
]->name
, NULL
);
6203 target
= calloc(1, sizeof(struct target
));
6205 LOG_ERROR("Out of memory");
6209 /* set empty smp cluster */
6210 target
->smp_targets
= &empty_smp_targets
;
6212 /* set target number */
6213 target
->target_number
= new_target_number();
6215 /* allocate memory for each unique target type */
6216 target
->type
= malloc(sizeof(struct target_type
));
6217 if (!target
->type
) {
6218 LOG_ERROR("Out of memory");
6223 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
6225 /* default to first core, override with -coreid */
6228 target
->working_area
= 0x0;
6229 target
->working_area_size
= 0x0;
6230 target
->working_areas
= NULL
;
6231 target
->backup_working_area
= 0;
6233 target
->state
= TARGET_UNKNOWN
;
6234 target
->debug_reason
= DBG_REASON_UNDEFINED
;
6235 target
->reg_cache
= NULL
;
6236 target
->breakpoints
= NULL
;
6237 target
->watchpoints
= NULL
;
6238 target
->next
= NULL
;
6239 target
->arch_info
= NULL
;
6241 target
->verbose_halt_msg
= true;
6243 target
->halt_issued
= false;
6245 /* initialize trace information */
6246 target
->trace_info
= calloc(1, sizeof(struct trace
));
6247 if (!target
->trace_info
) {
6248 LOG_ERROR("Out of memory");
6254 target
->dbgmsg
= NULL
;
6255 target
->dbg_msg_enabled
= 0;
6257 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
6259 target
->rtos
= NULL
;
6260 target
->rtos_auto_detect
= false;
6262 target
->gdb_port_override
= NULL
;
6263 target
->gdb_max_connections
= 1;
6265 /* Do the rest as "configure" options */
6266 goi
->isconfigure
= 1;
6267 e
= target_configure(goi
, target
);
6270 if (target
->has_dap
) {
6271 if (!target
->dap_configured
) {
6272 Jim_SetResultString(goi
->interp
, "-dap ?name? required when creating target", -1);
6276 if (!target
->tap_configured
) {
6277 Jim_SetResultString(goi
->interp
, "-chain-position ?name? required when creating target", -1);
6281 /* tap must be set after target was configured */
6287 rtos_destroy(target
);
6288 free(target
->gdb_port_override
);
6289 free(target
->trace_info
);
6295 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
6296 /* default endian to little if not specified */
6297 target
->endianness
= TARGET_LITTLE_ENDIAN
;
6300 cp
= Jim_GetString(new_cmd
, NULL
);
6301 target
->cmd_name
= strdup(cp
);
6302 if (!target
->cmd_name
) {
6303 LOG_ERROR("Out of memory");
6304 rtos_destroy(target
);
6305 free(target
->gdb_port_override
);
6306 free(target
->trace_info
);
6312 if (target
->type
->target_create
) {
6313 e
= (*(target
->type
->target_create
))(target
, goi
->interp
);
6314 if (e
!= ERROR_OK
) {
6315 LOG_DEBUG("target_create failed");
6316 free(target
->cmd_name
);
6317 rtos_destroy(target
);
6318 free(target
->gdb_port_override
);
6319 free(target
->trace_info
);
6326 /* create the target specific commands */
6327 if (target
->type
->commands
) {
6328 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
6330 LOG_ERROR("unable to register '%s' commands", cp
);
6333 /* now - create the new target name command */
6334 const struct command_registration target_subcommands
[] = {
6336 .chain
= target_instance_command_handlers
,
6339 .chain
= target
->type
->commands
,
6341 COMMAND_REGISTRATION_DONE
6343 const struct command_registration target_commands
[] = {
6346 .mode
= COMMAND_ANY
,
6347 .help
= "target command group",
6349 .chain
= target_subcommands
,
6351 COMMAND_REGISTRATION_DONE
6353 e
= register_commands_override_target(cmd_ctx
, NULL
, target_commands
, target
);
6354 if (e
!= ERROR_OK
) {
6355 if (target
->type
->deinit_target
)
6356 target
->type
->deinit_target(target
);
6357 free(target
->cmd_name
);
6358 rtos_destroy(target
);
6359 free(target
->gdb_port_override
);
6360 free(target
->trace_info
);
6366 /* append to end of list */
6367 append_to_list_all_targets(target
);
6369 cmd_ctx
->current_target
= target
;
6373 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
6376 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
6379 struct command_context
*cmd_ctx
= current_command_context(interp
);
6382 struct target
*target
= get_current_target_or_null(cmd_ctx
);
6384 Jim_SetResultString(interp
, target_name(target
), -1);
6388 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
6391 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
6394 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
6395 for (unsigned x
= 0; target_types
[x
]; x
++) {
6396 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
6397 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
6402 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
6405 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
6408 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
6409 struct target
*target
= all_targets
;
6411 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
6412 Jim_NewStringObj(interp
, target_name(target
), -1));
6413 target
= target
->next
;
6418 static struct target_list
*
6419 __attribute__((warn_unused_result
))
6420 create_target_list_node(Jim_Obj
*const name
) {
6422 const char *targetname
= Jim_GetString(name
, &len
);
6423 struct target
*target
= get_target(targetname
);
6424 LOG_DEBUG("%s ", targetname
);
6428 struct target_list
*new = malloc(sizeof(struct target_list
));
6430 LOG_ERROR("Out of memory");
6434 new->target
= target
;
6438 static int get_target_with_common_rtos_type(struct list_head
*lh
, struct target
**result
)
6440 struct target
*target
= NULL
;
6441 struct target_list
*curr
;
6442 foreach_smp_target(curr
, lh
) {
6443 struct rtos
*curr_rtos
= curr
->target
->rtos
;
6445 if (target
&& target
->rtos
&& target
->rtos
->type
!= curr_rtos
->type
) {
6446 LOG_ERROR("Different rtos types in members of one smp target!");
6449 target
= curr
->target
;
6456 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
6458 static int smp_group
= 1;
6461 LOG_DEBUG("Empty SMP target");
6464 LOG_DEBUG("%d", argc
);
6465 /* argv[1] = target to associate in smp
6466 * argv[2] = target to associate in smp
6470 struct list_head
*lh
= malloc(sizeof(*lh
));
6472 LOG_ERROR("Out of memory");
6477 for (int i
= 1; i
< argc
; i
++) {
6478 struct target_list
*new = create_target_list_node(argv
[i
]);
6480 list_add_tail(&new->lh
, lh
);
6482 /* now parse the list of cpu and put the target in smp mode*/
6483 struct target_list
*curr
;
6484 foreach_smp_target(curr
, lh
) {
6485 struct target
*target
= curr
->target
;
6486 target
->smp
= smp_group
;
6487 target
->smp_targets
= lh
;
6491 struct target
*rtos_target
;
6492 int retval
= get_target_with_common_rtos_type(lh
, &rtos_target
);
6493 if (retval
== JIM_OK
&& rtos_target
)
6494 retval
= rtos_smp_init(rtos_target
);
6500 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
6502 struct jim_getopt_info goi
;
6503 jim_getopt_setup(&goi
, interp
, argc
- 1, argv
+ 1);
6505 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
6506 "<name> <target_type> [<target_options> ...]");
6509 return target_create(&goi
);
6512 static const struct command_registration target_subcommand_handlers
[] = {
6515 .mode
= COMMAND_CONFIG
,
6516 .handler
= handle_target_init_command
,
6517 .help
= "initialize targets",
6522 .mode
= COMMAND_CONFIG
,
6523 .jim_handler
= jim_target_create
,
6524 .usage
= "name type '-chain-position' name [options ...]",
6525 .help
= "Creates and selects a new target",
6529 .mode
= COMMAND_ANY
,
6530 .jim_handler
= jim_target_current
,
6531 .help
= "Returns the currently selected target",
6535 .mode
= COMMAND_ANY
,
6536 .jim_handler
= jim_target_types
,
6537 .help
= "Returns the available target types as "
6538 "a list of strings",
6542 .mode
= COMMAND_ANY
,
6543 .jim_handler
= jim_target_names
,
6544 .help
= "Returns the names of all targets as a list of strings",
6548 .mode
= COMMAND_ANY
,
6549 .jim_handler
= jim_target_smp
,
6550 .usage
= "targetname1 targetname2 ...",
6551 .help
= "gather several target in a smp list"
6554 COMMAND_REGISTRATION_DONE
6558 target_addr_t address
;
6564 static int fastload_num
;
6565 static struct fast_load
*fastload
;
6567 static void free_fastload(void)
6570 for (int i
= 0; i
< fastload_num
; i
++)
6571 free(fastload
[i
].data
);
6577 COMMAND_HANDLER(handle_fast_load_image_command
)
6581 uint32_t image_size
;
6582 target_addr_t min_address
= 0;
6583 target_addr_t max_address
= -1;
6587 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command
,
6588 &image
, &min_address
, &max_address
);
6589 if (retval
!= ERROR_OK
)
6592 struct duration bench
;
6593 duration_start(&bench
);
6595 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
6596 if (retval
!= ERROR_OK
)
6601 fastload_num
= image
.num_sections
;
6602 fastload
= malloc(sizeof(struct fast_load
)*image
.num_sections
);
6604 command_print(CMD
, "out of memory");
6605 image_close(&image
);
6608 memset(fastload
, 0, sizeof(struct fast_load
)*image
.num_sections
);
6609 for (unsigned int i
= 0; i
< image
.num_sections
; i
++) {
6610 buffer
= malloc(image
.sections
[i
].size
);
6612 command_print(CMD
, "error allocating buffer for section (%d bytes)",
6613 (int)(image
.sections
[i
].size
));
6614 retval
= ERROR_FAIL
;
6618 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
6619 if (retval
!= ERROR_OK
) {
6624 uint32_t offset
= 0;
6625 uint32_t length
= buf_cnt
;
6627 /* DANGER!!! beware of unsigned comparison here!!! */
6629 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
6630 (image
.sections
[i
].base_address
< max_address
)) {
6631 if (image
.sections
[i
].base_address
< min_address
) {
6632 /* clip addresses below */
6633 offset
+= min_address
-image
.sections
[i
].base_address
;
6637 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
6638 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
6640 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
6641 fastload
[i
].data
= malloc(length
);
6642 if (!fastload
[i
].data
) {
6644 command_print(CMD
, "error allocating buffer for section (%" PRIu32
" bytes)",
6646 retval
= ERROR_FAIL
;
6649 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
6650 fastload
[i
].length
= length
;
6652 image_size
+= length
;
6653 command_print(CMD
, "%u bytes written at address 0x%8.8x",
6654 (unsigned int)length
,
6655 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
6661 if ((retval
== ERROR_OK
) && (duration_measure(&bench
) == ERROR_OK
)) {
6662 command_print(CMD
, "Loaded %" PRIu32
" bytes "
6663 "in %fs (%0.3f KiB/s)", image_size
,
6664 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
6667 "WARNING: image has not been loaded to target!"
6668 "You can issue a 'fast_load' to finish loading.");
6671 image_close(&image
);
6673 if (retval
!= ERROR_OK
)
6679 COMMAND_HANDLER(handle_fast_load_command
)
6682 return ERROR_COMMAND_SYNTAX_ERROR
;
6684 LOG_ERROR("No image in memory");
6688 int64_t ms
= timeval_ms();
6690 int retval
= ERROR_OK
;
6691 for (i
= 0; i
< fastload_num
; i
++) {
6692 struct target
*target
= get_current_target(CMD_CTX
);
6693 command_print(CMD
, "Write to 0x%08x, length 0x%08x",
6694 (unsigned int)(fastload
[i
].address
),
6695 (unsigned int)(fastload
[i
].length
));
6696 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
6697 if (retval
!= ERROR_OK
)
6699 size
+= fastload
[i
].length
;
6701 if (retval
== ERROR_OK
) {
6702 int64_t after
= timeval_ms();
6703 command_print(CMD
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
6708 static const struct command_registration target_command_handlers
[] = {
6711 .handler
= handle_targets_command
,
6712 .mode
= COMMAND_ANY
,
6713 .help
= "change current default target (one parameter) "
6714 "or prints table of all targets (no parameters)",
6715 .usage
= "[target]",
6719 .mode
= COMMAND_CONFIG
,
6720 .help
= "configure target",
6721 .chain
= target_subcommand_handlers
,
6724 COMMAND_REGISTRATION_DONE
6727 int target_register_commands(struct command_context
*cmd_ctx
)
6729 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
6732 static bool target_reset_nag
= true;
6734 bool get_target_reset_nag(void)
6736 return target_reset_nag
;
6739 COMMAND_HANDLER(handle_target_reset_nag
)
6741 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
6742 &target_reset_nag
, "Nag after each reset about options to improve "
6746 COMMAND_HANDLER(handle_ps_command
)
6748 struct target
*target
= get_current_target(CMD_CTX
);
6750 if (target
->state
!= TARGET_HALTED
) {
6751 LOG_INFO("target not halted !!");
6755 if ((target
->rtos
) && (target
->rtos
->type
)
6756 && (target
->rtos
->type
->ps_command
)) {
6757 display
= target
->rtos
->type
->ps_command(target
);
6758 command_print(CMD
, "%s", display
);
6763 return ERROR_TARGET_FAILURE
;
6767 static void binprint(struct command_invocation
*cmd
, const char *text
, const uint8_t *buf
, int size
)
6770 command_print_sameline(cmd
, "%s", text
);
6771 for (int i
= 0; i
< size
; i
++)
6772 command_print_sameline(cmd
, " %02x", buf
[i
]);
6773 command_print(cmd
, " ");
6776 COMMAND_HANDLER(handle_test_mem_access_command
)
6778 struct target
*target
= get_current_target(CMD_CTX
);
6780 int retval
= ERROR_OK
;
6782 if (target
->state
!= TARGET_HALTED
) {
6783 LOG_INFO("target not halted !!");
6788 return ERROR_COMMAND_SYNTAX_ERROR
;
6790 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], test_size
);
6793 size_t num_bytes
= test_size
+ 4;
6795 struct working_area
*wa
= NULL
;
6796 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6797 if (retval
!= ERROR_OK
) {
6798 LOG_ERROR("Not enough working area");
6802 uint8_t *test_pattern
= malloc(num_bytes
);
6804 for (size_t i
= 0; i
< num_bytes
; i
++)
6805 test_pattern
[i
] = rand();
6807 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6808 if (retval
!= ERROR_OK
) {
6809 LOG_ERROR("Test pattern write failed");
6813 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6814 for (int size
= 1; size
<= 4; size
*= 2) {
6815 for (int offset
= 0; offset
< 4; offset
++) {
6816 uint32_t count
= test_size
/ size
;
6817 size_t host_bufsiz
= (count
+ 2) * size
+ host_offset
;
6818 uint8_t *read_ref
= malloc(host_bufsiz
);
6819 uint8_t *read_buf
= malloc(host_bufsiz
);
6821 for (size_t i
= 0; i
< host_bufsiz
; i
++) {
6822 read_ref
[i
] = rand();
6823 read_buf
[i
] = read_ref
[i
];
6825 command_print_sameline(CMD
,
6826 "Test read %" PRIu32
" x %d @ %d to %saligned buffer: ", count
,
6827 size
, offset
, host_offset
? "un" : "");
6829 struct duration bench
;
6830 duration_start(&bench
);
6832 retval
= target_read_memory(target
, wa
->address
+ offset
, size
, count
,
6833 read_buf
+ size
+ host_offset
);
6835 duration_measure(&bench
);
6837 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6838 command_print(CMD
, "Unsupported alignment");
6840 } else if (retval
!= ERROR_OK
) {
6841 command_print(CMD
, "Memory read failed");
6845 /* replay on host */
6846 memcpy(read_ref
+ size
+ host_offset
, test_pattern
+ offset
, count
* size
);
6849 int result
= memcmp(read_ref
, read_buf
, host_bufsiz
);
6851 command_print(CMD
, "Pass in %fs (%0.3f KiB/s)",
6852 duration_elapsed(&bench
),
6853 duration_kbps(&bench
, count
* size
));
6855 command_print(CMD
, "Compare failed");
6856 binprint(CMD
, "ref:", read_ref
, host_bufsiz
);
6857 binprint(CMD
, "buf:", read_buf
, host_bufsiz
);
6869 target_free_working_area(target
, wa
);
6872 num_bytes
= test_size
+ 4 + 4 + 4;
6874 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6875 if (retval
!= ERROR_OK
) {
6876 LOG_ERROR("Not enough working area");
6880 test_pattern
= malloc(num_bytes
);
6882 for (size_t i
= 0; i
< num_bytes
; i
++)
6883 test_pattern
[i
] = rand();
6885 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6886 for (int size
= 1; size
<= 4; size
*= 2) {
6887 for (int offset
= 0; offset
< 4; offset
++) {
6888 uint32_t count
= test_size
/ size
;
6889 size_t host_bufsiz
= count
* size
+ host_offset
;
6890 uint8_t *read_ref
= malloc(num_bytes
);
6891 uint8_t *read_buf
= malloc(num_bytes
);
6892 uint8_t *write_buf
= malloc(host_bufsiz
);
6894 for (size_t i
= 0; i
< host_bufsiz
; i
++)
6895 write_buf
[i
] = rand();
6896 command_print_sameline(CMD
,
6897 "Test write %" PRIu32
" x %d @ %d from %saligned buffer: ", count
,
6898 size
, offset
, host_offset
? "un" : "");
6900 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6901 if (retval
!= ERROR_OK
) {
6902 command_print(CMD
, "Test pattern write failed");
6906 /* replay on host */
6907 memcpy(read_ref
, test_pattern
, num_bytes
);
6908 memcpy(read_ref
+ size
+ offset
, write_buf
+ host_offset
, count
* size
);
6910 struct duration bench
;
6911 duration_start(&bench
);
6913 retval
= target_write_memory(target
, wa
->address
+ size
+ offset
, size
, count
,
6914 write_buf
+ host_offset
);
6916 duration_measure(&bench
);
6918 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6919 command_print(CMD
, "Unsupported alignment");
6921 } else if (retval
!= ERROR_OK
) {
6922 command_print(CMD
, "Memory write failed");
6927 retval
= target_read_memory(target
, wa
->address
, 1, num_bytes
, read_buf
);
6928 if (retval
!= ERROR_OK
) {
6929 command_print(CMD
, "Test pattern write failed");
6934 int result
= memcmp(read_ref
, read_buf
, num_bytes
);
6936 command_print(CMD
, "Pass in %fs (%0.3f KiB/s)",
6937 duration_elapsed(&bench
),
6938 duration_kbps(&bench
, count
* size
));
6940 command_print(CMD
, "Compare failed");
6941 binprint(CMD
, "ref:", read_ref
, num_bytes
);
6942 binprint(CMD
, "buf:", read_buf
, num_bytes
);
6953 target_free_working_area(target
, wa
);
6957 static const struct command_registration target_exec_command_handlers
[] = {
6959 .name
= "fast_load_image",
6960 .handler
= handle_fast_load_image_command
,
6961 .mode
= COMMAND_ANY
,
6962 .help
= "Load image into server memory for later use by "
6963 "fast_load; primarily for profiling",
6964 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6965 "[min_address [max_length]]",
6968 .name
= "fast_load",
6969 .handler
= handle_fast_load_command
,
6970 .mode
= COMMAND_EXEC
,
6971 .help
= "loads active fast load image to current target "
6972 "- mainly for profiling purposes",
6977 .handler
= handle_profile_command
,
6978 .mode
= COMMAND_EXEC
,
6979 .usage
= "seconds filename [start end]",
6980 .help
= "profiling samples the CPU PC",
6982 /** @todo don't register virt2phys() unless target supports it */
6984 .name
= "virt2phys",
6985 .handler
= handle_virt2phys_command
,
6986 .mode
= COMMAND_ANY
,
6987 .help
= "translate a virtual address into a physical address",
6988 .usage
= "virtual_address",
6992 .handler
= handle_reg_command
,
6993 .mode
= COMMAND_EXEC
,
6994 .help
= "display (reread from target with \"force\") or set a register; "
6995 "with no arguments, displays all registers and their values",
6996 .usage
= "[(register_number|register_name) [(value|'force')]]",
7000 .handler
= handle_poll_command
,
7001 .mode
= COMMAND_EXEC
,
7002 .help
= "poll target state; or reconfigure background polling",
7003 .usage
= "['on'|'off']",
7006 .name
= "wait_halt",
7007 .handler
= handle_wait_halt_command
,
7008 .mode
= COMMAND_EXEC
,
7009 .help
= "wait up to the specified number of milliseconds "
7010 "(default 5000) for a previously requested halt",
7011 .usage
= "[milliseconds]",
7015 .handler
= handle_halt_command
,
7016 .mode
= COMMAND_EXEC
,
7017 .help
= "request target to halt, then wait up to the specified "
7018 "number of milliseconds (default 5000) for it to complete",
7019 .usage
= "[milliseconds]",
7023 .handler
= handle_resume_command
,
7024 .mode
= COMMAND_EXEC
,
7025 .help
= "resume target execution from current PC or address",
7026 .usage
= "[address]",
7030 .handler
= handle_reset_command
,
7031 .mode
= COMMAND_EXEC
,
7032 .usage
= "[run|halt|init]",
7033 .help
= "Reset all targets into the specified mode. "
7034 "Default reset mode is run, if not given.",
7037 .name
= "soft_reset_halt",
7038 .handler
= handle_soft_reset_halt_command
,
7039 .mode
= COMMAND_EXEC
,
7041 .help
= "halt the target and do a soft reset",
7045 .handler
= handle_step_command
,
7046 .mode
= COMMAND_EXEC
,
7047 .help
= "step one instruction from current PC or address",
7048 .usage
= "[address]",
7052 .handler
= handle_md_command
,
7053 .mode
= COMMAND_EXEC
,
7054 .help
= "display memory double-words",
7055 .usage
= "['phys'] address [count]",
7059 .handler
= handle_md_command
,
7060 .mode
= COMMAND_EXEC
,
7061 .help
= "display memory words",
7062 .usage
= "['phys'] address [count]",
7066 .handler
= handle_md_command
,
7067 .mode
= COMMAND_EXEC
,
7068 .help
= "display memory half-words",
7069 .usage
= "['phys'] address [count]",
7073 .handler
= handle_md_command
,
7074 .mode
= COMMAND_EXEC
,
7075 .help
= "display memory bytes",
7076 .usage
= "['phys'] address [count]",
7080 .handler
= handle_mw_command
,
7081 .mode
= COMMAND_EXEC
,
7082 .help
= "write memory double-word",
7083 .usage
= "['phys'] address value [count]",
7087 .handler
= handle_mw_command
,
7088 .mode
= COMMAND_EXEC
,
7089 .help
= "write memory word",
7090 .usage
= "['phys'] address value [count]",
7094 .handler
= handle_mw_command
,
7095 .mode
= COMMAND_EXEC
,
7096 .help
= "write memory half-word",
7097 .usage
= "['phys'] address value [count]",
7101 .handler
= handle_mw_command
,
7102 .mode
= COMMAND_EXEC
,
7103 .help
= "write memory byte",
7104 .usage
= "['phys'] address value [count]",
7108 .handler
= handle_bp_command
,
7109 .mode
= COMMAND_EXEC
,
7110 .help
= "list or set hardware or software breakpoint",
7111 .usage
= "[<address> [<asid>] <length> ['hw'|'hw_ctx']]",
7115 .handler
= handle_rbp_command
,
7116 .mode
= COMMAND_EXEC
,
7117 .help
= "remove breakpoint",
7118 .usage
= "'all' | address",
7122 .handler
= handle_wp_command
,
7123 .mode
= COMMAND_EXEC
,
7124 .help
= "list (no params) or create watchpoints",
7125 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
7129 .handler
= handle_rwp_command
,
7130 .mode
= COMMAND_EXEC
,
7131 .help
= "remove watchpoint",
7135 .name
= "load_image",
7136 .handler
= handle_load_image_command
,
7137 .mode
= COMMAND_EXEC
,
7138 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
7139 "[min_address] [max_length]",
7142 .name
= "dump_image",
7143 .handler
= handle_dump_image_command
,
7144 .mode
= COMMAND_EXEC
,
7145 .usage
= "filename address size",
7148 .name
= "verify_image_checksum",
7149 .handler
= handle_verify_image_checksum_command
,
7150 .mode
= COMMAND_EXEC
,
7151 .usage
= "filename [offset [type]]",
7154 .name
= "verify_image",
7155 .handler
= handle_verify_image_command
,
7156 .mode
= COMMAND_EXEC
,
7157 .usage
= "filename [offset [type]]",
7160 .name
= "test_image",
7161 .handler
= handle_test_image_command
,
7162 .mode
= COMMAND_EXEC
,
7163 .usage
= "filename [offset [type]]",
7167 .mode
= COMMAND_EXEC
,
7168 .jim_handler
= target_jim_get_reg
,
7169 .help
= "Get register values from the target",
7174 .mode
= COMMAND_EXEC
,
7175 .jim_handler
= target_jim_set_reg
,
7176 .help
= "Set target register values",
7180 .name
= "read_memory",
7181 .mode
= COMMAND_EXEC
,
7182 .handler
= handle_target_read_memory
,
7183 .help
= "Read Tcl list of 8/16/32/64 bit numbers from target memory",
7184 .usage
= "address width count ['phys']",
7187 .name
= "write_memory",
7188 .mode
= COMMAND_EXEC
,
7189 .jim_handler
= target_jim_write_memory
,
7190 .help
= "Write Tcl list of 8/16/32/64 bit numbers to target memory",
7191 .usage
= "address width data ['phys']",
7194 .name
= "reset_nag",
7195 .handler
= handle_target_reset_nag
,
7196 .mode
= COMMAND_ANY
,
7197 .help
= "Nag after each reset about options that could have been "
7198 "enabled to improve performance.",
7199 .usage
= "['enable'|'disable']",
7203 .handler
= handle_ps_command
,
7204 .mode
= COMMAND_EXEC
,
7205 .help
= "list all tasks",
7209 .name
= "test_mem_access",
7210 .handler
= handle_test_mem_access_command
,
7211 .mode
= COMMAND_EXEC
,
7212 .help
= "Test the target's memory access functions",
7216 COMMAND_REGISTRATION_DONE
7218 static int target_register_user_commands(struct command_context
*cmd_ctx
)
7220 int retval
= ERROR_OK
;
7221 retval
= target_request_register_commands(cmd_ctx
);
7222 if (retval
!= ERROR_OK
)
7225 retval
= trace_register_commands(cmd_ctx
);
7226 if (retval
!= ERROR_OK
)
7230 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);