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
;
112 static struct target_type
*target_types
[] = {
150 struct target
*all_targets
;
151 static struct target_event_callback
*target_event_callbacks
;
152 static struct target_timer_callback
*target_timer_callbacks
;
153 LIST_HEAD(target_reset_callback_list
);
154 LIST_HEAD(target_trace_callback_list
);
155 static const int polling_interval
= 100;
157 static const Jim_Nvp nvp_assert
[] = {
158 { .name
= "assert", NVP_ASSERT
},
159 { .name
= "deassert", NVP_DEASSERT
},
160 { .name
= "T", NVP_ASSERT
},
161 { .name
= "F", NVP_DEASSERT
},
162 { .name
= "t", NVP_ASSERT
},
163 { .name
= "f", NVP_DEASSERT
},
164 { .name
= NULL
, .value
= -1 }
167 static const Jim_Nvp nvp_error_target
[] = {
168 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
169 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
170 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
171 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
172 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
173 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
174 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
175 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
176 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
177 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
178 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
179 { .value
= -1, .name
= NULL
}
182 static const char *target_strerror_safe(int err
)
186 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
193 static const Jim_Nvp nvp_target_event
[] = {
195 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
196 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
197 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
198 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
199 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
201 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
202 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
204 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
205 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
206 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
207 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
208 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
209 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
210 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
211 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
213 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
214 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
216 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
217 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
219 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
220 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
222 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
223 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
225 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
226 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
228 { .value
= TARGET_EVENT_TRACE_CONFIG
, .name
= "trace-config" },
230 { .name
= NULL
, .value
= -1 }
233 static const Jim_Nvp nvp_target_state
[] = {
234 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
235 { .name
= "running", .value
= TARGET_RUNNING
},
236 { .name
= "halted", .value
= TARGET_HALTED
},
237 { .name
= "reset", .value
= TARGET_RESET
},
238 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
239 { .name
= NULL
, .value
= -1 },
242 static const Jim_Nvp nvp_target_debug_reason
[] = {
243 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
244 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
245 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
246 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
247 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
248 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
249 { .name
= "program-exit" , .value
= DBG_REASON_EXIT
},
250 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
251 { .name
= NULL
, .value
= -1 },
254 static const Jim_Nvp nvp_target_endian
[] = {
255 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
256 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
257 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
258 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
259 { .name
= NULL
, .value
= -1 },
262 static const Jim_Nvp nvp_reset_modes
[] = {
263 { .name
= "unknown", .value
= RESET_UNKNOWN
},
264 { .name
= "run" , .value
= RESET_RUN
},
265 { .name
= "halt" , .value
= RESET_HALT
},
266 { .name
= "init" , .value
= RESET_INIT
},
267 { .name
= NULL
, .value
= -1 },
270 const char *debug_reason_name(struct target
*t
)
274 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
275 t
->debug_reason
)->name
;
277 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
278 cp
= "(*BUG*unknown*BUG*)";
283 const char *target_state_name(struct target
*t
)
286 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
288 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
289 cp
= "(*BUG*unknown*BUG*)";
292 if (!target_was_examined(t
) && t
->defer_examine
)
293 cp
= "examine deferred";
298 const char *target_event_name(enum target_event event
)
301 cp
= Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
;
303 LOG_ERROR("Invalid target event: %d", (int)(event
));
304 cp
= "(*BUG*unknown*BUG*)";
309 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
312 cp
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
;
314 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
315 cp
= "(*BUG*unknown*BUG*)";
320 /* determine the number of the new target */
321 static int new_target_number(void)
326 /* number is 0 based */
330 if (x
< t
->target_number
)
331 x
= t
->target_number
;
337 /* read a uint64_t from a buffer in target memory endianness */
338 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
340 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
341 return le_to_h_u64(buffer
);
343 return be_to_h_u64(buffer
);
346 /* read a uint32_t from a buffer in target memory endianness */
347 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
349 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
350 return le_to_h_u32(buffer
);
352 return be_to_h_u32(buffer
);
355 /* read a uint24_t from a buffer in target memory endianness */
356 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
358 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
359 return le_to_h_u24(buffer
);
361 return be_to_h_u24(buffer
);
364 /* read a uint16_t from a buffer in target memory endianness */
365 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
367 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
368 return le_to_h_u16(buffer
);
370 return be_to_h_u16(buffer
);
373 /* read a uint8_t from a buffer in target memory endianness */
374 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
376 return *buffer
& 0x0ff;
379 /* write a uint64_t to a buffer in target memory endianness */
380 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
382 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
383 h_u64_to_le(buffer
, value
);
385 h_u64_to_be(buffer
, value
);
388 /* write a uint32_t to a buffer in target memory endianness */
389 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
391 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
392 h_u32_to_le(buffer
, value
);
394 h_u32_to_be(buffer
, value
);
397 /* write a uint24_t to a buffer in target memory endianness */
398 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
400 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
401 h_u24_to_le(buffer
, value
);
403 h_u24_to_be(buffer
, value
);
406 /* write a uint16_t to a buffer in target memory endianness */
407 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
409 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
410 h_u16_to_le(buffer
, value
);
412 h_u16_to_be(buffer
, value
);
415 /* write a uint8_t to a buffer in target memory endianness */
416 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
421 /* write a uint64_t array to a buffer in target memory endianness */
422 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
425 for (i
= 0; i
< count
; i
++)
426 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
429 /* write a uint32_t array to a buffer in target memory endianness */
430 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
433 for (i
= 0; i
< count
; i
++)
434 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
437 /* write a uint16_t array to a buffer in target memory endianness */
438 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
441 for (i
= 0; i
< count
; i
++)
442 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
445 /* write a uint64_t array to a buffer in target memory endianness */
446 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
449 for (i
= 0; i
< count
; i
++)
450 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
453 /* write a uint32_t array to a buffer in target memory endianness */
454 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
457 for (i
= 0; i
< count
; i
++)
458 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
461 /* write a uint16_t array to a buffer in target memory endianness */
462 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
465 for (i
= 0; i
< count
; i
++)
466 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
469 /* return a pointer to a configured target; id is name or number */
470 struct target
*get_target(const char *id
)
472 struct target
*target
;
474 /* try as tcltarget name */
475 for (target
= all_targets
; target
; target
= target
->next
) {
476 if (target_name(target
) == NULL
)
478 if (strcmp(id
, target_name(target
)) == 0)
482 /* It's OK to remove this fallback sometime after August 2010 or so */
484 /* no match, try as number */
486 if (parse_uint(id
, &num
) != ERROR_OK
)
489 for (target
= all_targets
; target
; target
= target
->next
) {
490 if (target
->target_number
== (int)num
) {
491 LOG_WARNING("use '%s' as target identifier, not '%u'",
492 target_name(target
), num
);
500 /* returns a pointer to the n-th configured target */
501 struct target
*get_target_by_num(int num
)
503 struct target
*target
= all_targets
;
506 if (target
->target_number
== num
)
508 target
= target
->next
;
514 struct target
*get_current_target(struct command_context
*cmd_ctx
)
516 struct target
*target
= cmd_ctx
->current_target_override
517 ? cmd_ctx
->current_target_override
518 : cmd_ctx
->current_target
;
520 if (target
== NULL
) {
521 LOG_ERROR("BUG: current_target out of bounds");
528 int target_poll(struct target
*target
)
532 /* We can't poll until after examine */
533 if (!target_was_examined(target
)) {
534 /* Fail silently lest we pollute the log */
538 retval
= target
->type
->poll(target
);
539 if (retval
!= ERROR_OK
)
542 if (target
->halt_issued
) {
543 if (target
->state
== TARGET_HALTED
)
544 target
->halt_issued
= false;
546 int64_t t
= timeval_ms() - target
->halt_issued_time
;
547 if (t
> DEFAULT_HALT_TIMEOUT
) {
548 target
->halt_issued
= false;
549 LOG_INFO("Halt timed out, wake up GDB.");
550 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
558 int target_halt(struct target
*target
)
561 /* We can't poll until after examine */
562 if (!target_was_examined(target
)) {
563 LOG_ERROR("Target not examined yet");
567 retval
= target
->type
->halt(target
);
568 if (retval
!= ERROR_OK
)
571 target
->halt_issued
= true;
572 target
->halt_issued_time
= timeval_ms();
578 * Make the target (re)start executing using its saved execution
579 * context (possibly with some modifications).
581 * @param target Which target should start executing.
582 * @param current True to use the target's saved program counter instead
583 * of the address parameter
584 * @param address Optionally used as the program counter.
585 * @param handle_breakpoints True iff breakpoints at the resumption PC
586 * should be skipped. (For example, maybe execution was stopped by
587 * such a breakpoint, in which case it would be counterprodutive to
589 * @param debug_execution False if all working areas allocated by OpenOCD
590 * should be released and/or restored to their original contents.
591 * (This would for example be true to run some downloaded "helper"
592 * algorithm code, which resides in one such working buffer and uses
593 * another for data storage.)
595 * @todo Resolve the ambiguity about what the "debug_execution" flag
596 * signifies. For example, Target implementations don't agree on how
597 * it relates to invalidation of the register cache, or to whether
598 * breakpoints and watchpoints should be enabled. (It would seem wrong
599 * to enable breakpoints when running downloaded "helper" algorithms
600 * (debug_execution true), since the breakpoints would be set to match
601 * target firmware being debugged, not the helper algorithm.... and
602 * enabling them could cause such helpers to malfunction (for example,
603 * by overwriting data with a breakpoint instruction. On the other
604 * hand the infrastructure for running such helpers might use this
605 * procedure but rely on hardware breakpoint to detect termination.)
607 int target_resume(struct target
*target
, int current
, target_addr_t address
,
608 int handle_breakpoints
, int debug_execution
)
612 /* We can't poll until after examine */
613 if (!target_was_examined(target
)) {
614 LOG_ERROR("Target not examined yet");
618 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
620 /* note that resume *must* be asynchronous. The CPU can halt before
621 * we poll. The CPU can even halt at the current PC as a result of
622 * a software breakpoint being inserted by (a bug?) the application.
624 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
625 if (retval
!= ERROR_OK
)
628 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
633 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
638 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
639 if (n
->name
== NULL
) {
640 LOG_ERROR("invalid reset mode");
644 struct target
*target
;
645 for (target
= all_targets
; target
; target
= target
->next
)
646 target_call_reset_callbacks(target
, reset_mode
);
648 /* disable polling during reset to make reset event scripts
649 * more predictable, i.e. dr/irscan & pathmove in events will
650 * not have JTAG operations injected into the middle of a sequence.
652 bool save_poll
= jtag_poll_get_enabled();
654 jtag_poll_set_enabled(false);
656 sprintf(buf
, "ocd_process_reset %s", n
->name
);
657 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
659 jtag_poll_set_enabled(save_poll
);
661 if (retval
!= JIM_OK
) {
662 Jim_MakeErrorMessage(cmd_ctx
->interp
);
663 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
667 /* We want any events to be processed before the prompt */
668 retval
= target_call_timer_callbacks_now();
670 for (target
= all_targets
; target
; target
= target
->next
) {
671 target
->type
->check_reset(target
);
672 target
->running_alg
= false;
678 static int identity_virt2phys(struct target
*target
,
679 target_addr_t
virtual, target_addr_t
*physical
)
685 static int no_mmu(struct target
*target
, int *enabled
)
691 static int default_examine(struct target
*target
)
693 target_set_examined(target
);
697 /* no check by default */
698 static int default_check_reset(struct target
*target
)
703 int target_examine_one(struct target
*target
)
705 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
707 int retval
= target
->type
->examine(target
);
708 if (retval
!= ERROR_OK
)
711 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
716 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
718 struct target
*target
= priv
;
720 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
723 jtag_unregister_event_callback(jtag_enable_callback
, target
);
725 return target_examine_one(target
);
728 /* Targets that correctly implement init + examine, i.e.
729 * no communication with target during init:
733 int target_examine(void)
735 int retval
= ERROR_OK
;
736 struct target
*target
;
738 for (target
= all_targets
; target
; target
= target
->next
) {
739 /* defer examination, but don't skip it */
740 if (!target
->tap
->enabled
) {
741 jtag_register_event_callback(jtag_enable_callback
,
746 if (target
->defer_examine
)
749 retval
= target_examine_one(target
);
750 if (retval
!= ERROR_OK
)
756 const char *target_type_name(struct target
*target
)
758 return target
->type
->name
;
761 static int target_soft_reset_halt(struct target
*target
)
763 if (!target_was_examined(target
)) {
764 LOG_ERROR("Target not examined yet");
767 if (!target
->type
->soft_reset_halt
) {
768 LOG_ERROR("Target %s does not support soft_reset_halt",
769 target_name(target
));
772 return target
->type
->soft_reset_halt(target
);
776 * Downloads a target-specific native code algorithm to the target,
777 * and executes it. * Note that some targets may need to set up, enable,
778 * and tear down a breakpoint (hard or * soft) to detect algorithm
779 * termination, while others may support lower overhead schemes where
780 * soft breakpoints embedded in the algorithm automatically terminate the
783 * @param target used to run the algorithm
784 * @param arch_info target-specific description of the algorithm.
786 int target_run_algorithm(struct target
*target
,
787 int num_mem_params
, struct mem_param
*mem_params
,
788 int num_reg_params
, struct reg_param
*reg_param
,
789 uint32_t entry_point
, uint32_t exit_point
,
790 int timeout_ms
, void *arch_info
)
792 int retval
= ERROR_FAIL
;
794 if (!target_was_examined(target
)) {
795 LOG_ERROR("Target not examined yet");
798 if (!target
->type
->run_algorithm
) {
799 LOG_ERROR("Target type '%s' does not support %s",
800 target_type_name(target
), __func__
);
804 target
->running_alg
= true;
805 retval
= target
->type
->run_algorithm(target
,
806 num_mem_params
, mem_params
,
807 num_reg_params
, reg_param
,
808 entry_point
, exit_point
, timeout_ms
, arch_info
);
809 target
->running_alg
= false;
816 * Executes a target-specific native code algorithm and leaves it running.
818 * @param target used to run the algorithm
819 * @param arch_info target-specific description of the algorithm.
821 int target_start_algorithm(struct target
*target
,
822 int num_mem_params
, struct mem_param
*mem_params
,
823 int num_reg_params
, struct reg_param
*reg_params
,
824 uint32_t entry_point
, uint32_t exit_point
,
827 int retval
= ERROR_FAIL
;
829 if (!target_was_examined(target
)) {
830 LOG_ERROR("Target not examined yet");
833 if (!target
->type
->start_algorithm
) {
834 LOG_ERROR("Target type '%s' does not support %s",
835 target_type_name(target
), __func__
);
838 if (target
->running_alg
) {
839 LOG_ERROR("Target is already running an algorithm");
843 target
->running_alg
= true;
844 retval
= target
->type
->start_algorithm(target
,
845 num_mem_params
, mem_params
,
846 num_reg_params
, reg_params
,
847 entry_point
, exit_point
, arch_info
);
854 * Waits for an algorithm started with target_start_algorithm() to complete.
856 * @param target used to run the algorithm
857 * @param arch_info target-specific description of the algorithm.
859 int target_wait_algorithm(struct target
*target
,
860 int num_mem_params
, struct mem_param
*mem_params
,
861 int num_reg_params
, struct reg_param
*reg_params
,
862 uint32_t exit_point
, int timeout_ms
,
865 int retval
= ERROR_FAIL
;
867 if (!target
->type
->wait_algorithm
) {
868 LOG_ERROR("Target type '%s' does not support %s",
869 target_type_name(target
), __func__
);
872 if (!target
->running_alg
) {
873 LOG_ERROR("Target is not running an algorithm");
877 retval
= target
->type
->wait_algorithm(target
,
878 num_mem_params
, mem_params
,
879 num_reg_params
, reg_params
,
880 exit_point
, timeout_ms
, arch_info
);
881 if (retval
!= ERROR_TARGET_TIMEOUT
)
882 target
->running_alg
= false;
889 * Streams data to a circular buffer on target intended for consumption by code
890 * running asynchronously on target.
892 * This is intended for applications where target-specific native code runs
893 * on the target, receives data from the circular buffer, does something with
894 * it (most likely writing it to a flash memory), and advances the circular
897 * This assumes that the helper algorithm has already been loaded to the target,
898 * but has not been started yet. Given memory and register parameters are passed
901 * The buffer is defined by (buffer_start, buffer_size) arguments and has the
904 * [buffer_start + 0, buffer_start + 4):
905 * Write Pointer address (aka head). Written and updated by this
906 * routine when new data is written to the circular buffer.
907 * [buffer_start + 4, buffer_start + 8):
908 * Read Pointer address (aka tail). Updated by code running on the
909 * target after it consumes data.
910 * [buffer_start + 8, buffer_start + buffer_size):
911 * Circular buffer contents.
913 * See contrib/loaders/flash/stm32f1x.S for an example.
915 * @param target used to run the algorithm
916 * @param buffer address on the host where data to be sent is located
917 * @param count number of blocks to send
918 * @param block_size size in bytes of each block
919 * @param num_mem_params count of memory-based params to pass to algorithm
920 * @param mem_params memory-based params to pass to algorithm
921 * @param num_reg_params count of register-based params to pass to algorithm
922 * @param reg_params memory-based params to pass to algorithm
923 * @param buffer_start address on the target of the circular buffer structure
924 * @param buffer_size size of the circular buffer structure
925 * @param entry_point address on the target to execute to start the algorithm
926 * @param exit_point address at which to set a breakpoint to catch the
927 * end of the algorithm; can be 0 if target triggers a breakpoint itself
930 int target_run_flash_async_algorithm(struct target
*target
,
931 const uint8_t *buffer
, uint32_t count
, int block_size
,
932 int num_mem_params
, struct mem_param
*mem_params
,
933 int num_reg_params
, struct reg_param
*reg_params
,
934 uint32_t buffer_start
, uint32_t buffer_size
,
935 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
940 const uint8_t *buffer_orig
= buffer
;
942 /* Set up working area. First word is write pointer, second word is read pointer,
943 * rest is fifo data area. */
944 uint32_t wp_addr
= buffer_start
;
945 uint32_t rp_addr
= buffer_start
+ 4;
946 uint32_t fifo_start_addr
= buffer_start
+ 8;
947 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
949 uint32_t wp
= fifo_start_addr
;
950 uint32_t rp
= fifo_start_addr
;
952 /* validate block_size is 2^n */
953 assert(!block_size
|| !(block_size
& (block_size
- 1)));
955 retval
= target_write_u32(target
, wp_addr
, wp
);
956 if (retval
!= ERROR_OK
)
958 retval
= target_write_u32(target
, rp_addr
, rp
);
959 if (retval
!= ERROR_OK
)
962 /* Start up algorithm on target and let it idle while writing the first chunk */
963 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
964 num_reg_params
, reg_params
,
969 if (retval
!= ERROR_OK
) {
970 LOG_ERROR("error starting target flash write algorithm");
976 retval
= target_read_u32(target
, rp_addr
, &rp
);
977 if (retval
!= ERROR_OK
) {
978 LOG_ERROR("failed to get read pointer");
982 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
983 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
986 LOG_ERROR("flash write algorithm aborted by target");
987 retval
= ERROR_FLASH_OPERATION_FAILED
;
991 if (((rp
- fifo_start_addr
) & (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
992 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
996 /* Count the number of bytes available in the fifo without
997 * crossing the wrap around. Make sure to not fill it completely,
998 * because that would make wp == rp and that's the empty condition. */
999 uint32_t thisrun_bytes
;
1001 thisrun_bytes
= rp
- wp
- block_size
;
1002 else if (rp
> fifo_start_addr
)
1003 thisrun_bytes
= fifo_end_addr
- wp
;
1005 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
1007 if (thisrun_bytes
== 0) {
1008 /* Throttle polling a bit if transfer is (much) faster than flash
1009 * programming. The exact delay shouldn't matter as long as it's
1010 * less than buffer size / flash speed. This is very unlikely to
1011 * run when using high latency connections such as USB. */
1014 /* to stop an infinite loop on some targets check and increment a timeout
1015 * this issue was observed on a stellaris using the new ICDI interface */
1016 if (timeout
++ >= 500) {
1017 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1018 return ERROR_FLASH_OPERATION_FAILED
;
1023 /* reset our timeout */
1026 /* Limit to the amount of data we actually want to write */
1027 if (thisrun_bytes
> count
* block_size
)
1028 thisrun_bytes
= count
* block_size
;
1030 /* Write data to fifo */
1031 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
1032 if (retval
!= ERROR_OK
)
1035 /* Update counters and wrap write pointer */
1036 buffer
+= thisrun_bytes
;
1037 count
-= thisrun_bytes
/ block_size
;
1038 wp
+= thisrun_bytes
;
1039 if (wp
>= fifo_end_addr
)
1040 wp
= fifo_start_addr
;
1042 /* Store updated write pointer to target */
1043 retval
= target_write_u32(target
, wp_addr
, wp
);
1044 if (retval
!= ERROR_OK
)
1048 if (retval
!= ERROR_OK
) {
1049 /* abort flash write algorithm on target */
1050 target_write_u32(target
, wp_addr
, 0);
1053 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1054 num_reg_params
, reg_params
,
1059 if (retval2
!= ERROR_OK
) {
1060 LOG_ERROR("error waiting for target flash write algorithm");
1064 if (retval
== ERROR_OK
) {
1065 /* check if algorithm set rp = 0 after fifo writer loop finished */
1066 retval
= target_read_u32(target
, rp_addr
, &rp
);
1067 if (retval
== ERROR_OK
&& rp
== 0) {
1068 LOG_ERROR("flash write algorithm aborted by target");
1069 retval
= ERROR_FLASH_OPERATION_FAILED
;
1076 int target_read_memory(struct target
*target
,
1077 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1079 if (!target_was_examined(target
)) {
1080 LOG_ERROR("Target not examined yet");
1083 if (!target
->type
->read_memory
) {
1084 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1087 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1090 int target_read_phys_memory(struct target
*target
,
1091 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1093 if (!target_was_examined(target
)) {
1094 LOG_ERROR("Target not examined yet");
1097 if (!target
->type
->read_phys_memory
) {
1098 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1101 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1104 int target_write_memory(struct target
*target
,
1105 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1107 if (!target_was_examined(target
)) {
1108 LOG_ERROR("Target not examined yet");
1111 if (!target
->type
->write_memory
) {
1112 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1115 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1118 int target_write_phys_memory(struct target
*target
,
1119 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1121 if (!target_was_examined(target
)) {
1122 LOG_ERROR("Target not examined yet");
1125 if (!target
->type
->write_phys_memory
) {
1126 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1129 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1132 int target_add_breakpoint(struct target
*target
,
1133 struct breakpoint
*breakpoint
)
1135 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1136 LOG_WARNING("target %s is not halted (add breakpoint)", target_name(target
));
1137 return ERROR_TARGET_NOT_HALTED
;
1139 return target
->type
->add_breakpoint(target
, breakpoint
);
1142 int target_add_context_breakpoint(struct target
*target
,
1143 struct breakpoint
*breakpoint
)
1145 if (target
->state
!= TARGET_HALTED
) {
1146 LOG_WARNING("target %s is not halted (add context breakpoint)", target_name(target
));
1147 return ERROR_TARGET_NOT_HALTED
;
1149 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1152 int target_add_hybrid_breakpoint(struct target
*target
,
1153 struct breakpoint
*breakpoint
)
1155 if (target
->state
!= TARGET_HALTED
) {
1156 LOG_WARNING("target %s is not halted (add hybrid breakpoint)", target_name(target
));
1157 return ERROR_TARGET_NOT_HALTED
;
1159 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1162 int target_remove_breakpoint(struct target
*target
,
1163 struct breakpoint
*breakpoint
)
1165 return target
->type
->remove_breakpoint(target
, breakpoint
);
1168 int target_add_watchpoint(struct target
*target
,
1169 struct watchpoint
*watchpoint
)
1171 if (target
->state
!= TARGET_HALTED
) {
1172 LOG_WARNING("target %s is not halted (add watchpoint)", target_name(target
));
1173 return ERROR_TARGET_NOT_HALTED
;
1175 return target
->type
->add_watchpoint(target
, watchpoint
);
1177 int target_remove_watchpoint(struct target
*target
,
1178 struct watchpoint
*watchpoint
)
1180 return target
->type
->remove_watchpoint(target
, watchpoint
);
1182 int target_hit_watchpoint(struct target
*target
,
1183 struct watchpoint
**hit_watchpoint
)
1185 if (target
->state
!= TARGET_HALTED
) {
1186 LOG_WARNING("target %s is not halted (hit watchpoint)", target
->cmd_name
);
1187 return ERROR_TARGET_NOT_HALTED
;
1190 if (target
->type
->hit_watchpoint
== NULL
) {
1191 /* For backward compatible, if hit_watchpoint is not implemented,
1192 * return ERROR_FAIL such that gdb_server will not take the nonsense
1197 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1200 int target_get_gdb_reg_list(struct target
*target
,
1201 struct reg
**reg_list
[], int *reg_list_size
,
1202 enum target_register_class reg_class
)
1204 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1206 int target_step(struct target
*target
,
1207 int current
, target_addr_t address
, int handle_breakpoints
)
1209 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1212 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1214 if (target
->state
!= TARGET_HALTED
) {
1215 LOG_WARNING("target %s is not halted (gdb fileio)", target
->cmd_name
);
1216 return ERROR_TARGET_NOT_HALTED
;
1218 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1221 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1223 if (target
->state
!= TARGET_HALTED
) {
1224 LOG_WARNING("target %s is not halted (gdb fileio end)", target
->cmd_name
);
1225 return ERROR_TARGET_NOT_HALTED
;
1227 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1230 int target_profiling(struct target
*target
, uint32_t *samples
,
1231 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1233 if (target
->state
!= TARGET_HALTED
) {
1234 LOG_WARNING("target %s is not halted (profiling)", target
->cmd_name
);
1235 return ERROR_TARGET_NOT_HALTED
;
1237 return target
->type
->profiling(target
, samples
, max_num_samples
,
1238 num_samples
, seconds
);
1242 * Reset the @c examined flag for the given target.
1243 * Pure paranoia -- targets are zeroed on allocation.
1245 static void target_reset_examined(struct target
*target
)
1247 target
->examined
= false;
1250 static int handle_target(void *priv
);
1252 static int target_init_one(struct command_context
*cmd_ctx
,
1253 struct target
*target
)
1255 target_reset_examined(target
);
1257 struct target_type
*type
= target
->type
;
1258 if (type
->examine
== NULL
)
1259 type
->examine
= default_examine
;
1261 if (type
->check_reset
== NULL
)
1262 type
->check_reset
= default_check_reset
;
1264 assert(type
->init_target
!= NULL
);
1266 int retval
= type
->init_target(cmd_ctx
, target
);
1267 if (ERROR_OK
!= retval
) {
1268 LOG_ERROR("target '%s' init failed", target_name(target
));
1272 /* Sanity-check MMU support ... stub in what we must, to help
1273 * implement it in stages, but warn if we need to do so.
1276 if (type
->virt2phys
== NULL
) {
1277 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1278 type
->virt2phys
= identity_virt2phys
;
1281 /* Make sure no-MMU targets all behave the same: make no
1282 * distinction between physical and virtual addresses, and
1283 * ensure that virt2phys() is always an identity mapping.
1285 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1286 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1289 type
->write_phys_memory
= type
->write_memory
;
1290 type
->read_phys_memory
= type
->read_memory
;
1291 type
->virt2phys
= identity_virt2phys
;
1294 if (target
->type
->read_buffer
== NULL
)
1295 target
->type
->read_buffer
= target_read_buffer_default
;
1297 if (target
->type
->write_buffer
== NULL
)
1298 target
->type
->write_buffer
= target_write_buffer_default
;
1300 if (target
->type
->get_gdb_fileio_info
== NULL
)
1301 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1303 if (target
->type
->gdb_fileio_end
== NULL
)
1304 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1306 if (target
->type
->profiling
== NULL
)
1307 target
->type
->profiling
= target_profiling_default
;
1312 static int target_init(struct command_context
*cmd_ctx
)
1314 struct target
*target
;
1317 for (target
= all_targets
; target
; target
= target
->next
) {
1318 retval
= target_init_one(cmd_ctx
, target
);
1319 if (ERROR_OK
!= retval
)
1326 retval
= target_register_user_commands(cmd_ctx
);
1327 if (ERROR_OK
!= retval
)
1330 retval
= target_register_timer_callback(&handle_target
,
1331 polling_interval
, 1, cmd_ctx
->interp
);
1332 if (ERROR_OK
!= retval
)
1338 COMMAND_HANDLER(handle_target_init_command
)
1343 return ERROR_COMMAND_SYNTAX_ERROR
;
1345 static bool target_initialized
;
1346 if (target_initialized
) {
1347 LOG_INFO("'target init' has already been called");
1350 target_initialized
= true;
1352 retval
= command_run_line(CMD_CTX
, "init_targets");
1353 if (ERROR_OK
!= retval
)
1356 retval
= command_run_line(CMD_CTX
, "init_target_events");
1357 if (ERROR_OK
!= retval
)
1360 retval
= command_run_line(CMD_CTX
, "init_board");
1361 if (ERROR_OK
!= retval
)
1364 LOG_DEBUG("Initializing targets...");
1365 return target_init(CMD_CTX
);
1368 int target_register_event_callback(int (*callback
)(struct target
*target
,
1369 enum target_event event
, void *priv
), void *priv
)
1371 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1373 if (callback
== NULL
)
1374 return ERROR_COMMAND_SYNTAX_ERROR
;
1377 while ((*callbacks_p
)->next
)
1378 callbacks_p
= &((*callbacks_p
)->next
);
1379 callbacks_p
= &((*callbacks_p
)->next
);
1382 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1383 (*callbacks_p
)->callback
= callback
;
1384 (*callbacks_p
)->priv
= priv
;
1385 (*callbacks_p
)->next
= NULL
;
1390 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1391 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1393 struct target_reset_callback
*entry
;
1395 if (callback
== NULL
)
1396 return ERROR_COMMAND_SYNTAX_ERROR
;
1398 entry
= malloc(sizeof(struct target_reset_callback
));
1399 if (entry
== NULL
) {
1400 LOG_ERROR("error allocating buffer for reset callback entry");
1401 return ERROR_COMMAND_SYNTAX_ERROR
;
1404 entry
->callback
= callback
;
1406 list_add(&entry
->list
, &target_reset_callback_list
);
1412 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1413 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1415 struct target_trace_callback
*entry
;
1417 if (callback
== NULL
)
1418 return ERROR_COMMAND_SYNTAX_ERROR
;
1420 entry
= malloc(sizeof(struct target_trace_callback
));
1421 if (entry
== NULL
) {
1422 LOG_ERROR("error allocating buffer for trace callback entry");
1423 return ERROR_COMMAND_SYNTAX_ERROR
;
1426 entry
->callback
= callback
;
1428 list_add(&entry
->list
, &target_trace_callback_list
);
1434 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1436 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1438 if (callback
== NULL
)
1439 return ERROR_COMMAND_SYNTAX_ERROR
;
1442 while ((*callbacks_p
)->next
)
1443 callbacks_p
= &((*callbacks_p
)->next
);
1444 callbacks_p
= &((*callbacks_p
)->next
);
1447 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1448 (*callbacks_p
)->callback
= callback
;
1449 (*callbacks_p
)->periodic
= periodic
;
1450 (*callbacks_p
)->time_ms
= time_ms
;
1451 (*callbacks_p
)->removed
= false;
1453 gettimeofday(&(*callbacks_p
)->when
, NULL
);
1454 timeval_add_time(&(*callbacks_p
)->when
, 0, time_ms
* 1000);
1456 (*callbacks_p
)->priv
= priv
;
1457 (*callbacks_p
)->next
= NULL
;
1462 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1463 enum target_event event
, void *priv
), void *priv
)
1465 struct target_event_callback
**p
= &target_event_callbacks
;
1466 struct target_event_callback
*c
= target_event_callbacks
;
1468 if (callback
== NULL
)
1469 return ERROR_COMMAND_SYNTAX_ERROR
;
1472 struct target_event_callback
*next
= c
->next
;
1473 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1485 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1486 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1488 struct target_reset_callback
*entry
;
1490 if (callback
== NULL
)
1491 return ERROR_COMMAND_SYNTAX_ERROR
;
1493 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1494 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1495 list_del(&entry
->list
);
1504 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1505 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1507 struct target_trace_callback
*entry
;
1509 if (callback
== NULL
)
1510 return ERROR_COMMAND_SYNTAX_ERROR
;
1512 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1513 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1514 list_del(&entry
->list
);
1523 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1525 if (callback
== NULL
)
1526 return ERROR_COMMAND_SYNTAX_ERROR
;
1528 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1530 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1539 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1541 struct target_event_callback
*callback
= target_event_callbacks
;
1542 struct target_event_callback
*next_callback
;
1544 if (event
== TARGET_EVENT_HALTED
) {
1545 /* execute early halted first */
1546 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1549 LOG_DEBUG("target event %i (%s)", event
,
1550 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1552 target_handle_event(target
, event
);
1555 next_callback
= callback
->next
;
1556 callback
->callback(target
, event
, callback
->priv
);
1557 callback
= next_callback
;
1563 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1565 struct target_reset_callback
*callback
;
1567 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1568 Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1570 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1571 callback
->callback(target
, reset_mode
, callback
->priv
);
1576 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1578 struct target_trace_callback
*callback
;
1580 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1581 callback
->callback(target
, len
, data
, callback
->priv
);
1586 static int target_timer_callback_periodic_restart(
1587 struct target_timer_callback
*cb
, struct timeval
*now
)
1590 timeval_add_time(&cb
->when
, 0, cb
->time_ms
* 1000L);
1594 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1595 struct timeval
*now
)
1597 cb
->callback(cb
->priv
);
1600 return target_timer_callback_periodic_restart(cb
, now
);
1602 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1605 static int target_call_timer_callbacks_check_time(int checktime
)
1607 static bool callback_processing
;
1609 /* Do not allow nesting */
1610 if (callback_processing
)
1613 callback_processing
= true;
1618 gettimeofday(&now
, NULL
);
1620 /* Store an address of the place containing a pointer to the
1621 * next item; initially, that's a standalone "root of the
1622 * list" variable. */
1623 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1625 if ((*callback
)->removed
) {
1626 struct target_timer_callback
*p
= *callback
;
1627 *callback
= (*callback
)->next
;
1632 bool call_it
= (*callback
)->callback
&&
1633 ((!checktime
&& (*callback
)->periodic
) ||
1634 timeval_compare(&now
, &(*callback
)->when
) >= 0);
1637 target_call_timer_callback(*callback
, &now
);
1639 callback
= &(*callback
)->next
;
1642 callback_processing
= false;
1646 int target_call_timer_callbacks(void)
1648 return target_call_timer_callbacks_check_time(1);
1651 /* invoke periodic callbacks immediately */
1652 int target_call_timer_callbacks_now(void)
1654 return target_call_timer_callbacks_check_time(0);
1657 /* Prints the working area layout for debug purposes */
1658 static void print_wa_layout(struct target
*target
)
1660 struct working_area
*c
= target
->working_areas
;
1663 LOG_DEBUG("%c%c " TARGET_ADDR_FMT
"-" TARGET_ADDR_FMT
" (%" PRIu32
" bytes)",
1664 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1665 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1670 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1671 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1673 assert(area
->free
); /* Shouldn't split an allocated area */
1674 assert(size
<= area
->size
); /* Caller should guarantee this */
1676 /* Split only if not already the right size */
1677 if (size
< area
->size
) {
1678 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1683 new_wa
->next
= area
->next
;
1684 new_wa
->size
= area
->size
- size
;
1685 new_wa
->address
= area
->address
+ size
;
1686 new_wa
->backup
= NULL
;
1687 new_wa
->user
= NULL
;
1688 new_wa
->free
= true;
1690 area
->next
= new_wa
;
1693 /* If backup memory was allocated to this area, it has the wrong size
1694 * now so free it and it will be reallocated if/when needed */
1697 area
->backup
= NULL
;
1702 /* Merge all adjacent free areas into one */
1703 static void target_merge_working_areas(struct target
*target
)
1705 struct working_area
*c
= target
->working_areas
;
1707 while (c
&& c
->next
) {
1708 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1710 /* Find two adjacent free areas */
1711 if (c
->free
&& c
->next
->free
) {
1712 /* Merge the last into the first */
1713 c
->size
+= c
->next
->size
;
1715 /* Remove the last */
1716 struct working_area
*to_be_freed
= c
->next
;
1717 c
->next
= c
->next
->next
;
1718 if (to_be_freed
->backup
)
1719 free(to_be_freed
->backup
);
1722 /* If backup memory was allocated to the remaining area, it's has
1723 * the wrong size now */
1734 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1736 /* Reevaluate working area address based on MMU state*/
1737 if (target
->working_areas
== NULL
) {
1741 retval
= target
->type
->mmu(target
, &enabled
);
1742 if (retval
!= ERROR_OK
)
1746 if (target
->working_area_phys_spec
) {
1747 LOG_DEBUG("MMU disabled, using physical "
1748 "address for working memory " TARGET_ADDR_FMT
,
1749 target
->working_area_phys
);
1750 target
->working_area
= target
->working_area_phys
;
1752 LOG_ERROR("No working memory available. "
1753 "Specify -work-area-phys to target.");
1754 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1757 if (target
->working_area_virt_spec
) {
1758 LOG_DEBUG("MMU enabled, using virtual "
1759 "address for working memory " TARGET_ADDR_FMT
,
1760 target
->working_area_virt
);
1761 target
->working_area
= target
->working_area_virt
;
1763 LOG_ERROR("No working memory available. "
1764 "Specify -work-area-virt to target.");
1765 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1769 /* Set up initial working area on first call */
1770 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1772 new_wa
->next
= NULL
;
1773 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1774 new_wa
->address
= target
->working_area
;
1775 new_wa
->backup
= NULL
;
1776 new_wa
->user
= NULL
;
1777 new_wa
->free
= true;
1780 target
->working_areas
= new_wa
;
1783 /* only allocate multiples of 4 byte */
1785 size
= (size
+ 3) & (~3UL);
1787 struct working_area
*c
= target
->working_areas
;
1789 /* Find the first large enough working area */
1791 if (c
->free
&& c
->size
>= size
)
1797 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1799 /* Split the working area into the requested size */
1800 target_split_working_area(c
, size
);
1802 LOG_DEBUG("allocated new working area of %" PRIu32
" bytes at address " TARGET_ADDR_FMT
,
1805 if (target
->backup_working_area
) {
1806 if (c
->backup
== NULL
) {
1807 c
->backup
= malloc(c
->size
);
1808 if (c
->backup
== NULL
)
1812 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1813 if (retval
!= ERROR_OK
)
1817 /* mark as used, and return the new (reused) area */
1824 print_wa_layout(target
);
1829 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1833 retval
= target_alloc_working_area_try(target
, size
, area
);
1834 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1835 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1840 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1842 int retval
= ERROR_OK
;
1844 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1845 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1846 if (retval
!= ERROR_OK
)
1847 LOG_ERROR("failed to restore %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1848 area
->size
, area
->address
);
1854 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1855 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1857 int retval
= ERROR_OK
;
1863 retval
= target_restore_working_area(target
, area
);
1864 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1865 if (retval
!= ERROR_OK
)
1871 LOG_DEBUG("freed %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1872 area
->size
, area
->address
);
1874 /* mark user pointer invalid */
1875 /* TODO: Is this really safe? It points to some previous caller's memory.
1876 * How could we know that the area pointer is still in that place and not
1877 * some other vital data? What's the purpose of this, anyway? */
1881 target_merge_working_areas(target
);
1883 print_wa_layout(target
);
1888 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1890 return target_free_working_area_restore(target
, area
, 1);
1893 static void target_destroy(struct target
*target
)
1895 if (target
->type
->deinit_target
)
1896 target
->type
->deinit_target(target
);
1898 if (target
->semihosting
)
1899 free(target
->semihosting
);
1901 jtag_unregister_event_callback(jtag_enable_callback
, target
);
1903 struct target_event_action
*teap
= target
->event_action
;
1905 struct target_event_action
*next
= teap
->next
;
1906 Jim_DecrRefCount(teap
->interp
, teap
->body
);
1911 target_free_all_working_areas(target
);
1912 /* Now we have none or only one working area marked as free */
1913 if (target
->working_areas
) {
1914 free(target
->working_areas
->backup
);
1915 free(target
->working_areas
);
1918 /* release the targets SMP list */
1920 struct target_list
*head
= target
->head
;
1921 while (head
!= NULL
) {
1922 struct target_list
*pos
= head
->next
;
1923 head
->target
->smp
= 0;
1930 free(target
->gdb_port_override
);
1932 free(target
->trace_info
);
1933 free(target
->fileio_info
);
1934 free(target
->cmd_name
);
1938 void target_quit(void)
1940 struct target_event_callback
*pe
= target_event_callbacks
;
1942 struct target_event_callback
*t
= pe
->next
;
1946 target_event_callbacks
= NULL
;
1948 struct target_timer_callback
*pt
= target_timer_callbacks
;
1950 struct target_timer_callback
*t
= pt
->next
;
1954 target_timer_callbacks
= NULL
;
1956 for (struct target
*target
= all_targets
; target
;) {
1960 target_destroy(target
);
1967 /* free resources and restore memory, if restoring memory fails,
1968 * free up resources anyway
1970 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1972 struct working_area
*c
= target
->working_areas
;
1974 LOG_DEBUG("freeing all working areas");
1976 /* Loop through all areas, restoring the allocated ones and marking them as free */
1980 target_restore_working_area(target
, c
);
1982 *c
->user
= NULL
; /* Same as above */
1988 /* Run a merge pass to combine all areas into one */
1989 target_merge_working_areas(target
);
1991 print_wa_layout(target
);
1994 void target_free_all_working_areas(struct target
*target
)
1996 target_free_all_working_areas_restore(target
, 1);
1999 /* Find the largest number of bytes that can be allocated */
2000 uint32_t target_get_working_area_avail(struct target
*target
)
2002 struct working_area
*c
= target
->working_areas
;
2003 uint32_t max_size
= 0;
2006 return target
->working_area_size
;
2009 if (c
->free
&& max_size
< c
->size
)
2018 int target_arch_state(struct target
*target
)
2021 if (target
== NULL
) {
2022 LOG_WARNING("No target has been configured");
2026 if (target
->state
!= TARGET_HALTED
)
2029 retval
= target
->type
->arch_state(target
);
2033 static int target_get_gdb_fileio_info_default(struct target
*target
,
2034 struct gdb_fileio_info
*fileio_info
)
2036 /* If target does not support semi-hosting function, target
2037 has no need to provide .get_gdb_fileio_info callback.
2038 It just return ERROR_FAIL and gdb_server will return "Txx"
2039 as target halted every time. */
2043 static int target_gdb_fileio_end_default(struct target
*target
,
2044 int retcode
, int fileio_errno
, bool ctrl_c
)
2049 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
2050 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
2052 struct timeval timeout
, now
;
2054 gettimeofday(&timeout
, NULL
);
2055 timeval_add_time(&timeout
, seconds
, 0);
2057 LOG_INFO("Starting profiling. Halting and resuming the"
2058 " target as often as we can...");
2060 uint32_t sample_count
= 0;
2061 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2062 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2064 int retval
= ERROR_OK
;
2066 target_poll(target
);
2067 if (target
->state
== TARGET_HALTED
) {
2068 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
2069 samples
[sample_count
++] = t
;
2070 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2071 retval
= target_resume(target
, 1, 0, 0, 0);
2072 target_poll(target
);
2073 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2074 } else if (target
->state
== TARGET_RUNNING
) {
2075 /* We want to quickly sample the PC. */
2076 retval
= target_halt(target
);
2078 LOG_INFO("Target not halted or running");
2083 if (retval
!= ERROR_OK
)
2086 gettimeofday(&now
, NULL
);
2087 if ((sample_count
>= max_num_samples
) || timeval_compare(&now
, &timeout
) >= 0) {
2088 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2093 *num_samples
= sample_count
;
2097 /* Single aligned words are guaranteed to use 16 or 32 bit access
2098 * mode respectively, otherwise data is handled as quickly as
2101 int target_write_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, const uint8_t *buffer
)
2103 LOG_DEBUG("writing buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2106 if (!target_was_examined(target
)) {
2107 LOG_ERROR("Target not examined yet");
2114 if ((address
+ size
- 1) < address
) {
2115 /* GDB can request this when e.g. PC is 0xfffffffc */
2116 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2122 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2125 static int target_write_buffer_default(struct target
*target
,
2126 target_addr_t address
, uint32_t count
, const uint8_t *buffer
)
2130 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2131 * will have something to do with the size we leave to it. */
2132 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2133 if (address
& size
) {
2134 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2135 if (retval
!= ERROR_OK
)
2143 /* Write the data with as large access size as possible. */
2144 for (; size
> 0; size
/= 2) {
2145 uint32_t aligned
= count
- count
% size
;
2147 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2148 if (retval
!= ERROR_OK
)
2159 /* Single aligned words are guaranteed to use 16 or 32 bit access
2160 * mode respectively, otherwise data is handled as quickly as
2163 int target_read_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
2165 LOG_DEBUG("reading buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2168 if (!target_was_examined(target
)) {
2169 LOG_ERROR("Target not examined yet");
2176 if ((address
+ size
- 1) < address
) {
2177 /* GDB can request this when e.g. PC is 0xfffffffc */
2178 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2184 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2187 static int target_read_buffer_default(struct target
*target
, target_addr_t address
, uint32_t count
, uint8_t *buffer
)
2191 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2192 * will have something to do with the size we leave to it. */
2193 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2194 if (address
& size
) {
2195 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2196 if (retval
!= ERROR_OK
)
2204 /* Read the data with as large access size as possible. */
2205 for (; size
> 0; size
/= 2) {
2206 uint32_t aligned
= count
- count
% size
;
2208 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2209 if (retval
!= ERROR_OK
)
2220 int target_checksum_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* crc
)
2225 uint32_t checksum
= 0;
2226 if (!target_was_examined(target
)) {
2227 LOG_ERROR("Target not examined yet");
2231 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2232 if (retval
!= ERROR_OK
) {
2233 buffer
= malloc(size
);
2234 if (buffer
== NULL
) {
2235 LOG_ERROR("error allocating buffer for section (%" PRId32
" bytes)", size
);
2236 return ERROR_COMMAND_SYNTAX_ERROR
;
2238 retval
= target_read_buffer(target
, address
, size
, buffer
);
2239 if (retval
!= ERROR_OK
) {
2244 /* convert to target endianness */
2245 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2246 uint32_t target_data
;
2247 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2248 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2251 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2260 int target_blank_check_memory(struct target
*target
,
2261 struct target_memory_check_block
*blocks
, int num_blocks
,
2262 uint8_t erased_value
)
2264 if (!target_was_examined(target
)) {
2265 LOG_ERROR("Target not examined yet");
2269 if (target
->type
->blank_check_memory
== NULL
)
2270 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2272 return target
->type
->blank_check_memory(target
, blocks
, num_blocks
, erased_value
);
2275 int target_read_u64(struct target
*target
, target_addr_t address
, uint64_t *value
)
2277 uint8_t value_buf
[8];
2278 if (!target_was_examined(target
)) {
2279 LOG_ERROR("Target not examined yet");
2283 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2285 if (retval
== ERROR_OK
) {
2286 *value
= target_buffer_get_u64(target
, value_buf
);
2287 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2292 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2299 int target_read_u32(struct target
*target
, target_addr_t address
, uint32_t *value
)
2301 uint8_t value_buf
[4];
2302 if (!target_was_examined(target
)) {
2303 LOG_ERROR("Target not examined yet");
2307 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2309 if (retval
== ERROR_OK
) {
2310 *value
= target_buffer_get_u32(target
, value_buf
);
2311 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2316 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2323 int target_read_u16(struct target
*target
, target_addr_t address
, uint16_t *value
)
2325 uint8_t value_buf
[2];
2326 if (!target_was_examined(target
)) {
2327 LOG_ERROR("Target not examined yet");
2331 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2333 if (retval
== ERROR_OK
) {
2334 *value
= target_buffer_get_u16(target
, value_buf
);
2335 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%4.4" PRIx16
,
2340 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2347 int target_read_u8(struct target
*target
, target_addr_t address
, uint8_t *value
)
2349 if (!target_was_examined(target
)) {
2350 LOG_ERROR("Target not examined yet");
2354 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2356 if (retval
== ERROR_OK
) {
2357 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2362 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2369 int target_write_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2372 uint8_t value_buf
[8];
2373 if (!target_was_examined(target
)) {
2374 LOG_ERROR("Target not examined yet");
2378 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2382 target_buffer_set_u64(target
, value_buf
, value
);
2383 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2384 if (retval
!= ERROR_OK
)
2385 LOG_DEBUG("failed: %i", retval
);
2390 int target_write_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2393 uint8_t value_buf
[4];
2394 if (!target_was_examined(target
)) {
2395 LOG_ERROR("Target not examined yet");
2399 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2403 target_buffer_set_u32(target
, value_buf
, value
);
2404 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2405 if (retval
!= ERROR_OK
)
2406 LOG_DEBUG("failed: %i", retval
);
2411 int target_write_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2414 uint8_t value_buf
[2];
2415 if (!target_was_examined(target
)) {
2416 LOG_ERROR("Target not examined yet");
2420 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2424 target_buffer_set_u16(target
, value_buf
, value
);
2425 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2426 if (retval
!= ERROR_OK
)
2427 LOG_DEBUG("failed: %i", retval
);
2432 int target_write_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2435 if (!target_was_examined(target
)) {
2436 LOG_ERROR("Target not examined yet");
2440 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2443 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2444 if (retval
!= ERROR_OK
)
2445 LOG_DEBUG("failed: %i", retval
);
2450 int target_write_phys_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2453 uint8_t value_buf
[8];
2454 if (!target_was_examined(target
)) {
2455 LOG_ERROR("Target not examined yet");
2459 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2463 target_buffer_set_u64(target
, value_buf
, value
);
2464 retval
= target_write_phys_memory(target
, address
, 8, 1, value_buf
);
2465 if (retval
!= ERROR_OK
)
2466 LOG_DEBUG("failed: %i", retval
);
2471 int target_write_phys_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2474 uint8_t value_buf
[4];
2475 if (!target_was_examined(target
)) {
2476 LOG_ERROR("Target not examined yet");
2480 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2484 target_buffer_set_u32(target
, value_buf
, value
);
2485 retval
= target_write_phys_memory(target
, address
, 4, 1, value_buf
);
2486 if (retval
!= ERROR_OK
)
2487 LOG_DEBUG("failed: %i", retval
);
2492 int target_write_phys_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2495 uint8_t value_buf
[2];
2496 if (!target_was_examined(target
)) {
2497 LOG_ERROR("Target not examined yet");
2501 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2505 target_buffer_set_u16(target
, value_buf
, value
);
2506 retval
= target_write_phys_memory(target
, address
, 2, 1, value_buf
);
2507 if (retval
!= ERROR_OK
)
2508 LOG_DEBUG("failed: %i", retval
);
2513 int target_write_phys_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2516 if (!target_was_examined(target
)) {
2517 LOG_ERROR("Target not examined yet");
2521 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2524 retval
= target_write_phys_memory(target
, address
, 1, 1, &value
);
2525 if (retval
!= ERROR_OK
)
2526 LOG_DEBUG("failed: %i", retval
);
2531 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2533 struct target
*target
= get_target(name
);
2534 if (target
== NULL
) {
2535 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2538 if (!target
->tap
->enabled
) {
2539 LOG_USER("Target: TAP %s is disabled, "
2540 "can't be the current target\n",
2541 target
->tap
->dotted_name
);
2545 cmd_ctx
->current_target
= target
;
2546 if (cmd_ctx
->current_target_override
)
2547 cmd_ctx
->current_target_override
= target
;
2553 COMMAND_HANDLER(handle_targets_command
)
2555 int retval
= ERROR_OK
;
2556 if (CMD_ARGC
== 1) {
2557 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2558 if (retval
== ERROR_OK
) {
2564 struct target
*target
= all_targets
;
2565 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2566 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2571 if (target
->tap
->enabled
)
2572 state
= target_state_name(target
);
2574 state
= "tap-disabled";
2576 if (CMD_CTX
->current_target
== target
)
2579 /* keep columns lined up to match the headers above */
2580 command_print(CMD_CTX
,
2581 "%2d%c %-18s %-10s %-6s %-18s %s",
2582 target
->target_number
,
2584 target_name(target
),
2585 target_type_name(target
),
2586 Jim_Nvp_value2name_simple(nvp_target_endian
,
2587 target
->endianness
)->name
,
2588 target
->tap
->dotted_name
,
2590 target
= target
->next
;
2596 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2598 static int powerDropout
;
2599 static int srstAsserted
;
2601 static int runPowerRestore
;
2602 static int runPowerDropout
;
2603 static int runSrstAsserted
;
2604 static int runSrstDeasserted
;
2606 static int sense_handler(void)
2608 static int prevSrstAsserted
;
2609 static int prevPowerdropout
;
2611 int retval
= jtag_power_dropout(&powerDropout
);
2612 if (retval
!= ERROR_OK
)
2616 powerRestored
= prevPowerdropout
&& !powerDropout
;
2618 runPowerRestore
= 1;
2620 int64_t current
= timeval_ms();
2621 static int64_t lastPower
;
2622 bool waitMore
= lastPower
+ 2000 > current
;
2623 if (powerDropout
&& !waitMore
) {
2624 runPowerDropout
= 1;
2625 lastPower
= current
;
2628 retval
= jtag_srst_asserted(&srstAsserted
);
2629 if (retval
!= ERROR_OK
)
2633 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2635 static int64_t lastSrst
;
2636 waitMore
= lastSrst
+ 2000 > current
;
2637 if (srstDeasserted
&& !waitMore
) {
2638 runSrstDeasserted
= 1;
2642 if (!prevSrstAsserted
&& srstAsserted
)
2643 runSrstAsserted
= 1;
2645 prevSrstAsserted
= srstAsserted
;
2646 prevPowerdropout
= powerDropout
;
2648 if (srstDeasserted
|| powerRestored
) {
2649 /* Other than logging the event we can't do anything here.
2650 * Issuing a reset is a particularly bad idea as we might
2651 * be inside a reset already.
2658 /* process target state changes */
2659 static int handle_target(void *priv
)
2661 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2662 int retval
= ERROR_OK
;
2664 if (!is_jtag_poll_safe()) {
2665 /* polling is disabled currently */
2669 /* we do not want to recurse here... */
2670 static int recursive
;
2674 /* danger! running these procedures can trigger srst assertions and power dropouts.
2675 * We need to avoid an infinite loop/recursion here and we do that by
2676 * clearing the flags after running these events.
2678 int did_something
= 0;
2679 if (runSrstAsserted
) {
2680 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2681 Jim_Eval(interp
, "srst_asserted");
2684 if (runSrstDeasserted
) {
2685 Jim_Eval(interp
, "srst_deasserted");
2688 if (runPowerDropout
) {
2689 LOG_INFO("Power dropout detected, running power_dropout proc.");
2690 Jim_Eval(interp
, "power_dropout");
2693 if (runPowerRestore
) {
2694 Jim_Eval(interp
, "power_restore");
2698 if (did_something
) {
2699 /* clear detect flags */
2703 /* clear action flags */
2705 runSrstAsserted
= 0;
2706 runSrstDeasserted
= 0;
2707 runPowerRestore
= 0;
2708 runPowerDropout
= 0;
2713 /* Poll targets for state changes unless that's globally disabled.
2714 * Skip targets that are currently disabled.
2716 for (struct target
*target
= all_targets
;
2717 is_jtag_poll_safe() && target
;
2718 target
= target
->next
) {
2720 if (!target_was_examined(target
))
2723 if (!target
->tap
->enabled
)
2726 if (target
->backoff
.times
> target
->backoff
.count
) {
2727 /* do not poll this time as we failed previously */
2728 target
->backoff
.count
++;
2731 target
->backoff
.count
= 0;
2733 /* only poll target if we've got power and srst isn't asserted */
2734 if (!powerDropout
&& !srstAsserted
) {
2735 /* polling may fail silently until the target has been examined */
2736 retval
= target_poll(target
);
2737 if (retval
!= ERROR_OK
) {
2738 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2739 if (target
->backoff
.times
* polling_interval
< 5000) {
2740 target
->backoff
.times
*= 2;
2741 target
->backoff
.times
++;
2744 /* Tell GDB to halt the debugger. This allows the user to
2745 * run monitor commands to handle the situation.
2747 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2749 if (target
->backoff
.times
> 0) {
2750 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
2751 target_reset_examined(target
);
2752 retval
= target_examine_one(target
);
2753 /* Target examination could have failed due to unstable connection,
2754 * but we set the examined flag anyway to repoll it later */
2755 if (retval
!= ERROR_OK
) {
2756 target
->examined
= true;
2757 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2758 target
->backoff
.times
* polling_interval
);
2763 /* Since we succeeded, we reset backoff count */
2764 target
->backoff
.times
= 0;
2771 COMMAND_HANDLER(handle_reg_command
)
2773 struct target
*target
;
2774 struct reg
*reg
= NULL
;
2780 target
= get_current_target(CMD_CTX
);
2782 /* list all available registers for the current target */
2783 if (CMD_ARGC
== 0) {
2784 struct reg_cache
*cache
= target
->reg_cache
;
2790 command_print(CMD_CTX
, "===== %s", cache
->name
);
2792 for (i
= 0, reg
= cache
->reg_list
;
2793 i
< cache
->num_regs
;
2794 i
++, reg
++, count
++) {
2795 /* only print cached values if they are valid */
2797 value
= buf_to_str(reg
->value
,
2799 command_print(CMD_CTX
,
2800 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2808 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2813 cache
= cache
->next
;
2819 /* access a single register by its ordinal number */
2820 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2822 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2824 struct reg_cache
*cache
= target
->reg_cache
;
2828 for (i
= 0; i
< cache
->num_regs
; i
++) {
2829 if (count
++ == num
) {
2830 reg
= &cache
->reg_list
[i
];
2836 cache
= cache
->next
;
2840 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2841 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2845 /* access a single register by its name */
2846 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2849 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2854 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2856 /* display a register */
2857 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2858 && (CMD_ARGV
[1][0] <= '9')))) {
2859 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2862 if (reg
->valid
== 0)
2863 reg
->type
->get(reg
);
2864 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2865 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2870 /* set register value */
2871 if (CMD_ARGC
== 2) {
2872 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2875 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2877 reg
->type
->set(reg
, buf
);
2879 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2880 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2888 return ERROR_COMMAND_SYNTAX_ERROR
;
2891 COMMAND_HANDLER(handle_poll_command
)
2893 int retval
= ERROR_OK
;
2894 struct target
*target
= get_current_target(CMD_CTX
);
2896 if (CMD_ARGC
== 0) {
2897 command_print(CMD_CTX
, "background polling: %s",
2898 jtag_poll_get_enabled() ? "on" : "off");
2899 command_print(CMD_CTX
, "TAP: %s (%s)",
2900 target
->tap
->dotted_name
,
2901 target
->tap
->enabled
? "enabled" : "disabled");
2902 if (!target
->tap
->enabled
)
2904 retval
= target_poll(target
);
2905 if (retval
!= ERROR_OK
)
2907 retval
= target_arch_state(target
);
2908 if (retval
!= ERROR_OK
)
2910 } else if (CMD_ARGC
== 1) {
2912 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2913 jtag_poll_set_enabled(enable
);
2915 return ERROR_COMMAND_SYNTAX_ERROR
;
2920 COMMAND_HANDLER(handle_wait_halt_command
)
2923 return ERROR_COMMAND_SYNTAX_ERROR
;
2925 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2926 if (1 == CMD_ARGC
) {
2927 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2928 if (ERROR_OK
!= retval
)
2929 return ERROR_COMMAND_SYNTAX_ERROR
;
2932 struct target
*target
= get_current_target(CMD_CTX
);
2933 return target_wait_state(target
, TARGET_HALTED
, ms
);
2936 /* wait for target state to change. The trick here is to have a low
2937 * latency for short waits and not to suck up all the CPU time
2940 * After 500ms, keep_alive() is invoked
2942 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2945 int64_t then
= 0, cur
;
2949 retval
= target_poll(target
);
2950 if (retval
!= ERROR_OK
)
2952 if (target
->state
== state
)
2957 then
= timeval_ms();
2958 LOG_DEBUG("waiting for target %s...",
2959 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2965 if ((cur
-then
) > ms
) {
2966 LOG_ERROR("timed out while waiting for target %s",
2967 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2975 COMMAND_HANDLER(handle_halt_command
)
2979 struct target
*target
= get_current_target(CMD_CTX
);
2981 target
->verbose_halt_msg
= true;
2983 int retval
= target_halt(target
);
2984 if (ERROR_OK
!= retval
)
2987 if (CMD_ARGC
== 1) {
2988 unsigned wait_local
;
2989 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2990 if (ERROR_OK
!= retval
)
2991 return ERROR_COMMAND_SYNTAX_ERROR
;
2996 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2999 COMMAND_HANDLER(handle_soft_reset_halt_command
)
3001 struct target
*target
= get_current_target(CMD_CTX
);
3003 LOG_USER("requesting target halt and executing a soft reset");
3005 target_soft_reset_halt(target
);
3010 COMMAND_HANDLER(handle_reset_command
)
3013 return ERROR_COMMAND_SYNTAX_ERROR
;
3015 enum target_reset_mode reset_mode
= RESET_RUN
;
3016 if (CMD_ARGC
== 1) {
3018 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
3019 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
3020 return ERROR_COMMAND_SYNTAX_ERROR
;
3021 reset_mode
= n
->value
;
3024 /* reset *all* targets */
3025 return target_process_reset(CMD_CTX
, reset_mode
);
3029 COMMAND_HANDLER(handle_resume_command
)
3033 return ERROR_COMMAND_SYNTAX_ERROR
;
3035 struct target
*target
= get_current_target(CMD_CTX
);
3037 /* with no CMD_ARGV, resume from current pc, addr = 0,
3038 * with one arguments, addr = CMD_ARGV[0],
3039 * handle breakpoints, not debugging */
3040 target_addr_t addr
= 0;
3041 if (CMD_ARGC
== 1) {
3042 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3046 return target_resume(target
, current
, addr
, 1, 0);
3049 COMMAND_HANDLER(handle_step_command
)
3052 return ERROR_COMMAND_SYNTAX_ERROR
;
3056 /* with no CMD_ARGV, step from current pc, addr = 0,
3057 * with one argument addr = CMD_ARGV[0],
3058 * handle breakpoints, debugging */
3059 target_addr_t addr
= 0;
3061 if (CMD_ARGC
== 1) {
3062 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3066 struct target
*target
= get_current_target(CMD_CTX
);
3068 return target
->type
->step(target
, current_pc
, addr
, 1);
3071 static void handle_md_output(struct command_context
*cmd_ctx
,
3072 struct target
*target
, target_addr_t address
, unsigned size
,
3073 unsigned count
, const uint8_t *buffer
)
3075 const unsigned line_bytecnt
= 32;
3076 unsigned line_modulo
= line_bytecnt
/ size
;
3078 char output
[line_bytecnt
* 4 + 1];
3079 unsigned output_len
= 0;
3081 const char *value_fmt
;
3084 value_fmt
= "%16.16"PRIx64
" ";
3087 value_fmt
= "%8.8"PRIx64
" ";
3090 value_fmt
= "%4.4"PRIx64
" ";
3093 value_fmt
= "%2.2"PRIx64
" ";
3096 /* "can't happen", caller checked */
3097 LOG_ERROR("invalid memory read size: %u", size
);
3101 for (unsigned i
= 0; i
< count
; i
++) {
3102 if (i
% line_modulo
== 0) {
3103 output_len
+= snprintf(output
+ output_len
,
3104 sizeof(output
) - output_len
,
3105 TARGET_ADDR_FMT
": ",
3106 (address
+ (i
* size
)));
3110 const uint8_t *value_ptr
= buffer
+ i
* size
;
3113 value
= target_buffer_get_u64(target
, value_ptr
);
3116 value
= target_buffer_get_u32(target
, value_ptr
);
3119 value
= target_buffer_get_u16(target
, value_ptr
);
3124 output_len
+= snprintf(output
+ output_len
,
3125 sizeof(output
) - output_len
,
3128 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
3129 command_print(cmd_ctx
, "%s", output
);
3135 COMMAND_HANDLER(handle_md_command
)
3138 return ERROR_COMMAND_SYNTAX_ERROR
;
3141 switch (CMD_NAME
[2]) {
3155 return ERROR_COMMAND_SYNTAX_ERROR
;
3158 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3159 int (*fn
)(struct target
*target
,
3160 target_addr_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
3164 fn
= target_read_phys_memory
;
3166 fn
= target_read_memory
;
3167 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
3168 return ERROR_COMMAND_SYNTAX_ERROR
;
3170 target_addr_t address
;
3171 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3175 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
3177 uint8_t *buffer
= calloc(count
, size
);
3178 if (buffer
== NULL
) {
3179 LOG_ERROR("Failed to allocate md read buffer");
3183 struct target
*target
= get_current_target(CMD_CTX
);
3184 int retval
= fn(target
, address
, size
, count
, buffer
);
3185 if (ERROR_OK
== retval
)
3186 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
3193 typedef int (*target_write_fn
)(struct target
*target
,
3194 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
3196 static int target_fill_mem(struct target
*target
,
3197 target_addr_t address
,
3205 /* We have to write in reasonably large chunks to be able
3206 * to fill large memory areas with any sane speed */
3207 const unsigned chunk_size
= 16384;
3208 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
3209 if (target_buf
== NULL
) {
3210 LOG_ERROR("Out of memory");
3214 for (unsigned i
= 0; i
< chunk_size
; i
++) {
3215 switch (data_size
) {
3217 target_buffer_set_u64(target
, target_buf
+ i
* data_size
, b
);
3220 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
3223 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
3226 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
3233 int retval
= ERROR_OK
;
3235 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
3238 if (current
> chunk_size
)
3239 current
= chunk_size
;
3240 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
3241 if (retval
!= ERROR_OK
)
3243 /* avoid GDB timeouts */
3252 COMMAND_HANDLER(handle_mw_command
)
3255 return ERROR_COMMAND_SYNTAX_ERROR
;
3256 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3261 fn
= target_write_phys_memory
;
3263 fn
= target_write_memory
;
3264 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
3265 return ERROR_COMMAND_SYNTAX_ERROR
;
3267 target_addr_t address
;
3268 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3270 target_addr_t value
;
3271 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], value
);
3275 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
3277 struct target
*target
= get_current_target(CMD_CTX
);
3279 switch (CMD_NAME
[2]) {
3293 return ERROR_COMMAND_SYNTAX_ERROR
;
3296 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
3299 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
3300 target_addr_t
*min_address
, target_addr_t
*max_address
)
3302 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
3303 return ERROR_COMMAND_SYNTAX_ERROR
;
3305 /* a base address isn't always necessary,
3306 * default to 0x0 (i.e. don't relocate) */
3307 if (CMD_ARGC
>= 2) {
3309 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3310 image
->base_address
= addr
;
3311 image
->base_address_set
= 1;
3313 image
->base_address_set
= 0;
3315 image
->start_address_set
= 0;
3318 COMMAND_PARSE_ADDRESS(CMD_ARGV
[3], *min_address
);
3319 if (CMD_ARGC
== 5) {
3320 COMMAND_PARSE_ADDRESS(CMD_ARGV
[4], *max_address
);
3321 /* use size (given) to find max (required) */
3322 *max_address
+= *min_address
;
3325 if (*min_address
> *max_address
)
3326 return ERROR_COMMAND_SYNTAX_ERROR
;
3331 COMMAND_HANDLER(handle_load_image_command
)
3335 uint32_t image_size
;
3336 target_addr_t min_address
= 0;
3337 target_addr_t max_address
= -1;
3341 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
3342 &image
, &min_address
, &max_address
);
3343 if (ERROR_OK
!= retval
)
3346 struct target
*target
= get_current_target(CMD_CTX
);
3348 struct duration bench
;
3349 duration_start(&bench
);
3351 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
3356 for (i
= 0; i
< image
.num_sections
; i
++) {
3357 buffer
= malloc(image
.sections
[i
].size
);
3358 if (buffer
== NULL
) {
3359 command_print(CMD_CTX
,
3360 "error allocating buffer for section (%d bytes)",
3361 (int)(image
.sections
[i
].size
));
3362 retval
= ERROR_FAIL
;
3366 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3367 if (retval
!= ERROR_OK
) {
3372 uint32_t offset
= 0;
3373 uint32_t length
= buf_cnt
;
3375 /* DANGER!!! beware of unsigned comparision here!!! */
3377 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3378 (image
.sections
[i
].base_address
< max_address
)) {
3380 if (image
.sections
[i
].base_address
< min_address
) {
3381 /* clip addresses below */
3382 offset
+= min_address
-image
.sections
[i
].base_address
;
3386 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3387 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3389 retval
= target_write_buffer(target
,
3390 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3391 if (retval
!= ERROR_OK
) {
3395 image_size
+= length
;
3396 command_print(CMD_CTX
, "%u bytes written at address " TARGET_ADDR_FMT
"",
3397 (unsigned int)length
,
3398 image
.sections
[i
].base_address
+ offset
);
3404 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3405 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
3406 "in %fs (%0.3f KiB/s)", image_size
,
3407 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3410 image_close(&image
);
3416 COMMAND_HANDLER(handle_dump_image_command
)
3418 struct fileio
*fileio
;
3420 int retval
, retvaltemp
;
3421 target_addr_t address
, size
;
3422 struct duration bench
;
3423 struct target
*target
= get_current_target(CMD_CTX
);
3426 return ERROR_COMMAND_SYNTAX_ERROR
;
3428 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], address
);
3429 COMMAND_PARSE_ADDRESS(CMD_ARGV
[2], size
);
3431 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
3432 buffer
= malloc(buf_size
);
3436 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3437 if (retval
!= ERROR_OK
) {
3442 duration_start(&bench
);
3445 size_t size_written
;
3446 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
3447 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
3448 if (retval
!= ERROR_OK
)
3451 retval
= fileio_write(fileio
, this_run_size
, buffer
, &size_written
);
3452 if (retval
!= ERROR_OK
)
3455 size
-= this_run_size
;
3456 address
+= this_run_size
;
3461 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3463 retval
= fileio_size(fileio
, &filesize
);
3464 if (retval
!= ERROR_OK
)
3466 command_print(CMD_CTX
,
3467 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize
,
3468 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3471 retvaltemp
= fileio_close(fileio
);
3472 if (retvaltemp
!= ERROR_OK
)
3481 IMAGE_CHECKSUM_ONLY
= 2
3484 static COMMAND_HELPER(handle_verify_image_command_internal
, enum verify_mode verify
)
3488 uint32_t image_size
;
3491 uint32_t checksum
= 0;
3492 uint32_t mem_checksum
= 0;
3496 struct target
*target
= get_current_target(CMD_CTX
);
3499 return ERROR_COMMAND_SYNTAX_ERROR
;
3502 LOG_ERROR("no target selected");
3506 struct duration bench
;
3507 duration_start(&bench
);
3509 if (CMD_ARGC
>= 2) {
3511 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3512 image
.base_address
= addr
;
3513 image
.base_address_set
= 1;
3515 image
.base_address_set
= 0;
3516 image
.base_address
= 0x0;
3519 image
.start_address_set
= 0;
3521 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3522 if (retval
!= ERROR_OK
)
3528 for (i
= 0; i
< image
.num_sections
; i
++) {
3529 buffer
= malloc(image
.sections
[i
].size
);
3530 if (buffer
== NULL
) {
3531 command_print(CMD_CTX
,
3532 "error allocating buffer for section (%d bytes)",
3533 (int)(image
.sections
[i
].size
));
3536 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3537 if (retval
!= ERROR_OK
) {
3542 if (verify
>= IMAGE_VERIFY
) {
3543 /* calculate checksum of image */
3544 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3545 if (retval
!= ERROR_OK
) {
3550 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3551 if (retval
!= ERROR_OK
) {
3555 if ((checksum
!= mem_checksum
) && (verify
== IMAGE_CHECKSUM_ONLY
)) {
3556 LOG_ERROR("checksum mismatch");
3558 retval
= ERROR_FAIL
;
3561 if (checksum
!= mem_checksum
) {
3562 /* failed crc checksum, fall back to a binary compare */
3566 LOG_ERROR("checksum mismatch - attempting binary compare");
3568 data
= malloc(buf_cnt
);
3570 /* Can we use 32bit word accesses? */
3572 int count
= buf_cnt
;
3573 if ((count
% 4) == 0) {
3577 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
3578 if (retval
== ERROR_OK
) {
3580 for (t
= 0; t
< buf_cnt
; t
++) {
3581 if (data
[t
] != buffer
[t
]) {
3582 command_print(CMD_CTX
,
3583 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3585 (unsigned)(t
+ image
.sections
[i
].base_address
),
3588 if (diffs
++ >= 127) {
3589 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
3601 command_print(CMD_CTX
, "address " TARGET_ADDR_FMT
" length 0x%08zx",
3602 image
.sections
[i
].base_address
,
3607 image_size
+= buf_cnt
;
3610 command_print(CMD_CTX
, "No more differences found.");
3613 retval
= ERROR_FAIL
;
3614 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3615 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3616 "in %fs (%0.3f KiB/s)", image_size
,
3617 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3620 image_close(&image
);
3625 COMMAND_HANDLER(handle_verify_image_checksum_command
)
3627 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_CHECKSUM_ONLY
);
3630 COMMAND_HANDLER(handle_verify_image_command
)
3632 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_VERIFY
);
3635 COMMAND_HANDLER(handle_test_image_command
)
3637 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_TEST
);
3640 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
3642 struct target
*target
= get_current_target(cmd_ctx
);
3643 struct breakpoint
*breakpoint
= target
->breakpoints
;
3644 while (breakpoint
) {
3645 if (breakpoint
->type
== BKPT_SOFT
) {
3646 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3647 breakpoint
->length
, 16);
3648 command_print(cmd_ctx
, "IVA breakpoint: " TARGET_ADDR_FMT
", 0x%x, %i, 0x%s",
3649 breakpoint
->address
,
3651 breakpoint
->set
, buf
);
3654 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3655 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3657 breakpoint
->length
, breakpoint
->set
);
3658 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3659 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3660 breakpoint
->address
,
3661 breakpoint
->length
, breakpoint
->set
);
3662 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3665 command_print(cmd_ctx
, "Breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3666 breakpoint
->address
,
3667 breakpoint
->length
, breakpoint
->set
);
3670 breakpoint
= breakpoint
->next
;
3675 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
3676 target_addr_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3678 struct target
*target
= get_current_target(cmd_ctx
);
3682 retval
= breakpoint_add(target
, addr
, length
, hw
);
3683 if (ERROR_OK
== retval
)
3684 command_print(cmd_ctx
, "breakpoint set at " TARGET_ADDR_FMT
"", addr
);
3686 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3689 } else if (addr
== 0) {
3690 if (target
->type
->add_context_breakpoint
== NULL
) {
3691 LOG_WARNING("Context breakpoint not available");
3694 retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3695 if (ERROR_OK
== retval
)
3696 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3698 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3702 if (target
->type
->add_hybrid_breakpoint
== NULL
) {
3703 LOG_WARNING("Hybrid breakpoint not available");
3706 retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3707 if (ERROR_OK
== retval
)
3708 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3710 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3717 COMMAND_HANDLER(handle_bp_command
)
3726 return handle_bp_command_list(CMD_CTX
);
3730 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3731 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3732 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3735 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
3737 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3738 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3740 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3741 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
3743 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
3744 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3746 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3751 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3752 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
3753 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
3754 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3757 return ERROR_COMMAND_SYNTAX_ERROR
;
3761 COMMAND_HANDLER(handle_rbp_command
)
3764 return ERROR_COMMAND_SYNTAX_ERROR
;
3767 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3769 struct target
*target
= get_current_target(CMD_CTX
);
3770 breakpoint_remove(target
, addr
);
3775 COMMAND_HANDLER(handle_wp_command
)
3777 struct target
*target
= get_current_target(CMD_CTX
);
3779 if (CMD_ARGC
== 0) {
3780 struct watchpoint
*watchpoint
= target
->watchpoints
;
3782 while (watchpoint
) {
3783 command_print(CMD_CTX
, "address: " TARGET_ADDR_FMT
3784 ", len: 0x%8.8" PRIx32
3785 ", r/w/a: %i, value: 0x%8.8" PRIx32
3786 ", mask: 0x%8.8" PRIx32
,
3787 watchpoint
->address
,
3789 (int)watchpoint
->rw
,
3792 watchpoint
= watchpoint
->next
;
3797 enum watchpoint_rw type
= WPT_ACCESS
;
3799 uint32_t length
= 0;
3800 uint32_t data_value
= 0x0;
3801 uint32_t data_mask
= 0xffffffff;
3805 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
3808 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3811 switch (CMD_ARGV
[2][0]) {
3822 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3823 return ERROR_COMMAND_SYNTAX_ERROR
;
3827 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3828 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3832 return ERROR_COMMAND_SYNTAX_ERROR
;
3835 int retval
= watchpoint_add(target
, addr
, length
, type
,
3836 data_value
, data_mask
);
3837 if (ERROR_OK
!= retval
)
3838 LOG_ERROR("Failure setting watchpoints");
3843 COMMAND_HANDLER(handle_rwp_command
)
3846 return ERROR_COMMAND_SYNTAX_ERROR
;
3849 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3851 struct target
*target
= get_current_target(CMD_CTX
);
3852 watchpoint_remove(target
, addr
);
3858 * Translate a virtual address to a physical address.
3860 * The low-level target implementation must have logged a detailed error
3861 * which is forwarded to telnet/GDB session.
3863 COMMAND_HANDLER(handle_virt2phys_command
)
3866 return ERROR_COMMAND_SYNTAX_ERROR
;
3869 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], va
);
3872 struct target
*target
= get_current_target(CMD_CTX
);
3873 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3874 if (retval
== ERROR_OK
)
3875 command_print(CMD_CTX
, "Physical address " TARGET_ADDR_FMT
"", pa
);
3880 static void writeData(FILE *f
, const void *data
, size_t len
)
3882 size_t written
= fwrite(data
, 1, len
, f
);
3884 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3887 static void writeLong(FILE *f
, int l
, struct target
*target
)
3891 target_buffer_set_u32(target
, val
, l
);
3892 writeData(f
, val
, 4);
3895 static void writeString(FILE *f
, char *s
)
3897 writeData(f
, s
, strlen(s
));
3900 typedef unsigned char UNIT
[2]; /* unit of profiling */
3902 /* Dump a gmon.out histogram file. */
3903 static void write_gmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
, bool with_range
,
3904 uint32_t start_address
, uint32_t end_address
, struct target
*target
, uint32_t duration_ms
)
3907 FILE *f
= fopen(filename
, "w");
3910 writeString(f
, "gmon");
3911 writeLong(f
, 0x00000001, target
); /* Version */
3912 writeLong(f
, 0, target
); /* padding */
3913 writeLong(f
, 0, target
); /* padding */
3914 writeLong(f
, 0, target
); /* padding */
3916 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3917 writeData(f
, &zero
, 1);
3919 /* figure out bucket size */
3923 min
= start_address
;
3928 for (i
= 0; i
< sampleNum
; i
++) {
3929 if (min
> samples
[i
])
3931 if (max
< samples
[i
])
3935 /* max should be (largest sample + 1)
3936 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
3940 int addressSpace
= max
- min
;
3941 assert(addressSpace
>= 2);
3943 /* FIXME: What is the reasonable number of buckets?
3944 * The profiling result will be more accurate if there are enough buckets. */
3945 static const uint32_t maxBuckets
= 128 * 1024; /* maximum buckets. */
3946 uint32_t numBuckets
= addressSpace
/ sizeof(UNIT
);
3947 if (numBuckets
> maxBuckets
)
3948 numBuckets
= maxBuckets
;
3949 int *buckets
= malloc(sizeof(int) * numBuckets
);
3950 if (buckets
== NULL
) {
3954 memset(buckets
, 0, sizeof(int) * numBuckets
);
3955 for (i
= 0; i
< sampleNum
; i
++) {
3956 uint32_t address
= samples
[i
];
3958 if ((address
< min
) || (max
<= address
))
3961 long long a
= address
- min
;
3962 long long b
= numBuckets
;
3963 long long c
= addressSpace
;
3964 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
3968 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3969 writeLong(f
, min
, target
); /* low_pc */
3970 writeLong(f
, max
, target
); /* high_pc */
3971 writeLong(f
, numBuckets
, target
); /* # of buckets */
3972 float sample_rate
= sampleNum
/ (duration_ms
/ 1000.0);
3973 writeLong(f
, sample_rate
, target
);
3974 writeString(f
, "seconds");
3975 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3976 writeData(f
, &zero
, 1);
3977 writeString(f
, "s");
3979 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3981 char *data
= malloc(2 * numBuckets
);
3983 for (i
= 0; i
< numBuckets
; i
++) {
3988 data
[i
* 2] = val
&0xff;
3989 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
3992 writeData(f
, data
, numBuckets
* 2);
4000 /* profiling samples the CPU PC as quickly as OpenOCD is able,
4001 * which will be used as a random sampling of PC */
4002 COMMAND_HANDLER(handle_profile_command
)
4004 struct target
*target
= get_current_target(CMD_CTX
);
4006 if ((CMD_ARGC
!= 2) && (CMD_ARGC
!= 4))
4007 return ERROR_COMMAND_SYNTAX_ERROR
;
4009 const uint32_t MAX_PROFILE_SAMPLE_NUM
= 10000;
4011 uint32_t num_of_samples
;
4012 int retval
= ERROR_OK
;
4014 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], offset
);
4016 uint32_t *samples
= malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM
);
4017 if (samples
== NULL
) {
4018 LOG_ERROR("No memory to store samples.");
4022 uint64_t timestart_ms
= timeval_ms();
4024 * Some cores let us sample the PC without the
4025 * annoying halt/resume step; for example, ARMv7 PCSR.
4026 * Provide a way to use that more efficient mechanism.
4028 retval
= target_profiling(target
, samples
, MAX_PROFILE_SAMPLE_NUM
,
4029 &num_of_samples
, offset
);
4030 if (retval
!= ERROR_OK
) {
4034 uint32_t duration_ms
= timeval_ms() - timestart_ms
;
4036 assert(num_of_samples
<= MAX_PROFILE_SAMPLE_NUM
);
4038 retval
= target_poll(target
);
4039 if (retval
!= ERROR_OK
) {
4043 if (target
->state
== TARGET_RUNNING
) {
4044 retval
= target_halt(target
);
4045 if (retval
!= ERROR_OK
) {
4051 retval
= target_poll(target
);
4052 if (retval
!= ERROR_OK
) {
4057 uint32_t start_address
= 0;
4058 uint32_t end_address
= 0;
4059 bool with_range
= false;
4060 if (CMD_ARGC
== 4) {
4062 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], start_address
);
4063 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], end_address
);
4066 write_gmon(samples
, num_of_samples
, CMD_ARGV
[1],
4067 with_range
, start_address
, end_address
, target
, duration_ms
);
4068 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
4074 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
4077 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4080 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4084 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4085 valObjPtr
= Jim_NewIntObj(interp
, val
);
4086 if (!nameObjPtr
|| !valObjPtr
) {
4091 Jim_IncrRefCount(nameObjPtr
);
4092 Jim_IncrRefCount(valObjPtr
);
4093 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
4094 Jim_DecrRefCount(interp
, nameObjPtr
);
4095 Jim_DecrRefCount(interp
, valObjPtr
);
4097 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
4101 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4103 struct command_context
*context
;
4104 struct target
*target
;
4106 context
= current_command_context(interp
);
4107 assert(context
!= NULL
);
4109 target
= get_current_target(context
);
4110 if (target
== NULL
) {
4111 LOG_ERROR("mem2array: no current target");
4115 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4118 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
4126 const char *varname
;
4132 /* argv[1] = name of array to receive the data
4133 * argv[2] = desired width
4134 * argv[3] = memory address
4135 * argv[4] = count of times to read
4138 if (argc
< 4 || argc
> 5) {
4139 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4142 varname
= Jim_GetString(argv
[0], &len
);
4143 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4145 e
= Jim_GetLong(interp
, argv
[1], &l
);
4150 e
= Jim_GetLong(interp
, argv
[2], &l
);
4154 e
= Jim_GetLong(interp
, argv
[3], &l
);
4160 phys
= Jim_GetString(argv
[4], &n
);
4161 if (!strncmp(phys
, "phys", n
))
4177 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4178 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
4182 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4183 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
4186 if ((addr
+ (len
* width
)) < addr
) {
4187 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4188 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
4191 /* absurd transfer size? */
4193 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4194 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
4199 ((width
== 2) && ((addr
& 1) == 0)) ||
4200 ((width
== 4) && ((addr
& 3) == 0))) {
4204 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4205 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
4208 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4217 size_t buffersize
= 4096;
4218 uint8_t *buffer
= malloc(buffersize
);
4225 /* Slurp... in buffer size chunks */
4227 count
= len
; /* in objects.. */
4228 if (count
> (buffersize
/ width
))
4229 count
= (buffersize
/ width
);
4232 retval
= target_read_phys_memory(target
, addr
, width
, count
, buffer
);
4234 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
4235 if (retval
!= ERROR_OK
) {
4237 LOG_ERROR("mem2array: Read @ 0x%08" PRIx32
", w=%" PRId32
", cnt=%" PRId32
", failed",
4241 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4242 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
4246 v
= 0; /* shut up gcc */
4247 for (i
= 0; i
< count
; i
++, n
++) {
4250 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
4253 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
4256 v
= buffer
[i
] & 0x0ff;
4259 new_int_array_element(interp
, varname
, n
, v
);
4262 addr
+= count
* width
;
4268 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4273 static int get_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t *val
)
4276 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4280 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4284 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4290 Jim_IncrRefCount(nameObjPtr
);
4291 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
4292 Jim_DecrRefCount(interp
, nameObjPtr
);
4294 if (valObjPtr
== NULL
)
4297 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
4298 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
4303 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4305 struct command_context
*context
;
4306 struct target
*target
;
4308 context
= current_command_context(interp
);
4309 assert(context
!= NULL
);
4311 target
= get_current_target(context
);
4312 if (target
== NULL
) {
4313 LOG_ERROR("array2mem: no current target");
4317 return target_array2mem(interp
, target
, argc
-1, argv
+ 1);
4320 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
4321 int argc
, Jim_Obj
*const *argv
)
4329 const char *varname
;
4335 /* argv[1] = name of array to get the data
4336 * argv[2] = desired width
4337 * argv[3] = memory address
4338 * argv[4] = count to write
4340 if (argc
< 4 || argc
> 5) {
4341 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4344 varname
= Jim_GetString(argv
[0], &len
);
4345 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4347 e
= Jim_GetLong(interp
, argv
[1], &l
);
4352 e
= Jim_GetLong(interp
, argv
[2], &l
);
4356 e
= Jim_GetLong(interp
, argv
[3], &l
);
4362 phys
= Jim_GetString(argv
[4], &n
);
4363 if (!strncmp(phys
, "phys", n
))
4379 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4380 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4381 "Invalid width param, must be 8/16/32", NULL
);
4385 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4386 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4387 "array2mem: zero width read?", NULL
);
4390 if ((addr
+ (len
* width
)) < addr
) {
4391 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4392 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4393 "array2mem: addr + len - wraps to zero?", NULL
);
4396 /* absurd transfer size? */
4398 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4399 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4400 "array2mem: absurd > 64K item request", NULL
);
4405 ((width
== 2) && ((addr
& 1) == 0)) ||
4406 ((width
== 4) && ((addr
& 3) == 0))) {
4410 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4411 sprintf(buf
, "array2mem address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
4414 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4425 size_t buffersize
= 4096;
4426 uint8_t *buffer
= malloc(buffersize
);
4431 /* Slurp... in buffer size chunks */
4433 count
= len
; /* in objects.. */
4434 if (count
> (buffersize
/ width
))
4435 count
= (buffersize
/ width
);
4437 v
= 0; /* shut up gcc */
4438 for (i
= 0; i
< count
; i
++, n
++) {
4439 get_int_array_element(interp
, varname
, n
, &v
);
4442 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
4445 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
4448 buffer
[i
] = v
& 0x0ff;
4455 retval
= target_write_phys_memory(target
, addr
, width
, count
, buffer
);
4457 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
4458 if (retval
!= ERROR_OK
) {
4460 LOG_ERROR("array2mem: Write @ 0x%08" PRIx32
", w=%" PRId32
", cnt=%" PRId32
", failed",
4464 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4465 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
4469 addr
+= count
* width
;
4474 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4479 /* FIX? should we propagate errors here rather than printing them
4482 void target_handle_event(struct target
*target
, enum target_event e
)
4484 struct target_event_action
*teap
;
4486 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4487 if (teap
->event
== e
) {
4488 LOG_DEBUG("target(%d): %s (%s) event: %d (%s) action: %s",
4489 target
->target_number
,
4490 target_name(target
),
4491 target_type_name(target
),
4493 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
4494 Jim_GetString(teap
->body
, NULL
));
4496 /* Override current target by the target an event
4497 * is issued from (lot of scripts need it).
4498 * Return back to previous override as soon
4499 * as the handler processing is done */
4500 struct command_context
*cmd_ctx
= current_command_context(teap
->interp
);
4501 struct target
*saved_target_override
= cmd_ctx
->current_target_override
;
4502 cmd_ctx
->current_target_override
= target
;
4504 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
) {
4505 Jim_MakeErrorMessage(teap
->interp
);
4506 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
4509 cmd_ctx
->current_target_override
= saved_target_override
;
4515 * Returns true only if the target has a handler for the specified event.
4517 bool target_has_event_action(struct target
*target
, enum target_event event
)
4519 struct target_event_action
*teap
;
4521 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4522 if (teap
->event
== event
)
4528 enum target_cfg_param
{
4531 TCFG_WORK_AREA_VIRT
,
4532 TCFG_WORK_AREA_PHYS
,
4533 TCFG_WORK_AREA_SIZE
,
4534 TCFG_WORK_AREA_BACKUP
,
4537 TCFG_CHAIN_POSITION
,
4544 static Jim_Nvp nvp_config_opts
[] = {
4545 { .name
= "-type", .value
= TCFG_TYPE
},
4546 { .name
= "-event", .value
= TCFG_EVENT
},
4547 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
4548 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
4549 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
4550 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
4551 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
4552 { .name
= "-coreid", .value
= TCFG_COREID
},
4553 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
4554 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
4555 { .name
= "-rtos", .value
= TCFG_RTOS
},
4556 { .name
= "-defer-examine", .value
= TCFG_DEFER_EXAMINE
},
4557 { .name
= "-gdb-port", .value
= TCFG_GDB_PORT
},
4558 { .name
= NULL
, .value
= -1 }
4561 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
4568 /* parse config or cget options ... */
4569 while (goi
->argc
> 0) {
4570 Jim_SetEmptyResult(goi
->interp
);
4571 /* Jim_GetOpt_Debug(goi); */
4573 if (target
->type
->target_jim_configure
) {
4574 /* target defines a configure function */
4575 /* target gets first dibs on parameters */
4576 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
4585 /* otherwise we 'continue' below */
4587 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
4589 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
4595 if (goi
->isconfigure
) {
4596 Jim_SetResultFormatted(goi
->interp
,
4597 "not settable: %s", n
->name
);
4601 if (goi
->argc
!= 0) {
4602 Jim_WrongNumArgs(goi
->interp
,
4603 goi
->argc
, goi
->argv
,
4608 Jim_SetResultString(goi
->interp
,
4609 target_type_name(target
), -1);
4613 if (goi
->argc
== 0) {
4614 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
4618 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
4620 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
4624 if (goi
->isconfigure
) {
4625 if (goi
->argc
!= 1) {
4626 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
4630 if (goi
->argc
!= 0) {
4631 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
4637 struct target_event_action
*teap
;
4639 teap
= target
->event_action
;
4640 /* replace existing? */
4642 if (teap
->event
== (enum target_event
)n
->value
)
4647 if (goi
->isconfigure
) {
4648 bool replace
= true;
4651 teap
= calloc(1, sizeof(*teap
));
4654 teap
->event
= n
->value
;
4655 teap
->interp
= goi
->interp
;
4656 Jim_GetOpt_Obj(goi
, &o
);
4658 Jim_DecrRefCount(teap
->interp
, teap
->body
);
4659 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
4662 * Tcl/TK - "tk events" have a nice feature.
4663 * See the "BIND" command.
4664 * We should support that here.
4665 * You can specify %X and %Y in the event code.
4666 * The idea is: %T - target name.
4667 * The idea is: %N - target number
4668 * The idea is: %E - event name.
4670 Jim_IncrRefCount(teap
->body
);
4673 /* add to head of event list */
4674 teap
->next
= target
->event_action
;
4675 target
->event_action
= teap
;
4677 Jim_SetEmptyResult(goi
->interp
);
4681 Jim_SetEmptyResult(goi
->interp
);
4683 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
4689 case TCFG_WORK_AREA_VIRT
:
4690 if (goi
->isconfigure
) {
4691 target_free_all_working_areas(target
);
4692 e
= Jim_GetOpt_Wide(goi
, &w
);
4695 target
->working_area_virt
= w
;
4696 target
->working_area_virt_spec
= true;
4701 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
4705 case TCFG_WORK_AREA_PHYS
:
4706 if (goi
->isconfigure
) {
4707 target_free_all_working_areas(target
);
4708 e
= Jim_GetOpt_Wide(goi
, &w
);
4711 target
->working_area_phys
= w
;
4712 target
->working_area_phys_spec
= true;
4717 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
4721 case TCFG_WORK_AREA_SIZE
:
4722 if (goi
->isconfigure
) {
4723 target_free_all_working_areas(target
);
4724 e
= Jim_GetOpt_Wide(goi
, &w
);
4727 target
->working_area_size
= w
;
4732 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4736 case TCFG_WORK_AREA_BACKUP
:
4737 if (goi
->isconfigure
) {
4738 target_free_all_working_areas(target
);
4739 e
= Jim_GetOpt_Wide(goi
, &w
);
4742 /* make this exactly 1 or 0 */
4743 target
->backup_working_area
= (!!w
);
4748 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
4749 /* loop for more e*/
4754 if (goi
->isconfigure
) {
4755 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
4757 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
4760 target
->endianness
= n
->value
;
4765 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4766 if (n
->name
== NULL
) {
4767 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4768 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4770 Jim_SetResultString(goi
->interp
, n
->name
, -1);
4775 if (goi
->isconfigure
) {
4776 e
= Jim_GetOpt_Wide(goi
, &w
);
4779 target
->coreid
= (int32_t)w
;
4784 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4788 case TCFG_CHAIN_POSITION
:
4789 if (goi
->isconfigure
) {
4791 struct jtag_tap
*tap
;
4793 if (target
->has_dap
) {
4794 Jim_SetResultString(goi
->interp
,
4795 "target requires -dap parameter instead of -chain-position!", -1);
4799 target_free_all_working_areas(target
);
4800 e
= Jim_GetOpt_Obj(goi
, &o_t
);
4803 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
4807 target
->tap_configured
= true;
4812 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
4813 /* loop for more e*/
4816 if (goi
->isconfigure
) {
4817 e
= Jim_GetOpt_Wide(goi
, &w
);
4820 target
->dbgbase
= (uint32_t)w
;
4821 target
->dbgbase_set
= true;
4826 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
4832 int result
= rtos_create(goi
, target
);
4833 if (result
!= JIM_OK
)
4839 case TCFG_DEFER_EXAMINE
:
4841 target
->defer_examine
= true;
4846 if (goi
->isconfigure
) {
4848 e
= Jim_GetOpt_String(goi
, &s
, NULL
);
4851 target
->gdb_port_override
= strdup(s
);
4856 Jim_SetResultString(goi
->interp
, target
->gdb_port_override
? : "undefined", -1);
4860 } /* while (goi->argc) */
4863 /* done - we return */
4867 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4871 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4872 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4874 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4875 "missing: -option ...");
4878 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4879 return target_configure(&goi
, target
);
4882 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4884 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4887 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4889 if (goi
.argc
< 2 || goi
.argc
> 4) {
4890 Jim_SetResultFormatted(goi
.interp
,
4891 "usage: %s [phys] <address> <data> [<count>]", cmd_name
);
4896 fn
= target_write_memory
;
4899 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4901 struct Jim_Obj
*obj
;
4902 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4906 fn
= target_write_phys_memory
;
4910 e
= Jim_GetOpt_Wide(&goi
, &a
);
4915 e
= Jim_GetOpt_Wide(&goi
, &b
);
4920 if (goi
.argc
== 1) {
4921 e
= Jim_GetOpt_Wide(&goi
, &c
);
4926 /* all args must be consumed */
4930 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4932 if (strcasecmp(cmd_name
, "mww") == 0)
4934 else if (strcasecmp(cmd_name
, "mwh") == 0)
4936 else if (strcasecmp(cmd_name
, "mwb") == 0)
4939 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4943 return (target_fill_mem(target
, a
, fn
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4947 * @brief Reads an array of words/halfwords/bytes from target memory starting at specified address.
4949 * Usage: mdw [phys] <address> [<count>] - for 32 bit reads
4950 * mdh [phys] <address> [<count>] - for 16 bit reads
4951 * mdb [phys] <address> [<count>] - for 8 bit reads
4953 * Count defaults to 1.
4955 * Calls target_read_memory or target_read_phys_memory depending on
4956 * the presence of the "phys" argument
4957 * Reads the target memory in blocks of max. 32 bytes, and returns an array of ints formatted
4958 * to int representation in base16.
4959 * Also outputs read data in a human readable form using command_print
4961 * @param phys if present target_read_phys_memory will be used instead of target_read_memory
4962 * @param address address where to start the read. May be specified in decimal or hex using the standard "0x" prefix
4963 * @param count optional count parameter to read an array of values. If not specified, defaults to 1.
4964 * @returns: JIM_ERR on error or JIM_OK on success and sets the result string to an array of ascii formatted numbers
4965 * on success, with [<count>] number of elements.
4967 * In case of little endian target:
4968 * Example1: "mdw 0x00000000" returns "10123456"
4969 * Exmaple2: "mdh 0x00000000 1" returns "3456"
4970 * Example3: "mdb 0x00000000" returns "56"
4971 * Example4: "mdh 0x00000000 2" returns "3456 1012"
4972 * Example5: "mdb 0x00000000 3" returns "56 34 12"
4974 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4976 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4979 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4981 if ((goi
.argc
< 1) || (goi
.argc
> 3)) {
4982 Jim_SetResultFormatted(goi
.interp
,
4983 "usage: %s [phys] <address> [<count>]", cmd_name
);
4987 int (*fn
)(struct target
*target
,
4988 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
4989 fn
= target_read_memory
;
4992 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4994 struct Jim_Obj
*obj
;
4995 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4999 fn
= target_read_phys_memory
;
5002 /* Read address parameter */
5004 e
= Jim_GetOpt_Wide(&goi
, &addr
);
5008 /* If next parameter exists, read it out as the count parameter, if not, set it to 1 (default) */
5010 if (goi
.argc
== 1) {
5011 e
= Jim_GetOpt_Wide(&goi
, &count
);
5017 /* all args must be consumed */
5021 jim_wide dwidth
= 1; /* shut up gcc */
5022 if (strcasecmp(cmd_name
, "mdw") == 0)
5024 else if (strcasecmp(cmd_name
, "mdh") == 0)
5026 else if (strcasecmp(cmd_name
, "mdb") == 0)
5029 LOG_ERROR("command '%s' unknown: ", cmd_name
);
5033 /* convert count to "bytes" */
5034 int bytes
= count
* dwidth
;
5036 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
5037 uint8_t target_buf
[32];
5040 y
= (bytes
< 16) ? bytes
: 16; /* y = min(bytes, 16); */
5042 /* Try to read out next block */
5043 e
= fn(target
, addr
, dwidth
, y
/ dwidth
, target_buf
);
5045 if (e
!= ERROR_OK
) {
5046 Jim_SetResultFormatted(interp
, "error reading target @ 0x%08lx", (long)addr
);
5050 command_print_sameline(NULL
, "0x%08x ", (int)(addr
));
5053 for (x
= 0; x
< 16 && x
< y
; x
+= 4) {
5054 z
= target_buffer_get_u32(target
, &(target_buf
[x
]));
5055 command_print_sameline(NULL
, "%08x ", (int)(z
));
5057 for (; (x
< 16) ; x
+= 4)
5058 command_print_sameline(NULL
, " ");
5061 for (x
= 0; x
< 16 && x
< y
; x
+= 2) {
5062 z
= target_buffer_get_u16(target
, &(target_buf
[x
]));
5063 command_print_sameline(NULL
, "%04x ", (int)(z
));
5065 for (; (x
< 16) ; x
+= 2)
5066 command_print_sameline(NULL
, " ");
5070 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
5071 z
= target_buffer_get_u8(target
, &(target_buf
[x
]));
5072 command_print_sameline(NULL
, "%02x ", (int)(z
));
5074 for (; (x
< 16) ; x
+= 1)
5075 command_print_sameline(NULL
, " ");
5078 /* ascii-ify the bytes */
5079 for (x
= 0 ; x
< y
; x
++) {
5080 if ((target_buf
[x
] >= 0x20) &&
5081 (target_buf
[x
] <= 0x7e)) {
5085 target_buf
[x
] = '.';
5090 target_buf
[x
] = ' ';
5095 /* print - with a newline */
5096 command_print_sameline(NULL
, "%s\n", target_buf
);
5104 static int jim_target_mem2array(Jim_Interp
*interp
,
5105 int argc
, Jim_Obj
*const *argv
)
5107 struct target
*target
= Jim_CmdPrivData(interp
);
5108 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
5111 static int jim_target_array2mem(Jim_Interp
*interp
,
5112 int argc
, Jim_Obj
*const *argv
)
5114 struct target
*target
= Jim_CmdPrivData(interp
);
5115 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
5118 static int jim_target_tap_disabled(Jim_Interp
*interp
)
5120 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
5124 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5126 bool allow_defer
= false;
5129 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5131 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5132 Jim_SetResultFormatted(goi
.interp
,
5133 "usage: %s ['allow-defer']", cmd_name
);
5137 strcmp(Jim_GetString(argv
[1], NULL
), "allow-defer") == 0) {
5139 struct Jim_Obj
*obj
;
5140 int e
= Jim_GetOpt_Obj(&goi
, &obj
);
5146 struct target
*target
= Jim_CmdPrivData(interp
);
5147 if (!target
->tap
->enabled
)
5148 return jim_target_tap_disabled(interp
);
5150 if (allow_defer
&& target
->defer_examine
) {
5151 LOG_INFO("Deferring arp_examine of %s", target_name(target
));
5152 LOG_INFO("Use arp_examine command to examine it manually!");
5156 int e
= target
->type
->examine(target
);
5162 static int jim_target_was_examined(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5164 struct target
*target
= Jim_CmdPrivData(interp
);
5166 Jim_SetResultBool(interp
, target_was_examined(target
));
5170 static int jim_target_examine_deferred(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5172 struct target
*target
= Jim_CmdPrivData(interp
);
5174 Jim_SetResultBool(interp
, target
->defer_examine
);
5178 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5181 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5184 struct target
*target
= Jim_CmdPrivData(interp
);
5186 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
5192 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5195 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5198 struct target
*target
= Jim_CmdPrivData(interp
);
5199 if (!target
->tap
->enabled
)
5200 return jim_target_tap_disabled(interp
);
5203 if (!(target_was_examined(target
)))
5204 e
= ERROR_TARGET_NOT_EXAMINED
;
5206 e
= target
->type
->poll(target
);
5212 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5215 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5217 if (goi
.argc
!= 2) {
5218 Jim_WrongNumArgs(interp
, 0, argv
,
5219 "([tT]|[fF]|assert|deassert) BOOL");
5224 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
5226 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
5229 /* the halt or not param */
5231 e
= Jim_GetOpt_Wide(&goi
, &a
);
5235 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
5236 if (!target
->tap
->enabled
)
5237 return jim_target_tap_disabled(interp
);
5239 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
5240 Jim_SetResultFormatted(interp
,
5241 "No target-specific reset for %s",
5242 target_name(target
));
5246 if (target
->defer_examine
)
5247 target_reset_examined(target
);
5249 /* determine if we should halt or not. */
5250 target
->reset_halt
= !!a
;
5251 /* When this happens - all workareas are invalid. */
5252 target_free_all_working_areas_restore(target
, 0);
5255 if (n
->value
== NVP_ASSERT
)
5256 e
= target
->type
->assert_reset(target
);
5258 e
= target
->type
->deassert_reset(target
);
5259 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5262 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5265 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5268 struct target
*target
= Jim_CmdPrivData(interp
);
5269 if (!target
->tap
->enabled
)
5270 return jim_target_tap_disabled(interp
);
5271 int e
= target
->type
->halt(target
);
5272 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5275 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5278 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5280 /* params: <name> statename timeoutmsecs */
5281 if (goi
.argc
!= 2) {
5282 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5283 Jim_SetResultFormatted(goi
.interp
,
5284 "%s <state_name> <timeout_in_msec>", cmd_name
);
5289 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
5291 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
, 1);
5295 e
= Jim_GetOpt_Wide(&goi
, &a
);
5298 struct target
*target
= Jim_CmdPrivData(interp
);
5299 if (!target
->tap
->enabled
)
5300 return jim_target_tap_disabled(interp
);
5302 e
= target_wait_state(target
, n
->value
, a
);
5303 if (e
!= ERROR_OK
) {
5304 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
5305 Jim_SetResultFormatted(goi
.interp
,
5306 "target: %s wait %s fails (%#s) %s",
5307 target_name(target
), n
->name
,
5308 eObj
, target_strerror_safe(e
));
5309 Jim_FreeNewObj(interp
, eObj
);
5314 /* List for human, Events defined for this target.
5315 * scripts/programs should use 'name cget -event NAME'
5317 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5319 struct command_context
*cmd_ctx
= current_command_context(interp
);
5320 assert(cmd_ctx
!= NULL
);
5322 struct target
*target
= Jim_CmdPrivData(interp
);
5323 struct target_event_action
*teap
= target
->event_action
;
5324 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
5325 target
->target_number
,
5326 target_name(target
));
5327 command_print(cmd_ctx
, "%-25s | Body", "Event");
5328 command_print(cmd_ctx
, "------------------------- | "
5329 "----------------------------------------");
5331 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
5332 command_print(cmd_ctx
, "%-25s | %s",
5333 opt
->name
, Jim_GetString(teap
->body
, NULL
));
5336 command_print(cmd_ctx
, "***END***");
5339 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5342 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5345 struct target
*target
= Jim_CmdPrivData(interp
);
5346 Jim_SetResultString(interp
, target_state_name(target
), -1);
5349 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5352 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5353 if (goi
.argc
!= 1) {
5354 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5355 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
5359 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
5361 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
5364 struct target
*target
= Jim_CmdPrivData(interp
);
5365 target_handle_event(target
, n
->value
);
5369 static const struct command_registration target_instance_command_handlers
[] = {
5371 .name
= "configure",
5372 .mode
= COMMAND_CONFIG
,
5373 .jim_handler
= jim_target_configure
,
5374 .help
= "configure a new target for use",
5375 .usage
= "[target_attribute ...]",
5379 .mode
= COMMAND_ANY
,
5380 .jim_handler
= jim_target_configure
,
5381 .help
= "returns the specified target attribute",
5382 .usage
= "target_attribute",
5386 .mode
= COMMAND_EXEC
,
5387 .jim_handler
= jim_target_mw
,
5388 .help
= "Write 32-bit word(s) to target memory",
5389 .usage
= "address data [count]",
5393 .mode
= COMMAND_EXEC
,
5394 .jim_handler
= jim_target_mw
,
5395 .help
= "Write 16-bit half-word(s) to target memory",
5396 .usage
= "address data [count]",
5400 .mode
= COMMAND_EXEC
,
5401 .jim_handler
= jim_target_mw
,
5402 .help
= "Write byte(s) to target memory",
5403 .usage
= "address data [count]",
5407 .mode
= COMMAND_EXEC
,
5408 .jim_handler
= jim_target_md
,
5409 .help
= "Display target memory as 32-bit words",
5410 .usage
= "address [count]",
5414 .mode
= COMMAND_EXEC
,
5415 .jim_handler
= jim_target_md
,
5416 .help
= "Display target memory as 16-bit half-words",
5417 .usage
= "address [count]",
5421 .mode
= COMMAND_EXEC
,
5422 .jim_handler
= jim_target_md
,
5423 .help
= "Display target memory as 8-bit bytes",
5424 .usage
= "address [count]",
5427 .name
= "array2mem",
5428 .mode
= COMMAND_EXEC
,
5429 .jim_handler
= jim_target_array2mem
,
5430 .help
= "Writes Tcl array of 8/16/32 bit numbers "
5432 .usage
= "arrayname bitwidth address count",
5435 .name
= "mem2array",
5436 .mode
= COMMAND_EXEC
,
5437 .jim_handler
= jim_target_mem2array
,
5438 .help
= "Loads Tcl array of 8/16/32 bit numbers "
5439 "from target memory",
5440 .usage
= "arrayname bitwidth address count",
5443 .name
= "eventlist",
5444 .mode
= COMMAND_EXEC
,
5445 .jim_handler
= jim_target_event_list
,
5446 .help
= "displays a table of events defined for this target",
5450 .mode
= COMMAND_EXEC
,
5451 .jim_handler
= jim_target_current_state
,
5452 .help
= "displays the current state of this target",
5455 .name
= "arp_examine",
5456 .mode
= COMMAND_EXEC
,
5457 .jim_handler
= jim_target_examine
,
5458 .help
= "used internally for reset processing",
5459 .usage
= "['allow-defer']",
5462 .name
= "was_examined",
5463 .mode
= COMMAND_EXEC
,
5464 .jim_handler
= jim_target_was_examined
,
5465 .help
= "used internally for reset processing",
5468 .name
= "examine_deferred",
5469 .mode
= COMMAND_EXEC
,
5470 .jim_handler
= jim_target_examine_deferred
,
5471 .help
= "used internally for reset processing",
5474 .name
= "arp_halt_gdb",
5475 .mode
= COMMAND_EXEC
,
5476 .jim_handler
= jim_target_halt_gdb
,
5477 .help
= "used internally for reset processing to halt GDB",
5481 .mode
= COMMAND_EXEC
,
5482 .jim_handler
= jim_target_poll
,
5483 .help
= "used internally for reset processing",
5486 .name
= "arp_reset",
5487 .mode
= COMMAND_EXEC
,
5488 .jim_handler
= jim_target_reset
,
5489 .help
= "used internally for reset processing",
5493 .mode
= COMMAND_EXEC
,
5494 .jim_handler
= jim_target_halt
,
5495 .help
= "used internally for reset processing",
5498 .name
= "arp_waitstate",
5499 .mode
= COMMAND_EXEC
,
5500 .jim_handler
= jim_target_wait_state
,
5501 .help
= "used internally for reset processing",
5504 .name
= "invoke-event",
5505 .mode
= COMMAND_EXEC
,
5506 .jim_handler
= jim_target_invoke_event
,
5507 .help
= "invoke handler for specified event",
5508 .usage
= "event_name",
5510 COMMAND_REGISTRATION_DONE
5513 static int target_create(Jim_GetOptInfo
*goi
)
5520 struct target
*target
;
5521 struct command_context
*cmd_ctx
;
5523 cmd_ctx
= current_command_context(goi
->interp
);
5524 assert(cmd_ctx
!= NULL
);
5526 if (goi
->argc
< 3) {
5527 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
5532 Jim_GetOpt_Obj(goi
, &new_cmd
);
5533 /* does this command exist? */
5534 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
5536 cp
= Jim_GetString(new_cmd
, NULL
);
5537 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
5542 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
5545 struct transport
*tr
= get_current_transport();
5546 if (tr
->override_target
) {
5547 e
= tr
->override_target(&cp
);
5548 if (e
!= ERROR_OK
) {
5549 LOG_ERROR("The selected transport doesn't support this target");
5552 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
5554 /* now does target type exist */
5555 for (x
= 0 ; target_types
[x
] ; x
++) {
5556 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
5561 /* check for deprecated name */
5562 if (target_types
[x
]->deprecated_name
) {
5563 if (0 == strcmp(cp
, target_types
[x
]->deprecated_name
)) {
5565 LOG_WARNING("target name is deprecated use: \'%s\'", target_types
[x
]->name
);
5570 if (target_types
[x
] == NULL
) {
5571 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
5572 for (x
= 0 ; target_types
[x
] ; x
++) {
5573 if (target_types
[x
+ 1]) {
5574 Jim_AppendStrings(goi
->interp
,
5575 Jim_GetResult(goi
->interp
),
5576 target_types
[x
]->name
,
5579 Jim_AppendStrings(goi
->interp
,
5580 Jim_GetResult(goi
->interp
),
5582 target_types
[x
]->name
, NULL
);
5589 target
= calloc(1, sizeof(struct target
));
5590 /* set target number */
5591 target
->target_number
= new_target_number();
5592 cmd_ctx
->current_target
= target
;
5594 /* allocate memory for each unique target type */
5595 target
->type
= calloc(1, sizeof(struct target_type
));
5597 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
5599 /* will be set by "-endian" */
5600 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5602 /* default to first core, override with -coreid */
5605 target
->working_area
= 0x0;
5606 target
->working_area_size
= 0x0;
5607 target
->working_areas
= NULL
;
5608 target
->backup_working_area
= 0;
5610 target
->state
= TARGET_UNKNOWN
;
5611 target
->debug_reason
= DBG_REASON_UNDEFINED
;
5612 target
->reg_cache
= NULL
;
5613 target
->breakpoints
= NULL
;
5614 target
->watchpoints
= NULL
;
5615 target
->next
= NULL
;
5616 target
->arch_info
= NULL
;
5618 target
->verbose_halt_msg
= true;
5620 target
->halt_issued
= false;
5622 /* initialize trace information */
5623 target
->trace_info
= calloc(1, sizeof(struct trace
));
5625 target
->dbgmsg
= NULL
;
5626 target
->dbg_msg_enabled
= 0;
5628 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5630 target
->rtos
= NULL
;
5631 target
->rtos_auto_detect
= false;
5633 target
->gdb_port_override
= NULL
;
5635 /* Do the rest as "configure" options */
5636 goi
->isconfigure
= 1;
5637 e
= target_configure(goi
, target
);
5640 if (target
->has_dap
) {
5641 if (!target
->dap_configured
) {
5642 Jim_SetResultString(goi
->interp
, "-dap ?name? required when creating target", -1);
5646 if (!target
->tap_configured
) {
5647 Jim_SetResultString(goi
->interp
, "-chain-position ?name? required when creating target", -1);
5651 /* tap must be set after target was configured */
5652 if (target
->tap
== NULL
)
5657 free(target
->gdb_port_override
);
5663 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
5664 /* default endian to little if not specified */
5665 target
->endianness
= TARGET_LITTLE_ENDIAN
;
5668 cp
= Jim_GetString(new_cmd
, NULL
);
5669 target
->cmd_name
= strdup(cp
);
5671 if (target
->type
->target_create
) {
5672 e
= (*(target
->type
->target_create
))(target
, goi
->interp
);
5673 if (e
!= ERROR_OK
) {
5674 LOG_DEBUG("target_create failed");
5675 free(target
->gdb_port_override
);
5677 free(target
->cmd_name
);
5683 /* create the target specific commands */
5684 if (target
->type
->commands
) {
5685 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
5687 LOG_ERROR("unable to register '%s' commands", cp
);
5690 /* append to end of list */
5692 struct target
**tpp
;
5693 tpp
= &(all_targets
);
5695 tpp
= &((*tpp
)->next
);
5699 /* now - create the new target name command */
5700 const struct command_registration target_subcommands
[] = {
5702 .chain
= target_instance_command_handlers
,
5705 .chain
= target
->type
->commands
,
5707 COMMAND_REGISTRATION_DONE
5709 const struct command_registration target_commands
[] = {
5712 .mode
= COMMAND_ANY
,
5713 .help
= "target command group",
5715 .chain
= target_subcommands
,
5717 COMMAND_REGISTRATION_DONE
5719 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
5723 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
5725 command_set_handler_data(c
, target
);
5727 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
5730 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5733 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5736 struct command_context
*cmd_ctx
= current_command_context(interp
);
5737 assert(cmd_ctx
!= NULL
);
5739 Jim_SetResultString(interp
, target_name(get_current_target(cmd_ctx
)), -1);
5743 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5746 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5749 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5750 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++) {
5751 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5752 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
5757 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5760 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5763 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5764 struct target
*target
= all_targets
;
5766 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5767 Jim_NewStringObj(interp
, target_name(target
), -1));
5768 target
= target
->next
;
5773 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5776 const char *targetname
;
5778 struct target
*target
= (struct target
*) NULL
;
5779 struct target_list
*head
, *curr
, *new;
5780 curr
= (struct target_list
*) NULL
;
5781 head
= (struct target_list
*) NULL
;
5784 LOG_DEBUG("%d", argc
);
5785 /* argv[1] = target to associate in smp
5786 * argv[2] = target to assoicate in smp
5790 for (i
= 1; i
< argc
; i
++) {
5792 targetname
= Jim_GetString(argv
[i
], &len
);
5793 target
= get_target(targetname
);
5794 LOG_DEBUG("%s ", targetname
);
5796 new = malloc(sizeof(struct target_list
));
5797 new->target
= target
;
5798 new->next
= (struct target_list
*)NULL
;
5799 if (head
== (struct target_list
*)NULL
) {
5808 /* now parse the list of cpu and put the target in smp mode*/
5811 while (curr
!= (struct target_list
*)NULL
) {
5812 target
= curr
->target
;
5814 target
->head
= head
;
5818 if (target
&& target
->rtos
)
5819 retval
= rtos_smp_init(head
->target
);
5825 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5828 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5830 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5831 "<name> <target_type> [<target_options> ...]");
5834 return target_create(&goi
);
5837 static const struct command_registration target_subcommand_handlers
[] = {
5840 .mode
= COMMAND_CONFIG
,
5841 .handler
= handle_target_init_command
,
5842 .help
= "initialize targets",
5846 /* REVISIT this should be COMMAND_CONFIG ... */
5847 .mode
= COMMAND_ANY
,
5848 .jim_handler
= jim_target_create
,
5849 .usage
= "name type '-chain-position' name [options ...]",
5850 .help
= "Creates and selects a new target",
5854 .mode
= COMMAND_ANY
,
5855 .jim_handler
= jim_target_current
,
5856 .help
= "Returns the currently selected target",
5860 .mode
= COMMAND_ANY
,
5861 .jim_handler
= jim_target_types
,
5862 .help
= "Returns the available target types as "
5863 "a list of strings",
5867 .mode
= COMMAND_ANY
,
5868 .jim_handler
= jim_target_names
,
5869 .help
= "Returns the names of all targets as a list of strings",
5873 .mode
= COMMAND_ANY
,
5874 .jim_handler
= jim_target_smp
,
5875 .usage
= "targetname1 targetname2 ...",
5876 .help
= "gather several target in a smp list"
5879 COMMAND_REGISTRATION_DONE
5883 target_addr_t address
;
5889 static int fastload_num
;
5890 static struct FastLoad
*fastload
;
5892 static void free_fastload(void)
5894 if (fastload
!= NULL
) {
5896 for (i
= 0; i
< fastload_num
; i
++) {
5897 if (fastload
[i
].data
)
5898 free(fastload
[i
].data
);
5905 COMMAND_HANDLER(handle_fast_load_image_command
)
5909 uint32_t image_size
;
5910 target_addr_t min_address
= 0;
5911 target_addr_t max_address
= -1;
5916 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
5917 &image
, &min_address
, &max_address
);
5918 if (ERROR_OK
!= retval
)
5921 struct duration bench
;
5922 duration_start(&bench
);
5924 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5925 if (retval
!= ERROR_OK
)
5930 fastload_num
= image
.num_sections
;
5931 fastload
= malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5932 if (fastload
== NULL
) {
5933 command_print(CMD_CTX
, "out of memory");
5934 image_close(&image
);
5937 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5938 for (i
= 0; i
< image
.num_sections
; i
++) {
5939 buffer
= malloc(image
.sections
[i
].size
);
5940 if (buffer
== NULL
) {
5941 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5942 (int)(image
.sections
[i
].size
));
5943 retval
= ERROR_FAIL
;
5947 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
5948 if (retval
!= ERROR_OK
) {
5953 uint32_t offset
= 0;
5954 uint32_t length
= buf_cnt
;
5956 /* DANGER!!! beware of unsigned comparision here!!! */
5958 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
5959 (image
.sections
[i
].base_address
< max_address
)) {
5960 if (image
.sections
[i
].base_address
< min_address
) {
5961 /* clip addresses below */
5962 offset
+= min_address
-image
.sections
[i
].base_address
;
5966 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5967 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5969 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5970 fastload
[i
].data
= malloc(length
);
5971 if (fastload
[i
].data
== NULL
) {
5973 command_print(CMD_CTX
, "error allocating buffer for section (%" PRIu32
" bytes)",
5975 retval
= ERROR_FAIL
;
5978 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5979 fastload
[i
].length
= length
;
5981 image_size
+= length
;
5982 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
5983 (unsigned int)length
,
5984 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
5990 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
5991 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
5992 "in %fs (%0.3f KiB/s)", image_size
,
5993 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
5995 command_print(CMD_CTX
,
5996 "WARNING: image has not been loaded to target!"
5997 "You can issue a 'fast_load' to finish loading.");
6000 image_close(&image
);
6002 if (retval
!= ERROR_OK
)
6008 COMMAND_HANDLER(handle_fast_load_command
)
6011 return ERROR_COMMAND_SYNTAX_ERROR
;
6012 if (fastload
== NULL
) {
6013 LOG_ERROR("No image in memory");
6017 int64_t ms
= timeval_ms();
6019 int retval
= ERROR_OK
;
6020 for (i
= 0; i
< fastload_num
; i
++) {
6021 struct target
*target
= get_current_target(CMD_CTX
);
6022 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
6023 (unsigned int)(fastload
[i
].address
),
6024 (unsigned int)(fastload
[i
].length
));
6025 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
6026 if (retval
!= ERROR_OK
)
6028 size
+= fastload
[i
].length
;
6030 if (retval
== ERROR_OK
) {
6031 int64_t after
= timeval_ms();
6032 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
6037 static const struct command_registration target_command_handlers
[] = {
6040 .handler
= handle_targets_command
,
6041 .mode
= COMMAND_ANY
,
6042 .help
= "change current default target (one parameter) "
6043 "or prints table of all targets (no parameters)",
6044 .usage
= "[target]",
6048 .mode
= COMMAND_CONFIG
,
6049 .help
= "configure target",
6051 .chain
= target_subcommand_handlers
,
6053 COMMAND_REGISTRATION_DONE
6056 int target_register_commands(struct command_context
*cmd_ctx
)
6058 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
6061 static bool target_reset_nag
= true;
6063 bool get_target_reset_nag(void)
6065 return target_reset_nag
;
6068 COMMAND_HANDLER(handle_target_reset_nag
)
6070 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
6071 &target_reset_nag
, "Nag after each reset about options to improve "
6075 COMMAND_HANDLER(handle_ps_command
)
6077 struct target
*target
= get_current_target(CMD_CTX
);
6079 if (target
->state
!= TARGET_HALTED
) {
6080 LOG_INFO("target not halted !!");
6084 if ((target
->rtos
) && (target
->rtos
->type
)
6085 && (target
->rtos
->type
->ps_command
)) {
6086 display
= target
->rtos
->type
->ps_command(target
);
6087 command_print(CMD_CTX
, "%s", display
);
6092 return ERROR_TARGET_FAILURE
;
6096 static void binprint(struct command_context
*cmd_ctx
, const char *text
, const uint8_t *buf
, int size
)
6099 command_print_sameline(cmd_ctx
, "%s", text
);
6100 for (int i
= 0; i
< size
; i
++)
6101 command_print_sameline(cmd_ctx
, " %02x", buf
[i
]);
6102 command_print(cmd_ctx
, " ");
6105 COMMAND_HANDLER(handle_test_mem_access_command
)
6107 struct target
*target
= get_current_target(CMD_CTX
);
6109 int retval
= ERROR_OK
;
6111 if (target
->state
!= TARGET_HALTED
) {
6112 LOG_INFO("target not halted !!");
6117 return ERROR_COMMAND_SYNTAX_ERROR
;
6119 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], test_size
);
6122 size_t num_bytes
= test_size
+ 4;
6124 struct working_area
*wa
= NULL
;
6125 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6126 if (retval
!= ERROR_OK
) {
6127 LOG_ERROR("Not enough working area");
6131 uint8_t *test_pattern
= malloc(num_bytes
);
6133 for (size_t i
= 0; i
< num_bytes
; i
++)
6134 test_pattern
[i
] = rand();
6136 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6137 if (retval
!= ERROR_OK
) {
6138 LOG_ERROR("Test pattern write failed");
6142 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6143 for (int size
= 1; size
<= 4; size
*= 2) {
6144 for (int offset
= 0; offset
< 4; offset
++) {
6145 uint32_t count
= test_size
/ size
;
6146 size_t host_bufsiz
= (count
+ 2) * size
+ host_offset
;
6147 uint8_t *read_ref
= malloc(host_bufsiz
);
6148 uint8_t *read_buf
= malloc(host_bufsiz
);
6150 for (size_t i
= 0; i
< host_bufsiz
; i
++) {
6151 read_ref
[i
] = rand();
6152 read_buf
[i
] = read_ref
[i
];
6154 command_print_sameline(CMD_CTX
,
6155 "Test read %" PRIu32
" x %d @ %d to %saligned buffer: ", count
,
6156 size
, offset
, host_offset
? "un" : "");
6158 struct duration bench
;
6159 duration_start(&bench
);
6161 retval
= target_read_memory(target
, wa
->address
+ offset
, size
, count
,
6162 read_buf
+ size
+ host_offset
);
6164 duration_measure(&bench
);
6166 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6167 command_print(CMD_CTX
, "Unsupported alignment");
6169 } else if (retval
!= ERROR_OK
) {
6170 command_print(CMD_CTX
, "Memory read failed");
6174 /* replay on host */
6175 memcpy(read_ref
+ size
+ host_offset
, test_pattern
+ offset
, count
* size
);
6178 int result
= memcmp(read_ref
, read_buf
, host_bufsiz
);
6180 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
6181 duration_elapsed(&bench
),
6182 duration_kbps(&bench
, count
* size
));
6184 command_print(CMD_CTX
, "Compare failed");
6185 binprint(CMD_CTX
, "ref:", read_ref
, host_bufsiz
);
6186 binprint(CMD_CTX
, "buf:", read_buf
, host_bufsiz
);
6199 target_free_working_area(target
, wa
);
6202 num_bytes
= test_size
+ 4 + 4 + 4;
6204 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6205 if (retval
!= ERROR_OK
) {
6206 LOG_ERROR("Not enough working area");
6210 test_pattern
= malloc(num_bytes
);
6212 for (size_t i
= 0; i
< num_bytes
; i
++)
6213 test_pattern
[i
] = rand();
6215 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6216 for (int size
= 1; size
<= 4; size
*= 2) {
6217 for (int offset
= 0; offset
< 4; offset
++) {
6218 uint32_t count
= test_size
/ size
;
6219 size_t host_bufsiz
= count
* size
+ host_offset
;
6220 uint8_t *read_ref
= malloc(num_bytes
);
6221 uint8_t *read_buf
= malloc(num_bytes
);
6222 uint8_t *write_buf
= malloc(host_bufsiz
);
6224 for (size_t i
= 0; i
< host_bufsiz
; i
++)
6225 write_buf
[i
] = rand();
6226 command_print_sameline(CMD_CTX
,
6227 "Test write %" PRIu32
" x %d @ %d from %saligned buffer: ", count
,
6228 size
, offset
, host_offset
? "un" : "");
6230 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6231 if (retval
!= ERROR_OK
) {
6232 command_print(CMD_CTX
, "Test pattern write failed");
6236 /* replay on host */
6237 memcpy(read_ref
, test_pattern
, num_bytes
);
6238 memcpy(read_ref
+ size
+ offset
, write_buf
+ host_offset
, count
* size
);
6240 struct duration bench
;
6241 duration_start(&bench
);
6243 retval
= target_write_memory(target
, wa
->address
+ size
+ offset
, size
, count
,
6244 write_buf
+ host_offset
);
6246 duration_measure(&bench
);
6248 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6249 command_print(CMD_CTX
, "Unsupported alignment");
6251 } else if (retval
!= ERROR_OK
) {
6252 command_print(CMD_CTX
, "Memory write failed");
6257 retval
= target_read_memory(target
, wa
->address
, 1, num_bytes
, read_buf
);
6258 if (retval
!= ERROR_OK
) {
6259 command_print(CMD_CTX
, "Test pattern write failed");
6264 int result
= memcmp(read_ref
, read_buf
, num_bytes
);
6266 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
6267 duration_elapsed(&bench
),
6268 duration_kbps(&bench
, count
* size
));
6270 command_print(CMD_CTX
, "Compare failed");
6271 binprint(CMD_CTX
, "ref:", read_ref
, num_bytes
);
6272 binprint(CMD_CTX
, "buf:", read_buf
, num_bytes
);
6284 target_free_working_area(target
, wa
);
6288 static const struct command_registration target_exec_command_handlers
[] = {
6290 .name
= "fast_load_image",
6291 .handler
= handle_fast_load_image_command
,
6292 .mode
= COMMAND_ANY
,
6293 .help
= "Load image into server memory for later use by "
6294 "fast_load; primarily for profiling",
6295 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6296 "[min_address [max_length]]",
6299 .name
= "fast_load",
6300 .handler
= handle_fast_load_command
,
6301 .mode
= COMMAND_EXEC
,
6302 .help
= "loads active fast load image to current target "
6303 "- mainly for profiling purposes",
6308 .handler
= handle_profile_command
,
6309 .mode
= COMMAND_EXEC
,
6310 .usage
= "seconds filename [start end]",
6311 .help
= "profiling samples the CPU PC",
6313 /** @todo don't register virt2phys() unless target supports it */
6315 .name
= "virt2phys",
6316 .handler
= handle_virt2phys_command
,
6317 .mode
= COMMAND_ANY
,
6318 .help
= "translate a virtual address into a physical address",
6319 .usage
= "virtual_address",
6323 .handler
= handle_reg_command
,
6324 .mode
= COMMAND_EXEC
,
6325 .help
= "display (reread from target with \"force\") or set a register; "
6326 "with no arguments, displays all registers and their values",
6327 .usage
= "[(register_number|register_name) [(value|'force')]]",
6331 .handler
= handle_poll_command
,
6332 .mode
= COMMAND_EXEC
,
6333 .help
= "poll target state; or reconfigure background polling",
6334 .usage
= "['on'|'off']",
6337 .name
= "wait_halt",
6338 .handler
= handle_wait_halt_command
,
6339 .mode
= COMMAND_EXEC
,
6340 .help
= "wait up to the specified number of milliseconds "
6341 "(default 5000) for a previously requested halt",
6342 .usage
= "[milliseconds]",
6346 .handler
= handle_halt_command
,
6347 .mode
= COMMAND_EXEC
,
6348 .help
= "request target to halt, then wait up to the specified"
6349 "number of milliseconds (default 5000) for it to complete",
6350 .usage
= "[milliseconds]",
6354 .handler
= handle_resume_command
,
6355 .mode
= COMMAND_EXEC
,
6356 .help
= "resume target execution from current PC or address",
6357 .usage
= "[address]",
6361 .handler
= handle_reset_command
,
6362 .mode
= COMMAND_EXEC
,
6363 .usage
= "[run|halt|init]",
6364 .help
= "Reset all targets into the specified mode."
6365 "Default reset mode is run, if not given.",
6368 .name
= "soft_reset_halt",
6369 .handler
= handle_soft_reset_halt_command
,
6370 .mode
= COMMAND_EXEC
,
6372 .help
= "halt the target and do a soft reset",
6376 .handler
= handle_step_command
,
6377 .mode
= COMMAND_EXEC
,
6378 .help
= "step one instruction from current PC or address",
6379 .usage
= "[address]",
6383 .handler
= handle_md_command
,
6384 .mode
= COMMAND_EXEC
,
6385 .help
= "display memory words",
6386 .usage
= "['phys'] address [count]",
6390 .handler
= handle_md_command
,
6391 .mode
= COMMAND_EXEC
,
6392 .help
= "display memory words",
6393 .usage
= "['phys'] address [count]",
6397 .handler
= handle_md_command
,
6398 .mode
= COMMAND_EXEC
,
6399 .help
= "display memory half-words",
6400 .usage
= "['phys'] address [count]",
6404 .handler
= handle_md_command
,
6405 .mode
= COMMAND_EXEC
,
6406 .help
= "display memory bytes",
6407 .usage
= "['phys'] address [count]",
6411 .handler
= handle_mw_command
,
6412 .mode
= COMMAND_EXEC
,
6413 .help
= "write memory word",
6414 .usage
= "['phys'] address value [count]",
6418 .handler
= handle_mw_command
,
6419 .mode
= COMMAND_EXEC
,
6420 .help
= "write memory word",
6421 .usage
= "['phys'] address value [count]",
6425 .handler
= handle_mw_command
,
6426 .mode
= COMMAND_EXEC
,
6427 .help
= "write memory half-word",
6428 .usage
= "['phys'] address value [count]",
6432 .handler
= handle_mw_command
,
6433 .mode
= COMMAND_EXEC
,
6434 .help
= "write memory byte",
6435 .usage
= "['phys'] address value [count]",
6439 .handler
= handle_bp_command
,
6440 .mode
= COMMAND_EXEC
,
6441 .help
= "list or set hardware or software breakpoint",
6442 .usage
= "<address> [<asid>] <length> ['hw'|'hw_ctx']",
6446 .handler
= handle_rbp_command
,
6447 .mode
= COMMAND_EXEC
,
6448 .help
= "remove breakpoint",
6453 .handler
= handle_wp_command
,
6454 .mode
= COMMAND_EXEC
,
6455 .help
= "list (no params) or create watchpoints",
6456 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
6460 .handler
= handle_rwp_command
,
6461 .mode
= COMMAND_EXEC
,
6462 .help
= "remove watchpoint",
6466 .name
= "load_image",
6467 .handler
= handle_load_image_command
,
6468 .mode
= COMMAND_EXEC
,
6469 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6470 "[min_address] [max_length]",
6473 .name
= "dump_image",
6474 .handler
= handle_dump_image_command
,
6475 .mode
= COMMAND_EXEC
,
6476 .usage
= "filename address size",
6479 .name
= "verify_image_checksum",
6480 .handler
= handle_verify_image_checksum_command
,
6481 .mode
= COMMAND_EXEC
,
6482 .usage
= "filename [offset [type]]",
6485 .name
= "verify_image",
6486 .handler
= handle_verify_image_command
,
6487 .mode
= COMMAND_EXEC
,
6488 .usage
= "filename [offset [type]]",
6491 .name
= "test_image",
6492 .handler
= handle_test_image_command
,
6493 .mode
= COMMAND_EXEC
,
6494 .usage
= "filename [offset [type]]",
6497 .name
= "mem2array",
6498 .mode
= COMMAND_EXEC
,
6499 .jim_handler
= jim_mem2array
,
6500 .help
= "read 8/16/32 bit memory and return as a TCL array "
6501 "for script processing",
6502 .usage
= "arrayname bitwidth address count",
6505 .name
= "array2mem",
6506 .mode
= COMMAND_EXEC
,
6507 .jim_handler
= jim_array2mem
,
6508 .help
= "convert a TCL array to memory locations "
6509 "and write the 8/16/32 bit values",
6510 .usage
= "arrayname bitwidth address count",
6513 .name
= "reset_nag",
6514 .handler
= handle_target_reset_nag
,
6515 .mode
= COMMAND_ANY
,
6516 .help
= "Nag after each reset about options that could have been "
6517 "enabled to improve performance. ",
6518 .usage
= "['enable'|'disable']",
6522 .handler
= handle_ps_command
,
6523 .mode
= COMMAND_EXEC
,
6524 .help
= "list all tasks ",
6528 .name
= "test_mem_access",
6529 .handler
= handle_test_mem_access_command
,
6530 .mode
= COMMAND_EXEC
,
6531 .help
= "Test the target's memory access functions",
6535 COMMAND_REGISTRATION_DONE
6537 static int target_register_user_commands(struct command_context
*cmd_ctx
)
6539 int retval
= ERROR_OK
;
6540 retval
= target_request_register_commands(cmd_ctx
);
6541 if (retval
!= ERROR_OK
)
6544 retval
= trace_register_commands(cmd_ctx
);
6545 if (retval
!= ERROR_OK
)
6549 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);