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
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
255 { .name
= NULL
, .value
= -1 },
258 static const Jim_Nvp nvp_target_endian
[] = {
259 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
260 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
261 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
262 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
263 { .name
= NULL
, .value
= -1 },
266 static const Jim_Nvp nvp_reset_modes
[] = {
267 { .name
= "unknown", .value
= RESET_UNKNOWN
},
268 { .name
= "run" , .value
= RESET_RUN
},
269 { .name
= "halt" , .value
= RESET_HALT
},
270 { .name
= "init" , .value
= RESET_INIT
},
271 { .name
= NULL
, .value
= -1 },
274 const char *debug_reason_name(struct target
*t
)
278 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
279 t
->debug_reason
)->name
;
281 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
282 cp
= "(*BUG*unknown*BUG*)";
287 const char *target_state_name(struct target
*t
)
290 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
292 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
293 cp
= "(*BUG*unknown*BUG*)";
296 if (!target_was_examined(t
) && t
->defer_examine
)
297 cp
= "examine deferred";
302 const char *target_event_name(enum target_event event
)
305 cp
= Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
;
307 LOG_ERROR("Invalid target event: %d", (int)(event
));
308 cp
= "(*BUG*unknown*BUG*)";
313 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
316 cp
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
;
318 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
319 cp
= "(*BUG*unknown*BUG*)";
324 /* determine the number of the new target */
325 static int new_target_number(void)
330 /* number is 0 based */
334 if (x
< t
->target_number
)
335 x
= t
->target_number
;
341 /* read a uint64_t from a buffer in target memory endianness */
342 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
344 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
345 return le_to_h_u64(buffer
);
347 return be_to_h_u64(buffer
);
350 /* read a uint32_t from a buffer in target memory endianness */
351 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
353 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
354 return le_to_h_u32(buffer
);
356 return be_to_h_u32(buffer
);
359 /* read a uint24_t from a buffer in target memory endianness */
360 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
362 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
363 return le_to_h_u24(buffer
);
365 return be_to_h_u24(buffer
);
368 /* read a uint16_t from a buffer in target memory endianness */
369 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
371 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
372 return le_to_h_u16(buffer
);
374 return be_to_h_u16(buffer
);
377 /* read a uint8_t from a buffer in target memory endianness */
378 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
380 return *buffer
& 0x0ff;
383 /* write a uint64_t to a buffer in target memory endianness */
384 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
386 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
387 h_u64_to_le(buffer
, value
);
389 h_u64_to_be(buffer
, value
);
392 /* write a uint32_t to a buffer in target memory endianness */
393 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
395 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
396 h_u32_to_le(buffer
, value
);
398 h_u32_to_be(buffer
, value
);
401 /* write a uint24_t to a buffer in target memory endianness */
402 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
404 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
405 h_u24_to_le(buffer
, value
);
407 h_u24_to_be(buffer
, value
);
410 /* write a uint16_t to a buffer in target memory endianness */
411 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
413 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
414 h_u16_to_le(buffer
, value
);
416 h_u16_to_be(buffer
, value
);
419 /* write a uint8_t to a buffer in target memory endianness */
420 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
425 /* write a uint64_t array to a buffer in target memory endianness */
426 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
429 for (i
= 0; i
< count
; i
++)
430 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
433 /* write a uint32_t array to a buffer in target memory endianness */
434 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
437 for (i
= 0; i
< count
; i
++)
438 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
441 /* write a uint16_t array to a buffer in target memory endianness */
442 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
445 for (i
= 0; i
< count
; i
++)
446 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
449 /* write a uint64_t array to a buffer in target memory endianness */
450 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
453 for (i
= 0; i
< count
; i
++)
454 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
457 /* write a uint32_t array to a buffer in target memory endianness */
458 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
461 for (i
= 0; i
< count
; i
++)
462 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
465 /* write a uint16_t array to a buffer in target memory endianness */
466 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
469 for (i
= 0; i
< count
; i
++)
470 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
473 /* return a pointer to a configured target; id is name or number */
474 struct target
*get_target(const char *id
)
476 struct target
*target
;
478 /* try as tcltarget name */
479 for (target
= all_targets
; target
; target
= target
->next
) {
480 if (target_name(target
) == NULL
)
482 if (strcmp(id
, target_name(target
)) == 0)
486 /* It's OK to remove this fallback sometime after August 2010 or so */
488 /* no match, try as number */
490 if (parse_uint(id
, &num
) != ERROR_OK
)
493 for (target
= all_targets
; target
; target
= target
->next
) {
494 if (target
->target_number
== (int)num
) {
495 LOG_WARNING("use '%s' as target identifier, not '%u'",
496 target_name(target
), num
);
504 /* returns a pointer to the n-th configured target */
505 struct target
*get_target_by_num(int num
)
507 struct target
*target
= all_targets
;
510 if (target
->target_number
== num
)
512 target
= target
->next
;
518 struct target
*get_current_target(struct command_context
*cmd_ctx
)
520 struct target
*target
= get_current_target_or_null(cmd_ctx
);
522 if (target
== NULL
) {
523 LOG_ERROR("BUG: current_target out of bounds");
530 struct target
*get_current_target_or_null(struct command_context
*cmd_ctx
)
532 return cmd_ctx
->current_target_override
533 ? cmd_ctx
->current_target_override
534 : cmd_ctx
->current_target
;
537 int target_poll(struct target
*target
)
541 /* We can't poll until after examine */
542 if (!target_was_examined(target
)) {
543 /* Fail silently lest we pollute the log */
547 retval
= target
->type
->poll(target
);
548 if (retval
!= ERROR_OK
)
551 if (target
->halt_issued
) {
552 if (target
->state
== TARGET_HALTED
)
553 target
->halt_issued
= false;
555 int64_t t
= timeval_ms() - target
->halt_issued_time
;
556 if (t
> DEFAULT_HALT_TIMEOUT
) {
557 target
->halt_issued
= false;
558 LOG_INFO("Halt timed out, wake up GDB.");
559 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
567 int target_halt(struct target
*target
)
570 /* We can't poll until after examine */
571 if (!target_was_examined(target
)) {
572 LOG_ERROR("Target not examined yet");
576 retval
= target
->type
->halt(target
);
577 if (retval
!= ERROR_OK
)
580 target
->halt_issued
= true;
581 target
->halt_issued_time
= timeval_ms();
587 * Make the target (re)start executing using its saved execution
588 * context (possibly with some modifications).
590 * @param target Which target should start executing.
591 * @param current True to use the target's saved program counter instead
592 * of the address parameter
593 * @param address Optionally used as the program counter.
594 * @param handle_breakpoints True iff breakpoints at the resumption PC
595 * should be skipped. (For example, maybe execution was stopped by
596 * such a breakpoint, in which case it would be counterprodutive to
598 * @param debug_execution False if all working areas allocated by OpenOCD
599 * should be released and/or restored to their original contents.
600 * (This would for example be true to run some downloaded "helper"
601 * algorithm code, which resides in one such working buffer and uses
602 * another for data storage.)
604 * @todo Resolve the ambiguity about what the "debug_execution" flag
605 * signifies. For example, Target implementations don't agree on how
606 * it relates to invalidation of the register cache, or to whether
607 * breakpoints and watchpoints should be enabled. (It would seem wrong
608 * to enable breakpoints when running downloaded "helper" algorithms
609 * (debug_execution true), since the breakpoints would be set to match
610 * target firmware being debugged, not the helper algorithm.... and
611 * enabling them could cause such helpers to malfunction (for example,
612 * by overwriting data with a breakpoint instruction. On the other
613 * hand the infrastructure for running such helpers might use this
614 * procedure but rely on hardware breakpoint to detect termination.)
616 int target_resume(struct target
*target
, int current
, target_addr_t address
,
617 int handle_breakpoints
, int debug_execution
)
621 /* We can't poll until after examine */
622 if (!target_was_examined(target
)) {
623 LOG_ERROR("Target not examined yet");
627 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
629 /* note that resume *must* be asynchronous. The CPU can halt before
630 * we poll. The CPU can even halt at the current PC as a result of
631 * a software breakpoint being inserted by (a bug?) the application.
633 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
634 if (retval
!= ERROR_OK
)
637 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
642 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
647 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
648 if (n
->name
== NULL
) {
649 LOG_ERROR("invalid reset mode");
653 struct target
*target
;
654 for (target
= all_targets
; target
; target
= target
->next
)
655 target_call_reset_callbacks(target
, reset_mode
);
657 /* disable polling during reset to make reset event scripts
658 * more predictable, i.e. dr/irscan & pathmove in events will
659 * not have JTAG operations injected into the middle of a sequence.
661 bool save_poll
= jtag_poll_get_enabled();
663 jtag_poll_set_enabled(false);
665 sprintf(buf
, "ocd_process_reset %s", n
->name
);
666 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
668 jtag_poll_set_enabled(save_poll
);
670 if (retval
!= JIM_OK
) {
671 Jim_MakeErrorMessage(cmd_ctx
->interp
);
672 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
676 /* We want any events to be processed before the prompt */
677 retval
= target_call_timer_callbacks_now();
679 for (target
= all_targets
; target
; target
= target
->next
) {
680 target
->type
->check_reset(target
);
681 target
->running_alg
= false;
687 static int identity_virt2phys(struct target
*target
,
688 target_addr_t
virtual, target_addr_t
*physical
)
694 static int no_mmu(struct target
*target
, int *enabled
)
700 static int default_examine(struct target
*target
)
702 target_set_examined(target
);
706 /* no check by default */
707 static int default_check_reset(struct target
*target
)
712 int target_examine_one(struct target
*target
)
714 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
716 int retval
= target
->type
->examine(target
);
717 if (retval
!= ERROR_OK
)
720 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
725 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
727 struct target
*target
= priv
;
729 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
732 jtag_unregister_event_callback(jtag_enable_callback
, target
);
734 return target_examine_one(target
);
737 /* Targets that correctly implement init + examine, i.e.
738 * no communication with target during init:
742 int target_examine(void)
744 int retval
= ERROR_OK
;
745 struct target
*target
;
747 for (target
= all_targets
; target
; target
= target
->next
) {
748 /* defer examination, but don't skip it */
749 if (!target
->tap
->enabled
) {
750 jtag_register_event_callback(jtag_enable_callback
,
755 if (target
->defer_examine
)
758 retval
= target_examine_one(target
);
759 if (retval
!= ERROR_OK
)
765 const char *target_type_name(struct target
*target
)
767 return target
->type
->name
;
770 static int target_soft_reset_halt(struct target
*target
)
772 if (!target_was_examined(target
)) {
773 LOG_ERROR("Target not examined yet");
776 if (!target
->type
->soft_reset_halt
) {
777 LOG_ERROR("Target %s does not support soft_reset_halt",
778 target_name(target
));
781 return target
->type
->soft_reset_halt(target
);
785 * Downloads a target-specific native code algorithm to the target,
786 * and executes it. * Note that some targets may need to set up, enable,
787 * and tear down a breakpoint (hard or * soft) to detect algorithm
788 * termination, while others may support lower overhead schemes where
789 * soft breakpoints embedded in the algorithm automatically terminate the
792 * @param target used to run the algorithm
793 * @param arch_info target-specific description of the algorithm.
795 int target_run_algorithm(struct target
*target
,
796 int num_mem_params
, struct mem_param
*mem_params
,
797 int num_reg_params
, struct reg_param
*reg_param
,
798 uint32_t entry_point
, uint32_t exit_point
,
799 int timeout_ms
, void *arch_info
)
801 int retval
= ERROR_FAIL
;
803 if (!target_was_examined(target
)) {
804 LOG_ERROR("Target not examined yet");
807 if (!target
->type
->run_algorithm
) {
808 LOG_ERROR("Target type '%s' does not support %s",
809 target_type_name(target
), __func__
);
813 target
->running_alg
= true;
814 retval
= target
->type
->run_algorithm(target
,
815 num_mem_params
, mem_params
,
816 num_reg_params
, reg_param
,
817 entry_point
, exit_point
, timeout_ms
, arch_info
);
818 target
->running_alg
= false;
825 * Executes a target-specific native code algorithm and leaves it running.
827 * @param target used to run the algorithm
828 * @param arch_info target-specific description of the algorithm.
830 int target_start_algorithm(struct target
*target
,
831 int num_mem_params
, struct mem_param
*mem_params
,
832 int num_reg_params
, struct reg_param
*reg_params
,
833 uint32_t entry_point
, uint32_t exit_point
,
836 int retval
= ERROR_FAIL
;
838 if (!target_was_examined(target
)) {
839 LOG_ERROR("Target not examined yet");
842 if (!target
->type
->start_algorithm
) {
843 LOG_ERROR("Target type '%s' does not support %s",
844 target_type_name(target
), __func__
);
847 if (target
->running_alg
) {
848 LOG_ERROR("Target is already running an algorithm");
852 target
->running_alg
= true;
853 retval
= target
->type
->start_algorithm(target
,
854 num_mem_params
, mem_params
,
855 num_reg_params
, reg_params
,
856 entry_point
, exit_point
, arch_info
);
863 * Waits for an algorithm started with target_start_algorithm() to complete.
865 * @param target used to run the algorithm
866 * @param arch_info target-specific description of the algorithm.
868 int target_wait_algorithm(struct target
*target
,
869 int num_mem_params
, struct mem_param
*mem_params
,
870 int num_reg_params
, struct reg_param
*reg_params
,
871 uint32_t exit_point
, int timeout_ms
,
874 int retval
= ERROR_FAIL
;
876 if (!target
->type
->wait_algorithm
) {
877 LOG_ERROR("Target type '%s' does not support %s",
878 target_type_name(target
), __func__
);
881 if (!target
->running_alg
) {
882 LOG_ERROR("Target is not running an algorithm");
886 retval
= target
->type
->wait_algorithm(target
,
887 num_mem_params
, mem_params
,
888 num_reg_params
, reg_params
,
889 exit_point
, timeout_ms
, arch_info
);
890 if (retval
!= ERROR_TARGET_TIMEOUT
)
891 target
->running_alg
= false;
898 * Streams data to a circular buffer on target intended for consumption by code
899 * running asynchronously on target.
901 * This is intended for applications where target-specific native code runs
902 * on the target, receives data from the circular buffer, does something with
903 * it (most likely writing it to a flash memory), and advances the circular
906 * This assumes that the helper algorithm has already been loaded to the target,
907 * but has not been started yet. Given memory and register parameters are passed
910 * The buffer is defined by (buffer_start, buffer_size) arguments and has the
913 * [buffer_start + 0, buffer_start + 4):
914 * Write Pointer address (aka head). Written and updated by this
915 * routine when new data is written to the circular buffer.
916 * [buffer_start + 4, buffer_start + 8):
917 * Read Pointer address (aka tail). Updated by code running on the
918 * target after it consumes data.
919 * [buffer_start + 8, buffer_start + buffer_size):
920 * Circular buffer contents.
922 * See contrib/loaders/flash/stm32f1x.S for an example.
924 * @param target used to run the algorithm
925 * @param buffer address on the host where data to be sent is located
926 * @param count number of blocks to send
927 * @param block_size size in bytes of each block
928 * @param num_mem_params count of memory-based params to pass to algorithm
929 * @param mem_params memory-based params to pass to algorithm
930 * @param num_reg_params count of register-based params to pass to algorithm
931 * @param reg_params memory-based params to pass to algorithm
932 * @param buffer_start address on the target of the circular buffer structure
933 * @param buffer_size size of the circular buffer structure
934 * @param entry_point address on the target to execute to start the algorithm
935 * @param exit_point address at which to set a breakpoint to catch the
936 * end of the algorithm; can be 0 if target triggers a breakpoint itself
939 int target_run_flash_async_algorithm(struct target
*target
,
940 const uint8_t *buffer
, uint32_t count
, int block_size
,
941 int num_mem_params
, struct mem_param
*mem_params
,
942 int num_reg_params
, struct reg_param
*reg_params
,
943 uint32_t buffer_start
, uint32_t buffer_size
,
944 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
949 const uint8_t *buffer_orig
= buffer
;
951 /* Set up working area. First word is write pointer, second word is read pointer,
952 * rest is fifo data area. */
953 uint32_t wp_addr
= buffer_start
;
954 uint32_t rp_addr
= buffer_start
+ 4;
955 uint32_t fifo_start_addr
= buffer_start
+ 8;
956 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
958 uint32_t wp
= fifo_start_addr
;
959 uint32_t rp
= fifo_start_addr
;
961 /* validate block_size is 2^n */
962 assert(!block_size
|| !(block_size
& (block_size
- 1)));
964 retval
= target_write_u32(target
, wp_addr
, wp
);
965 if (retval
!= ERROR_OK
)
967 retval
= target_write_u32(target
, rp_addr
, rp
);
968 if (retval
!= ERROR_OK
)
971 /* Start up algorithm on target and let it idle while writing the first chunk */
972 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
973 num_reg_params
, reg_params
,
978 if (retval
!= ERROR_OK
) {
979 LOG_ERROR("error starting target flash write algorithm");
985 retval
= target_read_u32(target
, rp_addr
, &rp
);
986 if (retval
!= ERROR_OK
) {
987 LOG_ERROR("failed to get read pointer");
991 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
992 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
995 LOG_ERROR("flash write algorithm aborted by target");
996 retval
= ERROR_FLASH_OPERATION_FAILED
;
1000 if (((rp
- fifo_start_addr
) & (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
1001 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
1005 /* Count the number of bytes available in the fifo without
1006 * crossing the wrap around. Make sure to not fill it completely,
1007 * because that would make wp == rp and that's the empty condition. */
1008 uint32_t thisrun_bytes
;
1010 thisrun_bytes
= rp
- wp
- block_size
;
1011 else if (rp
> fifo_start_addr
)
1012 thisrun_bytes
= fifo_end_addr
- wp
;
1014 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
1016 if (thisrun_bytes
== 0) {
1017 /* Throttle polling a bit if transfer is (much) faster than flash
1018 * programming. The exact delay shouldn't matter as long as it's
1019 * less than buffer size / flash speed. This is very unlikely to
1020 * run when using high latency connections such as USB. */
1023 /* to stop an infinite loop on some targets check and increment a timeout
1024 * this issue was observed on a stellaris using the new ICDI interface */
1025 if (timeout
++ >= 500) {
1026 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1027 return ERROR_FLASH_OPERATION_FAILED
;
1032 /* reset our timeout */
1035 /* Limit to the amount of data we actually want to write */
1036 if (thisrun_bytes
> count
* block_size
)
1037 thisrun_bytes
= count
* block_size
;
1039 /* Write data to fifo */
1040 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
1041 if (retval
!= ERROR_OK
)
1044 /* Update counters and wrap write pointer */
1045 buffer
+= thisrun_bytes
;
1046 count
-= thisrun_bytes
/ block_size
;
1047 wp
+= thisrun_bytes
;
1048 if (wp
>= fifo_end_addr
)
1049 wp
= fifo_start_addr
;
1051 /* Store updated write pointer to target */
1052 retval
= target_write_u32(target
, wp_addr
, wp
);
1053 if (retval
!= ERROR_OK
)
1056 /* Avoid GDB timeouts */
1060 if (retval
!= ERROR_OK
) {
1061 /* abort flash write algorithm on target */
1062 target_write_u32(target
, wp_addr
, 0);
1065 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1066 num_reg_params
, reg_params
,
1071 if (retval2
!= ERROR_OK
) {
1072 LOG_ERROR("error waiting for target flash write algorithm");
1076 if (retval
== ERROR_OK
) {
1077 /* check if algorithm set rp = 0 after fifo writer loop finished */
1078 retval
= target_read_u32(target
, rp_addr
, &rp
);
1079 if (retval
== ERROR_OK
&& rp
== 0) {
1080 LOG_ERROR("flash write algorithm aborted by target");
1081 retval
= ERROR_FLASH_OPERATION_FAILED
;
1088 int target_read_memory(struct target
*target
,
1089 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1091 if (!target_was_examined(target
)) {
1092 LOG_ERROR("Target not examined yet");
1095 if (!target
->type
->read_memory
) {
1096 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1099 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1102 int target_read_phys_memory(struct target
*target
,
1103 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1105 if (!target_was_examined(target
)) {
1106 LOG_ERROR("Target not examined yet");
1109 if (!target
->type
->read_phys_memory
) {
1110 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1113 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1116 int target_write_memory(struct target
*target
,
1117 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1119 if (!target_was_examined(target
)) {
1120 LOG_ERROR("Target not examined yet");
1123 if (!target
->type
->write_memory
) {
1124 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1127 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1130 int target_write_phys_memory(struct target
*target
,
1131 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1133 if (!target_was_examined(target
)) {
1134 LOG_ERROR("Target not examined yet");
1137 if (!target
->type
->write_phys_memory
) {
1138 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1141 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1144 int target_add_breakpoint(struct target
*target
,
1145 struct breakpoint
*breakpoint
)
1147 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1148 LOG_WARNING("target %s is not halted (add breakpoint)", target_name(target
));
1149 return ERROR_TARGET_NOT_HALTED
;
1151 return target
->type
->add_breakpoint(target
, breakpoint
);
1154 int target_add_context_breakpoint(struct target
*target
,
1155 struct breakpoint
*breakpoint
)
1157 if (target
->state
!= TARGET_HALTED
) {
1158 LOG_WARNING("target %s is not halted (add context breakpoint)", target_name(target
));
1159 return ERROR_TARGET_NOT_HALTED
;
1161 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1164 int target_add_hybrid_breakpoint(struct target
*target
,
1165 struct breakpoint
*breakpoint
)
1167 if (target
->state
!= TARGET_HALTED
) {
1168 LOG_WARNING("target %s is not halted (add hybrid breakpoint)", target_name(target
));
1169 return ERROR_TARGET_NOT_HALTED
;
1171 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1174 int target_remove_breakpoint(struct target
*target
,
1175 struct breakpoint
*breakpoint
)
1177 return target
->type
->remove_breakpoint(target
, breakpoint
);
1180 int target_add_watchpoint(struct target
*target
,
1181 struct watchpoint
*watchpoint
)
1183 if (target
->state
!= TARGET_HALTED
) {
1184 LOG_WARNING("target %s is not halted (add watchpoint)", target_name(target
));
1185 return ERROR_TARGET_NOT_HALTED
;
1187 return target
->type
->add_watchpoint(target
, watchpoint
);
1189 int target_remove_watchpoint(struct target
*target
,
1190 struct watchpoint
*watchpoint
)
1192 return target
->type
->remove_watchpoint(target
, watchpoint
);
1194 int target_hit_watchpoint(struct target
*target
,
1195 struct watchpoint
**hit_watchpoint
)
1197 if (target
->state
!= TARGET_HALTED
) {
1198 LOG_WARNING("target %s is not halted (hit watchpoint)", target
->cmd_name
);
1199 return ERROR_TARGET_NOT_HALTED
;
1202 if (target
->type
->hit_watchpoint
== NULL
) {
1203 /* For backward compatible, if hit_watchpoint is not implemented,
1204 * return ERROR_FAIL such that gdb_server will not take the nonsense
1209 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1212 const char *target_get_gdb_arch(struct target
*target
)
1214 if (target
->type
->get_gdb_arch
== NULL
)
1216 return target
->type
->get_gdb_arch(target
);
1219 int target_get_gdb_reg_list(struct target
*target
,
1220 struct reg
**reg_list
[], int *reg_list_size
,
1221 enum target_register_class reg_class
)
1223 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1226 bool target_supports_gdb_connection(struct target
*target
)
1229 * based on current code, we can simply exclude all the targets that
1230 * don't provide get_gdb_reg_list; this could change with new targets.
1232 return !!target
->type
->get_gdb_reg_list
;
1235 int target_step(struct target
*target
,
1236 int current
, target_addr_t address
, int handle_breakpoints
)
1238 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1241 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1243 if (target
->state
!= TARGET_HALTED
) {
1244 LOG_WARNING("target %s is not halted (gdb fileio)", target
->cmd_name
);
1245 return ERROR_TARGET_NOT_HALTED
;
1247 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1250 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1252 if (target
->state
!= TARGET_HALTED
) {
1253 LOG_WARNING("target %s is not halted (gdb fileio end)", target
->cmd_name
);
1254 return ERROR_TARGET_NOT_HALTED
;
1256 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1259 int target_profiling(struct target
*target
, uint32_t *samples
,
1260 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1262 if (target
->state
!= TARGET_HALTED
) {
1263 LOG_WARNING("target %s is not halted (profiling)", target
->cmd_name
);
1264 return ERROR_TARGET_NOT_HALTED
;
1266 return target
->type
->profiling(target
, samples
, max_num_samples
,
1267 num_samples
, seconds
);
1271 * Reset the @c examined flag for the given target.
1272 * Pure paranoia -- targets are zeroed on allocation.
1274 static void target_reset_examined(struct target
*target
)
1276 target
->examined
= false;
1279 static int handle_target(void *priv
);
1281 static int target_init_one(struct command_context
*cmd_ctx
,
1282 struct target
*target
)
1284 target_reset_examined(target
);
1286 struct target_type
*type
= target
->type
;
1287 if (type
->examine
== NULL
)
1288 type
->examine
= default_examine
;
1290 if (type
->check_reset
== NULL
)
1291 type
->check_reset
= default_check_reset
;
1293 assert(type
->init_target
!= NULL
);
1295 int retval
= type
->init_target(cmd_ctx
, target
);
1296 if (ERROR_OK
!= retval
) {
1297 LOG_ERROR("target '%s' init failed", target_name(target
));
1301 /* Sanity-check MMU support ... stub in what we must, to help
1302 * implement it in stages, but warn if we need to do so.
1305 if (type
->virt2phys
== NULL
) {
1306 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1307 type
->virt2phys
= identity_virt2phys
;
1310 /* Make sure no-MMU targets all behave the same: make no
1311 * distinction between physical and virtual addresses, and
1312 * ensure that virt2phys() is always an identity mapping.
1314 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1315 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1318 type
->write_phys_memory
= type
->write_memory
;
1319 type
->read_phys_memory
= type
->read_memory
;
1320 type
->virt2phys
= identity_virt2phys
;
1323 if (target
->type
->read_buffer
== NULL
)
1324 target
->type
->read_buffer
= target_read_buffer_default
;
1326 if (target
->type
->write_buffer
== NULL
)
1327 target
->type
->write_buffer
= target_write_buffer_default
;
1329 if (target
->type
->get_gdb_fileio_info
== NULL
)
1330 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1332 if (target
->type
->gdb_fileio_end
== NULL
)
1333 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1335 if (target
->type
->profiling
== NULL
)
1336 target
->type
->profiling
= target_profiling_default
;
1341 static int target_init(struct command_context
*cmd_ctx
)
1343 struct target
*target
;
1346 for (target
= all_targets
; target
; target
= target
->next
) {
1347 retval
= target_init_one(cmd_ctx
, target
);
1348 if (ERROR_OK
!= retval
)
1355 retval
= target_register_user_commands(cmd_ctx
);
1356 if (ERROR_OK
!= retval
)
1359 retval
= target_register_timer_callback(&handle_target
,
1360 polling_interval
, 1, cmd_ctx
->interp
);
1361 if (ERROR_OK
!= retval
)
1367 COMMAND_HANDLER(handle_target_init_command
)
1372 return ERROR_COMMAND_SYNTAX_ERROR
;
1374 static bool target_initialized
;
1375 if (target_initialized
) {
1376 LOG_INFO("'target init' has already been called");
1379 target_initialized
= true;
1381 retval
= command_run_line(CMD_CTX
, "init_targets");
1382 if (ERROR_OK
!= retval
)
1385 retval
= command_run_line(CMD_CTX
, "init_target_events");
1386 if (ERROR_OK
!= retval
)
1389 retval
= command_run_line(CMD_CTX
, "init_board");
1390 if (ERROR_OK
!= retval
)
1393 LOG_DEBUG("Initializing targets...");
1394 return target_init(CMD_CTX
);
1397 int target_register_event_callback(int (*callback
)(struct target
*target
,
1398 enum target_event event
, void *priv
), void *priv
)
1400 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1402 if (callback
== NULL
)
1403 return ERROR_COMMAND_SYNTAX_ERROR
;
1406 while ((*callbacks_p
)->next
)
1407 callbacks_p
= &((*callbacks_p
)->next
);
1408 callbacks_p
= &((*callbacks_p
)->next
);
1411 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1412 (*callbacks_p
)->callback
= callback
;
1413 (*callbacks_p
)->priv
= priv
;
1414 (*callbacks_p
)->next
= NULL
;
1419 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1420 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1422 struct target_reset_callback
*entry
;
1424 if (callback
== NULL
)
1425 return ERROR_COMMAND_SYNTAX_ERROR
;
1427 entry
= malloc(sizeof(struct target_reset_callback
));
1428 if (entry
== NULL
) {
1429 LOG_ERROR("error allocating buffer for reset callback entry");
1430 return ERROR_COMMAND_SYNTAX_ERROR
;
1433 entry
->callback
= callback
;
1435 list_add(&entry
->list
, &target_reset_callback_list
);
1441 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1442 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1444 struct target_trace_callback
*entry
;
1446 if (callback
== NULL
)
1447 return ERROR_COMMAND_SYNTAX_ERROR
;
1449 entry
= malloc(sizeof(struct target_trace_callback
));
1450 if (entry
== NULL
) {
1451 LOG_ERROR("error allocating buffer for trace callback entry");
1452 return ERROR_COMMAND_SYNTAX_ERROR
;
1455 entry
->callback
= callback
;
1457 list_add(&entry
->list
, &target_trace_callback_list
);
1463 int target_register_timer_callback(int (*callback
)(void *priv
),
1464 unsigned int time_ms
, enum target_timer_type type
, void *priv
)
1466 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1468 if (callback
== NULL
)
1469 return ERROR_COMMAND_SYNTAX_ERROR
;
1472 while ((*callbacks_p
)->next
)
1473 callbacks_p
= &((*callbacks_p
)->next
);
1474 callbacks_p
= &((*callbacks_p
)->next
);
1477 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1478 (*callbacks_p
)->callback
= callback
;
1479 (*callbacks_p
)->type
= type
;
1480 (*callbacks_p
)->time_ms
= time_ms
;
1481 (*callbacks_p
)->removed
= false;
1483 gettimeofday(&(*callbacks_p
)->when
, NULL
);
1484 timeval_add_time(&(*callbacks_p
)->when
, 0, time_ms
* 1000);
1486 (*callbacks_p
)->priv
= priv
;
1487 (*callbacks_p
)->next
= NULL
;
1492 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1493 enum target_event event
, void *priv
), void *priv
)
1495 struct target_event_callback
**p
= &target_event_callbacks
;
1496 struct target_event_callback
*c
= target_event_callbacks
;
1498 if (callback
== NULL
)
1499 return ERROR_COMMAND_SYNTAX_ERROR
;
1502 struct target_event_callback
*next
= c
->next
;
1503 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1515 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1516 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1518 struct target_reset_callback
*entry
;
1520 if (callback
== NULL
)
1521 return ERROR_COMMAND_SYNTAX_ERROR
;
1523 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1524 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1525 list_del(&entry
->list
);
1534 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1535 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1537 struct target_trace_callback
*entry
;
1539 if (callback
== NULL
)
1540 return ERROR_COMMAND_SYNTAX_ERROR
;
1542 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1543 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1544 list_del(&entry
->list
);
1553 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1555 if (callback
== NULL
)
1556 return ERROR_COMMAND_SYNTAX_ERROR
;
1558 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1560 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1569 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1571 struct target_event_callback
*callback
= target_event_callbacks
;
1572 struct target_event_callback
*next_callback
;
1574 if (event
== TARGET_EVENT_HALTED
) {
1575 /* execute early halted first */
1576 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1579 LOG_DEBUG("target event %i (%s)", event
,
1580 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1582 target_handle_event(target
, event
);
1585 next_callback
= callback
->next
;
1586 callback
->callback(target
, event
, callback
->priv
);
1587 callback
= next_callback
;
1593 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1595 struct target_reset_callback
*callback
;
1597 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1598 Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1600 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1601 callback
->callback(target
, reset_mode
, callback
->priv
);
1606 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1608 struct target_trace_callback
*callback
;
1610 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1611 callback
->callback(target
, len
, data
, callback
->priv
);
1616 static int target_timer_callback_periodic_restart(
1617 struct target_timer_callback
*cb
, struct timeval
*now
)
1620 timeval_add_time(&cb
->when
, 0, cb
->time_ms
* 1000L);
1624 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1625 struct timeval
*now
)
1627 cb
->callback(cb
->priv
);
1629 if (cb
->type
== TARGET_TIMER_TYPE_PERIODIC
)
1630 return target_timer_callback_periodic_restart(cb
, now
);
1632 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1635 static int target_call_timer_callbacks_check_time(int checktime
)
1637 static bool callback_processing
;
1639 /* Do not allow nesting */
1640 if (callback_processing
)
1643 callback_processing
= true;
1648 gettimeofday(&now
, NULL
);
1650 /* Store an address of the place containing a pointer to the
1651 * next item; initially, that's a standalone "root of the
1652 * list" variable. */
1653 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1655 if ((*callback
)->removed
) {
1656 struct target_timer_callback
*p
= *callback
;
1657 *callback
= (*callback
)->next
;
1662 bool call_it
= (*callback
)->callback
&&
1663 ((!checktime
&& (*callback
)->type
== TARGET_TIMER_TYPE_PERIODIC
) ||
1664 timeval_compare(&now
, &(*callback
)->when
) >= 0);
1667 target_call_timer_callback(*callback
, &now
);
1669 callback
= &(*callback
)->next
;
1672 callback_processing
= false;
1676 int target_call_timer_callbacks(void)
1678 return target_call_timer_callbacks_check_time(1);
1681 /* invoke periodic callbacks immediately */
1682 int target_call_timer_callbacks_now(void)
1684 return target_call_timer_callbacks_check_time(0);
1687 /* Prints the working area layout for debug purposes */
1688 static void print_wa_layout(struct target
*target
)
1690 struct working_area
*c
= target
->working_areas
;
1693 LOG_DEBUG("%c%c " TARGET_ADDR_FMT
"-" TARGET_ADDR_FMT
" (%" PRIu32
" bytes)",
1694 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1695 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1700 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1701 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1703 assert(area
->free
); /* Shouldn't split an allocated area */
1704 assert(size
<= area
->size
); /* Caller should guarantee this */
1706 /* Split only if not already the right size */
1707 if (size
< area
->size
) {
1708 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1713 new_wa
->next
= area
->next
;
1714 new_wa
->size
= area
->size
- size
;
1715 new_wa
->address
= area
->address
+ size
;
1716 new_wa
->backup
= NULL
;
1717 new_wa
->user
= NULL
;
1718 new_wa
->free
= true;
1720 area
->next
= new_wa
;
1723 /* If backup memory was allocated to this area, it has the wrong size
1724 * now so free it and it will be reallocated if/when needed */
1727 area
->backup
= NULL
;
1732 /* Merge all adjacent free areas into one */
1733 static void target_merge_working_areas(struct target
*target
)
1735 struct working_area
*c
= target
->working_areas
;
1737 while (c
&& c
->next
) {
1738 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1740 /* Find two adjacent free areas */
1741 if (c
->free
&& c
->next
->free
) {
1742 /* Merge the last into the first */
1743 c
->size
+= c
->next
->size
;
1745 /* Remove the last */
1746 struct working_area
*to_be_freed
= c
->next
;
1747 c
->next
= c
->next
->next
;
1748 if (to_be_freed
->backup
)
1749 free(to_be_freed
->backup
);
1752 /* If backup memory was allocated to the remaining area, it's has
1753 * the wrong size now */
1764 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1766 /* Reevaluate working area address based on MMU state*/
1767 if (target
->working_areas
== NULL
) {
1771 retval
= target
->type
->mmu(target
, &enabled
);
1772 if (retval
!= ERROR_OK
)
1776 if (target
->working_area_phys_spec
) {
1777 LOG_DEBUG("MMU disabled, using physical "
1778 "address for working memory " TARGET_ADDR_FMT
,
1779 target
->working_area_phys
);
1780 target
->working_area
= target
->working_area_phys
;
1782 LOG_ERROR("No working memory available. "
1783 "Specify -work-area-phys to target.");
1784 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1787 if (target
->working_area_virt_spec
) {
1788 LOG_DEBUG("MMU enabled, using virtual "
1789 "address for working memory " TARGET_ADDR_FMT
,
1790 target
->working_area_virt
);
1791 target
->working_area
= target
->working_area_virt
;
1793 LOG_ERROR("No working memory available. "
1794 "Specify -work-area-virt to target.");
1795 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1799 /* Set up initial working area on first call */
1800 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1802 new_wa
->next
= NULL
;
1803 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1804 new_wa
->address
= target
->working_area
;
1805 new_wa
->backup
= NULL
;
1806 new_wa
->user
= NULL
;
1807 new_wa
->free
= true;
1810 target
->working_areas
= new_wa
;
1813 /* only allocate multiples of 4 byte */
1815 size
= (size
+ 3) & (~3UL);
1817 struct working_area
*c
= target
->working_areas
;
1819 /* Find the first large enough working area */
1821 if (c
->free
&& c
->size
>= size
)
1827 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1829 /* Split the working area into the requested size */
1830 target_split_working_area(c
, size
);
1832 LOG_DEBUG("allocated new working area of %" PRIu32
" bytes at address " TARGET_ADDR_FMT
,
1835 if (target
->backup_working_area
) {
1836 if (c
->backup
== NULL
) {
1837 c
->backup
= malloc(c
->size
);
1838 if (c
->backup
== NULL
)
1842 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1843 if (retval
!= ERROR_OK
)
1847 /* mark as used, and return the new (reused) area */
1854 print_wa_layout(target
);
1859 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1863 retval
= target_alloc_working_area_try(target
, size
, area
);
1864 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1865 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1870 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1872 int retval
= ERROR_OK
;
1874 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1875 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1876 if (retval
!= ERROR_OK
)
1877 LOG_ERROR("failed to restore %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1878 area
->size
, area
->address
);
1884 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1885 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1887 int retval
= ERROR_OK
;
1893 retval
= target_restore_working_area(target
, area
);
1894 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1895 if (retval
!= ERROR_OK
)
1901 LOG_DEBUG("freed %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1902 area
->size
, area
->address
);
1904 /* mark user pointer invalid */
1905 /* TODO: Is this really safe? It points to some previous caller's memory.
1906 * How could we know that the area pointer is still in that place and not
1907 * some other vital data? What's the purpose of this, anyway? */
1911 target_merge_working_areas(target
);
1913 print_wa_layout(target
);
1918 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1920 return target_free_working_area_restore(target
, area
, 1);
1923 /* free resources and restore memory, if restoring memory fails,
1924 * free up resources anyway
1926 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1928 struct working_area
*c
= target
->working_areas
;
1930 LOG_DEBUG("freeing all working areas");
1932 /* Loop through all areas, restoring the allocated ones and marking them as free */
1936 target_restore_working_area(target
, c
);
1938 *c
->user
= NULL
; /* Same as above */
1944 /* Run a merge pass to combine all areas into one */
1945 target_merge_working_areas(target
);
1947 print_wa_layout(target
);
1950 void target_free_all_working_areas(struct target
*target
)
1952 target_free_all_working_areas_restore(target
, 1);
1954 /* Now we have none or only one working area marked as free */
1955 if (target
->working_areas
) {
1956 /* Free the last one to allow on-the-fly moving and resizing */
1957 free(target
->working_areas
->backup
);
1958 free(target
->working_areas
);
1959 target
->working_areas
= NULL
;
1963 /* Find the largest number of bytes that can be allocated */
1964 uint32_t target_get_working_area_avail(struct target
*target
)
1966 struct working_area
*c
= target
->working_areas
;
1967 uint32_t max_size
= 0;
1970 return target
->working_area_size
;
1973 if (c
->free
&& max_size
< c
->size
)
1982 static void target_destroy(struct target
*target
)
1984 if (target
->type
->deinit_target
)
1985 target
->type
->deinit_target(target
);
1987 if (target
->semihosting
)
1988 free(target
->semihosting
);
1990 jtag_unregister_event_callback(jtag_enable_callback
, target
);
1992 struct target_event_action
*teap
= target
->event_action
;
1994 struct target_event_action
*next
= teap
->next
;
1995 Jim_DecrRefCount(teap
->interp
, teap
->body
);
2000 target_free_all_working_areas(target
);
2002 /* release the targets SMP list */
2004 struct target_list
*head
= target
->head
;
2005 while (head
!= NULL
) {
2006 struct target_list
*pos
= head
->next
;
2007 head
->target
->smp
= 0;
2014 free(target
->gdb_port_override
);
2016 free(target
->trace_info
);
2017 free(target
->fileio_info
);
2018 free(target
->cmd_name
);
2022 void target_quit(void)
2024 struct target_event_callback
*pe
= target_event_callbacks
;
2026 struct target_event_callback
*t
= pe
->next
;
2030 target_event_callbacks
= NULL
;
2032 struct target_timer_callback
*pt
= target_timer_callbacks
;
2034 struct target_timer_callback
*t
= pt
->next
;
2038 target_timer_callbacks
= NULL
;
2040 for (struct target
*target
= all_targets
; target
;) {
2044 target_destroy(target
);
2051 int target_arch_state(struct target
*target
)
2054 if (target
== NULL
) {
2055 LOG_WARNING("No target has been configured");
2059 if (target
->state
!= TARGET_HALTED
)
2062 retval
= target
->type
->arch_state(target
);
2066 static int target_get_gdb_fileio_info_default(struct target
*target
,
2067 struct gdb_fileio_info
*fileio_info
)
2069 /* If target does not support semi-hosting function, target
2070 has no need to provide .get_gdb_fileio_info callback.
2071 It just return ERROR_FAIL and gdb_server will return "Txx"
2072 as target halted every time. */
2076 static int target_gdb_fileio_end_default(struct target
*target
,
2077 int retcode
, int fileio_errno
, bool ctrl_c
)
2082 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
2083 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
2085 struct timeval timeout
, now
;
2087 gettimeofday(&timeout
, NULL
);
2088 timeval_add_time(&timeout
, seconds
, 0);
2090 LOG_INFO("Starting profiling. Halting and resuming the"
2091 " target as often as we can...");
2093 uint32_t sample_count
= 0;
2094 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2095 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2097 int retval
= ERROR_OK
;
2099 target_poll(target
);
2100 if (target
->state
== TARGET_HALTED
) {
2101 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
2102 samples
[sample_count
++] = t
;
2103 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2104 retval
= target_resume(target
, 1, 0, 0, 0);
2105 target_poll(target
);
2106 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2107 } else if (target
->state
== TARGET_RUNNING
) {
2108 /* We want to quickly sample the PC. */
2109 retval
= target_halt(target
);
2111 LOG_INFO("Target not halted or running");
2116 if (retval
!= ERROR_OK
)
2119 gettimeofday(&now
, NULL
);
2120 if ((sample_count
>= max_num_samples
) || timeval_compare(&now
, &timeout
) >= 0) {
2121 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2126 *num_samples
= sample_count
;
2130 /* Single aligned words are guaranteed to use 16 or 32 bit access
2131 * mode respectively, otherwise data is handled as quickly as
2134 int target_write_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, const uint8_t *buffer
)
2136 LOG_DEBUG("writing buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2139 if (!target_was_examined(target
)) {
2140 LOG_ERROR("Target not examined yet");
2147 if ((address
+ size
- 1) < address
) {
2148 /* GDB can request this when e.g. PC is 0xfffffffc */
2149 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2155 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2158 static int target_write_buffer_default(struct target
*target
,
2159 target_addr_t address
, uint32_t count
, const uint8_t *buffer
)
2163 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2164 * will have something to do with the size we leave to it. */
2165 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2166 if (address
& size
) {
2167 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2168 if (retval
!= ERROR_OK
)
2176 /* Write the data with as large access size as possible. */
2177 for (; size
> 0; size
/= 2) {
2178 uint32_t aligned
= count
- count
% size
;
2180 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2181 if (retval
!= ERROR_OK
)
2192 /* Single aligned words are guaranteed to use 16 or 32 bit access
2193 * mode respectively, otherwise data is handled as quickly as
2196 int target_read_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
2198 LOG_DEBUG("reading buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2201 if (!target_was_examined(target
)) {
2202 LOG_ERROR("Target not examined yet");
2209 if ((address
+ size
- 1) < address
) {
2210 /* GDB can request this when e.g. PC is 0xfffffffc */
2211 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2217 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2220 static int target_read_buffer_default(struct target
*target
, target_addr_t address
, uint32_t count
, uint8_t *buffer
)
2224 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2225 * will have something to do with the size we leave to it. */
2226 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2227 if (address
& size
) {
2228 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2229 if (retval
!= ERROR_OK
)
2237 /* Read the data with as large access size as possible. */
2238 for (; size
> 0; size
/= 2) {
2239 uint32_t aligned
= count
- count
% size
;
2241 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2242 if (retval
!= ERROR_OK
)
2253 int target_checksum_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* crc
)
2258 uint32_t checksum
= 0;
2259 if (!target_was_examined(target
)) {
2260 LOG_ERROR("Target not examined yet");
2264 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2265 if (retval
!= ERROR_OK
) {
2266 buffer
= malloc(size
);
2267 if (buffer
== NULL
) {
2268 LOG_ERROR("error allocating buffer for section (%" PRId32
" bytes)", size
);
2269 return ERROR_COMMAND_SYNTAX_ERROR
;
2271 retval
= target_read_buffer(target
, address
, size
, buffer
);
2272 if (retval
!= ERROR_OK
) {
2277 /* convert to target endianness */
2278 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2279 uint32_t target_data
;
2280 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2281 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2284 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2293 int target_blank_check_memory(struct target
*target
,
2294 struct target_memory_check_block
*blocks
, int num_blocks
,
2295 uint8_t erased_value
)
2297 if (!target_was_examined(target
)) {
2298 LOG_ERROR("Target not examined yet");
2302 if (target
->type
->blank_check_memory
== NULL
)
2303 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2305 return target
->type
->blank_check_memory(target
, blocks
, num_blocks
, erased_value
);
2308 int target_read_u64(struct target
*target
, target_addr_t address
, uint64_t *value
)
2310 uint8_t value_buf
[8];
2311 if (!target_was_examined(target
)) {
2312 LOG_ERROR("Target not examined yet");
2316 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2318 if (retval
== ERROR_OK
) {
2319 *value
= target_buffer_get_u64(target
, value_buf
);
2320 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2325 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2332 int target_read_u32(struct target
*target
, target_addr_t address
, uint32_t *value
)
2334 uint8_t value_buf
[4];
2335 if (!target_was_examined(target
)) {
2336 LOG_ERROR("Target not examined yet");
2340 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2342 if (retval
== ERROR_OK
) {
2343 *value
= target_buffer_get_u32(target
, value_buf
);
2344 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2349 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2356 int target_read_u16(struct target
*target
, target_addr_t address
, uint16_t *value
)
2358 uint8_t value_buf
[2];
2359 if (!target_was_examined(target
)) {
2360 LOG_ERROR("Target not examined yet");
2364 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2366 if (retval
== ERROR_OK
) {
2367 *value
= target_buffer_get_u16(target
, value_buf
);
2368 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%4.4" PRIx16
,
2373 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2380 int target_read_u8(struct target
*target
, target_addr_t address
, uint8_t *value
)
2382 if (!target_was_examined(target
)) {
2383 LOG_ERROR("Target not examined yet");
2387 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2389 if (retval
== ERROR_OK
) {
2390 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2395 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2402 int target_write_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2405 uint8_t value_buf
[8];
2406 if (!target_was_examined(target
)) {
2407 LOG_ERROR("Target not examined yet");
2411 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2415 target_buffer_set_u64(target
, value_buf
, value
);
2416 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2417 if (retval
!= ERROR_OK
)
2418 LOG_DEBUG("failed: %i", retval
);
2423 int target_write_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2426 uint8_t value_buf
[4];
2427 if (!target_was_examined(target
)) {
2428 LOG_ERROR("Target not examined yet");
2432 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2436 target_buffer_set_u32(target
, value_buf
, value
);
2437 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2438 if (retval
!= ERROR_OK
)
2439 LOG_DEBUG("failed: %i", retval
);
2444 int target_write_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2447 uint8_t value_buf
[2];
2448 if (!target_was_examined(target
)) {
2449 LOG_ERROR("Target not examined yet");
2453 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2457 target_buffer_set_u16(target
, value_buf
, value
);
2458 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2459 if (retval
!= ERROR_OK
)
2460 LOG_DEBUG("failed: %i", retval
);
2465 int target_write_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2468 if (!target_was_examined(target
)) {
2469 LOG_ERROR("Target not examined yet");
2473 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2476 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2477 if (retval
!= ERROR_OK
)
2478 LOG_DEBUG("failed: %i", retval
);
2483 int target_write_phys_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2486 uint8_t value_buf
[8];
2487 if (!target_was_examined(target
)) {
2488 LOG_ERROR("Target not examined yet");
2492 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2496 target_buffer_set_u64(target
, value_buf
, value
);
2497 retval
= target_write_phys_memory(target
, address
, 8, 1, value_buf
);
2498 if (retval
!= ERROR_OK
)
2499 LOG_DEBUG("failed: %i", retval
);
2504 int target_write_phys_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2507 uint8_t value_buf
[4];
2508 if (!target_was_examined(target
)) {
2509 LOG_ERROR("Target not examined yet");
2513 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2517 target_buffer_set_u32(target
, value_buf
, value
);
2518 retval
= target_write_phys_memory(target
, address
, 4, 1, value_buf
);
2519 if (retval
!= ERROR_OK
)
2520 LOG_DEBUG("failed: %i", retval
);
2525 int target_write_phys_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2528 uint8_t value_buf
[2];
2529 if (!target_was_examined(target
)) {
2530 LOG_ERROR("Target not examined yet");
2534 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2538 target_buffer_set_u16(target
, value_buf
, value
);
2539 retval
= target_write_phys_memory(target
, address
, 2, 1, value_buf
);
2540 if (retval
!= ERROR_OK
)
2541 LOG_DEBUG("failed: %i", retval
);
2546 int target_write_phys_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2549 if (!target_was_examined(target
)) {
2550 LOG_ERROR("Target not examined yet");
2554 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2557 retval
= target_write_phys_memory(target
, address
, 1, 1, &value
);
2558 if (retval
!= ERROR_OK
)
2559 LOG_DEBUG("failed: %i", retval
);
2564 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2566 struct target
*target
= get_target(name
);
2567 if (target
== NULL
) {
2568 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2571 if (!target
->tap
->enabled
) {
2572 LOG_USER("Target: TAP %s is disabled, "
2573 "can't be the current target\n",
2574 target
->tap
->dotted_name
);
2578 cmd_ctx
->current_target
= target
;
2579 if (cmd_ctx
->current_target_override
)
2580 cmd_ctx
->current_target_override
= target
;
2586 COMMAND_HANDLER(handle_targets_command
)
2588 int retval
= ERROR_OK
;
2589 if (CMD_ARGC
== 1) {
2590 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2591 if (retval
== ERROR_OK
) {
2597 struct target
*target
= all_targets
;
2598 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2599 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2604 if (target
->tap
->enabled
)
2605 state
= target_state_name(target
);
2607 state
= "tap-disabled";
2609 if (CMD_CTX
->current_target
== target
)
2612 /* keep columns lined up to match the headers above */
2613 command_print(CMD_CTX
,
2614 "%2d%c %-18s %-10s %-6s %-18s %s",
2615 target
->target_number
,
2617 target_name(target
),
2618 target_type_name(target
),
2619 Jim_Nvp_value2name_simple(nvp_target_endian
,
2620 target
->endianness
)->name
,
2621 target
->tap
->dotted_name
,
2623 target
= target
->next
;
2629 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2631 static int powerDropout
;
2632 static int srstAsserted
;
2634 static int runPowerRestore
;
2635 static int runPowerDropout
;
2636 static int runSrstAsserted
;
2637 static int runSrstDeasserted
;
2639 static int sense_handler(void)
2641 static int prevSrstAsserted
;
2642 static int prevPowerdropout
;
2644 int retval
= jtag_power_dropout(&powerDropout
);
2645 if (retval
!= ERROR_OK
)
2649 powerRestored
= prevPowerdropout
&& !powerDropout
;
2651 runPowerRestore
= 1;
2653 int64_t current
= timeval_ms();
2654 static int64_t lastPower
;
2655 bool waitMore
= lastPower
+ 2000 > current
;
2656 if (powerDropout
&& !waitMore
) {
2657 runPowerDropout
= 1;
2658 lastPower
= current
;
2661 retval
= jtag_srst_asserted(&srstAsserted
);
2662 if (retval
!= ERROR_OK
)
2666 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2668 static int64_t lastSrst
;
2669 waitMore
= lastSrst
+ 2000 > current
;
2670 if (srstDeasserted
&& !waitMore
) {
2671 runSrstDeasserted
= 1;
2675 if (!prevSrstAsserted
&& srstAsserted
)
2676 runSrstAsserted
= 1;
2678 prevSrstAsserted
= srstAsserted
;
2679 prevPowerdropout
= powerDropout
;
2681 if (srstDeasserted
|| powerRestored
) {
2682 /* Other than logging the event we can't do anything here.
2683 * Issuing a reset is a particularly bad idea as we might
2684 * be inside a reset already.
2691 /* process target state changes */
2692 static int handle_target(void *priv
)
2694 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2695 int retval
= ERROR_OK
;
2697 if (!is_jtag_poll_safe()) {
2698 /* polling is disabled currently */
2702 /* we do not want to recurse here... */
2703 static int recursive
;
2707 /* danger! running these procedures can trigger srst assertions and power dropouts.
2708 * We need to avoid an infinite loop/recursion here and we do that by
2709 * clearing the flags after running these events.
2711 int did_something
= 0;
2712 if (runSrstAsserted
) {
2713 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2714 Jim_Eval(interp
, "srst_asserted");
2717 if (runSrstDeasserted
) {
2718 Jim_Eval(interp
, "srst_deasserted");
2721 if (runPowerDropout
) {
2722 LOG_INFO("Power dropout detected, running power_dropout proc.");
2723 Jim_Eval(interp
, "power_dropout");
2726 if (runPowerRestore
) {
2727 Jim_Eval(interp
, "power_restore");
2731 if (did_something
) {
2732 /* clear detect flags */
2736 /* clear action flags */
2738 runSrstAsserted
= 0;
2739 runSrstDeasserted
= 0;
2740 runPowerRestore
= 0;
2741 runPowerDropout
= 0;
2746 /* Poll targets for state changes unless that's globally disabled.
2747 * Skip targets that are currently disabled.
2749 for (struct target
*target
= all_targets
;
2750 is_jtag_poll_safe() && target
;
2751 target
= target
->next
) {
2753 if (!target_was_examined(target
))
2756 if (!target
->tap
->enabled
)
2759 if (target
->backoff
.times
> target
->backoff
.count
) {
2760 /* do not poll this time as we failed previously */
2761 target
->backoff
.count
++;
2764 target
->backoff
.count
= 0;
2766 /* only poll target if we've got power and srst isn't asserted */
2767 if (!powerDropout
&& !srstAsserted
) {
2768 /* polling may fail silently until the target has been examined */
2769 retval
= target_poll(target
);
2770 if (retval
!= ERROR_OK
) {
2771 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2772 if (target
->backoff
.times
* polling_interval
< 5000) {
2773 target
->backoff
.times
*= 2;
2774 target
->backoff
.times
++;
2777 /* Tell GDB to halt the debugger. This allows the user to
2778 * run monitor commands to handle the situation.
2780 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2782 if (target
->backoff
.times
> 0) {
2783 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
2784 target_reset_examined(target
);
2785 retval
= target_examine_one(target
);
2786 /* Target examination could have failed due to unstable connection,
2787 * but we set the examined flag anyway to repoll it later */
2788 if (retval
!= ERROR_OK
) {
2789 target
->examined
= true;
2790 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2791 target
->backoff
.times
* polling_interval
);
2796 /* Since we succeeded, we reset backoff count */
2797 target
->backoff
.times
= 0;
2804 COMMAND_HANDLER(handle_reg_command
)
2806 struct target
*target
;
2807 struct reg
*reg
= NULL
;
2813 target
= get_current_target(CMD_CTX
);
2815 /* list all available registers for the current target */
2816 if (CMD_ARGC
== 0) {
2817 struct reg_cache
*cache
= target
->reg_cache
;
2823 command_print(CMD_CTX
, "===== %s", cache
->name
);
2825 for (i
= 0, reg
= cache
->reg_list
;
2826 i
< cache
->num_regs
;
2827 i
++, reg
++, count
++) {
2828 if (reg
->exist
== false)
2830 /* only print cached values if they are valid */
2832 value
= buf_to_str(reg
->value
,
2834 command_print(CMD_CTX
,
2835 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2843 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2848 cache
= cache
->next
;
2854 /* access a single register by its ordinal number */
2855 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2857 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2859 struct reg_cache
*cache
= target
->reg_cache
;
2863 for (i
= 0; i
< cache
->num_regs
; i
++) {
2864 if (count
++ == num
) {
2865 reg
= &cache
->reg_list
[i
];
2871 cache
= cache
->next
;
2875 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2876 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2880 /* access a single register by its name */
2881 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2887 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2892 /* display a register */
2893 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2894 && (CMD_ARGV
[1][0] <= '9')))) {
2895 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2898 if (reg
->valid
== 0)
2899 reg
->type
->get(reg
);
2900 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2901 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2906 /* set register value */
2907 if (CMD_ARGC
== 2) {
2908 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2911 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2913 reg
->type
->set(reg
, buf
);
2915 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2916 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2924 return ERROR_COMMAND_SYNTAX_ERROR
;
2927 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2931 COMMAND_HANDLER(handle_poll_command
)
2933 int retval
= ERROR_OK
;
2934 struct target
*target
= get_current_target(CMD_CTX
);
2936 if (CMD_ARGC
== 0) {
2937 command_print(CMD_CTX
, "background polling: %s",
2938 jtag_poll_get_enabled() ? "on" : "off");
2939 command_print(CMD_CTX
, "TAP: %s (%s)",
2940 target
->tap
->dotted_name
,
2941 target
->tap
->enabled
? "enabled" : "disabled");
2942 if (!target
->tap
->enabled
)
2944 retval
= target_poll(target
);
2945 if (retval
!= ERROR_OK
)
2947 retval
= target_arch_state(target
);
2948 if (retval
!= ERROR_OK
)
2950 } else if (CMD_ARGC
== 1) {
2952 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2953 jtag_poll_set_enabled(enable
);
2955 return ERROR_COMMAND_SYNTAX_ERROR
;
2960 COMMAND_HANDLER(handle_wait_halt_command
)
2963 return ERROR_COMMAND_SYNTAX_ERROR
;
2965 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2966 if (1 == CMD_ARGC
) {
2967 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2968 if (ERROR_OK
!= retval
)
2969 return ERROR_COMMAND_SYNTAX_ERROR
;
2972 struct target
*target
= get_current_target(CMD_CTX
);
2973 return target_wait_state(target
, TARGET_HALTED
, ms
);
2976 /* wait for target state to change. The trick here is to have a low
2977 * latency for short waits and not to suck up all the CPU time
2980 * After 500ms, keep_alive() is invoked
2982 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2985 int64_t then
= 0, cur
;
2989 retval
= target_poll(target
);
2990 if (retval
!= ERROR_OK
)
2992 if (target
->state
== state
)
2997 then
= timeval_ms();
2998 LOG_DEBUG("waiting for target %s...",
2999 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
3005 if ((cur
-then
) > ms
) {
3006 LOG_ERROR("timed out while waiting for target %s",
3007 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
3015 COMMAND_HANDLER(handle_halt_command
)
3019 struct target
*target
= get_current_target(CMD_CTX
);
3021 target
->verbose_halt_msg
= true;
3023 int retval
= target_halt(target
);
3024 if (ERROR_OK
!= retval
)
3027 if (CMD_ARGC
== 1) {
3028 unsigned wait_local
;
3029 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
3030 if (ERROR_OK
!= retval
)
3031 return ERROR_COMMAND_SYNTAX_ERROR
;
3036 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
3039 COMMAND_HANDLER(handle_soft_reset_halt_command
)
3041 struct target
*target
= get_current_target(CMD_CTX
);
3043 LOG_USER("requesting target halt and executing a soft reset");
3045 target_soft_reset_halt(target
);
3050 COMMAND_HANDLER(handle_reset_command
)
3053 return ERROR_COMMAND_SYNTAX_ERROR
;
3055 enum target_reset_mode reset_mode
= RESET_RUN
;
3056 if (CMD_ARGC
== 1) {
3058 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
3059 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
3060 return ERROR_COMMAND_SYNTAX_ERROR
;
3061 reset_mode
= n
->value
;
3064 /* reset *all* targets */
3065 return target_process_reset(CMD_CTX
, reset_mode
);
3069 COMMAND_HANDLER(handle_resume_command
)
3073 return ERROR_COMMAND_SYNTAX_ERROR
;
3075 struct target
*target
= get_current_target(CMD_CTX
);
3077 /* with no CMD_ARGV, resume from current pc, addr = 0,
3078 * with one arguments, addr = CMD_ARGV[0],
3079 * handle breakpoints, not debugging */
3080 target_addr_t addr
= 0;
3081 if (CMD_ARGC
== 1) {
3082 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3086 return target_resume(target
, current
, addr
, 1, 0);
3089 COMMAND_HANDLER(handle_step_command
)
3092 return ERROR_COMMAND_SYNTAX_ERROR
;
3096 /* with no CMD_ARGV, step from current pc, addr = 0,
3097 * with one argument addr = CMD_ARGV[0],
3098 * handle breakpoints, debugging */
3099 target_addr_t addr
= 0;
3101 if (CMD_ARGC
== 1) {
3102 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3106 struct target
*target
= get_current_target(CMD_CTX
);
3108 return target
->type
->step(target
, current_pc
, addr
, 1);
3111 static void handle_md_output(struct command_context
*cmd_ctx
,
3112 struct target
*target
, target_addr_t address
, unsigned size
,
3113 unsigned count
, const uint8_t *buffer
)
3115 const unsigned line_bytecnt
= 32;
3116 unsigned line_modulo
= line_bytecnt
/ size
;
3118 char output
[line_bytecnt
* 4 + 1];
3119 unsigned output_len
= 0;
3121 const char *value_fmt
;
3124 value_fmt
= "%16.16"PRIx64
" ";
3127 value_fmt
= "%8.8"PRIx64
" ";
3130 value_fmt
= "%4.4"PRIx64
" ";
3133 value_fmt
= "%2.2"PRIx64
" ";
3136 /* "can't happen", caller checked */
3137 LOG_ERROR("invalid memory read size: %u", size
);
3141 for (unsigned i
= 0; i
< count
; i
++) {
3142 if (i
% line_modulo
== 0) {
3143 output_len
+= snprintf(output
+ output_len
,
3144 sizeof(output
) - output_len
,
3145 TARGET_ADDR_FMT
": ",
3146 (address
+ (i
* size
)));
3150 const uint8_t *value_ptr
= buffer
+ i
* size
;
3153 value
= target_buffer_get_u64(target
, value_ptr
);
3156 value
= target_buffer_get_u32(target
, value_ptr
);
3159 value
= target_buffer_get_u16(target
, value_ptr
);
3164 output_len
+= snprintf(output
+ output_len
,
3165 sizeof(output
) - output_len
,
3168 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
3169 command_print(cmd_ctx
, "%s", output
);
3175 COMMAND_HANDLER(handle_md_command
)
3178 return ERROR_COMMAND_SYNTAX_ERROR
;
3181 switch (CMD_NAME
[2]) {
3195 return ERROR_COMMAND_SYNTAX_ERROR
;
3198 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;