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/time_support.h>
45 #include <jtag/jtag.h>
46 #include <flash/nor/core.h>
49 #include "target_type.h"
50 #include "target_request.h"
51 #include "breakpoints.h"
55 #include "rtos/rtos.h"
56 #include "transport/transport.h"
59 /* default halt wait timeout (ms) */
60 #define DEFAULT_HALT_TIMEOUT 5000
62 static int target_read_buffer_default(struct target
*target
, target_addr_t address
,
63 uint32_t count
, uint8_t *buffer
);
64 static int target_write_buffer_default(struct target
*target
, target_addr_t address
,
65 uint32_t count
, const uint8_t *buffer
);
66 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
67 int argc
, Jim_Obj
* const *argv
);
68 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
69 int argc
, Jim_Obj
* const *argv
);
70 static int target_register_user_commands(struct command_context
*cmd_ctx
);
71 static int target_get_gdb_fileio_info_default(struct target
*target
,
72 struct gdb_fileio_info
*fileio_info
);
73 static int target_gdb_fileio_end_default(struct target
*target
, int retcode
,
74 int fileio_errno
, bool ctrl_c
);
75 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
76 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
);
79 extern struct target_type arm7tdmi_target
;
80 extern struct target_type arm720t_target
;
81 extern struct target_type arm9tdmi_target
;
82 extern struct target_type arm920t_target
;
83 extern struct target_type arm966e_target
;
84 extern struct target_type arm946e_target
;
85 extern struct target_type arm926ejs_target
;
86 extern struct target_type fa526_target
;
87 extern struct target_type feroceon_target
;
88 extern struct target_type dragonite_target
;
89 extern struct target_type xscale_target
;
90 extern struct target_type cortexm_target
;
91 extern struct target_type cortexa_target
;
92 extern struct target_type aarch64_target
;
93 extern struct target_type cortexr4_target
;
94 extern struct target_type arm11_target
;
95 extern struct target_type ls1_sap_target
;
96 extern struct target_type mips_m4k_target
;
97 extern struct target_type mips_mips64_target
;
98 extern struct target_type avr_target
;
99 extern struct target_type dsp563xx_target
;
100 extern struct target_type dsp5680xx_target
;
101 extern struct target_type testee_target
;
102 extern struct target_type avr32_ap7k_target
;
103 extern struct target_type hla_target
;
104 extern struct target_type nds32_v2_target
;
105 extern struct target_type nds32_v3_target
;
106 extern struct target_type nds32_v3m_target
;
107 extern struct target_type or1k_target
;
108 extern struct target_type quark_x10xx_target
;
109 extern struct target_type quark_d20xx_target
;
110 extern struct target_type stm8_target
;
111 extern struct target_type riscv_target
;
112 extern struct target_type mem_ap_target
;
113 extern struct target_type esirisc_target
;
114 extern struct target_type arcv2_target
;
116 static struct target_type
*target_types
[] = {
156 struct target
*all_targets
;
157 static struct target_event_callback
*target_event_callbacks
;
158 static struct target_timer_callback
*target_timer_callbacks
;
159 LIST_HEAD(target_reset_callback_list
);
160 LIST_HEAD(target_trace_callback_list
);
161 static const int polling_interval
= 100;
163 static const Jim_Nvp nvp_assert
[] = {
164 { .name
= "assert", NVP_ASSERT
},
165 { .name
= "deassert", NVP_DEASSERT
},
166 { .name
= "T", NVP_ASSERT
},
167 { .name
= "F", NVP_DEASSERT
},
168 { .name
= "t", NVP_ASSERT
},
169 { .name
= "f", NVP_DEASSERT
},
170 { .name
= NULL
, .value
= -1 }
173 static const Jim_Nvp nvp_error_target
[] = {
174 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
175 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
176 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
177 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
178 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
179 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
180 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
181 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
182 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
183 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
184 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
185 { .value
= -1, .name
= NULL
}
188 static const char *target_strerror_safe(int err
)
192 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
199 static const Jim_Nvp nvp_target_event
[] = {
201 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
202 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
203 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
204 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
205 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
206 { .value
= TARGET_EVENT_STEP_START
, .name
= "step-start" },
207 { .value
= TARGET_EVENT_STEP_END
, .name
= "step-end" },
209 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
210 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
212 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
213 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
214 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
215 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
216 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
217 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
218 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
219 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
221 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
222 { .value
= TARGET_EVENT_EXAMINE_FAIL
, .name
= "examine-fail" },
223 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
225 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
226 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
228 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
229 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
231 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
232 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
234 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
235 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
237 { .value
= TARGET_EVENT_TRACE_CONFIG
, .name
= "trace-config" },
239 { .name
= NULL
, .value
= -1 }
242 static const Jim_Nvp nvp_target_state
[] = {
243 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
244 { .name
= "running", .value
= TARGET_RUNNING
},
245 { .name
= "halted", .value
= TARGET_HALTED
},
246 { .name
= "reset", .value
= TARGET_RESET
},
247 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
248 { .name
= NULL
, .value
= -1 },
251 static const Jim_Nvp nvp_target_debug_reason
[] = {
252 { .name
= "debug-request", .value
= DBG_REASON_DBGRQ
},
253 { .name
= "breakpoint", .value
= DBG_REASON_BREAKPOINT
},
254 { .name
= "watchpoint", .value
= DBG_REASON_WATCHPOINT
},
255 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
256 { .name
= "single-step", .value
= DBG_REASON_SINGLESTEP
},
257 { .name
= "target-not-halted", .value
= DBG_REASON_NOTHALTED
},
258 { .name
= "program-exit", .value
= DBG_REASON_EXIT
},
259 { .name
= "exception-catch", .value
= DBG_REASON_EXC_CATCH
},
260 { .name
= "undefined", .value
= DBG_REASON_UNDEFINED
},
261 { .name
= NULL
, .value
= -1 },
264 static const Jim_Nvp nvp_target_endian
[] = {
265 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
266 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
267 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
268 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
269 { .name
= NULL
, .value
= -1 },
272 static const Jim_Nvp nvp_reset_modes
[] = {
273 { .name
= "unknown", .value
= RESET_UNKNOWN
},
274 { .name
= "run", .value
= RESET_RUN
},
275 { .name
= "halt", .value
= RESET_HALT
},
276 { .name
= "init", .value
= RESET_INIT
},
277 { .name
= NULL
, .value
= -1 },
280 const char *debug_reason_name(struct target
*t
)
284 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
285 t
->debug_reason
)->name
;
287 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
288 cp
= "(*BUG*unknown*BUG*)";
293 const char *target_state_name(struct target
*t
)
296 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
298 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
299 cp
= "(*BUG*unknown*BUG*)";
302 if (!target_was_examined(t
) && t
->defer_examine
)
303 cp
= "examine deferred";
308 const char *target_event_name(enum target_event event
)
311 cp
= Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
;
313 LOG_ERROR("Invalid target event: %d", (int)(event
));
314 cp
= "(*BUG*unknown*BUG*)";
319 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
322 cp
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
;
324 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
325 cp
= "(*BUG*unknown*BUG*)";
330 /* determine the number of the new target */
331 static int new_target_number(void)
336 /* number is 0 based */
340 if (x
< t
->target_number
)
341 x
= t
->target_number
;
347 /* read a uint64_t from a buffer in target memory endianness */
348 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
350 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
351 return le_to_h_u64(buffer
);
353 return be_to_h_u64(buffer
);
356 /* read a uint32_t from a buffer in target memory endianness */
357 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
359 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
360 return le_to_h_u32(buffer
);
362 return be_to_h_u32(buffer
);
365 /* read a uint24_t from a buffer in target memory endianness */
366 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
368 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
369 return le_to_h_u24(buffer
);
371 return be_to_h_u24(buffer
);
374 /* read a uint16_t from a buffer in target memory endianness */
375 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
377 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
378 return le_to_h_u16(buffer
);
380 return be_to_h_u16(buffer
);
383 /* write a uint64_t to a buffer in target memory endianness */
384 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
386 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
387 h_u64_to_le(buffer
, value
);
389 h_u64_to_be(buffer
, value
);
392 /* write a uint32_t to a buffer in target memory endianness */
393 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
395 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
396 h_u32_to_le(buffer
, value
);
398 h_u32_to_be(buffer
, value
);
401 /* write a uint24_t to a buffer in target memory endianness */
402 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
404 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
405 h_u24_to_le(buffer
, value
);
407 h_u24_to_be(buffer
, value
);
410 /* write a uint16_t to a buffer in target memory endianness */
411 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
413 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
414 h_u16_to_le(buffer
, value
);
416 h_u16_to_be(buffer
, value
);
419 /* write a uint8_t to a buffer in target memory endianness */
420 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
425 /* write a uint64_t array to a buffer in target memory endianness */
426 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
429 for (i
= 0; i
< count
; i
++)
430 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
433 /* write a uint32_t array to a buffer in target memory endianness */
434 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
437 for (i
= 0; i
< count
; i
++)
438 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
441 /* write a uint16_t array to a buffer in target memory endianness */
442 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
445 for (i
= 0; i
< count
; i
++)
446 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
449 /* write a uint64_t array to a buffer in target memory endianness */
450 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
453 for (i
= 0; i
< count
; i
++)
454 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
457 /* write a uint32_t array to a buffer in target memory endianness */
458 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
461 for (i
= 0; i
< count
; i
++)
462 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
465 /* write a uint16_t array to a buffer in target memory endianness */
466 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
469 for (i
= 0; i
< count
; i
++)
470 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
473 /* return a pointer to a configured target; id is name or number */
474 struct target
*get_target(const char *id
)
476 struct target
*target
;
478 /* try as tcltarget name */
479 for (target
= all_targets
; target
; target
= target
->next
) {
480 if (target_name(target
) == NULL
)
482 if (strcmp(id
, target_name(target
)) == 0)
486 /* It's OK to remove this fallback sometime after August 2010 or so */
488 /* no match, try as number */
490 if (parse_uint(id
, &num
) != ERROR_OK
)
493 for (target
= all_targets
; target
; target
= target
->next
) {
494 if (target
->target_number
== (int)num
) {
495 LOG_WARNING("use '%s' as target identifier, not '%u'",
496 target_name(target
), num
);
504 /* returns a pointer to the n-th configured target */
505 struct target
*get_target_by_num(int num
)
507 struct target
*target
= all_targets
;
510 if (target
->target_number
== num
)
512 target
= target
->next
;
518 struct target
*get_current_target(struct command_context
*cmd_ctx
)
520 struct target
*target
= get_current_target_or_null(cmd_ctx
);
522 if (target
== NULL
) {
523 LOG_ERROR("BUG: current_target out of bounds");
530 struct target
*get_current_target_or_null(struct command_context
*cmd_ctx
)
532 return cmd_ctx
->current_target_override
533 ? cmd_ctx
->current_target_override
534 : cmd_ctx
->current_target
;
537 int target_poll(struct target
*target
)
541 /* We can't poll until after examine */
542 if (!target_was_examined(target
)) {
543 /* Fail silently lest we pollute the log */
547 retval
= target
->type
->poll(target
);
548 if (retval
!= ERROR_OK
)
551 if (target
->halt_issued
) {
552 if (target
->state
== TARGET_HALTED
)
553 target
->halt_issued
= false;
555 int64_t t
= timeval_ms() - target
->halt_issued_time
;
556 if (t
> DEFAULT_HALT_TIMEOUT
) {
557 target
->halt_issued
= false;
558 LOG_INFO("Halt timed out, wake up GDB.");
559 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
567 int target_halt(struct target
*target
)
570 /* We can't poll until after examine */
571 if (!target_was_examined(target
)) {
572 LOG_ERROR("Target not examined yet");
576 retval
= target
->type
->halt(target
);
577 if (retval
!= ERROR_OK
)
580 target
->halt_issued
= true;
581 target
->halt_issued_time
= timeval_ms();
587 * Make the target (re)start executing using its saved execution
588 * context (possibly with some modifications).
590 * @param target Which target should start executing.
591 * @param current True to use the target's saved program counter instead
592 * of the address parameter
593 * @param address Optionally used as the program counter.
594 * @param handle_breakpoints True iff breakpoints at the resumption PC
595 * should be skipped. (For example, maybe execution was stopped by
596 * such a breakpoint, in which case it would be counterproductive to
598 * @param debug_execution False if all working areas allocated by OpenOCD
599 * should be released and/or restored to their original contents.
600 * (This would for example be true to run some downloaded "helper"
601 * algorithm code, which resides in one such working buffer and uses
602 * another for data storage.)
604 * @todo Resolve the ambiguity about what the "debug_execution" flag
605 * signifies. For example, Target implementations don't agree on how
606 * it relates to invalidation of the register cache, or to whether
607 * breakpoints and watchpoints should be enabled. (It would seem wrong
608 * to enable breakpoints when running downloaded "helper" algorithms
609 * (debug_execution true), since the breakpoints would be set to match
610 * target firmware being debugged, not the helper algorithm.... and
611 * enabling them could cause such helpers to malfunction (for example,
612 * by overwriting data with a breakpoint instruction. On the other
613 * hand the infrastructure for running such helpers might use this
614 * procedure but rely on hardware breakpoint to detect termination.)
616 int target_resume(struct target
*target
, int current
, target_addr_t address
,
617 int handle_breakpoints
, int debug_execution
)
621 /* We can't poll until after examine */
622 if (!target_was_examined(target
)) {
623 LOG_ERROR("Target not examined yet");
627 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
629 /* note that resume *must* be asynchronous. The CPU can halt before
630 * we poll. The CPU can even halt at the current PC as a result of
631 * a software breakpoint being inserted by (a bug?) the application.
633 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
634 if (retval
!= ERROR_OK
)
637 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
642 static int target_process_reset(struct command_invocation
*cmd
, enum target_reset_mode reset_mode
)
647 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
648 if (n
->name
== NULL
) {
649 LOG_ERROR("invalid reset mode");
653 struct target
*target
;
654 for (target
= all_targets
; target
; target
= target
->next
)
655 target_call_reset_callbacks(target
, reset_mode
);
657 /* disable polling during reset to make reset event scripts
658 * more predictable, i.e. dr/irscan & pathmove in events will
659 * not have JTAG operations injected into the middle of a sequence.
661 bool save_poll
= jtag_poll_get_enabled();
663 jtag_poll_set_enabled(false);
665 sprintf(buf
, "ocd_process_reset %s", n
->name
);
666 retval
= Jim_Eval(cmd
->ctx
->interp
, buf
);
668 jtag_poll_set_enabled(save_poll
);
670 if (retval
!= JIM_OK
) {
671 Jim_MakeErrorMessage(cmd
->ctx
->interp
);
672 command_print(cmd
, "%s", Jim_GetString(Jim_GetResult(cmd
->ctx
->interp
), NULL
));
676 /* We want any events to be processed before the prompt */
677 retval
= target_call_timer_callbacks_now();
679 for (target
= all_targets
; target
; target
= target
->next
) {
680 target
->type
->check_reset(target
);
681 target
->running_alg
= false;
687 static int identity_virt2phys(struct target
*target
,
688 target_addr_t
virtual, target_addr_t
*physical
)
694 static int no_mmu(struct target
*target
, int *enabled
)
700 static int default_examine(struct target
*target
)
702 target_set_examined(target
);
706 /* no check by default */
707 static int default_check_reset(struct target
*target
)
712 /* Equivalent Tcl code arp_examine_one is in src/target/startup.tcl
714 int target_examine_one(struct target
*target
)
716 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
718 int retval
= target
->type
->examine(target
);
719 if (retval
!= ERROR_OK
) {
720 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_FAIL
);
724 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
729 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
731 struct target
*target
= priv
;
733 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
736 jtag_unregister_event_callback(jtag_enable_callback
, target
);
738 return target_examine_one(target
);
741 /* Targets that correctly implement init + examine, i.e.
742 * no communication with target during init:
746 int target_examine(void)
748 int retval
= ERROR_OK
;
749 struct target
*target
;
751 for (target
= all_targets
; target
; target
= target
->next
) {
752 /* defer examination, but don't skip it */
753 if (!target
->tap
->enabled
) {
754 jtag_register_event_callback(jtag_enable_callback
,
759 if (target
->defer_examine
)
762 retval
= target_examine_one(target
);
763 if (retval
!= ERROR_OK
)
769 const char *target_type_name(struct target
*target
)
771 return target
->type
->name
;
774 static int target_soft_reset_halt(struct target
*target
)
776 if (!target_was_examined(target
)) {
777 LOG_ERROR("Target not examined yet");
780 if (!target
->type
->soft_reset_halt
) {
781 LOG_ERROR("Target %s does not support soft_reset_halt",
782 target_name(target
));
785 return target
->type
->soft_reset_halt(target
);
789 * Downloads a target-specific native code algorithm to the target,
790 * and executes it. * Note that some targets may need to set up, enable,
791 * and tear down a breakpoint (hard or * soft) to detect algorithm
792 * termination, while others may support lower overhead schemes where
793 * soft breakpoints embedded in the algorithm automatically terminate the
796 * @param target used to run the algorithm
797 * @param arch_info target-specific description of the algorithm.
799 int target_run_algorithm(struct target
*target
,
800 int num_mem_params
, struct mem_param
*mem_params
,
801 int num_reg_params
, struct reg_param
*reg_param
,
802 uint32_t entry_point
, uint32_t exit_point
,
803 int timeout_ms
, void *arch_info
)
805 int retval
= ERROR_FAIL
;
807 if (!target_was_examined(target
)) {
808 LOG_ERROR("Target not examined yet");
811 if (!target
->type
->run_algorithm
) {
812 LOG_ERROR("Target type '%s' does not support %s",
813 target_type_name(target
), __func__
);
817 target
->running_alg
= true;
818 retval
= target
->type
->run_algorithm(target
,
819 num_mem_params
, mem_params
,
820 num_reg_params
, reg_param
,
821 entry_point
, exit_point
, timeout_ms
, arch_info
);
822 target
->running_alg
= false;
829 * Executes a target-specific native code algorithm and leaves it running.
831 * @param target used to run the algorithm
832 * @param arch_info target-specific description of the algorithm.
834 int target_start_algorithm(struct target
*target
,
835 int num_mem_params
, struct mem_param
*mem_params
,
836 int num_reg_params
, struct reg_param
*reg_params
,
837 uint32_t entry_point
, uint32_t exit_point
,
840 int retval
= ERROR_FAIL
;
842 if (!target_was_examined(target
)) {
843 LOG_ERROR("Target not examined yet");
846 if (!target
->type
->start_algorithm
) {
847 LOG_ERROR("Target type '%s' does not support %s",
848 target_type_name(target
), __func__
);
851 if (target
->running_alg
) {
852 LOG_ERROR("Target is already running an algorithm");
856 target
->running_alg
= true;
857 retval
= target
->type
->start_algorithm(target
,
858 num_mem_params
, mem_params
,
859 num_reg_params
, reg_params
,
860 entry_point
, exit_point
, arch_info
);
867 * Waits for an algorithm started with target_start_algorithm() to complete.
869 * @param target used to run the algorithm
870 * @param arch_info target-specific description of the algorithm.
872 int target_wait_algorithm(struct target
*target
,
873 int num_mem_params
, struct mem_param
*mem_params
,
874 int num_reg_params
, struct reg_param
*reg_params
,
875 uint32_t exit_point
, int timeout_ms
,
878 int retval
= ERROR_FAIL
;
880 if (!target
->type
->wait_algorithm
) {
881 LOG_ERROR("Target type '%s' does not support %s",
882 target_type_name(target
), __func__
);
885 if (!target
->running_alg
) {
886 LOG_ERROR("Target is not running an algorithm");
890 retval
= target
->type
->wait_algorithm(target
,
891 num_mem_params
, mem_params
,
892 num_reg_params
, reg_params
,
893 exit_point
, timeout_ms
, arch_info
);
894 if (retval
!= ERROR_TARGET_TIMEOUT
)
895 target
->running_alg
= false;
902 * Streams data to a circular buffer on target intended for consumption by code
903 * running asynchronously on target.
905 * This is intended for applications where target-specific native code runs
906 * on the target, receives data from the circular buffer, does something with
907 * it (most likely writing it to a flash memory), and advances the circular
910 * This assumes that the helper algorithm has already been loaded to the target,
911 * but has not been started yet. Given memory and register parameters are passed
914 * The buffer is defined by (buffer_start, buffer_size) arguments and has the
917 * [buffer_start + 0, buffer_start + 4):
918 * Write Pointer address (aka head). Written and updated by this
919 * routine when new data is written to the circular buffer.
920 * [buffer_start + 4, buffer_start + 8):
921 * Read Pointer address (aka tail). Updated by code running on the
922 * target after it consumes data.
923 * [buffer_start + 8, buffer_start + buffer_size):
924 * Circular buffer contents.
926 * See contrib/loaders/flash/stm32f1x.S for an example.
928 * @param target used to run the algorithm
929 * @param buffer address on the host where data to be sent is located
930 * @param count number of blocks to send
931 * @param block_size size in bytes of each block
932 * @param num_mem_params count of memory-based params to pass to algorithm
933 * @param mem_params memory-based params to pass to algorithm
934 * @param num_reg_params count of register-based params to pass to algorithm
935 * @param reg_params memory-based params to pass to algorithm
936 * @param buffer_start address on the target of the circular buffer structure
937 * @param buffer_size size of the circular buffer structure
938 * @param entry_point address on the target to execute to start the algorithm
939 * @param exit_point address at which to set a breakpoint to catch the
940 * end of the algorithm; can be 0 if target triggers a breakpoint itself
943 int target_run_flash_async_algorithm(struct target
*target
,
944 const uint8_t *buffer
, uint32_t count
, int block_size
,
945 int num_mem_params
, struct mem_param
*mem_params
,
946 int num_reg_params
, struct reg_param
*reg_params
,
947 uint32_t buffer_start
, uint32_t buffer_size
,
948 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
953 const uint8_t *buffer_orig
= buffer
;
955 /* Set up working area. First word is write pointer, second word is read pointer,
956 * rest is fifo data area. */
957 uint32_t wp_addr
= buffer_start
;
958 uint32_t rp_addr
= buffer_start
+ 4;
959 uint32_t fifo_start_addr
= buffer_start
+ 8;
960 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
962 uint32_t wp
= fifo_start_addr
;
963 uint32_t rp
= fifo_start_addr
;
965 /* validate block_size is 2^n */
966 assert(!block_size
|| !(block_size
& (block_size
- 1)));
968 retval
= target_write_u32(target
, wp_addr
, wp
);
969 if (retval
!= ERROR_OK
)
971 retval
= target_write_u32(target
, rp_addr
, rp
);
972 if (retval
!= ERROR_OK
)
975 /* Start up algorithm on target and let it idle while writing the first chunk */
976 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
977 num_reg_params
, reg_params
,
982 if (retval
!= ERROR_OK
) {
983 LOG_ERROR("error starting target flash write algorithm");
989 retval
= target_read_u32(target
, rp_addr
, &rp
);
990 if (retval
!= ERROR_OK
) {
991 LOG_ERROR("failed to get read pointer");
995 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
996 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
999 LOG_ERROR("flash write algorithm aborted by target");
1000 retval
= ERROR_FLASH_OPERATION_FAILED
;
1004 if (((rp
- fifo_start_addr
) & (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
1005 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
1009 /* Count the number of bytes available in the fifo without
1010 * crossing the wrap around. Make sure to not fill it completely,
1011 * because that would make wp == rp and that's the empty condition. */
1012 uint32_t thisrun_bytes
;
1014 thisrun_bytes
= rp
- wp
- block_size
;
1015 else if (rp
> fifo_start_addr
)
1016 thisrun_bytes
= fifo_end_addr
- wp
;
1018 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
1020 if (thisrun_bytes
== 0) {
1021 /* Throttle polling a bit if transfer is (much) faster than flash
1022 * programming. The exact delay shouldn't matter as long as it's
1023 * less than buffer size / flash speed. This is very unlikely to
1024 * run when using high latency connections such as USB. */
1027 /* to stop an infinite loop on some targets check and increment a timeout
1028 * this issue was observed on a stellaris using the new ICDI interface */
1029 if (timeout
++ >= 500) {
1030 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1031 return ERROR_FLASH_OPERATION_FAILED
;
1036 /* reset our timeout */
1039 /* Limit to the amount of data we actually want to write */
1040 if (thisrun_bytes
> count
* block_size
)
1041 thisrun_bytes
= count
* block_size
;
1043 /* Write data to fifo */
1044 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
1045 if (retval
!= ERROR_OK
)
1048 /* Update counters and wrap write pointer */
1049 buffer
+= thisrun_bytes
;
1050 count
-= thisrun_bytes
/ block_size
;
1051 wp
+= thisrun_bytes
;
1052 if (wp
>= fifo_end_addr
)
1053 wp
= fifo_start_addr
;
1055 /* Store updated write pointer to target */
1056 retval
= target_write_u32(target
, wp_addr
, wp
);
1057 if (retval
!= ERROR_OK
)
1060 /* Avoid GDB timeouts */
1064 if (retval
!= ERROR_OK
) {
1065 /* abort flash write algorithm on target */
1066 target_write_u32(target
, wp_addr
, 0);
1069 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1070 num_reg_params
, reg_params
,
1075 if (retval2
!= ERROR_OK
) {
1076 LOG_ERROR("error waiting for target flash write algorithm");
1080 if (retval
== ERROR_OK
) {
1081 /* check if algorithm set rp = 0 after fifo writer loop finished */
1082 retval
= target_read_u32(target
, rp_addr
, &rp
);
1083 if (retval
== ERROR_OK
&& rp
== 0) {
1084 LOG_ERROR("flash write algorithm aborted by target");
1085 retval
= ERROR_FLASH_OPERATION_FAILED
;
1092 int target_read_memory(struct target
*target
,
1093 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1095 if (!target_was_examined(target
)) {
1096 LOG_ERROR("Target not examined yet");
1099 if (!target
->type
->read_memory
) {
1100 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1103 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1106 int target_read_phys_memory(struct target
*target
,
1107 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1109 if (!target_was_examined(target
)) {
1110 LOG_ERROR("Target not examined yet");
1113 if (!target
->type
->read_phys_memory
) {
1114 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1117 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1120 int target_write_memory(struct target
*target
,
1121 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1123 if (!target_was_examined(target
)) {
1124 LOG_ERROR("Target not examined yet");
1127 if (!target
->type
->write_memory
) {
1128 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1131 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1134 int target_write_phys_memory(struct target
*target
,
1135 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1137 if (!target_was_examined(target
)) {
1138 LOG_ERROR("Target not examined yet");
1141 if (!target
->type
->write_phys_memory
) {
1142 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1145 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1148 int target_add_breakpoint(struct target
*target
,
1149 struct breakpoint
*breakpoint
)
1151 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1152 LOG_WARNING("target %s is not halted (add breakpoint)", target_name(target
));
1153 return ERROR_TARGET_NOT_HALTED
;
1155 return target
->type
->add_breakpoint(target
, breakpoint
);
1158 int target_add_context_breakpoint(struct target
*target
,
1159 struct breakpoint
*breakpoint
)
1161 if (target
->state
!= TARGET_HALTED
) {
1162 LOG_WARNING("target %s is not halted (add context breakpoint)", target_name(target
));
1163 return ERROR_TARGET_NOT_HALTED
;
1165 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1168 int target_add_hybrid_breakpoint(struct target
*target
,
1169 struct breakpoint
*breakpoint
)
1171 if (target
->state
!= TARGET_HALTED
) {
1172 LOG_WARNING("target %s is not halted (add hybrid breakpoint)", target_name(target
));
1173 return ERROR_TARGET_NOT_HALTED
;
1175 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1178 int target_remove_breakpoint(struct target
*target
,
1179 struct breakpoint
*breakpoint
)
1181 return target
->type
->remove_breakpoint(target
, breakpoint
);
1184 int target_add_watchpoint(struct target
*target
,
1185 struct watchpoint
*watchpoint
)
1187 if (target
->state
!= TARGET_HALTED
) {
1188 LOG_WARNING("target %s is not halted (add watchpoint)", target_name(target
));
1189 return ERROR_TARGET_NOT_HALTED
;
1191 return target
->type
->add_watchpoint(target
, watchpoint
);
1193 int target_remove_watchpoint(struct target
*target
,
1194 struct watchpoint
*watchpoint
)
1196 return target
->type
->remove_watchpoint(target
, watchpoint
);
1198 int target_hit_watchpoint(struct target
*target
,
1199 struct watchpoint
**hit_watchpoint
)
1201 if (target
->state
!= TARGET_HALTED
) {
1202 LOG_WARNING("target %s is not halted (hit watchpoint)", target
->cmd_name
);
1203 return ERROR_TARGET_NOT_HALTED
;
1206 if (target
->type
->hit_watchpoint
== NULL
) {
1207 /* For backward compatible, if hit_watchpoint is not implemented,
1208 * return ERROR_FAIL such that gdb_server will not take the nonsense
1213 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1216 const char *target_get_gdb_arch(struct target
*target
)
1218 if (target
->type
->get_gdb_arch
== NULL
)
1220 return target
->type
->get_gdb_arch(target
);
1223 int target_get_gdb_reg_list(struct target
*target
,
1224 struct reg
**reg_list
[], int *reg_list_size
,
1225 enum target_register_class reg_class
)
1227 int result
= ERROR_FAIL
;
1229 if (!target_was_examined(target
)) {
1230 LOG_ERROR("Target not examined yet");
1234 result
= target
->type
->get_gdb_reg_list(target
, reg_list
,
1235 reg_list_size
, reg_class
);
1238 if (result
!= ERROR_OK
) {
1245 int target_get_gdb_reg_list_noread(struct target
*target
,
1246 struct reg
**reg_list
[], int *reg_list_size
,
1247 enum target_register_class reg_class
)
1249 if (target
->type
->get_gdb_reg_list_noread
&&
1250 target
->type
->get_gdb_reg_list_noread(target
, reg_list
,
1251 reg_list_size
, reg_class
) == ERROR_OK
)
1253 return target_get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1256 bool target_supports_gdb_connection(struct target
*target
)
1259 * based on current code, we can simply exclude all the targets that
1260 * don't provide get_gdb_reg_list; this could change with new targets.
1262 return !!target
->type
->get_gdb_reg_list
;
1265 int target_step(struct target
*target
,
1266 int current
, target_addr_t address
, int handle_breakpoints
)
1270 target_call_event_callbacks(target
, TARGET_EVENT_STEP_START
);
1272 retval
= target
->type
->step(target
, current
, address
, handle_breakpoints
);
1273 if (retval
!= ERROR_OK
)
1276 target_call_event_callbacks(target
, TARGET_EVENT_STEP_END
);
1281 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1283 if (target
->state
!= TARGET_HALTED
) {
1284 LOG_WARNING("target %s is not halted (gdb fileio)", target
->cmd_name
);
1285 return ERROR_TARGET_NOT_HALTED
;
1287 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1290 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1292 if (target
->state
!= TARGET_HALTED
) {
1293 LOG_WARNING("target %s is not halted (gdb fileio end)", target
->cmd_name
);
1294 return ERROR_TARGET_NOT_HALTED
;
1296 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1299 target_addr_t
target_address_max(struct target
*target
)
1301 unsigned bits
= target_address_bits(target
);
1302 if (sizeof(target_addr_t
) * 8 == bits
)
1303 return (target_addr_t
) -1;
1305 return (((target_addr_t
) 1) << bits
) - 1;
1308 unsigned target_address_bits(struct target
*target
)
1310 if (target
->type
->address_bits
)
1311 return target
->type
->address_bits(target
);
1315 int target_profiling(struct target
*target
, uint32_t *samples
,
1316 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1318 if (target
->state
!= TARGET_HALTED
) {
1319 LOG_WARNING("target %s is not halted (profiling)", target
->cmd_name
);
1320 return ERROR_TARGET_NOT_HALTED
;
1322 return target
->type
->profiling(target
, samples
, max_num_samples
,
1323 num_samples
, seconds
);
1327 * Reset the @c examined flag for the given target.
1328 * Pure paranoia -- targets are zeroed on allocation.
1330 static void target_reset_examined(struct target
*target
)
1332 target
->examined
= false;
1335 static int handle_target(void *priv
);
1337 static int target_init_one(struct command_context
*cmd_ctx
,
1338 struct target
*target
)
1340 target_reset_examined(target
);
1342 struct target_type
*type
= target
->type
;
1343 if (type
->examine
== NULL
)
1344 type
->examine
= default_examine
;
1346 if (type
->check_reset
== NULL
)
1347 type
->check_reset
= default_check_reset
;
1349 assert(type
->init_target
!= NULL
);
1351 int retval
= type
->init_target(cmd_ctx
, target
);
1352 if (ERROR_OK
!= retval
) {
1353 LOG_ERROR("target '%s' init failed", target_name(target
));
1357 /* Sanity-check MMU support ... stub in what we must, to help
1358 * implement it in stages, but warn if we need to do so.
1361 if (type
->virt2phys
== NULL
) {
1362 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1363 type
->virt2phys
= identity_virt2phys
;
1366 /* Make sure no-MMU targets all behave the same: make no
1367 * distinction between physical and virtual addresses, and
1368 * ensure that virt2phys() is always an identity mapping.
1370 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1371 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1374 type
->write_phys_memory
= type
->write_memory
;
1375 type
->read_phys_memory
= type
->read_memory
;
1376 type
->virt2phys
= identity_virt2phys
;
1379 if (target
->type
->read_buffer
== NULL
)
1380 target
->type
->read_buffer
= target_read_buffer_default
;
1382 if (target
->type
->write_buffer
== NULL
)
1383 target
->type
->write_buffer
= target_write_buffer_default
;
1385 if (target
->type
->get_gdb_fileio_info
== NULL
)
1386 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1388 if (target
->type
->gdb_fileio_end
== NULL
)
1389 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1391 if (target
->type
->profiling
== NULL
)
1392 target
->type
->profiling
= target_profiling_default
;
1397 static int target_init(struct command_context
*cmd_ctx
)
1399 struct target
*target
;
1402 for (target
= all_targets
; target
; target
= target
->next
) {
1403 retval
= target_init_one(cmd_ctx
, target
);
1404 if (ERROR_OK
!= retval
)
1411 retval
= target_register_user_commands(cmd_ctx
);
1412 if (ERROR_OK
!= retval
)
1415 retval
= target_register_timer_callback(&handle_target
,
1416 polling_interval
, TARGET_TIMER_TYPE_PERIODIC
, cmd_ctx
->interp
);
1417 if (ERROR_OK
!= retval
)
1423 COMMAND_HANDLER(handle_target_init_command
)
1428 return ERROR_COMMAND_SYNTAX_ERROR
;
1430 static bool target_initialized
;
1431 if (target_initialized
) {
1432 LOG_INFO("'target init' has already been called");
1435 target_initialized
= true;
1437 retval
= command_run_line(CMD_CTX
, "init_targets");
1438 if (ERROR_OK
!= retval
)
1441 retval
= command_run_line(CMD_CTX
, "init_target_events");
1442 if (ERROR_OK
!= retval
)
1445 retval
= command_run_line(CMD_CTX
, "init_board");
1446 if (ERROR_OK
!= retval
)
1449 LOG_DEBUG("Initializing targets...");
1450 return target_init(CMD_CTX
);
1453 int target_register_event_callback(int (*callback
)(struct target
*target
,
1454 enum target_event event
, void *priv
), void *priv
)
1456 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1458 if (callback
== NULL
)
1459 return ERROR_COMMAND_SYNTAX_ERROR
;
1462 while ((*callbacks_p
)->next
)
1463 callbacks_p
= &((*callbacks_p
)->next
);
1464 callbacks_p
= &((*callbacks_p
)->next
);
1467 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1468 (*callbacks_p
)->callback
= callback
;
1469 (*callbacks_p
)->priv
= priv
;
1470 (*callbacks_p
)->next
= NULL
;
1475 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1476 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1478 struct target_reset_callback
*entry
;
1480 if (callback
== NULL
)
1481 return ERROR_COMMAND_SYNTAX_ERROR
;
1483 entry
= malloc(sizeof(struct target_reset_callback
));
1484 if (entry
== NULL
) {
1485 LOG_ERROR("error allocating buffer for reset callback entry");
1486 return ERROR_COMMAND_SYNTAX_ERROR
;
1489 entry
->callback
= callback
;
1491 list_add(&entry
->list
, &target_reset_callback_list
);
1497 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1498 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1500 struct target_trace_callback
*entry
;
1502 if (callback
== NULL
)
1503 return ERROR_COMMAND_SYNTAX_ERROR
;
1505 entry
= malloc(sizeof(struct target_trace_callback
));
1506 if (entry
== NULL
) {
1507 LOG_ERROR("error allocating buffer for trace callback entry");
1508 return ERROR_COMMAND_SYNTAX_ERROR
;
1511 entry
->callback
= callback
;
1513 list_add(&entry
->list
, &target_trace_callback_list
);
1519 int target_register_timer_callback(int (*callback
)(void *priv
),
1520 unsigned int time_ms
, enum target_timer_type type
, void *priv
)
1522 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1524 if (callback
== NULL
)
1525 return ERROR_COMMAND_SYNTAX_ERROR
;
1528 while ((*callbacks_p
)->next
)
1529 callbacks_p
= &((*callbacks_p
)->next
);
1530 callbacks_p
= &((*callbacks_p
)->next
);
1533 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1534 (*callbacks_p
)->callback
= callback
;
1535 (*callbacks_p
)->type
= type
;
1536 (*callbacks_p
)->time_ms
= time_ms
;
1537 (*callbacks_p
)->removed
= false;
1539 gettimeofday(&(*callbacks_p
)->when
, NULL
);
1540 timeval_add_time(&(*callbacks_p
)->when
, 0, time_ms
* 1000);
1542 (*callbacks_p
)->priv
= priv
;
1543 (*callbacks_p
)->next
= NULL
;
1548 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1549 enum target_event event
, void *priv
), void *priv
)
1551 struct target_event_callback
**p
= &target_event_callbacks
;
1552 struct target_event_callback
*c
= target_event_callbacks
;
1554 if (callback
== NULL
)
1555 return ERROR_COMMAND_SYNTAX_ERROR
;
1558 struct target_event_callback
*next
= c
->next
;
1559 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1571 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1572 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1574 struct target_reset_callback
*entry
;
1576 if (callback
== NULL
)
1577 return ERROR_COMMAND_SYNTAX_ERROR
;
1579 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1580 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1581 list_del(&entry
->list
);
1590 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1591 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1593 struct target_trace_callback
*entry
;
1595 if (callback
== NULL
)
1596 return ERROR_COMMAND_SYNTAX_ERROR
;
1598 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1599 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1600 list_del(&entry
->list
);
1609 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1611 if (callback
== NULL
)
1612 return ERROR_COMMAND_SYNTAX_ERROR
;
1614 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1616 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1625 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1627 struct target_event_callback
*callback
= target_event_callbacks
;
1628 struct target_event_callback
*next_callback
;
1630 if (event
== TARGET_EVENT_HALTED
) {
1631 /* execute early halted first */
1632 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1635 LOG_DEBUG("target event %i (%s) for core %s", event
,
1636 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
,
1637 target_name(target
));
1639 target_handle_event(target
, event
);
1642 next_callback
= callback
->next
;
1643 callback
->callback(target
, event
, callback
->priv
);
1644 callback
= next_callback
;
1650 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1652 struct target_reset_callback
*callback
;
1654 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1655 Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1657 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1658 callback
->callback(target
, reset_mode
, callback
->priv
);
1663 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1665 struct target_trace_callback
*callback
;
1667 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1668 callback
->callback(target
, len
, data
, callback
->priv
);
1673 static int target_timer_callback_periodic_restart(
1674 struct target_timer_callback
*cb
, struct timeval
*now
)
1677 timeval_add_time(&cb
->when
, 0, cb
->time_ms
* 1000L);
1681 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1682 struct timeval
*now
)
1684 cb
->callback(cb
->priv
);
1686 if (cb
->type
== TARGET_TIMER_TYPE_PERIODIC
)
1687 return target_timer_callback_periodic_restart(cb
, now
);
1689 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1692 static int target_call_timer_callbacks_check_time(int checktime
)
1694 static bool callback_processing
;
1696 /* Do not allow nesting */
1697 if (callback_processing
)
1700 callback_processing
= true;
1705 gettimeofday(&now
, NULL
);
1707 /* Store an address of the place containing a pointer to the
1708 * next item; initially, that's a standalone "root of the
1709 * list" variable. */
1710 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1711 while (callback
&& *callback
) {
1712 if ((*callback
)->removed
) {
1713 struct target_timer_callback
*p
= *callback
;
1714 *callback
= (*callback
)->next
;
1719 bool call_it
= (*callback
)->callback
&&
1720 ((!checktime
&& (*callback
)->type
== TARGET_TIMER_TYPE_PERIODIC
) ||
1721 timeval_compare(&now
, &(*callback
)->when
) >= 0);
1724 target_call_timer_callback(*callback
, &now
);
1726 callback
= &(*callback
)->next
;
1729 callback_processing
= false;
1733 int target_call_timer_callbacks(void)
1735 return target_call_timer_callbacks_check_time(1);
1738 /* invoke periodic callbacks immediately */
1739 int target_call_timer_callbacks_now(void)
1741 return target_call_timer_callbacks_check_time(0);
1744 /* Prints the working area layout for debug purposes */
1745 static void print_wa_layout(struct target
*target
)
1747 struct working_area
*c
= target
->working_areas
;
1750 LOG_DEBUG("%c%c " TARGET_ADDR_FMT
"-" TARGET_ADDR_FMT
" (%" PRIu32
" bytes)",
1751 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1752 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1757 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1758 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1760 assert(area
->free
); /* Shouldn't split an allocated area */
1761 assert(size
<= area
->size
); /* Caller should guarantee this */
1763 /* Split only if not already the right size */
1764 if (size
< area
->size
) {
1765 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1770 new_wa
->next
= area
->next
;
1771 new_wa
->size
= area
->size
- size
;
1772 new_wa
->address
= area
->address
+ size
;
1773 new_wa
->backup
= NULL
;
1774 new_wa
->user
= NULL
;
1775 new_wa
->free
= true;
1777 area
->next
= new_wa
;
1780 /* If backup memory was allocated to this area, it has the wrong size
1781 * now so free it and it will be reallocated if/when needed */
1784 area
->backup
= NULL
;
1789 /* Merge all adjacent free areas into one */
1790 static void target_merge_working_areas(struct target
*target
)
1792 struct working_area
*c
= target
->working_areas
;
1794 while (c
&& c
->next
) {
1795 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1797 /* Find two adjacent free areas */
1798 if (c
->free
&& c
->next
->free
) {
1799 /* Merge the last into the first */
1800 c
->size
+= c
->next
->size
;
1802 /* Remove the last */
1803 struct working_area
*to_be_freed
= c
->next
;
1804 c
->next
= c
->next
->next
;
1805 if (to_be_freed
->backup
)
1806 free(to_be_freed
->backup
);
1809 /* If backup memory was allocated to the remaining area, it's has
1810 * the wrong size now */
1821 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1823 /* Reevaluate working area address based on MMU state*/
1824 if (target
->working_areas
== NULL
) {
1828 retval
= target
->type
->mmu(target
, &enabled
);
1829 if (retval
!= ERROR_OK
)
1833 if (target
->working_area_phys_spec
) {
1834 LOG_DEBUG("MMU disabled, using physical "
1835 "address for working memory " TARGET_ADDR_FMT
,
1836 target
->working_area_phys
);
1837 target
->working_area
= target
->working_area_phys
;
1839 LOG_ERROR("No working memory available. "
1840 "Specify -work-area-phys to target.");
1841 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1844 if (target
->working_area_virt_spec
) {
1845 LOG_DEBUG("MMU enabled, using virtual "
1846 "address for working memory " TARGET_ADDR_FMT
,
1847 target
->working_area_virt
);
1848 target
->working_area
= target
->working_area_virt
;
1850 LOG_ERROR("No working memory available. "
1851 "Specify -work-area-virt to target.");
1852 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1856 /* Set up initial working area on first call */
1857 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1859 new_wa
->next
= NULL
;
1860 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1861 new_wa
->address
= target
->working_area
;
1862 new_wa
->backup
= NULL
;
1863 new_wa
->user
= NULL
;
1864 new_wa
->free
= true;
1867 target
->working_areas
= new_wa
;
1870 /* only allocate multiples of 4 byte */
1872 size
= (size
+ 3) & (~3UL);
1874 struct working_area
*c
= target
->working_areas
;
1876 /* Find the first large enough working area */
1878 if (c
->free
&& c
->size
>= size
)
1884 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1886 /* Split the working area into the requested size */
1887 target_split_working_area(c
, size
);
1889 LOG_DEBUG("allocated new working area of %" PRIu32
" bytes at address " TARGET_ADDR_FMT
,
1892 if (target
->backup_working_area
) {
1893 if (c
->backup
== NULL
) {
1894 c
->backup
= malloc(c
->size
);
1895 if (c
->backup
== NULL
)
1899 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1900 if (retval
!= ERROR_OK
)
1904 /* mark as used, and return the new (reused) area */
1911 print_wa_layout(target
);
1916 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1920 retval
= target_alloc_working_area_try(target
, size
, area
);
1921 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1922 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1927 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1929 int retval
= ERROR_OK
;
1931 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1932 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1933 if (retval
!= ERROR_OK
)
1934 LOG_ERROR("failed to restore %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1935 area
->size
, area
->address
);
1941 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1942 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1944 int retval
= ERROR_OK
;
1950 retval
= target_restore_working_area(target
, area
);
1951 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1952 if (retval
!= ERROR_OK
)
1958 LOG_DEBUG("freed %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1959 area
->size
, area
->address
);
1961 /* mark user pointer invalid */
1962 /* TODO: Is this really safe? It points to some previous caller's memory.
1963 * How could we know that the area pointer is still in that place and not
1964 * some other vital data? What's the purpose of this, anyway? */
1968 target_merge_working_areas(target
);
1970 print_wa_layout(target
);
1975 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1977 return target_free_working_area_restore(target
, area
, 1);
1980 /* free resources and restore memory, if restoring memory fails,
1981 * free up resources anyway
1983 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1985 struct working_area
*c
= target
->working_areas
;
1987 LOG_DEBUG("freeing all working areas");
1989 /* Loop through all areas, restoring the allocated ones and marking them as free */
1993 target_restore_working_area(target
, c
);
1995 *c
->user
= NULL
; /* Same as above */
2001 /* Run a merge pass to combine all areas into one */
2002 target_merge_working_areas(target
);
2004 print_wa_layout(target
);
2007 void target_free_all_working_areas(struct target
*target
)
2009 target_free_all_working_areas_restore(target
, 1);
2011 /* Now we have none or only one working area marked as free */
2012 if (target
->working_areas
) {
2013 /* Free the last one to allow on-the-fly moving and resizing */
2014 free(target
->working_areas
->backup
);
2015 free(target
->working_areas
);
2016 target
->working_areas
= NULL
;
2020 /* Find the largest number of bytes that can be allocated */
2021 uint32_t target_get_working_area_avail(struct target
*target
)
2023 struct working_area
*c
= target
->working_areas
;
2024 uint32_t max_size
= 0;
2027 return target
->working_area_size
;
2030 if (c
->free
&& max_size
< c
->size
)
2039 static void target_destroy(struct target
*target
)
2041 if (target
->type
->deinit_target
)
2042 target
->type
->deinit_target(target
);
2044 if (target
->semihosting
)
2045 free(target
->semihosting
);
2047 jtag_unregister_event_callback(jtag_enable_callback
, target
);
2049 struct target_event_action
*teap
= target
->event_action
;
2051 struct target_event_action
*next
= teap
->next
;
2052 Jim_DecrRefCount(teap
->interp
, teap
->body
);
2057 target_free_all_working_areas(target
);
2059 /* release the targets SMP list */
2061 struct target_list
*head
= target
->head
;
2062 while (head
!= NULL
) {
2063 struct target_list
*pos
= head
->next
;
2064 head
->target
->smp
= 0;
2071 rtos_destroy(target
);
2073 free(target
->gdb_port_override
);
2075 free(target
->trace_info
);
2076 free(target
->fileio_info
);
2077 free(target
->cmd_name
);
2081 void target_quit(void)
2083 struct target_event_callback
*pe
= target_event_callbacks
;
2085 struct target_event_callback
*t
= pe
->next
;
2089 target_event_callbacks
= NULL
;
2091 struct target_timer_callback
*pt
= target_timer_callbacks
;
2093 struct target_timer_callback
*t
= pt
->next
;
2097 target_timer_callbacks
= NULL
;
2099 for (struct target
*target
= all_targets
; target
;) {
2103 target_destroy(target
);
2110 int target_arch_state(struct target
*target
)
2113 if (target
== NULL
) {
2114 LOG_WARNING("No target has been configured");
2118 if (target
->state
!= TARGET_HALTED
)
2121 retval
= target
->type
->arch_state(target
);
2125 static int target_get_gdb_fileio_info_default(struct target
*target
,
2126 struct gdb_fileio_info
*fileio_info
)
2128 /* If target does not support semi-hosting function, target
2129 has no need to provide .get_gdb_fileio_info callback.
2130 It just return ERROR_FAIL and gdb_server will return "Txx"
2131 as target halted every time. */
2135 static int target_gdb_fileio_end_default(struct target
*target
,
2136 int retcode
, int fileio_errno
, bool ctrl_c
)
2141 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
2142 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
2144 struct timeval timeout
, now
;
2146 gettimeofday(&timeout
, NULL
);
2147 timeval_add_time(&timeout
, seconds
, 0);
2149 LOG_INFO("Starting profiling. Halting and resuming the"
2150 " target as often as we can...");
2152 uint32_t sample_count
= 0;
2153 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2154 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2156 int retval
= ERROR_OK
;
2158 target_poll(target
);
2159 if (target
->state
== TARGET_HALTED
) {
2160 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
2161 samples
[sample_count
++] = t
;
2162 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2163 retval
= target_resume(target
, 1, 0, 0, 0);
2164 target_poll(target
);
2165 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2166 } else if (target
->state
== TARGET_RUNNING
) {
2167 /* We want to quickly sample the PC. */
2168 retval
= target_halt(target
);
2170 LOG_INFO("Target not halted or running");
2175 if (retval
!= ERROR_OK
)
2178 gettimeofday(&now
, NULL
);
2179 if ((sample_count
>= max_num_samples
) || timeval_compare(&now
, &timeout
) >= 0) {
2180 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2185 *num_samples
= sample_count
;
2189 /* Single aligned words are guaranteed to use 16 or 32 bit access
2190 * mode respectively, otherwise data is handled as quickly as
2193 int target_write_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, const uint8_t *buffer
)
2195 LOG_DEBUG("writing buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2198 if (!target_was_examined(target
)) {
2199 LOG_ERROR("Target not examined yet");
2206 if ((address
+ size
- 1) < address
) {
2207 /* GDB can request this when e.g. PC is 0xfffffffc */
2208 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2214 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2217 static int target_write_buffer_default(struct target
*target
,
2218 target_addr_t address
, uint32_t count
, const uint8_t *buffer
)
2222 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2223 * will have something to do with the size we leave to it. */
2224 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2225 if (address
& size
) {
2226 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2227 if (retval
!= ERROR_OK
)
2235 /* Write the data with as large access size as possible. */
2236 for (; size
> 0; size
/= 2) {
2237 uint32_t aligned
= count
- count
% size
;
2239 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2240 if (retval
!= ERROR_OK
)
2251 /* Single aligned words are guaranteed to use 16 or 32 bit access
2252 * mode respectively, otherwise data is handled as quickly as
2255 int target_read_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
2257 LOG_DEBUG("reading buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2260 if (!target_was_examined(target
)) {
2261 LOG_ERROR("Target not examined yet");
2268 if ((address
+ size
- 1) < address
) {
2269 /* GDB can request this when e.g. PC is 0xfffffffc */
2270 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2276 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2279 static int target_read_buffer_default(struct target
*target
, target_addr_t address
, uint32_t count
, uint8_t *buffer
)
2283 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2284 * will have something to do with the size we leave to it. */
2285 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2286 if (address
& size
) {
2287 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2288 if (retval
!= ERROR_OK
)
2296 /* Read the data with as large access size as possible. */
2297 for (; size
> 0; size
/= 2) {
2298 uint32_t aligned
= count
- count
% size
;
2300 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2301 if (retval
!= ERROR_OK
)
2312 int target_checksum_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t *crc
)
2317 uint32_t checksum
= 0;
2318 if (!target_was_examined(target
)) {
2319 LOG_ERROR("Target not examined yet");
2323 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2324 if (retval
!= ERROR_OK
) {
2325 buffer
= malloc(size
);
2326 if (buffer
== NULL
) {
2327 LOG_ERROR("error allocating buffer for section (%" PRId32
" bytes)", size
);
2328 return ERROR_COMMAND_SYNTAX_ERROR
;
2330 retval
= target_read_buffer(target
, address
, size
, buffer
);
2331 if (retval
!= ERROR_OK
) {
2336 /* convert to target endianness */
2337 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2338 uint32_t target_data
;
2339 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2340 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2343 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2352 int target_blank_check_memory(struct target
*target
,
2353 struct target_memory_check_block
*blocks
, int num_blocks
,
2354 uint8_t erased_value
)
2356 if (!target_was_examined(target
)) {
2357 LOG_ERROR("Target not examined yet");
2361 if (target
->type
->blank_check_memory
== NULL
)
2362 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2364 return target
->type
->blank_check_memory(target
, blocks
, num_blocks
, erased_value
);
2367 int target_read_u64(struct target
*target
, target_addr_t address
, uint64_t *value
)
2369 uint8_t value_buf
[8];
2370 if (!target_was_examined(target
)) {
2371 LOG_ERROR("Target not examined yet");
2375 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2377 if (retval
== ERROR_OK
) {
2378 *value
= target_buffer_get_u64(target
, value_buf
);
2379 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2384 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2391 int target_read_u32(struct target
*target
, target_addr_t address
, uint32_t *value
)
2393 uint8_t value_buf
[4];
2394 if (!target_was_examined(target
)) {
2395 LOG_ERROR("Target not examined yet");
2399 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2401 if (retval
== ERROR_OK
) {
2402 *value
= target_buffer_get_u32(target
, value_buf
);
2403 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2408 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2415 int target_read_u16(struct target
*target
, target_addr_t address
, uint16_t *value
)
2417 uint8_t value_buf
[2];
2418 if (!target_was_examined(target
)) {
2419 LOG_ERROR("Target not examined yet");
2423 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2425 if (retval
== ERROR_OK
) {
2426 *value
= target_buffer_get_u16(target
, value_buf
);
2427 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%4.4" PRIx16
,
2432 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2439 int target_read_u8(struct target
*target
, target_addr_t address
, uint8_t *value
)
2441 if (!target_was_examined(target
)) {
2442 LOG_ERROR("Target not examined yet");
2446 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2448 if (retval
== ERROR_OK
) {
2449 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2454 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2461 int target_write_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2464 uint8_t value_buf
[8];
2465 if (!target_was_examined(target
)) {
2466 LOG_ERROR("Target not examined yet");
2470 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2474 target_buffer_set_u64(target
, value_buf
, value
);
2475 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2476 if (retval
!= ERROR_OK
)
2477 LOG_DEBUG("failed: %i", retval
);
2482 int target_write_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2485 uint8_t value_buf
[4];
2486 if (!target_was_examined(target
)) {
2487 LOG_ERROR("Target not examined yet");
2491 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2495 target_buffer_set_u32(target
, value_buf
, value
);
2496 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2497 if (retval
!= ERROR_OK
)
2498 LOG_DEBUG("failed: %i", retval
);
2503 int target_write_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2506 uint8_t value_buf
[2];
2507 if (!target_was_examined(target
)) {
2508 LOG_ERROR("Target not examined yet");
2512 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2516 target_buffer_set_u16(target
, value_buf
, value
);
2517 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2518 if (retval
!= ERROR_OK
)
2519 LOG_DEBUG("failed: %i", retval
);
2524 int target_write_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2527 if (!target_was_examined(target
)) {
2528 LOG_ERROR("Target not examined yet");
2532 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2535 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2536 if (retval
!= ERROR_OK
)
2537 LOG_DEBUG("failed: %i", retval
);
2542 int target_write_phys_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2545 uint8_t value_buf
[8];
2546 if (!target_was_examined(target
)) {
2547 LOG_ERROR("Target not examined yet");
2551 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2555 target_buffer_set_u64(target
, value_buf
, value
);
2556 retval
= target_write_phys_memory(target
, address
, 8, 1, value_buf
);
2557 if (retval
!= ERROR_OK
)
2558 LOG_DEBUG("failed: %i", retval
);
2563 int target_write_phys_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2566 uint8_t value_buf
[4];
2567 if (!target_was_examined(target
)) {
2568 LOG_ERROR("Target not examined yet");
2572 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2576 target_buffer_set_u32(target
, value_buf
, value
);
2577 retval
= target_write_phys_memory(target
, address
, 4, 1, value_buf
);
2578 if (retval
!= ERROR_OK
)
2579 LOG_DEBUG("failed: %i", retval
);
2584 int target_write_phys_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2587 uint8_t value_buf
[2];
2588 if (!target_was_examined(target
)) {
2589 LOG_ERROR("Target not examined yet");
2593 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2597 target_buffer_set_u16(target
, value_buf
, value
);
2598 retval
= target_write_phys_memory(target
, address
, 2, 1, value_buf
);
2599 if (retval
!= ERROR_OK
)
2600 LOG_DEBUG("failed: %i", retval
);
2605 int target_write_phys_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2608 if (!target_was_examined(target
)) {
2609 LOG_ERROR("Target not examined yet");
2613 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2616 retval
= target_write_phys_memory(target
, address
, 1, 1, &value
);
2617 if (retval
!= ERROR_OK
)
2618 LOG_DEBUG("failed: %i", retval
);
2623 static int find_target(struct command_invocation
*cmd
, const char *name
)
2625 struct target
*target
= get_target(name
);
2626 if (target
== NULL
) {
2627 command_print(cmd
, "Target: %s is unknown, try one of:\n", name
);
2630 if (!target
->tap
->enabled
) {
2631 command_print(cmd
, "Target: TAP %s is disabled, "
2632 "can't be the current target\n",
2633 target
->tap
->dotted_name
);
2637 cmd
->ctx
->current_target
= target
;
2638 if (cmd
->ctx
->current_target_override
)
2639 cmd
->ctx
->current_target_override
= target
;
2645 COMMAND_HANDLER(handle_targets_command
)
2647 int retval
= ERROR_OK
;
2648 if (CMD_ARGC
== 1) {
2649 retval
= find_target(CMD
, CMD_ARGV
[0]);
2650 if (retval
== ERROR_OK
) {
2656 struct target
*target
= all_targets
;
2657 command_print(CMD
, " TargetName Type Endian TapName State ");
2658 command_print(CMD
, "-- ------------------ ---------- ------ ------------------ ------------");
2663 if (target
->tap
->enabled
)
2664 state
= target_state_name(target
);
2666 state
= "tap-disabled";
2668 if (CMD_CTX
->current_target
== target
)
2671 /* keep columns lined up to match the headers above */
2673 "%2d%c %-18s %-10s %-6s %-18s %s",
2674 target
->target_number
,
2676 target_name(target
),
2677 target_type_name(target
),
2678 Jim_Nvp_value2name_simple(nvp_target_endian
,
2679 target
->endianness
)->name
,
2680 target
->tap
->dotted_name
,
2682 target
= target
->next
;
2688 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2690 static int powerDropout
;
2691 static int srstAsserted
;
2693 static int runPowerRestore
;
2694 static int runPowerDropout
;
2695 static int runSrstAsserted
;
2696 static int runSrstDeasserted
;
2698 static int sense_handler(void)
2700 static int prevSrstAsserted
;
2701 static int prevPowerdropout
;
2703 int retval
= jtag_power_dropout(&powerDropout
);
2704 if (retval
!= ERROR_OK
)
2708 powerRestored
= prevPowerdropout
&& !powerDropout
;
2710 runPowerRestore
= 1;
2712 int64_t current
= timeval_ms();
2713 static int64_t lastPower
;
2714 bool waitMore
= lastPower
+ 2000 > current
;
2715 if (powerDropout
&& !waitMore
) {
2716 runPowerDropout
= 1;
2717 lastPower
= current
;
2720 retval
= jtag_srst_asserted(&srstAsserted
);
2721 if (retval
!= ERROR_OK
)
2725 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2727 static int64_t lastSrst
;
2728 waitMore
= lastSrst
+ 2000 > current
;
2729 if (srstDeasserted
&& !waitMore
) {
2730 runSrstDeasserted
= 1;
2734 if (!prevSrstAsserted
&& srstAsserted
)
2735 runSrstAsserted
= 1;
2737 prevSrstAsserted
= srstAsserted
;
2738 prevPowerdropout
= powerDropout
;
2740 if (srstDeasserted
|| powerRestored
) {
2741 /* Other than logging the event we can't do anything here.
2742 * Issuing a reset is a particularly bad idea as we might
2743 * be inside a reset already.
2750 /* process target state changes */
2751 static int handle_target(void *priv
)
2753 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2754 int retval
= ERROR_OK
;
2756 if (!is_jtag_poll_safe()) {
2757 /* polling is disabled currently */
2761 /* we do not want to recurse here... */
2762 static int recursive
;
2766 /* danger! running these procedures can trigger srst assertions and power dropouts.
2767 * We need to avoid an infinite loop/recursion here and we do that by
2768 * clearing the flags after running these events.
2770 int did_something
= 0;
2771 if (runSrstAsserted
) {
2772 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2773 Jim_Eval(interp
, "srst_asserted");
2776 if (runSrstDeasserted
) {
2777 Jim_Eval(interp
, "srst_deasserted");
2780 if (runPowerDropout
) {
2781 LOG_INFO("Power dropout detected, running power_dropout proc.");
2782 Jim_Eval(interp
, "power_dropout");
2785 if (runPowerRestore
) {
2786 Jim_Eval(interp
, "power_restore");
2790 if (did_something
) {
2791 /* clear detect flags */
2795 /* clear action flags */
2797 runSrstAsserted
= 0;
2798 runSrstDeasserted
= 0;
2799 runPowerRestore
= 0;
2800 runPowerDropout
= 0;
2805 /* Poll targets for state changes unless that's globally disabled.
2806 * Skip targets that are currently disabled.
2808 for (struct target
*target
= all_targets
;
2809 is_jtag_poll_safe() && target
;
2810 target
= target
->next
) {
2812 if (!target_was_examined(target
))
2815 if (!target
->tap
->enabled
)
2818 if (target
->backoff
.times
> target
->backoff
.count
) {
2819 /* do not poll this time as we failed previously */
2820 target
->backoff
.count
++;
2823 target
->backoff
.count
= 0;
2825 /* only poll target if we've got power and srst isn't asserted */
2826 if (!powerDropout
&& !srstAsserted
) {
2827 /* polling may fail silently until the target has been examined */
2828 retval
= target_poll(target
);
2829 if (retval
!= ERROR_OK
) {
2830 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2831 if (target
->backoff
.times
* polling_interval
< 5000) {
2832 target
->backoff
.times
*= 2;
2833 target
->backoff
.times
++;
2836 /* Tell GDB to halt the debugger. This allows the user to
2837 * run monitor commands to handle the situation.
2839 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2841 if (target
->backoff
.times
> 0) {
2842 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
2843 target_reset_examined(target
);
2844 retval
= target_examine_one(target
);
2845 /* Target examination could have failed due to unstable connection,
2846 * but we set the examined flag anyway to repoll it later */
2847 if (retval
!= ERROR_OK
) {
2848 target
->examined
= true;
2849 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2850 target
->backoff
.times
* polling_interval
);
2855 /* Since we succeeded, we reset backoff count */
2856 target
->backoff
.times
= 0;
2863 COMMAND_HANDLER(handle_reg_command
)
2865 struct target
*target
;
2866 struct reg
*reg
= NULL
;
2872 target
= get_current_target(CMD_CTX
);
2874 /* list all available registers for the current target */
2875 if (CMD_ARGC
== 0) {
2876 struct reg_cache
*cache
= target
->reg_cache
;
2882 command_print(CMD
, "===== %s", cache
->name
);
2884 for (i
= 0, reg
= cache
->reg_list
;
2885 i
< cache
->num_regs
;
2886 i
++, reg
++, count
++) {
2887 if (reg
->exist
== false)
2889 /* only print cached values if they are valid */
2891 value
= buf_to_str(reg
->value
,
2894 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2902 command_print(CMD
, "(%i) %s (/%" PRIu32
")",
2907 cache
= cache
->next
;
2913 /* access a single register by its ordinal number */
2914 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2916 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2918 struct reg_cache
*cache
= target
->reg_cache
;
2922 for (i
= 0; i
< cache
->num_regs
; i
++) {
2923 if (count
++ == num
) {
2924 reg
= &cache
->reg_list
[i
];
2930 cache
= cache
->next
;
2934 command_print(CMD
, "%i is out of bounds, the current target "
2935 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2939 /* access a single register by its name */
2940 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2946 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2951 /* display a register */
2952 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2953 && (CMD_ARGV
[1][0] <= '9')))) {
2954 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2957 if (reg
->valid
== 0)
2958 reg
->type
->get(reg
);
2959 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2960 command_print(CMD
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2965 /* set register value */
2966 if (CMD_ARGC
== 2) {
2967 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2970 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2972 reg
->type
->set(reg
, buf
);
2974 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2975 command_print(CMD
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2983 return ERROR_COMMAND_SYNTAX_ERROR
;
2986 command_print(CMD
, "register %s not found in current target", CMD_ARGV
[0]);
2990 COMMAND_HANDLER(handle_poll_command
)
2992 int retval
= ERROR_OK
;
2993 struct target
*target
= get_current_target(CMD_CTX
);
2995 if (CMD_ARGC
== 0) {
2996 command_print(CMD
, "background polling: %s",
2997 jtag_poll_get_enabled() ? "on" : "off");
2998 command_print(CMD
, "TAP: %s (%s)",
2999 target
->tap
->dotted_name
,
3000 target
->tap
->enabled
? "enabled" : "disabled");
3001 if (!target
->tap
->enabled
)
3003 retval
= target_poll(target
);
3004 if (retval
!= ERROR_OK
)
3006 retval
= target_arch_state(target
);
3007 if (retval
!= ERROR_OK
)
3009 } else if (CMD_ARGC
== 1) {
3011 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
3012 jtag_poll_set_enabled(enable
);
3014 return ERROR_COMMAND_SYNTAX_ERROR
;
3019 COMMAND_HANDLER(handle_wait_halt_command
)
3022 return ERROR_COMMAND_SYNTAX_ERROR
;
3024 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
3025 if (1 == CMD_ARGC
) {
3026 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
3027 if (ERROR_OK
!= retval
)
3028 return ERROR_COMMAND_SYNTAX_ERROR
;
3031 struct target
*target
= get_current_target(CMD_CTX
);
3032 return target_wait_state(target
, TARGET_HALTED
, ms
);
3035 /* wait for target state to change. The trick here is to have a low
3036 * latency for short waits and not to suck up all the CPU time
3039 * After 500ms, keep_alive() is invoked
3041 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
3044 int64_t then
= 0, cur
;
3048 retval
= target_poll(target
);
3049 if (retval
!= ERROR_OK
)
3051 if (target
->state
== state
)
3056 then
= timeval_ms();
3057 LOG_DEBUG("waiting for target %s...",
3058 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
3064 if ((cur
-then
) > ms
) {
3065 LOG_ERROR("timed out while waiting for target %s",
3066 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
3074 COMMAND_HANDLER(handle_halt_command
)
3078 struct target
*target
= get_current_target(CMD_CTX
);
3080 target
->verbose_halt_msg
= true;
3082 int retval
= target_halt(target
);
3083 if (ERROR_OK
!= retval
)
3086 if (CMD_ARGC
== 1) {
3087 unsigned wait_local
;
3088 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
3089 if (ERROR_OK
!= retval
)
3090 return ERROR_COMMAND_SYNTAX_ERROR
;
3095 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
3098 COMMAND_HANDLER(handle_soft_reset_halt_command
)
3100 struct target
*target
= get_current_target(CMD_CTX
);
3102 LOG_USER("requesting target halt and executing a soft reset");
3104 target_soft_reset_halt(target
);
3109 COMMAND_HANDLER(handle_reset_command
)
3112 return ERROR_COMMAND_SYNTAX_ERROR
;
3114 enum target_reset_mode reset_mode
= RESET_RUN
;
3115 if (CMD_ARGC
== 1) {
3117 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
3118 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
3119 return ERROR_COMMAND_SYNTAX_ERROR
;
3120 reset_mode
= n
->value
;
3123 /* reset *all* targets */
3124 return target_process_reset(CMD
, reset_mode
);
3128 COMMAND_HANDLER(handle_resume_command
)
3132 return ERROR_COMMAND_SYNTAX_ERROR
;
3134 struct target
*target
= get_current_target(CMD_CTX
);
3136 /* with no CMD_ARGV, resume from current pc, addr = 0,
3137 * with one arguments, addr = CMD_ARGV[0],
3138 * handle breakpoints, not debugging */
3139 target_addr_t addr
= 0;
3140 if (CMD_ARGC
== 1) {
3141 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3145 return target_resume(target
, current
, addr
, 1, 0);
3148 COMMAND_HANDLER(handle_step_command
)
3151 return ERROR_COMMAND_SYNTAX_ERROR
;
3155 /* with no CMD_ARGV, step from current pc, addr = 0,
3156 * with one argument addr = CMD_ARGV[0],
3157 * handle breakpoints, debugging */
3158 target_addr_t addr
= 0;
3160 if (CMD_ARGC
== 1) {
3161 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3165 struct target
*target
= get_current_target(CMD_CTX
);
3167 return target_step(target
, current_pc
, addr
, 1);
3170 void target_handle_md_output(struct command_invocation
*cmd
,
3171 struct target
*target
, target_addr_t address
, unsigned size
,
3172 unsigned count
, const uint8_t *buffer
)
3174 const unsigned line_bytecnt
= 32;
3175 unsigned line_modulo
= line_bytecnt
/ size
;
3177 char output
[line_bytecnt
* 4 + 1];
3178 unsigned output_len
= 0;
3180 const char *value_fmt
;
3183 value_fmt
= "%16.16"PRIx64
" ";
3186 value_fmt
= "%8.8"PRIx64
" ";
3189 value_fmt
= "%4.4"PRIx64
" ";
3192 value_fmt
= "%2.2"PRIx64
" ";
3195 /* "can't happen", caller checked */
3196 LOG_ERROR("invalid memory read size: %u", size
);
3200 for (unsigned i
= 0; i
< count
; i
++) {
3201 if (i
% line_modulo
== 0) {
3202 output_len
+= snprintf(output
+ output_len
,
3203 sizeof(output
) - output_len
,
3204 TARGET_ADDR_FMT
": ",
3205 (address
+ (i
* size
)));
3209 const uint8_t *value_ptr
= buffer
+ i
* size
;
3212 value
= target_buffer_get_u64(target
, value_ptr
);
3215 value
= target_buffer_get_u32(target
, value_ptr
);
3218 value
= target_buffer_get_u16(target
, value_ptr
);
3223 output_len
+= snprintf(output
+ output_len
,
3224 sizeof(output
) - output_len
,
3227 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
3228 command_print(cmd
, "%s", output
);
3234 COMMAND_HANDLER(handle_md_command
)
3237 return ERROR_COMMAND_SYNTAX_ERROR
;
3240 switch (CMD_NAME
[2]) {
3254 return ERROR_COMMAND_SYNTAX_ERROR
;
3257 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3258 int (*fn
)(struct target
*target
,
3259 target_addr_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
3263 fn
= target_read_phys_memory
;
3265 fn
= target_read_memory
;
3266 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
3267 return ERROR_COMMAND_SYNTAX_ERROR
;
3269 target_addr_t address
;
3270 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3274 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
3276 uint8_t *buffer
= calloc(count
, size
);
3277 if (buffer
== NULL
) {
3278 LOG_ERROR("Failed to allocate md read buffer");
3282 struct target
*target
= get_current_target(CMD_CTX
);
3283 int retval
= fn(target
, address
, size
, count
, buffer
);
3284 if (ERROR_OK
== retval
)
3285 target_handle_md_output(CMD
, target
, address
, size
, count
, buffer
);
3292 typedef int (*target_write_fn
)(struct target
*target
,
3293 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
3295 static int target_fill_mem(struct target
*target
,
3296 target_addr_t address
,
3304 /* We have to write in reasonably large chunks to be able
3305 * to fill large memory areas with any sane speed */
3306 const unsigned chunk_size
= 16384;
3307 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
3308 if (target_buf
== NULL
) {
3309 LOG_ERROR("Out of memory");
3313 for (unsigned i
= 0; i
< chunk_size
; i
++) {
3314 switch (data_size
) {
3316 target_buffer_set_u64(target
, target_buf
+ i
* data_size
, b
);
3319 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
3322 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
3325 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
3332 int retval
= ERROR_OK
;
3334 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
3337 if (current
> chunk_size
)
3338 current
= chunk_size
;
3339 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
3340 if (retval
!= ERROR_OK
)
3342 /* avoid GDB timeouts */
3351 COMMAND_HANDLER(handle_mw_command
)
3354 return ERROR_COMMAND_SYNTAX_ERROR
;
3355 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3360 fn
= target_write_phys_memory
;
3362 fn
= target_write_memory
;
3363 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
3364 return ERROR_COMMAND_SYNTAX_ERROR
;
3366 target_addr_t address
;
3367 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3370 COMMAND_PARSE_NUMBER(u64
, CMD_ARGV
[1], value
);
3374 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
3376 struct target
*target
= get_current_target(CMD_CTX
);
3378 switch (CMD_NAME
[2]) {
3392 return ERROR_COMMAND_SYNTAX_ERROR
;
3395 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
3398 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
3399 target_addr_t
*min_address
, target_addr_t
*max_address
)
3401 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
3402 return ERROR_COMMAND_SYNTAX_ERROR
;
3404 /* a base address isn't always necessary,
3405 * default to 0x0 (i.e. don't relocate) */
3406 if (CMD_ARGC
>= 2) {
3408 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3409 image
->base_address
= addr
;
3410 image
->base_address_set
= 1;
3412 image
->base_address_set
= 0;
3414 image
->start_address_set
= 0;
3417 COMMAND_PARSE_ADDRESS(CMD_ARGV
[3], *min_address
);
3418 if (CMD_ARGC
== 5) {
3419 COMMAND_PARSE_ADDRESS(CMD_ARGV
[4], *max_address
);
3420 /* use size (given) to find max (required) */
3421 *max_address
+= *min_address
;
3424 if (*min_address
> *max_address
)
3425 return ERROR_COMMAND_SYNTAX_ERROR
;
3430 COMMAND_HANDLER(handle_load_image_command
)
3434 uint32_t image_size
;
3435 target_addr_t min_address
= 0;
3436 target_addr_t max_address
= -1;
3440 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
3441 &image
, &min_address
, &max_address
);
3442 if (ERROR_OK
!= retval
)
3445 struct target
*target
= get_current_target(CMD_CTX
);
3447 struct duration bench
;
3448 duration_start(&bench
);
3450 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
3455 for (i
= 0; i
< image
.num_sections
; i
++) {
3456 buffer
= malloc(image
.sections
[i
].size
);
3457 if (buffer
== NULL
) {
3459 "error allocating buffer for section (%d bytes)",
3460 (int)(image
.sections
[i
].size
));
3461 retval
= ERROR_FAIL
;
3465 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3466 if (retval
!= ERROR_OK
) {
3471 uint32_t offset
= 0;
3472 uint32_t length
= buf_cnt
;
3474 /* DANGER!!! beware of unsigned comparison here!!! */
3476 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3477 (image
.sections
[i
].base_address
< max_address
)) {
3479 if (image
.sections
[i
].base_address
< min_address
) {
3480 /* clip addresses below */
3481 offset
+= min_address
-image
.sections
[i
].base_address
;
3485 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3486 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3488 retval
= target_write_buffer(target
,
3489 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3490 if (retval
!= ERROR_OK
) {
3494 image_size
+= length
;
3495 command_print(CMD
, "%u bytes written at address " TARGET_ADDR_FMT
"",
3496 (unsigned int)length
,
3497 image
.sections
[i
].base_address
+ offset
);
3503 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3504 command_print(CMD
, "downloaded %" PRIu32
" bytes "
3505 "in %fs (%0.3f KiB/s)", image_size
,
3506 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3509 image_close(&image
);
3515 COMMAND_HANDLER(handle_dump_image_command
)
3517 struct fileio
*fileio
;
3519 int retval
, retvaltemp
;
3520 target_addr_t address
, size
;
3521 struct duration bench
;
3522 struct target
*target
= get_current_target(CMD_CTX
);
3525 return ERROR_COMMAND_SYNTAX_ERROR
;
3527 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], address
);
3528 COMMAND_PARSE_ADDRESS(CMD_ARGV
[2], size
);
3530 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
3531 buffer
= malloc(buf_size
);
3535 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3536 if (retval
!= ERROR_OK
) {
3541 duration_start(&bench
);
3544 size_t size_written
;
3545 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
3546 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
3547 if (retval
!= ERROR_OK
)
3550 retval
= fileio_write(fileio
, this_run_size
, buffer
, &size_written
);
3551 if (retval
!= ERROR_OK
)
3554 size
-= this_run_size
;
3555 address
+= this_run_size
;
3560 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3562 retval
= fileio_size(fileio
, &filesize
);
3563 if (retval
!= ERROR_OK
)
3566 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize
,
3567 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3570 retvaltemp
= fileio_close(fileio
);
3571 if (retvaltemp
!= ERROR_OK
)
3580 IMAGE_CHECKSUM_ONLY
= 2
3583 static COMMAND_HELPER(handle_verify_image_command_internal
, enum verify_mode verify
)
3587 uint32_t image_size
;
3590 uint32_t checksum
= 0;
3591 uint32_t mem_checksum
= 0;
3595 struct target
*target
= get_current_target(CMD_CTX
);
3598 return ERROR_COMMAND_SYNTAX_ERROR
;
3601 LOG_ERROR("no target selected");
3605 struct duration bench
;
3606 duration_start(&bench
);
3608 if (CMD_ARGC
>= 2) {
3610 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3611 image
.base_address
= addr
;
3612 image
.base_address_set
= 1;
3614 image
.base_address_set
= 0;
3615 image
.base_address
= 0x0;
3618 image
.start_address_set
= 0;
3620 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3621 if (retval
!= ERROR_OK
)
3627 for (i
= 0; i
< image
.num_sections
; i
++) {
3628 buffer
= malloc(image
.sections
[i
].size
);
3629 if (buffer
== NULL
) {
3631 "error allocating buffer for section (%d bytes)",
3632 (int)(image
.sections
[i
].size
));
3635 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3636 if (retval
!= ERROR_OK
) {
3641 if (verify
>= IMAGE_VERIFY
) {
3642 /* calculate checksum of image */
3643 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3644 if (retval
!= ERROR_OK
) {
3649 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3650 if (retval
!= ERROR_OK
) {
3654 if ((checksum
!= mem_checksum
) && (verify
== IMAGE_CHECKSUM_ONLY
)) {
3655 LOG_ERROR("checksum mismatch");
3657 retval
= ERROR_FAIL
;
3660 if (checksum
!= mem_checksum
) {
3661 /* failed crc checksum, fall back to a binary compare */
3665 LOG_ERROR("checksum mismatch - attempting binary compare");
3667 data
= malloc(buf_cnt
);
3669 retval
= target_read_buffer(target
, image
.sections
[i
].base_address
, buf_cnt
, data
);
3670 if (retval
== ERROR_OK
) {
3672 for (t
= 0; t
< buf_cnt
; t
++) {
3673 if (data
[t
] != buffer
[t
]) {
3675 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3677 (unsigned)(t
+ image
.sections
[i
].base_address
),
3680 if (diffs
++ >= 127) {
3681 command_print(CMD
, "More than 128 errors, the rest are not printed.");
3693 command_print(CMD
, "address " TARGET_ADDR_FMT
" length 0x%08zx",
3694 image
.sections
[i
].base_address
,
3699 image_size
+= buf_cnt
;
3702 command_print(CMD
, "No more differences found.");
3705 retval
= ERROR_FAIL
;
3706 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3707 command_print(CMD
, "verified %" PRIu32
" bytes "
3708 "in %fs (%0.3f KiB/s)", image_size
,
3709 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3712 image_close(&image
);
3717 COMMAND_HANDLER(handle_verify_image_checksum_command
)
3719 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_CHECKSUM_ONLY
);
3722 COMMAND_HANDLER(handle_verify_image_command
)
3724 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_VERIFY
);
3727 COMMAND_HANDLER(handle_test_image_command
)
3729 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_TEST
);
3732 static int handle_bp_command_list(struct command_invocation
*cmd
)
3734 struct target
*target
= get_current_target(cmd
->ctx
);
3735 struct breakpoint
*breakpoint
= target
->breakpoints
;
3736 while (breakpoint
) {
3737 if (breakpoint
->type
== BKPT_SOFT
) {
3738 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3739 breakpoint
->length
, 16);
3740 command_print(cmd
, "IVA breakpoint: " TARGET_ADDR_FMT
", 0x%x, %i, 0x%s",
3741 breakpoint
->address
,
3743 breakpoint
->set
, buf
);
3746 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3747 command_print(cmd
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3749 breakpoint
->length
, breakpoint
->set
);
3750 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3751 command_print(cmd
, "Hybrid breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3752 breakpoint
->address
,
3753 breakpoint
->length
, breakpoint
->set
);
3754 command_print(cmd
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3757 command_print(cmd
, "Breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3758 breakpoint
->address
,
3759 breakpoint
->length
, breakpoint
->set
);
3762 breakpoint
= breakpoint
->next
;
3767 static int handle_bp_command_set(struct command_invocation
*cmd
,
3768 target_addr_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3770 struct target
*target
= get_current_target(cmd
->ctx
);
3774 retval
= breakpoint_add(target
, addr
, length
, hw
);
3775 /* error is always logged in breakpoint_add(), do not print it again */
3776 if (ERROR_OK
== retval
)
3777 command_print(cmd
, "breakpoint set at " TARGET_ADDR_FMT
"", addr
);
3779 } else if (addr
== 0) {
3780 if (target
->type
->add_context_breakpoint
== NULL
) {
3781 LOG_ERROR("Context breakpoint not available");
3782 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
3784 retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3785 /* error is always logged in context_breakpoint_add(), do not print it again */
3786 if (ERROR_OK
== retval
)
3787 command_print(cmd
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3790 if (target
->type
->add_hybrid_breakpoint
== NULL
) {
3791 LOG_ERROR("Hybrid breakpoint not available");
3792 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
3794 retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3795 /* error is always logged in hybrid_breakpoint_add(), do not print it again */
3796 if (ERROR_OK
== retval
)
3797 command_print(cmd
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3802 COMMAND_HANDLER(handle_bp_command
)
3811 return handle_bp_command_list(CMD
);
3815 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3816 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3817 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
3820 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
3822 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3823 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3825 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
3826 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
3828 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
3829 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3831 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
3836 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3837 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
3838 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
3839 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
3842 return ERROR_COMMAND_SYNTAX_ERROR
;
3846 COMMAND_HANDLER(handle_rbp_command
)
3849 return ERROR_COMMAND_SYNTAX_ERROR
;
3851 struct target
*target
= get_current_target(CMD_CTX
);
3853 if (!strcmp(CMD_ARGV
[0], "all")) {
3854 breakpoint_remove_all(target
);
3857 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3859 breakpoint_remove(target
, addr
);
3865 COMMAND_HANDLER(handle_wp_command
)
3867 struct target
*target
= get_current_target(CMD_CTX
);
3869 if (CMD_ARGC
== 0) {
3870 struct watchpoint
*watchpoint
= target
->watchpoints
;
3872 while (watchpoint
) {
3873 command_print(CMD
, "address: " TARGET_ADDR_FMT
3874 ", len: 0x%8.8" PRIx32
3875 ", r/w/a: %i, value: 0x%8.8" PRIx32
3876 ", mask: 0x%8.8" PRIx32
,
3877 watchpoint
->address
,
3879 (int)watchpoint
->rw
,
3882 watchpoint
= watchpoint
->next
;
3887 enum watchpoint_rw type
= WPT_ACCESS
;
3889 uint32_t length
= 0;
3890 uint32_t data_value
= 0x0;
3891 uint32_t data_mask
= 0xffffffff;
3895 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
3898 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3901 switch (CMD_ARGV
[2][0]) {
3912 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3913 return ERROR_COMMAND_SYNTAX_ERROR
;
3917 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3918 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3922 return ERROR_COMMAND_SYNTAX_ERROR
;
3925 int retval
= watchpoint_add(target
, addr
, length
, type
,
3926 data_value
, data_mask
);
3927 if (ERROR_OK
!= retval
)
3928 LOG_ERROR("Failure setting watchpoints");
3933 COMMAND_HANDLER(handle_rwp_command
)
3936 return ERROR_COMMAND_SYNTAX_ERROR
;
3939 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3941 struct target
*target
= get_current_target(CMD_CTX
);
3942 watchpoint_remove(target
, addr
);
3948 * Translate a virtual address to a physical address.
3950 * The low-level target implementation must have logged a detailed error
3951 * which is forwarded to telnet/GDB session.
3953 COMMAND_HANDLER(handle_virt2phys_command
)
3956 return ERROR_COMMAND_SYNTAX_ERROR
;
3959 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], va
);
3962 struct target
*target
= get_current_target(CMD_CTX
);
3963 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3964 if (retval
== ERROR_OK
)
3965 command_print(CMD
, "Physical address " TARGET_ADDR_FMT
"", pa
);
3970 static void writeData(FILE *f
, const void *data
, size_t len
)
3972 size_t written
= fwrite(data
, 1, len
, f
);
3974 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3977 static void writeLong(FILE *f
, int l
, struct target
*target
)
3981 target_buffer_set_u32(target
, val
, l
);
3982 writeData(f
, val
, 4);
3985 static void writeString(FILE *f
, char *s
)
3987 writeData(f
, s
, strlen(s
));
3990 typedef unsigned char UNIT
[2]; /* unit of profiling */
3992 /* Dump a gmon.out histogram file. */
3993 static void write_gmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
, bool with_range
,
3994 uint32_t start_address
, uint32_t end_address
, struct target
*target
, uint32_t duration_ms
)
3997 FILE *f
= fopen(filename
, "w");
4000 writeString(f
, "gmon");
4001 writeLong(f
, 0x00000001, target
); /* Version */
4002 writeLong(f
, 0, target
); /* padding */
4003 writeLong(f
, 0, target
); /* padding */
4004 writeLong(f
, 0, target
); /* padding */
4006 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
4007 writeData(f
, &zero
, 1);
4009 /* figure out bucket size */
4013 min
= start_address
;
4018 for (i
= 0; i
< sampleNum
; i
++) {
4019 if (min
> samples
[i
])
4021 if (max
< samples
[i
])
4025 /* max should be (largest sample + 1)
4026 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
4030 int addressSpace
= max
- min
;
4031 assert(addressSpace
>= 2);
4033 /* FIXME: What is the reasonable number of buckets?
4034 * The profiling result will be more accurate if there are enough buckets. */
4035 static const uint32_t maxBuckets
= 128 * 1024; /* maximum buckets. */
4036 uint32_t numBuckets
= addressSpace
/ sizeof(UNIT
);
4037 if (numBuckets
> maxBuckets
)
4038 numBuckets
= maxBuckets
;
4039 int *buckets
= malloc(sizeof(int) * numBuckets
);
4040 if (buckets
== NULL
) {
4044 memset(buckets
, 0, sizeof(int) * numBuckets
);
4045 for (i
= 0; i
< sampleNum
; i
++) {
4046 uint32_t address
= samples
[i
];
4048 if ((address
< min
) || (max
<= address
))
4051 long long a
= address
- min
;
4052 long long b
= numBuckets
;
4053 long long c
= addressSpace
;
4054 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
4058 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
4059 writeLong(f
, min
, target
); /* low_pc */
4060 writeLong(f
, max
, target
); /* high_pc */
4061 writeLong(f
, numBuckets
, target
); /* # of buckets */
4062 float sample_rate
= sampleNum
/ (duration_ms
/ 1000.0);
4063 writeLong(f
, sample_rate
, target
);
4064 writeString(f
, "seconds");
4065 for (i
= 0; i
< (15-strlen("seconds")); i
++)
4066 writeData(f
, &zero
, 1);
4067 writeString(f
, "s");
4069 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
4071 char *data
= malloc(2 * numBuckets
);
4073 for (i
= 0; i
< numBuckets
; i
++) {
4078 data
[i
* 2] = val
&0xff;
4079 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
4082 writeData(f
, data
, numBuckets
* 2);
4090 /* profiling samples the CPU PC as quickly as OpenOCD is able,
4091 * which will be used as a random sampling of PC */
4092 COMMAND_HANDLER(handle_profile_command
)
4094 struct target
*target
= get_current_target(CMD_CTX
);
4096 if ((CMD_ARGC
!= 2) && (CMD_ARGC
!= 4))
4097 return ERROR_COMMAND_SYNTAX_ERROR
;
4099 const uint32_t MAX_PROFILE_SAMPLE_NUM
= 10000;
4101 uint32_t num_of_samples
;
4102 int retval
= ERROR_OK
;
4104 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], offset
);
4106 uint32_t *samples
= malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM
);
4107 if (samples
== NULL
) {
4108 LOG_ERROR("No memory to store samples.");
4112 uint64_t timestart_ms
= timeval_ms();
4114 * Some cores let us sample the PC without the
4115 * annoying halt/resume step; for example, ARMv7 PCSR.
4116 * Provide a way to use that more efficient mechanism.
4118 retval
= target_profiling(target
, samples
, MAX_PROFILE_SAMPLE_NUM
,
4119 &num_of_samples
, offset
);
4120 if (retval
!= ERROR_OK
) {
4124 uint32_t duration_ms
= timeval_ms() - timestart_ms
;
4126 assert(num_of_samples
<= MAX_PROFILE_SAMPLE_NUM
);
4128 retval
= target_poll(target
);
4129 if (retval
!= ERROR_OK
) {
4133 if (target
->state
== TARGET_RUNNING
) {
4134 retval
= target_halt(target
);
4135 if (retval
!= ERROR_OK
) {
4141 retval
= target_poll(target
);
4142 if (retval
!= ERROR_OK
) {
4147 uint32_t start_address
= 0;
4148 uint32_t end_address
= 0;
4149 bool with_range
= false;
4150 if (CMD_ARGC
== 4) {
4152 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], start_address
);
4153 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], end_address
);
4156 write_gmon(samples
, num_of_samples
, CMD_ARGV
[1],
4157 with_range
, start_address
, end_address
, target
, duration_ms
);
4158 command_print(CMD
, "Wrote %s", CMD_ARGV
[1]);
4164 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
4167 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4170 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4174 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4175 valObjPtr
= Jim_NewIntObj(interp
, val
);
4176 if (!nameObjPtr
|| !valObjPtr
) {
4181 Jim_IncrRefCount(nameObjPtr
);
4182 Jim_IncrRefCount(valObjPtr
);
4183 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
4184 Jim_DecrRefCount(interp
, nameObjPtr
);
4185 Jim_DecrRefCount(interp
, valObjPtr
);
4187 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
4191 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4193 struct command_context
*context
;
4194 struct target
*target
;
4196 context
= current_command_context(interp
);
4197 assert(context
!= NULL
);
4199 target
= get_current_target(context
);
4200 if (target
== NULL
) {
4201 LOG_ERROR("mem2array: no current target");
4205 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4208 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
4216 const char *varname
;
4222 /* argv[1] = name of array to receive the data
4223 * argv[2] = desired width
4224 * argv[3] = memory address
4225 * argv[4] = count of times to read
4228 if (argc
< 4 || argc
> 5) {
4229 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4232 varname
= Jim_GetString(argv
[0], &len
);
4233 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4235 e
= Jim_GetLong(interp
, argv
[1], &l
);
4240 e
= Jim_GetLong(interp
, argv
[2], &l
);
4244 e
= Jim_GetLong(interp
, argv
[3], &l
);
4250 phys
= Jim_GetString(argv
[4], &n
);
4251 if (!strncmp(phys
, "phys", n
))
4267 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4268 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
4272 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4273 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
4276 if ((addr
+ (len
* width
)) < addr
) {
4277 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4278 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
4281 /* absurd transfer size? */
4283 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4284 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
4289 ((width
== 2) && ((addr
& 1) == 0)) ||
4290 ((width
== 4) && ((addr
& 3) == 0))) {
4294 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4295 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
4298 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4307 size_t buffersize
= 4096;
4308 uint8_t *buffer
= malloc(buffersize
);
4315 /* Slurp... in buffer size chunks */
4317 count
= len
; /* in objects.. */
4318 if (count
> (buffersize
/ width
))
4319 count
= (buffersize
/ width
);
4322 retval
= target_read_phys_memory(target
, addr
, width
, count
, buffer
);
4324 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
4325 if (retval
!= ERROR_OK
) {
4327 LOG_ERROR("mem2array: Read @ 0x%08" PRIx32
", w=%" PRId32
", cnt=%" PRId32
", failed",
4331 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4332 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
4336 v
= 0; /* shut up gcc */
4337 for (i
= 0; i
< count
; i
++, n
++) {
4340 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
4343 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
4346 v
= buffer
[i
] & 0x0ff;
4349 new_int_array_element(interp
, varname
, n
, v
);
4352 addr
+= count
* width
;
4358 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4363 static int get_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t *val
)
4366 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4370 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4374 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4380 Jim_IncrRefCount(nameObjPtr
);
4381 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
4382 Jim_DecrRefCount(interp
, nameObjPtr
);
4384 if (valObjPtr
== NULL
)
4387 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
4388 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
4393 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4395 struct command_context
*context
;
4396 struct target
*target
;
4398 context
= current_command_context(interp
);
4399 assert(context
!= NULL
);
4401 target
= get_current_target(context
);
4402 if (target
== NULL
) {
4403 LOG_ERROR("array2mem: no current target");
4407 return target_array2mem(interp
, target
, argc
-1, argv
+ 1);
4410 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
4411 int argc
, Jim_Obj
*const *argv
)
4419 const char *varname
;
4425 /* argv[1] = name of array to get the data
4426 * argv[2] = desired width
4427 * argv[3] = memory address
4428 * argv[4] = count to write
4430 if (argc
< 4 || argc
> 5) {
4431 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4434 varname
= Jim_GetString(argv
[0], &len
);
4435 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4437 e
= Jim_GetLong(interp
, argv
[1], &l
);
4442 e
= Jim_GetLong(interp
, argv
[2], &l
);
4446 e
= Jim_GetLong(interp
, argv
[3], &l
);
4452 phys
= Jim_GetString(argv
[4], &n
);
4453 if (!strncmp(phys
, "phys", n
))
4469 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4470 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4471 "Invalid width param, must be 8/16/32", NULL
);
4475 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4476 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4477 "array2mem: zero width read?", NULL
);
4480 if ((addr
+ (len
* width
)) < addr
) {
4481 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4482 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4483 "array2mem: addr + len - wraps to zero?", NULL
);
4486 /* absurd transfer size? */
4488 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4489 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4490 "array2mem: absurd > 64K item request", NULL
);
4495 ((width
== 2) && ((addr
& 1) == 0)) ||
4496 ((width
== 4) && ((addr
& 3) == 0))) {
4500 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4501 sprintf(buf
, "array2mem address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
4504 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4515 size_t buffersize
= 4096;
4516 uint8_t *buffer
= malloc(buffersize
);
4521 /* Slurp... in buffer size chunks */
4523 count
= len
; /* in objects.. */
4524 if (count
> (buffersize
/ width
))
4525 count
= (buffersize
/ width
);
4527 v
= 0; /* shut up gcc */
4528 for (i
= 0; i
< count
; i
++, n
++) {
4529 get_int_array_element(interp
, varname
, n
, &v
);
4532 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
4535 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
4538 buffer
[i
] = v
& 0x0ff;
4545 retval
= target_write_phys_memory(target
, addr
, width
, count
, buffer
);
4547 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
4548 if (retval
!= ERROR_OK
) {
4550 LOG_ERROR("array2mem: Write @ 0x%08" PRIx32
", w=%" PRId32
", cnt=%" PRId32
", failed",
4554 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4555 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
4559 addr
+= count
* width
;
4564 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4569 /* FIX? should we propagate errors here rather than printing them
4572 void target_handle_event(struct target
*target
, enum target_event e
)
4574 struct target_event_action
*teap
;
4577 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4578 if (teap
->event
== e
) {
4579 LOG_DEBUG("target(%d): %s (%s) event: %d (%s) action: %s",
4580 target
->target_number
,
4581 target_name(target
),
4582 target_type_name(target
),
4584 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
4585 Jim_GetString(teap
->body
, NULL
));
4587 /* Override current target by the target an event
4588 * is issued from (lot of scripts need it).
4589 * Return back to previous override as soon
4590 * as the handler processing is done */
4591 struct command_context
*cmd_ctx
= current_command_context(teap
->interp
);
4592 struct target
*saved_target_override
= cmd_ctx
->current_target_override
;
4593 cmd_ctx
->current_target_override
= target
;
4595 retval
= Jim_EvalObj(teap
->interp
, teap
->body
);
4597 cmd_ctx
->current_target_override
= saved_target_override
;
4599 if (retval
== ERROR_COMMAND_CLOSE_CONNECTION
)
4602 if (retval
== JIM_RETURN
)
4603 retval
= teap
->interp
->returnCode
;
4605 if (retval
!= JIM_OK
) {
4606 Jim_MakeErrorMessage(teap
->interp
);
4607 LOG_USER("Error executing event %s on target %s:\n%s",
4608 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
4609 target_name(target
),
4610 Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
4611 /* clean both error code and stacktrace before return */
4612 Jim_Eval(teap
->interp
, "error \"\" \"\"");
4619 * Returns true only if the target has a handler for the specified event.
4621 bool target_has_event_action(struct target
*target
, enum target_event event
)
4623 struct target_event_action
*teap
;
4625 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4626 if (teap
->event
== event
)
4632 enum target_cfg_param
{
4635 TCFG_WORK_AREA_VIRT
,
4636 TCFG_WORK_AREA_PHYS
,
4637 TCFG_WORK_AREA_SIZE
,
4638 TCFG_WORK_AREA_BACKUP
,
4641 TCFG_CHAIN_POSITION
,
4648 static Jim_Nvp nvp_config_opts
[] = {
4649 { .name
= "-type", .value
= TCFG_TYPE
},
4650 { .name
= "-event", .value
= TCFG_EVENT
},
4651 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
4652 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
4653 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
4654 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
4655 { .name
= "-endian", .value
= TCFG_ENDIAN
},
4656 { .name
= "-coreid", .value
= TCFG_COREID
},
4657 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
4658 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
4659 { .name
= "-rtos", .value
= TCFG_RTOS
},
4660 { .name
= "-defer-examine", .value
= TCFG_DEFER_EXAMINE
},
4661 { .name
= "-gdb-port", .value
= TCFG_GDB_PORT
},
4662 { .name
= NULL
, .value
= -1 }
4665 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
4672 /* parse config or cget options ... */
4673 while (goi
->argc
> 0) {
4674 Jim_SetEmptyResult(goi
->interp
);
4675 /* Jim_GetOpt_Debug(goi); */
4677 if (target
->type
->target_jim_configure
) {
4678 /* target defines a configure function */
4679 /* target gets first dibs on parameters */
4680 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
4689 /* otherwise we 'continue' below */
4691 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
4693 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
4699 if (goi
->isconfigure
) {
4700 Jim_SetResultFormatted(goi
->interp
,
4701 "not settable: %s", n
->name
);
4705 if (goi
->argc
!= 0) {
4706 Jim_WrongNumArgs(goi
->interp
,
4707 goi
->argc
, goi
->argv
,
4712 Jim_SetResultString(goi
->interp
,
4713 target_type_name(target
), -1);
4717 if (goi
->argc
== 0) {
4718 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
4722 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
4724 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
4728 if (goi
->isconfigure
) {
4729 if (goi
->argc
!= 1) {
4730 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
4734 if (goi
->argc
!= 0) {
4735 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
4741 struct target_event_action
*teap
;
4743 teap
= target
->event_action
;
4744 /* replace existing? */
4746 if (teap
->event
== (enum target_event
)n
->value
)
4751 if (goi
->isconfigure
) {
4752 bool replace
= true;
4755 teap
= calloc(1, sizeof(*teap
));
4758 teap
->event
= n
->value
;
4759 teap
->interp
= goi
->interp
;
4760 Jim_GetOpt_Obj(goi
, &o
);
4762 Jim_DecrRefCount(teap
->interp
, teap
->body
);
4763 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
4766 * Tcl/TK - "tk events" have a nice feature.
4767 * See the "BIND" command.
4768 * We should support that here.
4769 * You can specify %X and %Y in the event code.
4770 * The idea is: %T - target name.
4771 * The idea is: %N - target number
4772 * The idea is: %E - event name.
4774 Jim_IncrRefCount(teap
->body
);
4777 /* add to head of event list */
4778 teap
->next
= target
->event_action
;
4779 target
->event_action
= teap
;
4781 Jim_SetEmptyResult(goi
->interp
);
4785 Jim_SetEmptyResult(goi
->interp
);
4787 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
4793 case TCFG_WORK_AREA_VIRT
:
4794 if (goi
->isconfigure
) {
4795 target_free_all_working_areas(target
);
4796 e
= Jim_GetOpt_Wide(goi
, &w
);
4799 target
->working_area_virt
= w
;
4800 target
->working_area_virt_spec
= true;
4805 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
4809 case TCFG_WORK_AREA_PHYS
:
4810 if (goi
->isconfigure
) {
4811 target_free_all_working_areas(target
);
4812 e
= Jim_GetOpt_Wide(goi
, &w
);
4815 target
->working_area_phys
= w
;
4816 target
->working_area_phys_spec
= true;
4821 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
4825 case TCFG_WORK_AREA_SIZE
:
4826 if (goi
->isconfigure
) {
4827 target_free_all_working_areas(target
);
4828 e
= Jim_GetOpt_Wide(goi
, &w
);
4831 target
->working_area_size
= w
;
4836 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4840 case TCFG_WORK_AREA_BACKUP
:
4841 if (goi
->isconfigure
) {
4842 target_free_all_working_areas(target
);
4843 e
= Jim_GetOpt_Wide(goi
, &w
);
4846 /* make this exactly 1 or 0 */
4847 target
->backup_working_area
= (!!w
);
4852 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
4853 /* loop for more e*/
4858 if (goi
->isconfigure
) {
4859 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
4861 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
4864 target
->endianness
= n
->value
;
4869 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4870 if (n
->name
== NULL
) {
4871 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4872 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4874 Jim_SetResultString(goi
->interp
, n
->name
, -1);
4879 if (goi
->isconfigure
) {
4880 e
= Jim_GetOpt_Wide(goi
, &w
);
4883 target
->coreid
= (int32_t)w
;
4888 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->coreid
));
4892 case TCFG_CHAIN_POSITION
:
4893 if (goi
->isconfigure
) {
4895 struct jtag_tap
*tap
;
4897 if (target
->has_dap
) {
4898 Jim_SetResultString(goi
->interp
,
4899 "target requires -dap parameter instead of -chain-position!", -1);
4903 target_free_all_working_areas(target
);
4904 e
= Jim_GetOpt_Obj(goi
, &o_t
);
4907 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
4911 target
->tap_configured
= true;
4916 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
4917 /* loop for more e*/
4920 if (goi
->isconfigure
) {
4921 e
= Jim_GetOpt_Wide(goi
, &w
);
4924 target
->dbgbase
= (uint32_t)w
;
4925 target
->dbgbase_set
= true;
4930 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
4936 int result
= rtos_create(goi
, target
);
4937 if (result
!= JIM_OK
)
4943 case TCFG_DEFER_EXAMINE
:
4945 target
->defer_examine
= true;
4950 if (goi
->isconfigure
) {
4951 struct command_context
*cmd_ctx
= current_command_context(goi
->interp
);
4952 if (cmd_ctx
->mode
!= COMMAND_CONFIG
) {
4953 Jim_SetResultString(goi
->interp
, "-gdb-port must be configured before 'init'", -1);
4958 e
= Jim_GetOpt_String(goi
, &s
, NULL
);
4961 target
->gdb_port_override
= strdup(s
);
4966 Jim_SetResultString(goi
->interp
, target
->gdb_port_override
? : "undefined", -1);
4970 } /* while (goi->argc) */
4973 /* done - we return */
4977 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4981 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4982 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4984 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4985 "missing: -option ...");
4988 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4989 return target_configure(&goi
, target
);
4992 static int jim_target_mem2array(Jim_Interp
*interp
,
4993 int argc
, Jim_Obj
*const *argv
)
4995 struct target
*target
= Jim_CmdPrivData(interp
);
4996 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4999 static int jim_target_array2mem(Jim_Interp
*interp
,
5000 int argc
, Jim_Obj
*const *argv
)
5002 struct target
*target
= Jim_CmdPrivData(interp
);
5003 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
5006 static int jim_target_tap_disabled(Jim_Interp
*interp
)
5008 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
5012 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5014 bool allow_defer
= false;
5017 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5019 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5020 Jim_SetResultFormatted(goi
.interp
,
5021 "usage: %s ['allow-defer']", cmd_name
);
5025 strcmp(Jim_GetString(argv
[1], NULL
), "allow-defer") == 0) {
5028 int e
= Jim_GetOpt_Obj(&goi
, &obj
);
5034 struct target
*target
= Jim_CmdPrivData(interp
);
5035 if (!target
->tap
->enabled
)
5036 return jim_target_tap_disabled(interp
);
5038 if (allow_defer
&& target
->defer_examine
) {
5039 LOG_INFO("Deferring arp_examine of %s", target_name(target
));
5040 LOG_INFO("Use arp_examine command to examine it manually!");
5044 int e
= target
->type
->examine(target
);
5050 static int jim_target_was_examined(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5052 struct target
*target
= Jim_CmdPrivData(interp
);
5054 Jim_SetResultBool(interp
, target_was_examined(target
));
5058 static int jim_target_examine_deferred(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5060 struct target
*target
= Jim_CmdPrivData(interp
);
5062 Jim_SetResultBool(interp
, target
->defer_examine
);
5066 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5069 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5072 struct target
*target
= Jim_CmdPrivData(interp
);
5074 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
5080 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5083 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5086 struct target
*target
= Jim_CmdPrivData(interp
);
5087 if (!target
->tap
->enabled
)
5088 return jim_target_tap_disabled(interp
);
5091 if (!(target_was_examined(target
)))
5092 e
= ERROR_TARGET_NOT_EXAMINED
;
5094 e
= target
->type
->poll(target
);
5100 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5103 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5105 if (goi
.argc
!= 2) {
5106 Jim_WrongNumArgs(interp
, 0, argv
,
5107 "([tT]|[fF]|assert|deassert) BOOL");
5112 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
5114 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
5117 /* the halt or not param */
5119 e
= Jim_GetOpt_Wide(&goi
, &a
);
5123 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
5124 if (!target
->tap
->enabled
)
5125 return jim_target_tap_disabled(interp
);
5127 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
5128 Jim_SetResultFormatted(interp
,
5129 "No target-specific reset for %s",
5130 target_name(target
));
5134 if (target
->defer_examine
)
5135 target_reset_examined(target
);
5137 /* determine if we should halt or not. */
5138 target
->reset_halt
= !!a
;
5139 /* When this happens - all workareas are invalid. */
5140 target_free_all_working_areas_restore(target
, 0);
5143 if (n
->value
== NVP_ASSERT
)
5144 e
= target
->type
->assert_reset(target
);
5146 e
= target
->type
->deassert_reset(target
);
5147 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5150 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5153 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5156 struct target
*target
= Jim_CmdPrivData(interp
);
5157 if (!target
->tap
->enabled
)
5158 return jim_target_tap_disabled(interp
);
5159 int e
= target
->type
->halt(target
);
5160 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5163 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5166 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5168 /* params: <name> statename timeoutmsecs */
5169 if (goi
.argc
!= 2) {
5170 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5171 Jim_SetResultFormatted(goi
.interp
,
5172 "%s <state_name> <timeout_in_msec>", cmd_name
);
5177 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
5179 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
, 1);
5183 e
= Jim_GetOpt_Wide(&goi
, &a
);
5186 struct target
*target
= Jim_CmdPrivData(interp
);
5187 if (!target
->tap
->enabled
)
5188 return jim_target_tap_disabled(interp
);
5190 e
= target_wait_state(target
, n
->value
, a
);
5191 if (e
!= ERROR_OK
) {
5192 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
5193 Jim_SetResultFormatted(goi
.interp
,
5194 "target: %s wait %s fails (%#s) %s",
5195 target_name(target
), n
->name
,
5196 eObj
, target_strerror_safe(e
));
5201 /* List for human, Events defined for this target.
5202 * scripts/programs should use 'name cget -event NAME'
5204 COMMAND_HANDLER(handle_target_event_list
)
5206 struct target
*target
= get_current_target(CMD_CTX
);
5207 struct target_event_action
*teap
= target
->event_action
;
5209 command_print(CMD
, "Event actions for target (%d) %s\n",
5210 target
->target_number
,
5211 target_name(target
));
5212 command_print(CMD
, "%-25s | Body", "Event");
5213 command_print(CMD
, "------------------------- | "
5214 "----------------------------------------");
5216 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
5217 command_print(CMD
, "%-25s | %s",
5218 opt
->name
, Jim_GetString(teap
->body
, NULL
));
5221 command_print(CMD
, "***END***");
5224 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5227 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5230 struct target
*target
= Jim_CmdPrivData(interp
);
5231 Jim_SetResultString(interp
, target_state_name(target
), -1);
5234 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5237 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5238 if (goi
.argc
!= 1) {
5239 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5240 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
5244 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
5246 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
5249 struct target
*target
= Jim_CmdPrivData(interp
);
5250 target_handle_event(target
, n
->value
);
5254 static const struct command_registration target_instance_command_handlers
[] = {
5256 .name
= "configure",
5257 .mode
= COMMAND_ANY
,
5258 .jim_handler
= jim_target_configure
,
5259 .help
= "configure a new target for use",
5260 .usage
= "[target_attribute ...]",
5264 .mode
= COMMAND_ANY
,
5265 .jim_handler
= jim_target_configure
,
5266 .help
= "returns the specified target attribute",
5267 .usage
= "target_attribute",
5271 .handler
= handle_mw_command
,
5272 .mode
= COMMAND_EXEC
,
5273 .help
= "Write 64-bit word(s) to target memory",
5274 .usage
= "address data [count]",
5278 .handler
= handle_mw_command
,
5279 .mode
= COMMAND_EXEC
,
5280 .help
= "Write 32-bit word(s) to target memory",
5281 .usage
= "address data [count]",
5285 .handler
= handle_mw_command
,
5286 .mode
= COMMAND_EXEC
,
5287 .help
= "Write 16-bit half-word(s) to target memory",
5288 .usage
= "address data [count]",
5292 .handler
= handle_mw_command
,
5293 .mode
= COMMAND_EXEC
,
5294 .help
= "Write byte(s) to target memory",
5295 .usage
= "address data [count]",
5299 .handler
= handle_md_command
,
5300 .mode
= COMMAND_EXEC
,
5301 .help
= "Display target memory as 64-bit words",
5302 .usage
= "address [count]",
5306 .handler
= handle_md_command
,
5307 .mode
= COMMAND_EXEC
,
5308 .help
= "Display target memory as 32-bit words",
5309 .usage
= "address [count]",
5313 .handler
= handle_md_command
,
5314 .mode
= COMMAND_EXEC
,
5315 .help
= "Display target memory as 16-bit half-words",
5316 .usage
= "address [count]",
5320 .handler
= handle_md_command
,
5321 .mode
= COMMAND_EXEC
,
5322 .help
= "Display target memory as 8-bit bytes",
5323 .usage
= "address [count]",
5326 .name
= "array2mem",
5327 .mode
= COMMAND_EXEC
,
5328 .jim_handler
= jim_target_array2mem
,
5329 .help
= "Writes Tcl array of 8/16/32 bit numbers "
5331 .usage
= "arrayname bitwidth address count",
5334 .name
= "mem2array",
5335 .mode
= COMMAND_EXEC
,
5336 .jim_handler
= jim_target_mem2array
,
5337 .help
= "Loads Tcl array of 8/16/32 bit numbers "
5338 "from target memory",
5339 .usage
= "arrayname bitwidth address count",
5342 .name
= "eventlist",
5343 .handler
= handle_target_event_list
,
5344 .mode
= COMMAND_EXEC
,
5345 .help
= "displays a table of events defined for this target",
5350 .mode
= COMMAND_EXEC
,
5351 .jim_handler
= jim_target_current_state
,
5352 .help
= "displays the current state of this target",
5355 .name
= "arp_examine",
5356 .mode
= COMMAND_EXEC
,
5357 .jim_handler
= jim_target_examine
,
5358 .help
= "used internally for reset processing",
5359 .usage
= "['allow-defer']",
5362 .name
= "was_examined",
5363 .mode
= COMMAND_EXEC
,
5364 .jim_handler
= jim_target_was_examined
,
5365 .help
= "used internally for reset processing",
5368 .name
= "examine_deferred",
5369 .mode
= COMMAND_EXEC
,
5370 .jim_handler
= jim_target_examine_deferred
,
5371 .help
= "used internally for reset processing",
5374 .name
= "arp_halt_gdb",
5375 .mode
= COMMAND_EXEC
,
5376 .jim_handler
= jim_target_halt_gdb
,
5377 .help
= "used internally for reset processing to halt GDB",
5381 .mode
= COMMAND_EXEC
,
5382 .jim_handler
= jim_target_poll
,
5383 .help
= "used internally for reset processing",
5386 .name
= "arp_reset",
5387 .mode
= COMMAND_EXEC
,
5388 .jim_handler
= jim_target_reset
,
5389 .help
= "used internally for reset processing",
5393 .mode
= COMMAND_EXEC
,
5394 .jim_handler
= jim_target_halt
,
5395 .help
= "used internally for reset processing",
5398 .name
= "arp_waitstate",
5399 .mode
= COMMAND_EXEC
,
5400 .jim_handler
= jim_target_wait_state
,
5401 .help
= "used internally for reset processing",
5404 .name
= "invoke-event",
5405 .mode
= COMMAND_EXEC
,
5406 .jim_handler
= jim_target_invoke_event
,
5407 .help
= "invoke handler for specified event",
5408 .usage
= "event_name",
5410 COMMAND_REGISTRATION_DONE
5413 static int target_create(Jim_GetOptInfo
*goi
)
5420 struct target
*target
;
5421 struct command_context
*cmd_ctx
;
5423 cmd_ctx
= current_command_context(goi
->interp
);
5424 assert(cmd_ctx
!= NULL
);
5426 if (goi
->argc
< 3) {
5427 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
5432 Jim_GetOpt_Obj(goi
, &new_cmd
);
5433 /* does this command exist? */
5434 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
5436 cp
= Jim_GetString(new_cmd
, NULL
);
5437 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
5442 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
5445 struct transport
*tr
= get_current_transport();
5446 if (tr
->override_target
) {
5447 e
= tr
->override_target(&cp
);
5448 if (e
!= ERROR_OK
) {
5449 LOG_ERROR("The selected transport doesn't support this target");
5452 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
5454 /* now does target type exist */
5455 for (x
= 0 ; target_types
[x
] ; x
++) {
5456 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
5461 /* check for deprecated name */
5462 if (target_types
[x
]->deprecated_name
) {
5463 if (0 == strcmp(cp
, target_types
[x
]->deprecated_name
)) {
5465 LOG_WARNING("target name is deprecated use: \'%s\'", target_types
[x
]->name
);
5470 if (target_types
[x
] == NULL
) {
5471 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
5472 for (x
= 0 ; target_types
[x
] ; x
++) {
5473 if (target_types
[x
+ 1]) {
5474 Jim_AppendStrings(goi
->interp
,
5475 Jim_GetResult(goi
->interp
),
5476 target_types
[x
]->name
,
5479 Jim_AppendStrings(goi
->interp
,
5480 Jim_GetResult(goi
->interp
),
5482 target_types
[x
]->name
, NULL
);
5489 target
= calloc(1, sizeof(struct target
));
5490 /* set target number */
5491 target
->target_number
= new_target_number();
5492 cmd_ctx
->current_target
= target
;
5494 /* allocate memory for each unique target type */
5495 target
->type
= calloc(1, sizeof(struct target_type
));
5497 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
5499 /* will be set by "-endian" */
5500 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5502 /* default to first core, override with -coreid */
5505 target
->working_area
= 0x0;
5506 target
->working_area_size
= 0x0;
5507 target
->working_areas
= NULL
;
5508 target
->backup_working_area
= 0;
5510 target
->state
= TARGET_UNKNOWN
;
5511 target
->debug_reason
= DBG_REASON_UNDEFINED
;
5512 target
->reg_cache
= NULL
;
5513 target
->breakpoints
= NULL
;
5514 target
->watchpoints
= NULL
;
5515 target
->next
= NULL
;
5516 target
->arch_info
= NULL
;
5518 target
->verbose_halt_msg
= true;
5520 target
->halt_issued
= false;
5522 /* initialize trace information */
5523 target
->trace_info
= calloc(1, sizeof(struct trace
));
5525 target
->dbgmsg
= NULL
;
5526 target
->dbg_msg_enabled
= 0;
5528 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5530 target
->rtos
= NULL
;
5531 target
->rtos_auto_detect
= false;
5533 target
->gdb_port_override
= NULL
;
5535 /* Do the rest as "configure" options */
5536 goi
->isconfigure
= 1;
5537 e
= target_configure(goi
, target
);
5540 if (target
->has_dap
) {
5541 if (!target
->dap_configured
) {
5542 Jim_SetResultString(goi
->interp
, "-dap ?name? required when creating target", -1);
5546 if (!target
->tap_configured
) {
5547 Jim_SetResultString(goi
->interp
, "-chain-position ?name? required when creating target", -1);
5551 /* tap must be set after target was configured */
5552 if (target
->tap
== NULL
)
5557 free(target
->gdb_port_override
);
5563 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
5564 /* default endian to little if not specified */
5565 target
->endianness
= TARGET_LITTLE_ENDIAN
;
5568 cp
= Jim_GetString(new_cmd
, NULL
);
5569 target
->cmd_name
= strdup(cp
);
5571 if (target
->type
->target_create
) {
5572 e
= (*(target
->type
->target_create
))(target
, goi
->interp
);
5573 if (e
!= ERROR_OK
) {
5574 LOG_DEBUG("target_create failed");
5575 free(target
->gdb_port_override
);
5577 free(target
->cmd_name
);
5583 /* create the target specific commands */
5584 if (target
->type
->commands
) {
5585 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
5587 LOG_ERROR("unable to register '%s' commands", cp
);
5590 /* append to end of list */
5592 struct target
**tpp
;
5593 tpp
= &(all_targets
);
5595 tpp
= &((*tpp
)->next
);
5599 /* now - create the new target name command */
5600 const struct command_registration target_subcommands
[] = {
5602 .chain
= target_instance_command_handlers
,
5605 .chain
= target
->type
->commands
,
5607 COMMAND_REGISTRATION_DONE
5609 const struct command_registration target_commands
[] = {
5612 .mode
= COMMAND_ANY
,
5613 .help
= "target command group",
5615 .chain
= target_subcommands
,
5617 COMMAND_REGISTRATION_DONE
5619 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
5623 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
5625 command_set_handler_data(c
, target
);
5627 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
5630 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5633 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5636 struct command_context
*cmd_ctx
= current_command_context(interp
);
5637 assert(cmd_ctx
!= NULL
);
5639 Jim_SetResultString(interp
, target_name(get_current_target(cmd_ctx
)), -1);
5643 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5646 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5649 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5650 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++) {
5651 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5652 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
5657 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5660 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5663 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5664 struct target
*target
= all_targets
;
5666 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5667 Jim_NewStringObj(interp
, target_name(target
), -1));
5668 target
= target
->next
;
5673 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5676 const char *targetname
;
5678 struct target
*target
= (struct target
*) NULL
;
5679 struct target_list
*head
, *curr
, *new;
5680 curr
= (struct target_list
*) NULL
;
5681 head
= (struct target_list
*) NULL
;
5684 LOG_DEBUG("%d", argc
);
5685 /* argv[1] = target to associate in smp
5686 * argv[2] = target to associate in smp
5690 for (i
= 1; i
< argc
; i
++) {
5692 targetname
= Jim_GetString(argv
[i
], &len
);
5693 target
= get_target(targetname
);
5694 LOG_DEBUG("%s ", targetname
);
5696 new = malloc(sizeof(struct target_list
));
5697 new->target
= target
;
5698 new->next
= (struct target_list
*)NULL
;
5699 if (head
== (struct target_list
*)NULL
) {
5708 /* now parse the list of cpu and put the target in smp mode*/
5711 while (curr
!= (struct target_list
*)NULL
) {
5712 target
= curr
->target
;
5714 target
->head
= head
;
5718 if (target
&& target
->rtos
)
5719 retval
= rtos_smp_init(head
->target
);
5725 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5728 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5730 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5731 "<name> <target_type> [<target_options> ...]");
5734 return target_create(&goi
);
5737 static const struct command_registration target_subcommand_handlers
[] = {
5740 .mode
= COMMAND_CONFIG
,
5741 .handler
= handle_target_init_command
,
5742 .help
= "initialize targets",
5747 .mode
= COMMAND_CONFIG
,
5748 .jim_handler
= jim_target_create
,
5749 .usage
= "name type '-chain-position' name [options ...]",
5750 .help
= "Creates and selects a new target",
5754 .mode
= COMMAND_ANY
,
5755 .jim_handler
= jim_target_current
,
5756 .help
= "Returns the currently selected target",
5760 .mode
= COMMAND_ANY
,
5761 .jim_handler
= jim_target_types
,
5762 .help
= "Returns the available target types as "
5763 "a list of strings",
5767 .mode
= COMMAND_ANY
,
5768 .jim_handler
= jim_target_names
,
5769 .help
= "Returns the names of all targets as a list of strings",
5773 .mode
= COMMAND_ANY
,
5774 .jim_handler
= jim_target_smp
,
5775 .usage
= "targetname1 targetname2 ...",
5776 .help
= "gather several target in a smp list"
5779 COMMAND_REGISTRATION_DONE
5783 target_addr_t address
;
5789 static int fastload_num
;
5790 static struct FastLoad
*fastload
;
5792 static void free_fastload(void)
5794 if (fastload
!= NULL
) {
5796 for (i
= 0; i
< fastload_num
; i
++) {
5797 if (fastload
[i
].data
)
5798 free(fastload
[i
].data
);
5805 COMMAND_HANDLER(handle_fast_load_image_command
)
5809 uint32_t image_size
;
5810 target_addr_t min_address
= 0;
5811 target_addr_t max_address
= -1;
5816 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
5817 &image
, &min_address
, &max_address
);
5818 if (ERROR_OK
!= retval
)
5821 struct duration bench
;
5822 duration_start(&bench
);
5824 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5825 if (retval
!= ERROR_OK
)
5830 fastload_num
= image
.num_sections
;
5831 fastload
= malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5832 if (fastload
== NULL
) {
5833 command_print(CMD
, "out of memory");
5834 image_close(&image
);
5837 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5838 for (i
= 0; i
< image
.num_sections
; i
++) {
5839 buffer
= malloc(image
.sections
[i
].size
);
5840 if (buffer
== NULL
) {
5841 command_print(CMD
, "error allocating buffer for section (%d bytes)",
5842 (int)(image
.sections
[i
].size
));
5843 retval
= ERROR_FAIL
;
5847 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
5848 if (retval
!= ERROR_OK
) {
5853 uint32_t offset
= 0;
5854 uint32_t length
= buf_cnt
;
5856 /* DANGER!!! beware of unsigned comparison here!!! */
5858 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
5859 (image
.sections
[i
].base_address
< max_address
)) {
5860 if (image
.sections
[i
].base_address
< min_address
) {
5861 /* clip addresses below */
5862 offset
+= min_address
-image
.sections
[i
].base_address
;
5866 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5867 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5869 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5870 fastload
[i
].data
= malloc(length
);
5871 if (fastload
[i
].data
== NULL
) {
5873 command_print(CMD
, "error allocating buffer for section (%" PRIu32
" bytes)",
5875 retval
= ERROR_FAIL
;
5878 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5879 fastload
[i
].length
= length
;
5881 image_size
+= length
;
5882 command_print(CMD
, "%u bytes written at address 0x%8.8x",
5883 (unsigned int)length
,
5884 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
5890 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
5891 command_print(CMD
, "Loaded %" PRIu32
" bytes "
5892 "in %fs (%0.3f KiB/s)", image_size
,
5893 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
5896 "WARNING: image has not been loaded to target!"
5897 "You can issue a 'fast_load' to finish loading.");
5900 image_close(&image
);
5902 if (retval
!= ERROR_OK
)
5908 COMMAND_HANDLER(handle_fast_load_command
)
5911 return ERROR_COMMAND_SYNTAX_ERROR
;
5912 if (fastload
== NULL
) {
5913 LOG_ERROR("No image in memory");
5917 int64_t ms
= timeval_ms();
5919 int retval
= ERROR_OK
;
5920 for (i
= 0; i
< fastload_num
; i
++) {
5921 struct target
*target
= get_current_target(CMD_CTX
);
5922 command_print(CMD
, "Write to 0x%08x, length 0x%08x",
5923 (unsigned int)(fastload
[i
].address
),
5924 (unsigned int)(fastload
[i
].length
));
5925 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5926 if (retval
!= ERROR_OK
)
5928 size
+= fastload
[i
].length
;
5930 if (retval
== ERROR_OK
) {
5931 int64_t after
= timeval_ms();
5932 command_print(CMD
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5937 static const struct command_registration target_command_handlers
[] = {
5940 .handler
= handle_targets_command
,
5941 .mode
= COMMAND_ANY
,
5942 .help
= "change current default target (one parameter) "
5943 "or prints table of all targets (no parameters)",
5944 .usage
= "[target]",
5948 .mode
= COMMAND_CONFIG
,
5949 .help
= "configure target",
5950 .chain
= target_subcommand_handlers
,
5953 COMMAND_REGISTRATION_DONE
5956 int target_register_commands(struct command_context
*cmd_ctx
)
5958 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5961 static bool target_reset_nag
= true;
5963 bool get_target_reset_nag(void)
5965 return target_reset_nag
;
5968 COMMAND_HANDLER(handle_target_reset_nag
)
5970 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
5971 &target_reset_nag
, "Nag after each reset about options to improve "
5975 COMMAND_HANDLER(handle_ps_command
)
5977 struct target
*target
= get_current_target(CMD_CTX
);
5979 if (target
->state
!= TARGET_HALTED
) {
5980 LOG_INFO("target not halted !!");
5984 if ((target
->rtos
) && (target
->rtos
->type
)
5985 && (target
->rtos
->type
->ps_command
)) {
5986 display
= target
->rtos
->type
->ps_command(target
);
5987 command_print(CMD
, "%s", display
);
5992 return ERROR_TARGET_FAILURE
;
5996 static void binprint(struct command_invocation
*cmd
, const char *text
, const uint8_t *buf
, int size
)
5999 command_print_sameline(cmd
, "%s", text
);
6000 for (int i
= 0; i
< size
; i
++)
6001 command_print_sameline(cmd
, " %02x", buf
[i
]);
6002 command_print(cmd
, " ");
6005 COMMAND_HANDLER(handle_test_mem_access_command
)
6007 struct target
*target
= get_current_target(CMD_CTX
);
6009 int retval
= ERROR_OK
;
6011 if (target
->state
!= TARGET_HALTED
) {
6012 LOG_INFO("target not halted !!");
6017 return ERROR_COMMAND_SYNTAX_ERROR
;
6019 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], test_size
);
6022 size_t num_bytes
= test_size
+ 4;
6024 struct working_area
*wa
= NULL
;
6025 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6026 if (retval
!= ERROR_OK
) {
6027 LOG_ERROR("Not enough working area");
6031 uint8_t *test_pattern
= malloc(num_bytes
);
6033 for (size_t i
= 0; i
< num_bytes
; i
++)
6034 test_pattern
[i
] = rand();
6036 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6037 if (retval
!= ERROR_OK
) {
6038 LOG_ERROR("Test pattern write failed");
6042 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6043 for (int size
= 1; size
<= 4; size
*= 2) {
6044 for (int offset
= 0; offset
< 4; offset
++) {
6045 uint32_t count
= test_size
/ size
;
6046 size_t host_bufsiz
= (count
+ 2) * size
+ host_offset
;
6047 uint8_t *read_ref
= malloc(host_bufsiz
);
6048 uint8_t *read_buf
= malloc(host_bufsiz
);
6050 for (size_t i
= 0; i
< host_bufsiz
; i
++) {
6051 read_ref
[i
] = rand();
6052 read_buf
[i
] = read_ref
[i
];
6054 command_print_sameline(CMD
,
6055 "Test read %" PRIu32
" x %d @ %d to %saligned buffer: ", count
,
6056 size
, offset
, host_offset
? "un" : "");
6058 struct duration bench
;
6059 duration_start(&bench
);
6061 retval
= target_read_memory(target
, wa
->address
+ offset
, size
, count
,
6062 read_buf
+ size
+ host_offset
);
6064 duration_measure(&bench
);
6066 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6067 command_print(CMD
, "Unsupported alignment");
6069 } else if (retval
!= ERROR_OK
) {
6070 command_print(CMD
, "Memory read failed");
6074 /* replay on host */
6075 memcpy(read_ref
+ size
+ host_offset
, test_pattern
+ offset
, count
* size
);
6078 int result
= memcmp(read_ref
, read_buf
, host_bufsiz
);
6080 command_print(CMD
, "Pass in %fs (%0.3f KiB/s)",
6081 duration_elapsed(&bench
),
6082 duration_kbps(&bench
, count
* size
));
6084 command_print(CMD
, "Compare failed");
6085 binprint(CMD
, "ref:", read_ref
, host_bufsiz
);
6086 binprint(CMD
, "buf:", read_buf
, host_bufsiz
);
6099 target_free_working_area(target
, wa
);
6102 num_bytes
= test_size
+ 4 + 4 + 4;
6104 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6105 if (retval
!= ERROR_OK
) {
6106 LOG_ERROR("Not enough working area");
6110 test_pattern
= malloc(num_bytes
);
6112 for (size_t i
= 0; i
< num_bytes
; i
++)
6113 test_pattern
[i
] = rand();
6115 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6116 for (int size
= 1; size
<= 4; size
*= 2) {
6117 for (int offset
= 0; offset
< 4; offset
++) {
6118 uint32_t count
= test_size
/ size
;
6119 size_t host_bufsiz
= count
* size
+ host_offset
;
6120 uint8_t *read_ref
= malloc(num_bytes
);
6121 uint8_t *read_buf
= malloc(num_bytes
);
6122 uint8_t *write_buf
= malloc(host_bufsiz
);
6124 for (size_t i
= 0; i
< host_bufsiz
; i
++)
6125 write_buf
[i
] = rand();
6126 command_print_sameline(CMD
,
6127 "Test write %" PRIu32
" x %d @ %d from %saligned buffer: ", count
,
6128 size
, offset
, host_offset
? "un" : "");
6130 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6131 if (retval
!= ERROR_OK
) {
6132 command_print(CMD
, "Test pattern write failed");
6136 /* replay on host */
6137 memcpy(read_ref
, test_pattern
, num_bytes
);
6138 memcpy(read_ref
+ size
+ offset
, write_buf
+ host_offset
, count
* size
);
6140 struct duration bench
;
6141 duration_start(&bench
);
6143 retval
= target_write_memory(target
, wa
->address
+ size
+ offset
, size
, count
,
6144 write_buf
+ host_offset
);
6146 duration_measure(&bench
);
6148 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6149 command_print(CMD
, "Unsupported alignment");
6151 } else if (retval
!= ERROR_OK
) {
6152 command_print(CMD
, "Memory write failed");
6157 retval
= target_read_memory(target
, wa
->address
, 1, num_bytes
, read_buf
);
6158 if (retval
!= ERROR_OK
) {
6159 command_print(CMD
, "Test pattern write failed");
6164 int result
= memcmp(read_ref
, read_buf
, num_bytes
);
6166 command_print(CMD
, "Pass in %fs (%0.3f KiB/s)",
6167 duration_elapsed(&bench
),
6168 duration_kbps(&bench
, count
* size
));
6170 command_print(CMD
, "Compare failed");
6171 binprint(CMD
, "ref:", read_ref
, num_bytes
);
6172 binprint(CMD
, "buf:", read_buf
, num_bytes
);
6184 target_free_working_area(target
, wa
);
6188 static const struct command_registration target_exec_command_handlers
[] = {
6190 .name
= "fast_load_image",
6191 .handler
= handle_fast_load_image_command
,
6192 .mode
= COMMAND_ANY
,
6193 .help
= "Load image into server memory for later use by "
6194 "fast_load; primarily for profiling",
6195 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6196 "[min_address [max_length]]",
6199 .name
= "fast_load",
6200 .handler
= handle_fast_load_command
,
6201 .mode
= COMMAND_EXEC
,
6202 .help
= "loads active fast load image to current target "
6203 "- mainly for profiling purposes",
6208 .handler
= handle_profile_command
,
6209 .mode
= COMMAND_EXEC
,
6210 .usage
= "seconds filename [start end]",
6211 .help
= "profiling samples the CPU PC",
6213 /** @todo don't register virt2phys() unless target supports it */
6215 .name
= "virt2phys",
6216 .handler
= handle_virt2phys_command
,
6217 .mode
= COMMAND_ANY
,
6218 .help
= "translate a virtual address into a physical address",
6219 .usage
= "virtual_address",
6223 .handler
= handle_reg_command
,
6224 .mode
= COMMAND_EXEC
,
6225 .help
= "display (reread from target with \"force\") or set a register; "
6226 "with no arguments, displays all registers and their values",
6227 .usage
= "[(register_number|register_name) [(value|'force')]]",
6231 .handler
= handle_poll_command
,
6232 .mode
= COMMAND_EXEC
,
6233 .help
= "poll target state; or reconfigure background polling",
6234 .usage
= "['on'|'off']",
6237 .name
= "wait_halt",
6238 .handler
= handle_wait_halt_command
,
6239 .mode
= COMMAND_EXEC
,
6240 .help
= "wait up to the specified number of milliseconds "
6241 "(default 5000) for a previously requested halt",
6242 .usage
= "[milliseconds]",
6246 .handler
= handle_halt_command
,
6247 .mode
= COMMAND_EXEC
,
6248 .help
= "request target to halt, then wait up to the specified "
6249 "number of milliseconds (default 5000) for it to complete",
6250 .usage
= "[milliseconds]",
6254 .handler
= handle_resume_command
,
6255 .mode
= COMMAND_EXEC
,
6256 .help
= "resume target execution from current PC or address",
6257 .usage
= "[address]",
6261 .handler
= handle_reset_command
,
6262 .mode
= COMMAND_EXEC
,
6263 .usage
= "[run|halt|init]",
6264 .help
= "Reset all targets into the specified mode. "
6265 "Default reset mode is run, if not given.",
6268 .name
= "soft_reset_halt",
6269 .handler
= handle_soft_reset_halt_command
,
6270 .mode
= COMMAND_EXEC
,
6272 .help
= "halt the target and do a soft reset",
6276 .handler
= handle_step_command
,
6277 .mode
= COMMAND_EXEC
,
6278 .help
= "step one instruction from current PC or address",
6279 .usage
= "[address]",
6283 .handler
= handle_md_command
,
6284 .mode
= COMMAND_EXEC
,
6285 .help
= "display memory double-words",
6286 .usage
= "['phys'] address [count]",
6290 .handler
= handle_md_command
,
6291 .mode
= COMMAND_EXEC
,
6292 .help
= "display memory words",
6293 .usage
= "['phys'] address [count]",
6297 .handler
= handle_md_command
,
6298 .mode
= COMMAND_EXEC
,
6299 .help
= "display memory half-words",
6300 .usage
= "['phys'] address [count]",
6304 .handler
= handle_md_command
,
6305 .mode
= COMMAND_EXEC
,
6306 .help
= "display memory bytes",
6307 .usage
= "['phys'] address [count]",
6311 .handler
= handle_mw_command
,
6312 .mode
= COMMAND_EXEC
,
6313 .help
= "write memory double-word",
6314 .usage
= "['phys'] address value [count]",
6318 .handler
= handle_mw_command
,
6319 .mode
= COMMAND_EXEC
,
6320 .help
= "write memory word",
6321 .usage
= "['phys'] address value [count]",
6325 .handler
= handle_mw_command
,
6326 .mode
= COMMAND_EXEC
,
6327 .help
= "write memory half-word",
6328 .usage
= "['phys'] address value [count]",
6332 .handler
= handle_mw_command
,
6333 .mode
= COMMAND_EXEC
,
6334 .help
= "write memory byte",
6335 .usage
= "['phys'] address value [count]",
6339 .handler
= handle_bp_command
,
6340 .mode
= COMMAND_EXEC
,
6341 .help
= "list or set hardware or software breakpoint",
6342 .usage
= "[<address> [<asid>] <length> ['hw'|'hw_ctx']]",
6346 .handler
= handle_rbp_command
,
6347 .mode
= COMMAND_EXEC
,
6348 .help
= "remove breakpoint",
6349 .usage
= "'all' | address",
6353 .handler
= handle_wp_command
,
6354 .mode
= COMMAND_EXEC
,
6355 .help
= "list (no params) or create watchpoints",
6356 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
6360 .handler
= handle_rwp_command
,
6361 .mode
= COMMAND_EXEC
,
6362 .help
= "remove watchpoint",
6366 .name
= "load_image",
6367 .handler
= handle_load_image_command
,
6368 .mode
= COMMAND_EXEC
,
6369 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6370 "[min_address] [max_length]",
6373 .name
= "dump_image",
6374 .handler
= handle_dump_image_command
,
6375 .mode
= COMMAND_EXEC
,
6376 .usage
= "filename address size",
6379 .name
= "verify_image_checksum",
6380 .handler
= handle_verify_image_checksum_command
,
6381 .mode
= COMMAND_EXEC
,
6382 .usage
= "filename [offset [type]]",
6385 .name
= "verify_image",
6386 .handler
= handle_verify_image_command
,
6387 .mode
= COMMAND_EXEC
,
6388 .usage
= "filename [offset [type]]",
6391 .name
= "test_image",
6392 .handler
= handle_test_image_command
,
6393 .mode
= COMMAND_EXEC
,
6394 .usage
= "filename [offset [type]]",
6397 .name
= "mem2array",
6398 .mode
= COMMAND_EXEC
,
6399 .jim_handler
= jim_mem2array
,
6400 .help
= "read 8/16/32 bit memory and return as a TCL array "
6401 "for script processing",
6402 .usage
= "arrayname bitwidth address count",
6405 .name
= "array2mem",
6406 .mode
= COMMAND_EXEC
,
6407 .jim_handler
= jim_array2mem
,
6408 .help
= "convert a TCL array to memory locations "
6409 "and write the 8/16/32 bit values",
6410 .usage
= "arrayname bitwidth address count",
6413 .name
= "reset_nag",
6414 .handler
= handle_target_reset_nag
,
6415 .mode
= COMMAND_ANY
,
6416 .help
= "Nag after each reset about options that could have been "
6417 "enabled to improve performance. ",
6418 .usage
= "['enable'|'disable']",
6422 .handler
= handle_ps_command
,
6423 .mode
= COMMAND_EXEC
,
6424 .help
= "list all tasks ",
6428 .name
= "test_mem_access",
6429 .handler
= handle_test_mem_access_command
,
6430 .mode
= COMMAND_EXEC
,
6431 .help
= "Test the target's memory access functions",
6435 COMMAND_REGISTRATION_DONE
6437 static int target_register_user_commands(struct command_context
*cmd_ctx
)
6439 int retval
= ERROR_OK
;
6440 retval
= target_request_register_commands(cmd_ctx
);
6441 if (retval
!= ERROR_OK
)
6444 retval
= trace_register_commands(cmd_ctx
);
6445 if (retval
!= ERROR_OK
)
6449 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);