1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
17 * Copyright (C) 2011 by Broadcom Corporation *
18 * Evan Hunter - ehunter@broadcom.com *
20 * Copyright (C) ST-Ericsson SA 2011 *
21 * michel.jaouen@stericsson.com : smp minimum support *
23 * Copyright (C) 2011 Andreas Fritiofson *
24 * andreas.fritiofson@gmail.com *
26 * This program is free software; you can redistribute it and/or modify *
27 * it under the terms of the GNU General Public License as published by *
28 * the Free Software Foundation; either version 2 of the License, or *
29 * (at your option) any later version. *
31 * This program is distributed in the hope that it will be useful, *
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
34 * GNU General Public License for more details. *
36 * You should have received a copy of the GNU General Public License *
37 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
38 ***************************************************************************/
44 #include <helper/time_support.h>
45 #include <jtag/jtag.h>
46 #include <flash/nor/core.h>
49 #include "target_type.h"
50 #include "target_request.h"
51 #include "breakpoints.h"
55 #include "rtos/rtos.h"
56 #include "transport/transport.h"
59 /* default halt wait timeout (ms) */
60 #define DEFAULT_HALT_TIMEOUT 5000
62 static int target_read_buffer_default(struct target
*target
, target_addr_t address
,
63 uint32_t count
, uint8_t *buffer
);
64 static int target_write_buffer_default(struct target
*target
, target_addr_t address
,
65 uint32_t count
, const uint8_t *buffer
);
66 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
67 int argc
, Jim_Obj
* const *argv
);
68 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
69 int argc
, Jim_Obj
* const *argv
);
70 static int target_register_user_commands(struct command_context
*cmd_ctx
);
71 static int target_get_gdb_fileio_info_default(struct target
*target
,
72 struct gdb_fileio_info
*fileio_info
);
73 static int target_gdb_fileio_end_default(struct target
*target
, int retcode
,
74 int fileio_errno
, bool ctrl_c
);
75 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
76 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
);
79 extern struct target_type arm7tdmi_target
;
80 extern struct target_type arm720t_target
;
81 extern struct target_type arm9tdmi_target
;
82 extern struct target_type arm920t_target
;
83 extern struct target_type arm966e_target
;
84 extern struct target_type arm946e_target
;
85 extern struct target_type arm926ejs_target
;
86 extern struct target_type fa526_target
;
87 extern struct target_type feroceon_target
;
88 extern struct target_type dragonite_target
;
89 extern struct target_type xscale_target
;
90 extern struct target_type cortexm_target
;
91 extern struct target_type cortexa_target
;
92 extern struct target_type aarch64_target
;
93 extern struct target_type cortexr4_target
;
94 extern struct target_type arm11_target
;
95 extern struct target_type ls1_sap_target
;
96 extern struct target_type mips_m4k_target
;
97 extern struct target_type avr_target
;
98 extern struct target_type dsp563xx_target
;
99 extern struct target_type dsp5680xx_target
;
100 extern struct target_type testee_target
;
101 extern struct target_type avr32_ap7k_target
;
102 extern struct target_type hla_target
;
103 extern struct target_type nds32_v2_target
;
104 extern struct target_type nds32_v3_target
;
105 extern struct target_type nds32_v3m_target
;
106 extern struct target_type or1k_target
;
107 extern struct target_type quark_x10xx_target
;
108 extern struct target_type quark_d20xx_target
;
109 extern struct target_type stm8_target
;
110 extern struct target_type riscv_target
;
111 extern struct target_type mem_ap_target
;
112 extern struct target_type esirisc_target
;
114 static struct target_type
*target_types
[] = {
154 struct target
*all_targets
;
155 static struct target_event_callback
*target_event_callbacks
;
156 static struct target_timer_callback
*target_timer_callbacks
;
157 LIST_HEAD(target_reset_callback_list
);
158 LIST_HEAD(target_trace_callback_list
);
159 static const int polling_interval
= 100;
161 static const Jim_Nvp nvp_assert
[] = {
162 { .name
= "assert", NVP_ASSERT
},
163 { .name
= "deassert", NVP_DEASSERT
},
164 { .name
= "T", NVP_ASSERT
},
165 { .name
= "F", NVP_DEASSERT
},
166 { .name
= "t", NVP_ASSERT
},
167 { .name
= "f", NVP_DEASSERT
},
168 { .name
= NULL
, .value
= -1 }
171 static const Jim_Nvp nvp_error_target
[] = {
172 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
173 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
174 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
175 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
176 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
177 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
178 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
179 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
180 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
181 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
182 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
183 { .value
= -1, .name
= NULL
}
186 static const char *target_strerror_safe(int err
)
190 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
197 static const Jim_Nvp nvp_target_event
[] = {
199 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
200 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
201 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
202 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
203 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
205 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
206 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
208 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
209 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
210 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
211 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
212 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
213 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
214 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
215 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
217 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
218 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
220 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
221 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
223 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
224 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
226 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
227 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
229 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
230 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
232 { .value
= TARGET_EVENT_TRACE_CONFIG
, .name
= "trace-config" },
234 { .name
= NULL
, .value
= -1 }
237 static const Jim_Nvp nvp_target_state
[] = {
238 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
239 { .name
= "running", .value
= TARGET_RUNNING
},
240 { .name
= "halted", .value
= TARGET_HALTED
},
241 { .name
= "reset", .value
= TARGET_RESET
},
242 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
243 { .name
= NULL
, .value
= -1 },
246 static const Jim_Nvp nvp_target_debug_reason
[] = {
247 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
248 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
249 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
250 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
251 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
252 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
253 { .name
= "program-exit" , .value
= DBG_REASON_EXIT
},
254 { .name
= "exception-catch" , .value
= DBG_REASON_EXC_CATCH
},
255 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
256 { .name
= NULL
, .value
= -1 },
259 static const Jim_Nvp nvp_target_endian
[] = {
260 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
261 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
262 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
263 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
264 { .name
= NULL
, .value
= -1 },
267 static const Jim_Nvp nvp_reset_modes
[] = {
268 { .name
= "unknown", .value
= RESET_UNKNOWN
},
269 { .name
= "run" , .value
= RESET_RUN
},
270 { .name
= "halt" , .value
= RESET_HALT
},
271 { .name
= "init" , .value
= RESET_INIT
},
272 { .name
= NULL
, .value
= -1 },
275 const char *debug_reason_name(struct target
*t
)
279 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
280 t
->debug_reason
)->name
;
282 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
283 cp
= "(*BUG*unknown*BUG*)";
288 const char *target_state_name(struct target
*t
)
291 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
293 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
294 cp
= "(*BUG*unknown*BUG*)";
297 if (!target_was_examined(t
) && t
->defer_examine
)
298 cp
= "examine deferred";
303 const char *target_event_name(enum target_event event
)
306 cp
= Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
;
308 LOG_ERROR("Invalid target event: %d", (int)(event
));
309 cp
= "(*BUG*unknown*BUG*)";
314 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
317 cp
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
;
319 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
320 cp
= "(*BUG*unknown*BUG*)";
325 /* determine the number of the new target */
326 static int new_target_number(void)
331 /* number is 0 based */
335 if (x
< t
->target_number
)
336 x
= t
->target_number
;
342 /* read a uint64_t from a buffer in target memory endianness */
343 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
345 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
346 return le_to_h_u64(buffer
);
348 return be_to_h_u64(buffer
);
351 /* read a uint32_t from a buffer in target memory endianness */
352 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
354 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
355 return le_to_h_u32(buffer
);
357 return be_to_h_u32(buffer
);
360 /* read a uint24_t from a buffer in target memory endianness */
361 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
363 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
364 return le_to_h_u24(buffer
);
366 return be_to_h_u24(buffer
);
369 /* read a uint16_t from a buffer in target memory endianness */
370 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
372 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
373 return le_to_h_u16(buffer
);
375 return be_to_h_u16(buffer
);
378 /* read a uint8_t from a buffer in target memory endianness */
379 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
381 return *buffer
& 0x0ff;
384 /* write a uint64_t to a buffer in target memory endianness */
385 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
387 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
388 h_u64_to_le(buffer
, value
);
390 h_u64_to_be(buffer
, value
);
393 /* write a uint32_t to a buffer in target memory endianness */
394 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
396 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
397 h_u32_to_le(buffer
, value
);
399 h_u32_to_be(buffer
, value
);
402 /* write a uint24_t to a buffer in target memory endianness */
403 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
405 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
406 h_u24_to_le(buffer
, value
);
408 h_u24_to_be(buffer
, value
);
411 /* write a uint16_t to a buffer in target memory endianness */
412 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
414 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
415 h_u16_to_le(buffer
, value
);
417 h_u16_to_be(buffer
, value
);
420 /* write a uint8_t to a buffer in target memory endianness */
421 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
426 /* write a uint64_t array to a buffer in target memory endianness */
427 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
430 for (i
= 0; i
< count
; i
++)
431 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
434 /* write a uint32_t array to a buffer in target memory endianness */
435 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
438 for (i
= 0; i
< count
; i
++)
439 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
442 /* write a uint16_t array to a buffer in target memory endianness */
443 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
446 for (i
= 0; i
< count
; i
++)
447 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
450 /* write a uint64_t array to a buffer in target memory endianness */
451 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
454 for (i
= 0; i
< count
; i
++)
455 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
458 /* write a uint32_t array to a buffer in target memory endianness */
459 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
462 for (i
= 0; i
< count
; i
++)
463 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
466 /* write a uint16_t array to a buffer in target memory endianness */
467 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
470 for (i
= 0; i
< count
; i
++)
471 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
474 /* return a pointer to a configured target; id is name or number */
475 struct target
*get_target(const char *id
)
477 struct target
*target
;
479 /* try as tcltarget name */
480 for (target
= all_targets
; target
; target
= target
->next
) {
481 if (target_name(target
) == NULL
)
483 if (strcmp(id
, target_name(target
)) == 0)
487 /* It's OK to remove this fallback sometime after August 2010 or so */
489 /* no match, try as number */
491 if (parse_uint(id
, &num
) != ERROR_OK
)
494 for (target
= all_targets
; target
; target
= target
->next
) {
495 if (target
->target_number
== (int)num
) {
496 LOG_WARNING("use '%s' as target identifier, not '%u'",
497 target_name(target
), num
);
505 /* returns a pointer to the n-th configured target */
506 struct target
*get_target_by_num(int num
)
508 struct target
*target
= all_targets
;
511 if (target
->target_number
== num
)
513 target
= target
->next
;
519 struct target
*get_current_target(struct command_context
*cmd_ctx
)
521 struct target
*target
= get_current_target_or_null(cmd_ctx
);
523 if (target
== NULL
) {
524 LOG_ERROR("BUG: current_target out of bounds");
531 struct target
*get_current_target_or_null(struct command_context
*cmd_ctx
)
533 return cmd_ctx
->current_target_override
534 ? cmd_ctx
->current_target_override
535 : cmd_ctx
->current_target
;
538 int target_poll(struct target
*target
)
542 /* We can't poll until after examine */
543 if (!target_was_examined(target
)) {
544 /* Fail silently lest we pollute the log */
548 retval
= target
->type
->poll(target
);
549 if (retval
!= ERROR_OK
)
552 if (target
->halt_issued
) {
553 if (target
->state
== TARGET_HALTED
)
554 target
->halt_issued
= false;
556 int64_t t
= timeval_ms() - target
->halt_issued_time
;
557 if (t
> DEFAULT_HALT_TIMEOUT
) {
558 target
->halt_issued
= false;
559 LOG_INFO("Halt timed out, wake up GDB.");
560 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
568 int target_halt(struct target
*target
)
571 /* We can't poll until after examine */
572 if (!target_was_examined(target
)) {
573 LOG_ERROR("Target not examined yet");
577 retval
= target
->type
->halt(target
);
578 if (retval
!= ERROR_OK
)
581 target
->halt_issued
= true;
582 target
->halt_issued_time
= timeval_ms();
588 * Make the target (re)start executing using its saved execution
589 * context (possibly with some modifications).
591 * @param target Which target should start executing.
592 * @param current True to use the target's saved program counter instead
593 * of the address parameter
594 * @param address Optionally used as the program counter.
595 * @param handle_breakpoints True iff breakpoints at the resumption PC
596 * should be skipped. (For example, maybe execution was stopped by
597 * such a breakpoint, in which case it would be counterprodutive to
599 * @param debug_execution False if all working areas allocated by OpenOCD
600 * should be released and/or restored to their original contents.
601 * (This would for example be true to run some downloaded "helper"
602 * algorithm code, which resides in one such working buffer and uses
603 * another for data storage.)
605 * @todo Resolve the ambiguity about what the "debug_execution" flag
606 * signifies. For example, Target implementations don't agree on how
607 * it relates to invalidation of the register cache, or to whether
608 * breakpoints and watchpoints should be enabled. (It would seem wrong
609 * to enable breakpoints when running downloaded "helper" algorithms
610 * (debug_execution true), since the breakpoints would be set to match
611 * target firmware being debugged, not the helper algorithm.... and
612 * enabling them could cause such helpers to malfunction (for example,
613 * by overwriting data with a breakpoint instruction. On the other
614 * hand the infrastructure for running such helpers might use this
615 * procedure but rely on hardware breakpoint to detect termination.)
617 int target_resume(struct target
*target
, int current
, target_addr_t address
,
618 int handle_breakpoints
, int debug_execution
)
622 /* We can't poll until after examine */
623 if (!target_was_examined(target
)) {
624 LOG_ERROR("Target not examined yet");
628 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
630 /* note that resume *must* be asynchronous. The CPU can halt before
631 * we poll. The CPU can even halt at the current PC as a result of
632 * a software breakpoint being inserted by (a bug?) the application.
634 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
635 if (retval
!= ERROR_OK
)
638 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
643 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
648 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
649 if (n
->name
== NULL
) {
650 LOG_ERROR("invalid reset mode");
654 struct target
*target
;
655 for (target
= all_targets
; target
; target
= target
->next
)
656 target_call_reset_callbacks(target
, reset_mode
);
658 /* disable polling during reset to make reset event scripts
659 * more predictable, i.e. dr/irscan & pathmove in events will
660 * not have JTAG operations injected into the middle of a sequence.
662 bool save_poll
= jtag_poll_get_enabled();
664 jtag_poll_set_enabled(false);
666 sprintf(buf
, "ocd_process_reset %s", n
->name
);
667 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
669 jtag_poll_set_enabled(save_poll
);
671 if (retval
!= JIM_OK
) {
672 Jim_MakeErrorMessage(cmd_ctx
->interp
);
673 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
677 /* We want any events to be processed before the prompt */
678 retval
= target_call_timer_callbacks_now();
680 for (target
= all_targets
; target
; target
= target
->next
) {
681 target
->type
->check_reset(target
);
682 target
->running_alg
= false;
688 static int identity_virt2phys(struct target
*target
,
689 target_addr_t
virtual, target_addr_t
*physical
)
695 static int no_mmu(struct target
*target
, int *enabled
)
701 static int default_examine(struct target
*target
)
703 target_set_examined(target
);
707 /* no check by default */
708 static int default_check_reset(struct target
*target
)
713 int target_examine_one(struct target
*target
)
715 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
717 int retval
= target
->type
->examine(target
);
718 if (retval
!= ERROR_OK
)
721 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
726 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
728 struct target
*target
= priv
;
730 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
733 jtag_unregister_event_callback(jtag_enable_callback
, target
);
735 return target_examine_one(target
);
738 /* Targets that correctly implement init + examine, i.e.
739 * no communication with target during init:
743 int target_examine(void)
745 int retval
= ERROR_OK
;
746 struct target
*target
;
748 for (target
= all_targets
; target
; target
= target
->next
) {
749 /* defer examination, but don't skip it */
750 if (!target
->tap
->enabled
) {
751 jtag_register_event_callback(jtag_enable_callback
,
756 if (target
->defer_examine
)
759 retval
= target_examine_one(target
);
760 if (retval
!= ERROR_OK
)
766 const char *target_type_name(struct target
*target
)
768 return target
->type
->name
;
771 static int target_soft_reset_halt(struct target
*target
)
773 if (!target_was_examined(target
)) {
774 LOG_ERROR("Target not examined yet");
777 if (!target
->type
->soft_reset_halt
) {
778 LOG_ERROR("Target %s does not support soft_reset_halt",
779 target_name(target
));
782 return target
->type
->soft_reset_halt(target
);
786 * Downloads a target-specific native code algorithm to the target,
787 * and executes it. * Note that some targets may need to set up, enable,
788 * and tear down a breakpoint (hard or * soft) to detect algorithm
789 * termination, while others may support lower overhead schemes where
790 * soft breakpoints embedded in the algorithm automatically terminate the
793 * @param target used to run the algorithm
794 * @param arch_info target-specific description of the algorithm.
796 int target_run_algorithm(struct target
*target
,
797 int num_mem_params
, struct mem_param
*mem_params
,
798 int num_reg_params
, struct reg_param
*reg_param
,
799 uint32_t entry_point
, uint32_t exit_point
,
800 int timeout_ms
, void *arch_info
)
802 int retval
= ERROR_FAIL
;
804 if (!target_was_examined(target
)) {
805 LOG_ERROR("Target not examined yet");
808 if (!target
->type
->run_algorithm
) {
809 LOG_ERROR("Target type '%s' does not support %s",
810 target_type_name(target
), __func__
);
814 target
->running_alg
= true;
815 retval
= target
->type
->run_algorithm(target
,
816 num_mem_params
, mem_params
,
817 num_reg_params
, reg_param
,
818 entry_point
, exit_point
, timeout_ms
, arch_info
);
819 target
->running_alg
= false;
826 * Executes a target-specific native code algorithm and leaves it running.
828 * @param target used to run the algorithm
829 * @param arch_info target-specific description of the algorithm.
831 int target_start_algorithm(struct target
*target
,
832 int num_mem_params
, struct mem_param
*mem_params
,
833 int num_reg_params
, struct reg_param
*reg_params
,
834 uint32_t entry_point
, uint32_t exit_point
,
837 int retval
= ERROR_FAIL
;
839 if (!target_was_examined(target
)) {
840 LOG_ERROR("Target not examined yet");
843 if (!target
->type
->start_algorithm
) {
844 LOG_ERROR("Target type '%s' does not support %s",
845 target_type_name(target
), __func__
);
848 if (target
->running_alg
) {
849 LOG_ERROR("Target is already running an algorithm");
853 target
->running_alg
= true;
854 retval
= target
->type
->start_algorithm(target
,
855 num_mem_params
, mem_params
,
856 num_reg_params
, reg_params
,
857 entry_point
, exit_point
, arch_info
);
864 * Waits for an algorithm started with target_start_algorithm() to complete.
866 * @param target used to run the algorithm
867 * @param arch_info target-specific description of the algorithm.
869 int target_wait_algorithm(struct target
*target
,
870 int num_mem_params
, struct mem_param
*mem_params
,
871 int num_reg_params
, struct reg_param
*reg_params
,
872 uint32_t exit_point
, int timeout_ms
,
875 int retval
= ERROR_FAIL
;
877 if (!target
->type
->wait_algorithm
) {
878 LOG_ERROR("Target type '%s' does not support %s",
879 target_type_name(target
), __func__
);
882 if (!target
->running_alg
) {
883 LOG_ERROR("Target is not running an algorithm");
887 retval
= target
->type
->wait_algorithm(target
,
888 num_mem_params
, mem_params
,
889 num_reg_params
, reg_params
,
890 exit_point
, timeout_ms
, arch_info
);
891 if (retval
!= ERROR_TARGET_TIMEOUT
)
892 target
->running_alg
= false;
899 * Streams data to a circular buffer on target intended for consumption by code
900 * running asynchronously on target.
902 * This is intended for applications where target-specific native code runs
903 * on the target, receives data from the circular buffer, does something with
904 * it (most likely writing it to a flash memory), and advances the circular
907 * This assumes that the helper algorithm has already been loaded to the target,
908 * but has not been started yet. Given memory and register parameters are passed
911 * The buffer is defined by (buffer_start, buffer_size) arguments and has the
914 * [buffer_start + 0, buffer_start + 4):
915 * Write Pointer address (aka head). Written and updated by this
916 * routine when new data is written to the circular buffer.
917 * [buffer_start + 4, buffer_start + 8):
918 * Read Pointer address (aka tail). Updated by code running on the
919 * target after it consumes data.
920 * [buffer_start + 8, buffer_start + buffer_size):
921 * Circular buffer contents.
923 * See contrib/loaders/flash/stm32f1x.S for an example.
925 * @param target used to run the algorithm
926 * @param buffer address on the host where data to be sent is located
927 * @param count number of blocks to send
928 * @param block_size size in bytes of each block
929 * @param num_mem_params count of memory-based params to pass to algorithm
930 * @param mem_params memory-based params to pass to algorithm
931 * @param num_reg_params count of register-based params to pass to algorithm
932 * @param reg_params memory-based params to pass to algorithm
933 * @param buffer_start address on the target of the circular buffer structure
934 * @param buffer_size size of the circular buffer structure
935 * @param entry_point address on the target to execute to start the algorithm
936 * @param exit_point address at which to set a breakpoint to catch the
937 * end of the algorithm; can be 0 if target triggers a breakpoint itself
940 int target_run_flash_async_algorithm(struct target
*target
,
941 const uint8_t *buffer
, uint32_t count
, int block_size
,
942 int num_mem_params
, struct mem_param
*mem_params
,
943 int num_reg_params
, struct reg_param
*reg_params
,
944 uint32_t buffer_start
, uint32_t buffer_size
,
945 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
950 const uint8_t *buffer_orig
= buffer
;
952 /* Set up working area. First word is write pointer, second word is read pointer,
953 * rest is fifo data area. */
954 uint32_t wp_addr
= buffer_start
;
955 uint32_t rp_addr
= buffer_start
+ 4;
956 uint32_t fifo_start_addr
= buffer_start
+ 8;
957 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
959 uint32_t wp
= fifo_start_addr
;
960 uint32_t rp
= fifo_start_addr
;
962 /* validate block_size is 2^n */
963 assert(!block_size
|| !(block_size
& (block_size
- 1)));
965 retval
= target_write_u32(target
, wp_addr
, wp
);
966 if (retval
!= ERROR_OK
)
968 retval
= target_write_u32(target
, rp_addr
, rp
);
969 if (retval
!= ERROR_OK
)
972 /* Start up algorithm on target and let it idle while writing the first chunk */
973 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
974 num_reg_params
, reg_params
,
979 if (retval
!= ERROR_OK
) {
980 LOG_ERROR("error starting target flash write algorithm");
986 retval
= target_read_u32(target
, rp_addr
, &rp
);
987 if (retval
!= ERROR_OK
) {
988 LOG_ERROR("failed to get read pointer");
992 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
993 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
996 LOG_ERROR("flash write algorithm aborted by target");
997 retval
= ERROR_FLASH_OPERATION_FAILED
;
1001 if (((rp
- fifo_start_addr
) & (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
1002 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
1006 /* Count the number of bytes available in the fifo without
1007 * crossing the wrap around. Make sure to not fill it completely,
1008 * because that would make wp == rp and that's the empty condition. */
1009 uint32_t thisrun_bytes
;
1011 thisrun_bytes
= rp
- wp
- block_size
;
1012 else if (rp
> fifo_start_addr
)
1013 thisrun_bytes
= fifo_end_addr
- wp
;
1015 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
1017 if (thisrun_bytes
== 0) {
1018 /* Throttle polling a bit if transfer is (much) faster than flash
1019 * programming. The exact delay shouldn't matter as long as it's
1020 * less than buffer size / flash speed. This is very unlikely to
1021 * run when using high latency connections such as USB. */
1024 /* to stop an infinite loop on some targets check and increment a timeout
1025 * this issue was observed on a stellaris using the new ICDI interface */
1026 if (timeout
++ >= 500) {
1027 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1028 return ERROR_FLASH_OPERATION_FAILED
;
1033 /* reset our timeout */
1036 /* Limit to the amount of data we actually want to write */
1037 if (thisrun_bytes
> count
* block_size
)
1038 thisrun_bytes
= count
* block_size
;
1040 /* Write data to fifo */
1041 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
1042 if (retval
!= ERROR_OK
)
1045 /* Update counters and wrap write pointer */
1046 buffer
+= thisrun_bytes
;
1047 count
-= thisrun_bytes
/ block_size
;
1048 wp
+= thisrun_bytes
;
1049 if (wp
>= fifo_end_addr
)
1050 wp
= fifo_start_addr
;
1052 /* Store updated write pointer to target */
1053 retval
= target_write_u32(target
, wp_addr
, wp
);
1054 if (retval
!= ERROR_OK
)
1057 /* Avoid GDB timeouts */
1061 if (retval
!= ERROR_OK
) {
1062 /* abort flash write algorithm on target */
1063 target_write_u32(target
, wp_addr
, 0);
1066 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1067 num_reg_params
, reg_params
,
1072 if (retval2
!= ERROR_OK
) {
1073 LOG_ERROR("error waiting for target flash write algorithm");
1077 if (retval
== ERROR_OK
) {
1078 /* check if algorithm set rp = 0 after fifo writer loop finished */
1079 retval
= target_read_u32(target
, rp_addr
, &rp
);
1080 if (retval
== ERROR_OK
&& rp
== 0) {
1081 LOG_ERROR("flash write algorithm aborted by target");
1082 retval
= ERROR_FLASH_OPERATION_FAILED
;
1089 int target_read_memory(struct target
*target
,
1090 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1092 if (!target_was_examined(target
)) {
1093 LOG_ERROR("Target not examined yet");
1096 if (!target
->type
->read_memory
) {
1097 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1100 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1103 int target_read_phys_memory(struct target
*target
,
1104 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1106 if (!target_was_examined(target
)) {
1107 LOG_ERROR("Target not examined yet");
1110 if (!target
->type
->read_phys_memory
) {
1111 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1114 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1117 int target_write_memory(struct target
*target
,
1118 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1120 if (!target_was_examined(target
)) {
1121 LOG_ERROR("Target not examined yet");
1124 if (!target
->type
->write_memory
) {
1125 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1128 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1131 int target_write_phys_memory(struct target
*target
,
1132 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1134 if (!target_was_examined(target
)) {
1135 LOG_ERROR("Target not examined yet");
1138 if (!target
->type
->write_phys_memory
) {
1139 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1142 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1145 int target_add_breakpoint(struct target
*target
,
1146 struct breakpoint
*breakpoint
)
1148 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1149 LOG_WARNING("target %s is not halted (add breakpoint)", target_name(target
));
1150 return ERROR_TARGET_NOT_HALTED
;
1152 return target
->type
->add_breakpoint(target
, breakpoint
);
1155 int target_add_context_breakpoint(struct target
*target
,
1156 struct breakpoint
*breakpoint
)
1158 if (target
->state
!= TARGET_HALTED
) {
1159 LOG_WARNING("target %s is not halted (add context breakpoint)", target_name(target
));
1160 return ERROR_TARGET_NOT_HALTED
;
1162 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1165 int target_add_hybrid_breakpoint(struct target
*target
,
1166 struct breakpoint
*breakpoint
)
1168 if (target
->state
!= TARGET_HALTED
) {
1169 LOG_WARNING("target %s is not halted (add hybrid breakpoint)", target_name(target
));
1170 return ERROR_TARGET_NOT_HALTED
;
1172 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1175 int target_remove_breakpoint(struct target
*target
,
1176 struct breakpoint
*breakpoint
)
1178 return target
->type
->remove_breakpoint(target
, breakpoint
);
1181 int target_add_watchpoint(struct target
*target
,
1182 struct watchpoint
*watchpoint
)
1184 if (target
->state
!= TARGET_HALTED
) {
1185 LOG_WARNING("target %s is not halted (add watchpoint)", target_name(target
));
1186 return ERROR_TARGET_NOT_HALTED
;
1188 return target
->type
->add_watchpoint(target
, watchpoint
);
1190 int target_remove_watchpoint(struct target
*target
,
1191 struct watchpoint
*watchpoint
)
1193 return target
->type
->remove_watchpoint(target
, watchpoint
);
1195 int target_hit_watchpoint(struct target
*target
,
1196 struct watchpoint
**hit_watchpoint
)
1198 if (target
->state
!= TARGET_HALTED
) {
1199 LOG_WARNING("target %s is not halted (hit watchpoint)", target
->cmd_name
);
1200 return ERROR_TARGET_NOT_HALTED
;
1203 if (target
->type
->hit_watchpoint
== NULL
) {
1204 /* For backward compatible, if hit_watchpoint is not implemented,
1205 * return ERROR_FAIL such that gdb_server will not take the nonsense
1210 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1213 const char *target_get_gdb_arch(struct target
*target
)
1215 if (target
->type
->get_gdb_arch
== NULL
)
1217 return target
->type
->get_gdb_arch(target
);
1220 int target_get_gdb_reg_list(struct target
*target
,
1221 struct reg
**reg_list
[], int *reg_list_size
,
1222 enum target_register_class reg_class
)
1224 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1227 bool target_supports_gdb_connection(struct target
*target
)
1230 * based on current code, we can simply exclude all the targets that
1231 * don't provide get_gdb_reg_list; this could change with new targets.
1233 return !!target
->type
->get_gdb_reg_list
;
1236 int target_step(struct target
*target
,
1237 int current
, target_addr_t address
, int handle_breakpoints
)
1239 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1242 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1244 if (target
->state
!= TARGET_HALTED
) {
1245 LOG_WARNING("target %s is not halted (gdb fileio)", target
->cmd_name
);
1246 return ERROR_TARGET_NOT_HALTED
;
1248 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1251 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1253 if (target
->state
!= TARGET_HALTED
) {
1254 LOG_WARNING("target %s is not halted (gdb fileio end)", target
->cmd_name
);
1255 return ERROR_TARGET_NOT_HALTED
;
1257 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1260 int target_profiling(struct target
*target
, uint32_t *samples
,
1261 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1263 if (target
->state
!= TARGET_HALTED
) {
1264 LOG_WARNING("target %s is not halted (profiling)", target
->cmd_name
);
1265 return ERROR_TARGET_NOT_HALTED
;
1267 return target
->type
->profiling(target
, samples
, max_num_samples
,
1268 num_samples
, seconds
);
1272 * Reset the @c examined flag for the given target.
1273 * Pure paranoia -- targets are zeroed on allocation.
1275 static void target_reset_examined(struct target
*target
)
1277 target
->examined
= false;
1280 static int handle_target(void *priv
);
1282 static int target_init_one(struct command_context
*cmd_ctx
,
1283 struct target
*target
)
1285 target_reset_examined(target
);
1287 struct target_type
*type
= target
->type
;
1288 if (type
->examine
== NULL
)
1289 type
->examine
= default_examine
;
1291 if (type
->check_reset
== NULL
)
1292 type
->check_reset
= default_check_reset
;
1294 assert(type
->init_target
!= NULL
);
1296 int retval
= type
->init_target(cmd_ctx
, target
);
1297 if (ERROR_OK
!= retval
) {
1298 LOG_ERROR("target '%s' init failed", target_name(target
));
1302 /* Sanity-check MMU support ... stub in what we must, to help
1303 * implement it in stages, but warn if we need to do so.
1306 if (type
->virt2phys
== NULL
) {
1307 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1308 type
->virt2phys
= identity_virt2phys
;
1311 /* Make sure no-MMU targets all behave the same: make no
1312 * distinction between physical and virtual addresses, and
1313 * ensure that virt2phys() is always an identity mapping.
1315 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1316 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1319 type
->write_phys_memory
= type
->write_memory
;
1320 type
->read_phys_memory
= type
->read_memory
;
1321 type
->virt2phys
= identity_virt2phys
;
1324 if (target
->type
->read_buffer
== NULL
)
1325 target
->type
->read_buffer
= target_read_buffer_default
;
1327 if (target
->type
->write_buffer
== NULL
)
1328 target
->type
->write_buffer
= target_write_buffer_default
;
1330 if (target
->type
->get_gdb_fileio_info
== NULL
)
1331 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1333 if (target
->type
->gdb_fileio_end
== NULL
)
1334 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1336 if (target
->type
->profiling
== NULL
)
1337 target
->type
->profiling
= target_profiling_default
;
1342 static int target_init(struct command_context
*cmd_ctx
)
1344 struct target
*target
;
1347 for (target
= all_targets
; target
; target
= target
->next
) {
1348 retval
= target_init_one(cmd_ctx
, target
);
1349 if (ERROR_OK
!= retval
)
1356 retval
= target_register_user_commands(cmd_ctx
);
1357 if (ERROR_OK
!= retval
)
1360 retval
= target_register_timer_callback(&handle_target
,
1361 polling_interval
, TARGET_TIMER_TYPE_PERIODIC
, cmd_ctx
->interp
);
1362 if (ERROR_OK
!= retval
)
1368 COMMAND_HANDLER(handle_target_init_command
)
1373 return ERROR_COMMAND_SYNTAX_ERROR
;
1375 static bool target_initialized
;
1376 if (target_initialized
) {
1377 LOG_INFO("'target init' has already been called");
1380 target_initialized
= true;
1382 retval
= command_run_line(CMD_CTX
, "init_targets");
1383 if (ERROR_OK
!= retval
)
1386 retval
= command_run_line(CMD_CTX
, "init_target_events");
1387 if (ERROR_OK
!= retval
)
1390 retval
= command_run_line(CMD_CTX
, "init_board");
1391 if (ERROR_OK
!= retval
)
1394 LOG_DEBUG("Initializing targets...");
1395 return target_init(CMD_CTX
);
1398 int target_register_event_callback(int (*callback
)(struct target
*target
,
1399 enum target_event event
, void *priv
), void *priv
)
1401 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1403 if (callback
== NULL
)
1404 return ERROR_COMMAND_SYNTAX_ERROR
;
1407 while ((*callbacks_p
)->next
)
1408 callbacks_p
= &((*callbacks_p
)->next
);
1409 callbacks_p
= &((*callbacks_p
)->next
);
1412 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1413 (*callbacks_p
)->callback
= callback
;
1414 (*callbacks_p
)->priv
= priv
;
1415 (*callbacks_p
)->next
= NULL
;
1420 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1421 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1423 struct target_reset_callback
*entry
;
1425 if (callback
== NULL
)
1426 return ERROR_COMMAND_SYNTAX_ERROR
;
1428 entry
= malloc(sizeof(struct target_reset_callback
));
1429 if (entry
== NULL
) {
1430 LOG_ERROR("error allocating buffer for reset callback entry");
1431 return ERROR_COMMAND_SYNTAX_ERROR
;
1434 entry
->callback
= callback
;
1436 list_add(&entry
->list
, &target_reset_callback_list
);
1442 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1443 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1445 struct target_trace_callback
*entry
;
1447 if (callback
== NULL
)
1448 return ERROR_COMMAND_SYNTAX_ERROR
;
1450 entry
= malloc(sizeof(struct target_trace_callback
));
1451 if (entry
== NULL
) {
1452 LOG_ERROR("error allocating buffer for trace callback entry");
1453 return ERROR_COMMAND_SYNTAX_ERROR
;
1456 entry
->callback
= callback
;
1458 list_add(&entry
->list
, &target_trace_callback_list
);
1464 int target_register_timer_callback(int (*callback
)(void *priv
),
1465 unsigned int time_ms
, enum target_timer_type type
, void *priv
)
1467 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1469 if (callback
== NULL
)
1470 return ERROR_COMMAND_SYNTAX_ERROR
;
1473 while ((*callbacks_p
)->next
)
1474 callbacks_p
= &((*callbacks_p
)->next
);
1475 callbacks_p
= &((*callbacks_p
)->next
);
1478 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1479 (*callbacks_p
)->callback
= callback
;
1480 (*callbacks_p
)->type
= type
;
1481 (*callbacks_p
)->time_ms
= time_ms
;
1482 (*callbacks_p
)->removed
= false;
1484 gettimeofday(&(*callbacks_p
)->when
, NULL
);
1485 timeval_add_time(&(*callbacks_p
)->when
, 0, time_ms
* 1000);
1487 (*callbacks_p
)->priv
= priv
;
1488 (*callbacks_p
)->next
= NULL
;
1493 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1494 enum target_event event
, void *priv
), void *priv
)
1496 struct target_event_callback
**p
= &target_event_callbacks
;
1497 struct target_event_callback
*c
= target_event_callbacks
;
1499 if (callback
== NULL
)
1500 return ERROR_COMMAND_SYNTAX_ERROR
;
1503 struct target_event_callback
*next
= c
->next
;
1504 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1516 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1517 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1519 struct target_reset_callback
*entry
;
1521 if (callback
== NULL
)
1522 return ERROR_COMMAND_SYNTAX_ERROR
;
1524 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1525 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1526 list_del(&entry
->list
);
1535 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1536 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1538 struct target_trace_callback
*entry
;
1540 if (callback
== NULL
)
1541 return ERROR_COMMAND_SYNTAX_ERROR
;
1543 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1544 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1545 list_del(&entry
->list
);
1554 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1556 if (callback
== NULL
)
1557 return ERROR_COMMAND_SYNTAX_ERROR
;
1559 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1561 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1570 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1572 struct target_event_callback
*callback
= target_event_callbacks
;
1573 struct target_event_callback
*next_callback
;
1575 if (event
== TARGET_EVENT_HALTED
) {
1576 /* execute early halted first */
1577 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1580 LOG_DEBUG("target event %i (%s)", event
,
1581 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1583 target_handle_event(target
, event
);
1586 next_callback
= callback
->next
;
1587 callback
->callback(target
, event
, callback
->priv
);
1588 callback
= next_callback
;
1594 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1596 struct target_reset_callback
*callback
;
1598 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1599 Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1601 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1602 callback
->callback(target
, reset_mode
, callback
->priv
);
1607 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1609 struct target_trace_callback
*callback
;
1611 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1612 callback
->callback(target
, len
, data
, callback
->priv
);
1617 static int target_timer_callback_periodic_restart(
1618 struct target_timer_callback
*cb
, struct timeval
*now
)
1621 timeval_add_time(&cb
->when
, 0, cb
->time_ms
* 1000L);
1625 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1626 struct timeval
*now
)
1628 cb
->callback(cb
->priv
);
1630 if (cb
->type
== TARGET_TIMER_TYPE_PERIODIC
)
1631 return target_timer_callback_periodic_restart(cb
, now
);
1633 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1636 static int target_call_timer_callbacks_check_time(int checktime
)
1638 static bool callback_processing
;
1640 /* Do not allow nesting */
1641 if (callback_processing
)
1644 callback_processing
= true;
1649 gettimeofday(&now
, NULL
);
1651 /* Store an address of the place containing a pointer to the
1652 * next item; initially, that's a standalone "root of the
1653 * list" variable. */
1654 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1656 if ((*callback
)->removed
) {
1657 struct target_timer_callback
*p
= *callback
;
1658 *callback
= (*callback
)->next
;
1663 bool call_it
= (*callback
)->callback
&&
1664 ((!checktime
&& (*callback
)->type
== TARGET_TIMER_TYPE_PERIODIC
) ||
1665 timeval_compare(&now
, &(*callback
)->when
) >= 0);
1668 target_call_timer_callback(*callback
, &now
);
1670 callback
= &(*callback
)->next
;
1673 callback_processing
= false;
1677 int target_call_timer_callbacks(void)
1679 return target_call_timer_callbacks_check_time(1);
1682 /* invoke periodic callbacks immediately */
1683 int target_call_timer_callbacks_now(void)
1685 return target_call_timer_callbacks_check_time(0);
1688 /* Prints the working area layout for debug purposes */
1689 static void print_wa_layout(struct target
*target
)
1691 struct working_area
*c
= target
->working_areas
;
1694 LOG_DEBUG("%c%c " TARGET_ADDR_FMT
"-" TARGET_ADDR_FMT
" (%" PRIu32
" bytes)",
1695 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1696 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1701 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1702 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1704 assert(area
->free
); /* Shouldn't split an allocated area */
1705 assert(size
<= area
->size
); /* Caller should guarantee this */
1707 /* Split only if not already the right size */
1708 if (size
< area
->size
) {
1709 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1714 new_wa
->next
= area
->next
;
1715 new_wa
->size
= area
->size
- size
;
1716 new_wa
->address
= area
->address
+ size
;
1717 new_wa
->backup
= NULL
;
1718 new_wa
->user
= NULL
;
1719 new_wa
->free
= true;
1721 area
->next
= new_wa
;
1724 /* If backup memory was allocated to this area, it has the wrong size
1725 * now so free it and it will be reallocated if/when needed */
1728 area
->backup
= NULL
;
1733 /* Merge all adjacent free areas into one */
1734 static void target_merge_working_areas(struct target
*target
)
1736 struct working_area
*c
= target
->working_areas
;
1738 while (c
&& c
->next
) {
1739 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1741 /* Find two adjacent free areas */
1742 if (c
->free
&& c
->next
->free
) {
1743 /* Merge the last into the first */
1744 c
->size
+= c
->next
->size
;
1746 /* Remove the last */
1747 struct working_area
*to_be_freed
= c
->next
;
1748 c
->next
= c
->next
->next
;
1749 if (to_be_freed
->backup
)
1750 free(to_be_freed
->backup
);
1753 /* If backup memory was allocated to the remaining area, it's has
1754 * the wrong size now */
1765 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1767 /* Reevaluate working area address based on MMU state*/
1768 if (target
->working_areas
== NULL
) {
1772 retval
= target
->type
->mmu(target
, &enabled
);
1773 if (retval
!= ERROR_OK
)
1777 if (target
->working_area_phys_spec
) {
1778 LOG_DEBUG("MMU disabled, using physical "
1779 "address for working memory " TARGET_ADDR_FMT
,
1780 target
->working_area_phys
);
1781 target
->working_area
= target
->working_area_phys
;
1783 LOG_ERROR("No working memory available. "
1784 "Specify -work-area-phys to target.");
1785 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1788 if (target
->working_area_virt_spec
) {
1789 LOG_DEBUG("MMU enabled, using virtual "
1790 "address for working memory " TARGET_ADDR_FMT
,
1791 target
->working_area_virt
);
1792 target
->working_area
= target
->working_area_virt
;
1794 LOG_ERROR("No working memory available. "
1795 "Specify -work-area-virt to target.");
1796 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1800 /* Set up initial working area on first call */
1801 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1803 new_wa
->next
= NULL
;
1804 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1805 new_wa
->address
= target
->working_area
;
1806 new_wa
->backup
= NULL
;
1807 new_wa
->user
= NULL
;
1808 new_wa
->free
= true;
1811 target
->working_areas
= new_wa
;
1814 /* only allocate multiples of 4 byte */
1816 size
= (size
+ 3) & (~3UL);
1818 struct working_area
*c
= target
->working_areas
;
1820 /* Find the first large enough working area */
1822 if (c
->free
&& c
->size
>= size
)
1828 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1830 /* Split the working area into the requested size */
1831 target_split_working_area(c
, size
);
1833 LOG_DEBUG("allocated new working area of %" PRIu32
" bytes at address " TARGET_ADDR_FMT
,
1836 if (target
->backup_working_area
) {
1837 if (c
->backup
== NULL
) {
1838 c
->backup
= malloc(c
->size
);
1839 if (c
->backup
== NULL
)
1843 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1844 if (retval
!= ERROR_OK
)
1848 /* mark as used, and return the new (reused) area */
1855 print_wa_layout(target
);
1860 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1864 retval
= target_alloc_working_area_try(target
, size
, area
);
1865 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1866 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1871 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1873 int retval
= ERROR_OK
;
1875 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1876 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1877 if (retval
!= ERROR_OK
)
1878 LOG_ERROR("failed to restore %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1879 area
->size
, area
->address
);
1885 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1886 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1888 int retval
= ERROR_OK
;
1894 retval
= target_restore_working_area(target
, area
);
1895 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1896 if (retval
!= ERROR_OK
)
1902 LOG_DEBUG("freed %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1903 area
->size
, area
->address
);
1905 /* mark user pointer invalid */
1906 /* TODO: Is this really safe? It points to some previous caller's memory.
1907 * How could we know that the area pointer is still in that place and not
1908 * some other vital data? What's the purpose of this, anyway? */
1912 target_merge_working_areas(target
);
1914 print_wa_layout(target
);
1919 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1921 return target_free_working_area_restore(target
, area
, 1);
1924 /* free resources and restore memory, if restoring memory fails,
1925 * free up resources anyway
1927 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1929 struct working_area
*c
= target
->working_areas
;
1931 LOG_DEBUG("freeing all working areas");
1933 /* Loop through all areas, restoring the allocated ones and marking them as free */
1937 target_restore_working_area(target
, c
);
1939 *c
->user
= NULL
; /* Same as above */
1945 /* Run a merge pass to combine all areas into one */
1946 target_merge_working_areas(target
);
1948 print_wa_layout(target
);
1951 void target_free_all_working_areas(struct target
*target
)
1953 target_free_all_working_areas_restore(target
, 1);
1955 /* Now we have none or only one working area marked as free */
1956 if (target
->working_areas
) {
1957 /* Free the last one to allow on-the-fly moving and resizing */
1958 free(target
->working_areas
->backup
);
1959 free(target
->working_areas
);
1960 target
->working_areas
= NULL
;
1964 /* Find the largest number of bytes that can be allocated */
1965 uint32_t target_get_working_area_avail(struct target
*target
)
1967 struct working_area
*c
= target
->working_areas
;
1968 uint32_t max_size
= 0;
1971 return target
->working_area_size
;
1974 if (c
->free
&& max_size
< c
->size
)
1983 static void target_destroy(struct target
*target
)
1985 if (target
->type
->deinit_target
)
1986 target
->type
->deinit_target(target
);
1988 if (target
->semihosting
)
1989 free(target
->semihosting
);
1991 jtag_unregister_event_callback(jtag_enable_callback
, target
);
1993 struct target_event_action
*teap
= target
->event_action
;
1995 struct target_event_action
*next
= teap
->next
;
1996 Jim_DecrRefCount(teap
->interp
, teap
->body
);
2001 target_free_all_working_areas(target
);
2003 /* release the targets SMP list */
2005 struct target_list
*head
= target
->head
;
2006 while (head
!= NULL
) {
2007 struct target_list
*pos
= head
->next
;
2008 head
->target
->smp
= 0;
2015 free(target
->gdb_port_override
);
2017 free(target
->trace_info
);
2018 free(target
->fileio_info
);
2019 free(target
->cmd_name
);
2023 void target_quit(void)
2025 struct target_event_callback
*pe
= target_event_callbacks
;
2027 struct target_event_callback
*t
= pe
->next
;
2031 target_event_callbacks
= NULL
;
2033 struct target_timer_callback
*pt
= target_timer_callbacks
;
2035 struct target_timer_callback
*t
= pt
->next
;
2039 target_timer_callbacks
= NULL
;
2041 for (struct target
*target
= all_targets
; target
;) {
2045 target_destroy(target
);
2052 int target_arch_state(struct target
*target
)
2055 if (target
== NULL
) {
2056 LOG_WARNING("No target has been configured");
2060 if (target
->state
!= TARGET_HALTED
)
2063 retval
= target
->type
->arch_state(target
);
2067 static int target_get_gdb_fileio_info_default(struct target
*target
,
2068 struct gdb_fileio_info
*fileio_info
)
2070 /* If target does not support semi-hosting function, target
2071 has no need to provide .get_gdb_fileio_info callback.
2072 It just return ERROR_FAIL and gdb_server will return "Txx"
2073 as target halted every time. */
2077 static int target_gdb_fileio_end_default(struct target
*target
,
2078 int retcode
, int fileio_errno
, bool ctrl_c
)
2083 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
2084 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
2086 struct timeval timeout
, now
;
2088 gettimeofday(&timeout
, NULL
);
2089 timeval_add_time(&timeout
, seconds
, 0);
2091 LOG_INFO("Starting profiling. Halting and resuming the"
2092 " target as often as we can...");
2094 uint32_t sample_count
= 0;
2095 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2096 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2098 int retval
= ERROR_OK
;
2100 target_poll(target
);
2101 if (target
->state
== TARGET_HALTED
) {
2102 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
2103 samples
[sample_count
++] = t
;
2104 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2105 retval
= target_resume(target
, 1, 0, 0, 0);
2106 target_poll(target
);
2107 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2108 } else if (target
->state
== TARGET_RUNNING
) {
2109 /* We want to quickly sample the PC. */
2110 retval
= target_halt(target
);
2112 LOG_INFO("Target not halted or running");
2117 if (retval
!= ERROR_OK
)
2120 gettimeofday(&now
, NULL
);
2121 if ((sample_count
>= max_num_samples
) || timeval_compare(&now
, &timeout
) >= 0) {
2122 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2127 *num_samples
= sample_count
;
2131 /* Single aligned words are guaranteed to use 16 or 32 bit access
2132 * mode respectively, otherwise data is handled as quickly as
2135 int target_write_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, const uint8_t *buffer
)
2137 LOG_DEBUG("writing buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2140 if (!target_was_examined(target
)) {
2141 LOG_ERROR("Target not examined yet");
2148 if ((address
+ size
- 1) < address
) {
2149 /* GDB can request this when e.g. PC is 0xfffffffc */
2150 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2156 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2159 static int target_write_buffer_default(struct target
*target
,
2160 target_addr_t address
, uint32_t count
, const uint8_t *buffer
)
2164 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2165 * will have something to do with the size we leave to it. */
2166 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2167 if (address
& size
) {
2168 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2169 if (retval
!= ERROR_OK
)
2177 /* Write the data with as large access size as possible. */
2178 for (; size
> 0; size
/= 2) {
2179 uint32_t aligned
= count
- count
% size
;
2181 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2182 if (retval
!= ERROR_OK
)
2193 /* Single aligned words are guaranteed to use 16 or 32 bit access
2194 * mode respectively, otherwise data is handled as quickly as
2197 int target_read_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
2199 LOG_DEBUG("reading buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2202 if (!target_was_examined(target
)) {
2203 LOG_ERROR("Target not examined yet");
2210 if ((address
+ size
- 1) < address
) {
2211 /* GDB can request this when e.g. PC is 0xfffffffc */
2212 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2218 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2221 static int target_read_buffer_default(struct target
*target
, target_addr_t address
, uint32_t count
, uint8_t *buffer
)
2225 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2226 * will have something to do with the size we leave to it. */
2227 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2228 if (address
& size
) {
2229 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2230 if (retval
!= ERROR_OK
)
2238 /* Read the data with as large access size as possible. */
2239 for (; size
> 0; size
/= 2) {
2240 uint32_t aligned
= count
- count
% size
;
2242 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2243 if (retval
!= ERROR_OK
)
2254 int target_checksum_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* crc
)
2259 uint32_t checksum
= 0;
2260 if (!target_was_examined(target
)) {
2261 LOG_ERROR("Target not examined yet");
2265 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2266 if (retval
!= ERROR_OK
) {
2267 buffer
= malloc(size
);
2268 if (buffer
== NULL
) {
2269 LOG_ERROR("error allocating buffer for section (%" PRId32
" bytes)", size
);
2270 return ERROR_COMMAND_SYNTAX_ERROR
;
2272 retval
= target_read_buffer(target
, address
, size
, buffer
);
2273 if (retval
!= ERROR_OK
) {
2278 /* convert to target endianness */
2279 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2280 uint32_t target_data
;
2281 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2282 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2285 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2294 int target_blank_check_memory(struct target
*target
,
2295 struct target_memory_check_block
*blocks
, int num_blocks
,
2296 uint8_t erased_value
)
2298 if (!target_was_examined(target
)) {
2299 LOG_ERROR("Target not examined yet");
2303 if (target
->type
->blank_check_memory
== NULL
)
2304 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2306 return target
->type
->blank_check_memory(target
, blocks
, num_blocks
, erased_value
);
2309 int target_read_u64(struct target
*target
, target_addr_t address
, uint64_t *value
)
2311 uint8_t value_buf
[8];
2312 if (!target_was_examined(target
)) {
2313 LOG_ERROR("Target not examined yet");
2317 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2319 if (retval
== ERROR_OK
) {
2320 *value
= target_buffer_get_u64(target
, value_buf
);
2321 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2326 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2333 int target_read_u32(struct target
*target
, target_addr_t address
, uint32_t *value
)
2335 uint8_t value_buf
[4];
2336 if (!target_was_examined(target
)) {
2337 LOG_ERROR("Target not examined yet");
2341 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2343 if (retval
== ERROR_OK
) {
2344 *value
= target_buffer_get_u32(target
, value_buf
);
2345 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2350 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2357 int target_read_u16(struct target
*target
, target_addr_t address
, uint16_t *value
)
2359 uint8_t value_buf
[2];
2360 if (!target_was_examined(target
)) {
2361 LOG_ERROR("Target not examined yet");
2365 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2367 if (retval
== ERROR_OK
) {
2368 *value
= target_buffer_get_u16(target
, value_buf
);
2369 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%4.4" PRIx16
,
2374 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2381 int target_read_u8(struct target
*target
, target_addr_t address
, uint8_t *value
)
2383 if (!target_was_examined(target
)) {
2384 LOG_ERROR("Target not examined yet");
2388 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2390 if (retval
== ERROR_OK
) {
2391 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2396 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2403 int target_write_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2406 uint8_t value_buf
[8];
2407 if (!target_was_examined(target
)) {
2408 LOG_ERROR("Target not examined yet");
2412 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2416 target_buffer_set_u64(target
, value_buf
, value
);
2417 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2418 if (retval
!= ERROR_OK
)
2419 LOG_DEBUG("failed: %i", retval
);
2424 int target_write_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2427 uint8_t value_buf
[4];
2428 if (!target_was_examined(target
)) {
2429 LOG_ERROR("Target not examined yet");
2433 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2437 target_buffer_set_u32(target
, value_buf
, value
);
2438 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2439 if (retval
!= ERROR_OK
)
2440 LOG_DEBUG("failed: %i", retval
);
2445 int target_write_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2448 uint8_t value_buf
[2];
2449 if (!target_was_examined(target
)) {
2450 LOG_ERROR("Target not examined yet");
2454 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2458 target_buffer_set_u16(target
, value_buf
, value
);
2459 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2460 if (retval
!= ERROR_OK
)
2461 LOG_DEBUG("failed: %i", retval
);
2466 int target_write_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2469 if (!target_was_examined(target
)) {
2470 LOG_ERROR("Target not examined yet");
2474 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2477 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2478 if (retval
!= ERROR_OK
)
2479 LOG_DEBUG("failed: %i", retval
);
2484 int target_write_phys_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2487 uint8_t value_buf
[8];
2488 if (!target_was_examined(target
)) {
2489 LOG_ERROR("Target not examined yet");
2493 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2497 target_buffer_set_u64(target
, value_buf
, value
);
2498 retval
= target_write_phys_memory(target
, address
, 8, 1, value_buf
);
2499 if (retval
!= ERROR_OK
)
2500 LOG_DEBUG("failed: %i", retval
);
2505 int target_write_phys_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2508 uint8_t value_buf
[4];
2509 if (!target_was_examined(target
)) {
2510 LOG_ERROR("Target not examined yet");
2514 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2518 target_buffer_set_u32(target
, value_buf
, value
);
2519 retval
= target_write_phys_memory(target
, address
, 4, 1, value_buf
);
2520 if (retval
!= ERROR_OK
)
2521 LOG_DEBUG("failed: %i", retval
);
2526 int target_write_phys_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2529 uint8_t value_buf
[2];
2530 if (!target_was_examined(target
)) {
2531 LOG_ERROR("Target not examined yet");
2535 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2539 target_buffer_set_u16(target
, value_buf
, value
);
2540 retval
= target_write_phys_memory(target
, address
, 2, 1, value_buf
);
2541 if (retval
!= ERROR_OK
)
2542 LOG_DEBUG("failed: %i", retval
);
2547 int target_write_phys_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2550 if (!target_was_examined(target
)) {
2551 LOG_ERROR("Target not examined yet");
2555 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2558 retval
= target_write_phys_memory(target
, address
, 1, 1, &value
);
2559 if (retval
!= ERROR_OK
)
2560 LOG_DEBUG("failed: %i", retval
);
2565 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2567 struct target
*target
= get_target(name
);
2568 if (target
== NULL
) {
2569 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2572 if (!target
->tap
->enabled
) {
2573 LOG_USER("Target: TAP %s is disabled, "
2574 "can't be the current target\n",
2575 target
->tap
->dotted_name
);
2579 cmd_ctx
->current_target
= target
;
2580 if (cmd_ctx
->current_target_override
)
2581 cmd_ctx
->current_target_override
= target
;
2587 COMMAND_HANDLER(handle_targets_command
)
2589 int retval
= ERROR_OK
;
2590 if (CMD_ARGC
== 1) {
2591 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2592 if (retval
== ERROR_OK
) {
2598 struct target
*target
= all_targets
;
2599 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2600 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2605 if (target
->tap
->enabled
)
2606 state
= target_state_name(target
);
2608 state
= "tap-disabled";
2610 if (CMD_CTX
->current_target
== target
)
2613 /* keep columns lined up to match the headers above */
2614 command_print(CMD_CTX
,
2615 "%2d%c %-18s %-10s %-6s %-18s %s",
2616 target
->target_number
,
2618 target_name(target
),
2619 target_type_name(target
),
2620 Jim_Nvp_value2name_simple(nvp_target_endian
,
2621 target
->endianness
)->name
,
2622 target
->tap
->dotted_name
,
2624 target
= target
->next
;
2630 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2632 static int powerDropout
;
2633 static int srstAsserted
;
2635 static int runPowerRestore
;
2636 static int runPowerDropout
;
2637 static int runSrstAsserted
;
2638 static int runSrstDeasserted
;
2640 static int sense_handler(void)
2642 static int prevSrstAsserted
;
2643 static int prevPowerdropout
;
2645 int retval
= jtag_power_dropout(&powerDropout
);
2646 if (retval
!= ERROR_OK
)
2650 powerRestored
= prevPowerdropout
&& !powerDropout
;
2652 runPowerRestore
= 1;
2654 int64_t current
= timeval_ms();
2655 static int64_t lastPower
;
2656 bool waitMore
= lastPower
+ 2000 > current
;
2657 if (powerDropout
&& !waitMore
) {
2658 runPowerDropout
= 1;
2659 lastPower
= current
;
2662 retval
= jtag_srst_asserted(&srstAsserted
);
2663 if (retval
!= ERROR_OK
)
2667 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2669 static int64_t lastSrst
;
2670 waitMore
= lastSrst
+ 2000 > current
;
2671 if (srstDeasserted
&& !waitMore
) {
2672 runSrstDeasserted
= 1;
2676 if (!prevSrstAsserted
&& srstAsserted
)
2677 runSrstAsserted
= 1;
2679 prevSrstAsserted
= srstAsserted
;
2680 prevPowerdropout
= powerDropout
;
2682 if (srstDeasserted
|| powerRestored
) {
2683 /* Other than logging the event we can't do anything here.
2684 * Issuing a reset is a particularly bad idea as we might
2685 * be inside a reset already.
2692 /* process target state changes */
2693 static int handle_target(void *priv
)
2695 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2696 int retval
= ERROR_OK
;
2698 if (!is_jtag_poll_safe()) {
2699 /* polling is disabled currently */
2703 /* we do not want to recurse here... */
2704 static int recursive
;
2708 /* danger! running these procedures can trigger srst assertions and power dropouts.
2709 * We need to avoid an infinite loop/recursion here and we do that by
2710 * clearing the flags after running these events.
2712 int did_something
= 0;
2713 if (runSrstAsserted
) {
2714 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2715 Jim_Eval(interp
, "srst_asserted");
2718 if (runSrstDeasserted
) {
2719 Jim_Eval(interp
, "srst_deasserted");
2722 if (runPowerDropout
) {
2723 LOG_INFO("Power dropout detected, running power_dropout proc.");
2724 Jim_Eval(interp
, "power_dropout");
2727 if (runPowerRestore
) {
2728 Jim_Eval(interp
, "power_restore");
2732 if (did_something
) {
2733 /* clear detect flags */
2737 /* clear action flags */
2739 runSrstAsserted
= 0;
2740 runSrstDeasserted
= 0;
2741 runPowerRestore
= 0;
2742 runPowerDropout
= 0;
2747 /* Poll targets for state changes unless that's globally disabled.
2748 * Skip targets that are currently disabled.
2750 for (struct target
*target
= all_targets
;
2751 is_jtag_poll_safe() && target
;
2752 target
= target
->next
) {
2754 if (!target_was_examined(target
))
2757 if (!target
->tap
->enabled
)
2760 if (target
->backoff
.times
> target
->backoff
.count
) {
2761 /* do not poll this time as we failed previously */
2762 target
->backoff
.count
++;
2765 target
->backoff
.count
= 0;
2767 /* only poll target if we've got power and srst isn't asserted */
2768 if (!powerDropout
&& !srstAsserted
) {
2769 /* polling may fail silently until the target has been examined */
2770 retval
= target_poll(target
);
2771 if (retval
!= ERROR_OK
) {
2772 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2773 if (target
->backoff
.times
* polling_interval
< 5000) {
2774 target
->backoff
.times
*= 2;
2775 target
->backoff
.times
++;
2778 /* Tell GDB to halt the debugger. This allows the user to
2779 * run monitor commands to handle the situation.
2781 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2783 if (target
->backoff
.times
> 0) {
2784 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
2785 target_reset_examined(target
);
2786 retval
= target_examine_one(target
);
2787 /* Target examination could have failed due to unstable connection,
2788 * but we set the examined flag anyway to repoll it later */
2789 if (retval
!= ERROR_OK
) {
2790 target
->examined
= true;
2791 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2792 target
->backoff
.times
* polling_interval
);
2797 /* Since we succeeded, we reset backoff count */
2798 target
->backoff
.times
= 0;
2805 COMMAND_HANDLER(handle_reg_command
)
2807 struct target
*target
;
2808 struct reg
*reg
= NULL
;
2814 target
= get_current_target(CMD_CTX
);
2816 /* list all available registers for the current target */
2817 if (CMD_ARGC
== 0) {
2818 struct reg_cache
*cache
= target
->reg_cache
;
2824 command_print(CMD_CTX
, "===== %s", cache
->name
);
2826 for (i
= 0, reg
= cache
->reg_list
;
2827 i
< cache
->num_regs
;
2828 i
++, reg
++, count
++) {
2829 if (reg
->exist
== false)
2831 /* only print cached values if they are valid */
2833 value
= buf_to_str(reg
->value
,
2835 command_print(CMD_CTX
,
2836 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2844 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2849 cache
= cache
->next
;
2855 /* access a single register by its ordinal number */
2856 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2858 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2860 struct reg_cache
*cache
= target
->reg_cache
;
2864 for (i
= 0; i
< cache
->num_regs
; i
++) {
2865 if (count
++ == num
) {
2866 reg
= &cache
->reg_list
[i
];
2872 cache
= cache
->next
;
2876 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2877 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2881 /* access a single register by its name */
2882 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2888 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2893 /* display a register */
2894 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2895 && (CMD_ARGV
[1][0] <= '9')))) {
2896 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2899 if (reg
->valid
== 0)
2900 reg
->type
->get(reg
);
2901 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2902 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2907 /* set register value */
2908 if (CMD_ARGC
== 2) {
2909 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2912 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2914 reg
->type
->set(reg
, buf
);
2916 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2917 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2925 return ERROR_COMMAND_SYNTAX_ERROR
;
2928 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2932 COMMAND_HANDLER(handle_poll_command
)
2934 int retval
= ERROR_OK
;
2935 struct target
*target
= get_current_target(CMD_CTX
);
2937 if (CMD_ARGC
== 0) {
2938 command_print(CMD_CTX
, "background polling: %s",
2939 jtag_poll_get_enabled() ? "on" : "off");
2940 command_print(CMD_CTX
, "TAP: %s (%s)",
2941 target
->tap
->dotted_name
,
2942 target
->tap
->enabled
? "enabled" : "disabled");
2943 if (!target
->tap
->enabled
)
2945 retval
= target_poll(target
);
2946 if (retval
!= ERROR_OK
)
2948 retval
= target_arch_state(target
);
2949 if (retval
!= ERROR_OK
)
2951 } else if (CMD_ARGC
== 1) {
2953 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2954 jtag_poll_set_enabled(enable
);
2956 return ERROR_COMMAND_SYNTAX_ERROR
;
2961 COMMAND_HANDLER(handle_wait_halt_command
)
2964 return ERROR_COMMAND_SYNTAX_ERROR
;
2966 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2967 if (1 == CMD_ARGC
) {
2968 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2969 if (ERROR_OK
!= retval
)
2970 return ERROR_COMMAND_SYNTAX_ERROR
;
2973 struct target
*target
= get_current_target(CMD_CTX
);
2974 return target_wait_state(target
, TARGET_HALTED
, ms
);
2977 /* wait for target state to change. The trick here is to have a low
2978 * latency for short waits and not to suck up all the CPU time
2981 * After 500ms, keep_alive() is invoked
2983 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2986 int64_t then
= 0, cur
;
2990 retval
= target_poll(target
);
2991 if (retval
!= ERROR_OK
)
2993 if (target
->state
== state
)
2998 then
= timeval_ms();
2999 LOG_DEBUG("waiting for target %s...",
3000 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
3006 if ((cur
-then
) > ms
) {
3007 LOG_ERROR("timed out while waiting for target %s",
3008 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
3016 COMMAND_HANDLER(handle_halt_command
)
3020 struct target
*target
= get_current_target(CMD_CTX
);
3022 target
->verbose_halt_msg
= true;
3024 int retval
= target_halt(target
);
3025 if (ERROR_OK
!= retval
)
3028 if (CMD_ARGC
== 1) {
3029 unsigned wait_local
;
3030 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
3031 if (ERROR_OK
!= retval
)
3032 return ERROR_COMMAND_SYNTAX_ERROR
;
3037 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
3040 COMMAND_HANDLER(handle_soft_reset_halt_command
)
3042 struct target
*target
= get_current_target(CMD_CTX
);
3044 LOG_USER("requesting target halt and executing a soft reset");
3046 target_soft_reset_halt(target
);
3051 COMMAND_HANDLER(handle_reset_command
)
3054 return ERROR_COMMAND_SYNTAX_ERROR
;
3056 enum target_reset_mode reset_mode
= RESET_RUN
;
3057 if (CMD_ARGC
== 1) {
3059 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
3060 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
3061 return ERROR_COMMAND_SYNTAX_ERROR
;
3062 reset_mode
= n
->value
;
3065 /* reset *all* targets */
3066 return target_process_reset(CMD_CTX
, reset_mode
);
3070 COMMAND_HANDLER(handle_resume_command
)
3074 return ERROR_COMMAND_SYNTAX_ERROR
;
3076 struct target
*target
= get_current_target(CMD_CTX
);
3078 /* with no CMD_ARGV, resume from current pc, addr = 0,
3079 * with one arguments, addr = CMD_ARGV[0],
3080 * handle breakpoints, not debugging */
3081 target_addr_t addr
= 0;
3082 if (CMD_ARGC
== 1) {
3083 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3087 return target_resume(target
, current
, addr
, 1, 0);
3090 COMMAND_HANDLER(handle_step_command
)
3093 return ERROR_COMMAND_SYNTAX_ERROR
;
3097 /* with no CMD_ARGV, step from current pc, addr = 0,
3098 * with one argument addr = CMD_ARGV[0],
3099 * handle breakpoints, debugging */
3100 target_addr_t addr
= 0;
3102 if (CMD_ARGC
== 1) {
3103 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3107 struct target
*target
= get_current_target(CMD_CTX
);
3109 return target
->type
->step(target
, current_pc
, addr
, 1);
3112 static void handle_md_output(struct command_context
*cmd_ctx
,
3113 struct target
*target
, target_addr_t address
, unsigned size
,
3114 unsigned count
, const uint8_t *buffer
)
3116 const unsigned line_bytecnt
= 32;
3117 unsigned line_modulo
= line_bytecnt
/ size
;
3119 char output
[line_bytecnt
* 4 + 1];
3120 unsigned output_len
= 0;
3122 const char *value_fmt
;
3125 value_fmt
= "%16.16"PRIx64
" ";
3128 value_fmt
= "%8.8"PRIx64
" ";
3131 value_fmt
= "%4.4"PRIx64
" ";
3134 value_fmt
= "%2.2"PRIx64
" ";
3137 /* "can't happen", caller checked */
3138 LOG_ERROR("invalid memory read size: %u", size
);
3142 for (unsigned i
= 0; i
< count
; i
++) {
3143 if (i
% line_modulo
== 0) {
3144 output_len
+= snprintf(output
+ output_len
,
3145 sizeof(output
) - output_len
,
3146 TARGET_ADDR_FMT
": ",
3147 (address
+ (i
* size
)));
3151 const uint8_t *value_ptr
= buffer
+ i
* size
;
3154 value
= target_buffer_get_u64(target
, value_ptr
);
3157 value
= target_buffer_get_u32(target
, value_ptr
);
3160 value
= target_buffer_get_u16(target
, value_ptr
);
3165 output_len
+= snprintf(output
+ output_len
,
3166 sizeof(output
) - output_len
,
3169 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
3170 command_print(cmd_ctx
, "%s", output
);
3176 COMMAND_HANDLER(handle_md_command
)
3179 return ERROR_COMMAND_SYNTAX_ERROR
;
3182 switch (CMD_NAME
[2]) {
3196 return ERROR_COMMAND_SYNTAX_ERROR
;
3199 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3200 int (*fn
)(struct target
*target
,
3201 target_addr_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
3205 fn
= target_read_phys_memory
;
3207 fn
= target_read_memory
;
3208 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
3209 return ERROR_COMMAND_SYNTAX_ERROR
;
3211 target_addr_t address
;
3212 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3216 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
3218 uint8_t *buffer
= calloc(count
, size
);
3219 if (buffer
== NULL
) {
3220 LOG_ERROR("Failed to allocate md read buffer");
3224 struct target
*target
= get_current_target(CMD_CTX
);
3225 int retval
= fn(target
, address
, size
, count
, buffer
);
3226 if (ERROR_OK
== retval
)
3227 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
3234 typedef int (*target_write_fn
)(struct target
*target
,
3235 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
3237 static int target_fill_mem(struct target
*target
,
3238 target_addr_t address
,
3246 /* We have to write in reasonably large chunks to be able
3247 * to fill large memory areas with any sane speed */
3248 const unsigned chunk_size
= 16384;
3249 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
3250 if (target_buf
== NULL
) {
3251 LOG_ERROR("Out of memory");
3255 for (unsigned i
= 0; i
< chunk_size
; i
++) {
3256 switch (data_size
) {
3258 target_buffer_set_u64(target
, target_buf
+ i
* data_size
, b
);
3261 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
3264 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
3267 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
3274 int retval
= ERROR_OK
;
3276 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
3279 if (current
> chunk_size
)
3280 current
= chunk_size
;
3281 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
3282 if (retval
!= ERROR_OK
)
3284 /* avoid GDB timeouts */
3293 COMMAND_HANDLER(handle_mw_command
)
3296 return ERROR_COMMAND_SYNTAX_ERROR
;
3297 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3302 fn
= target_write_phys_memory
;
3304 fn
= target_write_memory
;
3305 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
3306 return ERROR_COMMAND_SYNTAX_ERROR
;
3308 target_addr_t address
;
3309 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3311 target_addr_t value
;
3312 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], value
);
3316 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
3318 struct target
*target
= get_current_target(CMD_CTX
);
3320 switch (CMD_NAME
[2]) {
3334 return ERROR_COMMAND_SYNTAX_ERROR
;
3337 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
3340 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
3341 target_addr_t
*min_address
, target_addr_t
*max_address
)
3343 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
3344 return ERROR_COMMAND_SYNTAX_ERROR
;
3346 /* a base address isn't always necessary,
3347 * default to 0x0 (i.e. don't relocate) */
3348 if (CMD_ARGC
>= 2) {
3350 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3351 image
->base_address
= addr
;
3352 image
->base_address_set
= 1;
3354 image
->base_address_set
= 0;
3356 image
->start_address_set
= 0;
3359 COMMAND_PARSE_ADDRESS(CMD_ARGV
[3], *min_address
);
3360 if (CMD_ARGC
== 5) {
3361 COMMAND_PARSE_ADDRESS(CMD_ARGV
[4], *max_address
);
3362 /* use size (given) to find max (required) */
3363 *max_address
+= *min_address
;
3366 if (*min_address
> *max_address
)
3367 return ERROR_COMMAND_SYNTAX_ERROR
;
3372 COMMAND_HANDLER(handle_load_image_command
)
3376 uint32_t image_size
;
3377 target_addr_t min_address
= 0;
3378 target_addr_t max_address
= -1;
3382 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
3383 &image
, &min_address
, &max_address
);
3384 if (ERROR_OK
!= retval
)
3387 struct target
*target
= get_current_target(CMD_CTX
);
3389 struct duration bench
;
3390 duration_start(&bench
);
3392 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
3397 for (i
= 0; i
< image
.num_sections
; i
++) {
3398 buffer
= malloc(image
.sections
[i
].size
);
3399 if (buffer
== NULL
) {
3400 command_print(CMD_CTX
,
3401 "error allocating buffer for section (%d bytes)",
3402 (int)(image
.sections
[i
].size
));
3403 retval
= ERROR_FAIL
;
3407 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3408 if (retval
!= ERROR_OK
) {
3413 uint32_t offset
= 0;
3414 uint32_t length
= buf_cnt
;
3416 /* DANGER!!! beware of unsigned comparision here!!! */
3418 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3419 (image
.sections
[i
].base_address
< max_address
)) {
3421 if (image
.sections
[i
].base_address
< min_address
) {
3422 /* clip addresses below */
3423 offset
+= min_address
-image
.sections
[i
].base_address
;
3427 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3428 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3430 retval
= target_write_buffer(target
,
3431 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3432 if (retval
!= ERROR_OK
) {
3436 image_size
+= length
;
3437 command_print(CMD_CTX
, "%u bytes written at address " TARGET_ADDR_FMT
"",
3438 (unsigned int)length
,
3439 image
.sections
[i
].base_address
+ offset
);
3445 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3446 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
3447 "in %fs (%0.3f KiB/s)", image_size
,
3448 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3451 image_close(&image
);
3457 COMMAND_HANDLER(handle_dump_image_command
)
3459 struct fileio
*fileio
;
3461 int retval
, retvaltemp
;
3462 target_addr_t address
, size
;
3463 struct duration bench
;
3464 struct target
*target
= get_current_target(CMD_CTX
);
3467 return ERROR_COMMAND_SYNTAX_ERROR
;
3469 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], address
);
3470 COMMAND_PARSE_ADDRESS(CMD_ARGV
[2], size
);
3472 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
3473 buffer
= malloc(buf_size
);
3477 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3478 if (retval
!= ERROR_OK
) {
3483 duration_start(&bench
);
3486 size_t size_written
;
3487 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
3488 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
3489 if (retval
!= ERROR_OK
)
3492 retval
= fileio_write(fileio
, this_run_size
, buffer
, &size_written
);
3493 if (retval
!= ERROR_OK
)
3496 size
-= this_run_size
;
3497 address
+= this_run_size
;
3502 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3504 retval
= fileio_size(fileio
, &filesize
);
3505 if (retval
!= ERROR_OK
)
3507 command_print(CMD_CTX
,
3508 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize
,
3509 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3512 retvaltemp
= fileio_close(fileio
);
3513 if (retvaltemp
!= ERROR_OK
)
3522 IMAGE_CHECKSUM_ONLY
= 2
3525 static COMMAND_HELPER(handle_verify_image_command_internal
, enum verify_mode verify
)
3529 uint32_t image_size
;
3532 uint32_t checksum
= 0;
3533 uint32_t mem_checksum
= 0;
3537 struct target
*target
= get_current_target(CMD_CTX
);
3540 return ERROR_COMMAND_SYNTAX_ERROR
;
3543 LOG_ERROR("no target selected");
3547 struct duration bench
;
3548 duration_start(&bench
);
3550 if (CMD_ARGC
>= 2) {
3552 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3553 image
.base_address
= addr
;
3554 image
.base_address_set
= 1;
3556 image
.base_address_set
= 0;
3557 image
.base_address
= 0x0;
3560 image
.start_address_set
= 0;
3562 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3563 if (retval
!= ERROR_OK
)
3569 for (i
= 0; i
< image
.num_sections
; i
++) {
3570 buffer
= malloc(image
.sections
[i
].size
);
3571 if (buffer
== NULL
) {
3572 command_print(CMD_CTX
,
3573 "error allocating buffer for section (%d bytes)",
3574 (int)(image
.sections
[i
].size
));
3577 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3578 if (retval
!= ERROR_OK
) {
3583 if (verify
>= IMAGE_VERIFY
) {
3584 /* calculate checksum of image */
3585 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3586 if (retval
!= ERROR_OK
) {
3591 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3592 if (retval
!= ERROR_OK
) {
3596 if ((checksum
!= mem_checksum
) && (verify
== IMAGE_CHECKSUM_ONLY
)) {
3597 LOG_ERROR("checksum mismatch");
3599 retval
= ERROR_FAIL
;
3602 if (checksum
!= mem_checksum
) {
3603 /* failed crc checksum, fall back to a binary compare */
3607 LOG_ERROR("checksum mismatch - attempting binary compare");
3609 data
= malloc(buf_cnt
);
3611 /* Can we use 32bit word accesses? */
3613 int count
= buf_cnt
;
3614 if ((count
% 4) == 0) {
3618 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
3619 if (retval
== ERROR_OK
) {
3621 for (t
= 0; t
< buf_cnt
; t
++) {
3622 if (data
[t
] != buffer
[t
]) {
3623 command_print(CMD_CTX
,
3624 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3626 (unsigned)(t
+ image
.sections
[i
].base_address
),
3629 if (diffs
++ >= 127) {
3630 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
3642 command_print(CMD_CTX
, "address " TARGET_ADDR_FMT
" length 0x%08zx",
3643 image
.sections
[i
].base_address
,
3648 image_size
+= buf_cnt
;
3651 command_print(CMD_CTX
, "No more differences found.");
3654 retval
= ERROR_FAIL
;
3655 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3656 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3657 "in %fs (%0.3f KiB/s)", image_size
,
3658 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3661 image_close(&image
);
3666 COMMAND_HANDLER(handle_verify_image_checksum_command
)
3668 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_CHECKSUM_ONLY
);
3671 COMMAND_HANDLER(handle_verify_image_command
)
3673 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_VERIFY
);
3676 COMMAND_HANDLER(handle_test_image_command
)
3678 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_TEST
);
3681 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
3683 struct target
*target
= get_current_target(cmd_ctx
);
3684 struct breakpoint
*breakpoint
= target
->breakpoints
;
3685 while (breakpoint
) {
3686 if (breakpoint
->type
== BKPT_SOFT
) {
3687 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3688 breakpoint
->length
, 16);
3689 command_print(cmd_ctx
, "IVA breakpoint: " TARGET_ADDR_FMT
", 0x%x, %i, 0x%s",
3690 breakpoint
->address
,
3692 breakpoint
->set
, buf
);
3695 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3696 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3698 breakpoint
->length
, breakpoint
->set
);
3699 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3700 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3701 breakpoint
->address
,
3702 breakpoint
->length
, breakpoint
->set
);
3703 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3706 command_print(cmd_ctx
, "Breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3707 breakpoint
->address
,
3708 breakpoint
->length
, breakpoint
->set
);
3711 breakpoint
= breakpoint
->next
;
3716 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
3717 target_addr_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3719 struct target
*target
= get_current_target(cmd_ctx
);
3723 retval
= breakpoint_add(target
, addr
, length
, hw
);
3724 /* error is always logged in breakpoint_add(), do not print it again */
3725 if (ERROR_OK
== retval
)
3726 command_print(cmd_ctx
, "breakpoint set at " TARGET_ADDR_FMT
"", addr
);
3728 } else if (addr
== 0) {
3729 if (target
->type
->add_context_breakpoint
== NULL
) {
3730 LOG_ERROR("Context breakpoint not available");
3731 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
3733 retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3734 /* error is always logged in context_breakpoint_add(), do not print it again */
3735 if (ERROR_OK
== retval
)
3736 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3739 if (target
->type
->add_hybrid_breakpoint
== NULL
) {
3740 LOG_ERROR("Hybrid breakpoint not available");
3741 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
3743 retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3744 /* error is always logged in hybrid_breakpoint_add(), do not print it again */
3745 if (ERROR_OK
== retval
)
3746 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3751 COMMAND_HANDLER(handle_bp_command
)
3760 return handle_bp_command_list(CMD_CTX
);
3764 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3765 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3766 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3769 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
3771 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3772 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3774 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3775 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
3777 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
3778 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3780 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3785 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3786 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
3787 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
3788 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3791 return ERROR_COMMAND_SYNTAX_ERROR
;
3795 COMMAND_HANDLER(handle_rbp_command
)
3798 return ERROR_COMMAND_SYNTAX_ERROR
;
3801 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3803 struct target
*target
= get_current_target(CMD_CTX
);
3804 breakpoint_remove(target
, addr
);
3809 COMMAND_HANDLER(handle_wp_command
)
3811 struct target
*target
= get_current_target(CMD_CTX
);
3813 if (CMD_ARGC
== 0) {
3814 struct watchpoint
*watchpoint
= target
->watchpoints
;
3816 while (watchpoint
) {
3817 command_print(CMD_CTX
, "address: " TARGET_ADDR_FMT
3818 ", len: 0x%8.8" PRIx32
3819 ", r/w/a: %i, value: 0x%8.8" PRIx32
3820 ", mask: 0x%8.8" PRIx32
,
3821 watchpoint
->address
,
3823 (int)watchpoint
->rw
,
3826 watchpoint
= watchpoint
->next
;
3831 enum watchpoint_rw type
= WPT_ACCESS
;
3833 uint32_t length
= 0;
3834 uint32_t data_value
= 0x0;
3835 uint32_t data_mask
= 0xffffffff;
3839 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
3842 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3845 switch (CMD_ARGV
[2][0]) {
3856 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3857 return ERROR_COMMAND_SYNTAX_ERROR
;
3861 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3862 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3866 return ERROR_COMMAND_SYNTAX_ERROR
;
3869 int retval
= watchpoint_add(target
, addr
, length
, type
,
3870 data_value
, data_mask
);
3871 if (ERROR_OK
!= retval
)
3872 LOG_ERROR("Failure setting watchpoints");
3877 COMMAND_HANDLER(handle_rwp_command
)
3880 return ERROR_COMMAND_SYNTAX_ERROR
;
3883 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3885 struct target
*target
= get_current_target(CMD_CTX
);
3886 watchpoint_remove(target
, addr
);
3892 * Translate a virtual address to a physical address.
3894 * The low-level target implementation must have logged a detailed error
3895 * which is forwarded to telnet/GDB session.
3897 COMMAND_HANDLER(handle_virt2phys_command
)
3900 return ERROR_COMMAND_SYNTAX_ERROR
;
3903 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], va
);
3906 struct target
*target
= get_current_target(CMD_CTX
);
3907 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3908 if (retval
== ERROR_OK
)
3909 command_print(CMD_CTX
, "Physical address " TARGET_ADDR_FMT
"", pa
);
3914 static void writeData(FILE *f
, const void *data
, size_t len
)
3916 size_t written
= fwrite(data
, 1, len
, f
);
3918 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3921 static void writeLong(FILE *f
, int l
, struct target
*target
)
3925 target_buffer_set_u32(target
, val
, l
);
3926 writeData(f
, val
, 4);
3929 static void writeString(FILE *f
, char *s
)
3931 writeData(f
, s
, strlen(s
));
3934 typedef unsigned char UNIT
[2]; /* unit of profiling */
3936 /* Dump a gmon.out histogram file. */
3937 static void write_gmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
, bool with_range
,
3938 uint32_t start_address
, uint32_t end_address
, struct target
*target
, uint32_t duration_ms
)
3941 FILE *f
= fopen(filename
, "w");
3944 writeString(f
, "gmon");
3945 writeLong(f
, 0x00000001, target
); /* Version */
3946 writeLong(f
, 0, target
); /* padding */
3947 writeLong(f
, 0, target
); /* padding */
3948 writeLong(f
, 0, target
); /* padding */
3950 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3951 writeData(f
, &zero
, 1);
3953 /* figure out bucket size */
3957 min
= start_address
;
3962 for (i
= 0; i
< sampleNum
; i
++) {
3963 if (min
> samples
[i
])
3965 if (max
< samples
[i
])
3969 /* max should be (largest sample + 1)
3970 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
3974 int addressSpace
= max
- min
;
3975 assert(addressSpace
>= 2);
3977 /* FIXME: What is the reasonable number of buckets?
3978 * The profiling result will be more accurate if there are enough buckets. */
3979 static const uint32_t maxBuckets
= 128 * 1024; /* maximum buckets. */
3980 uint32_t numBuckets
= addressSpace
/ sizeof(UNIT
);
3981 if (numBuckets
> maxBuckets
)
3982 numBuckets
= maxBuckets
;
3983 int *buckets
= malloc(sizeof(int) * numBuckets
);
3984 if (buckets
== NULL
) {
3988 memset(buckets
, 0, sizeof(int) * numBuckets
);
3989 for (i
= 0; i
< sampleNum
; i
++) {
3990 uint32_t address
= samples
[i
];
3992 if ((address
< min
) || (max
<= address
))
3995 long long a
= address
- min
;
3996 long long b
= numBuckets
;
3997 long long c
= addressSpace
;
3998 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
4002 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
4003 writeLong(f
, min
, target
); /* low_pc */
4004 writeLong(f
, max
, target
); /* high_pc */
4005 writeLong(f
, numBuckets
, target
); /* # of buckets */
4006 float sample_rate
= sampleNum
/ (duration_ms
/ 1000.0);
4007 writeLong(f
, sample_rate
, target
);
4008 writeString(f
, "seconds");
4009 for (i
= 0; i
< (15-strlen("seconds")); i
++)
4010 writeData(f
, &zero
, 1);
4011 writeString(f
, "s");
4013 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
4015 char *data
= malloc(2 * numBuckets
);
4017 for (i
= 0; i
< numBuckets
; i
++) {
4022 data
[i
* 2] = val
&0xff;
4023 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
4026 writeData(f
, data
, numBuckets
* 2);
4034 /* profiling samples the CPU PC as quickly as OpenOCD is able,
4035 * which will be used as a random sampling of PC */
4036 COMMAND_HANDLER(handle_profile_command
)
4038 struct target
*target
= get_current_target(CMD_CTX
);
4040 if ((CMD_ARGC
!= 2) && (CMD_ARGC
!= 4))
4041 return ERROR_COMMAND_SYNTAX_ERROR
;
4043 const uint32_t MAX_PROFILE_SAMPLE_NUM
= 10000;
4045 uint32_t num_of_samples
;
4046 int retval
= ERROR_OK
;
4048 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], offset
);
4050 uint32_t *samples
= malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM
);
4051 if (samples
== NULL
) {
4052 LOG_ERROR("No memory to store samples.");
4056 uint64_t timestart_ms
= timeval_ms();
4058 * Some cores let us sample the PC without the
4059 * annoying halt/resume step; for example, ARMv7 PCSR.
4060 * Provide a way to use that more efficient mechanism.
4062 retval
= target_profiling(target
, samples
, MAX_PROFILE_SAMPLE_NUM
,
4063 &num_of_samples
, offset
);
4064 if (retval
!= ERROR_OK
) {
4068 uint32_t duration_ms
= timeval_ms() - timestart_ms
;
4070 assert(num_of_samples
<= MAX_PROFILE_SAMPLE_NUM
);
4072 retval
= target_poll(target
);
4073 if (retval
!= ERROR_OK
) {
4077 if (target
->state
== TARGET_RUNNING
) {
4078 retval
= target_halt(target
);
4079 if (retval
!= ERROR_OK
) {
4085 retval
= target_poll(target
);
4086 if (retval
!= ERROR_OK
) {
4091 uint32_t start_address
= 0;
4092 uint32_t end_address
= 0;
4093 bool with_range
= false;
4094 if (CMD_ARGC
== 4) {
4096 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], start_address
);
4097 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], end_address
);
4100 write_gmon(samples
, num_of_samples
, CMD_ARGV
[1],
4101 with_range
, start_address
, end_address
, target
, duration_ms
);
4102 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
4108 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
4111 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4114 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4118 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4119 valObjPtr
= Jim_NewIntObj(interp
, val
);
4120 if (!nameObjPtr
|| !valObjPtr
) {
4125 Jim_IncrRefCount(nameObjPtr
);
4126 Jim_IncrRefCount(valObjPtr
);
4127 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
4128 Jim_DecrRefCount(interp
, nameObjPtr
);
4129 Jim_DecrRefCount(interp
, valObjPtr
);
4131 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
4135 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4137 struct command_context
*context
;
4138 struct target
*target
;
4140 context
= current_command_context(interp
);
4141 assert(context
!= NULL
);
4143 target
= get_current_target(context
);
4144 if (target
== NULL
) {
4145 LOG_ERROR("mem2array: no current target");
4149 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4152 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
4160 const char *varname
;
4166 /* argv[1] = name of array to receive the data
4167 * argv[2] = desired width
4168 * argv[3] = memory address
4169 * argv[4] = count of times to read
4172 if (argc
< 4 || argc
> 5) {
4173 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4176 varname
= Jim_GetString(argv
[0], &len
);
4177 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4179 e
= Jim_GetLong(interp
, argv
[1], &l
);
4184 e
= Jim_GetLong(interp
, argv
[2], &l
);
4188 e
= Jim_GetLong(interp
, argv
[3], &l
);
4194 phys
= Jim_GetString(argv
[4], &n
);
4195 if (!strncmp(phys
, "phys", n
))
4211 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4212 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
4216 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4217 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
4220 if ((addr
+ (len
* width
)) < addr
) {
4221 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4222 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
4225 /* absurd transfer size? */
4227 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4228 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
4233 ((width
== 2) && ((addr
& 1) == 0)) ||
4234 ((width
== 4) && ((addr
& 3) == 0))) {
4238 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4239 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
4242 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4251 size_t buffersize
= 4096;
4252 uint8_t *buffer
= malloc(buffersize
);
4259 /* Slurp... in buffer size chunks */
4261 count
= len
; /* in objects.. */
4262 if (count
> (buffersize
/ width
))
4263 count
= (buffersize
/ width
);
4266 retval
= target_read_phys_memory(target
, addr
, width
, count
, buffer
);
4268 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
4269 if (retval
!= ERROR_OK
) {
4271 LOG_ERROR("mem2array: Read @ 0x%08" PRIx32
", w=%" PRId32
", cnt=%" PRId32
", failed",
4275 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4276 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
4280 v
= 0; /* shut up gcc */
4281 for (i
= 0; i
< count
; i
++, n
++) {
4284 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
4287 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
4290 v
= buffer
[i
] & 0x0ff;
4293 new_int_array_element(interp
, varname
, n
, v
);
4296 addr
+= count
* width
;
4302 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4307 static int get_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t *val
)
4310 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4314 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4318 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4324 Jim_IncrRefCount(nameObjPtr
);
4325 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
4326 Jim_DecrRefCount(interp
, nameObjPtr
);
4328 if (valObjPtr
== NULL
)
4331 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
4332 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
4337 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4339 struct command_context
*context
;
4340 struct target
*target
;
4342 context
= current_command_context(interp
);
4343 assert(context
!= NULL
);
4345 target
= get_current_target(context
);
4346 if (target
== NULL
) {
4347 LOG_ERROR("array2mem: no current target");
4351 return target_array2mem(interp
, target
, argc
-1, argv
+ 1);
4354 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
4355 int argc
, Jim_Obj
*const *argv
)
4363 const char *varname
;
4369 /* argv[1] = name of array to get the data
4370 * argv[2] = desired width
4371 * argv[3] = memory address
4372 * argv[4] = count to write
4374 if (argc
< 4 || argc
> 5) {
4375 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4378 varname
= Jim_GetString(argv
[0], &len
);
4379 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4381 e
= Jim_GetLong(interp
, argv
[1], &l
);
4386 e
= Jim_GetLong(interp
, argv
[2], &l
);
4390 e
= Jim_GetLong(interp
, argv
[3], &l
);
4396 phys
= Jim_GetString(argv
[4], &n
);
4397 if (!strncmp(phys
, "phys", n
))
4413 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4414 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4415 "Invalid width param, must be 8/16/32", NULL
);
4419 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4420 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4421 "array2mem: zero width read?", NULL
);
4424 if ((addr
+ (len
* width
)) < addr
) {
4425 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4426 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4427 "array2mem: addr + len - wraps to zero?", NULL
);
4430 /* absurd transfer size? */
4432 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4433 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4434 "array2mem: absurd > 64K item request", NULL
);
4439 ((width
== 2) && ((addr
& 1) == 0)) ||
4440 ((width
== 4) && ((addr
& 3) == 0))) {
4444 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4445 sprintf(buf
, "array2mem address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
4448 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4459 size_t buffersize
= 4096;
4460 uint8_t *buffer
= malloc(buffersize
);
4465 /* Slurp... in buffer size chunks */
4467 count
= len
; /* in objects.. */
4468 if (count
> (buffersize
/ width
))
4469 count
= (buffersize
/ width
);
4471 v
= 0; /* shut up gcc */
4472 for (i
= 0; i
< count
; i
++, n
++) {
4473 get_int_array_element(interp
, varname
, n
, &v
);
4476 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
4479 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
4482 buffer
[i
] = v
& 0x0ff;
4489 retval
= target_write_phys_memory(target
, addr
, width
, count
, buffer
);
4491 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
4492 if (retval
!= ERROR_OK
) {
4494 LOG_ERROR("array2mem: Write @ 0x%08" PRIx32
", w=%" PRId32
", cnt=%" PRId32
", failed",
4498 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4499 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
4503 addr
+= count
* width
;
4508 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4513 /* FIX? should we propagate errors here rather than printing them
4516 void target_handle_event(struct target
*target
, enum target_event e
)
4518 struct target_event_action
*teap
;
4520 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4521 if (teap
->event
== e
) {
4522 LOG_DEBUG("target(%d): %s (%s) event: %d (%s) action: %s",
4523 target
->target_number
,
4524 target_name(target
),
4525 target_type_name(target
),
4527 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
4528 Jim_GetString(teap
->body
, NULL
));
4530 /* Override current target by the target an event
4531 * is issued from (lot of scripts need it).
4532 * Return back to previous override as soon
4533 * as the handler processing is done */
4534 struct command_context
*cmd_ctx
= current_command_context(teap
->interp
);
4535 struct target
*saved_target_override
= cmd_ctx
->current_target_override
;
4536 cmd_ctx
->current_target_override
= target
;
4538 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
) {
4539 Jim_MakeErrorMessage(teap
->interp
);
4540 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
4543 cmd_ctx
->current_target_override
= saved_target_override
;
4549 * Returns true only if the target has a handler for the specified event.
4551 bool target_has_event_action(struct target
*target
, enum target_event event
)
4553 struct target_event_action
*teap
;
4555 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4556 if (teap
->event
== event
)
4562 enum target_cfg_param
{
4565 TCFG_WORK_AREA_VIRT
,
4566 TCFG_WORK_AREA_PHYS
,
4567 TCFG_WORK_AREA_SIZE
,
4568 TCFG_WORK_AREA_BACKUP
,
4571 TCFG_CHAIN_POSITION
,
4578 static Jim_Nvp nvp_config_opts
[] = {
4579 { .name
= "-type", .value
= TCFG_TYPE
},
4580 { .name
= "-event", .value
= TCFG_EVENT
},
4581 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
4582 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
4583 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
4584 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
4585 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
4586 { .name
= "-coreid", .value
= TCFG_COREID
},
4587 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
4588 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
4589 { .name
= "-rtos", .value
= TCFG_RTOS
},
4590 { .name
= "-defer-examine", .value
= TCFG_DEFER_EXAMINE
},
4591 { .name
= "-gdb-port", .value
= TCFG_GDB_PORT
},
4592 { .name
= NULL
, .value
= -1 }
4595 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
4602 /* parse config or cget options ... */
4603 while (goi
->argc
> 0) {
4604 Jim_SetEmptyResult(goi
->interp
);
4605 /* Jim_GetOpt_Debug(goi); */
4607 if (target
->type
->target_jim_configure
) {
4608 /* target defines a configure function */
4609 /* target gets first dibs on parameters */
4610 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
4619 /* otherwise we 'continue' below */
4621 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
4623 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
4629 if (goi
->isconfigure
) {
4630 Jim_SetResultFormatted(goi
->interp
,
4631 "not settable: %s", n
->name
);
4635 if (goi
->argc
!= 0) {
4636 Jim_WrongNumArgs(goi
->interp
,
4637 goi
->argc
, goi
->argv
,
4642 Jim_SetResultString(goi
->interp
,
4643 target_type_name(target
), -1);
4647 if (goi
->argc
== 0) {
4648 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
4652 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
4654 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
4658 if (goi
->isconfigure
) {
4659 if (goi
->argc
!= 1) {
4660 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
4664 if (goi
->argc
!= 0) {
4665 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
4671 struct target_event_action
*teap
;
4673 teap
= target
->event_action
;
4674 /* replace existing? */
4676 if (teap
->event
== (enum target_event
)n
->value
)
4681 if (goi
->isconfigure
) {
4682 bool replace
= true;
4685 teap
= calloc(1, sizeof(*teap
));
4688 teap
->event
= n
->value
;
4689 teap
->interp
= goi
->interp
;
4690 Jim_GetOpt_Obj(goi
, &o
);
4692 Jim_DecrRefCount(teap
->interp
, teap
->body
);
4693 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
4696 * Tcl/TK - "tk events" have a nice feature.
4697 * See the "BIND" command.
4698 * We should support that here.
4699 * You can specify %X and %Y in the event code.
4700 * The idea is: %T - target name.
4701 * The idea is: %N - target number
4702 * The idea is: %E - event name.
4704 Jim_IncrRefCount(teap
->body
);
4707 /* add to head of event list */
4708 teap
->next
= target
->event_action
;
4709 target
->event_action
= teap
;
4711 Jim_SetEmptyResult(goi
->interp
);
4715 Jim_SetEmptyResult(goi
->interp
);
4717 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
4723 case TCFG_WORK_AREA_VIRT
:
4724 if (goi
->isconfigure
) {
4725 target_free_all_working_areas(target
);
4726 e
= Jim_GetOpt_Wide(goi
, &w
);
4729 target
->working_area_virt
= w
;
4730 target
->working_area_virt_spec
= true;
4735 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
4739 case TCFG_WORK_AREA_PHYS
:
4740 if (goi
->isconfigure
) {
4741 target_free_all_working_areas(target
);
4742 e
= Jim_GetOpt_Wide(goi
, &w
);
4745 target
->working_area_phys
= w
;
4746 target
->working_area_phys_spec
= true;
4751 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
4755 case TCFG_WORK_AREA_SIZE
:
4756 if (goi
->isconfigure
) {
4757 target_free_all_working_areas(target
);
4758 e
= Jim_GetOpt_Wide(goi
, &w
);
4761 target
->working_area_size
= w
;
4766 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4770 case TCFG_WORK_AREA_BACKUP
:
4771 if (goi
->isconfigure
) {
4772 target_free_all_working_areas(target
);
4773 e
= Jim_GetOpt_Wide(goi
, &w
);
4776 /* make this exactly 1 or 0 */
4777 target
->backup_working_area
= (!!w
);
4782 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
4783 /* loop for more e*/
4788 if (goi
->isconfigure
) {
4789 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
4791 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
4794 target
->endianness
= n
->value
;
4799 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4800 if (n
->name
== NULL
) {
4801 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4802 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4804 Jim_SetResultString(goi
->interp
, n
->name
, -1);
4809 if (goi
->isconfigure
) {
4810 e
= Jim_GetOpt_Wide(goi
, &w
);
4813 target
->coreid
= (int32_t)w
;
4818 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4822 case TCFG_CHAIN_POSITION
:
4823 if (goi
->isconfigure
) {
4825 struct jtag_tap
*tap
;
4827 if (target
->has_dap
) {
4828 Jim_SetResultString(goi
->interp
,
4829 "target requires -dap parameter instead of -chain-position!", -1);
4833 target_free_all_working_areas(target
);
4834 e
= Jim_GetOpt_Obj(goi
, &o_t
);
4837 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
4841 target
->tap_configured
= true;
4846 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
4847 /* loop for more e*/
4850 if (goi
->isconfigure
) {
4851 e
= Jim_GetOpt_Wide(goi
, &w
);
4854 target
->dbgbase
= (uint32_t)w
;
4855 target
->dbgbase_set
= true;
4860 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
4866 int result
= rtos_create(goi
, target
);
4867 if (result
!= JIM_OK
)
4873 case TCFG_DEFER_EXAMINE
:
4875 target
->defer_examine
= true;
4880 if (goi
->isconfigure
) {
4882 e
= Jim_GetOpt_String(goi
, &s
, NULL
);
4885 target
->gdb_port_override
= strdup(s
);
4890 Jim_SetResultString(goi
->interp
, target
->gdb_port_override
? : "undefined", -1);
4894 } /* while (goi->argc) */
4897 /* done - we return */
4901 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4905 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4906 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4908 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4909 "missing: -option ...");
4912 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4913 return target_configure(&goi
, target
);
4916 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4918 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4921 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4923 if (goi
.argc
< 2 || goi
.argc
> 4) {
4924 Jim_SetResultFormatted(goi
.interp
,
4925 "usage: %s [phys] <address> <data> [<count>]", cmd_name
);
4930 fn
= target_write_memory
;
4933 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4935 struct Jim_Obj
*obj
;
4936 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4940 fn
= target_write_phys_memory
;
4944 e
= Jim_GetOpt_Wide(&goi
, &a
);
4949 e
= Jim_GetOpt_Wide(&goi
, &b
);
4954 if (goi
.argc
== 1) {
4955 e
= Jim_GetOpt_Wide(&goi
, &c
);
4960 /* all args must be consumed */
4964 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4966 if (strcasecmp(cmd_name
, "mww") == 0)
4968 else if (strcasecmp(cmd_name
, "mwh") == 0)
4970 else if (strcasecmp(cmd_name
, "mwb") == 0)
4973 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4977 return (target_fill_mem(target
, a
, fn
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4981 * @brief Reads an array of words/halfwords/bytes from target memory starting at specified address.
4983 * Usage: mdw [phys] <address> [<count>] - for 32 bit reads
4984 * mdh [phys] <address> [<count>] - for 16 bit reads
4985 * mdb [phys] <address> [<count>] - for 8 bit reads
4987 * Count defaults to 1.
4989 * Calls target_read_memory or target_read_phys_memory depending on
4990 * the presence of the "phys" argument
4991 * Reads the target memory in blocks of max. 32 bytes, and returns an array of ints formatted
4992 * to int representation in base16.
4993 * Also outputs read data in a human readable form using command_print
4995 * @param phys if present target_read_phys_memory will be used instead of target_read_memory
4996 * @param address address where to start the read. May be specified in decimal or hex using the standard "0x" prefix
4997 * @param count optional count parameter to read an array of values. If not specified, defaults to 1.
4998 * @returns: JIM_ERR on error or JIM_OK on success and sets the result string to an array of ascii formatted numbers
4999 * on success, with [<count>] number of elements.
5001 * In case of little endian target:
5002 * Example1: "mdw 0x00000000" returns "10123456"
5003 * Exmaple2: "mdh 0x00000000 1" returns "3456"
5004 * Example3: "mdb 0x00000000" returns "56"
5005 * Example4: "mdh 0x00000000 2" returns "3456 1012"
5006 * Example5: "mdb 0x00000000 3" returns "56 34 12"
5008 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5010 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5013 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5015 if ((goi
.argc
< 1) || (goi
.argc
> 3)) {
5016 Jim_SetResultFormatted(goi
.interp
,
5017 "usage: %s [phys] <address> [<count>]", cmd_name
);
5021 int (*fn
)(struct target
*target
,
5022 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
5023 fn
= target_read_memory
;
5026 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
5028 struct Jim_Obj
*obj
;
5029 e
= Jim_GetOpt_Obj(&goi
, &obj
);
5033 fn
= target_read_phys_memory
;
5036 /* Read address parameter */
5038 e
= Jim_GetOpt_Wide(&goi
, &addr
);
5042 /* If next parameter exists, read it out as the count parameter, if not, set it to 1 (default) */
5044 if (goi
.argc
== 1) {
5045 e
= Jim_GetOpt_Wide(&goi
, &count
);
5051 /* all args must be consumed */
5055 jim_wide dwidth
= 1; /* shut up gcc */
5056 if (strcasecmp(cmd_name
, "mdw") == 0)
5058 else if (strcasecmp(cmd_name
, "mdh") == 0)
5060 else if (strcasecmp(cmd_name
, "mdb") == 0)
5063 LOG_ERROR("command '%s' unknown: ", cmd_name
);
5067 /* convert count to "bytes" */
5068 int bytes
= count
* dwidth
;
5070 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
5071 uint8_t target_buf
[32];
5074 y
= (bytes
< 16) ? bytes
: 16; /* y = min(bytes, 16); */
5076 /* Try to read out next block */
5077 e
= fn(target
, addr
, dwidth
, y
/ dwidth
, target_buf
);
5079 if (e
!= ERROR_OK
) {
5080 Jim_SetResultFormatted(interp
, "error reading target @ 0x%08lx", (long)addr
);
5084 command_print_sameline(NULL
, "0x%08x ", (int)(addr
));
5087 for (x
= 0; x
< 16 && x
< y
; x
+= 4) {
5088 z
= target_buffer_get_u32(target
, &(target_buf
[x
]));
5089 command_print_sameline(NULL
, "%08x ", (int)(z
));
5091 for (; (x
< 16) ; x
+= 4)
5092 command_print_sameline(NULL
, " ");
5095 for (x
= 0; x
< 16 && x
< y
; x
+= 2) {
5096 z
= target_buffer_get_u16(target
, &(target_buf
[x
]));
5097 command_print_sameline(NULL
, "%04x ", (int)(z
));
5099 for (; (x
< 16) ; x
+= 2)
5100 command_print_sameline(NULL
, " ");
5104 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
5105 z
= target_buffer_get_u8(target
, &(target_buf
[x
]));
5106 command_print_sameline(NULL
, "%02x ", (int)(z
));
5108 for (; (x
< 16) ; x
+= 1)
5109 command_print_sameline(NULL
, " ");
5112 /* ascii-ify the bytes */
5113 for (x
= 0 ; x
< y
; x
++) {
5114 if ((target_buf
[x
] >= 0x20) &&
5115 (target_buf
[x
] <= 0x7e)) {
5119 target_buf
[x
] = '.';
5124 target_buf
[x
] = ' ';
5129 /* print - with a newline */
5130 command_print_sameline(NULL
, "%s\n", target_buf
);
5138 static int jim_target_mem2array(Jim_Interp
*interp
,
5139 int argc
, Jim_Obj
*const *argv
)
5141 struct target
*target
= Jim_CmdPrivData(interp
);
5142 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
5145 static int jim_target_array2mem(Jim_Interp
*interp
,
5146 int argc
, Jim_Obj
*const *argv
)
5148 struct target
*target
= Jim_CmdPrivData(interp
);
5149 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
5152 static int jim_target_tap_disabled(Jim_Interp
*interp
)
5154 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
5158 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5160 bool allow_defer
= false;
5163 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5165 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5166 Jim_SetResultFormatted(goi
.interp
,
5167 "usage: %s ['allow-defer']", cmd_name
);
5171 strcmp(Jim_GetString(argv
[1], NULL
), "allow-defer") == 0) {
5173 struct Jim_Obj
*obj
;
5174 int e
= Jim_GetOpt_Obj(&goi
, &obj
);
5180 struct target
*target
= Jim_CmdPrivData(interp
);
5181 if (!target
->tap
->enabled
)
5182 return jim_target_tap_disabled(interp
);
5184 if (allow_defer
&& target
->defer_examine
) {
5185 LOG_INFO("Deferring arp_examine of %s", target_name(target
));
5186 LOG_INFO("Use arp_examine command to examine it manually!");
5190 int e
= target
->type
->examine(target
);
5196 static int jim_target_was_examined(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5198 struct target
*target
= Jim_CmdPrivData(interp
);
5200 Jim_SetResultBool(interp
, target_was_examined(target
));
5204 static int jim_target_examine_deferred(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5206 struct target
*target
= Jim_CmdPrivData(interp
);
5208 Jim_SetResultBool(interp
, target
->defer_examine
);
5212 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5215 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5218 struct target
*target
= Jim_CmdPrivData(interp
);
5220 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
5226 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5229 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5232 struct target
*target
= Jim_CmdPrivData(interp
);
5233 if (!target
->tap
->enabled
)
5234 return jim_target_tap_disabled(interp
);
5237 if (!(target_was_examined(target
)))
5238 e
= ERROR_TARGET_NOT_EXAMINED
;
5240 e
= target
->type
->poll(target
);
5246 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5249 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5251 if (goi
.argc
!= 2) {
5252 Jim_WrongNumArgs(interp
, 0, argv
,
5253 "([tT]|[fF]|assert|deassert) BOOL");
5258 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
5260 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
5263 /* the halt or not param */
5265 e
= Jim_GetOpt_Wide(&goi
, &a
);
5269 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
5270 if (!target
->tap
->enabled
)
5271 return jim_target_tap_disabled(interp
);
5273 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
5274 Jim_SetResultFormatted(interp
,
5275 "No target-specific reset for %s",
5276 target_name(target
));
5280 if (target
->defer_examine
)
5281 target_reset_examined(target
);
5283 /* determine if we should halt or not. */
5284 target
->reset_halt
= !!a
;
5285 /* When this happens - all workareas are invalid. */
5286 target_free_all_working_areas_restore(target
, 0);
5289 if (n
->value
== NVP_ASSERT
)
5290 e
= target
->type
->assert_reset(target
);
5292 e
= target
->type
->deassert_reset(target
);
5293 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5296 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5299 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5302 struct target
*target
= Jim_CmdPrivData(interp
);
5303 if (!target
->tap
->enabled
)
5304 return jim_target_tap_disabled(interp
);
5305 int e
= target
->type
->halt(target
);
5306 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5309 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5312 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5314 /* params: <name> statename timeoutmsecs */
5315 if (goi
.argc
!= 2) {
5316 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5317 Jim_SetResultFormatted(goi
.interp
,
5318 "%s <state_name> <timeout_in_msec>", cmd_name
);
5323 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
5325 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
, 1);
5329 e
= Jim_GetOpt_Wide(&goi
, &a
);
5332 struct target
*target
= Jim_CmdPrivData(interp
);
5333 if (!target
->tap
->enabled
)
5334 return jim_target_tap_disabled(interp
);
5336 e
= target_wait_state(target
, n
->value
, a
);
5337 if (e
!= ERROR_OK
) {
5338 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
5339 Jim_SetResultFormatted(goi
.interp
,
5340 "target: %s wait %s fails (%#s) %s",
5341 target_name(target
), n
->name
,
5342 eObj
, target_strerror_safe(e
));
5343 Jim_FreeNewObj(interp
, eObj
);
5348 /* List for human, Events defined for this target.
5349 * scripts/programs should use 'name cget -event NAME'
5351 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5353 struct command_context
*cmd_ctx
= current_command_context(interp
);
5354 assert(cmd_ctx
!= NULL
);
5356 struct target
*target
= Jim_CmdPrivData(interp
);
5357 struct target_event_action
*teap
= target
->event_action
;
5358 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
5359 target
->target_number
,
5360 target_name(target
));
5361 command_print(cmd_ctx
, "%-25s | Body", "Event");
5362 command_print(cmd_ctx
, "------------------------- | "
5363 "----------------------------------------");
5365 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
5366 command_print(cmd_ctx
, "%-25s | %s",
5367 opt
->name
, Jim_GetString(teap
->body
, NULL
));
5370 command_print(cmd_ctx
, "***END***");
5373 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5376 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5379 struct target
*target
= Jim_CmdPrivData(interp
);
5380 Jim_SetResultString(interp
, target_state_name(target
), -1);
5383 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5386 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5387 if (goi
.argc
!= 1) {
5388 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5389 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
5393 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
5395 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
5398 struct target
*target
= Jim_CmdPrivData(interp
);
5399 target_handle_event(target
, n
->value
);
5403 static const struct command_registration target_instance_command_handlers
[] = {
5405 .name
= "configure",
5406 .mode
= COMMAND_CONFIG
,
5407 .jim_handler
= jim_target_configure
,
5408 .help
= "configure a new target for use",
5409 .usage
= "[target_attribute ...]",
5413 .mode
= COMMAND_ANY
,
5414 .jim_handler
= jim_target_configure
,
5415 .help
= "returns the specified target attribute",
5416 .usage
= "target_attribute",
5420 .mode
= COMMAND_EXEC
,
5421 .jim_handler
= jim_target_mw
,
5422 .help
= "Write 32-bit word(s) to target memory",
5423 .usage
= "address data [count]",
5427 .mode
= COMMAND_EXEC
,
5428 .jim_handler
= jim_target_mw
,
5429 .help
= "Write 16-bit half-word(s) to target memory",
5430 .usage
= "address data [count]",
5434 .mode
= COMMAND_EXEC
,
5435 .jim_handler
= jim_target_mw
,
5436 .help
= "Write byte(s) to target memory",
5437 .usage
= "address data [count]",
5441 .mode
= COMMAND_EXEC
,
5442 .jim_handler
= jim_target_md
,
5443 .help
= "Display target memory as 32-bit words",
5444 .usage
= "address [count]",
5448 .mode
= COMMAND_EXEC
,
5449 .jim_handler
= jim_target_md
,
5450 .help
= "Display target memory as 16-bit half-words",
5451 .usage
= "address [count]",
5455 .mode
= COMMAND_EXEC
,
5456 .jim_handler
= jim_target_md
,
5457 .help
= "Display target memory as 8-bit bytes",
5458 .usage
= "address [count]",
5461 .name
= "array2mem",
5462 .mode
= COMMAND_EXEC
,
5463 .jim_handler
= jim_target_array2mem
,
5464 .help
= "Writes Tcl array of 8/16/32 bit numbers "
5466 .usage
= "arrayname bitwidth address count",
5469 .name
= "mem2array",
5470 .mode
= COMMAND_EXEC
,
5471 .jim_handler
= jim_target_mem2array
,
5472 .help
= "Loads Tcl array of 8/16/32 bit numbers "
5473 "from target memory",
5474 .usage
= "arrayname bitwidth address count",
5477 .name
= "eventlist",
5478 .mode
= COMMAND_EXEC
,
5479 .jim_handler
= jim_target_event_list
,
5480 .help
= "displays a table of events defined for this target",
5484 .mode
= COMMAND_EXEC
,
5485 .jim_handler
= jim_target_current_state
,
5486 .help
= "displays the current state of this target",
5489 .name
= "arp_examine",
5490 .mode
= COMMAND_EXEC
,
5491 .jim_handler
= jim_target_examine
,
5492 .help
= "used internally for reset processing",
5493 .usage
= "['allow-defer']",
5496 .name
= "was_examined",
5497 .mode
= COMMAND_EXEC
,
5498 .jim_handler
= jim_target_was_examined
,
5499 .help
= "used internally for reset processing",
5502 .name
= "examine_deferred",
5503 .mode
= COMMAND_EXEC
,
5504 .jim_handler
= jim_target_examine_deferred
,
5505 .help
= "used internally for reset processing",
5508 .name
= "arp_halt_gdb",
5509 .mode
= COMMAND_EXEC
,
5510 .jim_handler
= jim_target_halt_gdb
,
5511 .help
= "used internally for reset processing to halt GDB",
5515 .mode
= COMMAND_EXEC
,
5516 .jim_handler
= jim_target_poll
,
5517 .help
= "used internally for reset processing",
5520 .name
= "arp_reset",
5521 .mode
= COMMAND_EXEC
,
5522 .jim_handler
= jim_target_reset
,
5523 .help
= "used internally for reset processing",
5527 .mode
= COMMAND_EXEC
,
5528 .jim_handler
= jim_target_halt
,
5529 .help
= "used internally for reset processing",
5532 .name
= "arp_waitstate",
5533 .mode
= COMMAND_EXEC
,
5534 .jim_handler
= jim_target_wait_state
,
5535 .help
= "used internally for reset processing",
5538 .name
= "invoke-event",
5539 .mode
= COMMAND_EXEC
,
5540 .jim_handler
= jim_target_invoke_event
,
5541 .help
= "invoke handler for specified event",
5542 .usage
= "event_name",
5544 COMMAND_REGISTRATION_DONE
5547 static int target_create(Jim_GetOptInfo
*goi
)
5554 struct target
*target
;
5555 struct command_context
*cmd_ctx
;
5557 cmd_ctx
= current_command_context(goi
->interp
);
5558 assert(cmd_ctx
!= NULL
);
5560 if (goi
->argc
< 3) {
5561 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
5566 Jim_GetOpt_Obj(goi
, &new_cmd
);
5567 /* does this command exist? */
5568 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
5570 cp
= Jim_GetString(new_cmd
, NULL
);
5571 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
5576 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
5579 struct transport
*tr
= get_current_transport();
5580 if (tr
->override_target
) {
5581 e
= tr
->override_target(&cp
);
5582 if (e
!= ERROR_OK
) {
5583 LOG_ERROR("The selected transport doesn't support this target");
5586 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
5588 /* now does target type exist */
5589 for (x
= 0 ; target_types
[x
] ; x
++) {
5590 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
5595 /* check for deprecated name */
5596 if (target_types
[x
]->deprecated_name
) {
5597 if (0 == strcmp(cp
, target_types
[x
]->deprecated_name
)) {
5599 LOG_WARNING("target name is deprecated use: \'%s\'", target_types
[x
]->name
);
5604 if (target_types
[x
] == NULL
) {
5605 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
5606 for (x
= 0 ; target_types
[x
] ; x
++) {
5607 if (target_types
[x
+ 1]) {
5608 Jim_AppendStrings(goi
->interp
,
5609 Jim_GetResult(goi
->interp
),
5610 target_types
[x
]->name
,
5613 Jim_AppendStrings(goi
->interp
,
5614 Jim_GetResult(goi
->interp
),
5616 target_types
[x
]->name
, NULL
);
5623 target
= calloc(1, sizeof(struct target
));
5624 /* set target number */
5625 target
->target_number
= new_target_number();
5626 cmd_ctx
->current_target
= target
;
5628 /* allocate memory for each unique target type */
5629 target
->type
= calloc(1, sizeof(struct target_type
));
5631 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
5633 /* will be set by "-endian" */
5634 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5636 /* default to first core, override with -coreid */
5639 target
->working_area
= 0x0;
5640 target
->working_area_size
= 0x0;
5641 target
->working_areas
= NULL
;
5642 target
->backup_working_area
= 0;
5644 target
->state
= TARGET_UNKNOWN
;
5645 target
->debug_reason
= DBG_REASON_UNDEFINED
;
5646 target
->reg_cache
= NULL
;
5647 target
->breakpoints
= NULL
;
5648 target
->watchpoints
= NULL
;
5649 target
->next
= NULL
;
5650 target
->arch_info
= NULL
;
5652 target
->verbose_halt_msg
= true;
5654 target
->halt_issued
= false;
5656 /* initialize trace information */
5657 target
->trace_info
= calloc(1, sizeof(struct trace
));
5659 target
->dbgmsg
= NULL
;
5660 target
->dbg_msg_enabled
= 0;
5662 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5664 target
->rtos
= NULL
;
5665 target
->rtos_auto_detect
= false;
5667 target
->gdb_port_override
= NULL
;
5669 /* Do the rest as "configure" options */
5670 goi
->isconfigure
= 1;
5671 e
= target_configure(goi
, target
);
5674 if (target
->has_dap
) {
5675 if (!target
->dap_configured
) {
5676 Jim_SetResultString(goi
->interp
, "-dap ?name? required when creating target", -1);
5680 if (!target
->tap_configured
) {
5681 Jim_SetResultString(goi
->interp
, "-chain-position ?name? required when creating target", -1);
5685 /* tap must be set after target was configured */
5686 if (target
->tap
== NULL
)
5691 free(target
->gdb_port_override
);
5697 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
5698 /* default endian to little if not specified */
5699 target
->endianness
= TARGET_LITTLE_ENDIAN
;
5702 cp
= Jim_GetString(new_cmd
, NULL
);
5703 target
->cmd_name
= strdup(cp
);
5705 if (target
->type
->target_create
) {
5706 e
= (*(target
->type
->target_create
))(target
, goi
->interp
);
5707 if (e
!= ERROR_OK
) {
5708 LOG_DEBUG("target_create failed");
5709 free(target
->gdb_port_override
);
5711 free(target
->cmd_name
);
5717 /* create the target specific commands */
5718 if (target
->type
->commands
) {
5719 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
5721 LOG_ERROR("unable to register '%s' commands", cp
);
5724 /* append to end of list */
5726 struct target
**tpp
;
5727 tpp
= &(all_targets
);
5729 tpp
= &((*tpp
)->next
);
5733 /* now - create the new target name command */
5734 const struct command_registration target_subcommands
[] = {
5736 .chain
= target_instance_command_handlers
,
5739 .chain
= target
->type
->commands
,
5741 COMMAND_REGISTRATION_DONE
5743 const struct command_registration target_commands
[] = {
5746 .mode
= COMMAND_ANY
,
5747 .help
= "target command group",
5749 .chain
= target_subcommands
,
5751 COMMAND_REGISTRATION_DONE
5753 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
5757 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
5759 command_set_handler_data(c
, target
);
5761 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
5764 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5767 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5770 struct command_context
*cmd_ctx
= current_command_context(interp
);
5771 assert(cmd_ctx
!= NULL
);
5773 Jim_SetResultString(interp
, target_name(get_current_target(cmd_ctx
)), -1);
5777 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5780 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5783 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5784 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++) {
5785 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5786 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
5791 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5794 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5797 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5798 struct target
*target
= all_targets
;
5800 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5801 Jim_NewStringObj(interp
, target_name(target
), -1));
5802 target
= target
->next
;
5807 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5810 const char *targetname
;
5812 struct target
*target
= (struct target
*) NULL
;
5813 struct target_list
*head
, *curr
, *new;
5814 curr
= (struct target_list
*) NULL
;
5815 head
= (struct target_list
*) NULL
;
5818 LOG_DEBUG("%d", argc
);
5819 /* argv[1] = target to associate in smp
5820 * argv[2] = target to assoicate in smp
5824 for (i
= 1; i
< argc
; i
++) {
5826 targetname
= Jim_GetString(argv
[i
], &len
);
5827 target
= get_target(targetname
);
5828 LOG_DEBUG("%s ", targetname
);
5830 new = malloc(sizeof(struct target_list
));
5831 new->target
= target
;
5832 new->next
= (struct target_list
*)NULL
;
5833 if (head
== (struct target_list
*)NULL
) {
5842 /* now parse the list of cpu and put the target in smp mode*/
5845 while (curr
!= (struct target_list
*)NULL
) {
5846 target
= curr
->target
;
5848 target
->head
= head
;
5852 if (target
&& target
->rtos
)
5853 retval
= rtos_smp_init(head
->target
);
5859 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5862 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5864 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5865 "<name> <target_type> [<target_options> ...]");
5868 return target_create(&goi
);
5871 static const struct command_registration target_subcommand_handlers
[] = {
5874 .mode
= COMMAND_CONFIG
,
5875 .handler
= handle_target_init_command
,
5876 .help
= "initialize targets",
5880 .mode
= COMMAND_CONFIG
,
5881 .jim_handler
= jim_target_create
,
5882 .usage
= "name type '-chain-position' name [options ...]",
5883 .help
= "Creates and selects a new target",
5887 .mode
= COMMAND_ANY
,
5888 .jim_handler
= jim_target_current
,
5889 .help
= "Returns the currently selected target",
5893 .mode
= COMMAND_ANY
,
5894 .jim_handler
= jim_target_types
,
5895 .help
= "Returns the available target types as "
5896 "a list of strings",
5900 .mode
= COMMAND_ANY
,
5901 .jim_handler
= jim_target_names
,
5902 .help
= "Returns the names of all targets as a list of strings",
5906 .mode
= COMMAND_ANY
,
5907 .jim_handler
= jim_target_smp
,
5908 .usage
= "targetname1 targetname2 ...",
5909 .help
= "gather several target in a smp list"
5912 COMMAND_REGISTRATION_DONE
5916 target_addr_t address
;
5922 static int fastload_num
;
5923 static struct FastLoad
*fastload
;
5925 static void free_fastload(void)
5927 if (fastload
!= NULL
) {
5929 for (i
= 0; i
< fastload_num
; i
++) {
5930 if (fastload
[i
].data
)
5931 free(fastload
[i
].data
);
5938 COMMAND_HANDLER(handle_fast_load_image_command
)
5942 uint32_t image_size
;
5943 target_addr_t min_address
= 0;
5944 target_addr_t max_address
= -1;
5949 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
5950 &image
, &min_address
, &max_address
);
5951 if (ERROR_OK
!= retval
)
5954 struct duration bench
;
5955 duration_start(&bench
);
5957 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5958 if (retval
!= ERROR_OK
)
5963 fastload_num
= image
.num_sections
;
5964 fastload
= malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5965 if (fastload
== NULL
) {
5966 command_print(CMD_CTX
, "out of memory");
5967 image_close(&image
);
5970 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5971 for (i
= 0; i
< image
.num_sections
; i
++) {
5972 buffer
= malloc(image
.sections
[i
].size
);
5973 if (buffer
== NULL
) {
5974 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5975 (int)(image
.sections
[i
].size
));
5976 retval
= ERROR_FAIL
;
5980 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
5981 if (retval
!= ERROR_OK
) {
5986 uint32_t offset
= 0;
5987 uint32_t length
= buf_cnt
;
5989 /* DANGER!!! beware of unsigned comparision here!!! */
5991 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
5992 (image
.sections
[i
].base_address
< max_address
)) {
5993 if (image
.sections
[i
].base_address
< min_address
) {
5994 /* clip addresses below */
5995 offset
+= min_address
-image
.sections
[i
].base_address
;
5999 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
6000 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
6002 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
6003 fastload
[i
].data
= malloc(length
);
6004 if (fastload
[i
].data
== NULL
) {
6006 command_print(CMD_CTX
, "error allocating buffer for section (%" PRIu32
" bytes)",
6008 retval
= ERROR_FAIL
;
6011 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
6012 fastload
[i
].length
= length
;
6014 image_size
+= length
;
6015 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
6016 (unsigned int)length
,
6017 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
6023 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
6024 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
6025 "in %fs (%0.3f KiB/s)", image_size
,
6026 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
6028 command_print(CMD_CTX
,
6029 "WARNING: image has not been loaded to target!"
6030 "You can issue a 'fast_load' to finish loading.");
6033 image_close(&image
);
6035 if (retval
!= ERROR_OK
)
6041 COMMAND_HANDLER(handle_fast_load_command
)
6044 return ERROR_COMMAND_SYNTAX_ERROR
;
6045 if (fastload
== NULL
) {
6046 LOG_ERROR("No image in memory");
6050 int64_t ms
= timeval_ms();
6052 int retval
= ERROR_OK
;
6053 for (i
= 0; i
< fastload_num
; i
++) {
6054 struct target
*target
= get_current_target(CMD_CTX
);
6055 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
6056 (unsigned int)(fastload
[i
].address
),
6057 (unsigned int)(fastload
[i
].length
));
6058 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
6059 if (retval
!= ERROR_OK
)
6061 size
+= fastload
[i
].length
;
6063 if (retval
== ERROR_OK
) {
6064 int64_t after
= timeval_ms();
6065 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
6070 static const struct command_registration target_command_handlers
[] = {
6073 .handler
= handle_targets_command
,
6074 .mode
= COMMAND_ANY
,
6075 .help
= "change current default target (one parameter) "
6076 "or prints table of all targets (no parameters)",
6077 .usage
= "[target]",
6081 .mode
= COMMAND_CONFIG
,
6082 .help
= "configure target",
6084 .chain
= target_subcommand_handlers
,
6086 COMMAND_REGISTRATION_DONE
6089 int target_register_commands(struct command_context
*cmd_ctx
)
6091 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
6094 static bool target_reset_nag
= true;
6096 bool get_target_reset_nag(void)
6098 return target_reset_nag
;
6101 COMMAND_HANDLER(handle_target_reset_nag
)
6103 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
6104 &target_reset_nag
, "Nag after each reset about options to improve "
6108 COMMAND_HANDLER(handle_ps_command
)
6110 struct target
*target
= get_current_target(CMD_CTX
);
6112 if (target
->state
!= TARGET_HALTED
) {
6113 LOG_INFO("target not halted !!");
6117 if ((target
->rtos
) && (target
->rtos
->type
)
6118 && (target
->rtos
->type
->ps_command
)) {
6119 display
= target
->rtos
->type
->ps_command(target
);
6120 command_print(CMD_CTX
, "%s", display
);
6125 return ERROR_TARGET_FAILURE
;
6129 static void binprint(struct command_context
*cmd_ctx
, const char *text
, const uint8_t *buf
, int size
)
6132 command_print_sameline(cmd_ctx
, "%s", text
);
6133 for (int i
= 0; i
< size
; i
++)
6134 command_print_sameline(cmd_ctx
, " %02x", buf
[i
]);
6135 command_print(cmd_ctx
, " ");
6138 COMMAND_HANDLER(handle_test_mem_access_command
)
6140 struct target
*target
= get_current_target(CMD_CTX
);
6142 int retval
= ERROR_OK
;
6144 if (target
->state
!= TARGET_HALTED
) {
6145 LOG_INFO("target not halted !!");
6150 return ERROR_COMMAND_SYNTAX_ERROR
;
6152 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], test_size
);
6155 size_t num_bytes
= test_size
+ 4;
6157 struct working_area
*wa
= NULL
;
6158 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6159 if (retval
!= ERROR_OK
) {
6160 LOG_ERROR("Not enough working area");
6164 uint8_t *test_pattern
= malloc(num_bytes
);
6166 for (size_t i
= 0; i
< num_bytes
; i
++)
6167 test_pattern
[i
] = rand();
6169 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6170 if (retval
!= ERROR_OK
) {
6171 LOG_ERROR("Test pattern write failed");
6175 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6176 for (int size
= 1; size
<= 4; size
*= 2) {
6177 for (int offset
= 0; offset
< 4; offset
++) {
6178 uint32_t count
= test_size
/ size
;
6179 size_t host_bufsiz
= (count
+ 2) * size
+ host_offset
;
6180 uint8_t *read_ref
= malloc(host_bufsiz
);
6181 uint8_t *read_buf
= malloc(host_bufsiz
);
6183 for (size_t i
= 0; i
< host_bufsiz
; i
++) {
6184 read_ref
[i
] = rand();
6185 read_buf
[i
] = read_ref
[i
];
6187 command_print_sameline(CMD_CTX
,
6188 "Test read %" PRIu32
" x %d @ %d to %saligned buffer: ", count
,
6189 size
, offset
, host_offset
? "un" : "");
6191 struct duration bench
;
6192 duration_start(&bench
);
6194 retval
= target_read_memory(target
, wa
->address
+ offset
, size
, count
,
6195 read_buf
+ size
+ host_offset
);
6197 duration_measure(&bench
);
6199 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6200 command_print(CMD_CTX
, "Unsupported alignment");
6202 } else if (retval
!= ERROR_OK
) {
6203 command_print(CMD_CTX
, "Memory read failed");
6207 /* replay on host */
6208 memcpy(read_ref
+ size
+ host_offset
, test_pattern
+ offset
, count
* size
);
6211 int result
= memcmp(read_ref
, read_buf
, host_bufsiz
);
6213 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
6214 duration_elapsed(&bench
),
6215 duration_kbps(&bench
, count
* size
));
6217 command_print(CMD_CTX
, "Compare failed");
6218 binprint(CMD_CTX
, "ref:", read_ref
, host_bufsiz
);
6219 binprint(CMD_CTX
, "buf:", read_buf
, host_bufsiz
);
6232 target_free_working_area(target
, wa
);
6235 num_bytes
= test_size
+ 4 + 4 + 4;
6237 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6238 if (retval
!= ERROR_OK
) {
6239 LOG_ERROR("Not enough working area");
6243 test_pattern
= malloc(num_bytes
);
6245 for (size_t i
= 0; i
< num_bytes
; i
++)
6246 test_pattern
[i
] = rand();
6248 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6249 for (int size
= 1; size
<= 4; size
*= 2) {
6250 for (int offset
= 0; offset
< 4; offset
++) {
6251 uint32_t count
= test_size
/ size
;
6252 size_t host_bufsiz
= count
* size
+ host_offset
;
6253 uint8_t *read_ref
= malloc(num_bytes
);
6254 uint8_t *read_buf
= malloc(num_bytes
);
6255 uint8_t *write_buf
= malloc(host_bufsiz
);
6257 for (size_t i
= 0; i
< host_bufsiz
; i
++)
6258 write_buf
[i
] = rand();
6259 command_print_sameline(CMD_CTX
,
6260 "Test write %" PRIu32
" x %d @ %d from %saligned buffer: ", count
,
6261 size
, offset
, host_offset
? "un" : "");
6263 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6264 if (retval
!= ERROR_OK
) {
6265 command_print(CMD_CTX
, "Test pattern write failed");
6269 /* replay on host */
6270 memcpy(read_ref
, test_pattern
, num_bytes
);
6271 memcpy(read_ref
+ size
+ offset
, write_buf
+ host_offset
, count
* size
);
6273 struct duration bench
;
6274 duration_start(&bench
);
6276 retval
= target_write_memory(target
, wa
->address
+ size
+ offset
, size
, count
,
6277 write_buf
+ host_offset
);
6279 duration_measure(&bench
);
6281 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6282 command_print(CMD_CTX
, "Unsupported alignment");
6284 } else if (retval
!= ERROR_OK
) {
6285 command_print(CMD_CTX
, "Memory write failed");
6290 retval
= target_read_memory(target
, wa
->address
, 1, num_bytes
, read_buf
);
6291 if (retval
!= ERROR_OK
) {
6292 command_print(CMD_CTX
, "Test pattern write failed");
6297 int result
= memcmp(read_ref
, read_buf
, num_bytes
);
6299 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
6300 duration_elapsed(&bench
),
6301 duration_kbps(&bench
, count
* size
));
6303 command_print(CMD_CTX
, "Compare failed");
6304 binprint(CMD_CTX
, "ref:", read_ref
, num_bytes
);
6305 binprint(CMD_CTX
, "buf:", read_buf
, num_bytes
);
6317 target_free_working_area(target
, wa
);
6321 static const struct command_registration target_exec_command_handlers
[] = {
6323 .name
= "fast_load_image",
6324 .handler
= handle_fast_load_image_command
,
6325 .mode
= COMMAND_ANY
,
6326 .help
= "Load image into server memory for later use by "
6327 "fast_load; primarily for profiling",
6328 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6329 "[min_address [max_length]]",
6332 .name
= "fast_load",
6333 .handler
= handle_fast_load_command
,
6334 .mode
= COMMAND_EXEC
,
6335 .help
= "loads active fast load image to current target "
6336 "- mainly for profiling purposes",
6341 .handler
= handle_profile_command
,
6342 .mode
= COMMAND_EXEC
,
6343 .usage
= "seconds filename [start end]",
6344 .help
= "profiling samples the CPU PC",
6346 /** @todo don't register virt2phys() unless target supports it */
6348 .name
= "virt2phys",
6349 .handler
= handle_virt2phys_command
,
6350 .mode
= COMMAND_ANY
,
6351 .help
= "translate a virtual address into a physical address",
6352 .usage
= "virtual_address",
6356 .handler
= handle_reg_command
,
6357 .mode
= COMMAND_EXEC
,
6358 .help
= "display (reread from target with \"force\") or set a register; "
6359 "with no arguments, displays all registers and their values",
6360 .usage
= "[(register_number|register_name) [(value|'force')]]",
6364 .handler
= handle_poll_command
,
6365 .mode
= COMMAND_EXEC
,
6366 .help
= "poll target state; or reconfigure background polling",
6367 .usage
= "['on'|'off']",
6370 .name
= "wait_halt",
6371 .handler
= handle_wait_halt_command
,
6372 .mode
= COMMAND_EXEC
,
6373 .help
= "wait up to the specified number of milliseconds "
6374 "(default 5000) for a previously requested halt",
6375 .usage
= "[milliseconds]",
6379 .handler
= handle_halt_command
,
6380 .mode
= COMMAND_EXEC
,
6381 .help
= "request target to halt, then wait up to the specified"
6382 "number of milliseconds (default 5000) for it to complete",
6383 .usage
= "[milliseconds]",
6387 .handler
= handle_resume_command
,
6388 .mode
= COMMAND_EXEC
,
6389 .help
= "resume target execution from current PC or address",
6390 .usage
= "[address]",
6394 .handler
= handle_reset_command
,
6395 .mode
= COMMAND_EXEC
,
6396 .usage
= "[run|halt|init]",
6397 .help
= "Reset all targets into the specified mode."
6398 "Default reset mode is run, if not given.",
6401 .name
= "soft_reset_halt",
6402 .handler
= handle_soft_reset_halt_command
,
6403 .mode
= COMMAND_EXEC
,
6405 .help
= "halt the target and do a soft reset",
6409 .handler
= handle_step_command
,
6410 .mode
= COMMAND_EXEC
,
6411 .help
= "step one instruction from current PC or address",
6412 .usage
= "[address]",
6416 .handler
= handle_md_command
,
6417 .mode
= COMMAND_EXEC
,
6418 .help
= "display memory words",
6419 .usage
= "['phys'] address [count]",
6423 .handler
= handle_md_command
,
6424 .mode
= COMMAND_EXEC
,
6425 .help
= "display memory words",
6426 .usage
= "['phys'] address [count]",
6430 .handler
= handle_md_command
,
6431 .mode
= COMMAND_EXEC
,
6432 .help
= "display memory half-words",
6433 .usage
= "['phys'] address [count]",
6437 .handler
= handle_md_command
,
6438 .mode
= COMMAND_EXEC
,
6439 .help
= "display memory bytes",
6440 .usage
= "['phys'] address [count]",
6444 .handler
= handle_mw_command
,
6445 .mode
= COMMAND_EXEC
,
6446 .help
= "write memory word",
6447 .usage
= "['phys'] address value [count]",
6451 .handler
= handle_mw_command
,
6452 .mode
= COMMAND_EXEC
,
6453 .help
= "write memory word",
6454 .usage
= "['phys'] address value [count]",
6458 .handler
= handle_mw_command
,
6459 .mode
= COMMAND_EXEC
,
6460 .help
= "write memory half-word",
6461 .usage
= "['phys'] address value [count]",
6465 .handler
= handle_mw_command
,
6466 .mode
= COMMAND_EXEC
,
6467 .help
= "write memory byte",
6468 .usage
= "['phys'] address value [count]",
6472 .handler
= handle_bp_command
,
6473 .mode
= COMMAND_EXEC
,
6474 .help
= "list or set hardware or software breakpoint",
6475 .usage
= "<address> [<asid>] <length> ['hw'|'hw_ctx']",
6479 .handler
= handle_rbp_command
,
6480 .mode
= COMMAND_EXEC
,
6481 .help
= "remove breakpoint",
6486 .handler
= handle_wp_command
,
6487 .mode
= COMMAND_EXEC
,
6488 .help
= "list (no params) or create watchpoints",
6489 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
6493 .handler
= handle_rwp_command
,
6494 .mode
= COMMAND_EXEC
,
6495 .help
= "remove watchpoint",
6499 .name
= "load_image",
6500 .handler
= handle_load_image_command
,
6501 .mode
= COMMAND_EXEC
,
6502 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6503 "[min_address] [max_length]",
6506 .name
= "dump_image",
6507 .handler
= handle_dump_image_command
,
6508 .mode
= COMMAND_EXEC
,
6509 .usage
= "filename address size",
6512 .name
= "verify_image_checksum",
6513 .handler
= handle_verify_image_checksum_command
,
6514 .mode
= COMMAND_EXEC
,
6515 .usage
= "filename [offset [type]]",
6518 .name
= "verify_image",
6519 .handler
= handle_verify_image_command
,
6520 .mode
= COMMAND_EXEC
,
6521 .usage
= "filename [offset [type]]",
6524 .name
= "test_image",
6525 .handler
= handle_test_image_command
,
6526 .mode
= COMMAND_EXEC
,
6527 .usage
= "filename [offset [type]]",
6530 .name
= "mem2array",
6531 .mode
= COMMAND_EXEC
,
6532 .jim_handler
= jim_mem2array
,
6533 .help
= "read 8/16/32 bit memory and return as a TCL array "
6534 "for script processing",
6535 .usage
= "arrayname bitwidth address count",
6538 .name
= "array2mem",
6539 .mode
= COMMAND_EXEC
,
6540 .jim_handler
= jim_array2mem
,
6541 .help
= "convert a TCL array to memory locations "
6542 "and write the 8/16/32 bit values",
6543 .usage
= "arrayname bitwidth address count",
6546 .name
= "reset_nag",
6547 .handler
= handle_target_reset_nag
,
6548 .mode
= COMMAND_ANY
,
6549 .help
= "Nag after each reset about options that could have been "
6550 "enabled to improve performance. ",
6551 .usage
= "['enable'|'disable']",
6555 .handler
= handle_ps_command
,
6556 .mode
= COMMAND_EXEC
,
6557 .help
= "list all tasks ",
6561 .name
= "test_mem_access",
6562 .handler
= handle_test_mem_access_command
,
6563 .mode
= COMMAND_EXEC
,
6564 .help
= "Test the target's memory access functions",
6568 COMMAND_REGISTRATION_DONE
6570 static int target_register_user_commands(struct command_context
*cmd_ctx
)
6572 int retval
= ERROR_OK
;
6573 retval
= target_request_register_commands(cmd_ctx
);
6574 if (retval
!= ERROR_OK
)
6577 retval
= trace_register_commands(cmd_ctx
);
6578 if (retval
!= ERROR_OK
)
6582 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);