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 static void append_to_list_all_targets(struct target
*target
)
349 struct target
**t
= &all_targets
;
356 /* read a uint64_t from a buffer in target memory endianness */
357 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
359 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
360 return le_to_h_u64(buffer
);
362 return be_to_h_u64(buffer
);
365 /* read a uint32_t from a buffer in target memory endianness */
366 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
368 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
369 return le_to_h_u32(buffer
);
371 return be_to_h_u32(buffer
);
374 /* read a uint24_t from a buffer in target memory endianness */
375 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
377 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
378 return le_to_h_u24(buffer
);
380 return be_to_h_u24(buffer
);
383 /* read a uint16_t from a buffer in target memory endianness */
384 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
386 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
387 return le_to_h_u16(buffer
);
389 return be_to_h_u16(buffer
);
392 /* write a uint64_t to a buffer in target memory endianness */
393 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
395 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
396 h_u64_to_le(buffer
, value
);
398 h_u64_to_be(buffer
, value
);
401 /* write a uint32_t to a buffer in target memory endianness */
402 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
404 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
405 h_u32_to_le(buffer
, value
);
407 h_u32_to_be(buffer
, value
);
410 /* write a uint24_t to a buffer in target memory endianness */
411 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
413 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
414 h_u24_to_le(buffer
, value
);
416 h_u24_to_be(buffer
, value
);
419 /* write a uint16_t to a buffer in target memory endianness */
420 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
422 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
423 h_u16_to_le(buffer
, value
);
425 h_u16_to_be(buffer
, value
);
428 /* write a uint8_t to a buffer in target memory endianness */
429 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
434 /* write a uint64_t array to a buffer in target memory endianness */
435 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
438 for (i
= 0; i
< count
; i
++)
439 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
442 /* write a uint32_t array to a buffer in target memory endianness */
443 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
446 for (i
= 0; i
< count
; i
++)
447 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
450 /* write a uint16_t array to a buffer in target memory endianness */
451 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
454 for (i
= 0; i
< count
; i
++)
455 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
458 /* write a uint64_t array to a buffer in target memory endianness */
459 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
462 for (i
= 0; i
< count
; i
++)
463 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
466 /* write a uint32_t array to a buffer in target memory endianness */
467 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
470 for (i
= 0; i
< count
; i
++)
471 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
474 /* write a uint16_t array to a buffer in target memory endianness */
475 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
478 for (i
= 0; i
< count
; i
++)
479 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
482 /* return a pointer to a configured target; id is name or number */
483 struct target
*get_target(const char *id
)
485 struct target
*target
;
487 /* try as tcltarget name */
488 for (target
= all_targets
; target
; target
= target
->next
) {
489 if (target_name(target
) == NULL
)
491 if (strcmp(id
, target_name(target
)) == 0)
495 /* It's OK to remove this fallback sometime after August 2010 or so */
497 /* no match, try as number */
499 if (parse_uint(id
, &num
) != ERROR_OK
)
502 for (target
= all_targets
; target
; target
= target
->next
) {
503 if (target
->target_number
== (int)num
) {
504 LOG_WARNING("use '%s' as target identifier, not '%u'",
505 target_name(target
), num
);
513 /* returns a pointer to the n-th configured target */
514 struct target
*get_target_by_num(int num
)
516 struct target
*target
= all_targets
;
519 if (target
->target_number
== num
)
521 target
= target
->next
;
527 struct target
*get_current_target(struct command_context
*cmd_ctx
)
529 struct target
*target
= get_current_target_or_null(cmd_ctx
);
531 if (target
== NULL
) {
532 LOG_ERROR("BUG: current_target out of bounds");
539 struct target
*get_current_target_or_null(struct command_context
*cmd_ctx
)
541 return cmd_ctx
->current_target_override
542 ? cmd_ctx
->current_target_override
543 : cmd_ctx
->current_target
;
546 int target_poll(struct target
*target
)
550 /* We can't poll until after examine */
551 if (!target_was_examined(target
)) {
552 /* Fail silently lest we pollute the log */
556 retval
= target
->type
->poll(target
);
557 if (retval
!= ERROR_OK
)
560 if (target
->halt_issued
) {
561 if (target
->state
== TARGET_HALTED
)
562 target
->halt_issued
= false;
564 int64_t t
= timeval_ms() - target
->halt_issued_time
;
565 if (t
> DEFAULT_HALT_TIMEOUT
) {
566 target
->halt_issued
= false;
567 LOG_INFO("Halt timed out, wake up GDB.");
568 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
576 int target_halt(struct target
*target
)
579 /* We can't poll until after examine */
580 if (!target_was_examined(target
)) {
581 LOG_ERROR("Target not examined yet");
585 retval
= target
->type
->halt(target
);
586 if (retval
!= ERROR_OK
)
589 target
->halt_issued
= true;
590 target
->halt_issued_time
= timeval_ms();
596 * Make the target (re)start executing using its saved execution
597 * context (possibly with some modifications).
599 * @param target Which target should start executing.
600 * @param current True to use the target's saved program counter instead
601 * of the address parameter
602 * @param address Optionally used as the program counter.
603 * @param handle_breakpoints True iff breakpoints at the resumption PC
604 * should be skipped. (For example, maybe execution was stopped by
605 * such a breakpoint, in which case it would be counterproductive to
607 * @param debug_execution False if all working areas allocated by OpenOCD
608 * should be released and/or restored to their original contents.
609 * (This would for example be true to run some downloaded "helper"
610 * algorithm code, which resides in one such working buffer and uses
611 * another for data storage.)
613 * @todo Resolve the ambiguity about what the "debug_execution" flag
614 * signifies. For example, Target implementations don't agree on how
615 * it relates to invalidation of the register cache, or to whether
616 * breakpoints and watchpoints should be enabled. (It would seem wrong
617 * to enable breakpoints when running downloaded "helper" algorithms
618 * (debug_execution true), since the breakpoints would be set to match
619 * target firmware being debugged, not the helper algorithm.... and
620 * enabling them could cause such helpers to malfunction (for example,
621 * by overwriting data with a breakpoint instruction. On the other
622 * hand the infrastructure for running such helpers might use this
623 * procedure but rely on hardware breakpoint to detect termination.)
625 int target_resume(struct target
*target
, int current
, target_addr_t address
,
626 int handle_breakpoints
, int debug_execution
)
630 /* We can't poll until after examine */
631 if (!target_was_examined(target
)) {
632 LOG_ERROR("Target not examined yet");
636 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
638 /* note that resume *must* be asynchronous. The CPU can halt before
639 * we poll. The CPU can even halt at the current PC as a result of
640 * a software breakpoint being inserted by (a bug?) the application.
642 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
643 if (retval
!= ERROR_OK
)
646 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
651 static int target_process_reset(struct command_invocation
*cmd
, enum target_reset_mode reset_mode
)
656 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
657 if (n
->name
== NULL
) {
658 LOG_ERROR("invalid reset mode");
662 struct target
*target
;
663 for (target
= all_targets
; target
; target
= target
->next
)
664 target_call_reset_callbacks(target
, reset_mode
);
666 /* disable polling during reset to make reset event scripts
667 * more predictable, i.e. dr/irscan & pathmove in events will
668 * not have JTAG operations injected into the middle of a sequence.
670 bool save_poll
= jtag_poll_get_enabled();
672 jtag_poll_set_enabled(false);
674 sprintf(buf
, "ocd_process_reset %s", n
->name
);
675 retval
= Jim_Eval(cmd
->ctx
->interp
, buf
);
677 jtag_poll_set_enabled(save_poll
);
679 if (retval
!= JIM_OK
) {
680 Jim_MakeErrorMessage(cmd
->ctx
->interp
);
681 command_print(cmd
, "%s", Jim_GetString(Jim_GetResult(cmd
->ctx
->interp
), NULL
));
685 /* We want any events to be processed before the prompt */
686 retval
= target_call_timer_callbacks_now();
688 for (target
= all_targets
; target
; target
= target
->next
) {
689 target
->type
->check_reset(target
);
690 target
->running_alg
= false;
696 static int identity_virt2phys(struct target
*target
,
697 target_addr_t
virtual, target_addr_t
*physical
)
703 static int no_mmu(struct target
*target
, int *enabled
)
709 static int default_examine(struct target
*target
)
711 target_set_examined(target
);
715 /* no check by default */
716 static int default_check_reset(struct target
*target
)
721 /* Equivalent Tcl code arp_examine_one is in src/target/startup.tcl
723 int target_examine_one(struct target
*target
)
725 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
727 int retval
= target
->type
->examine(target
);
728 if (retval
!= ERROR_OK
) {
729 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_FAIL
);
733 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
738 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
740 struct target
*target
= priv
;
742 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
745 jtag_unregister_event_callback(jtag_enable_callback
, target
);
747 return target_examine_one(target
);
750 /* Targets that correctly implement init + examine, i.e.
751 * no communication with target during init:
755 int target_examine(void)
757 int retval
= ERROR_OK
;
758 struct target
*target
;
760 for (target
= all_targets
; target
; target
= target
->next
) {
761 /* defer examination, but don't skip it */
762 if (!target
->tap
->enabled
) {
763 jtag_register_event_callback(jtag_enable_callback
,
768 if (target
->defer_examine
)
771 retval
= target_examine_one(target
);
772 if (retval
!= ERROR_OK
)
778 const char *target_type_name(struct target
*target
)
780 return target
->type
->name
;
783 static int target_soft_reset_halt(struct target
*target
)
785 if (!target_was_examined(target
)) {
786 LOG_ERROR("Target not examined yet");
789 if (!target
->type
->soft_reset_halt
) {
790 LOG_ERROR("Target %s does not support soft_reset_halt",
791 target_name(target
));
794 return target
->type
->soft_reset_halt(target
);
798 * Downloads a target-specific native code algorithm to the target,
799 * and executes it. * Note that some targets may need to set up, enable,
800 * and tear down a breakpoint (hard or * soft) to detect algorithm
801 * termination, while others may support lower overhead schemes where
802 * soft breakpoints embedded in the algorithm automatically terminate the
805 * @param target used to run the algorithm
806 * @param arch_info target-specific description of the algorithm.
808 int target_run_algorithm(struct target
*target
,
809 int num_mem_params
, struct mem_param
*mem_params
,
810 int num_reg_params
, struct reg_param
*reg_param
,
811 uint32_t entry_point
, uint32_t exit_point
,
812 int timeout_ms
, void *arch_info
)
814 int retval
= ERROR_FAIL
;
816 if (!target_was_examined(target
)) {
817 LOG_ERROR("Target not examined yet");
820 if (!target
->type
->run_algorithm
) {
821 LOG_ERROR("Target type '%s' does not support %s",
822 target_type_name(target
), __func__
);
826 target
->running_alg
= true;
827 retval
= target
->type
->run_algorithm(target
,
828 num_mem_params
, mem_params
,
829 num_reg_params
, reg_param
,
830 entry_point
, exit_point
, timeout_ms
, arch_info
);
831 target
->running_alg
= false;
838 * Executes a target-specific native code algorithm and leaves it running.
840 * @param target used to run the algorithm
841 * @param arch_info target-specific description of the algorithm.
843 int target_start_algorithm(struct target
*target
,
844 int num_mem_params
, struct mem_param
*mem_params
,
845 int num_reg_params
, struct reg_param
*reg_params
,
846 uint32_t entry_point
, uint32_t exit_point
,
849 int retval
= ERROR_FAIL
;
851 if (!target_was_examined(target
)) {
852 LOG_ERROR("Target not examined yet");
855 if (!target
->type
->start_algorithm
) {
856 LOG_ERROR("Target type '%s' does not support %s",
857 target_type_name(target
), __func__
);
860 if (target
->running_alg
) {
861 LOG_ERROR("Target is already running an algorithm");
865 target
->running_alg
= true;
866 retval
= target
->type
->start_algorithm(target
,
867 num_mem_params
, mem_params
,
868 num_reg_params
, reg_params
,
869 entry_point
, exit_point
, arch_info
);
876 * Waits for an algorithm started with target_start_algorithm() to complete.
878 * @param target used to run the algorithm
879 * @param arch_info target-specific description of the algorithm.
881 int target_wait_algorithm(struct target
*target
,
882 int num_mem_params
, struct mem_param
*mem_params
,
883 int num_reg_params
, struct reg_param
*reg_params
,
884 uint32_t exit_point
, int timeout_ms
,
887 int retval
= ERROR_FAIL
;
889 if (!target
->type
->wait_algorithm
) {
890 LOG_ERROR("Target type '%s' does not support %s",
891 target_type_name(target
), __func__
);
894 if (!target
->running_alg
) {
895 LOG_ERROR("Target is not running an algorithm");
899 retval
= target
->type
->wait_algorithm(target
,
900 num_mem_params
, mem_params
,
901 num_reg_params
, reg_params
,
902 exit_point
, timeout_ms
, arch_info
);
903 if (retval
!= ERROR_TARGET_TIMEOUT
)
904 target
->running_alg
= false;
911 * Streams data to a circular buffer on target intended for consumption by code
912 * running asynchronously on target.
914 * This is intended for applications where target-specific native code runs
915 * on the target, receives data from the circular buffer, does something with
916 * it (most likely writing it to a flash memory), and advances the circular
919 * This assumes that the helper algorithm has already been loaded to the target,
920 * but has not been started yet. Given memory and register parameters are passed
923 * The buffer is defined by (buffer_start, buffer_size) arguments and has the
926 * [buffer_start + 0, buffer_start + 4):
927 * Write Pointer address (aka head). Written and updated by this
928 * routine when new data is written to the circular buffer.
929 * [buffer_start + 4, buffer_start + 8):
930 * Read Pointer address (aka tail). Updated by code running on the
931 * target after it consumes data.
932 * [buffer_start + 8, buffer_start + buffer_size):
933 * Circular buffer contents.
935 * See contrib/loaders/flash/stm32f1x.S for an example.
937 * @param target used to run the algorithm
938 * @param buffer address on the host where data to be sent is located
939 * @param count number of blocks to send
940 * @param block_size size in bytes of each block
941 * @param num_mem_params count of memory-based params to pass to algorithm
942 * @param mem_params memory-based params to pass to algorithm
943 * @param num_reg_params count of register-based params to pass to algorithm
944 * @param reg_params memory-based params to pass to algorithm
945 * @param buffer_start address on the target of the circular buffer structure
946 * @param buffer_size size of the circular buffer structure
947 * @param entry_point address on the target to execute to start the algorithm
948 * @param exit_point address at which to set a breakpoint to catch the
949 * end of the algorithm; can be 0 if target triggers a breakpoint itself
952 int target_run_flash_async_algorithm(struct target
*target
,
953 const uint8_t *buffer
, uint32_t count
, int block_size
,
954 int num_mem_params
, struct mem_param
*mem_params
,
955 int num_reg_params
, struct reg_param
*reg_params
,
956 uint32_t buffer_start
, uint32_t buffer_size
,
957 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
962 const uint8_t *buffer_orig
= buffer
;
964 /* Set up working area. First word is write pointer, second word is read pointer,
965 * rest is fifo data area. */
966 uint32_t wp_addr
= buffer_start
;
967 uint32_t rp_addr
= buffer_start
+ 4;
968 uint32_t fifo_start_addr
= buffer_start
+ 8;
969 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
971 uint32_t wp
= fifo_start_addr
;
972 uint32_t rp
= fifo_start_addr
;
974 /* validate block_size is 2^n */
975 assert(!block_size
|| !(block_size
& (block_size
- 1)));
977 retval
= target_write_u32(target
, wp_addr
, wp
);
978 if (retval
!= ERROR_OK
)
980 retval
= target_write_u32(target
, rp_addr
, rp
);
981 if (retval
!= ERROR_OK
)
984 /* Start up algorithm on target and let it idle while writing the first chunk */
985 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
986 num_reg_params
, reg_params
,
991 if (retval
!= ERROR_OK
) {
992 LOG_ERROR("error starting target flash write algorithm");
998 retval
= target_read_u32(target
, rp_addr
, &rp
);
999 if (retval
!= ERROR_OK
) {
1000 LOG_ERROR("failed to get read pointer");
1004 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
1005 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
1008 LOG_ERROR("flash write algorithm aborted by target");
1009 retval
= ERROR_FLASH_OPERATION_FAILED
;
1013 if (((rp
- fifo_start_addr
) & (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
1014 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
1018 /* Count the number of bytes available in the fifo without
1019 * crossing the wrap around. Make sure to not fill it completely,
1020 * because that would make wp == rp and that's the empty condition. */
1021 uint32_t thisrun_bytes
;
1023 thisrun_bytes
= rp
- wp
- block_size
;
1024 else if (rp
> fifo_start_addr
)
1025 thisrun_bytes
= fifo_end_addr
- wp
;
1027 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
1029 if (thisrun_bytes
== 0) {
1030 /* Throttle polling a bit if transfer is (much) faster than flash
1031 * programming. The exact delay shouldn't matter as long as it's
1032 * less than buffer size / flash speed. This is very unlikely to
1033 * run when using high latency connections such as USB. */
1036 /* to stop an infinite loop on some targets check and increment a timeout
1037 * this issue was observed on a stellaris using the new ICDI interface */
1038 if (timeout
++ >= 500) {
1039 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1040 return ERROR_FLASH_OPERATION_FAILED
;
1045 /* reset our timeout */
1048 /* Limit to the amount of data we actually want to write */
1049 if (thisrun_bytes
> count
* block_size
)
1050 thisrun_bytes
= count
* block_size
;
1052 /* Write data to fifo */
1053 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
1054 if (retval
!= ERROR_OK
)
1057 /* Update counters and wrap write pointer */
1058 buffer
+= thisrun_bytes
;
1059 count
-= thisrun_bytes
/ block_size
;
1060 wp
+= thisrun_bytes
;
1061 if (wp
>= fifo_end_addr
)
1062 wp
= fifo_start_addr
;
1064 /* Store updated write pointer to target */
1065 retval
= target_write_u32(target
, wp_addr
, wp
);
1066 if (retval
!= ERROR_OK
)
1069 /* Avoid GDB timeouts */
1073 if (retval
!= ERROR_OK
) {
1074 /* abort flash write algorithm on target */
1075 target_write_u32(target
, wp_addr
, 0);
1078 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1079 num_reg_params
, reg_params
,
1084 if (retval2
!= ERROR_OK
) {
1085 LOG_ERROR("error waiting for target flash write algorithm");
1089 if (retval
== ERROR_OK
) {
1090 /* check if algorithm set rp = 0 after fifo writer loop finished */
1091 retval
= target_read_u32(target
, rp_addr
, &rp
);
1092 if (retval
== ERROR_OK
&& rp
== 0) {
1093 LOG_ERROR("flash write algorithm aborted by target");
1094 retval
= ERROR_FLASH_OPERATION_FAILED
;
1101 int target_read_memory(struct target
*target
,
1102 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1104 if (!target_was_examined(target
)) {
1105 LOG_ERROR("Target not examined yet");
1108 if (!target
->type
->read_memory
) {
1109 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1112 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1115 int target_read_phys_memory(struct target
*target
,
1116 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1118 if (!target_was_examined(target
)) {
1119 LOG_ERROR("Target not examined yet");
1122 if (!target
->type
->read_phys_memory
) {
1123 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1126 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1129 int target_write_memory(struct target
*target
,
1130 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1132 if (!target_was_examined(target
)) {
1133 LOG_ERROR("Target not examined yet");
1136 if (!target
->type
->write_memory
) {
1137 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1140 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1143 int target_write_phys_memory(struct target
*target
,
1144 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1146 if (!target_was_examined(target
)) {
1147 LOG_ERROR("Target not examined yet");
1150 if (!target
->type
->write_phys_memory
) {
1151 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1154 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1157 int target_add_breakpoint(struct target
*target
,
1158 struct breakpoint
*breakpoint
)
1160 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1161 LOG_WARNING("target %s is not halted (add breakpoint)", target_name(target
));
1162 return ERROR_TARGET_NOT_HALTED
;
1164 return target
->type
->add_breakpoint(target
, breakpoint
);
1167 int target_add_context_breakpoint(struct target
*target
,
1168 struct breakpoint
*breakpoint
)
1170 if (target
->state
!= TARGET_HALTED
) {
1171 LOG_WARNING("target %s is not halted (add context breakpoint)", target_name(target
));
1172 return ERROR_TARGET_NOT_HALTED
;
1174 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1177 int target_add_hybrid_breakpoint(struct target
*target
,
1178 struct breakpoint
*breakpoint
)
1180 if (target
->state
!= TARGET_HALTED
) {
1181 LOG_WARNING("target %s is not halted (add hybrid breakpoint)", target_name(target
));
1182 return ERROR_TARGET_NOT_HALTED
;
1184 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1187 int target_remove_breakpoint(struct target
*target
,
1188 struct breakpoint
*breakpoint
)
1190 return target
->type
->remove_breakpoint(target
, breakpoint
);
1193 int target_add_watchpoint(struct target
*target
,
1194 struct watchpoint
*watchpoint
)
1196 if (target
->state
!= TARGET_HALTED
) {
1197 LOG_WARNING("target %s is not halted (add watchpoint)", target_name(target
));
1198 return ERROR_TARGET_NOT_HALTED
;
1200 return target
->type
->add_watchpoint(target
, watchpoint
);
1202 int target_remove_watchpoint(struct target
*target
,
1203 struct watchpoint
*watchpoint
)
1205 return target
->type
->remove_watchpoint(target
, watchpoint
);
1207 int target_hit_watchpoint(struct target
*target
,
1208 struct watchpoint
**hit_watchpoint
)
1210 if (target
->state
!= TARGET_HALTED
) {
1211 LOG_WARNING("target %s is not halted (hit watchpoint)", target
->cmd_name
);
1212 return ERROR_TARGET_NOT_HALTED
;
1215 if (target
->type
->hit_watchpoint
== NULL
) {
1216 /* For backward compatible, if hit_watchpoint is not implemented,
1217 * return ERROR_FAIL such that gdb_server will not take the nonsense
1222 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1225 const char *target_get_gdb_arch(struct target
*target
)
1227 if (target
->type
->get_gdb_arch
== NULL
)
1229 return target
->type
->get_gdb_arch(target
);
1232 int target_get_gdb_reg_list(struct target
*target
,
1233 struct reg
**reg_list
[], int *reg_list_size
,
1234 enum target_register_class reg_class
)
1236 int result
= ERROR_FAIL
;
1238 if (!target_was_examined(target
)) {
1239 LOG_ERROR("Target not examined yet");
1243 result
= target
->type
->get_gdb_reg_list(target
, reg_list
,
1244 reg_list_size
, reg_class
);
1247 if (result
!= ERROR_OK
) {
1254 int target_get_gdb_reg_list_noread(struct target
*target
,
1255 struct reg
**reg_list
[], int *reg_list_size
,
1256 enum target_register_class reg_class
)
1258 if (target
->type
->get_gdb_reg_list_noread
&&
1259 target
->type
->get_gdb_reg_list_noread(target
, reg_list
,
1260 reg_list_size
, reg_class
) == ERROR_OK
)
1262 return target_get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1265 bool target_supports_gdb_connection(struct target
*target
)
1268 * based on current code, we can simply exclude all the targets that
1269 * don't provide get_gdb_reg_list; this could change with new targets.
1271 return !!target
->type
->get_gdb_reg_list
;
1274 int target_step(struct target
*target
,
1275 int current
, target_addr_t address
, int handle_breakpoints
)
1279 target_call_event_callbacks(target
, TARGET_EVENT_STEP_START
);
1281 retval
= target
->type
->step(target
, current
, address
, handle_breakpoints
);
1282 if (retval
!= ERROR_OK
)
1285 target_call_event_callbacks(target
, TARGET_EVENT_STEP_END
);
1290 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1292 if (target
->state
!= TARGET_HALTED
) {
1293 LOG_WARNING("target %s is not halted (gdb fileio)", target
->cmd_name
);
1294 return ERROR_TARGET_NOT_HALTED
;
1296 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1299 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1301 if (target
->state
!= TARGET_HALTED
) {
1302 LOG_WARNING("target %s is not halted (gdb fileio end)", target
->cmd_name
);
1303 return ERROR_TARGET_NOT_HALTED
;
1305 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1308 target_addr_t
target_address_max(struct target
*target
)
1310 unsigned bits
= target_address_bits(target
);
1311 if (sizeof(target_addr_t
) * 8 == bits
)
1312 return (target_addr_t
) -1;
1314 return (((target_addr_t
) 1) << bits
) - 1;
1317 unsigned target_address_bits(struct target
*target
)
1319 if (target
->type
->address_bits
)
1320 return target
->type
->address_bits(target
);
1324 int target_profiling(struct target
*target
, uint32_t *samples
,
1325 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1327 if (target
->state
!= TARGET_HALTED
) {
1328 LOG_WARNING("target %s is not halted (profiling)", target
->cmd_name
);
1329 return ERROR_TARGET_NOT_HALTED
;
1331 return target
->type
->profiling(target
, samples
, max_num_samples
,
1332 num_samples
, seconds
);
1336 * Reset the @c examined flag for the given target.
1337 * Pure paranoia -- targets are zeroed on allocation.
1339 static void target_reset_examined(struct target
*target
)
1341 target
->examined
= false;
1344 static int handle_target(void *priv
);
1346 static int target_init_one(struct command_context
*cmd_ctx
,
1347 struct target
*target
)
1349 target_reset_examined(target
);
1351 struct target_type
*type
= target
->type
;
1352 if (type
->examine
== NULL
)
1353 type
->examine
= default_examine
;
1355 if (type
->check_reset
== NULL
)
1356 type
->check_reset
= default_check_reset
;
1358 assert(type
->init_target
!= NULL
);
1360 int retval
= type
->init_target(cmd_ctx
, target
);
1361 if (ERROR_OK
!= retval
) {
1362 LOG_ERROR("target '%s' init failed", target_name(target
));
1366 /* Sanity-check MMU support ... stub in what we must, to help
1367 * implement it in stages, but warn if we need to do so.
1370 if (type
->virt2phys
== NULL
) {
1371 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1372 type
->virt2phys
= identity_virt2phys
;
1375 /* Make sure no-MMU targets all behave the same: make no
1376 * distinction between physical and virtual addresses, and
1377 * ensure that virt2phys() is always an identity mapping.
1379 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1380 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1383 type
->write_phys_memory
= type
->write_memory
;
1384 type
->read_phys_memory
= type
->read_memory
;
1385 type
->virt2phys
= identity_virt2phys
;
1388 if (target
->type
->read_buffer
== NULL
)
1389 target
->type
->read_buffer
= target_read_buffer_default
;
1391 if (target
->type
->write_buffer
== NULL
)
1392 target
->type
->write_buffer
= target_write_buffer_default
;
1394 if (target
->type
->get_gdb_fileio_info
== NULL
)
1395 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1397 if (target
->type
->gdb_fileio_end
== NULL
)
1398 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1400 if (target
->type
->profiling
== NULL
)
1401 target
->type
->profiling
= target_profiling_default
;
1406 static int target_init(struct command_context
*cmd_ctx
)
1408 struct target
*target
;
1411 for (target
= all_targets
; target
; target
= target
->next
) {
1412 retval
= target_init_one(cmd_ctx
, target
);
1413 if (ERROR_OK
!= retval
)
1420 retval
= target_register_user_commands(cmd_ctx
);
1421 if (ERROR_OK
!= retval
)
1424 retval
= target_register_timer_callback(&handle_target
,
1425 polling_interval
, TARGET_TIMER_TYPE_PERIODIC
, cmd_ctx
->interp
);
1426 if (ERROR_OK
!= retval
)
1432 COMMAND_HANDLER(handle_target_init_command
)
1437 return ERROR_COMMAND_SYNTAX_ERROR
;
1439 static bool target_initialized
;
1440 if (target_initialized
) {
1441 LOG_INFO("'target init' has already been called");
1444 target_initialized
= true;
1446 retval
= command_run_line(CMD_CTX
, "init_targets");
1447 if (ERROR_OK
!= retval
)
1450 retval
= command_run_line(CMD_CTX
, "init_target_events");
1451 if (ERROR_OK
!= retval
)
1454 retval
= command_run_line(CMD_CTX
, "init_board");
1455 if (ERROR_OK
!= retval
)
1458 LOG_DEBUG("Initializing targets...");
1459 return target_init(CMD_CTX
);
1462 int target_register_event_callback(int (*callback
)(struct target
*target
,
1463 enum target_event event
, void *priv
), void *priv
)
1465 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1467 if (callback
== NULL
)
1468 return ERROR_COMMAND_SYNTAX_ERROR
;
1471 while ((*callbacks_p
)->next
)
1472 callbacks_p
= &((*callbacks_p
)->next
);
1473 callbacks_p
= &((*callbacks_p
)->next
);
1476 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1477 (*callbacks_p
)->callback
= callback
;
1478 (*callbacks_p
)->priv
= priv
;
1479 (*callbacks_p
)->next
= NULL
;
1484 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1485 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1487 struct target_reset_callback
*entry
;
1489 if (callback
== NULL
)
1490 return ERROR_COMMAND_SYNTAX_ERROR
;
1492 entry
= malloc(sizeof(struct target_reset_callback
));
1493 if (entry
== NULL
) {
1494 LOG_ERROR("error allocating buffer for reset callback entry");
1495 return ERROR_COMMAND_SYNTAX_ERROR
;
1498 entry
->callback
= callback
;
1500 list_add(&entry
->list
, &target_reset_callback_list
);
1506 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1507 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1509 struct target_trace_callback
*entry
;
1511 if (callback
== NULL
)
1512 return ERROR_COMMAND_SYNTAX_ERROR
;
1514 entry
= malloc(sizeof(struct target_trace_callback
));
1515 if (entry
== NULL
) {
1516 LOG_ERROR("error allocating buffer for trace callback entry");
1517 return ERROR_COMMAND_SYNTAX_ERROR
;
1520 entry
->callback
= callback
;
1522 list_add(&entry
->list
, &target_trace_callback_list
);
1528 int target_register_timer_callback(int (*callback
)(void *priv
),
1529 unsigned int time_ms
, enum target_timer_type type
, void *priv
)
1531 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1533 if (callback
== NULL
)
1534 return ERROR_COMMAND_SYNTAX_ERROR
;
1537 while ((*callbacks_p
)->next
)
1538 callbacks_p
= &((*callbacks_p
)->next
);
1539 callbacks_p
= &((*callbacks_p
)->next
);
1542 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1543 (*callbacks_p
)->callback
= callback
;
1544 (*callbacks_p
)->type
= type
;
1545 (*callbacks_p
)->time_ms
= time_ms
;
1546 (*callbacks_p
)->removed
= false;
1548 gettimeofday(&(*callbacks_p
)->when
, NULL
);
1549 timeval_add_time(&(*callbacks_p
)->when
, 0, time_ms
* 1000);
1551 (*callbacks_p
)->priv
= priv
;
1552 (*callbacks_p
)->next
= NULL
;
1557 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1558 enum target_event event
, void *priv
), void *priv
)
1560 struct target_event_callback
**p
= &target_event_callbacks
;
1561 struct target_event_callback
*c
= target_event_callbacks
;
1563 if (callback
== NULL
)
1564 return ERROR_COMMAND_SYNTAX_ERROR
;
1567 struct target_event_callback
*next
= c
->next
;
1568 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1580 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1581 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1583 struct target_reset_callback
*entry
;
1585 if (callback
== NULL
)
1586 return ERROR_COMMAND_SYNTAX_ERROR
;
1588 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1589 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1590 list_del(&entry
->list
);
1599 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1600 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1602 struct target_trace_callback
*entry
;
1604 if (callback
== NULL
)
1605 return ERROR_COMMAND_SYNTAX_ERROR
;
1607 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1608 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1609 list_del(&entry
->list
);
1618 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1620 if (callback
== NULL
)
1621 return ERROR_COMMAND_SYNTAX_ERROR
;
1623 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1625 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1634 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1636 struct target_event_callback
*callback
= target_event_callbacks
;
1637 struct target_event_callback
*next_callback
;
1639 if (event
== TARGET_EVENT_HALTED
) {
1640 /* execute early halted first */
1641 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1644 LOG_DEBUG("target event %i (%s) for core %s", event
,
1645 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
,
1646 target_name(target
));
1648 target_handle_event(target
, event
);
1651 next_callback
= callback
->next
;
1652 callback
->callback(target
, event
, callback
->priv
);
1653 callback
= next_callback
;
1659 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1661 struct target_reset_callback
*callback
;
1663 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1664 Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1666 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1667 callback
->callback(target
, reset_mode
, callback
->priv
);
1672 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1674 struct target_trace_callback
*callback
;
1676 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1677 callback
->callback(target
, len
, data
, callback
->priv
);
1682 static int target_timer_callback_periodic_restart(
1683 struct target_timer_callback
*cb
, struct timeval
*now
)
1686 timeval_add_time(&cb
->when
, 0, cb
->time_ms
* 1000L);
1690 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1691 struct timeval
*now
)
1693 cb
->callback(cb
->priv
);
1695 if (cb
->type
== TARGET_TIMER_TYPE_PERIODIC
)
1696 return target_timer_callback_periodic_restart(cb
, now
);
1698 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1701 static int target_call_timer_callbacks_check_time(int checktime
)
1703 static bool callback_processing
;
1705 /* Do not allow nesting */
1706 if (callback_processing
)
1709 callback_processing
= true;
1714 gettimeofday(&now
, NULL
);
1716 /* Store an address of the place containing a pointer to the
1717 * next item; initially, that's a standalone "root of the
1718 * list" variable. */
1719 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1720 while (callback
&& *callback
) {
1721 if ((*callback
)->removed
) {
1722 struct target_timer_callback
*p
= *callback
;
1723 *callback
= (*callback
)->next
;
1728 bool call_it
= (*callback
)->callback
&&
1729 ((!checktime
&& (*callback
)->type
== TARGET_TIMER_TYPE_PERIODIC
) ||
1730 timeval_compare(&now
, &(*callback
)->when
) >= 0);
1733 target_call_timer_callback(*callback
, &now
);
1735 callback
= &(*callback
)->next
;
1738 callback_processing
= false;
1742 int target_call_timer_callbacks(void)
1744 return target_call_timer_callbacks_check_time(1);
1747 /* invoke periodic callbacks immediately */
1748 int target_call_timer_callbacks_now(void)
1750 return target_call_timer_callbacks_check_time(0);
1753 /* Prints the working area layout for debug purposes */
1754 static void print_wa_layout(struct target
*target
)
1756 struct working_area
*c
= target
->working_areas
;
1759 LOG_DEBUG("%c%c " TARGET_ADDR_FMT
"-" TARGET_ADDR_FMT
" (%" PRIu32
" bytes)",
1760 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1761 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1766 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1767 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1769 assert(area
->free
); /* Shouldn't split an allocated area */
1770 assert(size
<= area
->size
); /* Caller should guarantee this */
1772 /* Split only if not already the right size */
1773 if (size
< area
->size
) {
1774 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1779 new_wa
->next
= area
->next
;
1780 new_wa
->size
= area
->size
- size
;
1781 new_wa
->address
= area
->address
+ size
;
1782 new_wa
->backup
= NULL
;
1783 new_wa
->user
= NULL
;
1784 new_wa
->free
= true;
1786 area
->next
= new_wa
;
1789 /* If backup memory was allocated to this area, it has the wrong size
1790 * now so free it and it will be reallocated if/when needed */
1792 area
->backup
= NULL
;
1796 /* Merge all adjacent free areas into one */
1797 static void target_merge_working_areas(struct target
*target
)
1799 struct working_area
*c
= target
->working_areas
;
1801 while (c
&& c
->next
) {
1802 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1804 /* Find two adjacent free areas */
1805 if (c
->free
&& c
->next
->free
) {
1806 /* Merge the last into the first */
1807 c
->size
+= c
->next
->size
;
1809 /* Remove the last */
1810 struct working_area
*to_be_freed
= c
->next
;
1811 c
->next
= c
->next
->next
;
1812 free(to_be_freed
->backup
);
1815 /* If backup memory was allocated to the remaining area, it's has
1816 * the wrong size now */
1825 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1827 /* Reevaluate working area address based on MMU state*/
1828 if (target
->working_areas
== NULL
) {
1832 retval
= target
->type
->mmu(target
, &enabled
);
1833 if (retval
!= ERROR_OK
)
1837 if (target
->working_area_phys_spec
) {
1838 LOG_DEBUG("MMU disabled, using physical "
1839 "address for working memory " TARGET_ADDR_FMT
,
1840 target
->working_area_phys
);
1841 target
->working_area
= target
->working_area_phys
;
1843 LOG_ERROR("No working memory available. "
1844 "Specify -work-area-phys to target.");
1845 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1848 if (target
->working_area_virt_spec
) {
1849 LOG_DEBUG("MMU enabled, using virtual "
1850 "address for working memory " TARGET_ADDR_FMT
,
1851 target
->working_area_virt
);
1852 target
->working_area
= target
->working_area_virt
;
1854 LOG_ERROR("No working memory available. "
1855 "Specify -work-area-virt to target.");
1856 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1860 /* Set up initial working area on first call */
1861 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1863 new_wa
->next
= NULL
;
1864 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1865 new_wa
->address
= target
->working_area
;
1866 new_wa
->backup
= NULL
;
1867 new_wa
->user
= NULL
;
1868 new_wa
->free
= true;
1871 target
->working_areas
= new_wa
;
1874 /* only allocate multiples of 4 byte */
1876 size
= (size
+ 3) & (~3UL);
1878 struct working_area
*c
= target
->working_areas
;
1880 /* Find the first large enough working area */
1882 if (c
->free
&& c
->size
>= size
)
1888 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1890 /* Split the working area into the requested size */
1891 target_split_working_area(c
, size
);
1893 LOG_DEBUG("allocated new working area of %" PRIu32
" bytes at address " TARGET_ADDR_FMT
,
1896 if (target
->backup_working_area
) {
1897 if (c
->backup
== NULL
) {
1898 c
->backup
= malloc(c
->size
);
1899 if (c
->backup
== NULL
)
1903 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1904 if (retval
!= ERROR_OK
)
1908 /* mark as used, and return the new (reused) area */
1915 print_wa_layout(target
);
1920 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1924 retval
= target_alloc_working_area_try(target
, size
, area
);
1925 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1926 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1931 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1933 int retval
= ERROR_OK
;
1935 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1936 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1937 if (retval
!= ERROR_OK
)
1938 LOG_ERROR("failed to restore %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1939 area
->size
, area
->address
);
1945 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1946 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1948 int retval
= ERROR_OK
;
1954 retval
= target_restore_working_area(target
, area
);
1955 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1956 if (retval
!= ERROR_OK
)
1962 LOG_DEBUG("freed %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1963 area
->size
, area
->address
);
1965 /* mark user pointer invalid */
1966 /* TODO: Is this really safe? It points to some previous caller's memory.
1967 * How could we know that the area pointer is still in that place and not
1968 * some other vital data? What's the purpose of this, anyway? */
1972 target_merge_working_areas(target
);
1974 print_wa_layout(target
);
1979 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1981 return target_free_working_area_restore(target
, area
, 1);
1984 /* free resources and restore memory, if restoring memory fails,
1985 * free up resources anyway
1987 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1989 struct working_area
*c
= target
->working_areas
;
1991 LOG_DEBUG("freeing all working areas");
1993 /* Loop through all areas, restoring the allocated ones and marking them as free */
1997 target_restore_working_area(target
, c
);
1999 *c
->user
= NULL
; /* Same as above */
2005 /* Run a merge pass to combine all areas into one */
2006 target_merge_working_areas(target
);
2008 print_wa_layout(target
);
2011 void target_free_all_working_areas(struct target
*target
)
2013 target_free_all_working_areas_restore(target
, 1);
2015 /* Now we have none or only one working area marked as free */
2016 if (target
->working_areas
) {
2017 /* Free the last one to allow on-the-fly moving and resizing */
2018 free(target
->working_areas
->backup
);
2019 free(target
->working_areas
);
2020 target
->working_areas
= NULL
;
2024 /* Find the largest number of bytes that can be allocated */
2025 uint32_t target_get_working_area_avail(struct target
*target
)
2027 struct working_area
*c
= target
->working_areas
;
2028 uint32_t max_size
= 0;
2031 return target
->working_area_size
;
2034 if (c
->free
&& max_size
< c
->size
)
2043 static void target_destroy(struct target
*target
)
2045 if (target
->type
->deinit_target
)
2046 target
->type
->deinit_target(target
);
2048 free(target
->semihosting
);
2050 jtag_unregister_event_callback(jtag_enable_callback
, target
);
2052 struct target_event_action
*teap
= target
->event_action
;
2054 struct target_event_action
*next
= teap
->next
;
2055 Jim_DecrRefCount(teap
->interp
, teap
->body
);
2060 target_free_all_working_areas(target
);
2062 /* release the targets SMP list */
2064 struct target_list
*head
= target
->head
;
2065 while (head
!= NULL
) {
2066 struct target_list
*pos
= head
->next
;
2067 head
->target
->smp
= 0;
2074 rtos_destroy(target
);
2076 free(target
->gdb_port_override
);
2078 free(target
->trace_info
);
2079 free(target
->fileio_info
);
2080 free(target
->cmd_name
);
2084 void target_quit(void)
2086 struct target_event_callback
*pe
= target_event_callbacks
;
2088 struct target_event_callback
*t
= pe
->next
;
2092 target_event_callbacks
= NULL
;
2094 struct target_timer_callback
*pt
= target_timer_callbacks
;
2096 struct target_timer_callback
*t
= pt
->next
;
2100 target_timer_callbacks
= NULL
;
2102 for (struct target
*target
= all_targets
; target
;) {
2106 target_destroy(target
);
2113 int target_arch_state(struct target
*target
)
2116 if (target
== NULL
) {
2117 LOG_WARNING("No target has been configured");
2121 if (target
->state
!= TARGET_HALTED
)
2124 retval
= target
->type
->arch_state(target
);
2128 static int target_get_gdb_fileio_info_default(struct target
*target
,
2129 struct gdb_fileio_info
*fileio_info
)
2131 /* If target does not support semi-hosting function, target
2132 has no need to provide .get_gdb_fileio_info callback.
2133 It just return ERROR_FAIL and gdb_server will return "Txx"
2134 as target halted every time. */
2138 static int target_gdb_fileio_end_default(struct target
*target
,
2139 int retcode
, int fileio_errno
, bool ctrl_c
)
2144 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
2145 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
2147 struct timeval timeout
, now
;
2149 gettimeofday(&timeout
, NULL
);
2150 timeval_add_time(&timeout
, seconds
, 0);
2152 LOG_INFO("Starting profiling. Halting and resuming the"
2153 " target as often as we can...");
2155 uint32_t sample_count
= 0;
2156 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2157 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2159 int retval
= ERROR_OK
;
2161 target_poll(target
);
2162 if (target
->state
== TARGET_HALTED
) {
2163 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
2164 samples
[sample_count
++] = t
;
2165 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2166 retval
= target_resume(target
, 1, 0, 0, 0);
2167 target_poll(target
);
2168 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2169 } else if (target
->state
== TARGET_RUNNING
) {
2170 /* We want to quickly sample the PC. */
2171 retval
= target_halt(target
);
2173 LOG_INFO("Target not halted or running");
2178 if (retval
!= ERROR_OK
)
2181 gettimeofday(&now
, NULL
);
2182 if ((sample_count
>= max_num_samples
) || timeval_compare(&now
, &timeout
) >= 0) {
2183 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2188 *num_samples
= sample_count
;
2192 /* Single aligned words are guaranteed to use 16 or 32 bit access
2193 * mode respectively, otherwise data is handled as quickly as
2196 int target_write_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, const uint8_t *buffer
)
2198 LOG_DEBUG("writing buffer of %" PRIu32
" byte at " TARGET_ADDR_FMT
,
2201 if (!target_was_examined(target
)) {
2202 LOG_ERROR("Target not examined yet");
2209 if ((address
+ size
- 1) < address
) {
2210 /* GDB can request this when e.g. PC is 0xfffffffc */
2211 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2217 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2220 static int target_write_buffer_default(struct target
*target
,
2221 target_addr_t address
, uint32_t count
, const uint8_t *buffer
)
2225 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2226 * will have something to do with the size we leave to it. */
2227 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2228 if (address
& size
) {
2229 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2230 if (retval
!= ERROR_OK
)
2238 /* Write the data with as large access size as possible. */
2239 for (; size
> 0; size
/= 2) {
2240 uint32_t aligned
= count
- count
% size
;
2242 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2243 if (retval
!= ERROR_OK
)
2254 /* Single aligned words are guaranteed to use 16 or 32 bit access
2255 * mode respectively, otherwise data is handled as quickly as
2258 int target_read_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
2260 LOG_DEBUG("reading buffer of %" PRIu32
" byte at " TARGET_ADDR_FMT
,
2263 if (!target_was_examined(target
)) {
2264 LOG_ERROR("Target not examined yet");
2271 if ((address
+ size
- 1) < address
) {
2272 /* GDB can request this when e.g. PC is 0xfffffffc */
2273 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2279 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2282 static int target_read_buffer_default(struct target
*target
, target_addr_t address
, uint32_t count
, uint8_t *buffer
)
2286 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2287 * will have something to do with the size we leave to it. */
2288 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2289 if (address
& size
) {
2290 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2291 if (retval
!= ERROR_OK
)
2299 /* Read the data with as large access size as possible. */
2300 for (; size
> 0; size
/= 2) {
2301 uint32_t aligned
= count
- count
% size
;
2303 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2304 if (retval
!= ERROR_OK
)
2315 int target_checksum_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t *crc
)
2320 uint32_t checksum
= 0;
2321 if (!target_was_examined(target
)) {
2322 LOG_ERROR("Target not examined yet");
2326 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2327 if (retval
!= ERROR_OK
) {
2328 buffer
= malloc(size
);
2329 if (buffer
== NULL
) {
2330 LOG_ERROR("error allocating buffer for section (%" PRIu32
" bytes)", size
);
2331 return ERROR_COMMAND_SYNTAX_ERROR
;
2333 retval
= target_read_buffer(target
, address
, size
, buffer
);
2334 if (retval
!= ERROR_OK
) {
2339 /* convert to target endianness */
2340 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2341 uint32_t target_data
;
2342 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2343 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2346 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2355 int target_blank_check_memory(struct target
*target
,
2356 struct target_memory_check_block
*blocks
, int num_blocks
,
2357 uint8_t erased_value
)
2359 if (!target_was_examined(target
)) {
2360 LOG_ERROR("Target not examined yet");
2364 if (target
->type
->blank_check_memory
== NULL
)
2365 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2367 return target
->type
->blank_check_memory(target
, blocks
, num_blocks
, erased_value
);
2370 int target_read_u64(struct target
*target
, target_addr_t address
, uint64_t *value
)
2372 uint8_t value_buf
[8];
2373 if (!target_was_examined(target
)) {
2374 LOG_ERROR("Target not examined yet");
2378 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2380 if (retval
== ERROR_OK
) {
2381 *value
= target_buffer_get_u64(target
, value_buf
);
2382 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2387 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2394 int target_read_u32(struct target
*target
, target_addr_t address
, uint32_t *value
)
2396 uint8_t value_buf
[4];
2397 if (!target_was_examined(target
)) {
2398 LOG_ERROR("Target not examined yet");
2402 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2404 if (retval
== ERROR_OK
) {
2405 *value
= target_buffer_get_u32(target
, value_buf
);
2406 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2411 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2418 int target_read_u16(struct target
*target
, target_addr_t address
, uint16_t *value
)
2420 uint8_t value_buf
[2];
2421 if (!target_was_examined(target
)) {
2422 LOG_ERROR("Target not examined yet");
2426 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2428 if (retval
== ERROR_OK
) {
2429 *value
= target_buffer_get_u16(target
, value_buf
);
2430 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%4.4" PRIx16
,
2435 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2442 int target_read_u8(struct target
*target
, target_addr_t address
, uint8_t *value
)
2444 if (!target_was_examined(target
)) {
2445 LOG_ERROR("Target not examined yet");
2449 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2451 if (retval
== ERROR_OK
) {
2452 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2457 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2464 int target_write_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2467 uint8_t value_buf
[8];
2468 if (!target_was_examined(target
)) {
2469 LOG_ERROR("Target not examined yet");
2473 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2477 target_buffer_set_u64(target
, value_buf
, value
);
2478 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2479 if (retval
!= ERROR_OK
)
2480 LOG_DEBUG("failed: %i", retval
);
2485 int target_write_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2488 uint8_t value_buf
[4];
2489 if (!target_was_examined(target
)) {
2490 LOG_ERROR("Target not examined yet");
2494 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2498 target_buffer_set_u32(target
, value_buf
, value
);
2499 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2500 if (retval
!= ERROR_OK
)
2501 LOG_DEBUG("failed: %i", retval
);
2506 int target_write_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2509 uint8_t value_buf
[2];
2510 if (!target_was_examined(target
)) {
2511 LOG_ERROR("Target not examined yet");
2515 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2519 target_buffer_set_u16(target
, value_buf
, value
);
2520 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2521 if (retval
!= ERROR_OK
)
2522 LOG_DEBUG("failed: %i", retval
);
2527 int target_write_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2530 if (!target_was_examined(target
)) {
2531 LOG_ERROR("Target not examined yet");
2535 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2538 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2539 if (retval
!= ERROR_OK
)
2540 LOG_DEBUG("failed: %i", retval
);
2545 int target_write_phys_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2548 uint8_t value_buf
[8];
2549 if (!target_was_examined(target
)) {
2550 LOG_ERROR("Target not examined yet");
2554 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2558 target_buffer_set_u64(target
, value_buf
, value
);
2559 retval
= target_write_phys_memory(target
, address
, 8, 1, value_buf
);
2560 if (retval
!= ERROR_OK
)
2561 LOG_DEBUG("failed: %i", retval
);
2566 int target_write_phys_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2569 uint8_t value_buf
[4];
2570 if (!target_was_examined(target
)) {
2571 LOG_ERROR("Target not examined yet");
2575 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2579 target_buffer_set_u32(target
, value_buf
, value
);
2580 retval
= target_write_phys_memory(target
, address
, 4, 1, value_buf
);
2581 if (retval
!= ERROR_OK
)
2582 LOG_DEBUG("failed: %i", retval
);
2587 int target_write_phys_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2590 uint8_t value_buf
[2];
2591 if (!target_was_examined(target
)) {
2592 LOG_ERROR("Target not examined yet");
2596 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2600 target_buffer_set_u16(target
, value_buf
, value
);
2601 retval
= target_write_phys_memory(target
, address
, 2, 1, value_buf
);
2602 if (retval
!= ERROR_OK
)
2603 LOG_DEBUG("failed: %i", retval
);
2608 int target_write_phys_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2611 if (!target_was_examined(target
)) {
2612 LOG_ERROR("Target not examined yet");
2616 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2619 retval
= target_write_phys_memory(target
, address
, 1, 1, &value
);
2620 if (retval
!= ERROR_OK
)
2621 LOG_DEBUG("failed: %i", retval
);
2626 static int find_target(struct command_invocation
*cmd
, const char *name
)
2628 struct target
*target
= get_target(name
);
2629 if (target
== NULL
) {
2630 command_print(cmd
, "Target: %s is unknown, try one of:\n", name
);
2633 if (!target
->tap
->enabled
) {
2634 command_print(cmd
, "Target: TAP %s is disabled, "
2635 "can't be the current target\n",
2636 target
->tap
->dotted_name
);
2640 cmd
->ctx
->current_target
= target
;
2641 if (cmd
->ctx
->current_target_override
)
2642 cmd
->ctx
->current_target_override
= target
;
2648 COMMAND_HANDLER(handle_targets_command
)
2650 int retval
= ERROR_OK
;
2651 if (CMD_ARGC
== 1) {
2652 retval
= find_target(CMD
, CMD_ARGV
[0]);
2653 if (retval
== ERROR_OK
) {
2659 struct target
*target
= all_targets
;
2660 command_print(CMD
, " TargetName Type Endian TapName State ");
2661 command_print(CMD
, "-- ------------------ ---------- ------ ------------------ ------------");
2666 if (target
->tap
->enabled
)
2667 state
= target_state_name(target
);
2669 state
= "tap-disabled";
2671 if (CMD_CTX
->current_target
== target
)
2674 /* keep columns lined up to match the headers above */
2676 "%2d%c %-18s %-10s %-6s %-18s %s",
2677 target
->target_number
,
2679 target_name(target
),
2680 target_type_name(target
),
2681 Jim_Nvp_value2name_simple(nvp_target_endian
,
2682 target
->endianness
)->name
,
2683 target
->tap
->dotted_name
,
2685 target
= target
->next
;
2691 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2693 static int powerDropout
;
2694 static int srstAsserted
;
2696 static int runPowerRestore
;
2697 static int runPowerDropout
;
2698 static int runSrstAsserted
;
2699 static int runSrstDeasserted
;
2701 static int sense_handler(void)
2703 static int prevSrstAsserted
;
2704 static int prevPowerdropout
;
2706 int retval
= jtag_power_dropout(&powerDropout
);
2707 if (retval
!= ERROR_OK
)
2711 powerRestored
= prevPowerdropout
&& !powerDropout
;
2713 runPowerRestore
= 1;
2715 int64_t current
= timeval_ms();
2716 static int64_t lastPower
;
2717 bool waitMore
= lastPower
+ 2000 > current
;
2718 if (powerDropout
&& !waitMore
) {
2719 runPowerDropout
= 1;
2720 lastPower
= current
;
2723 retval
= jtag_srst_asserted(&srstAsserted
);
2724 if (retval
!= ERROR_OK
)
2728 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2730 static int64_t lastSrst
;
2731 waitMore
= lastSrst
+ 2000 > current
;
2732 if (srstDeasserted
&& !waitMore
) {
2733 runSrstDeasserted
= 1;
2737 if (!prevSrstAsserted
&& srstAsserted
)
2738 runSrstAsserted
= 1;
2740 prevSrstAsserted
= srstAsserted
;
2741 prevPowerdropout
= powerDropout
;
2743 if (srstDeasserted
|| powerRestored
) {
2744 /* Other than logging the event we can't do anything here.
2745 * Issuing a reset is a particularly bad idea as we might
2746 * be inside a reset already.
2753 /* process target state changes */
2754 static int handle_target(void *priv
)
2756 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2757 int retval
= ERROR_OK
;
2759 if (!is_jtag_poll_safe()) {
2760 /* polling is disabled currently */
2764 /* we do not want to recurse here... */
2765 static int recursive
;
2769 /* danger! running these procedures can trigger srst assertions and power dropouts.
2770 * We need to avoid an infinite loop/recursion here and we do that by
2771 * clearing the flags after running these events.
2773 int did_something
= 0;
2774 if (runSrstAsserted
) {
2775 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2776 Jim_Eval(interp
, "srst_asserted");
2779 if (runSrstDeasserted
) {
2780 Jim_Eval(interp
, "srst_deasserted");
2783 if (runPowerDropout
) {
2784 LOG_INFO("Power dropout detected, running power_dropout proc.");
2785 Jim_Eval(interp
, "power_dropout");
2788 if (runPowerRestore
) {
2789 Jim_Eval(interp
, "power_restore");
2793 if (did_something
) {
2794 /* clear detect flags */
2798 /* clear action flags */
2800 runSrstAsserted
= 0;
2801 runSrstDeasserted
= 0;
2802 runPowerRestore
= 0;
2803 runPowerDropout
= 0;
2808 /* Poll targets for state changes unless that's globally disabled.
2809 * Skip targets that are currently disabled.
2811 for (struct target
*target
= all_targets
;
2812 is_jtag_poll_safe() && target
;
2813 target
= target
->next
) {
2815 if (!target_was_examined(target
))
2818 if (!target
->tap
->enabled
)
2821 if (target
->backoff
.times
> target
->backoff
.count
) {
2822 /* do not poll this time as we failed previously */
2823 target
->backoff
.count
++;
2826 target
->backoff
.count
= 0;
2828 /* only poll target if we've got power and srst isn't asserted */
2829 if (!powerDropout
&& !srstAsserted
) {
2830 /* polling may fail silently until the target has been examined */
2831 retval
= target_poll(target
);
2832 if (retval
!= ERROR_OK
) {
2833 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2834 if (target
->backoff
.times
* polling_interval
< 5000) {
2835 target
->backoff
.times
*= 2;
2836 target
->backoff
.times
++;
2839 /* Tell GDB to halt the debugger. This allows the user to
2840 * run monitor commands to handle the situation.
2842 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2844 if (target
->backoff
.times
> 0) {
2845 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
2846 target_reset_examined(target
);
2847 retval
= target_examine_one(target
);
2848 /* Target examination could have failed due to unstable connection,
2849 * but we set the examined flag anyway to repoll it later */
2850 if (retval
!= ERROR_OK
) {
2851 target
->examined
= true;
2852 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2853 target
->backoff
.times
* polling_interval
);
2858 /* Since we succeeded, we reset backoff count */
2859 target
->backoff
.times
= 0;
2866 COMMAND_HANDLER(handle_reg_command
)
2868 struct target
*target
;
2869 struct reg
*reg
= NULL
;
2875 target
= get_current_target(CMD_CTX
);
2877 /* list all available registers for the current target */
2878 if (CMD_ARGC
== 0) {
2879 struct reg_cache
*cache
= target
->reg_cache
;
2885 command_print(CMD
, "===== %s", cache
->name
);
2887 for (i
= 0, reg
= cache
->reg_list
;
2888 i
< cache
->num_regs
;
2889 i
++, reg
++, count
++) {
2890 if (reg
->exist
== false)
2892 /* only print cached values if they are valid */
2894 value
= buf_to_hex_str(reg
->value
,
2897 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2905 command_print(CMD
, "(%i) %s (/%" PRIu32
")",
2910 cache
= cache
->next
;
2916 /* access a single register by its ordinal number */
2917 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2919 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2921 struct reg_cache
*cache
= target
->reg_cache
;
2925 for (i
= 0; i
< cache
->num_regs
; i
++) {
2926 if (count
++ == num
) {
2927 reg
= &cache
->reg_list
[i
];
2933 cache
= cache
->next
;
2937 command_print(CMD
, "%i is out of bounds, the current target "
2938 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2942 /* access a single register by its name */
2943 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2949 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2954 /* display a register */
2955 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2956 && (CMD_ARGV
[1][0] <= '9')))) {
2957 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2960 if (reg
->valid
== 0)
2961 reg
->type
->get(reg
);
2962 value
= buf_to_hex_str(reg
->value
, reg
->size
);
2963 command_print(CMD
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2968 /* set register value */
2969 if (CMD_ARGC
== 2) {
2970 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2973 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2975 reg
->type
->set(reg
, buf
);
2977 value
= buf_to_hex_str(reg
->value
, reg
->size
);
2978 command_print(CMD
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2986 return ERROR_COMMAND_SYNTAX_ERROR
;
2989 command_print(CMD
, "register %s not found in current target", CMD_ARGV
[0]);
2993 COMMAND_HANDLER(handle_poll_command
)
2995 int retval
= ERROR_OK
;
2996 struct target
*target
= get_current_target(CMD_CTX
);
2998 if (CMD_ARGC
== 0) {
2999 command_print(CMD
, "background polling: %s",
3000 jtag_poll_get_enabled() ? "on" : "off");
3001 command_print(CMD
, "TAP: %s (%s)",
3002 target
->tap
->dotted_name
,
3003 target
->tap
->enabled
? "enabled" : "disabled");
3004 if (!target
->tap
->enabled
)
3006 retval
= target_poll(target
);
3007 if (retval
!= ERROR_OK
)
3009 retval
= target_arch_state(target
);
3010 if (retval
!= ERROR_OK
)
3012 } else if (CMD_ARGC
== 1) {
3014 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
3015 jtag_poll_set_enabled(enable
);
3017 return ERROR_COMMAND_SYNTAX_ERROR
;
3022 COMMAND_HANDLER(handle_wait_halt_command
)
3025 return ERROR_COMMAND_SYNTAX_ERROR
;
3027 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
3028 if (1 == CMD_ARGC
) {
3029 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
3030 if (ERROR_OK
!= retval
)
3031 return ERROR_COMMAND_SYNTAX_ERROR
;
3034 struct target
*target
= get_current_target(CMD_CTX
);
3035 return target_wait_state(target
, TARGET_HALTED
, ms
);
3038 /* wait for target state to change. The trick here is to have a low
3039 * latency for short waits and not to suck up all the CPU time
3042 * After 500ms, keep_alive() is invoked
3044 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
3047 int64_t then
= 0, cur
;
3051 retval
= target_poll(target
);
3052 if (retval
!= ERROR_OK
)
3054 if (target
->state
== state
)
3059 then
= timeval_ms();
3060 LOG_DEBUG("waiting for target %s...",
3061 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
3067 if ((cur
-then
) > ms
) {
3068 LOG_ERROR("timed out while waiting for target %s",
3069 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
3077 COMMAND_HANDLER(handle_halt_command
)
3081 struct target
*target
= get_current_target(CMD_CTX
);
3083 target
->verbose_halt_msg
= true;
3085 int retval
= target_halt(target
);
3086 if (ERROR_OK
!= retval
)
3089 if (CMD_ARGC
== 1) {
3090 unsigned wait_local
;
3091 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
3092 if (ERROR_OK
!= retval
)
3093 return ERROR_COMMAND_SYNTAX_ERROR
;
3098 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
3101 COMMAND_HANDLER(handle_soft_reset_halt_command
)
3103 struct target
*target
= get_current_target(CMD_CTX
);
3105 LOG_USER("requesting target halt and executing a soft reset");
3107 target_soft_reset_halt(target
);
3112 COMMAND_HANDLER(handle_reset_command
)
3115 return ERROR_COMMAND_SYNTAX_ERROR
;
3117 enum target_reset_mode reset_mode
= RESET_RUN
;
3118 if (CMD_ARGC
== 1) {
3120 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
3121 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
3122 return ERROR_COMMAND_SYNTAX_ERROR
;
3123 reset_mode
= n
->value
;
3126 /* reset *all* targets */
3127 return target_process_reset(CMD
, reset_mode
);
3131 COMMAND_HANDLER(handle_resume_command
)
3135 return ERROR_COMMAND_SYNTAX_ERROR
;
3137 struct target
*target
= get_current_target(CMD_CTX
);
3139 /* with no CMD_ARGV, resume from current pc, addr = 0,
3140 * with one arguments, addr = CMD_ARGV[0],
3141 * handle breakpoints, not debugging */
3142 target_addr_t addr
= 0;
3143 if (CMD_ARGC
== 1) {
3144 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3148 return target_resume(target
, current
, addr
, 1, 0);
3151 COMMAND_HANDLER(handle_step_command
)
3154 return ERROR_COMMAND_SYNTAX_ERROR
;
3158 /* with no CMD_ARGV, step from current pc, addr = 0,
3159 * with one argument addr = CMD_ARGV[0],
3160 * handle breakpoints, debugging */
3161 target_addr_t addr
= 0;
3163 if (CMD_ARGC
== 1) {
3164 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3168 struct target
*target
= get_current_target(CMD_CTX
);
3170 return target_step(target
, current_pc
, addr
, 1);
3173 void target_handle_md_output(struct command_invocation
*cmd
,
3174 struct target
*target
, target_addr_t address
, unsigned size
,
3175 unsigned count
, const uint8_t *buffer
)
3177 const unsigned line_bytecnt
= 32;
3178 unsigned line_modulo
= line_bytecnt
/ size
;
3180 char output
[line_bytecnt
* 4 + 1];
3181 unsigned output_len
= 0;
3183 const char *value_fmt
;
3186 value_fmt
= "%16.16"PRIx64
" ";
3189 value_fmt
= "%8.8"PRIx64
" ";
3192 value_fmt
= "%4.4"PRIx64
" ";
3195 value_fmt
= "%2.2"PRIx64
" ";
3198 /* "can't happen", caller checked */
3199 LOG_ERROR("invalid memory read size: %u", size
);
3203 for (unsigned i
= 0; i
< count
; i
++) {
3204 if (i
% line_modulo
== 0) {
3205 output_len
+= snprintf(output
+ output_len
,
3206 sizeof(output
) - output_len
,
3207 TARGET_ADDR_FMT
": ",
3208 (address
+ (i
* size
)));
3212 const uint8_t *value_ptr
= buffer
+ i
* size
;
3215 value
= target_buffer_get_u64(target
, value_ptr
);
3218 value
= target_buffer_get_u32(target
, value_ptr
);
3221 value
= target_buffer_get_u16(target
, value_ptr
);
3226 output_len
+= snprintf(output
+ output_len
,
3227 sizeof(output
) - output_len
,
3230 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
3231 command_print(cmd
, "%s", output
);
3237 COMMAND_HANDLER(handle_md_command
)
3240 return ERROR_COMMAND_SYNTAX_ERROR
;
3243 switch (CMD_NAME
[2]) {
3257 return ERROR_COMMAND_SYNTAX_ERROR
;
3260 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3261 int (*fn
)(struct target
*target
,
3262 target_addr_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
3266 fn
= target_read_phys_memory
;
3268 fn
= target_read_memory
;
3269 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
3270 return ERROR_COMMAND_SYNTAX_ERROR
;
3272 target_addr_t address
;
3273 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3277 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
3279 uint8_t *buffer
= calloc(count
, size
);
3280 if (buffer
== NULL
) {
3281 LOG_ERROR("Failed to allocate md read buffer");
3285 struct target
*target
= get_current_target(CMD_CTX
);
3286 int retval
= fn(target
, address
, size
, count
, buffer
);
3287 if (ERROR_OK
== retval
)
3288 target_handle_md_output(CMD
, target
, address
, size
, count
, buffer
);
3295 typedef int (*target_write_fn
)(struct target
*target
,
3296 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
3298 static int target_fill_mem(struct target
*target
,
3299 target_addr_t address
,
3307 /* We have to write in reasonably large chunks to be able
3308 * to fill large memory areas with any sane speed */
3309 const unsigned chunk_size
= 16384;
3310 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
3311 if (target_buf
== NULL
) {
3312 LOG_ERROR("Out of memory");
3316 for (unsigned i
= 0; i
< chunk_size
; i
++) {
3317 switch (data_size
) {
3319 target_buffer_set_u64(target
, target_buf
+ i
* data_size
, b
);
3322 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
3325 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
3328 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
3335 int retval
= ERROR_OK
;
3337 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
3340 if (current
> chunk_size
)
3341 current
= chunk_size
;
3342 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
3343 if (retval
!= ERROR_OK
)
3345 /* avoid GDB timeouts */
3354 COMMAND_HANDLER(handle_mw_command
)
3357 return ERROR_COMMAND_SYNTAX_ERROR
;
3358 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3363 fn
= target_write_phys_memory
;
3365 fn
= target_write_memory
;
3366 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
3367 return ERROR_COMMAND_SYNTAX_ERROR
;
3369 target_addr_t address
;
3370 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3373 COMMAND_PARSE_NUMBER(u64
, CMD_ARGV
[1], value
);
3377 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
3379 struct target
*target
= get_current_target(CMD_CTX
);
3381 switch (CMD_NAME
[2]) {
3395 return ERROR_COMMAND_SYNTAX_ERROR
;
3398 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
3401 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
3402 target_addr_t
*min_address
, target_addr_t
*max_address
)
3404 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
3405 return ERROR_COMMAND_SYNTAX_ERROR
;
3407 /* a base address isn't always necessary,
3408 * default to 0x0 (i.e. don't relocate) */
3409 if (CMD_ARGC
>= 2) {
3411 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3412 image
->base_address
= addr
;
3413 image
->base_address_set
= 1;
3415 image
->base_address_set
= 0;
3417 image
->start_address_set
= 0;
3420 COMMAND_PARSE_ADDRESS(CMD_ARGV
[3], *min_address
);
3421 if (CMD_ARGC
== 5) {
3422 COMMAND_PARSE_ADDRESS(CMD_ARGV
[4], *max_address
);
3423 /* use size (given) to find max (required) */
3424 *max_address
+= *min_address
;
3427 if (*min_address
> *max_address
)
3428 return ERROR_COMMAND_SYNTAX_ERROR
;
3433 COMMAND_HANDLER(handle_load_image_command
)
3437 uint32_t image_size
;
3438 target_addr_t min_address
= 0;
3439 target_addr_t max_address
= -1;
3443 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
3444 &image
, &min_address
, &max_address
);
3445 if (ERROR_OK
!= retval
)
3448 struct target
*target
= get_current_target(CMD_CTX
);
3450 struct duration bench
;
3451 duration_start(&bench
);
3453 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
3458 for (i
= 0; i
< image
.num_sections
; i
++) {
3459 buffer
= malloc(image
.sections
[i
].size
);
3460 if (buffer
== NULL
) {
3462 "error allocating buffer for section (%d bytes)",
3463 (int)(image
.sections
[i
].size
));
3464 retval
= ERROR_FAIL
;
3468 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3469 if (retval
!= ERROR_OK
) {
3474 uint32_t offset
= 0;
3475 uint32_t length
= buf_cnt
;
3477 /* DANGER!!! beware of unsigned comparison here!!! */
3479 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3480 (image
.sections
[i
].base_address
< max_address
)) {
3482 if (image
.sections
[i
].base_address
< min_address
) {
3483 /* clip addresses below */
3484 offset
+= min_address
-image
.sections
[i
].base_address
;
3488 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3489 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3491 retval
= target_write_buffer(target
,
3492 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3493 if (retval
!= ERROR_OK
) {
3497 image_size
+= length
;
3498 command_print(CMD
, "%u bytes written at address " TARGET_ADDR_FMT
"",
3499 (unsigned int)length
,
3500 image
.sections
[i
].base_address
+ offset
);
3506 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3507 command_print(CMD
, "downloaded %" PRIu32
" bytes "
3508 "in %fs (%0.3f KiB/s)", image_size
,
3509 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3512 image_close(&image
);
3518 COMMAND_HANDLER(handle_dump_image_command
)
3520 struct fileio
*fileio
;
3522 int retval
, retvaltemp
;
3523 target_addr_t address
, size
;
3524 struct duration bench
;
3525 struct target
*target
= get_current_target(CMD_CTX
);
3528 return ERROR_COMMAND_SYNTAX_ERROR
;
3530 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], address
);
3531 COMMAND_PARSE_ADDRESS(CMD_ARGV
[2], size
);
3533 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
3534 buffer
= malloc(buf_size
);
3538 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3539 if (retval
!= ERROR_OK
) {
3544 duration_start(&bench
);
3547 size_t size_written
;
3548 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
3549 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
3550 if (retval
!= ERROR_OK
)
3553 retval
= fileio_write(fileio
, this_run_size
, buffer
, &size_written
);
3554 if (retval
!= ERROR_OK
)
3557 size
-= this_run_size
;
3558 address
+= this_run_size
;
3563 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3565 retval
= fileio_size(fileio
, &filesize
);
3566 if (retval
!= ERROR_OK
)
3569 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize
,
3570 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3573 retvaltemp
= fileio_close(fileio
);
3574 if (retvaltemp
!= ERROR_OK
)
3583 IMAGE_CHECKSUM_ONLY
= 2
3586 static COMMAND_HELPER(handle_verify_image_command_internal
, enum verify_mode verify
)
3590 uint32_t image_size
;
3593 uint32_t checksum
= 0;
3594 uint32_t mem_checksum
= 0;
3598 struct target
*target
= get_current_target(CMD_CTX
);
3601 return ERROR_COMMAND_SYNTAX_ERROR
;
3604 LOG_ERROR("no target selected");
3608 struct duration bench
;
3609 duration_start(&bench
);
3611 if (CMD_ARGC
>= 2) {
3613 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3614 image
.base_address
= addr
;
3615 image
.base_address_set
= 1;
3617 image
.base_address_set
= 0;
3618 image
.base_address
= 0x0;
3621 image
.start_address_set
= 0;
3623 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3624 if (retval
!= ERROR_OK
)
3630 for (i
= 0; i
< image
.num_sections
; i
++) {
3631 buffer
= malloc(image
.sections
[i
].size
);
3632 if (buffer
== NULL
) {
3634 "error allocating buffer for section (%d bytes)",
3635 (int)(image
.sections
[i
].size
));
3638 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3639 if (retval
!= ERROR_OK
) {
3644 if (verify
>= IMAGE_VERIFY
) {
3645 /* calculate checksum of image */
3646 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3647 if (retval
!= ERROR_OK
) {
3652 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3653 if (retval
!= ERROR_OK
) {
3657 if ((checksum
!= mem_checksum
) && (verify
== IMAGE_CHECKSUM_ONLY
)) {
3658 LOG_ERROR("checksum mismatch");
3660 retval
= ERROR_FAIL
;
3663 if (checksum
!= mem_checksum
) {
3664 /* failed crc checksum, fall back to a binary compare */
3668 LOG_ERROR("checksum mismatch - attempting binary compare");
3670 data
= malloc(buf_cnt
);
3672 retval
= target_read_buffer(target
, image
.sections
[i
].base_address
, buf_cnt
, data
);
3673 if (retval
== ERROR_OK
) {
3675 for (t
= 0; t
< buf_cnt
; t
++) {
3676 if (data
[t
] != buffer
[t
]) {
3678 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3680 (unsigned)(t
+ image
.sections
[i
].base_address
),
3683 if (diffs
++ >= 127) {
3684 command_print(CMD
, "More than 128 errors, the rest are not printed.");
3696 command_print(CMD
, "address " TARGET_ADDR_FMT
" length 0x%08zx",
3697 image
.sections
[i
].base_address
,
3702 image_size
+= buf_cnt
;
3705 command_print(CMD
, "No more differences found.");
3708 retval
= ERROR_FAIL
;
3709 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3710 command_print(CMD
, "verified %" PRIu32
" bytes "
3711 "in %fs (%0.3f KiB/s)", image_size
,
3712 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3715 image_close(&image
);
3720 COMMAND_HANDLER(handle_verify_image_checksum_command
)
3722 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_CHECKSUM_ONLY
);
3725 COMMAND_HANDLER(handle_verify_image_command
)
3727 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_VERIFY
);
3730 COMMAND_HANDLER(handle_test_image_command
)
3732 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_TEST
);
3735 static int handle_bp_command_list(struct command_invocation
*cmd
)
3737 struct target
*target
= get_current_target(cmd
->ctx
);
3738 struct breakpoint
*breakpoint
= target
->breakpoints
;
3739 while (breakpoint
) {
3740 if (breakpoint
->type
== BKPT_SOFT
) {
3741 char *buf
= buf_to_hex_str(breakpoint
->orig_instr
,
3742 breakpoint
->length
);
3743 command_print(cmd
, "IVA breakpoint: " TARGET_ADDR_FMT
", 0x%x, %i, 0x%s",
3744 breakpoint
->address
,
3746 breakpoint
->set
, buf
);
3749 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3750 command_print(cmd
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3752 breakpoint
->length
, breakpoint
->set
);
3753 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3754 command_print(cmd
, "Hybrid breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3755 breakpoint
->address
,
3756 breakpoint
->length
, breakpoint
->set
);
3757 command_print(cmd
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3760 command_print(cmd
, "Breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3761 breakpoint
->address
,
3762 breakpoint
->length
, breakpoint
->set
);
3765 breakpoint
= breakpoint
->next
;
3770 static int handle_bp_command_set(struct command_invocation
*cmd
,
3771 target_addr_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3773 struct target
*target
= get_current_target(cmd
->ctx
);
3777 retval
= breakpoint_add(target
, addr
, length
, hw
);
3778 /* error is always logged in breakpoint_add(), do not print it again */
3779 if (ERROR_OK
== retval
)
3780 command_print(cmd
, "breakpoint set at " TARGET_ADDR_FMT
"", addr
);
3782 } else if (addr
== 0) {
3783 if (target
->type
->add_context_breakpoint
== NULL
) {
3784 LOG_ERROR("Context breakpoint not available");
3785 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
3787 retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3788 /* error is always logged in context_breakpoint_add(), do not print it again */
3789 if (ERROR_OK
== retval
)
3790 command_print(cmd
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3793 if (target
->type
->add_hybrid_breakpoint
== NULL
) {
3794 LOG_ERROR("Hybrid breakpoint not available");
3795 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
3797 retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3798 /* error is always logged in hybrid_breakpoint_add(), do not print it again */
3799 if (ERROR_OK
== retval
)
3800 command_print(cmd
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3805 COMMAND_HANDLER(handle_bp_command
)
3814 return handle_bp_command_list(CMD
);
3818 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3819 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3820 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
3823 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
3825 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3826 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3828 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
3829 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
3831 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
3832 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3834 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
3839 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3840 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
3841 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
3842 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
3845 return ERROR_COMMAND_SYNTAX_ERROR
;
3849 COMMAND_HANDLER(handle_rbp_command
)
3852 return ERROR_COMMAND_SYNTAX_ERROR
;
3854 struct target
*target
= get_current_target(CMD_CTX
);
3856 if (!strcmp(CMD_ARGV
[0], "all")) {
3857 breakpoint_remove_all(target
);
3860 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3862 breakpoint_remove(target
, addr
);
3868 COMMAND_HANDLER(handle_wp_command
)
3870 struct target
*target
= get_current_target(CMD_CTX
);
3872 if (CMD_ARGC
== 0) {
3873 struct watchpoint
*watchpoint
= target
->watchpoints
;
3875 while (watchpoint
) {
3876 command_print(CMD
, "address: " TARGET_ADDR_FMT
3877 ", len: 0x%8.8" PRIx32
3878 ", r/w/a: %i, value: 0x%8.8" PRIx32
3879 ", mask: 0x%8.8" PRIx32
,
3880 watchpoint
->address
,
3882 (int)watchpoint
->rw
,
3885 watchpoint
= watchpoint
->next
;
3890 enum watchpoint_rw type
= WPT_ACCESS
;
3892 uint32_t length
= 0;
3893 uint32_t data_value
= 0x0;
3894 uint32_t data_mask
= 0xffffffff;
3898 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
3901 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3904 switch (CMD_ARGV
[2][0]) {
3915 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3916 return ERROR_COMMAND_SYNTAX_ERROR
;
3920 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3921 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3925 return ERROR_COMMAND_SYNTAX_ERROR
;
3928 int retval
= watchpoint_add(target
, addr
, length
, type
,
3929 data_value
, data_mask
);
3930 if (ERROR_OK
!= retval
)
3931 LOG_ERROR("Failure setting watchpoints");
3936 COMMAND_HANDLER(handle_rwp_command
)
3939 return ERROR_COMMAND_SYNTAX_ERROR
;
3942 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3944 struct target
*target
= get_current_target(CMD_CTX
);
3945 watchpoint_remove(target
, addr
);
3951 * Translate a virtual address to a physical address.
3953 * The low-level target implementation must have logged a detailed error
3954 * which is forwarded to telnet/GDB session.
3956 COMMAND_HANDLER(handle_virt2phys_command
)
3959 return ERROR_COMMAND_SYNTAX_ERROR
;
3962 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], va
);
3965 struct target
*target
= get_current_target(CMD_CTX
);
3966 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3967 if (retval
== ERROR_OK
)
3968 command_print(CMD
, "Physical address " TARGET_ADDR_FMT
"", pa
);
3973 static void writeData(FILE *f
, const void *data
, size_t len
)
3975 size_t written
= fwrite(data
, 1, len
, f
);
3977 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3980 static void writeLong(FILE *f
, int l
, struct target
*target
)
3984 target_buffer_set_u32(target
, val
, l
);
3985 writeData(f
, val
, 4);
3988 static void writeString(FILE *f
, char *s
)
3990 writeData(f
, s
, strlen(s
));
3993 typedef unsigned char UNIT
[2]; /* unit of profiling */
3995 /* Dump a gmon.out histogram file. */
3996 static void write_gmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
, bool with_range
,
3997 uint32_t start_address
, uint32_t end_address
, struct target
*target
, uint32_t duration_ms
)
4000 FILE *f
= fopen(filename
, "w");
4003 writeString(f
, "gmon");
4004 writeLong(f
, 0x00000001, target
); /* Version */
4005 writeLong(f
, 0, target
); /* padding */
4006 writeLong(f
, 0, target
); /* padding */
4007 writeLong(f
, 0, target
); /* padding */
4009 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
4010 writeData(f
, &zero
, 1);
4012 /* figure out bucket size */
4016 min
= start_address
;
4021 for (i
= 0; i
< sampleNum
; i
++) {
4022 if (min
> samples
[i
])
4024 if (max
< samples
[i
])
4028 /* max should be (largest sample + 1)
4029 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
4033 int addressSpace
= max
- min
;
4034 assert(addressSpace
>= 2);
4036 /* FIXME: What is the reasonable number of buckets?
4037 * The profiling result will be more accurate if there are enough buckets. */
4038 static const uint32_t maxBuckets
= 128 * 1024; /* maximum buckets. */
4039 uint32_t numBuckets
= addressSpace
/ sizeof(UNIT
);
4040 if (numBuckets
> maxBuckets
)
4041 numBuckets
= maxBuckets
;
4042 int *buckets
= malloc(sizeof(int) * numBuckets
);
4043 if (buckets
== NULL
) {
4047 memset(buckets
, 0, sizeof(int) * numBuckets
);
4048 for (i
= 0; i
< sampleNum
; i
++) {
4049 uint32_t address
= samples
[i
];
4051 if ((address
< min
) || (max
<= address
))
4054 long long a
= address
- min
;
4055 long long b
= numBuckets
;
4056 long long c
= addressSpace
;
4057 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
4061 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
4062 writeLong(f
, min
, target
); /* low_pc */
4063 writeLong(f
, max
, target
); /* high_pc */
4064 writeLong(f
, numBuckets
, target
); /* # of buckets */
4065 float sample_rate
= sampleNum
/ (duration_ms
/ 1000.0);
4066 writeLong(f
, sample_rate
, target
);
4067 writeString(f
, "seconds");
4068 for (i
= 0; i
< (15-strlen("seconds")); i
++)
4069 writeData(f
, &zero
, 1);
4070 writeString(f
, "s");
4072 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
4074 char *data
= malloc(2 * numBuckets
);
4076 for (i
= 0; i
< numBuckets
; i
++) {
4081 data
[i
* 2] = val
&0xff;
4082 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
4085 writeData(f
, data
, numBuckets
* 2);
4093 /* profiling samples the CPU PC as quickly as OpenOCD is able,
4094 * which will be used as a random sampling of PC */
4095 COMMAND_HANDLER(handle_profile_command
)
4097 struct target
*target
= get_current_target(CMD_CTX
);
4099 if ((CMD_ARGC
!= 2) && (CMD_ARGC
!= 4))
4100 return ERROR_COMMAND_SYNTAX_ERROR
;
4102 const uint32_t MAX_PROFILE_SAMPLE_NUM
= 10000;
4104 uint32_t num_of_samples
;
4105 int retval
= ERROR_OK
;
4107 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], offset
);
4109 uint32_t *samples
= malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM
);
4110 if (samples
== NULL
) {
4111 LOG_ERROR("No memory to store samples.");
4115 uint64_t timestart_ms
= timeval_ms();
4117 * Some cores let us sample the PC without the
4118 * annoying halt/resume step; for example, ARMv7 PCSR.
4119 * Provide a way to use that more efficient mechanism.
4121 retval
= target_profiling(target
, samples
, MAX_PROFILE_SAMPLE_NUM
,
4122 &num_of_samples
, offset
);
4123 if (retval
!= ERROR_OK
) {
4127 uint32_t duration_ms
= timeval_ms() - timestart_ms
;
4129 assert(num_of_samples
<= MAX_PROFILE_SAMPLE_NUM
);
4131 retval
= target_poll(target
);
4132 if (retval
!= ERROR_OK
) {
4136 if (target
->state
== TARGET_RUNNING
) {
4137 retval
= target_halt(target
);
4138 if (retval
!= ERROR_OK
) {
4144 retval
= target_poll(target
);
4145 if (retval
!= ERROR_OK
) {
4150 uint32_t start_address
= 0;
4151 uint32_t end_address
= 0;
4152 bool with_range
= false;
4153 if (CMD_ARGC
== 4) {
4155 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], start_address
);
4156 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], end_address
);
4159 write_gmon(samples
, num_of_samples
, CMD_ARGV
[1],
4160 with_range
, start_address
, end_address
, target
, duration_ms
);
4161 command_print(CMD
, "Wrote %s", CMD_ARGV
[1]);
4167 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
4170 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4173 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4177 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4178 valObjPtr
= Jim_NewIntObj(interp
, val
);
4179 if (!nameObjPtr
|| !valObjPtr
) {
4184 Jim_IncrRefCount(nameObjPtr
);
4185 Jim_IncrRefCount(valObjPtr
);
4186 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
4187 Jim_DecrRefCount(interp
, nameObjPtr
);
4188 Jim_DecrRefCount(interp
, valObjPtr
);
4190 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
4194 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4196 struct command_context
*context
;
4197 struct target
*target
;
4199 context
= current_command_context(interp
);
4200 assert(context
!= NULL
);
4202 target
= get_current_target(context
);
4203 if (target
== NULL
) {
4204 LOG_ERROR("mem2array: no current target");
4208 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4211 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
4219 const char *varname
;
4225 /* argv[1] = name of array to receive the data
4226 * argv[2] = desired width
4227 * argv[3] = memory address
4228 * argv[4] = count of times to read
4231 if (argc
< 4 || argc
> 5) {
4232 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4235 varname
= Jim_GetString(argv
[0], &len
);
4236 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4238 e
= Jim_GetLong(interp
, argv
[1], &l
);
4243 e
= Jim_GetLong(interp
, argv
[2], &l
);
4247 e
= Jim_GetLong(interp
, argv
[3], &l
);
4253 phys
= Jim_GetString(argv
[4], &n
);
4254 if (!strncmp(phys
, "phys", n
))
4270 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4271 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
4275 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4276 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
4279 if ((addr
+ (len
* width
)) < addr
) {
4280 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4281 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
4284 /* absurd transfer size? */
4286 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4287 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
4292 ((width
== 2) && ((addr
& 1) == 0)) ||
4293 ((width
== 4) && ((addr
& 3) == 0))) {
4297 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4298 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRIu32
" byte reads",
4301 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4310 size_t buffersize
= 4096;
4311 uint8_t *buffer
= malloc(buffersize
);
4318 /* Slurp... in buffer size chunks */
4320 count
= len
; /* in objects.. */
4321 if (count
> (buffersize
/ width
))
4322 count
= (buffersize
/ width
);
4325 retval
= target_read_phys_memory(target
, addr
, width
, count
, buffer
);
4327 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
4328 if (retval
!= ERROR_OK
) {
4330 LOG_ERROR("mem2array: Read @ 0x%08" PRIx32
", w=%" PRIu32
", cnt=%" PRIu32
", failed",
4334 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4335 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
4339 v
= 0; /* shut up gcc */
4340 for (i
= 0; i
< count
; i
++, n
++) {
4343 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
4346 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
4349 v
= buffer
[i
] & 0x0ff;
4352 new_int_array_element(interp
, varname
, n
, v
);
4355 addr
+= count
* width
;
4361 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4366 static int get_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t *val
)
4369 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4373 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4377 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4383 Jim_IncrRefCount(nameObjPtr
);
4384 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
4385 Jim_DecrRefCount(interp
, nameObjPtr
);
4387 if (valObjPtr
== NULL
)
4390 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
4391 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
4396 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4398 struct command_context
*context
;
4399 struct target
*target
;
4401 context
= current_command_context(interp
);
4402 assert(context
!= NULL
);
4404 target
= get_current_target(context
);
4405 if (target
== NULL
) {
4406 LOG_ERROR("array2mem: no current target");
4410 return target_array2mem(interp
, target
, argc
-1, argv
+ 1);
4413 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
4414 int argc
, Jim_Obj
*const *argv
)
4422 const char *varname
;
4428 /* argv[1] = name of array to get the data
4429 * argv[2] = desired width
4430 * argv[3] = memory address
4431 * argv[4] = count to write
4433 if (argc
< 4 || argc
> 5) {
4434 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4437 varname
= Jim_GetString(argv
[0], &len
);
4438 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4440 e
= Jim_GetLong(interp
, argv
[1], &l
);
4445 e
= Jim_GetLong(interp
, argv
[2], &l
);
4449 e
= Jim_GetLong(interp
, argv
[3], &l
);
4455 phys
= Jim_GetString(argv
[4], &n
);
4456 if (!strncmp(phys
, "phys", n
))
4472 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4473 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4474 "Invalid width param, must be 8/16/32", NULL
);
4478 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4479 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4480 "array2mem: zero width read?", NULL
);
4483 if ((addr
+ (len
* width
)) < addr
) {
4484 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4485 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4486 "array2mem: addr + len - wraps to zero?", NULL
);
4489 /* absurd transfer size? */
4491 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4492 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4493 "array2mem: absurd > 64K item request", NULL
);
4498 ((width
== 2) && ((addr
& 1) == 0)) ||
4499 ((width
== 4) && ((addr
& 3) == 0))) {
4503 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4504 sprintf(buf
, "array2mem address: 0x%08" PRIx32
" is not aligned for %" PRIu32
" byte reads",
4507 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4518 size_t buffersize
= 4096;
4519 uint8_t *buffer
= malloc(buffersize
);
4524 /* Slurp... in buffer size chunks */
4526 count
= len
; /* in objects.. */
4527 if (count
> (buffersize
/ width
))
4528 count
= (buffersize
/ width
);
4530 v
= 0; /* shut up gcc */
4531 for (i
= 0; i
< count
; i
++, n
++) {
4532 get_int_array_element(interp
, varname
, n
, &v
);
4535 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
4538 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
4541 buffer
[i
] = v
& 0x0ff;
4548 retval
= target_write_phys_memory(target
, addr
, width
, count
, buffer
);
4550 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
4551 if (retval
!= ERROR_OK
) {
4553 LOG_ERROR("array2mem: Write @ 0x%08" PRIx32
", w=%" PRIu32
", cnt=%" PRIu32
", failed",
4557 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4558 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
4562 addr
+= count
* width
;
4567 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4572 /* FIX? should we propagate errors here rather than printing them
4575 void target_handle_event(struct target
*target
, enum target_event e
)
4577 struct target_event_action
*teap
;
4580 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4581 if (teap
->event
== e
) {
4582 LOG_DEBUG("target(%d): %s (%s) event: %d (%s) action: %s",
4583 target
->target_number
,
4584 target_name(target
),
4585 target_type_name(target
),
4587 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
4588 Jim_GetString(teap
->body
, NULL
));
4590 /* Override current target by the target an event
4591 * is issued from (lot of scripts need it).
4592 * Return back to previous override as soon
4593 * as the handler processing is done */
4594 struct command_context
*cmd_ctx
= current_command_context(teap
->interp
);
4595 struct target
*saved_target_override
= cmd_ctx
->current_target_override
;
4596 cmd_ctx
->current_target_override
= target
;
4598 retval
= Jim_EvalObj(teap
->interp
, teap
->body
);
4600 cmd_ctx
->current_target_override
= saved_target_override
;
4602 if (retval
== ERROR_COMMAND_CLOSE_CONNECTION
)
4605 if (retval
== JIM_RETURN
)
4606 retval
= teap
->interp
->returnCode
;
4608 if (retval
!= JIM_OK
) {
4609 Jim_MakeErrorMessage(teap
->interp
);
4610 LOG_USER("Error executing event %s on target %s:\n%s",
4611 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
4612 target_name(target
),
4613 Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
4614 /* clean both error code and stacktrace before return */
4615 Jim_Eval(teap
->interp
, "error \"\" \"\"");
4622 * Returns true only if the target has a handler for the specified event.
4624 bool target_has_event_action(struct target
*target
, enum target_event event
)
4626 struct target_event_action
*teap
;
4628 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4629 if (teap
->event
== event
)
4635 enum target_cfg_param
{
4638 TCFG_WORK_AREA_VIRT
,
4639 TCFG_WORK_AREA_PHYS
,
4640 TCFG_WORK_AREA_SIZE
,
4641 TCFG_WORK_AREA_BACKUP
,
4644 TCFG_CHAIN_POSITION
,
4651 static Jim_Nvp nvp_config_opts
[] = {
4652 { .name
= "-type", .value
= TCFG_TYPE
},
4653 { .name
= "-event", .value
= TCFG_EVENT
},
4654 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
4655 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
4656 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
4657 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
4658 { .name
= "-endian", .value
= TCFG_ENDIAN
},
4659 { .name
= "-coreid", .value
= TCFG_COREID
},
4660 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
4661 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
4662 { .name
= "-rtos", .value
= TCFG_RTOS
},
4663 { .name
= "-defer-examine", .value
= TCFG_DEFER_EXAMINE
},
4664 { .name
= "-gdb-port", .value
= TCFG_GDB_PORT
},
4665 { .name
= NULL
, .value
= -1 }
4668 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
4675 /* parse config or cget options ... */
4676 while (goi
->argc
> 0) {
4677 Jim_SetEmptyResult(goi
->interp
);
4678 /* Jim_GetOpt_Debug(goi); */
4680 if (target
->type
->target_jim_configure
) {
4681 /* target defines a configure function */
4682 /* target gets first dibs on parameters */
4683 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
4692 /* otherwise we 'continue' below */
4694 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
4696 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
4702 if (goi
->isconfigure
) {
4703 Jim_SetResultFormatted(goi
->interp
,
4704 "not settable: %s", n
->name
);
4708 if (goi
->argc
!= 0) {
4709 Jim_WrongNumArgs(goi
->interp
,
4710 goi
->argc
, goi
->argv
,
4715 Jim_SetResultString(goi
->interp
,
4716 target_type_name(target
), -1);
4720 if (goi
->argc
== 0) {
4721 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
4725 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
4727 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
4731 if (goi
->isconfigure
) {
4732 if (goi
->argc
!= 1) {
4733 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
4737 if (goi
->argc
!= 0) {
4738 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
4744 struct target_event_action
*teap
;
4746 teap
= target
->event_action
;
4747 /* replace existing? */
4749 if (teap
->event
== (enum target_event
)n
->value
)
4754 if (goi
->isconfigure
) {
4755 bool replace
= true;
4758 teap
= calloc(1, sizeof(*teap
));
4761 teap
->event
= n
->value
;
4762 teap
->interp
= goi
->interp
;
4763 Jim_GetOpt_Obj(goi
, &o
);
4765 Jim_DecrRefCount(teap
->interp
, teap
->body
);
4766 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
4769 * Tcl/TK - "tk events" have a nice feature.
4770 * See the "BIND" command.
4771 * We should support that here.
4772 * You can specify %X and %Y in the event code.
4773 * The idea is: %T - target name.
4774 * The idea is: %N - target number
4775 * The idea is: %E - event name.
4777 Jim_IncrRefCount(teap
->body
);
4780 /* add to head of event list */
4781 teap
->next
= target
->event_action
;
4782 target
->event_action
= teap
;
4784 Jim_SetEmptyResult(goi
->interp
);
4788 Jim_SetEmptyResult(goi
->interp
);
4790 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
4796 case TCFG_WORK_AREA_VIRT
:
4797 if (goi
->isconfigure
) {
4798 target_free_all_working_areas(target
);
4799 e
= Jim_GetOpt_Wide(goi
, &w
);
4802 target
->working_area_virt
= w
;
4803 target
->working_area_virt_spec
= true;
4808 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
4812 case TCFG_WORK_AREA_PHYS
:
4813 if (goi
->isconfigure
) {
4814 target_free_all_working_areas(target
);
4815 e
= Jim_GetOpt_Wide(goi
, &w
);
4818 target
->working_area_phys
= w
;
4819 target
->working_area_phys_spec
= true;
4824 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
4828 case TCFG_WORK_AREA_SIZE
:
4829 if (goi
->isconfigure
) {
4830 target_free_all_working_areas(target
);
4831 e
= Jim_GetOpt_Wide(goi
, &w
);
4834 target
->working_area_size
= w
;
4839 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4843 case TCFG_WORK_AREA_BACKUP
:
4844 if (goi
->isconfigure
) {
4845 target_free_all_working_areas(target
);
4846 e
= Jim_GetOpt_Wide(goi
, &w
);
4849 /* make this exactly 1 or 0 */
4850 target
->backup_working_area
= (!!w
);
4855 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
4856 /* loop for more e*/
4861 if (goi
->isconfigure
) {
4862 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
4864 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
4867 target
->endianness
= n
->value
;
4872 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4873 if (n
->name
== NULL
) {
4874 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4875 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4877 Jim_SetResultString(goi
->interp
, n
->name
, -1);
4882 if (goi
->isconfigure
) {
4883 e
= Jim_GetOpt_Wide(goi
, &w
);
4886 target
->coreid
= (int32_t)w
;
4891 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->coreid
));
4895 case TCFG_CHAIN_POSITION
:
4896 if (goi
->isconfigure
) {
4898 struct jtag_tap
*tap
;
4900 if (target
->has_dap
) {
4901 Jim_SetResultString(goi
->interp
,
4902 "target requires -dap parameter instead of -chain-position!", -1);
4906 target_free_all_working_areas(target
);
4907 e
= Jim_GetOpt_Obj(goi
, &o_t
);
4910 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
4914 target
->tap_configured
= true;
4919 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
4920 /* loop for more e*/
4923 if (goi
->isconfigure
) {
4924 e
= Jim_GetOpt_Wide(goi
, &w
);
4927 target
->dbgbase
= (uint32_t)w
;
4928 target
->dbgbase_set
= true;
4933 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
4939 int result
= rtos_create(goi
, target
);
4940 if (result
!= JIM_OK
)
4946 case TCFG_DEFER_EXAMINE
:
4948 target
->defer_examine
= true;
4953 if (goi
->isconfigure
) {
4954 struct command_context
*cmd_ctx
= current_command_context(goi
->interp
);
4955 if (cmd_ctx
->mode
!= COMMAND_CONFIG
) {
4956 Jim_SetResultString(goi
->interp
, "-gdb-port must be configured before 'init'", -1);
4961 e
= Jim_GetOpt_String(goi
, &s
, NULL
);
4964 target
->gdb_port_override
= strdup(s
);
4969 Jim_SetResultString(goi
->interp
, target
->gdb_port_override
? : "undefined", -1);
4973 } /* while (goi->argc) */
4976 /* done - we return */
4980 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4984 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4985 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4987 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4988 "missing: -option ...");
4991 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4992 return target_configure(&goi
, target
);
4995 static int jim_target_mem2array(Jim_Interp
*interp
,
4996 int argc
, Jim_Obj
*const *argv
)
4998 struct target
*target
= Jim_CmdPrivData(interp
);
4999 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
5002 static int jim_target_array2mem(Jim_Interp
*interp
,
5003 int argc
, Jim_Obj
*const *argv
)
5005 struct target
*target
= Jim_CmdPrivData(interp
);
5006 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
5009 static int jim_target_tap_disabled(Jim_Interp
*interp
)
5011 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
5015 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5017 bool allow_defer
= false;
5020 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5022 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5023 Jim_SetResultFormatted(goi
.interp
,
5024 "usage: %s ['allow-defer']", cmd_name
);
5028 strcmp(Jim_GetString(argv
[1], NULL
), "allow-defer") == 0) {
5031 int e
= Jim_GetOpt_Obj(&goi
, &obj
);
5037 struct target
*target
= Jim_CmdPrivData(interp
);
5038 if (!target
->tap
->enabled
)
5039 return jim_target_tap_disabled(interp
);
5041 if (allow_defer
&& target
->defer_examine
) {
5042 LOG_INFO("Deferring arp_examine of %s", target_name(target
));
5043 LOG_INFO("Use arp_examine command to examine it manually!");
5047 int e
= target
->type
->examine(target
);
5053 static int jim_target_was_examined(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5055 struct target
*target
= Jim_CmdPrivData(interp
);
5057 Jim_SetResultBool(interp
, target_was_examined(target
));
5061 static int jim_target_examine_deferred(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5063 struct target
*target
= Jim_CmdPrivData(interp
);
5065 Jim_SetResultBool(interp
, target
->defer_examine
);
5069 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5072 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5075 struct target
*target
= Jim_CmdPrivData(interp
);
5077 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
5083 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5086 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5089 struct target
*target
= Jim_CmdPrivData(interp
);
5090 if (!target
->tap
->enabled
)
5091 return jim_target_tap_disabled(interp
);
5094 if (!(target_was_examined(target
)))
5095 e
= ERROR_TARGET_NOT_EXAMINED
;
5097 e
= target
->type
->poll(target
);
5103 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5106 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5108 if (goi
.argc
!= 2) {
5109 Jim_WrongNumArgs(interp
, 0, argv
,
5110 "([tT]|[fF]|assert|deassert) BOOL");
5115 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
5117 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
5120 /* the halt or not param */
5122 e
= Jim_GetOpt_Wide(&goi
, &a
);
5126 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
5127 if (!target
->tap
->enabled
)
5128 return jim_target_tap_disabled(interp
);
5130 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
5131 Jim_SetResultFormatted(interp
,
5132 "No target-specific reset for %s",
5133 target_name(target
));
5137 if (target
->defer_examine
)
5138 target_reset_examined(target
);
5140 /* determine if we should halt or not. */
5141 target
->reset_halt
= !!a
;
5142 /* When this happens - all workareas are invalid. */
5143 target_free_all_working_areas_restore(target
, 0);
5146 if (n
->value
== NVP_ASSERT
)
5147 e
= target
->type
->assert_reset(target
);
5149 e
= target
->type
->deassert_reset(target
);
5150 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5153 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5156 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5159 struct target
*target
= Jim_CmdPrivData(interp
);
5160 if (!target
->tap
->enabled
)
5161 return jim_target_tap_disabled(interp
);
5162 int e
= target
->type
->halt(target
);
5163 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5166 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5169 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5171 /* params: <name> statename timeoutmsecs */
5172 if (goi
.argc
!= 2) {
5173 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5174 Jim_SetResultFormatted(goi
.interp
,
5175 "%s <state_name> <timeout_in_msec>", cmd_name
);
5180 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
5182 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
, 1);
5186 e
= Jim_GetOpt_Wide(&goi
, &a
);
5189 struct target
*target
= Jim_CmdPrivData(interp
);
5190 if (!target
->tap
->enabled
)
5191 return jim_target_tap_disabled(interp
);
5193 e
= target_wait_state(target
, n
->value
, a
);
5194 if (e
!= ERROR_OK
) {
5195 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
5196 Jim_SetResultFormatted(goi
.interp
,
5197 "target: %s wait %s fails (%#s) %s",
5198 target_name(target
), n
->name
,
5199 eObj
, target_strerror_safe(e
));
5204 /* List for human, Events defined for this target.
5205 * scripts/programs should use 'name cget -event NAME'
5207 COMMAND_HANDLER(handle_target_event_list
)
5209 struct target
*target
= get_current_target(CMD_CTX
);
5210 struct target_event_action
*teap
= target
->event_action
;
5212 command_print(CMD
, "Event actions for target (%d) %s\n",
5213 target
->target_number
,
5214 target_name(target
));
5215 command_print(CMD
, "%-25s | Body", "Event");
5216 command_print(CMD
, "------------------------- | "
5217 "----------------------------------------");
5219 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
5220 command_print(CMD
, "%-25s | %s",
5221 opt
->name
, Jim_GetString(teap
->body
, NULL
));
5224 command_print(CMD
, "***END***");
5227 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5230 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5233 struct target
*target
= Jim_CmdPrivData(interp
);
5234 Jim_SetResultString(interp
, target_state_name(target
), -1);
5237 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5240 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5241 if (goi
.argc
!= 1) {
5242 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5243 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
5247 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
5249 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
5252 struct target
*target
= Jim_CmdPrivData(interp
);
5253 target_handle_event(target
, n
->value
);
5257 static const struct command_registration target_instance_command_handlers
[] = {
5259 .name
= "configure",
5260 .mode
= COMMAND_ANY
,
5261 .jim_handler
= jim_target_configure
,
5262 .help
= "configure a new target for use",
5263 .usage
= "[target_attribute ...]",
5267 .mode
= COMMAND_ANY
,
5268 .jim_handler
= jim_target_configure
,
5269 .help
= "returns the specified target attribute",
5270 .usage
= "target_attribute",
5274 .handler
= handle_mw_command
,
5275 .mode
= COMMAND_EXEC
,
5276 .help
= "Write 64-bit word(s) to target memory",
5277 .usage
= "address data [count]",
5281 .handler
= handle_mw_command
,
5282 .mode
= COMMAND_EXEC
,
5283 .help
= "Write 32-bit word(s) to target memory",
5284 .usage
= "address data [count]",
5288 .handler
= handle_mw_command
,
5289 .mode
= COMMAND_EXEC
,
5290 .help
= "Write 16-bit half-word(s) to target memory",
5291 .usage
= "address data [count]",
5295 .handler
= handle_mw_command
,
5296 .mode
= COMMAND_EXEC
,
5297 .help
= "Write byte(s) to target memory",
5298 .usage
= "address data [count]",
5302 .handler
= handle_md_command
,
5303 .mode
= COMMAND_EXEC
,
5304 .help
= "Display target memory as 64-bit words",
5305 .usage
= "address [count]",
5309 .handler
= handle_md_command
,
5310 .mode
= COMMAND_EXEC
,
5311 .help
= "Display target memory as 32-bit words",
5312 .usage
= "address [count]",
5316 .handler
= handle_md_command
,
5317 .mode
= COMMAND_EXEC
,
5318 .help
= "Display target memory as 16-bit half-words",
5319 .usage
= "address [count]",
5323 .handler
= handle_md_command
,
5324 .mode
= COMMAND_EXEC
,
5325 .help
= "Display target memory as 8-bit bytes",
5326 .usage
= "address [count]",
5329 .name
= "array2mem",
5330 .mode
= COMMAND_EXEC
,
5331 .jim_handler
= jim_target_array2mem
,
5332 .help
= "Writes Tcl array of 8/16/32 bit numbers "
5334 .usage
= "arrayname bitwidth address count",
5337 .name
= "mem2array",
5338 .mode
= COMMAND_EXEC
,
5339 .jim_handler
= jim_target_mem2array
,
5340 .help
= "Loads Tcl array of 8/16/32 bit numbers "
5341 "from target memory",
5342 .usage
= "arrayname bitwidth address count",
5345 .name
= "eventlist",
5346 .handler
= handle_target_event_list
,
5347 .mode
= COMMAND_EXEC
,
5348 .help
= "displays a table of events defined for this target",
5353 .mode
= COMMAND_EXEC
,
5354 .jim_handler
= jim_target_current_state
,
5355 .help
= "displays the current state of this target",
5358 .name
= "arp_examine",
5359 .mode
= COMMAND_EXEC
,
5360 .jim_handler
= jim_target_examine
,
5361 .help
= "used internally for reset processing",
5362 .usage
= "['allow-defer']",
5365 .name
= "was_examined",
5366 .mode
= COMMAND_EXEC
,
5367 .jim_handler
= jim_target_was_examined
,
5368 .help
= "used internally for reset processing",
5371 .name
= "examine_deferred",
5372 .mode
= COMMAND_EXEC
,
5373 .jim_handler
= jim_target_examine_deferred
,
5374 .help
= "used internally for reset processing",
5377 .name
= "arp_halt_gdb",
5378 .mode
= COMMAND_EXEC
,
5379 .jim_handler
= jim_target_halt_gdb
,
5380 .help
= "used internally for reset processing to halt GDB",
5384 .mode
= COMMAND_EXEC
,
5385 .jim_handler
= jim_target_poll
,
5386 .help
= "used internally for reset processing",
5389 .name
= "arp_reset",
5390 .mode
= COMMAND_EXEC
,
5391 .jim_handler
= jim_target_reset
,
5392 .help
= "used internally for reset processing",
5396 .mode
= COMMAND_EXEC
,
5397 .jim_handler
= jim_target_halt
,
5398 .help
= "used internally for reset processing",
5401 .name
= "arp_waitstate",
5402 .mode
= COMMAND_EXEC
,
5403 .jim_handler
= jim_target_wait_state
,
5404 .help
= "used internally for reset processing",
5407 .name
= "invoke-event",
5408 .mode
= COMMAND_EXEC
,
5409 .jim_handler
= jim_target_invoke_event
,
5410 .help
= "invoke handler for specified event",
5411 .usage
= "event_name",
5413 COMMAND_REGISTRATION_DONE
5416 static int target_create(Jim_GetOptInfo
*goi
)
5423 struct target
*target
;
5424 struct command_context
*cmd_ctx
;
5426 cmd_ctx
= current_command_context(goi
->interp
);
5427 assert(cmd_ctx
!= NULL
);
5429 if (goi
->argc
< 3) {
5430 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
5435 Jim_GetOpt_Obj(goi
, &new_cmd
);
5436 /* does this command exist? */
5437 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
5439 cp
= Jim_GetString(new_cmd
, NULL
);
5440 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
5445 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
5448 struct transport
*tr
= get_current_transport();
5449 if (tr
->override_target
) {
5450 e
= tr
->override_target(&cp
);
5451 if (e
!= ERROR_OK
) {
5452 LOG_ERROR("The selected transport doesn't support this target");
5455 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
5457 /* now does target type exist */
5458 for (x
= 0 ; target_types
[x
] ; x
++) {
5459 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
5464 /* check for deprecated name */
5465 if (target_types
[x
]->deprecated_name
) {
5466 if (0 == strcmp(cp
, target_types
[x
]->deprecated_name
)) {
5468 LOG_WARNING("target name is deprecated use: \'%s\'", target_types
[x
]->name
);
5473 if (target_types
[x
] == NULL
) {
5474 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
5475 for (x
= 0 ; target_types
[x
] ; x
++) {
5476 if (target_types
[x
+ 1]) {
5477 Jim_AppendStrings(goi
->interp
,
5478 Jim_GetResult(goi
->interp
),
5479 target_types
[x
]->name
,
5482 Jim_AppendStrings(goi
->interp
,
5483 Jim_GetResult(goi
->interp
),
5485 target_types
[x
]->name
, NULL
);
5492 target
= calloc(1, sizeof(struct target
));
5494 LOG_ERROR("Out of memory");
5498 /* set target number */
5499 target
->target_number
= new_target_number();
5501 /* allocate memory for each unique target type */
5502 target
->type
= malloc(sizeof(struct target_type
));
5503 if (!target
->type
) {
5504 LOG_ERROR("Out of memory");
5509 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
5511 /* will be set by "-endian" */
5512 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5514 /* default to first core, override with -coreid */
5517 target
->working_area
= 0x0;
5518 target
->working_area_size
= 0x0;
5519 target
->working_areas
= NULL
;
5520 target
->backup_working_area
= 0;
5522 target
->state
= TARGET_UNKNOWN
;
5523 target
->debug_reason
= DBG_REASON_UNDEFINED
;
5524 target
->reg_cache
= NULL
;
5525 target
->breakpoints
= NULL
;
5526 target
->watchpoints
= NULL
;
5527 target
->next
= NULL
;
5528 target
->arch_info
= NULL
;
5530 target
->verbose_halt_msg
= true;
5532 target
->halt_issued
= false;
5534 /* initialize trace information */
5535 target
->trace_info
= calloc(1, sizeof(struct trace
));
5536 if (!target
->trace_info
) {
5537 LOG_ERROR("Out of memory");
5543 target
->dbgmsg
= NULL
;
5544 target
->dbg_msg_enabled
= 0;
5546 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5548 target
->rtos
= NULL
;
5549 target
->rtos_auto_detect
= false;
5551 target
->gdb_port_override
= NULL
;
5553 /* Do the rest as "configure" options */
5554 goi
->isconfigure
= 1;
5555 e
= target_configure(goi
, target
);
5558 if (target
->has_dap
) {
5559 if (!target
->dap_configured
) {
5560 Jim_SetResultString(goi
->interp
, "-dap ?name? required when creating target", -1);
5564 if (!target
->tap_configured
) {
5565 Jim_SetResultString(goi
->interp
, "-chain-position ?name? required when creating target", -1);
5569 /* tap must be set after target was configured */
5570 if (target
->tap
== NULL
)
5575 rtos_destroy(target
);
5576 free(target
->gdb_port_override
);
5577 free(target
->trace_info
);
5583 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
5584 /* default endian to little if not specified */
5585 target
->endianness
= TARGET_LITTLE_ENDIAN
;
5588 cp
= Jim_GetString(new_cmd
, NULL
);
5589 target
->cmd_name
= strdup(cp
);
5590 if (!target
->cmd_name
) {
5591 LOG_ERROR("Out of memory");
5592 rtos_destroy(target
);
5593 free(target
->gdb_port_override
);
5594 free(target
->trace_info
);
5600 if (target
->type
->target_create
) {
5601 e
= (*(target
->type
->target_create
))(target
, goi
->interp
);
5602 if (e
!= ERROR_OK
) {
5603 LOG_DEBUG("target_create failed");
5604 free(target
->cmd_name
);
5605 rtos_destroy(target
);
5606 free(target
->gdb_port_override
);
5607 free(target
->trace_info
);
5614 /* create the target specific commands */
5615 if (target
->type
->commands
) {
5616 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
5618 LOG_ERROR("unable to register '%s' commands", cp
);
5621 /* now - create the new target name command */
5622 const struct command_registration target_subcommands
[] = {
5624 .chain
= target_instance_command_handlers
,
5627 .chain
= target
->type
->commands
,
5629 COMMAND_REGISTRATION_DONE
5631 const struct command_registration target_commands
[] = {
5634 .mode
= COMMAND_ANY
,
5635 .help
= "target command group",
5637 .chain
= target_subcommands
,
5639 COMMAND_REGISTRATION_DONE
5641 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
5642 if (e
!= ERROR_OK
) {
5643 if (target
->type
->deinit_target
)
5644 target
->type
->deinit_target(target
);
5645 free(target
->cmd_name
);
5646 rtos_destroy(target
);
5647 free(target
->gdb_port_override
);
5648 free(target
->trace_info
);
5654 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
5656 command_set_handler_data(c
, target
);
5658 /* append to end of list */
5659 append_to_list_all_targets(target
);
5661 cmd_ctx
->current_target
= target
;
5665 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5668 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5671 struct command_context
*cmd_ctx
= current_command_context(interp
);
5672 assert(cmd_ctx
!= NULL
);
5674 Jim_SetResultString(interp
, target_name(get_current_target(cmd_ctx
)), -1);
5678 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5681 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5684 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5685 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++) {
5686 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5687 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
5692 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5695 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5698 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5699 struct target
*target
= all_targets
;
5701 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5702 Jim_NewStringObj(interp
, target_name(target
), -1));
5703 target
= target
->next
;
5708 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5711 const char *targetname
;
5713 struct target
*target
= (struct target
*) NULL
;
5714 struct target_list
*head
, *curr
, *new;
5715 curr
= (struct target_list
*) NULL
;
5716 head
= (struct target_list
*) NULL
;
5719 LOG_DEBUG("%d", argc
);
5720 /* argv[1] = target to associate in smp
5721 * argv[2] = target to associate in smp
5725 for (i
= 1; i
< argc
; i
++) {
5727 targetname
= Jim_GetString(argv
[i
], &len
);
5728 target
= get_target(targetname
);
5729 LOG_DEBUG("%s ", targetname
);
5731 new = malloc(sizeof(struct target_list
));
5732 new->target
= target
;
5733 new->next
= (struct target_list
*)NULL
;
5734 if (head
== (struct target_list
*)NULL
) {
5743 /* now parse the list of cpu and put the target in smp mode*/
5746 while (curr
!= (struct target_list
*)NULL
) {
5747 target
= curr
->target
;
5749 target
->head
= head
;
5753 if (target
&& target
->rtos
)
5754 retval
= rtos_smp_init(head
->target
);
5760 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5763 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5765 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5766 "<name> <target_type> [<target_options> ...]");
5769 return target_create(&goi
);
5772 static const struct command_registration target_subcommand_handlers
[] = {
5775 .mode
= COMMAND_CONFIG
,
5776 .handler
= handle_target_init_command
,
5777 .help
= "initialize targets",
5782 .mode
= COMMAND_CONFIG
,
5783 .jim_handler
= jim_target_create
,
5784 .usage
= "name type '-chain-position' name [options ...]",
5785 .help
= "Creates and selects a new target",
5789 .mode
= COMMAND_ANY
,
5790 .jim_handler
= jim_target_current
,
5791 .help
= "Returns the currently selected target",
5795 .mode
= COMMAND_ANY
,
5796 .jim_handler
= jim_target_types
,
5797 .help
= "Returns the available target types as "
5798 "a list of strings",
5802 .mode
= COMMAND_ANY
,
5803 .jim_handler
= jim_target_names
,
5804 .help
= "Returns the names of all targets as a list of strings",
5808 .mode
= COMMAND_ANY
,
5809 .jim_handler
= jim_target_smp
,
5810 .usage
= "targetname1 targetname2 ...",
5811 .help
= "gather several target in a smp list"
5814 COMMAND_REGISTRATION_DONE
5818 target_addr_t address
;
5824 static int fastload_num
;
5825 static struct FastLoad
*fastload
;
5827 static void free_fastload(void)
5829 if (fastload
!= NULL
) {
5830 for (int i
= 0; i
< fastload_num
; i
++)
5831 free(fastload
[i
].data
);
5837 COMMAND_HANDLER(handle_fast_load_image_command
)
5841 uint32_t image_size
;
5842 target_addr_t min_address
= 0;
5843 target_addr_t max_address
= -1;
5848 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
5849 &image
, &min_address
, &max_address
);
5850 if (ERROR_OK
!= retval
)
5853 struct duration bench
;
5854 duration_start(&bench
);
5856 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5857 if (retval
!= ERROR_OK
)
5862 fastload_num
= image
.num_sections
;
5863 fastload
= malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5864 if (fastload
== NULL
) {
5865 command_print(CMD
, "out of memory");
5866 image_close(&image
);
5869 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5870 for (i
= 0; i
< image
.num_sections
; i
++) {
5871 buffer
= malloc(image
.sections
[i
].size
);
5872 if (buffer
== NULL
) {
5873 command_print(CMD
, "error allocating buffer for section (%d bytes)",
5874 (int)(image
.sections
[i
].size
));
5875 retval
= ERROR_FAIL
;
5879 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
5880 if (retval
!= ERROR_OK
) {
5885 uint32_t offset
= 0;
5886 uint32_t length
= buf_cnt
;
5888 /* DANGER!!! beware of unsigned comparison here!!! */
5890 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
5891 (image
.sections
[i
].base_address
< max_address
)) {
5892 if (image
.sections
[i
].base_address
< min_address
) {
5893 /* clip addresses below */
5894 offset
+= min_address
-image
.sections
[i
].base_address
;
5898 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5899 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5901 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5902 fastload
[i
].data
= malloc(length
);
5903 if (fastload
[i
].data
== NULL
) {
5905 command_print(CMD
, "error allocating buffer for section (%" PRIu32
" bytes)",
5907 retval
= ERROR_FAIL
;
5910 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5911 fastload
[i
].length
= length
;
5913 image_size
+= length
;
5914 command_print(CMD
, "%u bytes written at address 0x%8.8x",
5915 (unsigned int)length
,
5916 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
5922 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
5923 command_print(CMD
, "Loaded %" PRIu32
" bytes "
5924 "in %fs (%0.3f KiB/s)", image_size
,
5925 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
5928 "WARNING: image has not been loaded to target!"
5929 "You can issue a 'fast_load' to finish loading.");
5932 image_close(&image
);
5934 if (retval
!= ERROR_OK
)
5940 COMMAND_HANDLER(handle_fast_load_command
)
5943 return ERROR_COMMAND_SYNTAX_ERROR
;
5944 if (fastload
== NULL
) {
5945 LOG_ERROR("No image in memory");
5949 int64_t ms
= timeval_ms();
5951 int retval
= ERROR_OK
;
5952 for (i
= 0; i
< fastload_num
; i
++) {
5953 struct target
*target
= get_current_target(CMD_CTX
);
5954 command_print(CMD
, "Write to 0x%08x, length 0x%08x",
5955 (unsigned int)(fastload
[i
].address
),
5956 (unsigned int)(fastload
[i
].length
));
5957 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5958 if (retval
!= ERROR_OK
)
5960 size
+= fastload
[i
].length
;
5962 if (retval
== ERROR_OK
) {
5963 int64_t after
= timeval_ms();
5964 command_print(CMD
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5969 static const struct command_registration target_command_handlers
[] = {
5972 .handler
= handle_targets_command
,
5973 .mode
= COMMAND_ANY
,
5974 .help
= "change current default target (one parameter) "
5975 "or prints table of all targets (no parameters)",
5976 .usage
= "[target]",
5980 .mode
= COMMAND_CONFIG
,
5981 .help
= "configure target",
5982 .chain
= target_subcommand_handlers
,
5985 COMMAND_REGISTRATION_DONE
5988 int target_register_commands(struct command_context
*cmd_ctx
)
5990 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5993 static bool target_reset_nag
= true;
5995 bool get_target_reset_nag(void)
5997 return target_reset_nag
;
6000 COMMAND_HANDLER(handle_target_reset_nag
)
6002 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
6003 &target_reset_nag
, "Nag after each reset about options to improve "
6007 COMMAND_HANDLER(handle_ps_command
)
6009 struct target
*target
= get_current_target(CMD_CTX
);
6011 if (target
->state
!= TARGET_HALTED
) {
6012 LOG_INFO("target not halted !!");
6016 if ((target
->rtos
) && (target
->rtos
->type
)
6017 && (target
->rtos
->type
->ps_command
)) {
6018 display
= target
->rtos
->type
->ps_command(target
);
6019 command_print(CMD
, "%s", display
);
6024 return ERROR_TARGET_FAILURE
;
6028 static void binprint(struct command_invocation
*cmd
, const char *text
, const uint8_t *buf
, int size
)
6031 command_print_sameline(cmd
, "%s", text
);
6032 for (int i
= 0; i
< size
; i
++)
6033 command_print_sameline(cmd
, " %02x", buf
[i
]);
6034 command_print(cmd
, " ");
6037 COMMAND_HANDLER(handle_test_mem_access_command
)
6039 struct target
*target
= get_current_target(CMD_CTX
);
6041 int retval
= ERROR_OK
;
6043 if (target
->state
!= TARGET_HALTED
) {
6044 LOG_INFO("target not halted !!");
6049 return ERROR_COMMAND_SYNTAX_ERROR
;
6051 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], test_size
);
6054 size_t num_bytes
= test_size
+ 4;
6056 struct working_area
*wa
= NULL
;
6057 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6058 if (retval
!= ERROR_OK
) {
6059 LOG_ERROR("Not enough working area");
6063 uint8_t *test_pattern
= malloc(num_bytes
);
6065 for (size_t i
= 0; i
< num_bytes
; i
++)
6066 test_pattern
[i
] = rand();
6068 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6069 if (retval
!= ERROR_OK
) {
6070 LOG_ERROR("Test pattern write failed");
6074 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6075 for (int size
= 1; size
<= 4; size
*= 2) {
6076 for (int offset
= 0; offset
< 4; offset
++) {
6077 uint32_t count
= test_size
/ size
;
6078 size_t host_bufsiz
= (count
+ 2) * size
+ host_offset
;
6079 uint8_t *read_ref
= malloc(host_bufsiz
);
6080 uint8_t *read_buf
= malloc(host_bufsiz
);
6082 for (size_t i
= 0; i
< host_bufsiz
; i
++) {
6083 read_ref
[i
] = rand();
6084 read_buf
[i
] = read_ref
[i
];
6086 command_print_sameline(CMD
,
6087 "Test read %" PRIu32
" x %d @ %d to %saligned buffer: ", count
,
6088 size
, offset
, host_offset
? "un" : "");
6090 struct duration bench
;
6091 duration_start(&bench
);
6093 retval
= target_read_memory(target
, wa
->address
+ offset
, size
, count
,
6094 read_buf
+ size
+ host_offset
);
6096 duration_measure(&bench
);
6098 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6099 command_print(CMD
, "Unsupported alignment");
6101 } else if (retval
!= ERROR_OK
) {
6102 command_print(CMD
, "Memory read failed");
6106 /* replay on host */
6107 memcpy(read_ref
+ size
+ host_offset
, test_pattern
+ offset
, count
* size
);
6110 int result
= memcmp(read_ref
, read_buf
, host_bufsiz
);
6112 command_print(CMD
, "Pass in %fs (%0.3f KiB/s)",
6113 duration_elapsed(&bench
),
6114 duration_kbps(&bench
, count
* size
));
6116 command_print(CMD
, "Compare failed");
6117 binprint(CMD
, "ref:", read_ref
, host_bufsiz
);
6118 binprint(CMD
, "buf:", read_buf
, host_bufsiz
);
6131 target_free_working_area(target
, wa
);
6134 num_bytes
= test_size
+ 4 + 4 + 4;
6136 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6137 if (retval
!= ERROR_OK
) {
6138 LOG_ERROR("Not enough working area");
6142 test_pattern
= malloc(num_bytes
);
6144 for (size_t i
= 0; i
< num_bytes
; i
++)
6145 test_pattern
[i
] = rand();
6147 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6148 for (int size
= 1; size
<= 4; size
*= 2) {
6149 for (int offset
= 0; offset
< 4; offset
++) {
6150 uint32_t count
= test_size
/ size
;
6151 size_t host_bufsiz
= count
* size
+ host_offset
;
6152 uint8_t *read_ref
= malloc(num_bytes
);
6153 uint8_t *read_buf
= malloc(num_bytes
);
6154 uint8_t *write_buf
= malloc(host_bufsiz
);
6156 for (size_t i
= 0; i
< host_bufsiz
; i
++)
6157 write_buf
[i
] = rand();
6158 command_print_sameline(CMD
,
6159 "Test write %" PRIu32
" x %d @ %d from %saligned buffer: ", count
,
6160 size
, offset
, host_offset
? "un" : "");
6162 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6163 if (retval
!= ERROR_OK
) {
6164 command_print(CMD
, "Test pattern write failed");
6168 /* replay on host */
6169 memcpy(read_ref
, test_pattern
, num_bytes
);
6170 memcpy(read_ref
+ size
+ offset
, write_buf
+ host_offset
, count
* size
);
6172 struct duration bench
;
6173 duration_start(&bench
);
6175 retval
= target_write_memory(target
, wa
->address
+ size
+ offset
, size
, count
,
6176 write_buf
+ host_offset
);
6178 duration_measure(&bench
);
6180 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6181 command_print(CMD
, "Unsupported alignment");
6183 } else if (retval
!= ERROR_OK
) {
6184 command_print(CMD
, "Memory write failed");
6189 retval
= target_read_memory(target
, wa
->address
, 1, num_bytes
, read_buf
);
6190 if (retval
!= ERROR_OK
) {
6191 command_print(CMD
, "Test pattern write failed");
6196 int result
= memcmp(read_ref
, read_buf
, num_bytes
);
6198 command_print(CMD
, "Pass in %fs (%0.3f KiB/s)",
6199 duration_elapsed(&bench
),
6200 duration_kbps(&bench
, count
* size
));
6202 command_print(CMD
, "Compare failed");
6203 binprint(CMD
, "ref:", read_ref
, num_bytes
);
6204 binprint(CMD
, "buf:", read_buf
, num_bytes
);
6216 target_free_working_area(target
, wa
);
6220 static const struct command_registration target_exec_command_handlers
[] = {
6222 .name
= "fast_load_image",
6223 .handler
= handle_fast_load_image_command
,
6224 .mode
= COMMAND_ANY
,
6225 .help
= "Load image into server memory for later use by "
6226 "fast_load; primarily for profiling",
6227 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6228 "[min_address [max_length]]",
6231 .name
= "fast_load",
6232 .handler
= handle_fast_load_command
,
6233 .mode
= COMMAND_EXEC
,
6234 .help
= "loads active fast load image to current target "
6235 "- mainly for profiling purposes",
6240 .handler
= handle_profile_command
,
6241 .mode
= COMMAND_EXEC
,
6242 .usage
= "seconds filename [start end]",
6243 .help
= "profiling samples the CPU PC",
6245 /** @todo don't register virt2phys() unless target supports it */
6247 .name
= "virt2phys",
6248 .handler
= handle_virt2phys_command
,
6249 .mode
= COMMAND_ANY
,
6250 .help
= "translate a virtual address into a physical address",
6251 .usage
= "virtual_address",
6255 .handler
= handle_reg_command
,
6256 .mode
= COMMAND_EXEC
,
6257 .help
= "display (reread from target with \"force\") or set a register; "
6258 "with no arguments, displays all registers and their values",
6259 .usage
= "[(register_number|register_name) [(value|'force')]]",
6263 .handler
= handle_poll_command
,
6264 .mode
= COMMAND_EXEC
,
6265 .help
= "poll target state; or reconfigure background polling",
6266 .usage
= "['on'|'off']",
6269 .name
= "wait_halt",
6270 .handler
= handle_wait_halt_command
,
6271 .mode
= COMMAND_EXEC
,
6272 .help
= "wait up to the specified number of milliseconds "
6273 "(default 5000) for a previously requested halt",
6274 .usage
= "[milliseconds]",
6278 .handler
= handle_halt_command
,
6279 .mode
= COMMAND_EXEC
,
6280 .help
= "request target to halt, then wait up to the specified "
6281 "number of milliseconds (default 5000) for it to complete",
6282 .usage
= "[milliseconds]",
6286 .handler
= handle_resume_command
,
6287 .mode
= COMMAND_EXEC
,
6288 .help
= "resume target execution from current PC or address",
6289 .usage
= "[address]",
6293 .handler
= handle_reset_command
,
6294 .mode
= COMMAND_EXEC
,
6295 .usage
= "[run|halt|init]",
6296 .help
= "Reset all targets into the specified mode. "
6297 "Default reset mode is run, if not given.",
6300 .name
= "soft_reset_halt",
6301 .handler
= handle_soft_reset_halt_command
,
6302 .mode
= COMMAND_EXEC
,
6304 .help
= "halt the target and do a soft reset",
6308 .handler
= handle_step_command
,
6309 .mode
= COMMAND_EXEC
,
6310 .help
= "step one instruction from current PC or address",
6311 .usage
= "[address]",
6315 .handler
= handle_md_command
,
6316 .mode
= COMMAND_EXEC
,
6317 .help
= "display memory double-words",
6318 .usage
= "['phys'] address [count]",
6322 .handler
= handle_md_command
,
6323 .mode
= COMMAND_EXEC
,
6324 .help
= "display memory words",
6325 .usage
= "['phys'] address [count]",
6329 .handler
= handle_md_command
,
6330 .mode
= COMMAND_EXEC
,
6331 .help
= "display memory half-words",
6332 .usage
= "['phys'] address [count]",
6336 .handler
= handle_md_command
,
6337 .mode
= COMMAND_EXEC
,
6338 .help
= "display memory bytes",
6339 .usage
= "['phys'] address [count]",
6343 .handler
= handle_mw_command
,
6344 .mode
= COMMAND_EXEC
,
6345 .help
= "write memory double-word",
6346 .usage
= "['phys'] address value [count]",
6350 .handler
= handle_mw_command
,
6351 .mode
= COMMAND_EXEC
,
6352 .help
= "write memory word",
6353 .usage
= "['phys'] address value [count]",
6357 .handler
= handle_mw_command
,
6358 .mode
= COMMAND_EXEC
,
6359 .help
= "write memory half-word",
6360 .usage
= "['phys'] address value [count]",
6364 .handler
= handle_mw_command
,
6365 .mode
= COMMAND_EXEC
,
6366 .help
= "write memory byte",
6367 .usage
= "['phys'] address value [count]",
6371 .handler
= handle_bp_command
,
6372 .mode
= COMMAND_EXEC
,
6373 .help
= "list or set hardware or software breakpoint",
6374 .usage
= "[<address> [<asid>] <length> ['hw'|'hw_ctx']]",
6378 .handler
= handle_rbp_command
,
6379 .mode
= COMMAND_EXEC
,
6380 .help
= "remove breakpoint",
6381 .usage
= "'all' | address",
6385 .handler
= handle_wp_command
,
6386 .mode
= COMMAND_EXEC
,
6387 .help
= "list (no params) or create watchpoints",
6388 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
6392 .handler
= handle_rwp_command
,
6393 .mode
= COMMAND_EXEC
,
6394 .help
= "remove watchpoint",
6398 .name
= "load_image",
6399 .handler
= handle_load_image_command
,
6400 .mode
= COMMAND_EXEC
,
6401 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6402 "[min_address] [max_length]",
6405 .name
= "dump_image",
6406 .handler
= handle_dump_image_command
,
6407 .mode
= COMMAND_EXEC
,
6408 .usage
= "filename address size",
6411 .name
= "verify_image_checksum",
6412 .handler
= handle_verify_image_checksum_command
,
6413 .mode
= COMMAND_EXEC
,
6414 .usage
= "filename [offset [type]]",
6417 .name
= "verify_image",
6418 .handler
= handle_verify_image_command
,
6419 .mode
= COMMAND_EXEC
,
6420 .usage
= "filename [offset [type]]",
6423 .name
= "test_image",
6424 .handler
= handle_test_image_command
,
6425 .mode
= COMMAND_EXEC
,
6426 .usage
= "filename [offset [type]]",
6429 .name
= "mem2array",
6430 .mode
= COMMAND_EXEC
,
6431 .jim_handler
= jim_mem2array
,
6432 .help
= "read 8/16/32 bit memory and return as a TCL array "
6433 "for script processing",
6434 .usage
= "arrayname bitwidth address count",
6437 .name
= "array2mem",
6438 .mode
= COMMAND_EXEC
,
6439 .jim_handler
= jim_array2mem
,
6440 .help
= "convert a TCL array to memory locations "
6441 "and write the 8/16/32 bit values",
6442 .usage
= "arrayname bitwidth address count",
6445 .name
= "reset_nag",
6446 .handler
= handle_target_reset_nag
,
6447 .mode
= COMMAND_ANY
,
6448 .help
= "Nag after each reset about options that could have been "
6449 "enabled to improve performance. ",
6450 .usage
= "['enable'|'disable']",
6454 .handler
= handle_ps_command
,
6455 .mode
= COMMAND_EXEC
,
6456 .help
= "list all tasks ",
6460 .name
= "test_mem_access",
6461 .handler
= handle_test_mem_access_command
,
6462 .mode
= COMMAND_EXEC
,
6463 .help
= "Test the target's memory access functions",
6467 COMMAND_REGISTRATION_DONE
6469 static int target_register_user_commands(struct command_context
*cmd_ctx
)
6471 int retval
= ERROR_OK
;
6472 retval
= target_request_register_commands(cmd_ctx
);
6473 if (retval
!= ERROR_OK
)
6476 retval
= trace_register_commands(cmd_ctx
);
6477 if (retval
!= ERROR_OK
)
6481 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);