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 __attribute__((unused
)) 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_invocation
*cmd
, 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(cmd
->ctx
, "%s", 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 target_addr_t
target_address_max(struct target
*target
)
1262 unsigned bits
= target_address_bits(target
);
1263 if (sizeof(target_addr_t
) * 8 == bits
)
1264 return (target_addr_t
) -1;
1266 return (((target_addr_t
) 1) << bits
) - 1;
1269 unsigned target_address_bits(struct target
*target
)
1271 if (target
->type
->address_bits
)
1272 return target
->type
->address_bits(target
);
1276 int target_profiling(struct target
*target
, uint32_t *samples
,
1277 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1279 if (target
->state
!= TARGET_HALTED
) {
1280 LOG_WARNING("target %s is not halted (profiling)", target
->cmd_name
);
1281 return ERROR_TARGET_NOT_HALTED
;
1283 return target
->type
->profiling(target
, samples
, max_num_samples
,
1284 num_samples
, seconds
);
1288 * Reset the @c examined flag for the given target.
1289 * Pure paranoia -- targets are zeroed on allocation.
1291 static void target_reset_examined(struct target
*target
)
1293 target
->examined
= false;
1296 static int handle_target(void *priv
);
1298 static int target_init_one(struct command_context
*cmd_ctx
,
1299 struct target
*target
)
1301 target_reset_examined(target
);
1303 struct target_type
*type
= target
->type
;
1304 if (type
->examine
== NULL
)
1305 type
->examine
= default_examine
;
1307 if (type
->check_reset
== NULL
)
1308 type
->check_reset
= default_check_reset
;
1310 assert(type
->init_target
!= NULL
);
1312 int retval
= type
->init_target(cmd_ctx
, target
);
1313 if (ERROR_OK
!= retval
) {
1314 LOG_ERROR("target '%s' init failed", target_name(target
));
1318 /* Sanity-check MMU support ... stub in what we must, to help
1319 * implement it in stages, but warn if we need to do so.
1322 if (type
->virt2phys
== NULL
) {
1323 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1324 type
->virt2phys
= identity_virt2phys
;
1327 /* Make sure no-MMU targets all behave the same: make no
1328 * distinction between physical and virtual addresses, and
1329 * ensure that virt2phys() is always an identity mapping.
1331 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1332 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1335 type
->write_phys_memory
= type
->write_memory
;
1336 type
->read_phys_memory
= type
->read_memory
;
1337 type
->virt2phys
= identity_virt2phys
;
1340 if (target
->type
->read_buffer
== NULL
)
1341 target
->type
->read_buffer
= target_read_buffer_default
;
1343 if (target
->type
->write_buffer
== NULL
)
1344 target
->type
->write_buffer
= target_write_buffer_default
;
1346 if (target
->type
->get_gdb_fileio_info
== NULL
)
1347 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1349 if (target
->type
->gdb_fileio_end
== NULL
)
1350 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1352 if (target
->type
->profiling
== NULL
)
1353 target
->type
->profiling
= target_profiling_default
;
1358 static int target_init(struct command_context
*cmd_ctx
)
1360 struct target
*target
;
1363 for (target
= all_targets
; target
; target
= target
->next
) {
1364 retval
= target_init_one(cmd_ctx
, target
);
1365 if (ERROR_OK
!= retval
)
1372 retval
= target_register_user_commands(cmd_ctx
);
1373 if (ERROR_OK
!= retval
)
1376 retval
= target_register_timer_callback(&handle_target
,
1377 polling_interval
, TARGET_TIMER_TYPE_PERIODIC
, cmd_ctx
->interp
);
1378 if (ERROR_OK
!= retval
)
1384 COMMAND_HANDLER(handle_target_init_command
)
1389 return ERROR_COMMAND_SYNTAX_ERROR
;
1391 static bool target_initialized
;
1392 if (target_initialized
) {
1393 LOG_INFO("'target init' has already been called");
1396 target_initialized
= true;
1398 retval
= command_run_line(CMD_CTX
, "init_targets");
1399 if (ERROR_OK
!= retval
)
1402 retval
= command_run_line(CMD_CTX
, "init_target_events");
1403 if (ERROR_OK
!= retval
)
1406 retval
= command_run_line(CMD_CTX
, "init_board");
1407 if (ERROR_OK
!= retval
)
1410 LOG_DEBUG("Initializing targets...");
1411 return target_init(CMD_CTX
);
1414 int target_register_event_callback(int (*callback
)(struct target
*target
,
1415 enum target_event event
, void *priv
), void *priv
)
1417 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1419 if (callback
== NULL
)
1420 return ERROR_COMMAND_SYNTAX_ERROR
;
1423 while ((*callbacks_p
)->next
)
1424 callbacks_p
= &((*callbacks_p
)->next
);
1425 callbacks_p
= &((*callbacks_p
)->next
);
1428 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1429 (*callbacks_p
)->callback
= callback
;
1430 (*callbacks_p
)->priv
= priv
;
1431 (*callbacks_p
)->next
= NULL
;
1436 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1437 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1439 struct target_reset_callback
*entry
;
1441 if (callback
== NULL
)
1442 return ERROR_COMMAND_SYNTAX_ERROR
;
1444 entry
= malloc(sizeof(struct target_reset_callback
));
1445 if (entry
== NULL
) {
1446 LOG_ERROR("error allocating buffer for reset callback entry");
1447 return ERROR_COMMAND_SYNTAX_ERROR
;
1450 entry
->callback
= callback
;
1452 list_add(&entry
->list
, &target_reset_callback_list
);
1458 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1459 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1461 struct target_trace_callback
*entry
;
1463 if (callback
== NULL
)
1464 return ERROR_COMMAND_SYNTAX_ERROR
;
1466 entry
= malloc(sizeof(struct target_trace_callback
));
1467 if (entry
== NULL
) {
1468 LOG_ERROR("error allocating buffer for trace callback entry");
1469 return ERROR_COMMAND_SYNTAX_ERROR
;
1472 entry
->callback
= callback
;
1474 list_add(&entry
->list
, &target_trace_callback_list
);
1480 int target_register_timer_callback(int (*callback
)(void *priv
),
1481 unsigned int time_ms
, enum target_timer_type type
, void *priv
)
1483 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1485 if (callback
== NULL
)
1486 return ERROR_COMMAND_SYNTAX_ERROR
;
1489 while ((*callbacks_p
)->next
)
1490 callbacks_p
= &((*callbacks_p
)->next
);
1491 callbacks_p
= &((*callbacks_p
)->next
);
1494 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1495 (*callbacks_p
)->callback
= callback
;
1496 (*callbacks_p
)->type
= type
;
1497 (*callbacks_p
)->time_ms
= time_ms
;
1498 (*callbacks_p
)->removed
= false;
1500 gettimeofday(&(*callbacks_p
)->when
, NULL
);
1501 timeval_add_time(&(*callbacks_p
)->when
, 0, time_ms
* 1000);
1503 (*callbacks_p
)->priv
= priv
;
1504 (*callbacks_p
)->next
= NULL
;
1509 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1510 enum target_event event
, void *priv
), void *priv
)
1512 struct target_event_callback
**p
= &target_event_callbacks
;
1513 struct target_event_callback
*c
= target_event_callbacks
;
1515 if (callback
== NULL
)
1516 return ERROR_COMMAND_SYNTAX_ERROR
;
1519 struct target_event_callback
*next
= c
->next
;
1520 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1532 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1533 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1535 struct target_reset_callback
*entry
;
1537 if (callback
== NULL
)
1538 return ERROR_COMMAND_SYNTAX_ERROR
;
1540 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1541 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1542 list_del(&entry
->list
);
1551 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1552 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1554 struct target_trace_callback
*entry
;
1556 if (callback
== NULL
)
1557 return ERROR_COMMAND_SYNTAX_ERROR
;
1559 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1560 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1561 list_del(&entry
->list
);
1570 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1572 if (callback
== NULL
)
1573 return ERROR_COMMAND_SYNTAX_ERROR
;
1575 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1577 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1586 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1588 struct target_event_callback
*callback
= target_event_callbacks
;
1589 struct target_event_callback
*next_callback
;
1591 if (event
== TARGET_EVENT_HALTED
) {
1592 /* execute early halted first */
1593 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1596 LOG_DEBUG("target event %i (%s)", event
,
1597 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1599 target_handle_event(target
, event
);
1602 next_callback
= callback
->next
;
1603 callback
->callback(target
, event
, callback
->priv
);
1604 callback
= next_callback
;
1610 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1612 struct target_reset_callback
*callback
;
1614 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1615 Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1617 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1618 callback
->callback(target
, reset_mode
, callback
->priv
);
1623 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1625 struct target_trace_callback
*callback
;
1627 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1628 callback
->callback(target
, len
, data
, callback
->priv
);
1633 static int target_timer_callback_periodic_restart(
1634 struct target_timer_callback
*cb
, struct timeval
*now
)
1637 timeval_add_time(&cb
->when
, 0, cb
->time_ms
* 1000L);
1641 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1642 struct timeval
*now
)
1644 cb
->callback(cb
->priv
);
1646 if (cb
->type
== TARGET_TIMER_TYPE_PERIODIC
)
1647 return target_timer_callback_periodic_restart(cb
, now
);
1649 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1652 static int target_call_timer_callbacks_check_time(int checktime
)
1654 static bool callback_processing
;
1656 /* Do not allow nesting */
1657 if (callback_processing
)
1660 callback_processing
= true;
1665 gettimeofday(&now
, NULL
);
1667 /* Store an address of the place containing a pointer to the
1668 * next item; initially, that's a standalone "root of the
1669 * list" variable. */
1670 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1672 if ((*callback
)->removed
) {
1673 struct target_timer_callback
*p
= *callback
;
1674 *callback
= (*callback
)->next
;
1679 bool call_it
= (*callback
)->callback
&&
1680 ((!checktime
&& (*callback
)->type
== TARGET_TIMER_TYPE_PERIODIC
) ||
1681 timeval_compare(&now
, &(*callback
)->when
) >= 0);
1684 target_call_timer_callback(*callback
, &now
);
1686 callback
= &(*callback
)->next
;
1689 callback_processing
= false;
1693 int target_call_timer_callbacks(void)
1695 return target_call_timer_callbacks_check_time(1);
1698 /* invoke periodic callbacks immediately */
1699 int target_call_timer_callbacks_now(void)
1701 return target_call_timer_callbacks_check_time(0);
1704 /* Prints the working area layout for debug purposes */
1705 static void print_wa_layout(struct target
*target
)
1707 struct working_area
*c
= target
->working_areas
;
1710 LOG_DEBUG("%c%c " TARGET_ADDR_FMT
"-" TARGET_ADDR_FMT
" (%" PRIu32
" bytes)",
1711 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1712 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1717 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1718 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1720 assert(area
->free
); /* Shouldn't split an allocated area */
1721 assert(size
<= area
->size
); /* Caller should guarantee this */
1723 /* Split only if not already the right size */
1724 if (size
< area
->size
) {
1725 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1730 new_wa
->next
= area
->next
;
1731 new_wa
->size
= area
->size
- size
;
1732 new_wa
->address
= area
->address
+ size
;
1733 new_wa
->backup
= NULL
;
1734 new_wa
->user
= NULL
;
1735 new_wa
->free
= true;
1737 area
->next
= new_wa
;
1740 /* If backup memory was allocated to this area, it has the wrong size
1741 * now so free it and it will be reallocated if/when needed */
1744 area
->backup
= NULL
;
1749 /* Merge all adjacent free areas into one */
1750 static void target_merge_working_areas(struct target
*target
)
1752 struct working_area
*c
= target
->working_areas
;
1754 while (c
&& c
->next
) {
1755 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1757 /* Find two adjacent free areas */
1758 if (c
->free
&& c
->next
->free
) {
1759 /* Merge the last into the first */
1760 c
->size
+= c
->next
->size
;
1762 /* Remove the last */
1763 struct working_area
*to_be_freed
= c
->next
;
1764 c
->next
= c
->next
->next
;
1765 if (to_be_freed
->backup
)
1766 free(to_be_freed
->backup
);
1769 /* If backup memory was allocated to the remaining area, it's has
1770 * the wrong size now */
1781 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1783 /* Reevaluate working area address based on MMU state*/
1784 if (target
->working_areas
== NULL
) {
1788 retval
= target
->type
->mmu(target
, &enabled
);
1789 if (retval
!= ERROR_OK
)
1793 if (target
->working_area_phys_spec
) {
1794 LOG_DEBUG("MMU disabled, using physical "
1795 "address for working memory " TARGET_ADDR_FMT
,
1796 target
->working_area_phys
);
1797 target
->working_area
= target
->working_area_phys
;
1799 LOG_ERROR("No working memory available. "
1800 "Specify -work-area-phys to target.");
1801 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1804 if (target
->working_area_virt_spec
) {
1805 LOG_DEBUG("MMU enabled, using virtual "
1806 "address for working memory " TARGET_ADDR_FMT
,
1807 target
->working_area_virt
);
1808 target
->working_area
= target
->working_area_virt
;
1810 LOG_ERROR("No working memory available. "
1811 "Specify -work-area-virt to target.");
1812 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1816 /* Set up initial working area on first call */
1817 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1819 new_wa
->next
= NULL
;
1820 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1821 new_wa
->address
= target
->working_area
;
1822 new_wa
->backup
= NULL
;
1823 new_wa
->user
= NULL
;
1824 new_wa
->free
= true;
1827 target
->working_areas
= new_wa
;
1830 /* only allocate multiples of 4 byte */
1832 size
= (size
+ 3) & (~3UL);
1834 struct working_area
*c
= target
->working_areas
;
1836 /* Find the first large enough working area */
1838 if (c
->free
&& c
->size
>= size
)
1844 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1846 /* Split the working area into the requested size */
1847 target_split_working_area(c
, size
);
1849 LOG_DEBUG("allocated new working area of %" PRIu32
" bytes at address " TARGET_ADDR_FMT
,
1852 if (target
->backup_working_area
) {
1853 if (c
->backup
== NULL
) {
1854 c
->backup
= malloc(c
->size
);
1855 if (c
->backup
== NULL
)
1859 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1860 if (retval
!= ERROR_OK
)
1864 /* mark as used, and return the new (reused) area */
1871 print_wa_layout(target
);
1876 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1880 retval
= target_alloc_working_area_try(target
, size
, area
);
1881 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1882 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1887 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1889 int retval
= ERROR_OK
;
1891 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1892 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1893 if (retval
!= ERROR_OK
)
1894 LOG_ERROR("failed to restore %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1895 area
->size
, area
->address
);
1901 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1902 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1904 int retval
= ERROR_OK
;
1910 retval
= target_restore_working_area(target
, area
);
1911 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1912 if (retval
!= ERROR_OK
)
1918 LOG_DEBUG("freed %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1919 area
->size
, area
->address
);
1921 /* mark user pointer invalid */
1922 /* TODO: Is this really safe? It points to some previous caller's memory.
1923 * How could we know that the area pointer is still in that place and not
1924 * some other vital data? What's the purpose of this, anyway? */
1928 target_merge_working_areas(target
);
1930 print_wa_layout(target
);
1935 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1937 return target_free_working_area_restore(target
, area
, 1);
1940 /* free resources and restore memory, if restoring memory fails,
1941 * free up resources anyway
1943 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1945 struct working_area
*c
= target
->working_areas
;
1947 LOG_DEBUG("freeing all working areas");
1949 /* Loop through all areas, restoring the allocated ones and marking them as free */
1953 target_restore_working_area(target
, c
);
1955 *c
->user
= NULL
; /* Same as above */
1961 /* Run a merge pass to combine all areas into one */
1962 target_merge_working_areas(target
);
1964 print_wa_layout(target
);
1967 void target_free_all_working_areas(struct target
*target
)
1969 target_free_all_working_areas_restore(target
, 1);
1971 /* Now we have none or only one working area marked as free */
1972 if (target
->working_areas
) {
1973 /* Free the last one to allow on-the-fly moving and resizing */
1974 free(target
->working_areas
->backup
);
1975 free(target
->working_areas
);
1976 target
->working_areas
= NULL
;
1980 /* Find the largest number of bytes that can be allocated */
1981 uint32_t target_get_working_area_avail(struct target
*target
)
1983 struct working_area
*c
= target
->working_areas
;
1984 uint32_t max_size
= 0;
1987 return target
->working_area_size
;
1990 if (c
->free
&& max_size
< c
->size
)
1999 static void target_destroy(struct target
*target
)
2001 if (target
->type
->deinit_target
)
2002 target
->type
->deinit_target(target
);
2004 if (target
->semihosting
)
2005 free(target
->semihosting
);
2007 jtag_unregister_event_callback(jtag_enable_callback
, target
);
2009 struct target_event_action
*teap
= target
->event_action
;
2011 struct target_event_action
*next
= teap
->next
;
2012 Jim_DecrRefCount(teap
->interp
, teap
->body
);
2017 target_free_all_working_areas(target
);
2019 /* release the targets SMP list */
2021 struct target_list
*head
= target
->head
;
2022 while (head
!= NULL
) {
2023 struct target_list
*pos
= head
->next
;
2024 head
->target
->smp
= 0;
2031 free(target
->gdb_port_override
);
2033 free(target
->trace_info
);
2034 free(target
->fileio_info
);
2035 free(target
->cmd_name
);
2039 void target_quit(void)
2041 struct target_event_callback
*pe
= target_event_callbacks
;
2043 struct target_event_callback
*t
= pe
->next
;
2047 target_event_callbacks
= NULL
;
2049 struct target_timer_callback
*pt
= target_timer_callbacks
;
2051 struct target_timer_callback
*t
= pt
->next
;
2055 target_timer_callbacks
= NULL
;
2057 for (struct target
*target
= all_targets
; target
;) {
2061 target_destroy(target
);
2068 int target_arch_state(struct target
*target
)
2071 if (target
== NULL
) {
2072 LOG_WARNING("No target has been configured");
2076 if (target
->state
!= TARGET_HALTED
)
2079 retval
= target
->type
->arch_state(target
);
2083 static int target_get_gdb_fileio_info_default(struct target
*target
,
2084 struct gdb_fileio_info
*fileio_info
)
2086 /* If target does not support semi-hosting function, target
2087 has no need to provide .get_gdb_fileio_info callback.
2088 It just return ERROR_FAIL and gdb_server will return "Txx"
2089 as target halted every time. */
2093 static int target_gdb_fileio_end_default(struct target
*target
,
2094 int retcode
, int fileio_errno
, bool ctrl_c
)
2099 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
2100 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
2102 struct timeval timeout
, now
;
2104 gettimeofday(&timeout
, NULL
);
2105 timeval_add_time(&timeout
, seconds
, 0);
2107 LOG_INFO("Starting profiling. Halting and resuming the"
2108 " target as often as we can...");
2110 uint32_t sample_count
= 0;
2111 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2112 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2114 int retval
= ERROR_OK
;
2116 target_poll(target
);
2117 if (target
->state
== TARGET_HALTED
) {
2118 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
2119 samples
[sample_count
++] = t
;
2120 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2121 retval
= target_resume(target
, 1, 0, 0, 0);
2122 target_poll(target
);
2123 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2124 } else if (target
->state
== TARGET_RUNNING
) {
2125 /* We want to quickly sample the PC. */
2126 retval
= target_halt(target
);
2128 LOG_INFO("Target not halted or running");
2133 if (retval
!= ERROR_OK
)
2136 gettimeofday(&now
, NULL
);
2137 if ((sample_count
>= max_num_samples
) || timeval_compare(&now
, &timeout
) >= 0) {
2138 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2143 *num_samples
= sample_count
;
2147 /* Single aligned words are guaranteed to use 16 or 32 bit access
2148 * mode respectively, otherwise data is handled as quickly as
2151 int target_write_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, const uint8_t *buffer
)
2153 LOG_DEBUG("writing buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2156 if (!target_was_examined(target
)) {
2157 LOG_ERROR("Target not examined yet");
2164 if ((address
+ size
- 1) < address
) {
2165 /* GDB can request this when e.g. PC is 0xfffffffc */
2166 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2172 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2175 static int target_write_buffer_default(struct target
*target
,
2176 target_addr_t address
, uint32_t count
, const uint8_t *buffer
)
2180 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2181 * will have something to do with the size we leave to it. */
2182 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2183 if (address
& size
) {
2184 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2185 if (retval
!= ERROR_OK
)
2193 /* Write the data with as large access size as possible. */
2194 for (; size
> 0; size
/= 2) {
2195 uint32_t aligned
= count
- count
% size
;
2197 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2198 if (retval
!= ERROR_OK
)
2209 /* Single aligned words are guaranteed to use 16 or 32 bit access
2210 * mode respectively, otherwise data is handled as quickly as
2213 int target_read_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
2215 LOG_DEBUG("reading buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2218 if (!target_was_examined(target
)) {
2219 LOG_ERROR("Target not examined yet");
2226 if ((address
+ size
- 1) < address
) {
2227 /* GDB can request this when e.g. PC is 0xfffffffc */
2228 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2234 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2237 static int target_read_buffer_default(struct target
*target
, target_addr_t address
, uint32_t count
, uint8_t *buffer
)
2241 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2242 * will have something to do with the size we leave to it. */
2243 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2244 if (address
& size
) {
2245 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2246 if (retval
!= ERROR_OK
)
2254 /* Read the data with as large access size as possible. */
2255 for (; size
> 0; size
/= 2) {
2256 uint32_t aligned
= count
- count
% size
;
2258 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2259 if (retval
!= ERROR_OK
)
2270 int target_checksum_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* crc
)
2275 uint32_t checksum
= 0;
2276 if (!target_was_examined(target
)) {
2277 LOG_ERROR("Target not examined yet");
2281 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2282 if (retval
!= ERROR_OK
) {
2283 buffer
= malloc(size
);
2284 if (buffer
== NULL
) {
2285 LOG_ERROR("error allocating buffer for section (%" PRId32
" bytes)", size
);
2286 return ERROR_COMMAND_SYNTAX_ERROR
;
2288 retval
= target_read_buffer(target
, address
, size
, buffer
);
2289 if (retval
!= ERROR_OK
) {
2294 /* convert to target endianness */
2295 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2296 uint32_t target_data
;
2297 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2298 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2301 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2310 int target_blank_check_memory(struct target
*target
,
2311 struct target_memory_check_block
*blocks
, int num_blocks
,
2312 uint8_t erased_value
)
2314 if (!target_was_examined(target
)) {
2315 LOG_ERROR("Target not examined yet");
2319 if (target
->type
->blank_check_memory
== NULL
)
2320 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2322 return target
->type
->blank_check_memory(target
, blocks
, num_blocks
, erased_value
);
2325 int target_read_u64(struct target
*target
, target_addr_t address
, uint64_t *value
)
2327 uint8_t value_buf
[8];
2328 if (!target_was_examined(target
)) {
2329 LOG_ERROR("Target not examined yet");
2333 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2335 if (retval
== ERROR_OK
) {
2336 *value
= target_buffer_get_u64(target
, value_buf
);
2337 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2342 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2349 int target_read_u32(struct target
*target
, target_addr_t address
, uint32_t *value
)
2351 uint8_t value_buf
[4];
2352 if (!target_was_examined(target
)) {
2353 LOG_ERROR("Target not examined yet");
2357 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2359 if (retval
== ERROR_OK
) {
2360 *value
= target_buffer_get_u32(target
, value_buf
);
2361 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2366 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2373 int target_read_u16(struct target
*target
, target_addr_t address
, uint16_t *value
)
2375 uint8_t value_buf
[2];
2376 if (!target_was_examined(target
)) {
2377 LOG_ERROR("Target not examined yet");
2381 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2383 if (retval
== ERROR_OK
) {
2384 *value
= target_buffer_get_u16(target
, value_buf
);
2385 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%4.4" PRIx16
,
2390 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2397 int target_read_u8(struct target
*target
, target_addr_t address
, uint8_t *value
)
2399 if (!target_was_examined(target
)) {
2400 LOG_ERROR("Target not examined yet");
2404 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2406 if (retval
== ERROR_OK
) {
2407 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2412 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2419 int target_write_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2422 uint8_t value_buf
[8];
2423 if (!target_was_examined(target
)) {
2424 LOG_ERROR("Target not examined yet");
2428 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2432 target_buffer_set_u64(target
, value_buf
, value
);
2433 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2434 if (retval
!= ERROR_OK
)
2435 LOG_DEBUG("failed: %i", retval
);
2440 int target_write_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2443 uint8_t value_buf
[4];
2444 if (!target_was_examined(target
)) {
2445 LOG_ERROR("Target not examined yet");
2449 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2453 target_buffer_set_u32(target
, value_buf
, value
);
2454 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2455 if (retval
!= ERROR_OK
)
2456 LOG_DEBUG("failed: %i", retval
);
2461 int target_write_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2464 uint8_t value_buf
[2];
2465 if (!target_was_examined(target
)) {
2466 LOG_ERROR("Target not examined yet");
2470 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2474 target_buffer_set_u16(target
, value_buf
, value
);
2475 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2476 if (retval
!= ERROR_OK
)
2477 LOG_DEBUG("failed: %i", retval
);
2482 int target_write_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2485 if (!target_was_examined(target
)) {
2486 LOG_ERROR("Target not examined yet");
2490 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2493 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2494 if (retval
!= ERROR_OK
)
2495 LOG_DEBUG("failed: %i", retval
);
2500 int target_write_phys_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2503 uint8_t value_buf
[8];
2504 if (!target_was_examined(target
)) {
2505 LOG_ERROR("Target not examined yet");
2509 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2513 target_buffer_set_u64(target
, value_buf
, value
);
2514 retval
= target_write_phys_memory(target
, address
, 8, 1, value_buf
);
2515 if (retval
!= ERROR_OK
)
2516 LOG_DEBUG("failed: %i", retval
);
2521 int target_write_phys_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2524 uint8_t value_buf
[4];
2525 if (!target_was_examined(target
)) {
2526 LOG_ERROR("Target not examined yet");
2530 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2534 target_buffer_set_u32(target
, value_buf
, value
);
2535 retval
= target_write_phys_memory(target
, address
, 4, 1, value_buf
);
2536 if (retval
!= ERROR_OK
)
2537 LOG_DEBUG("failed: %i", retval
);
2542 int target_write_phys_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2545 uint8_t value_buf
[2];
2546 if (!target_was_examined(target
)) {
2547 LOG_ERROR("Target not examined yet");
2551 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2555 target_buffer_set_u16(target
, value_buf
, value
);
2556 retval
= target_write_phys_memory(target
, address
, 2, 1, value_buf
);
2557 if (retval
!= ERROR_OK
)
2558 LOG_DEBUG("failed: %i", retval
);
2563 int target_write_phys_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2566 if (!target_was_examined(target
)) {
2567 LOG_ERROR("Target not examined yet");
2571 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2574 retval
= target_write_phys_memory(target
, address
, 1, 1, &value
);
2575 if (retval
!= ERROR_OK
)
2576 LOG_DEBUG("failed: %i", retval
);
2581 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2583 struct target
*target
= get_target(name
);
2584 if (target
== NULL
) {
2585 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2588 if (!target
->tap
->enabled
) {
2589 LOG_USER("Target: TAP %s is disabled, "
2590 "can't be the current target\n",
2591 target
->tap
->dotted_name
);
2595 cmd_ctx
->current_target
= target
;
2596 if (cmd_ctx
->current_target_override
)
2597 cmd_ctx
->current_target_override
= target
;
2603 COMMAND_HANDLER(handle_targets_command
)
2605 int retval
= ERROR_OK
;
2606 if (CMD_ARGC
== 1) {
2607 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2608 if (retval
== ERROR_OK
) {
2614 struct target
*target
= all_targets
;
2615 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2616 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2621 if (target
->tap
->enabled
)
2622 state
= target_state_name(target
);
2624 state
= "tap-disabled";
2626 if (CMD_CTX
->current_target
== target
)
2629 /* keep columns lined up to match the headers above */
2630 command_print(CMD_CTX
,
2631 "%2d%c %-18s %-10s %-6s %-18s %s",
2632 target
->target_number
,
2634 target_name(target
),
2635 target_type_name(target
),
2636 Jim_Nvp_value2name_simple(nvp_target_endian
,
2637 target
->endianness
)->name
,
2638 target
->tap
->dotted_name
,
2640 target
= target
->next
;
2646 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2648 static int powerDropout
;
2649 static int srstAsserted
;
2651 static int runPowerRestore
;
2652 static int runPowerDropout
;
2653 static int runSrstAsserted
;
2654 static int runSrstDeasserted
;
2656 static int sense_handler(void)
2658 static int prevSrstAsserted
;
2659 static int prevPowerdropout
;
2661 int retval
= jtag_power_dropout(&powerDropout
);
2662 if (retval
!= ERROR_OK
)
2666 powerRestored
= prevPowerdropout
&& !powerDropout
;
2668 runPowerRestore
= 1;
2670 int64_t current
= timeval_ms();
2671 static int64_t lastPower
;
2672 bool waitMore
= lastPower
+ 2000 > current
;
2673 if (powerDropout
&& !waitMore
) {
2674 runPowerDropout
= 1;
2675 lastPower
= current
;
2678 retval
= jtag_srst_asserted(&srstAsserted
);
2679 if (retval
!= ERROR_OK
)
2683 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2685 static int64_t lastSrst
;
2686 waitMore
= lastSrst
+ 2000 > current
;
2687 if (srstDeasserted
&& !waitMore
) {
2688 runSrstDeasserted
= 1;
2692 if (!prevSrstAsserted
&& srstAsserted
)
2693 runSrstAsserted
= 1;
2695 prevSrstAsserted
= srstAsserted
;
2696 prevPowerdropout
= powerDropout
;
2698 if (srstDeasserted
|| powerRestored
) {
2699 /* Other than logging the event we can't do anything here.
2700 * Issuing a reset is a particularly bad idea as we might
2701 * be inside a reset already.
2708 /* process target state changes */
2709 static int handle_target(void *priv
)
2711 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2712 int retval
= ERROR_OK
;
2714 if (!is_jtag_poll_safe()) {
2715 /* polling is disabled currently */
2719 /* we do not want to recurse here... */
2720 static int recursive
;
2724 /* danger! running these procedures can trigger srst assertions and power dropouts.
2725 * We need to avoid an infinite loop/recursion here and we do that by
2726 * clearing the flags after running these events.
2728 int did_something
= 0;
2729 if (runSrstAsserted
) {
2730 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2731 Jim_Eval(interp
, "srst_asserted");
2734 if (runSrstDeasserted
) {
2735 Jim_Eval(interp
, "srst_deasserted");
2738 if (runPowerDropout
) {
2739 LOG_INFO("Power dropout detected, running power_dropout proc.");
2740 Jim_Eval(interp
, "power_dropout");
2743 if (runPowerRestore
) {
2744 Jim_Eval(interp
, "power_restore");
2748 if (did_something
) {
2749 /* clear detect flags */
2753 /* clear action flags */
2755 runSrstAsserted
= 0;
2756 runSrstDeasserted
= 0;
2757 runPowerRestore
= 0;
2758 runPowerDropout
= 0;
2763 /* Poll targets for state changes unless that's globally disabled.
2764 * Skip targets that are currently disabled.
2766 for (struct target
*target
= all_targets
;
2767 is_jtag_poll_safe() && target
;
2768 target
= target
->next
) {
2770 if (!target_was_examined(target
))
2773 if (!target
->tap
->enabled
)
2776 if (target
->backoff
.times
> target
->backoff
.count
) {
2777 /* do not poll this time as we failed previously */
2778 target
->backoff
.count
++;
2781 target
->backoff
.count
= 0;
2783 /* only poll target if we've got power and srst isn't asserted */
2784 if (!powerDropout
&& !srstAsserted
) {
2785 /* polling may fail silently until the target has been examined */
2786 retval
= target_poll(target
);
2787 if (retval
!= ERROR_OK
) {
2788 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2789 if (target
->backoff
.times
* polling_interval
< 5000) {
2790 target
->backoff
.times
*= 2;
2791 target
->backoff
.times
++;
2794 /* Tell GDB to halt the debugger. This allows the user to
2795 * run monitor commands to handle the situation.
2797 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2799 if (target
->backoff
.times
> 0) {
2800 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
2801 target_reset_examined(target
);
2802 retval
= target_examine_one(target
);
2803 /* Target examination could have failed due to unstable connection,
2804 * but we set the examined flag anyway to repoll it later */
2805 if (retval
!= ERROR_OK
) {
2806 target
->examined
= true;
2807 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2808 target
->backoff
.times
* polling_interval
);
2813 /* Since we succeeded, we reset backoff count */
2814 target
->backoff
.times
= 0;
2821 COMMAND_HANDLER(handle_reg_command
)
2823 struct target
*target
;
2824 struct reg
*reg
= NULL
;
2830 target
= get_current_target(CMD_CTX
);
2832 /* list all available registers for the current target */
2833 if (CMD_ARGC
== 0) {
2834 struct reg_cache
*cache
= target
->reg_cache
;
2840 command_print(CMD_CTX
, "===== %s", cache
->name
);
2842 for (i
= 0, reg
= cache
->reg_list
;
2843 i
< cache
->num_regs
;
2844 i
++, reg
++, count
++) {
2845 if (reg
->exist
== false)
2847 /* only print cached values if they are valid */
2849 value
= buf_to_str(reg
->value
,
2851 command_print(CMD_CTX
,
2852 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2860 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2865 cache
= cache
->next
;
2871 /* access a single register by its ordinal number */
2872 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2874 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2876 struct reg_cache
*cache
= target
->reg_cache
;
2880 for (i
= 0; i
< cache
->num_regs
; i
++) {
2881 if (count
++ == num
) {
2882 reg
= &cache
->reg_list
[i
];
2888 cache
= cache
->next
;
2892 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2893 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2897 /* access a single register by its name */
2898 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2904 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2909 /* display a register */
2910 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2911 && (CMD_ARGV
[1][0] <= '9')))) {
2912 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2915 if (reg
->valid
== 0)
2916 reg
->type
->get(reg
);
2917 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2918 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2923 /* set register value */
2924 if (CMD_ARGC
== 2) {
2925 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2928 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2930 reg
->type
->set(reg
, buf
);
2932 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2933 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2941 return ERROR_COMMAND_SYNTAX_ERROR
;
2944 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2948 COMMAND_HANDLER(handle_poll_command
)
2950 int retval
= ERROR_OK
;
2951 struct target
*target
= get_current_target(CMD_CTX
);
2953 if (CMD_ARGC
== 0) {
2954 command_print(CMD_CTX
, "background polling: %s",
2955 jtag_poll_get_enabled() ? "on" : "off");
2956 command_print(CMD_CTX
, "TAP: %s (%s)",
2957 target
->tap
->dotted_name
,
2958 target
->tap
->enabled
? "enabled" : "disabled");
2959 if (!target
->tap
->enabled
)
2961 retval
= target_poll(target
);
2962 if (retval
!= ERROR_OK
)
2964 retval
= target_arch_state(target
);
2965 if (retval
!= ERROR_OK
)
2967 } else if (CMD_ARGC
== 1) {
2969 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2970 jtag_poll_set_enabled(enable
);
2972 return ERROR_COMMAND_SYNTAX_ERROR
;
2977 COMMAND_HANDLER(handle_wait_halt_command
)
2980 return ERROR_COMMAND_SYNTAX_ERROR
;
2982 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2983 if (1 == CMD_ARGC
) {
2984 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2985 if (ERROR_OK
!= retval
)
2986 return ERROR_COMMAND_SYNTAX_ERROR
;
2989 struct target
*target
= get_current_target(CMD_CTX
);
2990 return target_wait_state(target
, TARGET_HALTED
, ms
);
2993 /* wait for target state to change. The trick here is to have a low
2994 * latency for short waits and not to suck up all the CPU time
2997 * After 500ms, keep_alive() is invoked
2999 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
3002 int64_t then
= 0, cur
;
3006 retval
= target_poll(target
);
3007 if (retval
!= ERROR_OK
)
3009 if (target
->state
== state
)
3014 then
= timeval_ms();
3015 LOG_DEBUG("waiting for target %s...",
3016 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
3022 if ((cur
-then
) > ms
) {
3023 LOG_ERROR("timed out while waiting for target %s",
3024 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
3032 COMMAND_HANDLER(handle_halt_command
)
3036 struct target
*target
= get_current_target(CMD_CTX
);
3038 target
->verbose_halt_msg
= true;
3040 int retval
= target_halt(target
);
3041 if (ERROR_OK
!= retval
)
3044 if (CMD_ARGC
== 1) {
3045 unsigned wait_local
;
3046 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
3047 if (ERROR_OK
!= retval
)
3048 return ERROR_COMMAND_SYNTAX_ERROR
;
3053 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
3056 COMMAND_HANDLER(handle_soft_reset_halt_command
)
3058 struct target
*target
= get_current_target(CMD_CTX
);
3060 LOG_USER("requesting target halt and executing a soft reset");
3062 target_soft_reset_halt(target
);
3067 COMMAND_HANDLER(handle_reset_command
)
3070 return ERROR_COMMAND_SYNTAX_ERROR
;
3072 enum target_reset_mode reset_mode
= RESET_RUN
;
3073 if (CMD_ARGC
== 1) {
3075 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
3076 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
3077 return ERROR_COMMAND_SYNTAX_ERROR
;
3078 reset_mode
= n
->value
;
3081 /* reset *all* targets */
3082 return target_process_reset(CMD
, reset_mode
);
3086 COMMAND_HANDLER(handle_resume_command
)
3090 return ERROR_COMMAND_SYNTAX_ERROR
;
3092 struct target
*target
= get_current_target(CMD_CTX
);
3094 /* with no CMD_ARGV, resume from current pc, addr = 0,
3095 * with one arguments, addr = CMD_ARGV[0],
3096 * handle breakpoints, not debugging */
3097 target_addr_t addr
= 0;
3098 if (CMD_ARGC
== 1) {
3099 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3103 return target_resume(target
, current
, addr
, 1, 0);
3106 COMMAND_HANDLER(handle_step_command
)
3109 return ERROR_COMMAND_SYNTAX_ERROR
;
3113 /* with no CMD_ARGV, step from current pc, addr = 0,
3114 * with one argument addr = CMD_ARGV[0],
3115 * handle breakpoints, debugging */
3116 target_addr_t addr
= 0;
3118 if (CMD_ARGC
== 1) {
3119 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3123 struct target
*target
= get_current_target(CMD_CTX
);
3125 return target
->type
->step(target
, current_pc
, addr
, 1);
3128 static void handle_md_output(struct command_invocation
*cmd
,
3129 struct target
*target
, target_addr_t address
, unsigned size
,
3130 unsigned count
, const uint8_t *buffer
)
3132 const unsigned line_bytecnt
= 32;
3133 unsigned line_modulo
= line_bytecnt
/ size
;
3135 char output
[line_bytecnt
* 4 + 1];
3136 unsigned output_len
= 0;
3138 const char *value_fmt
;
3141 value_fmt
= "%16.16"PRIx64
" ";
3144 value_fmt
= "%8.8"PRIx64
" ";
3147 value_fmt
= "%4.4"PRIx64
" ";
3150 value_fmt
= "%2.2"PRIx64
" ";
3153 /* "can't happen", caller checked */
3154 LOG_ERROR("invalid memory read size: %u", size
);
3158 for (unsigned i
= 0; i
< count
; i
++) {
3159 if (i
% line_modulo
== 0) {
3160 output_len
+= snprintf(output
+ output_len
,
3161 sizeof(output
) - output_len
,
3162 TARGET_ADDR_FMT
": ",
3163 (address
+ (i
* size
)));
3167 const uint8_t *value_ptr
= buffer
+ i
* size
;
3170 value
= target_buffer_get_u64(target
, value_ptr
);
3173 value
= target_buffer_get_u32(target
, value_ptr
);
3176 value
= target_buffer_get_u16(target
, value_ptr
);
3181 output_len
+= snprintf(output
+ output_len
,
3182 sizeof(output
) - output_len
,
3185 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
3186 command_print(cmd
->ctx
, "%s", output
);
3192 COMMAND_HANDLER(handle_md_command
)
3195 return ERROR_COMMAND_SYNTAX_ERROR
;
3198 switch (CMD_NAME
[2]) {
3212 return ERROR_COMMAND_SYNTAX_ERROR
;
3215 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3216 int (*fn
)(struct target
*target
,
3217 target_addr_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
3221 fn
= target_read_phys_memory
;
3223 fn
= target_read_memory
;
3224 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
3225 return ERROR_COMMAND_SYNTAX_ERROR
;
3227 target_addr_t address
;
3228 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3232 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
3234 uint8_t *buffer
= calloc(count
, size
);
3235 if (buffer
== NULL
) {
3236 LOG_ERROR("Failed to allocate md read buffer");
3240 struct target
*target
= get_current_target(CMD_CTX
);
3241 int retval
= fn(target
, address
, size
, count
, buffer
);
3242 if (ERROR_OK
== retval
)
3243 handle_md_output(CMD
, target
, address
, size
, count
, buffer
);
3250 typedef int (*target_write_fn
)(struct target
*target
,
3251 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
3253 static int target_fill_mem(struct target
*target
,
3254 target_addr_t address
,
3262 /* We have to write in reasonably large chunks to be able
3263 * to fill large memory areas with any sane speed */
3264 const unsigned chunk_size
= 16384;
3265 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
3266 if (target_buf
== NULL
) {
3267 LOG_ERROR("Out of memory");
3271 for (unsigned i
= 0; i
< chunk_size
; i
++) {
3272 switch (data_size
) {
3274 target_buffer_set_u64(target
, target_buf
+ i
* data_size
, b
);
3277 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
3280 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
3283 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
3290 int retval
= ERROR_OK
;
3292 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
3295 if (current
> chunk_size
)
3296 current
= chunk_size
;
3297 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
3298 if (retval
!= ERROR_OK
)
3300 /* avoid GDB timeouts */
3309 COMMAND_HANDLER(handle_mw_command
)
3312 return ERROR_COMMAND_SYNTAX_ERROR
;
3313 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3318 fn
= target_write_phys_memory
;
3320 fn
= target_write_memory
;
3321 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
3322 return ERROR_COMMAND_SYNTAX_ERROR
;
3324 target_addr_t address
;
3325 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3327 target_addr_t value
;
3328 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], value
);
3332 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
3334 struct target
*target
= get_current_target(CMD_CTX
);
3336 switch (CMD_NAME
[2]) {
3350 return ERROR_COMMAND_SYNTAX_ERROR
;
3353 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
3356 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
3357 target_addr_t
*min_address
, target_addr_t
*max_address
)
3359 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
3360 return ERROR_COMMAND_SYNTAX_ERROR
;
3362 /* a base address isn't always necessary,
3363 * default to 0x0 (i.e. don't relocate) */
3364 if (CMD_ARGC
>= 2) {
3366 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3367 image
->base_address
= addr
;
3368 image
->base_address_set
= 1;
3370 image
->base_address_set
= 0;
3372 image
->start_address_set
= 0;
3375 COMMAND_PARSE_ADDRESS(CMD_ARGV
[3], *min_address
);
3376 if (CMD_ARGC
== 5) {
3377 COMMAND_PARSE_ADDRESS(CMD_ARGV
[4], *max_address
);
3378 /* use size (given) to find max (required) */
3379 *max_address
+= *min_address
;
3382 if (*min_address
> *max_address
)
3383 return ERROR_COMMAND_SYNTAX_ERROR
;
3388 COMMAND_HANDLER(handle_load_image_command
)
3392 uint32_t image_size
;
3393 target_addr_t min_address
= 0;
3394 target_addr_t max_address
= -1;
3398 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
3399 &image
, &min_address
, &max_address
);
3400 if (ERROR_OK
!= retval
)
3403 struct target
*target
= get_current_target(CMD_CTX
);
3405 struct duration bench
;
3406 duration_start(&bench
);
3408 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
3413 for (i
= 0; i
< image
.num_sections
; i
++) {
3414 buffer
= malloc(image
.sections
[i
].size
);
3415 if (buffer
== NULL
) {
3416 command_print(CMD_CTX
,
3417 "error allocating buffer for section (%d bytes)",
3418 (int)(image
.sections
[i
].size
));
3419 retval
= ERROR_FAIL
;
3423 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3424 if (retval
!= ERROR_OK
) {
3429 uint32_t offset
= 0;
3430 uint32_t length
= buf_cnt
;
3432 /* DANGER!!! beware of unsigned comparision here!!! */
3434 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3435 (image
.sections
[i
].base_address
< max_address
)) {
3437 if (image
.sections
[i
].base_address
< min_address
) {
3438 /* clip addresses below */
3439 offset
+= min_address
-image
.sections
[i
].base_address
;
3443 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3444 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3446 retval
= target_write_buffer(target
,
3447 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3448 if (retval
!= ERROR_OK
) {
3452 image_size
+= length
;
3453 command_print(CMD_CTX
, "%u bytes written at address " TARGET_ADDR_FMT
"",
3454 (unsigned int)length
,
3455 image
.sections
[i
].base_address
+ offset
);
3461 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3462 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
3463 "in %fs (%0.3f KiB/s)", image_size
,
3464 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3467 image_close(&image
);
3473 COMMAND_HANDLER(handle_dump_image_command
)
3475 struct fileio
*fileio
;
3477 int retval
, retvaltemp
;
3478 target_addr_t address
, size
;
3479 struct duration bench
;
3480 struct target
*target
= get_current_target(CMD_CTX
);
3483 return ERROR_COMMAND_SYNTAX_ERROR
;
3485 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], address
);
3486 COMMAND_PARSE_ADDRESS(CMD_ARGV
[2], size
);
3488 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
3489 buffer
= malloc(buf_size
);
3493 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3494 if (retval
!= ERROR_OK
) {
3499 duration_start(&bench
);
3502 size_t size_written
;
3503 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
3504 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
3505 if (retval
!= ERROR_OK
)
3508 retval
= fileio_write(fileio
, this_run_size
, buffer
, &size_written
);
3509 if (retval
!= ERROR_OK
)
3512 size
-= this_run_size
;
3513 address
+= this_run_size
;
3518 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3520 retval
= fileio_size(fileio
, &filesize
);
3521 if (retval
!= ERROR_OK
)
3523 command_print(CMD_CTX
,
3524 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize
,
3525 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3528 retvaltemp
= fileio_close(fileio
);
3529 if (retvaltemp
!= ERROR_OK
)
3538 IMAGE_CHECKSUM_ONLY
= 2
3541 static COMMAND_HELPER(handle_verify_image_command_internal
, enum verify_mode verify
)
3545 uint32_t image_size
;
3548 uint32_t checksum
= 0;
3549 uint32_t mem_checksum
= 0;
3553 struct target
*target
= get_current_target(CMD_CTX
);
3556 return ERROR_COMMAND_SYNTAX_ERROR
;
3559 LOG_ERROR("no target selected");
3563 struct duration bench
;
3564 duration_start(&bench
);
3566 if (CMD_ARGC
>= 2) {
3568 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3569 image
.base_address
= addr
;
3570 image
.base_address_set
= 1;
3572 image
.base_address_set
= 0;
3573 image
.base_address
= 0x0;
3576 image
.start_address_set
= 0;
3578 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3579 if (retval
!= ERROR_OK
)
3585 for (i
= 0; i
< image
.num_sections
; i
++) {
3586 buffer
= malloc(image
.sections
[i
].size
);
3587 if (buffer
== NULL
) {
3588 command_print(CMD_CTX
,
3589 "error allocating buffer for section (%d bytes)",
3590 (int)(image
.sections
[i
].size
));
3593 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3594 if (retval
!= ERROR_OK
) {
3599 if (verify
>= IMAGE_VERIFY
) {
3600 /* calculate checksum of image */
3601 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3602 if (retval
!= ERROR_OK
) {
3607 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3608 if (retval
!= ERROR_OK
) {
3612 if ((checksum
!= mem_checksum
) && (verify
== IMAGE_CHECKSUM_ONLY
)) {
3613 LOG_ERROR("checksum mismatch");
3615 retval
= ERROR_FAIL
;
3618 if (checksum
!= mem_checksum
) {
3619 /* failed crc checksum, fall back to a binary compare */
3623 LOG_ERROR("checksum mismatch - attempting binary compare");
3625 data
= malloc(buf_cnt
);
3627 /* Can we use 32bit word accesses? */
3629 int count
= buf_cnt
;
3630 if ((count
% 4) == 0) {
3634 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
3635 if (retval
== ERROR_OK
) {
3637 for (t
= 0; t
< buf_cnt
; t
++) {
3638 if (data
[t
] != buffer
[t
]) {
3639 command_print(CMD_CTX
,
3640 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3642 (unsigned)(t
+ image
.sections
[i
].base_address
),
3645 if (diffs
++ >= 127) {
3646 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
3658 command_print(CMD_CTX
, "address " TARGET_ADDR_FMT
" length 0x%08zx",
3659 image
.sections
[i
].base_address
,
3664 image_size
+= buf_cnt
;
3667 command_print(CMD_CTX
, "No more differences found.");
3670 retval
= ERROR_FAIL
;
3671 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3672 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3673 "in %fs (%0.3f KiB/s)", image_size
,
3674 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3677 image_close(&image
);
3682 COMMAND_HANDLER(handle_verify_image_checksum_command
)
3684 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_CHECKSUM_ONLY
);
3687 COMMAND_HANDLER(handle_verify_image_command
)
3689 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_VERIFY
);
3692 COMMAND_HANDLER(handle_test_image_command
)
3694 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_TEST
);
3697 static int handle_bp_command_list(struct command_invocation
*cmd
)
3699 struct target
*target
= get_current_target(cmd
->ctx
);
3700 struct breakpoint
*breakpoint
= target
->breakpoints
;
3701 while (breakpoint
) {
3702 if (breakpoint
->type
== BKPT_SOFT
) {
3703 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3704 breakpoint
->length
, 16);
3705 command_print(cmd
->ctx
, "IVA breakpoint: " TARGET_ADDR_FMT
", 0x%x, %i, 0x%s",
3706 breakpoint
->address
,
3708 breakpoint
->set
, buf
);
3711 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3712 command_print(cmd
->ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3714 breakpoint
->length
, breakpoint
->set
);
3715 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3716 command_print(cmd
->ctx
, "Hybrid breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3717 breakpoint
->address
,
3718 breakpoint
->length
, breakpoint
->set
);
3719 command_print(cmd
->ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3722 command_print(cmd
->ctx
, "Breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3723 breakpoint
->address
,
3724 breakpoint
->length
, breakpoint
->set
);
3727 breakpoint
= breakpoint
->next
;
3732 static int handle_bp_command_set(struct command_invocation
*cmd
,
3733 target_addr_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3735 struct target
*target
= get_current_target(cmd
->ctx
);
3739 retval
= breakpoint_add(target
, addr
, length
, hw
);
3740 /* error is always logged in breakpoint_add(), do not print it again */
3741 if (ERROR_OK
== retval
)
3742 command_print(cmd
->ctx
, "breakpoint set at " TARGET_ADDR_FMT
"", addr
);
3744 } else if (addr
== 0) {
3745 if (target
->type
->add_context_breakpoint
== NULL
) {
3746 LOG_ERROR("Context breakpoint not available");
3747 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
3749 retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3750 /* error is always logged in context_breakpoint_add(), do not print it again */
3751 if (ERROR_OK
== retval
)
3752 command_print(cmd
->ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3755 if (target
->type
->add_hybrid_breakpoint
== NULL
) {
3756 LOG_ERROR("Hybrid breakpoint not available");
3757 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
3759 retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3760 /* error is always logged in hybrid_breakpoint_add(), do not print it again */
3761 if (ERROR_OK
== retval
)
3762 command_print(cmd
->ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3767 COMMAND_HANDLER(handle_bp_command
)
3776 return handle_bp_command_list(CMD
);
3780 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3781 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3782 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
3785 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
3787 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3788 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3790 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
3791 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
3793 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
3794 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3796 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
3801 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3802 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
3803 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
3804 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
3807 return ERROR_COMMAND_SYNTAX_ERROR
;
3811 COMMAND_HANDLER(handle_rbp_command
)
3814 return ERROR_COMMAND_SYNTAX_ERROR
;
3817 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3819 struct target
*target
= get_current_target(CMD_CTX
);
3820 breakpoint_remove(target
, addr
);
3825 COMMAND_HANDLER(handle_wp_command
)
3827 struct target
*target
= get_current_target(CMD_CTX
);
3829 if (CMD_ARGC
== 0) {
3830 struct watchpoint
*watchpoint
= target
->watchpoints
;
3832 while (watchpoint
) {
3833 command_print(CMD_CTX
, "address: " TARGET_ADDR_FMT
3834 ", len: 0x%8.8" PRIx32
3835 ", r/w/a: %i, value: 0x%8.8" PRIx32
3836 ", mask: 0x%8.8" PRIx32
,
3837 watchpoint
->address
,
3839 (int)watchpoint
->rw
,
3842 watchpoint
= watchpoint
->next
;
3847 enum watchpoint_rw type
= WPT_ACCESS
;
3849 uint32_t length
= 0;
3850 uint32_t data_value
= 0x0;
3851 uint32_t data_mask
= 0xffffffff;
3855 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
3858 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3861 switch (CMD_ARGV
[2][0]) {
3872 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3873 return ERROR_COMMAND_SYNTAX_ERROR
;
3877 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3878 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3882 return ERROR_COMMAND_SYNTAX_ERROR
;
3885 int retval
= watchpoint_add(target
, addr
, length
, type
,
3886 data_value
, data_mask
);
3887 if (ERROR_OK
!= retval
)
3888 LOG_ERROR("Failure setting watchpoints");
3893 COMMAND_HANDLER(handle_rwp_command
)
3896 return ERROR_COMMAND_SYNTAX_ERROR
;
3899 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3901 struct target
*target
= get_current_target(CMD_CTX
);
3902 watchpoint_remove(target
, addr
);
3908 * Translate a virtual address to a physical address.
3910 * The low-level target implementation must have logged a detailed error
3911 * which is forwarded to telnet/GDB session.
3913 COMMAND_HANDLER(handle_virt2phys_command
)
3916 return ERROR_COMMAND_SYNTAX_ERROR
;
3919 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], va
);
3922 struct target
*target
= get_current_target(CMD_CTX
);
3923 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3924 if (retval
== ERROR_OK
)
3925 command_print(CMD_CTX
, "Physical address " TARGET_ADDR_FMT
"", pa
);
3930 static void writeData(FILE *f
, const void *data
, size_t len
)
3932 size_t written
= fwrite(data
, 1, len
, f
);
3934 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3937 static void writeLong(FILE *f
, int l
, struct target
*target
)
3941 target_buffer_set_u32(target
, val
, l
);
3942 writeData(f
, val
, 4);
3945 static void writeString(FILE *f
, char *s
)
3947 writeData(f
, s
, strlen(s
));
3950 typedef unsigned char UNIT
[2]; /* unit of profiling */
3952 /* Dump a gmon.out histogram file. */
3953 static void write_gmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
, bool with_range
,
3954 uint32_t start_address
, uint32_t end_address
, struct target
*target
, uint32_t duration_ms
)
3957 FILE *f
= fopen(filename
, "w");
3960 writeString(f
, "gmon");
3961 writeLong(f
, 0x00000001, target
); /* Version */
3962 writeLong(f
, 0, target
); /* padding */
3963 writeLong(f
, 0, target
); /* padding */
3964 writeLong(f
, 0, target
); /* padding */
3966 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3967 writeData(f
, &zero
, 1);
3969 /* figure out bucket size */
3973 min
= start_address
;
3978 for (i
= 0; i
< sampleNum
; i
++) {
3979 if (min
> samples
[i
])
3981 if (max
< samples
[i
])
3985 /* max should be (largest sample + 1)
3986 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
3990 int addressSpace
= max
- min
;
3991 assert(addressSpace
>= 2);
3993 /* FIXME: What is the reasonable number of buckets?
3994 * The profiling result will be more accurate if there are enough buckets. */
3995 static const uint32_t maxBuckets
= 128 * 1024; /* maximum buckets. */
3996 uint32_t numBuckets
= addressSpace
/ sizeof(UNIT
);
3997 if (numBuckets
> maxBuckets
)
3998 numBuckets
= maxBuckets
;
3999 int *buckets
= malloc(sizeof(int) * numBuckets
);
4000 if (buckets
== NULL
) {
4004 memset(buckets
, 0, sizeof(int) * numBuckets
);
4005 for (i
= 0; i
< sampleNum
; i
++) {
4006 uint32_t address
= samples
[i
];
4008 if ((address
< min
) || (max
<= address
))
4011 long long a
= address
- min
;
4012 long long b
= numBuckets
;
4013 long long c
= addressSpace
;
4014 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
4018 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
4019 writeLong(f
, min
, target
); /* low_pc */
4020 writeLong(f
, max
, target
); /* high_pc */
4021 writeLong(f
, numBuckets
, target
); /* # of buckets */
4022 float sample_rate
= sampleNum
/ (duration_ms
/ 1000.0);
4023 writeLong(f
, sample_rate
, target
);
4024 writeString(f
, "seconds");
4025 for (i
= 0; i
< (15-strlen("seconds")); i
++)
4026 writeData(f
, &zero
, 1);
4027 writeString(f
, "s");
4029 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
4031 char *data
= malloc(2 * numBuckets
);
4033 for (i
= 0; i
< numBuckets
; i
++) {
4038 data
[i
* 2] = val
&0xff;
4039 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
4042 writeData(f
, data
, numBuckets
* 2);
4050 /* profiling samples the CPU PC as quickly as OpenOCD is able,
4051 * which will be used as a random sampling of PC */
4052 COMMAND_HANDLER(handle_profile_command
)
4054 struct target
*target
= get_current_target(CMD_CTX
);
4056 if ((CMD_ARGC
!= 2) && (CMD_ARGC
!= 4))
4057 return ERROR_COMMAND_SYNTAX_ERROR
;
4059 const uint32_t MAX_PROFILE_SAMPLE_NUM
= 10000;
4061 uint32_t num_of_samples
;
4062 int retval
= ERROR_OK
;
4064 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], offset
);
4066 uint32_t *samples
= malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM
);
4067 if (samples
== NULL
) {
4068 LOG_ERROR("No memory to store samples.");
4072 uint64_t timestart_ms
= timeval_ms();
4074 * Some cores let us sample the PC without the
4075 * annoying halt/resume step; for example, ARMv7 PCSR.
4076 * Provide a way to use that more efficient mechanism.
4078 retval
= target_profiling(target
, samples
, MAX_PROFILE_SAMPLE_NUM
,
4079 &num_of_samples
, offset
);
4080 if (retval
!= ERROR_OK
) {
4084 uint32_t duration_ms
= timeval_ms() - timestart_ms
;
4086 assert(num_of_samples
<= MAX_PROFILE_SAMPLE_NUM
);
4088 retval
= target_poll(target
);
4089 if (retval
!= ERROR_OK
) {
4093 if (target
->state
== TARGET_RUNNING
) {
4094 retval
= target_halt(target
);
4095 if (retval
!= ERROR_OK
) {
4101 retval
= target_poll(target
);
4102 if (retval
!= ERROR_OK
) {
4107 uint32_t start_address
= 0;
4108 uint32_t end_address
= 0;
4109 bool with_range
= false;
4110 if (CMD_ARGC
== 4) {
4112 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], start_address
);
4113 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], end_address
);
4116 write_gmon(samples
, num_of_samples
, CMD_ARGV
[1],
4117 with_range
, start_address
, end_address
, target
, duration_ms
);
4118 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
4124 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
4127 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4130 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4134 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4135 valObjPtr
= Jim_NewIntObj(interp
, val
);
4136 if (!nameObjPtr
|| !valObjPtr
) {
4141 Jim_IncrRefCount(nameObjPtr
);
4142 Jim_IncrRefCount(valObjPtr
);
4143 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
4144 Jim_DecrRefCount(interp
, nameObjPtr
);
4145 Jim_DecrRefCount(interp
, valObjPtr
);
4147 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
4151 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4153 struct command_context
*context
;
4154 struct target
*target
;
4156 context
= current_command_context(interp
);
4157 assert(context
!= NULL
);
4159 target
= get_current_target(context
);
4160 if (target
== NULL
) {
4161 LOG_ERROR("mem2array: no current target");
4165 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4168 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
4176 const char *varname
;
4182 /* argv[1] = name of array to receive the data
4183 * argv[2] = desired width
4184 * argv[3] = memory address
4185 * argv[4] = count of times to read
4188 if (argc
< 4 || argc
> 5) {
4189 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4192 varname
= Jim_GetString(argv
[0], &len
);
4193 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4195 e
= Jim_GetLong(interp
, argv
[1], &l
);
4200 e
= Jim_GetLong(interp
, argv
[2], &l
);
4204 e
= Jim_GetLong(interp
, argv
[3], &l
);
4210 phys
= Jim_GetString(argv
[4], &n
);
4211 if (!strncmp(phys
, "phys", n
))
4227 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4228 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
4232 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4233 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
4236 if ((addr
+ (len
* width
)) < addr
) {
4237 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4238 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
4241 /* absurd transfer size? */
4243 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4244 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
4249 ((width
== 2) && ((addr
& 1) == 0)) ||
4250 ((width
== 4) && ((addr
& 3) == 0))) {
4254 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4255 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
4258 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4267 size_t buffersize
= 4096;
4268 uint8_t *buffer
= malloc(buffersize
);
4275 /* Slurp... in buffer size chunks */
4277 count
= len
; /* in objects.. */
4278 if (count
> (buffersize
/ width
))
4279 count
= (buffersize
/ width
);
4282 retval
= target_read_phys_memory(target
, addr
, width
, count
, buffer
);
4284 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
4285 if (retval
!= ERROR_OK
) {
4287 LOG_ERROR("mem2array: Read @ 0x%08" PRIx32
", w=%" PRId32
", cnt=%" PRId32
", failed",
4291 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4292 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
4296 v
= 0; /* shut up gcc */
4297 for (i
= 0; i
< count
; i
++, n
++) {
4300 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
4303 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
4306 v
= buffer
[i
] & 0x0ff;
4309 new_int_array_element(interp
, varname
, n
, v
);
4312 addr
+= count
* width
;
4318 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4323 static int get_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t *val
)
4326 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4330 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4334 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4340 Jim_IncrRefCount(nameObjPtr
);
4341 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
4342 Jim_DecrRefCount(interp
, nameObjPtr
);
4344 if (valObjPtr
== NULL
)
4347 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
4348 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
4353 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4355 struct command_context
*context
;
4356 struct target
*target
;
4358 context
= current_command_context(interp
);
4359 assert(context
!= NULL
);
4361 target
= get_current_target(context
);
4362 if (target
== NULL
) {
4363 LOG_ERROR("array2mem: no current target");
4367 return target_array2mem(interp
, target
, argc
-1, argv
+ 1);
4370 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
4371 int argc
, Jim_Obj
*const *argv
)
4379 const char *varname
;
4385 /* argv[1] = name of array to get the data
4386 * argv[2] = desired width
4387 * argv[3] = memory address
4388 * argv[4] = count to write
4390 if (argc
< 4 || argc
> 5) {
4391 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4394 varname
= Jim_GetString(argv
[0], &len
);
4395 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4397 e
= Jim_GetLong(interp
, argv
[1], &l
);
4402 e
= Jim_GetLong(interp
, argv
[2], &l
);
4406 e
= Jim_GetLong(interp
, argv
[3], &l
);
4412 phys
= Jim_GetString(argv
[4], &n
);
4413 if (!strncmp(phys
, "phys", n
))
4429 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4430 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4431 "Invalid width param, must be 8/16/32", NULL
);
4435 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4436 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4437 "array2mem: zero width read?", NULL
);
4440 if ((addr
+ (len
* width
)) < addr
) {
4441 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4442 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4443 "array2mem: addr + len - wraps to zero?", NULL
);
4446 /* absurd transfer size? */
4448 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4449 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4450 "array2mem: absurd > 64K item request", NULL
);
4455 ((width
== 2) && ((addr
& 1) == 0)) ||
4456 ((width
== 4) && ((addr
& 3) == 0))) {
4460 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4461 sprintf(buf
, "array2mem address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
4464 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4475 size_t buffersize
= 4096;
4476 uint8_t *buffer
= malloc(buffersize
);
4481 /* Slurp... in buffer size chunks */
4483 count
= len
; /* in objects.. */
4484 if (count
> (buffersize
/ width
))
4485 count
= (buffersize
/ width
);
4487 v
= 0; /* shut up gcc */
4488 for (i
= 0; i
< count
; i
++, n
++) {
4489 get_int_array_element(interp
, varname
, n
, &v
);
4492 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
4495 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
4498 buffer
[i
] = v
& 0x0ff;
4505 retval
= target_write_phys_memory(target
, addr
, width
, count
, buffer
);
4507 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
4508 if (retval
!= ERROR_OK
) {
4510 LOG_ERROR("array2mem: Write @ 0x%08" PRIx32
", w=%" PRId32
", cnt=%" PRId32
", failed",
4514 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4515 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
4519 addr
+= count
* width
;
4524 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4529 /* FIX? should we propagate errors here rather than printing them
4532 void target_handle_event(struct target
*target
, enum target_event e
)
4534 struct target_event_action
*teap
;
4536 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4537 if (teap
->event
== e
) {
4538 LOG_DEBUG("target(%d): %s (%s) event: %d (%s) action: %s",
4539 target
->target_number
,
4540 target_name(target
),
4541 target_type_name(target
),
4543 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
4544 Jim_GetString(teap
->body
, NULL
));
4546 /* Override current target by the target an event
4547 * is issued from (lot of scripts need it).
4548 * Return back to previous override as soon
4549 * as the handler processing is done */
4550 struct command_context
*cmd_ctx
= current_command_context(teap
->interp
);
4551 struct target
*saved_target_override
= cmd_ctx
->current_target_override
;
4552 cmd_ctx
->current_target_override
= target
;
4554 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
) {
4555 Jim_MakeErrorMessage(teap
->interp
);
4556 LOG_USER("Error executing event %s on target %s:\n%s",
4557 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
4558 target_name(target
),
4559 Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
4560 /* clean both error code and stacktrace before return */
4561 Jim_Eval(teap
->interp
, "error \"\" \"\"");
4564 cmd_ctx
->current_target_override
= saved_target_override
;
4570 * Returns true only if the target has a handler for the specified event.
4572 bool target_has_event_action(struct target
*target
, enum target_event event
)
4574 struct target_event_action
*teap
;
4576 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4577 if (teap
->event
== event
)
4583 enum target_cfg_param
{
4586 TCFG_WORK_AREA_VIRT
,
4587 TCFG_WORK_AREA_PHYS
,
4588 TCFG_WORK_AREA_SIZE
,
4589 TCFG_WORK_AREA_BACKUP
,
4592 TCFG_CHAIN_POSITION
,
4599 static Jim_Nvp nvp_config_opts
[] = {
4600 { .name
= "-type", .value
= TCFG_TYPE
},
4601 { .name
= "-event", .value
= TCFG_EVENT
},
4602 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
4603 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
4604 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
4605 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
4606 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
4607 { .name
= "-coreid", .value
= TCFG_COREID
},
4608 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
4609 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
4610 { .name
= "-rtos", .value
= TCFG_RTOS
},
4611 { .name
= "-defer-examine", .value
= TCFG_DEFER_EXAMINE
},
4612 { .name
= "-gdb-port", .value
= TCFG_GDB_PORT
},
4613 { .name
= NULL
, .value
= -1 }
4616 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
4623 /* parse config or cget options ... */
4624 while (goi
->argc
> 0) {
4625 Jim_SetEmptyResult(goi
->interp
);
4626 /* Jim_GetOpt_Debug(goi); */
4628 if (target
->type
->target_jim_configure
) {
4629 /* target defines a configure function */
4630 /* target gets first dibs on parameters */
4631 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
4640 /* otherwise we 'continue' below */
4642 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
4644 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
4650 if (goi
->isconfigure
) {
4651 Jim_SetResultFormatted(goi
->interp
,
4652 "not settable: %s", n
->name
);
4656 if (goi
->argc
!= 0) {
4657 Jim_WrongNumArgs(goi
->interp
,
4658 goi
->argc
, goi
->argv
,
4663 Jim_SetResultString(goi
->interp
,
4664 target_type_name(target
), -1);
4668 if (goi
->argc
== 0) {
4669 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
4673 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
4675 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
4679 if (goi
->isconfigure
) {
4680 if (goi
->argc
!= 1) {
4681 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
4685 if (goi
->argc
!= 0) {
4686 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
4692 struct target_event_action
*teap
;
4694 teap
= target
->event_action
;
4695 /* replace existing? */
4697 if (teap
->event
== (enum target_event
)n
->value
)
4702 if (goi
->isconfigure
) {
4703 bool replace
= true;
4706 teap
= calloc(1, sizeof(*teap
));
4709 teap
->event
= n
->value
;
4710 teap
->interp
= goi
->interp
;
4711 Jim_GetOpt_Obj(goi
, &o
);
4713 Jim_DecrRefCount(teap
->interp
, teap
->body
);
4714 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
4717 * Tcl/TK - "tk events" have a nice feature.
4718 * See the "BIND" command.
4719 * We should support that here.
4720 * You can specify %X and %Y in the event code.
4721 * The idea is: %T - target name.
4722 * The idea is: %N - target number
4723 * The idea is: %E - event name.
4725 Jim_IncrRefCount(teap
->body
);
4728 /* add to head of event list */
4729 teap
->next
= target
->event_action
;
4730 target
->event_action
= teap
;
4732 Jim_SetEmptyResult(goi
->interp
);
4736 Jim_SetEmptyResult(goi
->interp
);
4738 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
4744 case TCFG_WORK_AREA_VIRT
:
4745 if (goi
->isconfigure
) {
4746 target_free_all_working_areas(target
);
4747 e
= Jim_GetOpt_Wide(goi
, &w
);
4750 target
->working_area_virt
= w
;
4751 target
->working_area_virt_spec
= true;
4756 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
4760 case TCFG_WORK_AREA_PHYS
:
4761 if (goi
->isconfigure
) {
4762 target_free_all_working_areas(target
);
4763 e
= Jim_GetOpt_Wide(goi
, &w
);
4766 target
->working_area_phys
= w
;
4767 target
->working_area_phys_spec
= true;
4772 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
4776 case TCFG_WORK_AREA_SIZE
:
4777 if (goi
->isconfigure
) {
4778 target_free_all_working_areas(target
);
4779 e
= Jim_GetOpt_Wide(goi
, &w
);
4782 target
->working_area_size
= w
;
4787 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4791 case TCFG_WORK_AREA_BACKUP
:
4792 if (goi
->isconfigure
) {
4793 target_free_all_working_areas(target
);
4794 e
= Jim_GetOpt_Wide(goi
, &w
);
4797 /* make this exactly 1 or 0 */
4798 target
->backup_working_area
= (!!w
);
4803 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
4804 /* loop for more e*/
4809 if (goi
->isconfigure
) {
4810 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
4812 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
4815 target
->endianness
= n
->value
;
4820 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4821 if (n
->name
== NULL
) {
4822 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4823 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4825 Jim_SetResultString(goi
->interp
, n
->name
, -1);
4830 if (goi
->isconfigure
) {
4831 e
= Jim_GetOpt_Wide(goi
, &w
);
4834 target
->coreid
= (int32_t)w
;
4839 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->coreid
));
4843 case TCFG_CHAIN_POSITION
:
4844 if (goi
->isconfigure
) {
4846 struct jtag_tap
*tap
;
4848 if (target
->has_dap
) {
4849 Jim_SetResultString(goi
->interp
,
4850 "target requires -dap parameter instead of -chain-position!", -1);
4854 target_free_all_working_areas(target
);
4855 e
= Jim_GetOpt_Obj(goi
, &o_t
);
4858 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
4862 target
->tap_configured
= true;
4867 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
4868 /* loop for more e*/
4871 if (goi
->isconfigure
) {
4872 e
= Jim_GetOpt_Wide(goi
, &w
);
4875 target
->dbgbase
= (uint32_t)w
;
4876 target
->dbgbase_set
= true;
4881 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
4887 int result
= rtos_create(goi
, target
);
4888 if (result
!= JIM_OK
)
4894 case TCFG_DEFER_EXAMINE
:
4896 target
->defer_examine
= true;
4901 if (goi
->isconfigure
) {
4903 e
= Jim_GetOpt_String(goi
, &s
, NULL
);
4906 target
->gdb_port_override
= strdup(s
);
4911 Jim_SetResultString(goi
->interp
, target
->gdb_port_override
? : "undefined", -1);
4915 } /* while (goi->argc) */
4918 /* done - we return */
4922 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4926 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4927 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4929 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4930 "missing: -option ...");
4933 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4934 return target_configure(&goi
, target
);
4937 static int jim_target_mem2array(Jim_Interp
*interp
,
4938 int argc
, Jim_Obj
*const *argv
)
4940 struct target
*target
= Jim_CmdPrivData(interp
);
4941 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4944 static int jim_target_array2mem(Jim_Interp
*interp
,
4945 int argc
, Jim_Obj
*const *argv
)
4947 struct target
*target
= Jim_CmdPrivData(interp
);
4948 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4951 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4953 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
4957 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4959 bool allow_defer
= false;
4962 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4964 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4965 Jim_SetResultFormatted(goi
.interp
,
4966 "usage: %s ['allow-defer']", cmd_name
);
4970 strcmp(Jim_GetString(argv
[1], NULL
), "allow-defer") == 0) {
4972 struct Jim_Obj
*obj
;
4973 int e
= Jim_GetOpt_Obj(&goi
, &obj
);
4979 struct target
*target
= Jim_CmdPrivData(interp
);
4980 if (!target
->tap
->enabled
)
4981 return jim_target_tap_disabled(interp
);
4983 if (allow_defer
&& target
->defer_examine
) {
4984 LOG_INFO("Deferring arp_examine of %s", target_name(target
));
4985 LOG_INFO("Use arp_examine command to examine it manually!");
4989 int e
= target
->type
->examine(target
);
4995 static int jim_target_was_examined(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4997 struct target
*target
= Jim_CmdPrivData(interp
);
4999 Jim_SetResultBool(interp
, target_was_examined(target
));
5003 static int jim_target_examine_deferred(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5005 struct target
*target
= Jim_CmdPrivData(interp
);
5007 Jim_SetResultBool(interp
, target
->defer_examine
);
5011 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5014 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5017 struct target
*target
= Jim_CmdPrivData(interp
);
5019 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
5025 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5028 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5031 struct target
*target
= Jim_CmdPrivData(interp
);
5032 if (!target
->tap
->enabled
)
5033 return jim_target_tap_disabled(interp
);
5036 if (!(target_was_examined(target
)))
5037 e
= ERROR_TARGET_NOT_EXAMINED
;
5039 e
= target
->type
->poll(target
);
5045 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5048 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5050 if (goi
.argc
!= 2) {
5051 Jim_WrongNumArgs(interp
, 0, argv
,
5052 "([tT]|[fF]|assert|deassert) BOOL");
5057 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
5059 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
5062 /* the halt or not param */
5064 e
= Jim_GetOpt_Wide(&goi
, &a
);
5068 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
5069 if (!target
->tap
->enabled
)
5070 return jim_target_tap_disabled(interp
);
5072 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
5073 Jim_SetResultFormatted(interp
,
5074 "No target-specific reset for %s",
5075 target_name(target
));
5079 if (target
->defer_examine
)
5080 target_reset_examined(target
);
5082 /* determine if we should halt or not. */
5083 target
->reset_halt
= !!a
;
5084 /* When this happens - all workareas are invalid. */
5085 target_free_all_working_areas_restore(target
, 0);
5088 if (n
->value
== NVP_ASSERT
)
5089 e
= target
->type
->assert_reset(target
);
5091 e
= target
->type
->deassert_reset(target
);
5092 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5095 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5098 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5101 struct target
*target
= Jim_CmdPrivData(interp
);
5102 if (!target
->tap
->enabled
)
5103 return jim_target_tap_disabled(interp
);
5104 int e
= target
->type
->halt(target
);
5105 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5108 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5111 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5113 /* params: <name> statename timeoutmsecs */
5114 if (goi
.argc
!= 2) {
5115 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5116 Jim_SetResultFormatted(goi
.interp
,
5117 "%s <state_name> <timeout_in_msec>", cmd_name
);
5122 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
5124 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
, 1);
5128 e
= Jim_GetOpt_Wide(&goi
, &a
);
5131 struct target
*target
= Jim_CmdPrivData(interp
);
5132 if (!target
->tap
->enabled
)
5133 return jim_target_tap_disabled(interp
);
5135 e
= target_wait_state(target
, n
->value
, a
);
5136 if (e
!= ERROR_OK
) {
5137 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
5138 Jim_SetResultFormatted(goi
.interp
,
5139 "target: %s wait %s fails (%#s) %s",
5140 target_name(target
), n
->name
,
5141 eObj
, target_strerror_safe(e
));
5142 Jim_FreeNewObj(interp
, eObj
);
5147 /* List for human, Events defined for this target.
5148 * scripts/programs should use 'name cget -event NAME'
5150 COMMAND_HANDLER(handle_target_event_list
)
5152 struct target
*target
= get_current_target(CMD_CTX
);
5153 struct target_event_action
*teap
= target
->event_action
;
5155 command_print(CMD_CTX
, "Event actions for target (%d) %s\n",
5156 target
->target_number
,
5157 target_name(target
));
5158 command_print(CMD_CTX
, "%-25s | Body", "Event");
5159 command_print(CMD_CTX
, "------------------------- | "
5160 "----------------------------------------");
5162 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
5163 command_print(CMD_CTX
, "%-25s | %s",
5164 opt
->name
, Jim_GetString(teap
->body
, NULL
));
5167 command_print(CMD_CTX
, "***END***");
5170 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5173 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5176 struct target
*target
= Jim_CmdPrivData(interp
);
5177 Jim_SetResultString(interp
, target_state_name(target
), -1);
5180 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5183 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5184 if (goi
.argc
!= 1) {
5185 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5186 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
5190 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
5192 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
5195 struct target
*target
= Jim_CmdPrivData(interp
);
5196 target_handle_event(target
, n
->value
);
5200 static const struct command_registration target_instance_command_handlers
[] = {
5202 .name
= "configure",
5203 .mode
= COMMAND_CONFIG
,
5204 .jim_handler
= jim_target_configure
,
5205 .help
= "configure a new target for use",
5206 .usage
= "[target_attribute ...]",
5210 .mode
= COMMAND_ANY
,
5211 .jim_handler
= jim_target_configure
,
5212 .help
= "returns the specified target attribute",
5213 .usage
= "target_attribute",
5217 .handler
= handle_mw_command
,
5218 .mode
= COMMAND_EXEC
,
5219 .help
= "Write 64-bit word(s) to target memory",
5220 .usage
= "address data [count]",
5224 .handler
= handle_mw_command
,
5225 .mode
= COMMAND_EXEC
,
5226 .help
= "Write 32-bit word(s) to target memory",
5227 .usage
= "address data [count]",
5231 .handler
= handle_mw_command
,
5232 .mode
= COMMAND_EXEC
,
5233 .help
= "Write 16-bit half-word(s) to target memory",
5234 .usage
= "address data [count]",
5238 .handler
= handle_mw_command
,
5239 .mode
= COMMAND_EXEC
,
5240 .help
= "Write byte(s) to target memory",
5241 .usage
= "address data [count]",
5245 .handler
= handle_md_command
,
5246 .mode
= COMMAND_EXEC
,
5247 .help
= "Display target memory as 64-bit words",
5248 .usage
= "address [count]",
5252 .handler
= handle_md_command
,
5253 .mode
= COMMAND_EXEC
,
5254 .help
= "Display target memory as 32-bit words",
5255 .usage
= "address [count]",
5259 .handler
= handle_md_command
,
5260 .mode
= COMMAND_EXEC
,
5261 .help
= "Display target memory as 16-bit half-words",
5262 .usage
= "address [count]",
5266 .handler
= handle_md_command
,
5267 .mode
= COMMAND_EXEC
,
5268 .help
= "Display target memory as 8-bit bytes",
5269 .usage
= "address [count]",
5272 .name
= "array2mem",
5273 .mode
= COMMAND_EXEC
,
5274 .jim_handler
= jim_target_array2mem
,
5275 .help
= "Writes Tcl array of 8/16/32 bit numbers "
5277 .usage
= "arrayname bitwidth address count",
5280 .name
= "mem2array",
5281 .mode
= COMMAND_EXEC
,
5282 .jim_handler
= jim_target_mem2array
,
5283 .help
= "Loads Tcl array of 8/16/32 bit numbers "
5284 "from target memory",
5285 .usage
= "arrayname bitwidth address count",
5288 .name
= "eventlist",
5289 .handler
= handle_target_event_list
,
5290 .mode
= COMMAND_EXEC
,
5291 .help
= "displays a table of events defined for this target",
5296 .mode
= COMMAND_EXEC
,
5297 .jim_handler
= jim_target_current_state
,
5298 .help
= "displays the current state of this target",
5301 .name
= "arp_examine",
5302 .mode
= COMMAND_EXEC
,
5303 .jim_handler
= jim_target_examine
,
5304 .help
= "used internally for reset processing",
5305 .usage
= "['allow-defer']",
5308 .name
= "was_examined",
5309 .mode
= COMMAND_EXEC
,
5310 .jim_handler
= jim_target_was_examined
,
5311 .help
= "used internally for reset processing",
5314 .name
= "examine_deferred",
5315 .mode
= COMMAND_EXEC
,
5316 .jim_handler
= jim_target_examine_deferred
,
5317 .help
= "used internally for reset processing",
5320 .name
= "arp_halt_gdb",
5321 .mode
= COMMAND_EXEC
,
5322 .jim_handler
= jim_target_halt_gdb
,
5323 .help
= "used internally for reset processing to halt GDB",
5327 .mode
= COMMAND_EXEC
,
5328 .jim_handler
= jim_target_poll
,
5329 .help
= "used internally for reset processing",
5332 .name
= "arp_reset",
5333 .mode
= COMMAND_EXEC
,
5334 .jim_handler
= jim_target_reset
,
5335 .help
= "used internally for reset processing",
5339 .mode
= COMMAND_EXEC
,
5340 .jim_handler
= jim_target_halt
,
5341 .help
= "used internally for reset processing",
5344 .name
= "arp_waitstate",
5345 .mode
= COMMAND_EXEC
,
5346 .jim_handler
= jim_target_wait_state
,
5347 .help
= "used internally for reset processing",
5350 .name
= "invoke-event",
5351 .mode
= COMMAND_EXEC
,
5352 .jim_handler
= jim_target_invoke_event
,
5353 .help
= "invoke handler for specified event",
5354 .usage
= "event_name",
5356 COMMAND_REGISTRATION_DONE
5359 static int target_create(Jim_GetOptInfo
*goi
)
5366 struct target
*target
;
5367 struct command_context
*cmd_ctx
;
5369 cmd_ctx
= current_command_context(goi
->interp
);
5370 assert(cmd_ctx
!= NULL
);
5372 if (goi
->argc
< 3) {
5373 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
5378 Jim_GetOpt_Obj(goi
, &new_cmd
);
5379 /* does this command exist? */
5380 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
5382 cp
= Jim_GetString(new_cmd
, NULL
);
5383 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
5388 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
5391 struct transport
*tr
= get_current_transport();
5392 if (tr
->override_target
) {
5393 e
= tr
->override_target(&cp
);
5394 if (e
!= ERROR_OK
) {
5395 LOG_ERROR("The selected transport doesn't support this target");
5398 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
5400 /* now does target type exist */
5401 for (x
= 0 ; target_types
[x
] ; x
++) {
5402 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
5407 /* check for deprecated name */
5408 if (target_types
[x
]->deprecated_name
) {
5409 if (0 == strcmp(cp
, target_types
[x
]->deprecated_name
)) {
5411 LOG_WARNING("target name is deprecated use: \'%s\'", target_types
[x
]->name
);
5416 if (target_types
[x
] == NULL
) {
5417 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
5418 for (x
= 0 ; target_types
[x
] ; x
++) {
5419 if (target_types
[x
+ 1]) {
5420 Jim_AppendStrings(goi
->interp
,
5421 Jim_GetResult(goi
->interp
),
5422 target_types
[x
]->name
,
5425 Jim_AppendStrings(goi
->interp
,
5426 Jim_GetResult(goi
->interp
),
5428 target_types
[x
]->name
, NULL
);
5435 target
= calloc(1, sizeof(struct target
));
5436 /* set target number */
5437 target
->target_number
= new_target_number();
5438 cmd_ctx
->current_target
= target
;
5440 /* allocate memory for each unique target type */
5441 target
->type
= calloc(1, sizeof(struct target_type
));
5443 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
5445 /* will be set by "-endian" */
5446 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5448 /* default to first core, override with -coreid */
5451 target
->working_area
= 0x0;
5452 target
->working_area_size
= 0x0;
5453 target
->working_areas
= NULL
;
5454 target
->backup_working_area
= 0;
5456 target
->state
= TARGET_UNKNOWN
;
5457 target
->debug_reason
= DBG_REASON_UNDEFINED
;
5458 target
->reg_cache
= NULL
;
5459 target
->breakpoints
= NULL
;
5460 target
->watchpoints
= NULL
;
5461 target
->next
= NULL
;
5462 target
->arch_info
= NULL
;
5464 target
->verbose_halt_msg
= true;
5466 target
->halt_issued
= false;
5468 /* initialize trace information */
5469 target
->trace_info
= calloc(1, sizeof(struct trace
));
5471 target
->dbgmsg
= NULL
;
5472 target
->dbg_msg_enabled
= 0;
5474 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5476 target
->rtos
= NULL
;
5477 target
->rtos_auto_detect
= false;
5479 target
->gdb_port_override
= NULL
;
5481 /* Do the rest as "configure" options */
5482 goi
->isconfigure
= 1;
5483 e
= target_configure(goi
, target
);
5486 if (target
->has_dap
) {
5487 if (!target
->dap_configured
) {
5488 Jim_SetResultString(goi
->interp
, "-dap ?name? required when creating target", -1);
5492 if (!target
->tap_configured
) {
5493 Jim_SetResultString(goi
->interp
, "-chain-position ?name? required when creating target", -1);
5497 /* tap must be set after target was configured */
5498 if (target
->tap
== NULL
)
5503 free(target
->gdb_port_override
);
5509 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
5510 /* default endian to little if not specified */
5511 target
->endianness
= TARGET_LITTLE_ENDIAN
;
5514 cp
= Jim_GetString(new_cmd
, NULL
);
5515 target
->cmd_name
= strdup(cp
);
5517 if (target
->type
->target_create
) {
5518 e
= (*(target
->type
->target_create
))(target
, goi
->interp
);
5519 if (e
!= ERROR_OK
) {
5520 LOG_DEBUG("target_create failed");
5521 free(target
->gdb_port_override
);
5523 free(target
->cmd_name
);
5529 /* create the target specific commands */
5530 if (target
->type
->commands
) {
5531 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
5533 LOG_ERROR("unable to register '%s' commands", cp
);
5536 /* append to end of list */
5538 struct target
**tpp
;
5539 tpp
= &(all_targets
);
5541 tpp
= &((*tpp
)->next
);
5545 /* now - create the new target name command */
5546 const struct command_registration target_subcommands
[] = {
5548 .chain
= target_instance_command_handlers
,
5551 .chain
= target
->type
->commands
,
5553 COMMAND_REGISTRATION_DONE
5555 const struct command_registration target_commands
[] = {
5558 .mode
= COMMAND_ANY
,
5559 .help
= "target command group",
5561 .chain
= target_subcommands
,
5563 COMMAND_REGISTRATION_DONE
5565 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
5569 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
5571 command_set_handler_data(c
, target
);
5573 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
5576 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5579 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5582 struct command_context
*cmd_ctx
= current_command_context(interp
);
5583 assert(cmd_ctx
!= NULL
);
5585 Jim_SetResultString(interp
, target_name(get_current_target(cmd_ctx
)), -1);
5589 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5592 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5595 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5596 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++) {
5597 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5598 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
5603 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5606 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5609 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5610 struct target
*target
= all_targets
;
5612 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5613 Jim_NewStringObj(interp
, target_name(target
), -1));
5614 target
= target
->next
;
5619 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5622 const char *targetname
;
5624 struct target
*target
= (struct target
*) NULL
;
5625 struct target_list
*head
, *curr
, *new;
5626 curr
= (struct target_list
*) NULL
;
5627 head
= (struct target_list
*) NULL
;
5630 LOG_DEBUG("%d", argc
);
5631 /* argv[1] = target to associate in smp
5632 * argv[2] = target to assoicate in smp
5636 for (i
= 1; i
< argc
; i
++) {
5638 targetname
= Jim_GetString(argv
[i
], &len
);
5639 target
= get_target(targetname
);
5640 LOG_DEBUG("%s ", targetname
);
5642 new = malloc(sizeof(struct target_list
));
5643 new->target
= target
;
5644 new->next
= (struct target_list
*)NULL
;
5645 if (head
== (struct target_list
*)NULL
) {
5654 /* now parse the list of cpu and put the target in smp mode*/
5657 while (curr
!= (struct target_list
*)NULL
) {
5658 target
= curr
->target
;
5660 target
->head
= head
;
5664 if (target
&& target
->rtos
)
5665 retval
= rtos_smp_init(head
->target
);
5671 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5674 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5676 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5677 "<name> <target_type> [<target_options> ...]");
5680 return target_create(&goi
);
5683 static const struct command_registration target_subcommand_handlers
[] = {
5686 .mode
= COMMAND_CONFIG
,
5687 .handler
= handle_target_init_command
,
5688 .help
= "initialize targets",
5693 .mode
= COMMAND_CONFIG
,
5694 .jim_handler
= jim_target_create
,
5695 .usage
= "name type '-chain-position' name [options ...]",
5696 .help
= "Creates and selects a new target",
5700 .mode
= COMMAND_ANY
,
5701 .jim_handler
= jim_target_current
,
5702 .help
= "Returns the currently selected target",
5706 .mode
= COMMAND_ANY
,
5707 .jim_handler
= jim_target_types
,
5708 .help
= "Returns the available target types as "
5709 "a list of strings",
5713 .mode
= COMMAND_ANY
,
5714 .jim_handler
= jim_target_names
,
5715 .help
= "Returns the names of all targets as a list of strings",
5719 .mode
= COMMAND_ANY
,
5720 .jim_handler
= jim_target_smp
,
5721 .usage
= "targetname1 targetname2 ...",
5722 .help
= "gather several target in a smp list"
5725 COMMAND_REGISTRATION_DONE
5729 target_addr_t address
;
5735 static int fastload_num
;
5736 static struct FastLoad
*fastload
;
5738 static void free_fastload(void)
5740 if (fastload
!= NULL
) {
5742 for (i
= 0; i
< fastload_num
; i
++) {
5743 if (fastload
[i
].data
)
5744 free(fastload
[i
].data
);
5751 COMMAND_HANDLER(handle_fast_load_image_command
)
5755 uint32_t image_size
;
5756 target_addr_t min_address
= 0;
5757 target_addr_t max_address
= -1;
5762 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
5763 &image
, &min_address
, &max_address
);
5764 if (ERROR_OK
!= retval
)
5767 struct duration bench
;
5768 duration_start(&bench
);
5770 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5771 if (retval
!= ERROR_OK
)
5776 fastload_num
= image
.num_sections
;
5777 fastload
= malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5778 if (fastload
== NULL
) {
5779 command_print(CMD_CTX
, "out of memory");
5780 image_close(&image
);
5783 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5784 for (i
= 0; i
< image
.num_sections
; i
++) {
5785 buffer
= malloc(image
.sections
[i
].size
);
5786 if (buffer
== NULL
) {
5787 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5788 (int)(image
.sections
[i
].size
));
5789 retval
= ERROR_FAIL
;
5793 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
5794 if (retval
!= ERROR_OK
) {
5799 uint32_t offset
= 0;
5800 uint32_t length
= buf_cnt
;
5802 /* DANGER!!! beware of unsigned comparision here!!! */
5804 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
5805 (image
.sections
[i
].base_address
< max_address
)) {
5806 if (image
.sections
[i
].base_address
< min_address
) {
5807 /* clip addresses below */
5808 offset
+= min_address
-image
.sections
[i
].base_address
;
5812 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5813 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5815 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5816 fastload
[i
].data
= malloc(length
);
5817 if (fastload
[i
].data
== NULL
) {
5819 command_print(CMD_CTX
, "error allocating buffer for section (%" PRIu32
" bytes)",
5821 retval
= ERROR_FAIL
;
5824 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5825 fastload
[i
].length
= length
;
5827 image_size
+= length
;
5828 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
5829 (unsigned int)length
,
5830 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
5836 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
5837 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
5838 "in %fs (%0.3f KiB/s)", image_size
,
5839 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
5841 command_print(CMD_CTX
,
5842 "WARNING: image has not been loaded to target!"
5843 "You can issue a 'fast_load' to finish loading.");
5846 image_close(&image
);
5848 if (retval
!= ERROR_OK
)
5854 COMMAND_HANDLER(handle_fast_load_command
)
5857 return ERROR_COMMAND_SYNTAX_ERROR
;
5858 if (fastload
== NULL
) {
5859 LOG_ERROR("No image in memory");
5863 int64_t ms
= timeval_ms();
5865 int retval
= ERROR_OK
;
5866 for (i
= 0; i
< fastload_num
; i
++) {
5867 struct target
*target
= get_current_target(CMD_CTX
);
5868 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
5869 (unsigned int)(fastload
[i
].address
),
5870 (unsigned int)(fastload
[i
].length
));
5871 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5872 if (retval
!= ERROR_OK
)
5874 size
+= fastload
[i
].length
;
5876 if (retval
== ERROR_OK
) {
5877 int64_t after
= timeval_ms();
5878 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5883 static const struct command_registration target_command_handlers
[] = {
5886 .handler
= handle_targets_command
,
5887 .mode
= COMMAND_ANY
,
5888 .help
= "change current default target (one parameter) "
5889 "or prints table of all targets (no parameters)",
5890 .usage
= "[target]",
5894 .mode
= COMMAND_CONFIG
,
5895 .help
= "configure target",
5896 .chain
= target_subcommand_handlers
,
5899 COMMAND_REGISTRATION_DONE
5902 int target_register_commands(struct command_context
*cmd_ctx
)
5904 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5907 static bool target_reset_nag
= true;
5909 bool get_target_reset_nag(void)
5911 return target_reset_nag
;
5914 COMMAND_HANDLER(handle_target_reset_nag
)
5916 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
5917 &target_reset_nag
, "Nag after each reset about options to improve "
5921 COMMAND_HANDLER(handle_ps_command
)
5923 struct target
*target
= get_current_target(CMD_CTX
);
5925 if (target
->state
!= TARGET_HALTED
) {
5926 LOG_INFO("target not halted !!");
5930 if ((target
->rtos
) && (target
->rtos
->type
)
5931 && (target
->rtos
->type
->ps_command
)) {
5932 display
= target
->rtos
->type
->ps_command(target
);
5933 command_print(CMD_CTX
, "%s", display
);
5938 return ERROR_TARGET_FAILURE
;
5942 static void binprint(struct command_invocation
*cmd
, const char *text
, const uint8_t *buf
, int size
)
5945 command_print_sameline(cmd
->ctx
, "%s", text
);
5946 for (int i
= 0; i
< size
; i
++)
5947 command_print_sameline(cmd
->ctx
, " %02x", buf
[i
]);
5948 command_print(cmd
->ctx
, " ");
5951 COMMAND_HANDLER(handle_test_mem_access_command
)
5953 struct target
*target
= get_current_target(CMD_CTX
);
5955 int retval
= ERROR_OK
;
5957 if (target
->state
!= TARGET_HALTED
) {
5958 LOG_INFO("target not halted !!");
5963 return ERROR_COMMAND_SYNTAX_ERROR
;
5965 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], test_size
);
5968 size_t num_bytes
= test_size
+ 4;
5970 struct working_area
*wa
= NULL
;
5971 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
5972 if (retval
!= ERROR_OK
) {
5973 LOG_ERROR("Not enough working area");
5977 uint8_t *test_pattern
= malloc(num_bytes
);
5979 for (size_t i
= 0; i
< num_bytes
; i
++)
5980 test_pattern
[i
] = rand();
5982 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
5983 if (retval
!= ERROR_OK
) {
5984 LOG_ERROR("Test pattern write failed");
5988 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
5989 for (int size
= 1; size
<= 4; size
*= 2) {
5990 for (int offset
= 0; offset
< 4; offset
++) {
5991 uint32_t count
= test_size
/ size
;
5992 size_t host_bufsiz
= (count
+ 2) * size
+ host_offset
;
5993 uint8_t *read_ref
= malloc(host_bufsiz
);
5994 uint8_t *read_buf
= malloc(host_bufsiz
);
5996 for (size_t i
= 0; i
< host_bufsiz
; i
++) {
5997 read_ref
[i
] = rand();
5998 read_buf
[i
] = read_ref
[i
];
6000 command_print_sameline(CMD_CTX
,
6001 "Test read %" PRIu32
" x %d @ %d to %saligned buffer: ", count
,
6002 size
, offset
, host_offset
? "un" : "");
6004 struct duration bench
;
6005 duration_start(&bench
);
6007 retval
= target_read_memory(target
, wa
->address
+ offset
, size
, count
,
6008 read_buf
+ size
+ host_offset
);
6010 duration_measure(&bench
);
6012 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6013 command_print(CMD_CTX
, "Unsupported alignment");
6015 } else if (retval
!= ERROR_OK
) {
6016 command_print(CMD_CTX
, "Memory read failed");
6020 /* replay on host */
6021 memcpy(read_ref
+ size
+ host_offset
, test_pattern
+ offset
, count
* size
);
6024 int result
= memcmp(read_ref
, read_buf
, host_bufsiz
);
6026 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
6027 duration_elapsed(&bench
),
6028 duration_kbps(&bench
, count
* size
));
6030 command_print(CMD_CTX
, "Compare failed");
6031 binprint(CMD
, "ref:", read_ref
, host_bufsiz
);
6032 binprint(CMD
, "buf:", read_buf
, host_bufsiz
);
6045 target_free_working_area(target
, wa
);
6048 num_bytes
= test_size
+ 4 + 4 + 4;
6050 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6051 if (retval
!= ERROR_OK
) {
6052 LOG_ERROR("Not enough working area");
6056 test_pattern
= malloc(num_bytes
);
6058 for (size_t i
= 0; i
< num_bytes
; i
++)
6059 test_pattern
[i
] = rand();
6061 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6062 for (int size
= 1; size
<= 4; size
*= 2) {
6063 for (int offset
= 0; offset
< 4; offset
++) {
6064 uint32_t count
= test_size
/ size
;
6065 size_t host_bufsiz
= count
* size
+ host_offset
;
6066 uint8_t *read_ref
= malloc(num_bytes
);
6067 uint8_t *read_buf
= malloc(num_bytes
);
6068 uint8_t *write_buf
= malloc(host_bufsiz
);
6070 for (size_t i
= 0; i
< host_bufsiz
; i
++)
6071 write_buf
[i
] = rand();
6072 command_print_sameline(CMD_CTX
,
6073 "Test write %" PRIu32
" x %d @ %d from %saligned buffer: ", count
,
6074 size
, offset
, host_offset
? "un" : "");
6076 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6077 if (retval
!= ERROR_OK
) {
6078 command_print(CMD_CTX
, "Test pattern write failed");
6082 /* replay on host */
6083 memcpy(read_ref
, test_pattern
, num_bytes
);
6084 memcpy(read_ref
+ size
+ offset
, write_buf
+ host_offset
, count
* size
);
6086 struct duration bench
;
6087 duration_start(&bench
);
6089 retval
= target_write_memory(target
, wa
->address
+ size
+ offset
, size
, count
,
6090 write_buf
+ host_offset
);
6092 duration_measure(&bench
);
6094 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6095 command_print(CMD_CTX
, "Unsupported alignment");
6097 } else if (retval
!= ERROR_OK
) {
6098 command_print(CMD_CTX
, "Memory write failed");
6103 retval
= target_read_memory(target
, wa
->address
, 1, num_bytes
, read_buf
);
6104 if (retval
!= ERROR_OK
) {
6105 command_print(CMD_CTX
, "Test pattern write failed");
6110 int result
= memcmp(read_ref
, read_buf
, num_bytes
);
6112 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
6113 duration_elapsed(&bench
),
6114 duration_kbps(&bench
, count
* size
));
6116 command_print(CMD_CTX
, "Compare failed");
6117 binprint(CMD
, "ref:", read_ref
, num_bytes
);
6118 binprint(CMD
, "buf:", read_buf
, num_bytes
);
6130 target_free_working_area(target
, wa
);
6134 static const struct command_registration target_exec_command_handlers
[] = {
6136 .name
= "fast_load_image",
6137 .handler
= handle_fast_load_image_command
,
6138 .mode
= COMMAND_ANY
,
6139 .help
= "Load image into server memory for later use by "
6140 "fast_load; primarily for profiling",
6141 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6142 "[min_address [max_length]]",
6145 .name
= "fast_load",
6146 .handler
= handle_fast_load_command
,
6147 .mode
= COMMAND_EXEC
,
6148 .help
= "loads active fast load image to current target "
6149 "- mainly for profiling purposes",
6154 .handler
= handle_profile_command
,
6155 .mode
= COMMAND_EXEC
,
6156 .usage
= "seconds filename [start end]",
6157 .help
= "profiling samples the CPU PC",
6159 /** @todo don't register virt2phys() unless target supports it */
6161 .name
= "virt2phys",
6162 .handler
= handle_virt2phys_command
,
6163 .mode
= COMMAND_ANY
,
6164 .help
= "translate a virtual address into a physical address",
6165 .usage
= "virtual_address",
6169 .handler
= handle_reg_command
,
6170 .mode
= COMMAND_EXEC
,
6171 .help
= "display (reread from target with \"force\") or set a register; "
6172 "with no arguments, displays all registers and their values",
6173 .usage
= "[(register_number|register_name) [(value|'force')]]",
6177 .handler
= handle_poll_command
,
6178 .mode
= COMMAND_EXEC
,
6179 .help
= "poll target state; or reconfigure background polling",
6180 .usage
= "['on'|'off']",
6183 .name
= "wait_halt",
6184 .handler
= handle_wait_halt_command
,
6185 .mode
= COMMAND_EXEC
,
6186 .help
= "wait up to the specified number of milliseconds "
6187 "(default 5000) for a previously requested halt",
6188 .usage
= "[milliseconds]",
6192 .handler
= handle_halt_command
,
6193 .mode
= COMMAND_EXEC
,
6194 .help
= "request target to halt, then wait up to the specified"
6195 "number of milliseconds (default 5000) for it to complete",
6196 .usage
= "[milliseconds]",
6200 .handler
= handle_resume_command
,
6201 .mode
= COMMAND_EXEC
,
6202 .help
= "resume target execution from current PC or address",
6203 .usage
= "[address]",
6207 .handler
= handle_reset_command
,
6208 .mode
= COMMAND_EXEC
,
6209 .usage
= "[run|halt|init]",
6210 .help
= "Reset all targets into the specified mode."
6211 "Default reset mode is run, if not given.",
6214 .name
= "soft_reset_halt",
6215 .handler
= handle_soft_reset_halt_command
,
6216 .mode
= COMMAND_EXEC
,
6218 .help
= "halt the target and do a soft reset",
6222 .handler
= handle_step_command
,
6223 .mode
= COMMAND_EXEC
,
6224 .help
= "step one instruction from current PC or address",
6225 .usage
= "[address]",
6229 .handler
= handle_md_command
,
6230 .mode
= COMMAND_EXEC
,
6231 .help
= "display memory double-words",
6232 .usage
= "['phys'] address [count]",
6236 .handler
= handle_md_command
,
6237 .mode
= COMMAND_EXEC
,
6238 .help
= "display memory words",
6239 .usage
= "['phys'] address [count]",
6243 .handler
= handle_md_command
,
6244 .mode
= COMMAND_EXEC
,
6245 .help
= "display memory half-words",
6246 .usage
= "['phys'] address [count]",
6250 .handler
= handle_md_command
,
6251 .mode
= COMMAND_EXEC
,
6252 .help
= "display memory bytes",
6253 .usage
= "['phys'] address [count]",
6257 .handler
= handle_mw_command
,
6258 .mode
= COMMAND_EXEC
,
6259 .help
= "write memory double-word",
6260 .usage
= "['phys'] address value [count]",
6264 .handler
= handle_mw_command
,
6265 .mode
= COMMAND_EXEC
,
6266 .help
= "write memory word",
6267 .usage
= "['phys'] address value [count]",
6271 .handler
= handle_mw_command
,
6272 .mode
= COMMAND_EXEC
,
6273 .help
= "write memory half-word",
6274 .usage
= "['phys'] address value [count]",
6278 .handler
= handle_mw_command
,
6279 .mode
= COMMAND_EXEC
,
6280 .help
= "write memory byte",
6281 .usage
= "['phys'] address value [count]",
6285 .handler
= handle_bp_command
,
6286 .mode
= COMMAND_EXEC
,
6287 .help
= "list or set hardware or software breakpoint",
6288 .usage
= "<address> [<asid>] <length> ['hw'|'hw_ctx']",
6292 .handler
= handle_rbp_command
,
6293 .mode
= COMMAND_EXEC
,
6294 .help
= "remove breakpoint",
6299 .handler
= handle_wp_command
,
6300 .mode
= COMMAND_EXEC
,
6301 .help
= "list (no params) or create watchpoints",
6302 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
6306 .handler
= handle_rwp_command
,
6307 .mode
= COMMAND_EXEC
,
6308 .help
= "remove watchpoint",
6312 .name
= "load_image",
6313 .handler
= handle_load_image_command
,
6314 .mode
= COMMAND_EXEC
,
6315 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6316 "[min_address] [max_length]",
6319 .name
= "dump_image",
6320 .handler
= handle_dump_image_command
,
6321 .mode
= COMMAND_EXEC
,
6322 .usage
= "filename address size",
6325 .name
= "verify_image_checksum",
6326 .handler
= handle_verify_image_checksum_command
,
6327 .mode
= COMMAND_EXEC
,
6328 .usage
= "filename [offset [type]]",
6331 .name
= "verify_image",
6332 .handler
= handle_verify_image_command
,
6333 .mode
= COMMAND_EXEC
,
6334 .usage
= "filename [offset [type]]",
6337 .name
= "test_image",
6338 .handler
= handle_test_image_command
,
6339 .mode
= COMMAND_EXEC
,
6340 .usage
= "filename [offset [type]]",
6343 .name
= "mem2array",
6344 .mode
= COMMAND_EXEC
,
6345 .jim_handler
= jim_mem2array
,
6346 .help
= "read 8/16/32 bit memory and return as a TCL array "
6347 "for script processing",
6348 .usage
= "arrayname bitwidth address count",
6351 .name
= "array2mem",
6352 .mode
= COMMAND_EXEC
,
6353 .jim_handler
= jim_array2mem
,
6354 .help
= "convert a TCL array to memory locations "
6355 "and write the 8/16/32 bit values",
6356 .usage
= "arrayname bitwidth address count",
6359 .name
= "reset_nag",
6360 .handler
= handle_target_reset_nag
,
6361 .mode
= COMMAND_ANY
,
6362 .help
= "Nag after each reset about options that could have been "
6363 "enabled to improve performance. ",
6364 .usage
= "['enable'|'disable']",
6368 .handler
= handle_ps_command
,
6369 .mode
= COMMAND_EXEC
,
6370 .help
= "list all tasks ",
6374 .name
= "test_mem_access",
6375 .handler
= handle_test_mem_access_command
,
6376 .mode
= COMMAND_EXEC
,
6377 .help
= "Test the target's memory access functions",
6381 COMMAND_REGISTRATION_DONE
6383 static int target_register_user_commands(struct command_context
*cmd_ctx
)
6385 int retval
= ERROR_OK
;
6386 retval
= target_request_register_commands(cmd_ctx
);
6387 if (retval
!= ERROR_OK
)
6390 retval
= trace_register_commands(cmd_ctx
);
6391 if (retval
!= ERROR_OK
)
6395 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);