1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
17 * Copyright (C) 2011 by Broadcom Corporation *
18 * Evan Hunter - ehunter@broadcom.com *
20 * Copyright (C) ST-Ericsson SA 2011 *
21 * michel.jaouen@stericsson.com : smp minimum support *
23 * Copyright (C) 2011 Andreas Fritiofson *
24 * andreas.fritiofson@gmail.com *
26 * This program is free software; you can redistribute it and/or modify *
27 * it under the terms of the GNU General Public License as published by *
28 * the Free Software Foundation; either version 2 of the License, or *
29 * (at your option) any later version. *
31 * This program is distributed in the hope that it will be useful, *
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
34 * GNU General Public License for more details. *
36 * You should have received a copy of the GNU General Public License *
37 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
38 ***************************************************************************/
44 #include <helper/align.h>
45 #include <helper/time_support.h>
46 #include <jtag/jtag.h>
47 #include <flash/nor/core.h>
50 #include "target_type.h"
51 #include "target_request.h"
52 #include "breakpoints.h"
56 #include "rtos/rtos.h"
57 #include "transport/transport.h"
60 #include "semihosting_common.h"
62 /* default halt wait timeout (ms) */
63 #define DEFAULT_HALT_TIMEOUT 5000
65 static int target_read_buffer_default(struct target
*target
, target_addr_t address
,
66 uint32_t count
, uint8_t *buffer
);
67 static int target_write_buffer_default(struct target
*target
, target_addr_t address
,
68 uint32_t count
, const uint8_t *buffer
);
69 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
70 int argc
, Jim_Obj
* const *argv
);
71 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
72 int argc
, Jim_Obj
* const *argv
);
73 static int target_register_user_commands(struct command_context
*cmd_ctx
);
74 static int target_get_gdb_fileio_info_default(struct target
*target
,
75 struct gdb_fileio_info
*fileio_info
);
76 static int target_gdb_fileio_end_default(struct target
*target
, int retcode
,
77 int fileio_errno
, bool ctrl_c
);
80 extern struct target_type arm7tdmi_target
;
81 extern struct target_type arm720t_target
;
82 extern struct target_type arm9tdmi_target
;
83 extern struct target_type arm920t_target
;
84 extern struct target_type arm966e_target
;
85 extern struct target_type arm946e_target
;
86 extern struct target_type arm926ejs_target
;
87 extern struct target_type fa526_target
;
88 extern struct target_type feroceon_target
;
89 extern struct target_type dragonite_target
;
90 extern struct target_type xscale_target
;
91 extern struct target_type cortexm_target
;
92 extern struct target_type cortexa_target
;
93 extern struct target_type aarch64_target
;
94 extern struct target_type cortexr4_target
;
95 extern struct target_type arm11_target
;
96 extern struct target_type ls1_sap_target
;
97 extern struct target_type mips_m4k_target
;
98 extern struct target_type mips_mips64_target
;
99 extern struct target_type avr_target
;
100 extern struct target_type dsp563xx_target
;
101 extern struct target_type dsp5680xx_target
;
102 extern struct target_type testee_target
;
103 extern struct target_type avr32_ap7k_target
;
104 extern struct target_type hla_target
;
105 extern struct target_type nds32_v2_target
;
106 extern struct target_type nds32_v3_target
;
107 extern struct target_type nds32_v3m_target
;
108 extern struct target_type esp32s2_target
;
109 extern struct target_type or1k_target
;
110 extern struct target_type quark_x10xx_target
;
111 extern struct target_type quark_d20xx_target
;
112 extern struct target_type stm8_target
;
113 extern struct target_type riscv_target
;
114 extern struct target_type mem_ap_target
;
115 extern struct target_type esirisc_target
;
116 extern struct target_type arcv2_target
;
118 static struct target_type
*target_types
[] = {
159 struct target
*all_targets
;
160 static struct target_event_callback
*target_event_callbacks
;
161 static struct target_timer_callback
*target_timer_callbacks
;
162 static int64_t target_timer_next_event_value
;
163 static LIST_HEAD(target_reset_callback_list
);
164 static LIST_HEAD(target_trace_callback_list
);
165 static const int polling_interval
= TARGET_DEFAULT_POLLING_INTERVAL
;
166 static LIST_HEAD(empty_smp_targets
);
168 static const struct jim_nvp nvp_assert
[] = {
169 { .name
= "assert", NVP_ASSERT
},
170 { .name
= "deassert", NVP_DEASSERT
},
171 { .name
= "T", NVP_ASSERT
},
172 { .name
= "F", NVP_DEASSERT
},
173 { .name
= "t", NVP_ASSERT
},
174 { .name
= "f", NVP_DEASSERT
},
175 { .name
= NULL
, .value
= -1 }
178 static const struct jim_nvp nvp_error_target
[] = {
179 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
180 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
181 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
182 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
183 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
184 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
185 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
186 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
187 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
188 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
189 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
190 { .value
= -1, .name
= NULL
}
193 static const char *target_strerror_safe(int err
)
195 const struct jim_nvp
*n
;
197 n
= jim_nvp_value2name_simple(nvp_error_target
, err
);
204 static const struct jim_nvp nvp_target_event
[] = {
206 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
207 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
208 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
209 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
210 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
211 { .value
= TARGET_EVENT_STEP_START
, .name
= "step-start" },
212 { .value
= TARGET_EVENT_STEP_END
, .name
= "step-end" },
214 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
215 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
217 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
218 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
219 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
220 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
221 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
222 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
223 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
224 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
226 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
227 { .value
= TARGET_EVENT_EXAMINE_FAIL
, .name
= "examine-fail" },
228 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
230 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
231 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
233 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
234 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
236 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
237 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
239 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
240 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
242 { .value
= TARGET_EVENT_TRACE_CONFIG
, .name
= "trace-config" },
244 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0x100
, .name
= "semihosting-user-cmd-0x100" },
245 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0x101
, .name
= "semihosting-user-cmd-0x101" },
246 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0x102
, .name
= "semihosting-user-cmd-0x102" },
247 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0x103
, .name
= "semihosting-user-cmd-0x103" },
248 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0x104
, .name
= "semihosting-user-cmd-0x104" },
249 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0x105
, .name
= "semihosting-user-cmd-0x105" },
250 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0x106
, .name
= "semihosting-user-cmd-0x106" },
251 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0x107
, .name
= "semihosting-user-cmd-0x107" },
253 { .name
= NULL
, .value
= -1 }
256 static const struct jim_nvp nvp_target_state
[] = {
257 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
258 { .name
= "running", .value
= TARGET_RUNNING
},
259 { .name
= "halted", .value
= TARGET_HALTED
},
260 { .name
= "reset", .value
= TARGET_RESET
},
261 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
262 { .name
= NULL
, .value
= -1 },
265 static const struct jim_nvp nvp_target_debug_reason
[] = {
266 { .name
= "debug-request", .value
= DBG_REASON_DBGRQ
},
267 { .name
= "breakpoint", .value
= DBG_REASON_BREAKPOINT
},
268 { .name
= "watchpoint", .value
= DBG_REASON_WATCHPOINT
},
269 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
270 { .name
= "single-step", .value
= DBG_REASON_SINGLESTEP
},
271 { .name
= "target-not-halted", .value
= DBG_REASON_NOTHALTED
},
272 { .name
= "program-exit", .value
= DBG_REASON_EXIT
},
273 { .name
= "exception-catch", .value
= DBG_REASON_EXC_CATCH
},
274 { .name
= "undefined", .value
= DBG_REASON_UNDEFINED
},
275 { .name
= NULL
, .value
= -1 },
278 static const struct jim_nvp nvp_target_endian
[] = {
279 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
280 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
281 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
282 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
283 { .name
= NULL
, .value
= -1 },
286 static const struct jim_nvp nvp_reset_modes
[] = {
287 { .name
= "unknown", .value
= RESET_UNKNOWN
},
288 { .name
= "run", .value
= RESET_RUN
},
289 { .name
= "halt", .value
= RESET_HALT
},
290 { .name
= "init", .value
= RESET_INIT
},
291 { .name
= NULL
, .value
= -1 },
294 const char *debug_reason_name(struct target
*t
)
298 cp
= jim_nvp_value2name_simple(nvp_target_debug_reason
,
299 t
->debug_reason
)->name
;
301 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
302 cp
= "(*BUG*unknown*BUG*)";
307 const char *target_state_name(struct target
*t
)
310 cp
= jim_nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
312 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
313 cp
= "(*BUG*unknown*BUG*)";
316 if (!target_was_examined(t
) && t
->defer_examine
)
317 cp
= "examine deferred";
322 const char *target_event_name(enum target_event event
)
325 cp
= jim_nvp_value2name_simple(nvp_target_event
, event
)->name
;
327 LOG_ERROR("Invalid target event: %d", (int)(event
));
328 cp
= "(*BUG*unknown*BUG*)";
333 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
336 cp
= jim_nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
;
338 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
339 cp
= "(*BUG*unknown*BUG*)";
344 /* determine the number of the new target */
345 static int new_target_number(void)
350 /* number is 0 based */
354 if (x
< t
->target_number
)
355 x
= t
->target_number
;
361 static void append_to_list_all_targets(struct target
*target
)
363 struct target
**t
= &all_targets
;
370 /* read a uint64_t from a buffer in target memory endianness */
371 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
373 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
374 return le_to_h_u64(buffer
);
376 return be_to_h_u64(buffer
);
379 /* read a uint32_t from a buffer in target memory endianness */
380 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
382 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
383 return le_to_h_u32(buffer
);
385 return be_to_h_u32(buffer
);
388 /* read a uint24_t from a buffer in target memory endianness */
389 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
391 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
392 return le_to_h_u24(buffer
);
394 return be_to_h_u24(buffer
);
397 /* read a uint16_t from a buffer in target memory endianness */
398 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
400 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
401 return le_to_h_u16(buffer
);
403 return be_to_h_u16(buffer
);
406 /* write a uint64_t to a buffer in target memory endianness */
407 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
409 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
410 h_u64_to_le(buffer
, value
);
412 h_u64_to_be(buffer
, value
);
415 /* write a uint32_t to a buffer in target memory endianness */
416 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
418 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
419 h_u32_to_le(buffer
, value
);
421 h_u32_to_be(buffer
, value
);
424 /* write a uint24_t to a buffer in target memory endianness */
425 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
427 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
428 h_u24_to_le(buffer
, value
);
430 h_u24_to_be(buffer
, value
);
433 /* write a uint16_t to a buffer in target memory endianness */
434 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
436 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
437 h_u16_to_le(buffer
, value
);
439 h_u16_to_be(buffer
, value
);
442 /* write a uint8_t to a buffer in target memory endianness */
443 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
448 /* write a uint64_t array to a buffer in target memory endianness */
449 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
452 for (i
= 0; i
< count
; i
++)
453 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
456 /* write a uint32_t array to a buffer in target memory endianness */
457 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
460 for (i
= 0; i
< count
; i
++)
461 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
464 /* write a uint16_t array to a buffer in target memory endianness */
465 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
468 for (i
= 0; i
< count
; i
++)
469 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
472 /* write a uint64_t array to a buffer in target memory endianness */
473 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
476 for (i
= 0; i
< count
; i
++)
477 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
480 /* write a uint32_t array to a buffer in target memory endianness */
481 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
484 for (i
= 0; i
< count
; i
++)
485 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
488 /* write a uint16_t array to a buffer in target memory endianness */
489 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
492 for (i
= 0; i
< count
; i
++)
493 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
496 /* return a pointer to a configured target; id is name or number */
497 struct target
*get_target(const char *id
)
499 struct target
*target
;
501 /* try as tcltarget name */
502 for (target
= all_targets
; target
; target
= target
->next
) {
503 if (!target_name(target
))
505 if (strcmp(id
, target_name(target
)) == 0)
509 /* It's OK to remove this fallback sometime after August 2010 or so */
511 /* no match, try as number */
513 if (parse_uint(id
, &num
) != ERROR_OK
)
516 for (target
= all_targets
; target
; target
= target
->next
) {
517 if (target
->target_number
== (int)num
) {
518 LOG_WARNING("use '%s' as target identifier, not '%u'",
519 target_name(target
), num
);
527 /* returns a pointer to the n-th configured target */
528 struct target
*get_target_by_num(int num
)
530 struct target
*target
= all_targets
;
533 if (target
->target_number
== num
)
535 target
= target
->next
;
541 struct target
*get_current_target(struct command_context
*cmd_ctx
)
543 struct target
*target
= get_current_target_or_null(cmd_ctx
);
546 LOG_ERROR("BUG: current_target out of bounds");
553 struct target
*get_current_target_or_null(struct command_context
*cmd_ctx
)
555 return cmd_ctx
->current_target_override
556 ? cmd_ctx
->current_target_override
557 : cmd_ctx
->current_target
;
560 int target_poll(struct target
*target
)
564 /* We can't poll until after examine */
565 if (!target_was_examined(target
)) {
566 /* Fail silently lest we pollute the log */
570 retval
= target
->type
->poll(target
);
571 if (retval
!= ERROR_OK
)
574 if (target
->halt_issued
) {
575 if (target
->state
== TARGET_HALTED
)
576 target
->halt_issued
= false;
578 int64_t t
= timeval_ms() - target
->halt_issued_time
;
579 if (t
> DEFAULT_HALT_TIMEOUT
) {
580 target
->halt_issued
= false;
581 LOG_INFO("Halt timed out, wake up GDB.");
582 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
590 int target_halt(struct target
*target
)
593 /* We can't poll until after examine */
594 if (!target_was_examined(target
)) {
595 LOG_ERROR("Target not examined yet");
599 retval
= target
->type
->halt(target
);
600 if (retval
!= ERROR_OK
)
603 target
->halt_issued
= true;
604 target
->halt_issued_time
= timeval_ms();
610 * Make the target (re)start executing using its saved execution
611 * context (possibly with some modifications).
613 * @param target Which target should start executing.
614 * @param current True to use the target's saved program counter instead
615 * of the address parameter
616 * @param address Optionally used as the program counter.
617 * @param handle_breakpoints True iff breakpoints at the resumption PC
618 * should be skipped. (For example, maybe execution was stopped by
619 * such a breakpoint, in which case it would be counterproductive to
621 * @param debug_execution False if all working areas allocated by OpenOCD
622 * should be released and/or restored to their original contents.
623 * (This would for example be true to run some downloaded "helper"
624 * algorithm code, which resides in one such working buffer and uses
625 * another for data storage.)
627 * @todo Resolve the ambiguity about what the "debug_execution" flag
628 * signifies. For example, Target implementations don't agree on how
629 * it relates to invalidation of the register cache, or to whether
630 * breakpoints and watchpoints should be enabled. (It would seem wrong
631 * to enable breakpoints when running downloaded "helper" algorithms
632 * (debug_execution true), since the breakpoints would be set to match
633 * target firmware being debugged, not the helper algorithm.... and
634 * enabling them could cause such helpers to malfunction (for example,
635 * by overwriting data with a breakpoint instruction. On the other
636 * hand the infrastructure for running such helpers might use this
637 * procedure but rely on hardware breakpoint to detect termination.)
639 int target_resume(struct target
*target
, int current
, target_addr_t address
,
640 int handle_breakpoints
, int debug_execution
)
644 /* We can't poll until after examine */
645 if (!target_was_examined(target
)) {
646 LOG_ERROR("Target not examined yet");
650 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
652 /* note that resume *must* be asynchronous. The CPU can halt before
653 * we poll. The CPU can even halt at the current PC as a result of
654 * a software breakpoint being inserted by (a bug?) the application.
657 * resume() triggers the event 'resumed'. The execution of TCL commands
658 * in the event handler causes the polling of targets. If the target has
659 * already halted for a breakpoint, polling will run the 'halted' event
660 * handler before the pending 'resumed' handler.
661 * Disable polling during resume() to guarantee the execution of handlers
662 * in the correct order.
664 bool save_poll
= jtag_poll_get_enabled();
665 jtag_poll_set_enabled(false);
666 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
667 jtag_poll_set_enabled(save_poll
);
668 if (retval
!= ERROR_OK
)
671 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
676 static int target_process_reset(struct command_invocation
*cmd
, enum target_reset_mode reset_mode
)
681 n
= jim_nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
683 LOG_ERROR("invalid reset mode");
687 struct target
*target
;
688 for (target
= all_targets
; target
; target
= target
->next
)
689 target_call_reset_callbacks(target
, reset_mode
);
691 /* disable polling during reset to make reset event scripts
692 * more predictable, i.e. dr/irscan & pathmove in events will
693 * not have JTAG operations injected into the middle of a sequence.
695 bool save_poll
= jtag_poll_get_enabled();
697 jtag_poll_set_enabled(false);
699 sprintf(buf
, "ocd_process_reset %s", n
->name
);
700 retval
= Jim_Eval(cmd
->ctx
->interp
, buf
);
702 jtag_poll_set_enabled(save_poll
);
704 if (retval
!= JIM_OK
) {
705 Jim_MakeErrorMessage(cmd
->ctx
->interp
);
706 command_print(cmd
, "%s", Jim_GetString(Jim_GetResult(cmd
->ctx
->interp
), NULL
));
710 /* We want any events to be processed before the prompt */
711 retval
= target_call_timer_callbacks_now();
713 for (target
= all_targets
; target
; target
= target
->next
) {
714 target
->type
->check_reset(target
);
715 target
->running_alg
= false;
721 static int identity_virt2phys(struct target
*target
,
722 target_addr_t
virtual, target_addr_t
*physical
)
728 static int no_mmu(struct target
*target
, int *enabled
)
735 * Reset the @c examined flag for the given target.
736 * Pure paranoia -- targets are zeroed on allocation.
738 static inline void target_reset_examined(struct target
*target
)
740 target
->examined
= false;
743 static int default_examine(struct target
*target
)
745 target_set_examined(target
);
749 /* no check by default */
750 static int default_check_reset(struct target
*target
)
755 /* Equivalent Tcl code arp_examine_one is in src/target/startup.tcl
757 int target_examine_one(struct target
*target
)
759 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
761 int retval
= target
->type
->examine(target
);
762 if (retval
!= ERROR_OK
) {
763 target_reset_examined(target
);
764 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_FAIL
);
768 target_set_examined(target
);
769 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
774 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
776 struct target
*target
= priv
;
778 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
781 jtag_unregister_event_callback(jtag_enable_callback
, target
);
783 return target_examine_one(target
);
786 /* Targets that correctly implement init + examine, i.e.
787 * no communication with target during init:
791 int target_examine(void)
793 int retval
= ERROR_OK
;
794 struct target
*target
;
796 for (target
= all_targets
; target
; target
= target
->next
) {
797 /* defer examination, but don't skip it */
798 if (!target
->tap
->enabled
) {
799 jtag_register_event_callback(jtag_enable_callback
,
804 if (target
->defer_examine
)
807 int retval2
= target_examine_one(target
);
808 if (retval2
!= ERROR_OK
) {
809 LOG_WARNING("target %s examination failed", target_name(target
));
816 const char *target_type_name(struct target
*target
)
818 return target
->type
->name
;
821 static int target_soft_reset_halt(struct target
*target
)
823 if (!target_was_examined(target
)) {
824 LOG_ERROR("Target not examined yet");
827 if (!target
->type
->soft_reset_halt
) {
828 LOG_ERROR("Target %s does not support soft_reset_halt",
829 target_name(target
));
832 return target
->type
->soft_reset_halt(target
);
836 * Downloads a target-specific native code algorithm to the target,
837 * and executes it. * Note that some targets may need to set up, enable,
838 * and tear down a breakpoint (hard or * soft) to detect algorithm
839 * termination, while others may support lower overhead schemes where
840 * soft breakpoints embedded in the algorithm automatically terminate the
843 * @param target used to run the algorithm
844 * @param num_mem_params
846 * @param num_reg_params
851 * @param arch_info target-specific description of the algorithm.
853 int target_run_algorithm(struct target
*target
,
854 int num_mem_params
, struct mem_param
*mem_params
,
855 int num_reg_params
, struct reg_param
*reg_param
,
856 target_addr_t entry_point
, target_addr_t exit_point
,
857 int timeout_ms
, void *arch_info
)
859 int retval
= ERROR_FAIL
;
861 if (!target_was_examined(target
)) {
862 LOG_ERROR("Target not examined yet");
865 if (!target
->type
->run_algorithm
) {
866 LOG_ERROR("Target type '%s' does not support %s",
867 target_type_name(target
), __func__
);
871 target
->running_alg
= true;
872 retval
= target
->type
->run_algorithm(target
,
873 num_mem_params
, mem_params
,
874 num_reg_params
, reg_param
,
875 entry_point
, exit_point
, timeout_ms
, arch_info
);
876 target
->running_alg
= false;
883 * Executes a target-specific native code algorithm and leaves it running.
885 * @param target used to run the algorithm
886 * @param num_mem_params
888 * @param num_reg_params
892 * @param arch_info target-specific description of the algorithm.
894 int target_start_algorithm(struct target
*target
,
895 int num_mem_params
, struct mem_param
*mem_params
,
896 int num_reg_params
, struct reg_param
*reg_params
,
897 target_addr_t entry_point
, target_addr_t exit_point
,
900 int retval
= ERROR_FAIL
;
902 if (!target_was_examined(target
)) {
903 LOG_ERROR("Target not examined yet");
906 if (!target
->type
->start_algorithm
) {
907 LOG_ERROR("Target type '%s' does not support %s",
908 target_type_name(target
), __func__
);
911 if (target
->running_alg
) {
912 LOG_ERROR("Target is already running an algorithm");
916 target
->running_alg
= true;
917 retval
= target
->type
->start_algorithm(target
,
918 num_mem_params
, mem_params
,
919 num_reg_params
, reg_params
,
920 entry_point
, exit_point
, arch_info
);
927 * Waits for an algorithm started with target_start_algorithm() to complete.
929 * @param target used to run the algorithm
930 * @param num_mem_params
932 * @param num_reg_params
936 * @param arch_info target-specific description of the algorithm.
938 int target_wait_algorithm(struct target
*target
,
939 int num_mem_params
, struct mem_param
*mem_params
,
940 int num_reg_params
, struct reg_param
*reg_params
,
941 target_addr_t exit_point
, int timeout_ms
,
944 int retval
= ERROR_FAIL
;
946 if (!target
->type
->wait_algorithm
) {
947 LOG_ERROR("Target type '%s' does not support %s",
948 target_type_name(target
), __func__
);
951 if (!target
->running_alg
) {
952 LOG_ERROR("Target is not running an algorithm");
956 retval
= target
->type
->wait_algorithm(target
,
957 num_mem_params
, mem_params
,
958 num_reg_params
, reg_params
,
959 exit_point
, timeout_ms
, arch_info
);
960 if (retval
!= ERROR_TARGET_TIMEOUT
)
961 target
->running_alg
= false;
968 * Streams data to a circular buffer on target intended for consumption by code
969 * running asynchronously on target.
971 * This is intended for applications where target-specific native code runs
972 * on the target, receives data from the circular buffer, does something with
973 * it (most likely writing it to a flash memory), and advances the circular
976 * This assumes that the helper algorithm has already been loaded to the target,
977 * but has not been started yet. Given memory and register parameters are passed
980 * The buffer is defined by (buffer_start, buffer_size) arguments and has the
983 * [buffer_start + 0, buffer_start + 4):
984 * Write Pointer address (aka head). Written and updated by this
985 * routine when new data is written to the circular buffer.
986 * [buffer_start + 4, buffer_start + 8):
987 * Read Pointer address (aka tail). Updated by code running on the
988 * target after it consumes data.
989 * [buffer_start + 8, buffer_start + buffer_size):
990 * Circular buffer contents.
992 * See contrib/loaders/flash/stm32f1x.S for an example.
994 * @param target used to run the algorithm
995 * @param buffer address on the host where data to be sent is located
996 * @param count number of blocks to send
997 * @param block_size size in bytes of each block
998 * @param num_mem_params count of memory-based params to pass to algorithm
999 * @param mem_params memory-based params to pass to algorithm
1000 * @param num_reg_params count of register-based params to pass to algorithm
1001 * @param reg_params memory-based params to pass to algorithm
1002 * @param buffer_start address on the target of the circular buffer structure
1003 * @param buffer_size size of the circular buffer structure
1004 * @param entry_point address on the target to execute to start the algorithm
1005 * @param exit_point address at which to set a breakpoint to catch the
1006 * end of the algorithm; can be 0 if target triggers a breakpoint itself
1010 int target_run_flash_async_algorithm(struct target
*target
,
1011 const uint8_t *buffer
, uint32_t count
, int block_size
,
1012 int num_mem_params
, struct mem_param
*mem_params
,
1013 int num_reg_params
, struct reg_param
*reg_params
,
1014 uint32_t buffer_start
, uint32_t buffer_size
,
1015 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
1020 const uint8_t *buffer_orig
= buffer
;
1022 /* Set up working area. First word is write pointer, second word is read pointer,
1023 * rest is fifo data area. */
1024 uint32_t wp_addr
= buffer_start
;
1025 uint32_t rp_addr
= buffer_start
+ 4;
1026 uint32_t fifo_start_addr
= buffer_start
+ 8;
1027 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
1029 uint32_t wp
= fifo_start_addr
;
1030 uint32_t rp
= fifo_start_addr
;
1032 /* validate block_size is 2^n */
1033 assert(IS_PWR_OF_2(block_size
));
1035 retval
= target_write_u32(target
, wp_addr
, wp
);
1036 if (retval
!= ERROR_OK
)
1038 retval
= target_write_u32(target
, rp_addr
, rp
);
1039 if (retval
!= ERROR_OK
)
1042 /* Start up algorithm on target and let it idle while writing the first chunk */
1043 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
1044 num_reg_params
, reg_params
,
1049 if (retval
!= ERROR_OK
) {
1050 LOG_ERROR("error starting target flash write algorithm");
1056 retval
= target_read_u32(target
, rp_addr
, &rp
);
1057 if (retval
!= ERROR_OK
) {
1058 LOG_ERROR("failed to get read pointer");
1062 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
1063 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
1066 LOG_ERROR("flash write algorithm aborted by target");
1067 retval
= ERROR_FLASH_OPERATION_FAILED
;
1071 if (!IS_ALIGNED(rp
- fifo_start_addr
, block_size
) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
1072 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
1076 /* Count the number of bytes available in the fifo without
1077 * crossing the wrap around. Make sure to not fill it completely,
1078 * because that would make wp == rp and that's the empty condition. */
1079 uint32_t thisrun_bytes
;
1081 thisrun_bytes
= rp
- wp
- block_size
;
1082 else if (rp
> fifo_start_addr
)
1083 thisrun_bytes
= fifo_end_addr
- wp
;
1085 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
1087 if (thisrun_bytes
== 0) {
1088 /* Throttle polling a bit if transfer is (much) faster than flash
1089 * programming. The exact delay shouldn't matter as long as it's
1090 * less than buffer size / flash speed. This is very unlikely to
1091 * run when using high latency connections such as USB. */
1094 /* to stop an infinite loop on some targets check and increment a timeout
1095 * this issue was observed on a stellaris using the new ICDI interface */
1096 if (timeout
++ >= 2500) {
1097 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1098 return ERROR_FLASH_OPERATION_FAILED
;
1103 /* reset our timeout */
1106 /* Limit to the amount of data we actually want to write */
1107 if (thisrun_bytes
> count
* block_size
)
1108 thisrun_bytes
= count
* block_size
;
1110 /* Force end of large blocks to be word aligned */
1111 if (thisrun_bytes
>= 16)
1112 thisrun_bytes
-= (rp
+ thisrun_bytes
) & 0x03;
1114 /* Write data to fifo */
1115 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
1116 if (retval
!= ERROR_OK
)
1119 /* Update counters and wrap write pointer */
1120 buffer
+= thisrun_bytes
;
1121 count
-= thisrun_bytes
/ block_size
;
1122 wp
+= thisrun_bytes
;
1123 if (wp
>= fifo_end_addr
)
1124 wp
= fifo_start_addr
;
1126 /* Store updated write pointer to target */
1127 retval
= target_write_u32(target
, wp_addr
, wp
);
1128 if (retval
!= ERROR_OK
)
1131 /* Avoid GDB timeouts */
1135 if (retval
!= ERROR_OK
) {
1136 /* abort flash write algorithm on target */
1137 target_write_u32(target
, wp_addr
, 0);
1140 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1141 num_reg_params
, reg_params
,
1146 if (retval2
!= ERROR_OK
) {
1147 LOG_ERROR("error waiting for target flash write algorithm");
1151 if (retval
== ERROR_OK
) {
1152 /* check if algorithm set rp = 0 after fifo writer loop finished */
1153 retval
= target_read_u32(target
, rp_addr
, &rp
);
1154 if (retval
== ERROR_OK
&& rp
== 0) {
1155 LOG_ERROR("flash write algorithm aborted by target");
1156 retval
= ERROR_FLASH_OPERATION_FAILED
;
1163 int target_run_read_async_algorithm(struct target
*target
,
1164 uint8_t *buffer
, uint32_t count
, int block_size
,
1165 int num_mem_params
, struct mem_param
*mem_params
,
1166 int num_reg_params
, struct reg_param
*reg_params
,
1167 uint32_t buffer_start
, uint32_t buffer_size
,
1168 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
1173 const uint8_t *buffer_orig
= buffer
;
1175 /* Set up working area. First word is write pointer, second word is read pointer,
1176 * rest is fifo data area. */
1177 uint32_t wp_addr
= buffer_start
;
1178 uint32_t rp_addr
= buffer_start
+ 4;
1179 uint32_t fifo_start_addr
= buffer_start
+ 8;
1180 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
1182 uint32_t wp
= fifo_start_addr
;
1183 uint32_t rp
= fifo_start_addr
;
1185 /* validate block_size is 2^n */
1186 assert(IS_PWR_OF_2(block_size
));
1188 retval
= target_write_u32(target
, wp_addr
, wp
);
1189 if (retval
!= ERROR_OK
)
1191 retval
= target_write_u32(target
, rp_addr
, rp
);
1192 if (retval
!= ERROR_OK
)
1195 /* Start up algorithm on target */
1196 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
1197 num_reg_params
, reg_params
,
1202 if (retval
!= ERROR_OK
) {
1203 LOG_ERROR("error starting target flash read algorithm");
1208 retval
= target_read_u32(target
, wp_addr
, &wp
);
1209 if (retval
!= ERROR_OK
) {
1210 LOG_ERROR("failed to get write pointer");
1214 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
1215 (size_t)(buffer
- buffer_orig
), count
, wp
, rp
);
1218 LOG_ERROR("flash read algorithm aborted by target");
1219 retval
= ERROR_FLASH_OPERATION_FAILED
;
1223 if (!IS_ALIGNED(wp
- fifo_start_addr
, block_size
) || wp
< fifo_start_addr
|| wp
>= fifo_end_addr
) {
1224 LOG_ERROR("corrupted fifo write pointer 0x%" PRIx32
, wp
);
1228 /* Count the number of bytes available in the fifo without
1229 * crossing the wrap around. */
1230 uint32_t thisrun_bytes
;
1232 thisrun_bytes
= wp
- rp
;
1234 thisrun_bytes
= fifo_end_addr
- rp
;
1236 if (thisrun_bytes
== 0) {
1237 /* Throttle polling a bit if transfer is (much) faster than flash
1238 * reading. The exact delay shouldn't matter as long as it's
1239 * less than buffer size / flash speed. This is very unlikely to
1240 * run when using high latency connections such as USB. */
1243 /* to stop an infinite loop on some targets check and increment a timeout
1244 * this issue was observed on a stellaris using the new ICDI interface */
1245 if (timeout
++ >= 2500) {
1246 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1247 return ERROR_FLASH_OPERATION_FAILED
;
1252 /* Reset our timeout */
1255 /* Limit to the amount of data we actually want to read */
1256 if (thisrun_bytes
> count
* block_size
)
1257 thisrun_bytes
= count
* block_size
;
1259 /* Force end of large blocks to be word aligned */
1260 if (thisrun_bytes
>= 16)
1261 thisrun_bytes
-= (rp
+ thisrun_bytes
) & 0x03;
1263 /* Read data from fifo */
1264 retval
= target_read_buffer(target
, rp
, thisrun_bytes
, buffer
);
1265 if (retval
!= ERROR_OK
)
1268 /* Update counters and wrap write pointer */
1269 buffer
+= thisrun_bytes
;
1270 count
-= thisrun_bytes
/ block_size
;
1271 rp
+= thisrun_bytes
;
1272 if (rp
>= fifo_end_addr
)
1273 rp
= fifo_start_addr
;
1275 /* Store updated write pointer to target */
1276 retval
= target_write_u32(target
, rp_addr
, rp
);
1277 if (retval
!= ERROR_OK
)
1280 /* Avoid GDB timeouts */
1285 if (retval
!= ERROR_OK
) {
1286 /* abort flash write algorithm on target */
1287 target_write_u32(target
, rp_addr
, 0);
1290 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1291 num_reg_params
, reg_params
,
1296 if (retval2
!= ERROR_OK
) {
1297 LOG_ERROR("error waiting for target flash write algorithm");
1301 if (retval
== ERROR_OK
) {
1302 /* check if algorithm set wp = 0 after fifo writer loop finished */
1303 retval
= target_read_u32(target
, wp_addr
, &wp
);
1304 if (retval
== ERROR_OK
&& wp
== 0) {
1305 LOG_ERROR("flash read algorithm aborted by target");
1306 retval
= ERROR_FLASH_OPERATION_FAILED
;
1313 int target_read_memory(struct target
*target
,
1314 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1316 if (!target_was_examined(target
)) {
1317 LOG_ERROR("Target not examined yet");
1320 if (!target
->type
->read_memory
) {
1321 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1324 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1327 int target_read_phys_memory(struct target
*target
,
1328 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1330 if (!target_was_examined(target
)) {
1331 LOG_ERROR("Target not examined yet");
1334 if (!target
->type
->read_phys_memory
) {
1335 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1338 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1341 int target_write_memory(struct target
*target
,
1342 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1344 if (!target_was_examined(target
)) {
1345 LOG_ERROR("Target not examined yet");
1348 if (!target
->type
->write_memory
) {
1349 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1352 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1355 int target_write_phys_memory(struct target
*target
,
1356 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1358 if (!target_was_examined(target
)) {
1359 LOG_ERROR("Target not examined yet");
1362 if (!target
->type
->write_phys_memory
) {
1363 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1366 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1369 int target_add_breakpoint(struct target
*target
,
1370 struct breakpoint
*breakpoint
)
1372 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1373 LOG_WARNING("target %s is not halted (add breakpoint)", target_name(target
));
1374 return ERROR_TARGET_NOT_HALTED
;
1376 return target
->type
->add_breakpoint(target
, breakpoint
);
1379 int target_add_context_breakpoint(struct target
*target
,
1380 struct breakpoint
*breakpoint
)
1382 if (target
->state
!= TARGET_HALTED
) {
1383 LOG_WARNING("target %s is not halted (add context breakpoint)", target_name(target
));
1384 return ERROR_TARGET_NOT_HALTED
;
1386 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1389 int target_add_hybrid_breakpoint(struct target
*target
,
1390 struct breakpoint
*breakpoint
)
1392 if (target
->state
!= TARGET_HALTED
) {
1393 LOG_WARNING("target %s is not halted (add hybrid breakpoint)", target_name(target
));
1394 return ERROR_TARGET_NOT_HALTED
;
1396 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1399 int target_remove_breakpoint(struct target
*target
,
1400 struct breakpoint
*breakpoint
)
1402 return target
->type
->remove_breakpoint(target
, breakpoint
);
1405 int target_add_watchpoint(struct target
*target
,
1406 struct watchpoint
*watchpoint
)
1408 if (target
->state
!= TARGET_HALTED
) {
1409 LOG_WARNING("target %s is not halted (add watchpoint)", target_name(target
));
1410 return ERROR_TARGET_NOT_HALTED
;
1412 return target
->type
->add_watchpoint(target
, watchpoint
);
1414 int target_remove_watchpoint(struct target
*target
,
1415 struct watchpoint
*watchpoint
)
1417 return target
->type
->remove_watchpoint(target
, watchpoint
);
1419 int target_hit_watchpoint(struct target
*target
,
1420 struct watchpoint
**hit_watchpoint
)
1422 if (target
->state
!= TARGET_HALTED
) {
1423 LOG_WARNING("target %s is not halted (hit watchpoint)", target
->cmd_name
);
1424 return ERROR_TARGET_NOT_HALTED
;
1427 if (!target
->type
->hit_watchpoint
) {
1428 /* For backward compatible, if hit_watchpoint is not implemented,
1429 * return ERROR_FAIL such that gdb_server will not take the nonsense
1434 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1437 const char *target_get_gdb_arch(struct target
*target
)
1439 if (!target
->type
->get_gdb_arch
)
1441 return target
->type
->get_gdb_arch(target
);
1444 int target_get_gdb_reg_list(struct target
*target
,
1445 struct reg
**reg_list
[], int *reg_list_size
,
1446 enum target_register_class reg_class
)
1448 int result
= ERROR_FAIL
;
1450 if (!target_was_examined(target
)) {
1451 LOG_ERROR("Target not examined yet");
1455 result
= target
->type
->get_gdb_reg_list(target
, reg_list
,
1456 reg_list_size
, reg_class
);
1459 if (result
!= ERROR_OK
) {
1466 int target_get_gdb_reg_list_noread(struct target
*target
,
1467 struct reg
**reg_list
[], int *reg_list_size
,
1468 enum target_register_class reg_class
)
1470 if (target
->type
->get_gdb_reg_list_noread
&&
1471 target
->type
->get_gdb_reg_list_noread(target
, reg_list
,
1472 reg_list_size
, reg_class
) == ERROR_OK
)
1474 return target_get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1477 bool target_supports_gdb_connection(struct target
*target
)
1480 * exclude all the targets that don't provide get_gdb_reg_list
1481 * or that have explicit gdb_max_connection == 0
1483 return !!target
->type
->get_gdb_reg_list
&& !!target
->gdb_max_connections
;
1486 int target_step(struct target
*target
,
1487 int current
, target_addr_t address
, int handle_breakpoints
)
1491 target_call_event_callbacks(target
, TARGET_EVENT_STEP_START
);
1493 retval
= target
->type
->step(target
, current
, address
, handle_breakpoints
);
1494 if (retval
!= ERROR_OK
)
1497 target_call_event_callbacks(target
, TARGET_EVENT_STEP_END
);
1502 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1504 if (target
->state
!= TARGET_HALTED
) {
1505 LOG_WARNING("target %s is not halted (gdb fileio)", target
->cmd_name
);
1506 return ERROR_TARGET_NOT_HALTED
;
1508 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1511 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1513 if (target
->state
!= TARGET_HALTED
) {
1514 LOG_WARNING("target %s is not halted (gdb fileio end)", target
->cmd_name
);
1515 return ERROR_TARGET_NOT_HALTED
;
1517 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1520 target_addr_t
target_address_max(struct target
*target
)
1522 unsigned bits
= target_address_bits(target
);
1523 if (sizeof(target_addr_t
) * 8 == bits
)
1524 return (target_addr_t
) -1;
1526 return (((target_addr_t
) 1) << bits
) - 1;
1529 unsigned target_address_bits(struct target
*target
)
1531 if (target
->type
->address_bits
)
1532 return target
->type
->address_bits(target
);
1536 unsigned int target_data_bits(struct target
*target
)
1538 if (target
->type
->data_bits
)
1539 return target
->type
->data_bits(target
);
1543 static int target_profiling(struct target
*target
, uint32_t *samples
,
1544 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1546 return target
->type
->profiling(target
, samples
, max_num_samples
,
1547 num_samples
, seconds
);
1550 static int handle_target(void *priv
);
1552 static int target_init_one(struct command_context
*cmd_ctx
,
1553 struct target
*target
)
1555 target_reset_examined(target
);
1557 struct target_type
*type
= target
->type
;
1559 type
->examine
= default_examine
;
1561 if (!type
->check_reset
)
1562 type
->check_reset
= default_check_reset
;
1564 assert(type
->init_target
);
1566 int retval
= type
->init_target(cmd_ctx
, target
);
1567 if (retval
!= ERROR_OK
) {
1568 LOG_ERROR("target '%s' init failed", target_name(target
));
1572 /* Sanity-check MMU support ... stub in what we must, to help
1573 * implement it in stages, but warn if we need to do so.
1576 if (!type
->virt2phys
) {
1577 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1578 type
->virt2phys
= identity_virt2phys
;
1581 /* Make sure no-MMU targets all behave the same: make no
1582 * distinction between physical and virtual addresses, and
1583 * ensure that virt2phys() is always an identity mapping.
1585 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1586 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1589 type
->write_phys_memory
= type
->write_memory
;
1590 type
->read_phys_memory
= type
->read_memory
;
1591 type
->virt2phys
= identity_virt2phys
;
1594 if (!target
->type
->read_buffer
)
1595 target
->type
->read_buffer
= target_read_buffer_default
;
1597 if (!target
->type
->write_buffer
)
1598 target
->type
->write_buffer
= target_write_buffer_default
;
1600 if (!target
->type
->get_gdb_fileio_info
)
1601 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1603 if (!target
->type
->gdb_fileio_end
)
1604 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1606 if (!target
->type
->profiling
)
1607 target
->type
->profiling
= target_profiling_default
;
1612 static int target_init(struct command_context
*cmd_ctx
)
1614 struct target
*target
;
1617 for (target
= all_targets
; target
; target
= target
->next
) {
1618 retval
= target_init_one(cmd_ctx
, target
);
1619 if (retval
!= ERROR_OK
)
1626 retval
= target_register_user_commands(cmd_ctx
);
1627 if (retval
!= ERROR_OK
)
1630 retval
= target_register_timer_callback(&handle_target
,
1631 polling_interval
, TARGET_TIMER_TYPE_PERIODIC
, cmd_ctx
->interp
);
1632 if (retval
!= ERROR_OK
)
1638 COMMAND_HANDLER(handle_target_init_command
)
1643 return ERROR_COMMAND_SYNTAX_ERROR
;
1645 static bool target_initialized
;
1646 if (target_initialized
) {
1647 LOG_INFO("'target init' has already been called");
1650 target_initialized
= true;
1652 retval
= command_run_line(CMD_CTX
, "init_targets");
1653 if (retval
!= ERROR_OK
)
1656 retval
= command_run_line(CMD_CTX
, "init_target_events");
1657 if (retval
!= ERROR_OK
)
1660 retval
= command_run_line(CMD_CTX
, "init_board");
1661 if (retval
!= ERROR_OK
)
1664 LOG_DEBUG("Initializing targets...");
1665 return target_init(CMD_CTX
);
1668 int target_register_event_callback(int (*callback
)(struct target
*target
,
1669 enum target_event event
, void *priv
), void *priv
)
1671 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1674 return ERROR_COMMAND_SYNTAX_ERROR
;
1677 while ((*callbacks_p
)->next
)
1678 callbacks_p
= &((*callbacks_p
)->next
);
1679 callbacks_p
= &((*callbacks_p
)->next
);
1682 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1683 (*callbacks_p
)->callback
= callback
;
1684 (*callbacks_p
)->priv
= priv
;
1685 (*callbacks_p
)->next
= NULL
;
1690 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1691 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1693 struct target_reset_callback
*entry
;
1696 return ERROR_COMMAND_SYNTAX_ERROR
;
1698 entry
= malloc(sizeof(struct target_reset_callback
));
1700 LOG_ERROR("error allocating buffer for reset callback entry");
1701 return ERROR_COMMAND_SYNTAX_ERROR
;
1704 entry
->callback
= callback
;
1706 list_add(&entry
->list
, &target_reset_callback_list
);
1712 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1713 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1715 struct target_trace_callback
*entry
;
1718 return ERROR_COMMAND_SYNTAX_ERROR
;
1720 entry
= malloc(sizeof(struct target_trace_callback
));
1722 LOG_ERROR("error allocating buffer for trace callback entry");
1723 return ERROR_COMMAND_SYNTAX_ERROR
;
1726 entry
->callback
= callback
;
1728 list_add(&entry
->list
, &target_trace_callback_list
);
1734 int target_register_timer_callback(int (*callback
)(void *priv
),
1735 unsigned int time_ms
, enum target_timer_type type
, void *priv
)
1737 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1740 return ERROR_COMMAND_SYNTAX_ERROR
;
1743 while ((*callbacks_p
)->next
)
1744 callbacks_p
= &((*callbacks_p
)->next
);
1745 callbacks_p
= &((*callbacks_p
)->next
);
1748 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1749 (*callbacks_p
)->callback
= callback
;
1750 (*callbacks_p
)->type
= type
;
1751 (*callbacks_p
)->time_ms
= time_ms
;
1752 (*callbacks_p
)->removed
= false;
1754 (*callbacks_p
)->when
= timeval_ms() + time_ms
;
1755 target_timer_next_event_value
= MIN(target_timer_next_event_value
, (*callbacks_p
)->when
);
1757 (*callbacks_p
)->priv
= priv
;
1758 (*callbacks_p
)->next
= NULL
;
1763 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1764 enum target_event event
, void *priv
), void *priv
)
1766 struct target_event_callback
**p
= &target_event_callbacks
;
1767 struct target_event_callback
*c
= target_event_callbacks
;
1770 return ERROR_COMMAND_SYNTAX_ERROR
;
1773 struct target_event_callback
*next
= c
->next
;
1774 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1786 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1787 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1789 struct target_reset_callback
*entry
;
1792 return ERROR_COMMAND_SYNTAX_ERROR
;
1794 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1795 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1796 list_del(&entry
->list
);
1805 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1806 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1808 struct target_trace_callback
*entry
;
1811 return ERROR_COMMAND_SYNTAX_ERROR
;
1813 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1814 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1815 list_del(&entry
->list
);
1824 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1827 return ERROR_COMMAND_SYNTAX_ERROR
;
1829 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1831 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1840 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1842 struct target_event_callback
*callback
= target_event_callbacks
;
1843 struct target_event_callback
*next_callback
;
1845 if (event
== TARGET_EVENT_HALTED
) {
1846 /* execute early halted first */
1847 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1850 LOG_DEBUG("target event %i (%s) for core %s", event
,
1851 target_event_name(event
),
1852 target_name(target
));
1854 target_handle_event(target
, event
);
1857 next_callback
= callback
->next
;
1858 callback
->callback(target
, event
, callback
->priv
);
1859 callback
= next_callback
;
1865 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1867 struct target_reset_callback
*callback
;
1869 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1870 jim_nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1872 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1873 callback
->callback(target
, reset_mode
, callback
->priv
);
1878 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1880 struct target_trace_callback
*callback
;
1882 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1883 callback
->callback(target
, len
, data
, callback
->priv
);
1888 static int target_timer_callback_periodic_restart(
1889 struct target_timer_callback
*cb
, int64_t *now
)
1891 cb
->when
= *now
+ cb
->time_ms
;
1895 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1898 cb
->callback(cb
->priv
);
1900 if (cb
->type
== TARGET_TIMER_TYPE_PERIODIC
)
1901 return target_timer_callback_periodic_restart(cb
, now
);
1903 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1906 static int target_call_timer_callbacks_check_time(int checktime
)
1908 static bool callback_processing
;
1910 /* Do not allow nesting */
1911 if (callback_processing
)
1914 callback_processing
= true;
1918 int64_t now
= timeval_ms();
1920 /* Initialize to a default value that's a ways into the future.
1921 * The loop below will make it closer to now if there are
1922 * callbacks that want to be called sooner. */
1923 target_timer_next_event_value
= now
+ 1000;
1925 /* Store an address of the place containing a pointer to the
1926 * next item; initially, that's a standalone "root of the
1927 * list" variable. */
1928 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1929 while (callback
&& *callback
) {
1930 if ((*callback
)->removed
) {
1931 struct target_timer_callback
*p
= *callback
;
1932 *callback
= (*callback
)->next
;
1937 bool call_it
= (*callback
)->callback
&&
1938 ((!checktime
&& (*callback
)->type
== TARGET_TIMER_TYPE_PERIODIC
) ||
1939 now
>= (*callback
)->when
);
1942 target_call_timer_callback(*callback
, &now
);
1944 if (!(*callback
)->removed
&& (*callback
)->when
< target_timer_next_event_value
)
1945 target_timer_next_event_value
= (*callback
)->when
;
1947 callback
= &(*callback
)->next
;
1950 callback_processing
= false;
1954 int target_call_timer_callbacks()
1956 return target_call_timer_callbacks_check_time(1);
1959 /* invoke periodic callbacks immediately */
1960 int target_call_timer_callbacks_now()
1962 return target_call_timer_callbacks_check_time(0);
1965 int64_t target_timer_next_event(void)
1967 return target_timer_next_event_value
;
1970 /* Prints the working area layout for debug purposes */
1971 static void print_wa_layout(struct target
*target
)
1973 struct working_area
*c
= target
->working_areas
;
1976 LOG_DEBUG("%c%c " TARGET_ADDR_FMT
"-" TARGET_ADDR_FMT
" (%" PRIu32
" bytes)",
1977 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1978 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1983 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1984 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1986 assert(area
->free
); /* Shouldn't split an allocated area */
1987 assert(size
<= area
->size
); /* Caller should guarantee this */
1989 /* Split only if not already the right size */
1990 if (size
< area
->size
) {
1991 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1996 new_wa
->next
= area
->next
;
1997 new_wa
->size
= area
->size
- size
;
1998 new_wa
->address
= area
->address
+ size
;
1999 new_wa
->backup
= NULL
;
2000 new_wa
->user
= NULL
;
2001 new_wa
->free
= true;
2003 area
->next
= new_wa
;
2006 /* If backup memory was allocated to this area, it has the wrong size
2007 * now so free it and it will be reallocated if/when needed */
2009 area
->backup
= NULL
;
2013 /* Merge all adjacent free areas into one */
2014 static void target_merge_working_areas(struct target
*target
)
2016 struct working_area
*c
= target
->working_areas
;
2018 while (c
&& c
->next
) {
2019 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
2021 /* Find two adjacent free areas */
2022 if (c
->free
&& c
->next
->free
) {
2023 /* Merge the last into the first */
2024 c
->size
+= c
->next
->size
;
2026 /* Remove the last */
2027 struct working_area
*to_be_freed
= c
->next
;
2028 c
->next
= c
->next
->next
;
2029 free(to_be_freed
->backup
);
2032 /* If backup memory was allocated to the remaining area, it's has
2033 * the wrong size now */
2042 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
2044 /* Reevaluate working area address based on MMU state*/
2045 if (!target
->working_areas
) {
2049 retval
= target
->type
->mmu(target
, &enabled
);
2050 if (retval
!= ERROR_OK
)
2054 if (target
->working_area_phys_spec
) {
2055 LOG_DEBUG("MMU disabled, using physical "
2056 "address for working memory " TARGET_ADDR_FMT
,
2057 target
->working_area_phys
);
2058 target
->working_area
= target
->working_area_phys
;
2060 LOG_ERROR("No working memory available. "
2061 "Specify -work-area-phys to target.");
2062 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2065 if (target
->working_area_virt_spec
) {
2066 LOG_DEBUG("MMU enabled, using virtual "
2067 "address for working memory " TARGET_ADDR_FMT
,
2068 target
->working_area_virt
);
2069 target
->working_area
= target
->working_area_virt
;
2071 LOG_ERROR("No working memory available. "
2072 "Specify -work-area-virt to target.");
2073 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2077 /* Set up initial working area on first call */
2078 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
2080 new_wa
->next
= NULL
;
2081 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
2082 new_wa
->address
= target
->working_area
;
2083 new_wa
->backup
= NULL
;
2084 new_wa
->user
= NULL
;
2085 new_wa
->free
= true;
2088 target
->working_areas
= new_wa
;
2091 /* only allocate multiples of 4 byte */
2093 size
= (size
+ 3) & (~3UL);
2095 struct working_area
*c
= target
->working_areas
;
2097 /* Find the first large enough working area */
2099 if (c
->free
&& c
->size
>= size
)
2105 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2107 /* Split the working area into the requested size */
2108 target_split_working_area(c
, size
);
2110 LOG_DEBUG("allocated new working area of %" PRIu32
" bytes at address " TARGET_ADDR_FMT
,
2113 if (target
->backup_working_area
) {
2115 c
->backup
= malloc(c
->size
);
2120 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
2121 if (retval
!= ERROR_OK
)
2125 /* mark as used, and return the new (reused) area */
2132 print_wa_layout(target
);
2137 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
2141 retval
= target_alloc_working_area_try(target
, size
, area
);
2142 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
2143 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
2148 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
2150 int retval
= ERROR_OK
;
2152 if (target
->backup_working_area
&& area
->backup
) {
2153 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
2154 if (retval
!= ERROR_OK
)
2155 LOG_ERROR("failed to restore %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
2156 area
->size
, area
->address
);
2162 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
2163 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
2165 if (!area
|| area
->free
)
2168 int retval
= ERROR_OK
;
2170 retval
= target_restore_working_area(target
, area
);
2171 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
2172 if (retval
!= ERROR_OK
)
2178 LOG_DEBUG("freed %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
2179 area
->size
, area
->address
);
2181 /* mark user pointer invalid */
2182 /* TODO: Is this really safe? It points to some previous caller's memory.
2183 * How could we know that the area pointer is still in that place and not
2184 * some other vital data? What's the purpose of this, anyway? */
2188 target_merge_working_areas(target
);
2190 print_wa_layout(target
);
2195 int target_free_working_area(struct target
*target
, struct working_area
*area
)
2197 return target_free_working_area_restore(target
, area
, 1);
2200 /* free resources and restore memory, if restoring memory fails,
2201 * free up resources anyway
2203 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
2205 struct working_area
*c
= target
->working_areas
;
2207 LOG_DEBUG("freeing all working areas");
2209 /* Loop through all areas, restoring the allocated ones and marking them as free */
2213 target_restore_working_area(target
, c
);
2215 *c
->user
= NULL
; /* Same as above */
2221 /* Run a merge pass to combine all areas into one */
2222 target_merge_working_areas(target
);
2224 print_wa_layout(target
);
2227 void target_free_all_working_areas(struct target
*target
)
2229 target_free_all_working_areas_restore(target
, 1);
2231 /* Now we have none or only one working area marked as free */
2232 if (target
->working_areas
) {
2233 /* Free the last one to allow on-the-fly moving and resizing */
2234 free(target
->working_areas
->backup
);
2235 free(target
->working_areas
);
2236 target
->working_areas
= NULL
;
2240 /* Find the largest number of bytes that can be allocated */
2241 uint32_t target_get_working_area_avail(struct target
*target
)
2243 struct working_area
*c
= target
->working_areas
;
2244 uint32_t max_size
= 0;
2247 return target
->working_area_size
;
2250 if (c
->free
&& max_size
< c
->size
)
2259 static void target_destroy(struct target
*target
)
2261 if (target
->type
->deinit_target
)
2262 target
->type
->deinit_target(target
);
2264 if (target
->semihosting
)
2265 free(target
->semihosting
->basedir
);
2266 free(target
->semihosting
);
2268 jtag_unregister_event_callback(jtag_enable_callback
, target
);
2270 struct target_event_action
*teap
= target
->event_action
;
2272 struct target_event_action
*next
= teap
->next
;
2273 Jim_DecrRefCount(teap
->interp
, teap
->body
);
2278 target_free_all_working_areas(target
);
2280 /* release the targets SMP list */
2282 struct target_list
*head
, *tmp
;
2284 list_for_each_entry_safe(head
, tmp
, target
->smp_targets
, lh
) {
2285 list_del(&head
->lh
);
2286 head
->target
->smp
= 0;
2289 if (target
->smp_targets
!= &empty_smp_targets
)
2290 free(target
->smp_targets
);
2294 rtos_destroy(target
);
2296 free(target
->gdb_port_override
);
2298 free(target
->trace_info
);
2299 free(target
->fileio_info
);
2300 free(target
->cmd_name
);
2304 void target_quit(void)
2306 struct target_event_callback
*pe
= target_event_callbacks
;
2308 struct target_event_callback
*t
= pe
->next
;
2312 target_event_callbacks
= NULL
;
2314 struct target_timer_callback
*pt
= target_timer_callbacks
;
2316 struct target_timer_callback
*t
= pt
->next
;
2320 target_timer_callbacks
= NULL
;
2322 for (struct target
*target
= all_targets
; target
;) {
2326 target_destroy(target
);
2333 int target_arch_state(struct target
*target
)
2337 LOG_WARNING("No target has been configured");
2341 if (target
->state
!= TARGET_HALTED
)
2344 retval
= target
->type
->arch_state(target
);
2348 static int target_get_gdb_fileio_info_default(struct target
*target
,
2349 struct gdb_fileio_info
*fileio_info
)
2351 /* If target does not support semi-hosting function, target
2352 has no need to provide .get_gdb_fileio_info callback.
2353 It just return ERROR_FAIL and gdb_server will return "Txx"
2354 as target halted every time. */
2358 static int target_gdb_fileio_end_default(struct target
*target
,
2359 int retcode
, int fileio_errno
, bool ctrl_c
)
2364 int target_profiling_default(struct target
*target
, uint32_t *samples
,
2365 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
2367 struct timeval timeout
, now
;
2369 gettimeofday(&timeout
, NULL
);
2370 timeval_add_time(&timeout
, seconds
, 0);
2372 LOG_INFO("Starting profiling. Halting and resuming the"
2373 " target as often as we can...");
2375 uint32_t sample_count
= 0;
2376 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2377 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", true);
2379 int retval
= ERROR_OK
;
2381 target_poll(target
);
2382 if (target
->state
== TARGET_HALTED
) {
2383 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
2384 samples
[sample_count
++] = t
;
2385 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2386 retval
= target_resume(target
, 1, 0, 0, 0);
2387 target_poll(target
);
2388 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2389 } else if (target
->state
== TARGET_RUNNING
) {
2390 /* We want to quickly sample the PC. */
2391 retval
= target_halt(target
);
2393 LOG_INFO("Target not halted or running");
2398 if (retval
!= ERROR_OK
)
2401 gettimeofday(&now
, NULL
);
2402 if ((sample_count
>= max_num_samples
) || timeval_compare(&now
, &timeout
) >= 0) {
2403 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2408 *num_samples
= sample_count
;
2412 /* Single aligned words are guaranteed to use 16 or 32 bit access
2413 * mode respectively, otherwise data is handled as quickly as
2416 int target_write_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, const uint8_t *buffer
)
2418 LOG_DEBUG("writing buffer of %" PRIu32
" byte at " TARGET_ADDR_FMT
,
2421 if (!target_was_examined(target
)) {
2422 LOG_ERROR("Target not examined yet");
2429 if ((address
+ size
- 1) < address
) {
2430 /* GDB can request this when e.g. PC is 0xfffffffc */
2431 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2437 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2440 static int target_write_buffer_default(struct target
*target
,
2441 target_addr_t address
, uint32_t count
, const uint8_t *buffer
)
2444 unsigned int data_bytes
= target_data_bits(target
) / 8;
2446 /* Align up to maximum bytes. The loop condition makes sure the next pass
2447 * will have something to do with the size we leave to it. */
2449 size
< data_bytes
&& count
>= size
* 2 + (address
& size
);
2451 if (address
& size
) {
2452 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2453 if (retval
!= ERROR_OK
)
2461 /* Write the data with as large access size as possible. */
2462 for (; size
> 0; size
/= 2) {
2463 uint32_t aligned
= count
- count
% size
;
2465 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2466 if (retval
!= ERROR_OK
)
2477 /* Single aligned words are guaranteed to use 16 or 32 bit access
2478 * mode respectively, otherwise data is handled as quickly as
2481 int target_read_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
2483 LOG_DEBUG("reading buffer of %" PRIu32
" byte at " TARGET_ADDR_FMT
,
2486 if (!target_was_examined(target
)) {
2487 LOG_ERROR("Target not examined yet");
2494 if ((address
+ size
- 1) < address
) {
2495 /* GDB can request this when e.g. PC is 0xfffffffc */
2496 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2502 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2505 static int target_read_buffer_default(struct target
*target
, target_addr_t address
, uint32_t count
, uint8_t *buffer
)
2508 unsigned int data_bytes
= target_data_bits(target
) / 8;
2510 /* Align up to maximum bytes. The loop condition makes sure the next pass
2511 * will have something to do with the size we leave to it. */
2513 size
< data_bytes
&& count
>= size
* 2 + (address
& size
);
2515 if (address
& size
) {
2516 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2517 if (retval
!= ERROR_OK
)
2525 /* Read the data with as large access size as possible. */
2526 for (; size
> 0; size
/= 2) {
2527 uint32_t aligned
= count
- count
% size
;
2529 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2530 if (retval
!= ERROR_OK
)
2541 int target_checksum_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t *crc
)
2546 uint32_t checksum
= 0;
2547 if (!target_was_examined(target
)) {
2548 LOG_ERROR("Target not examined yet");
2551 if (!target
->type
->checksum_memory
) {
2552 LOG_ERROR("Target %s doesn't support checksum_memory", target_name(target
));
2556 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2557 if (retval
!= ERROR_OK
) {
2558 buffer
= malloc(size
);
2560 LOG_ERROR("error allocating buffer for section (%" PRIu32
" bytes)", size
);
2561 return ERROR_COMMAND_SYNTAX_ERROR
;
2563 retval
= target_read_buffer(target
, address
, size
, buffer
);
2564 if (retval
!= ERROR_OK
) {
2569 /* convert to target endianness */
2570 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2571 uint32_t target_data
;
2572 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2573 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2576 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2585 int target_blank_check_memory(struct target
*target
,
2586 struct target_memory_check_block
*blocks
, int num_blocks
,
2587 uint8_t erased_value
)
2589 if (!target_was_examined(target
)) {
2590 LOG_ERROR("Target not examined yet");
2594 if (!target
->type
->blank_check_memory
)
2595 return ERROR_NOT_IMPLEMENTED
;
2597 return target
->type
->blank_check_memory(target
, blocks
, num_blocks
, erased_value
);
2600 int target_read_u64(struct target
*target
, target_addr_t address
, uint64_t *value
)
2602 uint8_t value_buf
[8];
2603 if (!target_was_examined(target
)) {
2604 LOG_ERROR("Target not examined yet");
2608 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2610 if (retval
== ERROR_OK
) {
2611 *value
= target_buffer_get_u64(target
, value_buf
);
2612 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2617 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2624 int target_read_u32(struct target
*target
, target_addr_t address
, uint32_t *value
)
2626 uint8_t value_buf
[4];
2627 if (!target_was_examined(target
)) {
2628 LOG_ERROR("Target not examined yet");
2632 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2634 if (retval
== ERROR_OK
) {
2635 *value
= target_buffer_get_u32(target
, value_buf
);
2636 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2641 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2648 int target_read_u16(struct target
*target
, target_addr_t address
, uint16_t *value
)
2650 uint8_t value_buf
[2];
2651 if (!target_was_examined(target
)) {
2652 LOG_ERROR("Target not examined yet");
2656 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2658 if (retval
== ERROR_OK
) {
2659 *value
= target_buffer_get_u16(target
, value_buf
);
2660 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%4.4" PRIx16
,
2665 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2672 int target_read_u8(struct target
*target
, target_addr_t address
, uint8_t *value
)
2674 if (!target_was_examined(target
)) {
2675 LOG_ERROR("Target not examined yet");
2679 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2681 if (retval
== ERROR_OK
) {
2682 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2687 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2694 int target_write_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2697 uint8_t value_buf
[8];
2698 if (!target_was_examined(target
)) {
2699 LOG_ERROR("Target not examined yet");
2703 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2707 target_buffer_set_u64(target
, value_buf
, value
);
2708 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2709 if (retval
!= ERROR_OK
)
2710 LOG_DEBUG("failed: %i", retval
);
2715 int target_write_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2718 uint8_t value_buf
[4];
2719 if (!target_was_examined(target
)) {
2720 LOG_ERROR("Target not examined yet");
2724 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2728 target_buffer_set_u32(target
, value_buf
, value
);
2729 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2730 if (retval
!= ERROR_OK
)
2731 LOG_DEBUG("failed: %i", retval
);
2736 int target_write_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2739 uint8_t value_buf
[2];
2740 if (!target_was_examined(target
)) {
2741 LOG_ERROR("Target not examined yet");
2745 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2749 target_buffer_set_u16(target
, value_buf
, value
);
2750 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2751 if (retval
!= ERROR_OK
)
2752 LOG_DEBUG("failed: %i", retval
);
2757 int target_write_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2760 if (!target_was_examined(target
)) {
2761 LOG_ERROR("Target not examined yet");
2765 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2768 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2769 if (retval
!= ERROR_OK
)
2770 LOG_DEBUG("failed: %i", retval
);
2775 int target_write_phys_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2778 uint8_t value_buf
[8];
2779 if (!target_was_examined(target
)) {
2780 LOG_ERROR("Target not examined yet");
2784 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2788 target_buffer_set_u64(target
, value_buf
, value
);
2789 retval
= target_write_phys_memory(target
, address
, 8, 1, value_buf
);
2790 if (retval
!= ERROR_OK
)
2791 LOG_DEBUG("failed: %i", retval
);
2796 int target_write_phys_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2799 uint8_t value_buf
[4];
2800 if (!target_was_examined(target
)) {
2801 LOG_ERROR("Target not examined yet");
2805 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2809 target_buffer_set_u32(target
, value_buf
, value
);
2810 retval
= target_write_phys_memory(target
, address
, 4, 1, value_buf
);
2811 if (retval
!= ERROR_OK
)
2812 LOG_DEBUG("failed: %i", retval
);
2817 int target_write_phys_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2820 uint8_t value_buf
[2];
2821 if (!target_was_examined(target
)) {
2822 LOG_ERROR("Target not examined yet");
2826 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2830 target_buffer_set_u16(target
, value_buf
, value
);
2831 retval
= target_write_phys_memory(target
, address
, 2, 1, value_buf
);
2832 if (retval
!= ERROR_OK
)
2833 LOG_DEBUG("failed: %i", retval
);
2838 int target_write_phys_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2841 if (!target_was_examined(target
)) {
2842 LOG_ERROR("Target not examined yet");
2846 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2849 retval
= target_write_phys_memory(target
, address
, 1, 1, &value
);
2850 if (retval
!= ERROR_OK
)
2851 LOG_DEBUG("failed: %i", retval
);
2856 static int find_target(struct command_invocation
*cmd
, const char *name
)
2858 struct target
*target
= get_target(name
);
2860 command_print(cmd
, "Target: %s is unknown, try one of:\n", name
);
2863 if (!target
->tap
->enabled
) {
2864 command_print(cmd
, "Target: TAP %s is disabled, "
2865 "can't be the current target\n",
2866 target
->tap
->dotted_name
);
2870 cmd
->ctx
->current_target
= target
;
2871 if (cmd
->ctx
->current_target_override
)
2872 cmd
->ctx
->current_target_override
= target
;
2878 COMMAND_HANDLER(handle_targets_command
)
2880 int retval
= ERROR_OK
;
2881 if (CMD_ARGC
== 1) {
2882 retval
= find_target(CMD
, CMD_ARGV
[0]);
2883 if (retval
== ERROR_OK
) {
2889 struct target
*target
= all_targets
;
2890 command_print(CMD
, " TargetName Type Endian TapName State ");
2891 command_print(CMD
, "-- ------------------ ---------- ------ ------------------ ------------");
2896 if (target
->tap
->enabled
)
2897 state
= target_state_name(target
);
2899 state
= "tap-disabled";
2901 if (CMD_CTX
->current_target
== target
)
2904 /* keep columns lined up to match the headers above */
2906 "%2d%c %-18s %-10s %-6s %-18s %s",
2907 target
->target_number
,
2909 target_name(target
),
2910 target_type_name(target
),
2911 jim_nvp_value2name_simple(nvp_target_endian
,
2912 target
->endianness
)->name
,
2913 target
->tap
->dotted_name
,
2915 target
= target
->next
;
2921 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2923 static int power_dropout
;
2924 static int srst_asserted
;
2926 static int run_power_restore
;
2927 static int run_power_dropout
;
2928 static int run_srst_asserted
;
2929 static int run_srst_deasserted
;
2931 static int sense_handler(void)
2933 static int prev_srst_asserted
;
2934 static int prev_power_dropout
;
2936 int retval
= jtag_power_dropout(&power_dropout
);
2937 if (retval
!= ERROR_OK
)
2941 power_restored
= prev_power_dropout
&& !power_dropout
;
2943 run_power_restore
= 1;
2945 int64_t current
= timeval_ms();
2946 static int64_t last_power
;
2947 bool wait_more
= last_power
+ 2000 > current
;
2948 if (power_dropout
&& !wait_more
) {
2949 run_power_dropout
= 1;
2950 last_power
= current
;
2953 retval
= jtag_srst_asserted(&srst_asserted
);
2954 if (retval
!= ERROR_OK
)
2957 int srst_deasserted
;
2958 srst_deasserted
= prev_srst_asserted
&& !srst_asserted
;
2960 static int64_t last_srst
;
2961 wait_more
= last_srst
+ 2000 > current
;
2962 if (srst_deasserted
&& !wait_more
) {
2963 run_srst_deasserted
= 1;
2964 last_srst
= current
;
2967 if (!prev_srst_asserted
&& srst_asserted
)
2968 run_srst_asserted
= 1;
2970 prev_srst_asserted
= srst_asserted
;
2971 prev_power_dropout
= power_dropout
;
2973 if (srst_deasserted
|| power_restored
) {
2974 /* Other than logging the event we can't do anything here.
2975 * Issuing a reset is a particularly bad idea as we might
2976 * be inside a reset already.
2983 /* process target state changes */
2984 static int handle_target(void *priv
)
2986 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2987 int retval
= ERROR_OK
;
2989 if (!is_jtag_poll_safe()) {
2990 /* polling is disabled currently */
2994 /* we do not want to recurse here... */
2995 static int recursive
;
2999 /* danger! running these procedures can trigger srst assertions and power dropouts.
3000 * We need to avoid an infinite loop/recursion here and we do that by
3001 * clearing the flags after running these events.
3003 int did_something
= 0;
3004 if (run_srst_asserted
) {
3005 LOG_INFO("srst asserted detected, running srst_asserted proc.");
3006 Jim_Eval(interp
, "srst_asserted");
3009 if (run_srst_deasserted
) {
3010 Jim_Eval(interp
, "srst_deasserted");
3013 if (run_power_dropout
) {
3014 LOG_INFO("Power dropout detected, running power_dropout proc.");
3015 Jim_Eval(interp
, "power_dropout");
3018 if (run_power_restore
) {
3019 Jim_Eval(interp
, "power_restore");
3023 if (did_something
) {
3024 /* clear detect flags */
3028 /* clear action flags */
3030 run_srst_asserted
= 0;
3031 run_srst_deasserted
= 0;
3032 run_power_restore
= 0;
3033 run_power_dropout
= 0;
3038 /* Poll targets for state changes unless that's globally disabled.
3039 * Skip targets that are currently disabled.
3041 for (struct target
*target
= all_targets
;
3042 is_jtag_poll_safe() && target
;
3043 target
= target
->next
) {
3045 if (!target_was_examined(target
))
3048 if (!target
->tap
->enabled
)
3051 if (target
->backoff
.times
> target
->backoff
.count
) {
3052 /* do not poll this time as we failed previously */
3053 target
->backoff
.count
++;
3056 target
->backoff
.count
= 0;
3058 /* only poll target if we've got power and srst isn't asserted */
3059 if (!power_dropout
&& !srst_asserted
) {
3060 /* polling may fail silently until the target has been examined */
3061 retval
= target_poll(target
);
3062 if (retval
!= ERROR_OK
) {
3063 /* 100ms polling interval. Increase interval between polling up to 5000ms */
3064 if (target
->backoff
.times
* polling_interval
< 5000) {
3065 target
->backoff
.times
*= 2;
3066 target
->backoff
.times
++;
3069 /* Tell GDB to halt the debugger. This allows the user to
3070 * run monitor commands to handle the situation.
3072 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
3074 if (target
->backoff
.times
> 0) {
3075 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
3076 target_reset_examined(target
);
3077 retval
= target_examine_one(target
);
3078 /* Target examination could have failed due to unstable connection,
3079 * but we set the examined flag anyway to repoll it later */
3080 if (retval
!= ERROR_OK
) {
3081 target_set_examined(target
);
3082 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
3083 target
->backoff
.times
* polling_interval
);
3088 /* Since we succeeded, we reset backoff count */
3089 target
->backoff
.times
= 0;
3096 COMMAND_HANDLER(handle_reg_command
)
3100 struct target
*target
= get_current_target(CMD_CTX
);
3101 struct reg
*reg
= NULL
;
3103 /* list all available registers for the current target */
3104 if (CMD_ARGC
== 0) {
3105 struct reg_cache
*cache
= target
->reg_cache
;
3107 unsigned int count
= 0;
3111 command_print(CMD
, "===== %s", cache
->name
);
3113 for (i
= 0, reg
= cache
->reg_list
;
3114 i
< cache
->num_regs
;
3115 i
++, reg
++, count
++) {
3116 if (reg
->exist
== false || reg
->hidden
)
3118 /* only print cached values if they are valid */
3120 char *value
= buf_to_hex_str(reg
->value
,
3123 "(%i) %s (/%" PRIu32
"): 0x%s%s",
3131 command_print(CMD
, "(%i) %s (/%" PRIu32
")",
3136 cache
= cache
->next
;
3142 /* access a single register by its ordinal number */
3143 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
3145 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
3147 struct reg_cache
*cache
= target
->reg_cache
;
3148 unsigned int count
= 0;
3151 for (i
= 0; i
< cache
->num_regs
; i
++) {
3152 if (count
++ == num
) {
3153 reg
= &cache
->reg_list
[i
];
3159 cache
= cache
->next
;
3163 command_print(CMD
, "%i is out of bounds, the current target "
3164 "has only %i registers (0 - %i)", num
, count
, count
- 1);
3168 /* access a single register by its name */
3169 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], true);
3175 assert(reg
); /* give clang a hint that we *know* reg is != NULL here */
3180 /* display a register */
3181 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
3182 && (CMD_ARGV
[1][0] <= '9')))) {
3183 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
3186 if (reg
->valid
== 0) {
3187 int retval
= reg
->type
->get(reg
);
3188 if (retval
!= ERROR_OK
) {
3189 LOG_ERROR("Could not read register '%s'", reg
->name
);
3193 char *value
= buf_to_hex_str(reg
->value
, reg
->size
);
3194 command_print(CMD
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
3199 /* set register value */
3200 if (CMD_ARGC
== 2) {
3201 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
3204 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
3206 int retval
= reg
->type
->set(reg
, buf
);
3207 if (retval
!= ERROR_OK
) {
3208 LOG_ERROR("Could not write to register '%s'", reg
->name
);
3210 char *value
= buf_to_hex_str(reg
->value
, reg
->size
);
3211 command_print(CMD
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
3220 return ERROR_COMMAND_SYNTAX_ERROR
;
3223 command_print(CMD
, "register %s not found in current target", CMD_ARGV
[0]);
3227 COMMAND_HANDLER(handle_poll_command
)
3229 int retval
= ERROR_OK
;
3230 struct target
*target
= get_current_target(CMD_CTX
);
3232 if (CMD_ARGC
== 0) {
3233 command_print(CMD
, "background polling: %s",
3234 jtag_poll_get_enabled() ? "on" : "off");
3235 command_print(CMD
, "TAP: %s (%s)",
3236 target
->tap
->dotted_name
,
3237 target
->tap
->enabled
? "enabled" : "disabled");
3238 if (!target
->tap
->enabled
)
3240 retval
= target_poll(target
);
3241 if (retval
!= ERROR_OK
)
3243 retval
= target_arch_state(target
);
3244 if (retval
!= ERROR_OK
)
3246 } else if (CMD_ARGC
== 1) {
3248 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
3249 jtag_poll_set_enabled(enable
);
3251 return ERROR_COMMAND_SYNTAX_ERROR
;
3256 COMMAND_HANDLER(handle_wait_halt_command
)
3259 return ERROR_COMMAND_SYNTAX_ERROR
;
3261 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
3262 if (1 == CMD_ARGC
) {
3263 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
3264 if (retval
!= ERROR_OK
)
3265 return ERROR_COMMAND_SYNTAX_ERROR
;
3268 struct target
*target
= get_current_target(CMD_CTX
);
3269 return target_wait_state(target
, TARGET_HALTED
, ms
);
3272 /* wait for target state to change. The trick here is to have a low
3273 * latency for short waits and not to suck up all the CPU time
3276 * After 500ms, keep_alive() is invoked
3278 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
3281 int64_t then
= 0, cur
;
3285 retval
= target_poll(target
);
3286 if (retval
!= ERROR_OK
)
3288 if (target
->state
== state
)
3293 then
= timeval_ms();
3294 LOG_DEBUG("waiting for target %s...",
3295 jim_nvp_value2name_simple(nvp_target_state
, state
)->name
);
3301 if ((cur
-then
) > ms
) {
3302 LOG_ERROR("timed out while waiting for target %s",
3303 jim_nvp_value2name_simple(nvp_target_state
, state
)->name
);
3311 COMMAND_HANDLER(handle_halt_command
)
3315 struct target
*target
= get_current_target(CMD_CTX
);
3317 target
->verbose_halt_msg
= true;
3319 int retval
= target_halt(target
);
3320 if (retval
!= ERROR_OK
)
3323 if (CMD_ARGC
== 1) {
3324 unsigned wait_local
;
3325 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
3326 if (retval
!= ERROR_OK
)
3327 return ERROR_COMMAND_SYNTAX_ERROR
;
3332 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
3335 COMMAND_HANDLER(handle_soft_reset_halt_command
)
3337 struct target
*target
= get_current_target(CMD_CTX
);
3339 LOG_USER("requesting target halt and executing a soft reset");
3341 target_soft_reset_halt(target
);
3346 COMMAND_HANDLER(handle_reset_command
)
3349 return ERROR_COMMAND_SYNTAX_ERROR
;
3351 enum target_reset_mode reset_mode
= RESET_RUN
;
3352 if (CMD_ARGC
== 1) {
3353 const struct jim_nvp
*n
;
3354 n
= jim_nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
3355 if ((!n
->name
) || (n
->value
== RESET_UNKNOWN
))
3356 return ERROR_COMMAND_SYNTAX_ERROR
;
3357 reset_mode
= n
->value
;
3360 /* reset *all* targets */
3361 return target_process_reset(CMD
, reset_mode
);
3365 COMMAND_HANDLER(handle_resume_command
)
3369 return ERROR_COMMAND_SYNTAX_ERROR
;
3371 struct target
*target
= get_current_target(CMD_CTX
);
3373 /* with no CMD_ARGV, resume from current pc, addr = 0,
3374 * with one arguments, addr = CMD_ARGV[0],
3375 * handle breakpoints, not debugging */
3376 target_addr_t addr
= 0;
3377 if (CMD_ARGC
== 1) {
3378 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3382 return target_resume(target
, current
, addr
, 1, 0);
3385 COMMAND_HANDLER(handle_step_command
)
3388 return ERROR_COMMAND_SYNTAX_ERROR
;
3392 /* with no CMD_ARGV, step from current pc, addr = 0,
3393 * with one argument addr = CMD_ARGV[0],
3394 * handle breakpoints, debugging */
3395 target_addr_t addr
= 0;
3397 if (CMD_ARGC
== 1) {
3398 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3402 struct target
*target
= get_current_target(CMD_CTX
);
3404 return target_step(target
, current_pc
, addr
, 1);
3407 void target_handle_md_output(struct command_invocation
*cmd
,
3408 struct target
*target
, target_addr_t address
, unsigned size
,
3409 unsigned count
, const uint8_t *buffer
)
3411 const unsigned line_bytecnt
= 32;
3412 unsigned line_modulo
= line_bytecnt
/ size
;
3414 char output
[line_bytecnt
* 4 + 1];
3415 unsigned output_len
= 0;
3417 const char *value_fmt
;
3420 value_fmt
= "%16.16"PRIx64
" ";
3423 value_fmt
= "%8.8"PRIx64
" ";
3426 value_fmt
= "%4.4"PRIx64
" ";
3429 value_fmt
= "%2.2"PRIx64
" ";
3432 /* "can't happen", caller checked */
3433 LOG_ERROR("invalid memory read size: %u", size
);
3437 for (unsigned i
= 0; i
< count
; i
++) {
3438 if (i
% line_modulo
== 0) {
3439 output_len
+= snprintf(output
+ output_len
,
3440 sizeof(output
) - output_len
,
3441 TARGET_ADDR_FMT
": ",
3442 (address
+ (i
* size
)));
3446 const uint8_t *value_ptr
= buffer
+ i
* size
;
3449 value
= target_buffer_get_u64(target
, value_ptr
);
3452 value
= target_buffer_get_u32(target
, value_ptr
);
3455 value
= target_buffer_get_u16(target
, value_ptr
);
3460 output_len
+= snprintf(output
+ output_len
,
3461 sizeof(output
) - output_len
,
3464 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
3465 command_print(cmd
, "%s", output
);
3471 COMMAND_HANDLER(handle_md_command
)
3474 return ERROR_COMMAND_SYNTAX_ERROR
;
3477 switch (CMD_NAME
[2]) {
3491 return ERROR_COMMAND_SYNTAX_ERROR
;
3494 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3495 int (*fn
)(struct target
*target
,
3496 target_addr_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
3500 fn
= target_read_phys_memory
;
3502 fn
= target_read_memory
;
3503 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
3504 return ERROR_COMMAND_SYNTAX_ERROR
;
3506 target_addr_t address
;
3507 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3511 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
3513 uint8_t *buffer
= calloc(count
, size
);
3515 LOG_ERROR("Failed to allocate md read buffer");
3519 struct target
*target
= get_current_target(CMD_CTX
);
3520 int retval
= fn(target
, address
, size
, count
, buffer
);
3521 if (retval
== ERROR_OK
)
3522 target_handle_md_output(CMD
, target
, address
, size
, count
, buffer
);
3529 typedef int (*target_write_fn
)(struct target
*target
,
3530 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
3532 static int target_fill_mem(struct target
*target
,
3533 target_addr_t address
,
3541 /* We have to write in reasonably large chunks to be able
3542 * to fill large memory areas with any sane speed */
3543 const unsigned chunk_size
= 16384;
3544 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
3546 LOG_ERROR("Out of memory");
3550 for (unsigned i
= 0; i
< chunk_size
; i
++) {
3551 switch (data_size
) {
3553 target_buffer_set_u64(target
, target_buf
+ i
* data_size
, b
);
3556 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
3559 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
3562 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
3569 int retval
= ERROR_OK
;
3571 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
3574 if (current
> chunk_size
)
3575 current
= chunk_size
;
3576 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
3577 if (retval
!= ERROR_OK
)
3579 /* avoid GDB timeouts */
3588 COMMAND_HANDLER(handle_mw_command
)
3591 return ERROR_COMMAND_SYNTAX_ERROR
;
3592 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3597 fn
= target_write_phys_memory
;
3599 fn
= target_write_memory
;
3600 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
3601 return ERROR_COMMAND_SYNTAX_ERROR
;
3603 target_addr_t address
;
3604 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3607 COMMAND_PARSE_NUMBER(u64
, CMD_ARGV
[1], value
);
3611 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
3613 struct target
*target
= get_current_target(CMD_CTX
);
3615 switch (CMD_NAME
[2]) {
3629 return ERROR_COMMAND_SYNTAX_ERROR
;
3632 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
3635 static COMMAND_HELPER(parse_load_image_command
, struct image
*image
,
3636 target_addr_t
*min_address
, target_addr_t
*max_address
)
3638 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
3639 return ERROR_COMMAND_SYNTAX_ERROR
;
3641 /* a base address isn't always necessary,
3642 * default to 0x0 (i.e. don't relocate) */
3643 if (CMD_ARGC
>= 2) {
3645 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3646 image
->base_address
= addr
;
3647 image
->base_address_set
= true;
3649 image
->base_address_set
= false;
3651 image
->start_address_set
= false;
3654 COMMAND_PARSE_ADDRESS(CMD_ARGV
[3], *min_address
);
3655 if (CMD_ARGC
== 5) {
3656 COMMAND_PARSE_ADDRESS(CMD_ARGV
[4], *max_address
);
3657 /* use size (given) to find max (required) */
3658 *max_address
+= *min_address
;
3661 if (*min_address
> *max_address
)
3662 return ERROR_COMMAND_SYNTAX_ERROR
;
3667 COMMAND_HANDLER(handle_load_image_command
)
3671 uint32_t image_size
;
3672 target_addr_t min_address
= 0;
3673 target_addr_t max_address
= -1;
3676 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command
,
3677 &image
, &min_address
, &max_address
);
3678 if (retval
!= ERROR_OK
)
3681 struct target
*target
= get_current_target(CMD_CTX
);
3683 struct duration bench
;
3684 duration_start(&bench
);
3686 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
3691 for (unsigned int i
= 0; i
< image
.num_sections
; i
++) {
3692 buffer
= malloc(image
.sections
[i
].size
);
3695 "error allocating buffer for section (%d bytes)",
3696 (int)(image
.sections
[i
].size
));
3697 retval
= ERROR_FAIL
;
3701 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3702 if (retval
!= ERROR_OK
) {
3707 uint32_t offset
= 0;
3708 uint32_t length
= buf_cnt
;
3710 /* DANGER!!! beware of unsigned comparison here!!! */
3712 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3713 (image
.sections
[i
].base_address
< max_address
)) {
3715 if (image
.sections
[i
].base_address
< min_address
) {
3716 /* clip addresses below */
3717 offset
+= min_address
-image
.sections
[i
].base_address
;
3721 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3722 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3724 retval
= target_write_buffer(target
,
3725 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3726 if (retval
!= ERROR_OK
) {
3730 image_size
+= length
;
3731 command_print(CMD
, "%u bytes written at address " TARGET_ADDR_FMT
"",
3732 (unsigned int)length
,
3733 image
.sections
[i
].base_address
+ offset
);
3739 if ((retval
== ERROR_OK
) && (duration_measure(&bench
) == ERROR_OK
)) {
3740 command_print(CMD
, "downloaded %" PRIu32
" bytes "
3741 "in %fs (%0.3f KiB/s)", image_size
,
3742 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3745 image_close(&image
);
3751 COMMAND_HANDLER(handle_dump_image_command
)
3753 struct fileio
*fileio
;
3755 int retval
, retvaltemp
;
3756 target_addr_t address
, size
;
3757 struct duration bench
;
3758 struct target
*target
= get_current_target(CMD_CTX
);
3761 return ERROR_COMMAND_SYNTAX_ERROR
;
3763 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], address
);
3764 COMMAND_PARSE_ADDRESS(CMD_ARGV
[2], size
);
3766 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
3767 buffer
= malloc(buf_size
);
3771 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3772 if (retval
!= ERROR_OK
) {
3777 duration_start(&bench
);
3780 size_t size_written
;
3781 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
3782 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
3783 if (retval
!= ERROR_OK
)
3786 retval
= fileio_write(fileio
, this_run_size
, buffer
, &size_written
);
3787 if (retval
!= ERROR_OK
)
3790 size
-= this_run_size
;
3791 address
+= this_run_size
;
3796 if ((retval
== ERROR_OK
) && (duration_measure(&bench
) == ERROR_OK
)) {
3798 retval
= fileio_size(fileio
, &filesize
);
3799 if (retval
!= ERROR_OK
)
3802 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize
,
3803 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3806 retvaltemp
= fileio_close(fileio
);
3807 if (retvaltemp
!= ERROR_OK
)
3816 IMAGE_CHECKSUM_ONLY
= 2
3819 static COMMAND_HELPER(handle_verify_image_command_internal
, enum verify_mode verify
)
3823 uint32_t image_size
;
3825 uint32_t checksum
= 0;
3826 uint32_t mem_checksum
= 0;
3830 struct target
*target
= get_current_target(CMD_CTX
);
3833 return ERROR_COMMAND_SYNTAX_ERROR
;
3836 LOG_ERROR("no target selected");
3840 struct duration bench
;
3841 duration_start(&bench
);
3843 if (CMD_ARGC
>= 2) {
3845 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3846 image
.base_address
= addr
;
3847 image
.base_address_set
= true;
3849 image
.base_address_set
= false;
3850 image
.base_address
= 0x0;
3853 image
.start_address_set
= false;
3855 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3856 if (retval
!= ERROR_OK
)
3862 for (unsigned int i
= 0; i
< image
.num_sections
; i
++) {
3863 buffer
= malloc(image
.sections
[i
].size
);
3866 "error allocating buffer for section (%" PRIu32
" bytes)",
3867 image
.sections
[i
].size
);
3870 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3871 if (retval
!= ERROR_OK
) {
3876 if (verify
>= IMAGE_VERIFY
) {
3877 /* calculate checksum of image */
3878 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3879 if (retval
!= ERROR_OK
) {
3884 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3885 if (retval
!= ERROR_OK
) {
3889 if ((checksum
!= mem_checksum
) && (verify
== IMAGE_CHECKSUM_ONLY
)) {
3890 LOG_ERROR("checksum mismatch");
3892 retval
= ERROR_FAIL
;
3895 if (checksum
!= mem_checksum
) {
3896 /* failed crc checksum, fall back to a binary compare */
3900 LOG_ERROR("checksum mismatch - attempting binary compare");
3902 data
= malloc(buf_cnt
);
3904 retval
= target_read_buffer(target
, image
.sections
[i
].base_address
, buf_cnt
, data
);
3905 if (retval
== ERROR_OK
) {
3907 for (t
= 0; t
< buf_cnt
; t
++) {
3908 if (data
[t
] != buffer
[t
]) {
3910 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3912 (unsigned)(t
+ image
.sections
[i
].base_address
),
3915 if (diffs
++ >= 127) {
3916 command_print(CMD
, "More than 128 errors, the rest are not printed.");
3928 command_print(CMD
, "address " TARGET_ADDR_FMT
" length 0x%08zx",
3929 image
.sections
[i
].base_address
,
3934 image_size
+= buf_cnt
;
3937 command_print(CMD
, "No more differences found.");
3940 retval
= ERROR_FAIL
;
3941 if ((retval
== ERROR_OK
) && (duration_measure(&bench
) == ERROR_OK
)) {
3942 command_print(CMD
, "verified %" PRIu32
" bytes "
3943 "in %fs (%0.3f KiB/s)", image_size
,
3944 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3947 image_close(&image
);
3952 COMMAND_HANDLER(handle_verify_image_checksum_command
)
3954 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_CHECKSUM_ONLY
);
3957 COMMAND_HANDLER(handle_verify_image_command
)
3959 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_VERIFY
);
3962 COMMAND_HANDLER(handle_test_image_command
)
3964 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_TEST
);
3967 static int handle_bp_command_list(struct command_invocation
*cmd
)
3969 struct target
*target
= get_current_target(cmd
->ctx
);
3970 struct breakpoint
*breakpoint
= target
->breakpoints
;
3971 while (breakpoint
) {
3972 if (breakpoint
->type
== BKPT_SOFT
) {
3973 char *buf
= buf_to_hex_str(breakpoint
->orig_instr
,
3974 breakpoint
->length
);
3975 command_print(cmd
, "IVA breakpoint: " TARGET_ADDR_FMT
", 0x%x, 0x%s",
3976 breakpoint
->address
,
3981 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3982 command_print(cmd
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %u",
3984 breakpoint
->length
, breakpoint
->number
);
3985 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3986 command_print(cmd
, "Hybrid breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %u",
3987 breakpoint
->address
,
3988 breakpoint
->length
, breakpoint
->number
);
3989 command_print(cmd
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3992 command_print(cmd
, "Breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %u",
3993 breakpoint
->address
,
3994 breakpoint
->length
, breakpoint
->number
);
3997 breakpoint
= breakpoint
->next
;
4002 static int handle_bp_command_set(struct command_invocation
*cmd
,
4003 target_addr_t addr
, uint32_t asid
, uint32_t length
, int hw
)
4005 struct target
*target
= get_current_target(cmd
->ctx
);
4009 retval
= breakpoint_add(target
, addr
, length
, hw
);
4010 /* error is always logged in breakpoint_add(), do not print it again */
4011 if (retval
== ERROR_OK
)
4012 command_print(cmd
, "breakpoint set at " TARGET_ADDR_FMT
"", addr
);
4014 } else if (addr
== 0) {
4015 if (!target
->type
->add_context_breakpoint
) {
4016 LOG_ERROR("Context breakpoint not available");
4017 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
4019 retval
= context_breakpoint_add(target
, asid
, length
, hw
);
4020 /* error is always logged in context_breakpoint_add(), do not print it again */
4021 if (retval
== ERROR_OK
)
4022 command_print(cmd
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
4025 if (!target
->type
->add_hybrid_breakpoint
) {
4026 LOG_ERROR("Hybrid breakpoint not available");
4027 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
4029 retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
4030 /* error is always logged in hybrid_breakpoint_add(), do not print it again */
4031 if (retval
== ERROR_OK
)
4032 command_print(cmd
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
4037 COMMAND_HANDLER(handle_bp_command
)
4046 return handle_bp_command_list(CMD
);
4050 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4051 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
4052 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
4055 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
4057 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4058 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
4060 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
4061 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
4063 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
4064 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
4066 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
4071 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4072 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
4073 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
4074 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
4077 return ERROR_COMMAND_SYNTAX_ERROR
;
4081 COMMAND_HANDLER(handle_rbp_command
)
4084 return ERROR_COMMAND_SYNTAX_ERROR
;
4086 struct target
*target
= get_current_target(CMD_CTX
);
4088 if (!strcmp(CMD_ARGV
[0], "all")) {
4089 breakpoint_remove_all(target
);
4092 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4094 breakpoint_remove(target
, addr
);
4100 COMMAND_HANDLER(handle_wp_command
)
4102 struct target
*target
= get_current_target(CMD_CTX
);
4104 if (CMD_ARGC
== 0) {
4105 struct watchpoint
*watchpoint
= target
->watchpoints
;
4107 while (watchpoint
) {
4108 command_print(CMD
, "address: " TARGET_ADDR_FMT
4109 ", len: 0x%8.8" PRIx32
4110 ", r/w/a: %i, value: 0x%8.8" PRIx32
4111 ", mask: 0x%8.8" PRIx32
,
4112 watchpoint
->address
,
4114 (int)watchpoint
->rw
,
4117 watchpoint
= watchpoint
->next
;
4122 enum watchpoint_rw type
= WPT_ACCESS
;
4123 target_addr_t addr
= 0;
4124 uint32_t length
= 0;
4125 uint32_t data_value
= 0x0;
4126 uint32_t data_mask
= 0xffffffff;
4130 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
4133 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
4136 switch (CMD_ARGV
[2][0]) {
4147 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
4148 return ERROR_COMMAND_SYNTAX_ERROR
;
4152 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
4153 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4157 return ERROR_COMMAND_SYNTAX_ERROR
;
4160 int retval
= watchpoint_add(target
, addr
, length
, type
,
4161 data_value
, data_mask
);
4162 if (retval
!= ERROR_OK
)
4163 LOG_ERROR("Failure setting watchpoints");
4168 COMMAND_HANDLER(handle_rwp_command
)
4171 return ERROR_COMMAND_SYNTAX_ERROR
;
4174 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4176 struct target
*target
= get_current_target(CMD_CTX
);
4177 watchpoint_remove(target
, addr
);
4183 * Translate a virtual address to a physical address.
4185 * The low-level target implementation must have logged a detailed error
4186 * which is forwarded to telnet/GDB session.
4188 COMMAND_HANDLER(handle_virt2phys_command
)
4191 return ERROR_COMMAND_SYNTAX_ERROR
;
4194 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], va
);
4197 struct target
*target
= get_current_target(CMD_CTX
);
4198 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
4199 if (retval
== ERROR_OK
)
4200 command_print(CMD
, "Physical address " TARGET_ADDR_FMT
"", pa
);
4205 static void write_data(FILE *f
, const void *data
, size_t len
)
4207 size_t written
= fwrite(data
, 1, len
, f
);
4209 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
4212 static void write_long(FILE *f
, int l
, struct target
*target
)
4216 target_buffer_set_u32(target
, val
, l
);
4217 write_data(f
, val
, 4);
4220 static void write_string(FILE *f
, char *s
)
4222 write_data(f
, s
, strlen(s
));
4225 typedef unsigned char UNIT
[2]; /* unit of profiling */
4227 /* Dump a gmon.out histogram file. */
4228 static void write_gmon(uint32_t *samples
, uint32_t sample_num
, const char *filename
, bool with_range
,
4229 uint32_t start_address
, uint32_t end_address
, struct target
*target
, uint32_t duration_ms
)
4232 FILE *f
= fopen(filename
, "w");
4235 write_string(f
, "gmon");
4236 write_long(f
, 0x00000001, target
); /* Version */
4237 write_long(f
, 0, target
); /* padding */
4238 write_long(f
, 0, target
); /* padding */
4239 write_long(f
, 0, target
); /* padding */
4241 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
4242 write_data(f
, &zero
, 1);
4244 /* figure out bucket size */
4248 min
= start_address
;
4253 for (i
= 0; i
< sample_num
; i
++) {
4254 if (min
> samples
[i
])
4256 if (max
< samples
[i
])
4260 /* max should be (largest sample + 1)
4261 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
4265 int address_space
= max
- min
;
4266 assert(address_space
>= 2);
4268 /* FIXME: What is the reasonable number of buckets?
4269 * The profiling result will be more accurate if there are enough buckets. */
4270 static const uint32_t max_buckets
= 128 * 1024; /* maximum buckets. */
4271 uint32_t num_buckets
= address_space
/ sizeof(UNIT
);
4272 if (num_buckets
> max_buckets
)
4273 num_buckets
= max_buckets
;
4274 int *buckets
= malloc(sizeof(int) * num_buckets
);
4279 memset(buckets
, 0, sizeof(int) * num_buckets
);
4280 for (i
= 0; i
< sample_num
; i
++) {
4281 uint32_t address
= samples
[i
];
4283 if ((address
< min
) || (max
<= address
))
4286 long long a
= address
- min
;
4287 long long b
= num_buckets
;
4288 long long c
= address_space
;
4289 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
4293 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
4294 write_long(f
, min
, target
); /* low_pc */
4295 write_long(f
, max
, target
); /* high_pc */
4296 write_long(f
, num_buckets
, target
); /* # of buckets */
4297 float sample_rate
= sample_num
/ (duration_ms
/ 1000.0);
4298 write_long(f
, sample_rate
, target
);
4299 write_string(f
, "seconds");
4300 for (i
= 0; i
< (15-strlen("seconds")); i
++)
4301 write_data(f
, &zero
, 1);
4302 write_string(f
, "s");
4304 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
4306 char *data
= malloc(2 * num_buckets
);
4308 for (i
= 0; i
< num_buckets
; i
++) {
4313 data
[i
* 2] = val
&0xff;
4314 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
4317 write_data(f
, data
, num_buckets
* 2);
4325 /* profiling samples the CPU PC as quickly as OpenOCD is able,
4326 * which will be used as a random sampling of PC */
4327 COMMAND_HANDLER(handle_profile_command
)
4329 struct target
*target
= get_current_target(CMD_CTX
);
4331 if ((CMD_ARGC
!= 2) && (CMD_ARGC
!= 4))
4332 return ERROR_COMMAND_SYNTAX_ERROR
;
4334 const uint32_t MAX_PROFILE_SAMPLE_NUM
= 10000;
4336 uint32_t num_of_samples
;
4337 int retval
= ERROR_OK
;
4338 bool halted_before_profiling
= target
->state
== TARGET_HALTED
;
4340 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], offset
);
4342 uint32_t *samples
= malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM
);
4344 LOG_ERROR("No memory to store samples.");
4348 uint64_t timestart_ms
= timeval_ms();
4350 * Some cores let us sample the PC without the
4351 * annoying halt/resume step; for example, ARMv7 PCSR.
4352 * Provide a way to use that more efficient mechanism.
4354 retval
= target_profiling(target
, samples
, MAX_PROFILE_SAMPLE_NUM
,
4355 &num_of_samples
, offset
);
4356 if (retval
!= ERROR_OK
) {
4360 uint32_t duration_ms
= timeval_ms() - timestart_ms
;
4362 assert(num_of_samples
<= MAX_PROFILE_SAMPLE_NUM
);
4364 retval
= target_poll(target
);
4365 if (retval
!= ERROR_OK
) {
4370 if (target
->state
== TARGET_RUNNING
&& halted_before_profiling
) {
4371 /* The target was halted before we started and is running now. Halt it,
4372 * for consistency. */
4373 retval
= target_halt(target
);
4374 if (retval
!= ERROR_OK
) {
4378 } else if (target
->state
== TARGET_HALTED
&& !halted_before_profiling
) {
4379 /* The target was running before we started and is halted now. Resume
4380 * it, for consistency. */
4381 retval
= target_resume(target
, 1, 0, 0, 0);
4382 if (retval
!= ERROR_OK
) {
4388 retval
= target_poll(target
);
4389 if (retval
!= ERROR_OK
) {
4394 uint32_t start_address
= 0;
4395 uint32_t end_address
= 0;
4396 bool with_range
= false;
4397 if (CMD_ARGC
== 4) {
4399 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], start_address
);
4400 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], end_address
);
4403 write_gmon(samples
, num_of_samples
, CMD_ARGV
[1],
4404 with_range
, start_address
, end_address
, target
, duration_ms
);
4405 command_print(CMD
, "Wrote %s", CMD_ARGV
[1]);
4411 static int new_u64_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint64_t val
)
4414 Jim_Obj
*obj_name
, *obj_val
;
4417 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4421 obj_name
= Jim_NewStringObj(interp
, namebuf
, -1);
4422 jim_wide wide_val
= val
;
4423 obj_val
= Jim_NewWideObj(interp
, wide_val
);
4424 if (!obj_name
|| !obj_val
) {
4429 Jim_IncrRefCount(obj_name
);
4430 Jim_IncrRefCount(obj_val
);
4431 result
= Jim_SetVariable(interp
, obj_name
, obj_val
);
4432 Jim_DecrRefCount(interp
, obj_name
);
4433 Jim_DecrRefCount(interp
, obj_val
);
4435 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
4439 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
4443 LOG_WARNING("DEPRECATED! use 'read_memory' not 'mem2array'");
4445 /* argv[0] = name of array to receive the data
4446 * argv[1] = desired element width in bits
4447 * argv[2] = memory address
4448 * argv[3] = count of times to read
4449 * argv[4] = optional "phys"
4451 if (argc
< 4 || argc
> 5) {
4452 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4456 /* Arg 0: Name of the array variable */
4457 const char *varname
= Jim_GetString(argv
[0], NULL
);
4459 /* Arg 1: Bit width of one element */
4461 e
= Jim_GetLong(interp
, argv
[1], &l
);
4464 const unsigned int width_bits
= l
;
4466 if (width_bits
!= 8 &&
4470 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4471 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4472 "Invalid width param. Must be one of: 8, 16, 32 or 64.", NULL
);
4475 const unsigned int width
= width_bits
/ 8;
4477 /* Arg 2: Memory address */
4479 e
= Jim_GetWide(interp
, argv
[2], &wide_addr
);
4482 target_addr_t addr
= (target_addr_t
)wide_addr
;
4484 /* Arg 3: Number of elements to read */
4485 e
= Jim_GetLong(interp
, argv
[3], &l
);
4491 bool is_phys
= false;
4494 const char *phys
= Jim_GetString(argv
[4], &str_len
);
4495 if (!strncmp(phys
, "phys", str_len
))
4501 /* Argument checks */
4503 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4504 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
4507 if ((addr
+ (len
* width
)) < addr
) {
4508 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4509 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
4513 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4514 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4515 "mem2array: too large read request, exceeds 64K items", NULL
);
4520 ((width
== 2) && ((addr
& 1) == 0)) ||
4521 ((width
== 4) && ((addr
& 3) == 0)) ||
4522 ((width
== 8) && ((addr
& 7) == 0))) {
4523 /* alignment correct */
4526 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4527 sprintf(buf
, "mem2array address: " TARGET_ADDR_FMT
" is not aligned for %" PRIu32
" byte reads",
4530 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4539 const size_t buffersize
= 4096;
4540 uint8_t *buffer
= malloc(buffersize
);
4547 /* Slurp... in buffer size chunks */
4548 const unsigned int max_chunk_len
= buffersize
/ width
;
4549 const size_t chunk_len
= MIN(len
, max_chunk_len
); /* in elements.. */
4553 retval
= target_read_phys_memory(target
, addr
, width
, chunk_len
, buffer
);
4555 retval
= target_read_memory(target
, addr
, width
, chunk_len
, buffer
);
4556 if (retval
!= ERROR_OK
) {
4558 LOG_ERROR("mem2array: Read @ " TARGET_ADDR_FMT
", w=%u, cnt=%zu, failed",
4562 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4563 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
4567 for (size_t i
= 0; i
< chunk_len
; i
++, idx
++) {
4571 v
= target_buffer_get_u64(target
, &buffer
[i
*width
]);
4574 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
4577 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
4580 v
= buffer
[i
] & 0x0ff;
4583 new_u64_array_element(interp
, varname
, idx
, v
);
4586 addr
+= chunk_len
* width
;
4592 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4597 static int target_jim_read_memory(Jim_Interp
*interp
, int argc
,
4598 Jim_Obj
* const *argv
)
4601 * argv[1] = memory address
4602 * argv[2] = desired element width in bits
4603 * argv[3] = number of elements to read
4604 * argv[4] = optional "phys"
4607 if (argc
< 4 || argc
> 5) {
4608 Jim_WrongNumArgs(interp
, 1, argv
, "address width count ['phys']");
4612 /* Arg 1: Memory address. */
4615 e
= Jim_GetWide(interp
, argv
[1], &wide_addr
);
4620 target_addr_t addr
= (target_addr_t
)wide_addr
;
4622 /* Arg 2: Bit width of one element. */
4624 e
= Jim_GetLong(interp
, argv
[2], &l
);
4629 const unsigned int width_bits
= l
;
4631 /* Arg 3: Number of elements to read. */
4632 e
= Jim_GetLong(interp
, argv
[3], &l
);
4639 /* Arg 4: Optional 'phys'. */
4640 bool is_phys
= false;
4643 const char *phys
= Jim_GetString(argv
[4], NULL
);
4645 if (strcmp(phys
, "phys")) {
4646 Jim_SetResultFormatted(interp
, "invalid argument '%s', must be 'phys'", phys
);
4653 switch (width_bits
) {
4660 Jim_SetResultString(interp
, "invalid width, must be 8, 16, 32 or 64", -1);
4664 const unsigned int width
= width_bits
/ 8;
4666 if ((addr
+ (count
* width
)) < addr
) {
4667 Jim_SetResultString(interp
, "read_memory: addr + count wraps to zero", -1);
4671 if (count
> 65536) {
4672 Jim_SetResultString(interp
, "read_memory: too large read request, exeeds 64K elements", -1);
4676 struct command_context
*cmd_ctx
= current_command_context(interp
);
4677 assert(cmd_ctx
!= NULL
);
4678 struct target
*target
= get_current_target(cmd_ctx
);
4680 const size_t buffersize
= 4096;
4681 uint8_t *buffer
= malloc(buffersize
);
4684 LOG_ERROR("Failed to allocate memory");
4688 Jim_Obj
*result_list
= Jim_NewListObj(interp
, NULL
, 0);
4689 Jim_IncrRefCount(result_list
);
4692 const unsigned int max_chunk_len
= buffersize
/ width
;
4693 const size_t chunk_len
= MIN(count
, max_chunk_len
);
4698 retval
= target_read_phys_memory(target
, addr
, width
, chunk_len
, buffer
);
4700 retval
= target_read_memory(target
, addr
, width
, chunk_len
, buffer
);
4702 if (retval
!= ERROR_OK
) {
4703 LOG_ERROR("read_memory: read at " TARGET_ADDR_FMT
" with width=%u and count=%zu failed",
4704 addr
, width_bits
, chunk_len
);
4705 Jim_SetResultString(interp
, "read_memory: failed to read memory", -1);
4710 for (size_t i
= 0; i
< chunk_len
; i
++) {
4715 v
= target_buffer_get_u64(target
, &buffer
[i
* width
]);
4718 v
= target_buffer_get_u32(target
, &buffer
[i
* width
]);
4721 v
= target_buffer_get_u16(target
, &buffer
[i
* width
]);
4729 snprintf(value_buf
, sizeof(value_buf
), "0x%" PRIx64
, v
);
4731 Jim_ListAppendElement(interp
, result_list
,
4732 Jim_NewStringObj(interp
, value_buf
, -1));
4736 addr
+= chunk_len
* width
;
4742 Jim_DecrRefCount(interp
, result_list
);
4746 Jim_SetResult(interp
, result_list
);
4747 Jim_DecrRefCount(interp
, result_list
);
4752 static int get_u64_array_element(Jim_Interp
*interp
, const char *varname
, size_t idx
, uint64_t *val
)
4754 char *namebuf
= alloc_printf("%s(%zu)", varname
, idx
);
4758 Jim_Obj
*obj_name
= Jim_NewStringObj(interp
, namebuf
, -1);
4764 Jim_IncrRefCount(obj_name
);
4765 Jim_Obj
*obj_val
= Jim_GetVariable(interp
, obj_name
, JIM_ERRMSG
);
4766 Jim_DecrRefCount(interp
, obj_name
);
4772 int result
= Jim_GetWide(interp
, obj_val
, &wide_val
);
4777 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
4778 int argc
, Jim_Obj
*const *argv
)
4782 LOG_WARNING("DEPRECATED! use 'write_memory' not 'array2mem'");
4784 /* argv[0] = name of array from which to read the data
4785 * argv[1] = desired element width in bits
4786 * argv[2] = memory address
4787 * argv[3] = number of elements to write
4788 * argv[4] = optional "phys"
4790 if (argc
< 4 || argc
> 5) {
4791 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4795 /* Arg 0: Name of the array variable */
4796 const char *varname
= Jim_GetString(argv
[0], NULL
);
4798 /* Arg 1: Bit width of one element */
4800 e
= Jim_GetLong(interp
, argv
[1], &l
);
4803 const unsigned int width_bits
= l
;
4805 if (width_bits
!= 8 &&
4809 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4810 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4811 "Invalid width param. Must be one of: 8, 16, 32 or 64.", NULL
);
4814 const unsigned int width
= width_bits
/ 8;
4816 /* Arg 2: Memory address */
4818 e
= Jim_GetWide(interp
, argv
[2], &wide_addr
);
4821 target_addr_t addr
= (target_addr_t
)wide_addr
;
4823 /* Arg 3: Number of elements to write */
4824 e
= Jim_GetLong(interp
, argv
[3], &l
);
4830 bool is_phys
= false;
4833 const char *phys
= Jim_GetString(argv
[4], &str_len
);
4834 if (!strncmp(phys
, "phys", str_len
))
4840 /* Argument checks */
4842 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4843 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4844 "array2mem: zero width read?", NULL
);
4848 if ((addr
+ (len
* width
)) < addr
) {
4849 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4850 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4851 "array2mem: addr + len - wraps to zero?", NULL
);
4856 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4857 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4858 "array2mem: too large memory write request, exceeds 64K items", NULL
);
4863 ((width
== 2) && ((addr
& 1) == 0)) ||
4864 ((width
== 4) && ((addr
& 3) == 0)) ||
4865 ((width
== 8) && ((addr
& 7) == 0))) {
4866 /* alignment correct */
4869 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4870 sprintf(buf
, "array2mem address: " TARGET_ADDR_FMT
" is not aligned for %" PRIu32
" byte reads",
4873 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4882 const size_t buffersize
= 4096;
4883 uint8_t *buffer
= malloc(buffersize
);
4891 /* Slurp... in buffer size chunks */
4892 const unsigned int max_chunk_len
= buffersize
/ width
;
4894 const size_t chunk_len
= MIN(len
, max_chunk_len
); /* in elements.. */
4896 /* Fill the buffer */
4897 for (size_t i
= 0; i
< chunk_len
; i
++, idx
++) {
4899 if (get_u64_array_element(interp
, varname
, idx
, &v
) != JIM_OK
) {
4905 target_buffer_set_u64(target
, &buffer
[i
* width
], v
);
4908 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
4911 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
4914 buffer
[i
] = v
& 0x0ff;
4920 /* Write the buffer to memory */
4923 retval
= target_write_phys_memory(target
, addr
, width
, chunk_len
, buffer
);
4925 retval
= target_write_memory(target
, addr
, width
, chunk_len
, buffer
);
4926 if (retval
!= ERROR_OK
) {
4928 LOG_ERROR("array2mem: Write @ " TARGET_ADDR_FMT
", w=%u, cnt=%zu, failed",
4932 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4933 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
4937 addr
+= chunk_len
* width
;
4942 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4947 static int target_jim_write_memory(Jim_Interp
*interp
, int argc
,
4948 Jim_Obj
* const *argv
)
4951 * argv[1] = memory address
4952 * argv[2] = desired element width in bits
4953 * argv[3] = list of data to write
4954 * argv[4] = optional "phys"
4957 if (argc
< 4 || argc
> 5) {
4958 Jim_WrongNumArgs(interp
, 1, argv
, "address width data ['phys']");
4962 /* Arg 1: Memory address. */
4965 e
= Jim_GetWide(interp
, argv
[1], &wide_addr
);
4970 target_addr_t addr
= (target_addr_t
)wide_addr
;
4972 /* Arg 2: Bit width of one element. */
4974 e
= Jim_GetLong(interp
, argv
[2], &l
);
4979 const unsigned int width_bits
= l
;
4980 size_t count
= Jim_ListLength(interp
, argv
[3]);
4982 /* Arg 4: Optional 'phys'. */
4983 bool is_phys
= false;
4986 const char *phys
= Jim_GetString(argv
[4], NULL
);
4988 if (strcmp(phys
, "phys")) {
4989 Jim_SetResultFormatted(interp
, "invalid argument '%s', must be 'phys'", phys
);
4996 switch (width_bits
) {
5003 Jim_SetResultString(interp
, "invalid width, must be 8, 16, 32 or 64", -1);
5007 const unsigned int width
= width_bits
/ 8;
5009 if ((addr
+ (count
* width
)) < addr
) {
5010 Jim_SetResultString(interp
, "write_memory: addr + len wraps to zero", -1);
5014 if (count
> 65536) {
5015 Jim_SetResultString(interp
, "write_memory: too large memory write request, exceeds 64K elements", -1);
5019 struct command_context
*cmd_ctx
= current_command_context(interp
);
5020 assert(cmd_ctx
!= NULL
);
5021 struct target
*target
= get_current_target(cmd_ctx
);
5023 const size_t buffersize
= 4096;
5024 uint8_t *buffer
= malloc(buffersize
);
5027 LOG_ERROR("Failed to allocate memory");
5034 const unsigned int max_chunk_len
= buffersize
/ width
;
5035 const size_t chunk_len
= MIN(count
, max_chunk_len
);
5037 for (size_t i
= 0; i
< chunk_len
; i
++, j
++) {
5038 Jim_Obj
*tmp
= Jim_ListGetIndex(interp
, argv
[3], j
);
5039 jim_wide element_wide
;
5040 Jim_GetWide(interp
, tmp
, &element_wide
);
5042 const uint64_t v
= element_wide
;
5046 target_buffer_set_u64(target
, &buffer
[i
* width
], v
);
5049 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
5052 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
5055 buffer
[i
] = v
& 0x0ff;
5065 retval
= target_write_phys_memory(target
, addr
, width
, chunk_len
, buffer
);
5067 retval
= target_write_memory(target
, addr
, width
, chunk_len
, buffer
);
5069 if (retval
!= ERROR_OK
) {
5070 LOG_ERROR("write_memory: write at " TARGET_ADDR_FMT
" with width=%u and count=%zu failed",
5071 addr
, width_bits
, chunk_len
);
5072 Jim_SetResultString(interp
, "write_memory: failed to write memory", -1);
5077 addr
+= chunk_len
* width
;
5085 /* FIX? should we propagate errors here rather than printing them
5088 void target_handle_event(struct target
*target
, enum target_event e
)
5090 struct target_event_action
*teap
;
5093 for (teap
= target
->event_action
; teap
; teap
= teap
->next
) {
5094 if (teap
->event
== e
) {
5095 LOG_DEBUG("target(%d): %s (%s) event: %d (%s) action: %s",
5096 target
->target_number
,
5097 target_name(target
),
5098 target_type_name(target
),
5100 target_event_name(e
),
5101 Jim_GetString(teap
->body
, NULL
));
5103 /* Override current target by the target an event
5104 * is issued from (lot of scripts need it).
5105 * Return back to previous override as soon
5106 * as the handler processing is done */
5107 struct command_context
*cmd_ctx
= current_command_context(teap
->interp
);
5108 struct target
*saved_target_override
= cmd_ctx
->current_target_override
;
5109 cmd_ctx
->current_target_override
= target
;
5111 retval
= Jim_EvalObj(teap
->interp
, teap
->body
);
5113 cmd_ctx
->current_target_override
= saved_target_override
;
5115 if (retval
== ERROR_COMMAND_CLOSE_CONNECTION
)
5118 if (retval
== JIM_RETURN
)
5119 retval
= teap
->interp
->returnCode
;
5121 if (retval
!= JIM_OK
) {
5122 Jim_MakeErrorMessage(teap
->interp
);
5123 LOG_USER("Error executing event %s on target %s:\n%s",
5124 target_event_name(e
),
5125 target_name(target
),
5126 Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
5127 /* clean both error code and stacktrace before return */
5128 Jim_Eval(teap
->interp
, "error \"\" \"\"");
5134 static int target_jim_get_reg(Jim_Interp
*interp
, int argc
,
5135 Jim_Obj
* const *argv
)
5140 const char *option
= Jim_GetString(argv
[1], NULL
);
5142 if (!strcmp(option
, "-force")) {
5147 Jim_SetResultFormatted(interp
, "invalid option '%s'", option
);
5153 Jim_WrongNumArgs(interp
, 1, argv
, "[-force] list");
5157 const int length
= Jim_ListLength(interp
, argv
[1]);
5159 Jim_Obj
*result_dict
= Jim_NewDictObj(interp
, NULL
, 0);
5164 struct command_context
*cmd_ctx
= current_command_context(interp
);
5165 assert(cmd_ctx
!= NULL
);
5166 const struct target
*target
= get_current_target(cmd_ctx
);
5168 for (int i
= 0; i
< length
; i
++) {
5169 Jim_Obj
*elem
= Jim_ListGetIndex(interp
, argv
[1], i
);
5174 const char *reg_name
= Jim_String(elem
);
5176 struct reg
*reg
= register_get_by_name(target
->reg_cache
, reg_name
,
5179 if (!reg
|| !reg
->exist
) {
5180 Jim_SetResultFormatted(interp
, "unknown register '%s'", reg_name
);
5185 int retval
= reg
->type
->get(reg
);
5187 if (retval
!= ERROR_OK
) {
5188 Jim_SetResultFormatted(interp
, "failed to read register '%s'",
5194 char *reg_value
= buf_to_hex_str(reg
->value
, reg
->size
);
5197 LOG_ERROR("Failed to allocate memory");
5201 char *tmp
= alloc_printf("0x%s", reg_value
);
5206 LOG_ERROR("Failed to allocate memory");
5210 Jim_DictAddElement(interp
, result_dict
, elem
,
5211 Jim_NewStringObj(interp
, tmp
, -1));
5216 Jim_SetResult(interp
, result_dict
);
5221 static int target_jim_set_reg(Jim_Interp
*interp
, int argc
,
5222 Jim_Obj
* const *argv
)
5225 Jim_WrongNumArgs(interp
, 1, argv
, "dict");
5230 #if JIM_VERSION >= 80
5231 Jim_Obj
**dict
= Jim_DictPairs(interp
, argv
[1], &tmp
);
5237 int ret
= Jim_DictPairs(interp
, argv
[1], &dict
, &tmp
);
5243 const unsigned int length
= tmp
;
5244 struct command_context
*cmd_ctx
= current_command_context(interp
);
5246 const struct target
*target
= get_current_target(cmd_ctx
);
5248 for (unsigned int i
= 0; i
< length
; i
+= 2) {
5249 const char *reg_name
= Jim_String(dict
[i
]);
5250 const char *reg_value
= Jim_String(dict
[i
+ 1]);
5251 struct reg
*reg
= register_get_by_name(target
->reg_cache
, reg_name
,
5254 if (!reg
|| !reg
->exist
) {
5255 Jim_SetResultFormatted(interp
, "unknown register '%s'", reg_name
);
5259 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
5262 LOG_ERROR("Failed to allocate memory");
5266 str_to_buf(reg_value
, strlen(reg_value
), buf
, reg
->size
, 0);
5267 int retval
= reg
->type
->set(reg
, buf
);
5270 if (retval
!= ERROR_OK
) {
5271 Jim_SetResultFormatted(interp
, "failed to set '%s' to register '%s'",
5272 reg_value
, reg_name
);
5281 * Returns true only if the target has a handler for the specified event.
5283 bool target_has_event_action(struct target
*target
, enum target_event event
)
5285 struct target_event_action
*teap
;
5287 for (teap
= target
->event_action
; teap
; teap
= teap
->next
) {
5288 if (teap
->event
== event
)
5294 enum target_cfg_param
{
5297 TCFG_WORK_AREA_VIRT
,
5298 TCFG_WORK_AREA_PHYS
,
5299 TCFG_WORK_AREA_SIZE
,
5300 TCFG_WORK_AREA_BACKUP
,
5303 TCFG_CHAIN_POSITION
,
5308 TCFG_GDB_MAX_CONNECTIONS
,
5311 static struct jim_nvp nvp_config_opts
[] = {
5312 { .name
= "-type", .value
= TCFG_TYPE
},
5313 { .name
= "-event", .value
= TCFG_EVENT
},
5314 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
5315 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
5316 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
5317 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
5318 { .name
= "-endian", .value
= TCFG_ENDIAN
},
5319 { .name
= "-coreid", .value
= TCFG_COREID
},
5320 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
5321 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
5322 { .name
= "-rtos", .value
= TCFG_RTOS
},
5323 { .name
= "-defer-examine", .value
= TCFG_DEFER_EXAMINE
},
5324 { .name
= "-gdb-port", .value
= TCFG_GDB_PORT
},
5325 { .name
= "-gdb-max-connections", .value
= TCFG_GDB_MAX_CONNECTIONS
},
5326 { .name
= NULL
, .value
= -1 }
5329 static int target_configure(struct jim_getopt_info
*goi
, struct target
*target
)
5336 /* parse config or cget options ... */
5337 while (goi
->argc
> 0) {
5338 Jim_SetEmptyResult(goi
->interp
);
5339 /* jim_getopt_debug(goi); */
5341 if (target
->type
->target_jim_configure
) {
5342 /* target defines a configure function */
5343 /* target gets first dibs on parameters */
5344 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
5353 /* otherwise we 'continue' below */
5355 e
= jim_getopt_nvp(goi
, nvp_config_opts
, &n
);
5357 jim_getopt_nvp_unknown(goi
, nvp_config_opts
, 0);
5363 if (goi
->isconfigure
) {
5364 Jim_SetResultFormatted(goi
->interp
,
5365 "not settable: %s", n
->name
);
5369 if (goi
->argc
!= 0) {
5370 Jim_WrongNumArgs(goi
->interp
,
5371 goi
->argc
, goi
->argv
,
5376 Jim_SetResultString(goi
->interp
,
5377 target_type_name(target
), -1);
5381 if (goi
->argc
== 0) {
5382 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
5386 e
= jim_getopt_nvp(goi
, nvp_target_event
, &n
);
5388 jim_getopt_nvp_unknown(goi
, nvp_target_event
, 1);
5392 if (goi
->isconfigure
) {
5393 if (goi
->argc
!= 1) {
5394 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
5398 if (goi
->argc
!= 0) {
5399 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
5405 struct target_event_action
*teap
;
5407 teap
= target
->event_action
;
5408 /* replace existing? */
5410 if (teap
->event
== (enum target_event
)n
->value
)
5415 if (goi
->isconfigure
) {
5416 /* START_DEPRECATED_TPIU */
5417 if (n
->value
== TARGET_EVENT_TRACE_CONFIG
)
5418 LOG_INFO("DEPRECATED target event %s; use TPIU events {pre,post}-{enable,disable}", n
->name
);
5419 /* END_DEPRECATED_TPIU */
5421 bool replace
= true;
5424 teap
= calloc(1, sizeof(*teap
));
5427 teap
->event
= n
->value
;
5428 teap
->interp
= goi
->interp
;
5429 jim_getopt_obj(goi
, &o
);
5431 Jim_DecrRefCount(teap
->interp
, teap
->body
);
5432 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
5435 * Tcl/TK - "tk events" have a nice feature.
5436 * See the "BIND" command.
5437 * We should support that here.
5438 * You can specify %X and %Y in the event code.
5439 * The idea is: %T - target name.
5440 * The idea is: %N - target number
5441 * The idea is: %E - event name.
5443 Jim_IncrRefCount(teap
->body
);
5446 /* add to head of event list */
5447 teap
->next
= target
->event_action
;
5448 target
->event_action
= teap
;
5450 Jim_SetEmptyResult(goi
->interp
);
5454 Jim_SetEmptyResult(goi
->interp
);
5456 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
5462 case TCFG_WORK_AREA_VIRT
:
5463 if (goi
->isconfigure
) {
5464 target_free_all_working_areas(target
);
5465 e
= jim_getopt_wide(goi
, &w
);
5468 target
->working_area_virt
= w
;
5469 target
->working_area_virt_spec
= true;
5474 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
5478 case TCFG_WORK_AREA_PHYS
:
5479 if (goi
->isconfigure
) {
5480 target_free_all_working_areas(target
);
5481 e
= jim_getopt_wide(goi
, &w
);
5484 target
->working_area_phys
= w
;
5485 target
->working_area_phys_spec
= true;
5490 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
5494 case TCFG_WORK_AREA_SIZE
:
5495 if (goi
->isconfigure
) {
5496 target_free_all_working_areas(target
);
5497 e
= jim_getopt_wide(goi
, &w
);
5500 target
->working_area_size
= w
;
5505 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
5509 case TCFG_WORK_AREA_BACKUP
:
5510 if (goi
->isconfigure
) {
5511 target_free_all_working_areas(target
);
5512 e
= jim_getopt_wide(goi
, &w
);
5515 /* make this exactly 1 or 0 */
5516 target
->backup_working_area
= (!!w
);
5521 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
5522 /* loop for more e*/
5527 if (goi
->isconfigure
) {
5528 e
= jim_getopt_nvp(goi
, nvp_target_endian
, &n
);
5530 jim_getopt_nvp_unknown(goi
, nvp_target_endian
, 1);
5533 target
->endianness
= n
->value
;
5538 n
= jim_nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
5540 target
->endianness
= TARGET_LITTLE_ENDIAN
;
5541 n
= jim_nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
5543 Jim_SetResultString(goi
->interp
, n
->name
, -1);
5548 if (goi
->isconfigure
) {
5549 e
= jim_getopt_wide(goi
, &w
);
5552 target
->coreid
= (int32_t)w
;
5557 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->coreid
));
5561 case TCFG_CHAIN_POSITION
:
5562 if (goi
->isconfigure
) {
5564 struct jtag_tap
*tap
;
5566 if (target
->has_dap
) {
5567 Jim_SetResultString(goi
->interp
,
5568 "target requires -dap parameter instead of -chain-position!", -1);
5572 target_free_all_working_areas(target
);
5573 e
= jim_getopt_obj(goi
, &o_t
);
5576 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
5580 target
->tap_configured
= true;
5585 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
5586 /* loop for more e*/
5589 if (goi
->isconfigure
) {
5590 e
= jim_getopt_wide(goi
, &w
);
5593 target
->dbgbase
= (uint32_t)w
;
5594 target
->dbgbase_set
= true;
5599 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
5605 int result
= rtos_create(goi
, target
);
5606 if (result
!= JIM_OK
)
5612 case TCFG_DEFER_EXAMINE
:
5614 target
->defer_examine
= true;
5619 if (goi
->isconfigure
) {
5620 struct command_context
*cmd_ctx
= current_command_context(goi
->interp
);
5621 if (cmd_ctx
->mode
!= COMMAND_CONFIG
) {
5622 Jim_SetResultString(goi
->interp
, "-gdb-port must be configured before 'init'", -1);
5627 e
= jim_getopt_string(goi
, &s
, NULL
);
5630 free(target
->gdb_port_override
);
5631 target
->gdb_port_override
= strdup(s
);
5636 Jim_SetResultString(goi
->interp
, target
->gdb_port_override
? target
->gdb_port_override
: "undefined", -1);
5640 case TCFG_GDB_MAX_CONNECTIONS
:
5641 if (goi
->isconfigure
) {
5642 struct command_context
*cmd_ctx
= current_command_context(goi
->interp
);
5643 if (cmd_ctx
->mode
!= COMMAND_CONFIG
) {
5644 Jim_SetResultString(goi
->interp
, "-gdb-max-connections must be configured before 'init'", -1);
5648 e
= jim_getopt_wide(goi
, &w
);
5651 target
->gdb_max_connections
= (w
< 0) ? CONNECTION_LIMIT_UNLIMITED
: (int)w
;
5656 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->gdb_max_connections
));
5659 } /* while (goi->argc) */
5662 /* done - we return */
5666 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5668 struct command
*c
= jim_to_command(interp
);
5669 struct jim_getopt_info goi
;
5671 jim_getopt_setup(&goi
, interp
, argc
- 1, argv
+ 1);
5672 goi
.isconfigure
= !strcmp(c
->name
, "configure");
5674 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5675 "missing: -option ...");
5678 struct command_context
*cmd_ctx
= current_command_context(interp
);
5680 struct target
*target
= get_current_target(cmd_ctx
);
5681 return target_configure(&goi
, target
);
5684 static int jim_target_mem2array(Jim_Interp
*interp
,
5685 int argc
, Jim_Obj
*const *argv
)
5687 struct command_context
*cmd_ctx
= current_command_context(interp
);
5689 struct target
*target
= get_current_target(cmd_ctx
);
5690 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
5693 static int jim_target_array2mem(Jim_Interp
*interp
,
5694 int argc
, Jim_Obj
*const *argv
)
5696 struct command_context
*cmd_ctx
= current_command_context(interp
);
5698 struct target
*target
= get_current_target(cmd_ctx
);
5699 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
5702 static int jim_target_tap_disabled(Jim_Interp
*interp
)
5704 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
5708 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5710 bool allow_defer
= false;
5712 struct jim_getopt_info goi
;
5713 jim_getopt_setup(&goi
, interp
, argc
- 1, argv
+ 1);
5715 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5716 Jim_SetResultFormatted(goi
.interp
,
5717 "usage: %s ['allow-defer']", cmd_name
);
5721 strcmp(Jim_GetString(argv
[1], NULL
), "allow-defer") == 0) {
5724 int e
= jim_getopt_obj(&goi
, &obj
);
5730 struct command_context
*cmd_ctx
= current_command_context(interp
);
5732 struct target
*target
= get_current_target(cmd_ctx
);
5733 if (!target
->tap
->enabled
)
5734 return jim_target_tap_disabled(interp
);
5736 if (allow_defer
&& target
->defer_examine
) {
5737 LOG_INFO("Deferring arp_examine of %s", target_name(target
));
5738 LOG_INFO("Use arp_examine command to examine it manually!");
5742 int e
= target
->type
->examine(target
);
5743 if (e
!= ERROR_OK
) {
5744 target_reset_examined(target
);
5748 target_set_examined(target
);
5753 static int jim_target_was_examined(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5755 struct command_context
*cmd_ctx
= current_command_context(interp
);
5757 struct target
*target
= get_current_target(cmd_ctx
);
5759 Jim_SetResultBool(interp
, target_was_examined(target
));
5763 static int jim_target_examine_deferred(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5765 struct command_context
*cmd_ctx
= current_command_context(interp
);
5767 struct target
*target
= get_current_target(cmd_ctx
);
5769 Jim_SetResultBool(interp
, target
->defer_examine
);
5773 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5776 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5779 struct command_context
*cmd_ctx
= current_command_context(interp
);
5781 struct target
*target
= get_current_target(cmd_ctx
);
5783 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
5789 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5792 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5795 struct command_context
*cmd_ctx
= current_command_context(interp
);
5797 struct target
*target
= get_current_target(cmd_ctx
);
5798 if (!target
->tap
->enabled
)
5799 return jim_target_tap_disabled(interp
);
5802 if (!(target_was_examined(target
)))
5803 e
= ERROR_TARGET_NOT_EXAMINED
;
5805 e
= target
->type
->poll(target
);
5811 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5813 struct jim_getopt_info goi
;
5814 jim_getopt_setup(&goi
, interp
, argc
- 1, argv
+ 1);
5816 if (goi
.argc
!= 2) {
5817 Jim_WrongNumArgs(interp
, 0, argv
,
5818 "([tT]|[fF]|assert|deassert) BOOL");
5823 int e
= jim_getopt_nvp(&goi
, nvp_assert
, &n
);
5825 jim_getopt_nvp_unknown(&goi
, nvp_assert
, 1);
5828 /* the halt or not param */
5830 e
= jim_getopt_wide(&goi
, &a
);
5834 struct command_context
*cmd_ctx
= current_command_context(interp
);
5836 struct target
*target
= get_current_target(cmd_ctx
);
5837 if (!target
->tap
->enabled
)
5838 return jim_target_tap_disabled(interp
);
5840 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
5841 Jim_SetResultFormatted(interp
,
5842 "No target-specific reset for %s",
5843 target_name(target
));
5847 if (target
->defer_examine
)
5848 target_reset_examined(target
);
5850 /* determine if we should halt or not. */
5851 target
->reset_halt
= (a
!= 0);
5852 /* When this happens - all workareas are invalid. */
5853 target_free_all_working_areas_restore(target
, 0);
5856 if (n
->value
== NVP_ASSERT
)
5857 e
= target
->type
->assert_reset(target
);
5859 e
= target
->type
->deassert_reset(target
);
5860 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5863 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5866 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5869 struct command_context
*cmd_ctx
= current_command_context(interp
);
5871 struct target
*target
= get_current_target(cmd_ctx
);
5872 if (!target
->tap
->enabled
)
5873 return jim_target_tap_disabled(interp
);
5874 int e
= target
->type
->halt(target
);
5875 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5878 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5880 struct jim_getopt_info goi
;
5881 jim_getopt_setup(&goi
, interp
, argc
- 1, argv
+ 1);
5883 /* params: <name> statename timeoutmsecs */
5884 if (goi
.argc
!= 2) {
5885 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5886 Jim_SetResultFormatted(goi
.interp
,
5887 "%s <state_name> <timeout_in_msec>", cmd_name
);
5892 int e
= jim_getopt_nvp(&goi
, nvp_target_state
, &n
);
5894 jim_getopt_nvp_unknown(&goi
, nvp_target_state
, 1);
5898 e
= jim_getopt_wide(&goi
, &a
);
5901 struct command_context
*cmd_ctx
= current_command_context(interp
);
5903 struct target
*target
= get_current_target(cmd_ctx
);
5904 if (!target
->tap
->enabled
)
5905 return jim_target_tap_disabled(interp
);
5907 e
= target_wait_state(target
, n
->value
, a
);
5908 if (e
!= ERROR_OK
) {
5909 Jim_Obj
*obj
= Jim_NewIntObj(interp
, e
);
5910 Jim_SetResultFormatted(goi
.interp
,
5911 "target: %s wait %s fails (%#s) %s",
5912 target_name(target
), n
->name
,
5913 obj
, target_strerror_safe(e
));
5918 /* List for human, Events defined for this target.
5919 * scripts/programs should use 'name cget -event NAME'
5921 COMMAND_HANDLER(handle_target_event_list
)
5923 struct target
*target
= get_current_target(CMD_CTX
);
5924 struct target_event_action
*teap
= target
->event_action
;
5926 command_print(CMD
, "Event actions for target (%d) %s\n",
5927 target
->target_number
,
5928 target_name(target
));
5929 command_print(CMD
, "%-25s | Body", "Event");
5930 command_print(CMD
, "------------------------- | "
5931 "----------------------------------------");
5933 command_print(CMD
, "%-25s | %s",
5934 target_event_name(teap
->event
),
5935 Jim_GetString(teap
->body
, NULL
));
5938 command_print(CMD
, "***END***");
5941 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5944 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5947 struct command_context
*cmd_ctx
= current_command_context(interp
);
5949 struct target
*target
= get_current_target(cmd_ctx
);
5950 Jim_SetResultString(interp
, target_state_name(target
), -1);
5953 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5955 struct jim_getopt_info goi
;
5956 jim_getopt_setup(&goi
, interp
, argc
- 1, argv
+ 1);
5957 if (goi
.argc
!= 1) {
5958 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5959 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
5963 int e
= jim_getopt_nvp(&goi
, nvp_target_event
, &n
);
5965 jim_getopt_nvp_unknown(&goi
, nvp_target_event
, 1);
5968 struct command_context
*cmd_ctx
= current_command_context(interp
);
5970 struct target
*target
= get_current_target(cmd_ctx
);
5971 target_handle_event(target
, n
->value
);
5975 static const struct command_registration target_instance_command_handlers
[] = {
5977 .name
= "configure",
5978 .mode
= COMMAND_ANY
,
5979 .jim_handler
= jim_target_configure
,
5980 .help
= "configure a new target for use",
5981 .usage
= "[target_attribute ...]",
5985 .mode
= COMMAND_ANY
,
5986 .jim_handler
= jim_target_configure
,
5987 .help
= "returns the specified target attribute",
5988 .usage
= "target_attribute",
5992 .handler
= handle_mw_command
,
5993 .mode
= COMMAND_EXEC
,
5994 .help
= "Write 64-bit word(s) to target memory",
5995 .usage
= "address data [count]",
5999 .handler
= handle_mw_command
,
6000 .mode
= COMMAND_EXEC
,
6001 .help
= "Write 32-bit word(s) to target memory",
6002 .usage
= "address data [count]",
6006 .handler
= handle_mw_command
,
6007 .mode
= COMMAND_EXEC
,
6008 .help
= "Write 16-bit half-word(s) to target memory",
6009 .usage
= "address data [count]",
6013 .handler
= handle_mw_command
,
6014 .mode
= COMMAND_EXEC
,
6015 .help
= "Write byte(s) to target memory",
6016 .usage
= "address data [count]",
6020 .handler
= handle_md_command
,
6021 .mode
= COMMAND_EXEC
,
6022 .help
= "Display target memory as 64-bit words",
6023 .usage
= "address [count]",
6027 .handler
= handle_md_command
,
6028 .mode
= COMMAND_EXEC
,
6029 .help
= "Display target memory as 32-bit words",
6030 .usage
= "address [count]",
6034 .handler
= handle_md_command
,
6035 .mode
= COMMAND_EXEC
,
6036 .help
= "Display target memory as 16-bit half-words",
6037 .usage
= "address [count]",
6041 .handler
= handle_md_command
,
6042 .mode
= COMMAND_EXEC
,
6043 .help
= "Display target memory as 8-bit bytes",
6044 .usage
= "address [count]",
6047 .name
= "array2mem",
6048 .mode
= COMMAND_EXEC
,
6049 .jim_handler
= jim_target_array2mem
,
6050 .help
= "Writes Tcl array of 8/16/32 bit numbers "
6052 .usage
= "arrayname bitwidth address count",
6055 .name
= "mem2array",
6056 .mode
= COMMAND_EXEC
,
6057 .jim_handler
= jim_target_mem2array
,
6058 .help
= "Loads Tcl array of 8/16/32 bit numbers "
6059 "from target memory",
6060 .usage
= "arrayname bitwidth address count",
6064 .mode
= COMMAND_EXEC
,
6065 .jim_handler
= target_jim_get_reg
,
6066 .help
= "Get register values from the target",
6071 .mode
= COMMAND_EXEC
,
6072 .jim_handler
= target_jim_set_reg
,
6073 .help
= "Set target register values",
6077 .name
= "read_memory",
6078 .mode
= COMMAND_EXEC
,
6079 .jim_handler
= target_jim_read_memory
,
6080 .help
= "Read Tcl list of 8/16/32/64 bit numbers from target memory",
6081 .usage
= "address width count ['phys']",
6084 .name
= "write_memory",
6085 .mode
= COMMAND_EXEC
,
6086 .jim_handler
= target_jim_write_memory
,
6087 .help
= "Write Tcl list of 8/16/32/64 bit numbers to target memory",
6088 .usage
= "address width data ['phys']",
6091 .name
= "eventlist",
6092 .handler
= handle_target_event_list
,
6093 .mode
= COMMAND_EXEC
,
6094 .help
= "displays a table of events defined for this target",
6099 .mode
= COMMAND_EXEC
,
6100 .jim_handler
= jim_target_current_state
,
6101 .help
= "displays the current state of this target",
6104 .name
= "arp_examine",
6105 .mode
= COMMAND_EXEC
,
6106 .jim_handler
= jim_target_examine
,
6107 .help
= "used internally for reset processing",
6108 .usage
= "['allow-defer']",
6111 .name
= "was_examined",
6112 .mode
= COMMAND_EXEC
,
6113 .jim_handler
= jim_target_was_examined
,
6114 .help
= "used internally for reset processing",
6117 .name
= "examine_deferred",
6118 .mode
= COMMAND_EXEC
,
6119 .jim_handler
= jim_target_examine_deferred
,
6120 .help
= "used internally for reset processing",
6123 .name
= "arp_halt_gdb",
6124 .mode
= COMMAND_EXEC
,
6125 .jim_handler
= jim_target_halt_gdb
,
6126 .help
= "used internally for reset processing to halt GDB",
6130 .mode
= COMMAND_EXEC
,
6131 .jim_handler
= jim_target_poll
,
6132 .help
= "used internally for reset processing",
6135 .name
= "arp_reset",
6136 .mode
= COMMAND_EXEC
,
6137 .jim_handler
= jim_target_reset
,
6138 .help
= "used internally for reset processing",
6142 .mode
= COMMAND_EXEC
,
6143 .jim_handler
= jim_target_halt
,
6144 .help
= "used internally for reset processing",
6147 .name
= "arp_waitstate",
6148 .mode
= COMMAND_EXEC
,
6149 .jim_handler
= jim_target_wait_state
,
6150 .help
= "used internally for reset processing",
6153 .name
= "invoke-event",
6154 .mode
= COMMAND_EXEC
,
6155 .jim_handler
= jim_target_invoke_event
,
6156 .help
= "invoke handler for specified event",
6157 .usage
= "event_name",
6159 COMMAND_REGISTRATION_DONE
6162 static int target_create(struct jim_getopt_info
*goi
)
6169 struct target
*target
;
6170 struct command_context
*cmd_ctx
;
6172 cmd_ctx
= current_command_context(goi
->interp
);
6175 if (goi
->argc
< 3) {
6176 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
6181 jim_getopt_obj(goi
, &new_cmd
);
6182 /* does this command exist? */
6183 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_NONE
);
6185 cp
= Jim_GetString(new_cmd
, NULL
);
6186 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
6191 e
= jim_getopt_string(goi
, &cp
, NULL
);
6194 struct transport
*tr
= get_current_transport();
6195 if (tr
->override_target
) {
6196 e
= tr
->override_target(&cp
);
6197 if (e
!= ERROR_OK
) {
6198 LOG_ERROR("The selected transport doesn't support this target");
6201 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
6203 /* now does target type exist */
6204 for (x
= 0 ; target_types
[x
] ; x
++) {
6205 if (strcmp(cp
, target_types
[x
]->name
) == 0) {
6210 if (!target_types
[x
]) {
6211 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
6212 for (x
= 0 ; target_types
[x
] ; x
++) {
6213 if (target_types
[x
+ 1]) {
6214 Jim_AppendStrings(goi
->interp
,
6215 Jim_GetResult(goi
->interp
),
6216 target_types
[x
]->name
,
6219 Jim_AppendStrings(goi
->interp
,
6220 Jim_GetResult(goi
->interp
),
6222 target_types
[x
]->name
, NULL
);
6229 target
= calloc(1, sizeof(struct target
));
6231 LOG_ERROR("Out of memory");
6235 /* set empty smp cluster */
6236 target
->smp_targets
= &empty_smp_targets
;
6238 /* set target number */
6239 target
->target_number
= new_target_number();
6241 /* allocate memory for each unique target type */
6242 target
->type
= malloc(sizeof(struct target_type
));
6243 if (!target
->type
) {
6244 LOG_ERROR("Out of memory");
6249 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
6251 /* default to first core, override with -coreid */
6254 target
->working_area
= 0x0;
6255 target
->working_area_size
= 0x0;
6256 target
->working_areas
= NULL
;
6257 target
->backup_working_area
= 0;
6259 target
->state
= TARGET_UNKNOWN
;
6260 target
->debug_reason
= DBG_REASON_UNDEFINED
;
6261 target
->reg_cache
= NULL
;
6262 target
->breakpoints
= NULL
;
6263 target
->watchpoints
= NULL
;
6264 target
->next
= NULL
;
6265 target
->arch_info
= NULL
;
6267 target
->verbose_halt_msg
= true;
6269 target
->halt_issued
= false;
6271 /* initialize trace information */
6272 target
->trace_info
= calloc(1, sizeof(struct trace
));
6273 if (!target
->trace_info
) {
6274 LOG_ERROR("Out of memory");
6280 target
->dbgmsg
= NULL
;
6281 target
->dbg_msg_enabled
= 0;
6283 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
6285 target
->rtos
= NULL
;
6286 target
->rtos_auto_detect
= false;
6288 target
->gdb_port_override
= NULL
;
6289 target
->gdb_max_connections
= 1;
6291 /* Do the rest as "configure" options */
6292 goi
->isconfigure
= 1;
6293 e
= target_configure(goi
, target
);
6296 if (target
->has_dap
) {
6297 if (!target
->dap_configured
) {
6298 Jim_SetResultString(goi
->interp
, "-dap ?name? required when creating target", -1);
6302 if (!target
->tap_configured
) {
6303 Jim_SetResultString(goi
->interp
, "-chain-position ?name? required when creating target", -1);
6307 /* tap must be set after target was configured */
6313 rtos_destroy(target
);
6314 free(target
->gdb_port_override
);
6315 free(target
->trace_info
);
6321 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
6322 /* default endian to little if not specified */
6323 target
->endianness
= TARGET_LITTLE_ENDIAN
;
6326 cp
= Jim_GetString(new_cmd
, NULL
);
6327 target
->cmd_name
= strdup(cp
);
6328 if (!target
->cmd_name
) {
6329 LOG_ERROR("Out of memory");
6330 rtos_destroy(target
);
6331 free(target
->gdb_port_override
);
6332 free(target
->trace_info
);
6338 if (target
->type
->target_create
) {
6339 e
= (*(target
->type
->target_create
))(target
, goi
->interp
);
6340 if (e
!= ERROR_OK
) {
6341 LOG_DEBUG("target_create failed");
6342 free(target
->cmd_name
);
6343 rtos_destroy(target
);
6344 free(target
->gdb_port_override
);
6345 free(target
->trace_info
);
6352 /* create the target specific commands */
6353 if (target
->type
->commands
) {
6354 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
6356 LOG_ERROR("unable to register '%s' commands", cp
);
6359 /* now - create the new target name command */
6360 const struct command_registration target_subcommands
[] = {
6362 .chain
= target_instance_command_handlers
,
6365 .chain
= target
->type
->commands
,
6367 COMMAND_REGISTRATION_DONE
6369 const struct command_registration target_commands
[] = {
6372 .mode
= COMMAND_ANY
,
6373 .help
= "target command group",
6375 .chain
= target_subcommands
,
6377 COMMAND_REGISTRATION_DONE
6379 e
= register_commands_override_target(cmd_ctx
, NULL
, target_commands
, target
);
6380 if (e
!= ERROR_OK
) {
6381 if (target
->type
->deinit_target
)
6382 target
->type
->deinit_target(target
);
6383 free(target
->cmd_name
);
6384 rtos_destroy(target
);
6385 free(target
->gdb_port_override
);
6386 free(target
->trace_info
);
6392 /* append to end of list */
6393 append_to_list_all_targets(target
);
6395 cmd_ctx
->current_target
= target
;
6399 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
6402 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
6405 struct command_context
*cmd_ctx
= current_command_context(interp
);
6408 struct target
*target
= get_current_target_or_null(cmd_ctx
);
6410 Jim_SetResultString(interp
, target_name(target
), -1);
6414 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
6417 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
6420 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
6421 for (unsigned x
= 0; target_types
[x
]; x
++) {
6422 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
6423 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
6428 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
6431 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
6434 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
6435 struct target
*target
= all_targets
;
6437 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
6438 Jim_NewStringObj(interp
, target_name(target
), -1));
6439 target
= target
->next
;
6444 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
6447 const char *targetname
;
6449 static int smp_group
= 1;
6450 struct target
*target
= NULL
;
6451 struct target_list
*head
, *new;
6454 LOG_DEBUG("%d", argc
);
6455 /* argv[1] = target to associate in smp
6456 * argv[2] = target to associate in smp
6460 struct list_head
*lh
= malloc(sizeof(*lh
));
6462 LOG_ERROR("Out of memory");
6467 for (i
= 1; i
< argc
; i
++) {
6469 targetname
= Jim_GetString(argv
[i
], &len
);
6470 target
= get_target(targetname
);
6471 LOG_DEBUG("%s ", targetname
);
6473 new = malloc(sizeof(struct target_list
));
6474 new->target
= target
;
6475 list_add_tail(&new->lh
, lh
);
6478 /* now parse the list of cpu and put the target in smp mode*/
6479 foreach_smp_target(head
, lh
) {
6480 target
= head
->target
;
6481 target
->smp
= smp_group
;
6482 target
->smp_targets
= lh
;
6486 if (target
&& target
->rtos
)
6487 retval
= rtos_smp_init(target
);
6493 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
6495 struct jim_getopt_info goi
;
6496 jim_getopt_setup(&goi
, interp
, argc
- 1, argv
+ 1);
6498 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
6499 "<name> <target_type> [<target_options> ...]");
6502 return target_create(&goi
);
6505 static const struct command_registration target_subcommand_handlers
[] = {
6508 .mode
= COMMAND_CONFIG
,
6509 .handler
= handle_target_init_command
,
6510 .help
= "initialize targets",
6515 .mode
= COMMAND_CONFIG
,
6516 .jim_handler
= jim_target_create
,
6517 .usage
= "name type '-chain-position' name [options ...]",
6518 .help
= "Creates and selects a new target",
6522 .mode
= COMMAND_ANY
,
6523 .jim_handler
= jim_target_current
,
6524 .help
= "Returns the currently selected target",
6528 .mode
= COMMAND_ANY
,
6529 .jim_handler
= jim_target_types
,
6530 .help
= "Returns the available target types as "
6531 "a list of strings",
6535 .mode
= COMMAND_ANY
,
6536 .jim_handler
= jim_target_names
,
6537 .help
= "Returns the names of all targets as a list of strings",
6541 .mode
= COMMAND_ANY
,
6542 .jim_handler
= jim_target_smp
,
6543 .usage
= "targetname1 targetname2 ...",
6544 .help
= "gather several target in a smp list"
6547 COMMAND_REGISTRATION_DONE
6551 target_addr_t address
;
6557 static int fastload_num
;
6558 static struct fast_load
*fastload
;
6560 static void free_fastload(void)
6563 for (int i
= 0; i
< fastload_num
; i
++)
6564 free(fastload
[i
].data
);
6570 COMMAND_HANDLER(handle_fast_load_image_command
)
6574 uint32_t image_size
;
6575 target_addr_t min_address
= 0;
6576 target_addr_t max_address
= -1;
6580 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command
,
6581 &image
, &min_address
, &max_address
);
6582 if (retval
!= ERROR_OK
)
6585 struct duration bench
;
6586 duration_start(&bench
);
6588 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
6589 if (retval
!= ERROR_OK
)
6594 fastload_num
= image
.num_sections
;
6595 fastload
= malloc(sizeof(struct fast_load
)*image
.num_sections
);
6597 command_print(CMD
, "out of memory");
6598 image_close(&image
);
6601 memset(fastload
, 0, sizeof(struct fast_load
)*image
.num_sections
);
6602 for (unsigned int i
= 0; i
< image
.num_sections
; i
++) {
6603 buffer
= malloc(image
.sections
[i
].size
);
6605 command_print(CMD
, "error allocating buffer for section (%d bytes)",
6606 (int)(image
.sections
[i
].size
));
6607 retval
= ERROR_FAIL
;
6611 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
6612 if (retval
!= ERROR_OK
) {
6617 uint32_t offset
= 0;
6618 uint32_t length
= buf_cnt
;
6620 /* DANGER!!! beware of unsigned comparison here!!! */
6622 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
6623 (image
.sections
[i
].base_address
< max_address
)) {
6624 if (image
.sections
[i
].base_address
< min_address
) {
6625 /* clip addresses below */
6626 offset
+= min_address
-image
.sections
[i
].base_address
;
6630 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
6631 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
6633 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
6634 fastload
[i
].data
= malloc(length
);
6635 if (!fastload
[i
].data
) {
6637 command_print(CMD
, "error allocating buffer for section (%" PRIu32
" bytes)",
6639 retval
= ERROR_FAIL
;
6642 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
6643 fastload
[i
].length
= length
;
6645 image_size
+= length
;
6646 command_print(CMD
, "%u bytes written at address 0x%8.8x",
6647 (unsigned int)length
,
6648 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
6654 if ((retval
== ERROR_OK
) && (duration_measure(&bench
) == ERROR_OK
)) {
6655 command_print(CMD
, "Loaded %" PRIu32
" bytes "
6656 "in %fs (%0.3f KiB/s)", image_size
,
6657 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
6660 "WARNING: image has not been loaded to target!"
6661 "You can issue a 'fast_load' to finish loading.");
6664 image_close(&image
);
6666 if (retval
!= ERROR_OK
)
6672 COMMAND_HANDLER(handle_fast_load_command
)
6675 return ERROR_COMMAND_SYNTAX_ERROR
;
6677 LOG_ERROR("No image in memory");
6681 int64_t ms
= timeval_ms();
6683 int retval
= ERROR_OK
;
6684 for (i
= 0; i
< fastload_num
; i
++) {
6685 struct target
*target
= get_current_target(CMD_CTX
);
6686 command_print(CMD
, "Write to 0x%08x, length 0x%08x",
6687 (unsigned int)(fastload
[i
].address
),
6688 (unsigned int)(fastload
[i
].length
));
6689 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
6690 if (retval
!= ERROR_OK
)
6692 size
+= fastload
[i
].length
;
6694 if (retval
== ERROR_OK
) {
6695 int64_t after
= timeval_ms();
6696 command_print(CMD
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
6701 static const struct command_registration target_command_handlers
[] = {
6704 .handler
= handle_targets_command
,
6705 .mode
= COMMAND_ANY
,
6706 .help
= "change current default target (one parameter) "
6707 "or prints table of all targets (no parameters)",
6708 .usage
= "[target]",
6712 .mode
= COMMAND_CONFIG
,
6713 .help
= "configure target",
6714 .chain
= target_subcommand_handlers
,
6717 COMMAND_REGISTRATION_DONE
6720 int target_register_commands(struct command_context
*cmd_ctx
)
6722 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
6725 static bool target_reset_nag
= true;
6727 bool get_target_reset_nag(void)
6729 return target_reset_nag
;
6732 COMMAND_HANDLER(handle_target_reset_nag
)
6734 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
6735 &target_reset_nag
, "Nag after each reset about options to improve "
6739 COMMAND_HANDLER(handle_ps_command
)
6741 struct target
*target
= get_current_target(CMD_CTX
);
6743 if (target
->state
!= TARGET_HALTED
) {
6744 LOG_INFO("target not halted !!");
6748 if ((target
->rtos
) && (target
->rtos
->type
)
6749 && (target
->rtos
->type
->ps_command
)) {
6750 display
= target
->rtos
->type
->ps_command(target
);
6751 command_print(CMD
, "%s", display
);
6756 return ERROR_TARGET_FAILURE
;
6760 static void binprint(struct command_invocation
*cmd
, const char *text
, const uint8_t *buf
, int size
)
6763 command_print_sameline(cmd
, "%s", text
);
6764 for (int i
= 0; i
< size
; i
++)
6765 command_print_sameline(cmd
, " %02x", buf
[i
]);
6766 command_print(cmd
, " ");
6769 COMMAND_HANDLER(handle_test_mem_access_command
)
6771 struct target
*target
= get_current_target(CMD_CTX
);
6773 int retval
= ERROR_OK
;
6775 if (target
->state
!= TARGET_HALTED
) {
6776 LOG_INFO("target not halted !!");
6781 return ERROR_COMMAND_SYNTAX_ERROR
;
6783 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], test_size
);
6786 size_t num_bytes
= test_size
+ 4;
6788 struct working_area
*wa
= NULL
;
6789 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6790 if (retval
!= ERROR_OK
) {
6791 LOG_ERROR("Not enough working area");
6795 uint8_t *test_pattern
= malloc(num_bytes
);
6797 for (size_t i
= 0; i
< num_bytes
; i
++)
6798 test_pattern
[i
] = rand();
6800 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6801 if (retval
!= ERROR_OK
) {
6802 LOG_ERROR("Test pattern write failed");
6806 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6807 for (int size
= 1; size
<= 4; size
*= 2) {
6808 for (int offset
= 0; offset
< 4; offset
++) {
6809 uint32_t count
= test_size
/ size
;
6810 size_t host_bufsiz
= (count
+ 2) * size
+ host_offset
;
6811 uint8_t *read_ref
= malloc(host_bufsiz
);
6812 uint8_t *read_buf
= malloc(host_bufsiz
);
6814 for (size_t i
= 0; i
< host_bufsiz
; i
++) {
6815 read_ref
[i
] = rand();
6816 read_buf
[i
] = read_ref
[i
];
6818 command_print_sameline(CMD
,
6819 "Test read %" PRIu32
" x %d @ %d to %saligned buffer: ", count
,
6820 size
, offset
, host_offset
? "un" : "");
6822 struct duration bench
;
6823 duration_start(&bench
);
6825 retval
= target_read_memory(target
, wa
->address
+ offset
, size
, count
,
6826 read_buf
+ size
+ host_offset
);
6828 duration_measure(&bench
);
6830 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6831 command_print(CMD
, "Unsupported alignment");
6833 } else if (retval
!= ERROR_OK
) {
6834 command_print(CMD
, "Memory read failed");
6838 /* replay on host */
6839 memcpy(read_ref
+ size
+ host_offset
, test_pattern
+ offset
, count
* size
);
6842 int result
= memcmp(read_ref
, read_buf
, host_bufsiz
);
6844 command_print(CMD
, "Pass in %fs (%0.3f KiB/s)",
6845 duration_elapsed(&bench
),
6846 duration_kbps(&bench
, count
* size
));
6848 command_print(CMD
, "Compare failed");
6849 binprint(CMD
, "ref:", read_ref
, host_bufsiz
);
6850 binprint(CMD
, "buf:", read_buf
, host_bufsiz
);
6862 target_free_working_area(target
, wa
);
6865 num_bytes
= test_size
+ 4 + 4 + 4;
6867 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6868 if (retval
!= ERROR_OK
) {
6869 LOG_ERROR("Not enough working area");
6873 test_pattern
= malloc(num_bytes
);
6875 for (size_t i
= 0; i
< num_bytes
; i
++)
6876 test_pattern
[i
] = rand();
6878 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6879 for (int size
= 1; size
<= 4; size
*= 2) {
6880 for (int offset
= 0; offset
< 4; offset
++) {
6881 uint32_t count
= test_size
/ size
;
6882 size_t host_bufsiz
= count
* size
+ host_offset
;
6883 uint8_t *read_ref
= malloc(num_bytes
);
6884 uint8_t *read_buf
= malloc(num_bytes
);
6885 uint8_t *write_buf
= malloc(host_bufsiz
);
6887 for (size_t i
= 0; i
< host_bufsiz
; i
++)
6888 write_buf
[i
] = rand();
6889 command_print_sameline(CMD
,
6890 "Test write %" PRIu32
" x %d @ %d from %saligned buffer: ", count
,
6891 size
, offset
, host_offset
? "un" : "");
6893 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6894 if (retval
!= ERROR_OK
) {
6895 command_print(CMD
, "Test pattern write failed");
6899 /* replay on host */
6900 memcpy(read_ref
, test_pattern
, num_bytes
);
6901 memcpy(read_ref
+ size
+ offset
, write_buf
+ host_offset
, count
* size
);
6903 struct duration bench
;
6904 duration_start(&bench
);
6906 retval
= target_write_memory(target
, wa
->address
+ size
+ offset
, size
, count
,
6907 write_buf
+ host_offset
);
6909 duration_measure(&bench
);
6911 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6912 command_print(CMD
, "Unsupported alignment");
6914 } else if (retval
!= ERROR_OK
) {
6915 command_print(CMD
, "Memory write failed");
6920 retval
= target_read_memory(target
, wa
->address
, 1, num_bytes
, read_buf
);
6921 if (retval
!= ERROR_OK
) {
6922 command_print(CMD
, "Test pattern write failed");
6927 int result
= memcmp(read_ref
, read_buf
, num_bytes
);
6929 command_print(CMD
, "Pass in %fs (%0.3f KiB/s)",
6930 duration_elapsed(&bench
),
6931 duration_kbps(&bench
, count
* size
));
6933 command_print(CMD
, "Compare failed");
6934 binprint(CMD
, "ref:", read_ref
, num_bytes
);
6935 binprint(CMD
, "buf:", read_buf
, num_bytes
);
6946 target_free_working_area(target
, wa
);
6950 static const struct command_registration target_exec_command_handlers
[] = {
6952 .name
= "fast_load_image",
6953 .handler
= handle_fast_load_image_command
,
6954 .mode
= COMMAND_ANY
,
6955 .help
= "Load image into server memory for later use by "
6956 "fast_load; primarily for profiling",
6957 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6958 "[min_address [max_length]]",
6961 .name
= "fast_load",
6962 .handler
= handle_fast_load_command
,
6963 .mode
= COMMAND_EXEC
,
6964 .help
= "loads active fast load image to current target "
6965 "- mainly for profiling purposes",
6970 .handler
= handle_profile_command
,
6971 .mode
= COMMAND_EXEC
,
6972 .usage
= "seconds filename [start end]",
6973 .help
= "profiling samples the CPU PC",
6975 /** @todo don't register virt2phys() unless target supports it */
6977 .name
= "virt2phys",
6978 .handler
= handle_virt2phys_command
,
6979 .mode
= COMMAND_ANY
,
6980 .help
= "translate a virtual address into a physical address",
6981 .usage
= "virtual_address",
6985 .handler
= handle_reg_command
,
6986 .mode
= COMMAND_EXEC
,
6987 .help
= "display (reread from target with \"force\") or set a register; "
6988 "with no arguments, displays all registers and their values",
6989 .usage
= "[(register_number|register_name) [(value|'force')]]",
6993 .handler
= handle_poll_command
,
6994 .mode
= COMMAND_EXEC
,
6995 .help
= "poll target state; or reconfigure background polling",
6996 .usage
= "['on'|'off']",
6999 .name
= "wait_halt",
7000 .handler
= handle_wait_halt_command
,
7001 .mode
= COMMAND_EXEC
,
7002 .help
= "wait up to the specified number of milliseconds "
7003 "(default 5000) for a previously requested halt",
7004 .usage
= "[milliseconds]",
7008 .handler
= handle_halt_command
,
7009 .mode
= COMMAND_EXEC
,
7010 .help
= "request target to halt, then wait up to the specified "
7011 "number of milliseconds (default 5000) for it to complete",
7012 .usage
= "[milliseconds]",
7016 .handler
= handle_resume_command
,
7017 .mode
= COMMAND_EXEC
,
7018 .help
= "resume target execution from current PC or address",
7019 .usage
= "[address]",
7023 .handler
= handle_reset_command
,
7024 .mode
= COMMAND_EXEC
,
7025 .usage
= "[run|halt|init]",
7026 .help
= "Reset all targets into the specified mode. "
7027 "Default reset mode is run, if not given.",
7030 .name
= "soft_reset_halt",
7031 .handler
= handle_soft_reset_halt_command
,
7032 .mode
= COMMAND_EXEC
,
7034 .help
= "halt the target and do a soft reset",
7038 .handler
= handle_step_command
,
7039 .mode
= COMMAND_EXEC
,
7040 .help
= "step one instruction from current PC or address",
7041 .usage
= "[address]",
7045 .handler
= handle_md_command
,
7046 .mode
= COMMAND_EXEC
,
7047 .help
= "display memory double-words",
7048 .usage
= "['phys'] address [count]",
7052 .handler
= handle_md_command
,
7053 .mode
= COMMAND_EXEC
,
7054 .help
= "display memory words",
7055 .usage
= "['phys'] address [count]",
7059 .handler
= handle_md_command
,
7060 .mode
= COMMAND_EXEC
,
7061 .help
= "display memory half-words",
7062 .usage
= "['phys'] address [count]",
7066 .handler
= handle_md_command
,
7067 .mode
= COMMAND_EXEC
,
7068 .help
= "display memory bytes",
7069 .usage
= "['phys'] address [count]",
7073 .handler
= handle_mw_command
,
7074 .mode
= COMMAND_EXEC
,
7075 .help
= "write memory double-word",
7076 .usage
= "['phys'] address value [count]",
7080 .handler
= handle_mw_command
,
7081 .mode
= COMMAND_EXEC
,
7082 .help
= "write memory word",
7083 .usage
= "['phys'] address value [count]",
7087 .handler
= handle_mw_command
,
7088 .mode
= COMMAND_EXEC
,
7089 .help
= "write memory half-word",
7090 .usage
= "['phys'] address value [count]",
7094 .handler
= handle_mw_command
,
7095 .mode
= COMMAND_EXEC
,
7096 .help
= "write memory byte",
7097 .usage
= "['phys'] address value [count]",
7101 .handler
= handle_bp_command
,
7102 .mode
= COMMAND_EXEC
,
7103 .help
= "list or set hardware or software breakpoint",
7104 .usage
= "[<address> [<asid>] <length> ['hw'|'hw_ctx']]",
7108 .handler
= handle_rbp_command
,
7109 .mode
= COMMAND_EXEC
,
7110 .help
= "remove breakpoint",
7111 .usage
= "'all' | address",
7115 .handler
= handle_wp_command
,
7116 .mode
= COMMAND_EXEC
,
7117 .help
= "list (no params) or create watchpoints",
7118 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
7122 .handler
= handle_rwp_command
,
7123 .mode
= COMMAND_EXEC
,
7124 .help
= "remove watchpoint",
7128 .name
= "load_image",
7129 .handler
= handle_load_image_command
,
7130 .mode
= COMMAND_EXEC
,
7131 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
7132 "[min_address] [max_length]",
7135 .name
= "dump_image",
7136 .handler
= handle_dump_image_command
,
7137 .mode
= COMMAND_EXEC
,
7138 .usage
= "filename address size",
7141 .name
= "verify_image_checksum",
7142 .handler
= handle_verify_image_checksum_command
,
7143 .mode
= COMMAND_EXEC
,
7144 .usage
= "filename [offset [type]]",
7147 .name
= "verify_image",
7148 .handler
= handle_verify_image_command
,
7149 .mode
= COMMAND_EXEC
,
7150 .usage
= "filename [offset [type]]",
7153 .name
= "test_image",
7154 .handler
= handle_test_image_command
,
7155 .mode
= COMMAND_EXEC
,
7156 .usage
= "filename [offset [type]]",
7160 .mode
= COMMAND_EXEC
,
7161 .jim_handler
= target_jim_get_reg
,
7162 .help
= "Get register values from the target",
7167 .mode
= COMMAND_EXEC
,
7168 .jim_handler
= target_jim_set_reg
,
7169 .help
= "Set target register values",
7173 .name
= "read_memory",
7174 .mode
= COMMAND_EXEC
,
7175 .jim_handler
= target_jim_read_memory
,
7176 .help
= "Read Tcl list of 8/16/32/64 bit numbers from target memory",
7177 .usage
= "address width count ['phys']",
7180 .name
= "write_memory",
7181 .mode
= COMMAND_EXEC
,
7182 .jim_handler
= target_jim_write_memory
,
7183 .help
= "Write Tcl list of 8/16/32/64 bit numbers to target memory",
7184 .usage
= "address width data ['phys']",
7187 .name
= "reset_nag",
7188 .handler
= handle_target_reset_nag
,
7189 .mode
= COMMAND_ANY
,
7190 .help
= "Nag after each reset about options that could have been "
7191 "enabled to improve performance.",
7192 .usage
= "['enable'|'disable']",
7196 .handler
= handle_ps_command
,
7197 .mode
= COMMAND_EXEC
,
7198 .help
= "list all tasks",
7202 .name
= "test_mem_access",
7203 .handler
= handle_test_mem_access_command
,
7204 .mode
= COMMAND_EXEC
,
7205 .help
= "Test the target's memory access functions",
7209 COMMAND_REGISTRATION_DONE
7211 static int target_register_user_commands(struct command_context
*cmd_ctx
)
7213 int retval
= ERROR_OK
;
7214 retval
= target_request_register_commands(cmd_ctx
);
7215 if (retval
!= ERROR_OK
)
7218 retval
= trace_register_commands(cmd_ctx
);
7219 if (retval
!= ERROR_OK
)
7223 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);