1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
17 * Copyright (C) 2011 by Broadcom Corporation *
18 * Evan Hunter - ehunter@broadcom.com *
20 * Copyright (C) ST-Ericsson SA 2011 *
21 * michel.jaouen@stericsson.com : smp minimum support *
23 * Copyright (C) 2011 Andreas Fritiofson *
24 * andreas.fritiofson@gmail.com *
26 * This program is free software; you can redistribute it and/or modify *
27 * it under the terms of the GNU General Public License as published by *
28 * the Free Software Foundation; either version 2 of the License, or *
29 * (at your option) any later version. *
31 * This program is distributed in the hope that it will be useful, *
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
34 * GNU General Public License for more details. *
36 * You should have received a copy of the GNU General Public License *
37 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
38 ***************************************************************************/
44 #include <helper/time_support.h>
45 #include <jtag/jtag.h>
46 #include <flash/nor/core.h>
49 #include "target_type.h"
50 #include "target_request.h"
51 #include "breakpoints.h"
55 #include "rtos/rtos.h"
56 #include "transport/transport.h"
59 /* default halt wait timeout (ms) */
60 #define DEFAULT_HALT_TIMEOUT 5000
62 static int target_read_buffer_default(struct target
*target
, target_addr_t address
,
63 uint32_t count
, uint8_t *buffer
);
64 static int target_write_buffer_default(struct target
*target
, target_addr_t address
,
65 uint32_t count
, const uint8_t *buffer
);
66 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
67 int argc
, Jim_Obj
* const *argv
);
68 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
69 int argc
, Jim_Obj
* const *argv
);
70 static int target_register_user_commands(struct command_context
*cmd_ctx
);
71 static int target_get_gdb_fileio_info_default(struct target
*target
,
72 struct gdb_fileio_info
*fileio_info
);
73 static int target_gdb_fileio_end_default(struct target
*target
, int retcode
,
74 int fileio_errno
, bool ctrl_c
);
75 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
76 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
);
79 extern struct target_type arm7tdmi_target
;
80 extern struct target_type arm720t_target
;
81 extern struct target_type arm9tdmi_target
;
82 extern struct target_type arm920t_target
;
83 extern struct target_type arm966e_target
;
84 extern struct target_type arm946e_target
;
85 extern struct target_type arm926ejs_target
;
86 extern struct target_type fa526_target
;
87 extern struct target_type feroceon_target
;
88 extern struct target_type dragonite_target
;
89 extern struct target_type xscale_target
;
90 extern struct target_type cortexm_target
;
91 extern struct target_type cortexa_target
;
92 extern struct target_type aarch64_target
;
93 extern struct target_type cortexr4_target
;
94 extern struct target_type arm11_target
;
95 extern struct target_type ls1_sap_target
;
96 extern struct target_type mips_m4k_target
;
97 extern struct target_type avr_target
;
98 extern struct target_type dsp563xx_target
;
99 extern struct target_type dsp5680xx_target
;
100 extern struct target_type testee_target
;
101 extern struct target_type avr32_ap7k_target
;
102 extern struct target_type hla_target
;
103 extern struct target_type nds32_v2_target
;
104 extern struct target_type nds32_v3_target
;
105 extern struct target_type nds32_v3m_target
;
106 extern struct target_type or1k_target
;
107 extern struct target_type quark_x10xx_target
;
108 extern struct target_type quark_d20xx_target
;
109 extern struct target_type stm8_target
;
110 extern struct target_type riscv_target
;
111 extern struct target_type mem_ap_target
;
113 static struct target_type
*target_types
[] = {
152 struct target
*all_targets
;
153 static struct target_event_callback
*target_event_callbacks
;
154 static struct target_timer_callback
*target_timer_callbacks
;
155 LIST_HEAD(target_reset_callback_list
);
156 LIST_HEAD(target_trace_callback_list
);
157 static const int polling_interval
= 100;
159 static const Jim_Nvp nvp_assert
[] = {
160 { .name
= "assert", NVP_ASSERT
},
161 { .name
= "deassert", NVP_DEASSERT
},
162 { .name
= "T", NVP_ASSERT
},
163 { .name
= "F", NVP_DEASSERT
},
164 { .name
= "t", NVP_ASSERT
},
165 { .name
= "f", NVP_DEASSERT
},
166 { .name
= NULL
, .value
= -1 }
169 static const Jim_Nvp nvp_error_target
[] = {
170 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
171 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
172 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
173 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
174 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
175 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
176 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
177 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
178 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
179 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
180 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
181 { .value
= -1, .name
= NULL
}
184 static const char *target_strerror_safe(int err
)
188 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
195 static const Jim_Nvp nvp_target_event
[] = {
197 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
198 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
199 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
200 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
201 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
203 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
204 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
206 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
207 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
208 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
209 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
210 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
211 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
212 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
213 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
215 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
216 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
218 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
219 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
221 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
222 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
224 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
225 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
227 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
228 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
230 { .value
= TARGET_EVENT_TRACE_CONFIG
, .name
= "trace-config" },
232 { .name
= NULL
, .value
= -1 }
235 static const Jim_Nvp nvp_target_state
[] = {
236 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
237 { .name
= "running", .value
= TARGET_RUNNING
},
238 { .name
= "halted", .value
= TARGET_HALTED
},
239 { .name
= "reset", .value
= TARGET_RESET
},
240 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
241 { .name
= NULL
, .value
= -1 },
244 static const Jim_Nvp nvp_target_debug_reason
[] = {
245 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
246 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
247 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
248 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
249 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
250 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
251 { .name
= "program-exit" , .value
= DBG_REASON_EXIT
},
252 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
253 { .name
= NULL
, .value
= -1 },
256 static const Jim_Nvp nvp_target_endian
[] = {
257 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
258 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
259 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
260 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
261 { .name
= NULL
, .value
= -1 },
264 static const Jim_Nvp nvp_reset_modes
[] = {
265 { .name
= "unknown", .value
= RESET_UNKNOWN
},
266 { .name
= "run" , .value
= RESET_RUN
},
267 { .name
= "halt" , .value
= RESET_HALT
},
268 { .name
= "init" , .value
= RESET_INIT
},
269 { .name
= NULL
, .value
= -1 },
272 const char *debug_reason_name(struct target
*t
)
276 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
277 t
->debug_reason
)->name
;
279 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
280 cp
= "(*BUG*unknown*BUG*)";
285 const char *target_state_name(struct target
*t
)
288 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
290 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
291 cp
= "(*BUG*unknown*BUG*)";
294 if (!target_was_examined(t
) && t
->defer_examine
)
295 cp
= "examine deferred";
300 const char *target_event_name(enum target_event event
)
303 cp
= Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
;
305 LOG_ERROR("Invalid target event: %d", (int)(event
));
306 cp
= "(*BUG*unknown*BUG*)";
311 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
314 cp
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
;
316 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
317 cp
= "(*BUG*unknown*BUG*)";
322 /* determine the number of the new target */
323 static int new_target_number(void)
328 /* number is 0 based */
332 if (x
< t
->target_number
)
333 x
= t
->target_number
;
339 /* read a uint64_t from a buffer in target memory endianness */
340 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
342 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
343 return le_to_h_u64(buffer
);
345 return be_to_h_u64(buffer
);
348 /* read a uint32_t from a buffer in target memory endianness */
349 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
351 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
352 return le_to_h_u32(buffer
);
354 return be_to_h_u32(buffer
);
357 /* read a uint24_t from a buffer in target memory endianness */
358 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
360 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
361 return le_to_h_u24(buffer
);
363 return be_to_h_u24(buffer
);
366 /* read a uint16_t from a buffer in target memory endianness */
367 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
369 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
370 return le_to_h_u16(buffer
);
372 return be_to_h_u16(buffer
);
375 /* read a uint8_t from a buffer in target memory endianness */
376 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
378 return *buffer
& 0x0ff;
381 /* write a uint64_t to a buffer in target memory endianness */
382 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
384 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
385 h_u64_to_le(buffer
, value
);
387 h_u64_to_be(buffer
, value
);
390 /* write a uint32_t to a buffer in target memory endianness */
391 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
393 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
394 h_u32_to_le(buffer
, value
);
396 h_u32_to_be(buffer
, value
);
399 /* write a uint24_t to a buffer in target memory endianness */
400 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
402 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
403 h_u24_to_le(buffer
, value
);
405 h_u24_to_be(buffer
, value
);
408 /* write a uint16_t to a buffer in target memory endianness */
409 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
411 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
412 h_u16_to_le(buffer
, value
);
414 h_u16_to_be(buffer
, value
);
417 /* write a uint8_t to a buffer in target memory endianness */
418 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
423 /* write a uint64_t array to a buffer in target memory endianness */
424 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
427 for (i
= 0; i
< count
; i
++)
428 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
431 /* write a uint32_t array to a buffer in target memory endianness */
432 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
435 for (i
= 0; i
< count
; i
++)
436 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
439 /* write a uint16_t array to a buffer in target memory endianness */
440 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
443 for (i
= 0; i
< count
; i
++)
444 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
447 /* write a uint64_t array to a buffer in target memory endianness */
448 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
451 for (i
= 0; i
< count
; i
++)
452 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
455 /* write a uint32_t array to a buffer in target memory endianness */
456 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
459 for (i
= 0; i
< count
; i
++)
460 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
463 /* write a uint16_t array to a buffer in target memory endianness */
464 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
467 for (i
= 0; i
< count
; i
++)
468 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
471 /* return a pointer to a configured target; id is name or number */
472 struct target
*get_target(const char *id
)
474 struct target
*target
;
476 /* try as tcltarget name */
477 for (target
= all_targets
; target
; target
= target
->next
) {
478 if (target_name(target
) == NULL
)
480 if (strcmp(id
, target_name(target
)) == 0)
484 /* It's OK to remove this fallback sometime after August 2010 or so */
486 /* no match, try as number */
488 if (parse_uint(id
, &num
) != ERROR_OK
)
491 for (target
= all_targets
; target
; target
= target
->next
) {
492 if (target
->target_number
== (int)num
) {
493 LOG_WARNING("use '%s' as target identifier, not '%u'",
494 target_name(target
), num
);
502 /* returns a pointer to the n-th configured target */
503 struct target
*get_target_by_num(int num
)
505 struct target
*target
= all_targets
;
508 if (target
->target_number
== num
)
510 target
= target
->next
;
516 struct target
*get_current_target(struct command_context
*cmd_ctx
)
518 struct target
*target
= cmd_ctx
->current_target_override
519 ? cmd_ctx
->current_target_override
520 : cmd_ctx
->current_target
;
522 if (target
== NULL
) {
523 LOG_ERROR("BUG: current_target out of bounds");
530 int target_poll(struct target
*target
)
534 /* We can't poll until after examine */
535 if (!target_was_examined(target
)) {
536 /* Fail silently lest we pollute the log */
540 retval
= target
->type
->poll(target
);
541 if (retval
!= ERROR_OK
)
544 if (target
->halt_issued
) {
545 if (target
->state
== TARGET_HALTED
)
546 target
->halt_issued
= false;
548 int64_t t
= timeval_ms() - target
->halt_issued_time
;
549 if (t
> DEFAULT_HALT_TIMEOUT
) {
550 target
->halt_issued
= false;
551 LOG_INFO("Halt timed out, wake up GDB.");
552 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
560 int target_halt(struct target
*target
)
563 /* We can't poll until after examine */
564 if (!target_was_examined(target
)) {
565 LOG_ERROR("Target not examined yet");
569 retval
= target
->type
->halt(target
);
570 if (retval
!= ERROR_OK
)
573 target
->halt_issued
= true;
574 target
->halt_issued_time
= timeval_ms();
580 * Make the target (re)start executing using its saved execution
581 * context (possibly with some modifications).
583 * @param target Which target should start executing.
584 * @param current True to use the target's saved program counter instead
585 * of the address parameter
586 * @param address Optionally used as the program counter.
587 * @param handle_breakpoints True iff breakpoints at the resumption PC
588 * should be skipped. (For example, maybe execution was stopped by
589 * such a breakpoint, in which case it would be counterprodutive to
591 * @param debug_execution False if all working areas allocated by OpenOCD
592 * should be released and/or restored to their original contents.
593 * (This would for example be true to run some downloaded "helper"
594 * algorithm code, which resides in one such working buffer and uses
595 * another for data storage.)
597 * @todo Resolve the ambiguity about what the "debug_execution" flag
598 * signifies. For example, Target implementations don't agree on how
599 * it relates to invalidation of the register cache, or to whether
600 * breakpoints and watchpoints should be enabled. (It would seem wrong
601 * to enable breakpoints when running downloaded "helper" algorithms
602 * (debug_execution true), since the breakpoints would be set to match
603 * target firmware being debugged, not the helper algorithm.... and
604 * enabling them could cause such helpers to malfunction (for example,
605 * by overwriting data with a breakpoint instruction. On the other
606 * hand the infrastructure for running such helpers might use this
607 * procedure but rely on hardware breakpoint to detect termination.)
609 int target_resume(struct target
*target
, int current
, target_addr_t address
,
610 int handle_breakpoints
, int debug_execution
)
614 /* We can't poll until after examine */
615 if (!target_was_examined(target
)) {
616 LOG_ERROR("Target not examined yet");
620 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
622 /* note that resume *must* be asynchronous. The CPU can halt before
623 * we poll. The CPU can even halt at the current PC as a result of
624 * a software breakpoint being inserted by (a bug?) the application.
626 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
627 if (retval
!= ERROR_OK
)
630 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
635 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
640 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
641 if (n
->name
== NULL
) {
642 LOG_ERROR("invalid reset mode");
646 struct target
*target
;
647 for (target
= all_targets
; target
; target
= target
->next
)
648 target_call_reset_callbacks(target
, reset_mode
);
650 /* disable polling during reset to make reset event scripts
651 * more predictable, i.e. dr/irscan & pathmove in events will
652 * not have JTAG operations injected into the middle of a sequence.
654 bool save_poll
= jtag_poll_get_enabled();
656 jtag_poll_set_enabled(false);
658 sprintf(buf
, "ocd_process_reset %s", n
->name
);
659 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
661 jtag_poll_set_enabled(save_poll
);
663 if (retval
!= JIM_OK
) {
664 Jim_MakeErrorMessage(cmd_ctx
->interp
);
665 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
669 /* We want any events to be processed before the prompt */
670 retval
= target_call_timer_callbacks_now();
672 for (target
= all_targets
; target
; target
= target
->next
) {
673 target
->type
->check_reset(target
);
674 target
->running_alg
= false;
680 static int identity_virt2phys(struct target
*target
,
681 target_addr_t
virtual, target_addr_t
*physical
)
687 static int no_mmu(struct target
*target
, int *enabled
)
693 static int default_examine(struct target
*target
)
695 target_set_examined(target
);
699 /* no check by default */
700 static int default_check_reset(struct target
*target
)
705 int target_examine_one(struct target
*target
)
707 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
709 int retval
= target
->type
->examine(target
);
710 if (retval
!= ERROR_OK
)
713 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
718 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
720 struct target
*target
= priv
;
722 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
725 jtag_unregister_event_callback(jtag_enable_callback
, target
);
727 return target_examine_one(target
);
730 /* Targets that correctly implement init + examine, i.e.
731 * no communication with target during init:
735 int target_examine(void)
737 int retval
= ERROR_OK
;
738 struct target
*target
;
740 for (target
= all_targets
; target
; target
= target
->next
) {
741 /* defer examination, but don't skip it */
742 if (!target
->tap
->enabled
) {
743 jtag_register_event_callback(jtag_enable_callback
,
748 if (target
->defer_examine
)
751 retval
= target_examine_one(target
);
752 if (retval
!= ERROR_OK
)
758 const char *target_type_name(struct target
*target
)
760 return target
->type
->name
;
763 static int target_soft_reset_halt(struct target
*target
)
765 if (!target_was_examined(target
)) {
766 LOG_ERROR("Target not examined yet");
769 if (!target
->type
->soft_reset_halt
) {
770 LOG_ERROR("Target %s does not support soft_reset_halt",
771 target_name(target
));
774 return target
->type
->soft_reset_halt(target
);
778 * Downloads a target-specific native code algorithm to the target,
779 * and executes it. * Note that some targets may need to set up, enable,
780 * and tear down a breakpoint (hard or * soft) to detect algorithm
781 * termination, while others may support lower overhead schemes where
782 * soft breakpoints embedded in the algorithm automatically terminate the
785 * @param target used to run the algorithm
786 * @param arch_info target-specific description of the algorithm.
788 int target_run_algorithm(struct target
*target
,
789 int num_mem_params
, struct mem_param
*mem_params
,
790 int num_reg_params
, struct reg_param
*reg_param
,
791 uint32_t entry_point
, uint32_t exit_point
,
792 int timeout_ms
, void *arch_info
)
794 int retval
= ERROR_FAIL
;
796 if (!target_was_examined(target
)) {
797 LOG_ERROR("Target not examined yet");
800 if (!target
->type
->run_algorithm
) {
801 LOG_ERROR("Target type '%s' does not support %s",
802 target_type_name(target
), __func__
);
806 target
->running_alg
= true;
807 retval
= target
->type
->run_algorithm(target
,
808 num_mem_params
, mem_params
,
809 num_reg_params
, reg_param
,
810 entry_point
, exit_point
, timeout_ms
, arch_info
);
811 target
->running_alg
= false;
818 * Executes a target-specific native code algorithm and leaves it running.
820 * @param target used to run the algorithm
821 * @param arch_info target-specific description of the algorithm.
823 int target_start_algorithm(struct target
*target
,
824 int num_mem_params
, struct mem_param
*mem_params
,
825 int num_reg_params
, struct reg_param
*reg_params
,
826 uint32_t entry_point
, uint32_t exit_point
,
829 int retval
= ERROR_FAIL
;
831 if (!target_was_examined(target
)) {
832 LOG_ERROR("Target not examined yet");
835 if (!target
->type
->start_algorithm
) {
836 LOG_ERROR("Target type '%s' does not support %s",
837 target_type_name(target
), __func__
);
840 if (target
->running_alg
) {
841 LOG_ERROR("Target is already running an algorithm");
845 target
->running_alg
= true;
846 retval
= target
->type
->start_algorithm(target
,
847 num_mem_params
, mem_params
,
848 num_reg_params
, reg_params
,
849 entry_point
, exit_point
, arch_info
);
856 * Waits for an algorithm started with target_start_algorithm() to complete.
858 * @param target used to run the algorithm
859 * @param arch_info target-specific description of the algorithm.
861 int target_wait_algorithm(struct target
*target
,
862 int num_mem_params
, struct mem_param
*mem_params
,
863 int num_reg_params
, struct reg_param
*reg_params
,
864 uint32_t exit_point
, int timeout_ms
,
867 int retval
= ERROR_FAIL
;
869 if (!target
->type
->wait_algorithm
) {
870 LOG_ERROR("Target type '%s' does not support %s",
871 target_type_name(target
), __func__
);
874 if (!target
->running_alg
) {
875 LOG_ERROR("Target is not running an algorithm");
879 retval
= target
->type
->wait_algorithm(target
,
880 num_mem_params
, mem_params
,
881 num_reg_params
, reg_params
,
882 exit_point
, timeout_ms
, arch_info
);
883 if (retval
!= ERROR_TARGET_TIMEOUT
)
884 target
->running_alg
= false;
891 * Streams data to a circular buffer on target intended for consumption by code
892 * running asynchronously on target.
894 * This is intended for applications where target-specific native code runs
895 * on the target, receives data from the circular buffer, does something with
896 * it (most likely writing it to a flash memory), and advances the circular
899 * This assumes that the helper algorithm has already been loaded to the target,
900 * but has not been started yet. Given memory and register parameters are passed
903 * The buffer is defined by (buffer_start, buffer_size) arguments and has the
906 * [buffer_start + 0, buffer_start + 4):
907 * Write Pointer address (aka head). Written and updated by this
908 * routine when new data is written to the circular buffer.
909 * [buffer_start + 4, buffer_start + 8):
910 * Read Pointer address (aka tail). Updated by code running on the
911 * target after it consumes data.
912 * [buffer_start + 8, buffer_start + buffer_size):
913 * Circular buffer contents.
915 * See contrib/loaders/flash/stm32f1x.S for an example.
917 * @param target used to run the algorithm
918 * @param buffer address on the host where data to be sent is located
919 * @param count number of blocks to send
920 * @param block_size size in bytes of each block
921 * @param num_mem_params count of memory-based params to pass to algorithm
922 * @param mem_params memory-based params to pass to algorithm
923 * @param num_reg_params count of register-based params to pass to algorithm
924 * @param reg_params memory-based params to pass to algorithm
925 * @param buffer_start address on the target of the circular buffer structure
926 * @param buffer_size size of the circular buffer structure
927 * @param entry_point address on the target to execute to start the algorithm
928 * @param exit_point address at which to set a breakpoint to catch the
929 * end of the algorithm; can be 0 if target triggers a breakpoint itself
932 int target_run_flash_async_algorithm(struct target
*target
,
933 const uint8_t *buffer
, uint32_t count
, int block_size
,
934 int num_mem_params
, struct mem_param
*mem_params
,
935 int num_reg_params
, struct reg_param
*reg_params
,
936 uint32_t buffer_start
, uint32_t buffer_size
,
937 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
942 const uint8_t *buffer_orig
= buffer
;
944 /* Set up working area. First word is write pointer, second word is read pointer,
945 * rest is fifo data area. */
946 uint32_t wp_addr
= buffer_start
;
947 uint32_t rp_addr
= buffer_start
+ 4;
948 uint32_t fifo_start_addr
= buffer_start
+ 8;
949 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
951 uint32_t wp
= fifo_start_addr
;
952 uint32_t rp
= fifo_start_addr
;
954 /* validate block_size is 2^n */
955 assert(!block_size
|| !(block_size
& (block_size
- 1)));
957 retval
= target_write_u32(target
, wp_addr
, wp
);
958 if (retval
!= ERROR_OK
)
960 retval
= target_write_u32(target
, rp_addr
, rp
);
961 if (retval
!= ERROR_OK
)
964 /* Start up algorithm on target and let it idle while writing the first chunk */
965 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
966 num_reg_params
, reg_params
,
971 if (retval
!= ERROR_OK
) {
972 LOG_ERROR("error starting target flash write algorithm");
978 retval
= target_read_u32(target
, rp_addr
, &rp
);
979 if (retval
!= ERROR_OK
) {
980 LOG_ERROR("failed to get read pointer");
984 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
985 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
988 LOG_ERROR("flash write algorithm aborted by target");
989 retval
= ERROR_FLASH_OPERATION_FAILED
;
993 if (((rp
- fifo_start_addr
) & (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
994 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
998 /* Count the number of bytes available in the fifo without
999 * crossing the wrap around. Make sure to not fill it completely,
1000 * because that would make wp == rp and that's the empty condition. */
1001 uint32_t thisrun_bytes
;
1003 thisrun_bytes
= rp
- wp
- block_size
;
1004 else if (rp
> fifo_start_addr
)
1005 thisrun_bytes
= fifo_end_addr
- wp
;
1007 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
1009 if (thisrun_bytes
== 0) {
1010 /* Throttle polling a bit if transfer is (much) faster than flash
1011 * programming. The exact delay shouldn't matter as long as it's
1012 * less than buffer size / flash speed. This is very unlikely to
1013 * run when using high latency connections such as USB. */
1016 /* to stop an infinite loop on some targets check and increment a timeout
1017 * this issue was observed on a stellaris using the new ICDI interface */
1018 if (timeout
++ >= 500) {
1019 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1020 return ERROR_FLASH_OPERATION_FAILED
;
1025 /* reset our timeout */
1028 /* Limit to the amount of data we actually want to write */
1029 if (thisrun_bytes
> count
* block_size
)
1030 thisrun_bytes
= count
* block_size
;
1032 /* Write data to fifo */
1033 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
1034 if (retval
!= ERROR_OK
)
1037 /* Update counters and wrap write pointer */
1038 buffer
+= thisrun_bytes
;
1039 count
-= thisrun_bytes
/ block_size
;
1040 wp
+= thisrun_bytes
;
1041 if (wp
>= fifo_end_addr
)
1042 wp
= fifo_start_addr
;
1044 /* Store updated write pointer to target */
1045 retval
= target_write_u32(target
, wp_addr
, wp
);
1046 if (retval
!= ERROR_OK
)
1050 if (retval
!= ERROR_OK
) {
1051 /* abort flash write algorithm on target */
1052 target_write_u32(target
, wp_addr
, 0);
1055 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1056 num_reg_params
, reg_params
,
1061 if (retval2
!= ERROR_OK
) {
1062 LOG_ERROR("error waiting for target flash write algorithm");
1066 if (retval
== ERROR_OK
) {
1067 /* check if algorithm set rp = 0 after fifo writer loop finished */
1068 retval
= target_read_u32(target
, rp_addr
, &rp
);
1069 if (retval
== ERROR_OK
&& rp
== 0) {
1070 LOG_ERROR("flash write algorithm aborted by target");
1071 retval
= ERROR_FLASH_OPERATION_FAILED
;
1078 int target_read_memory(struct target
*target
,
1079 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1081 if (!target_was_examined(target
)) {
1082 LOG_ERROR("Target not examined yet");
1085 if (!target
->type
->read_memory
) {
1086 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1089 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1092 int target_read_phys_memory(struct target
*target
,
1093 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1095 if (!target_was_examined(target
)) {
1096 LOG_ERROR("Target not examined yet");
1099 if (!target
->type
->read_phys_memory
) {
1100 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1103 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1106 int target_write_memory(struct target
*target
,
1107 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1109 if (!target_was_examined(target
)) {
1110 LOG_ERROR("Target not examined yet");
1113 if (!target
->type
->write_memory
) {
1114 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1117 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1120 int target_write_phys_memory(struct target
*target
,
1121 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1123 if (!target_was_examined(target
)) {
1124 LOG_ERROR("Target not examined yet");
1127 if (!target
->type
->write_phys_memory
) {
1128 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1131 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1134 int target_add_breakpoint(struct target
*target
,
1135 struct breakpoint
*breakpoint
)
1137 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1138 LOG_WARNING("target %s is not halted (add breakpoint)", target_name(target
));
1139 return ERROR_TARGET_NOT_HALTED
;
1141 return target
->type
->add_breakpoint(target
, breakpoint
);
1144 int target_add_context_breakpoint(struct target
*target
,
1145 struct breakpoint
*breakpoint
)
1147 if (target
->state
!= TARGET_HALTED
) {
1148 LOG_WARNING("target %s is not halted (add context breakpoint)", target_name(target
));
1149 return ERROR_TARGET_NOT_HALTED
;
1151 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1154 int target_add_hybrid_breakpoint(struct target
*target
,
1155 struct breakpoint
*breakpoint
)
1157 if (target
->state
!= TARGET_HALTED
) {
1158 LOG_WARNING("target %s is not halted (add hybrid breakpoint)", target_name(target
));
1159 return ERROR_TARGET_NOT_HALTED
;
1161 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1164 int target_remove_breakpoint(struct target
*target
,
1165 struct breakpoint
*breakpoint
)
1167 return target
->type
->remove_breakpoint(target
, breakpoint
);
1170 int target_add_watchpoint(struct target
*target
,
1171 struct watchpoint
*watchpoint
)
1173 if (target
->state
!= TARGET_HALTED
) {
1174 LOG_WARNING("target %s is not halted (add watchpoint)", target_name(target
));
1175 return ERROR_TARGET_NOT_HALTED
;
1177 return target
->type
->add_watchpoint(target
, watchpoint
);
1179 int target_remove_watchpoint(struct target
*target
,
1180 struct watchpoint
*watchpoint
)
1182 return target
->type
->remove_watchpoint(target
, watchpoint
);
1184 int target_hit_watchpoint(struct target
*target
,
1185 struct watchpoint
**hit_watchpoint
)
1187 if (target
->state
!= TARGET_HALTED
) {
1188 LOG_WARNING("target %s is not halted (hit watchpoint)", target
->cmd_name
);
1189 return ERROR_TARGET_NOT_HALTED
;
1192 if (target
->type
->hit_watchpoint
== NULL
) {
1193 /* For backward compatible, if hit_watchpoint is not implemented,
1194 * return ERROR_FAIL such that gdb_server will not take the nonsense
1199 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1202 int target_get_gdb_reg_list(struct target
*target
,
1203 struct reg
**reg_list
[], int *reg_list_size
,
1204 enum target_register_class reg_class
)
1206 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1208 int target_step(struct target
*target
,
1209 int current
, target_addr_t address
, int handle_breakpoints
)
1211 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1214 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1216 if (target
->state
!= TARGET_HALTED
) {
1217 LOG_WARNING("target %s is not halted (gdb fileio)", target
->cmd_name
);
1218 return ERROR_TARGET_NOT_HALTED
;
1220 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1223 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1225 if (target
->state
!= TARGET_HALTED
) {
1226 LOG_WARNING("target %s is not halted (gdb fileio end)", target
->cmd_name
);
1227 return ERROR_TARGET_NOT_HALTED
;
1229 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1232 int target_profiling(struct target
*target
, uint32_t *samples
,
1233 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1235 if (target
->state
!= TARGET_HALTED
) {
1236 LOG_WARNING("target %s is not halted (profiling)", target
->cmd_name
);
1237 return ERROR_TARGET_NOT_HALTED
;
1239 return target
->type
->profiling(target
, samples
, max_num_samples
,
1240 num_samples
, seconds
);
1244 * Reset the @c examined flag for the given target.
1245 * Pure paranoia -- targets are zeroed on allocation.
1247 static void target_reset_examined(struct target
*target
)
1249 target
->examined
= false;
1252 static int handle_target(void *priv
);
1254 static int target_init_one(struct command_context
*cmd_ctx
,
1255 struct target
*target
)
1257 target_reset_examined(target
);
1259 struct target_type
*type
= target
->type
;
1260 if (type
->examine
== NULL
)
1261 type
->examine
= default_examine
;
1263 if (type
->check_reset
== NULL
)
1264 type
->check_reset
= default_check_reset
;
1266 assert(type
->init_target
!= NULL
);
1268 int retval
= type
->init_target(cmd_ctx
, target
);
1269 if (ERROR_OK
!= retval
) {
1270 LOG_ERROR("target '%s' init failed", target_name(target
));
1274 /* Sanity-check MMU support ... stub in what we must, to help
1275 * implement it in stages, but warn if we need to do so.
1278 if (type
->virt2phys
== NULL
) {
1279 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1280 type
->virt2phys
= identity_virt2phys
;
1283 /* Make sure no-MMU targets all behave the same: make no
1284 * distinction between physical and virtual addresses, and
1285 * ensure that virt2phys() is always an identity mapping.
1287 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1288 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1291 type
->write_phys_memory
= type
->write_memory
;
1292 type
->read_phys_memory
= type
->read_memory
;
1293 type
->virt2phys
= identity_virt2phys
;
1296 if (target
->type
->read_buffer
== NULL
)
1297 target
->type
->read_buffer
= target_read_buffer_default
;
1299 if (target
->type
->write_buffer
== NULL
)
1300 target
->type
->write_buffer
= target_write_buffer_default
;
1302 if (target
->type
->get_gdb_fileio_info
== NULL
)
1303 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1305 if (target
->type
->gdb_fileio_end
== NULL
)
1306 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1308 if (target
->type
->profiling
== NULL
)
1309 target
->type
->profiling
= target_profiling_default
;
1314 static int target_init(struct command_context
*cmd_ctx
)
1316 struct target
*target
;
1319 for (target
= all_targets
; target
; target
= target
->next
) {
1320 retval
= target_init_one(cmd_ctx
, target
);
1321 if (ERROR_OK
!= retval
)
1328 retval
= target_register_user_commands(cmd_ctx
);
1329 if (ERROR_OK
!= retval
)
1332 retval
= target_register_timer_callback(&handle_target
,
1333 polling_interval
, 1, cmd_ctx
->interp
);
1334 if (ERROR_OK
!= retval
)
1340 COMMAND_HANDLER(handle_target_init_command
)
1345 return ERROR_COMMAND_SYNTAX_ERROR
;
1347 static bool target_initialized
;
1348 if (target_initialized
) {
1349 LOG_INFO("'target init' has already been called");
1352 target_initialized
= true;
1354 retval
= command_run_line(CMD_CTX
, "init_targets");
1355 if (ERROR_OK
!= retval
)
1358 retval
= command_run_line(CMD_CTX
, "init_target_events");
1359 if (ERROR_OK
!= retval
)
1362 retval
= command_run_line(CMD_CTX
, "init_board");
1363 if (ERROR_OK
!= retval
)
1366 LOG_DEBUG("Initializing targets...");
1367 return target_init(CMD_CTX
);
1370 int target_register_event_callback(int (*callback
)(struct target
*target
,
1371 enum target_event event
, void *priv
), void *priv
)
1373 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1375 if (callback
== NULL
)
1376 return ERROR_COMMAND_SYNTAX_ERROR
;
1379 while ((*callbacks_p
)->next
)
1380 callbacks_p
= &((*callbacks_p
)->next
);
1381 callbacks_p
= &((*callbacks_p
)->next
);
1384 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1385 (*callbacks_p
)->callback
= callback
;
1386 (*callbacks_p
)->priv
= priv
;
1387 (*callbacks_p
)->next
= NULL
;
1392 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1393 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1395 struct target_reset_callback
*entry
;
1397 if (callback
== NULL
)
1398 return ERROR_COMMAND_SYNTAX_ERROR
;
1400 entry
= malloc(sizeof(struct target_reset_callback
));
1401 if (entry
== NULL
) {
1402 LOG_ERROR("error allocating buffer for reset callback entry");
1403 return ERROR_COMMAND_SYNTAX_ERROR
;
1406 entry
->callback
= callback
;
1408 list_add(&entry
->list
, &target_reset_callback_list
);
1414 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1415 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1417 struct target_trace_callback
*entry
;
1419 if (callback
== NULL
)
1420 return ERROR_COMMAND_SYNTAX_ERROR
;
1422 entry
= malloc(sizeof(struct target_trace_callback
));
1423 if (entry
== NULL
) {
1424 LOG_ERROR("error allocating buffer for trace callback entry");
1425 return ERROR_COMMAND_SYNTAX_ERROR
;
1428 entry
->callback
= callback
;
1430 list_add(&entry
->list
, &target_trace_callback_list
);
1436 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1438 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1440 if (callback
== NULL
)
1441 return ERROR_COMMAND_SYNTAX_ERROR
;
1444 while ((*callbacks_p
)->next
)
1445 callbacks_p
= &((*callbacks_p
)->next
);
1446 callbacks_p
= &((*callbacks_p
)->next
);
1449 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1450 (*callbacks_p
)->callback
= callback
;
1451 (*callbacks_p
)->periodic
= periodic
;
1452 (*callbacks_p
)->time_ms
= time_ms
;
1453 (*callbacks_p
)->removed
= false;
1455 gettimeofday(&(*callbacks_p
)->when
, NULL
);
1456 timeval_add_time(&(*callbacks_p
)->when
, 0, time_ms
* 1000);
1458 (*callbacks_p
)->priv
= priv
;
1459 (*callbacks_p
)->next
= NULL
;
1464 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1465 enum target_event event
, void *priv
), void *priv
)
1467 struct target_event_callback
**p
= &target_event_callbacks
;
1468 struct target_event_callback
*c
= target_event_callbacks
;
1470 if (callback
== NULL
)
1471 return ERROR_COMMAND_SYNTAX_ERROR
;
1474 struct target_event_callback
*next
= c
->next
;
1475 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1487 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1488 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1490 struct target_reset_callback
*entry
;
1492 if (callback
== NULL
)
1493 return ERROR_COMMAND_SYNTAX_ERROR
;
1495 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1496 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1497 list_del(&entry
->list
);
1506 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1507 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1509 struct target_trace_callback
*entry
;
1511 if (callback
== NULL
)
1512 return ERROR_COMMAND_SYNTAX_ERROR
;
1514 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1515 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1516 list_del(&entry
->list
);
1525 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1527 if (callback
== NULL
)
1528 return ERROR_COMMAND_SYNTAX_ERROR
;
1530 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1532 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1541 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1543 struct target_event_callback
*callback
= target_event_callbacks
;
1544 struct target_event_callback
*next_callback
;
1546 if (event
== TARGET_EVENT_HALTED
) {
1547 /* execute early halted first */
1548 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1551 LOG_DEBUG("target event %i (%s)", event
,
1552 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1554 target_handle_event(target
, event
);
1557 next_callback
= callback
->next
;
1558 callback
->callback(target
, event
, callback
->priv
);
1559 callback
= next_callback
;
1565 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1567 struct target_reset_callback
*callback
;
1569 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1570 Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1572 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1573 callback
->callback(target
, reset_mode
, callback
->priv
);
1578 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1580 struct target_trace_callback
*callback
;
1582 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1583 callback
->callback(target
, len
, data
, callback
->priv
);
1588 static int target_timer_callback_periodic_restart(
1589 struct target_timer_callback
*cb
, struct timeval
*now
)
1592 timeval_add_time(&cb
->when
, 0, cb
->time_ms
* 1000L);
1596 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1597 struct timeval
*now
)
1599 cb
->callback(cb
->priv
);
1602 return target_timer_callback_periodic_restart(cb
, now
);
1604 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1607 static int target_call_timer_callbacks_check_time(int checktime
)
1609 static bool callback_processing
;
1611 /* Do not allow nesting */
1612 if (callback_processing
)
1615 callback_processing
= true;
1620 gettimeofday(&now
, NULL
);
1622 /* Store an address of the place containing a pointer to the
1623 * next item; initially, that's a standalone "root of the
1624 * list" variable. */
1625 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1627 if ((*callback
)->removed
) {
1628 struct target_timer_callback
*p
= *callback
;
1629 *callback
= (*callback
)->next
;
1634 bool call_it
= (*callback
)->callback
&&
1635 ((!checktime
&& (*callback
)->periodic
) ||
1636 timeval_compare(&now
, &(*callback
)->when
) >= 0);
1639 target_call_timer_callback(*callback
, &now
);
1641 callback
= &(*callback
)->next
;
1644 callback_processing
= false;
1648 int target_call_timer_callbacks(void)
1650 return target_call_timer_callbacks_check_time(1);
1653 /* invoke periodic callbacks immediately */
1654 int target_call_timer_callbacks_now(void)
1656 return target_call_timer_callbacks_check_time(0);
1659 /* Prints the working area layout for debug purposes */
1660 static void print_wa_layout(struct target
*target
)
1662 struct working_area
*c
= target
->working_areas
;
1665 LOG_DEBUG("%c%c " TARGET_ADDR_FMT
"-" TARGET_ADDR_FMT
" (%" PRIu32
" bytes)",
1666 c
->backup ?
'b' : ' ', c
->free ?
' ' : '*',
1667 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1672 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1673 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1675 assert(area
->free
); /* Shouldn't split an allocated area */
1676 assert(size
<= area
->size
); /* Caller should guarantee this */
1678 /* Split only if not already the right size */
1679 if (size
< area
->size
) {
1680 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1685 new_wa
->next
= area
->next
;
1686 new_wa
->size
= area
->size
- size
;
1687 new_wa
->address
= area
->address
+ size
;
1688 new_wa
->backup
= NULL
;
1689 new_wa
->user
= NULL
;
1690 new_wa
->free
= true;
1692 area
->next
= new_wa
;
1695 /* If backup memory was allocated to this area, it has the wrong size
1696 * now so free it and it will be reallocated if/when needed */
1699 area
->backup
= NULL
;
1704 /* Merge all adjacent free areas into one */
1705 static void target_merge_working_areas(struct target
*target
)
1707 struct working_area
*c
= target
->working_areas
;
1709 while (c
&& c
->next
) {
1710 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1712 /* Find two adjacent free areas */
1713 if (c
->free
&& c
->next
->free
) {
1714 /* Merge the last into the first */
1715 c
->size
+= c
->next
->size
;
1717 /* Remove the last */
1718 struct working_area
*to_be_freed
= c
->next
;
1719 c
->next
= c
->next
->next
;
1720 if (to_be_freed
->backup
)
1721 free(to_be_freed
->backup
);
1724 /* If backup memory was allocated to the remaining area, it's has
1725 * the wrong size now */
1736 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1738 /* Reevaluate working area address based on MMU state*/
1739 if (target
->working_areas
== NULL
) {
1743 retval
= target
->type
->mmu(target
, &enabled
);
1744 if (retval
!= ERROR_OK
)
1748 if (target
->working_area_phys_spec
) {
1749 LOG_DEBUG("MMU disabled, using physical "
1750 "address for working memory " TARGET_ADDR_FMT
,
1751 target
->working_area_phys
);
1752 target
->working_area
= target
->working_area_phys
;
1754 LOG_ERROR("No working memory available. "
1755 "Specify -work-area-phys to target.");
1756 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1759 if (target
->working_area_virt_spec
) {
1760 LOG_DEBUG("MMU enabled, using virtual "
1761 "address for working memory " TARGET_ADDR_FMT
,
1762 target
->working_area_virt
);
1763 target
->working_area
= target
->working_area_virt
;
1765 LOG_ERROR("No working memory available. "
1766 "Specify -work-area-virt to target.");
1767 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1771 /* Set up initial working area on first call */
1772 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1774 new_wa
->next
= NULL
;
1775 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1776 new_wa
->address
= target
->working_area
;
1777 new_wa
->backup
= NULL
;
1778 new_wa
->user
= NULL
;
1779 new_wa
->free
= true;
1782 target
->working_areas
= new_wa
;
1785 /* only allocate multiples of 4 byte */
1787 size
= (size
+ 3) & (~3UL);
1789 struct working_area
*c
= target
->working_areas
;
1791 /* Find the first large enough working area */
1793 if (c
->free
&& c
->size
>= size
)
1799 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1801 /* Split the working area into the requested size */
1802 target_split_working_area(c
, size
);
1804 LOG_DEBUG("allocated new working area of %" PRIu32
" bytes at address " TARGET_ADDR_FMT
,
1807 if (target
->backup_working_area
) {
1808 if (c
->backup
== NULL
) {
1809 c
->backup
= malloc(c
->size
);
1810 if (c
->backup
== NULL
)
1814 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1815 if (retval
!= ERROR_OK
)
1819 /* mark as used, and return the new (reused) area */
1826 print_wa_layout(target
);
1831 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1835 retval
= target_alloc_working_area_try(target
, size
, area
);
1836 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1837 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1842 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1844 int retval
= ERROR_OK
;
1846 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1847 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1848 if (retval
!= ERROR_OK
)
1849 LOG_ERROR("failed to restore %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1850 area
->size
, area
->address
);
1856 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1857 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1859 int retval
= ERROR_OK
;
1865 retval
= target_restore_working_area(target
, area
);
1866 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1867 if (retval
!= ERROR_OK
)
1873 LOG_DEBUG("freed %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1874 area
->size
, area
->address
);
1876 /* mark user pointer invalid */
1877 /* TODO: Is this really safe? It points to some previous caller's memory.
1878 * How could we know that the area pointer is still in that place and not
1879 * some other vital data? What's the purpose of this, anyway? */
1883 target_merge_working_areas(target
);
1885 print_wa_layout(target
);
1890 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1892 return target_free_working_area_restore(target
, area
, 1);
1895 static void target_destroy(struct target
*target
)
1897 if (target
->type
->deinit_target
)
1898 target
->type
->deinit_target(target
);
1900 if (target
->semihosting
)
1901 free(target
->semihosting
);
1903 jtag_unregister_event_callback(jtag_enable_callback
, target
);
1905 struct target_event_action
*teap
= target
->event_action
;
1907 struct target_event_action
*next
= teap
->next
;
1908 Jim_DecrRefCount(teap
->interp
, teap
->body
);
1913 target_free_all_working_areas(target
);
1914 /* Now we have none or only one working area marked as free */
1915 if (target
->working_areas
) {
1916 free(target
->working_areas
->backup
);
1917 free(target
->working_areas
);
1920 /* release the targets SMP list */
1922 struct target_list
*head
= target
->head
;
1923 while (head
!= NULL
) {
1924 struct target_list
*pos
= head
->next
;
1925 head
->target
->smp
= 0;
1932 free(target
->gdb_port_override
);
1934 free(target
->trace_info
);
1935 free(target
->fileio_info
);
1936 free(target
->cmd_name
);
1940 void target_quit(void)
1942 struct target_event_callback
*pe
= target_event_callbacks
;
1944 struct target_event_callback
*t
= pe
->next
;
1948 target_event_callbacks
= NULL
;
1950 struct target_timer_callback
*pt
= target_timer_callbacks
;
1952 struct target_timer_callback
*t
= pt
->next
;
1956 target_timer_callbacks
= NULL
;
1958 for (struct target
*target
= all_targets
; target
;) {
1962 target_destroy(target
);
1969 /* free resources and restore memory, if restoring memory fails,
1970 * free up resources anyway
1972 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1974 struct working_area
*c
= target
->working_areas
;
1976 LOG_DEBUG("freeing all working areas");
1978 /* Loop through all areas, restoring the allocated ones and marking them as free */
1982 target_restore_working_area(target
, c
);
1984 *c
->user
= NULL
; /* Same as above */
1990 /* Run a merge pass to combine all areas into one */
1991 target_merge_working_areas(target
);
1993 print_wa_layout(target
);
1996 void target_free_all_working_areas(struct target
*target
)
1998 target_free_all_working_areas_restore(target
, 1);
2001 /* Find the largest number of bytes that can be allocated */
2002 uint32_t target_get_working_area_avail(struct target
*target
)
2004 struct working_area
*c
= target
->working_areas
;
2005 uint32_t max_size
= 0;
2008 return target
->working_area_size
;
2011 if (c
->free
&& max_size
< c
->size
)
2020 int target_arch_state(struct target
*target
)
2023 if (target
== NULL
) {
2024 LOG_WARNING("No target has been configured");
2028 if (target
->state
!= TARGET_HALTED
)
2031 retval
= target
->type
->arch_state(target
);
2035 static int target_get_gdb_fileio_info_default(struct target
*target
,
2036 struct gdb_fileio_info
*fileio_info
)
2038 /* If target does not support semi-hosting function, target
2039 has no need to provide .get_gdb_fileio_info callback.
2040 It just return ERROR_FAIL and gdb_server will return "Txx"
2041 as target halted every time. */
2045 static int target_gdb_fileio_end_default(struct target
*target
,
2046 int retcode
, int fileio_errno
, bool ctrl_c
)
2051 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
2052 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
2054 struct timeval timeout
, now
;
2056 gettimeofday(&timeout
, NULL
);
2057 timeval_add_time(&timeout
, seconds
, 0);
2059 LOG_INFO("Starting profiling. Halting and resuming the"
2060 " target as often as we can...");
2062 uint32_t sample_count
= 0;
2063 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2064 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2066 int retval
= ERROR_OK
;
2068 target_poll(target
);
2069 if (target
->state
== TARGET_HALTED
) {
2070 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
2071 samples
[sample_count
++] = t
;
2072 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2073 retval
= target_resume(target
, 1, 0, 0, 0);
2074 target_poll(target
);
2075 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2076 } else if (target
->state
== TARGET_RUNNING
) {
2077 /* We want to quickly sample the PC. */
2078 retval
= target_halt(target
);
2080 LOG_INFO("Target not halted or running");
2085 if (retval
!= ERROR_OK
)
2088 gettimeofday(&now
, NULL
);
2089 if ((sample_count
>= max_num_samples
) || timeval_compare(&now
, &timeout
) >= 0) {
2090 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2095 *num_samples
= sample_count
;
2099 /* Single aligned words are guaranteed to use 16 or 32 bit access
2100 * mode respectively, otherwise data is handled as quickly as
2103 int target_write_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, const uint8_t *buffer
)
2105 LOG_DEBUG("writing buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2108 if (!target_was_examined(target
)) {
2109 LOG_ERROR("Target not examined yet");
2116 if ((address
+ size
- 1) < address
) {
2117 /* GDB can request this when e.g. PC is 0xfffffffc */
2118 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2124 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2127 static int target_write_buffer_default(struct target
*target
,
2128 target_addr_t address
, uint32_t count
, const uint8_t *buffer
)
2132 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2133 * will have something to do with the size we leave to it. */
2134 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2135 if (address
& size
) {
2136 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2137 if (retval
!= ERROR_OK
)
2145 /* Write the data with as large access size as possible. */
2146 for (; size
> 0; size
/= 2) {
2147 uint32_t aligned
= count
- count
% size
;
2149 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2150 if (retval
!= ERROR_OK
)
2161 /* Single aligned words are guaranteed to use 16 or 32 bit access
2162 * mode respectively, otherwise data is handled as quickly as
2165 int target_read_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
2167 LOG_DEBUG("reading buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2170 if (!target_was_examined(target
)) {
2171 LOG_ERROR("Target not examined yet");
2178 if ((address
+ size
- 1) < address
) {
2179 /* GDB can request this when e.g. PC is 0xfffffffc */
2180 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2186 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2189 static int target_read_buffer_default(struct target
*target
, target_addr_t address
, uint32_t count
, uint8_t *buffer
)
2193 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2194 * will have something to do with the size we leave to it. */
2195 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2196 if (address
& size
) {
2197 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2198 if (retval
!= ERROR_OK
)
2206 /* Read the data with as large access size as possible. */
2207 for (; size
> 0; size
/= 2) {
2208 uint32_t aligned
= count
- count
% size
;
2210 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2211 if (retval
!= ERROR_OK
)
2222 int target_checksum_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* crc
)
2227 uint32_t checksum
= 0;
2228 if (!target_was_examined(target
)) {
2229 LOG_ERROR("Target not examined yet");
2233 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2234 if (retval
!= ERROR_OK
) {
2235 buffer
= malloc(size
);
2236 if (buffer
== NULL
) {
2237 LOG_ERROR("error allocating buffer for section (%" PRId32
" bytes)", size
);
2238 return ERROR_COMMAND_SYNTAX_ERROR
;
2240 retval
= target_read_buffer(target
, address
, size
, buffer
);
2241 if (retval
!= ERROR_OK
) {
2246 /* convert to target endianness */
2247 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2248 uint32_t target_data
;
2249 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2250 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2253 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2262 int target_blank_check_memory(struct target
*target
,
2263 struct target_memory_check_block
*blocks
, int num_blocks
,
2264 uint8_t erased_value
)
2266 if (!target_was_examined(target
)) {
2267 LOG_ERROR("Target not examined yet");
2271 if (target
->type
->blank_check_memory
== NULL
)
2272 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2274 return target
->type
->blank_check_memory(target
, blocks
, num_blocks
, erased_value
);
2277 int target_read_u64(struct target
*target
, target_addr_t address
, uint64_t *value
)
2279 uint8_t value_buf
[8];
2280 if (!target_was_examined(target
)) {
2281 LOG_ERROR("Target not examined yet");
2285 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2287 if (retval
== ERROR_OK
) {
2288 *value
= target_buffer_get_u64(target
, value_buf
);
2289 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2294 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2301 int target_read_u32(struct target
*target
, target_addr_t address
, uint32_t *value
)
2303 uint8_t value_buf
[4];
2304 if (!target_was_examined(target
)) {
2305 LOG_ERROR("Target not examined yet");
2309 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2311 if (retval
== ERROR_OK
) {
2312 *value
= target_buffer_get_u32(target
, value_buf
);
2313 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2318 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2325 int target_read_u16(struct target
*target
, target_addr_t address
, uint16_t *value
)
2327 uint8_t value_buf
[2];
2328 if (!target_was_examined(target
)) {
2329 LOG_ERROR("Target not examined yet");
2333 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2335 if (retval
== ERROR_OK
) {
2336 *value
= target_buffer_get_u16(target
, value_buf
);
2337 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%4.4" PRIx16
,
2342 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2349 int target_read_u8(struct target
*target
, target_addr_t address
, uint8_t *value
)
2351 if (!target_was_examined(target
)) {
2352 LOG_ERROR("Target not examined yet");
2356 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2358 if (retval
== ERROR_OK
) {
2359 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2364 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2371 int target_write_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2374 uint8_t value_buf
[8];
2375 if (!target_was_examined(target
)) {
2376 LOG_ERROR("Target not examined yet");
2380 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2384 target_buffer_set_u64(target
, value_buf
, value
);
2385 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2386 if (retval
!= ERROR_OK
)
2387 LOG_DEBUG("failed: %i", retval
);
2392 int target_write_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2395 uint8_t value_buf
[4];
2396 if (!target_was_examined(target
)) {
2397 LOG_ERROR("Target not examined yet");
2401 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2405 target_buffer_set_u32(target
, value_buf
, value
);
2406 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2407 if (retval
!= ERROR_OK
)
2408 LOG_DEBUG("failed: %i", retval
);
2413 int target_write_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2416 uint8_t value_buf
[2];
2417 if (!target_was_examined(target
)) {
2418 LOG_ERROR("Target not examined yet");
2422 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2426 target_buffer_set_u16(target
, value_buf
, value
);
2427 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2428 if (retval
!= ERROR_OK
)
2429 LOG_DEBUG("failed: %i", retval
);
2434 int target_write_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2437 if (!target_was_examined(target
)) {
2438 LOG_ERROR("Target not examined yet");
2442 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2445 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2446 if (retval
!= ERROR_OK
)
2447 LOG_DEBUG("failed: %i", retval
);
2452 int target_write_phys_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2455 uint8_t value_buf
[8];
2456 if (!target_was_examined(target
)) {
2457 LOG_ERROR("Target not examined yet");
2461 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2465 target_buffer_set_u64(target
, value_buf
, value
);
2466 retval
= target_write_phys_memory(target
, address
, 8, 1, value_buf
);
2467 if (retval
!= ERROR_OK
)
2468 LOG_DEBUG("failed: %i", retval
);
2473 int target_write_phys_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2476 uint8_t value_buf
[4];
2477 if (!target_was_examined(target
)) {
2478 LOG_ERROR("Target not examined yet");
2482 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2486 target_buffer_set_u32(target
, value_buf
, value
);
2487 retval
= target_write_phys_memory(target
, address
, 4, 1, value_buf
);
2488 if (retval
!= ERROR_OK
)
2489 LOG_DEBUG("failed: %i", retval
);
2494 int target_write_phys_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2497 uint8_t value_buf
[2];
2498 if (!target_was_examined(target
)) {
2499 LOG_ERROR("Target not examined yet");
2503 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2507 target_buffer_set_u16(target
, value_buf
, value
);
2508 retval
= target_write_phys_memory(target
, address
, 2, 1, value_buf
);
2509 if (retval
!= ERROR_OK
)
2510 LOG_DEBUG("failed: %i", retval
);
2515 int target_write_phys_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2518 if (!target_was_examined(target
)) {
2519 LOG_ERROR("Target not examined yet");
2523 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2526 retval
= target_write_phys_memory(target
, address
, 1, 1, &value
);
2527 if (retval
!= ERROR_OK
)
2528 LOG_DEBUG("failed: %i", retval
);
2533 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2535 struct target
*target
= get_target(name
);
2536 if (target
== NULL
) {
2537 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2540 if (!target
->tap
->enabled
) {
2541 LOG_USER("Target: TAP %s is disabled, "
2542 "can't be the current target\n",
2543 target
->tap
->dotted_name
);
2547 cmd_ctx
->current_target
= target
;
2548 if (cmd_ctx
->current_target_override
)
2549 cmd_ctx
->current_target_override
= target
;
2555 COMMAND_HANDLER(handle_targets_command
)
2557 int retval
= ERROR_OK
;
2558 if (CMD_ARGC
== 1) {
2559 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2560 if (retval
== ERROR_OK
) {
2566 struct target
*target
= all_targets
;
2567 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2568 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2573 if (target
->tap
->enabled
)
2574 state
= target_state_name(target
);
2576 state
= "tap-disabled";
2578 if (CMD_CTX
->current_target
== target
)
2581 /* keep columns lined up to match the headers above */
2582 command_print(CMD_CTX
,
2583 "%2d%c %-18s %-10s %-6s %-18s %s",
2584 target
->target_number
,
2586 target_name(target
),
2587 target_type_name(target
),
2588 Jim_Nvp_value2name_simple(nvp_target_endian
,
2589 target
->endianness
)->name
,
2590 target
->tap
->dotted_name
,
2592 target
= target
->next
;
2598 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2600 static int powerDropout
;
2601 static int srstAsserted
;
2603 static int runPowerRestore
;
2604 static int runPowerDropout
;
2605 static int runSrstAsserted
;
2606 static int runSrstDeasserted
;
2608 static int sense_handler(void)
2610 static int prevSrstAsserted
;
2611 static int prevPowerdropout
;
2613 int retval
= jtag_power_dropout(&powerDropout
);
2614 if (retval
!= ERROR_OK
)
2618 powerRestored
= prevPowerdropout
&& !powerDropout
;
2620 runPowerRestore
= 1;
2622 int64_t current
= timeval_ms();
2623 static int64_t lastPower
;
2624 bool waitMore
= lastPower
+ 2000 > current
;
2625 if (powerDropout
&& !waitMore
) {
2626 runPowerDropout
= 1;
2627 lastPower
= current
;
2630 retval
= jtag_srst_asserted(&srstAsserted
);
2631 if (retval
!= ERROR_OK
)
2635 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2637 static int64_t lastSrst
;
2638 waitMore
= lastSrst
+ 2000 > current
;
2639 if (srstDeasserted
&& !waitMore
) {
2640 runSrstDeasserted
= 1;
2644 if (!prevSrstAsserted
&& srstAsserted
)
2645 runSrstAsserted
= 1;
2647 prevSrstAsserted
= srstAsserted
;
2648 prevPowerdropout
= powerDropout
;
2650 if (srstDeasserted
|| powerRestored
) {
2651 /* Other than logging the event we can't do anything here.
2652 * Issuing a reset is a particularly bad idea as we might
2653 * be inside a reset already.
2660 /* process target state changes */
2661 static int handle_target(void *priv
)
2663 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2664 int retval
= ERROR_OK
;
2666 if (!is_jtag_poll_safe()) {
2667 /* polling is disabled currently */
2671 /* we do not want to recurse here... */
2672 static int recursive
;
2676 /* danger! running these procedures can trigger srst assertions and power dropouts.
2677 * We need to avoid an infinite loop/recursion here and we do that by
2678 * clearing the flags after running these events.
2680 int did_something
= 0;
2681 if (runSrstAsserted
) {
2682 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2683 Jim_Eval(interp
, "srst_asserted");
2686 if (runSrstDeasserted
) {
2687 Jim_Eval(interp
, "srst_deasserted");
2690 if (runPowerDropout
) {
2691 LOG_INFO("Power dropout detected, running power_dropout proc.");
2692 Jim_Eval(interp
, "power_dropout");
2695 if (runPowerRestore
) {
2696 Jim_Eval(interp
, "power_restore");
2700 if (did_something
) {
2701 /* clear detect flags */
2705 /* clear action flags */
2707 runSrstAsserted
= 0;
2708 runSrstDeasserted
= 0;
2709 runPowerRestore
= 0;
2710 runPowerDropout
= 0;
2715 /* Poll targets for state changes unless that's globally disabled.
2716 * Skip targets that are currently disabled.
2718 for (struct target
*target
= all_targets
;
2719 is_jtag_poll_safe() && target
;
2720 target
= target
->next
) {
2722 if (!target_was_examined(target
))
2725 if (!target
->tap
->enabled
)
2728 if (target
->backoff
.times
> target
->backoff
.count
) {
2729 /* do not poll this time as we failed previously */
2730 target
->backoff
.count
++;
2733 target
->backoff
.count
= 0;
2735 /* only poll target if we've got power and srst isn't asserted */
2736 if (!powerDropout
&& !srstAsserted
) {
2737 /* polling may fail silently until the target has been examined */
2738 retval
= target_poll(target
);
2739 if (retval
!= ERROR_OK
) {
2740 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2741 if (target
->backoff
.times
* polling_interval
< 5000) {
2742 target
->backoff
.times
*= 2;
2743 target
->backoff
.times
++;
2746 /* Tell GDB to halt the debugger. This allows the user to
2747 * run monitor commands to handle the situation.
2749 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2751 if (target
->backoff
.times
> 0) {
2752 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
2753 target_reset_examined(target
);
2754 retval
= target_examine_one(target
);
2755 /* Target examination could have failed due to unstable connection,
2756 * but we set the examined flag anyway to repoll it later */
2757 if (retval
!= ERROR_OK
) {
2758 target
->examined
= true;
2759 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2760 target
->backoff
.times
* polling_interval
);
2765 /* Since we succeeded, we reset backoff count */
2766 target
->backoff
.times
= 0;
2773 COMMAND_HANDLER(handle_reg_command
)
2775 struct target
*target
;
2776 struct reg
*reg
= NULL
;
2782 target
= get_current_target(CMD_CTX
);
2784 /* list all available registers for the current target */
2785 if (CMD_ARGC
== 0) {
2786 struct reg_cache
*cache
= target
->reg_cache
;
2792 command_print(CMD_CTX
, "===== %s", cache
->name
);
2794 for (i
= 0, reg
= cache
->reg_list
;
2795 i
< cache
->num_regs
;
2796 i
++, reg
++, count
++) {
2797 /* only print cached values if they are valid */
2799 value
= buf_to_str(reg
->value
,
2801 command_print(CMD_CTX
,
2802 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2810 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2815 cache
= cache
->next
;
2821 /* access a single register by its ordinal number */
2822 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2824 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2826 struct reg_cache
*cache
= target
->reg_cache
;
2830 for (i
= 0; i
< cache
->num_regs
; i
++) {
2831 if (count
++ == num
) {
2832 reg
= &cache
->reg_list
[i
];
2838 cache
= cache
->next
;
2842 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2843 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2847 /* access a single register by its name */
2848 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2851 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2856 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2858 /* display a register */
2859 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2860 && (CMD_ARGV
[1][0] <= '9')))) {
2861 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2864 if (reg
->valid
== 0)
2865 reg
->type
->get(reg
);
2866 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2867 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2872 /* set register value */
2873 if (CMD_ARGC
== 2) {
2874 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2877 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2879 reg
->type
->set(reg
, buf
);
2881 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2882 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2890 return ERROR_COMMAND_SYNTAX_ERROR
;
2893 COMMAND_HANDLER(handle_poll_command
)
2895 int retval
= ERROR_OK
;
2896 struct target
*target
= get_current_target(CMD_CTX
);
2898 if (CMD_ARGC
== 0) {
2899 command_print(CMD_CTX
, "background polling: %s",
2900 jtag_poll_get_enabled() ?
"on" : "off");
2901 command_print(CMD_CTX
, "TAP: %s (%s)",
2902 target
->tap
->dotted_name
,
2903 target
->tap
->enabled ?
"enabled" : "disabled");
2904 if (!target
->tap
->enabled
)
2906 retval
= target_poll(target
);
2907 if (retval
!= ERROR_OK
)
2909 retval
= target_arch_state(target
);
2910 if (retval
!= ERROR_OK
)
2912 } else if (CMD_ARGC
== 1) {
2914 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2915 jtag_poll_set_enabled(enable
);
2917 return ERROR_COMMAND_SYNTAX_ERROR
;
2922 COMMAND_HANDLER(handle_wait_halt_command
)
2925 return ERROR_COMMAND_SYNTAX_ERROR
;
2927 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2928 if (1 == CMD_ARGC
) {
2929 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2930 if (ERROR_OK
!= retval
)
2931 return ERROR_COMMAND_SYNTAX_ERROR
;
2934 struct target
*target
= get_current_target(CMD_CTX
);
2935 return target_wait_state(target
, TARGET_HALTED
, ms
);
2938 /* wait for target state to change. The trick here is to have a low
2939 * latency for short waits and not to suck up all the CPU time
2942 * After 500ms, keep_alive() is invoked
2944 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2947 int64_t then
= 0, cur
;
2951 retval
= target_poll(target
);
2952 if (retval
!= ERROR_OK
)
2954 if (target
->state
== state
)
2959 then
= timeval_ms();
2960 LOG_DEBUG("waiting for target %s...",
2961 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2967 if ((cur
-then
) > ms
) {
2968 LOG_ERROR("timed out while waiting for target %s",
2969 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2977 COMMAND_HANDLER(handle_halt_command
)
2981 struct target
*target
= get_current_target(CMD_CTX
);
2983 target
->verbose_halt_msg
= true;
2985 int retval
= target_halt(target
);
2986 if (ERROR_OK
!= retval
)
2989 if (CMD_ARGC
== 1) {
2990 unsigned wait_local
;
2991 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2992 if (ERROR_OK
!= retval
)
2993 return ERROR_COMMAND_SYNTAX_ERROR
;
2998 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
3001 COMMAND_HANDLER(handle_soft_reset_halt_command
)
3003 struct target
*target
= get_current_target(CMD_CTX
);
3005 LOG_USER("requesting target halt and executing a soft reset");
3007 target_soft_reset_halt(target
);
3012 COMMAND_HANDLER(handle_reset_command
)
3015 return ERROR_COMMAND_SYNTAX_ERROR
;
3017 enum target_reset_mode reset_mode
= RESET_RUN
;
3018 if (CMD_ARGC
== 1) {
3020 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
3021 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
3022 return ERROR_COMMAND_SYNTAX_ERROR
;
3023 reset_mode
= n
->value
;
3026 /* reset *all* targets */
3027 return target_process_reset(CMD_CTX
, reset_mode
);
3031 COMMAND_HANDLER(handle_resume_command
)
3035 return ERROR_COMMAND_SYNTAX_ERROR
;
3037 struct target
*target
= get_current_target(CMD_CTX
);
3039 /* with no CMD_ARGV, resume from current pc, addr = 0,
3040 * with one arguments, addr = CMD_ARGV[0],
3041 * handle breakpoints, not debugging */
3042 target_addr_t addr
= 0;
3043 if (CMD_ARGC
== 1) {
3044 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3048 return target_resume(target
, current
, addr
, 1, 0);
3051 COMMAND_HANDLER(handle_step_command
)
3054 return ERROR_COMMAND_SYNTAX_ERROR
;
3058 /* with no CMD_ARGV, step from current pc, addr = 0,
3059 * with one argument addr = CMD_ARGV[0],
3060 * handle breakpoints, debugging */
3061 target_addr_t addr
= 0;
3063 if (CMD_ARGC
== 1) {
3064 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3068 struct target
*target
= get_current_target(CMD_CTX
);
3070 return target
->type
->step(target
, current_pc
, addr
, 1);
3073 static void handle_md_output(struct command_context
*cmd_ctx
,
3074 struct target
*target
, target_addr_t address
, unsigned size
,
3075 unsigned count
, const uint8_t *buffer
)
3077 const unsigned line_bytecnt
= 32;
3078 unsigned line_modulo
= line_bytecnt
/ size
;
3080 char output
[line_bytecnt
* 4 + 1];
3081 unsigned output_len
= 0;
3083 const char *value_fmt
;
3086 value_fmt
= "%16.16"PRIx64
" ";
3089 value_fmt
= "%8.8"PRIx64
" ";
3092 value_fmt
= "%4.4"PRIx64
" ";
3095 value_fmt
= "%2.2"PRIx64
" ";
3098 /* "can't happen", caller checked */
3099 LOG_ERROR("invalid memory read size: %u", size
);
3103 for (unsigned i
= 0; i
< count
; i
++) {
3104 if (i
% line_modulo
== 0) {
3105 output_len
+= snprintf(output
+ output_len
,
3106 sizeof(output
) - output_len
,
3107 TARGET_ADDR_FMT
": ",
3108 (address
+ (i
* size
)));
3112 const uint8_t *value_ptr
= buffer
+ i
* size
;
3115 value
= target_buffer_get_u64(target
, value_ptr
);
3118 value
= target_buffer_get_u32(target
, value_ptr
);
3121 value
= target_buffer_get_u16(target
, value_ptr
);
3126 output_len
+= snprintf(output
+ output_len
,
3127 sizeof(output
) - output_len
,
3130 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
3131 command_print(cmd_ctx
, "%s", output
);
3137 COMMAND_HANDLER(handle_md_command
)
3140 return ERROR_COMMAND_SYNTAX_ERROR
;
3143 switch (CMD_NAME
[2]) {
3157 return ERROR_COMMAND_SYNTAX_ERROR
;
3160 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3161 int (*fn
)(struct target
*target
,
3162 target_addr_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
3166 fn
= target_read_phys_memory
;
3168 fn
= target_read_memory
;
3169 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
3170 return ERROR_COMMAND_SYNTAX_ERROR
;
3172 target_addr_t address
;
3173 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3177 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
3179 uint8_t *buffer
= calloc(count
, size
);
3180 if (buffer
== NULL
) {
3181 LOG_ERROR("Failed to allocate md read buffer");
3185 struct target
*target
= get_current_target(CMD_CTX
);
3186 int retval
= fn(target
, address
, size
, count
, buffer
);
3187 if (ERROR_OK
== retval
)
3188 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
3195 typedef int (*target_write_fn
)(struct target
*target
,
3196 target_addr_t address
, uint32_t size
, uint32_t count
, <