1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
17 * Copyright (C) 2011 by Broadcom Corporation *
18 * Evan Hunter - ehunter@broadcom.com *
20 * Copyright (C) ST-Ericsson SA 2011 *
21 * michel.jaouen@stericsson.com : smp minimum support *
23 * Copyright (C) 2011 Andreas Fritiofson *
24 * andreas.fritiofson@gmail.com *
26 * This program is free software; you can redistribute it and/or modify *
27 * it under the terms of the GNU General Public License as published by *
28 * the Free Software Foundation; either version 2 of the License, or *
29 * (at your option) any later version. *
31 * This program is distributed in the hope that it will be useful, *
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
34 * GNU General Public License for more details. *
36 * You should have received a copy of the GNU General Public License *
37 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
38 ***************************************************************************/
44 #include <helper/time_support.h>
45 #include <jtag/jtag.h>
46 #include <flash/nor/core.h>
49 #include "target_type.h"
50 #include "target_request.h"
51 #include "breakpoints.h"
55 #include "rtos/rtos.h"
56 #include "transport/transport.h"
59 /* default halt wait timeout (ms) */
60 #define DEFAULT_HALT_TIMEOUT 5000
62 static int target_read_buffer_default(struct target
*target
, target_addr_t address
,
63 uint32_t count
, uint8_t *buffer
);
64 static int target_write_buffer_default(struct target
*target
, target_addr_t address
,
65 uint32_t count
, const uint8_t *buffer
);
66 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
67 int argc
, Jim_Obj
* const *argv
);
68 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
69 int argc
, Jim_Obj
* const *argv
);
70 static int target_register_user_commands(struct command_context
*cmd_ctx
);
71 static int target_get_gdb_fileio_info_default(struct target
*target
,
72 struct gdb_fileio_info
*fileio_info
);
73 static int target_gdb_fileio_end_default(struct target
*target
, int retcode
,
74 int fileio_errno
, bool ctrl_c
);
75 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
76 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
);
79 extern struct target_type arm7tdmi_target
;
80 extern struct target_type arm720t_target
;
81 extern struct target_type arm9tdmi_target
;
82 extern struct target_type arm920t_target
;
83 extern struct target_type arm966e_target
;
84 extern struct target_type arm946e_target
;
85 extern struct target_type arm926ejs_target
;
86 extern struct target_type fa526_target
;
87 extern struct target_type feroceon_target
;
88 extern struct target_type dragonite_target
;
89 extern struct target_type xscale_target
;
90 extern struct target_type cortexm_target
;
91 extern struct target_type cortexa_target
;
92 extern struct target_type aarch64_target
;
93 extern struct target_type cortexr4_target
;
94 extern struct target_type arm11_target
;
95 extern struct target_type ls1_sap_target
;
96 extern struct target_type mips_m4k_target
;
97 extern struct target_type avr_target
;
98 extern struct target_type dsp563xx_target
;
99 extern struct target_type dsp5680xx_target
;
100 extern struct target_type testee_target
;
101 extern struct target_type avr32_ap7k_target
;
102 extern struct target_type hla_target
;
103 extern struct target_type nds32_v2_target
;
104 extern struct target_type nds32_v3_target
;
105 extern struct target_type nds32_v3m_target
;
106 extern struct target_type or1k_target
;
107 extern struct target_type quark_x10xx_target
;
108 extern struct target_type quark_d20xx_target
;
109 extern struct target_type stm8_target
;
110 extern struct target_type riscv_target
;
111 extern struct target_type mem_ap_target
;
112 extern struct target_type esirisc_target
;
114 static struct target_type
*target_types
[] = {
154 struct target
*all_targets
;
155 static struct target_event_callback
*target_event_callbacks
;
156 static struct target_timer_callback
*target_timer_callbacks
;
157 LIST_HEAD(target_reset_callback_list
);
158 LIST_HEAD(target_trace_callback_list
);
159 static const int polling_interval
= 100;
161 static const Jim_Nvp nvp_assert
[] = {
162 { .name
= "assert", NVP_ASSERT
},
163 { .name
= "deassert", NVP_DEASSERT
},
164 { .name
= "T", NVP_ASSERT
},
165 { .name
= "F", NVP_DEASSERT
},
166 { .name
= "t", NVP_ASSERT
},
167 { .name
= "f", NVP_DEASSERT
},
168 { .name
= NULL
, .value
= -1 }
171 static const Jim_Nvp nvp_error_target
[] = {
172 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
173 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
174 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
175 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
176 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
177 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
178 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
179 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
180 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
181 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
182 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
183 { .value
= -1, .name
= NULL
}
186 static const char *target_strerror_safe(int err
)
190 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
197 static const Jim_Nvp nvp_target_event
[] = {
199 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
200 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
201 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
202 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
203 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
205 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
206 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
208 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
209 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
210 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
211 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
212 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
213 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
214 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
215 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
217 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
218 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
220 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
221 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
223 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
224 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
226 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
227 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
229 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
230 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
232 { .value
= TARGET_EVENT_TRACE_CONFIG
, .name
= "trace-config" },
234 { .name
= NULL
, .value
= -1 }
237 static const Jim_Nvp nvp_target_state
[] = {
238 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
239 { .name
= "running", .value
= TARGET_RUNNING
},
240 { .name
= "halted", .value
= TARGET_HALTED
},
241 { .name
= "reset", .value
= TARGET_RESET
},
242 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
243 { .name
= NULL
, .value
= -1 },
246 static const Jim_Nvp nvp_target_debug_reason
[] = {
247 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
248 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
249 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
250 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
251 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
252 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
253 { .name
= "program-exit" , .value
= DBG_REASON_EXIT
},
254 { .name
= "exception-catch" , .value
= DBG_REASON_EXC_CATCH
},
255 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
256 { .name
= NULL
, .value
= -1 },
259 static const Jim_Nvp nvp_target_endian
[] = {
260 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
261 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
262 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
263 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
264 { .name
= NULL
, .value
= -1 },
267 static const Jim_Nvp nvp_reset_modes
[] = {
268 { .name
= "unknown", .value
= RESET_UNKNOWN
},
269 { .name
= "run" , .value
= RESET_RUN
},
270 { .name
= "halt" , .value
= RESET_HALT
},
271 { .name
= "init" , .value
= RESET_INIT
},
272 { .name
= NULL
, .value
= -1 },
275 const char *debug_reason_name(struct target
*t
)
279 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
280 t
->debug_reason
)->name
;
282 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
283 cp
= "(*BUG*unknown*BUG*)";
288 const char *target_state_name(struct target
*t
)
291 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
293 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
294 cp
= "(*BUG*unknown*BUG*)";
297 if (!target_was_examined(t
) && t
->defer_examine
)
298 cp
= "examine deferred";
303 const char *target_event_name(enum target_event event
)
306 cp
= Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
;
308 LOG_ERROR("Invalid target event: %d", (int)(event
));
309 cp
= "(*BUG*unknown*BUG*)";
314 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
317 cp
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
;
319 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
320 cp
= "(*BUG*unknown*BUG*)";
325 /* determine the number of the new target */
326 static int new_target_number(void)
331 /* number is 0 based */
335 if (x
< t
->target_number
)
336 x
= t
->target_number
;
342 /* read a uint64_t from a buffer in target memory endianness */
343 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
345 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
346 return le_to_h_u64(buffer
);
348 return be_to_h_u64(buffer
);
351 /* read a uint32_t from a buffer in target memory endianness */
352 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
354 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
355 return le_to_h_u32(buffer
);
357 return be_to_h_u32(buffer
);
360 /* read a uint24_t from a buffer in target memory endianness */
361 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
363 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
364 return le_to_h_u24(buffer
);
366 return be_to_h_u24(buffer
);
369 /* read a uint16_t from a buffer in target memory endianness */
370 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
372 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
373 return le_to_h_u16(buffer
);
375 return be_to_h_u16(buffer
);
378 /* read a uint8_t from a buffer in target memory endianness */
379 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
381 return *buffer
& 0x0ff;
384 /* write a uint64_t to a buffer in target memory endianness */
385 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
387 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
388 h_u64_to_le(buffer
, value
);
390 h_u64_to_be(buffer
, value
);
393 /* write a uint32_t to a buffer in target memory endianness */
394 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
396 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
397 h_u32_to_le(buffer
, value
);
399 h_u32_to_be(buffer
, value
);
402 /* write a uint24_t to a buffer in target memory endianness */
403 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
405 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
406 h_u24_to_le(buffer
, value
);
408 h_u24_to_be(buffer
, value
);
411 /* write a uint16_t to a buffer in target memory endianness */
412 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
414 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
415 h_u16_to_le(buffer
, value
);
417 h_u16_to_be(buffer
, value
);
420 /* write a uint8_t to a buffer in target memory endianness */
421 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
426 /* write a uint64_t array to a buffer in target memory endianness */
427 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
430 for (i
= 0; i
< count
; i
++)
431 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
434 /* write a uint32_t array to a buffer in target memory endianness */
435 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
438 for (i
= 0; i
< count
; i
++)
439 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
442 /* write a uint16_t array to a buffer in target memory endianness */
443 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
446 for (i
= 0; i
< count
; i
++)
447 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
450 /* write a uint64_t array to a buffer in target memory endianness */
451 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
454 for (i
= 0; i
< count
; i
++)
455 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
458 /* write a uint32_t array to a buffer in target memory endianness */
459 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
462 for (i
= 0; i
< count
; i
++)
463 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
466 /* write a uint16_t array to a buffer in target memory endianness */
467 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
470 for (i
= 0; i
< count
; i
++)
471 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
474 /* return a pointer to a configured target; id is name or number */
475 struct target
*get_target(const char *id
)
477 struct target
*target
;
479 /* try as tcltarget name */
480 for (target
= all_targets
; target
; target
= target
->next
) {
481 if (target_name(target
) == NULL
)
483 if (strcmp(id
, target_name(target
)) == 0)
487 /* It's OK to remove this fallback sometime after August 2010 or so */
489 /* no match, try as number */
491 if (parse_uint(id
, &num
) != ERROR_OK
)
494 for (target
= all_targets
; target
; target
= target
->next
) {
495 if (target
->target_number
== (int)num
) {
496 LOG_WARNING("use '%s' as target identifier, not '%u'",
497 target_name(target
), num
);
505 /* returns a pointer to the n-th configured target */
506 struct target
*get_target_by_num(int num
)
508 struct target
*target
= all_targets
;
511 if (target
->target_number
== num
)
513 target
= target
->next
;
519 struct target
*get_current_target(struct command_context
*cmd_ctx
)
521 struct target
*target
= get_current_target_or_null(cmd_ctx
);
523 if (target
== NULL
) {
524 LOG_ERROR("BUG: current_target out of bounds");
531 struct target
*get_current_target_or_null(struct command_context
*cmd_ctx
)
533 return cmd_ctx
->current_target_override
534 ? cmd_ctx
->current_target_override
535 : cmd_ctx
->current_target
;
538 int target_poll(struct target
*target
)
542 /* We can't poll until after examine */
543 if (!target_was_examined(target
)) {
544 /* Fail silently lest we pollute the log */
548 retval
= target
->type
->poll(target
);
549 if (retval
!= ERROR_OK
)
552 if (target
->halt_issued
) {
553 if (target
->state
== TARGET_HALTED
)
554 target
->halt_issued
= false;
556 int64_t t
= timeval_ms() - target
->halt_issued_time
;
557 if (t
> DEFAULT_HALT_TIMEOUT
) {
558 target
->halt_issued
= false;
559 LOG_INFO("Halt timed out, wake up GDB.");
560 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
568 int target_halt(struct target
*target
)
571 /* We can't poll until after examine */
572 if (!target_was_examined(target
)) {
573 LOG_ERROR("Target not examined yet");
577 retval
= target
->type
->halt(target
);
578 if (retval
!= ERROR_OK
)
581 target
->halt_issued
= true;
582 target
->halt_issued_time
= timeval_ms();
588 * Make the target (re)start executing using its saved execution
589 * context (possibly with some modifications).
591 * @param target Which target should start executing.
592 * @param current True to use the target's saved program counter instead
593 * of the address parameter
594 * @param address Optionally used as the program counter.
595 * @param handle_breakpoints True iff breakpoints at the resumption PC
596 * should be skipped. (For example, maybe execution was stopped by
597 * such a breakpoint, in which case it would be counterprodutive to
599 * @param debug_execution False if all working areas allocated by OpenOCD
600 * should be released and/or restored to their original contents.
601 * (This would for example be true to run some downloaded "helper"
602 * algorithm code, which resides in one such working buffer and uses
603 * another for data storage.)
605 * @todo Resolve the ambiguity about what the "debug_execution" flag
606 * signifies. For example, Target implementations don't agree on how
607 * it relates to invalidation of the register cache, or to whether
608 * breakpoints and watchpoints should be enabled. (It would seem wrong
609 * to enable breakpoints when running downloaded "helper" algorithms
610 * (debug_execution true), since the breakpoints would be set to match
611 * target firmware being debugged, not the helper algorithm.... and
612 * enabling them could cause such helpers to malfunction (for example,
613 * by overwriting data with a breakpoint instruction. On the other
614 * hand the infrastructure for running such helpers might use this
615 * procedure but rely on hardware breakpoint to detect termination.)
617 int target_resume(struct target
*target
, int current
, target_addr_t address
,
618 int handle_breakpoints
, int debug_execution
)
622 /* We can't poll until after examine */
623 if (!target_was_examined(target
)) {
624 LOG_ERROR("Target not examined yet");
628 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
630 /* note that resume *must* be asynchronous. The CPU can halt before
631 * we poll. The CPU can even halt at the current PC as a result of
632 * a software breakpoint being inserted by (a bug?) the application.
634 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
635 if (retval
!= ERROR_OK
)
638 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
643 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
648 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
649 if (n
->name
== NULL
) {
650 LOG_ERROR("invalid reset mode");
654 struct target
*target
;
655 for (target
= all_targets
; target
; target
= target
->next
)
656 target_call_reset_callbacks(target
, reset_mode
);
658 /* disable polling during reset to make reset event scripts
659 * more predictable, i.e. dr/irscan & pathmove in events will
660 * not have JTAG operations injected into the middle of a sequence.
662 bool save_poll
= jtag_poll_get_enabled();
664 jtag_poll_set_enabled(false);
666 sprintf(buf
, "ocd_process_reset %s", n
->name
);
667 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
669 jtag_poll_set_enabled(save_poll
);
671 if (retval
!= JIM_OK
) {
672 Jim_MakeErrorMessage(cmd_ctx
->interp
);
673 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
677 /* We want any events to be processed before the prompt */
678 retval
= target_call_timer_callbacks_now();
680 for (target
= all_targets
; target
; target
= target
->next
) {
681 target
->type
->check_reset(target
);
682 target
->running_alg
= false;
688 static int identity_virt2phys(struct target
*target
,
689 target_addr_t
virtual, target_addr_t
*physical
)
695 static int no_mmu(struct target
*target
, int *enabled
)
701 static int default_examine(struct target
*target
)
703 target_set_examined(target
);
707 /* no check by default */
708 static int default_check_reset(struct target
*target
)
713 int target_examine_one(struct target
*target
)
715 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
717 int retval
= target
->type
->examine(target
);
718 if (retval
!= ERROR_OK
)
721 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
726 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
728 struct target
*target
= priv
;
730 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
733 jtag_unregister_event_callback(jtag_enable_callback
, target
);
735 return target_examine_one(target
);
738 /* Targets that correctly implement init + examine, i.e.
739 * no communication with target during init:
743 int target_examine(void)
745 int retval
= ERROR_OK
;
746 struct target
*target
;
748 for (target
= all_targets
; target
; target
= target
->next
) {
749 /* defer examination, but don't skip it */
750 if (!target
->tap
->enabled
) {
751 jtag_register_event_callback(jtag_enable_callback
,
756 if (target
->defer_examine
)
759 retval
= target_examine_one(target
);
760 if (retval
!= ERROR_OK
)
766 const char *target_type_name(struct target
*target
)
768 return target
->type
->name
;
771 static int target_soft_reset_halt(struct target
*target
)
773 if (!target_was_examined(target
)) {
774 LOG_ERROR("Target not examined yet");
777 if (!target
->type
->soft_reset_halt
) {
778 LOG_ERROR("Target %s does not support soft_reset_halt",
779 target_name(target
));
782 return target
->type
->soft_reset_halt(target
);
786 * Downloads a target-specific native code algorithm to the target,
787 * and executes it. * Note that some targets may need to set up, enable,
788 * and tear down a breakpoint (hard or * soft) to detect algorithm
789 * termination, while others may support lower overhead schemes where
790 * soft breakpoints embedded in the algorithm automatically terminate the
793 * @param target used to run the algorithm
794 * @param arch_info target-specific description of the algorithm.
796 int target_run_algorithm(struct target
*target
,
797 int num_mem_params
, struct mem_param
*mem_params
,
798 int num_reg_params
, struct reg_param
*reg_param
,
799 uint32_t entry_point
, uint32_t exit_point
,
800 int timeout_ms
, void *arch_info
)
802 int retval
= ERROR_FAIL
;
804 if (!target_was_examined(target
)) {
805 LOG_ERROR("Target not examined yet");
808 if (!target
->type
->run_algorithm
) {
809 LOG_ERROR("Target type '%s' does not support %s",
810 target_type_name(target
), __func__
);
814 target
->running_alg
= true;
815 retval
= target
->type
->run_algorithm(target
,
816 num_mem_params
, mem_params
,
817 num_reg_params
, reg_param
,
818 entry_point
, exit_point
, timeout_ms
, arch_info
);
819 target
->running_alg
= false;
826 * Executes a target-specific native code algorithm and leaves it running.
828 * @param target used to run the algorithm
829 * @param arch_info target-specific description of the algorithm.
831 int target_start_algorithm(struct target
*target
,
832 int num_mem_params
, struct mem_param
*mem_params
,
833 int num_reg_params
, struct reg_param
*reg_params
,
834 uint32_t entry_point
, uint32_t exit_point
,
837 int retval
= ERROR_FAIL
;
839 if (!target_was_examined(target
)) {
840 LOG_ERROR("Target not examined yet");
843 if (!target
->type
->start_algorithm
) {
844 LOG_ERROR("Target type '%s' does not support %s",
845 target_type_name(target
), __func__
);
848 if (target
->running_alg
) {
849 LOG_ERROR("Target is already running an algorithm");
853 target
->running_alg
= true;
854 retval
= target
->type
->start_algorithm(target
,
855 num_mem_params
, mem_params
,
856 num_reg_params
, reg_params
,
857 entry_point
, exit_point
, arch_info
);
864 * Waits for an algorithm started with target_start_algorithm() to complete.
866 * @param target used to run the algorithm
867 * @param arch_info target-specific description of the algorithm.
869 int target_wait_algorithm(struct target
*target
,
870 int num_mem_params
, struct mem_param
*mem_params
,
871 int num_reg_params
, struct reg_param
*reg_params
,
872 uint32_t exit_point
, int timeout_ms
,
875 int retval
= ERROR_FAIL
;
877 if (!target
->type
->wait_algorithm
) {
878 LOG_ERROR("Target type '%s' does not support %s",
879 target_type_name(target
), __func__
);
882 if (!target
->running_alg
) {
883 LOG_ERROR("Target is not running an algorithm");
887 retval
= target
->type
->wait_algorithm(target
,
888 num_mem_params
, mem_params
,
889 num_reg_params
, reg_params
,
890 exit_point
, timeout_ms
, arch_info
);
891 if (retval
!= ERROR_TARGET_TIMEOUT
)
892 target
->running_alg
= false;
899 * Streams data to a circular buffer on target intended for consumption by code
900 * running asynchronously on target.
902 * This is intended for applications where target-specific native code runs
903 * on the target, receives data from the circular buffer, does something with
904 * it (most likely writing it to a flash memory), and advances the circular
907 * This assumes that the helper algorithm has already been loaded to the target,
908 * but has not been started yet. Given memory and register parameters are passed
911 * The buffer is defined by (buffer_start, buffer_size) arguments and has the
914 * [buffer_start + 0, buffer_start + 4):
915 * Write Pointer address (aka head). Written and updated by this
916 * routine when new data is written to the circular buffer.
917 * [buffer_start + 4, buffer_start + 8):
918 * Read Pointer address (aka tail). Updated by code running on the
919 * target after it consumes data.
920 * [buffer_start + 8, buffer_start + buffer_size):
921 * Circular buffer contents.
923 * See contrib/loaders/flash/stm32f1x.S for an example.
925 * @param target used to run the algorithm
926 * @param buffer address on the host where data to be sent is located
927 * @param count number of blocks to send
928 * @param block_size size in bytes of each block
929 * @param num_mem_params count of memory-based params to pass to algorithm
930 * @param mem_params memory-based params to pass to algorithm
931 * @param num_reg_params count of register-based params to pass to algorithm
932 * @param reg_params memory-based params to pass to algorithm
933 * @param buffer_start address on the target of the circular buffer structure
934 * @param buffer_size size of the circular buffer structure
935 * @param entry_point address on the target to execute to start the algorithm
936 * @param exit_point address at which to set a breakpoint to catch the
937 * end of the algorithm; can be 0 if target triggers a breakpoint itself
940 int target_run_flash_async_algorithm(struct target
*target
,
941 const uint8_t *buffer
, uint32_t count
, int block_size
,
942 int num_mem_params
, struct mem_param
*mem_params
,
943 int num_reg_params
, struct reg_param
*reg_params
,
944 uint32_t buffer_start
, uint32_t buffer_size
,
945 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
950 const uint8_t *buffer_orig
= buffer
;
952 /* Set up working area. First word is write pointer, second word is read pointer,
953 * rest is fifo data area. */
954 uint32_t wp_addr
= buffer_start
;
955 uint32_t rp_addr
= buffer_start
+ 4;
956 uint32_t fifo_start_addr
= buffer_start
+ 8;
957 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
959 uint32_t wp
= fifo_start_addr
;
960 uint32_t rp
= fifo_start_addr
;
962 /* validate block_size is 2^n */
963 assert(!block_size
|| !(block_size
& (block_size
- 1)));
965 retval
= target_write_u32(target
, wp_addr
, wp
);
966 if (retval
!= ERROR_OK
)
968 retval
= target_write_u32(target
, rp_addr
, rp
);
969 if (retval
!= ERROR_OK
)
972 /* Start up algorithm on target and let it idle while writing the first chunk */
973 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
974 num_reg_params
, reg_params
,
979 if (retval
!= ERROR_OK
) {
980 LOG_ERROR("error starting target flash write algorithm");
986 retval
= target_read_u32(target
, rp_addr
, &rp
);
987 if (retval
!= ERROR_OK
) {
988 LOG_ERROR("failed to get read pointer");
992 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
993 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
996 LOG_ERROR("flash write algorithm aborted by target");
997 retval
= ERROR_FLASH_OPERATION_FAILED
;
1001 if (((rp
- fifo_start_addr
) & (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
1002 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
1006 /* Count the number of bytes available in the fifo without
1007 * crossing the wrap around. Make sure to not fill it completely,
1008 * because that would make wp == rp and that's the empty condition. */
1009 uint32_t thisrun_bytes
;
1011 thisrun_bytes
= rp
- wp
- block_size
;
1012 else if (rp
> fifo_start_addr
)
1013 thisrun_bytes
= fifo_end_addr
- wp
;
1015 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
1017 if (thisrun_bytes
== 0) {
1018 /* Throttle polling a bit if transfer is (much) faster than flash
1019 * programming. The exact delay shouldn't matter as long as it's
1020 * less than buffer size / flash speed. This is very unlikely to
1021 * run when using high latency connections such as USB. */
1024 /* to stop an infinite loop on some targets check and increment a timeout
1025 * this issue was observed on a stellaris using the new ICDI interface */
1026 if (timeout
++ >= 500) {
1027 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1028 return ERROR_FLASH_OPERATION_FAILED
;
1033 /* reset our timeout */
1036 /* Limit to the amount of data we actually want to write */
1037 if (thisrun_bytes
> count
* block_size
)
1038 thisrun_bytes
= count
* block_size
;
1040 /* Write data to fifo */
1041 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
1042 if (retval
!= ERROR_OK
)
1045 /* Update counters and wrap write pointer */
1046 buffer
+= thisrun_bytes
;
1047 count
-= thisrun_bytes
/ block_size
;
1048 wp
+= thisrun_bytes
;
1049 if (wp
>= fifo_end_addr
)
1050 wp
= fifo_start_addr
;
1052 /* Store updated write pointer to target */
1053 retval
= target_write_u32(target
, wp_addr
, wp
);
1054 if (retval
!= ERROR_OK
)
1057 /* Avoid GDB timeouts */
1061 if (retval
!= ERROR_OK
) {
1062 /* abort flash write algorithm on target */
1063 target_write_u32(target
, wp_addr
, 0);
1066 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1067 num_reg_params
, reg_params
,
1072 if (retval2
!= ERROR_OK
) {
1073 LOG_ERROR("error waiting for target flash write algorithm");
1077 if (retval
== ERROR_OK
) {
1078 /* check if algorithm set rp = 0 after fifo writer loop finished */
1079 retval
= target_read_u32(target
, rp_addr
, &rp
);
1080 if (retval
== ERROR_OK
&& rp
== 0) {
1081 LOG_ERROR("flash write algorithm aborted by target");
1082 retval
= ERROR_FLASH_OPERATION_FAILED
;
1089 int target_read_memory(struct target
*target
,
1090 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1092 if (!target_was_examined(target
)) {
1093 LOG_ERROR("Target not examined yet");
1096 if (!target
->type
->read_memory
) {
1097 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1100 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1103 int target_read_phys_memory(struct target
*target
,
1104 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1106 if (!target_was_examined(target
)) {
1107 LOG_ERROR("Target not examined yet");
1110 if (!target
->type
->read_phys_memory
) {
1111 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1114 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1117 int target_write_memory(struct target
*target
,
1118 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1120 if (!target_was_examined(target
)) {
1121 LOG_ERROR("Target not examined yet");
1124 if (!target
->type
->write_memory
) {
1125 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1128 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1131 int target_write_phys_memory(struct target
*target
,
1132 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1134 if (!target_was_examined(target
)) {
1135 LOG_ERROR("Target not examined yet");
1138 if (!target
->type
->write_phys_memory
) {
1139 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1142 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1145 int target_add_breakpoint(struct target
*target
,
1146 struct breakpoint
*breakpoint
)
1148 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1149 LOG_WARNING("target %s is not halted (add breakpoint)", target_name(target
));
1150 return ERROR_TARGET_NOT_HALTED
;
1152 return target
->type
->add_breakpoint(target
, breakpoint
);
1155 int target_add_context_breakpoint(struct target
*target
,
1156 struct breakpoint
*breakpoint
)
1158 if (target
->state
!= TARGET_HALTED
) {
1159 LOG_WARNING("target %s is not halted (add context breakpoint)", target_name(target
));
1160 return ERROR_TARGET_NOT_HALTED
;
1162 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1165 int target_add_hybrid_breakpoint(struct target
*target
,
1166 struct breakpoint
*breakpoint
)
1168 if (target
->state
!= TARGET_HALTED
) {
1169 LOG_WARNING("target %s is not halted (add hybrid breakpoint)", target_name(target
));
1170 return ERROR_TARGET_NOT_HALTED
;
1172 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1175 int target_remove_breakpoint(struct target
*target
,
1176 struct breakpoint
*breakpoint
)
1178 return target
->type
->remove_breakpoint(target
, breakpoint
);
1181 int target_add_watchpoint(struct target
*target
,
1182 struct watchpoint
*watchpoint
)
1184 if (target
->state
!= TARGET_HALTED
) {
1185 LOG_WARNING("target %s is not halted (add watchpoint)", target_name(target
));
1186 return ERROR_TARGET_NOT_HALTED
;
1188 return target
->type
->add_watchpoint(target
, watchpoint
);
1190 int target_remove_watchpoint(struct target
*target
,
1191 struct watchpoint
*watchpoint
)
1193 return target
->type
->remove_watchpoint(target
, watchpoint
);
1195 int target_hit_watchpoint(struct target
*target
,
1196 struct watchpoint
**hit_watchpoint
)
1198 if (target
->state
!= TARGET_HALTED
) {
1199 LOG_WARNING("target %s is not halted (hit watchpoint)", target
->cmd_name
);
1200 return ERROR_TARGET_NOT_HALTED
;
1203 if (target
->type
->hit_watchpoint
== NULL
) {
1204 /* For backward compatible, if hit_watchpoint is not implemented,
1205 * return ERROR_FAIL such that gdb_server will not take the nonsense
1210 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1213 const char *target_get_gdb_arch(struct target
*target
)
1215 if (target
->type
->get_gdb_arch
== NULL
)
1217 return target
->type
->get_gdb_arch(target
);
1220 int target_get_gdb_reg_list(struct target
*target
,
1221 struct reg
**reg_list
[], int *reg_list_size
,
1222 enum target_register_class reg_class
)
1224 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1227 bool target_supports_gdb_connection(struct target
*target
)
1230 * based on current code, we can simply exclude all the targets that
1231 * don't provide get_gdb_reg_list; this could change with new targets.
1233 return !!target
->type
->get_gdb_reg_list
;
1236 int target_step(struct target
*target
,
1237 int current
, target_addr_t address
, int handle_breakpoints
)
1239 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1242 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1244 if (target
->state
!= TARGET_HALTED
) {
1245 LOG_WARNING("target %s is not halted (gdb fileio)", target
->cmd_name
);
1246 return ERROR_TARGET_NOT_HALTED
;
1248 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1251 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1253 if (target
->state
!= TARGET_HALTED
) {
1254 LOG_WARNING("target %s is not halted (gdb fileio end)", target
->cmd_name
);
1255 return ERROR_TARGET_NOT_HALTED
;
1257 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1260 int target_profiling(struct target
*target
, uint32_t *samples
,
1261 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1263 if (target
->state
!= TARGET_HALTED
) {
1264 LOG_WARNING("target %s is not halted (profiling)", target
->cmd_name
);
1265 return ERROR_TARGET_NOT_HALTED
;
1267 return target
->type
->profiling(target
, samples
, max_num_samples
,
1268 num_samples
, seconds
);
1272 * Reset the @c examined flag for the given target.
1273 * Pure paranoia -- targets are zeroed on allocation.
1275 static void target_reset_examined(struct target
*target
)
1277 target
->examined
= false;
1280 static int handle_target(void *priv
);
1282 static int target_init_one(struct command_context
*cmd_ctx
,
1283 struct target
*target
)
1285 target_reset_examined(target
);
1287 struct target_type
*type
= target
->type
;
1288 if (type
->examine
== NULL
)
1289 type
->examine
= default_examine
;
1291 if (type
->check_reset
== NULL
)
1292 type
->check_reset
= default_check_reset
;
1294 assert(type
->init_target
!= NULL
);
1296 int retval
= type
->init_target(cmd_ctx
, target
);
1297 if (ERROR_OK
!= retval
) {
1298 LOG_ERROR("target '%s' init failed", target_name(target
));
1302 /* Sanity-check MMU support ... stub in what we must, to help
1303 * implement it in stages, but warn if we need to do so.
1306 if (type
->virt2phys
== NULL
) {
1307 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1308 type
->virt2phys
= identity_virt2phys
;
1311 /* Make sure no-MMU targets all behave the same: make no
1312 * distinction between physical and virtual addresses, and
1313 * ensure that virt2phys() is always an identity mapping.
1315 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1316 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1319 type
->write_phys_memory
= type
->write_memory
;
1320 type
->read_phys_memory
= type
->read_memory
;
1321 type
->virt2phys
= identity_virt2phys
;
1324 if (target
->type
->read_buffer
== NULL
)
1325 target
->type
->read_buffer
= target_read_buffer_default
;
1327 if (target
->type
->write_buffer
== NULL
)
1328 target
->type
->write_buffer
= target_write_buffer_default
;
1330 if (target
->type
->get_gdb_fileio_info
== NULL
)
1331 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1333 if (target
->type
->gdb_fileio_end
== NULL
)
1334 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1336 if (target
->type
->profiling
== NULL
)
1337 target
->type
->profiling
= target_profiling_default
;
1342 static int target_init(struct command_context
*cmd_ctx
)
1344 struct target
*target
;
1347 for (target
= all_targets
; target
; target
= target
->next
) {
1348 retval
= target_init_one(cmd_ctx
, target
);
1349 if (ERROR_OK
!= retval
)
1356 retval
= target_register_user_commands(cmd_ctx
);
1357 if (ERROR_OK
!= retval
)
1360 retval
= target_register_timer_callback(&handle_target
,
1361 polling_interval
, TARGET_TIMER_TYPE_PERIODIC
, cmd_ctx
->interp
);
1362 if (ERROR_OK
!= retval
)
1368 COMMAND_HANDLER(handle_target_init_command
)
1373 return ERROR_COMMAND_SYNTAX_ERROR
;
1375 static bool target_initialized
;
1376 if (target_initialized
) {
1377 LOG_INFO("'target init' has already been called");
1380 target_initialized
= true;
1382 retval
= command_run_line(CMD_CTX
, "init_targets");
1383 if (ERROR_OK
!= retval
)
1386 retval
= command_run_line(CMD_CTX
, "init_target_events");
1387 if (ERROR_OK
!= retval
)
1390 retval
= command_run_line(CMD_CTX
, "init_board");
1391 if (ERROR_OK
!= retval
)
1394 LOG_DEBUG("Initializing targets...");
1395 return target_init(CMD_CTX
);
1398 int target_register_event_callback(int (*callback
)(struct target
*target
,
1399 enum target_event event
, void *priv
), void *priv
)
1401 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1403 if (callback
== NULL
)
1404 return ERROR_COMMAND_SYNTAX_ERROR
;
1407 while ((*callbacks_p
)->next
)
1408 callbacks_p
= &((*callbacks_p
)->next
);
1409 callbacks_p
= &((*callbacks_p
)->next
);
1412 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1413 (*callbacks_p
)->callback
= callback
;
1414 (*callbacks_p
)->priv
= priv
;
1415 (*callbacks_p
)->next
= NULL
;
1420 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1421 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1423 struct target_reset_callback
*entry
;
1425 if (callback
== NULL
)
1426 return ERROR_COMMAND_SYNTAX_ERROR
;
1428 entry
= malloc(sizeof(struct target_reset_callback
));
1429 if (entry
== NULL
) {
1430 LOG_ERROR("error allocating buffer for reset callback entry");
1431 return ERROR_COMMAND_SYNTAX_ERROR
;
1434 entry
->callback
= callback
;
1436 list_add(&entry
->list
, &target_reset_callback_list
);
1442 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1443 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1445 struct target_trace_callback
*entry
;
1447 if (callback
== NULL
)
1448 return ERROR_COMMAND_SYNTAX_ERROR
;
1450 entry
= malloc(sizeof(struct target_trace_callback
));
1451 if (entry
== NULL
) {
1452 LOG_ERROR("error allocating buffer for trace callback entry");
1453 return ERROR_COMMAND_SYNTAX_ERROR
;
1456 entry
->callback
= callback
;
1458 list_add(&entry
->list
, &target_trace_callback_list
);
1464 int target_register_timer_callback(int (*callback
)(void *priv
),
1465 unsigned int time_ms
, enum target_timer_type type
, void *priv
)
1467 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1469 if (callback
== NULL
)
1470 return ERROR_COMMAND_SYNTAX_ERROR
;
1473 while ((*callbacks_p
)->next
)
1474 callbacks_p
= &((*callbacks_p
)->next
);
1475 callbacks_p
= &((*callbacks_p
)->next
);
1478 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1479 (*callbacks_p
)->callback
= callback
;
1480 (*callbacks_p
)->type
= type
;
1481 (*callbacks_p
)->time_ms
= time_ms
;
1482 (*callbacks_p
)->removed
= false;
1484 gettimeofday(&(*callbacks_p
)->when
, NULL
);
1485 timeval_add_time(&(*callbacks_p
)->when
, 0, time_ms
* 1000);
1487 (*callbacks_p
)->priv
= priv
;
1488 (*callbacks_p
)->next
= NULL
;
1493 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1494 enum target_event event
, void *priv
), void *priv
)
1496 struct target_event_callback
**p
= &target_event_callbacks
;
1497 struct target_event_callback
*c
= target_event_callbacks
;
1499 if (callback
== NULL
)
1500 return ERROR_COMMAND_SYNTAX_ERROR
;
1503 struct target_event_callback
*next
= c
->next
;
1504 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1516 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1517 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1519 struct target_reset_callback
*entry
;
1521 if (callback
== NULL
)
1522 return ERROR_COMMAND_SYNTAX_ERROR
;
1524 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1525 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1526 list_del(&entry
->list
);
1535 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1536 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1538 struct target_trace_callback
*entry
;
1540 if (callback
== NULL
)
1541 return ERROR_COMMAND_SYNTAX_ERROR
;
1543 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1544 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1545 list_del(&entry
->list
);
1554 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1556 if (callback
== NULL
)
1557 return ERROR_COMMAND_SYNTAX_ERROR
;
1559 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1561 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1570 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1572 struct target_event_callback
*callback
= target_event_callbacks
;
1573 struct target_event_callback
*next_callback
;
1575 if (event
== TARGET_EVENT_HALTED
) {
1576 /* execute early halted first */
1577 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1580 LOG_DEBUG("target event %i (%s)", event
,
1581 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1583 target_handle_event(target
, event
);
1586 next_callback
= callback
->next
;
1587 callback
->callback(target
, event
, callback
->priv
);
1588 callback
= next_callback
;
1594 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1596 struct target_reset_callback
*callback
;
1598 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1599 Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1601 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1602 callback
->callback(target
, reset_mode
, callback
->priv
);
1607 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1609 struct target_trace_callback
*callback
;
1611 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1612 callback
->callback(target
, len
, data
, callback
->priv
);
1617 static int target_timer_callback_periodic_restart(
1618 struct target_timer_callback
*cb
, struct timeval
*now
)
1621 timeval_add_time(&cb
->when
, 0, cb
->time_ms
* 1000L);
1625 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1626 struct timeval
*now
)
1628 cb
->callback(cb
->priv
);
1630 if (cb
->type
== TARGET_TIMER_TYPE_PERIODIC
)
1631 return target_timer_callback_periodic_restart(cb
, now
);
1633 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1636 static int target_call_timer_callbacks_check_time(int checktime
)
1638 static bool callback_processing
;
1640 /* Do not allow nesting */
1641 if (callback_processing
)
1644 callback_processing
= true;
1649 gettimeofday(&now
, NULL
);
1651 /* Store an address of the place containing a pointer to the
1652 * next item; initially, that's a standalone "root of the
1653 * list" variable. */
1654 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1656 if ((*callback
)->removed
) {
1657 struct target_timer_callback
*p
= *callback
;
1658 *callback
= (*callback
)->next
;
1663 bool call_it
= (*callback
)->callback
&&
1664 ((!checktime
&& (*callback
)->type
== TARGET_TIMER_TYPE_PERIODIC
) ||
1665 timeval_compare(&now
, &(*callback
)->when
) >= 0);
1668 target_call_timer_callback(*callback
, &now
);
1670 callback
= &(*callback
)->next
;
1673 callback_processing
= false;
1677 int target_call_timer_callbacks(void)
1679 return target_call_timer_callbacks_check_time(1);
1682 /* invoke periodic callbacks immediately */
1683 int target_call_timer_callbacks_now(void)
1685 return target_call_timer_callbacks_check_time(0);
1688 /* Prints the working area layout for debug purposes */
1689 static void print_wa_layout(struct target
*target
)
1691 struct working_area
*c
= target
->working_areas
;
1694 LOG_DEBUG("%c%c " TARGET_ADDR_FMT
"-" TARGET_ADDR_FMT
" (%" PRIu32
" bytes)",
1695 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1696 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1701 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1702 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1704 assert(area
->free
); /* Shouldn't split an allocated area */
1705 assert(size
<= area
->size
); /* Caller should guarantee this */
1707 /* Split only if not already the right size */
1708 if (size
< area
->size
) {
1709 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1714 new_wa
->next
= area
->next
;
1715 new_wa
->size
= area
->size
- size
;
1716 new_wa
->address
= area
->address
+ size
;
1717 new_wa
->backup
= NULL
;
1718 new_wa
->user
= NULL
;
1719 new_wa
->free
= true;
1721 area
->next
= new_wa
;
1724 /* If backup memory was allocated to this area, it has the wrong size
1725 * now so free it and it will be reallocated if/when needed */
1728 area
->backup
= NULL
;
1733 /* Merge all adjacent free areas into one */
1734 static void target_merge_working_areas(struct target
*target
)
1736 struct working_area
*c
= target
->working_areas
;
1738 while (c
&& c
->next
) {
1739 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1741 /* Find two adjacent free areas */
1742 if (c
->free
&& c
->next
->free
) {
1743 /* Merge the last into the first */
1744 c
->size
+= c
->next
->size
;
1746 /* Remove the last */
1747 struct working_area
*to_be_freed
= c
->next
;
1748 c
->next
= c
->next
->next
;
1749 if (to_be_freed
->backup
)
1750 free(to_be_freed
->backup
);
1753 /* If backup memory was allocated to the remaining area, it's has
1754 * the wrong size now */
1765 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1767 /* Reevaluate working area address based on MMU state*/
1768 if (target
->working_areas
== NULL
) {
1772 retval
= target
->type
->mmu(target
, &enabled
);
1773 if (retval
!= ERROR_OK
)
1777 if (target
->working_area_phys_spec
) {
1778 LOG_DEBUG("MMU disabled, using physical "
1779 "address for working memory " TARGET_ADDR_FMT
,
1780 target
->working_area_phys
);
1781 target
->working_area
= target
->working_area_phys
;
1783 LOG_ERROR("No working memory available. "
1784 "Specify -work-area-phys to target.");
1785 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1788 if (target
->working_area_virt_spec
) {
1789 LOG_DEBUG("MMU enabled, using virtual "
1790 "address for working memory " TARGET_ADDR_FMT
,
1791 target
->working_area_virt
);
1792 target
->working_area
= target
->working_area_virt
;
1794 LOG_ERROR("No working memory available. "
1795 "Specify -work-area-virt to target.");
1796 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1800 /* Set up initial working area on first call */
1801 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1803 new_wa
->next
= NULL
;
1804 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1805 new_wa
->address
= target
->working_area
;
1806 new_wa
->backup
= NULL
;
1807 new_wa
->user
= NULL
;
1808 new_wa
->free
= true;
1811 target
->working_areas
= new_wa
;
1814 /* only allocate multiples of 4 byte */
1816 size
= (size
+ 3) & (~3UL);
1818 struct working_area
*c
= target
->working_areas
;
1820 /* Find the first large enough working area */
1822 if (c
->free
&& c
->size
>= size
)
1828 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1830 /* Split the working area into the requested size */
1831 target_split_working_area(c
, size
);
1833 LOG_DEBUG("allocated new working area of %" PRIu32
" bytes at address " TARGET_ADDR_FMT
,
1836 if (target
->backup_working_area
) {
1837 if (c
->backup
== NULL
) {
1838 c
->backup
= malloc(c
->size
);
1839 if (c
->backup
== NULL
)
1843 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1844 if (retval
!= ERROR_OK
)
1848 /* mark as used, and return the new (reused) area */
1855 print_wa_layout(target
);
1860 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1864 retval
= target_alloc_working_area_try(target
, size
, area
);
1865 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1866 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1871 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1873 int retval
= ERROR_OK
;
1875 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1876 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1877 if (retval
!= ERROR_OK
)
1878 LOG_ERROR("failed to restore %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1879 area
->size
, area
->address
);
1885 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1886 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1888 int retval
= ERROR_OK
;
1894 retval
= target_restore_working_area(target
, area
);
1895 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1896 if (retval
!= ERROR_OK
)
1902 LOG_DEBUG("freed %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1903 area
->size
, area
->address
);
1905 /* mark user pointer invalid */
1906 /* TODO: Is this really safe? It points to some previous caller's memory.
1907 * How could we know that the area pointer is still in that place and not
1908 * some other vital data? What's the purpose of this, anyway? */
1912 target_merge_working_areas(target
);
1914 print_wa_layout(target
);
1919 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1921 return target_free_working_area_restore(target
, area
, 1);
1924 /* free resources and restore memory, if restoring memory fails,
1925 * free up resources anyway
1927 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1929 struct working_area
*c
= target
->working_areas
;
1931 LOG_DEBUG("freeing all working areas");
1933 /* Loop through all areas, restoring the allocated ones and marking them as free */
1937 target_restore_working_area(target
, c
);
1939 *c
->user
= NULL
; /* Same as above */
1945 /* Run a merge pass to combine all areas into one */
1946 target_merge_working_areas(target
);
1948 print_wa_layout(target
);
1951 void target_free_all_working_areas(struct target
*target
)
1953 target_free_all_working_areas_restore(target
, 1);
1955 /* Now we have none or only one working area marked as free */
1956 if (target
->working_areas
) {
1957 /* Free the last one to allow on-the-fly moving and resizing */
1958 free(target
->working_areas
->backup
);
1959 free(target
->working_areas
);
1960 target
->working_areas
= NULL
;
1964 /* Find the largest number of bytes that can be allocated */
1965 uint32_t target_get_working_area_avail(struct target
*target
)
1967 struct working_area
*c
= target
->working_areas
;
1968 uint32_t max_size
= 0;
1971 return target
->working_area_size
;
1974 if (c
->free
&& max_size
< c
->size
)
1983 static void target_destroy(struct target
*target
)
1985 if (target
->type
->deinit_target
)
1986 target
->type
->deinit_target(target
);
1988 if (target
->semihosting
)
1989 free(target
->semihosting
);
1991 jtag_unregister_event_callback(jtag_enable_callback
, target
);
1993 struct target_event_action
*teap
= target
->event_action
;
1995 struct target_event_action
*next
= teap
->next
;
1996 Jim_DecrRefCount(teap
->interp
, teap
->body
);
2001 target_free_all_working_areas(target
);
2003 /* release the targets SMP list */
2005 struct target_list
*head
= target
->head
;
2006 while (head
!= NULL
) {
2007 struct target_list
*pos
= head
->next
;
2008 head
->target
->smp
= 0;
2015 free(target
->gdb_port_override
);
2017 free(target
->trace_info
);
2018 free(target
->fileio_info
);
2019 free(target
->cmd_name
);
2023 void target_quit(void)
2025 struct target_event_callback
*pe
= target_event_callbacks
;
2027 struct target_event_callback
*t
= pe
->next
;
2031 target_event_callbacks
= NULL
;
2033 struct target_timer_callback
*pt
= target_timer_callbacks
;
2035 struct target_timer_callback
*t
= pt
->next
;
2039 target_timer_callbacks
= NULL
;
2041 for (struct target
*target
= all_targets
; target
;) {
2045 target_destroy(target
);
2052 int target_arch_state(struct target
*target
)
2055 if (target
== NULL
) {
2056 LOG_WARNING("No target has been configured");
2060 if (target
->state
!= TARGET_HALTED
)
2063 retval
= target
->type
->arch_state(target
);
2067 static int target_get_gdb_fileio_info_default(struct target
*target
,
2068 struct gdb_fileio_info
*fileio_info
)
2070 /* If target does not support semi-hosting function, target
2071 has no need to provide .get_gdb_fileio_info callback.
2072 It just return ERROR_FAIL and gdb_server will return "Txx"
2073 as target halted every time. */
2077 static int target_gdb_fileio_end_default(struct target
*target
,
2078 int retcode
, int fileio_errno
, bool ctrl_c
)
2083 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
2084 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
2086 struct timeval timeout
, now
;
2088 gettimeofday(&timeout
, NULL
);
2089 timeval_add_time(&timeout
, seconds
, 0);
2091 LOG_INFO("Starting profiling. Halting and resuming the"
2092 " target as often as we can...");
2094 uint32_t sample_count
= 0;
2095 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2096 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2098 int retval
= ERROR_OK
;
2100 target_poll(target
);
2101 if (target
->state
== TARGET_HALTED
) {
2102 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
2103 samples
[sample_count
++] = t
;
2104 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2105 retval
= target_resume(target
, 1, 0, 0, 0);
2106 target_poll(target
);
2107 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2108 } else if (target
->state
== TARGET_RUNNING
) {
2109 /* We want to quickly sample the PC. */
2110 retval
= target_halt(target
);
2112 LOG_INFO("Target not halted or running");
2117 if (retval
!= ERROR_OK
)
2120 gettimeofday(&now
, NULL
);
2121 if ((sample_count
>= max_num_samples
) || timeval_compare(&now
, &timeout
) >= 0) {
2122 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2127 *num_samples
= sample_count
;
2131 /* Single aligned words are guaranteed to use 16 or 32 bit access
2132 * mode respectively, otherwise data is handled as quickly as
2135 int target_write_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, const uint8_t *buffer
)
2137 LOG_DEBUG("writing buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2140 if (!target_was_examined(target
)) {
2141 LOG_ERROR("Target not examined yet");
2148 if ((address
+ size
- 1) < address
) {
2149 /* GDB can request this when e.g. PC is 0xfffffffc */
2150 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2156 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2159 static int target_write_buffer_default(struct target
*target
,
2160 target_addr_t address
, uint32_t count
, const uint8_t *buffer
)
2164 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2165 * will have something to do with the size we leave to it. */
2166 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2167 if (address
& size
) {
2168 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2169 if (retval
!= ERROR_OK
)
2177 /* Write the data with as large access size as possible. */
2178 for (; size
> 0; size
/= 2) {
2179 uint32_t aligned
= count
- count
% size
;
2181 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2182 if (retval
!= ERROR_OK
)
2193 /* Single aligned words are guaranteed to use 16 or 32 bit access
2194 * mode respectively, otherwise data is handled as quickly as
2197 int target_read_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
2199 LOG_DEBUG("reading buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2202 if (!target_was_examined(target
)) {
2203 LOG_ERROR("Target not examined yet");
2210 if ((address
+ size
- 1) < address
) {
2211 /* GDB can request this when e.g. PC is 0xfffffffc */
2212 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2218 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2221 static int target_read_buffer_default(struct target
*target
, target_addr_t address
, uint32_t count
, uint8_t *buffer
)
2225 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2226 * will have something to do with the size we leave to it. */
2227 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2228 if (address
& size
) {
2229 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2230 if (retval
!= ERROR_OK
)
2238 /* Read the data with as large access size as possible. */
2239 for (; size
> 0; size
/= 2) {
2240 uint32_t aligned
= count
- count
% size
;
2242 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2243 if (retval
!= ERROR_OK
)
2254 int target_checksum_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* crc
)
2259 uint32_t checksum
= 0;
2260 if (!target_was_examined(target
)) {
2261 LOG_ERROR("Target not examined yet");
2265 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2266 if (retval
!= ERROR_OK
) {
2267 buffer
= malloc(size
);
2268 if (buffer
== NULL
) {
2269 LOG_ERROR("error allocating buffer for section (%" PRId32
" bytes)", size
);
2270 return ERROR_COMMAND_SYNTAX_ERROR
;
2272 retval
= target_read_buffer(target
, address
, size
, buffer
);
2273 if (retval
!= ERROR_OK
) {
2278 /* convert to target endianness */
2279 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2280 uint32_t target_data
;
2281 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2282 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2285 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2294 int target_blank_check_memory(struct target
*target
,
2295 struct target_memory_check_block
*blocks
, int num_blocks
,
2296 uint8_t erased_value
)
2298 if (!target_was_examined(target
)) {
2299 LOG_ERROR("Target not examined yet");
2303 if (target
->type
->blank_check_memory
== NULL
)
2304 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2306 return target
->type
->blank_check_memory(target
, blocks
, num_blocks
, erased_value
);
2309 int target_read_u64(struct target
*target
, target_addr_t address
, uint64_t *value
)
2311 uint8_t value_buf
[8];
2312 if (!target_was_examined(target
)) {
2313 LOG_ERROR("Target not examined yet");
2317 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2319 if (retval
== ERROR_OK
) {
2320 *value
= target_buffer_get_u64(target
, value_buf
);
2321 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2326 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2333 int target_read_u32(struct target
*target
, target_addr_t address
, uint32_t *value
)
2335 uint8_t value_buf
[4];
2336 if (!target_was_examined(target
)) {
2337 LOG_ERROR("Target not examined yet");
2341 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2343 if (retval
== ERROR_OK
) {
2344 *value
= target_buffer_get_u32(target
, value_buf
);
2345 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2350 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2357 int target_read_u16(struct target
*target
, target_addr_t address
, uint16_t *value
)
2359 uint8_t value_buf
[2];
2360 if (!target_was_examined(target
)) {
2361 LOG_ERROR("Target not examined yet");
2365 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2367 if (retval
== ERROR_OK
) {
2368 *value
= target_buffer_get_u16(target
, value_buf
);
2369 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%4.4" PRIx16
,
2374 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2381 int target_read_u8(struct target
*target
, target_addr_t address
, uint8_t *value
)
2383 if (!target_was_examined(target
)) {
2384 LOG_ERROR("Target not examined yet");
2388 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2390 if (retval
== ERROR_OK
) {
2391 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2396 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2403 int target_write_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2406 uint8_t value_buf
[8];
2407 if (!target_was_examined(target
)) {
2408 LOG_ERROR("Target not examined yet");
2412 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2416 target_buffer_set_u64(target
, value_buf
, value
);
2417 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2418 if (retval
!= ERROR_OK
)
2419 LOG_DEBUG("failed: %i", retval
);
2424 int target_write_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2427 uint8_t value_buf
[4];
2428 if (!target_was_examined(target
)) {
2429 LOG_ERROR("Target not examined yet");
2433 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2437 target_buffer_set_u32(target
, value_buf
, value
);
2438 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2439 if (retval
!= ERROR_OK
)
2440 LOG_DEBUG("failed: %i", retval
);
2445 int target_write_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2448 uint8_t value_buf
[2];
2449 if (!target_was_examined(target
)) {
2450 LOG_ERROR("Target not examined yet");
2454 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2458 target_buffer_set_u16(target
, value_buf
, value
);
2459 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2460 if (retval
!= ERROR_OK
)
2461 LOG_DEBUG("failed: %i", retval
);
2466 int target_write_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2469 if (!target_was_examined(target
)) {
2470 LOG_ERROR("Target not examined yet");
2474 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2477 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2478 if (retval
!= ERROR_OK
)
2479 LOG_DEBUG("failed: %i", retval
);
2484 int target_write_phys_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2487 uint8_t value_buf
[8];
2488 if (!target_was_examined(target
)) {
2489 LOG_ERROR("Target not examined yet");
2493 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2497 target_buffer_set_u64(target
, value_buf
, value
);
2498 retval
= target_write_phys_memory(target
, address
, 8, 1, value_buf
);
2499 if (retval
!= ERROR_OK
)
2500 LOG_DEBUG("failed: %i", retval
);
2505 int target_write_phys_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2508 uint8_t value_buf
[4];
2509 if (!target_was_examined(target
)) {
2510 LOG_ERROR("Target not examined yet");
2514 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2518 target_buffer_set_u32(target
, value_buf
, value
);
2519 retval
= target_write_phys_memory(target
, address
, 4, 1, value_buf
);
2520 if (retval
!= ERROR_OK
)
2521 LOG_DEBUG("failed: %i", retval
);
2526 int target_write_phys_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2529 uint8_t value_buf
[2];
2530 if (!target_was_examined(target
)) {
2531 LOG_ERROR("Target not examined yet");
2535 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2539 target_buffer_set_u16(target
, value_buf
, value
);
2540 retval
= target_write_phys_memory(target
, address
, 2, 1, value_buf
);
2541 if (retval
!= ERROR_OK
)
2542 LOG_DEBUG("failed: %i", retval
);
2547 int target_write_phys_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2550 if (!target_was_examined(target
)) {
2551 LOG_ERROR("Target not examined yet");
2555 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2558 retval
= target_write_phys_memory(target
, address
, 1, 1, &value
);
2559 if (retval
!= ERROR_OK
)
2560 LOG_DEBUG("failed: %i", retval
);
2565 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2567 struct target
*target
= get_target(name
);
2568 if (target
== NULL
) {
2569 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2572 if (!target
->tap
->enabled
) {
2573 LOG_USER("Target: TAP %s is disabled, "
2574 "can't be the current target\n",
2575 target
->tap
->dotted_name
);
2579 cmd_ctx
->current_target
= target
;
2580 if (cmd_ctx
->current_target_override
)
2581 cmd_ctx
->current_target_override
= target
;
2587 COMMAND_HANDLER(handle_targets_command
)
2589 int retval
= ERROR_OK
;
2590 if (CMD_ARGC
== 1) {
2591 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2592 if (retval
== ERROR_OK
) {
2598 struct target
*target
= all_targets
;
2599 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2600 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2605 if (target
->tap
->enabled
)
2606 state
= target_state_name(target
);
2608 state
= "tap-disabled";
2610 if (CMD_CTX
->current_target
== target
)
2613 /* keep columns lined up to match the headers above */
2614 command_print(CMD_CTX
,
2615 "%2d%c %-18s %-10s %-6s %-18s %s",
2616 target
->target_number
,
2618 target_name(target
),
2619 target_type_name(target
),
2620 Jim_Nvp_value2name_simple(nvp_target_endian
,
2621 target
->endianness
)->name
,
2622 target
->tap
->dotted_name
,
2624 target
= target
->next
;
2630 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2632 static int powerDropout
;
2633 static int srstAsserted
;
2635 static int runPowerRestore
;
2636 static int runPowerDropout
;
2637 static int runSrstAsserted
;
2638 static int runSrstDeasserted
;
2640 static int sense_handler(void)
2642 static int prevSrstAsserted
;
2643 static int prevPowerdropout
;
2645 int retval
= jtag_power_dropout(&powerDropout
);
2646 if (retval
!= ERROR_OK
)
2650 powerRestored
= prevPowerdropout
&& !powerDropout
;
2652 runPowerRestore
= 1;
2654 int64_t current
= timeval_ms();
2655 static int64_t lastPower
;
2656 bool waitMore
= lastPower
+ 2000 > current
;
2657 if (powerDropout
&& !waitMore
) {
2658 runPowerDropout
= 1;
2659 lastPower
= current
;
2662 retval
= jtag_srst_asserted(&srstAsserted
);
2663 if (retval
!= ERROR_OK
)
2667 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2669 static int64_t lastSrst
;
2670 waitMore
= lastSrst
+ 2000 > current
;
2671 if (srstDeasserted
&& !waitMore
) {
2672 runSrstDeasserted
= 1;
2676 if (!prevSrstAsserted
&& srstAsserted
)
2677 runSrstAsserted
= 1;
2679 prevSrstAsserted
= srstAsserted
;
2680 prevPowerdropout
= powerDropout
;
2682 if (srstDeasserted
|| powerRestored
) {
2683 /* Other than logging the event we can't do anything here.
2684 * Issuing a reset is a particularly bad idea as we might
2685 * be inside a reset already.
2692 /* process target state changes */
2693 static int handle_target(void *priv
)
2695 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2696 int retval
= ERROR_OK
;
2698 if (!is_jtag_poll_safe()) {
2699 /* polling is disabled currently */
2703 /* we do not want to recurse here... */
2704 static int recursive
;
2708 /* danger! running these procedures can trigger srst assertions and power dropouts.
2709 * We need to avoid an infinite loop/recursion here and we do that by
2710 * clearing the flags after running these events.
2712 int did_something
= 0;
2713 if (runSrstAsserted
) {
2714 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2715 Jim_Eval(interp
, "srst_asserted");
2718 if (runSrstDeasserted
) {
2719 Jim_Eval(interp
, "srst_deasserted");
2722 if (runPowerDropout
) {
2723 LOG_INFO("Power dropout detected, running power_dropout proc.");
2724 Jim_Eval(interp
, "power_dropout");
2727 if (runPowerRestore
) {
2728 Jim_Eval(interp
, "power_restore");
2732 if (did_something
) {
2733 /* clear detect flags */
2737 /* clear action flags */
2739 runSrstAsserted
= 0;
2740 runSrstDeasserted
= 0;
2741 runPowerRestore
= 0;
2742 runPowerDropout
= 0;
2747 /* Poll targets for state changes unless that's globally disabled.
2748 * Skip targets that are currently disabled.
2750 for (struct target
*target
= all_targets
;
2751 is_jtag_poll_safe() && target
;
2752 target
= target
->next
) {
2754 if (!target_was_examined(target
))
2757 if (!target
->tap
->enabled
)
2760 if (target
->backoff
.times
> target
->backoff
.count
) {
2761 /* do not poll this time as we failed previously */
2762 target
->backoff
.count
++;
2765 target
->backoff
.count
= 0;
2767 /* only poll target if we've got power and srst isn't asserted */
2768 if (!powerDropout
&& !srstAsserted
) {
2769 /* polling may fail silently until the target has been examined */
2770 retval
= target_poll(target
);
2771 if (retval
!= ERROR_OK
) {
2772 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2773 if (target
->backoff
.times
* polling_interval
< 5000) {
2774 target
->backoff
.times
*= 2;
2775 target
->backoff
.times
++;
2778 /* Tell GDB to halt the debugger. This allows the user to
2779 * run monitor commands to handle the situation.
2781 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2783 if (target
->backoff
.times
> 0) {
2784 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
2785 target_reset_examined(target
);
2786 retval
= target_examine_one(target
);
2787 /* Target examination could have failed due to unstable connection,
2788 * but we set the examined flag anyway to repoll it later */
2789 if (retval
!= ERROR_OK
) {
2790 target
->examined
= true;
2791 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2792 target
->backoff
.times
* polling_interval
);
2797 /* Since we succeeded, we reset backoff count */
2798 target
->backoff
.times
= 0;
2805 COMMAND_HANDLER(handle_reg_command
)
2807 struct target
*target
;
2808 struct reg
*reg
= NULL
;
2814 target
= get_current_target(CMD_CTX
);
2816 /* list all available registers for the current target */
2817 if (CMD_ARGC
== 0) {
2818 struct reg_cache
*cache
= target
->reg_cache
;
2824 command_print(CMD_CTX
, "===== %s", cache
->name
);
2826 for (i
= 0, reg
= cache
->reg_list
;
2827 i
< cache
->num_regs
;
2828 i
++, reg
++, count
++) {
2829 if (reg
->exist
== false)
2831 /* only print cached values if they are valid */
2833 value
= buf_to_str(reg
->value
,
2835 command_print(CMD_CTX
,
2836 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2844 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2849 cache
= cache
->next
;
2855 /* access a single register by its ordinal number */
2856 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2858 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2860 struct reg_cache
*cache
= target
->reg_cache
;
2864 for (i
= 0; i
< cache
->num_regs
; i
++) {
2865 if (count
++ == num
) {
2866 reg
= &cache
->reg_list
[i
];
2872 cache
= cache
->next
;
2876 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2877 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2881 /* access a single register by its name */
2882 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2888 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2893 /* display a register */
2894 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2895 && (CMD_ARGV
[1][0] <= '9')))) {
2896 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2899 if (reg
->valid
== 0)
2900 reg
->type
->get(reg
);
2901 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2902 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2907 /* set register value */
2908 if (CMD_ARGC
== 2) {
2909 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2912 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2914 reg
->type
->set(reg
, buf
);
2916 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2917 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2925 return ERROR_COMMAND_SYNTAX_ERROR
;
2928 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2932 COMMAND_HANDLER(handle_poll_command
)
2934 int retval
= ERROR_OK
;
2935 struct target
*target
= get_current_target(CMD_CTX
);
2937 if (CMD_ARGC
== 0) {
2938 command_print(CMD_CTX
, "background polling: %s",
2939 jtag_poll_get_enabled() ? "on" : "off");
2940 command_print(CMD_CTX
, "TAP: %s (%s)",
2941 target
->tap
->dotted_name
,
2942 target
->tap
->enabled
? "enabled" : "disabled");
2943 if (!target
->tap
->enabled
)
2945 retval
= target_poll(target
);
2946 if (retval
!= ERROR_OK
)
2948 retval
= target_arch_state(target
);
2949 if (retval
!= ERROR_OK
)
2951 } else if (CMD_ARGC
== 1) {
2953 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2954 jtag_poll_set_enabled(enable
);
2956 return ERROR_COMMAND_SYNTAX_ERROR
;
2961 COMMAND_HANDLER(handle_wait_halt_command
)
2964 return ERROR_COMMAND_SYNTAX_ERROR
;
2966 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2967 if (1 == CMD_ARGC
) {
2968 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2969 if (ERROR_OK
!= retval
)
2970 return ERROR_COMMAND_SYNTAX_ERROR
;
2973 struct target
*target
= get_current_target(CMD_CTX
);
2974 return target_wait_state(target
, TARGET_HALTED
, ms
);
2977 /* wait for target state to change. The trick here is to have a low
2978 * latency for short waits and not to suck up all the CPU time
2981 * After 500ms, keep_alive() is invoked
2983 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2986 int64_t then
= 0, cur
;
2990 retval
= target_poll(target
);
2991 if (retval
!= ERROR_OK
)
2993 if (target
->state
== state
)
2998 then
= timeval_ms();
2999 LOG_DEBUG("waiting for target %s...",
3000 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
3006 if ((cur
-then
) > ms
) {
3007 LOG_ERROR("timed out while waiting for target %s",
3008 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
3016 COMMAND_HANDLER(handle_halt_command
)
3020 struct target
*target
= get_current_target(CMD_CTX
);
3022 target
->verbose_halt_msg
= true;
3024 int retval
= target_halt(target
);
3025 if (ERROR_OK
!= retval
)
3028 if (CMD_ARGC
== 1) {
3029 unsigned wait_local
;
3030 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
3031 if (ERROR_OK
!= retval
)
3032 return ERROR_COMMAND_SYNTAX_ERROR
;
3037 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
3040 COMMAND_HANDLER(handle_soft_reset_halt_command
)
3042 struct target
*target
= get_current_target(CMD_CTX
);
3044 LOG_USER("requesting target halt and executing a soft reset");
3046 target_soft_reset_halt(target
);
3051 COMMAND_HANDLER(handle_reset_command
)
3054 return ERROR_COMMAND_SYNTAX_ERROR
;
3056 enum target_reset_mode reset_mode
= RESET_RUN
;
3057 if (CMD_ARGC
== 1) {
3059 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
3060 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
3061 return ERROR_COMMAND_SYNTAX_ERROR
;
3062 reset_mode
= n
->value
;
3065 /* reset *all* targets */
3066 return target_process_reset(CMD_CTX
, reset_mode
);
3070 COMMAND_HANDLER(handle_resume_command
)
3074 return ERROR_COMMAND_SYNTAX_ERROR
;
3076 struct target
*target
= get_current_target(CMD_CTX
);
3078 /* with no CMD_ARGV, resume from current pc, addr = 0,
3079 * with one arguments, addr = CMD_ARGV[0],
3080 * handle breakpoints, not debugging */
3081 target_addr_t addr
= 0;
3082 if (CMD_ARGC
== 1) {
3083 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3087 return target_resume(target
, current
, addr
, 1, 0);
3090 COMMAND_HANDLER(handle_step_command
)
3093 return ERROR_COMMAND_SYNTAX_ERROR
;
3097 /* with no CMD_ARGV, step from current pc, addr = 0,
3098 * with one argument addr = CMD_ARGV[0],
3099 * handle breakpoints, debugging */
3100 target_addr_t addr
= 0;
3102 if (CMD_ARGC
== 1) {
3103 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3107 struct target
*target
= get_current_target(CMD_CTX
);
3109 return target
->type
->step(target
, current_pc
, addr
, 1);
3112 static void handle_md_output(struct command_context
*cmd_ctx
,
3113 struct target
*target
, target_addr_t address
, unsigned size
,
3114 unsigned count
, const uint8_t *buffer
)
3116 const unsigned line_bytecnt
= 32;
3117 unsigned line_modulo
= line_bytecnt
/ size
;
3119 char output
[line_bytecnt
* 4 + 1];
3120 unsigned output_len
= 0;
3122 const char *value_fmt
;
3125 value_fmt
= "%16.16"PRIx64
" ";
3128 value_fmt
= "%8.8"PRIx64
" ";
3131 value_fmt
= "%4.4"PRIx64
" ";
3134 value_fmt
= "%2.2"PRIx64
" ";
3137 /* "can't happen", caller checked */
3138 LOG_ERROR("invalid memory read size: %u", size
);
3142 for (unsigned i
= 0; i
< count
; i
++) {
3143 if (i
% line_modulo
== 0) {
3144 output_len
+= snprintf(output
+ output_len
,
3145 sizeof(output
) - output_len
,
3146 TARGET_ADDR_FMT
": ",
3147 (address
+ (i
* size
)));
3151 const uint8_t *value_ptr
= buffer
+ i
* size
;
3154 value
= target_buffer_get_u64(target
, value_ptr
);
3157 value
= target_buffer_get_u32(target
, value_ptr
);
3160 value
= target_buffer_get_u16(target
, value_ptr
);
3165 output_len
+= snprintf(output
+ output_len
,
3166 sizeof(output
) - output_len
,
3169 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
3170 command_print(cmd_ctx
, "%s", output
);
3176 COMMAND_HANDLER(handle_md_command
)
3179 return ERROR_COMMAND_SYNTAX_ERROR
;
3182 switch (CMD_NAME
[2]) {
3196 return ERROR_COMMAND_SYNTAX_ERROR
;
3199 bool physical
= strcmp(