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"
58 /* default halt wait timeout (ms) */
59 #define DEFAULT_HALT_TIMEOUT 5000
61 static int target_read_buffer_default(struct target
*target
, target_addr_t address
,
62 uint32_t count
, uint8_t *buffer
);
63 static int target_write_buffer_default(struct target
*target
, target_addr_t address
,
64 uint32_t count
, const uint8_t *buffer
);
65 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
66 int argc
, Jim_Obj
* const *argv
);
67 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
68 int argc
, Jim_Obj
* const *argv
);
69 static int target_register_user_commands(struct command_context
*cmd_ctx
);
70 static int target_get_gdb_fileio_info_default(struct target
*target
,
71 struct gdb_fileio_info
*fileio_info
);
72 static int target_gdb_fileio_end_default(struct target
*target
, int retcode
,
73 int fileio_errno
, bool ctrl_c
);
74 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
75 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
);
78 extern struct target_type arm7tdmi_target
;
79 extern struct target_type arm720t_target
;
80 extern struct target_type arm9tdmi_target
;
81 extern struct target_type arm920t_target
;
82 extern struct target_type arm966e_target
;
83 extern struct target_type arm946e_target
;
84 extern struct target_type arm926ejs_target
;
85 extern struct target_type fa526_target
;
86 extern struct target_type feroceon_target
;
87 extern struct target_type dragonite_target
;
88 extern struct target_type xscale_target
;
89 extern struct target_type cortexm_target
;
90 extern struct target_type cortexa_target
;
91 extern struct target_type aarch64_target
;
92 extern struct target_type cortexr4_target
;
93 extern struct target_type arm11_target
;
94 extern struct target_type ls1_sap_target
;
95 extern struct target_type mips_m4k_target
;
96 extern struct target_type avr_target
;
97 extern struct target_type dsp563xx_target
;
98 extern struct target_type dsp5680xx_target
;
99 extern struct target_type testee_target
;
100 extern struct target_type avr32_ap7k_target
;
101 extern struct target_type hla_target
;
102 extern struct target_type nds32_v2_target
;
103 extern struct target_type nds32_v3_target
;
104 extern struct target_type nds32_v3m_target
;
105 extern struct target_type or1k_target
;
106 extern struct target_type quark_x10xx_target
;
107 extern struct target_type quark_d20xx_target
;
108 extern struct target_type stm8_target
;
110 static struct target_type
*target_types
[] = {
147 struct target
*all_targets
;
148 static struct target_event_callback
*target_event_callbacks
;
149 static struct target_timer_callback
*target_timer_callbacks
;
150 LIST_HEAD(target_reset_callback_list
);
151 LIST_HEAD(target_trace_callback_list
);
152 static const int polling_interval
= 100;
154 static const Jim_Nvp nvp_assert
[] = {
155 { .name
= "assert", NVP_ASSERT
},
156 { .name
= "deassert", NVP_DEASSERT
},
157 { .name
= "T", NVP_ASSERT
},
158 { .name
= "F", NVP_DEASSERT
},
159 { .name
= "t", NVP_ASSERT
},
160 { .name
= "f", NVP_DEASSERT
},
161 { .name
= NULL
, .value
= -1 }
164 static const Jim_Nvp nvp_error_target
[] = {
165 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
166 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
167 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
168 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
169 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
170 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
171 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
172 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
173 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
174 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
175 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
176 { .value
= -1, .name
= NULL
}
179 static const char *target_strerror_safe(int err
)
183 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
190 static const Jim_Nvp nvp_target_event
[] = {
192 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
193 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
194 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
195 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
196 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
198 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
199 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
201 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
202 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
203 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
204 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
205 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
206 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
207 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
208 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
210 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
211 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
213 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
214 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
216 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
217 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
219 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
220 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
222 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
223 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
225 { .value
= TARGET_EVENT_TRACE_CONFIG
, .name
= "trace-config" },
227 { .name
= NULL
, .value
= -1 }
230 static const Jim_Nvp nvp_target_state
[] = {
231 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
232 { .name
= "running", .value
= TARGET_RUNNING
},
233 { .name
= "halted", .value
= TARGET_HALTED
},
234 { .name
= "reset", .value
= TARGET_RESET
},
235 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
236 { .name
= NULL
, .value
= -1 },
239 static const Jim_Nvp nvp_target_debug_reason
[] = {
240 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
241 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
242 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
243 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
244 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
245 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
246 { .name
= "program-exit" , .value
= DBG_REASON_EXIT
},
247 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
248 { .name
= NULL
, .value
= -1 },
251 static const Jim_Nvp nvp_target_endian
[] = {
252 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
253 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
254 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
255 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
256 { .name
= NULL
, .value
= -1 },
259 static const Jim_Nvp nvp_reset_modes
[] = {
260 { .name
= "unknown", .value
= RESET_UNKNOWN
},
261 { .name
= "run" , .value
= RESET_RUN
},
262 { .name
= "halt" , .value
= RESET_HALT
},
263 { .name
= "init" , .value
= RESET_INIT
},
264 { .name
= NULL
, .value
= -1 },
267 const char *debug_reason_name(struct target
*t
)
271 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
272 t
->debug_reason
)->name
;
274 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
275 cp
= "(*BUG*unknown*BUG*)";
280 const char *target_state_name(struct target
*t
)
283 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
285 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
286 cp
= "(*BUG*unknown*BUG*)";
289 if (!target_was_examined(t
) && t
->defer_examine
)
290 cp
= "examine deferred";
295 const char *target_event_name(enum target_event event
)
298 cp
= Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
;
300 LOG_ERROR("Invalid target event: %d", (int)(event
));
301 cp
= "(*BUG*unknown*BUG*)";
306 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
309 cp
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
;
311 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
312 cp
= "(*BUG*unknown*BUG*)";
317 /* determine the number of the new target */
318 static int new_target_number(void)
323 /* number is 0 based */
327 if (x
< t
->target_number
)
328 x
= t
->target_number
;
334 /* read a uint64_t from a buffer in target memory endianness */
335 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
337 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
338 return le_to_h_u64(buffer
);
340 return be_to_h_u64(buffer
);
343 /* read a uint32_t from a buffer in target memory endianness */
344 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
346 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
347 return le_to_h_u32(buffer
);
349 return be_to_h_u32(buffer
);
352 /* read a uint24_t from a buffer in target memory endianness */
353 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
355 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
356 return le_to_h_u24(buffer
);
358 return be_to_h_u24(buffer
);
361 /* read a uint16_t from a buffer in target memory endianness */
362 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
364 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
365 return le_to_h_u16(buffer
);
367 return be_to_h_u16(buffer
);
370 /* read a uint8_t from a buffer in target memory endianness */
371 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
373 return *buffer
& 0x0ff;
376 /* write a uint64_t to a buffer in target memory endianness */
377 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
379 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
380 h_u64_to_le(buffer
, value
);
382 h_u64_to_be(buffer
, value
);
385 /* write a uint32_t to a buffer in target memory endianness */
386 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
388 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
389 h_u32_to_le(buffer
, value
);
391 h_u32_to_be(buffer
, value
);
394 /* write a uint24_t to a buffer in target memory endianness */
395 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
397 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
398 h_u24_to_le(buffer
, value
);
400 h_u24_to_be(buffer
, value
);
403 /* write a uint16_t to a buffer in target memory endianness */
404 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
406 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
407 h_u16_to_le(buffer
, value
);
409 h_u16_to_be(buffer
, value
);
412 /* write a uint8_t to a buffer in target memory endianness */
413 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
418 /* write a uint64_t array to a buffer in target memory endianness */
419 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
422 for (i
= 0; i
< count
; i
++)
423 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
426 /* write a uint32_t array to a buffer in target memory endianness */
427 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
430 for (i
= 0; i
< count
; i
++)
431 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
434 /* write a uint16_t array to a buffer in target memory endianness */
435 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
438 for (i
= 0; i
< count
; i
++)
439 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
442 /* write a uint64_t array to a buffer in target memory endianness */
443 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
446 for (i
= 0; i
< count
; i
++)
447 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
450 /* write a uint32_t array to a buffer in target memory endianness */
451 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
454 for (i
= 0; i
< count
; i
++)
455 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
458 /* write a uint16_t array to a buffer in target memory endianness */
459 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
462 for (i
= 0; i
< count
; i
++)
463 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
466 /* return a pointer to a configured target; id is name or number */
467 struct target
*get_target(const char *id
)
469 struct target
*target
;
471 /* try as tcltarget name */
472 for (target
= all_targets
; target
; target
= target
->next
) {
473 if (target_name(target
) == NULL
)
475 if (strcmp(id
, target_name(target
)) == 0)
479 /* It's OK to remove this fallback sometime after August 2010 or so */
481 /* no match, try as number */
483 if (parse_uint(id
, &num
) != ERROR_OK
)
486 for (target
= all_targets
; target
; target
= target
->next
) {
487 if (target
->target_number
== (int)num
) {
488 LOG_WARNING("use '%s' as target identifier, not '%u'",
489 target_name(target
), num
);
497 /* returns a pointer to the n-th configured target */
498 struct target
*get_target_by_num(int num
)
500 struct target
*target
= all_targets
;
503 if (target
->target_number
== num
)
505 target
= target
->next
;
511 struct target
*get_current_target(struct command_context
*cmd_ctx
)
513 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
515 if (target
== NULL
) {
516 LOG_ERROR("BUG: current_target out of bounds");
523 int target_poll(struct target
*target
)
527 /* We can't poll until after examine */
528 if (!target_was_examined(target
)) {
529 /* Fail silently lest we pollute the log */
533 retval
= target
->type
->poll(target
);
534 if (retval
!= ERROR_OK
)
537 if (target
->halt_issued
) {
538 if (target
->state
== TARGET_HALTED
)
539 target
->halt_issued
= false;
541 int64_t t
= timeval_ms() - target
->halt_issued_time
;
542 if (t
> DEFAULT_HALT_TIMEOUT
) {
543 target
->halt_issued
= false;
544 LOG_INFO("Halt timed out, wake up GDB.");
545 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
553 int target_halt(struct target
*target
)
556 /* We can't poll until after examine */
557 if (!target_was_examined(target
)) {
558 LOG_ERROR("Target not examined yet");
562 retval
= target
->type
->halt(target
);
563 if (retval
!= ERROR_OK
)
566 target
->halt_issued
= true;
567 target
->halt_issued_time
= timeval_ms();
573 * Make the target (re)start executing using its saved execution
574 * context (possibly with some modifications).
576 * @param target Which target should start executing.
577 * @param current True to use the target's saved program counter instead
578 * of the address parameter
579 * @param address Optionally used as the program counter.
580 * @param handle_breakpoints True iff breakpoints at the resumption PC
581 * should be skipped. (For example, maybe execution was stopped by
582 * such a breakpoint, in which case it would be counterprodutive to
584 * @param debug_execution False if all working areas allocated by OpenOCD
585 * should be released and/or restored to their original contents.
586 * (This would for example be true to run some downloaded "helper"
587 * algorithm code, which resides in one such working buffer and uses
588 * another for data storage.)
590 * @todo Resolve the ambiguity about what the "debug_execution" flag
591 * signifies. For example, Target implementations don't agree on how
592 * it relates to invalidation of the register cache, or to whether
593 * breakpoints and watchpoints should be enabled. (It would seem wrong
594 * to enable breakpoints when running downloaded "helper" algorithms
595 * (debug_execution true), since the breakpoints would be set to match
596 * target firmware being debugged, not the helper algorithm.... and
597 * enabling them could cause such helpers to malfunction (for example,
598 * by overwriting data with a breakpoint instruction. On the other
599 * hand the infrastructure for running such helpers might use this
600 * procedure but rely on hardware breakpoint to detect termination.)
602 int target_resume(struct target
*target
, int current
, target_addr_t address
,
603 int handle_breakpoints
, int debug_execution
)
607 /* We can't poll until after examine */
608 if (!target_was_examined(target
)) {
609 LOG_ERROR("Target not examined yet");
613 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
615 /* note that resume *must* be asynchronous. The CPU can halt before
616 * we poll. The CPU can even halt at the current PC as a result of
617 * a software breakpoint being inserted by (a bug?) the application.
619 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
620 if (retval
!= ERROR_OK
)
623 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
628 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
633 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
634 if (n
->name
== NULL
) {
635 LOG_ERROR("invalid reset mode");
639 struct target
*target
;
640 for (target
= all_targets
; target
; target
= target
->next
)
641 target_call_reset_callbacks(target
, reset_mode
);
643 /* disable polling during reset to make reset event scripts
644 * more predictable, i.e. dr/irscan & pathmove in events will
645 * not have JTAG operations injected into the middle of a sequence.
647 bool save_poll
= jtag_poll_get_enabled();
649 jtag_poll_set_enabled(false);
651 sprintf(buf
, "ocd_process_reset %s", n
->name
);
652 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
654 jtag_poll_set_enabled(save_poll
);
656 if (retval
!= JIM_OK
) {
657 Jim_MakeErrorMessage(cmd_ctx
->interp
);
658 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
662 /* We want any events to be processed before the prompt */
663 retval
= target_call_timer_callbacks_now();
665 for (target
= all_targets
; target
; target
= target
->next
) {
666 target
->type
->check_reset(target
);
667 target
->running_alg
= false;
673 static int identity_virt2phys(struct target
*target
,
674 target_addr_t
virtual, target_addr_t
*physical
)
680 static int no_mmu(struct target
*target
, int *enabled
)
686 static int default_examine(struct target
*target
)
688 target_set_examined(target
);
692 /* no check by default */
693 static int default_check_reset(struct target
*target
)
698 int target_examine_one(struct target
*target
)
700 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
702 int retval
= target
->type
->examine(target
);
703 if (retval
!= ERROR_OK
)
706 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
711 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
713 struct target
*target
= priv
;
715 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
718 jtag_unregister_event_callback(jtag_enable_callback
, target
);
720 return target_examine_one(target
);
723 /* Targets that correctly implement init + examine, i.e.
724 * no communication with target during init:
728 int target_examine(void)
730 int retval
= ERROR_OK
;
731 struct target
*target
;
733 for (target
= all_targets
; target
; target
= target
->next
) {
734 /* defer examination, but don't skip it */
735 if (!target
->tap
->enabled
) {
736 jtag_register_event_callback(jtag_enable_callback
,
741 if (target
->defer_examine
)
744 retval
= target_examine_one(target
);
745 if (retval
!= ERROR_OK
)
751 const char *target_type_name(struct target
*target
)
753 return target
->type
->name
;
756 static int target_soft_reset_halt(struct target
*target
)
758 if (!target_was_examined(target
)) {
759 LOG_ERROR("Target not examined yet");
762 if (!target
->type
->soft_reset_halt
) {
763 LOG_ERROR("Target %s does not support soft_reset_halt",
764 target_name(target
));
767 return target
->type
->soft_reset_halt(target
);
771 * Downloads a target-specific native code algorithm to the target,
772 * and executes it. * Note that some targets may need to set up, enable,
773 * and tear down a breakpoint (hard or * soft) to detect algorithm
774 * termination, while others may support lower overhead schemes where
775 * soft breakpoints embedded in the algorithm automatically terminate the
778 * @param target used to run the algorithm
779 * @param arch_info target-specific description of the algorithm.
781 int target_run_algorithm(struct target
*target
,
782 int num_mem_params
, struct mem_param
*mem_params
,
783 int num_reg_params
, struct reg_param
*reg_param
,
784 uint32_t entry_point
, uint32_t exit_point
,
785 int timeout_ms
, void *arch_info
)
787 int retval
= ERROR_FAIL
;
789 if (!target_was_examined(target
)) {
790 LOG_ERROR("Target not examined yet");
793 if (!target
->type
->run_algorithm
) {
794 LOG_ERROR("Target type '%s' does not support %s",
795 target_type_name(target
), __func__
);
799 target
->running_alg
= true;
800 retval
= target
->type
->run_algorithm(target
,
801 num_mem_params
, mem_params
,
802 num_reg_params
, reg_param
,
803 entry_point
, exit_point
, timeout_ms
, arch_info
);
804 target
->running_alg
= false;
811 * Executes a target-specific native code algorithm and leaves it running.
813 * @param target used to run the algorithm
814 * @param arch_info target-specific description of the algorithm.
816 int target_start_algorithm(struct target
*target
,
817 int num_mem_params
, struct mem_param
*mem_params
,
818 int num_reg_params
, struct reg_param
*reg_params
,
819 uint32_t entry_point
, uint32_t exit_point
,
822 int retval
= ERROR_FAIL
;
824 if (!target_was_examined(target
)) {
825 LOG_ERROR("Target not examined yet");
828 if (!target
->type
->start_algorithm
) {
829 LOG_ERROR("Target type '%s' does not support %s",
830 target_type_name(target
), __func__
);
833 if (target
->running_alg
) {
834 LOG_ERROR("Target is already running an algorithm");
838 target
->running_alg
= true;
839 retval
= target
->type
->start_algorithm(target
,
840 num_mem_params
, mem_params
,
841 num_reg_params
, reg_params
,
842 entry_point
, exit_point
, arch_info
);
849 * Waits for an algorithm started with target_start_algorithm() to complete.
851 * @param target used to run the algorithm
852 * @param arch_info target-specific description of the algorithm.
854 int target_wait_algorithm(struct target
*target
,
855 int num_mem_params
, struct mem_param
*mem_params
,
856 int num_reg_params
, struct reg_param
*reg_params
,
857 uint32_t exit_point
, int timeout_ms
,
860 int retval
= ERROR_FAIL
;
862 if (!target
->type
->wait_algorithm
) {
863 LOG_ERROR("Target type '%s' does not support %s",
864 target_type_name(target
), __func__
);
867 if (!target
->running_alg
) {
868 LOG_ERROR("Target is not running an algorithm");
872 retval
= target
->type
->wait_algorithm(target
,
873 num_mem_params
, mem_params
,
874 num_reg_params
, reg_params
,
875 exit_point
, timeout_ms
, arch_info
);
876 if (retval
!= ERROR_TARGET_TIMEOUT
)
877 target
->running_alg
= false;
884 * Streams data to a circular buffer on target intended for consumption by code
885 * running asynchronously on target.
887 * This is intended for applications where target-specific native code runs
888 * on the target, receives data from the circular buffer, does something with
889 * it (most likely writing it to a flash memory), and advances the circular
892 * This assumes that the helper algorithm has already been loaded to the target,
893 * but has not been started yet. Given memory and register parameters are passed
896 * The buffer is defined by (buffer_start, buffer_size) arguments and has the
899 * [buffer_start + 0, buffer_start + 4):
900 * Write Pointer address (aka head). Written and updated by this
901 * routine when new data is written to the circular buffer.
902 * [buffer_start + 4, buffer_start + 8):
903 * Read Pointer address (aka tail). Updated by code running on the
904 * target after it consumes data.
905 * [buffer_start + 8, buffer_start + buffer_size):
906 * Circular buffer contents.
908 * See contrib/loaders/flash/stm32f1x.S for an example.
910 * @param target used to run the algorithm
911 * @param buffer address on the host where data to be sent is located
912 * @param count number of blocks to send
913 * @param block_size size in bytes of each block
914 * @param num_mem_params count of memory-based params to pass to algorithm
915 * @param mem_params memory-based params to pass to algorithm
916 * @param num_reg_params count of register-based params to pass to algorithm
917 * @param reg_params memory-based params to pass to algorithm
918 * @param buffer_start address on the target of the circular buffer structure
919 * @param buffer_size size of the circular buffer structure
920 * @param entry_point address on the target to execute to start the algorithm
921 * @param exit_point address at which to set a breakpoint to catch the
922 * end of the algorithm; can be 0 if target triggers a breakpoint itself
925 int target_run_flash_async_algorithm(struct target
*target
,
926 const uint8_t *buffer
, uint32_t count
, int block_size
,
927 int num_mem_params
, struct mem_param
*mem_params
,
928 int num_reg_params
, struct reg_param
*reg_params
,
929 uint32_t buffer_start
, uint32_t buffer_size
,
930 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
935 const uint8_t *buffer_orig
= buffer
;
937 /* Set up working area. First word is write pointer, second word is read pointer,
938 * rest is fifo data area. */
939 uint32_t wp_addr
= buffer_start
;
940 uint32_t rp_addr
= buffer_start
+ 4;
941 uint32_t fifo_start_addr
= buffer_start
+ 8;
942 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
944 uint32_t wp
= fifo_start_addr
;
945 uint32_t rp
= fifo_start_addr
;
947 /* validate block_size is 2^n */
948 assert(!block_size
|| !(block_size
& (block_size
- 1)));
950 retval
= target_write_u32(target
, wp_addr
, wp
);
951 if (retval
!= ERROR_OK
)
953 retval
= target_write_u32(target
, rp_addr
, rp
);
954 if (retval
!= ERROR_OK
)
957 /* Start up algorithm on target and let it idle while writing the first chunk */
958 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
959 num_reg_params
, reg_params
,
964 if (retval
!= ERROR_OK
) {
965 LOG_ERROR("error starting target flash write algorithm");
971 retval
= target_read_u32(target
, rp_addr
, &rp
);
972 if (retval
!= ERROR_OK
) {
973 LOG_ERROR("failed to get read pointer");
977 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
978 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
981 LOG_ERROR("flash write algorithm aborted by target");
982 retval
= ERROR_FLASH_OPERATION_FAILED
;
986 if (((rp
- fifo_start_addr
) & (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
987 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
991 /* Count the number of bytes available in the fifo without
992 * crossing the wrap around. Make sure to not fill it completely,
993 * because that would make wp == rp and that's the empty condition. */
994 uint32_t thisrun_bytes
;
996 thisrun_bytes
= rp
- wp
- block_size
;
997 else if (rp
> fifo_start_addr
)
998 thisrun_bytes
= fifo_end_addr
- wp
;
1000 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
1002 if (thisrun_bytes
== 0) {
1003 /* Throttle polling a bit if transfer is (much) faster than flash
1004 * programming. The exact delay shouldn't matter as long as it's
1005 * less than buffer size / flash speed. This is very unlikely to
1006 * run when using high latency connections such as USB. */
1009 /* to stop an infinite loop on some targets check and increment a timeout
1010 * this issue was observed on a stellaris using the new ICDI interface */
1011 if (timeout
++ >= 500) {
1012 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1013 return ERROR_FLASH_OPERATION_FAILED
;
1018 /* reset our timeout */
1021 /* Limit to the amount of data we actually want to write */
1022 if (thisrun_bytes
> count
* block_size
)
1023 thisrun_bytes
= count
* block_size
;
1025 /* Write data to fifo */
1026 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
1027 if (retval
!= ERROR_OK
)
1030 /* Update counters and wrap write pointer */
1031 buffer
+= thisrun_bytes
;
1032 count
-= thisrun_bytes
/ block_size
;
1033 wp
+= thisrun_bytes
;
1034 if (wp
>= fifo_end_addr
)
1035 wp
= fifo_start_addr
;
1037 /* Store updated write pointer to target */
1038 retval
= target_write_u32(target
, wp_addr
, wp
);
1039 if (retval
!= ERROR_OK
)
1043 if (retval
!= ERROR_OK
) {
1044 /* abort flash write algorithm on target */
1045 target_write_u32(target
, wp_addr
, 0);
1048 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1049 num_reg_params
, reg_params
,
1054 if (retval2
!= ERROR_OK
) {
1055 LOG_ERROR("error waiting for target flash write algorithm");
1059 if (retval
== ERROR_OK
) {
1060 /* check if algorithm set rp = 0 after fifo writer loop finished */
1061 retval
= target_read_u32(target
, rp_addr
, &rp
);
1062 if (retval
== ERROR_OK
&& rp
== 0) {
1063 LOG_ERROR("flash write algorithm aborted by target");
1064 retval
= ERROR_FLASH_OPERATION_FAILED
;
1071 int target_read_memory(struct target
*target
,
1072 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1074 if (!target_was_examined(target
)) {
1075 LOG_ERROR("Target not examined yet");
1078 if (!target
->type
->read_memory
) {
1079 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1082 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1085 int target_read_phys_memory(struct target
*target
,
1086 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1088 if (!target_was_examined(target
)) {
1089 LOG_ERROR("Target not examined yet");
1092 if (!target
->type
->read_phys_memory
) {
1093 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1096 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1099 int target_write_memory(struct target
*target
,
1100 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1102 if (!target_was_examined(target
)) {
1103 LOG_ERROR("Target not examined yet");
1106 if (!target
->type
->write_memory
) {
1107 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1110 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1113 int target_write_phys_memory(struct target
*target
,
1114 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1116 if (!target_was_examined(target
)) {
1117 LOG_ERROR("Target not examined yet");
1120 if (!target
->type
->write_phys_memory
) {
1121 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1124 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1127 int target_add_breakpoint(struct target
*target
,
1128 struct breakpoint
*breakpoint
)
1130 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1131 LOG_WARNING("target %s is not halted (add breakpoint)", target_name(target
));
1132 return ERROR_TARGET_NOT_HALTED
;
1134 return target
->type
->add_breakpoint(target
, breakpoint
);
1137 int target_add_context_breakpoint(struct target
*target
,
1138 struct breakpoint
*breakpoint
)
1140 if (target
->state
!= TARGET_HALTED
) {
1141 LOG_WARNING("target %s is not halted (add context breakpoint)", target_name(target
));
1142 return ERROR_TARGET_NOT_HALTED
;
1144 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1147 int target_add_hybrid_breakpoint(struct target
*target
,
1148 struct breakpoint
*breakpoint
)
1150 if (target
->state
!= TARGET_HALTED
) {
1151 LOG_WARNING("target %s is not halted (add hybrid breakpoint)", target_name(target
));
1152 return ERROR_TARGET_NOT_HALTED
;
1154 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1157 int target_remove_breakpoint(struct target
*target
,
1158 struct breakpoint
*breakpoint
)
1160 return target
->type
->remove_breakpoint(target
, breakpoint
);
1163 int target_add_watchpoint(struct target
*target
,
1164 struct watchpoint
*watchpoint
)
1166 if (target
->state
!= TARGET_HALTED
) {
1167 LOG_WARNING("target %s is not halted (add watchpoint)", target_name(target
));
1168 return ERROR_TARGET_NOT_HALTED
;
1170 return target
->type
->add_watchpoint(target
, watchpoint
);
1172 int target_remove_watchpoint(struct target
*target
,
1173 struct watchpoint
*watchpoint
)
1175 return target
->type
->remove_watchpoint(target
, watchpoint
);
1177 int target_hit_watchpoint(struct target
*target
,
1178 struct watchpoint
**hit_watchpoint
)
1180 if (target
->state
!= TARGET_HALTED
) {
1181 LOG_WARNING("target %s is not halted (hit watchpoint)", target
->cmd_name
);
1182 return ERROR_TARGET_NOT_HALTED
;
1185 if (target
->type
->hit_watchpoint
== NULL
) {
1186 /* For backward compatible, if hit_watchpoint is not implemented,
1187 * return ERROR_FAIL such that gdb_server will not take the nonsense
1192 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1195 int target_get_gdb_reg_list(struct target
*target
,
1196 struct reg
**reg_list
[], int *reg_list_size
,
1197 enum target_register_class reg_class
)
1199 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1201 int target_step(struct target
*target
,
1202 int current
, target_addr_t address
, int handle_breakpoints
)
1204 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1207 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1209 if (target
->state
!= TARGET_HALTED
) {
1210 LOG_WARNING("target %s is not halted (gdb fileio)", target
->cmd_name
);
1211 return ERROR_TARGET_NOT_HALTED
;
1213 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1216 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1218 if (target
->state
!= TARGET_HALTED
) {
1219 LOG_WARNING("target %s is not halted (gdb fileio end)", target
->cmd_name
);
1220 return ERROR_TARGET_NOT_HALTED
;
1222 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1225 int target_profiling(struct target
*target
, uint32_t *samples
,
1226 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1228 if (target
->state
!= TARGET_HALTED
) {
1229 LOG_WARNING("target %s is not halted (profiling)", target
->cmd_name
);
1230 return ERROR_TARGET_NOT_HALTED
;
1232 return target
->type
->profiling(target
, samples
, max_num_samples
,
1233 num_samples
, seconds
);
1237 * Reset the @c examined flag for the given target.
1238 * Pure paranoia -- targets are zeroed on allocation.
1240 static void target_reset_examined(struct target
*target
)
1242 target
->examined
= false;
1245 static int handle_target(void *priv
);
1247 static int target_init_one(struct command_context
*cmd_ctx
,
1248 struct target
*target
)
1250 target_reset_examined(target
);
1252 struct target_type
*type
= target
->type
;
1253 if (type
->examine
== NULL
)
1254 type
->examine
= default_examine
;
1256 if (type
->check_reset
== NULL
)
1257 type
->check_reset
= default_check_reset
;
1259 assert(type
->init_target
!= NULL
);
1261 int retval
= type
->init_target(cmd_ctx
, target
);
1262 if (ERROR_OK
!= retval
) {
1263 LOG_ERROR("target '%s' init failed", target_name(target
));
1267 /* Sanity-check MMU support ... stub in what we must, to help
1268 * implement it in stages, but warn if we need to do so.
1271 if (type
->virt2phys
== NULL
) {
1272 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1273 type
->virt2phys
= identity_virt2phys
;
1276 /* Make sure no-MMU targets all behave the same: make no
1277 * distinction between physical and virtual addresses, and
1278 * ensure that virt2phys() is always an identity mapping.
1280 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1281 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1284 type
->write_phys_memory
= type
->write_memory
;
1285 type
->read_phys_memory
= type
->read_memory
;
1286 type
->virt2phys
= identity_virt2phys
;
1289 if (target
->type
->read_buffer
== NULL
)
1290 target
->type
->read_buffer
= target_read_buffer_default
;
1292 if (target
->type
->write_buffer
== NULL
)
1293 target
->type
->write_buffer
= target_write_buffer_default
;
1295 if (target
->type
->get_gdb_fileio_info
== NULL
)
1296 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1298 if (target
->type
->gdb_fileio_end
== NULL
)
1299 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1301 if (target
->type
->profiling
== NULL
)
1302 target
->type
->profiling
= target_profiling_default
;
1307 static int target_init(struct command_context
*cmd_ctx
)
1309 struct target
*target
;
1312 for (target
= all_targets
; target
; target
= target
->next
) {
1313 retval
= target_init_one(cmd_ctx
, target
);
1314 if (ERROR_OK
!= retval
)
1321 retval
= target_register_user_commands(cmd_ctx
);
1322 if (ERROR_OK
!= retval
)
1325 retval
= target_register_timer_callback(&handle_target
,
1326 polling_interval
, 1, cmd_ctx
->interp
);
1327 if (ERROR_OK
!= retval
)
1333 COMMAND_HANDLER(handle_target_init_command
)
1338 return ERROR_COMMAND_SYNTAX_ERROR
;
1340 static bool target_initialized
;
1341 if (target_initialized
) {
1342 LOG_INFO("'target init' has already been called");
1345 target_initialized
= true;
1347 retval
= command_run_line(CMD_CTX
, "init_targets");
1348 if (ERROR_OK
!= retval
)
1351 retval
= command_run_line(CMD_CTX
, "init_target_events");
1352 if (ERROR_OK
!= retval
)
1355 retval
= command_run_line(CMD_CTX
, "init_board");
1356 if (ERROR_OK
!= retval
)
1359 LOG_DEBUG("Initializing targets...");
1360 return target_init(CMD_CTX
);
1363 int target_register_event_callback(int (*callback
)(struct target
*target
,
1364 enum target_event event
, void *priv
), void *priv
)
1366 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1368 if (callback
== NULL
)
1369 return ERROR_COMMAND_SYNTAX_ERROR
;
1372 while ((*callbacks_p
)->next
)
1373 callbacks_p
= &((*callbacks_p
)->next
);
1374 callbacks_p
= &((*callbacks_p
)->next
);
1377 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1378 (*callbacks_p
)->callback
= callback
;
1379 (*callbacks_p
)->priv
= priv
;
1380 (*callbacks_p
)->next
= NULL
;
1385 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1386 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1388 struct target_reset_callback
*entry
;
1390 if (callback
== NULL
)
1391 return ERROR_COMMAND_SYNTAX_ERROR
;
1393 entry
= malloc(sizeof(struct target_reset_callback
));
1394 if (entry
== NULL
) {
1395 LOG_ERROR("error allocating buffer for reset callback entry");
1396 return ERROR_COMMAND_SYNTAX_ERROR
;
1399 entry
->callback
= callback
;
1401 list_add(&entry
->list
, &target_reset_callback_list
);
1407 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1408 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1410 struct target_trace_callback
*entry
;
1412 if (callback
== NULL
)
1413 return ERROR_COMMAND_SYNTAX_ERROR
;
1415 entry
= malloc(sizeof(struct target_trace_callback
));
1416 if (entry
== NULL
) {
1417 LOG_ERROR("error allocating buffer for trace callback entry");
1418 return ERROR_COMMAND_SYNTAX_ERROR
;
1421 entry
->callback
= callback
;
1423 list_add(&entry
->list
, &target_trace_callback_list
);
1429 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1431 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1433 if (callback
== NULL
)
1434 return ERROR_COMMAND_SYNTAX_ERROR
;
1437 while ((*callbacks_p
)->next
)
1438 callbacks_p
= &((*callbacks_p
)->next
);
1439 callbacks_p
= &((*callbacks_p
)->next
);
1442 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1443 (*callbacks_p
)->callback
= callback
;
1444 (*callbacks_p
)->periodic
= periodic
;
1445 (*callbacks_p
)->time_ms
= time_ms
;
1446 (*callbacks_p
)->removed
= false;
1448 gettimeofday(&(*callbacks_p
)->when
, NULL
);
1449 timeval_add_time(&(*callbacks_p
)->when
, 0, time_ms
* 1000);
1451 (*callbacks_p
)->priv
= priv
;
1452 (*callbacks_p
)->next
= NULL
;
1457 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1458 enum target_event event
, void *priv
), void *priv
)
1460 struct target_event_callback
**p
= &target_event_callbacks
;
1461 struct target_event_callback
*c
= target_event_callbacks
;
1463 if (callback
== NULL
)
1464 return ERROR_COMMAND_SYNTAX_ERROR
;
1467 struct target_event_callback
*next
= c
->next
;
1468 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1480 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1481 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1483 struct target_reset_callback
*entry
;
1485 if (callback
== NULL
)
1486 return ERROR_COMMAND_SYNTAX_ERROR
;
1488 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1489 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1490 list_del(&entry
->list
);
1499 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1500 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1502 struct target_trace_callback
*entry
;
1504 if (callback
== NULL
)
1505 return ERROR_COMMAND_SYNTAX_ERROR
;
1507 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1508 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1509 list_del(&entry
->list
);
1518 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1520 if (callback
== NULL
)
1521 return ERROR_COMMAND_SYNTAX_ERROR
;
1523 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1525 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1534 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1536 struct target_event_callback
*callback
= target_event_callbacks
;
1537 struct target_event_callback
*next_callback
;
1539 if (event
== TARGET_EVENT_HALTED
) {
1540 /* execute early halted first */
1541 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1544 LOG_DEBUG("target event %i (%s)", event
,
1545 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1547 target_handle_event(target
, event
);
1550 next_callback
= callback
->next
;
1551 callback
->callback(target
, event
, callback
->priv
);
1552 callback
= next_callback
;
1558 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1560 struct target_reset_callback
*callback
;
1562 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1563 Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1565 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1566 callback
->callback(target
, reset_mode
, callback
->priv
);
1571 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1573 struct target_trace_callback
*callback
;
1575 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1576 callback
->callback(target
, len
, data
, callback
->priv
);
1581 static int target_timer_callback_periodic_restart(
1582 struct target_timer_callback
*cb
, struct timeval
*now
)
1585 timeval_add_time(&cb
->when
, 0, cb
->time_ms
* 1000L);
1589 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1590 struct timeval
*now
)
1592 cb
->callback(cb
->priv
);
1595 return target_timer_callback_periodic_restart(cb
, now
);
1597 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1600 static int target_call_timer_callbacks_check_time(int checktime
)
1602 static bool callback_processing
;
1604 /* Do not allow nesting */
1605 if (callback_processing
)
1608 callback_processing
= true;
1613 gettimeofday(&now
, NULL
);
1615 /* Store an address of the place containing a pointer to the
1616 * next item; initially, that's a standalone "root of the
1617 * list" variable. */
1618 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1620 if ((*callback
)->removed
) {
1621 struct target_timer_callback
*p
= *callback
;
1622 *callback
= (*callback
)->next
;
1627 bool call_it
= (*callback
)->callback
&&
1628 ((!checktime
&& (*callback
)->periodic
) ||
1629 timeval_compare(&now
, &(*callback
)->when
) >= 0);
1632 target_call_timer_callback(*callback
, &now
);
1634 callback
= &(*callback
)->next
;
1637 callback_processing
= false;
1641 int target_call_timer_callbacks(void)
1643 return target_call_timer_callbacks_check_time(1);
1646 /* invoke periodic callbacks immediately */
1647 int target_call_timer_callbacks_now(void)
1649 return target_call_timer_callbacks_check_time(0);
1652 /* Prints the working area layout for debug purposes */
1653 static void print_wa_layout(struct target
*target
)
1655 struct working_area
*c
= target
->working_areas
;
1658 LOG_DEBUG("%c%c " TARGET_ADDR_FMT
"-" TARGET_ADDR_FMT
" (%" PRIu32
" bytes)",
1659 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1660 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1665 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1666 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1668 assert(area
->free
); /* Shouldn't split an allocated area */
1669 assert(size
<= area
->size
); /* Caller should guarantee this */
1671 /* Split only if not already the right size */
1672 if (size
< area
->size
) {
1673 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1678 new_wa
->next
= area
->next
;
1679 new_wa
->size
= area
->size
- size
;
1680 new_wa
->address
= area
->address
+ size
;
1681 new_wa
->backup
= NULL
;
1682 new_wa
->user
= NULL
;
1683 new_wa
->free
= true;
1685 area
->next
= new_wa
;
1688 /* If backup memory was allocated to this area, it has the wrong size
1689 * now so free it and it will be reallocated if/when needed */
1692 area
->backup
= NULL
;
1697 /* Merge all adjacent free areas into one */
1698 static void target_merge_working_areas(struct target
*target
)
1700 struct working_area
*c
= target
->working_areas
;
1702 while (c
&& c
->next
) {
1703 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1705 /* Find two adjacent free areas */
1706 if (c
->free
&& c
->next
->free
) {
1707 /* Merge the last into the first */
1708 c
->size
+= c
->next
->size
;
1710 /* Remove the last */
1711 struct working_area
*to_be_freed
= c
->next
;
1712 c
->next
= c
->next
->next
;
1713 if (to_be_freed
->backup
)
1714 free(to_be_freed
->backup
);
1717 /* If backup memory was allocated to the remaining area, it's has
1718 * the wrong size now */
1729 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1731 /* Reevaluate working area address based on MMU state*/
1732 if (target
->working_areas
== NULL
) {
1736 retval
= target
->type
->mmu(target
, &enabled
);
1737 if (retval
!= ERROR_OK
)
1741 if (target
->working_area_phys_spec
) {
1742 LOG_DEBUG("MMU disabled, using physical "
1743 "address for working memory " TARGET_ADDR_FMT
,
1744 target
->working_area_phys
);
1745 target
->working_area
= target
->working_area_phys
;
1747 LOG_ERROR("No working memory available. "
1748 "Specify -work-area-phys to target.");
1749 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1752 if (target
->working_area_virt_spec
) {
1753 LOG_DEBUG("MMU enabled, using virtual "
1754 "address for working memory " TARGET_ADDR_FMT
,
1755 target
->working_area_virt
);
1756 target
->working_area
= target
->working_area_virt
;
1758 LOG_ERROR("No working memory available. "
1759 "Specify -work-area-virt to target.");
1760 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1764 /* Set up initial working area on first call */
1765 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1767 new_wa
->next
= NULL
;
1768 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1769 new_wa
->address
= target
->working_area
;
1770 new_wa
->backup
= NULL
;
1771 new_wa
->user
= NULL
;
1772 new_wa
->free
= true;
1775 target
->working_areas
= new_wa
;
1778 /* only allocate multiples of 4 byte */
1780 size
= (size
+ 3) & (~3UL);
1782 struct working_area
*c
= target
->working_areas
;
1784 /* Find the first large enough working area */
1786 if (c
->free
&& c
->size
>= size
)
1792 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1794 /* Split the working area into the requested size */
1795 target_split_working_area(c
, size
);
1797 LOG_DEBUG("allocated new working area of %" PRIu32
" bytes at address " TARGET_ADDR_FMT
,
1800 if (target
->backup_working_area
) {
1801 if (c
->backup
== NULL
) {
1802 c
->backup
= malloc(c
->size
);
1803 if (c
->backup
== NULL
)
1807 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1808 if (retval
!= ERROR_OK
)
1812 /* mark as used, and return the new (reused) area */
1819 print_wa_layout(target
);
1824 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1828 retval
= target_alloc_working_area_try(target
, size
, area
);
1829 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1830 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1835 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1837 int retval
= ERROR_OK
;
1839 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1840 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1841 if (retval
!= ERROR_OK
)
1842 LOG_ERROR("failed to restore %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1843 area
->size
, area
->address
);
1849 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1850 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1852 int retval
= ERROR_OK
;
1858 retval
= target_restore_working_area(target
, area
);
1859 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1860 if (retval
!= ERROR_OK
)
1866 LOG_DEBUG("freed %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1867 area
->size
, area
->address
);
1869 /* mark user pointer invalid */
1870 /* TODO: Is this really safe? It points to some previous caller's memory.
1871 * How could we know that the area pointer is still in that place and not
1872 * some other vital data? What's the purpose of this, anyway? */
1876 target_merge_working_areas(target
);
1878 print_wa_layout(target
);
1883 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1885 return target_free_working_area_restore(target
, area
, 1);
1888 static void target_destroy(struct target
*target
)
1890 if (target
->type
->deinit_target
)
1891 target
->type
->deinit_target(target
);
1894 free(target
->trace_info
);
1895 free(target
->cmd_name
);
1899 void target_quit(void)
1901 struct target_event_callback
*pe
= target_event_callbacks
;
1903 struct target_event_callback
*t
= pe
->next
;
1907 target_event_callbacks
= NULL
;
1909 struct target_timer_callback
*pt
= target_timer_callbacks
;
1911 struct target_timer_callback
*t
= pt
->next
;
1915 target_timer_callbacks
= NULL
;
1917 for (struct target
*target
= all_targets
; target
;) {
1921 target_destroy(target
);
1928 /* free resources and restore memory, if restoring memory fails,
1929 * free up resources anyway
1931 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1933 struct working_area
*c
= target
->working_areas
;
1935 LOG_DEBUG("freeing all working areas");
1937 /* Loop through all areas, restoring the allocated ones and marking them as free */
1941 target_restore_working_area(target
, c
);
1943 *c
->user
= NULL
; /* Same as above */
1949 /* Run a merge pass to combine all areas into one */
1950 target_merge_working_areas(target
);
1952 print_wa_layout(target
);
1955 void target_free_all_working_areas(struct target
*target
)
1957 target_free_all_working_areas_restore(target
, 1);
1960 /* Find the largest number of bytes that can be allocated */
1961 uint32_t target_get_working_area_avail(struct target
*target
)
1963 struct working_area
*c
= target
->working_areas
;
1964 uint32_t max_size
= 0;
1967 return target
->working_area_size
;
1970 if (c
->free
&& max_size
< c
->size
)
1979 int target_arch_state(struct target
*target
)
1982 if (target
== NULL
) {
1983 LOG_WARNING("No target has been configured");
1987 if (target
->state
!= TARGET_HALTED
)
1990 retval
= target
->type
->arch_state(target
);
1994 static int target_get_gdb_fileio_info_default(struct target
*target
,
1995 struct gdb_fileio_info
*fileio_info
)
1997 /* If target does not support semi-hosting function, target
1998 has no need to provide .get_gdb_fileio_info callback.
1999 It just return ERROR_FAIL and gdb_server will return "Txx"
2000 as target halted every time. */
2004 static int target_gdb_fileio_end_default(struct target
*target
,
2005 int retcode
, int fileio_errno
, bool ctrl_c
)
2010 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
2011 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
2013 struct timeval timeout
, now
;
2015 gettimeofday(&timeout
, NULL
);
2016 timeval_add_time(&timeout
, seconds
, 0);
2018 LOG_INFO("Starting profiling. Halting and resuming the"
2019 " target as often as we can...");
2021 uint32_t sample_count
= 0;
2022 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2023 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2025 int retval
= ERROR_OK
;
2027 target_poll(target
);
2028 if (target
->state
== TARGET_HALTED
) {
2029 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
2030 samples
[sample_count
++] = t
;
2031 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2032 retval
= target_resume(target
, 1, 0, 0, 0);
2033 target_poll(target
);
2034 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2035 } else if (target
->state
== TARGET_RUNNING
) {
2036 /* We want to quickly sample the PC. */
2037 retval
= target_halt(target
);
2039 LOG_INFO("Target not halted or running");
2044 if (retval
!= ERROR_OK
)
2047 gettimeofday(&now
, NULL
);
2048 if ((sample_count
>= max_num_samples
) || timeval_compare(&now
, &timeout
) >= 0) {
2049 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2054 *num_samples
= sample_count
;
2058 /* Single aligned words are guaranteed to use 16 or 32 bit access
2059 * mode respectively, otherwise data is handled as quickly as
2062 int target_write_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, const uint8_t *buffer
)
2064 LOG_DEBUG("writing buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2067 if (!target_was_examined(target
)) {
2068 LOG_ERROR("Target not examined yet");
2075 if ((address
+ size
- 1) < address
) {
2076 /* GDB can request this when e.g. PC is 0xfffffffc */
2077 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2083 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2086 static int target_write_buffer_default(struct target
*target
,
2087 target_addr_t address
, uint32_t count
, const uint8_t *buffer
)
2091 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2092 * will have something to do with the size we leave to it. */
2093 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2094 if (address
& size
) {
2095 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2096 if (retval
!= ERROR_OK
)
2104 /* Write the data with as large access size as possible. */
2105 for (; size
> 0; size
/= 2) {
2106 uint32_t aligned
= count
- count
% size
;
2108 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2109 if (retval
!= ERROR_OK
)
2120 /* Single aligned words are guaranteed to use 16 or 32 bit access
2121 * mode respectively, otherwise data is handled as quickly as
2124 int target_read_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
2126 LOG_DEBUG("reading buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2129 if (!target_was_examined(target
)) {
2130 LOG_ERROR("Target not examined yet");
2137 if ((address
+ size
- 1) < address
) {
2138 /* GDB can request this when e.g. PC is 0xfffffffc */
2139 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2145 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2148 static int target_read_buffer_default(struct target
*target
, target_addr_t address
, uint32_t count
, uint8_t *buffer
)
2152 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2153 * will have something to do with the size we leave to it. */
2154 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2155 if (address
& size
) {
2156 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2157 if (retval
!= ERROR_OK
)
2165 /* Read the data with as large access size as possible. */
2166 for (; size
> 0; size
/= 2) {
2167 uint32_t aligned
= count
- count
% size
;
2169 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2170 if (retval
!= ERROR_OK
)
2181 int target_checksum_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* crc
)
2186 uint32_t checksum
= 0;
2187 if (!target_was_examined(target
)) {
2188 LOG_ERROR("Target not examined yet");
2192 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2193 if (retval
!= ERROR_OK
) {
2194 buffer
= malloc(size
);
2195 if (buffer
== NULL
) {
2196 LOG_ERROR("error allocating buffer for section (%" PRId32
" bytes)", size
);
2197 return ERROR_COMMAND_SYNTAX_ERROR
;
2199 retval
= target_read_buffer(target
, address
, size
, buffer
);
2200 if (retval
!= ERROR_OK
) {
2205 /* convert to target endianness */
2206 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2207 uint32_t target_data
;
2208 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2209 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2212 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2221 int target_blank_check_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* blank
,
2222 uint8_t erased_value
)
2225 if (!target_was_examined(target
)) {
2226 LOG_ERROR("Target not examined yet");
2230 if (target
->type
->blank_check_memory
== 0)
2231 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2233 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
, erased_value
);
2238 int target_read_u64(struct target
*target
, target_addr_t address
, uint64_t *value
)
2240 uint8_t value_buf
[8];
2241 if (!target_was_examined(target
)) {
2242 LOG_ERROR("Target not examined yet");
2246 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2248 if (retval
== ERROR_OK
) {
2249 *value
= target_buffer_get_u64(target
, value_buf
);
2250 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2255 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2262 int target_read_u32(struct target
*target
, target_addr_t address
, uint32_t *value
)
2264 uint8_t value_buf
[4];
2265 if (!target_was_examined(target
)) {
2266 LOG_ERROR("Target not examined yet");
2270 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2272 if (retval
== ERROR_OK
) {
2273 *value
= target_buffer_get_u32(target
, value_buf
);
2274 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2279 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2286 int target_read_u16(struct target
*target
, target_addr_t address
, uint16_t *value
)
2288 uint8_t value_buf
[2];
2289 if (!target_was_examined(target
)) {
2290 LOG_ERROR("Target not examined yet");
2294 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2296 if (retval
== ERROR_OK
) {
2297 *value
= target_buffer_get_u16(target
, value_buf
);
2298 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%4.4" PRIx16
,
2303 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2310 int target_read_u8(struct target
*target
, target_addr_t address
, uint8_t *value
)
2312 if (!target_was_examined(target
)) {
2313 LOG_ERROR("Target not examined yet");
2317 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2319 if (retval
== ERROR_OK
) {
2320 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2325 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2332 int target_write_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2335 uint8_t value_buf
[8];
2336 if (!target_was_examined(target
)) {
2337 LOG_ERROR("Target not examined yet");
2341 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2345 target_buffer_set_u64(target
, value_buf
, value
);
2346 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2347 if (retval
!= ERROR_OK
)
2348 LOG_DEBUG("failed: %i", retval
);
2353 int target_write_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2356 uint8_t value_buf
[4];
2357 if (!target_was_examined(target
)) {
2358 LOG_ERROR("Target not examined yet");
2362 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2366 target_buffer_set_u32(target
, value_buf
, value
);
2367 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2368 if (retval
!= ERROR_OK
)
2369 LOG_DEBUG("failed: %i", retval
);
2374 int target_write_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2377 uint8_t value_buf
[2];
2378 if (!target_was_examined(target
)) {
2379 LOG_ERROR("Target not examined yet");
2383 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2387 target_buffer_set_u16(target
, value_buf
, value
);
2388 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2389 if (retval
!= ERROR_OK
)
2390 LOG_DEBUG("failed: %i", retval
);
2395 int target_write_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2398 if (!target_was_examined(target
)) {
2399 LOG_ERROR("Target not examined yet");
2403 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2406 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2407 if (retval
!= ERROR_OK
)
2408 LOG_DEBUG("failed: %i", retval
);
2413 int target_write_phys_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2416 uint8_t value_buf
[8];
2417 if (!target_was_examined(target
)) {
2418 LOG_ERROR("Target not examined yet");
2422 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2426 target_buffer_set_u64(target
, value_buf
, value
);
2427 retval
= target_write_phys_memory(target
, address
, 8, 1, value_buf
);
2428 if (retval
!= ERROR_OK
)
2429 LOG_DEBUG("failed: %i", retval
);
2434 int target_write_phys_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2437 uint8_t value_buf
[4];
2438 if (!target_was_examined(target
)) {
2439 LOG_ERROR("Target not examined yet");
2443 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2447 target_buffer_set_u32(target
, value_buf
, value
);
2448 retval
= target_write_phys_memory(target
, address
, 4, 1, value_buf
);
2449 if (retval
!= ERROR_OK
)
2450 LOG_DEBUG("failed: %i", retval
);
2455 int target_write_phys_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2458 uint8_t value_buf
[2];
2459 if (!target_was_examined(target
)) {
2460 LOG_ERROR("Target not examined yet");
2464 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2468 target_buffer_set_u16(target
, value_buf
, value
);
2469 retval
= target_write_phys_memory(target
, address
, 2, 1, value_buf
);
2470 if (retval
!= ERROR_OK
)
2471 LOG_DEBUG("failed: %i", retval
);
2476 int target_write_phys_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2479 if (!target_was_examined(target
)) {
2480 LOG_ERROR("Target not examined yet");
2484 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2487 retval
= target_write_phys_memory(target
, address
, 1, 1, &value
);
2488 if (retval
!= ERROR_OK
)
2489 LOG_DEBUG("failed: %i", retval
);
2494 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2496 struct target
*target
= get_target(name
);
2497 if (target
== NULL
) {
2498 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2501 if (!target
->tap
->enabled
) {
2502 LOG_USER("Target: TAP %s is disabled, "
2503 "can't be the current target\n",
2504 target
->tap
->dotted_name
);
2508 cmd_ctx
->current_target
= target
->target_number
;
2513 COMMAND_HANDLER(handle_targets_command
)
2515 int retval
= ERROR_OK
;
2516 if (CMD_ARGC
== 1) {
2517 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2518 if (retval
== ERROR_OK
) {
2524 struct target
*target
= all_targets
;
2525 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2526 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2531 if (target
->tap
->enabled
)
2532 state
= target_state_name(target
);
2534 state
= "tap-disabled";
2536 if (CMD_CTX
->current_target
== target
->target_number
)
2539 /* keep columns lined up to match the headers above */
2540 command_print(CMD_CTX
,
2541 "%2d%c %-18s %-10s %-6s %-18s %s",
2542 target
->target_number
,
2544 target_name(target
),
2545 target_type_name(target
),
2546 Jim_Nvp_value2name_simple(nvp_target_endian
,
2547 target
->endianness
)->name
,
2548 target
->tap
->dotted_name
,
2550 target
= target
->next
;
2556 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2558 static int powerDropout
;
2559 static int srstAsserted
;
2561 static int runPowerRestore
;
2562 static int runPowerDropout
;
2563 static int runSrstAsserted
;
2564 static int runSrstDeasserted
;
2566 static int sense_handler(void)
2568 static int prevSrstAsserted
;
2569 static int prevPowerdropout
;
2571 int retval
= jtag_power_dropout(&powerDropout
);
2572 if (retval
!= ERROR_OK
)
2576 powerRestored
= prevPowerdropout
&& !powerDropout
;
2578 runPowerRestore
= 1;
2580 int64_t current
= timeval_ms();
2581 static int64_t lastPower
;
2582 bool waitMore
= lastPower
+ 2000 > current
;
2583 if (powerDropout
&& !waitMore
) {
2584 runPowerDropout
= 1;
2585 lastPower
= current
;
2588 retval
= jtag_srst_asserted(&srstAsserted
);
2589 if (retval
!= ERROR_OK
)
2593 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2595 static int64_t lastSrst
;
2596 waitMore
= lastSrst
+ 2000 > current
;
2597 if (srstDeasserted
&& !waitMore
) {
2598 runSrstDeasserted
= 1;
2602 if (!prevSrstAsserted
&& srstAsserted
)
2603 runSrstAsserted
= 1;
2605 prevSrstAsserted
= srstAsserted
;
2606 prevPowerdropout
= powerDropout
;
2608 if (srstDeasserted
|| powerRestored
) {
2609 /* Other than logging the event we can't do anything here.
2610 * Issuing a reset is a particularly bad idea as we might
2611 * be inside a reset already.
2618 /* process target state changes */
2619 static int handle_target(void *priv
)
2621 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2622 int retval
= ERROR_OK
;
2624 if (!is_jtag_poll_safe()) {
2625 /* polling is disabled currently */
2629 /* we do not want to recurse here... */
2630 static int recursive
;
2634 /* danger! running these procedures can trigger srst assertions and power dropouts.
2635 * We need to avoid an infinite loop/recursion here and we do that by
2636 * clearing the flags after running these events.
2638 int did_something
= 0;
2639 if (runSrstAsserted
) {
2640 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2641 Jim_Eval(interp
, "srst_asserted");
2644 if (runSrstDeasserted
) {
2645 Jim_Eval(interp
, "srst_deasserted");
2648 if (runPowerDropout
) {
2649 LOG_INFO("Power dropout detected, running power_dropout proc.");
2650 Jim_Eval(interp
, "power_dropout");
2653 if (runPowerRestore
) {
2654 Jim_Eval(interp
, "power_restore");
2658 if (did_something
) {
2659 /* clear detect flags */
2663 /* clear action flags */
2665 runSrstAsserted
= 0;
2666 runSrstDeasserted
= 0;
2667 runPowerRestore
= 0;
2668 runPowerDropout
= 0;
2673 /* Poll targets for state changes unless that's globally disabled.
2674 * Skip targets that are currently disabled.
2676 for (struct target
*target
= all_targets
;
2677 is_jtag_poll_safe() && target
;
2678 target
= target
->next
) {
2680 if (!target_was_examined(target
))
2683 if (!target
->tap
->enabled
)
2686 if (target
->backoff
.times
> target
->backoff
.count
) {
2687 /* do not poll this time as we failed previously */
2688 target
->backoff
.count
++;
2691 target
->backoff
.count
= 0;
2693 /* only poll target if we've got power and srst isn't asserted */
2694 if (!powerDropout
&& !srstAsserted
) {
2695 /* polling may fail silently until the target has been examined */
2696 retval
= target_poll(target
);
2697 if (retval
!= ERROR_OK
) {
2698 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2699 if (target
->backoff
.times
* polling_interval
< 5000) {
2700 target
->backoff
.times
*= 2;
2701 target
->backoff
.times
++;
2704 /* Tell GDB to halt the debugger. This allows the user to
2705 * run monitor commands to handle the situation.
2707 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2709 if (target
->backoff
.times
> 0) {
2710 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
2711 target_reset_examined(target
);
2712 retval
= target_examine_one(target
);
2713 /* Target examination could have failed due to unstable connection,
2714 * but we set the examined flag anyway to repoll it later */
2715 if (retval
!= ERROR_OK
) {
2716 target
->examined
= true;
2717 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2718 target
->backoff
.times
* polling_interval
);
2723 /* Since we succeeded, we reset backoff count */
2724 target
->backoff
.times
= 0;
2731 COMMAND_HANDLER(handle_reg_command
)
2733 struct target
*target
;
2734 struct reg
*reg
= NULL
;
2740 target
= get_current_target(CMD_CTX
);
2742 /* list all available registers for the current target */
2743 if (CMD_ARGC
== 0) {
2744 struct reg_cache
*cache
= target
->reg_cache
;
2750 command_print(CMD_CTX
, "===== %s", cache
->name
);
2752 for (i
= 0, reg
= cache
->reg_list
;
2753 i
< cache
->num_regs
;
2754 i
++, reg
++, count
++) {
2755 /* only print cached values if they are valid */
2757 value
= buf_to_str(reg
->value
,
2759 command_print(CMD_CTX
,
2760 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2768 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2773 cache
= cache
->next
;
2779 /* access a single register by its ordinal number */
2780 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2782 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2784 struct reg_cache
*cache
= target
->reg_cache
;
2788 for (i
= 0; i
< cache
->num_regs
; i
++) {
2789 if (count
++ == num
) {
2790 reg
= &cache
->reg_list
[i
];
2796 cache
= cache
->next
;
2800 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2801 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2805 /* access a single register by its name */
2806 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2809 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2814 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2816 /* display a register */
2817 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2818 && (CMD_ARGV
[1][0] <= '9')))) {
2819 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2822 if (reg
->valid
== 0)
2823 reg
->type
->get(reg
);
2824 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2825 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2830 /* set register value */
2831 if (CMD_ARGC
== 2) {
2832 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2835 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2837 reg
->type
->set(reg
, buf
);
2839 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2840 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2848 return ERROR_COMMAND_SYNTAX_ERROR
;
2851 COMMAND_HANDLER(handle_poll_command
)
2853 int retval
= ERROR_OK
;
2854 struct target
*target
= get_current_target(CMD_CTX
);
2856 if (CMD_ARGC
== 0) {
2857 command_print(CMD_CTX
, "background polling: %s",
2858 jtag_poll_get_enabled() ? "on" : "off");
2859 command_print(CMD_CTX
, "TAP: %s (%s)",
2860 target
->tap
->dotted_name
,
2861 target
->tap
->enabled
? "enabled" : "disabled");
2862 if (!target
->tap
->enabled
)
2864 retval
= target_poll(target
);
2865 if (retval
!= ERROR_OK
)
2867 retval
= target_arch_state(target
);
2868 if (retval
!= ERROR_OK
)
2870 } else if (CMD_ARGC
== 1) {
2872 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2873 jtag_poll_set_enabled(enable
);
2875 return ERROR_COMMAND_SYNTAX_ERROR
;
2880 COMMAND_HANDLER(handle_wait_halt_command
)
2883 return ERROR_COMMAND_SYNTAX_ERROR
;
2885 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2886 if (1 == CMD_ARGC
) {
2887 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2888 if (ERROR_OK
!= retval
)
2889 return ERROR_COMMAND_SYNTAX_ERROR
;
2892 struct target
*target
= get_current_target(CMD_CTX
);
2893 return target_wait_state(target
, TARGET_HALTED
, ms
);
2896 /* wait for target state to change. The trick here is to have a low
2897 * latency for short waits and not to suck up all the CPU time
2900 * After 500ms, keep_alive() is invoked
2902 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2905 int64_t then
= 0, cur
;
2909 retval
= target_poll(target
);
2910 if (retval
!= ERROR_OK
)
2912 if (target
->state
== state
)
2917 then
= timeval_ms();
2918 LOG_DEBUG("waiting for target %s...",
2919 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2925 if ((cur
-then
) > ms
) {
2926 LOG_ERROR("timed out while waiting for target %s",
2927 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2935 COMMAND_HANDLER(handle_halt_command
)
2939 struct target
*target
= get_current_target(CMD_CTX
);
2940 int retval
= target_halt(target
);
2941 if (ERROR_OK
!= retval
)
2944 if (CMD_ARGC
== 1) {
2945 unsigned wait_local
;
2946 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2947 if (ERROR_OK
!= retval
)
2948 return ERROR_COMMAND_SYNTAX_ERROR
;
2953 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2956 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2958 struct target
*target
= get_current_target(CMD_CTX
);
2960 LOG_USER("requesting target halt and executing a soft reset");
2962 target_soft_reset_halt(target
);
2967 COMMAND_HANDLER(handle_reset_command
)
2970 return ERROR_COMMAND_SYNTAX_ERROR
;
2972 enum target_reset_mode reset_mode
= RESET_RUN
;
2973 if (CMD_ARGC
== 1) {
2975 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2976 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
2977 return ERROR_COMMAND_SYNTAX_ERROR
;
2978 reset_mode
= n
->value
;
2981 /* reset *all* targets */
2982 return target_process_reset(CMD_CTX
, reset_mode
);
2986 COMMAND_HANDLER(handle_resume_command
)
2990 return ERROR_COMMAND_SYNTAX_ERROR
;
2992 struct target
*target
= get_current_target(CMD_CTX
);
2994 /* with no CMD_ARGV, resume from current pc, addr = 0,
2995 * with one arguments, addr = CMD_ARGV[0],
2996 * handle breakpoints, not debugging */
2997 target_addr_t addr
= 0;
2998 if (CMD_ARGC
== 1) {
2999 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3003 return target_resume(target
, current
, addr
, 1, 0);
3006 COMMAND_HANDLER(handle_step_command
)
3009 return ERROR_COMMAND_SYNTAX_ERROR
;
3013 /* with no CMD_ARGV, step from current pc, addr = 0,
3014 * with one argument addr = CMD_ARGV[0],
3015 * handle breakpoints, debugging */
3016 target_addr_t addr
= 0;
3018 if (CMD_ARGC
== 1) {
3019 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3023 struct target
*target
= get_current_target(CMD_CTX
);
3025 return target
->type
->step(target
, current_pc
, addr
, 1);
3028 static void handle_md_output(struct command_context
*cmd_ctx
,
3029 struct target
*target
, target_addr_t address
, unsigned size
,
3030 unsigned count
, const uint8_t *buffer
)
3032 const unsigned line_bytecnt
= 32;
3033 unsigned line_modulo
= line_bytecnt
/ size
;
3035 char output
[line_bytecnt
* 4 + 1];
3036 unsigned output_len
= 0;
3038 const char *value_fmt
;
3041 value_fmt
= "%16.16"PRIx64
" ";
3044 value_fmt
= "%8.8"PRIx64
" ";
3047 value_fmt
= "%4.4"PRIx64
" ";
3050 value_fmt
= "%2.2"PRIx64
" ";
3053 /* "can't happen", caller checked */
3054 LOG_ERROR("invalid memory read size: %u", size
);
3058 for (unsigned i
= 0; i
< count
; i
++) {
3059 if (i
% line_modulo
== 0) {
3060 output_len
+= snprintf(output
+ output_len
,
3061 sizeof(output
) - output_len
,
3062 TARGET_ADDR_FMT
": ",
3063 (address
+ (i
* size
)));
3067 const uint8_t *value_ptr
= buffer
+ i
* size
;
3070 value
= target_buffer_get_u64(target
, value_ptr
);
3073 value
= target_buffer_get_u32(target
, value_ptr
);
3076 value
= target_buffer_get_u16(target
, value_ptr
);
3081 output_len
+= snprintf(output
+ output_len
,
3082 sizeof(output
) - output_len
,
3085 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
3086 command_print(cmd_ctx
, "%s", output
);
3092 COMMAND_HANDLER(handle_md_command
)
3095 return ERROR_COMMAND_SYNTAX_ERROR
;
3098 switch (CMD_NAME
[2]) {
3112 return ERROR_COMMAND_SYNTAX_ERROR
;
3115 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3116 int (*fn
)(struct target
*target
,
3117 target_addr_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
3121 fn
= target_read_phys_memory
;
3123 fn
= target_read_memory
;
3124 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
3125 return ERROR_COMMAND_SYNTAX_ERROR
;
3127 target_addr_t address
;
3128 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3132 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
3134 uint8_t *buffer
= calloc(count
, size
);
3135 if (buffer
== NULL
) {
3136 LOG_ERROR("Failed to allocate md read buffer");
3140 struct target
*target
= get_current_target(CMD_CTX
);
3141 int retval
= fn(target
, address
, size
, count
, buffer
);
3142 if (ERROR_OK
== retval
)
3143 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
3150 typedef int (*target_write_fn
)(struct target
*target
,
3151 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
3153 static int target_fill_mem(struct target
*target
,
3154 target_addr_t address
,
3162 /* We have to write in reasonably large chunks to be able
3163 * to fill large memory areas with any sane speed */
3164 const unsigned chunk_size
= 16384;
3165 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
3166 if (target_buf
== NULL
) {
3167 LOG_ERROR("Out of memory");
3171 for (unsigned i
= 0; i
< chunk_size
; i
++) {
3172 switch (data_size
) {
3174 target_buffer_set_u64(target
, target_buf
+ i
* data_size
, b
);
3177 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
3180 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
3183 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
3190 int retval
= ERROR_OK
;
3192 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {