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 avr_target
;
98 extern struct target_type dsp563xx_target
;
99 extern struct target_type dsp5680xx_target
;
100 extern struct target_type testee_target
;
101 extern struct target_type avr32_ap7k_target
;
102 extern struct target_type hla_target
;
103 extern struct target_type nds32_v2_target
;
104 extern struct target_type nds32_v3_target
;
105 extern struct target_type nds32_v3m_target
;
106 extern struct target_type or1k_target
;
107 extern struct target_type quark_x10xx_target
;
108 extern struct target_type quark_d20xx_target
;
109 extern struct target_type stm8_target
;
110 extern struct target_type riscv_target
;
111 extern struct target_type mem_ap_target
;
112 extern struct target_type esirisc_target
;
114 static struct target_type
*target_types
[] = {
154 struct target
*all_targets
;
155 static struct target_event_callback
*target_event_callbacks
;
156 static struct target_timer_callback
*target_timer_callbacks
;
157 LIST_HEAD(target_reset_callback_list
);
158 LIST_HEAD(target_trace_callback_list
);
159 static const int polling_interval
= 100;
161 static const Jim_Nvp nvp_assert
[] = {
162 { .name
= "assert", NVP_ASSERT
},
163 { .name
= "deassert", NVP_DEASSERT
},
164 { .name
= "T", NVP_ASSERT
},
165 { .name
= "F", NVP_DEASSERT
},
166 { .name
= "t", NVP_ASSERT
},
167 { .name
= "f", NVP_DEASSERT
},
168 { .name
= NULL
, .value
= -1 }
171 static const Jim_Nvp nvp_error_target
[] = {
172 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
173 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
174 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
175 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
176 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
177 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
178 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
179 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
180 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
181 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
182 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
183 { .value
= -1, .name
= NULL
}
186 static const char *target_strerror_safe(int err
)
190 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
197 static const Jim_Nvp nvp_target_event
[] = {
199 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
200 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
201 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
202 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
203 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
205 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
206 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
208 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
209 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
210 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
211 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
212 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
213 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
214 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
215 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
217 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
218 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
220 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
221 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
223 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
224 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
226 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
227 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
229 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
230 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
232 { .value
= TARGET_EVENT_TRACE_CONFIG
, .name
= "trace-config" },
234 { .name
= NULL
, .value
= -1 }
237 static const Jim_Nvp nvp_target_state
[] = {
238 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
239 { .name
= "running", .value
= TARGET_RUNNING
},
240 { .name
= "halted", .value
= TARGET_HALTED
},
241 { .name
= "reset", .value
= TARGET_RESET
},
242 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
243 { .name
= NULL
, .value
= -1 },
246 static const Jim_Nvp nvp_target_debug_reason
[] = {
247 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
248 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
249 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
250 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
251 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
252 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
253 { .name
= "program-exit" , .value
= DBG_REASON_EXIT
},
254 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
255 { .name
= NULL
, .value
= -1 },
258 static const Jim_Nvp nvp_target_endian
[] = {
259 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
260 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
261 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
262 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
263 { .name
= NULL
, .value
= -1 },
266 static const Jim_Nvp nvp_reset_modes
[] = {
267 { .name
= "unknown", .value
= RESET_UNKNOWN
},
268 { .name
= "run" , .value
= RESET_RUN
},
269 { .name
= "halt" , .value
= RESET_HALT
},
270 { .name
= "init" , .value
= RESET_INIT
},
271 { .name
= NULL
, .value
= -1 },
274 const char *debug_reason_name(struct target
*t
)
278 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
279 t
->debug_reason
)->name
;
281 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
282 cp
= "(*BUG*unknown*BUG*)";
287 const char *target_state_name(struct target
*t
)
290 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
292 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
293 cp
= "(*BUG*unknown*BUG*)";
296 if (!target_was_examined(t
) && t
->defer_examine
)
297 cp
= "examine deferred";
302 const char *target_event_name(enum target_event event
)
305 cp
= Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
;
307 LOG_ERROR("Invalid target event: %d", (int)(event
));
308 cp
= "(*BUG*unknown*BUG*)";
313 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
316 cp
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
;
318 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
319 cp
= "(*BUG*unknown*BUG*)";
324 /* determine the number of the new target */
325 static int new_target_number(void)
330 /* number is 0 based */
334 if (x
< t
->target_number
)
335 x
= t
->target_number
;
341 /* read a uint64_t from a buffer in target memory endianness */
342 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
344 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
345 return le_to_h_u64(buffer
);
347 return be_to_h_u64(buffer
);
350 /* read a uint32_t from a buffer in target memory endianness */
351 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
353 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
354 return le_to_h_u32(buffer
);
356 return be_to_h_u32(buffer
);
359 /* read a uint24_t from a buffer in target memory endianness */
360 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
362 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
363 return le_to_h_u24(buffer
);
365 return be_to_h_u24(buffer
);
368 /* read a uint16_t from a buffer in target memory endianness */
369 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
371 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
372 return le_to_h_u16(buffer
);
374 return be_to_h_u16(buffer
);
377 /* read a uint8_t from a buffer in target memory endianness */
378 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
380 return *buffer
& 0x0ff;
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_context
*cmd_ctx
, 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(NULL
, "%s\n", 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 int target_examine_one(struct target
*target
)
714 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
716 int retval
= target
->type
->examine(target
);
717 if (retval
!= ERROR_OK
)
720 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
725 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
727 struct target
*target
= priv
;
729 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
732 jtag_unregister_event_callback(jtag_enable_callback
, target
);
734 return target_examine_one(target
);
737 /* Targets that correctly implement init + examine, i.e.
738 * no communication with target during init:
742 int target_examine(void)
744 int retval
= ERROR_OK
;
745 struct target
*target
;
747 for (target
= all_targets
; target
; target
= target
->next
) {
748 /* defer examination, but don't skip it */
749 if (!target
->tap
->enabled
) {
750 jtag_register_event_callback(jtag_enable_callback
,
755 if (target
->defer_examine
)
758 retval
= target_examine_one(target
);
759 if (retval
!= ERROR_OK
)
765 const char *target_type_name(struct target
*target
)
767 return target
->type
->name
;
770 static int target_soft_reset_halt(struct target
*target
)
772 if (!target_was_examined(target
)) {
773 LOG_ERROR("Target not examined yet");
776 if (!target
->type
->soft_reset_halt
) {
777 LOG_ERROR("Target %s does not support soft_reset_halt",
778 target_name(target
));
781 return target
->type
->soft_reset_halt(target
);
785 * Downloads a target-specific native code algorithm to the target,
786 * and executes it. * Note that some targets may need to set up, enable,
787 * and tear down a breakpoint (hard or * soft) to detect algorithm
788 * termination, while others may support lower overhead schemes where
789 * soft breakpoints embedded in the algorithm automatically terminate the
792 * @param target used to run the algorithm
793 * @param arch_info target-specific description of the algorithm.
795 int target_run_algorithm(struct target
*target
,
796 int num_mem_params
, struct mem_param
*mem_params
,
797 int num_reg_params
, struct reg_param
*reg_param
,
798 uint32_t entry_point
, uint32_t exit_point
,
799 int timeout_ms
, void *arch_info
)
801 int retval
= ERROR_FAIL
;
803 if (!target_was_examined(target
)) {
804 LOG_ERROR("Target not examined yet");
807 if (!target
->type
->run_algorithm
) {
808 LOG_ERROR("Target type '%s' does not support %s",
809 target_type_name(target
), __func__
);
813 target
->running_alg
= true;
814 retval
= target
->type
->run_algorithm(target
,
815 num_mem_params
, mem_params
,
816 num_reg_params
, reg_param
,
817 entry_point
, exit_point
, timeout_ms
, arch_info
);
818 target
->running_alg
= false;
825 * Executes a target-specific native code algorithm and leaves it running.
827 * @param target used to run the algorithm
828 * @param arch_info target-specific description of the algorithm.
830 int target_start_algorithm(struct target
*target
,
831 int num_mem_params
, struct mem_param
*mem_params
,
832 int num_reg_params
, struct reg_param
*reg_params
,
833 uint32_t entry_point
, uint32_t exit_point
,
836 int retval
= ERROR_FAIL
;
838 if (!target_was_examined(target
)) {
839 LOG_ERROR("Target not examined yet");
842 if (!target
->type
->start_algorithm
) {
843 LOG_ERROR("Target type '%s' does not support %s",
844 target_type_name(target
), __func__
);
847 if (target
->running_alg
) {
848 LOG_ERROR("Target is already running an algorithm");
852 target
->running_alg
= true;
853 retval
= target
->type
->start_algorithm(target
,
854 num_mem_params
, mem_params
,
855 num_reg_params
, reg_params
,
856 entry_point
, exit_point
, arch_info
);
863 * Waits for an algorithm started with target_start_algorithm() to complete.
865 * @param target used to run the algorithm
866 * @param arch_info target-specific description of the algorithm.
868 int target_wait_algorithm(struct target
*target
,
869 int num_mem_params
, struct mem_param
*mem_params
,
870 int num_reg_params
, struct reg_param
*reg_params
,
871 uint32_t exit_point
, int timeout_ms
,
874 int retval
= ERROR_FAIL
;
876 if (!target
->type
->wait_algorithm
) {
877 LOG_ERROR("Target type '%s' does not support %s",
878 target_type_name(target
), __func__
);
881 if (!target
->running_alg
) {
882 LOG_ERROR("Target is not running an algorithm");
886 retval
= target
->type
->wait_algorithm(target
,
887 num_mem_params
, mem_params
,
888 num_reg_params
, reg_params
,
889 exit_point
, timeout_ms
, arch_info
);
890 if (retval
!= ERROR_TARGET_TIMEOUT
)
891 target
->running_alg
= false;
898 * Streams data to a circular buffer on target intended for consumption by code
899 * running asynchronously on target.
901 * This is intended for applications where target-specific native code runs
902 * on the target, receives data from the circular buffer, does something with
903 * it (most likely writing it to a flash memory), and advances the circular
906 * This assumes that the helper algorithm has already been loaded to the target,
907 * but has not been started yet. Given memory and register parameters are passed
910 * The buffer is defined by (buffer_start, buffer_size) arguments and has the
913 * [buffer_start + 0, buffer_start + 4):
914 * Write Pointer address (aka head). Written and updated by this
915 * routine when new data is written to the circular buffer.
916 * [buffer_start + 4, buffer_start + 8):
917 * Read Pointer address (aka tail). Updated by code running on the
918 * target after it consumes data.
919 * [buffer_start + 8, buffer_start + buffer_size):
920 * Circular buffer contents.
922 * See contrib/loaders/flash/stm32f1x.S for an example.
924 * @param target used to run the algorithm
925 * @param buffer address on the host where data to be sent is located
926 * @param count number of blocks to send
927 * @param block_size size in bytes of each block
928 * @param num_mem_params count of memory-based params to pass to algorithm
929 * @param mem_params memory-based params to pass to algorithm
930 * @param num_reg_params count of register-based params to pass to algorithm
931 * @param reg_params memory-based params to pass to algorithm
932 * @param buffer_start address on the target of the circular buffer structure
933 * @param buffer_size size of the circular buffer structure
934 * @param entry_point address on the target to execute to start the algorithm
935 * @param exit_point address at which to set a breakpoint to catch the
936 * end of the algorithm; can be 0 if target triggers a breakpoint itself
939 int target_run_flash_async_algorithm(struct target
*target
,
940 const uint8_t *buffer
, uint32_t count
, int block_size
,
941 int num_mem_params
, struct mem_param
*mem_params
,
942 int num_reg_params
, struct reg_param
*reg_params
,
943 uint32_t buffer_start
, uint32_t buffer_size
,
944 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
949 const uint8_t *buffer_orig
= buffer
;
951 /* Set up working area. First word is write pointer, second word is read pointer,
952 * rest is fifo data area. */
953 uint32_t wp_addr
= buffer_start
;
954 uint32_t rp_addr
= buffer_start
+ 4;
955 uint32_t fifo_start_addr
= buffer_start
+ 8;
956 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
958 uint32_t wp
= fifo_start_addr
;
959 uint32_t rp
= fifo_start_addr
;
961 /* validate block_size is 2^n */
962 assert(!block_size
|| !(block_size
& (block_size
- 1)));
964 retval
= target_write_u32(target
, wp_addr
, wp
);
965 if (retval
!= ERROR_OK
)
967 retval
= target_write_u32(target
, rp_addr
, rp
);
968 if (retval
!= ERROR_OK
)
971 /* Start up algorithm on target and let it idle while writing the first chunk */
972 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
973 num_reg_params
, reg_params
,
978 if (retval
!= ERROR_OK
) {
979 LOG_ERROR("error starting target flash write algorithm");
985 retval
= target_read_u32(target
, rp_addr
, &rp
);
986 if (retval
!= ERROR_OK
) {
987 LOG_ERROR("failed to get read pointer");
991 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
992 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
995 LOG_ERROR("flash write algorithm aborted by target");
996 retval
= ERROR_FLASH_OPERATION_FAILED
;
1000 if (((rp
- fifo_start_addr
) & (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
1001 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
1005 /* Count the number of bytes available in the fifo without
1006 * crossing the wrap around. Make sure to not fill it completely,
1007 * because that would make wp == rp and that's the empty condition. */
1008 uint32_t thisrun_bytes
;
1010 thisrun_bytes
= rp
- wp
- block_size
;
1011 else if (rp
> fifo_start_addr
)
1012 thisrun_bytes
= fifo_end_addr
- wp
;
1014 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
1016 if (thisrun_bytes
== 0) {
1017 /* Throttle polling a bit if transfer is (much) faster than flash
1018 * programming. The exact delay shouldn't matter as long as it's
1019 * less than buffer size / flash speed. This is very unlikely to
1020 * run when using high latency connections such as USB. */
1023 /* to stop an infinite loop on some targets check and increment a timeout
1024 * this issue was observed on a stellaris using the new ICDI interface */
1025 if (timeout
++ >= 500) {
1026 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1027 return ERROR_FLASH_OPERATION_FAILED
;
1032 /* reset our timeout */
1035 /* Limit to the amount of data we actually want to write */
1036 if (thisrun_bytes
> count
* block_size
)
1037 thisrun_bytes
= count
* block_size
;
1039 /* Write data to fifo */
1040 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
1041 if (retval
!= ERROR_OK
)
1044 /* Update counters and wrap write pointer */
1045 buffer
+= thisrun_bytes
;
1046 count
-= thisrun_bytes
/ block_size
;
1047 wp
+= thisrun_bytes
;
1048 if (wp
>= fifo_end_addr
)
1049 wp
= fifo_start_addr
;
1051 /* Store updated write pointer to target */
1052 retval
= target_write_u32(target
, wp_addr
, wp
);
1053 if (retval
!= ERROR_OK
)
1056 /* Avoid GDB timeouts */
1060 if (retval
!= ERROR_OK
) {
1061 /* abort flash write algorithm on target */
1062 target_write_u32(target
, wp_addr
, 0);
1065 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1066 num_reg_params
, reg_params
,
1071 if (retval2
!= ERROR_OK
) {
1072 LOG_ERROR("error waiting for target flash write algorithm");
1076 if (retval
== ERROR_OK
) {
1077 /* check if algorithm set rp = 0 after fifo writer loop finished */
1078 retval
= target_read_u32(target
, rp_addr
, &rp
);
1079 if (retval
== ERROR_OK
&& rp
== 0) {
1080 LOG_ERROR("flash write algorithm aborted by target");
1081 retval
= ERROR_FLASH_OPERATION_FAILED
;
1088 int target_read_memory(struct target
*target
,
1089 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1091 if (!target_was_examined(target
)) {
1092 LOG_ERROR("Target not examined yet");
1095 if (!target
->type
->read_memory
) {
1096 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1099 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1102 int target_read_phys_memory(struct target
*target
,
1103 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1105 if (!target_was_examined(target
)) {
1106 LOG_ERROR("Target not examined yet");
1109 if (!target
->type
->read_phys_memory
) {
1110 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1113 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1116 int target_write_memory(struct target
*target
,
1117 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1119 if (!target_was_examined(target
)) {
1120 LOG_ERROR("Target not examined yet");
1123 if (!target
->type
->write_memory
) {
1124 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1127 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1130 int target_write_phys_memory(struct target
*target
,
1131 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1133 if (!target_was_examined(target
)) {
1134 LOG_ERROR("Target not examined yet");
1137 if (!target
->type
->write_phys_memory
) {
1138 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1141 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1144 int target_add_breakpoint(struct target
*target
,
1145 struct breakpoint
*breakpoint
)
1147 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1148 LOG_WARNING("target %s is not halted (add breakpoint)", target_name(target
));
1149 return ERROR_TARGET_NOT_HALTED
;
1151 return target
->type
->add_breakpoint(target
, breakpoint
);
1154 int target_add_context_breakpoint(struct target
*target
,
1155 struct breakpoint
*breakpoint
)
1157 if (target
->state
!= TARGET_HALTED
) {
1158 LOG_WARNING("target %s is not halted (add context breakpoint)", target_name(target
));
1159 return ERROR_TARGET_NOT_HALTED
;
1161 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1164 int target_add_hybrid_breakpoint(struct target
*target
,
1165 struct breakpoint
*breakpoint
)
1167 if (target
->state
!= TARGET_HALTED
) {
1168 LOG_WARNING("target %s is not halted (add hybrid breakpoint)", target_name(target
));
1169 return ERROR_TARGET_NOT_HALTED
;
1171 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1174 int target_remove_breakpoint(struct target
*target
,
1175 struct breakpoint
*breakpoint
)
1177 return target
->type
->remove_breakpoint(target
, breakpoint
);
1180 int target_add_watchpoint(struct target
*target
,
1181 struct watchpoint
*watchpoint
)
1183 if (target
->state
!= TARGET_HALTED
) {
1184 LOG_WARNING("target %s is not halted (add watchpoint)", target_name(target
));
1185 return ERROR_TARGET_NOT_HALTED
;
1187 return target
->type
->add_watchpoint(target
, watchpoint
);
1189 int target_remove_watchpoint(struct target
*target
,
1190 struct watchpoint
*watchpoint
)
1192 return target
->type
->remove_watchpoint(target
, watchpoint
);
1194 int target_hit_watchpoint(struct target
*target
,
1195 struct watchpoint
**hit_watchpoint
)
1197 if (target
->state
!= TARGET_HALTED
) {
1198 LOG_WARNING("target %s is not halted (hit watchpoint)", target
->cmd_name
);
1199 return ERROR_TARGET_NOT_HALTED
;
1202 if (target
->type
->hit_watchpoint
== NULL
) {
1203 /* For backward compatible, if hit_watchpoint is not implemented,
1204 * return ERROR_FAIL such that gdb_server will not take the nonsense
1209 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1212 const char *target_get_gdb_arch(struct target
*target
)
1214 if (target
->type
->get_gdb_arch
== NULL
)
1216 return target
->type
->get_gdb_arch(target
);
1219 int target_get_gdb_reg_list(struct target
*target
,
1220 struct reg
**reg_list
[], int *reg_list_size
,
1221 enum target_register_class reg_class
)
1223 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1226 bool target_supports_gdb_connection(struct target
*target
)
1229 * based on current code, we can simply exclude all the targets that
1230 * don't provide get_gdb_reg_list; this could change with new targets.
1232 return !!target
->type
->get_gdb_reg_list
;
1235 int target_step(struct target
*target
,
1236 int current
, target_addr_t address
, int handle_breakpoints
)
1238 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1241 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1243 if (target
->state
!= TARGET_HALTED
) {
1244 LOG_WARNING("target %s is not halted (gdb fileio)", target
->cmd_name
);
1245 return ERROR_TARGET_NOT_HALTED
;
1247 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1250 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1252 if (target
->state
!= TARGET_HALTED
) {
1253 LOG_WARNING("target %s is not halted (gdb fileio end)", target
->cmd_name
);
1254 return ERROR_TARGET_NOT_HALTED
;
1256 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1259 int target_profiling(struct target
*target
, uint32_t *samples
,
1260 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1262 if (target
->state
!= TARGET_HALTED
) {
1263 LOG_WARNING("target %s is not halted (profiling)", target
->cmd_name
);
1264 return ERROR_TARGET_NOT_HALTED
;
1266 return target
->type
->profiling(target
, samples
, max_num_samples
,
1267 num_samples
, seconds
);
1271 * Reset the @c examined flag for the given target.
1272 * Pure paranoia -- targets are zeroed on allocation.
1274 static void target_reset_examined(struct target
*target
)
1276 target
->examined
= false;
1279 static int handle_target(void *priv
);
1281 static int target_init_one(struct command_context
*cmd_ctx
,
1282 struct target
*target
)
1284 target_reset_examined(target
);
1286 struct target_type
*type
= target
->type
;
1287 if (type
->examine
== NULL
)
1288 type
->examine
= default_examine
;
1290 if (type
->check_reset
== NULL
)
1291 type
->check_reset
= default_check_reset
;
1293 assert(type
->init_target
!= NULL
);
1295 int retval
= type
->init_target(cmd_ctx
, target
);
1296 if (ERROR_OK
!= retval
) {
1297 LOG_ERROR("target '%s' init failed", target_name(target
));
1301 /* Sanity-check MMU support ... stub in what we must, to help
1302 * implement it in stages, but warn if we need to do so.
1305 if (type
->virt2phys
== NULL
) {
1306 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1307 type
->virt2phys
= identity_virt2phys
;
1310 /* Make sure no-MMU targets all behave the same: make no
1311 * distinction between physical and virtual addresses, and
1312 * ensure that virt2phys() is always an identity mapping.
1314 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1315 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1318 type
->write_phys_memory
= type
->write_memory
;
1319 type
->read_phys_memory
= type
->read_memory
;
1320 type
->virt2phys
= identity_virt2phys
;
1323 if (target
->type
->read_buffer
== NULL
)
1324 target
->type
->read_buffer
= target_read_buffer_default
;
1326 if (target
->type
->write_buffer
== NULL
)
1327 target
->type
->write_buffer
= target_write_buffer_default
;
1329 if (target
->type
->get_gdb_fileio_info
== NULL
)
1330 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1332 if (target
->type
->gdb_fileio_end
== NULL
)
1333 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1335 if (target
->type
->profiling
== NULL
)
1336 target
->type
->profiling
= target_profiling_default
;
1341 static int target_init(struct command_context
*cmd_ctx
)
1343 struct target
*target
;
1346 for (target
= all_targets
; target
; target
= target
->next
) {
1347 retval
= target_init_one(cmd_ctx
, target
);
1348 if (ERROR_OK
!= retval
)
1355 retval
= target_register_user_commands(cmd_ctx
);
1356 if (ERROR_OK
!= retval
)
1359 retval
= target_register_timer_callback(&handle_target
,
1360 polling_interval
, 1, cmd_ctx
->interp
);
1361 if (ERROR_OK
!= retval
)
1367 COMMAND_HANDLER(handle_target_init_command
)
1372 return ERROR_COMMAND_SYNTAX_ERROR
;
1374 static bool target_initialized
;
1375 if (target_initialized
) {
1376 LOG_INFO("'target init' has already been called");
1379 target_initialized
= true;
1381 retval
= command_run_line(CMD_CTX
, "init_targets");
1382 if (ERROR_OK
!= retval
)
1385 retval
= command_run_line(CMD_CTX
, "init_target_events");
1386 if (ERROR_OK
!= retval
)
1389 retval
= command_run_line(CMD_CTX
, "init_board");
1390 if (ERROR_OK
!= retval
)
1393 LOG_DEBUG("Initializing targets...");
1394 return target_init(CMD_CTX
);
1397 int target_register_event_callback(int (*callback
)(struct target
*target
,
1398 enum target_event event
, void *priv
), void *priv
)
1400 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1402 if (callback
== NULL
)
1403 return ERROR_COMMAND_SYNTAX_ERROR
;
1406 while ((*callbacks_p
)->next
)
1407 callbacks_p
= &((*callbacks_p
)->next
);
1408 callbacks_p
= &((*callbacks_p
)->next
);
1411 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1412 (*callbacks_p
)->callback
= callback
;
1413 (*callbacks_p
)->priv
= priv
;
1414 (*callbacks_p
)->next
= NULL
;
1419 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1420 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1422 struct target_reset_callback
*entry
;
1424 if (callback
== NULL
)
1425 return ERROR_COMMAND_SYNTAX_ERROR
;
1427 entry
= malloc(sizeof(struct target_reset_callback
));
1428 if (entry
== NULL
) {
1429 LOG_ERROR("error allocating buffer for reset callback entry");
1430 return ERROR_COMMAND_SYNTAX_ERROR
;
1433 entry
->callback
= callback
;
1435 list_add(&entry
->list
, &target_reset_callback_list
);
1441 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1442 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1444 struct target_trace_callback
*entry
;
1446 if (callback
== NULL
)
1447 return ERROR_COMMAND_SYNTAX_ERROR
;
1449 entry
= malloc(sizeof(struct target_trace_callback
));
1450 if (entry
== NULL
) {
1451 LOG_ERROR("error allocating buffer for trace callback entry");
1452 return ERROR_COMMAND_SYNTAX_ERROR
;
1455 entry
->callback
= callback
;
1457 list_add(&entry
->list
, &target_trace_callback_list
);
1463 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1465 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1467 if (callback
== NULL
)
1468 return ERROR_COMMAND_SYNTAX_ERROR
;
1471 while ((*callbacks_p
)->next
)
1472 callbacks_p
= &((*callbacks_p
)->next
);
1473 callbacks_p
= &((*callbacks_p
)->next
);
1476 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1477 (*callbacks_p
)->callback
= callback
;
1478 (*callbacks_p
)->periodic
= periodic
;
1479 (*callbacks_p
)->time_ms
= time_ms
;
1480 (*callbacks_p
)->removed
= false;
1482 gettimeofday(&(*callbacks_p
)->when
, NULL
);
1483 timeval_add_time(&(*callbacks_p
)->when
, 0, time_ms
* 1000);
1485 (*callbacks_p
)->priv
= priv
;
1486 (*callbacks_p
)->next
= NULL
;
1491 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1492 enum target_event event
, void *priv
), void *priv
)
1494 struct target_event_callback
**p
= &target_event_callbacks
;
1495 struct target_event_callback
*c
= target_event_callbacks
;
1497 if (callback
== NULL
)
1498 return ERROR_COMMAND_SYNTAX_ERROR
;
1501 struct target_event_callback
*next
= c
->next
;
1502 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1514 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1515 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1517 struct target_reset_callback
*entry
;
1519 if (callback
== NULL
)
1520 return ERROR_COMMAND_SYNTAX_ERROR
;
1522 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1523 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1524 list_del(&entry
->list
);
1533 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1534 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1536 struct target_trace_callback
*entry
;
1538 if (callback
== NULL
)
1539 return ERROR_COMMAND_SYNTAX_ERROR
;
1541 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1542 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1543 list_del(&entry
->list
);
1552 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1554 if (callback
== NULL
)
1555 return ERROR_COMMAND_SYNTAX_ERROR
;
1557 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1559 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1568 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1570 struct target_event_callback
*callback
= target_event_callbacks
;
1571 struct target_event_callback
*next_callback
;
1573 if (event
== TARGET_EVENT_HALTED
) {
1574 /* execute early halted first */
1575 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1578 LOG_DEBUG("target event %i (%s)", event
,
1579 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1581 target_handle_event(target
, event
);
1584 next_callback
= callback
->next
;
1585 callback
->callback(target
, event
, callback
->priv
);
1586 callback
= next_callback
;
1592 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1594 struct target_reset_callback
*callback
;
1596 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1597 Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1599 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1600 callback
->callback(target
, reset_mode
, callback
->priv
);
1605 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1607 struct target_trace_callback
*callback
;
1609 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1610 callback
->callback(target
, len
, data
, callback
->priv
);
1615 static int target_timer_callback_periodic_restart(
1616 struct target_timer_callback
*cb
, struct timeval
*now
)
1619 timeval_add_time(&cb
->when
, 0, cb
->time_ms
* 1000L);
1623 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1624 struct timeval
*now
)
1626 cb
->callback(cb
->priv
);
1629 return target_timer_callback_periodic_restart(cb
, now
);
1631 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1634 static int target_call_timer_callbacks_check_time(int checktime
)
1636 static bool callback_processing
;
1638 /* Do not allow nesting */
1639 if (callback_processing
)
1642 callback_processing
= true;
1647 gettimeofday(&now
, NULL
);
1649 /* Store an address of the place containing a pointer to the
1650 * next item; initially, that's a standalone "root of the
1651 * list" variable. */
1652 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1654 if ((*callback
)->removed
) {
1655 struct target_timer_callback
*p
= *callback
;
1656 *callback
= (*callback
)->next
;
1661 bool call_it
= (*callback
)->callback
&&
1662 ((!checktime
&& (*callback
)->periodic
) ||
1663 timeval_compare(&now
, &(*callback
)->when
) >= 0);
1666 target_call_timer_callback(*callback
, &now
);
1668 callback
= &(*callback
)->next
;
1671 callback_processing
= false;
1675 int target_call_timer_callbacks(void)
1677 return target_call_timer_callbacks_check_time(1);
1680 /* invoke periodic callbacks immediately */
1681 int target_call_timer_callbacks_now(void)
1683 return target_call_timer_callbacks_check_time(0);
1686 /* Prints the working area layout for debug purposes */
1687 static void print_wa_layout(struct target
*target
)
1689 struct working_area
*c
= target
->working_areas
;
1692 LOG_DEBUG("%c%c " TARGET_ADDR_FMT
"-" TARGET_ADDR_FMT
" (%" PRIu32
" bytes)",
1693 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1694 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1699 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1700 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1702 assert(area
->free
); /* Shouldn't split an allocated area */
1703 assert(size
<= area
->size
); /* Caller should guarantee this */
1705 /* Split only if not already the right size */
1706 if (size
< area
->size
) {
1707 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1712 new_wa
->next
= area
->next
;
1713 new_wa
->size
= area
->size
- size
;
1714 new_wa
->address
= area
->address
+ size
;
1715 new_wa
->backup
= NULL
;
1716 new_wa
->user
= NULL
;
1717 new_wa
->free
= true;
1719 area
->next
= new_wa
;
1722 /* If backup memory was allocated to this area, it has the wrong size
1723 * now so free it and it will be reallocated if/when needed */
1726 area
->backup
= NULL
;
1731 /* Merge all adjacent free areas into one */
1732 static void target_merge_working_areas(struct target
*target
)
1734 struct working_area
*c
= target
->working_areas
;
1736 while (c
&& c
->next
) {
1737 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1739 /* Find two adjacent free areas */
1740 if (c
->free
&& c
->next
->free
) {
1741 /* Merge the last into the first */
1742 c
->size
+= c
->next
->size
;
1744 /* Remove the last */
1745 struct working_area
*to_be_freed
= c
->next
;
1746 c
->next
= c
->next
->next
;
1747 if (to_be_freed
->backup
)
1748 free(to_be_freed
->backup
);
1751 /* If backup memory was allocated to the remaining area, it's has
1752 * the wrong size now */
1763 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1765 /* Reevaluate working area address based on MMU state*/
1766 if (target
->working_areas
== NULL
) {
1770 retval
= target
->type
->mmu(target
, &enabled
);
1771 if (retval
!= ERROR_OK
)
1775 if (target
->working_area_phys_spec
) {
1776 LOG_DEBUG("MMU disabled, using physical "
1777 "address for working memory " TARGET_ADDR_FMT
,
1778 target
->working_area_phys
);
1779 target
->working_area
= target
->working_area_phys
;
1781 LOG_ERROR("No working memory available. "
1782 "Specify -work-area-phys to target.");
1783 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1786 if (target
->working_area_virt_spec
) {
1787 LOG_DEBUG("MMU enabled, using virtual "
1788 "address for working memory " TARGET_ADDR_FMT
,
1789 target
->working_area_virt
);
1790 target
->working_area
= target
->working_area_virt
;
1792 LOG_ERROR("No working memory available. "
1793 "Specify -work-area-virt to target.");
1794 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1798 /* Set up initial working area on first call */
1799 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1801 new_wa
->next
= NULL
;
1802 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1803 new_wa
->address
= target
->working_area
;
1804 new_wa
->backup
= NULL
;
1805 new_wa
->user
= NULL
;
1806 new_wa
->free
= true;
1809 target
->working_areas
= new_wa
;
1812 /* only allocate multiples of 4 byte */
1814 size
= (size
+ 3) & (~3UL);
1816 struct working_area
*c
= target
->working_areas
;
1818 /* Find the first large enough working area */
1820 if (c
->free
&& c
->size
>= size
)
1826 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1828 /* Split the working area into the requested size */
1829 target_split_working_area(c
, size
);
1831 LOG_DEBUG("allocated new working area of %" PRIu32
" bytes at address " TARGET_ADDR_FMT
,
1834 if (target
->backup_working_area
) {
1835 if (c
->backup
== NULL
) {
1836 c
->backup
= malloc(c
->size
);
1837 if (c
->backup
== NULL
)
1841 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1842 if (retval
!= ERROR_OK
)
1846 /* mark as used, and return the new (reused) area */
1853 print_wa_layout(target
);
1858 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1862 retval
= target_alloc_working_area_try(target
, size
, area
);
1863 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1864 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1869 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1871 int retval
= ERROR_OK
;
1873 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1874 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1875 if (retval
!= ERROR_OK
)
1876 LOG_ERROR("failed to restore %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1877 area
->size
, area
->address
);
1883 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1884 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1886 int retval
= ERROR_OK
;
1892 retval
= target_restore_working_area(target
, area
);
1893 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1894 if (retval
!= ERROR_OK
)
1900 LOG_DEBUG("freed %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1901 area
->size
, area
->address
);
1903 /* mark user pointer invalid */
1904 /* TODO: Is this really safe? It points to some previous caller's memory.
1905 * How could we know that the area pointer is still in that place and not
1906 * some other vital data? What's the purpose of this, anyway? */
1910 target_merge_working_areas(target
);
1912 print_wa_layout(target
);
1917 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1919 return target_free_working_area_restore(target
, area
, 1);
1922 static void target_destroy(struct target
*target
)
1924 if (target
->type
->deinit_target
)
1925 target
->type
->deinit_target(target
);
1927 if (target
->semihosting
)
1928 free(target
->semihosting
);
1930 jtag_unregister_event_callback(jtag_enable_callback
, target
);
1932 struct target_event_action
*teap
= target
->event_action
;
1934 struct target_event_action
*next
= teap
->next
;
1935 Jim_DecrRefCount(teap
->interp
, teap
->body
);
1940 target_free_all_working_areas(target
);
1941 /* Now we have none or only one working area marked as free */
1942 if (target
->working_areas
) {
1943 free(target
->working_areas
->backup
);
1944 free(target
->working_areas
);
1947 /* release the targets SMP list */
1949 struct target_list
*head
= target
->head
;
1950 while (head
!= NULL
) {
1951 struct target_list
*pos
= head
->next
;
1952 head
->target
->smp
= 0;
1959 free(target
->gdb_port_override
);
1961 free(target
->trace_info
);
1962 free(target
->fileio_info
);
1963 free(target
->cmd_name
);
1967 void target_quit(void)
1969 struct target_event_callback
*pe
= target_event_callbacks
;
1971 struct target_event_callback
*t
= pe
->next
;
1975 target_event_callbacks
= NULL
;
1977 struct target_timer_callback
*pt
= target_timer_callbacks
;
1979 struct target_timer_callback
*t
= pt
->next
;
1983 target_timer_callbacks
= NULL
;
1985 for (struct target
*target
= all_targets
; target
;) {
1989 target_destroy(target
);
1996 /* free resources and restore memory, if restoring memory fails,
1997 * free up resources anyway
1999 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
2001 struct working_area
*c
= target
->working_areas
;
2003 LOG_DEBUG("freeing all working areas");
2005 /* Loop through all areas, restoring the allocated ones and marking them as free */
2009 target_restore_working_area(target
, c
);
2011 *c
->user
= NULL
; /* Same as above */
2017 /* Run a merge pass to combine all areas into one */
2018 target_merge_working_areas(target
);
2020 print_wa_layout(target
);
2023 void target_free_all_working_areas(struct target
*target
)
2025 target_free_all_working_areas_restore(target
, 1);
2028 /* Find the largest number of bytes that can be allocated */
2029 uint32_t target_get_working_area_avail(struct target
*target
)
2031 struct working_area
*c
= target
->working_areas
;
2032 uint32_t max_size
= 0;
2035 return target
->working_area_size
;
2038 if (c
->free
&& max_size
< c
->size
)
2047 int target_arch_state(struct target
*target
)
2050 if (target
== NULL
) {
2051 LOG_WARNING("No target has been configured");
2055 if (target
->state
!= TARGET_HALTED
)
2058 retval
= target
->type
->arch_state(target
);
2062 static int target_get_gdb_fileio_info_default(struct target
*target
,
2063 struct gdb_fileio_info
*fileio_info
)
2065 /* If target does not support semi-hosting function, target
2066 has no need to provide .get_gdb_fileio_info callback.
2067 It just return ERROR_FAIL and gdb_server will return "Txx"
2068 as target halted every time. */
2072 static int target_gdb_fileio_end_default(struct target
*target
,
2073 int retcode
, int fileio_errno
, bool ctrl_c
)
2078 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
2079 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
2081 struct timeval timeout
, now
;
2083 gettimeofday(&timeout
, NULL
);
2084 timeval_add_time(&timeout
, seconds
, 0);
2086 LOG_INFO("Starting profiling. Halting and resuming the"
2087 " target as often as we can...");
2089 uint32_t sample_count
= 0;
2090 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2091 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2093 int retval
= ERROR_OK
;
2095 target_poll(target
);
2096 if (target
->state
== TARGET_HALTED
) {
2097 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
2098 samples
[sample_count
++] = t
;
2099 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2100 retval
= target_resume(target
, 1, 0, 0, 0);
2101 target_poll(target
);
2102 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2103 } else if (target
->state
== TARGET_RUNNING
) {
2104 /* We want to quickly sample the PC. */
2105 retval
= target_halt(target
);
2107 LOG_INFO("Target not halted or running");
2112 if (retval
!= ERROR_OK
)
2115 gettimeofday(&now
, NULL
);
2116 if ((sample_count
>= max_num_samples
) || timeval_compare(&now
, &timeout
) >= 0) {
2117 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2122 *num_samples
= sample_count
;
2126 /* Single aligned words are guaranteed to use 16 or 32 bit access
2127 * mode respectively, otherwise data is handled as quickly as
2130 int target_write_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, const uint8_t *buffer
)
2132 LOG_DEBUG("writing buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2135 if (!target_was_examined(target
)) {
2136 LOG_ERROR("Target not examined yet");
2143 if ((address
+ size
- 1) < address
) {
2144 /* GDB can request this when e.g. PC is 0xfffffffc */
2145 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2151 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2154 static int target_write_buffer_default(struct target
*target
,
2155 target_addr_t address
, uint32_t count
, const uint8_t *buffer
)
2159 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2160 * will have something to do with the size we leave to it. */
2161 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2162 if (address
& size
) {
2163 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2164 if (retval
!= ERROR_OK
)
2172 /* Write the data with as large access size as possible. */
2173 for (; size
> 0; size
/= 2) {
2174 uint32_t aligned
= count
- count
% size
;
2176 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2177 if (retval
!= ERROR_OK
)
2188 /* Single aligned words are guaranteed to use 16 or 32 bit access
2189 * mode respectively, otherwise data is handled as quickly as
2192 int target_read_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
2194 LOG_DEBUG("reading buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2197 if (!target_was_examined(target
)) {
2198 LOG_ERROR("Target not examined yet");
2205 if ((address
+ size
- 1) < address
) {
2206 /* GDB can request this when e.g. PC is 0xfffffffc */
2207 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2213 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2216 static int target_read_buffer_default(struct target
*target
, target_addr_t address
, uint32_t count
, uint8_t *buffer
)
2220 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2221 * will have something to do with the size we leave to it. */
2222 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2223 if (address
& size
) {
2224 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2225 if (retval
!= ERROR_OK
)
2233 /* Read the data with as large access size as possible. */
2234 for (; size
> 0; size
/= 2) {
2235 uint32_t aligned
= count
- count
% size
;
2237 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2238 if (retval
!= ERROR_OK
)
2249 int target_checksum_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* crc
)
2254 uint32_t checksum
= 0;
2255 if (!target_was_examined(target
)) {
2256 LOG_ERROR("Target not examined yet");
2260 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2261 if (retval
!= ERROR_OK
) {
2262 buffer
= malloc(size
);
2263 if (buffer
== NULL
) {
2264 LOG_ERROR("error allocating buffer for section (%" PRId32
" bytes)", size
);
2265 return ERROR_COMMAND_SYNTAX_ERROR
;
2267 retval
= target_read_buffer(target
, address
, size
, buffer
);
2268 if (retval
!= ERROR_OK
) {
2273 /* convert to target endianness */
2274 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2275 uint32_t target_data
;
2276 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2277 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2280 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2289 int target_blank_check_memory(struct target
*target
,
2290 struct target_memory_check_block
*blocks
, int num_blocks
,
2291 uint8_t erased_value
)
2293 if (!target_was_examined(target
)) {
2294 LOG_ERROR("Target not examined yet");
2298 if (target
->type
->blank_check_memory
== NULL
)
2299 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2301 return target
->type
->blank_check_memory(target
, blocks
, num_blocks
, erased_value
);
2304 int target_read_u64(struct target
*target
, target_addr_t address
, uint64_t *value
)
2306 uint8_t value_buf
[8];
2307 if (!target_was_examined(target
)) {
2308 LOG_ERROR("Target not examined yet");
2312 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2314 if (retval
== ERROR_OK
) {
2315 *value
= target_buffer_get_u64(target
, value_buf
);
2316 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2321 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2328 int target_read_u32(struct target
*target
, target_addr_t address
, uint32_t *value
)
2330 uint8_t value_buf
[4];
2331 if (!target_was_examined(target
)) {
2332 LOG_ERROR("Target not examined yet");
2336 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2338 if (retval
== ERROR_OK
) {
2339 *value
= target_buffer_get_u32(target
, value_buf
);
2340 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2345 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2352 int target_read_u16(struct target
*target
, target_addr_t address
, uint16_t *value
)
2354 uint8_t value_buf
[2];
2355 if (!target_was_examined(target
)) {
2356 LOG_ERROR("Target not examined yet");
2360 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2362 if (retval
== ERROR_OK
) {
2363 *value
= target_buffer_get_u16(target
, value_buf
);
2364 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%4.4" PRIx16
,
2369 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2376 int target_read_u8(struct target
*target
, target_addr_t address
, uint8_t *value
)
2378 if (!target_was_examined(target
)) {
2379 LOG_ERROR("Target not examined yet");
2383 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2385 if (retval
== ERROR_OK
) {
2386 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2391 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2398 int target_write_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2401 uint8_t value_buf
[8];
2402 if (!target_was_examined(target
)) {
2403 LOG_ERROR("Target not examined yet");
2407 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2411 target_buffer_set_u64(target
, value_buf
, value
);
2412 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2413 if (retval
!= ERROR_OK
)
2414 LOG_DEBUG("failed: %i", retval
);
2419 int target_write_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2422 uint8_t value_buf
[4];
2423 if (!target_was_examined(target
)) {
2424 LOG_ERROR("Target not examined yet");
2428 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2432 target_buffer_set_u32(target
, value_buf
, value
);
2433 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2434 if (retval
!= ERROR_OK
)
2435 LOG_DEBUG("failed: %i", retval
);
2440 int target_write_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2443 uint8_t value_buf
[2];
2444 if (!target_was_examined(target
)) {
2445 LOG_ERROR("Target not examined yet");
2449 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2453 target_buffer_set_u16(target
, value_buf
, value
);
2454 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2455 if (retval
!= ERROR_OK
)
2456 LOG_DEBUG("failed: %i", retval
);
2461 int target_write_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2464 if (!target_was_examined(target
)) {
2465 LOG_ERROR("Target not examined yet");
2469 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2472 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2473 if (retval
!= ERROR_OK
)
2474 LOG_DEBUG("failed: %i", retval
);
2479 int target_write_phys_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2482 uint8_t value_buf
[8];
2483 if (!target_was_examined(target
)) {
2484 LOG_ERROR("Target not examined yet");
2488 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2492 target_buffer_set_u64(target
, value_buf
, value
);
2493 retval
= target_write_phys_memory(target
, address
, 8, 1, value_buf
);
2494 if (retval
!= ERROR_OK
)
2495 LOG_DEBUG("failed: %i", retval
);
2500 int target_write_phys_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2503 uint8_t value_buf
[4];
2504 if (!target_was_examined(target
)) {
2505 LOG_ERROR("Target not examined yet");
2509 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2513 target_buffer_set_u32(target
, value_buf
, value
);
2514 retval
= target_write_phys_memory(target
, address
, 4, 1, value_buf
);
2515 if (retval
!= ERROR_OK
)
2516 LOG_DEBUG("failed: %i", retval
);
2521 int target_write_phys_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2524 uint8_t value_buf
[2];
2525 if (!target_was_examined(target
)) {
2526 LOG_ERROR("Target not examined yet");
2530 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2534 target_buffer_set_u16(target
, value_buf
, value
);
2535 retval
= target_write_phys_memory(target
, address
, 2, 1, value_buf
);
2536 if (retval
!= ERROR_OK
)
2537 LOG_DEBUG("failed: %i", retval
);
2542 int target_write_phys_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2545 if (!target_was_examined(target
)) {
2546 LOG_ERROR("Target not examined yet");
2550 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2553 retval
= target_write_phys_memory(target
, address
, 1, 1, &value
);
2554 if (retval
!= ERROR_OK
)
2555 LOG_DEBUG("failed: %i", retval
);
2560 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2562 struct target
*target
= get_target(name
);
2563 if (target
== NULL
) {
2564 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2567 if (!target
->tap
->enabled
) {
2568 LOG_USER("Target: TAP %s is disabled, "
2569 "can't be the current target\n",
2570 target
->tap
->dotted_name
);
2574 cmd_ctx
->current_target
= target
;
2575 if (cmd_ctx
->current_target_override
)
2576 cmd_ctx
->current_target_override
= target
;
2582 COMMAND_HANDLER(handle_targets_command
)
2584 int retval
= ERROR_OK
;
2585 if (CMD_ARGC
== 1) {
2586 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2587 if (retval
== ERROR_OK
) {
2593 struct target
*target
= all_targets
;
2594 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2595 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2600 if (target
->tap
->enabled
)
2601 state
= target_state_name(target
);
2603 state
= "tap-disabled";
2605 if (CMD_CTX
->current_target
== target
)
2608 /* keep columns lined up to match the headers above */
2609 command_print(CMD_CTX
,
2610 "%2d%c %-18s %-10s %-6s %-18s %s",
2611 target
->target_number
,
2613 target_name(target
),
2614 target_type_name(target
),
2615 Jim_Nvp_value2name_simple(nvp_target_endian
,
2616 target
->endianness
)->name
,
2617 target
->tap
->dotted_name
,
2619 target
= target
->next
;
2625 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2627 static int powerDropout
;
2628 static int srstAsserted
;
2630 static int runPowerRestore
;
2631 static int runPowerDropout
;
2632 static int runSrstAsserted
;
2633 static int runSrstDeasserted
;
2635 static int sense_handler(void)
2637 static int prevSrstAsserted
;
2638 static int prevPowerdropout
;
2640 int retval
= jtag_power_dropout(&powerDropout
);
2641 if (retval
!= ERROR_OK
)
2645 powerRestored
= prevPowerdropout
&& !powerDropout
;
2647 runPowerRestore
= 1;
2649 int64_t current
= timeval_ms();
2650 static int64_t lastPower
;
2651 bool waitMore
= lastPower
+ 2000 > current
;
2652 if (powerDropout
&& !waitMore
) {
2653 runPowerDropout
= 1;
2654 lastPower
= current
;
2657 retval
= jtag_srst_asserted(&srstAsserted
);
2658 if (retval
!= ERROR_OK
)
2662 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2664 static int64_t lastSrst
;
2665 waitMore
= lastSrst
+ 2000 > current
;
2666 if (srstDeasserted
&& !waitMore
) {
2667 runSrstDeasserted
= 1;
2671 if (!prevSrstAsserted
&& srstAsserted
)
2672 runSrstAsserted
= 1;
2674 prevSrstAsserted
= srstAsserted
;
2675 prevPowerdropout
= powerDropout
;
2677 if (srstDeasserted
|| powerRestored
) {
2678 /* Other than logging the event we can't do anything here.
2679 * Issuing a reset is a particularly bad idea as we might
2680 * be inside a reset already.
2687 /* process target state changes */
2688 static int handle_target(void *priv
)
2690 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2691 int retval
= ERROR_OK
;
2693 if (!is_jtag_poll_safe()) {
2694 /* polling is disabled currently */
2698 /* we do not want to recurse here... */
2699 static int recursive
;
2703 /* danger! running these procedures can trigger srst assertions and power dropouts.
2704 * We need to avoid an infinite loop/recursion here and we do that by
2705 * clearing the flags after running these events.
2707 int did_something
= 0;
2708 if (runSrstAsserted
) {
2709 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2710 Jim_Eval(interp
, "srst_asserted");
2713 if (runSrstDeasserted
) {
2714 Jim_Eval(interp
, "srst_deasserted");
2717 if (runPowerDropout
) {
2718 LOG_INFO("Power dropout detected, running power_dropout proc.");
2719 Jim_Eval(interp
, "power_dropout");
2722 if (runPowerRestore
) {
2723 Jim_Eval(interp
, "power_restore");
2727 if (did_something
) {
2728 /* clear detect flags */
2732 /* clear action flags */
2734 runSrstAsserted
= 0;
2735 runSrstDeasserted
= 0;
2736 runPowerRestore
= 0;
2737 runPowerDropout
= 0;
2742 /* Poll targets for state changes unless that's globally disabled.
2743 * Skip targets that are currently disabled.
2745 for (struct target
*target
= all_targets
;
2746 is_jtag_poll_safe() && target
;
2747 target
= target
->next
) {
2749 if (!target_was_examined(target
))
2752 if (!target
->tap
->enabled
)
2755 if (target
->backoff
.times
> target
->backoff
.count
) {
2756 /* do not poll this time as we failed previously */
2757 target
->backoff
.count
++;
2760 target
->backoff
.count
= 0;
2762 /* only poll target if we've got power and srst isn't asserted */
2763 if (!powerDropout
&& !srstAsserted
) {
2764 /* polling may fail silently until the target has been examined */
2765 retval
= target_poll(target
);
2766 if (retval
!= ERROR_OK
) {
2767 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2768 if (target
->backoff
.times
* polling_interval
< 5000) {
2769 target
->backoff
.times
*= 2;
2770 target
->backoff
.times
++;
2773 /* Tell GDB to halt the debugger. This allows the user to
2774 * run monitor commands to handle the situation.
2776 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2778 if (target
->backoff
.times
> 0) {
2779 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
2780 target_reset_examined(target
);
2781 retval
= target_examine_one(target
);
2782 /* Target examination could have failed due to unstable connection,
2783 * but we set the examined flag anyway to repoll it later */
2784 if (retval
!= ERROR_OK
) {
2785 target
->examined
= true;
2786 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2787 target
->backoff
.times
* polling_interval
);
2792 /* Since we succeeded, we reset backoff count */
2793 target
->backoff
.times
= 0;
2800 COMMAND_HANDLER(handle_reg_command
)
2802 struct target
*target
;
2803 struct reg
*reg
= NULL
;
2809 target
= get_current_target(CMD_CTX
);
2811 /* list all available registers for the current target */
2812 if (CMD_ARGC
== 0) {
2813 struct reg_cache
*cache
= target
->reg_cache
;
2819 command_print(CMD_CTX
, "===== %s", cache
->name
);
2821 for (i
= 0, reg
= cache
->reg_list
;
2822 i
< cache
->num_regs
;
2823 i
++, reg
++, count
++) {
2824 if (reg
->exist
== false)
2826 /* only print cached values if they are valid */
2828 value
= buf_to_str(reg
->value
,
2830 command_print(CMD_CTX
,
2831 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2839 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2844 cache
= cache
->next
;
2850 /* access a single register by its ordinal number */
2851 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2853 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2855 struct reg_cache
*cache
= target
->reg_cache
;
2859 for (i
= 0; i
< cache
->num_regs
; i
++) {
2860 if (count
++ == num
) {
2861 reg
= &cache
->reg_list
[i
];
2867 cache
= cache
->next
;
2871 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2872 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2876 /* access a single register by its name */
2877 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2883 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2888 /* display a register */
2889 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2890 && (CMD_ARGV
[1][0] <= '9')))) {
2891 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2894 if (reg
->valid
== 0)
2895 reg
->type
->get(reg
);
2896 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2897 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2902 /* set register value */
2903 if (CMD_ARGC
== 2) {
2904 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2907 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2909 reg
->type
->set(reg
, buf
);
2911 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2912 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2920 return ERROR_COMMAND_SYNTAX_ERROR
;
2923 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2927 COMMAND_HANDLER(handle_poll_command
)
2929 int retval
= ERROR_OK
;
2930 struct target
*target
= get_current_target(CMD_CTX
);
2932 if (CMD_ARGC
== 0) {
2933 command_print(CMD_CTX
, "background polling: %s",
2934 jtag_poll_get_enabled() ? "on" : "off");
2935 command_print(CMD_CTX
, "TAP: %s (%s)",
2936 target
->tap
->dotted_name
,
2937 target
->tap
->enabled
? "enabled" : "disabled");
2938 if (!target
->tap
->enabled
)
2940 retval
= target_poll(target
);
2941 if (retval
!= ERROR_OK
)
2943 retval
= target_arch_state(target
);
2944 if (retval
!= ERROR_OK
)
2946 } else if (CMD_ARGC
== 1) {
2948 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2949 jtag_poll_set_enabled(enable
);
2951 return ERROR_COMMAND_SYNTAX_ERROR
;
2956 COMMAND_HANDLER(handle_wait_halt_command
)
2959 return ERROR_COMMAND_SYNTAX_ERROR
;
2961 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2962 if (1 == CMD_ARGC
) {
2963 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2964 if (ERROR_OK
!= retval
)
2965 return ERROR_COMMAND_SYNTAX_ERROR
;
2968 struct target
*target
= get_current_target(CMD_CTX
);
2969 return target_wait_state(target
, TARGET_HALTED
, ms
);
2972 /* wait for target state to change. The trick here is to have a low
2973 * latency for short waits and not to suck up all the CPU time
2976 * After 500ms, keep_alive() is invoked
2978 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2981 int64_t then
= 0, cur
;
2985 retval
= target_poll(target
);
2986 if (retval
!= ERROR_OK
)
2988 if (target
->state
== state
)
2993 then
= timeval_ms();
2994 LOG_DEBUG("waiting for target %s...",
2995 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
3001 if ((cur
-then
) > ms
) {
3002 LOG_ERROR("timed out while waiting for target %s",
3003 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
3011 COMMAND_HANDLER(handle_halt_command
)
3015 struct target
*target
= get_current_target(CMD_CTX
);
3017 target
->verbose_halt_msg
= true;
3019 int retval
= target_halt(target
);
3020 if (ERROR_OK
!= retval
)
3023 if (CMD_ARGC
== 1) {
3024 unsigned wait_local
;
3025 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
3026 if (ERROR_OK
!= retval
)
3027 return ERROR_COMMAND_SYNTAX_ERROR
;
3032 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
3035 COMMAND_HANDLER(handle_soft_reset_halt_command
)
3037 struct target
*target
= get_current_target(CMD_CTX
);
3039 LOG_USER("requesting target halt and executing a soft reset");
3041 target_soft_reset_halt(target
);
3046 COMMAND_HANDLER(handle_reset_command
)
3049 return ERROR_COMMAND_SYNTAX_ERROR
;
3051 enum target_reset_mode reset_mode
= RESET_RUN
;
3052 if (CMD_ARGC
== 1) {
3054 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
3055 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
3056 return ERROR_COMMAND_SYNTAX_ERROR
;
3057 reset_mode
= n
->value
;
3060 /* reset *all* targets */
3061 return target_process_reset(CMD_CTX
, reset_mode
);
3065 COMMAND_HANDLER(handle_resume_command
)
3069 return ERROR_COMMAND_SYNTAX_ERROR
;
3071 struct target
*target
= get_current_target(CMD_CTX
);
3073 /* with no CMD_ARGV, resume from current pc, addr = 0,
3074 * with one arguments, addr = CMD_ARGV[0],
3075 * handle breakpoints, not debugging */
3076 target_addr_t addr
= 0;
3077 if (CMD_ARGC
== 1) {
3078 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3082 return target_resume(target
, current
, addr
, 1, 0);
3085 COMMAND_HANDLER(handle_step_command
)
3088 return ERROR_COMMAND_SYNTAX_ERROR
;
3092 /* with no CMD_ARGV, step from current pc, addr = 0,
3093 * with one argument addr = CMD_ARGV[0],
3094 * handle breakpoints, debugging */
3095 target_addr_t addr
= 0;
3097 if (CMD_ARGC
== 1) {
3098 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3102 struct target
*target
= get_current_target(CMD_CTX
);
3104 return target
->type
->step(target
, current_pc
, addr
, 1);
3107 static void handle_md_output(struct command_context
*cmd_ctx
,
3108 struct target
*target
, target_addr_t address
, unsigned size
,
3109 unsigned count
, const uint8_t *buffer
)
3111 const unsigned line_bytecnt
= 32;
3112 unsigned line_modulo
= line_bytecnt
/ size
;
3114 char output
[line_bytecnt
* 4 + 1];
3115 unsigned output_len
= 0;
3117 const char *value_fmt
;
3120 value_fmt
= "%16.16"PRIx64
" ";
3123 value_fmt
= "%8.8"PRIx64
" ";
3126 value_fmt
= "%4.4"PRIx64
" ";
3129 value_fmt
= "%2.2"PRIx64
" ";
3132 /* "can't happen", caller checked */
3133 LOG_ERROR("invalid memory read size: %u", size
);
3137 for (unsigned i
= 0; i
< count
; i
++) {
3138 if (i
% line_modulo
== 0) {
3139 output_len
+= snprintf(output
+ output_len
,
3140 sizeof(output
) - output_len
,
3141 TARGET_ADDR_FMT
": ",
3142 (address
+ (i
* size
)));
3146 const uint8_t *value_ptr
= buffer
+ i
* size
;
3149 value
= target_buffer_get_u64(target
, value_ptr
);
3152 value
= target_buffer_get_u32(target
, value_ptr
);
3155 value
= target_buffer_get_u16(target
, value_ptr
);
3160 output_len
+= snprintf(output
+ output_len
,
3161 sizeof(output
) - output_len
,
3164 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
3165 command_print(cmd_ctx
, "%s", output
);
3171 COMMAND_HANDLER(handle_md_command
)
3174 return ERROR_COMMAND_SYNTAX_ERROR
;
3177 switch (CMD_NAME
[2]) {
3191 return ERROR_COMMAND_SYNTAX_ERROR
;
3194 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3195 int (*fn
)(struct target
*target
,
3196 target_addr_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
3200 fn
= target_read_phys_memory
;
3202 fn
= target_read_memory
;
3203 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
3204 return ERROR_COMMAND_SYNTAX_ERROR
;
3206 target_addr_t address
;
3207 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3211 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
3213 uint8_t *buffer
= calloc(count
, size
);
3214 if (buffer
== NULL
) {
3215 LOG_ERROR("Failed to allocate md read buffer");
3219 struct target
*target
= get_current_target(CMD_CTX
);
3220 int retval
= fn(target
, address
, size
, count
, buffer
);
3221 if (ERROR_OK
== retval
)
3222 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
3229 typedef int (*target_write_fn
)(struct target
*target
,
3230 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
3232 static int target_fill_mem(struct target
*target
,
3233 target_addr_t address
,
3241 /* We have to write in reasonably large chunks to be able
3242 * to fill large memory areas with any sane speed */
3243 const unsigned chunk_size
= 16384;
3244 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
3245 if (target_buf
== NULL
) {
3246 LOG_ERROR("Out of memory");
3250 for (unsigned i
= 0; i
< chunk_size
; i
++) {
3251 switch (data_size
) {
3253 target_buffer_set_u64(target
, target_buf
+ i
* data_size
, b
);
3256 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
3259 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
3262 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
3269 int retval
= ERROR_OK
;
3271 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
3274 if (current
> chunk_size
)
3275 current
= chunk_size
;
3276 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
3277 if (retval
!= ERROR_OK
)
3279 /* avoid GDB timeouts */
3288 COMMAND_HANDLER(handle_mw_command
)
3291 return ERROR_COMMAND_SYNTAX_ERROR
;
3292 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3297 fn
= target_write_phys_memory
;
3299 fn
= target_write_memory
;
3300 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
3301 return ERROR_COMMAND_SYNTAX_ERROR
;
3303 target_addr_t address
;
3304 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3306 target_addr_t value
;
3307 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], value
);
3311 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
3313 struct target
*target
= get_current_target(CMD_CTX
);
3315 switch (CMD_NAME
[2]) {
3329 return ERROR_COMMAND_SYNTAX_ERROR
;
3332 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
3335 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
3336 target_addr_t
*min_address
, target_addr_t
*max_address
)
3338 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
3339 return ERROR_COMMAND_SYNTAX_ERROR
;
3341 /* a base address isn't always necessary,
3342 * default to 0x0 (i.e. don't relocate) */
3343 if (CMD_ARGC
>= 2) {
3345 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3346 image
->base_address
= addr
;
3347 image
->base_address_set
= 1;
3349 image
->base_address_set
= 0;
3351 image
->start_address_set
= 0;
3354 COMMAND_PARSE_ADDRESS(CMD_ARGV
[3], *min_address
);
3355 if (CMD_ARGC
== 5) {
3356 COMMAND_PARSE_ADDRESS(CMD_ARGV
[4], *max_address
);
3357 /* use size (given) to find max (required) */
3358 *max_address
+= *min_address
;
3361 if (*min_address
> *max_address
)
3362 return ERROR_COMMAND_SYNTAX_ERROR
;
3367 COMMAND_HANDLER(handle_load_image_command
)
3371 uint32_t image_size
;
3372 target_addr_t min_address
= 0;
3373 target_addr_t max_address
= -1;
3377 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
3378 &image
, &min_address
, &max_address
);
3379 if (ERROR_OK
!= retval
)
3382 struct target
*target
= get_current_target(CMD_CTX
);
3384 struct duration bench
;
3385 duration_start(&bench
);
3387 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
3392 for (i
= 0; i
< image
.num_sections
; i
++) {
3393 buffer
= malloc(image
.sections
[i
].size
);
3394 if (buffer
== NULL
) {
3395 command_print(CMD_CTX
,
3396 "error allocating buffer for section (%d bytes)",
3397 (int)(image
.sections
[i
].size
));
3398 retval
= ERROR_FAIL
;
3402 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3403 if (retval
!= ERROR_OK
) {
3408 uint32_t offset
= 0;
3409 uint32_t length
= buf_cnt
;
3411 /* DANGER!!! beware of unsigned comparision here!!! */
3413 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3414 (image
.sections
[i
].base_address
< max_address
)) {
3416 if (image
.sections
[i
].base_address
< min_address
) {
3417 /* clip addresses below */
3418 offset
+= min_address
-image
.sections
[i
].base_address
;
3422 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3423 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3425 retval
= target_write_buffer(target
,
3426 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3427 if (retval
!= ERROR_OK
) {
3431 image_size
+= length
;
3432 command_print(CMD_CTX
, "%u bytes written at address " TARGET_ADDR_FMT
"",
3433 (unsigned int)length
,
3434 image
.sections
[i
].base_address
+ offset
);
3440 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3441 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
3442 "in %fs (%0.3f KiB/s)", image_size
,
3443 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3446 image_close(&image
);
3452 COMMAND_HANDLER(handle_dump_image_command
)
3454 struct fileio
*fileio
;
3456 int retval
, retvaltemp
;
3457 target_addr_t address
, size
;
3458 struct duration bench
;
3459 struct target
*target
= get_current_target(CMD_CTX
);
3462 return ERROR_COMMAND_SYNTAX_ERROR
;
3464 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], address
);
3465 COMMAND_PARSE_ADDRESS(CMD_ARGV
[2], size
);
3467 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
3468 buffer
= malloc(buf_size
);
3472 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3473 if (retval
!= ERROR_OK
) {
3478 duration_start(&bench
);
3481 size_t size_written
;
3482 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
3483 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
3484 if (retval
!= ERROR_OK
)
3487 retval
= fileio_write(fileio
, this_run_size
, buffer
, &size_written
);
3488 if (retval
!= ERROR_OK
)
3491 size
-= this_run_size
;
3492 address
+= this_run_size
;
3497 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3499 retval
= fileio_size(fileio
, &filesize
);
3500 if (retval
!= ERROR_OK
)
3502 command_print(CMD_CTX
,
3503 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize
,
3504 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3507 retvaltemp
= fileio_close(fileio
);
3508 if (retvaltemp
!= ERROR_OK
)
3517 IMAGE_CHECKSUM_ONLY
= 2
3520 static COMMAND_HELPER(handle_verify_image_command_internal
, enum verify_mode verify
)
3524 uint32_t image_size
;
3527 uint32_t checksum
= 0;
3528 uint32_t mem_checksum
= 0;
3532 struct target
*target
= get_current_target(CMD_CTX
);
3535 return ERROR_COMMAND_SYNTAX_ERROR
;
3538 LOG_ERROR("no target selected");
3542 struct duration bench
;
3543 duration_start(&bench
);
3545 if (CMD_ARGC
>= 2) {
3547 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3548 image
.base_address
= addr
;
3549 image
.base_address_set
= 1;
3551 image
.base_address_set
= 0;
3552 image
.base_address
= 0x0;
3555 image
.start_address_set
= 0;
3557 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3558 if (retval
!= ERROR_OK
)
3564 for (i
= 0; i
< image
.num_sections
; i
++) {
3565 buffer
= malloc(image
.sections
[i
].size
);
3566 if (buffer
== NULL
) {
3567 command_print(CMD_CTX
,
3568 "error allocating buffer for section (%d bytes)",
3569 (int)(image
.sections
[i
].size
));
3572 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3573 if (retval
!= ERROR_OK
) {
3578 if (verify
>= IMAGE_VERIFY
) {
3579 /* calculate checksum of image */
3580 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3581 if (retval
!= ERROR_OK
) {
3586 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3587 if (retval
!= ERROR_OK
) {
3591 if ((checksum
!= mem_checksum
) && (verify
== IMAGE_CHECKSUM_ONLY
)) {
3592 LOG_ERROR("checksum mismatch");
3594 retval
= ERROR_FAIL
;
3597 if (checksum
!= mem_checksum
) {
3598 /* failed crc checksum, fall back to a binary compare */
3602 LOG_ERROR("checksum mismatch - attempting binary compare");
3604 data
= malloc(buf_cnt
);
3606 /* Can we use 32bit word accesses? */
3608 int count
= buf_cnt
;
3609 if ((count
% 4) == 0) {
3613 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
3614 if (retval
== ERROR_OK
) {
3616 for (t
= 0; t
< buf_cnt
; t
++) {
3617 if (data
[t
] != buffer
[t
]) {
3618 command_print(CMD_CTX
,
3619 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3621 (unsigned)(t
+ image
.sections
[i
].base_address
),
3624 if (diffs
++ >= 127) {
3625 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
3637 command_print(CMD_CTX
, "address " TARGET_ADDR_FMT
" length 0x%08zx",
3638 image
.sections
[i
].base_address
,
3643 image_size
+= buf_cnt
;
3646 command_print(CMD_CTX
, "No more differences found.");
3649 retval
= ERROR_FAIL
;
3650 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3651 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3652 "in %fs (%0.3f KiB/s)", image_size
,
3653 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3656 image_close(&image
);
3661 COMMAND_HANDLER(handle_verify_image_checksum_command
)
3663 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_CHECKSUM_ONLY
);
3666 COMMAND_HANDLER(handle_verify_image_command
)
3668 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_VERIFY
);
3671 COMMAND_HANDLER(handle_test_image_command
)
3673 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_TEST
);
3676 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
3678 struct target
*target
= get_current_target(cmd_ctx
);
3679 struct breakpoint
*breakpoint
= target
->breakpoints
;
3680 while (breakpoint
) {
3681 if (breakpoint
->type
== BKPT_SOFT
) {
3682 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3683 breakpoint
->length
, 16);
3684 command_print(cmd_ctx
, "IVA breakpoint: " TARGET_ADDR_FMT
", 0x%x, %i, 0x%s",
3685 breakpoint
->address
,
3687 breakpoint
->set
, buf
);
3690 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3691 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3693 breakpoint
->length
, breakpoint
->set
);
3694 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3695 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3696 breakpoint
->address
,
3697 breakpoint
->length
, breakpoint
->set
);
3698 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3701 command_print(cmd_ctx
, "Breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3702 breakpoint
->address
,
3703 breakpoint
->length
, breakpoint
->set
);
3706 breakpoint
= breakpoint
->next
;
3711 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
3712 target_addr_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3714 struct target
*target
= get_current_target(cmd_ctx
);
3718 retval
= breakpoint_add(target
, addr
, length
, hw
);
3719 if (ERROR_OK
== retval
)
3720 command_print(cmd_ctx
, "breakpoint set at " TARGET_ADDR_FMT
"", addr
);
3722 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3725 } else if (addr
== 0) {
3726 if (target
->type
->add_context_breakpoint
== NULL
) {
3727 LOG_WARNING("Context breakpoint not available");
3730 retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3731 if (ERROR_OK
== retval
)
3732 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3734 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3738 if (target
->type
->add_hybrid_breakpoint
== NULL
) {
3739 LOG_WARNING("Hybrid breakpoint not available");
3742 retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3743 if (ERROR_OK
== retval
)
3744 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3746 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3753 COMMAND_HANDLER(handle_bp_command
)
3762 return handle_bp_command_list(CMD_CTX
);
3766 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3767 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3768 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3771 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
3773 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3774 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3776 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3777 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
3779 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
3780 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3782 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3787 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3788 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
3789 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
3790 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3793 return ERROR_COMMAND_SYNTAX_ERROR
;
3797 COMMAND_HANDLER(handle_rbp_command
)
3800 return ERROR_COMMAND_SYNTAX_ERROR
;
3803 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3805 struct target
*target
= get_current_target(CMD_CTX
);
3806 breakpoint_remove(target
, addr
);
3811 COMMAND_HANDLER(handle_wp_command
)
3813 struct target
*target
= get_current_target(CMD_CTX
);
3815 if (CMD_ARGC
== 0) {
3816 struct watchpoint
*watchpoint
= target
->watchpoints
;
3818 while (watchpoint
) {
3819 command_print(CMD_CTX
, "address: " TARGET_ADDR_FMT
3820 ", len: 0x%8.8" PRIx32
3821 ", r/w/a: %i, value: 0x%8.8" PRIx32
3822 ", mask: 0x%8.8" PRIx32
,
3823 watchpoint
->address
,
3825 (int)watchpoint
->rw
,
3828 watchpoint
= watchpoint
->next
;
3833 enum watchpoint_rw type
= WPT_ACCESS
;
3835 uint32_t length
= 0;
3836 uint32_t data_value
= 0x0;
3837 uint32_t data_mask
= 0xffffffff;
3841 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
3844 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3847 switch (CMD_ARGV
[2][0]) {
3858 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3859 return ERROR_COMMAND_SYNTAX_ERROR
;
3863 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3864 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3868 return ERROR_COMMAND_SYNTAX_ERROR
;
3871 int retval
= watchpoint_add(target
, addr
, length
, type
,
3872 data_value
, data_mask
);
3873 if (ERROR_OK
!= retval
)
3874 LOG_ERROR("Failure setting watchpoints");
3879 COMMAND_HANDLER(handle_rwp_command
)
3882 return ERROR_COMMAND_SYNTAX_ERROR
;
3885 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3887 struct target
*target
= get_current_target(CMD_CTX
);
3888 watchpoint_remove(target
, addr
);
3894 * Translate a virtual address to a physical address.
3896 * The low-level target implementation must have logged a detailed error
3897 * which is forwarded to telnet/GDB session.
3899 COMMAND_HANDLER(handle_virt2phys_command
)
3902 return ERROR_COMMAND_SYNTAX_ERROR
;
3905 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], va
);
3908 struct target
*target
= get_current_target(CMD_CTX
);
3909 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3910 if (retval
== ERROR_OK
)
3911 command_print(CMD_CTX
, "Physical address " TARGET_ADDR_FMT
"", pa
);
3916 static void writeData(FILE *f
, const void *data
, size_t len
)
3918 size_t written
= fwrite(data
, 1, len
, f
);
3920 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3923 static void writeLong(FILE *f
, int l
, struct target
*target
)
3927 target_buffer_set_u32(target
, val
, l
);
3928 writeData(f
, val
, 4);
3931 static void writeString(FILE *f
, char *s
)
3933 writeData(f
, s
, strlen(s
));
3936 typedef unsigned char UNIT
[2]; /* unit of profiling */
3938 /* Dump a gmon.out histogram file. */
3939 static void write_gmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
, bool with_range
,
3940 uint32_t start_address
, uint32_t end_address
, struct target
*target
, uint32_t duration_ms
)
3943 FILE *f
= fopen(filename
, "w");
3946 writeString(f
, "gmon");
3947 writeLong(f
, 0x00000001, target
); /* Version */
3948 writeLong(f
, 0, target
); /* padding */
3949 writeLong(f
, 0, target
); /* padding */
3950 writeLong(f
, 0, target
); /* padding */
3952 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3953 writeData(f
, &zero
, 1);
3955 /* figure out bucket size */
3959 min
= start_address
;
3964 for (i
= 0; i
< sampleNum
; i
++) {
3965 if (min
> samples
[i
])
3967 if (max
< samples
[i
])
3971 /* max should be (largest sample + 1)
3972 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
3976 int addressSpace
= max
- min
;
3977 assert(addressSpace
>= 2);
3979 /* FIXME: What is the reasonable number of buckets?
3980 * The profiling result will be more accurate if there are enough buckets. */
3981 static const uint32_t maxBuckets
= 128 * 1024; /* maximum buckets. */
3982 uint32_t numBuckets
= addressSpace
/ sizeof(UNIT
);
3983 if (numBuckets
> maxBuckets
)
3984 numBuckets
= maxBuckets
;
3985 int *buckets
= malloc(sizeof(int) * numBuckets
);
3986 if (buckets
== NULL
) {
3990 memset(buckets
, 0, sizeof(int) * numBuckets
);
3991 for (i
= 0; i
< sampleNum
; i
++) {
3992 uint32_t address
= samples
[i
];
3994 if ((address
< min
) || (max
<= address
))
3997 long long a
= address
- min
;
3998 long long b
= numBuckets
;
3999 long long c
= addressSpace
;
4000 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
4004 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
4005 writeLong(f
, min
, target
); /* low_pc */
4006 writeLong(f
, max
, target
); /* high_pc */
4007 writeLong(f
, numBuckets
, target
); /* # of buckets */
4008 float sample_rate
= sampleNum
/ (duration_ms
/ 1000.0);
4009 writeLong(f
, sample_rate
, target
);
4010 writeString(f
, "seconds");
4011 for (i
= 0; i
< (15-strlen("seconds")); i
++)
4012 writeData(f
, &zero
, 1);
4013 writeString(f
, "s");
4015 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
4017 char *data
= malloc(2 * numBuckets
);
4019 for (i
= 0; i
< numBuckets
; i
++) {
4024 data
[i
* 2] = val
&0xff;
4025 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
4028 writeData(f
, data
, numBuckets
* 2);
4036 /* profiling samples the CPU PC as quickly as OpenOCD is able,
4037 * which will be used as a random sampling of PC */
4038 COMMAND_HANDLER(handle_profile_command
)
4040 struct target
*target
= get_current_target(CMD_CTX
);
4042 if ((CMD_ARGC
!= 2) && (CMD_ARGC
!= 4))
4043 return ERROR_COMMAND_SYNTAX_ERROR
;
4045 const uint32_t MAX_PROFILE_SAMPLE_NUM
= 10000;
4047 uint32_t num_of_samples
;
4048 int retval
= ERROR_OK
;
4050 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], offset
);
4052 uint32_t *samples
= malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM
);
4053 if (samples
== NULL
) {
4054 LOG_ERROR("No memory to store samples.");
4058 uint64_t timestart_ms
= timeval_ms();
4060 * Some cores let us sample the PC without the
4061 * annoying halt/resume step; for example, ARMv7 PCSR.
4062 * Provide a way to use that more efficient mechanism.
4064 retval
= target_profiling(target
, samples
, MAX_PROFILE_SAMPLE_NUM
,
4065 &num_of_samples
, offset
);
4066 if (retval
!= ERROR_OK
) {
4070 uint32_t duration_ms
= timeval_ms() - timestart_ms
;
4072 assert(num_of_samples
<= MAX_PROFILE_SAMPLE_NUM
);
4074 retval
= target_poll(target
);
4075 if (retval
!= ERROR_OK
) {
4079 if (target
->state
== TARGET_RUNNING
) {
4080 retval
= target_halt(target
);
4081 if (retval
!= ERROR_OK
) {
4087 retval
= target_poll(target
);
4088 if (retval
!= ERROR_OK
) {
4093 uint32_t start_address
= 0;
4094 uint32_t end_address
= 0;
4095 bool with_range
= false;
4096 if (CMD_ARGC
== 4) {
4098 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], start_address
);
4099 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], end_address
);
4102 write_gmon(samples
, num_of_samples
, CMD_ARGV
[1],
4103 with_range
, start_address
, end_address
, target
, duration_ms
);
4104 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
4110 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
4113 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4116 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4120 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4121 valObjPtr
= Jim_NewIntObj(interp
, val
);
4122 if (!nameObjPtr
|| !valObjPtr
) {
4127 Jim_IncrRefCount(nameObjPtr
);
4128 Jim_IncrRefCount(valObjPtr
);
4129 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
4130 Jim_DecrRefCount(interp
, nameObjPtr
);
4131 Jim_DecrRefCount(interp
, valObjPtr
);
4133 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
4137 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4139 struct command_context
*context
;
4140 struct target
*target
;
4142 context
= current_command_context(interp
);
4143 assert(context
!= NULL
);
4145 target
= get_current_target(context
);
4146 if (target
== NULL
) {
4147 LOG_ERROR("mem2array: no current target");
4151 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4154 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
4162 const char *varname
;
4168 /* argv[1] = name of array to receive the data
4169 * argv[2] = desired width
4170 * argv[3] = memory address
4171 * argv[4] = count of times to read
4174 if (argc
< 4 || argc
> 5) {
4175 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4178 varname
= Jim_GetString(argv
[0], &len
);
4179 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4181 e
= Jim_GetLong(interp
, argv
[1], &l
);
4186 e
= Jim_GetLong(interp
, argv
[2], &l
);
4190 e
= Jim_GetLong(interp
, argv
[3], &l
);
4196 phys
= Jim_GetString(argv
[4], &n
);
4197 if (!strncmp(phys
, "phys", n
))
4213 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4214 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
4218 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4219 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
4222 if ((addr
+ (len
* width
)) < addr
) {
4223 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4224 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
4227 /* absurd transfer size? */
4229 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4230 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
4235 ((width
== 2) && ((addr
& 1) == 0)) ||
4236 ((width
== 4) && ((addr
& 3) == 0))) {
4240 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4241 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
4244 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4253 size_t buffersize
= 4096;
4254 uint8_t *buffer
= malloc(buffersize
);
4261 /* Slurp... in buffer size chunks */
4263 count
= len
; /* in objects.. */
4264 if (count
> (buffersize
/ width
))
4265 count
= (buffersize
/ width
);
4268 retval
= target_read_phys_memory(target
, addr
, width
, count
, buffer
);
4270 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
4271 if (retval
!= ERROR_OK
) {
4273 LOG_ERROR("mem2array: Read @ 0x%08" PRIx32
", w=%" PRId32
", cnt=%" PRId32
", failed",
4277 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4278 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
4282 v
= 0; /* shut up gcc */
4283 for (i
= 0; i
< count
; i
++, n
++) {
4286 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
4289 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
4292 v
= buffer
[i
] & 0x0ff;
4295 new_int_array_element(interp
, varname
, n
, v
);
4298 addr
+= count
* width
;
4304 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4309 static int get_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t *val
)
4312 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4316 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4320 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4326 Jim_IncrRefCount(nameObjPtr
);
4327 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
4328 Jim_DecrRefCount(interp
, nameObjPtr
);
4330 if (valObjPtr
== NULL
)
4333 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
4334 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
4339 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4341 struct command_context
*context
;
4342 struct target
*target
;
4344 context
= current_command_context(interp
);
4345 assert(context
!= NULL
);
4347 target
= get_current_target(context
);
4348 if (target
== NULL
) {
4349 LOG_ERROR("array2mem: no current target");
4353 return target_array2mem(interp
, target
, argc
-1, argv
+ 1);
4356 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
4357 int argc
, Jim_Obj
*const *argv
)
4365 const char *varname
;
4371 /* argv[1] = name of array to get the data
4372 * argv[2] = desired width
4373 * argv[3] = memory address
4374 * argv[4] = count to write
4376 if (argc
< 4 || argc
> 5) {
4377 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4380 varname
= Jim_GetString(argv
[0], &len
);
4381 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4383 e
= Jim_GetLong(interp
, argv
[1], &l
);
4388 e
= Jim_GetLong(interp
, argv
[2], &l
);
4392 e
= Jim_GetLong(interp
, argv
[3], &l
);
4398 phys
= Jim_GetString(argv
[4], &n
);
4399 if (!strncmp(phys
, "phys", n
))
4415 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4416 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4417 "Invalid width param, must be 8/16/32", NULL
);
4421 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4422 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4423 "array2mem: zero width read?", NULL
);
4426 if ((addr
+ (len
* width
)) < addr
) {
4427 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4428 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4429 "array2mem: addr + len - wraps to zero?", NULL
);
4432 /* absurd transfer size? */
4434 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4435 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4436 "array2mem: absurd > 64K item request", NULL
);
4441 ((width
== 2) && ((addr
& 1) == 0)) ||
4442 ((width
== 4) && ((addr
& 3) == 0))) {
4446 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4447 sprintf(buf
, "array2mem address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
4450 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4461 size_t buffersize
= 4096;
4462 uint8_t *buffer
= malloc(buffersize
);
4467 /* Slurp... in buffer size chunks */
4469 count
= len
; /* in objects.. */
4470 if (count
> (buffersize
/ width
))
4471 count
= (buffersize
/ width
);
4473 v
= 0; /* shut up gcc */
4474 for (i
= 0; i
< count
; i
++, n
++) {
4475 get_int_array_element(interp
, varname
, n
, &v
);
4478 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
4481 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
4484 buffer
[i
] = v
& 0x0ff;
4491 retval
= target_write_phys_memory(target
, addr
, width
, count
, buffer
);
4493 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
4494 if (retval
!= ERROR_OK
) {
4496 LOG_ERROR("array2mem: Write @ 0x%08" PRIx32
", w=%" PRId32
", cnt=%" PRId32
", failed",
4500 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4501 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
4505 addr
+= count
* width
;
4510 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4515 /* FIX? should we propagate errors here rather than printing them
4518 void target_handle_event(struct target
*target
, enum target_event e
)
4520 struct target_event_action
*teap
;
4522 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4523 if (teap
->event
== e
) {
4524 LOG_DEBUG("target(%d): %s (%s) event: %d (%s) action: %s",
4525 target
->target_number
,
4526 target_name(target
),
4527 target_type_name(target
),
4529 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
4530 Jim_GetString(teap
->body
, NULL
));
4532 /* Override current target by the target an event
4533 * is issued from (lot of scripts need it).
4534 * Return back to previous override as soon
4535 * as the handler processing is done */
4536 struct command_context
*cmd_ctx
= current_command_context(teap
->interp
);
4537 struct target
*saved_target_override
= cmd_ctx
->current_target_override
;
4538 cmd_ctx
->current_target_override
= target
;
4540 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
) {
4541 Jim_MakeErrorMessage(teap
->interp
);
4542 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
4545 cmd_ctx
->current_target_override
= saved_target_override
;
4551 * Returns true only if the target has a handler for the specified event.
4553 bool target_has_event_action(struct target
*target
, enum target_event event
)
4555 struct target_event_action
*teap
;
4557 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4558 if (teap
->event
== event
)
4564 enum target_cfg_param
{
4567 TCFG_WORK_AREA_VIRT
,
4568 TCFG_WORK_AREA_PHYS
,
4569 TCFG_WORK_AREA_SIZE
,
4570 TCFG_WORK_AREA_BACKUP
,
4573 TCFG_CHAIN_POSITION
,
4580 static Jim_Nvp nvp_config_opts
[] = {
4581 { .name
= "-type", .value
= TCFG_TYPE
},
4582 { .name
= "-event", .value
= TCFG_EVENT
},
4583 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
4584 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
4585 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
4586 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
4587 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
4588 { .name
= "-coreid", .value
= TCFG_COREID
},
4589 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
4590 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
4591 { .name
= "-rtos", .value
= TCFG_RTOS
},
4592 { .name
= "-defer-examine", .value
= TCFG_DEFER_EXAMINE
},
4593 { .name
= "-gdb-port", .value
= TCFG_GDB_PORT
},
4594 { .name
= NULL
, .value
= -1 }
4597 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
4604 /* parse config or cget options ... */
4605 while (goi
->argc
> 0) {
4606 Jim_SetEmptyResult(goi
->interp
);
4607 /* Jim_GetOpt_Debug(goi); */
4609 if (target
->type
->target_jim_configure
) {
4610 /* target defines a configure function */
4611 /* target gets first dibs on parameters */
4612 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
4621 /* otherwise we 'continue' below */
4623 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
4625 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
4631 if (goi
->isconfigure
) {
4632 Jim_SetResultFormatted(goi
->interp
,
4633 "not settable: %s", n
->name
);
4637 if (goi
->argc
!= 0) {
4638 Jim_WrongNumArgs(goi
->interp
,
4639 goi
->argc
, goi
->argv
,
4644 Jim_SetResultString(goi
->interp
,
4645 target_type_name(target
), -1);
4649 if (goi
->argc
== 0) {
4650 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
4654 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
4656 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
4660 if (goi
->isconfigure
) {
4661 if (goi
->argc
!= 1) {
4662 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
4666 if (goi
->argc
!= 0) {
4667 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
4673 struct target_event_action
*teap
;
4675 teap
= target
->event_action
;
4676 /* replace existing? */
4678 if (teap
->event
== (enum target_event
)n
->value
)
4683 if (goi
->isconfigure
) {
4684 bool replace
= true;
4687 teap
= calloc(1, sizeof(*teap
));
4690 teap
->event
= n
->value
;
4691 teap
->interp
= goi
->interp
;
4692 Jim_GetOpt_Obj(goi
, &o
);
4694 Jim_DecrRefCount(teap
->interp
, teap
->body
);
4695 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
4698 * Tcl/TK - "tk events" have a nice feature.
4699 * See the "BIND" command.
4700 * We should support that here.
4701 * You can specify %X and %Y in the event code.
4702 * The idea is: %T - target name.
4703 * The idea is: %N - target number
4704 * The idea is: %E - event name.
4706 Jim_IncrRefCount(teap
->body
);
4709 /* add to head of event list */
4710 teap
->next
= target
->event_action
;
4711 target
->event_action
= teap
;
4713 Jim_SetEmptyResult(goi
->interp
);
4717 Jim_SetEmptyResult(goi
->interp
);
4719 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
4725 case TCFG_WORK_AREA_VIRT
:
4726 if (goi
->isconfigure
) {
4727 target_free_all_working_areas(target
);
4728 e
= Jim_GetOpt_Wide(goi
, &w
);
4731 target
->working_area_virt
= w
;
4732 target
->working_area_virt_spec
= true;
4737 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
4741 case TCFG_WORK_AREA_PHYS
:
4742 if (goi
->isconfigure
) {
4743 target_free_all_working_areas(target
);
4744 e
= Jim_GetOpt_Wide(goi
, &w
);
4747 target
->working_area_phys
= w
;
4748 target
->working_area_phys_spec
= true;
4753 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
4757 case TCFG_WORK_AREA_SIZE
:
4758 if (goi
->isconfigure
) {
4759 target_free_all_working_areas(target
);
4760 e
= Jim_GetOpt_Wide(goi
, &w
);
4763 target
->working_area_size
= w
;
4768 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4772 case TCFG_WORK_AREA_BACKUP
:
4773 if (goi
->isconfigure
) {
4774 target_free_all_working_areas(target
);
4775 e
= Jim_GetOpt_Wide(goi
, &w
);
4778 /* make this exactly 1 or 0 */
4779 target
->backup_working_area
= (!!w
);
4784 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
4785 /* loop for more e*/
4790 if (goi
->isconfigure
) {
4791 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
4793 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
4796 target
->endianness
= n
->value
;
4801 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4802 if (n
->name
== NULL
) {
4803 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4804 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4806 Jim_SetResultString(goi
->interp
, n
->name
, -1);
4811 if (goi
->isconfigure
) {
4812 e
= Jim_GetOpt_Wide(goi
, &w
);
4815 target
->coreid
= (int32_t)w
;
4820 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4824 case TCFG_CHAIN_POSITION
:
4825 if (goi
->isconfigure
) {
4827 struct jtag_tap
*tap
;
4829 if (target
->has_dap
) {
4830 Jim_SetResultString(goi
->interp
,
4831 "target requires -dap parameter instead of -chain-position!", -1);
4835 target_free_all_working_areas(target
);
4836 e
= Jim_GetOpt_Obj(goi
, &o_t
);
4839 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
4843 target
->tap_configured
= true;
4848 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
4849 /* loop for more e*/
4852 if (goi
->isconfigure
) {
4853 e
= Jim_GetOpt_Wide(goi
, &w
);
4856 target
->dbgbase
= (uint32_t)w
;
4857 target
->dbgbase_set
= true;
4862 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
4868 int result
= rtos_create(goi
, target
);
4869 if (result
!= JIM_OK
)
4875 case TCFG_DEFER_EXAMINE
:
4877 target
->defer_examine
= true;
4882 if (goi
->isconfigure
) {
4884 e
= Jim_GetOpt_String(goi
, &s
, NULL
);
4887 target
->gdb_port_override
= strdup(s
);
4892 Jim_SetResultString(goi
->interp
, target
->gdb_port_override
? : "undefined", -1);
4896 } /* while (goi->argc) */
4899 /* done - we return */
4903 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4907 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4908 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4910 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4911 "missing: -option ...");
4914 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4915 return target_configure(&goi
, target
);
4918 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4920 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4923 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4925 if (goi
.argc
< 2 || goi
.argc
> 4) {
4926 Jim_SetResultFormatted(goi
.interp
,
4927 "usage: %s [phys] <address> <data> [<count>]", cmd_name
);
4932 fn
= target_write_memory
;
4935 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4937 struct Jim_Obj
*obj
;
4938 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4942 fn
= target_write_phys_memory
;
4946 e
= Jim_GetOpt_Wide(&goi
, &a
);
4951 e
= Jim_GetOpt_Wide(&goi
, &b
);
4956 if (goi
.argc
== 1) {
4957 e
= Jim_GetOpt_Wide(&goi
, &c
);
4962 /* all args must be consumed */
4966 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4968 if (strcasecmp(cmd_name
, "mww") == 0)
4970 else if (strcasecmp(cmd_name
, "mwh") == 0)
4972 else if (strcasecmp(cmd_name
, "mwb") == 0)
4975 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4979 return (target_fill_mem(target
, a
, fn
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4983 * @brief Reads an array of words/halfwords/bytes from target memory starting at specified address.
4985 * Usage: mdw [phys] <address> [<count>] - for 32 bit reads
4986 * mdh [phys] <address> [<count>] - for 16 bit reads
4987 * mdb [phys] <address> [<count>] - for 8 bit reads
4989 * Count defaults to 1.
4991 * Calls target_read_memory or target_read_phys_memory depending on
4992 * the presence of the "phys" argument
4993 * Reads the target memory in blocks of max. 32 bytes, and returns an array of ints formatted
4994 * to int representation in base16.
4995 * Also outputs read data in a human readable form using command_print
4997 * @param phys if present target_read_phys_memory will be used instead of target_read_memory
4998 * @param address address where to start the read. May be specified in decimal or hex using the standard "0x" prefix
4999 * @param count optional count parameter to read an array of values. If not specified, defaults to 1.
5000 * @returns: JIM_ERR on error or JIM_OK on success and sets the result string to an array of ascii formatted numbers
5001 * on success, with [<count>] number of elements.
5003 * In case of little endian target:
5004 * Example1: "mdw 0x00000000" returns "10123456"
5005 * Exmaple2: "mdh 0x00000000 1" returns "3456"
5006 * Example3: "mdb 0x00000000" returns "56"
5007 * Example4: "mdh 0x00000000 2" returns "3456 1012"
5008 * Example5: "mdb 0x00000000 3" returns "56 34 12"
5010 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5012 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5015 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5017 if ((goi
.argc
< 1) || (goi
.argc
> 3)) {
5018 Jim_SetResultFormatted(goi
.interp
,
5019 "usage: %s [phys] <address> [<count>]", cmd_name
);
5023 int (*fn
)(struct target
*target
,
5024 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
5025 fn
= target_read_memory
;
5028 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
5030 struct Jim_Obj
*obj
;
5031 e
= Jim_GetOpt_Obj(&goi
, &obj
);
5035 fn
= target_read_phys_memory
;
5038 /* Read address parameter */
5040 e
= Jim_GetOpt_Wide(&goi
, &addr
);
5044 /* If next parameter exists, read it out as the count parameter, if not, set it to 1 (default) */
5046 if (goi
.argc
== 1) {
5047 e
= Jim_GetOpt_Wide(&goi
, &count
);
5053 /* all args must be consumed */
5057 jim_wide dwidth
= 1; /* shut up gcc */
5058 if (strcasecmp(cmd_name
, "mdw") == 0)
5060 else if (strcasecmp(cmd_name
, "mdh") == 0)
5062 else if (strcasecmp(cmd_name
, "mdb") == 0)
5065 LOG_ERROR("command '%s' unknown: ", cmd_name
);
5069 /* convert count to "bytes" */
5070 int bytes
= count
* dwidth
;
5072 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
5073 uint8_t target_buf
[32];
5076 y
= (bytes
< 16) ? bytes
: 16; /* y = min(bytes, 16); */
5078 /* Try to read out next block */
5079 e
= fn(target
, addr
, dwidth
, y
/ dwidth
, target_buf
);
5081 if (e
!= ERROR_OK
) {
5082 Jim_SetResultFormatted(interp
, "error reading target @ 0x%08lx", (long)addr
);
5086 command_print_sameline(NULL
, "0x%08x ", (int)(addr
));
5089 for (x
= 0; x
< 16 && x
< y
; x
+= 4) {
5090 z
= target_buffer_get_u32(target
, &(target_buf
[x
]));
5091 command_print_sameline(NULL
, "%08x ", (int)(z
));
5093 for (; (x
< 16) ; x
+= 4)
5094 command_print_sameline(NULL
, " ");
5097 for (x
= 0; x
< 16 && x
< y
; x
+= 2) {
5098 z
= target_buffer_get_u16(target
, &(target_buf
[x
]));
5099 command_print_sameline(NULL
, "%04x ", (int)(z
));
5101 for (; (x
< 16) ; x
+= 2)
5102 command_print_sameline(NULL
, " ");
5106 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
5107 z
= target_buffer_get_u8(target
, &(target_buf
[x
]));
5108 command_print_sameline(NULL
, "%02x ", (int)(z
));
5110 for (; (x
< 16) ; x
+= 1)
5111 command_print_sameline(NULL
, " ");
5114 /* ascii-ify the bytes */
5115 for (x
= 0 ; x
< y
; x
++) {
5116 if ((target_buf
[x
] >= 0x20) &&
5117 (target_buf
[x
] <= 0x7e)) {
5121 target_buf
[x
] = '.';
5126 target_buf
[x
] = ' ';
5131 /* print - with a newline */
5132 command_print_sameline(NULL
, "%s\n", target_buf
);
5140 static int jim_target_mem2array(Jim_Interp
*interp
,
5141 int argc
, Jim_Obj
*const *argv
)
5143 struct target
*target
= Jim_CmdPrivData(interp
);
5144 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
5147 static int jim_target_array2mem(Jim_Interp
*interp
,
5148 int argc
, Jim_Obj
*const *argv
)
5150 struct target
*target
= Jim_CmdPrivData(interp
);
5151 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
5154 static int jim_target_tap_disabled(Jim_Interp
*interp
)
5156 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
5160 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5162 bool allow_defer
= false;
5165 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5167 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5168 Jim_SetResultFormatted(goi
.interp
,
5169 "usage: %s ['allow-defer']", cmd_name
);
5173 strcmp(Jim_GetString(argv
[1], NULL
), "allow-defer") == 0) {
5175 struct Jim_Obj
*obj
;
5176 int e
= Jim_GetOpt_Obj(&goi
, &obj
);
5182 struct target
*target
= Jim_CmdPrivData(interp
);
5183 if (!target
->tap
->enabled
)
5184 return jim_target_tap_disabled(interp
);
5186 if (allow_defer
&& target
->defer_examine
) {
5187 LOG_INFO("Deferring arp_examine of %s", target_name(target
));
5188 LOG_INFO("Use arp_examine command to examine it manually!");
5192 int e
= target
->type
->examine(target
);
5198 static int jim_target_was_examined(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5200 struct target
*target
= Jim_CmdPrivData(interp
);
5202 Jim_SetResultBool(interp
, target_was_examined(target
));
5206 static int jim_target_examine_deferred(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5208 struct target
*target
= Jim_CmdPrivData(interp
);
5210 Jim_SetResultBool(interp
, target
->defer_examine
);
5214 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5217 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5220 struct target
*target
= Jim_CmdPrivData(interp
);
5222 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
5228 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5231 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5234 struct target
*target
= Jim_CmdPrivData(interp
);
5235 if (!target
->tap
->enabled
)
5236 return jim_target_tap_disabled(interp
);
5239 if (!(target_was_examined(target
)))
5240 e
= ERROR_TARGET_NOT_EXAMINED
;
5242 e
= target
->type
->poll(target
);
5248 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5251 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5253 if (goi
.argc
!= 2) {
5254 Jim_WrongNumArgs(interp
, 0, argv
,
5255 "([tT]|[fF]|assert|deassert) BOOL");
5260 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
5262 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
5265 /* the halt or not param */
5267 e
= Jim_GetOpt_Wide(&goi
, &a
);
5271 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
5272 if (!target
->tap
->enabled
)
5273 return jim_target_tap_disabled(interp
);
5275 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
5276 Jim_SetResultFormatted(interp
,
5277 "No target-specific reset for %s",
5278 target_name(target
));
5282 if (target
->defer_examine
)
5283 target_reset_examined(target
);
5285 /* determine if we should halt or not. */
5286 target
->reset_halt
= !!a
;
5287 /* When this happens - all workareas are invalid. */
5288 target_free_all_working_areas_restore(target
, 0);
5291 if (n
->value
== NVP_ASSERT
)
5292 e
= target
->type
->assert_reset(target
);
5294 e
= target
->type
->deassert_reset(target
);
5295 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5298 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5301 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5304 struct target
*target
= Jim_CmdPrivData(interp
);
5305 if (!target
->tap
->enabled
)
5306 return jim_target_tap_disabled(interp
);
5307 int e
= target
->type
->halt(target
);
5308 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5311 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5314 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5316 /* params: <name> statename timeoutmsecs */
5317 if (goi
.argc
!= 2) {
5318 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5319 Jim_SetResultFormatted(goi
.interp
,
5320 "%s <state_name> <timeout_in_msec>", cmd_name
);
5325 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
5327 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
, 1);
5331 e
= Jim_GetOpt_Wide(&goi
, &a
);
5334 struct target
*target
= Jim_CmdPrivData(interp
);
5335 if (!target
->tap
->enabled
)
5336 return jim_target_tap_disabled(interp
);
5338 e
= target_wait_state(target
, n
->value
, a
);
5339 if (e
!= ERROR_OK
) {
5340 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
5341 Jim_SetResultFormatted(goi
.interp
,
5342 "target: %s wait %s fails (%#s) %s",
5343 target_name(target
), n
->name
,
5344 eObj
, target_strerror_safe(e
));
5345 Jim_FreeNewObj(interp
, eObj
);
5350 /* List for human, Events defined for this target.
5351 * scripts/programs should use 'name cget -event NAME'
5353 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5355 struct command_context
*cmd_ctx
= current_command_context(interp
);
5356 assert(cmd_ctx
!= NULL
);
5358 struct target
*target
= Jim_CmdPrivData(interp
);
5359 struct target_event_action
*teap
= target
->event_action
;
5360 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
5361 target
->target_number
,
5362 target_name(target
));
5363 command_print(cmd_ctx
, "%-25s | Body", "Event");
5364 command_print(cmd_ctx
, "------------------------- | "
5365 "----------------------------------------");
5367 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
5368 command_print(cmd_ctx
, "%-25s | %s",
5369 opt
->name
, Jim_GetString(teap
->body
, NULL
));
5372 command_print(cmd_ctx
, "***END***");
5375 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5378 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5381 struct target
*target
= Jim_CmdPrivData(interp
);
5382 Jim_SetResultString(interp
, target_state_name(target
), -1);
5385 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5388 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5389 if (goi
.argc
!= 1) {
5390 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5391 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
5395 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
5397 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
5400 struct target
*target
= Jim_CmdPrivData(interp
);
5401 target_handle_event(target
, n
->value
);
5405 static const struct command_registration target_instance_command_handlers
[] = {
5407 .name
= "configure",
5408 .mode
= COMMAND_CONFIG
,
5409 .jim_handler
= jim_target_configure
,
5410 .help
= "configure a new target for use",
5411 .usage
= "[target_attribute ...]",
5415 .mode
= COMMAND_ANY
,
5416 .jim_handler
= jim_target_configure
,
5417 .help
= "returns the specified target attribute",
5418 .usage
= "target_attribute",
5422 .mode
= COMMAND_EXEC
,
5423 .jim_handler
= jim_target_mw
,
5424 .help
= "Write 32-bit word(s) to target memory",
5425 .usage
= "address data [count]",
5429 .mode
= COMMAND_EXEC
,
5430 .jim_handler
= jim_target_mw
,
5431 .help
= "Write 16-bit half-word(s) to target memory",
5432 .usage
= "address data [count]",
5436 .mode
= COMMAND_EXEC
,
5437 .jim_handler
= jim_target_mw
,
5438 .help
= "Write byte(s) to target memory",
5439 .usage
= "address data [count]",
5443 .mode
= COMMAND_EXEC
,
5444 .jim_handler
= jim_target_md
,
5445 .help
= "Display target memory as 32-bit words",
5446 .usage
= "address [count]",
5450 .mode
= COMMAND_EXEC
,
5451 .jim_handler
= jim_target_md
,
5452 .help
= "Display target memory as 16-bit half-words",
5453 .usage
= "address [count]",
5457 .mode
= COMMAND_EXEC
,
5458 .jim_handler
= jim_target_md
,
5459 .help
= "Display target memory as 8-bit bytes",
5460 .usage
= "address [count]",
5463 .name
= "array2mem",
5464 .mode
= COMMAND_EXEC
,
5465 .jim_handler
= jim_target_array2mem
,
5466 .help
= "Writes Tcl array of 8/16/32 bit numbers "
5468 .usage
= "arrayname bitwidth address count",
5471 .name
= "mem2array",
5472 .mode
= COMMAND_EXEC
,
5473 .jim_handler
= jim_target_mem2array
,
5474 .help
= "Loads Tcl array of 8/16/32 bit numbers "
5475 "from target memory",
5476 .usage
= "arrayname bitwidth address count",
5479 .name
= "eventlist",
5480 .mode
= COMMAND_EXEC
,
5481 .jim_handler
= jim_target_event_list
,
5482 .help
= "displays a table of events defined for this target",
5486 .mode
= COMMAND_EXEC
,
5487 .jim_handler
= jim_target_current_state
,
5488 .help
= "displays the current state of this target",
5491 .name
= "arp_examine",
5492 .mode
= COMMAND_EXEC
,
5493 .jim_handler
= jim_target_examine
,
5494 .help
= "used internally for reset processing",
5495 .usage
= "['allow-defer']",
5498 .name
= "was_examined",
5499 .mode
= COMMAND_EXEC
,
5500 .jim_handler
= jim_target_was_examined
,
5501 .help
= "used internally for reset processing",
5504 .name
= "examine_deferred",
5505 .mode
= COMMAND_EXEC
,
5506 .jim_handler
= jim_target_examine_deferred
,
5507 .help
= "used internally for reset processing",
5510 .name
= "arp_halt_gdb",
5511 .mode
= COMMAND_EXEC
,
5512 .jim_handler
= jim_target_halt_gdb
,
5513 .help
= "used internally for reset processing to halt GDB",
5517 .mode
= COMMAND_EXEC
,
5518 .jim_handler
= jim_target_poll
,
5519 .help
= "used internally for reset processing",
5522 .name
= "arp_reset",
5523 .mode
= COMMAND_EXEC
,
5524 .jim_handler
= jim_target_reset
,
5525 .help
= "used internally for reset processing",
5529 .mode
= COMMAND_EXEC
,
5530 .jim_handler
= jim_target_halt
,
5531 .help
= "used internally for reset processing",
5534 .name
= "arp_waitstate",
5535 .mode
= COMMAND_EXEC
,
5536 .jim_handler
= jim_target_wait_state
,
5537 .help
= "used internally for reset processing",
5540 .name
= "invoke-event",
5541 .mode
= COMMAND_EXEC
,
5542 .jim_handler
= jim_target_invoke_event
,
5543 .help
= "invoke handler for specified event",
5544 .usage
= "event_name",
5546 COMMAND_REGISTRATION_DONE
5549 static int target_create(Jim_GetOptInfo
*goi
)
5556 struct target
*target
;
5557 struct command_context
*cmd_ctx
;
5559 cmd_ctx
= current_command_context(goi
->interp
);
5560 assert(cmd_ctx
!= NULL
);
5562 if (goi
->argc
< 3) {
5563 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
5568 Jim_GetOpt_Obj(goi
, &new_cmd
);
5569 /* does this command exist? */
5570 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
5572 cp
= Jim_GetString(new_cmd
, NULL
);
5573 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
5578 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
5581 struct transport
*tr
= get_current_transport();
5582 if (tr
->override_target
) {
5583 e
= tr
->override_target(&cp
);
5584 if (e
!= ERROR_OK
) {
5585 LOG_ERROR("The selected transport doesn't support this target");
5588 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
5590 /* now does target type exist */
5591 for (x
= 0 ; target_types
[x
] ; x
++) {
5592 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
5597 /* check for deprecated name */
5598 if (target_types
[x
]->deprecated_name
) {
5599 if (0 == strcmp(cp
, target_types
[x
]->deprecated_name
)) {
5601 LOG_WARNING("target name is deprecated use: \'%s\'", target_types
[x
]->name
);
5606 if (target_types
[x
] == NULL
) {
5607 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
5608 for (x
= 0 ; target_types
[x
] ; x
++) {
5609 if (target_types
[x
+ 1]) {
5610 Jim_AppendStrings(goi
->interp
,
5611 Jim_GetResult(goi
->interp
),
5612 target_types
[x
]->name
,
5615 Jim_AppendStrings(goi
->interp
,
5616 Jim_GetResult(goi
->interp
),
5618 target_types
[x
]->name
, NULL
);
5625 target
= calloc(1, sizeof(struct target
));
5626 /* set target number */
5627 target
->target_number
= new_target_number();
5628 cmd_ctx
->current_target
= target
;
5630 /* allocate memory for each unique target type */
5631 target
->type
= calloc(1, sizeof(struct target_type
));
5633 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
5635 /* will be set by "-endian" */
5636 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5638 /* default to first core, override with -coreid */
5641 target
->working_area
= 0x0;
5642 target
->working_area_size
= 0x0;
5643 target
->working_areas
= NULL
;
5644 target
->backup_working_area
= 0;
5646 target
->state
= TARGET_UNKNOWN
;
5647 target
->debug_reason
= DBG_REASON_UNDEFINED
;
5648 target
->reg_cache
= NULL
;
5649 target
->breakpoints
= NULL
;
5650 target
->watchpoints
= NULL
;
5651 target
->next
= NULL
;
5652 target
->arch_info
= NULL
;
5654 target
->verbose_halt_msg
= true;
5656 target
->halt_issued
= false;
5658 /* initialize trace information */
5659 target
->trace_info
= calloc(1, sizeof(struct trace
));
5661 target
->dbgmsg
= NULL
;
5662 target
->dbg_msg_enabled
= 0;
5664 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5666 target
->rtos
= NULL
;
5667 target
->rtos_auto_detect
= false;
5669 target
->gdb_port_override
= NULL
;
5671 /* Do the rest as "configure" options */
5672 goi
->isconfigure
= 1;
5673 e
= target_configure(goi
, target
);
5676 if (target
->has_dap
) {
5677 if (!target
->dap_configured
) {
5678 Jim_SetResultString(goi
->interp
, "-dap ?name? required when creating target", -1);
5682 if (!target
->tap_configured
) {
5683 Jim_SetResultString(goi
->interp
, "-chain-position ?name? required when creating target", -1);
5687 /* tap must be set after target was configured */
5688 if (target
->tap
== NULL
)
5693 free(target
->gdb_port_override
);
5699 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
5700 /* default endian to little if not specified */
5701 target
->endianness
= TARGET_LITTLE_ENDIAN
;
5704 cp
= Jim_GetString(new_cmd
, NULL
);
5705 target
->cmd_name
= strdup(cp
);
5707 if (target
->type
->target_create
) {
5708 e
= (*(target
->type
->target_create
))(target
, goi
->interp
);
5709 if (e
!= ERROR_OK
) {
5710 LOG_DEBUG("target_create failed");
5711 free(target
->gdb_port_override
);
5713 free(target
->cmd_name
);
5719 /* create the target specific commands */
5720 if (target
->type
->commands
) {
5721 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
5723 LOG_ERROR("unable to register '%s' commands", cp
);
5726 /* append to end of list */
5728 struct target
**tpp
;
5729 tpp
= &(all_targets
);
5731 tpp
= &((*tpp
)->next
);
5735 /* now - create the new target name command */
5736 const struct command_registration target_subcommands
[] = {
5738 .chain
= target_instance_command_handlers
,
5741 .chain
= target
->type
->commands
,
5743 COMMAND_REGISTRATION_DONE
5745 const struct command_registration target_commands
[] = {
5748 .mode
= COMMAND_ANY
,
5749 .help
= "target command group",
5751 .chain
= target_subcommands
,
5753 COMMAND_REGISTRATION_DONE
5755 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
5759 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
5761 command_set_handler_data(c
, target
);
5763 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
5766 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5769 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5772 struct command_context
*cmd_ctx
= current_command_context(interp
);
5773 assert(cmd_ctx
!= NULL
);
5775 Jim_SetResultString(interp
, target_name(get_current_target(cmd_ctx
)), -1);
5779 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5782 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5785 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5786 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++) {
5787 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5788 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
5793 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5796 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5799 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5800 struct target
*target
= all_targets
;
5802 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5803 Jim_NewStringObj(interp
, target_name(target
), -1));
5804 target
= target
->next
;
5809 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5812 const char *targetname
;
5814 struct target
*target
= (struct target
*) NULL
;
5815 struct target_list
*head
, *curr
, *new;
5816 curr
= (struct target_list
*) NULL
;
5817 head
= (struct target_list
*) NULL
;
5820 LOG_DEBUG("%d", argc
);
5821 /* argv[1] = target to associate in smp
5822 * argv[2] = target to assoicate in smp
5826 for (i
= 1; i
< argc
; i
++) {
5828 targetname
= Jim_GetString(argv
[i
], &len
);
5829 target
= get_target(targetname
);
5830 LOG_DEBUG("%s ", targetname
);
5832 new = malloc(sizeof(struct target_list
));
5833 new->target
= target
;
5834 new->next
= (struct target_list
*)NULL
;
5835 if (head
== (struct target_list
*)NULL
) {
5844 /* now parse the list of cpu and put the target in smp mode*/
5847 while (curr
!= (struct target_list
*)NULL
) {
5848 target
= curr
->target
;
5850 target
->head
= head
;
5854 if (target
&& target
->rtos
)
5855 retval
= rtos_smp_init(head
->target
);
5861 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5864 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5866 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5867 "<name> <target_type> [<target_options> ...]");
5870 return target_create(&goi
);
5873 static const struct command_registration target_subcommand_handlers
[] = {
5876 .mode
= COMMAND_CONFIG
,
5877 .handler
= handle_target_init_command
,
5878 .help
= "initialize targets",
5882 /* REVISIT this should be COMMAND_CONFIG ... */
5883 .mode
= COMMAND_ANY
,
5884 .jim_handler
= jim_target_create
,
5885 .usage
= "name type '-chain-position' name [options ...]",
5886 .help
= "Creates and selects a new target",
5890 .mode
= COMMAND_ANY
,
5891 .jim_handler
= jim_target_current
,
5892 .help
= "Returns the currently selected target",
5896 .mode
= COMMAND_ANY
,
5897 .jim_handler
= jim_target_types
,
5898 .help
= "Returns the available target types as "
5899 "a list of strings",
5903 .mode
= COMMAND_ANY
,
5904 .jim_handler
= jim_target_names
,
5905 .help
= "Returns the names of all targets as a list of strings",
5909 .mode
= COMMAND_ANY
,
5910 .jim_handler
= jim_target_smp
,
5911 .usage
= "targetname1 targetname2 ...",
5912 .help
= "gather several target in a smp list"
5915 COMMAND_REGISTRATION_DONE
5919 target_addr_t address
;
5925 static int fastload_num
;
5926 static struct FastLoad
*fastload
;
5928 static void free_fastload(void)
5930 if (fastload
!= NULL
) {
5932 for (i
= 0; i
< fastload_num
; i
++) {
5933 if (fastload
[i
].data
)
5934 free(fastload
[i
].data
);
5941 COMMAND_HANDLER(handle_fast_load_image_command
)
5945 uint32_t image_size
;
5946 target_addr_t min_address
= 0;
5947 target_addr_t max_address
= -1;
5952 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
5953 &image
, &min_address
, &max_address
);
5954 if (ERROR_OK
!= retval
)
5957 struct duration bench
;
5958 duration_start(&bench
);
5960 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5961 if (retval
!= ERROR_OK
)
5966 fastload_num
= image
.num_sections
;
5967 fastload
= malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5968 if (fastload
== NULL
) {
5969 command_print(CMD_CTX
, "out of memory");
5970 image_close(&image
);
5973 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5974 for (i
= 0; i
< image
.num_sections
; i
++) {
5975 buffer
= malloc(image
.sections
[i
].size
);
5976 if (buffer
== NULL
) {
5977 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5978 (int)(image
.sections
[i
].size
));
5979 retval
= ERROR_FAIL
;
5983 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
5984 if (retval
!= ERROR_OK
) {
5989 uint32_t offset
= 0;
5990 uint32_t length
= buf_cnt
;
5992 /* DANGER!!! beware of unsigned comparision here!!! */
5994 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
5995 (image
.sections
[i
].base_address
< max_address
)) {
5996 if (image
.sections
[i
].base_address
< min_address
) {
5997 /* clip addresses below */
5998 offset
+= min_address
-image
.sections
[i
].base_address
;
6002 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
6003 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
6005 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
6006 fastload
[i
].data
= malloc(length
);
6007 if (fastload
[i
].data
== NULL
) {
6009 command_print(CMD_CTX
, "error allocating buffer for section (%" PRIu32
" bytes)",
6011 retval
= ERROR_FAIL
;
6014 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
6015 fastload
[i
].length
= length
;
6017 image_size
+= length
;
6018 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
6019 (unsigned int)length
,
6020 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
6026 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
6027 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
6028 "in %fs (%0.3f KiB/s)", image_size
,
6029 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
6031 command_print(CMD_CTX
,
6032 "WARNING: image has not been loaded to target!"
6033 "You can issue a 'fast_load' to finish loading.");
6036 image_close(&image
);
6038 if (retval
!= ERROR_OK
)
6044 COMMAND_HANDLER(handle_fast_load_command
)
6047 return ERROR_COMMAND_SYNTAX_ERROR
;
6048 if (fastload
== NULL
) {
6049 LOG_ERROR("No image in memory");
6053 int64_t ms
= timeval_ms();
6055 int retval
= ERROR_OK
;
6056 for (i
= 0; i
< fastload_num
; i
++) {
6057 struct target
*target
= get_current_target(CMD_CTX
);
6058 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
6059 (unsigned int)(fastload
[i
].address
),
6060 (unsigned int)(fastload
[i
].length
));
6061 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
6062 if (retval
!= ERROR_OK
)
6064 size
+= fastload
[i
].length
;
6066 if (retval
== ERROR_OK
) {
6067 int64_t after
= timeval_ms();
6068 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
6073 static const struct command_registration target_command_handlers
[] = {
6076 .handler
= handle_targets_command
,
6077 .mode
= COMMAND_ANY
,
6078 .help
= "change current default target (one parameter) "
6079 "or prints table of all targets (no parameters)",
6080 .usage
= "[target]",
6084 .mode
= COMMAND_CONFIG
,
6085 .help
= "configure target",
6087 .chain
= target_subcommand_handlers
,
6089 COMMAND_REGISTRATION_DONE
6092 int target_register_commands(struct command_context
*cmd_ctx
)
6094 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
6097 static bool target_reset_nag
= true;
6099 bool get_target_reset_nag(void)
6101 return target_reset_nag
;
6104 COMMAND_HANDLER(handle_target_reset_nag
)
6106 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
6107 &target_reset_nag
, "Nag after each reset about options to improve "
6111 COMMAND_HANDLER(handle_ps_command
)
6113 struct target
*target
= get_current_target(CMD_CTX
);
6115 if (target
->state
!= TARGET_HALTED
) {
6116 LOG_INFO("target not halted !!");
6120 if ((target
->rtos
) && (target
->rtos
->type
)
6121 && (target
->rtos
->type
->ps_command
)) {
6122 display
= target
->rtos
->type
->ps_command(target
);
6123 command_print(CMD_CTX
, "%s", display
);
6128 return ERROR_TARGET_FAILURE
;
6132 static void binprint(struct command_context
*cmd_ctx
, const char *text
, const uint8_t *buf
, int size
)
6135 command_print_sameline(cmd_ctx
, "%s", text
);
6136 for (int i
= 0; i
< size
; i
++)
6137 command_print_sameline(cmd_ctx
, " %02x", buf
[i
]);
6138 command_print(cmd_ctx
, " ");
6141 COMMAND_HANDLER(handle_test_mem_access_command
)
6143 struct target
*target
= get_current_target(CMD_CTX
);
6145 int retval
= ERROR_OK
;
6147 if (target
->state
!= TARGET_HALTED
) {
6148 LOG_INFO("target not halted !!");
6153 return ERROR_COMMAND_SYNTAX_ERROR
;
6155 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], test_size
);
6158 size_t num_bytes
= test_size
+ 4;
6160 struct working_area
*wa
= NULL
;
6161 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6162 if (retval
!= ERROR_OK
) {
6163 LOG_ERROR("Not enough working area");
6167 uint8_t *test_pattern
= malloc(num_bytes
);
6169 for (size_t i
= 0; i
< num_bytes
; i
++)
6170 test_pattern
[i
] = rand();
6172 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6173 if (retval
!= ERROR_OK
) {
6174 LOG_ERROR("Test pattern write failed");
6178 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6179 for (int size
= 1; size
<= 4; size
*= 2) {
6180 for (int offset
= 0; offset
< 4; offset
++) {
6181 uint32_t count
= test_size
/ size
;
6182 size_t host_bufsiz
= (count
+ 2) * size
+ host_offset
;
6183 uint8_t *read_ref
= malloc(host_bufsiz
);
6184 uint8_t *read_buf
= malloc(host_bufsiz
);
6186 for (size_t i
= 0; i
< host_bufsiz
; i
++) {
6187 read_ref
[i
] = rand();
6188 read_buf
[i
] = read_ref
[i
];
6190 command_print_sameline(CMD_CTX
,
6191 "Test read %" PRIu32
" x %d @ %d to %saligned buffer: ", count
,
6192 size
, offset
, host_offset
? "un" : "");
6194 struct duration bench
;
6195 duration_start(&bench
);
6197 retval
= target_read_memory(target
, wa
->address
+ offset
, size
, count
,
6198 read_buf
+ size
+ host_offset
);
6200 duration_measure(&bench
);
6202 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6203 command_print(CMD_CTX
, "Unsupported alignment");
6205 } else if (retval
!= ERROR_OK
) {
6206 command_print(CMD_CTX
, "Memory read failed");
6210 /* replay on host */
6211 memcpy(read_ref
+ size
+ host_offset
, test_pattern
+ offset
, count
* size
);
6214 int result
= memcmp(read_ref
, read_buf
, host_bufsiz
);
6216 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
6217 duration_elapsed(&bench
),
6218 duration_kbps(&bench
, count
* size
));
6220 command_print(CMD_CTX
, "Compare failed");
6221 binprint(CMD_CTX
, "ref:", read_ref
, host_bufsiz
);
6222 binprint(CMD_CTX
, "buf:", read_buf
, host_bufsiz
);
6235 target_free_working_area(target
, wa
);
6238 num_bytes
= test_size
+ 4 + 4 + 4;
6240 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6241 if (retval
!= ERROR_OK
) {
6242 LOG_ERROR("Not enough working area");
6246 test_pattern
= malloc(num_bytes
);
6248 for (size_t i
= 0; i
< num_bytes
; i
++)
6249 test_pattern
[i
] = rand();
6251 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6252 for (int size
= 1; size
<= 4; size
*= 2) {
6253 for (int offset
= 0; offset
< 4; offset
++) {
6254 uint32_t count
= test_size
/ size
;
6255 size_t host_bufsiz
= count
* size
+ host_offset
;
6256 uint8_t *read_ref
= malloc(num_bytes
);
6257 uint8_t *read_buf
= malloc(num_bytes
);
6258 uint8_t *write_buf
= malloc(host_bufsiz
);
6260 for (size_t i
= 0; i
< host_bufsiz
; i
++)
6261 write_buf
[i
] = rand();
6262 command_print_sameline(CMD_CTX
,
6263 "Test write %" PRIu32
" x %d @ %d from %saligned buffer: ", count
,
6264 size
, offset
, host_offset
? "un" : "");
6266 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6267 if (retval
!= ERROR_OK
) {
6268 command_print(CMD_CTX
, "Test pattern write failed");
6272 /* replay on host */
6273 memcpy(read_ref
, test_pattern
, num_bytes
);
6274 memcpy(read_ref
+ size
+ offset
, write_buf
+ host_offset
, count
* size
);
6276 struct duration bench
;
6277 duration_start(&bench
);
6279 retval
= target_write_memory(target
, wa
->address
+ size
+ offset
, size
, count
,
6280 write_buf
+ host_offset
);
6282 duration_measure(&bench
);
6284 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6285 command_print(CMD_CTX
, "Unsupported alignment");
6287 } else if (retval
!= ERROR_OK
) {
6288 command_print(CMD_CTX
, "Memory write failed");
6293 retval
= target_read_memory(target
, wa
->address
, 1, num_bytes
, read_buf
);
6294 if (retval
!= ERROR_OK
) {
6295 command_print(CMD_CTX
, "Test pattern write failed");
6300 int result
= memcmp(read_ref
, read_buf
, num_bytes
);
6302 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
6303 duration_elapsed(&bench
),
6304 duration_kbps(&bench
, count
* size
));
6306 command_print(CMD_CTX
, "Compare failed");
6307 binprint(CMD_CTX
, "ref:", read_ref
, num_bytes
);
6308 binprint(CMD_CTX
, "buf:", read_buf
, num_bytes
);
6320 target_free_working_area(target
, wa
);
6324 static const struct command_registration target_exec_command_handlers
[] = {
6326 .name
= "fast_load_image",
6327 .handler
= handle_fast_load_image_command
,
6328 .mode
= COMMAND_ANY
,
6329 .help
= "Load image into server memory for later use by "
6330 "fast_load; primarily for profiling",
6331 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6332 "[min_address [max_length]]",
6335 .name
= "fast_load",
6336 .handler
= handle_fast_load_command
,
6337 .mode
= COMMAND_EXEC
,
6338 .help
= "loads active fast load image to current target "
6339 "- mainly for profiling purposes",
6344 .handler
= handle_profile_command
,
6345 .mode
= COMMAND_EXEC
,
6346 .usage
= "seconds filename [start end]",
6347 .help
= "profiling samples the CPU PC",
6349 /** @todo don't register virt2phys() unless target supports it */
6351 .name
= "virt2phys",
6352 .handler
= handle_virt2phys_command
,
6353 .mode
= COMMAND_ANY
,
6354 .help
= "translate a virtual address into a physical address",
6355 .usage
= "virtual_address",
6359 .handler
= handle_reg_command
,
6360 .mode
= COMMAND_EXEC
,
6361 .help
= "display (reread from target with \"force\") or set a register; "
6362 "with no arguments, displays all registers and their values",
6363 .usage
= "[(register_number|register_name) [(value|'force')]]",
6367 .handler
= handle_poll_command
,
6368 .mode
= COMMAND_EXEC
,
6369 .help
= "poll target state; or reconfigure background polling",
6370 .usage
= "['on'|'off']",
6373 .name
= "wait_halt",
6374 .handler
= handle_wait_halt_command
,
6375 .mode
= COMMAND_EXEC
,
6376 .help
= "wait up to the specified number of milliseconds "
6377 "(default 5000) for a previously requested halt",
6378 .usage
= "[milliseconds]",
6382 .handler
= handle_halt_command
,
6383 .mode
= COMMAND_EXEC
,
6384 .help
= "request target to halt, then wait up to the specified"
6385 "number of milliseconds (default 5000) for it to complete",
6386 .usage
= "[milliseconds]",
6390 .handler
= handle_resume_command
,
6391 .mode
= COMMAND_EXEC
,
6392 .help
= "resume target execution from current PC or address",
6393 .usage
= "[address]",
6397 .handler
= handle_reset_command
,
6398 .mode
= COMMAND_EXEC
,
6399 .usage
= "[run|halt|init]",
6400 .help
= "Reset all targets into the specified mode."
6401 "Default reset mode is run, if not given.",
6404 .name
= "soft_reset_halt",
6405 .handler
= handle_soft_reset_halt_command
,
6406 .mode
= COMMAND_EXEC
,
6408 .help
= "halt the target and do a soft reset",
6412 .handler
= handle_step_command
,
6413 .mode
= COMMAND_EXEC
,
6414 .help
= "step one instruction from current PC or address",
6415 .usage
= "[address]",
6419 .handler
= handle_md_command
,
6420 .mode
= COMMAND_EXEC
,
6421 .help
= "display memory words",
6422 .usage
= "['phys'] address [count]",
6426 .handler
= handle_md_command
,
6427 .mode
= COMMAND_EXEC
,
6428 .help
= "display memory words",
6429 .usage
= "['phys'] address [count]",
6433 .handler
= handle_md_command
,
6434 .mode
= COMMAND_EXEC
,
6435 .help
= "display memory half-words",
6436 .usage
= "['phys'] address [count]",
6440 .handler
= handle_md_command
,
6441 .mode
= COMMAND_EXEC
,
6442 .help
= "display memory bytes",
6443 .usage
= "['phys'] address [count]",
6447 .handler
= handle_mw_command
,
6448 .mode
= COMMAND_EXEC
,
6449 .help
= "write memory word",
6450 .usage
= "['phys'] address value [count]",
6454 .handler
= handle_mw_command
,
6455 .mode
= COMMAND_EXEC
,
6456 .help
= "write memory word",
6457 .usage
= "['phys'] address value [count]",
6461 .handler
= handle_mw_command
,
6462 .mode
= COMMAND_EXEC
,
6463 .help
= "write memory half-word",
6464 .usage
= "['phys'] address value [count]",
6468 .handler
= handle_mw_command
,
6469 .mode
= COMMAND_EXEC
,
6470 .help
= "write memory byte",
6471 .usage
= "['phys'] address value [count]",
6475 .handler
= handle_bp_command
,
6476 .mode
= COMMAND_EXEC
,
6477 .help
= "list or set hardware or software breakpoint",
6478 .usage
= "<address> [<asid>] <length> ['hw'|'hw_ctx']",
6482 .handler
= handle_rbp_command
,
6483 .mode
= COMMAND_EXEC
,
6484 .help
= "remove breakpoint",
6489 .handler
= handle_wp_command
,
6490 .mode
= COMMAND_EXEC
,
6491 .help
= "list (no params) or create watchpoints",
6492 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
6496 .handler
= handle_rwp_command
,
6497 .mode
= COMMAND_EXEC
,
6498 .help
= "remove watchpoint",
6502 .name
= "load_image",
6503 .handler
= handle_load_image_command
,
6504 .mode
= COMMAND_EXEC
,
6505 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6506 "[min_address] [max_length]",
6509 .name
= "dump_image",
6510 .handler
= handle_dump_image_command
,
6511 .mode
= COMMAND_EXEC
,
6512 .usage
= "filename address size",
6515 .name
= "verify_image_checksum",
6516 .handler
= handle_verify_image_checksum_command
,
6517 .mode
= COMMAND_EXEC
,
6518 .usage
= "filename [offset [type]]",
6521 .name
= "verify_image",
6522 .handler
= handle_verify_image_command
,
6523 .mode
= COMMAND_EXEC
,
6524 .usage
= "filename [offset [type]]",
6527 .name
= "test_image",
6528 .handler
= handle_test_image_command
,
6529 .mode
= COMMAND_EXEC
,
6530 .usage
= "filename [offset [type]]",
6533 .name
= "mem2array",
6534 .mode
= COMMAND_EXEC
,
6535 .jim_handler
= jim_mem2array
,
6536 .help
= "read 8/16/32 bit memory and return as a TCL array "
6537 "for script processing",
6538 .usage
= "arrayname bitwidth address count",
6541 .name
= "array2mem",
6542 .mode
= COMMAND_EXEC
,
6543 .jim_handler
= jim_array2mem
,
6544 .help
= "convert a TCL array to memory locations "
6545 "and write the 8/16/32 bit values",
6546 .usage
= "arrayname bitwidth address count",
6549 .name
= "reset_nag",
6550 .handler
= handle_target_reset_nag
,
6551 .mode
= COMMAND_ANY
,
6552 .help
= "Nag after each reset about options that could have been "
6553 "enabled to improve performance. ",
6554 .usage
= "['enable'|'disable']",
6558 .handler
= handle_ps_command
,
6559 .mode
= COMMAND_EXEC
,
6560 .help
= "list all tasks ",
6564 .name
= "test_mem_access",
6565 .handler
= handle_test_mem_access_command
,
6566 .mode
= COMMAND_EXEC
,
6567 .help
= "Test the target's memory access functions",
6571 COMMAND_REGISTRATION_DONE
6573 static int target_register_user_commands(struct command_context
*cmd_ctx
)
6575 int retval
= ERROR_OK
;
6576 retval
= target_request_register_commands(cmd_ctx
);
6577 if (retval
!= ERROR_OK
)
6580 retval
= trace_register_commands(cmd_ctx
);
6581 if (retval
!= ERROR_OK
)
6585 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);