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
;
113 static struct target_type
*target_types
[] = {
152 struct target
*all_targets
;
153 static struct target_event_callback
*target_event_callbacks
;
154 static struct target_timer_callback
*target_timer_callbacks
;
155 LIST_HEAD(target_reset_callback_list
);
156 LIST_HEAD(target_trace_callback_list
);
157 static const int polling_interval
= 100;
159 static const Jim_Nvp nvp_assert
[] = {
160 { .name
= "assert", NVP_ASSERT
},
161 { .name
= "deassert", NVP_DEASSERT
},
162 { .name
= "T", NVP_ASSERT
},
163 { .name
= "F", NVP_DEASSERT
},
164 { .name
= "t", NVP_ASSERT
},
165 { .name
= "f", NVP_DEASSERT
},
166 { .name
= NULL
, .value
= -1 }
169 static const Jim_Nvp nvp_error_target
[] = {
170 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
171 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
172 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
173 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
174 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
175 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
176 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
177 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
178 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
179 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
180 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
181 { .value
= -1, .name
= NULL
}
184 static const char *target_strerror_safe(int err
)
188 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
195 static const Jim_Nvp nvp_target_event
[] = {
197 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
198 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
199 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
200 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
201 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
203 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
204 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
206 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
207 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
208 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
209 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
210 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
211 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
212 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
213 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
215 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
216 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
218 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
219 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
221 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
222 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
224 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
225 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
227 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
228 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
230 { .value
= TARGET_EVENT_TRACE_CONFIG
, .name
= "trace-config" },
232 { .name
= NULL
, .value
= -1 }
235 static const Jim_Nvp nvp_target_state
[] = {
236 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
237 { .name
= "running", .value
= TARGET_RUNNING
},
238 { .name
= "halted", .value
= TARGET_HALTED
},
239 { .name
= "reset", .value
= TARGET_RESET
},
240 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
241 { .name
= NULL
, .value
= -1 },
244 static const Jim_Nvp nvp_target_debug_reason
[] = {
245 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
246 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
247 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
248 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
249 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
250 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
251 { .name
= "program-exit" , .value
= DBG_REASON_EXIT
},
252 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
253 { .name
= NULL
, .value
= -1 },
256 static const Jim_Nvp nvp_target_endian
[] = {
257 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
258 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
259 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
260 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
261 { .name
= NULL
, .value
= -1 },
264 static const Jim_Nvp nvp_reset_modes
[] = {
265 { .name
= "unknown", .value
= RESET_UNKNOWN
},
266 { .name
= "run" , .value
= RESET_RUN
},
267 { .name
= "halt" , .value
= RESET_HALT
},
268 { .name
= "init" , .value
= RESET_INIT
},
269 { .name
= NULL
, .value
= -1 },
272 const char *debug_reason_name(struct target
*t
)
276 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
277 t
->debug_reason
)->name
;
279 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
280 cp
= "(*BUG*unknown*BUG*)";
285 const char *target_state_name(struct target
*t
)
288 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
290 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
291 cp
= "(*BUG*unknown*BUG*)";
294 if (!target_was_examined(t
) && t
->defer_examine
)
295 cp
= "examine deferred";
300 const char *target_event_name(enum target_event event
)
303 cp
= Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
;
305 LOG_ERROR("Invalid target event: %d", (int)(event
));
306 cp
= "(*BUG*unknown*BUG*)";
311 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
314 cp
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
;
316 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
317 cp
= "(*BUG*unknown*BUG*)";
322 /* determine the number of the new target */
323 static int new_target_number(void)
328 /* number is 0 based */
332 if (x
< t
->target_number
)
333 x
= t
->target_number
;
339 /* read a uint64_t from a buffer in target memory endianness */
340 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
342 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
343 return le_to_h_u64(buffer
);
345 return be_to_h_u64(buffer
);
348 /* read a uint32_t from a buffer in target memory endianness */
349 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
351 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
352 return le_to_h_u32(buffer
);
354 return be_to_h_u32(buffer
);
357 /* read a uint24_t from a buffer in target memory endianness */
358 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
360 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
361 return le_to_h_u24(buffer
);
363 return be_to_h_u24(buffer
);
366 /* read a uint16_t from a buffer in target memory endianness */
367 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
369 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
370 return le_to_h_u16(buffer
);
372 return be_to_h_u16(buffer
);
375 /* read a uint8_t from a buffer in target memory endianness */
376 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
378 return *buffer
& 0x0ff;
381 /* write a uint64_t to a buffer in target memory endianness */
382 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
384 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
385 h_u64_to_le(buffer
, value
);
387 h_u64_to_be(buffer
, value
);
390 /* write a uint32_t to a buffer in target memory endianness */
391 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
393 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
394 h_u32_to_le(buffer
, value
);
396 h_u32_to_be(buffer
, value
);
399 /* write a uint24_t to a buffer in target memory endianness */
400 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
402 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
403 h_u24_to_le(buffer
, value
);
405 h_u24_to_be(buffer
, value
);
408 /* write a uint16_t to a buffer in target memory endianness */
409 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
411 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
412 h_u16_to_le(buffer
, value
);
414 h_u16_to_be(buffer
, value
);
417 /* write a uint8_t to a buffer in target memory endianness */
418 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
423 /* write a uint64_t array to a buffer in target memory endianness */
424 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
427 for (i
= 0; i
< count
; i
++)
428 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
431 /* write a uint32_t array to a buffer in target memory endianness */
432 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
435 for (i
= 0; i
< count
; i
++)
436 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
439 /* write a uint16_t array to a buffer in target memory endianness */
440 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
443 for (i
= 0; i
< count
; i
++)
444 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
447 /* write a uint64_t array to a buffer in target memory endianness */
448 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
451 for (i
= 0; i
< count
; i
++)
452 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
455 /* write a uint32_t array to a buffer in target memory endianness */
456 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
459 for (i
= 0; i
< count
; i
++)
460 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
463 /* write a uint16_t array to a buffer in target memory endianness */
464 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
467 for (i
= 0; i
< count
; i
++)
468 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
471 /* return a pointer to a configured target; id is name or number */
472 struct target
*get_target(const char *id
)
474 struct target
*target
;
476 /* try as tcltarget name */
477 for (target
= all_targets
; target
; target
= target
->next
) {
478 if (target_name(target
) == NULL
)
480 if (strcmp(id
, target_name(target
)) == 0)
484 /* It's OK to remove this fallback sometime after August 2010 or so */
486 /* no match, try as number */
488 if (parse_uint(id
, &num
) != ERROR_OK
)
491 for (target
= all_targets
; target
; target
= target
->next
) {
492 if (target
->target_number
== (int)num
) {
493 LOG_WARNING("use '%s' as target identifier, not '%u'",
494 target_name(target
), num
);
502 /* returns a pointer to the n-th configured target */
503 struct target
*get_target_by_num(int num
)
505 struct target
*target
= all_targets
;
508 if (target
->target_number
== num
)
510 target
= target
->next
;
516 struct target
*get_current_target(struct command_context
*cmd_ctx
)
518 struct target
*target
= cmd_ctx
->current_target_override
519 ? cmd_ctx
->current_target_override
520 : cmd_ctx
->current_target
;
522 if (target
== NULL
) {
523 LOG_ERROR("BUG: current_target out of bounds");
530 int target_poll(struct target
*target
)
534 /* We can't poll until after examine */
535 if (!target_was_examined(target
)) {
536 /* Fail silently lest we pollute the log */
540 retval
= target
->type
->poll(target
);
541 if (retval
!= ERROR_OK
)
544 if (target
->halt_issued
) {
545 if (target
->state
== TARGET_HALTED
)
546 target
->halt_issued
= false;
548 int64_t t
= timeval_ms() - target
->halt_issued_time
;
549 if (t
> DEFAULT_HALT_TIMEOUT
) {
550 target
->halt_issued
= false;
551 LOG_INFO("Halt timed out, wake up GDB.");
552 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
560 int target_halt(struct target
*target
)
563 /* We can't poll until after examine */
564 if (!target_was_examined(target
)) {
565 LOG_ERROR("Target not examined yet");
569 retval
= target
->type
->halt(target
);
570 if (retval
!= ERROR_OK
)
573 target
->halt_issued
= true;
574 target
->halt_issued_time
= timeval_ms();
580 * Make the target (re)start executing using its saved execution
581 * context (possibly with some modifications).
583 * @param target Which target should start executing.
584 * @param current True to use the target's saved program counter instead
585 * of the address parameter
586 * @param address Optionally used as the program counter.
587 * @param handle_breakpoints True iff breakpoints at the resumption PC
588 * should be skipped. (For example, maybe execution was stopped by
589 * such a breakpoint, in which case it would be counterprodutive to
591 * @param debug_execution False if all working areas allocated by OpenOCD
592 * should be released and/or restored to their original contents.
593 * (This would for example be true to run some downloaded "helper"
594 * algorithm code, which resides in one such working buffer and uses
595 * another for data storage.)
597 * @todo Resolve the ambiguity about what the "debug_execution" flag
598 * signifies. For example, Target implementations don't agree on how
599 * it relates to invalidation of the register cache, or to whether
600 * breakpoints and watchpoints should be enabled. (It would seem wrong
601 * to enable breakpoints when running downloaded "helper" algorithms
602 * (debug_execution true), since the breakpoints would be set to match
603 * target firmware being debugged, not the helper algorithm.... and
604 * enabling them could cause such helpers to malfunction (for example,
605 * by overwriting data with a breakpoint instruction. On the other
606 * hand the infrastructure for running such helpers might use this
607 * procedure but rely on hardware breakpoint to detect termination.)
609 int target_resume(struct target
*target
, int current
, target_addr_t address
,
610 int handle_breakpoints
, int debug_execution
)
614 /* We can't poll until after examine */
615 if (!target_was_examined(target
)) {
616 LOG_ERROR("Target not examined yet");
620 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
622 /* note that resume *must* be asynchronous. The CPU can halt before
623 * we poll. The CPU can even halt at the current PC as a result of
624 * a software breakpoint being inserted by (a bug?) the application.
626 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
627 if (retval
!= ERROR_OK
)
630 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
635 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
640 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
641 if (n
->name
== NULL
) {
642 LOG_ERROR("invalid reset mode");
646 struct target
*target
;
647 for (target
= all_targets
; target
; target
= target
->next
)
648 target_call_reset_callbacks(target
, reset_mode
);
650 /* disable polling during reset to make reset event scripts
651 * more predictable, i.e. dr/irscan & pathmove in events will
652 * not have JTAG operations injected into the middle of a sequence.
654 bool save_poll
= jtag_poll_get_enabled();
656 jtag_poll_set_enabled(false);
658 sprintf(buf
, "ocd_process_reset %s", n
->name
);
659 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
661 jtag_poll_set_enabled(save_poll
);
663 if (retval
!= JIM_OK
) {
664 Jim_MakeErrorMessage(cmd_ctx
->interp
);
665 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
669 /* We want any events to be processed before the prompt */
670 retval
= target_call_timer_callbacks_now();
672 for (target
= all_targets
; target
; target
= target
->next
) {
673 target
->type
->check_reset(target
);
674 target
->running_alg
= false;
680 static int identity_virt2phys(struct target
*target
,
681 target_addr_t
virtual, target_addr_t
*physical
)
687 static int no_mmu(struct target
*target
, int *enabled
)
693 static int default_examine(struct target
*target
)
695 target_set_examined(target
);
699 /* no check by default */
700 static int default_check_reset(struct target
*target
)
705 int target_examine_one(struct target
*target
)
707 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
709 int retval
= target
->type
->examine(target
);
710 if (retval
!= ERROR_OK
)
713 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
718 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
720 struct target
*target
= priv
;
722 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
725 jtag_unregister_event_callback(jtag_enable_callback
, target
);
727 return target_examine_one(target
);
730 /* Targets that correctly implement init + examine, i.e.
731 * no communication with target during init:
735 int target_examine(void)
737 int retval
= ERROR_OK
;
738 struct target
*target
;
740 for (target
= all_targets
; target
; target
= target
->next
) {
741 /* defer examination, but don't skip it */
742 if (!target
->tap
->enabled
) {
743 jtag_register_event_callback(jtag_enable_callback
,
748 if (target
->defer_examine
)
751 retval
= target_examine_one(target
);
752 if (retval
!= ERROR_OK
)
758 const char *target_type_name(struct target
*target
)
760 return target
->type
->name
;
763 static int target_soft_reset_halt(struct target
*target
)
765 if (!target_was_examined(target
)) {
766 LOG_ERROR("Target not examined yet");
769 if (!target
->type
->soft_reset_halt
) {
770 LOG_ERROR("Target %s does not support soft_reset_halt",
771 target_name(target
));
774 return target
->type
->soft_reset_halt(target
);
778 * Downloads a target-specific native code algorithm to the target,
779 * and executes it. * Note that some targets may need to set up, enable,
780 * and tear down a breakpoint (hard or * soft) to detect algorithm
781 * termination, while others may support lower overhead schemes where
782 * soft breakpoints embedded in the algorithm automatically terminate the
785 * @param target used to run the algorithm
786 * @param arch_info target-specific description of the algorithm.
788 int target_run_algorithm(struct target
*target
,
789 int num_mem_params
, struct mem_param
*mem_params
,
790 int num_reg_params
, struct reg_param
*reg_param
,
791 uint32_t entry_point
, uint32_t exit_point
,
792 int timeout_ms
, void *arch_info
)
794 int retval
= ERROR_FAIL
;
796 if (!target_was_examined(target
)) {
797 LOG_ERROR("Target not examined yet");
800 if (!target
->type
->run_algorithm
) {
801 LOG_ERROR("Target type '%s' does not support %s",
802 target_type_name(target
), __func__
);
806 target
->running_alg
= true;
807 retval
= target
->type
->run_algorithm(target
,
808 num_mem_params
, mem_params
,
809 num_reg_params
, reg_param
,
810 entry_point
, exit_point
, timeout_ms
, arch_info
);
811 target
->running_alg
= false;
818 * Executes a target-specific native code algorithm and leaves it running.
820 * @param target used to run the algorithm
821 * @param arch_info target-specific description of the algorithm.
823 int target_start_algorithm(struct target
*target
,
824 int num_mem_params
, struct mem_param
*mem_params
,
825 int num_reg_params
, struct reg_param
*reg_params
,
826 uint32_t entry_point
, uint32_t exit_point
,
829 int retval
= ERROR_FAIL
;
831 if (!target_was_examined(target
)) {
832 LOG_ERROR("Target not examined yet");
835 if (!target
->type
->start_algorithm
) {
836 LOG_ERROR("Target type '%s' does not support %s",
837 target_type_name(target
), __func__
);
840 if (target
->running_alg
) {
841 LOG_ERROR("Target is already running an algorithm");
845 target
->running_alg
= true;
846 retval
= target
->type
->start_algorithm(target
,
847 num_mem_params
, mem_params
,
848 num_reg_params
, reg_params
,
849 entry_point
, exit_point
, arch_info
);
856 * Waits for an algorithm started with target_start_algorithm() to complete.
858 * @param target used to run the algorithm
859 * @param arch_info target-specific description of the algorithm.
861 int target_wait_algorithm(struct target
*target
,
862 int num_mem_params
, struct mem_param
*mem_params
,
863 int num_reg_params
, struct reg_param
*reg_params
,
864 uint32_t exit_point
, int timeout_ms
,
867 int retval
= ERROR_FAIL
;
869 if (!target
->type
->wait_algorithm
) {
870 LOG_ERROR("Target type '%s' does not support %s",
871 target_type_name(target
), __func__
);
874 if (!target
->running_alg
) {
875 LOG_ERROR("Target is not running an algorithm");
879 retval
= target
->type
->wait_algorithm(target
,
880 num_mem_params
, mem_params
,
881 num_reg_params
, reg_params
,
882 exit_point
, timeout_ms
, arch_info
);
883 if (retval
!= ERROR_TARGET_TIMEOUT
)
884 target
->running_alg
= false;
891 * Streams data to a circular buffer on target intended for consumption by code
892 * running asynchronously on target.
894 * This is intended for applications where target-specific native code runs
895 * on the target, receives data from the circular buffer, does something with
896 * it (most likely writing it to a flash memory), and advances the circular
899 * This assumes that the helper algorithm has already been loaded to the target,
900 * but has not been started yet. Given memory and register parameters are passed
903 * The buffer is defined by (buffer_start, buffer_size) arguments and has the
906 * [buffer_start + 0, buffer_start + 4):
907 * Write Pointer address (aka head). Written and updated by this
908 * routine when new data is written to the circular buffer.
909 * [buffer_start + 4, buffer_start + 8):
910 * Read Pointer address (aka tail). Updated by code running on the
911 * target after it consumes data.
912 * [buffer_start + 8, buffer_start + buffer_size):
913 * Circular buffer contents.
915 * See contrib/loaders/flash/stm32f1x.S for an example.
917 * @param target used to run the algorithm
918 * @param buffer address on the host where data to be sent is located
919 * @param count number of blocks to send
920 * @param block_size size in bytes of each block
921 * @param num_mem_params count of memory-based params to pass to algorithm
922 * @param mem_params memory-based params to pass to algorithm
923 * @param num_reg_params count of register-based params to pass to algorithm
924 * @param reg_params memory-based params to pass to algorithm
925 * @param buffer_start address on the target of the circular buffer structure
926 * @param buffer_size size of the circular buffer structure
927 * @param entry_point address on the target to execute to start the algorithm
928 * @param exit_point address at which to set a breakpoint to catch the
929 * end of the algorithm; can be 0 if target triggers a breakpoint itself
932 int target_run_flash_async_algorithm(struct target
*target
,
933 const uint8_t *buffer
, uint32_t count
, int block_size
,
934 int num_mem_params
, struct mem_param
*mem_params
,
935 int num_reg_params
, struct reg_param
*reg_params
,
936 uint32_t buffer_start
, uint32_t buffer_size
,
937 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
942 const uint8_t *buffer_orig
= buffer
;
944 /* Set up working area. First word is write pointer, second word is read pointer,
945 * rest is fifo data area. */
946 uint32_t wp_addr
= buffer_start
;
947 uint32_t rp_addr
= buffer_start
+ 4;
948 uint32_t fifo_start_addr
= buffer_start
+ 8;
949 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
951 uint32_t wp
= fifo_start_addr
;
952 uint32_t rp
= fifo_start_addr
;
954 /* validate block_size is 2^n */
955 assert(!block_size
|| !(block_size
& (block_size
- 1)));
957 retval
= target_write_u32(target
, wp_addr
, wp
);
958 if (retval
!= ERROR_OK
)
960 retval
= target_write_u32(target
, rp_addr
, rp
);
961 if (retval
!= ERROR_OK
)
964 /* Start up algorithm on target and let it idle while writing the first chunk */
965 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
966 num_reg_params
, reg_params
,
971 if (retval
!= ERROR_OK
) {
972 LOG_ERROR("error starting target flash write algorithm");
978 retval
= target_read_u32(target
, rp_addr
, &rp
);
979 if (retval
!= ERROR_OK
) {
980 LOG_ERROR("failed to get read pointer");
984 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
985 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
988 LOG_ERROR("flash write algorithm aborted by target");
989 retval
= ERROR_FLASH_OPERATION_FAILED
;
993 if (((rp
- fifo_start_addr
) & (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
994 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
998 /* Count the number of bytes available in the fifo without
999 * crossing the wrap around. Make sure to not fill it completely,
1000 * because that would make wp == rp and that's the empty condition. */
1001 uint32_t thisrun_bytes
;
1003 thisrun_bytes
= rp
- wp
- block_size
;
1004 else if (rp
> fifo_start_addr
)
1005 thisrun_bytes
= fifo_end_addr
- wp
;
1007 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
1009 if (thisrun_bytes
== 0) {
1010 /* Throttle polling a bit if transfer is (much) faster than flash
1011 * programming. The exact delay shouldn't matter as long as it's
1012 * less than buffer size / flash speed. This is very unlikely to
1013 * run when using high latency connections such as USB. */
1016 /* to stop an infinite loop on some targets check and increment a timeout
1017 * this issue was observed on a stellaris using the new ICDI interface */
1018 if (timeout
++ >= 500) {
1019 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1020 return ERROR_FLASH_OPERATION_FAILED
;
1025 /* reset our timeout */
1028 /* Limit to the amount of data we actually want to write */
1029 if (thisrun_bytes
> count
* block_size
)
1030 thisrun_bytes
= count
* block_size
;
1032 /* Write data to fifo */
1033 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
1034 if (retval
!= ERROR_OK
)
1037 /* Update counters and wrap write pointer */
1038 buffer
+= thisrun_bytes
;
1039 count
-= thisrun_bytes
/ block_size
;
1040 wp
+= thisrun_bytes
;
1041 if (wp
>= fifo_end_addr
)
1042 wp
= fifo_start_addr
;
1044 /* Store updated write pointer to target */
1045 retval
= target_write_u32(target
, wp_addr
, wp
);
1046 if (retval
!= ERROR_OK
)
1050 if (retval
!= ERROR_OK
) {
1051 /* abort flash write algorithm on target */
1052 target_write_u32(target
, wp_addr
, 0);
1055 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1056 num_reg_params
, reg_params
,
1061 if (retval2
!= ERROR_OK
) {
1062 LOG_ERROR("error waiting for target flash write algorithm");
1066 if (retval
== ERROR_OK
) {
1067 /* check if algorithm set rp = 0 after fifo writer loop finished */
1068 retval
= target_read_u32(target
, rp_addr
, &rp
);
1069 if (retval
== ERROR_OK
&& rp
== 0) {
1070 LOG_ERROR("flash write algorithm aborted by target");
1071 retval
= ERROR_FLASH_OPERATION_FAILED
;
1078 int target_read_memory(struct target
*target
,
1079 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1081 if (!target_was_examined(target
)) {
1082 LOG_ERROR("Target not examined yet");
1085 if (!target
->type
->read_memory
) {
1086 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1089 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1092 int target_read_phys_memory(struct target
*target
,
1093 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1095 if (!target_was_examined(target
)) {
1096 LOG_ERROR("Target not examined yet");
1099 if (!target
->type
->read_phys_memory
) {
1100 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1103 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1106 int target_write_memory(struct target
*target
,
1107 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1109 if (!target_was_examined(target
)) {
1110 LOG_ERROR("Target not examined yet");
1113 if (!target
->type
->write_memory
) {
1114 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1117 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1120 int target_write_phys_memory(struct target
*target
,
1121 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1123 if (!target_was_examined(target
)) {
1124 LOG_ERROR("Target not examined yet");
1127 if (!target
->type
->write_phys_memory
) {
1128 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1131 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1134 int target_add_breakpoint(struct target
*target
,
1135 struct breakpoint
*breakpoint
)
1137 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1138 LOG_WARNING("target %s is not halted (add breakpoint)", target_name(target
));
1139 return ERROR_TARGET_NOT_HALTED
;
1141 return target
->type
->add_breakpoint(target
, breakpoint
);
1144 int target_add_context_breakpoint(struct target
*target
,
1145 struct breakpoint
*breakpoint
)
1147 if (target
->state
!= TARGET_HALTED
) {
1148 LOG_WARNING("target %s is not halted (add context breakpoint)", target_name(target
));
1149 return ERROR_TARGET_NOT_HALTED
;
1151 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1154 int target_add_hybrid_breakpoint(struct target
*target
,
1155 struct breakpoint
*breakpoint
)
1157 if (target
->state
!= TARGET_HALTED
) {
1158 LOG_WARNING("target %s is not halted (add hybrid breakpoint)", target_name(target
));
1159 return ERROR_TARGET_NOT_HALTED
;
1161 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1164 int target_remove_breakpoint(struct target
*target
,
1165 struct breakpoint
*breakpoint
)
1167 return target
->type
->remove_breakpoint(target
, breakpoint
);
1170 int target_add_watchpoint(struct target
*target
,
1171 struct watchpoint
*watchpoint
)
1173 if (target
->state
!= TARGET_HALTED
) {
1174 LOG_WARNING("target %s is not halted (add watchpoint)", target_name(target
));
1175 return ERROR_TARGET_NOT_HALTED
;
1177 return target
->type
->add_watchpoint(target
, watchpoint
);
1179 int target_remove_watchpoint(struct target
*target
,
1180 struct watchpoint
*watchpoint
)
1182 return target
->type
->remove_watchpoint(target
, watchpoint
);
1184 int target_hit_watchpoint(struct target
*target
,
1185 struct watchpoint
**hit_watchpoint
)
1187 if (target
->state
!= TARGET_HALTED
) {
1188 LOG_WARNING("target %s is not halted (hit watchpoint)", target
->cmd_name
);
1189 return ERROR_TARGET_NOT_HALTED
;
1192 if (target
->type
->hit_watchpoint
== NULL
) {
1193 /* For backward compatible, if hit_watchpoint is not implemented,
1194 * return ERROR_FAIL such that gdb_server will not take the nonsense
1199 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1202 int target_get_gdb_reg_list(struct target
*target
,
1203 struct reg
**reg_list
[], int *reg_list_size
,
1204 enum target_register_class reg_class
)
1206 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1209 bool target_supports_gdb_connection(struct target
*target
)
1212 * based on current code, we can simply exclude all the targets that
1213 * don't provide get_gdb_reg_list; this could change with new targets.
1215 return !!target
->type
->get_gdb_reg_list
;
1218 int target_step(struct target
*target
,
1219 int current
, target_addr_t address
, int handle_breakpoints
)
1221 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1224 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1226 if (target
->state
!= TARGET_HALTED
) {
1227 LOG_WARNING("target %s is not halted (gdb fileio)", target
->cmd_name
);
1228 return ERROR_TARGET_NOT_HALTED
;
1230 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1233 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1235 if (target
->state
!= TARGET_HALTED
) {
1236 LOG_WARNING("target %s is not halted (gdb fileio end)", target
->cmd_name
);
1237 return ERROR_TARGET_NOT_HALTED
;
1239 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1242 int target_profiling(struct target
*target
, uint32_t *samples
,
1243 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1245 if (target
->state
!= TARGET_HALTED
) {
1246 LOG_WARNING("target %s is not halted (profiling)", target
->cmd_name
);
1247 return ERROR_TARGET_NOT_HALTED
;
1249 return target
->type
->profiling(target
, samples
, max_num_samples
,
1250 num_samples
, seconds
);
1254 * Reset the @c examined flag for the given target.
1255 * Pure paranoia -- targets are zeroed on allocation.
1257 static void target_reset_examined(struct target
*target
)
1259 target
->examined
= false;
1262 static int handle_target(void *priv
);
1264 static int target_init_one(struct command_context
*cmd_ctx
,
1265 struct target
*target
)
1267 target_reset_examined(target
);
1269 struct target_type
*type
= target
->type
;
1270 if (type
->examine
== NULL
)
1271 type
->examine
= default_examine
;
1273 if (type
->check_reset
== NULL
)
1274 type
->check_reset
= default_check_reset
;
1276 assert(type
->init_target
!= NULL
);
1278 int retval
= type
->init_target(cmd_ctx
, target
);
1279 if (ERROR_OK
!= retval
) {
1280 LOG_ERROR("target '%s' init failed", target_name(target
));
1284 /* Sanity-check MMU support ... stub in what we must, to help
1285 * implement it in stages, but warn if we need to do so.
1288 if (type
->virt2phys
== NULL
) {
1289 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1290 type
->virt2phys
= identity_virt2phys
;
1293 /* Make sure no-MMU targets all behave the same: make no
1294 * distinction between physical and virtual addresses, and
1295 * ensure that virt2phys() is always an identity mapping.
1297 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1298 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1301 type
->write_phys_memory
= type
->write_memory
;
1302 type
->read_phys_memory
= type
->read_memory
;
1303 type
->virt2phys
= identity_virt2phys
;
1306 if (target
->type
->read_buffer
== NULL
)
1307 target
->type
->read_buffer
= target_read_buffer_default
;
1309 if (target
->type
->write_buffer
== NULL
)
1310 target
->type
->write_buffer
= target_write_buffer_default
;
1312 if (target
->type
->get_gdb_fileio_info
== NULL
)
1313 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1315 if (target
->type
->gdb_fileio_end
== NULL
)
1316 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1318 if (target
->type
->profiling
== NULL
)
1319 target
->type
->profiling
= target_profiling_default
;
1324 static int target_init(struct command_context
*cmd_ctx
)
1326 struct target
*target
;
1329 for (target
= all_targets
; target
; target
= target
->next
) {
1330 retval
= target_init_one(cmd_ctx
, target
);
1331 if (ERROR_OK
!= retval
)
1338 retval
= target_register_user_commands(cmd_ctx
);
1339 if (ERROR_OK
!= retval
)
1342 retval
= target_register_timer_callback(&handle_target
,
1343 polling_interval
, 1, cmd_ctx
->interp
);
1344 if (ERROR_OK
!= retval
)
1350 COMMAND_HANDLER(handle_target_init_command
)
1355 return ERROR_COMMAND_SYNTAX_ERROR
;
1357 static bool target_initialized
;
1358 if (target_initialized
) {
1359 LOG_INFO("'target init' has already been called");
1362 target_initialized
= true;
1364 retval
= command_run_line(CMD_CTX
, "init_targets");
1365 if (ERROR_OK
!= retval
)
1368 retval
= command_run_line(CMD_CTX
, "init_target_events");
1369 if (ERROR_OK
!= retval
)
1372 retval
= command_run_line(CMD_CTX
, "init_board");
1373 if (ERROR_OK
!= retval
)
1376 LOG_DEBUG("Initializing targets...");
1377 return target_init(CMD_CTX
);
1380 int target_register_event_callback(int (*callback
)(struct target
*target
,
1381 enum target_event event
, void *priv
), void *priv
)
1383 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1385 if (callback
== NULL
)
1386 return ERROR_COMMAND_SYNTAX_ERROR
;
1389 while ((*callbacks_p
)->next
)
1390 callbacks_p
= &((*callbacks_p
)->next
);
1391 callbacks_p
= &((*callbacks_p
)->next
);
1394 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1395 (*callbacks_p
)->callback
= callback
;
1396 (*callbacks_p
)->priv
= priv
;
1397 (*callbacks_p
)->next
= NULL
;
1402 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1403 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1405 struct target_reset_callback
*entry
;
1407 if (callback
== NULL
)
1408 return ERROR_COMMAND_SYNTAX_ERROR
;
1410 entry
= malloc(sizeof(struct target_reset_callback
));
1411 if (entry
== NULL
) {
1412 LOG_ERROR("error allocating buffer for reset callback entry");
1413 return ERROR_COMMAND_SYNTAX_ERROR
;
1416 entry
->callback
= callback
;
1418 list_add(&entry
->list
, &target_reset_callback_list
);
1424 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1425 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1427 struct target_trace_callback
*entry
;
1429 if (callback
== NULL
)
1430 return ERROR_COMMAND_SYNTAX_ERROR
;
1432 entry
= malloc(sizeof(struct target_trace_callback
));
1433 if (entry
== NULL
) {
1434 LOG_ERROR("error allocating buffer for trace callback entry");
1435 return ERROR_COMMAND_SYNTAX_ERROR
;
1438 entry
->callback
= callback
;
1440 list_add(&entry
->list
, &target_trace_callback_list
);
1446 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1448 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1450 if (callback
== NULL
)
1451 return ERROR_COMMAND_SYNTAX_ERROR
;
1454 while ((*callbacks_p
)->next
)
1455 callbacks_p
= &((*callbacks_p
)->next
);
1456 callbacks_p
= &((*callbacks_p
)->next
);
1459 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1460 (*callbacks_p
)->callback
= callback
;
1461 (*callbacks_p
)->periodic
= periodic
;
1462 (*callbacks_p
)->time_ms
= time_ms
;
1463 (*callbacks_p
)->removed
= false;
1465 gettimeofday(&(*callbacks_p
)->when
, NULL
);
1466 timeval_add_time(&(*callbacks_p
)->when
, 0, time_ms
* 1000);
1468 (*callbacks_p
)->priv
= priv
;
1469 (*callbacks_p
)->next
= NULL
;
1474 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1475 enum target_event event
, void *priv
), void *priv
)
1477 struct target_event_callback
**p
= &target_event_callbacks
;
1478 struct target_event_callback
*c
= target_event_callbacks
;
1480 if (callback
== NULL
)
1481 return ERROR_COMMAND_SYNTAX_ERROR
;
1484 struct target_event_callback
*next
= c
->next
;
1485 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1497 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1498 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1500 struct target_reset_callback
*entry
;
1502 if (callback
== NULL
)
1503 return ERROR_COMMAND_SYNTAX_ERROR
;
1505 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1506 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1507 list_del(&entry
->list
);
1516 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1517 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1519 struct target_trace_callback
*entry
;
1521 if (callback
== NULL
)
1522 return ERROR_COMMAND_SYNTAX_ERROR
;
1524 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1525 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1526 list_del(&entry
->list
);
1535 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1537 if (callback
== NULL
)
1538 return ERROR_COMMAND_SYNTAX_ERROR
;
1540 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1542 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1551 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1553 struct target_event_callback
*callback
= target_event_callbacks
;
1554 struct target_event_callback
*next_callback
;
1556 if (event
== TARGET_EVENT_HALTED
) {
1557 /* execute early halted first */
1558 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1561 LOG_DEBUG("target event %i (%s)", event
,
1562 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1564 target_handle_event(target
, event
);
1567 next_callback
= callback
->next
;
1568 callback
->callback(target
, event
, callback
->priv
);
1569 callback
= next_callback
;
1575 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1577 struct target_reset_callback
*callback
;
1579 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1580 Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1582 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1583 callback
->callback(target
, reset_mode
, callback
->priv
);
1588 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1590 struct target_trace_callback
*callback
;
1592 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1593 callback
->callback(target
, len
, data
, callback
->priv
);
1598 static int target_timer_callback_periodic_restart(
1599 struct target_timer_callback
*cb
, struct timeval
*now
)
1602 timeval_add_time(&cb
->when
, 0, cb
->time_ms
* 1000L);
1606 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1607 struct timeval
*now
)
1609 cb
->callback(cb
->priv
);
1612 return target_timer_callback_periodic_restart(cb
, now
);
1614 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1617 static int target_call_timer_callbacks_check_time(int checktime
)
1619 static bool callback_processing
;
1621 /* Do not allow nesting */
1622 if (callback_processing
)
1625 callback_processing
= true;
1630 gettimeofday(&now
, NULL
);
1632 /* Store an address of the place containing a pointer to the
1633 * next item; initially, that's a standalone "root of the
1634 * list" variable. */
1635 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1637 if ((*callback
)->removed
) {
1638 struct target_timer_callback
*p
= *callback
;
1639 *callback
= (*callback
)->next
;
1644 bool call_it
= (*callback
)->callback
&&
1645 ((!checktime
&& (*callback
)->periodic
) ||
1646 timeval_compare(&now
, &(*callback
)->when
) >= 0);
1649 target_call_timer_callback(*callback
, &now
);
1651 callback
= &(*callback
)->next
;
1654 callback_processing
= false;
1658 int target_call_timer_callbacks(void)
1660 return target_call_timer_callbacks_check_time(1);
1663 /* invoke periodic callbacks immediately */
1664 int target_call_timer_callbacks_now(void)
1666 return target_call_timer_callbacks_check_time(0);
1669 /* Prints the working area layout for debug purposes */
1670 static void print_wa_layout(struct target
*target
)
1672 struct working_area
*c
= target
->working_areas
;
1675 LOG_DEBUG("%c%c " TARGET_ADDR_FMT
"-" TARGET_ADDR_FMT
" (%" PRIu32
" bytes)",
1676 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1677 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1682 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1683 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1685 assert(area
->free
); /* Shouldn't split an allocated area */
1686 assert(size
<= area
->size
); /* Caller should guarantee this */
1688 /* Split only if not already the right size */
1689 if (size
< area
->size
) {
1690 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1695 new_wa
->next
= area
->next
;
1696 new_wa
->size
= area
->size
- size
;
1697 new_wa
->address
= area
->address
+ size
;
1698 new_wa
->backup
= NULL
;
1699 new_wa
->user
= NULL
;
1700 new_wa
->free
= true;
1702 area
->next
= new_wa
;
1705 /* If backup memory was allocated to this area, it has the wrong size
1706 * now so free it and it will be reallocated if/when needed */
1709 area
->backup
= NULL
;
1714 /* Merge all adjacent free areas into one */
1715 static void target_merge_working_areas(struct target
*target
)
1717 struct working_area
*c
= target
->working_areas
;
1719 while (c
&& c
->next
) {
1720 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1722 /* Find two adjacent free areas */
1723 if (c
->free
&& c
->next
->free
) {
1724 /* Merge the last into the first */
1725 c
->size
+= c
->next
->size
;
1727 /* Remove the last */
1728 struct working_area
*to_be_freed
= c
->next
;
1729 c
->next
= c
->next
->next
;
1730 if (to_be_freed
->backup
)
1731 free(to_be_freed
->backup
);
1734 /* If backup memory was allocated to the remaining area, it's has
1735 * the wrong size now */
1746 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1748 /* Reevaluate working area address based on MMU state*/
1749 if (target
->working_areas
== NULL
) {
1753 retval
= target
->type
->mmu(target
, &enabled
);
1754 if (retval
!= ERROR_OK
)
1758 if (target
->working_area_phys_spec
) {
1759 LOG_DEBUG("MMU disabled, using physical "
1760 "address for working memory " TARGET_ADDR_FMT
,
1761 target
->working_area_phys
);
1762 target
->working_area
= target
->working_area_phys
;
1764 LOG_ERROR("No working memory available. "
1765 "Specify -work-area-phys to target.");
1766 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1769 if (target
->working_area_virt_spec
) {
1770 LOG_DEBUG("MMU enabled, using virtual "
1771 "address for working memory " TARGET_ADDR_FMT
,
1772 target
->working_area_virt
);
1773 target
->working_area
= target
->working_area_virt
;
1775 LOG_ERROR("No working memory available. "
1776 "Specify -work-area-virt to target.");
1777 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1781 /* Set up initial working area on first call */
1782 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1784 new_wa
->next
= NULL
;
1785 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1786 new_wa
->address
= target
->working_area
;
1787 new_wa
->backup
= NULL
;
1788 new_wa
->user
= NULL
;
1789 new_wa
->free
= true;
1792 target
->working_areas
= new_wa
;
1795 /* only allocate multiples of 4 byte */
1797 size
= (size
+ 3) & (~3UL);
1799 struct working_area
*c
= target
->working_areas
;
1801 /* Find the first large enough working area */
1803 if (c
->free
&& c
->size
>= size
)
1809 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1811 /* Split the working area into the requested size */
1812 target_split_working_area(c
, size
);
1814 LOG_DEBUG("allocated new working area of %" PRIu32
" bytes at address " TARGET_ADDR_FMT
,
1817 if (target
->backup_working_area
) {
1818 if (c
->backup
== NULL
) {
1819 c
->backup
= malloc(c
->size
);
1820 if (c
->backup
== NULL
)
1824 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1825 if (retval
!= ERROR_OK
)
1829 /* mark as used, and return the new (reused) area */
1836 print_wa_layout(target
);
1841 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1845 retval
= target_alloc_working_area_try(target
, size
, area
);
1846 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1847 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1852 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1854 int retval
= ERROR_OK
;
1856 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1857 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1858 if (retval
!= ERROR_OK
)
1859 LOG_ERROR("failed to restore %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1860 area
->size
, area
->address
);
1866 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1867 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1869 int retval
= ERROR_OK
;
1875 retval
= target_restore_working_area(target
, area
);
1876 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1877 if (retval
!= ERROR_OK
)
1883 LOG_DEBUG("freed %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1884 area
->size
, area
->address
);
1886 /* mark user pointer invalid */
1887 /* TODO: Is this really safe? It points to some previous caller's memory.
1888 * How could we know that the area pointer is still in that place and not
1889 * some other vital data? What's the purpose of this, anyway? */
1893 target_merge_working_areas(target
);
1895 print_wa_layout(target
);
1900 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1902 return target_free_working_area_restore(target
, area
, 1);
1905 static void target_destroy(struct target
*target
)
1907 if (target
->type
->deinit_target
)
1908 target
->type
->deinit_target(target
);
1910 if (target
->semihosting
)
1911 free(target
->semihosting
);
1913 jtag_unregister_event_callback(jtag_enable_callback
, target
);
1915 struct target_event_action
*teap
= target
->event_action
;
1917 struct target_event_action
*next
= teap
->next
;
1918 Jim_DecrRefCount(teap
->interp
, teap
->body
);
1923 target_free_all_working_areas(target
);
1924 /* Now we have none or only one working area marked as free */
1925 if (target
->working_areas
) {
1926 free(target
->working_areas
->backup
);
1927 free(target
->working_areas
);
1930 /* release the targets SMP list */
1932 struct target_list
*head
= target
->head
;
1933 while (head
!= NULL
) {
1934 struct target_list
*pos
= head
->next
;
1935 head
->target
->smp
= 0;
1942 free(target
->gdb_port_override
);
1944 free(target
->trace_info
);
1945 free(target
->fileio_info
);
1946 free(target
->cmd_name
);
1950 void target_quit(void)
1952 struct target_event_callback
*pe
= target_event_callbacks
;
1954 struct target_event_callback
*t
= pe
->next
;
1958 target_event_callbacks
= NULL
;
1960 struct target_timer_callback
*pt
= target_timer_callbacks
;
1962 struct target_timer_callback
*t
= pt
->next
;
1966 target_timer_callbacks
= NULL
;
1968 for (struct target
*target
= all_targets
; target
;) {
1972 target_destroy(target
);
1979 /* free resources and restore memory, if restoring memory fails,
1980 * free up resources anyway
1982 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1984 struct working_area
*c
= target
->working_areas
;
1986 LOG_DEBUG("freeing all working areas");
1988 /* Loop through all areas, restoring the allocated ones and marking them as free */
1992 target_restore_working_area(target
, c
);
1994 *c
->user
= NULL
; /* Same as above */
2000 /* Run a merge pass to combine all areas into one */
2001 target_merge_working_areas(target
);
2003 print_wa_layout(target
);
2006 void target_free_all_working_areas(struct target
*target
)
2008 target_free_all_working_areas_restore(target
, 1);
2011 /* Find the largest number of bytes that can be allocated */
2012 uint32_t target_get_working_area_avail(struct target
*target
)
2014 struct working_area
*c
= target
->working_areas
;
2015 uint32_t max_size
= 0;
2018 return target
->working_area_size
;
2021 if (c
->free
&& max_size
< c
->size
)
2030 int target_arch_state(struct target
*target
)
2033 if (target
== NULL
) {
2034 LOG_WARNING("No target has been configured");
2038 if (target
->state
!= TARGET_HALTED
)
2041 retval
= target
->type
->arch_state(target
);
2045 static int target_get_gdb_fileio_info_default(struct target
*target
,
2046 struct gdb_fileio_info
*fileio_info
)
2048 /* If target does not support semi-hosting function, target
2049 has no need to provide .get_gdb_fileio_info callback.
2050 It just return ERROR_FAIL and gdb_server will return "Txx"
2051 as target halted every time. */
2055 static int target_gdb_fileio_end_default(struct target
*target
,
2056 int retcode
, int fileio_errno
, bool ctrl_c
)
2061 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
2062 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
2064 struct timeval timeout
, now
;
2066 gettimeofday(&timeout
, NULL
);
2067 timeval_add_time(&timeout
, seconds
, 0);
2069 LOG_INFO("Starting profiling. Halting and resuming the"
2070 " target as often as we can...");
2072 uint32_t sample_count
= 0;
2073 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2074 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2076 int retval
= ERROR_OK
;
2078 target_poll(target
);
2079 if (target
->state
== TARGET_HALTED
) {
2080 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
2081 samples
[sample_count
++] = t
;
2082 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2083 retval
= target_resume(target
, 1, 0, 0, 0);
2084 target_poll(target
);
2085 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2086 } else if (target
->state
== TARGET_RUNNING
) {
2087 /* We want to quickly sample the PC. */
2088 retval
= target_halt(target
);
2090 LOG_INFO("Target not halted or running");
2095 if (retval
!= ERROR_OK
)
2098 gettimeofday(&now
, NULL
);
2099 if ((sample_count
>= max_num_samples
) || timeval_compare(&now
, &timeout
) >= 0) {
2100 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2105 *num_samples
= sample_count
;
2109 /* Single aligned words are guaranteed to use 16 or 32 bit access
2110 * mode respectively, otherwise data is handled as quickly as
2113 int target_write_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, const uint8_t *buffer
)
2115 LOG_DEBUG("writing buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2118 if (!target_was_examined(target
)) {
2119 LOG_ERROR("Target not examined yet");
2126 if ((address
+ size
- 1) < address
) {
2127 /* GDB can request this when e.g. PC is 0xfffffffc */
2128 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2134 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2137 static int target_write_buffer_default(struct target
*target
,
2138 target_addr_t address
, uint32_t count
, const uint8_t *buffer
)
2142 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2143 * will have something to do with the size we leave to it. */
2144 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2145 if (address
& size
) {
2146 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2147 if (retval
!= ERROR_OK
)
2155 /* Write the data with as large access size as possible. */
2156 for (; size
> 0; size
/= 2) {
2157 uint32_t aligned
= count
- count
% size
;
2159 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2160 if (retval
!= ERROR_OK
)
2171 /* Single aligned words are guaranteed to use 16 or 32 bit access
2172 * mode respectively, otherwise data is handled as quickly as
2175 int target_read_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
2177 LOG_DEBUG("reading buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2180 if (!target_was_examined(target
)) {
2181 LOG_ERROR("Target not examined yet");
2188 if ((address
+ size
- 1) < address
) {
2189 /* GDB can request this when e.g. PC is 0xfffffffc */
2190 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2196 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2199 static int target_read_buffer_default(struct target
*target
, target_addr_t address
, uint32_t count
, uint8_t *buffer
)
2203 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2204 * will have something to do with the size we leave to it. */
2205 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2206 if (address
& size
) {
2207 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2208 if (retval
!= ERROR_OK
)
2216 /* Read the data with as large access size as possible. */
2217 for (; size
> 0; size
/= 2) {
2218 uint32_t aligned
= count
- count
% size
;
2220 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2221 if (retval
!= ERROR_OK
)
2232 int target_checksum_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* crc
)
2237 uint32_t checksum
= 0;
2238 if (!target_was_examined(target
)) {
2239 LOG_ERROR("Target not examined yet");
2243 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2244 if (retval
!= ERROR_OK
) {
2245 buffer
= malloc(size
);
2246 if (buffer
== NULL
) {
2247 LOG_ERROR("error allocating buffer for section (%" PRId32
" bytes)", size
);
2248 return ERROR_COMMAND_SYNTAX_ERROR
;
2250 retval
= target_read_buffer(target
, address
, size
, buffer
);
2251 if (retval
!= ERROR_OK
) {
2256 /* convert to target endianness */
2257 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2258 uint32_t target_data
;
2259 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2260 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2263 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2272 int target_blank_check_memory(struct target
*target
,
2273 struct target_memory_check_block
*blocks
, int num_blocks
,
2274 uint8_t erased_value
)
2276 if (!target_was_examined(target
)) {
2277 LOG_ERROR("Target not examined yet");
2281 if (target
->type
->blank_check_memory
== NULL
)
2282 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2284 return target
->type
->blank_check_memory(target
, blocks
, num_blocks
, erased_value
);
2287 int target_read_u64(struct target
*target
, target_addr_t address
, uint64_t *value
)
2289 uint8_t value_buf
[8];
2290 if (!target_was_examined(target
)) {
2291 LOG_ERROR("Target not examined yet");
2295 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2297 if (retval
== ERROR_OK
) {
2298 *value
= target_buffer_get_u64(target
, value_buf
);
2299 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2304 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2311 int target_read_u32(struct target
*target
, target_addr_t address
, uint32_t *value
)
2313 uint8_t value_buf
[4];
2314 if (!target_was_examined(target
)) {
2315 LOG_ERROR("Target not examined yet");
2319 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2321 if (retval
== ERROR_OK
) {
2322 *value
= target_buffer_get_u32(target
, value_buf
);
2323 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2328 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2335 int target_read_u16(struct target
*target
, target_addr_t address
, uint16_t *value
)
2337 uint8_t value_buf
[2];
2338 if (!target_was_examined(target
)) {
2339 LOG_ERROR("Target not examined yet");
2343 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2345 if (retval
== ERROR_OK
) {
2346 *value
= target_buffer_get_u16(target
, value_buf
);
2347 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%4.4" PRIx16
,
2352 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2359 int target_read_u8(struct target
*target
, target_addr_t address
, uint8_t *value
)
2361 if (!target_was_examined(target
)) {
2362 LOG_ERROR("Target not examined yet");
2366 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2368 if (retval
== ERROR_OK
) {
2369 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2374 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2381 int target_write_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2384 uint8_t value_buf
[8];
2385 if (!target_was_examined(target
)) {
2386 LOG_ERROR("Target not examined yet");
2390 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2394 target_buffer_set_u64(target
, value_buf
, value
);
2395 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2396 if (retval
!= ERROR_OK
)
2397 LOG_DEBUG("failed: %i", retval
);
2402 int target_write_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2405 uint8_t value_buf
[4];
2406 if (!target_was_examined(target
)) {
2407 LOG_ERROR("Target not examined yet");
2411 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2415 target_buffer_set_u32(target
, value_buf
, value
);
2416 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2417 if (retval
!= ERROR_OK
)
2418 LOG_DEBUG("failed: %i", retval
);
2423 int target_write_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2426 uint8_t value_buf
[2];
2427 if (!target_was_examined(target
)) {
2428 LOG_ERROR("Target not examined yet");
2432 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2436 target_buffer_set_u16(target
, value_buf
, value
);
2437 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2438 if (retval
!= ERROR_OK
)
2439 LOG_DEBUG("failed: %i", retval
);
2444 int target_write_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2447 if (!target_was_examined(target
)) {
2448 LOG_ERROR("Target not examined yet");
2452 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2455 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2456 if (retval
!= ERROR_OK
)
2457 LOG_DEBUG("failed: %i", retval
);
2462 int target_write_phys_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2465 uint8_t value_buf
[8];
2466 if (!target_was_examined(target
)) {
2467 LOG_ERROR("Target not examined yet");
2471 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2475 target_buffer_set_u64(target
, value_buf
, value
);
2476 retval
= target_write_phys_memory(target
, address
, 8, 1, value_buf
);
2477 if (retval
!= ERROR_OK
)
2478 LOG_DEBUG("failed: %i", retval
);
2483 int target_write_phys_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2486 uint8_t value_buf
[4];
2487 if (!target_was_examined(target
)) {
2488 LOG_ERROR("Target not examined yet");
2492 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2496 target_buffer_set_u32(target
, value_buf
, value
);
2497 retval
= target_write_phys_memory(target
, address
, 4, 1, value_buf
);
2498 if (retval
!= ERROR_OK
)
2499 LOG_DEBUG("failed: %i", retval
);
2504 int target_write_phys_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2507 uint8_t value_buf
[2];
2508 if (!target_was_examined(target
)) {
2509 LOG_ERROR("Target not examined yet");
2513 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2517 target_buffer_set_u16(target
, value_buf
, value
);
2518 retval
= target_write_phys_memory(target
, address
, 2, 1, value_buf
);
2519 if (retval
!= ERROR_OK
)
2520 LOG_DEBUG("failed: %i", retval
);
2525 int target_write_phys_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2528 if (!target_was_examined(target
)) {
2529 LOG_ERROR("Target not examined yet");
2533 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2536 retval
= target_write_phys_memory(target
, address
, 1, 1, &value
);
2537 if (retval
!= ERROR_OK
)
2538 LOG_DEBUG("failed: %i", retval
);
2543 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2545 struct target
*target
= get_target(name
);
2546 if (target
== NULL
) {
2547 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2550 if (!target
->tap
->enabled
) {
2551 LOG_USER("Target: TAP %s is disabled, "
2552 "can't be the current target\n",
2553 target
->tap
->dotted_name
);
2557 cmd_ctx
->current_target
= target
;
2558 if (cmd_ctx
->current_target_override
)
2559 cmd_ctx
->current_target_override
= target
;
2565 COMMAND_HANDLER(handle_targets_command
)
2567 int retval
= ERROR_OK
;
2568 if (CMD_ARGC
== 1) {
2569 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2570 if (retval
== ERROR_OK
) {
2576 struct target
*target
= all_targets
;
2577 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2578 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2583 if (target
->tap
->enabled
)
2584 state
= target_state_name(target
);
2586 state
= "tap-disabled";
2588 if (CMD_CTX
->current_target
== target
)
2591 /* keep columns lined up to match the headers above */
2592 command_print(CMD_CTX
,
2593 "%2d%c %-18s %-10s %-6s %-18s %s",
2594 target
->target_number
,
2596 target_name(target
),
2597 target_type_name(target
),
2598 Jim_Nvp_value2name_simple(nvp_target_endian
,
2599 target
->endianness
)->name
,
2600 target
->tap
->dotted_name
,
2602 target
= target
->next
;
2608 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2610 static int powerDropout
;
2611 static int srstAsserted
;
2613 static int runPowerRestore
;
2614 static int runPowerDropout
;
2615 static int runSrstAsserted
;
2616 static int runSrstDeasserted
;
2618 static int sense_handler(void)
2620 static int prevSrstAsserted
;
2621 static int prevPowerdropout
;
2623 int retval
= jtag_power_dropout(&powerDropout
);
2624 if (retval
!= ERROR_OK
)
2628 powerRestored
= prevPowerdropout
&& !powerDropout
;
2630 runPowerRestore
= 1;
2632 int64_t current
= timeval_ms();
2633 static int64_t lastPower
;
2634 bool waitMore
= lastPower
+ 2000 > current
;
2635 if (powerDropout
&& !waitMore
) {
2636 runPowerDropout
= 1;
2637 lastPower
= current
;
2640 retval
= jtag_srst_asserted(&srstAsserted
);
2641 if (retval
!= ERROR_OK
)
2645 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2647 static int64_t lastSrst
;
2648 waitMore
= lastSrst
+ 2000 > current
;
2649 if (srstDeasserted
&& !waitMore
) {
2650 runSrstDeasserted
= 1;
2654 if (!prevSrstAsserted
&& srstAsserted
)
2655 runSrstAsserted
= 1;
2657 prevSrstAsserted
= srstAsserted
;
2658 prevPowerdropout
= powerDropout
;
2660 if (srstDeasserted
|| powerRestored
) {
2661 /* Other than logging the event we can't do anything here.
2662 * Issuing a reset is a particularly bad idea as we might
2663 * be inside a reset already.
2670 /* process target state changes */
2671 static int handle_target(void *priv
)
2673 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2674 int retval
= ERROR_OK
;
2676 if (!is_jtag_poll_safe()) {
2677 /* polling is disabled currently */
2681 /* we do not want to recurse here... */
2682 static int recursive
;
2686 /* danger! running these procedures can trigger srst assertions and power dropouts.
2687 * We need to avoid an infinite loop/recursion here and we do that by
2688 * clearing the flags after running these events.
2690 int did_something
= 0;
2691 if (runSrstAsserted
) {
2692 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2693 Jim_Eval(interp
, "srst_asserted");
2696 if (runSrstDeasserted
) {
2697 Jim_Eval(interp
, "srst_deasserted");
2700 if (runPowerDropout
) {
2701 LOG_INFO("Power dropout detected, running power_dropout proc.");
2702 Jim_Eval(interp
, "power_dropout");
2705 if (runPowerRestore
) {
2706 Jim_Eval(interp
, "power_restore");
2710 if (did_something
) {
2711 /* clear detect flags */
2715 /* clear action flags */
2717 runSrstAsserted
= 0;
2718 runSrstDeasserted
= 0;
2719 runPowerRestore
= 0;
2720 runPowerDropout
= 0;
2725 /* Poll targets for state changes unless that's globally disabled.
2726 * Skip targets that are currently disabled.
2728 for (struct target
*target
= all_targets
;
2729 is_jtag_poll_safe() && target
;
2730 target
= target
->next
) {
2732 if (!target_was_examined(target
))
2735 if (!target
->tap
->enabled
)
2738 if (target
->backoff
.times
> target
->backoff
.count
) {
2739 /* do not poll this time as we failed previously */
2740 target
->backoff
.count
++;
2743 target
->backoff
.count
= 0;
2745 /* only poll target if we've got power and srst isn't asserted */
2746 if (!powerDropout
&& !srstAsserted
) {
2747 /* polling may fail silently until the target has been examined */
2748 retval
= target_poll(target
);
2749 if (retval
!= ERROR_OK
) {
2750 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2751 if (target
->backoff
.times
* polling_interval
< 5000) {
2752 target
->backoff
.times
*= 2;
2753 target
->backoff
.times
++;
2756 /* Tell GDB to halt the debugger. This allows the user to
2757 * run monitor commands to handle the situation.
2759 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2761 if (target
->backoff
.times
> 0) {
2762 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
2763 target_reset_examined(target
);
2764 retval
= target_examine_one(target
);
2765 /* Target examination could have failed due to unstable connection,
2766 * but we set the examined flag anyway to repoll it later */
2767 if (retval
!= ERROR_OK
) {
2768 target
->examined
= true;
2769 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2770 target
->backoff
.times
* polling_interval
);
2775 /* Since we succeeded, we reset backoff count */
2776 target
->backoff
.times
= 0;
2783 COMMAND_HANDLER(handle_reg_command
)
2785 struct target
*target
;
2786 struct reg
*reg
= NULL
;
2792 target
= get_current_target(CMD_CTX
);
2794 /* list all available registers for the current target */
2795 if (CMD_ARGC
== 0) {
2796 struct reg_cache
*cache
= target
->reg_cache
;
2802 command_print(CMD_CTX
, "===== %s", cache
->name
);
2804 for (i
= 0, reg
= cache
->reg_list
;
2805 i
< cache
->num_regs
;
2806 i
++, reg
++, count
++) {
2807 /* only print cached values if they are valid */
2809 value
= buf_to_str(reg
->value
,
2811 command_print(CMD_CTX
,
2812 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2820 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2825 cache
= cache
->next
;
2831 /* access a single register by its ordinal number */
2832 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2834 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2836 struct reg_cache
*cache
= target
->reg_cache
;
2840 for (i
= 0; i
< cache
->num_regs
; i
++) {
2841 if (count
++ == num
) {
2842 reg
= &cache
->reg_list
[i
];
2848 cache
= cache
->next
;
2852 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2853 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2857 /* access a single register by its name */
2858 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2861 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2866 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2868 /* display a register */
2869 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2870 && (CMD_ARGV
[1][0] <= '9')))) {
2871 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2874 if (reg
->valid
== 0)
2875 reg
->type
->get(reg
);
2876 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2877 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2882 /* set register value */
2883 if (CMD_ARGC
== 2) {
2884 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2887 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2889 reg
->type
->set(reg
, buf
);
2891 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2892 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2900 return ERROR_COMMAND_SYNTAX_ERROR
;
2903 COMMAND_HANDLER(handle_poll_command
)
2905 int retval
= ERROR_OK
;
2906 struct target
*target
= get_current_target(CMD_CTX
);
2908 if (CMD_ARGC
== 0) {
2909 command_print(CMD_CTX
, "background polling: %s",
2910 jtag_poll_get_enabled() ? "on" : "off");
2911 command_print(CMD_CTX
, "TAP: %s (%s)",
2912 target
->tap
->dotted_name
,
2913 target
->tap
->enabled
? "enabled" : "disabled");
2914 if (!target
->tap
->enabled
)
2916 retval
= target_poll(target
);
2917 if (retval
!= ERROR_OK
)
2919 retval
= target_arch_state(target
);
2920 if (retval
!= ERROR_OK
)
2922 } else if (CMD_ARGC
== 1) {
2924 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2925 jtag_poll_set_enabled(enable
);
2927 return ERROR_COMMAND_SYNTAX_ERROR
;
2932 COMMAND_HANDLER(handle_wait_halt_command
)
2935 return ERROR_COMMAND_SYNTAX_ERROR
;
2937 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2938 if (1 == CMD_ARGC
) {
2939 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2940 if (ERROR_OK
!= retval
)
2941 return ERROR_COMMAND_SYNTAX_ERROR
;
2944 struct target
*target
= get_current_target(CMD_CTX
);
2945 return target_wait_state(target
, TARGET_HALTED
, ms
);
2948 /* wait for target state to change. The trick here is to have a low
2949 * latency for short waits and not to suck up all the CPU time
2952 * After 500ms, keep_alive() is invoked
2954 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2957 int64_t then
= 0, cur
;
2961 retval
= target_poll(target
);
2962 if (retval
!= ERROR_OK
)
2964 if (target
->state
== state
)
2969 then
= timeval_ms();
2970 LOG_DEBUG("waiting for target %s...",
2971 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2977 if ((cur
-then
) > ms
) {
2978 LOG_ERROR("timed out while waiting for target %s",
2979 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2987 COMMAND_HANDLER(handle_halt_command
)
2991 struct target
*target
= get_current_target(CMD_CTX
);
2993 target
->verbose_halt_msg
= true;
2995 int retval
= target_halt(target
);
2996 if (ERROR_OK
!= retval
)
2999 if (CMD_ARGC
== 1) {
3000 unsigned wait_local
;
3001 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
3002 if (ERROR_OK
!= retval
)
3003 return ERROR_COMMAND_SYNTAX_ERROR
;
3008 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
3011 COMMAND_HANDLER(handle_soft_reset_halt_command
)
3013 struct target
*target
= get_current_target(CMD_CTX
);
3015 LOG_USER("requesting target halt and executing a soft reset");
3017 target_soft_reset_halt(target
);
3022 COMMAND_HANDLER(handle_reset_command
)
3025 return ERROR_COMMAND_SYNTAX_ERROR
;
3027 enum target_reset_mode reset_mode
= RESET_RUN
;
3028 if (CMD_ARGC
== 1) {
3030 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
3031 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
3032 return ERROR_COMMAND_SYNTAX_ERROR
;
3033 reset_mode
= n
->value
;
3036 /* reset *all* targets */
3037 return target_process_reset(CMD_CTX
, reset_mode
);
3041 COMMAND_HANDLER(handle_resume_command
)
3045 return ERROR_COMMAND_SYNTAX_ERROR
;
3047 struct target
*target
= get_current_target(CMD_CTX
);
3049 /* with no CMD_ARGV, resume from current pc, addr = 0,
3050 * with one arguments, addr = CMD_ARGV[0],
3051 * handle breakpoints, not debugging */
3052 target_addr_t addr
= 0;
3053 if (CMD_ARGC
== 1) {
3054 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3058 return target_resume(target
, current
, addr
, 1, 0);
3061 COMMAND_HANDLER(handle_step_command
)
3064 return ERROR_COMMAND_SYNTAX_ERROR
;
3068 /* with no CMD_ARGV, step from current pc, addr = 0,
3069 * with one argument addr = CMD_ARGV[0],
3070 * handle breakpoints, debugging */
3071 target_addr_t addr
= 0;
3073 if (CMD_ARGC
== 1) {
3074 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3078 struct target
*target
= get_current_target(CMD_CTX
);
3080 return target
->type
->step(target
, current_pc
, addr
, 1);
3083 static void handle_md_output(struct command_context
*cmd_ctx
,
3084 struct target
*target
, target_addr_t address
, unsigned size
,
3085 unsigned count
, const uint8_t *buffer
)
3087 const unsigned line_bytecnt
= 32;
3088 unsigned line_modulo
= line_bytecnt
/ size
;
3090 char output
[line_bytecnt
* 4 + 1];
3091 unsigned output_len
= 0;
3093 const char *value_fmt
;
3096 value_fmt
= "%16.16"PRIx64
" ";
3099 value_fmt
= "%8.8"PRIx64
" ";
3102 value_fmt
= "%4.4"PRIx64
" ";
3105 value_fmt
= "%2.2"PRIx64
" ";
3108 /* "can't happen", caller checked */
3109 LOG_ERROR("invalid memory read size: %u", size
);
3113 for (unsigned i
= 0; i
< count
; i
++) {
3114 if (i
% line_modulo
== 0) {
3115 output_len
+= snprintf(output
+ output_len
,
3116 sizeof(output
) - output_len
,
3117 TARGET_ADDR_FMT
": ",
3118 (address
+ (i
* size
)));
3122 const uint8_t *value_ptr
= buffer
+ i
* size
;
3125 value
= target_buffer_get_u64(target
, value_ptr
);
3128 value
= target_buffer_get_u32(target
, value_ptr
);
3131 value
= target_buffer_get_u16(target
, value_ptr
);
3136 output_len
+= snprintf(output
+ output_len
,
3137 sizeof(output
) - output_len
,
3140 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
3141 command_print(cmd_ctx
, "%s", output
);
3147 COMMAND_HANDLER(handle_md_command
)
3150 return ERROR_COMMAND_SYNTAX_ERROR
;
3153 switch (CMD_NAME
[2]) {
3167 return ERROR_COMMAND_SYNTAX_ERROR
;
3170 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3171 int (*fn
)(struct target
*target
,
3172 target_addr_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
3176 fn
= target_read_phys_memory
;
3178 fn
= target_read_memory
;
3179 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
3180 return ERROR_COMMAND_SYNTAX_ERROR
;
3182 target_addr_t address
;
3183 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3187 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
3189 uint8_t *buffer
= calloc(count
, size
);
3190 if (buffer
== NULL
) {
3191 LOG_ERROR("Failed to allocate md read buffer");
3195 struct target
*target
= get_current_target(CMD_CTX
);
3196 int retval
= fn(target
, address
, size
, count
, buffer
);
3197 if (ERROR_OK
== retval
)
3198 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
3205 typedef int (*target_write_fn
)(struct target
*target
,
3206 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
3208 static int target_fill_mem(struct target
*target
,
3209 target_addr_t address
,
3217 /* We have to write in reasonably large chunks to be able
3218 * to fill large memory areas with any sane speed */
3219 const unsigned chunk_size
= 16384;
3220 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
3221 if (target_buf
== NULL
) {
3222 LOG_ERROR("Out of memory");
3226 for (unsigned i
= 0; i
< chunk_size
; i
++) {
3227 switch (data_size
) {
3229 target_buffer_set_u64(target
, target_buf
+ i
* data_size
, b
);
3232 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
3235 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
3238 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
3245 int retval
= ERROR_OK
;
3247 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
3250 if (current
> chunk_size
)
3251 current
= chunk_size
;
3252 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
3253 if (retval
!= ERROR_OK
)
3255 /* avoid GDB timeouts */
3264 COMMAND_HANDLER(handle_mw_command
)
3267 return ERROR_COMMAND_SYNTAX_ERROR
;
3268 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3273 fn
= target_write_phys_memory
;
3275 fn
= target_write_memory
;
3276 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
3277 return ERROR_COMMAND_SYNTAX_ERROR
;
3279 target_addr_t address
;
3280 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3282 target_addr_t value
;
3283 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], value
);
3287 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
3289 struct target
*target
= get_current_target(CMD_CTX
);
3291 switch (CMD_NAME
[2]) {
3305 return ERROR_COMMAND_SYNTAX_ERROR
;
3308 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
3311 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
3312 target_addr_t
*min_address
, target_addr_t
*max_address
)
3314 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
3315 return ERROR_COMMAND_SYNTAX_ERROR
;
3317 /* a base address isn't always necessary,
3318 * default to 0x0 (i.e. don't relocate) */
3319 if (CMD_ARGC
>= 2) {
3321 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3322 image
->base_address
= addr
;
3323 image
->base_address_set
= 1;
3325 image
->base_address_set
= 0;
3327 image
->start_address_set
= 0;
3330 COMMAND_PARSE_ADDRESS(CMD_ARGV
[3], *min_address
);
3331 if (CMD_ARGC
== 5) {
3332 COMMAND_PARSE_ADDRESS(CMD_ARGV
[4], *max_address
);
3333 /* use size (given) to find max (required) */
3334 *max_address
+= *min_address
;
3337 if (*min_address
> *max_address
)
3338 return ERROR_COMMAND_SYNTAX_ERROR
;
3343 COMMAND_HANDLER(handle_load_image_command
)
3347 uint32_t image_size
;
3348 target_addr_t min_address
= 0;
3349 target_addr_t max_address
= -1;
3353 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
3354 &image
, &min_address
, &max_address
);
3355 if (ERROR_OK
!= retval
)
3358 struct target
*target
= get_current_target(CMD_CTX
);
3360 struct duration bench
;
3361 duration_start(&bench
);
3363 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
3368 for (i
= 0; i
< image
.num_sections
; i
++) {
3369 buffer
= malloc(image
.sections
[i
].size
);
3370 if (buffer
== NULL
) {
3371 command_print(CMD_CTX
,
3372 "error allocating buffer for section (%d bytes)",
3373 (int)(image
.sections
[i
].size
));
3374 retval
= ERROR_FAIL
;
3378 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3379 if (retval
!= ERROR_OK
) {
3384 uint32_t offset
= 0;
3385 uint32_t length
= buf_cnt
;
3387 /* DANGER!!! beware of unsigned comparision here!!! */
3389 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3390 (image
.sections
[i
].base_address
< max_address
)) {
3392 if (image
.sections
[i
].base_address
< min_address
) {
3393 /* clip addresses below */
3394 offset
+= min_address
-image
.sections
[i
].base_address
;
3398 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3399 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3401 retval
= target_write_buffer(target
,
3402 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3403 if (retval
!= ERROR_OK
) {
3407 image_size
+= length
;
3408 command_print(CMD_CTX
, "%u bytes written at address " TARGET_ADDR_FMT
"",
3409 (unsigned int)length
,
3410 image
.sections
[i
].base_address
+ offset
);
3416 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3417 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
3418 "in %fs (%0.3f KiB/s)", image_size
,
3419 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3422 image_close(&image
);
3428 COMMAND_HANDLER(handle_dump_image_command
)
3430 struct fileio
*fileio
;
3432 int retval
, retvaltemp
;
3433 target_addr_t address
, size
;
3434 struct duration bench
;
3435 struct target
*target
= get_current_target(CMD_CTX
);
3438 return ERROR_COMMAND_SYNTAX_ERROR
;
3440 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], address
);
3441 COMMAND_PARSE_ADDRESS(CMD_ARGV
[2], size
);
3443 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
3444 buffer
= malloc(buf_size
);
3448 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3449 if (retval
!= ERROR_OK
) {
3454 duration_start(&bench
);
3457 size_t size_written
;
3458 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
3459 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
3460 if (retval
!= ERROR_OK
)
3463 retval
= fileio_write(fileio
, this_run_size
, buffer
, &size_written
);
3464 if (retval
!= ERROR_OK
)
3467 size
-= this_run_size
;
3468 address
+= this_run_size
;
3473 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3475 retval
= fileio_size(fileio
, &filesize
);
3476 if (retval
!= ERROR_OK
)
3478 command_print(CMD_CTX
,
3479 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize
,
3480 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3483 retvaltemp
= fileio_close(fileio
);
3484 if (retvaltemp
!= ERROR_OK
)
3493 IMAGE_CHECKSUM_ONLY
= 2
3496 static COMMAND_HELPER(handle_verify_image_command_internal
, enum verify_mode verify
)
3500 uint32_t image_size
;
3503 uint32_t checksum
= 0;
3504 uint32_t mem_checksum
= 0;
3508 struct target
*target
= get_current_target(CMD_CTX
);
3511 return ERROR_COMMAND_SYNTAX_ERROR
;
3514 LOG_ERROR("no target selected");
3518 struct duration bench
;
3519 duration_start(&bench
);
3521 if (CMD_ARGC
>= 2) {
3523 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3524 image
.base_address
= addr
;
3525 image
.base_address_set
= 1;
3527 image
.base_address_set
= 0;
3528 image
.base_address
= 0x0;
3531 image
.start_address_set
= 0;
3533 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3534 if (retval
!= ERROR_OK
)
3540 for (i
= 0; i
< image
.num_sections
; i
++) {
3541 buffer
= malloc(image
.sections
[i
].size
);
3542 if (buffer
== NULL
) {
3543 command_print(CMD_CTX
,
3544 "error allocating buffer for section (%d bytes)",
3545 (int)(image
.sections
[i
].size
));
3548 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3549 if (retval
!= ERROR_OK
) {
3554 if (verify
>= IMAGE_VERIFY
) {
3555 /* calculate checksum of image */
3556 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3557 if (retval
!= ERROR_OK
) {
3562 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3563 if (retval
!= ERROR_OK
) {
3567 if ((checksum
!= mem_checksum
) && (verify
== IMAGE_CHECKSUM_ONLY
)) {
3568 LOG_ERROR("checksum mismatch");
3570 retval
= ERROR_FAIL
;
3573 if (checksum
!= mem_checksum
) {
3574 /* failed crc checksum, fall back to a binary compare */
3578 LOG_ERROR("checksum mismatch - attempting binary compare");
3580 data
= malloc(buf_cnt
);
3582 /* Can we use 32bit word accesses? */
3584 int count
= buf_cnt
;
3585 if ((count
% 4) == 0) {
3589 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
3590 if (retval
== ERROR_OK
) {
3592 for (t
= 0; t
< buf_cnt
; t
++) {
3593 if (data
[t
] != buffer
[t
]) {
3594 command_print(CMD_CTX
,
3595 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3597 (unsigned)(t
+ image
.sections
[i
].base_address
),
3600 if (diffs
++ >= 127) {
3601 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
3613 command_print(CMD_CTX
, "address " TARGET_ADDR_FMT
" length 0x%08zx",
3614 image
.sections
[i
].base_address
,
3619 image_size
+= buf_cnt
;
3622 command_print(CMD_CTX
, "No more differences found.");
3625 retval
= ERROR_FAIL
;
3626 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3627 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3628 "in %fs (%0.3f KiB/s)", image_size
,
3629 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3632 image_close(&image
);
3637 COMMAND_HANDLER(handle_verify_image_checksum_command
)
3639 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_CHECKSUM_ONLY
);
3642 COMMAND_HANDLER(handle_verify_image_command
)
3644 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_VERIFY
);
3647 COMMAND_HANDLER(handle_test_image_command
)
3649 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_TEST
);
3652 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
3654 struct target
*target
= get_current_target(cmd_ctx
);
3655 struct breakpoint
*breakpoint
= target
->breakpoints
;
3656 while (breakpoint
) {
3657 if (breakpoint
->type
== BKPT_SOFT
) {
3658 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3659 breakpoint
->length
, 16);
3660 command_print(cmd_ctx
, "IVA breakpoint: " TARGET_ADDR_FMT
", 0x%x, %i, 0x%s",
3661 breakpoint
->address
,
3663 breakpoint
->set
, buf
);
3666 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3667 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3669 breakpoint
->length
, breakpoint
->set
);
3670 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3671 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3672 breakpoint
->address
,
3673 breakpoint
->length
, breakpoint
->set
);
3674 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3677 command_print(cmd_ctx
, "Breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3678 breakpoint
->address
,
3679 breakpoint
->length
, breakpoint
->set
);
3682 breakpoint
= breakpoint
->next
;
3687 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
3688 target_addr_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3690 struct target
*target
= get_current_target(cmd_ctx
);
3694 retval
= breakpoint_add(target
, addr
, length
, hw
);
3695 if (ERROR_OK
== retval
)
3696 command_print(cmd_ctx
, "breakpoint set at " TARGET_ADDR_FMT
"", addr
);
3698 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3701 } else if (addr
== 0) {
3702 if (target
->type
->add_context_breakpoint
== NULL
) {
3703 LOG_WARNING("Context breakpoint not available");
3706 retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3707 if (ERROR_OK
== retval
)
3708 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3710 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3714 if (target
->type
->add_hybrid_breakpoint
== NULL
) {
3715 LOG_WARNING("Hybrid breakpoint not available");
3718 retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3719 if (ERROR_OK
== retval
)
3720 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3722 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3729 COMMAND_HANDLER(handle_bp_command
)
3738 return handle_bp_command_list(CMD_CTX
);
3742 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3743 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3744 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3747 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
3749 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3750 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3752 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3753 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
3755 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
3756 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3758 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3763 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3764 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
3765 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
3766 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3769 return ERROR_COMMAND_SYNTAX_ERROR
;
3773 COMMAND_HANDLER(handle_rbp_command
)
3776 return ERROR_COMMAND_SYNTAX_ERROR
;
3779 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3781 struct target
*target
= get_current_target(CMD_CTX
);
3782 breakpoint_remove(target
, addr
);
3787 COMMAND_HANDLER(handle_wp_command
)
3789 struct target
*target
= get_current_target(CMD_CTX
);
3791 if (CMD_ARGC
== 0) {
3792 struct watchpoint
*watchpoint
= target
->watchpoints
;
3794 while (watchpoint
) {
3795 command_print(CMD_CTX
, "address: " TARGET_ADDR_FMT
3796 ", len: 0x%8.8" PRIx32
3797 ", r/w/a: %i, value: 0x%8.8" PRIx32
3798 ", mask: 0x%8.8" PRIx32
,
3799 watchpoint
->address
,
3801 (int)watchpoint
->rw
,
3804 watchpoint
= watchpoint
->next
;
3809 enum watchpoint_rw type
= WPT_ACCESS
;
3811 uint32_t length
= 0;
3812 uint32_t data_value
= 0x0;
3813 uint32_t data_mask
= 0xffffffff;
3817 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
3820 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3823 switch (CMD_ARGV
[2][0]) {
3834 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3835 return ERROR_COMMAND_SYNTAX_ERROR
;
3839 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3840 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3844 return ERROR_COMMAND_SYNTAX_ERROR
;
3847 int retval
= watchpoint_add(target
, addr
, length
, type
,
3848 data_value
, data_mask
);
3849 if (ERROR_OK
!= retval
)
3850 LOG_ERROR("Failure setting watchpoints");
3855 COMMAND_HANDLER(handle_rwp_command
)
3858 return ERROR_COMMAND_SYNTAX_ERROR
;
3861 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3863 struct target
*target
= get_current_target(CMD_CTX
);
3864 watchpoint_remove(target
, addr
);
3870 * Translate a virtual address to a physical address.
3872 * The low-level target implementation must have logged a detailed error
3873 * which is forwarded to telnet/GDB session.
3875 COMMAND_HANDLER(handle_virt2phys_command
)
3878 return ERROR_COMMAND_SYNTAX_ERROR
;
3881 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], va
);
3884 struct target
*target
= get_current_target(CMD_CTX
);
3885 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3886 if (retval
== ERROR_OK
)
3887 command_print(CMD_CTX
, "Physical address " TARGET_ADDR_FMT
"", pa
);
3892 static void writeData(FILE *f
, const void *data
, size_t len
)
3894 size_t written
= fwrite(data
, 1, len
, f
);
3896 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3899 static void writeLong(FILE *f
, int l
, struct target
*target
)
3903 target_buffer_set_u32(target
, val
, l
);
3904 writeData(f
, val
, 4);
3907 static void writeString(FILE *f
, char *s
)
3909 writeData(f
, s
, strlen(s
));
3912 typedef unsigned char UNIT
[2]; /* unit of profiling */
3914 /* Dump a gmon.out histogram file. */
3915 static void write_gmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
, bool with_range
,
3916 uint32_t start_address
, uint32_t end_address
, struct target
*target
, uint32_t duration_ms
)
3919 FILE *f
= fopen(filename
, "w");
3922 writeString(f
, "gmon");
3923 writeLong(f
, 0x00000001, target
); /* Version */
3924 writeLong(f
, 0, target
); /* padding */
3925 writeLong(f
, 0, target
); /* padding */
3926 writeLong(f
, 0, target
); /* padding */
3928 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3929 writeData(f
, &zero
, 1);
3931 /* figure out bucket size */
3935 min
= start_address
;
3940 for (i
= 0; i
< sampleNum
; i
++) {
3941 if (min
> samples
[i
])
3943 if (max
< samples
[i
])
3947 /* max should be (largest sample + 1)
3948 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
3952 int addressSpace
= max
- min
;
3953 assert(addressSpace
>= 2);
3955 /* FIXME: What is the reasonable number of buckets?
3956 * The profiling result will be more accurate if there are enough buckets. */
3957 static const uint32_t maxBuckets
= 128 * 1024; /* maximum buckets. */
3958 uint32_t numBuckets
= addressSpace
/ sizeof(UNIT
);
3959 if (numBuckets
> maxBuckets
)
3960 numBuckets
= maxBuckets
;
3961 int *buckets
= malloc(sizeof(int) * numBuckets
);
3962 if (buckets
== NULL
) {
3966 memset(buckets
, 0, sizeof(int) * numBuckets
);
3967 for (i
= 0; i
< sampleNum
; i
++) {
3968 uint32_t address
= samples
[i
];
3970 if ((address
< min
) || (max
<= address
))
3973 long long a
= address
- min
;
3974 long long b
= numBuckets
;
3975 long long c
= addressSpace
;
3976 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
3980 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3981 writeLong(f
, min
, target
); /* low_pc */
3982 writeLong(f
, max
, target
); /* high_pc */
3983 writeLong(f
, numBuckets
, target
); /* # of buckets */
3984 float sample_rate
= sampleNum
/ (duration_ms
/ 1000.0);
3985 writeLong(f
, sample_rate
, target
);
3986 writeString(f
, "seconds");
3987 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3988 writeData(f
, &zero
, 1);
3989 writeString(f
, "s");
3991 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3993 char *data
= malloc(2 * numBuckets
);
3995 for (i
= 0; i
< numBuckets
; i
++) {
4000 data
[i
* 2] = val
&0xff;
4001 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
4004 writeData(f
, data
, numBuckets
* 2);
4012 /* profiling samples the CPU PC as quickly as OpenOCD is able,
4013 * which will be used as a random sampling of PC */
4014 COMMAND_HANDLER(handle_profile_command
)
4016 struct target
*target
= get_current_target(CMD_CTX
);
4018 if ((CMD_ARGC
!= 2) && (CMD_ARGC
!= 4))
4019 return ERROR_COMMAND_SYNTAX_ERROR
;
4021 const uint32_t MAX_PROFILE_SAMPLE_NUM
= 10000;
4023 uint32_t num_of_samples
;
4024 int retval
= ERROR_OK
;
4026 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], offset
);
4028 uint32_t *samples
= malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM
);
4029 if (samples
== NULL
) {
4030 LOG_ERROR("No memory to store samples.");
4034 uint64_t timestart_ms
= timeval_ms();
4036 * Some cores let us sample the PC without the
4037 * annoying halt/resume step; for example, ARMv7 PCSR.
4038 * Provide a way to use that more efficient mechanism.
4040 retval
= target_profiling(target
, samples
, MAX_PROFILE_SAMPLE_NUM
,
4041 &num_of_samples
, offset
);
4042 if (retval
!= ERROR_OK
) {
4046 uint32_t duration_ms
= timeval_ms() - timestart_ms
;
4048 assert(num_of_samples
<= MAX_PROFILE_SAMPLE_NUM
);
4050 retval
= target_poll(target
);
4051 if (retval
!= ERROR_OK
) {
4055 if (target
->state
== TARGET_RUNNING
) {
4056 retval
= target_halt(target
);
4057 if (retval
!= ERROR_OK
) {
4063 retval
= target_poll(target
);
4064 if (retval
!= ERROR_OK
) {
4069 uint32_t start_address
= 0;
4070 uint32_t end_address
= 0;
4071 bool with_range
= false;
4072 if (CMD_ARGC
== 4) {
4074 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], start_address
);
4075 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], end_address
);
4078 write_gmon(samples
, num_of_samples
, CMD_ARGV
[1],
4079 with_range
, start_address
, end_address
, target
, duration_ms
);
4080 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
4086 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
4089 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4092 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4096 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4097 valObjPtr
= Jim_NewIntObj(interp
, val
);
4098 if (!nameObjPtr
|| !valObjPtr
) {
4103 Jim_IncrRefCount(nameObjPtr
);
4104 Jim_IncrRefCount(valObjPtr
);
4105 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
4106 Jim_DecrRefCount(interp
, nameObjPtr
);
4107 Jim_DecrRefCount(interp
, valObjPtr
);
4109 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
4113 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4115 struct command_context
*context
;
4116 struct target
*target
;
4118 context
= current_command_context(interp
);
4119 assert(context
!= NULL
);
4121 target
= get_current_target(context
);
4122 if (target
== NULL
) {
4123 LOG_ERROR("mem2array: no current target");
4127 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4130 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
4138 const char *varname
;
4144 /* argv[1] = name of array to receive the data
4145 * argv[2] = desired width
4146 * argv[3] = memory address
4147 * argv[4] = count of times to read
4150 if (argc
< 4 || argc
> 5) {
4151 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4154 varname
= Jim_GetString(argv
[0], &len
);
4155 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4157 e
= Jim_GetLong(interp
, argv
[1], &l
);
4162 e
= Jim_GetLong(interp
, argv
[2], &l
);
4166 e
= Jim_GetLong(interp
, argv
[3], &l
);
4172 phys
= Jim_GetString(argv
[4], &n
);
4173 if (!strncmp(phys
, "phys", n
))
4189 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4190 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
4194 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4195 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
4198 if ((addr
+ (len
* width
)) < addr
) {
4199 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4200 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
4203 /* absurd transfer size? */
4205 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4206 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
4211 ((width
== 2) && ((addr
& 1) == 0)) ||
4212 ((width
== 4) && ((addr
& 3) == 0))) {
4216 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4217 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
4220 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4229 size_t buffersize
= 4096;
4230 uint8_t *buffer
= malloc(buffersize
);
4237 /* Slurp... in buffer size chunks */
4239 count
= len
; /* in objects.. */
4240 if (count
> (buffersize
/ width
))
4241 count
= (buffersize
/ width
);
4244 retval
= target_read_phys_memory(target
, addr
, width
, count
, buffer
);
4246 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
4247 if (retval
!= ERROR_OK
) {
4249 LOG_ERROR("mem2array: Read @ 0x%08" PRIx32
", w=%" PRId32
", cnt=%" PRId32
", failed",
4253 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4254 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
4258 v
= 0; /* shut up gcc */
4259 for (i
= 0; i
< count
; i
++, n
++) {
4262 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
4265 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
4268 v
= buffer
[i
] & 0x0ff;
4271 new_int_array_element(interp
, varname
, n
, v
);
4274 addr
+= count
* width
;
4280 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4285 static int get_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t *val
)
4288 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4292 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4296 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4302 Jim_IncrRefCount(nameObjPtr
);
4303 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
4304 Jim_DecrRefCount(interp
, nameObjPtr
);
4306 if (valObjPtr
== NULL
)
4309 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
4310 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
4315 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4317 struct command_context
*context
;
4318 struct target
*target
;
4320 context
= current_command_context(interp
);
4321 assert(context
!= NULL
);
4323 target
= get_current_target(context
);
4324 if (target
== NULL
) {
4325 LOG_ERROR("array2mem: no current target");
4329 return target_array2mem(interp
, target
, argc
-1, argv
+ 1);
4332 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
4333 int argc
, Jim_Obj
*const *argv
)
4341 const char *varname
;
4347 /* argv[1] = name of array to get the data
4348 * argv[2] = desired width
4349 * argv[3] = memory address
4350 * argv[4] = count to write
4352 if (argc
< 4 || argc
> 5) {
4353 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4356 varname
= Jim_GetString(argv
[0], &len
);
4357 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4359 e
= Jim_GetLong(interp
, argv
[1], &l
);
4364 e
= Jim_GetLong(interp
, argv
[2], &l
);
4368 e
= Jim_GetLong(interp
, argv
[3], &l
);
4374 phys
= Jim_GetString(argv
[4], &n
);
4375 if (!strncmp(phys
, "phys", n
))
4391 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4392 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4393 "Invalid width param, must be 8/16/32", NULL
);
4397 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4398 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4399 "array2mem: zero width read?", NULL
);
4402 if ((addr
+ (len
* width
)) < addr
) {
4403 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4404 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4405 "array2mem: addr + len - wraps to zero?", NULL
);
4408 /* absurd transfer size? */
4410 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4411 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4412 "array2mem: absurd > 64K item request", NULL
);
4417 ((width
== 2) && ((addr
& 1) == 0)) ||
4418 ((width
== 4) && ((addr
& 3) == 0))) {
4422 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4423 sprintf(buf
, "array2mem address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
4426 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4437 size_t buffersize
= 4096;
4438 uint8_t *buffer
= malloc(buffersize
);
4443 /* Slurp... in buffer size chunks */
4445 count
= len
; /* in objects.. */
4446 if (count
> (buffersize
/ width
))
4447 count
= (buffersize
/ width
);
4449 v
= 0; /* shut up gcc */
4450 for (i
= 0; i
< count
; i
++, n
++) {
4451 get_int_array_element(interp
, varname
, n
, &v
);
4454 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
4457 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
4460 buffer
[i
] = v
& 0x0ff;
4467 retval
= target_write_phys_memory(target
, addr
, width
, count
, buffer
);
4469 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
4470 if (retval
!= ERROR_OK
) {
4472 LOG_ERROR("array2mem: Write @ 0x%08" PRIx32
", w=%" PRId32
", cnt=%" PRId32
", failed",
4476 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4477 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
4481 addr
+= count
* width
;
4486 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4491 /* FIX? should we propagate errors here rather than printing them
4494 void target_handle_event(struct target
*target
, enum target_event e
)
4496 struct target_event_action
*teap
;
4498 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4499 if (teap
->event
== e
) {
4500 LOG_DEBUG("target(%d): %s (%s) event: %d (%s) action: %s",
4501 target
->target_number
,
4502 target_name(target
),
4503 target_type_name(target
),
4505 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
4506 Jim_GetString(teap
->body
, NULL
));
4508 /* Override current target by the target an event
4509 * is issued from (lot of scripts need it).
4510 * Return back to previous override as soon
4511 * as the handler processing is done */
4512 struct command_context
*cmd_ctx
= current_command_context(teap
->interp
);
4513 struct target
*saved_target_override
= cmd_ctx
->current_target_override
;
4514 cmd_ctx
->current_target_override
= target
;
4516 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
) {
4517 Jim_MakeErrorMessage(teap
->interp
);
4518 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
4521 cmd_ctx
->current_target_override
= saved_target_override
;
4527 * Returns true only if the target has a handler for the specified event.
4529 bool target_has_event_action(struct target
*target
, enum target_event event
)
4531 struct target_event_action
*teap
;
4533 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4534 if (teap
->event
== event
)
4540 enum target_cfg_param
{
4543 TCFG_WORK_AREA_VIRT
,
4544 TCFG_WORK_AREA_PHYS
,
4545 TCFG_WORK_AREA_SIZE
,
4546 TCFG_WORK_AREA_BACKUP
,
4549 TCFG_CHAIN_POSITION
,
4556 static Jim_Nvp nvp_config_opts
[] = {
4557 { .name
= "-type", .value
= TCFG_TYPE
},
4558 { .name
= "-event", .value
= TCFG_EVENT
},
4559 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
4560 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
4561 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
4562 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
4563 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
4564 { .name
= "-coreid", .value
= TCFG_COREID
},
4565 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
4566 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
4567 { .name
= "-rtos", .value
= TCFG_RTOS
},
4568 { .name
= "-defer-examine", .value
= TCFG_DEFER_EXAMINE
},
4569 { .name
= "-gdb-port", .value
= TCFG_GDB_PORT
},
4570 { .name
= NULL
, .value
= -1 }
4573 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
4580 /* parse config or cget options ... */
4581 while (goi
->argc
> 0) {
4582 Jim_SetEmptyResult(goi
->interp
);
4583 /* Jim_GetOpt_Debug(goi); */
4585 if (target
->type
->target_jim_configure
) {
4586 /* target defines a configure function */
4587 /* target gets first dibs on parameters */
4588 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
4597 /* otherwise we 'continue' below */
4599 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
4601 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
4607 if (goi
->isconfigure
) {
4608 Jim_SetResultFormatted(goi
->interp
,
4609 "not settable: %s", n
->name
);
4613 if (goi
->argc
!= 0) {
4614 Jim_WrongNumArgs(goi
->interp
,
4615 goi
->argc
, goi
->argv
,
4620 Jim_SetResultString(goi
->interp
,
4621 target_type_name(target
), -1);
4625 if (goi
->argc
== 0) {
4626 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
4630 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
4632 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
4636 if (goi
->isconfigure
) {
4637 if (goi
->argc
!= 1) {
4638 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
4642 if (goi
->argc
!= 0) {
4643 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
4649 struct target_event_action
*teap
;
4651 teap
= target
->event_action
;
4652 /* replace existing? */
4654 if (teap
->event
== (enum target_event
)n
->value
)
4659 if (goi
->isconfigure
) {
4660 bool replace
= true;
4663 teap
= calloc(1, sizeof(*teap
));
4666 teap
->event
= n
->value
;
4667 teap
->interp
= goi
->interp
;
4668 Jim_GetOpt_Obj(goi
, &o
);
4670 Jim_DecrRefCount(teap
->interp
, teap
->body
);
4671 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
4674 * Tcl/TK - "tk events" have a nice feature.
4675 * See the "BIND" command.
4676 * We should support that here.
4677 * You can specify %X and %Y in the event code.
4678 * The idea is: %T - target name.
4679 * The idea is: %N - target number
4680 * The idea is: %E - event name.
4682 Jim_IncrRefCount(teap
->body
);
4685 /* add to head of event list */
4686 teap
->next
= target
->event_action
;
4687 target
->event_action
= teap
;
4689 Jim_SetEmptyResult(goi
->interp
);
4693 Jim_SetEmptyResult(goi
->interp
);
4695 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
4701 case TCFG_WORK_AREA_VIRT
:
4702 if (goi
->isconfigure
) {
4703 target_free_all_working_areas(target
);
4704 e
= Jim_GetOpt_Wide(goi
, &w
);
4707 target
->working_area_virt
= w
;
4708 target
->working_area_virt_spec
= true;
4713 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
4717 case TCFG_WORK_AREA_PHYS
:
4718 if (goi
->isconfigure
) {
4719 target_free_all_working_areas(target
);
4720 e
= Jim_GetOpt_Wide(goi
, &w
);
4723 target
->working_area_phys
= w
;
4724 target
->working_area_phys_spec
= true;
4729 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
4733 case TCFG_WORK_AREA_SIZE
:
4734 if (goi
->isconfigure
) {
4735 target_free_all_working_areas(target
);
4736 e
= Jim_GetOpt_Wide(goi
, &w
);
4739 target
->working_area_size
= w
;
4744 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4748 case TCFG_WORK_AREA_BACKUP
:
4749 if (goi
->isconfigure
) {
4750 target_free_all_working_areas(target
);
4751 e
= Jim_GetOpt_Wide(goi
, &w
);
4754 /* make this exactly 1 or 0 */
4755 target
->backup_working_area
= (!!w
);
4760 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
4761 /* loop for more e*/
4766 if (goi
->isconfigure
) {
4767 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
4769 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
4772 target
->endianness
= n
->value
;
4777 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4778 if (n
->name
== NULL
) {
4779 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4780 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4782 Jim_SetResultString(goi
->interp
, n
->name
, -1);
4787 if (goi
->isconfigure
) {
4788 e
= Jim_GetOpt_Wide(goi
, &w
);
4791 target
->coreid
= (int32_t)w
;
4796 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4800 case TCFG_CHAIN_POSITION
:
4801 if (goi
->isconfigure
) {
4803 struct jtag_tap
*tap
;
4805 if (target
->has_dap
) {
4806 Jim_SetResultString(goi
->interp
,
4807 "target requires -dap parameter instead of -chain-position!", -1);
4811 target_free_all_working_areas(target
);
4812 e
= Jim_GetOpt_Obj(goi
, &o_t
);
4815 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
4819 target
->tap_configured
= true;
4824 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
4825 /* loop for more e*/
4828 if (goi
->isconfigure
) {
4829 e
= Jim_GetOpt_Wide(goi
, &w
);
4832 target
->dbgbase
= (uint32_t)w
;
4833 target
->dbgbase_set
= true;
4838 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
4844 int result
= rtos_create(goi
, target
);
4845 if (result
!= JIM_OK
)
4851 case TCFG_DEFER_EXAMINE
:
4853 target
->defer_examine
= true;
4858 if (goi
->isconfigure
) {
4860 e
= Jim_GetOpt_String(goi
, &s
, NULL
);
4863 target
->gdb_port_override
= strdup(s
);
4868 Jim_SetResultString(goi
->interp
, target
->gdb_port_override
? : "undefined", -1);
4872 } /* while (goi->argc) */
4875 /* done - we return */
4879 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4883 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4884 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4886 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4887 "missing: -option ...");
4890 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4891 return target_configure(&goi
, target
);
4894 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4896 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4899 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4901 if (goi
.argc
< 2 || goi
.argc
> 4) {
4902 Jim_SetResultFormatted(goi
.interp
,
4903 "usage: %s [phys] <address> <data> [<count>]", cmd_name
);
4908 fn
= target_write_memory
;
4911 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4913 struct Jim_Obj
*obj
;
4914 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4918 fn
= target_write_phys_memory
;
4922 e
= Jim_GetOpt_Wide(&goi
, &a
);
4927 e
= Jim_GetOpt_Wide(&goi
, &b
);
4932 if (goi
.argc
== 1) {
4933 e
= Jim_GetOpt_Wide(&goi
, &c
);
4938 /* all args must be consumed */
4942 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4944 if (strcasecmp(cmd_name
, "mww") == 0)
4946 else if (strcasecmp(cmd_name
, "mwh") == 0)
4948 else if (strcasecmp(cmd_name
, "mwb") == 0)
4951 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4955 return (target_fill_mem(target
, a
, fn
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4959 * @brief Reads an array of words/halfwords/bytes from target memory starting at specified address.
4961 * Usage: mdw [phys] <address> [<count>] - for 32 bit reads
4962 * mdh [phys] <address> [<count>] - for 16 bit reads
4963 * mdb [phys] <address> [<count>] - for 8 bit reads
4965 * Count defaults to 1.
4967 * Calls target_read_memory or target_read_phys_memory depending on
4968 * the presence of the "phys" argument
4969 * Reads the target memory in blocks of max. 32 bytes, and returns an array of ints formatted
4970 * to int representation in base16.
4971 * Also outputs read data in a human readable form using command_print
4973 * @param phys if present target_read_phys_memory will be used instead of target_read_memory
4974 * @param address address where to start the read. May be specified in decimal or hex using the standard "0x" prefix
4975 * @param count optional count parameter to read an array of values. If not specified, defaults to 1.
4976 * @returns: JIM_ERR on error or JIM_OK on success and sets the result string to an array of ascii formatted numbers
4977 * on success, with [<count>] number of elements.
4979 * In case of little endian target:
4980 * Example1: "mdw 0x00000000" returns "10123456"
4981 * Exmaple2: "mdh 0x00000000 1" returns "3456"
4982 * Example3: "mdb 0x00000000" returns "56"
4983 * Example4: "mdh 0x00000000 2" returns "3456 1012"
4984 * Example5: "mdb 0x00000000 3" returns "56 34 12"
4986 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4988 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4991 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4993 if ((goi
.argc
< 1) || (goi
.argc
> 3)) {
4994 Jim_SetResultFormatted(goi
.interp
,
4995 "usage: %s [phys] <address> [<count>]", cmd_name
);
4999 int (*fn
)(struct target
*target
,
5000 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
5001 fn
= target_read_memory
;
5004 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
5006 struct Jim_Obj
*obj
;
5007 e
= Jim_GetOpt_Obj(&goi
, &obj
);
5011 fn
= target_read_phys_memory
;
5014 /* Read address parameter */
5016 e
= Jim_GetOpt_Wide(&goi
, &addr
);
5020 /* If next parameter exists, read it out as the count parameter, if not, set it to 1 (default) */
5022 if (goi
.argc
== 1) {
5023 e
= Jim_GetOpt_Wide(&goi
, &count
);
5029 /* all args must be consumed */
5033 jim_wide dwidth
= 1; /* shut up gcc */
5034 if (strcasecmp(cmd_name
, "mdw") == 0)
5036 else if (strcasecmp(cmd_name
, "mdh") == 0)
5038 else if (strcasecmp(cmd_name
, "mdb") == 0)
5041 LOG_ERROR("command '%s' unknown: ", cmd_name
);
5045 /* convert count to "bytes" */
5046 int bytes
= count
* dwidth
;
5048 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
5049 uint8_t target_buf
[32];
5052 y
= (bytes
< 16) ? bytes
: 16; /* y = min(bytes, 16); */
5054 /* Try to read out next block */
5055 e
= fn(target
, addr
, dwidth
, y
/ dwidth
, target_buf
);
5057 if (e
!= ERROR_OK
) {
5058 Jim_SetResultFormatted(interp
, "error reading target @ 0x%08lx", (long)addr
);
5062 command_print_sameline(NULL
, "0x%08x ", (int)(addr
));
5065 for (x
= 0; x
< 16 && x
< y
; x
+= 4) {
5066 z
= target_buffer_get_u32(target
, &(target_buf
[x
]));
5067 command_print_sameline(NULL
, "%08x ", (int)(z
));
5069 for (; (x
< 16) ; x
+= 4)
5070 command_print_sameline(NULL
, " ");
5073 for (x
= 0; x
< 16 && x
< y
; x
+= 2) {
5074 z
= target_buffer_get_u16(target
, &(target_buf
[x
]));
5075 command_print_sameline(NULL
, "%04x ", (int)(z
));
5077 for (; (x
< 16) ; x
+= 2)
5078 command_print_sameline(NULL
, " ");
5082 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
5083 z
= target_buffer_get_u8(target
, &(target_buf
[x
]));
5084 command_print_sameline(NULL
, "%02x ", (int)(z
));
5086 for (; (x
< 16) ; x
+= 1)
5087 command_print_sameline(NULL
, " ");
5090 /* ascii-ify the bytes */
5091 for (x
= 0 ; x
< y
; x
++) {
5092 if ((target_buf
[x
] >= 0x20) &&
5093 (target_buf
[x
] <= 0x7e)) {
5097 target_buf
[x
] = '.';
5102 target_buf
[x
] = ' ';
5107 /* print - with a newline */
5108 command_print_sameline(NULL
, "%s\n", target_buf
);
5116 static int jim_target_mem2array(Jim_Interp
*interp
,
5117 int argc
, Jim_Obj
*const *argv
)
5119 struct target
*target
= Jim_CmdPrivData(interp
);
5120 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
5123 static int jim_target_array2mem(Jim_Interp
*interp
,
5124 int argc
, Jim_Obj
*const *argv
)
5126 struct target
*target
= Jim_CmdPrivData(interp
);
5127 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
5130 static int jim_target_tap_disabled(Jim_Interp
*interp
)
5132 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
5136 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5138 bool allow_defer
= false;
5141 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5143 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5144 Jim_SetResultFormatted(goi
.interp
,
5145 "usage: %s ['allow-defer']", cmd_name
);
5149 strcmp(Jim_GetString(argv
[1], NULL
), "allow-defer") == 0) {
5151 struct Jim_Obj
*obj
;
5152 int e
= Jim_GetOpt_Obj(&goi
, &obj
);
5158 struct target
*target
= Jim_CmdPrivData(interp
);
5159 if (!target
->tap
->enabled
)
5160 return jim_target_tap_disabled(interp
);
5162 if (allow_defer
&& target
->defer_examine
) {
5163 LOG_INFO("Deferring arp_examine of %s", target_name(target
));
5164 LOG_INFO("Use arp_examine command to examine it manually!");
5168 int e
= target
->type
->examine(target
);
5174 static int jim_target_was_examined(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5176 struct target
*target
= Jim_CmdPrivData(interp
);
5178 Jim_SetResultBool(interp
, target_was_examined(target
));
5182 static int jim_target_examine_deferred(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5184 struct target
*target
= Jim_CmdPrivData(interp
);
5186 Jim_SetResultBool(interp
, target
->defer_examine
);
5190 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5193 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5196 struct target
*target
= Jim_CmdPrivData(interp
);
5198 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
5204 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5207 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5210 struct target
*target
= Jim_CmdPrivData(interp
);
5211 if (!target
->tap
->enabled
)
5212 return jim_target_tap_disabled(interp
);
5215 if (!(target_was_examined(target
)))
5216 e
= ERROR_TARGET_NOT_EXAMINED
;
5218 e
= target
->type
->poll(target
);
5224 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5227 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5229 if (goi
.argc
!= 2) {
5230 Jim_WrongNumArgs(interp
, 0, argv
,
5231 "([tT]|[fF]|assert|deassert) BOOL");
5236 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
5238 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
5241 /* the halt or not param */
5243 e
= Jim_GetOpt_Wide(&goi
, &a
);
5247 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
5248 if (!target
->tap
->enabled
)
5249 return jim_target_tap_disabled(interp
);
5251 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
5252 Jim_SetResultFormatted(interp
,
5253 "No target-specific reset for %s",
5254 target_name(target
));
5258 if (target
->defer_examine
)
5259 target_reset_examined(target
);
5261 /* determine if we should halt or not. */
5262 target
->reset_halt
= !!a
;
5263 /* When this happens - all workareas are invalid. */
5264 target_free_all_working_areas_restore(target
, 0);
5267 if (n
->value
== NVP_ASSERT
)
5268 e
= target
->type
->assert_reset(target
);
5270 e
= target
->type
->deassert_reset(target
);
5271 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5274 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5277 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5280 struct target
*target
= Jim_CmdPrivData(interp
);
5281 if (!target
->tap
->enabled
)
5282 return jim_target_tap_disabled(interp
);
5283 int e
= target
->type
->halt(target
);
5284 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5287 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5290 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5292 /* params: <name> statename timeoutmsecs */
5293 if (goi
.argc
!= 2) {
5294 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5295 Jim_SetResultFormatted(goi
.interp
,
5296 "%s <state_name> <timeout_in_msec>", cmd_name
);
5301 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
5303 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
, 1);
5307 e
= Jim_GetOpt_Wide(&goi
, &a
);
5310 struct target
*target
= Jim_CmdPrivData(interp
);
5311 if (!target
->tap
->enabled
)
5312 return jim_target_tap_disabled(interp
);
5314 e
= target_wait_state(target
, n
->value
, a
);
5315 if (e
!= ERROR_OK
) {
5316 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
5317 Jim_SetResultFormatted(goi
.interp
,
5318 "target: %s wait %s fails (%#s) %s",
5319 target_name(target
), n
->name
,
5320 eObj
, target_strerror_safe(e
));
5321 Jim_FreeNewObj(interp
, eObj
);
5326 /* List for human, Events defined for this target.
5327 * scripts/programs should use 'name cget -event NAME'
5329 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5331 struct command_context
*cmd_ctx
= current_command_context(interp
);
5332 assert(cmd_ctx
!= NULL
);
5334 struct target
*target
= Jim_CmdPrivData(interp
);
5335 struct target_event_action
*teap
= target
->event_action
;
5336 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
5337 target
->target_number
,
5338 target_name(target
));
5339 command_print(cmd_ctx
, "%-25s | Body", "Event");
5340 command_print(cmd_ctx
, "------------------------- | "
5341 "----------------------------------------");
5343 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
5344 command_print(cmd_ctx
, "%-25s | %s",
5345 opt
->name
, Jim_GetString(teap
->body
, NULL
));
5348 command_print(cmd_ctx
, "***END***");
5351 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5354 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5357 struct target
*target
= Jim_CmdPrivData(interp
);
5358 Jim_SetResultString(interp
, target_state_name(target
), -1);
5361 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5364 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5365 if (goi
.argc
!= 1) {
5366 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5367 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
5371 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
5373 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
5376 struct target
*target
= Jim_CmdPrivData(interp
);
5377 target_handle_event(target
, n
->value
);
5381 static const struct command_registration target_instance_command_handlers
[] = {
5383 .name
= "configure",
5384 .mode
= COMMAND_CONFIG
,
5385 .jim_handler
= jim_target_configure
,
5386 .help
= "configure a new target for use",
5387 .usage
= "[target_attribute ...]",
5391 .mode
= COMMAND_ANY
,
5392 .jim_handler
= jim_target_configure
,
5393 .help
= "returns the specified target attribute",
5394 .usage
= "target_attribute",
5398 .mode
= COMMAND_EXEC
,
5399 .jim_handler
= jim_target_mw
,
5400 .help
= "Write 32-bit word(s) to target memory",
5401 .usage
= "address data [count]",
5405 .mode
= COMMAND_EXEC
,
5406 .jim_handler
= jim_target_mw
,
5407 .help
= "Write 16-bit half-word(s) to target memory",
5408 .usage
= "address data [count]",
5412 .mode
= COMMAND_EXEC
,
5413 .jim_handler
= jim_target_mw
,
5414 .help
= "Write byte(s) to target memory",
5415 .usage
= "address data [count]",
5419 .mode
= COMMAND_EXEC
,
5420 .jim_handler
= jim_target_md
,
5421 .help
= "Display target memory as 32-bit words",
5422 .usage
= "address [count]",
5426 .mode
= COMMAND_EXEC
,
5427 .jim_handler
= jim_target_md
,
5428 .help
= "Display target memory as 16-bit half-words",
5429 .usage
= "address [count]",
5433 .mode
= COMMAND_EXEC
,
5434 .jim_handler
= jim_target_md
,
5435 .help
= "Display target memory as 8-bit bytes",
5436 .usage
= "address [count]",
5439 .name
= "array2mem",
5440 .mode
= COMMAND_EXEC
,
5441 .jim_handler
= jim_target_array2mem
,
5442 .help
= "Writes Tcl array of 8/16/32 bit numbers "
5444 .usage
= "arrayname bitwidth address count",
5447 .name
= "mem2array",
5448 .mode
= COMMAND_EXEC
,
5449 .jim_handler
= jim_target_mem2array
,
5450 .help
= "Loads Tcl array of 8/16/32 bit numbers "
5451 "from target memory",
5452 .usage
= "arrayname bitwidth address count",
5455 .name
= "eventlist",
5456 .mode
= COMMAND_EXEC
,
5457 .jim_handler
= jim_target_event_list
,
5458 .help
= "displays a table of events defined for this target",
5462 .mode
= COMMAND_EXEC
,
5463 .jim_handler
= jim_target_current_state
,
5464 .help
= "displays the current state of this target",
5467 .name
= "arp_examine",
5468 .mode
= COMMAND_EXEC
,
5469 .jim_handler
= jim_target_examine
,
5470 .help
= "used internally for reset processing",
5471 .usage
= "['allow-defer']",
5474 .name
= "was_examined",
5475 .mode
= COMMAND_EXEC
,
5476 .jim_handler
= jim_target_was_examined
,
5477 .help
= "used internally for reset processing",
5480 .name
= "examine_deferred",
5481 .mode
= COMMAND_EXEC
,
5482 .jim_handler
= jim_target_examine_deferred
,
5483 .help
= "used internally for reset processing",
5486 .name
= "arp_halt_gdb",
5487 .mode
= COMMAND_EXEC
,
5488 .jim_handler
= jim_target_halt_gdb
,
5489 .help
= "used internally for reset processing to halt GDB",
5493 .mode
= COMMAND_EXEC
,
5494 .jim_handler
= jim_target_poll
,
5495 .help
= "used internally for reset processing",
5498 .name
= "arp_reset",
5499 .mode
= COMMAND_EXEC
,
5500 .jim_handler
= jim_target_reset
,
5501 .help
= "used internally for reset processing",
5505 .mode
= COMMAND_EXEC
,
5506 .jim_handler
= jim_target_halt
,
5507 .help
= "used internally for reset processing",
5510 .name
= "arp_waitstate",
5511 .mode
= COMMAND_EXEC
,
5512 .jim_handler
= jim_target_wait_state
,
5513 .help
= "used internally for reset processing",
5516 .name
= "invoke-event",
5517 .mode
= COMMAND_EXEC
,
5518 .jim_handler
= jim_target_invoke_event
,
5519 .help
= "invoke handler for specified event",
5520 .usage
= "event_name",
5522 COMMAND_REGISTRATION_DONE
5525 static int target_create(Jim_GetOptInfo
*goi
)
5532 struct target
*target
;
5533 struct command_context
*cmd_ctx
;
5535 cmd_ctx
= current_command_context(goi
->interp
);
5536 assert(cmd_ctx
!= NULL
);
5538 if (goi
->argc
< 3) {
5539 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
5544 Jim_GetOpt_Obj(goi
, &new_cmd
);
5545 /* does this command exist? */
5546 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
5548 cp
= Jim_GetString(new_cmd
, NULL
);
5549 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
5554 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
5557 struct transport
*tr
= get_current_transport();
5558 if (tr
->override_target
) {
5559 e
= tr
->override_target(&cp
);
5560 if (e
!= ERROR_OK
) {
5561 LOG_ERROR("The selected transport doesn't support this target");
5564 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
5566 /* now does target type exist */
5567 for (x
= 0 ; target_types
[x
] ; x
++) {
5568 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
5573 /* check for deprecated name */
5574 if (target_types
[x
]->deprecated_name
) {
5575 if (0 == strcmp(cp
, target_types
[x
]->deprecated_name
)) {
5577 LOG_WARNING("target name is deprecated use: \'%s\'", target_types
[x
]->name
);
5582 if (target_types
[x
] == NULL
) {
5583 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
5584 for (x
= 0 ; target_types
[x
] ; x
++) {
5585 if (target_types
[x
+ 1]) {
5586 Jim_AppendStrings(goi
->interp
,
5587 Jim_GetResult(goi
->interp
),
5588 target_types
[x
]->name
,
5591 Jim_AppendStrings(goi
->interp
,
5592 Jim_GetResult(goi
->interp
),
5594 target_types
[x
]->name
, NULL
);
5601 target
= calloc(1, sizeof(struct target
));
5602 /* set target number */
5603 target
->target_number
= new_target_number();
5604 cmd_ctx
->current_target
= target
;
5606 /* allocate memory for each unique target type */
5607 target
->type
= calloc(1, sizeof(struct target_type
));
5609 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
5611 /* will be set by "-endian" */
5612 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5614 /* default to first core, override with -coreid */
5617 target
->working_area
= 0x0;
5618 target
->working_area_size
= 0x0;
5619 target
->working_areas
= NULL
;
5620 target
->backup_working_area
= 0;
5622 target
->state
= TARGET_UNKNOWN
;
5623 target
->debug_reason
= DBG_REASON_UNDEFINED
;
5624 target
->reg_cache
= NULL
;
5625 target
->breakpoints
= NULL
;
5626 target
->watchpoints
= NULL
;
5627 target
->next
= NULL
;
5628 target
->arch_info
= NULL
;
5630 target
->verbose_halt_msg
= true;
5632 target
->halt_issued
= false;
5634 /* initialize trace information */
5635 target
->trace_info
= calloc(1, sizeof(struct trace
));
5637 target
->dbgmsg
= NULL
;
5638 target
->dbg_msg_enabled
= 0;
5640 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5642 target
->rtos
= NULL
;
5643 target
->rtos_auto_detect
= false;
5645 target
->gdb_port_override
= NULL
;
5647 /* Do the rest as "configure" options */
5648 goi
->isconfigure
= 1;
5649 e
= target_configure(goi
, target
);
5652 if (target
->has_dap
) {
5653 if (!target
->dap_configured
) {
5654 Jim_SetResultString(goi
->interp
, "-dap ?name? required when creating target", -1);
5658 if (!target
->tap_configured
) {
5659 Jim_SetResultString(goi
->interp
, "-chain-position ?name? required when creating target", -1);
5663 /* tap must be set after target was configured */
5664 if (target
->tap
== NULL
)
5669 free(target
->gdb_port_override
);
5675 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
5676 /* default endian to little if not specified */
5677 target
->endianness
= TARGET_LITTLE_ENDIAN
;
5680 cp
= Jim_GetString(new_cmd
, NULL
);
5681 target
->cmd_name
= strdup(cp
);
5683 if (target
->type
->target_create
) {
5684 e
= (*(target
->type
->target_create
))(target
, goi
->interp
);
5685 if (e
!= ERROR_OK
) {
5686 LOG_DEBUG("target_create failed");
5687 free(target
->gdb_port_override
);
5689 free(target
->cmd_name
);
5695 /* create the target specific commands */
5696 if (target
->type
->commands
) {
5697 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
5699 LOG_ERROR("unable to register '%s' commands", cp
);
5702 /* append to end of list */
5704 struct target
**tpp
;
5705 tpp
= &(all_targets
);
5707 tpp
= &((*tpp
)->next
);
5711 /* now - create the new target name command */
5712 const struct command_registration target_subcommands
[] = {
5714 .chain
= target_instance_command_handlers
,
5717 .chain
= target
->type
->commands
,
5719 COMMAND_REGISTRATION_DONE
5721 const struct command_registration target_commands
[] = {
5724 .mode
= COMMAND_ANY
,
5725 .help
= "target command group",
5727 .chain
= target_subcommands
,
5729 COMMAND_REGISTRATION_DONE
5731 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
5735 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
5737 command_set_handler_data(c
, target
);
5739 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
5742 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5745 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5748 struct command_context
*cmd_ctx
= current_command_context(interp
);
5749 assert(cmd_ctx
!= NULL
);
5751 Jim_SetResultString(interp
, target_name(get_current_target(cmd_ctx
)), -1);
5755 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5758 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5761 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5762 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++) {
5763 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5764 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
5769 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5772 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5775 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5776 struct target
*target
= all_targets
;
5778 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5779 Jim_NewStringObj(interp
, target_name(target
), -1));
5780 target
= target
->next
;
5785 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5788 const char *targetname
;
5790 struct target
*target
= (struct target
*) NULL
;
5791 struct target_list
*head
, *curr
, *new;
5792 curr
= (struct target_list
*) NULL
;
5793 head
= (struct target_list
*) NULL
;
5796 LOG_DEBUG("%d", argc
);
5797 /* argv[1] = target to associate in smp
5798 * argv[2] = target to assoicate in smp
5802 for (i
= 1; i
< argc
; i
++) {
5804 targetname
= Jim_GetString(argv
[i
], &len
);
5805 target
= get_target(targetname
);
5806 LOG_DEBUG("%s ", targetname
);
5808 new = malloc(sizeof(struct target_list
));
5809 new->target
= target
;
5810 new->next
= (struct target_list
*)NULL
;
5811 if (head
== (struct target_list
*)NULL
) {
5820 /* now parse the list of cpu and put the target in smp mode*/
5823 while (curr
!= (struct target_list
*)NULL
) {
5824 target
= curr
->target
;
5826 target
->head
= head
;
5830 if (target
&& target
->rtos
)
5831 retval
= rtos_smp_init(head
->target
);
5837 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5840 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5842 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5843 "<name> <target_type> [<target_options> ...]");
5846 return target_create(&goi
);
5849 static const struct command_registration target_subcommand_handlers
[] = {
5852 .mode
= COMMAND_CONFIG
,
5853 .handler
= handle_target_init_command
,
5854 .help
= "initialize targets",
5858 /* REVISIT this should be COMMAND_CONFIG ... */
5859 .mode
= COMMAND_ANY
,
5860 .jim_handler
= jim_target_create
,
5861 .usage
= "name type '-chain-position' name [options ...]",
5862 .help
= "Creates and selects a new target",
5866 .mode
= COMMAND_ANY
,
5867 .jim_handler
= jim_target_current
,
5868 .help
= "Returns the currently selected target",
5872 .mode
= COMMAND_ANY
,
5873 .jim_handler
= jim_target_types
,
5874 .help
= "Returns the available target types as "
5875 "a list of strings",
5879 .mode
= COMMAND_ANY
,
5880 .jim_handler
= jim_target_names
,
5881 .help
= "Returns the names of all targets as a list of strings",
5885 .mode
= COMMAND_ANY
,
5886 .jim_handler
= jim_target_smp
,
5887 .usage
= "targetname1 targetname2 ...",
5888 .help
= "gather several target in a smp list"
5891 COMMAND_REGISTRATION_DONE
5895 target_addr_t address
;
5901 static int fastload_num
;
5902 static struct FastLoad
*fastload
;
5904 static void free_fastload(void)
5906 if (fastload
!= NULL
) {
5908 for (i
= 0; i
< fastload_num
; i
++) {
5909 if (fastload
[i
].data
)
5910 free(fastload
[i
].data
);
5917 COMMAND_HANDLER(handle_fast_load_image_command
)
5921 uint32_t image_size
;
5922 target_addr_t min_address
= 0;
5923 target_addr_t max_address
= -1;
5928 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
5929 &image
, &min_address
, &max_address
);
5930 if (ERROR_OK
!= retval
)
5933 struct duration bench
;
5934 duration_start(&bench
);
5936 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5937 if (retval
!= ERROR_OK
)
5942 fastload_num
= image
.num_sections
;
5943 fastload
= malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5944 if (fastload
== NULL
) {
5945 command_print(CMD_CTX
, "out of memory");
5946 image_close(&image
);
5949 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5950 for (i
= 0; i
< image
.num_sections
; i
++) {
5951 buffer
= malloc(image
.sections
[i
].size
);
5952 if (buffer
== NULL
) {
5953 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5954 (int)(image
.sections
[i
].size
));
5955 retval
= ERROR_FAIL
;
5959 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
5960 if (retval
!= ERROR_OK
) {
5965 uint32_t offset
= 0;
5966 uint32_t length
= buf_cnt
;
5968 /* DANGER!!! beware of unsigned comparision here!!! */
5970 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
5971 (image
.sections
[i
].base_address
< max_address
)) {
5972 if (image
.sections
[i
].base_address
< min_address
) {
5973 /* clip addresses below */
5974 offset
+= min_address
-image
.sections
[i
].base_address
;
5978 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5979 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5981 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5982 fastload
[i
].data
= malloc(length
);
5983 if (fastload
[i
].data
== NULL
) {
5985 command_print(CMD_CTX
, "error allocating buffer for section (%" PRIu32
" bytes)",
5987 retval
= ERROR_FAIL
;
5990 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5991 fastload
[i
].length
= length
;
5993 image_size
+= length
;
5994 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
5995 (unsigned int)length
,
5996 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
6002 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
6003 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
6004 "in %fs (%0.3f KiB/s)", image_size
,
6005 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
6007 command_print(CMD_CTX
,
6008 "WARNING: image has not been loaded to target!"
6009 "You can issue a 'fast_load' to finish loading.");
6012 image_close(&image
);
6014 if (retval
!= ERROR_OK
)
6020 COMMAND_HANDLER(handle_fast_load_command
)
6023 return ERROR_COMMAND_SYNTAX_ERROR
;
6024 if (fastload
== NULL
) {
6025 LOG_ERROR("No image in memory");
6029 int64_t ms
= timeval_ms();
6031 int retval
= ERROR_OK
;
6032 for (i
= 0; i
< fastload_num
; i
++) {
6033 struct target
*target
= get_current_target(CMD_CTX
);
6034 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
6035 (unsigned int)(fastload
[i
].address
),
6036 (unsigned int)(fastload
[i
].length
));
6037 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
6038 if (retval
!= ERROR_OK
)
6040 size
+= fastload
[i
].length
;
6042 if (retval
== ERROR_OK
) {
6043 int64_t after
= timeval_ms();
6044 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
6049 static const struct command_registration target_command_handlers
[] = {
6052 .handler
= handle_targets_command
,
6053 .mode
= COMMAND_ANY
,
6054 .help
= "change current default target (one parameter) "
6055 "or prints table of all targets (no parameters)",
6056 .usage
= "[target]",
6060 .mode
= COMMAND_CONFIG
,
6061 .help
= "configure target",
6063 .chain
= target_subcommand_handlers
,
6065 COMMAND_REGISTRATION_DONE
6068 int target_register_commands(struct command_context
*cmd_ctx
)
6070 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
6073 static bool target_reset_nag
= true;
6075 bool get_target_reset_nag(void)
6077 return target_reset_nag
;
6080 COMMAND_HANDLER(handle_target_reset_nag
)
6082 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
6083 &target_reset_nag
, "Nag after each reset about options to improve "
6087 COMMAND_HANDLER(handle_ps_command
)
6089 struct target
*target
= get_current_target(CMD_CTX
);
6091 if (target
->state
!= TARGET_HALTED
) {
6092 LOG_INFO("target not halted !!");
6096 if ((target
->rtos
) && (target
->rtos
->type
)
6097 && (target
->rtos
->type
->ps_command
)) {
6098 display
= target
->rtos
->type
->ps_command(target
);
6099 command_print(CMD_CTX
, "%s", display
);
6104 return ERROR_TARGET_FAILURE
;
6108 static void binprint(struct command_context
*cmd_ctx
, const char *text
, const uint8_t *buf
, int size
)
6111 command_print_sameline(cmd_ctx
, "%s", text
);
6112 for (int i
= 0; i
< size
; i
++)
6113 command_print_sameline(cmd_ctx
, " %02x", buf
[i
]);
6114 command_print(cmd_ctx
, " ");
6117 COMMAND_HANDLER(handle_test_mem_access_command
)
6119 struct target
*target
= get_current_target(CMD_CTX
);
6121 int retval
= ERROR_OK
;
6123 if (target
->state
!= TARGET_HALTED
) {
6124 LOG_INFO("target not halted !!");
6129 return ERROR_COMMAND_SYNTAX_ERROR
;
6131 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], test_size
);
6134 size_t num_bytes
= test_size
+ 4;
6136 struct working_area
*wa
= NULL
;
6137 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6138 if (retval
!= ERROR_OK
) {
6139 LOG_ERROR("Not enough working area");
6143 uint8_t *test_pattern
= malloc(num_bytes
);
6145 for (size_t i
= 0; i
< num_bytes
; i
++)
6146 test_pattern
[i
] = rand();
6148 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6149 if (retval
!= ERROR_OK
) {
6150 LOG_ERROR("Test pattern write failed");
6154 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6155 for (int size
= 1; size
<= 4; size
*= 2) {
6156 for (int offset
= 0; offset
< 4; offset
++) {
6157 uint32_t count
= test_size
/ size
;
6158 size_t host_bufsiz
= (count
+ 2) * size
+ host_offset
;
6159 uint8_t *read_ref
= malloc(host_bufsiz
);
6160 uint8_t *read_buf
= malloc(host_bufsiz
);
6162 for (size_t i
= 0; i
< host_bufsiz
; i
++) {
6163 read_ref
[i
] = rand();
6164 read_buf
[i
] = read_ref
[i
];
6166 command_print_sameline(CMD_CTX
,
6167 "Test read %" PRIu32
" x %d @ %d to %saligned buffer: ", count
,
6168 size
, offset
, host_offset
? "un" : "");
6170 struct duration bench
;
6171 duration_start(&bench
);
6173 retval
= target_read_memory(target
, wa
->address
+ offset
, size
, count
,
6174 read_buf
+ size
+ host_offset
);
6176 duration_measure(&bench
);
6178 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6179 command_print(CMD_CTX
, "Unsupported alignment");
6181 } else if (retval
!= ERROR_OK
) {
6182 command_print(CMD_CTX
, "Memory read failed");
6186 /* replay on host */
6187 memcpy(read_ref
+ size
+ host_offset
, test_pattern
+ offset
, count
* size
);
6190 int result
= memcmp(read_ref
, read_buf
, host_bufsiz
);
6192 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
6193 duration_elapsed(&bench
),
6194 duration_kbps(&bench
, count
* size
));
6196 command_print(CMD_CTX
, "Compare failed");
6197 binprint(CMD_CTX
, "ref:", read_ref
, host_bufsiz
);
6198 binprint(CMD_CTX
, "buf:", read_buf
, host_bufsiz
);
6211 target_free_working_area(target
, wa
);
6214 num_bytes
= test_size
+ 4 + 4 + 4;
6216 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6217 if (retval
!= ERROR_OK
) {
6218 LOG_ERROR("Not enough working area");
6222 test_pattern
= malloc(num_bytes
);
6224 for (size_t i
= 0; i
< num_bytes
; i
++)
6225 test_pattern
[i
] = rand();
6227 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6228 for (int size
= 1; size
<= 4; size
*= 2) {
6229 for (int offset
= 0; offset
< 4; offset
++) {
6230 uint32_t count
= test_size
/ size
;
6231 size_t host_bufsiz
= count
* size
+ host_offset
;
6232 uint8_t *read_ref
= malloc(num_bytes
);
6233 uint8_t *read_buf
= malloc(num_bytes
);
6234 uint8_t *write_buf
= malloc(host_bufsiz
);
6236 for (size_t i
= 0; i
< host_bufsiz
; i
++)
6237 write_buf
[i
] = rand();
6238 command_print_sameline(CMD_CTX
,
6239 "Test write %" PRIu32
" x %d @ %d from %saligned buffer: ", count
,
6240 size
, offset
, host_offset
? "un" : "");
6242 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6243 if (retval
!= ERROR_OK
) {
6244 command_print(CMD_CTX
, "Test pattern write failed");
6248 /* replay on host */
6249 memcpy(read_ref
, test_pattern
, num_bytes
);
6250 memcpy(read_ref
+ size
+ offset
, write_buf
+ host_offset
, count
* size
);
6252 struct duration bench
;
6253 duration_start(&bench
);
6255 retval
= target_write_memory(target
, wa
->address
+ size
+ offset
, size
, count
,
6256 write_buf
+ host_offset
);
6258 duration_measure(&bench
);
6260 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6261 command_print(CMD_CTX
, "Unsupported alignment");
6263 } else if (retval
!= ERROR_OK
) {
6264 command_print(CMD_CTX
, "Memory write failed");
6269 retval
= target_read_memory(target
, wa
->address
, 1, num_bytes
, read_buf
);
6270 if (retval
!= ERROR_OK
) {
6271 command_print(CMD_CTX
, "Test pattern write failed");
6276 int result
= memcmp(read_ref
, read_buf
, num_bytes
);
6278 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
6279 duration_elapsed(&bench
),
6280 duration_kbps(&bench
, count
* size
));
6282 command_print(CMD_CTX
, "Compare failed");
6283 binprint(CMD_CTX
, "ref:", read_ref
, num_bytes
);
6284 binprint(CMD_CTX
, "buf:", read_buf
, num_bytes
);
6296 target_free_working_area(target
, wa
);
6300 static const struct command_registration target_exec_command_handlers
[] = {
6302 .name
= "fast_load_image",
6303 .handler
= handle_fast_load_image_command
,
6304 .mode
= COMMAND_ANY
,
6305 .help
= "Load image into server memory for later use by "
6306 "fast_load; primarily for profiling",
6307 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6308 "[min_address [max_length]]",
6311 .name
= "fast_load",
6312 .handler
= handle_fast_load_command
,
6313 .mode
= COMMAND_EXEC
,
6314 .help
= "loads active fast load image to current target "
6315 "- mainly for profiling purposes",
6320 .handler
= handle_profile_command
,
6321 .mode
= COMMAND_EXEC
,
6322 .usage
= "seconds filename [start end]",
6323 .help
= "profiling samples the CPU PC",
6325 /** @todo don't register virt2phys() unless target supports it */
6327 .name
= "virt2phys",
6328 .handler
= handle_virt2phys_command
,
6329 .mode
= COMMAND_ANY
,
6330 .help
= "translate a virtual address into a physical address",
6331 .usage
= "virtual_address",
6335 .handler
= handle_reg_command
,
6336 .mode
= COMMAND_EXEC
,
6337 .help
= "display (reread from target with \"force\") or set a register; "
6338 "with no arguments, displays all registers and their values",
6339 .usage
= "[(register_number|register_name) [(value|'force')]]",
6343 .handler
= handle_poll_command
,
6344 .mode
= COMMAND_EXEC
,
6345 .help
= "poll target state; or reconfigure background polling",
6346 .usage
= "['on'|'off']",
6349 .name
= "wait_halt",
6350 .handler
= handle_wait_halt_command
,
6351 .mode
= COMMAND_EXEC
,
6352 .help
= "wait up to the specified number of milliseconds "
6353 "(default 5000) for a previously requested halt",
6354 .usage
= "[milliseconds]",
6358 .handler
= handle_halt_command
,
6359 .mode
= COMMAND_EXEC
,
6360 .help
= "request target to halt, then wait up to the specified"
6361 "number of milliseconds (default 5000) for it to complete",
6362 .usage
= "[milliseconds]",
6366 .handler
= handle_resume_command
,
6367 .mode
= COMMAND_EXEC
,
6368 .help
= "resume target execution from current PC or address",
6369 .usage
= "[address]",
6373 .handler
= handle_reset_command
,
6374 .mode
= COMMAND_EXEC
,
6375 .usage
= "[run|halt|init]",
6376 .help
= "Reset all targets into the specified mode."
6377 "Default reset mode is run, if not given.",
6380 .name
= "soft_reset_halt",
6381 .handler
= handle_soft_reset_halt_command
,
6382 .mode
= COMMAND_EXEC
,
6384 .help
= "halt the target and do a soft reset",
6388 .handler
= handle_step_command
,
6389 .mode
= COMMAND_EXEC
,
6390 .help
= "step one instruction from current PC or address",
6391 .usage
= "[address]",
6395 .handler
= handle_md_command
,
6396 .mode
= COMMAND_EXEC
,
6397 .help
= "display memory words",
6398 .usage
= "['phys'] address [count]",
6402 .handler
= handle_md_command
,
6403 .mode
= COMMAND_EXEC
,
6404 .help
= "display memory words",
6405 .usage
= "['phys'] address [count]",
6409 .handler
= handle_md_command
,
6410 .mode
= COMMAND_EXEC
,
6411 .help
= "display memory half-words",
6412 .usage
= "['phys'] address [count]",
6416 .handler
= handle_md_command
,
6417 .mode
= COMMAND_EXEC
,
6418 .help
= "display memory bytes",
6419 .usage
= "['phys'] address [count]",
6423 .handler
= handle_mw_command
,
6424 .mode
= COMMAND_EXEC
,
6425 .help
= "write memory word",
6426 .usage
= "['phys'] address value [count]",
6430 .handler
= handle_mw_command
,
6431 .mode
= COMMAND_EXEC
,
6432 .help
= "write memory word",
6433 .usage
= "['phys'] address value [count]",
6437 .handler
= handle_mw_command
,
6438 .mode
= COMMAND_EXEC
,
6439 .help
= "write memory half-word",
6440 .usage
= "['phys'] address value [count]",
6444 .handler
= handle_mw_command
,
6445 .mode
= COMMAND_EXEC
,
6446 .help
= "write memory byte",
6447 .usage
= "['phys'] address value [count]",
6451 .handler
= handle_bp_command
,
6452 .mode
= COMMAND_EXEC
,
6453 .help
= "list or set hardware or software breakpoint",
6454 .usage
= "<address> [<asid>] <length> ['hw'|'hw_ctx']",
6458 .handler
= handle_rbp_command
,
6459 .mode
= COMMAND_EXEC
,
6460 .help
= "remove breakpoint",
6465 .handler
= handle_wp_command
,
6466 .mode
= COMMAND_EXEC
,
6467 .help
= "list (no params) or create watchpoints",
6468 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
6472 .handler
= handle_rwp_command
,
6473 .mode
= COMMAND_EXEC
,
6474 .help
= "remove watchpoint",
6478 .name
= "load_image",
6479 .handler
= handle_load_image_command
,
6480 .mode
= COMMAND_EXEC
,
6481 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6482 "[min_address] [max_length]",
6485 .name
= "dump_image",
6486 .handler
= handle_dump_image_command
,
6487 .mode
= COMMAND_EXEC
,
6488 .usage
= "filename address size",
6491 .name
= "verify_image_checksum",
6492 .handler
= handle_verify_image_checksum_command
,
6493 .mode
= COMMAND_EXEC
,
6494 .usage
= "filename [offset [type]]",
6497 .name
= "verify_image",
6498 .handler
= handle_verify_image_command
,
6499 .mode
= COMMAND_EXEC
,
6500 .usage
= "filename [offset [type]]",
6503 .name
= "test_image",
6504 .handler
= handle_test_image_command
,
6505 .mode
= COMMAND_EXEC
,
6506 .usage
= "filename [offset [type]]",
6509 .name
= "mem2array",
6510 .mode
= COMMAND_EXEC
,
6511 .jim_handler
= jim_mem2array
,
6512 .help
= "read 8/16/32 bit memory and return as a TCL array "
6513 "for script processing",
6514 .usage
= "arrayname bitwidth address count",
6517 .name
= "array2mem",
6518 .mode
= COMMAND_EXEC
,
6519 .jim_handler
= jim_array2mem
,
6520 .help
= "convert a TCL array to memory locations "
6521 "and write the 8/16/32 bit values",
6522 .usage
= "arrayname bitwidth address count",
6525 .name
= "reset_nag",
6526 .handler
= handle_target_reset_nag
,
6527 .mode
= COMMAND_ANY
,
6528 .help
= "Nag after each reset about options that could have been "
6529 "enabled to improve performance. ",
6530 .usage
= "['enable'|'disable']",
6534 .handler
= handle_ps_command
,
6535 .mode
= COMMAND_EXEC
,
6536 .help
= "list all tasks ",
6540 .name
= "test_mem_access",
6541 .handler
= handle_test_mem_access_command
,
6542 .mode
= COMMAND_EXEC
,
6543 .help
= "Test the target's memory access functions",
6547 COMMAND_REGISTRATION_DONE
6549 static int target_register_user_commands(struct command_context
*cmd_ctx
)
6551 int retval
= ERROR_OK
;
6552 retval
= target_request_register_commands(cmd_ctx
);
6553 if (retval
!= ERROR_OK
)
6556 retval
= trace_register_commands(cmd_ctx
);
6557 if (retval
!= ERROR_OK
)
6561 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);