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
[] = {
158 struct target
*all_targets
;
159 static struct target_event_callback
*target_event_callbacks
;
160 static struct target_timer_callback
*target_timer_callbacks
;
161 LIST_HEAD(target_reset_callback_list
);
162 LIST_HEAD(target_trace_callback_list
);
163 static const int polling_interval
= 100;
165 static const Jim_Nvp nvp_assert
[] = {
166 { .name
= "assert", NVP_ASSERT
},
167 { .name
= "deassert", NVP_DEASSERT
},
168 { .name
= "T", NVP_ASSERT
},
169 { .name
= "F", NVP_DEASSERT
},
170 { .name
= "t", NVP_ASSERT
},
171 { .name
= "f", NVP_DEASSERT
},
172 { .name
= NULL
, .value
= -1 }
175 static const Jim_Nvp nvp_error_target
[] = {
176 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
177 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
178 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
179 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
180 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
181 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
182 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
183 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
184 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
185 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
186 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
187 { .value
= -1, .name
= NULL
}
190 static const char *target_strerror_safe(int err
)
194 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
201 static const Jim_Nvp nvp_target_event
[] = {
203 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
204 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
205 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
206 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
207 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
209 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
210 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
212 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
213 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
214 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
215 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
216 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
217 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
218 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
219 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
221 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
222 { .value
= TARGET_EVENT_EXAMINE_FAIL
, .name
= "examine-fail" },
223 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
225 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
226 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
228 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
229 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
231 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
232 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
234 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
235 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
237 { .value
= TARGET_EVENT_TRACE_CONFIG
, .name
= "trace-config" },
239 { .name
= NULL
, .value
= -1 }
242 static const Jim_Nvp nvp_target_state
[] = {
243 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
244 { .name
= "running", .value
= TARGET_RUNNING
},
245 { .name
= "halted", .value
= TARGET_HALTED
},
246 { .name
= "reset", .value
= TARGET_RESET
},
247 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
248 { .name
= NULL
, .value
= -1 },
251 static const Jim_Nvp nvp_target_debug_reason
[] = {
252 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
253 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
254 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
255 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
256 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
257 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
258 { .name
= "program-exit" , .value
= DBG_REASON_EXIT
},
259 { .name
= "exception-catch" , .value
= DBG_REASON_EXC_CATCH
},
260 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
261 { .name
= NULL
, .value
= -1 },
264 static const Jim_Nvp nvp_target_endian
[] = {
265 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
266 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
267 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
268 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
269 { .name
= NULL
, .value
= -1 },
272 static const Jim_Nvp nvp_reset_modes
[] = {
273 { .name
= "unknown", .value
= RESET_UNKNOWN
},
274 { .name
= "run" , .value
= RESET_RUN
},
275 { .name
= "halt" , .value
= RESET_HALT
},
276 { .name
= "init" , .value
= RESET_INIT
},
277 { .name
= NULL
, .value
= -1 },
280 const char *debug_reason_name(struct target
*t
)
284 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
285 t
->debug_reason
)->name
;
287 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
288 cp
= "(*BUG*unknown*BUG*)";
293 const char *target_state_name(struct target
*t
)
296 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
298 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
299 cp
= "(*BUG*unknown*BUG*)";
302 if (!target_was_examined(t
) && t
->defer_examine
)
303 cp
= "examine deferred";
308 const char *target_event_name(enum target_event event
)
311 cp
= Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
;
313 LOG_ERROR("Invalid target event: %d", (int)(event
));
314 cp
= "(*BUG*unknown*BUG*)";
319 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
322 cp
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
;
324 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
325 cp
= "(*BUG*unknown*BUG*)";
330 /* determine the number of the new target */
331 static int new_target_number(void)
336 /* number is 0 based */
340 if (x
< t
->target_number
)
341 x
= t
->target_number
;
347 /* read a uint64_t from a buffer in target memory endianness */
348 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
350 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
351 return le_to_h_u64(buffer
);
353 return be_to_h_u64(buffer
);
356 /* read a uint32_t from a buffer in target memory endianness */
357 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
359 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
360 return le_to_h_u32(buffer
);
362 return be_to_h_u32(buffer
);
365 /* read a uint24_t from a buffer in target memory endianness */
366 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
368 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
369 return le_to_h_u24(buffer
);
371 return be_to_h_u24(buffer
);
374 /* read a uint16_t from a buffer in target memory endianness */
375 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
377 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
378 return le_to_h_u16(buffer
);
380 return be_to_h_u16(buffer
);
383 /* write a uint64_t to a buffer in target memory endianness */
384 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
386 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
387 h_u64_to_le(buffer
, value
);
389 h_u64_to_be(buffer
, value
);
392 /* write a uint32_t to a buffer in target memory endianness */
393 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
395 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
396 h_u32_to_le(buffer
, value
);
398 h_u32_to_be(buffer
, value
);
401 /* write a uint24_t to a buffer in target memory endianness */
402 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
404 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
405 h_u24_to_le(buffer
, value
);
407 h_u24_to_be(buffer
, value
);
410 /* write a uint16_t to a buffer in target memory endianness */
411 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
413 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
414 h_u16_to_le(buffer
, value
);
416 h_u16_to_be(buffer
, value
);
419 /* write a uint8_t to a buffer in target memory endianness */
420 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
425 /* write a uint64_t array to a buffer in target memory endianness */
426 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
429 for (i
= 0; i
< count
; i
++)
430 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
433 /* write a uint32_t array to a buffer in target memory endianness */
434 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
437 for (i
= 0; i
< count
; i
++)
438 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
441 /* write a uint16_t array to a buffer in target memory endianness */
442 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
445 for (i
= 0; i
< count
; i
++)
446 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
449 /* write a uint64_t array to a buffer in target memory endianness */
450 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
453 for (i
= 0; i
< count
; i
++)
454 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
457 /* write a uint32_t array to a buffer in target memory endianness */
458 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
461 for (i
= 0; i
< count
; i
++)
462 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
465 /* write a uint16_t array to a buffer in target memory endianness */
466 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
469 for (i
= 0; i
< count
; i
++)
470 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
473 /* return a pointer to a configured target; id is name or number */
474 struct target
*get_target(const char *id
)
476 struct target
*target
;
478 /* try as tcltarget name */
479 for (target
= all_targets
; target
; target
= target
->next
) {
480 if (target_name(target
) == NULL
)
482 if (strcmp(id
, target_name(target
)) == 0)
486 /* It's OK to remove this fallback sometime after August 2010 or so */
488 /* no match, try as number */
490 if (parse_uint(id
, &num
) != ERROR_OK
)
493 for (target
= all_targets
; target
; target
= target
->next
) {
494 if (target
->target_number
== (int)num
) {
495 LOG_WARNING("use '%s' as target identifier, not '%u'",
496 target_name(target
), num
);
504 /* returns a pointer to the n-th configured target */
505 struct target
*get_target_by_num(int num
)
507 struct target
*target
= all_targets
;
510 if (target
->target_number
== num
)
512 target
= target
->next
;
518 struct target
*get_current_target(struct command_context
*cmd_ctx
)
520 struct target
*target
= get_current_target_or_null(cmd_ctx
);
522 if (target
== NULL
) {
523 LOG_ERROR("BUG: current_target out of bounds");
530 struct target
*get_current_target_or_null(struct command_context
*cmd_ctx
)
532 return cmd_ctx
->current_target_override
533 ? cmd_ctx
->current_target_override
534 : cmd_ctx
->current_target
;
537 int target_poll(struct target
*target
)
541 /* We can't poll until after examine */
542 if (!target_was_examined(target
)) {
543 /* Fail silently lest we pollute the log */
547 retval
= target
->type
->poll(target
);
548 if (retval
!= ERROR_OK
)
551 if (target
->halt_issued
) {
552 if (target
->state
== TARGET_HALTED
)
553 target
->halt_issued
= false;
555 int64_t t
= timeval_ms() - target
->halt_issued_time
;
556 if (t
> DEFAULT_HALT_TIMEOUT
) {
557 target
->halt_issued
= false;
558 LOG_INFO("Halt timed out, wake up GDB.");
559 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
567 int target_halt(struct target
*target
)
570 /* We can't poll until after examine */
571 if (!target_was_examined(target
)) {
572 LOG_ERROR("Target not examined yet");
576 retval
= target
->type
->halt(target
);
577 if (retval
!= ERROR_OK
)
580 target
->halt_issued
= true;
581 target
->halt_issued_time
= timeval_ms();
587 * Make the target (re)start executing using its saved execution
588 * context (possibly with some modifications).
590 * @param target Which target should start executing.
591 * @param current True to use the target's saved program counter instead
592 * of the address parameter
593 * @param address Optionally used as the program counter.
594 * @param handle_breakpoints True iff breakpoints at the resumption PC
595 * should be skipped. (For example, maybe execution was stopped by
596 * such a breakpoint, in which case it would be counterprodutive to
598 * @param debug_execution False if all working areas allocated by OpenOCD
599 * should be released and/or restored to their original contents.
600 * (This would for example be true to run some downloaded "helper"
601 * algorithm code, which resides in one such working buffer and uses
602 * another for data storage.)
604 * @todo Resolve the ambiguity about what the "debug_execution" flag
605 * signifies. For example, Target implementations don't agree on how
606 * it relates to invalidation of the register cache, or to whether
607 * breakpoints and watchpoints should be enabled. (It would seem wrong
608 * to enable breakpoints when running downloaded "helper" algorithms
609 * (debug_execution true), since the breakpoints would be set to match
610 * target firmware being debugged, not the helper algorithm.... and
611 * enabling them could cause such helpers to malfunction (for example,
612 * by overwriting data with a breakpoint instruction. On the other
613 * hand the infrastructure for running such helpers might use this
614 * procedure but rely on hardware breakpoint to detect termination.)
616 int target_resume(struct target
*target
, int current
, target_addr_t address
,
617 int handle_breakpoints
, int debug_execution
)
621 /* We can't poll until after examine */
622 if (!target_was_examined(target
)) {
623 LOG_ERROR("Target not examined yet");
627 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
629 /* note that resume *must* be asynchronous. The CPU can halt before
630 * we poll. The CPU can even halt at the current PC as a result of
631 * a software breakpoint being inserted by (a bug?) the application.
633 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
634 if (retval
!= ERROR_OK
)
637 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
642 static int target_process_reset(struct command_invocation
*cmd
, enum target_reset_mode reset_mode
)
647 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
648 if (n
->name
== NULL
) {
649 LOG_ERROR("invalid reset mode");
653 struct target
*target
;
654 for (target
= all_targets
; target
; target
= target
->next
)
655 target_call_reset_callbacks(target
, reset_mode
);
657 /* disable polling during reset to make reset event scripts
658 * more predictable, i.e. dr/irscan & pathmove in events will
659 * not have JTAG operations injected into the middle of a sequence.
661 bool save_poll
= jtag_poll_get_enabled();
663 jtag_poll_set_enabled(false);
665 sprintf(buf
, "ocd_process_reset %s", n
->name
);
666 retval
= Jim_Eval(cmd
->ctx
->interp
, buf
);
668 jtag_poll_set_enabled(save_poll
);
670 if (retval
!= JIM_OK
) {
671 Jim_MakeErrorMessage(cmd
->ctx
->interp
);
672 command_print(cmd
, "%s", Jim_GetString(Jim_GetResult(cmd
->ctx
->interp
), NULL
));
676 /* We want any events to be processed before the prompt */
677 retval
= target_call_timer_callbacks_now();
679 for (target
= all_targets
; target
; target
= target
->next
) {
680 target
->type
->check_reset(target
);
681 target
->running_alg
= false;
687 static int identity_virt2phys(struct target
*target
,
688 target_addr_t
virtual, target_addr_t
*physical
)
694 static int no_mmu(struct target
*target
, int *enabled
)
700 static int default_examine(struct target
*target
)
702 target_set_examined(target
);
706 /* no check by default */
707 static int default_check_reset(struct target
*target
)
712 /* Equvivalent Tcl code arp_examine_one is in src/target/startup.tcl
714 int target_examine_one(struct target
*target
)
716 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
718 int retval
= target
->type
->examine(target
);
719 if (retval
!= ERROR_OK
) {
720 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_FAIL
);
724 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
729 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
731 struct target
*target
= priv
;
733 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
736 jtag_unregister_event_callback(jtag_enable_callback
, target
);
738 return target_examine_one(target
);
741 /* Targets that correctly implement init + examine, i.e.
742 * no communication with target during init:
746 int target_examine(void)
748 int retval
= ERROR_OK
;
749 struct target
*target
;
751 for (target
= all_targets
; target
; target
= target
->next
) {
752 /* defer examination, but don't skip it */
753 if (!target
->tap
->enabled
) {
754 jtag_register_event_callback(jtag_enable_callback
,
759 if (target
->defer_examine
)
762 retval
= target_examine_one(target
);
763 if (retval
!= ERROR_OK
)
769 const char *target_type_name(struct target
*target
)
771 return target
->type
->name
;
774 static int target_soft_reset_halt(struct target
*target
)
776 if (!target_was_examined(target
)) {
777 LOG_ERROR("Target not examined yet");
780 if (!target
->type
->soft_reset_halt
) {
781 LOG_ERROR("Target %s does not support soft_reset_halt",
782 target_name(target
));
785 return target
->type
->soft_reset_halt(target
);
789 * Downloads a target-specific native code algorithm to the target,
790 * and executes it. * Note that some targets may need to set up, enable,
791 * and tear down a breakpoint (hard or * soft) to detect algorithm
792 * termination, while others may support lower overhead schemes where
793 * soft breakpoints embedded in the algorithm automatically terminate the
796 * @param target used to run the algorithm
797 * @param arch_info target-specific description of the algorithm.
799 int target_run_algorithm(struct target
*target
,
800 int num_mem_params
, struct mem_param
*mem_params
,
801 int num_reg_params
, struct reg_param
*reg_param
,
802 uint32_t entry_point
, uint32_t exit_point
,
803 int timeout_ms
, void *arch_info
)
805 int retval
= ERROR_FAIL
;
807 if (!target_was_examined(target
)) {
808 LOG_ERROR("Target not examined yet");
811 if (!target
->type
->run_algorithm
) {
812 LOG_ERROR("Target type '%s' does not support %s",
813 target_type_name(target
), __func__
);
817 target
->running_alg
= true;
818 retval
= target
->type
->run_algorithm(target
,
819 num_mem_params
, mem_params
,
820 num_reg_params
, reg_param
,
821 entry_point
, exit_point
, timeout_ms
, arch_info
);
822 target
->running_alg
= false;
829 * Executes a target-specific native code algorithm and leaves it running.
831 * @param target used to run the algorithm
832 * @param arch_info target-specific description of the algorithm.
834 int target_start_algorithm(struct target
*target
,
835 int num_mem_params
, struct mem_param
*mem_params
,
836 int num_reg_params
, struct reg_param
*reg_params
,
837 uint32_t entry_point
, uint32_t exit_point
,
840 int retval
= ERROR_FAIL
;
842 if (!target_was_examined(target
)) {
843 LOG_ERROR("Target not examined yet");
846 if (!target
->type
->start_algorithm
) {
847 LOG_ERROR("Target type '%s' does not support %s",
848 target_type_name(target
), __func__
);
851 if (target
->running_alg
) {
852 LOG_ERROR("Target is already running an algorithm");
856 target
->running_alg
= true;
857 retval
= target
->type
->start_algorithm(target
,
858 num_mem_params
, mem_params
,
859 num_reg_params
, reg_params
,
860 entry_point
, exit_point
, arch_info
);
867 * Waits for an algorithm started with target_start_algorithm() to complete.
869 * @param target used to run the algorithm
870 * @param arch_info target-specific description of the algorithm.
872 int target_wait_algorithm(struct target
*target
,
873 int num_mem_params
, struct mem_param
*mem_params
,
874 int num_reg_params
, struct reg_param
*reg_params
,
875 uint32_t exit_point
, int timeout_ms
,
878 int retval
= ERROR_FAIL
;
880 if (!target
->type
->wait_algorithm
) {
881 LOG_ERROR("Target type '%s' does not support %s",
882 target_type_name(target
), __func__
);
885 if (!target
->running_alg
) {
886 LOG_ERROR("Target is not running an algorithm");
890 retval
= target
->type
->wait_algorithm(target
,
891 num_mem_params
, mem_params
,
892 num_reg_params
, reg_params
,
893 exit_point
, timeout_ms
, arch_info
);
894 if (retval
!= ERROR_TARGET_TIMEOUT
)
895 target
->running_alg
= false;
902 * Streams data to a circular buffer on target intended for consumption by code
903 * running asynchronously on target.
905 * This is intended for applications where target-specific native code runs
906 * on the target, receives data from the circular buffer, does something with
907 * it (most likely writing it to a flash memory), and advances the circular
910 * This assumes that the helper algorithm has already been loaded to the target,
911 * but has not been started yet. Given memory and register parameters are passed
914 * The buffer is defined by (buffer_start, buffer_size) arguments and has the
917 * [buffer_start + 0, buffer_start + 4):
918 * Write Pointer address (aka head). Written and updated by this
919 * routine when new data is written to the circular buffer.
920 * [buffer_start + 4, buffer_start + 8):
921 * Read Pointer address (aka tail). Updated by code running on the
922 * target after it consumes data.
923 * [buffer_start + 8, buffer_start + buffer_size):
924 * Circular buffer contents.
926 * See contrib/loaders/flash/stm32f1x.S for an example.
928 * @param target used to run the algorithm
929 * @param buffer address on the host where data to be sent is located
930 * @param count number of blocks to send
931 * @param block_size size in bytes of each block
932 * @param num_mem_params count of memory-based params to pass to algorithm
933 * @param mem_params memory-based params to pass to algorithm
934 * @param num_reg_params count of register-based params to pass to algorithm
935 * @param reg_params memory-based params to pass to algorithm
936 * @param buffer_start address on the target of the circular buffer structure
937 * @param buffer_size size of the circular buffer structure
938 * @param entry_point address on the target to execute to start the algorithm
939 * @param exit_point address at which to set a breakpoint to catch the
940 * end of the algorithm; can be 0 if target triggers a breakpoint itself
943 int target_run_flash_async_algorithm(struct target
*target
,
944 const uint8_t *buffer
, uint32_t count
, int block_size
,
945 int num_mem_params
, struct mem_param
*mem_params
,
946 int num_reg_params
, struct reg_param
*reg_params
,
947 uint32_t buffer_start
, uint32_t buffer_size
,
948 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
953 const uint8_t *buffer_orig
= buffer
;
955 /* Set up working area. First word is write pointer, second word is read pointer,
956 * rest is fifo data area. */
957 uint32_t wp_addr
= buffer_start
;
958 uint32_t rp_addr
= buffer_start
+ 4;
959 uint32_t fifo_start_addr
= buffer_start
+ 8;
960 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
962 uint32_t wp
= fifo_start_addr
;
963 uint32_t rp
= fifo_start_addr
;
965 /* validate block_size is 2^n */
966 assert(!block_size
|| !(block_size
& (block_size
- 1)));
968 retval
= target_write_u32(target
, wp_addr
, wp
);
969 if (retval
!= ERROR_OK
)
971 retval
= target_write_u32(target
, rp_addr
, rp
);
972 if (retval
!= ERROR_OK
)
975 /* Start up algorithm on target and let it idle while writing the first chunk */
976 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
977 num_reg_params
, reg_params
,
982 if (retval
!= ERROR_OK
) {
983 LOG_ERROR("error starting target flash write algorithm");
989 retval
= target_read_u32(target
, rp_addr
, &rp
);
990 if (retval
!= ERROR_OK
) {
991 LOG_ERROR("failed to get read pointer");
995 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
996 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
999 LOG_ERROR("flash write algorithm aborted by target");
1000 retval
= ERROR_FLASH_OPERATION_FAILED
;
1004 if (((rp
- fifo_start_addr
) & (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
1005 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
1009 /* Count the number of bytes available in the fifo without
1010 * crossing the wrap around. Make sure to not fill it completely,
1011 * because that would make wp == rp and that's the empty condition. */
1012 uint32_t thisrun_bytes
;
1014 thisrun_bytes
= rp
- wp
- block_size
;
1015 else if (rp
> fifo_start_addr
)
1016 thisrun_bytes
= fifo_end_addr
- wp
;
1018 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
1020 if (thisrun_bytes
== 0) {
1021 /* Throttle polling a bit if transfer is (much) faster than flash
1022 * programming. The exact delay shouldn't matter as long as it's
1023 * less than buffer size / flash speed. This is very unlikely to
1024 * run when using high latency connections such as USB. */
1027 /* to stop an infinite loop on some targets check and increment a timeout
1028 * this issue was observed on a stellaris using the new ICDI interface */
1029 if (timeout
++ >= 500) {
1030 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1031 return ERROR_FLASH_OPERATION_FAILED
;
1036 /* reset our timeout */
1039 /* Limit to the amount of data we actually want to write */
1040 if (thisrun_bytes
> count
* block_size
)
1041 thisrun_bytes
= count
* block_size
;
1043 /* Write data to fifo */
1044 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
1045 if (retval
!= ERROR_OK
)
1048 /* Update counters and wrap write pointer */
1049 buffer
+= thisrun_bytes
;
1050 count
-= thisrun_bytes
/ block_size
;
1051 wp
+= thisrun_bytes
;
1052 if (wp
>= fifo_end_addr
)
1053 wp
= fifo_start_addr
;
1055 /* Store updated write pointer to target */
1056 retval
= target_write_u32(target
, wp_addr
, wp
);
1057 if (retval
!= ERROR_OK
)
1060 /* Avoid GDB timeouts */
1064 if (retval
!= ERROR_OK
) {
1065 /* abort flash write algorithm on target */
1066 target_write_u32(target
, wp_addr
, 0);
1069 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1070 num_reg_params
, reg_params
,
1075 if (retval2
!= ERROR_OK
) {
1076 LOG_ERROR("error waiting for target flash write algorithm");
1080 if (retval
== ERROR_OK
) {
1081 /* check if algorithm set rp = 0 after fifo writer loop finished */
1082 retval
= target_read_u32(target
, rp_addr
, &rp
);
1083 if (retval
== ERROR_OK
&& rp
== 0) {
1084 LOG_ERROR("flash write algorithm aborted by target");
1085 retval
= ERROR_FLASH_OPERATION_FAILED
;
1092 int target_read_memory(struct target
*target
,
1093 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1095 if (!target_was_examined(target
)) {
1096 LOG_ERROR("Target not examined yet");
1099 if (!target
->type
->read_memory
) {
1100 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1103 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1106 int target_read_phys_memory(struct target
*target
,
1107 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1109 if (!target_was_examined(target
)) {
1110 LOG_ERROR("Target not examined yet");
1113 if (!target
->type
->read_phys_memory
) {
1114 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1117 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1120 int target_write_memory(struct target
*target
,
1121 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1123 if (!target_was_examined(target
)) {
1124 LOG_ERROR("Target not examined yet");
1127 if (!target
->type
->write_memory
) {
1128 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1131 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1134 int target_write_phys_memory(struct target
*target
,
1135 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1137 if (!target_was_examined(target
)) {
1138 LOG_ERROR("Target not examined yet");
1141 if (!target
->type
->write_phys_memory
) {
1142 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1145 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1148 int target_add_breakpoint(struct target
*target
,
1149 struct breakpoint
*breakpoint
)
1151 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1152 LOG_WARNING("target %s is not halted (add breakpoint)", target_name(target
));
1153 return ERROR_TARGET_NOT_HALTED
;
1155 return target
->type
->add_breakpoint(target
, breakpoint
);
1158 int target_add_context_breakpoint(struct target
*target
,
1159 struct breakpoint
*breakpoint
)
1161 if (target
->state
!= TARGET_HALTED
) {
1162 LOG_WARNING("target %s is not halted (add context breakpoint)", target_name(target
));
1163 return ERROR_TARGET_NOT_HALTED
;
1165 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1168 int target_add_hybrid_breakpoint(struct target
*target
,
1169 struct breakpoint
*breakpoint
)
1171 if (target
->state
!= TARGET_HALTED
) {
1172 LOG_WARNING("target %s is not halted (add hybrid breakpoint)", target_name(target
));
1173 return ERROR_TARGET_NOT_HALTED
;
1175 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1178 int target_remove_breakpoint(struct target
*target
,
1179 struct breakpoint
*breakpoint
)
1181 return target
->type
->remove_breakpoint(target
, breakpoint
);
1184 int target_add_watchpoint(struct target
*target
,
1185 struct watchpoint
*watchpoint
)
1187 if (target
->state
!= TARGET_HALTED
) {
1188 LOG_WARNING("target %s is not halted (add watchpoint)", target_name(target
));
1189 return ERROR_TARGET_NOT_HALTED
;
1191 return target
->type
->add_watchpoint(target
, watchpoint
);
1193 int target_remove_watchpoint(struct target
*target
,
1194 struct watchpoint
*watchpoint
)
1196 return target
->type
->remove_watchpoint(target
, watchpoint
);
1198 int target_hit_watchpoint(struct target
*target
,
1199 struct watchpoint
**hit_watchpoint
)
1201 if (target
->state
!= TARGET_HALTED
) {
1202 LOG_WARNING("target %s is not halted (hit watchpoint)", target
->cmd_name
);
1203 return ERROR_TARGET_NOT_HALTED
;
1206 if (target
->type
->hit_watchpoint
== NULL
) {
1207 /* For backward compatible, if hit_watchpoint is not implemented,
1208 * return ERROR_FAIL such that gdb_server will not take the nonsense
1213 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1216 const char *target_get_gdb_arch(struct target
*target
)
1218 if (target
->type
->get_gdb_arch
== NULL
)
1220 return target
->type
->get_gdb_arch(target
);
1223 int target_get_gdb_reg_list(struct target
*target
,
1224 struct reg
**reg_list
[], int *reg_list_size
,
1225 enum target_register_class reg_class
)
1227 int result
= target
->type
->get_gdb_reg_list(target
, reg_list
,
1228 reg_list_size
, reg_class
);
1229 if (result
!= ERROR_OK
) {
1236 int target_get_gdb_reg_list_noread(struct target
*target
,
1237 struct reg
**reg_list
[], int *reg_list_size
,
1238 enum target_register_class reg_class
)
1240 if (target
->type
->get_gdb_reg_list_noread
&&
1241 target
->type
->get_gdb_reg_list_noread(target
, reg_list
,
1242 reg_list_size
, reg_class
) == ERROR_OK
)
1244 return target_get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1247 bool target_supports_gdb_connection(struct target
*target
)
1250 * based on current code, we can simply exclude all the targets that
1251 * don't provide get_gdb_reg_list; this could change with new targets.
1253 return !!target
->type
->get_gdb_reg_list
;
1256 int target_step(struct target
*target
,
1257 int current
, target_addr_t address
, int handle_breakpoints
)
1259 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1262 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1264 if (target
->state
!= TARGET_HALTED
) {
1265 LOG_WARNING("target %s is not halted (gdb fileio)", target
->cmd_name
);
1266 return ERROR_TARGET_NOT_HALTED
;
1268 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1271 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1273 if (target
->state
!= TARGET_HALTED
) {
1274 LOG_WARNING("target %s is not halted (gdb fileio end)", target
->cmd_name
);
1275 return ERROR_TARGET_NOT_HALTED
;
1277 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1280 target_addr_t
target_address_max(struct target
*target
)
1282 unsigned bits
= target_address_bits(target
);
1283 if (sizeof(target_addr_t
) * 8 == bits
)
1284 return (target_addr_t
) -1;
1286 return (((target_addr_t
) 1) << bits
) - 1;
1289 unsigned target_address_bits(struct target
*target
)
1291 if (target
->type
->address_bits
)
1292 return target
->type
->address_bits(target
);
1296 int target_profiling(struct target
*target
, uint32_t *samples
,
1297 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1299 if (target
->state
!= TARGET_HALTED
) {
1300 LOG_WARNING("target %s is not halted (profiling)", target
->cmd_name
);
1301 return ERROR_TARGET_NOT_HALTED
;
1303 return target
->type
->profiling(target
, samples
, max_num_samples
,
1304 num_samples
, seconds
);
1308 * Reset the @c examined flag for the given target.
1309 * Pure paranoia -- targets are zeroed on allocation.
1311 static void target_reset_examined(struct target
*target
)
1313 target
->examined
= false;
1316 static int handle_target(void *priv
);
1318 static int target_init_one(struct command_context
*cmd_ctx
,
1319 struct target
*target
)
1321 target_reset_examined(target
);
1323 struct target_type
*type
= target
->type
;
1324 if (type
->examine
== NULL
)
1325 type
->examine
= default_examine
;
1327 if (type
->check_reset
== NULL
)
1328 type
->check_reset
= default_check_reset
;
1330 assert(type
->init_target
!= NULL
);
1332 int retval
= type
->init_target(cmd_ctx
, target
);
1333 if (ERROR_OK
!= retval
) {
1334 LOG_ERROR("target '%s' init failed", target_name(target
));
1338 /* Sanity-check MMU support ... stub in what we must, to help
1339 * implement it in stages, but warn if we need to do so.
1342 if (type
->virt2phys
== NULL
) {
1343 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1344 type
->virt2phys
= identity_virt2phys
;
1347 /* Make sure no-MMU targets all behave the same: make no
1348 * distinction between physical and virtual addresses, and
1349 * ensure that virt2phys() is always an identity mapping.
1351 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1352 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1355 type
->write_phys_memory
= type
->write_memory
;
1356 type
->read_phys_memory
= type
->read_memory
;
1357 type
->virt2phys
= identity_virt2phys
;
1360 if (target
->type
->read_buffer
== NULL
)
1361 target
->type
->read_buffer
= target_read_buffer_default
;
1363 if (target
->type
->write_buffer
== NULL
)
1364 target
->type
->write_buffer
= target_write_buffer_default
;
1366 if (target
->type
->get_gdb_fileio_info
== NULL
)
1367 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1369 if (target
->type
->gdb_fileio_end
== NULL
)
1370 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1372 if (target
->type
->profiling
== NULL
)
1373 target
->type
->profiling
= target_profiling_default
;
1378 static int target_init(struct command_context
*cmd_ctx
)
1380 struct target
*target
;
1383 for (target
= all_targets
; target
; target
= target
->next
) {
1384 retval
= target_init_one(cmd_ctx
, target
);
1385 if (ERROR_OK
!= retval
)
1392 retval
= target_register_user_commands(cmd_ctx
);
1393 if (ERROR_OK
!= retval
)
1396 retval
= target_register_timer_callback(&handle_target
,
1397 polling_interval
, TARGET_TIMER_TYPE_PERIODIC
, cmd_ctx
->interp
);
1398 if (ERROR_OK
!= retval
)
1404 COMMAND_HANDLER(handle_target_init_command
)
1409 return ERROR_COMMAND_SYNTAX_ERROR
;
1411 static bool target_initialized
;
1412 if (target_initialized
) {
1413 LOG_INFO("'target init' has already been called");
1416 target_initialized
= true;
1418 retval
= command_run_line(CMD_CTX
, "init_targets");
1419 if (ERROR_OK
!= retval
)
1422 retval
= command_run_line(CMD_CTX
, "init_target_events");
1423 if (ERROR_OK
!= retval
)
1426 retval
= command_run_line(CMD_CTX
, "init_board");
1427 if (ERROR_OK
!= retval
)
1430 LOG_DEBUG("Initializing targets...");
1431 return target_init(CMD_CTX
);
1434 int target_register_event_callback(int (*callback
)(struct target
*target
,
1435 enum target_event event
, void *priv
), void *priv
)
1437 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1439 if (callback
== NULL
)
1440 return ERROR_COMMAND_SYNTAX_ERROR
;
1443 while ((*callbacks_p
)->next
)
1444 callbacks_p
= &((*callbacks_p
)->next
);
1445 callbacks_p
= &((*callbacks_p
)->next
);
1448 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1449 (*callbacks_p
)->callback
= callback
;
1450 (*callbacks_p
)->priv
= priv
;
1451 (*callbacks_p
)->next
= NULL
;
1456 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1457 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1459 struct target_reset_callback
*entry
;
1461 if (callback
== NULL
)
1462 return ERROR_COMMAND_SYNTAX_ERROR
;
1464 entry
= malloc(sizeof(struct target_reset_callback
));
1465 if (entry
== NULL
) {
1466 LOG_ERROR("error allocating buffer for reset callback entry");
1467 return ERROR_COMMAND_SYNTAX_ERROR
;
1470 entry
->callback
= callback
;
1472 list_add(&entry
->list
, &target_reset_callback_list
);
1478 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1479 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1481 struct target_trace_callback
*entry
;
1483 if (callback
== NULL
)
1484 return ERROR_COMMAND_SYNTAX_ERROR
;
1486 entry
= malloc(sizeof(struct target_trace_callback
));
1487 if (entry
== NULL
) {
1488 LOG_ERROR("error allocating buffer for trace callback entry");
1489 return ERROR_COMMAND_SYNTAX_ERROR
;
1492 entry
->callback
= callback
;
1494 list_add(&entry
->list
, &target_trace_callback_list
);
1500 int target_register_timer_callback(int (*callback
)(void *priv
),
1501 unsigned int time_ms
, enum target_timer_type type
, void *priv
)
1503 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1505 if (callback
== NULL
)
1506 return ERROR_COMMAND_SYNTAX_ERROR
;
1509 while ((*callbacks_p
)->next
)
1510 callbacks_p
= &((*callbacks_p
)->next
);
1511 callbacks_p
= &((*callbacks_p
)->next
);
1514 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1515 (*callbacks_p
)->callback
= callback
;
1516 (*callbacks_p
)->type
= type
;
1517 (*callbacks_p
)->time_ms
= time_ms
;
1518 (*callbacks_p
)->removed
= false;
1520 gettimeofday(&(*callbacks_p
)->when
, NULL
);
1521 timeval_add_time(&(*callbacks_p
)->when
, 0, time_ms
* 1000);
1523 (*callbacks_p
)->priv
= priv
;
1524 (*callbacks_p
)->next
= NULL
;
1529 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1530 enum target_event event
, void *priv
), void *priv
)
1532 struct target_event_callback
**p
= &target_event_callbacks
;
1533 struct target_event_callback
*c
= target_event_callbacks
;
1535 if (callback
== NULL
)
1536 return ERROR_COMMAND_SYNTAX_ERROR
;
1539 struct target_event_callback
*next
= c
->next
;
1540 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1552 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1553 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1555 struct target_reset_callback
*entry
;
1557 if (callback
== NULL
)
1558 return ERROR_COMMAND_SYNTAX_ERROR
;
1560 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1561 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1562 list_del(&entry
->list
);
1571 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1572 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1574 struct target_trace_callback
*entry
;
1576 if (callback
== NULL
)
1577 return ERROR_COMMAND_SYNTAX_ERROR
;
1579 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1580 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1581 list_del(&entry
->list
);
1590 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1592 if (callback
== NULL
)
1593 return ERROR_COMMAND_SYNTAX_ERROR
;
1595 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1597 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1606 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1608 struct target_event_callback
*callback
= target_event_callbacks
;
1609 struct target_event_callback
*next_callback
;
1611 if (event
== TARGET_EVENT_HALTED
) {
1612 /* execute early halted first */
1613 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1616 LOG_DEBUG("target event %i (%s) for core %s", event
,
1617 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
,
1618 target_name(target
));
1620 target_handle_event(target
, event
);
1623 next_callback
= callback
->next
;
1624 callback
->callback(target
, event
, callback
->priv
);
1625 callback
= next_callback
;
1631 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1633 struct target_reset_callback
*callback
;
1635 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1636 Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1638 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1639 callback
->callback(target
, reset_mode
, callback
->priv
);
1644 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1646 struct target_trace_callback
*callback
;
1648 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1649 callback
->callback(target
, len
, data
, callback
->priv
);
1654 static int target_timer_callback_periodic_restart(
1655 struct target_timer_callback
*cb
, struct timeval
*now
)
1658 timeval_add_time(&cb
->when
, 0, cb
->time_ms
* 1000L);
1662 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1663 struct timeval
*now
)
1665 cb
->callback(cb
->priv
);
1667 if (cb
->type
== TARGET_TIMER_TYPE_PERIODIC
)
1668 return target_timer_callback_periodic_restart(cb
, now
);
1670 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1673 static int target_call_timer_callbacks_check_time(int checktime
)
1675 static bool callback_processing
;
1677 /* Do not allow nesting */
1678 if (callback_processing
)
1681 callback_processing
= true;
1686 gettimeofday(&now
, NULL
);
1688 /* Store an address of the place containing a pointer to the
1689 * next item; initially, that's a standalone "root of the
1690 * list" variable. */
1691 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1692 while (callback
&& *callback
) {
1693 if ((*callback
)->removed
) {
1694 struct target_timer_callback
*p
= *callback
;
1695 *callback
= (*callback
)->next
;
1700 bool call_it
= (*callback
)->callback
&&
1701 ((!checktime
&& (*callback
)->type
== TARGET_TIMER_TYPE_PERIODIC
) ||
1702 timeval_compare(&now
, &(*callback
)->when
) >= 0);
1705 target_call_timer_callback(*callback
, &now
);
1707 callback
= &(*callback
)->next
;
1710 callback_processing
= false;
1714 int target_call_timer_callbacks(void)
1716 return target_call_timer_callbacks_check_time(1);
1719 /* invoke periodic callbacks immediately */
1720 int target_call_timer_callbacks_now(void)
1722 return target_call_timer_callbacks_check_time(0);
1725 /* Prints the working area layout for debug purposes */
1726 static void print_wa_layout(struct target
*target
)
1728 struct working_area
*c
= target
->working_areas
;
1731 LOG_DEBUG("%c%c " TARGET_ADDR_FMT
"-" TARGET_ADDR_FMT
" (%" PRIu32
" bytes)",
1732 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1733 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1738 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1739 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1741 assert(area
->free
); /* Shouldn't split an allocated area */
1742 assert(size
<= area
->size
); /* Caller should guarantee this */
1744 /* Split only if not already the right size */
1745 if (size
< area
->size
) {
1746 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1751 new_wa
->next
= area
->next
;
1752 new_wa
->size
= area
->size
- size
;
1753 new_wa
->address
= area
->address
+ size
;
1754 new_wa
->backup
= NULL
;
1755 new_wa
->user
= NULL
;
1756 new_wa
->free
= true;
1758 area
->next
= new_wa
;
1761 /* If backup memory was allocated to this area, it has the wrong size
1762 * now so free it and it will be reallocated if/when needed */
1765 area
->backup
= NULL
;
1770 /* Merge all adjacent free areas into one */
1771 static void target_merge_working_areas(struct target
*target
)
1773 struct working_area
*c
= target
->working_areas
;
1775 while (c
&& c
->next
) {
1776 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1778 /* Find two adjacent free areas */
1779 if (c
->free
&& c
->next
->free
) {
1780 /* Merge the last into the first */
1781 c
->size
+= c
->next
->size
;
1783 /* Remove the last */
1784 struct working_area
*to_be_freed
= c
->next
;
1785 c
->next
= c
->next
->next
;
1786 if (to_be_freed
->backup
)
1787 free(to_be_freed
->backup
);
1790 /* If backup memory was allocated to the remaining area, it's has
1791 * the wrong size now */
1802 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1804 /* Reevaluate working area address based on MMU state*/
1805 if (target
->working_areas
== NULL
) {
1809 retval
= target
->type
->mmu(target
, &enabled
);
1810 if (retval
!= ERROR_OK
)
1814 if (target
->working_area_phys_spec
) {
1815 LOG_DEBUG("MMU disabled, using physical "
1816 "address for working memory " TARGET_ADDR_FMT
,
1817 target
->working_area_phys
);
1818 target
->working_area
= target
->working_area_phys
;
1820 LOG_ERROR("No working memory available. "
1821 "Specify -work-area-phys to target.");
1822 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1825 if (target
->working_area_virt_spec
) {
1826 LOG_DEBUG("MMU enabled, using virtual "
1827 "address for working memory " TARGET_ADDR_FMT
,
1828 target
->working_area_virt
);
1829 target
->working_area
= target
->working_area_virt
;
1831 LOG_ERROR("No working memory available. "
1832 "Specify -work-area-virt to target.");
1833 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1837 /* Set up initial working area on first call */
1838 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1840 new_wa
->next
= NULL
;
1841 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1842 new_wa
->address
= target
->working_area
;
1843 new_wa
->backup
= NULL
;
1844 new_wa
->user
= NULL
;
1845 new_wa
->free
= true;
1848 target
->working_areas
= new_wa
;
1851 /* only allocate multiples of 4 byte */
1853 size
= (size
+ 3) & (~3UL);
1855 struct working_area
*c
= target
->working_areas
;
1857 /* Find the first large enough working area */
1859 if (c
->free
&& c
->size
>= size
)
1865 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1867 /* Split the working area into the requested size */
1868 target_split_working_area(c
, size
);
1870 LOG_DEBUG("allocated new working area of %" PRIu32
" bytes at address " TARGET_ADDR_FMT
,
1873 if (target
->backup_working_area
) {
1874 if (c
->backup
== NULL
) {
1875 c
->backup
= malloc(c
->size
);
1876 if (c
->backup
== NULL
)
1880 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1881 if (retval
!= ERROR_OK
)
1885 /* mark as used, and return the new (reused) area */
1892 print_wa_layout(target
);
1897 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1901 retval
= target_alloc_working_area_try(target
, size
, area
);
1902 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1903 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1908 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1910 int retval
= ERROR_OK
;
1912 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1913 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1914 if (retval
!= ERROR_OK
)
1915 LOG_ERROR("failed to restore %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1916 area
->size
, area
->address
);
1922 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1923 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1925 int retval
= ERROR_OK
;
1931 retval
= target_restore_working_area(target
, area
);
1932 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1933 if (retval
!= ERROR_OK
)
1939 LOG_DEBUG("freed %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1940 area
->size
, area
->address
);
1942 /* mark user pointer invalid */
1943 /* TODO: Is this really safe? It points to some previous caller's memory.
1944 * How could we know that the area pointer is still in that place and not
1945 * some other vital data? What's the purpose of this, anyway? */
1949 target_merge_working_areas(target
);
1951 print_wa_layout(target
);
1956 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1958 return target_free_working_area_restore(target
, area
, 1);
1961 /* free resources and restore memory, if restoring memory fails,
1962 * free up resources anyway
1964 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1966 struct working_area
*c
= target
->working_areas
;
1968 LOG_DEBUG("freeing all working areas");
1970 /* Loop through all areas, restoring the allocated ones and marking them as free */
1974 target_restore_working_area(target
, c
);
1976 *c
->user
= NULL
; /* Same as above */
1982 /* Run a merge pass to combine all areas into one */
1983 target_merge_working_areas(target
);
1985 print_wa_layout(target
);
1988 void target_free_all_working_areas(struct target
*target
)
1990 target_free_all_working_areas_restore(target
, 1);
1992 /* Now we have none or only one working area marked as free */
1993 if (target
->working_areas
) {
1994 /* Free the last one to allow on-the-fly moving and resizing */
1995 free(target
->working_areas
->backup
);
1996 free(target
->working_areas
);
1997 target
->working_areas
= NULL
;
2001 /* Find the largest number of bytes that can be allocated */
2002 uint32_t target_get_working_area_avail(struct target
*target
)
2004 struct working_area
*c
= target
->working_areas
;
2005 uint32_t max_size
= 0;
2008 return target
->working_area_size
;
2011 if (c
->free
&& max_size
< c
->size
)
2020 static void target_destroy(struct target
*target
)
2022 if (target
->type
->deinit_target
)
2023 target
->type
->deinit_target(target
);
2025 if (target
->semihosting
)
2026 free(target
->semihosting
);
2028 jtag_unregister_event_callback(jtag_enable_callback
, target
);
2030 struct target_event_action
*teap
= target
->event_action
;
2032 struct target_event_action
*next
= teap
->next
;
2033 Jim_DecrRefCount(teap
->interp
, teap
->body
);
2038 target_free_all_working_areas(target
);
2040 /* release the targets SMP list */
2042 struct target_list
*head
= target
->head
;
2043 while (head
!= NULL
) {
2044 struct target_list
*pos
= head
->next
;
2045 head
->target
->smp
= 0;
2052 rtos_destroy(target
);
2054 free(target
->gdb_port_override
);
2056 free(target
->trace_info
);
2057 free(target
->fileio_info
);
2058 free(target
->cmd_name
);
2062 void target_quit(void)
2064 struct target_event_callback
*pe
= target_event_callbacks
;
2066 struct target_event_callback
*t
= pe
->next
;
2070 target_event_callbacks
= NULL
;
2072 struct target_timer_callback
*pt
= target_timer_callbacks
;
2074 struct target_timer_callback
*t
= pt
->next
;
2078 target_timer_callbacks
= NULL
;
2080 for (struct target
*target
= all_targets
; target
;) {
2084 target_destroy(target
);
2091 int target_arch_state(struct target
*target
)
2094 if (target
== NULL
) {
2095 LOG_WARNING("No target has been configured");
2099 if (target
->state
!= TARGET_HALTED
)
2102 retval
= target
->type
->arch_state(target
);
2106 static int target_get_gdb_fileio_info_default(struct target
*target
,
2107 struct gdb_fileio_info
*fileio_info
)
2109 /* If target does not support semi-hosting function, target
2110 has no need to provide .get_gdb_fileio_info callback.
2111 It just return ERROR_FAIL and gdb_server will return "Txx"
2112 as target halted every time. */
2116 static int target_gdb_fileio_end_default(struct target
*target
,
2117 int retcode
, int fileio_errno
, bool ctrl_c
)
2122 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
2123 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
2125 struct timeval timeout
, now
;
2127 gettimeofday(&timeout
, NULL
);
2128 timeval_add_time(&timeout
, seconds
, 0);
2130 LOG_INFO("Starting profiling. Halting and resuming the"
2131 " target as often as we can...");
2133 uint32_t sample_count
= 0;
2134 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2135 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2137 int retval
= ERROR_OK
;
2139 target_poll(target
);
2140 if (target
->state
== TARGET_HALTED
) {
2141 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
2142 samples
[sample_count
++] = t
;
2143 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2144 retval
= target_resume(target
, 1, 0, 0, 0);
2145 target_poll(target
);
2146 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2147 } else if (target
->state
== TARGET_RUNNING
) {
2148 /* We want to quickly sample the PC. */
2149 retval
= target_halt(target
);
2151 LOG_INFO("Target not halted or running");
2156 if (retval
!= ERROR_OK
)
2159 gettimeofday(&now
, NULL
);
2160 if ((sample_count
>= max_num_samples
) || timeval_compare(&now
, &timeout
) >= 0) {
2161 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2166 *num_samples
= sample_count
;
2170 /* Single aligned words are guaranteed to use 16 or 32 bit access
2171 * mode respectively, otherwise data is handled as quickly as
2174 int target_write_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, const uint8_t *buffer
)
2176 LOG_DEBUG("writing buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2179 if (!target_was_examined(target
)) {
2180 LOG_ERROR("Target not examined yet");
2187 if ((address
+ size
- 1) < address
) {
2188 /* GDB can request this when e.g. PC is 0xfffffffc */
2189 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2195 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2198 static int target_write_buffer_default(struct target
*target
,
2199 target_addr_t address
, uint32_t count
, const uint8_t *buffer
)
2203 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2204 * will have something to do with the size we leave to it. */
2205 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2206 if (address
& size
) {
2207 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2208 if (retval
!= ERROR_OK
)
2216 /* Write the data with as large access size as possible. */
2217 for (; size
> 0; size
/= 2) {
2218 uint32_t aligned
= count
- count
% size
;
2220 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2221 if (retval
!= ERROR_OK
)
2232 /* Single aligned words are guaranteed to use 16 or 32 bit access
2233 * mode respectively, otherwise data is handled as quickly as
2236 int target_read_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
2238 LOG_DEBUG("reading buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2241 if (!target_was_examined(target
)) {
2242 LOG_ERROR("Target not examined yet");
2249 if ((address
+ size
- 1) < address
) {
2250 /* GDB can request this when e.g. PC is 0xfffffffc */
2251 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2257 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2260 static int target_read_buffer_default(struct target
*target
, target_addr_t address
, uint32_t count
, uint8_t *buffer
)
2264 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2265 * will have something to do with the size we leave to it. */
2266 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2267 if (address
& size
) {
2268 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2269 if (retval
!= ERROR_OK
)
2277 /* Read the data with as large access size as possible. */
2278 for (; size
> 0; size
/= 2) {
2279 uint32_t aligned
= count
- count
% size
;
2281 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2282 if (retval
!= ERROR_OK
)
2293 int target_checksum_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t *crc
)
2298 uint32_t checksum
= 0;
2299 if (!target_was_examined(target
)) {
2300 LOG_ERROR("Target not examined yet");
2304 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2305 if (retval
!= ERROR_OK
) {
2306 buffer
= malloc(size
);
2307 if (buffer
== NULL
) {
2308 LOG_ERROR("error allocating buffer for section (%" PRId32
" bytes)", size
);
2309 return ERROR_COMMAND_SYNTAX_ERROR
;
2311 retval
= target_read_buffer(target
, address
, size
, buffer
);
2312 if (retval
!= ERROR_OK
) {
2317 /* convert to target endianness */
2318 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2319 uint32_t target_data
;
2320 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2321 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2324 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2333 int target_blank_check_memory(struct target
*target
,
2334 struct target_memory_check_block
*blocks
, int num_blocks
,
2335 uint8_t erased_value
)
2337 if (!target_was_examined(target
)) {
2338 LOG_ERROR("Target not examined yet");
2342 if (target
->type
->blank_check_memory
== NULL
)
2343 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2345 return target
->type
->blank_check_memory(target
, blocks
, num_blocks
, erased_value
);
2348 int target_read_u64(struct target
*target
, target_addr_t address
, uint64_t *value
)
2350 uint8_t value_buf
[8];
2351 if (!target_was_examined(target
)) {
2352 LOG_ERROR("Target not examined yet");
2356 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2358 if (retval
== ERROR_OK
) {
2359 *value
= target_buffer_get_u64(target
, value_buf
);
2360 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2365 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2372 int target_read_u32(struct target
*target
, target_addr_t address
, uint32_t *value
)
2374 uint8_t value_buf
[4];
2375 if (!target_was_examined(target
)) {
2376 LOG_ERROR("Target not examined yet");
2380 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2382 if (retval
== ERROR_OK
) {
2383 *value
= target_buffer_get_u32(target
, value_buf
);
2384 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2389 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2396 int target_read_u16(struct target
*target
, target_addr_t address
, uint16_t *value
)
2398 uint8_t value_buf
[2];
2399 if (!target_was_examined(target
)) {
2400 LOG_ERROR("Target not examined yet");
2404 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2406 if (retval
== ERROR_OK
) {
2407 *value
= target_buffer_get_u16(target
, value_buf
);
2408 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%4.4" PRIx16
,
2413 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2420 int target_read_u8(struct target
*target
, target_addr_t address
, uint8_t *value
)
2422 if (!target_was_examined(target
)) {
2423 LOG_ERROR("Target not examined yet");
2427 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2429 if (retval
== ERROR_OK
) {
2430 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2435 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2442 int target_write_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2445 uint8_t value_buf
[8];
2446 if (!target_was_examined(target
)) {
2447 LOG_ERROR("Target not examined yet");
2451 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2455 target_buffer_set_u64(target
, value_buf
, value
);
2456 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2457 if (retval
!= ERROR_OK
)
2458 LOG_DEBUG("failed: %i", retval
);
2463 int target_write_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2466 uint8_t value_buf
[4];
2467 if (!target_was_examined(target
)) {
2468 LOG_ERROR("Target not examined yet");
2472 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2476 target_buffer_set_u32(target
, value_buf
, value
);
2477 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2478 if (retval
!= ERROR_OK
)
2479 LOG_DEBUG("failed: %i", retval
);
2484 int target_write_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2487 uint8_t value_buf
[2];
2488 if (!target_was_examined(target
)) {
2489 LOG_ERROR("Target not examined yet");
2493 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2497 target_buffer_set_u16(target
, value_buf
, value
);
2498 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2499 if (retval
!= ERROR_OK
)
2500 LOG_DEBUG("failed: %i", retval
);
2505 int target_write_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2508 if (!target_was_examined(target
)) {
2509 LOG_ERROR("Target not examined yet");
2513 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2516 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2517 if (retval
!= ERROR_OK
)
2518 LOG_DEBUG("failed: %i", retval
);
2523 int target_write_phys_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2526 uint8_t value_buf
[8];
2527 if (!target_was_examined(target
)) {
2528 LOG_ERROR("Target not examined yet");
2532 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2536 target_buffer_set_u64(target
, value_buf
, value
);
2537 retval
= target_write_phys_memory(target
, address
, 8, 1, value_buf
);
2538 if (retval
!= ERROR_OK
)
2539 LOG_DEBUG("failed: %i", retval
);
2544 int target_write_phys_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2547 uint8_t value_buf
[4];
2548 if (!target_was_examined(target
)) {
2549 LOG_ERROR("Target not examined yet");
2553 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2557 target_buffer_set_u32(target
, value_buf
, value
);
2558 retval
= target_write_phys_memory(target
, address
, 4, 1, value_buf
);
2559 if (retval
!= ERROR_OK
)
2560 LOG_DEBUG("failed: %i", retval
);
2565 int target_write_phys_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2568 uint8_t value_buf
[2];
2569 if (!target_was_examined(target
)) {
2570 LOG_ERROR("Target not examined yet");
2574 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2578 target_buffer_set_u16(target
, value_buf
, value
);
2579 retval
= target_write_phys_memory(target
, address
, 2, 1, value_buf
);
2580 if (retval
!= ERROR_OK
)
2581 LOG_DEBUG("failed: %i", retval
);
2586 int target_write_phys_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2589 if (!target_was_examined(target
)) {
2590 LOG_ERROR("Target not examined yet");
2594 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2597 retval
= target_write_phys_memory(target
, address
, 1, 1, &value
);
2598 if (retval
!= ERROR_OK
)
2599 LOG_DEBUG("failed: %i", retval
);
2604 static int find_target(struct command_invocation
*cmd
, const char *name
)
2606 struct target
*target
= get_target(name
);
2607 if (target
== NULL
) {
2608 command_print(cmd
, "Target: %s is unknown, try one of:\n", name
);
2611 if (!target
->tap
->enabled
) {
2612 command_print(cmd
, "Target: TAP %s is disabled, "
2613 "can't be the current target\n",
2614 target
->tap
->dotted_name
);
2618 cmd
->ctx
->current_target
= target
;
2619 if (cmd
->ctx
->current_target_override
)
2620 cmd
->ctx
->current_target_override
= target
;
2626 COMMAND_HANDLER(handle_targets_command
)
2628 int retval
= ERROR_OK
;
2629 if (CMD_ARGC
== 1) {
2630 retval
= find_target(CMD
, CMD_ARGV
[0]);
2631 if (retval
== ERROR_OK
) {
2637 struct target
*target
= all_targets
;
2638 command_print(CMD
, " TargetName Type Endian TapName State ");
2639 command_print(CMD
, "-- ------------------ ---------- ------ ------------------ ------------");
2644 if (target
->tap
->enabled
)
2645 state
= target_state_name(target
);
2647 state
= "tap-disabled";
2649 if (CMD_CTX
->current_target
== target
)
2652 /* keep columns lined up to match the headers above */
2654 "%2d%c %-18s %-10s %-6s %-18s %s",
2655 target
->target_number
,
2657 target_name(target
),
2658 target_type_name(target
),
2659 Jim_Nvp_value2name_simple(nvp_target_endian
,
2660 target
->endianness
)->name
,
2661 target
->tap
->dotted_name
,
2663 target
= target
->next
;
2669 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2671 static int powerDropout
;
2672 static int srstAsserted
;
2674 static int runPowerRestore
;
2675 static int runPowerDropout
;
2676 static int runSrstAsserted
;
2677 static int runSrstDeasserted
;
2679 static int sense_handler(void)
2681 static int prevSrstAsserted
;
2682 static int prevPowerdropout
;
2684 int retval
= jtag_power_dropout(&powerDropout
);
2685 if (retval
!= ERROR_OK
)
2689 powerRestored
= prevPowerdropout
&& !powerDropout
;
2691 runPowerRestore
= 1;
2693 int64_t current
= timeval_ms();
2694 static int64_t lastPower
;
2695 bool waitMore
= lastPower
+ 2000 > current
;
2696 if (powerDropout
&& !waitMore
) {
2697 runPowerDropout
= 1;
2698 lastPower
= current
;
2701 retval
= jtag_srst_asserted(&srstAsserted
);
2702 if (retval
!= ERROR_OK
)
2706 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2708 static int64_t lastSrst
;
2709 waitMore
= lastSrst
+ 2000 > current
;
2710 if (srstDeasserted
&& !waitMore
) {
2711 runSrstDeasserted
= 1;
2715 if (!prevSrstAsserted
&& srstAsserted
)
2716 runSrstAsserted
= 1;
2718 prevSrstAsserted
= srstAsserted
;
2719 prevPowerdropout
= powerDropout
;
2721 if (srstDeasserted
|| powerRestored
) {
2722 /* Other than logging the event we can't do anything here.
2723 * Issuing a reset is a particularly bad idea as we might
2724 * be inside a reset already.
2731 /* process target state changes */
2732 static int handle_target(void *priv
)
2734 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2735 int retval
= ERROR_OK
;
2737 if (!is_jtag_poll_safe()) {
2738 /* polling is disabled currently */
2742 /* we do not want to recurse here... */
2743 static int recursive
;
2747 /* danger! running these procedures can trigger srst assertions and power dropouts.
2748 * We need to avoid an infinite loop/recursion here and we do that by
2749 * clearing the flags after running these events.
2751 int did_something
= 0;
2752 if (runSrstAsserted
) {
2753 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2754 Jim_Eval(interp
, "srst_asserted");
2757 if (runSrstDeasserted
) {
2758 Jim_Eval(interp
, "srst_deasserted");
2761 if (runPowerDropout
) {
2762 LOG_INFO("Power dropout detected, running power_dropout proc.");
2763 Jim_Eval(interp
, "power_dropout");
2766 if (runPowerRestore
) {
2767 Jim_Eval(interp
, "power_restore");
2771 if (did_something
) {
2772 /* clear detect flags */
2776 /* clear action flags */
2778 runSrstAsserted
= 0;
2779 runSrstDeasserted
= 0;
2780 runPowerRestore
= 0;
2781 runPowerDropout
= 0;
2786 /* Poll targets for state changes unless that's globally disabled.
2787 * Skip targets that are currently disabled.
2789 for (struct target
*target
= all_targets
;
2790 is_jtag_poll_safe() && target
;
2791 target
= target
->next
) {
2793 if (!target_was_examined(target
))
2796 if (!target
->tap
->enabled
)
2799 if (target
->backoff
.times
> target
->backoff
.count
) {
2800 /* do not poll this time as we failed previously */
2801 target
->backoff
.count
++;
2804 target
->backoff
.count
= 0;
2806 /* only poll target if we've got power and srst isn't asserted */
2807 if (!powerDropout
&& !srstAsserted
) {
2808 /* polling may fail silently until the target has been examined */
2809 retval
= target_poll(target
);
2810 if (retval
!= ERROR_OK
) {
2811 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2812 if (target
->backoff
.times
* polling_interval
< 5000) {
2813 target
->backoff
.times
*= 2;
2814 target
->backoff
.times
++;
2817 /* Tell GDB to halt the debugger. This allows the user to
2818 * run monitor commands to handle the situation.
2820 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2822 if (target
->backoff
.times
> 0) {
2823 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
2824 target_reset_examined(target
);
2825 retval
= target_examine_one(target
);
2826 /* Target examination could have failed due to unstable connection,
2827 * but we set the examined flag anyway to repoll it later */
2828 if (retval
!= ERROR_OK
) {
2829 target
->examined
= true;
2830 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2831 target
->backoff
.times
* polling_interval
);
2836 /* Since we succeeded, we reset backoff count */
2837 target
->backoff
.times
= 0;
2844 COMMAND_HANDLER(handle_reg_command
)
2846 struct target
*target
;
2847 struct reg
*reg
= NULL
;
2853 target
= get_current_target(CMD_CTX
);
2855 /* list all available registers for the current target */
2856 if (CMD_ARGC
== 0) {
2857 struct reg_cache
*cache
= target
->reg_cache
;
2863 command_print(CMD
, "===== %s", cache
->name
);
2865 for (i
= 0, reg
= cache
->reg_list
;
2866 i
< cache
->num_regs
;
2867 i
++, reg
++, count
++) {
2868 if (reg
->exist
== false)
2870 /* only print cached values if they are valid */
2872 value
= buf_to_str(reg
->value
,
2875 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2883 command_print(CMD
, "(%i) %s (/%" PRIu32
")",
2888 cache
= cache
->next
;
2894 /* access a single register by its ordinal number */
2895 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2897 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2899 struct reg_cache
*cache
= target
->reg_cache
;
2903 for (i
= 0; i
< cache
->num_regs
; i
++) {
2904 if (count
++ == num
) {
2905 reg
= &cache
->reg_list
[i
];
2911 cache
= cache
->next
;
2915 command_print(CMD
, "%i is out of bounds, the current target "
2916 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2920 /* access a single register by its name */
2921 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2927 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2932 /* display a register */
2933 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2934 && (CMD_ARGV
[1][0] <= '9')))) {
2935 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2938 if (reg
->valid
== 0)
2939 reg
->type
->get(reg
);
2940 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2941 command_print(CMD
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2946 /* set register value */
2947 if (CMD_ARGC
== 2) {
2948 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2951 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2953 reg
->type
->set(reg
, buf
);
2955 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2956 command_print(CMD
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2964 return ERROR_COMMAND_SYNTAX_ERROR
;
2967 command_print(CMD
, "register %s not found in current target", CMD_ARGV
[0]);
2971 COMMAND_HANDLER(handle_poll_command
)
2973 int retval
= ERROR_OK
;
2974 struct target
*target
= get_current_target(CMD_CTX
);
2976 if (CMD_ARGC
== 0) {
2977 command_print(CMD
, "background polling: %s",
2978 jtag_poll_get_enabled() ? "on" : "off");
2979 command_print(CMD
, "TAP: %s (%s)",
2980 target
->tap
->dotted_name
,
2981 target
->tap
->enabled
? "enabled" : "disabled");
2982 if (!target
->tap
->enabled
)
2984 retval
= target_poll(target
);
2985 if (retval
!= ERROR_OK
)
2987 retval
= target_arch_state(target
);
2988 if (retval
!= ERROR_OK
)
2990 } else if (CMD_ARGC
== 1) {
2992 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2993 jtag_poll_set_enabled(enable
);
2995 return ERROR_COMMAND_SYNTAX_ERROR
;
3000 COMMAND_HANDLER(handle_wait_halt_command
)
3003 return ERROR_COMMAND_SYNTAX_ERROR
;
3005 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
3006 if (1 == CMD_ARGC
) {
3007 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
3008 if (ERROR_OK
!= retval
)
3009 return ERROR_COMMAND_SYNTAX_ERROR
;
3012 struct target
*target
= get_current_target(CMD_CTX
);
3013 return target_wait_state(target
, TARGET_HALTED
, ms
);
3016 /* wait for target state to change. The trick here is to have a low
3017 * latency for short waits and not to suck up all the CPU time
3020 * After 500ms, keep_alive() is invoked
3022 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
3025 int64_t then
= 0, cur
;
3029 retval
= target_poll(target
);
3030 if (retval
!= ERROR_OK
)
3032 if (target
->state
== state
)
3037 then
= timeval_ms();
3038 LOG_DEBUG("waiting for target %s...",
3039 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
3045 if ((cur
-then
) > ms
) {
3046 LOG_ERROR("timed out while waiting for target %s",
3047 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
3055 COMMAND_HANDLER(handle_halt_command
)
3059 struct target
*target
= get_current_target(CMD_CTX
);
3061 target
->verbose_halt_msg
= true;
3063 int retval
= target_halt(target
);
3064 if (ERROR_OK
!= retval
)
3067 if (CMD_ARGC
== 1) {
3068 unsigned wait_local
;
3069 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
3070 if (ERROR_OK
!= retval
)
3071 return ERROR_COMMAND_SYNTAX_ERROR
;
3076 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
3079 COMMAND_HANDLER(handle_soft_reset_halt_command
)
3081 struct target
*target
= get_current_target(CMD_CTX
);
3083 LOG_USER("requesting target halt and executing a soft reset");
3085 target_soft_reset_halt(target
);
3090 COMMAND_HANDLER(handle_reset_command
)
3093 return ERROR_COMMAND_SYNTAX_ERROR
;
3095 enum target_reset_mode reset_mode
= RESET_RUN
;
3096 if (CMD_ARGC
== 1) {
3098 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
3099 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
3100 return ERROR_COMMAND_SYNTAX_ERROR
;
3101 reset_mode
= n
->value
;
3104 /* reset *all* targets */
3105 return target_process_reset(CMD
, reset_mode
);
3109 COMMAND_HANDLER(handle_resume_command
)
3113 return ERROR_COMMAND_SYNTAX_ERROR
;
3115 struct target
*target
= get_current_target(CMD_CTX
);
3117 /* with no CMD_ARGV, resume from current pc, addr = 0,
3118 * with one arguments, addr = CMD_ARGV[0],
3119 * handle breakpoints, not debugging */
3120 target_addr_t addr
= 0;
3121 if (CMD_ARGC
== 1) {
3122 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3126 return target_resume(target
, current
, addr
, 1, 0);
3129 COMMAND_HANDLER(handle_step_command
)
3132 return ERROR_COMMAND_SYNTAX_ERROR
;
3136 /* with no CMD_ARGV, step from current pc, addr = 0,
3137 * with one argument addr = CMD_ARGV[0],
3138 * handle breakpoints, debugging */
3139 target_addr_t addr
= 0;
3141 if (CMD_ARGC
== 1) {
3142 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3146 struct target
*target
= get_current_target(CMD_CTX
);
3148 return target
->type
->step(target
, current_pc
, addr
, 1);
3151 void target_handle_md_output(struct command_invocation
*cmd
,
3152 struct target
*target
, target_addr_t address
, unsigned size
,
3153 unsigned count
, const uint8_t *buffer
)
3155 const unsigned line_bytecnt
= 32;
3156 unsigned line_modulo
= line_bytecnt
/ size
;
3158 char output
[line_bytecnt
* 4 + 1];
3159 unsigned output_len
= 0;
3161 const char *value_fmt
;
3164 value_fmt
= "%16.16"PRIx64
" ";
3167 value_fmt
= "%8.8"PRIx64
" ";
3170 value_fmt
= "%4.4"PRIx64
" ";
3173 value_fmt
= "%2.2"PRIx64
" ";
3176 /* "can't happen", caller checked */
3177 LOG_ERROR("invalid memory read size: %u", size
);
3181 for (unsigned i
= 0; i
< count
; i
++) {
3182 if (i
% line_modulo
== 0) {
3183 output_len
+= snprintf(output
+ output_len
,
3184 sizeof(output
) - output_len
,
3185 TARGET_ADDR_FMT
": ",
3186 (address
+ (i
* size
)));
3190 const uint8_t *value_ptr
= buffer
+ i
* size
;
3193 value
= target_buffer_get_u64(target
, value_ptr
);
3196 value
= target_buffer_get_u32(target
, value_ptr
);
3199 value
= target_buffer_get_u16(target
, value_ptr
);
3204 output_len
+= snprintf(output
+ output_len
,
3205 sizeof(output
) - output_len
,
3208 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
3209 command_print(cmd
, "%s", output
);
3215 COMMAND_HANDLER(handle_md_command
)
3218 return ERROR_COMMAND_SYNTAX_ERROR
;
3221 switch (CMD_NAME
[2]) {
3235 return ERROR_COMMAND_SYNTAX_ERROR
;
3238 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3239 int (*fn
)(struct target
*target
,
3240 target_addr_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
3244 fn
= target_read_phys_memory
;
3246 fn
= target_read_memory
;
3247 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
3248 return ERROR_COMMAND_SYNTAX_ERROR
;
3250 target_addr_t address
;
3251 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3255 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
3257 uint8_t *buffer
= calloc(count
, size
);
3258 if (buffer
== NULL
) {
3259 LOG_ERROR("Failed to allocate md read buffer");
3263 struct target
*target
= get_current_target(CMD_CTX
);
3264 int retval
= fn(target
, address
, size
, count
, buffer
);
3265 if (ERROR_OK
== retval
)
3266 target_handle_md_output(CMD
, target
, address
, size
, count
, buffer
);
3273 typedef int (*target_write_fn
)(struct target
*target
,
3274 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
3276 static int target_fill_mem(struct target
*target
,
3277 target_addr_t address
,
3285 /* We have to write in reasonably large chunks to be able
3286 * to fill large memory areas with any sane speed */
3287 const unsigned chunk_size
= 16384;
3288 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
3289 if (target_buf
== NULL
) {
3290 LOG_ERROR("Out of memory");
3294 for (unsigned i
= 0; i
< chunk_size
; i
++) {
3295 switch (data_size
) {
3297 target_buffer_set_u64(target
, target_buf
+ i
* data_size
, b
);
3300 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
3303 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
3306 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
3313 int retval
= ERROR_OK
;
3315 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
3318 if (current
> chunk_size
)
3319 current
= chunk_size
;
3320 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
3321 if (retval
!= ERROR_OK
)
3323 /* avoid GDB timeouts */
3332 COMMAND_HANDLER(handle_mw_command
)
3335 return ERROR_COMMAND_SYNTAX_ERROR
;
3336 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3341 fn
= target_write_phys_memory
;
3343 fn
= target_write_memory
;
3344 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
3345 return ERROR_COMMAND_SYNTAX_ERROR
;
3347 target_addr_t address
;
3348 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3351 COMMAND_PARSE_NUMBER(u64
, CMD_ARGV
[1], value
);
3355 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
3357 struct target
*target
= get_current_target(CMD_CTX
);
3359 switch (CMD_NAME
[2]) {
3373 return ERROR_COMMAND_SYNTAX_ERROR
;
3376 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
3379 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
3380 target_addr_t
*min_address
, target_addr_t
*max_address
)
3382 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
3383 return ERROR_COMMAND_SYNTAX_ERROR
;
3385 /* a base address isn't always necessary,
3386 * default to 0x0 (i.e. don't relocate) */
3387 if (CMD_ARGC
>= 2) {
3389 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3390 image
->base_address
= addr
;
3391 image
->base_address_set
= 1;
3393 image
->base_address_set
= 0;
3395 image
->start_address_set
= 0;
3398 COMMAND_PARSE_ADDRESS(CMD_ARGV
[3], *min_address
);
3399 if (CMD_ARGC
== 5) {
3400 COMMAND_PARSE_ADDRESS(CMD_ARGV
[4], *max_address
);
3401 /* use size (given) to find max (required) */
3402 *max_address
+= *min_address
;
3405 if (*min_address
> *max_address
)
3406 return ERROR_COMMAND_SYNTAX_ERROR
;
3411 COMMAND_HANDLER(handle_load_image_command
)
3415 uint32_t image_size
;
3416 target_addr_t min_address
= 0;
3417 target_addr_t max_address
= -1;
3421 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
3422 &image
, &min_address
, &max_address
);
3423 if (ERROR_OK
!= retval
)
3426 struct target
*target
= get_current_target(CMD_CTX
);
3428 struct duration bench
;
3429 duration_start(&bench
);
3431 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
3436 for (i
= 0; i
< image
.num_sections
; i
++) {
3437 buffer
= malloc(image
.sections
[i
].size
);
3438 if (buffer
== NULL
) {
3440 "error allocating buffer for section (%d bytes)",
3441 (int)(image
.sections
[i
].size
));
3442 retval
= ERROR_FAIL
;
3446 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3447 if (retval
!= ERROR_OK
) {
3452 uint32_t offset
= 0;
3453 uint32_t length
= buf_cnt
;
3455 /* DANGER!!! beware of unsigned comparision here!!! */
3457 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3458 (image
.sections
[i
].base_address
< max_address
)) {
3460 if (image
.sections
[i
].base_address
< min_address
) {
3461 /* clip addresses below */
3462 offset
+= min_address
-image
.sections
[i
].base_address
;
3466 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3467 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3469 retval
= target_write_buffer(target
,
3470 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3471 if (retval
!= ERROR_OK
) {
3475 image_size
+= length
;
3476 command_print(CMD
, "%u bytes written at address " TARGET_ADDR_FMT
"",
3477 (unsigned int)length
,
3478 image
.sections
[i
].base_address
+ offset
);
3484 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3485 command_print(CMD
, "downloaded %" PRIu32
" bytes "
3486 "in %fs (%0.3f KiB/s)", image_size
,
3487 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3490 image_close(&image
);
3496 COMMAND_HANDLER(handle_dump_image_command
)
3498 struct fileio
*fileio
;
3500 int retval
, retvaltemp
;
3501 target_addr_t address
, size
;
3502 struct duration bench
;
3503 struct target
*target
= get_current_target(CMD_CTX
);
3506 return ERROR_COMMAND_SYNTAX_ERROR
;
3508 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], address
);
3509 COMMAND_PARSE_ADDRESS(CMD_ARGV
[2], size
);
3511 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
3512 buffer
= malloc(buf_size
);
3516 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3517 if (retval
!= ERROR_OK
) {
3522 duration_start(&bench
);
3525 size_t size_written
;
3526 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
3527 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
3528 if (retval
!= ERROR_OK
)
3531 retval
= fileio_write(fileio
, this_run_size
, buffer
, &size_written
);
3532 if (retval
!= ERROR_OK
)
3535 size
-= this_run_size
;
3536 address
+= this_run_size
;
3541 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3543 retval
= fileio_size(fileio
, &filesize
);
3544 if (retval
!= ERROR_OK
)
3547 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize
,
3548 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3551 retvaltemp
= fileio_close(fileio
);
3552 if (retvaltemp
!= ERROR_OK
)
3561 IMAGE_CHECKSUM_ONLY
= 2
3564 static COMMAND_HELPER(handle_verify_image_command_internal
, enum verify_mode verify
)
3568 uint32_t image_size
;
3571 uint32_t checksum
= 0;
3572 uint32_t mem_checksum
= 0;
3576 struct target
*target
= get_current_target(CMD_CTX
);
3579 return ERROR_COMMAND_SYNTAX_ERROR
;
3582 LOG_ERROR("no target selected");
3586 struct duration bench
;
3587 duration_start(&bench
);
3589 if (CMD_ARGC
>= 2) {
3591 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3592 image
.base_address
= addr
;
3593 image
.base_address_set
= 1;
3595 image
.base_address_set
= 0;
3596 image
.base_address
= 0x0;
3599 image
.start_address_set
= 0;
3601 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3602 if (retval
!= ERROR_OK
)
3608 for (i
= 0; i
< image
.num_sections
; i
++) {
3609 buffer
= malloc(image
.sections
[i
].size
);
3610 if (buffer
== NULL
) {
3612 "error allocating buffer for section (%d bytes)",
3613 (int)(image
.sections
[i
].size
));
3616 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3617 if (retval
!= ERROR_OK
) {
3622 if (verify
>= IMAGE_VERIFY
) {
3623 /* calculate checksum of image */
3624 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3625 if (retval
!= ERROR_OK
) {
3630 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3631 if (retval
!= ERROR_OK
) {
3635 if ((checksum
!= mem_checksum
) && (verify
== IMAGE_CHECKSUM_ONLY
)) {
3636 LOG_ERROR("checksum mismatch");
3638 retval
= ERROR_FAIL
;
3641 if (checksum
!= mem_checksum
) {
3642 /* failed crc checksum, fall back to a binary compare */
3646 LOG_ERROR("checksum mismatch - attempting binary compare");
3648 data
= malloc(buf_cnt
);
3650 retval
= target_read_buffer(target
, image
.sections
[i
].base_address
, buf_cnt
, data
);
3651 if (retval
== ERROR_OK
) {
3653 for (t
= 0; t
< buf_cnt
; t
++) {
3654 if (data
[t
] != buffer
[t
]) {
3656 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3658 (unsigned)(t
+ image
.sections
[i
].base_address
),
3661 if (diffs
++ >= 127) {
3662 command_print(CMD
, "More than 128 errors, the rest are not printed.");
3674 command_print(CMD
, "address " TARGET_ADDR_FMT
" length 0x%08zx",
3675 image
.sections
[i
].base_address
,
3680 image_size
+= buf_cnt
;
3683 command_print(CMD
, "No more differences found.");
3686 retval
= ERROR_FAIL
;
3687 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3688 command_print(CMD
, "verified %" PRIu32
" bytes "
3689 "in %fs (%0.3f KiB/s)", image_size
,
3690 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3693 image_close(&image
);
3698 COMMAND_HANDLER(handle_verify_image_checksum_command
)
3700 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_CHECKSUM_ONLY
);
3703 COMMAND_HANDLER(handle_verify_image_command
)
3705 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_VERIFY
);
3708 COMMAND_HANDLER(handle_test_image_command
)
3710 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_TEST
);
3713 static int handle_bp_command_list(struct command_invocation
*cmd
)
3715 struct target
*target
= get_current_target(cmd
->ctx
);
3716 struct breakpoint
*breakpoint
= target
->breakpoints
;
3717 while (breakpoint
) {
3718 if (breakpoint
->type
== BKPT_SOFT
) {
3719 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3720 breakpoint
->length
, 16);
3721 command_print(cmd
, "IVA breakpoint: " TARGET_ADDR_FMT
", 0x%x, %i, 0x%s",
3722 breakpoint
->address
,
3724 breakpoint
->set
, buf
);
3727 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3728 command_print(cmd
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3730 breakpoint
->length
, breakpoint
->set
);
3731 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3732 command_print(cmd
, "Hybrid breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3733 breakpoint
->address
,
3734 breakpoint
->length
, breakpoint
->set
);
3735 command_print(cmd
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3738 command_print(cmd
, "Breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3739 breakpoint
->address
,
3740 breakpoint
->length
, breakpoint
->set
);
3743 breakpoint
= breakpoint
->next
;
3748 static int handle_bp_command_set(struct command_invocation
*cmd
,
3749 target_addr_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3751 struct target
*target
= get_current_target(cmd
->ctx
);
3755 retval
= breakpoint_add(target
, addr
, length
, hw
);
3756 /* error is always logged in breakpoint_add(), do not print it again */
3757 if (ERROR_OK
== retval
)
3758 command_print(cmd
, "breakpoint set at " TARGET_ADDR_FMT
"", addr
);
3760 } else if (addr
== 0) {
3761 if (target
->type
->add_context_breakpoint
== NULL
) {
3762 LOG_ERROR("Context breakpoint not available");
3763 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
3765 retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3766 /* error is always logged in context_breakpoint_add(), do not print it again */
3767 if (ERROR_OK
== retval
)
3768 command_print(cmd
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3771 if (target
->type
->add_hybrid_breakpoint
== NULL
) {
3772 LOG_ERROR("Hybrid breakpoint not available");
3773 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
3775 retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3776 /* error is always logged in hybrid_breakpoint_add(), do not print it again */
3777 if (ERROR_OK
== retval
)
3778 command_print(cmd
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3783 COMMAND_HANDLER(handle_bp_command
)
3792 return handle_bp_command_list(CMD
);
3796 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3797 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3798 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
3801 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
3803 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3804 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3806 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
3807 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
3809 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
3810 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3812 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
3817 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3818 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
3819 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
3820 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
3823 return ERROR_COMMAND_SYNTAX_ERROR
;
3827 COMMAND_HANDLER(handle_rbp_command
)
3830 return ERROR_COMMAND_SYNTAX_ERROR
;
3832 struct target
*target
= get_current_target(CMD_CTX
);
3834 if (!strcmp(CMD_ARGV
[0], "all")) {
3835 breakpoint_remove_all(target
);
3838 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3840 breakpoint_remove(target
, addr
);
3846 COMMAND_HANDLER(handle_wp_command
)
3848 struct target
*target
= get_current_target(CMD_CTX
);
3850 if (CMD_ARGC
== 0) {
3851 struct watchpoint
*watchpoint
= target
->watchpoints
;
3853 while (watchpoint
) {
3854 command_print(CMD
, "address: " TARGET_ADDR_FMT
3855 ", len: 0x%8.8" PRIx32
3856 ", r/w/a: %i, value: 0x%8.8" PRIx32
3857 ", mask: 0x%8.8" PRIx32
,
3858 watchpoint
->address
,
3860 (int)watchpoint
->rw
,
3863 watchpoint
= watchpoint
->next
;
3868 enum watchpoint_rw type
= WPT_ACCESS
;
3870 uint32_t length
= 0;
3871 uint32_t data_value
= 0x0;
3872 uint32_t data_mask
= 0xffffffff;
3876 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
3879 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3882 switch (CMD_ARGV
[2][0]) {
3893 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3894 return ERROR_COMMAND_SYNTAX_ERROR
;
3898 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3899 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3903 return ERROR_COMMAND_SYNTAX_ERROR
;
3906 int retval
= watchpoint_add(target
, addr
, length
, type
,
3907 data_value
, data_mask
);
3908 if (ERROR_OK
!= retval
)
3909 LOG_ERROR("Failure setting watchpoints");
3914 COMMAND_HANDLER(handle_rwp_command
)
3917 return ERROR_COMMAND_SYNTAX_ERROR
;
3920 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3922 struct target
*target
= get_current_target(CMD_CTX
);
3923 watchpoint_remove(target
, addr
);
3929 * Translate a virtual address to a physical address.
3931 * The low-level target implementation must have logged a detailed error
3932 * which is forwarded to telnet/GDB session.
3934 COMMAND_HANDLER(handle_virt2phys_command
)
3937 return ERROR_COMMAND_SYNTAX_ERROR
;
3940 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], va
);
3943 struct target
*target
= get_current_target(CMD_CTX
);
3944 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3945 if (retval
== ERROR_OK
)
3946 command_print(CMD
, "Physical address " TARGET_ADDR_FMT
"", pa
);
3951 static void writeData(FILE *f
, const void *data
, size_t len
)
3953 size_t written
= fwrite(data
, 1, len
, f
);
3955 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3958 static void writeLong(FILE *f
, int l
, struct target
*target
)
3962 target_buffer_set_u32(target
, val
, l
);
3963 writeData(f
, val
, 4);
3966 static void writeString(FILE *f
, char *s
)
3968 writeData(f
, s
, strlen(s
));
3971 typedef unsigned char UNIT
[2]; /* unit of profiling */
3973 /* Dump a gmon.out histogram file. */
3974 static void write_gmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
, bool with_range
,
3975 uint32_t start_address
, uint32_t end_address
, struct target
*target
, uint32_t duration_ms
)
3978 FILE *f
= fopen(filename
, "w");
3981 writeString(f
, "gmon");
3982 writeLong(f
, 0x00000001, target
); /* Version */
3983 writeLong(f
, 0, target
); /* padding */
3984 writeLong(f
, 0, target
); /* padding */
3985 writeLong(f
, 0, target
); /* padding */
3987 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3988 writeData(f
, &zero
, 1);
3990 /* figure out bucket size */
3994 min
= start_address
;
3999 for (i
= 0; i
< sampleNum
; i
++) {
4000 if (min
> samples
[i
])
4002 if (max
< samples
[i
])
4006 /* max should be (largest sample + 1)
4007 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
4011 int addressSpace
= max
- min
;
4012 assert(addressSpace
>= 2);
4014 /* FIXME: What is the reasonable number of buckets?
4015 * The profiling result will be more accurate if there are enough buckets. */
4016 static const uint32_t maxBuckets
= 128 * 1024; /* maximum buckets. */
4017 uint32_t numBuckets
= addressSpace
/ sizeof(UNIT
);
4018 if (numBuckets
> maxBuckets
)
4019 numBuckets
= maxBuckets
;
4020 int *buckets
= malloc(sizeof(int) * numBuckets
);
4021 if (buckets
== NULL
) {
4025 memset(buckets
, 0, sizeof(int) * numBuckets
);
4026 for (i
= 0; i
< sampleNum
; i
++) {
4027 uint32_t address
= samples
[i
];
4029 if ((address
< min
) || (max
<= address
))
4032 long long a
= address
- min
;
4033 long long b
= numBuckets
;
4034 long long c
= addressSpace
;
4035 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
4039 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
4040 writeLong(f
, min
, target
); /* low_pc */
4041 writeLong(f
, max
, target
); /* high_pc */
4042 writeLong(f
, numBuckets
, target
); /* # of buckets */
4043 float sample_rate
= sampleNum
/ (duration_ms
/ 1000.0);
4044 writeLong(f
, sample_rate
, target
);
4045 writeString(f
, "seconds");
4046 for (i
= 0; i
< (15-strlen("seconds")); i
++)
4047 writeData(f
, &zero
, 1);
4048 writeString(f
, "s");
4050 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
4052 char *data
= malloc(2 * numBuckets
);
4054 for (i
= 0; i
< numBuckets
; i
++) {
4059 data
[i
* 2] = val
&0xff;
4060 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
4063 writeData(f
, data
, numBuckets
* 2);
4071 /* profiling samples the CPU PC as quickly as OpenOCD is able,
4072 * which will be used as a random sampling of PC */
4073 COMMAND_HANDLER(handle_profile_command
)
4075 struct target
*target
= get_current_target(CMD_CTX
);
4077 if ((CMD_ARGC
!= 2) && (CMD_ARGC
!= 4))
4078 return ERROR_COMMAND_SYNTAX_ERROR
;
4080 const uint32_t MAX_PROFILE_SAMPLE_NUM
= 10000;
4082 uint32_t num_of_samples
;
4083 int retval
= ERROR_OK
;
4085 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], offset
);
4087 uint32_t *samples
= malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM
);
4088 if (samples
== NULL
) {
4089 LOG_ERROR("No memory to store samples.");
4093 uint64_t timestart_ms
= timeval_ms();
4095 * Some cores let us sample the PC without the
4096 * annoying halt/resume step; for example, ARMv7 PCSR.
4097 * Provide a way to use that more efficient mechanism.
4099 retval
= target_profiling(target
, samples
, MAX_PROFILE_SAMPLE_NUM
,
4100 &num_of_samples
, offset
);
4101 if (retval
!= ERROR_OK
) {
4105 uint32_t duration_ms
= timeval_ms() - timestart_ms
;
4107 assert(num_of_samples
<= MAX_PROFILE_SAMPLE_NUM
);
4109 retval
= target_poll(target
);
4110 if (retval
!= ERROR_OK
) {
4114 if (target
->state
== TARGET_RUNNING
) {
4115 retval
= target_halt(target
);
4116 if (retval
!= ERROR_OK
) {
4122 retval
= target_poll(target
);
4123 if (retval
!= ERROR_OK
) {
4128 uint32_t start_address
= 0;
4129 uint32_t end_address
= 0;
4130 bool with_range
= false;
4131 if (CMD_ARGC
== 4) {
4133 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], start_address
);
4134 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], end_address
);
4137 write_gmon(samples
, num_of_samples
, CMD_ARGV
[1],
4138 with_range
, start_address
, end_address
, target
, duration_ms
);
4139 command_print(CMD
, "Wrote %s", CMD_ARGV
[1]);
4145 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
4148 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4151 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4155 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4156 valObjPtr
= Jim_NewIntObj(interp
, val
);
4157 if (!nameObjPtr
|| !valObjPtr
) {
4162 Jim_IncrRefCount(nameObjPtr
);
4163 Jim_IncrRefCount(valObjPtr
);
4164 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
4165 Jim_DecrRefCount(interp
, nameObjPtr
);
4166 Jim_DecrRefCount(interp
, valObjPtr
);
4168 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
4172 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4174 struct command_context
*context
;
4175 struct target
*target
;
4177 context
= current_command_context(interp
);
4178 assert(context
!= NULL
);
4180 target
= get_current_target(context
);
4181 if (target
== NULL
) {
4182 LOG_ERROR("mem2array: no current target");
4186 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4189 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
4197 const char *varname
;
4203 /* argv[1] = name of array to receive the data
4204 * argv[2] = desired width
4205 * argv[3] = memory address
4206 * argv[4] = count of times to read
4209 if (argc
< 4 || argc
> 5) {
4210 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4213 varname
= Jim_GetString(argv
[0], &len
);
4214 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4216 e
= Jim_GetLong(interp
, argv
[1], &l
);
4221 e
= Jim_GetLong(interp
, argv
[2], &l
);
4225 e
= Jim_GetLong(interp
, argv
[3], &l
);
4231 phys
= Jim_GetString(argv
[4], &n
);
4232 if (!strncmp(phys
, "phys", n
))
4248 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4249 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
4253 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4254 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
4257 if ((addr
+ (len
* width
)) < addr
) {
4258 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4259 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
4262 /* absurd transfer size? */
4264 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4265 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
4270 ((width
== 2) && ((addr
& 1) == 0)) ||
4271 ((width
== 4) && ((addr
& 3) == 0))) {
4275 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4276 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
4279 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4288 size_t buffersize
= 4096;
4289 uint8_t *buffer
= malloc(buffersize
);
4296 /* Slurp... in buffer size chunks */
4298 count
= len
; /* in objects.. */
4299 if (count
> (buffersize
/ width
))
4300 count
= (buffersize
/ width
);
4303 retval
= target_read_phys_memory(target
, addr
, width
, count
, buffer
);
4305 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
4306 if (retval
!= ERROR_OK
) {
4308 LOG_ERROR("mem2array: Read @ 0x%08" PRIx32
", w=%" PRId32
", cnt=%" PRId32
", failed",
4312 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4313 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
4317 v
= 0; /* shut up gcc */
4318 for (i
= 0; i
< count
; i
++, n
++) {
4321 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
4324 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
4327 v
= buffer
[i
] & 0x0ff;
4330 new_int_array_element(interp
, varname
, n
, v
);
4333 addr
+= count
* width
;
4339 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4344 static int get_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t *val
)
4347 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4351 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4355 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4361 Jim_IncrRefCount(nameObjPtr
);
4362 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
4363 Jim_DecrRefCount(interp
, nameObjPtr
);
4365 if (valObjPtr
== NULL
)
4368 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
4369 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
4374 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4376 struct command_context
*context
;
4377 struct target
*target
;
4379 context
= current_command_context(interp
);
4380 assert(context
!= NULL
);
4382 target
= get_current_target(context
);
4383 if (target
== NULL
) {
4384 LOG_ERROR("array2mem: no current target");
4388 return target_array2mem(interp
, target
, argc
-1, argv
+ 1);
4391 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
4392 int argc
, Jim_Obj
*const *argv
)
4400 const char *varname
;
4406 /* argv[1] = name of array to get the data
4407 * argv[2] = desired width
4408 * argv[3] = memory address
4409 * argv[4] = count to write
4411 if (argc
< 4 || argc
> 5) {
4412 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4415 varname
= Jim_GetString(argv
[0], &len
);
4416 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4418 e
= Jim_GetLong(interp
, argv
[1], &l
);
4423 e
= Jim_GetLong(interp
, argv
[2], &l
);
4427 e
= Jim_GetLong(interp
, argv
[3], &l
);
4433 phys
= Jim_GetString(argv
[4], &n
);
4434 if (!strncmp(phys
, "phys", n
))
4450 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4451 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4452 "Invalid width param, must be 8/16/32", NULL
);
4456 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4457 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4458 "array2mem: zero width read?", NULL
);
4461 if ((addr
+ (len
* width
)) < addr
) {
4462 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4463 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4464 "array2mem: addr + len - wraps to zero?", NULL
);
4467 /* absurd transfer size? */
4469 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4470 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4471 "array2mem: absurd > 64K item request", NULL
);
4476 ((width
== 2) && ((addr
& 1) == 0)) ||
4477 ((width
== 4) && ((addr
& 3) == 0))) {
4481 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4482 sprintf(buf
, "array2mem address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
4485 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4496 size_t buffersize
= 4096;
4497 uint8_t *buffer
= malloc(buffersize
);
4502 /* Slurp... in buffer size chunks */
4504 count
= len
; /* in objects.. */
4505 if (count
> (buffersize
/ width
))
4506 count
= (buffersize
/ width
);
4508 v
= 0; /* shut up gcc */
4509 for (i
= 0; i
< count
; i
++, n
++) {
4510 get_int_array_element(interp
, varname
, n
, &v
);
4513 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
4516 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
4519 buffer
[i
] = v
& 0x0ff;
4526 retval
= target_write_phys_memory(target
, addr
, width
, count
, buffer
);
4528 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
4529 if (retval
!= ERROR_OK
) {
4531 LOG_ERROR("array2mem: Write @ 0x%08" PRIx32
", w=%" PRId32
", cnt=%" PRId32
", failed",
4535 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4536 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
4540 addr
+= count
* width
;
4545 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4550 /* FIX? should we propagate errors here rather than printing them
4553 void target_handle_event(struct target
*target
, enum target_event e
)
4555 struct target_event_action
*teap
;
4558 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4559 if (teap
->event
== e
) {
4560 LOG_DEBUG("target(%d): %s (%s) event: %d (%s) action: %s",
4561 target
->target_number
,
4562 target_name(target
),
4563 target_type_name(target
),
4565 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
4566 Jim_GetString(teap
->body
, NULL
));
4568 /* Override current target by the target an event
4569 * is issued from (lot of scripts need it).
4570 * Return back to previous override as soon
4571 * as the handler processing is done */
4572 struct command_context
*cmd_ctx
= current_command_context(teap
->interp
);
4573 struct target
*saved_target_override
= cmd_ctx
->current_target_override
;
4574 cmd_ctx
->current_target_override
= target
;
4575 retval
= Jim_EvalObj(teap
->interp
, teap
->body
);
4577 if (retval
== JIM_RETURN
)
4578 retval
= teap
->interp
->returnCode
;
4580 if (retval
!= JIM_OK
) {
4581 Jim_MakeErrorMessage(teap
->interp
);
4582 LOG_USER("Error executing event %s on target %s:\n%s",
4583 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
4584 target_name(target
),
4585 Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
4586 /* clean both error code and stacktrace before return */
4587 Jim_Eval(teap
->interp
, "error \"\" \"\"");
4590 cmd_ctx
->current_target_override
= saved_target_override
;
4596 * Returns true only if the target has a handler for the specified event.
4598 bool target_has_event_action(struct target
*target
, enum target_event event
)
4600 struct target_event_action
*teap
;
4602 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4603 if (teap
->event
== event
)
4609 enum target_cfg_param
{
4612 TCFG_WORK_AREA_VIRT
,
4613 TCFG_WORK_AREA_PHYS
,
4614 TCFG_WORK_AREA_SIZE
,
4615 TCFG_WORK_AREA_BACKUP
,
4618 TCFG_CHAIN_POSITION
,
4625 static Jim_Nvp nvp_config_opts
[] = {
4626 { .name
= "-type", .value
= TCFG_TYPE
},
4627 { .name
= "-event", .value
= TCFG_EVENT
},
4628 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
4629 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
4630 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
4631 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
4632 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
4633 { .name
= "-coreid", .value
= TCFG_COREID
},
4634 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
4635 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
4636 { .name
= "-rtos", .value
= TCFG_RTOS
},
4637 { .name
= "-defer-examine", .value
= TCFG_DEFER_EXAMINE
},
4638 { .name
= "-gdb-port", .value
= TCFG_GDB_PORT
},
4639 { .name
= NULL
, .value
= -1 }
4642 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
4649 /* parse config or cget options ... */
4650 while (goi
->argc
> 0) {
4651 Jim_SetEmptyResult(goi
->interp
);
4652 /* Jim_GetOpt_Debug(goi); */
4654 if (target
->type
->target_jim_configure
) {
4655 /* target defines a configure function */
4656 /* target gets first dibs on parameters */
4657 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
4666 /* otherwise we 'continue' below */
4668 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
4670 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
4676 if (goi
->isconfigure
) {
4677 Jim_SetResultFormatted(goi
->interp
,
4678 "not settable: %s", n
->name
);
4682 if (goi
->argc
!= 0) {
4683 Jim_WrongNumArgs(goi
->interp
,
4684 goi
->argc
, goi
->argv
,
4689 Jim_SetResultString(goi
->interp
,
4690 target_type_name(target
), -1);
4694 if (goi
->argc
== 0) {
4695 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
4699 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
4701 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
4705 if (goi
->isconfigure
) {
4706 if (goi
->argc
!= 1) {
4707 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
4711 if (goi
->argc
!= 0) {
4712 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
4718 struct target_event_action
*teap
;
4720 teap
= target
->event_action
;
4721 /* replace existing? */
4723 if (teap
->event
== (enum target_event
)n
->value
)
4728 if (goi
->isconfigure
) {
4729 bool replace
= true;
4732 teap
= calloc(1, sizeof(*teap
));
4735 teap
->event
= n
->value
;
4736 teap
->interp
= goi
->interp
;
4737 Jim_GetOpt_Obj(goi
, &o
);
4739 Jim_DecrRefCount(teap
->interp
, teap
->body
);
4740 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
4743 * Tcl/TK - "tk events" have a nice feature.
4744 * See the "BIND" command.
4745 * We should support that here.
4746 * You can specify %X and %Y in the event code.
4747 * The idea is: %T - target name.
4748 * The idea is: %N - target number
4749 * The idea is: %E - event name.
4751 Jim_IncrRefCount(teap
->body
);
4754 /* add to head of event list */
4755 teap
->next
= target
->event_action
;
4756 target
->event_action
= teap
;
4758 Jim_SetEmptyResult(goi
->interp
);
4762 Jim_SetEmptyResult(goi
->interp
);
4764 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
4770 case TCFG_WORK_AREA_VIRT
:
4771 if (goi
->isconfigure
) {
4772 target_free_all_working_areas(target
);
4773 e
= Jim_GetOpt_Wide(goi
, &w
);
4776 target
->working_area_virt
= w
;
4777 target
->working_area_virt_spec
= true;
4782 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
4786 case TCFG_WORK_AREA_PHYS
:
4787 if (goi
->isconfigure
) {
4788 target_free_all_working_areas(target
);
4789 e
= Jim_GetOpt_Wide(goi
, &w
);
4792 target
->working_area_phys
= w
;
4793 target
->working_area_phys_spec
= true;
4798 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
4802 case TCFG_WORK_AREA_SIZE
:
4803 if (goi
->isconfigure
) {
4804 target_free_all_working_areas(target
);
4805 e
= Jim_GetOpt_Wide(goi
, &w
);
4808 target
->working_area_size
= w
;
4813 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4817 case TCFG_WORK_AREA_BACKUP
:
4818 if (goi
->isconfigure
) {
4819 target_free_all_working_areas(target
);
4820 e
= Jim_GetOpt_Wide(goi
, &w
);
4823 /* make this exactly 1 or 0 */
4824 target
->backup_working_area
= (!!w
);
4829 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
4830 /* loop for more e*/
4835 if (goi
->isconfigure
) {
4836 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
4838 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
4841 target
->endianness
= n
->value
;
4846 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4847 if (n
->name
== NULL
) {
4848 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4849 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4851 Jim_SetResultString(goi
->interp
, n
->name
, -1);
4856 if (goi
->isconfigure
) {
4857 e
= Jim_GetOpt_Wide(goi
, &w
);
4860 target
->coreid
= (int32_t)w
;
4865 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->coreid
));
4869 case TCFG_CHAIN_POSITION
:
4870 if (goi
->isconfigure
) {
4872 struct jtag_tap
*tap
;
4874 if (target
->has_dap
) {
4875 Jim_SetResultString(goi
->interp
,
4876 "target requires -dap parameter instead of -chain-position!", -1);
4880 target_free_all_working_areas(target
);
4881 e
= Jim_GetOpt_Obj(goi
, &o_t
);
4884 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
4888 target
->tap_configured
= true;
4893 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
4894 /* loop for more e*/
4897 if (goi
->isconfigure
) {
4898 e
= Jim_GetOpt_Wide(goi
, &w
);
4901 target
->dbgbase
= (uint32_t)w
;
4902 target
->dbgbase_set
= true;
4907 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
4913 int result
= rtos_create(goi
, target
);
4914 if (result
!= JIM_OK
)
4920 case TCFG_DEFER_EXAMINE
:
4922 target
->defer_examine
= true;
4927 if (goi
->isconfigure
) {
4928 struct command_context
*cmd_ctx
= current_command_context(goi
->interp
);
4929 if (cmd_ctx
->mode
!= COMMAND_CONFIG
) {
4930 Jim_SetResultString(goi
->interp
, "-gdb-port must be configured before 'init'", -1);
4935 e
= Jim_GetOpt_String(goi
, &s
, NULL
);
4938 target
->gdb_port_override
= strdup(s
);
4943 Jim_SetResultString(goi
->interp
, target
->gdb_port_override
? : "undefined", -1);
4947 } /* while (goi->argc) */
4950 /* done - we return */
4954 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4958 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4959 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4961 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4962 "missing: -option ...");
4965 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4966 return target_configure(&goi
, target
);
4969 static int jim_target_mem2array(Jim_Interp
*interp
,
4970 int argc
, Jim_Obj
*const *argv
)
4972 struct target
*target
= Jim_CmdPrivData(interp
);
4973 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4976 static int jim_target_array2mem(Jim_Interp
*interp
,
4977 int argc
, Jim_Obj
*const *argv
)
4979 struct target
*target
= Jim_CmdPrivData(interp
);
4980 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4983 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4985 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
4989 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4991 bool allow_defer
= false;
4994 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4996 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4997 Jim_SetResultFormatted(goi
.interp
,
4998 "usage: %s ['allow-defer']", cmd_name
);
5002 strcmp(Jim_GetString(argv
[1], NULL
), "allow-defer") == 0) {
5004 struct Jim_Obj
*obj
;
5005 int e
= Jim_GetOpt_Obj(&goi
, &obj
);
5011 struct target
*target
= Jim_CmdPrivData(interp
);
5012 if (!target
->tap
->enabled
)
5013 return jim_target_tap_disabled(interp
);
5015 if (allow_defer
&& target
->defer_examine
) {
5016 LOG_INFO("Deferring arp_examine of %s", target_name(target
));
5017 LOG_INFO("Use arp_examine command to examine it manually!");
5021 int e
= target
->type
->examine(target
);
5027 static int jim_target_was_examined(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5029 struct target
*target
= Jim_CmdPrivData(interp
);
5031 Jim_SetResultBool(interp
, target_was_examined(target
));
5035 static int jim_target_examine_deferred(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5037 struct target
*target
= Jim_CmdPrivData(interp
);
5039 Jim_SetResultBool(interp
, target
->defer_examine
);
5043 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5046 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5049 struct target
*target
= Jim_CmdPrivData(interp
);
5051 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
5057 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5060 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5063 struct target
*target
= Jim_CmdPrivData(interp
);
5064 if (!target
->tap
->enabled
)
5065 return jim_target_tap_disabled(interp
);
5068 if (!(target_was_examined(target
)))
5069 e
= ERROR_TARGET_NOT_EXAMINED
;
5071 e
= target
->type
->poll(target
);
5077 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5080 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5082 if (goi
.argc
!= 2) {
5083 Jim_WrongNumArgs(interp
, 0, argv
,
5084 "([tT]|[fF]|assert|deassert) BOOL");
5089 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
5091 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
5094 /* the halt or not param */
5096 e
= Jim_GetOpt_Wide(&goi
, &a
);
5100 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
5101 if (!target
->tap
->enabled
)
5102 return jim_target_tap_disabled(interp
);
5104 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
5105 Jim_SetResultFormatted(interp
,
5106 "No target-specific reset for %s",
5107 target_name(target
));
5111 if (target
->defer_examine
)
5112 target_reset_examined(target
);
5114 /* determine if we should halt or not. */
5115 target
->reset_halt
= !!a
;
5116 /* When this happens - all workareas are invalid. */
5117 target_free_all_working_areas_restore(target
, 0);
5120 if (n
->value
== NVP_ASSERT
)
5121 e
= target
->type
->assert_reset(target
);
5123 e
= target
->type
->deassert_reset(target
);
5124 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5127 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5130 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5133 struct target
*target
= Jim_CmdPrivData(interp
);
5134 if (!target
->tap
->enabled
)
5135 return jim_target_tap_disabled(interp
);
5136 int e
= target
->type
->halt(target
);
5137 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5140 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5143 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5145 /* params: <name> statename timeoutmsecs */
5146 if (goi
.argc
!= 2) {
5147 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5148 Jim_SetResultFormatted(goi
.interp
,
5149 "%s <state_name> <timeout_in_msec>", cmd_name
);
5154 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
5156 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
, 1);
5160 e
= Jim_GetOpt_Wide(&goi
, &a
);
5163 struct target
*target
= Jim_CmdPrivData(interp
);
5164 if (!target
->tap
->enabled
)
5165 return jim_target_tap_disabled(interp
);
5167 e
= target_wait_state(target
, n
->value
, a
);
5168 if (e
!= ERROR_OK
) {
5169 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
5170 Jim_SetResultFormatted(goi
.interp
,
5171 "target: %s wait %s fails (%#s) %s",
5172 target_name(target
), n
->name
,
5173 eObj
, target_strerror_safe(e
));
5178 /* List for human, Events defined for this target.
5179 * scripts/programs should use 'name cget -event NAME'
5181 COMMAND_HANDLER(handle_target_event_list
)
5183 struct target
*target
= get_current_target(CMD_CTX
);
5184 struct target_event_action
*teap
= target
->event_action
;
5186 command_print(CMD
, "Event actions for target (%d) %s\n",
5187 target
->target_number
,
5188 target_name(target
));
5189 command_print(CMD
, "%-25s | Body", "Event");
5190 command_print(CMD
, "------------------------- | "
5191 "----------------------------------------");
5193 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
5194 command_print(CMD
, "%-25s | %s",
5195 opt
->name
, Jim_GetString(teap
->body
, NULL
));
5198 command_print(CMD
, "***END***");
5201 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5204 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5207 struct target
*target
= Jim_CmdPrivData(interp
);
5208 Jim_SetResultString(interp
, target_state_name(target
), -1);
5211 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5214 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5215 if (goi
.argc
!= 1) {
5216 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5217 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
5221 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
5223 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
5226 struct target
*target
= Jim_CmdPrivData(interp
);
5227 target_handle_event(target
, n
->value
);
5231 static const struct command_registration target_instance_command_handlers
[] = {
5233 .name
= "configure",
5234 .mode
= COMMAND_ANY
,
5235 .jim_handler
= jim_target_configure
,
5236 .help
= "configure a new target for use",
5237 .usage
= "[target_attribute ...]",
5241 .mode
= COMMAND_ANY
,
5242 .jim_handler
= jim_target_configure
,
5243 .help
= "returns the specified target attribute",
5244 .usage
= "target_attribute",
5248 .handler
= handle_mw_command
,
5249 .mode
= COMMAND_EXEC
,
5250 .help
= "Write 64-bit word(s) to target memory",
5251 .usage
= "address data [count]",
5255 .handler
= handle_mw_command
,
5256 .mode
= COMMAND_EXEC
,
5257 .help
= "Write 32-bit word(s) to target memory",
5258 .usage
= "address data [count]",
5262 .handler
= handle_mw_command
,
5263 .mode
= COMMAND_EXEC
,
5264 .help
= "Write 16-bit half-word(s) to target memory",
5265 .usage
= "address data [count]",
5269 .handler
= handle_mw_command
,
5270 .mode
= COMMAND_EXEC
,
5271 .help
= "Write byte(s) to target memory",
5272 .usage
= "address data [count]",
5276 .handler
= handle_md_command
,
5277 .mode
= COMMAND_EXEC
,
5278 .help
= "Display target memory as 64-bit words",
5279 .usage
= "address [count]",
5283 .handler
= handle_md_command
,
5284 .mode
= COMMAND_EXEC
,
5285 .help
= "Display target memory as 32-bit words",
5286 .usage
= "address [count]",
5290 .handler
= handle_md_command
,
5291 .mode
= COMMAND_EXEC
,
5292 .help
= "Display target memory as 16-bit half-words",
5293 .usage
= "address [count]",
5297 .handler
= handle_md_command
,
5298 .mode
= COMMAND_EXEC
,
5299 .help
= "Display target memory as 8-bit bytes",
5300 .usage
= "address [count]",
5303 .name
= "array2mem",
5304 .mode
= COMMAND_EXEC
,
5305 .jim_handler
= jim_target_array2mem
,
5306 .help
= "Writes Tcl array of 8/16/32 bit numbers "
5308 .usage
= "arrayname bitwidth address count",
5311 .name
= "mem2array",
5312 .mode
= COMMAND_EXEC
,
5313 .jim_handler
= jim_target_mem2array
,
5314 .help
= "Loads Tcl array of 8/16/32 bit numbers "
5315 "from target memory",
5316 .usage
= "arrayname bitwidth address count",
5319 .name
= "eventlist",
5320 .handler
= handle_target_event_list
,
5321 .mode
= COMMAND_EXEC
,
5322 .help
= "displays a table of events defined for this target",
5327 .mode
= COMMAND_EXEC
,
5328 .jim_handler
= jim_target_current_state
,
5329 .help
= "displays the current state of this target",
5332 .name
= "arp_examine",
5333 .mode
= COMMAND_EXEC
,
5334 .jim_handler
= jim_target_examine
,
5335 .help
= "used internally for reset processing",
5336 .usage
= "['allow-defer']",
5339 .name
= "was_examined",
5340 .mode
= COMMAND_EXEC
,
5341 .jim_handler
= jim_target_was_examined
,
5342 .help
= "used internally for reset processing",
5345 .name
= "examine_deferred",
5346 .mode
= COMMAND_EXEC
,
5347 .jim_handler
= jim_target_examine_deferred
,
5348 .help
= "used internally for reset processing",
5351 .name
= "arp_halt_gdb",
5352 .mode
= COMMAND_EXEC
,
5353 .jim_handler
= jim_target_halt_gdb
,
5354 .help
= "used internally for reset processing to halt GDB",
5358 .mode
= COMMAND_EXEC
,
5359 .jim_handler
= jim_target_poll
,
5360 .help
= "used internally for reset processing",
5363 .name
= "arp_reset",
5364 .mode
= COMMAND_EXEC
,
5365 .jim_handler
= jim_target_reset
,
5366 .help
= "used internally for reset processing",
5370 .mode
= COMMAND_EXEC
,
5371 .jim_handler
= jim_target_halt
,
5372 .help
= "used internally for reset processing",
5375 .name
= "arp_waitstate",
5376 .mode
= COMMAND_EXEC
,
5377 .jim_handler
= jim_target_wait_state
,
5378 .help
= "used internally for reset processing",
5381 .name
= "invoke-event",
5382 .mode
= COMMAND_EXEC
,
5383 .jim_handler
= jim_target_invoke_event
,
5384 .help
= "invoke handler for specified event",
5385 .usage
= "event_name",
5387 COMMAND_REGISTRATION_DONE
5390 static int target_create(Jim_GetOptInfo
*goi
)
5397 struct target
*target
;
5398 struct command_context
*cmd_ctx
;
5400 cmd_ctx
= current_command_context(goi
->interp
);
5401 assert(cmd_ctx
!= NULL
);
5403 if (goi
->argc
< 3) {
5404 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
5409 Jim_GetOpt_Obj(goi
, &new_cmd
);
5410 /* does this command exist? */
5411 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
5413 cp
= Jim_GetString(new_cmd
, NULL
);
5414 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
5419 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
5422 struct transport
*tr
= get_current_transport();
5423 if (tr
->override_target
) {
5424 e
= tr
->override_target(&cp
);
5425 if (e
!= ERROR_OK
) {
5426 LOG_ERROR("The selected transport doesn't support this target");
5429 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
5431 /* now does target type exist */
5432 for (x
= 0 ; target_types
[x
] ; x
++) {
5433 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
5438 /* check for deprecated name */
5439 if (target_types
[x
]->deprecated_name
) {
5440 if (0 == strcmp(cp
, target_types
[x
]->deprecated_name
)) {
5442 LOG_WARNING("target name is deprecated use: \'%s\'", target_types
[x
]->name
);
5447 if (target_types
[x
] == NULL
) {
5448 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
5449 for (x
= 0 ; target_types
[x
] ; x
++) {
5450 if (target_types
[x
+ 1]) {
5451 Jim_AppendStrings(goi
->interp
,
5452 Jim_GetResult(goi
->interp
),
5453 target_types
[x
]->name
,
5456 Jim_AppendStrings(goi
->interp
,
5457 Jim_GetResult(goi
->interp
),
5459 target_types
[x
]->name
, NULL
);
5466 target
= calloc(1, sizeof(struct target
));
5467 /* set target number */
5468 target
->target_number
= new_target_number();
5469 cmd_ctx
->current_target
= target
;
5471 /* allocate memory for each unique target type */
5472 target
->type
= calloc(1, sizeof(struct target_type
));
5474 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
5476 /* will be set by "-endian" */
5477 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5479 /* default to first core, override with -coreid */
5482 target
->working_area
= 0x0;
5483 target
->working_area_size
= 0x0;
5484 target
->working_areas
= NULL
;
5485 target
->backup_working_area
= 0;
5487 target
->state
= TARGET_UNKNOWN
;
5488 target
->debug_reason
= DBG_REASON_UNDEFINED
;
5489 target
->reg_cache
= NULL
;
5490 target
->breakpoints
= NULL
;
5491 target
->watchpoints
= NULL
;
5492 target
->next
= NULL
;
5493 target
->arch_info
= NULL
;
5495 target
->verbose_halt_msg
= true;
5497 target
->halt_issued
= false;
5499 /* initialize trace information */
5500 target
->trace_info
= calloc(1, sizeof(struct trace
));
5502 target
->dbgmsg
= NULL
;
5503 target
->dbg_msg_enabled
= 0;
5505 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5507 target
->rtos
= NULL
;
5508 target
->rtos_auto_detect
= false;
5510 target
->gdb_port_override
= NULL
;
5512 /* Do the rest as "configure" options */
5513 goi
->isconfigure
= 1;
5514 e
= target_configure(goi
, target
);
5517 if (target
->has_dap
) {
5518 if (!target
->dap_configured
) {
5519 Jim_SetResultString(goi
->interp
, "-dap ?name? required when creating target", -1);
5523 if (!target
->tap_configured
) {
5524 Jim_SetResultString(goi
->interp
, "-chain-position ?name? required when creating target", -1);
5528 /* tap must be set after target was configured */
5529 if (target
->tap
== NULL
)
5534 free(target
->gdb_port_override
);
5540 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
5541 /* default endian to little if not specified */
5542 target
->endianness
= TARGET_LITTLE_ENDIAN
;
5545 cp
= Jim_GetString(new_cmd
, NULL
);
5546 target
->cmd_name
= strdup(cp
);
5548 if (target
->type
->target_create
) {
5549 e
= (*(target
->type
->target_create
))(target
, goi
->interp
);
5550 if (e
!= ERROR_OK
) {
5551 LOG_DEBUG("target_create failed");
5552 free(target
->gdb_port_override
);
5554 free(target
->cmd_name
);
5560 /* create the target specific commands */
5561 if (target
->type
->commands
) {
5562 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
5564 LOG_ERROR("unable to register '%s' commands", cp
);
5567 /* append to end of list */
5569 struct target
**tpp
;
5570 tpp
= &(all_targets
);
5572 tpp
= &((*tpp
)->next
);
5576 /* now - create the new target name command */
5577 const struct command_registration target_subcommands
[] = {
5579 .chain
= target_instance_command_handlers
,
5582 .chain
= target
->type
->commands
,
5584 COMMAND_REGISTRATION_DONE
5586 const struct command_registration target_commands
[] = {
5589 .mode
= COMMAND_ANY
,
5590 .help
= "target command group",
5592 .chain
= target_subcommands
,
5594 COMMAND_REGISTRATION_DONE
5596 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
5600 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
5602 command_set_handler_data(c
, target
);
5604 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
5607 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5610 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5613 struct command_context
*cmd_ctx
= current_command_context(interp
);
5614 assert(cmd_ctx
!= NULL
);
5616 Jim_SetResultString(interp
, target_name(get_current_target(cmd_ctx
)), -1);
5620 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5623 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5626 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5627 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++) {
5628 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5629 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
5634 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5637 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5640 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5641 struct target
*target
= all_targets
;
5643 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5644 Jim_NewStringObj(interp
, target_name(target
), -1));
5645 target
= target
->next
;
5650 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5653 const char *targetname
;
5655 struct target
*target
= (struct target
*) NULL
;
5656 struct target_list
*head
, *curr
, *new;
5657 curr
= (struct target_list
*) NULL
;
5658 head
= (struct target_list
*) NULL
;
5661 LOG_DEBUG("%d", argc
);
5662 /* argv[1] = target to associate in smp
5663 * argv[2] = target to assoicate in smp
5667 for (i
= 1; i
< argc
; i
++) {
5669 targetname
= Jim_GetString(argv
[i
], &len
);
5670 target
= get_target(targetname
);
5671 LOG_DEBUG("%s ", targetname
);
5673 new = malloc(sizeof(struct target_list
));
5674 new->target
= target
;
5675 new->next
= (struct target_list
*)NULL
;
5676 if (head
== (struct target_list
*)NULL
) {
5685 /* now parse the list of cpu and put the target in smp mode*/
5688 while (curr
!= (struct target_list
*)NULL
) {
5689 target
= curr
->target
;
5691 target
->head
= head
;
5695 if (target
&& target
->rtos
)
5696 retval
= rtos_smp_init(head
->target
);
5702 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5705 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5707 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5708 "<name> <target_type> [<target_options> ...]");
5711 return target_create(&goi
);
5714 static const struct command_registration target_subcommand_handlers
[] = {
5717 .mode
= COMMAND_CONFIG
,
5718 .handler
= handle_target_init_command
,
5719 .help
= "initialize targets",
5724 .mode
= COMMAND_CONFIG
,
5725 .jim_handler
= jim_target_create
,
5726 .usage
= "name type '-chain-position' name [options ...]",
5727 .help
= "Creates and selects a new target",
5731 .mode
= COMMAND_ANY
,
5732 .jim_handler
= jim_target_current
,
5733 .help
= "Returns the currently selected target",
5737 .mode
= COMMAND_ANY
,
5738 .jim_handler
= jim_target_types
,
5739 .help
= "Returns the available target types as "
5740 "a list of strings",
5744 .mode
= COMMAND_ANY
,
5745 .jim_handler
= jim_target_names
,
5746 .help
= "Returns the names of all targets as a list of strings",
5750 .mode
= COMMAND_ANY
,
5751 .jim_handler
= jim_target_smp
,
5752 .usage
= "targetname1 targetname2 ...",
5753 .help
= "gather several target in a smp list"
5756 COMMAND_REGISTRATION_DONE
5760 target_addr_t address
;
5766 static int fastload_num
;
5767 static struct FastLoad
*fastload
;
5769 static void free_fastload(void)
5771 if (fastload
!= NULL
) {
5773 for (i
= 0; i
< fastload_num
; i
++) {
5774 if (fastload
[i
].data
)
5775 free(fastload
[i
].data
);
5782 COMMAND_HANDLER(handle_fast_load_image_command
)
5786 uint32_t image_size
;
5787 target_addr_t min_address
= 0;
5788 target_addr_t max_address
= -1;
5793 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
5794 &image
, &min_address
, &max_address
);
5795 if (ERROR_OK
!= retval
)
5798 struct duration bench
;
5799 duration_start(&bench
);
5801 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5802 if (retval
!= ERROR_OK
)
5807 fastload_num
= image
.num_sections
;
5808 fastload
= malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5809 if (fastload
== NULL
) {
5810 command_print(CMD
, "out of memory");
5811 image_close(&image
);
5814 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5815 for (i
= 0; i
< image
.num_sections
; i
++) {
5816 buffer
= malloc(image
.sections
[i
].size
);
5817 if (buffer
== NULL
) {
5818 command_print(CMD
, "error allocating buffer for section (%d bytes)",
5819 (int)(image
.sections
[i
].size
));
5820 retval
= ERROR_FAIL
;
5824 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
5825 if (retval
!= ERROR_OK
) {
5830 uint32_t offset
= 0;
5831 uint32_t length
= buf_cnt
;
5833 /* DANGER!!! beware of unsigned comparision here!!! */
5835 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
5836 (image
.sections
[i
].base_address
< max_address
)) {
5837 if (image
.sections
[i
].base_address
< min_address
) {
5838 /* clip addresses below */
5839 offset
+= min_address
-image
.sections
[i
].base_address
;
5843 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5844 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5846 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5847 fastload
[i
].data
= malloc(length
);
5848 if (fastload
[i
].data
== NULL
) {
5850 command_print(CMD
, "error allocating buffer for section (%" PRIu32
" bytes)",
5852 retval
= ERROR_FAIL
;
5855 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5856 fastload
[i
].length
= length
;
5858 image_size
+= length
;
5859 command_print(CMD
, "%u bytes written at address 0x%8.8x",
5860 (unsigned int)length
,
5861 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
5867 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
5868 command_print(CMD
, "Loaded %" PRIu32
" bytes "
5869 "in %fs (%0.3f KiB/s)", image_size
,
5870 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
5873 "WARNING: image has not been loaded to target!"
5874 "You can issue a 'fast_load' to finish loading.");
5877 image_close(&image
);
5879 if (retval
!= ERROR_OK
)
5885 COMMAND_HANDLER(handle_fast_load_command
)
5888 return ERROR_COMMAND_SYNTAX_ERROR
;
5889 if (fastload
== NULL
) {
5890 LOG_ERROR("No image in memory");
5894 int64_t ms
= timeval_ms();
5896 int retval
= ERROR_OK
;
5897 for (i
= 0; i
< fastload_num
; i
++) {
5898 struct target
*target
= get_current_target(CMD_CTX
);
5899 command_print(CMD
, "Write to 0x%08x, length 0x%08x",
5900 (unsigned int)(fastload
[i
].address
),
5901 (unsigned int)(fastload
[i
].length
));
5902 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5903 if (retval
!= ERROR_OK
)
5905 size
+= fastload
[i
].length
;
5907 if (retval
== ERROR_OK
) {
5908 int64_t after
= timeval_ms();
5909 command_print(CMD
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5914 static const struct command_registration target_command_handlers
[] = {
5917 .handler
= handle_targets_command
,
5918 .mode
= COMMAND_ANY
,
5919 .help
= "change current default target (one parameter) "
5920 "or prints table of all targets (no parameters)",
5921 .usage
= "[target]",
5925 .mode
= COMMAND_CONFIG
,
5926 .help
= "configure target",
5927 .chain
= target_subcommand_handlers
,
5930 COMMAND_REGISTRATION_DONE
5933 int target_register_commands(struct command_context
*cmd_ctx
)
5935 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5938 static bool target_reset_nag
= true;
5940 bool get_target_reset_nag(void)
5942 return target_reset_nag
;
5945 COMMAND_HANDLER(handle_target_reset_nag
)
5947 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
5948 &target_reset_nag
, "Nag after each reset about options to improve "
5952 COMMAND_HANDLER(handle_ps_command
)
5954 struct target
*target
= get_current_target(CMD_CTX
);
5956 if (target
->state
!= TARGET_HALTED
) {
5957 LOG_INFO("target not halted !!");
5961 if ((target
->rtos
) && (target
->rtos
->type
)
5962 && (target
->rtos
->type
->ps_command
)) {
5963 display
= target
->rtos
->type
->ps_command(target
);
5964 command_print(CMD
, "%s", display
);
5969 return ERROR_TARGET_FAILURE
;
5973 static void binprint(struct command_invocation
*cmd
, const char *text
, const uint8_t *buf
, int size
)
5976 command_print_sameline(cmd
, "%s", text
);
5977 for (int i
= 0; i
< size
; i
++)
5978 command_print_sameline(cmd
, " %02x", buf
[i
]);
5979 command_print(cmd
, " ");
5982 COMMAND_HANDLER(handle_test_mem_access_command
)
5984 struct target
*target
= get_current_target(CMD_CTX
);
5986 int retval
= ERROR_OK
;
5988 if (target
->state
!= TARGET_HALTED
) {
5989 LOG_INFO("target not halted !!");
5994 return ERROR_COMMAND_SYNTAX_ERROR
;
5996 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], test_size
);
5999 size_t num_bytes
= test_size
+ 4;
6001 struct working_area
*wa
= NULL
;
6002 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6003 if (retval
!= ERROR_OK
) {
6004 LOG_ERROR("Not enough working area");
6008 uint8_t *test_pattern
= malloc(num_bytes
);
6010 for (size_t i
= 0; i
< num_bytes
; i
++)
6011 test_pattern
[i
] = rand();
6013 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6014 if (retval
!= ERROR_OK
) {
6015 LOG_ERROR("Test pattern write failed");
6019 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6020 for (int size
= 1; size
<= 4; size
*= 2) {
6021 for (int offset
= 0; offset
< 4; offset
++) {
6022 uint32_t count
= test_size
/ size
;
6023 size_t host_bufsiz
= (count
+ 2) * size
+ host_offset
;
6024 uint8_t *read_ref
= malloc(host_bufsiz
);
6025 uint8_t *read_buf
= malloc(host_bufsiz
);
6027 for (size_t i
= 0; i
< host_bufsiz
; i
++) {
6028 read_ref
[i
] = rand();
6029 read_buf
[i
] = read_ref
[i
];
6031 command_print_sameline(CMD
,
6032 "Test read %" PRIu32
" x %d @ %d to %saligned buffer: ", count
,
6033 size
, offset
, host_offset
? "un" : "");
6035 struct duration bench
;
6036 duration_start(&bench
);
6038 retval
= target_read_memory(target
, wa
->address
+ offset
, size
, count
,
6039 read_buf
+ size
+ host_offset
);
6041 duration_measure(&bench
);
6043 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6044 command_print(CMD
, "Unsupported alignment");
6046 } else if (retval
!= ERROR_OK
) {
6047 command_print(CMD
, "Memory read failed");
6051 /* replay on host */
6052 memcpy(read_ref
+ size
+ host_offset
, test_pattern
+ offset
, count
* size
);
6055 int result
= memcmp(read_ref
, read_buf
, host_bufsiz
);
6057 command_print(CMD
, "Pass in %fs (%0.3f KiB/s)",
6058 duration_elapsed(&bench
),
6059 duration_kbps(&bench
, count
* size
));
6061 command_print(CMD
, "Compare failed");
6062 binprint(CMD
, "ref:", read_ref
, host_bufsiz
);
6063 binprint(CMD
, "buf:", read_buf
, host_bufsiz
);
6076 target_free_working_area(target
, wa
);
6079 num_bytes
= test_size
+ 4 + 4 + 4;
6081 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6082 if (retval
!= ERROR_OK
) {
6083 LOG_ERROR("Not enough working area");
6087 test_pattern
= malloc(num_bytes
);
6089 for (size_t i
= 0; i
< num_bytes
; i
++)
6090 test_pattern
[i
] = rand();
6092 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6093 for (int size
= 1; size
<= 4; size
*= 2) {
6094 for (int offset
= 0; offset
< 4; offset
++) {
6095 uint32_t count
= test_size
/ size
;
6096 size_t host_bufsiz
= count
* size
+ host_offset
;
6097 uint8_t *read_ref
= malloc(num_bytes
);
6098 uint8_t *read_buf
= malloc(num_bytes
);
6099 uint8_t *write_buf
= malloc(host_bufsiz
);
6101 for (size_t i
= 0; i
< host_bufsiz
; i
++)
6102 write_buf
[i
] = rand();
6103 command_print_sameline(CMD
,
6104 "Test write %" PRIu32
" x %d @ %d from %saligned buffer: ", count
,
6105 size
, offset
, host_offset
? "un" : "");
6107 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6108 if (retval
!= ERROR_OK
) {
6109 command_print(CMD
, "Test pattern write failed");
6113 /* replay on host */
6114 memcpy(read_ref
, test_pattern
, num_bytes
);
6115 memcpy(read_ref
+ size
+ offset
, write_buf
+ host_offset
, count
* size
);
6117 struct duration bench
;
6118 duration_start(&bench
);
6120 retval
= target_write_memory(target
, wa
->address
+ size
+ offset
, size
, count
,
6121 write_buf
+ host_offset
);
6123 duration_measure(&bench
);
6125 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6126 command_print(CMD
, "Unsupported alignment");
6128 } else if (retval
!= ERROR_OK
) {
6129 command_print(CMD
, "Memory write failed");
6134 retval
= target_read_memory(target
, wa
->address
, 1, num_bytes
, read_buf
);
6135 if (retval
!= ERROR_OK
) {
6136 command_print(CMD
, "Test pattern write failed");
6141 int result
= memcmp(read_ref
, read_buf
, num_bytes
);
6143 command_print(CMD
, "Pass in %fs (%0.3f KiB/s)",
6144 duration_elapsed(&bench
),
6145 duration_kbps(&bench
, count
* size
));
6147 command_print(CMD
, "Compare failed");
6148 binprint(CMD
, "ref:", read_ref
, num_bytes
);
6149 binprint(CMD
, "buf:", read_buf
, num_bytes
);
6161 target_free_working_area(target
, wa
);
6165 static const struct command_registration target_exec_command_handlers
[] = {
6167 .name
= "fast_load_image",
6168 .handler
= handle_fast_load_image_command
,
6169 .mode
= COMMAND_ANY
,
6170 .help
= "Load image into server memory for later use by "
6171 "fast_load; primarily for profiling",
6172 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6173 "[min_address [max_length]]",
6176 .name
= "fast_load",
6177 .handler
= handle_fast_load_command
,
6178 .mode
= COMMAND_EXEC
,
6179 .help
= "loads active fast load image to current target "
6180 "- mainly for profiling purposes",
6185 .handler
= handle_profile_command
,
6186 .mode
= COMMAND_EXEC
,
6187 .usage
= "seconds filename [start end]",
6188 .help
= "profiling samples the CPU PC",
6190 /** @todo don't register virt2phys() unless target supports it */
6192 .name
= "virt2phys",
6193 .handler
= handle_virt2phys_command
,
6194 .mode
= COMMAND_ANY
,
6195 .help
= "translate a virtual address into a physical address",
6196 .usage
= "virtual_address",
6200 .handler
= handle_reg_command
,
6201 .mode
= COMMAND_EXEC
,
6202 .help
= "display (reread from target with \"force\") or set a register; "
6203 "with no arguments, displays all registers and their values",
6204 .usage
= "[(register_number|register_name) [(value|'force')]]",
6208 .handler
= handle_poll_command
,
6209 .mode
= COMMAND_EXEC
,
6210 .help
= "poll target state; or reconfigure background polling",
6211 .usage
= "['on'|'off']",
6214 .name
= "wait_halt",
6215 .handler
= handle_wait_halt_command
,
6216 .mode
= COMMAND_EXEC
,
6217 .help
= "wait up to the specified number of milliseconds "
6218 "(default 5000) for a previously requested halt",
6219 .usage
= "[milliseconds]",
6223 .handler
= handle_halt_command
,
6224 .mode
= COMMAND_EXEC
,
6225 .help
= "request target to halt, then wait up to the specified"
6226 "number of milliseconds (default 5000) for it to complete",
6227 .usage
= "[milliseconds]",
6231 .handler
= handle_resume_command
,
6232 .mode
= COMMAND_EXEC
,
6233 .help
= "resume target execution from current PC or address",
6234 .usage
= "[address]",
6238 .handler
= handle_reset_command
,
6239 .mode
= COMMAND_EXEC
,
6240 .usage
= "[run|halt|init]",
6241 .help
= "Reset all targets into the specified mode."
6242 "Default reset mode is run, if not given.",
6245 .name
= "soft_reset_halt",
6246 .handler
= handle_soft_reset_halt_command
,
6247 .mode
= COMMAND_EXEC
,
6249 .help
= "halt the target and do a soft reset",
6253 .handler
= handle_step_command
,
6254 .mode
= COMMAND_EXEC
,
6255 .help
= "step one instruction from current PC or address",
6256 .usage
= "[address]",
6260 .handler
= handle_md_command
,
6261 .mode
= COMMAND_EXEC
,
6262 .help
= "display memory double-words",
6263 .usage
= "['phys'] address [count]",
6267 .handler
= handle_md_command
,
6268 .mode
= COMMAND_EXEC
,
6269 .help
= "display memory words",
6270 .usage
= "['phys'] address [count]",
6274 .handler
= handle_md_command
,
6275 .mode
= COMMAND_EXEC
,
6276 .help
= "display memory half-words",
6277 .usage
= "['phys'] address [count]",
6281 .handler
= handle_md_command
,
6282 .mode
= COMMAND_EXEC
,
6283 .help
= "display memory bytes",
6284 .usage
= "['phys'] address [count]",
6288 .handler
= handle_mw_command
,
6289 .mode
= COMMAND_EXEC
,
6290 .help
= "write memory double-word",
6291 .usage
= "['phys'] address value [count]",
6295 .handler
= handle_mw_command
,
6296 .mode
= COMMAND_EXEC
,
6297 .help
= "write memory word",
6298 .usage
= "['phys'] address value [count]",
6302 .handler
= handle_mw_command
,
6303 .mode
= COMMAND_EXEC
,
6304 .help
= "write memory half-word",
6305 .usage
= "['phys'] address value [count]",
6309 .handler
= handle_mw_command
,
6310 .mode
= COMMAND_EXEC
,
6311 .help
= "write memory byte",
6312 .usage
= "['phys'] address value [count]",
6316 .handler
= handle_bp_command
,
6317 .mode
= COMMAND_EXEC
,
6318 .help
= "list or set hardware or software breakpoint",
6319 .usage
= "[<address> [<asid>] <length> ['hw'|'hw_ctx']]",
6323 .handler
= handle_rbp_command
,
6324 .mode
= COMMAND_EXEC
,
6325 .help
= "remove breakpoint",
6326 .usage
= "'all' | address",
6330 .handler
= handle_wp_command
,
6331 .mode
= COMMAND_EXEC
,
6332 .help
= "list (no params) or create watchpoints",
6333 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
6337 .handler
= handle_rwp_command
,
6338 .mode
= COMMAND_EXEC
,
6339 .help
= "remove watchpoint",
6343 .name
= "load_image",
6344 .handler
= handle_load_image_command
,
6345 .mode
= COMMAND_EXEC
,
6346 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6347 "[min_address] [max_length]",
6350 .name
= "dump_image",
6351 .handler
= handle_dump_image_command
,
6352 .mode
= COMMAND_EXEC
,
6353 .usage
= "filename address size",
6356 .name
= "verify_image_checksum",
6357 .handler
= handle_verify_image_checksum_command
,
6358 .mode
= COMMAND_EXEC
,
6359 .usage
= "filename [offset [type]]",
6362 .name
= "verify_image",
6363 .handler
= handle_verify_image_command
,
6364 .mode
= COMMAND_EXEC
,
6365 .usage
= "filename [offset [type]]",
6368 .name
= "test_image",
6369 .handler
= handle_test_image_command
,
6370 .mode
= COMMAND_EXEC
,
6371 .usage
= "filename [offset [type]]",
6374 .name
= "mem2array",
6375 .mode
= COMMAND_EXEC
,
6376 .jim_handler
= jim_mem2array
,
6377 .help
= "read 8/16/32 bit memory and return as a TCL array "
6378 "for script processing",
6379 .usage
= "arrayname bitwidth address count",
6382 .name
= "array2mem",
6383 .mode
= COMMAND_EXEC
,
6384 .jim_handler
= jim_array2mem
,
6385 .help
= "convert a TCL array to memory locations "
6386 "and write the 8/16/32 bit values",
6387 .usage
= "arrayname bitwidth address count",
6390 .name
= "reset_nag",
6391 .handler
= handle_target_reset_nag
,
6392 .mode
= COMMAND_ANY
,
6393 .help
= "Nag after each reset about options that could have been "
6394 "enabled to improve performance. ",
6395 .usage
= "['enable'|'disable']",
6399 .handler
= handle_ps_command
,
6400 .mode
= COMMAND_EXEC
,
6401 .help
= "list all tasks ",
6405 .name
= "test_mem_access",
6406 .handler
= handle_test_mem_access_command
,
6407 .mode
= COMMAND_EXEC
,
6408 .help
= "Test the target's memory access functions",
6412 COMMAND_REGISTRATION_DONE
6414 static int target_register_user_commands(struct command_context
*cmd_ctx
)
6416 int retval
= ERROR_OK
;
6417 retval
= target_request_register_commands(cmd_ctx
);
6418 if (retval
!= ERROR_OK
)
6421 retval
= trace_register_commands(cmd_ctx
);
6422 if (retval
!= ERROR_OK
)
6426 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);