1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
17 * Copyright (C) 2011 by Broadcom Corporation *
18 * Evan Hunter - ehunter@broadcom.com *
20 * Copyright (C) ST-Ericsson SA 2011 *
21 * michel.jaouen@stericsson.com : smp minimum support *
23 * Copyright (C) 2011 Andreas Fritiofson *
24 * andreas.fritiofson@gmail.com *
26 * This program is free software; you can redistribute it and/or modify *
27 * it under the terms of the GNU General Public License as published by *
28 * the Free Software Foundation; either version 2 of the License, or *
29 * (at your option) any later version. *
31 * This program is distributed in the hope that it will be useful, *
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
34 * GNU General Public License for more details. *
36 * You should have received a copy of the GNU General Public License *
37 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
38 ***************************************************************************/
44 #include <helper/time_support.h>
45 #include <jtag/jtag.h>
46 #include <flash/nor/core.h>
49 #include "target_type.h"
50 #include "target_request.h"
51 #include "breakpoints.h"
55 #include "rtos/rtos.h"
56 #include "transport/transport.h"
59 /* default halt wait timeout (ms) */
60 #define DEFAULT_HALT_TIMEOUT 5000
62 static int target_read_buffer_default(struct target
*target
, target_addr_t address
,
63 uint32_t count
, uint8_t *buffer
);
64 static int target_write_buffer_default(struct target
*target
, target_addr_t address
,
65 uint32_t count
, const uint8_t *buffer
);
66 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
67 int argc
, Jim_Obj
* const *argv
);
68 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
69 int argc
, Jim_Obj
* const *argv
);
70 static int target_register_user_commands(struct command_context
*cmd_ctx
);
71 static int target_get_gdb_fileio_info_default(struct target
*target
,
72 struct gdb_fileio_info
*fileio_info
);
73 static int target_gdb_fileio_end_default(struct target
*target
, int retcode
,
74 int fileio_errno
, bool ctrl_c
);
75 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
76 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
);
79 extern struct target_type arm7tdmi_target
;
80 extern struct target_type arm720t_target
;
81 extern struct target_type arm9tdmi_target
;
82 extern struct target_type arm920t_target
;
83 extern struct target_type arm966e_target
;
84 extern struct target_type arm946e_target
;
85 extern struct target_type arm926ejs_target
;
86 extern struct target_type fa526_target
;
87 extern struct target_type feroceon_target
;
88 extern struct target_type dragonite_target
;
89 extern struct target_type xscale_target
;
90 extern struct target_type cortexm_target
;
91 extern struct target_type cortexa_target
;
92 extern struct target_type aarch64_target
;
93 extern struct target_type cortexr4_target
;
94 extern struct target_type arm11_target
;
95 extern struct target_type ls1_sap_target
;
96 extern struct target_type mips_m4k_target
;
97 extern struct target_type avr_target
;
98 extern struct target_type dsp563xx_target
;
99 extern struct target_type dsp5680xx_target
;
100 extern struct target_type testee_target
;
101 extern struct target_type avr32_ap7k_target
;
102 extern struct target_type hla_target
;
103 extern struct target_type nds32_v2_target
;
104 extern struct target_type nds32_v3_target
;
105 extern struct target_type nds32_v3m_target
;
106 extern struct target_type or1k_target
;
107 extern struct target_type quark_x10xx_target
;
108 extern struct target_type quark_d20xx_target
;
109 extern struct target_type stm8_target
;
110 extern struct target_type riscv_target
;
111 extern struct target_type mem_ap_target
;
112 extern struct target_type esirisc_target
;
114 static struct target_type
*target_types
[] = {
154 struct target
*all_targets
;
155 static struct target_event_callback
*target_event_callbacks
;
156 static struct target_timer_callback
*target_timer_callbacks
;
157 LIST_HEAD(target_reset_callback_list
);
158 LIST_HEAD(target_trace_callback_list
);
159 static const int polling_interval
= 100;
161 static const Jim_Nvp nvp_assert
[] = {
162 { .name
= "assert", NVP_ASSERT
},
163 { .name
= "deassert", NVP_DEASSERT
},
164 { .name
= "T", NVP_ASSERT
},
165 { .name
= "F", NVP_DEASSERT
},
166 { .name
= "t", NVP_ASSERT
},
167 { .name
= "f", NVP_DEASSERT
},
168 { .name
= NULL
, .value
= -1 }
171 static const Jim_Nvp nvp_error_target
[] = {
172 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
173 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
174 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
175 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
176 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
177 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
178 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
179 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
180 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
181 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
182 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
183 { .value
= -1, .name
= NULL
}
186 static const char *target_strerror_safe(int err
)
190 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
197 static const Jim_Nvp nvp_target_event
[] = {
199 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
200 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
201 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
202 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
203 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
205 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
206 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
208 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
209 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
210 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
211 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
212 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
213 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
214 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
215 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
217 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
218 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
220 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
221 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
223 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
224 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
226 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
227 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
229 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
230 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
232 { .value
= TARGET_EVENT_TRACE_CONFIG
, .name
= "trace-config" },
234 { .name
= NULL
, .value
= -1 }
237 static const Jim_Nvp nvp_target_state
[] = {
238 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
239 { .name
= "running", .value
= TARGET_RUNNING
},
240 { .name
= "halted", .value
= TARGET_HALTED
},
241 { .name
= "reset", .value
= TARGET_RESET
},
242 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
243 { .name
= NULL
, .value
= -1 },
246 static const Jim_Nvp nvp_target_debug_reason
[] = {
247 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
248 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
249 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
250 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
251 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
252 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
253 { .name
= "program-exit" , .value
= DBG_REASON_EXIT
},
254 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
255 { .name
= NULL
, .value
= -1 },
258 static const Jim_Nvp nvp_target_endian
[] = {
259 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
260 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
261 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
262 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
263 { .name
= NULL
, .value
= -1 },
266 static const Jim_Nvp nvp_reset_modes
[] = {
267 { .name
= "unknown", .value
= RESET_UNKNOWN
},
268 { .name
= "run" , .value
= RESET_RUN
},
269 { .name
= "halt" , .value
= RESET_HALT
},
270 { .name
= "init" , .value
= RESET_INIT
},
271 { .name
= NULL
, .value
= -1 },
274 const char *debug_reason_name(struct target
*t
)
278 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
279 t
->debug_reason
)->name
;
281 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
282 cp
= "(*BUG*unknown*BUG*)";
287 const char *target_state_name(struct target
*t
)
290 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
292 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
293 cp
= "(*BUG*unknown*BUG*)";
296 if (!target_was_examined(t
) && t
->defer_examine
)
297 cp
= "examine deferred";
302 const char *target_event_name(enum target_event event
)
305 cp
= Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
;
307 LOG_ERROR("Invalid target event: %d", (int)(event
));
308 cp
= "(*BUG*unknown*BUG*)";
313 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
316 cp
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
;
318 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
319 cp
= "(*BUG*unknown*BUG*)";
324 /* determine the number of the new target */
325 static int new_target_number(void)
330 /* number is 0 based */
334 if (x
< t
->target_number
)
335 x
= t
->target_number
;
341 /* read a uint64_t from a buffer in target memory endianness */
342 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
344 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
345 return le_to_h_u64(buffer
);
347 return be_to_h_u64(buffer
);
350 /* read a uint32_t from a buffer in target memory endianness */
351 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
353 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
354 return le_to_h_u32(buffer
);
356 return be_to_h_u32(buffer
);
359 /* read a uint24_t from a buffer in target memory endianness */
360 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
362 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
363 return le_to_h_u24(buffer
);
365 return be_to_h_u24(buffer
);
368 /* read a uint16_t from a buffer in target memory endianness */
369 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
371 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
372 return le_to_h_u16(buffer
);
374 return be_to_h_u16(buffer
);
377 /* read a uint8_t from a buffer in target memory endianness */
378 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
380 return *buffer
& 0x0ff;
383 /* write a uint64_t to a buffer in target memory endianness */
384 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
386 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
387 h_u64_to_le(buffer
, value
);
389 h_u64_to_be(buffer
, value
);
392 /* write a uint32_t to a buffer in target memory endianness */
393 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
395 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
396 h_u32_to_le(buffer
, value
);
398 h_u32_to_be(buffer
, value
);
401 /* write a uint24_t to a buffer in target memory endianness */
402 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
404 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
405 h_u24_to_le(buffer
, value
);
407 h_u24_to_be(buffer
, value
);
410 /* write a uint16_t to a buffer in target memory endianness */
411 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
413 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
414 h_u16_to_le(buffer
, value
);
416 h_u16_to_be(buffer
, value
);
419 /* write a uint8_t to a buffer in target memory endianness */
420 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
425 /* write a uint64_t array to a buffer in target memory endianness */
426 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
429 for (i
= 0; i
< count
; i
++)
430 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
433 /* write a uint32_t array to a buffer in target memory endianness */
434 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
437 for (i
= 0; i
< count
; i
++)
438 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
441 /* write a uint16_t array to a buffer in target memory endianness */
442 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
445 for (i
= 0; i
< count
; i
++)
446 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
449 /* write a uint64_t array to a buffer in target memory endianness */
450 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
453 for (i
= 0; i
< count
; i
++)
454 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
457 /* write a uint32_t array to a buffer in target memory endianness */
458 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
461 for (i
= 0; i
< count
; i
++)
462 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
465 /* write a uint16_t array to a buffer in target memory endianness */
466 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
469 for (i
= 0; i
< count
; i
++)
470 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
473 /* return a pointer to a configured target; id is name or number */
474 struct target
*get_target(const char *id
)
476 struct target
*target
;
478 /* try as tcltarget name */
479 for (target
= all_targets
; target
; target
= target
->next
) {
480 if (target_name(target
) == NULL
)
482 if (strcmp(id
, target_name(target
)) == 0)
486 /* It's OK to remove this fallback sometime after August 2010 or so */
488 /* no match, try as number */
490 if (parse_uint(id
, &num
) != ERROR_OK
)
493 for (target
= all_targets
; target
; target
= target
->next
) {
494 if (target
->target_number
== (int)num
) {
495 LOG_WARNING("use '%s' as target identifier, not '%u'",
496 target_name(target
), num
);
504 /* returns a pointer to the n-th configured target */
505 struct target
*get_target_by_num(int num
)
507 struct target
*target
= all_targets
;
510 if (target
->target_number
== num
)
512 target
= target
->next
;
518 struct target
*get_current_target(struct command_context
*cmd_ctx
)
520 struct target
*target
= cmd_ctx
->current_target_override
521 ? cmd_ctx
->current_target_override
522 : cmd_ctx
->current_target
;
524 if (target
== NULL
) {
525 LOG_ERROR("BUG: current_target out of bounds");
532 int target_poll(struct target
*target
)
536 /* We can't poll until after examine */
537 if (!target_was_examined(target
)) {
538 /* Fail silently lest we pollute the log */
542 retval
= target
->type
->poll(target
);
543 if (retval
!= ERROR_OK
)
546 if (target
->halt_issued
) {
547 if (target
->state
== TARGET_HALTED
)
548 target
->halt_issued
= false;
550 int64_t t
= timeval_ms() - target
->halt_issued_time
;
551 if (t
> DEFAULT_HALT_TIMEOUT
) {
552 target
->halt_issued
= false;
553 LOG_INFO("Halt timed out, wake up GDB.");
554 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
562 int target_halt(struct target
*target
)
565 /* We can't poll until after examine */
566 if (!target_was_examined(target
)) {
567 LOG_ERROR("Target not examined yet");
571 retval
= target
->type
->halt(target
);
572 if (retval
!= ERROR_OK
)
575 target
->halt_issued
= true;
576 target
->halt_issued_time
= timeval_ms();
582 * Make the target (re)start executing using its saved execution
583 * context (possibly with some modifications).
585 * @param target Which target should start executing.
586 * @param current True to use the target's saved program counter instead
587 * of the address parameter
588 * @param address Optionally used as the program counter.
589 * @param handle_breakpoints True iff breakpoints at the resumption PC
590 * should be skipped. (For example, maybe execution was stopped by
591 * such a breakpoint, in which case it would be counterprodutive to
593 * @param debug_execution False if all working areas allocated by OpenOCD
594 * should be released and/or restored to their original contents.
595 * (This would for example be true to run some downloaded "helper"
596 * algorithm code, which resides in one such working buffer and uses
597 * another for data storage.)
599 * @todo Resolve the ambiguity about what the "debug_execution" flag
600 * signifies. For example, Target implementations don't agree on how
601 * it relates to invalidation of the register cache, or to whether
602 * breakpoints and watchpoints should be enabled. (It would seem wrong
603 * to enable breakpoints when running downloaded "helper" algorithms
604 * (debug_execution true), since the breakpoints would be set to match
605 * target firmware being debugged, not the helper algorithm.... and
606 * enabling them could cause such helpers to malfunction (for example,
607 * by overwriting data with a breakpoint instruction. On the other
608 * hand the infrastructure for running such helpers might use this
609 * procedure but rely on hardware breakpoint to detect termination.)
611 int target_resume(struct target
*target
, int current
, target_addr_t address
,
612 int handle_breakpoints
, int debug_execution
)
616 /* We can't poll until after examine */
617 if (!target_was_examined(target
)) {
618 LOG_ERROR("Target not examined yet");
622 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
624 /* note that resume *must* be asynchronous. The CPU can halt before
625 * we poll. The CPU can even halt at the current PC as a result of
626 * a software breakpoint being inserted by (a bug?) the application.
628 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
629 if (retval
!= ERROR_OK
)
632 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
637 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
642 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
643 if (n
->name
== NULL
) {
644 LOG_ERROR("invalid reset mode");
648 struct target
*target
;
649 for (target
= all_targets
; target
; target
= target
->next
)
650 target_call_reset_callbacks(target
, reset_mode
);
652 /* disable polling during reset to make reset event scripts
653 * more predictable, i.e. dr/irscan & pathmove in events will
654 * not have JTAG operations injected into the middle of a sequence.
656 bool save_poll
= jtag_poll_get_enabled();
658 jtag_poll_set_enabled(false);
660 sprintf(buf
, "ocd_process_reset %s", n
->name
);
661 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
663 jtag_poll_set_enabled(save_poll
);
665 if (retval
!= JIM_OK
) {
666 Jim_MakeErrorMessage(cmd_ctx
->interp
);
667 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
671 /* We want any events to be processed before the prompt */
672 retval
= target_call_timer_callbacks_now();
674 for (target
= all_targets
; target
; target
= target
->next
) {
675 target
->type
->check_reset(target
);
676 target
->running_alg
= false;
682 static int identity_virt2phys(struct target
*target
,
683 target_addr_t
virtual, target_addr_t
*physical
)
689 static int no_mmu(struct target
*target
, int *enabled
)
695 static int default_examine(struct target
*target
)
697 target_set_examined(target
);
701 /* no check by default */
702 static int default_check_reset(struct target
*target
)
707 int target_examine_one(struct target
*target
)
709 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
711 int retval
= target
->type
->examine(target
);
712 if (retval
!= ERROR_OK
)
715 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
720 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
722 struct target
*target
= priv
;
724 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
727 jtag_unregister_event_callback(jtag_enable_callback
, target
);
729 return target_examine_one(target
);
732 /* Targets that correctly implement init + examine, i.e.
733 * no communication with target during init:
737 int target_examine(void)
739 int retval
= ERROR_OK
;
740 struct target
*target
;
742 for (target
= all_targets
; target
; target
= target
->next
) {
743 /* defer examination, but don't skip it */
744 if (!target
->tap
->enabled
) {
745 jtag_register_event_callback(jtag_enable_callback
,
750 if (target
->defer_examine
)
753 retval
= target_examine_one(target
);
754 if (retval
!= ERROR_OK
)
760 const char *target_type_name(struct target
*target
)
762 return target
->type
->name
;
765 static int target_soft_reset_halt(struct target
*target
)
767 if (!target_was_examined(target
)) {
768 LOG_ERROR("Target not examined yet");
771 if (!target
->type
->soft_reset_halt
) {
772 LOG_ERROR("Target %s does not support soft_reset_halt",
773 target_name(target
));
776 return target
->type
->soft_reset_halt(target
);
780 * Downloads a target-specific native code algorithm to the target,
781 * and executes it. * Note that some targets may need to set up, enable,
782 * and tear down a breakpoint (hard or * soft) to detect algorithm
783 * termination, while others may support lower overhead schemes where
784 * soft breakpoints embedded in the algorithm automatically terminate the
787 * @param target used to run the algorithm
788 * @param arch_info target-specific description of the algorithm.
790 int target_run_algorithm(struct target
*target
,
791 int num_mem_params
, struct mem_param
*mem_params
,
792 int num_reg_params
, struct reg_param
*reg_param
,
793 uint32_t entry_point
, uint32_t exit_point
,
794 int timeout_ms
, void *arch_info
)
796 int retval
= ERROR_FAIL
;
798 if (!target_was_examined(target
)) {
799 LOG_ERROR("Target not examined yet");
802 if (!target
->type
->run_algorithm
) {
803 LOG_ERROR("Target type '%s' does not support %s",
804 target_type_name(target
), __func__
);
808 target
->running_alg
= true;
809 retval
= target
->type
->run_algorithm(target
,
810 num_mem_params
, mem_params
,
811 num_reg_params
, reg_param
,
812 entry_point
, exit_point
, timeout_ms
, arch_info
);
813 target
->running_alg
= false;
820 * Executes a target-specific native code algorithm and leaves it running.
822 * @param target used to run the algorithm
823 * @param arch_info target-specific description of the algorithm.
825 int target_start_algorithm(struct target
*target
,
826 int num_mem_params
, struct mem_param
*mem_params
,
827 int num_reg_params
, struct reg_param
*reg_params
,
828 uint32_t entry_point
, uint32_t exit_point
,
831 int retval
= ERROR_FAIL
;
833 if (!target_was_examined(target
)) {
834 LOG_ERROR("Target not examined yet");
837 if (!target
->type
->start_algorithm
) {
838 LOG_ERROR("Target type '%s' does not support %s",
839 target_type_name(target
), __func__
);
842 if (target
->running_alg
) {
843 LOG_ERROR("Target is already running an algorithm");
847 target
->running_alg
= true;
848 retval
= target
->type
->start_algorithm(target
,
849 num_mem_params
, mem_params
,
850 num_reg_params
, reg_params
,
851 entry_point
, exit_point
, arch_info
);
858 * Waits for an algorithm started with target_start_algorithm() to complete.
860 * @param target used to run the algorithm
861 * @param arch_info target-specific description of the algorithm.
863 int target_wait_algorithm(struct target
*target
,
864 int num_mem_params
, struct mem_param
*mem_params
,
865 int num_reg_params
, struct reg_param
*reg_params
,
866 uint32_t exit_point
, int timeout_ms
,
869 int retval
= ERROR_FAIL
;
871 if (!target
->type
->wait_algorithm
) {
872 LOG_ERROR("Target type '%s' does not support %s",
873 target_type_name(target
), __func__
);
876 if (!target
->running_alg
) {
877 LOG_ERROR("Target is not running an algorithm");
881 retval
= target
->type
->wait_algorithm(target
,
882 num_mem_params
, mem_params
,
883 num_reg_params
, reg_params
,
884 exit_point
, timeout_ms
, arch_info
);
885 if (retval
!= ERROR_TARGET_TIMEOUT
)
886 target
->running_alg
= false;
893 * Streams data to a circular buffer on target intended for consumption by code
894 * running asynchronously on target.
896 * This is intended for applications where target-specific native code runs
897 * on the target, receives data from the circular buffer, does something with
898 * it (most likely writing it to a flash memory), and advances the circular
901 * This assumes that the helper algorithm has already been loaded to the target,
902 * but has not been started yet. Given memory and register parameters are passed
905 * The buffer is defined by (buffer_start, buffer_size) arguments and has the
908 * [buffer_start + 0, buffer_start + 4):
909 * Write Pointer address (aka head). Written and updated by this
910 * routine when new data is written to the circular buffer.
911 * [buffer_start + 4, buffer_start + 8):
912 * Read Pointer address (aka tail). Updated by code running on the
913 * target after it consumes data.
914 * [buffer_start + 8, buffer_start + buffer_size):
915 * Circular buffer contents.
917 * See contrib/loaders/flash/stm32f1x.S for an example.
919 * @param target used to run the algorithm
920 * @param buffer address on the host where data to be sent is located
921 * @param count number of blocks to send
922 * @param block_size size in bytes of each block
923 * @param num_mem_params count of memory-based params to pass to algorithm
924 * @param mem_params memory-based params to pass to algorithm
925 * @param num_reg_params count of register-based params to pass to algorithm
926 * @param reg_params memory-based params to pass to algorithm
927 * @param buffer_start address on the target of the circular buffer structure
928 * @param buffer_size size of the circular buffer structure
929 * @param entry_point address on the target to execute to start the algorithm
930 * @param exit_point address at which to set a breakpoint to catch the
931 * end of the algorithm; can be 0 if target triggers a breakpoint itself
934 int target_run_flash_async_algorithm(struct target
*target
,
935 const uint8_t *buffer
, uint32_t count
, int block_size
,
936 int num_mem_params
, struct mem_param
*mem_params
,
937 int num_reg_params
, struct reg_param
*reg_params
,
938 uint32_t buffer_start
, uint32_t buffer_size
,
939 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
944 const uint8_t *buffer_orig
= buffer
;
946 /* Set up working area. First word is write pointer, second word is read pointer,
947 * rest is fifo data area. */
948 uint32_t wp_addr
= buffer_start
;
949 uint32_t rp_addr
= buffer_start
+ 4;
950 uint32_t fifo_start_addr
= buffer_start
+ 8;
951 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
953 uint32_t wp
= fifo_start_addr
;
954 uint32_t rp
= fifo_start_addr
;
956 /* validate block_size is 2^n */
957 assert(!block_size
|| !(block_size
& (block_size
- 1)));
959 retval
= target_write_u32(target
, wp_addr
, wp
);
960 if (retval
!= ERROR_OK
)
962 retval
= target_write_u32(target
, rp_addr
, rp
);
963 if (retval
!= ERROR_OK
)
966 /* Start up algorithm on target and let it idle while writing the first chunk */
967 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
968 num_reg_params
, reg_params
,
973 if (retval
!= ERROR_OK
) {
974 LOG_ERROR("error starting target flash write algorithm");
980 retval
= target_read_u32(target
, rp_addr
, &rp
);
981 if (retval
!= ERROR_OK
) {
982 LOG_ERROR("failed to get read pointer");
986 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
987 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
990 LOG_ERROR("flash write algorithm aborted by target");
991 retval
= ERROR_FLASH_OPERATION_FAILED
;
995 if (((rp
- fifo_start_addr
) & (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
996 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
1000 /* Count the number of bytes available in the fifo without
1001 * crossing the wrap around. Make sure to not fill it completely,
1002 * because that would make wp == rp and that's the empty condition. */
1003 uint32_t thisrun_bytes
;
1005 thisrun_bytes
= rp
- wp
- block_size
;
1006 else if (rp
> fifo_start_addr
)
1007 thisrun_bytes
= fifo_end_addr
- wp
;
1009 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
1011 if (thisrun_bytes
== 0) {
1012 /* Throttle polling a bit if transfer is (much) faster than flash
1013 * programming. The exact delay shouldn't matter as long as it's
1014 * less than buffer size / flash speed. This is very unlikely to
1015 * run when using high latency connections such as USB. */
1018 /* to stop an infinite loop on some targets check and increment a timeout
1019 * this issue was observed on a stellaris using the new ICDI interface */
1020 if (timeout
++ >= 500) {
1021 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1022 return ERROR_FLASH_OPERATION_FAILED
;
1027 /* reset our timeout */
1030 /* Limit to the amount of data we actually want to write */
1031 if (thisrun_bytes
> count
* block_size
)
1032 thisrun_bytes
= count
* block_size
;
1034 /* Write data to fifo */
1035 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
1036 if (retval
!= ERROR_OK
)
1039 /* Update counters and wrap write pointer */
1040 buffer
+= thisrun_bytes
;
1041 count
-= thisrun_bytes
/ block_size
;
1042 wp
+= thisrun_bytes
;
1043 if (wp
>= fifo_end_addr
)
1044 wp
= fifo_start_addr
;
1046 /* Store updated write pointer to target */
1047 retval
= target_write_u32(target
, wp_addr
, wp
);
1048 if (retval
!= ERROR_OK
)
1052 if (retval
!= ERROR_OK
) {
1053 /* abort flash write algorithm on target */
1054 target_write_u32(target
, wp_addr
, 0);
1057 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1058 num_reg_params
, reg_params
,
1063 if (retval2
!= ERROR_OK
) {
1064 LOG_ERROR("error waiting for target flash write algorithm");
1068 if (retval
== ERROR_OK
) {
1069 /* check if algorithm set rp = 0 after fifo writer loop finished */
1070 retval
= target_read_u32(target
, rp_addr
, &rp
);
1071 if (retval
== ERROR_OK
&& rp
== 0) {
1072 LOG_ERROR("flash write algorithm aborted by target");
1073 retval
= ERROR_FLASH_OPERATION_FAILED
;
1080 int target_read_memory(struct target
*target
,
1081 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1083 if (!target_was_examined(target
)) {
1084 LOG_ERROR("Target not examined yet");
1087 if (!target
->type
->read_memory
) {
1088 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1091 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1094 int target_read_phys_memory(struct target
*target
,
1095 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1097 if (!target_was_examined(target
)) {
1098 LOG_ERROR("Target not examined yet");
1101 if (!target
->type
->read_phys_memory
) {
1102 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1105 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1108 int target_write_memory(struct target
*target
,
1109 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1111 if (!target_was_examined(target
)) {
1112 LOG_ERROR("Target not examined yet");
1115 if (!target
->type
->write_memory
) {
1116 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1119 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1122 int target_write_phys_memory(struct target
*target
,
1123 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1125 if (!target_was_examined(target
)) {
1126 LOG_ERROR("Target not examined yet");
1129 if (!target
->type
->write_phys_memory
) {
1130 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1133 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1136 int target_add_breakpoint(struct target
*target
,
1137 struct breakpoint
*breakpoint
)
1139 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1140 LOG_WARNING("target %s is not halted (add breakpoint)", target_name(target
));
1141 return ERROR_TARGET_NOT_HALTED
;
1143 return target
->type
->add_breakpoint(target
, breakpoint
);
1146 int target_add_context_breakpoint(struct target
*target
,
1147 struct breakpoint
*breakpoint
)
1149 if (target
->state
!= TARGET_HALTED
) {
1150 LOG_WARNING("target %s is not halted (add context breakpoint)", target_name(target
));
1151 return ERROR_TARGET_NOT_HALTED
;
1153 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1156 int target_add_hybrid_breakpoint(struct target
*target
,
1157 struct breakpoint
*breakpoint
)
1159 if (target
->state
!= TARGET_HALTED
) {
1160 LOG_WARNING("target %s is not halted (add hybrid breakpoint)", target_name(target
));
1161 return ERROR_TARGET_NOT_HALTED
;
1163 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1166 int target_remove_breakpoint(struct target
*target
,
1167 struct breakpoint
*breakpoint
)
1169 return target
->type
->remove_breakpoint(target
, breakpoint
);
1172 int target_add_watchpoint(struct target
*target
,
1173 struct watchpoint
*watchpoint
)
1175 if (target
->state
!= TARGET_HALTED
) {
1176 LOG_WARNING("target %s is not halted (add watchpoint)", target_name(target
));
1177 return ERROR_TARGET_NOT_HALTED
;
1179 return target
->type
->add_watchpoint(target
, watchpoint
);
1181 int target_remove_watchpoint(struct target
*target
,
1182 struct watchpoint
*watchpoint
)
1184 return target
->type
->remove_watchpoint(target
, watchpoint
);
1186 int target_hit_watchpoint(struct target
*target
,
1187 struct watchpoint
**hit_watchpoint
)
1189 if (target
->state
!= TARGET_HALTED
) {
1190 LOG_WARNING("target %s is not halted (hit watchpoint)", target
->cmd_name
);
1191 return ERROR_TARGET_NOT_HALTED
;
1194 if (target
->type
->hit_watchpoint
== NULL
) {
1195 /* For backward compatible, if hit_watchpoint is not implemented,
1196 * return ERROR_FAIL such that gdb_server will not take the nonsense
1201 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1204 const char *target_get_gdb_arch(struct target
*target
)
1206 if (target
->type
->get_gdb_arch
== NULL
)
1208 return target
->type
->get_gdb_arch(target
);
1211 int target_get_gdb_reg_list(struct target
*target
,
1212 struct reg
**reg_list
[], int *reg_list_size
,
1213 enum target_register_class reg_class
)
1215 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1218 bool target_supports_gdb_connection(struct target
*target
)
1221 * based on current code, we can simply exclude all the targets that
1222 * don't provide get_gdb_reg_list; this could change with new targets.
1224 return !!target
->type
->get_gdb_reg_list
;
1227 int target_step(struct target
*target
,
1228 int current
, target_addr_t address
, int handle_breakpoints
)
1230 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1233 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1235 if (target
->state
!= TARGET_HALTED
) {
1236 LOG_WARNING("target %s is not halted (gdb fileio)", target
->cmd_name
);
1237 return ERROR_TARGET_NOT_HALTED
;
1239 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1242 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1244 if (target
->state
!= TARGET_HALTED
) {
1245 LOG_WARNING("target %s is not halted (gdb fileio end)", target
->cmd_name
);
1246 return ERROR_TARGET_NOT_HALTED
;
1248 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1251 int target_profiling(struct target
*target
, uint32_t *samples
,
1252 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1254 if (target
->state
!= TARGET_HALTED
) {
1255 LOG_WARNING("target %s is not halted (profiling)", target
->cmd_name
);
1256 return ERROR_TARGET_NOT_HALTED
;
1258 return target
->type
->profiling(target
, samples
, max_num_samples
,
1259 num_samples
, seconds
);
1263 * Reset the @c examined flag for the given target.
1264 * Pure paranoia -- targets are zeroed on allocation.
1266 static void target_reset_examined(struct target
*target
)
1268 target
->examined
= false;
1271 static int handle_target(void *priv
);
1273 static int target_init_one(struct command_context
*cmd_ctx
,
1274 struct target
*target
)
1276 target_reset_examined(target
);
1278 struct target_type
*type
= target
->type
;
1279 if (type
->examine
== NULL
)
1280 type
->examine
= default_examine
;
1282 if (type
->check_reset
== NULL
)
1283 type
->check_reset
= default_check_reset
;
1285 assert(type
->init_target
!= NULL
);
1287 int retval
= type
->init_target(cmd_ctx
, target
);
1288 if (ERROR_OK
!= retval
) {
1289 LOG_ERROR("target '%s' init failed", target_name(target
));
1293 /* Sanity-check MMU support ... stub in what we must, to help
1294 * implement it in stages, but warn if we need to do so.
1297 if (type
->virt2phys
== NULL
) {
1298 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1299 type
->virt2phys
= identity_virt2phys
;
1302 /* Make sure no-MMU targets all behave the same: make no
1303 * distinction between physical and virtual addresses, and
1304 * ensure that virt2phys() is always an identity mapping.
1306 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1307 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1310 type
->write_phys_memory
= type
->write_memory
;
1311 type
->read_phys_memory
= type
->read_memory
;
1312 type
->virt2phys
= identity_virt2phys
;
1315 if (target
->type
->read_buffer
== NULL
)
1316 target
->type
->read_buffer
= target_read_buffer_default
;
1318 if (target
->type
->write_buffer
== NULL
)
1319 target
->type
->write_buffer
= target_write_buffer_default
;
1321 if (target
->type
->get_gdb_fileio_info
== NULL
)
1322 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1324 if (target
->type
->gdb_fileio_end
== NULL
)
1325 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1327 if (target
->type
->profiling
== NULL
)
1328 target
->type
->profiling
= target_profiling_default
;
1333 static int target_init(struct command_context
*cmd_ctx
)
1335 struct target
*target
;
1338 for (target
= all_targets
; target
; target
= target
->next
) {
1339 retval
= target_init_one(cmd_ctx
, target
);
1340 if (ERROR_OK
!= retval
)
1347 retval
= target_register_user_commands(cmd_ctx
);
1348 if (ERROR_OK
!= retval
)
1351 retval
= target_register_timer_callback(&handle_target
,
1352 polling_interval
, 1, cmd_ctx
->interp
);
1353 if (ERROR_OK
!= retval
)
1359 COMMAND_HANDLER(handle_target_init_command
)
1364 return ERROR_COMMAND_SYNTAX_ERROR
;
1366 static bool target_initialized
;
1367 if (target_initialized
) {
1368 LOG_INFO("'target init' has already been called");
1371 target_initialized
= true;
1373 retval
= command_run_line(CMD_CTX
, "init_targets");
1374 if (ERROR_OK
!= retval
)
1377 retval
= command_run_line(CMD_CTX
, "init_target_events");
1378 if (ERROR_OK
!= retval
)
1381 retval
= command_run_line(CMD_CTX
, "init_board");
1382 if (ERROR_OK
!= retval
)
1385 LOG_DEBUG("Initializing targets...");
1386 return target_init(CMD_CTX
);
1389 int target_register_event_callback(int (*callback
)(struct target
*target
,
1390 enum target_event event
, void *priv
), void *priv
)
1392 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1394 if (callback
== NULL
)
1395 return ERROR_COMMAND_SYNTAX_ERROR
;
1398 while ((*callbacks_p
)->next
)
1399 callbacks_p
= &((*callbacks_p
)->next
);
1400 callbacks_p
= &((*callbacks_p
)->next
);
1403 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1404 (*callbacks_p
)->callback
= callback
;
1405 (*callbacks_p
)->priv
= priv
;
1406 (*callbacks_p
)->next
= NULL
;
1411 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1412 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1414 struct target_reset_callback
*entry
;
1416 if (callback
== NULL
)
1417 return ERROR_COMMAND_SYNTAX_ERROR
;
1419 entry
= malloc(sizeof(struct target_reset_callback
));
1420 if (entry
== NULL
) {
1421 LOG_ERROR("error allocating buffer for reset callback entry");
1422 return ERROR_COMMAND_SYNTAX_ERROR
;
1425 entry
->callback
= callback
;
1427 list_add(&entry
->list
, &target_reset_callback_list
);
1433 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1434 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1436 struct target_trace_callback
*entry
;
1438 if (callback
== NULL
)
1439 return ERROR_COMMAND_SYNTAX_ERROR
;
1441 entry
= malloc(sizeof(struct target_trace_callback
));
1442 if (entry
== NULL
) {
1443 LOG_ERROR("error allocating buffer for trace callback entry");
1444 return ERROR_COMMAND_SYNTAX_ERROR
;
1447 entry
->callback
= callback
;
1449 list_add(&entry
->list
, &target_trace_callback_list
);
1455 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1457 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1459 if (callback
== NULL
)
1460 return ERROR_COMMAND_SYNTAX_ERROR
;
1463 while ((*callbacks_p
)->next
)
1464 callbacks_p
= &((*callbacks_p
)->next
);
1465 callbacks_p
= &((*callbacks_p
)->next
);
1468 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1469 (*callbacks_p
)->callback
= callback
;
1470 (*callbacks_p
)->periodic
= periodic
;
1471 (*callbacks_p
)->time_ms
= time_ms
;
1472 (*callbacks_p
)->removed
= false;
1474 gettimeofday(&(*callbacks_p
)->when
, NULL
);
1475 timeval_add_time(&(*callbacks_p
)->when
, 0, time_ms
* 1000);
1477 (*callbacks_p
)->priv
= priv
;
1478 (*callbacks_p
)->next
= NULL
;
1483 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1484 enum target_event event
, void *priv
), void *priv
)
1486 struct target_event_callback
**p
= &target_event_callbacks
;
1487 struct target_event_callback
*c
= target_event_callbacks
;
1489 if (callback
== NULL
)
1490 return ERROR_COMMAND_SYNTAX_ERROR
;
1493 struct target_event_callback
*next
= c
->next
;
1494 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1506 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1507 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1509 struct target_reset_callback
*entry
;
1511 if (callback
== NULL
)
1512 return ERROR_COMMAND_SYNTAX_ERROR
;
1514 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1515 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1516 list_del(&entry
->list
);
1525 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1526 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1528 struct target_trace_callback
*entry
;
1530 if (callback
== NULL
)
1531 return ERROR_COMMAND_SYNTAX_ERROR
;
1533 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1534 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1535 list_del(&entry
->list
);
1544 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1546 if (callback
== NULL
)
1547 return ERROR_COMMAND_SYNTAX_ERROR
;
1549 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1551 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1560 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1562 struct target_event_callback
*callback
= target_event_callbacks
;
1563 struct target_event_callback
*next_callback
;
1565 if (event
== TARGET_EVENT_HALTED
) {
1566 /* execute early halted first */
1567 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1570 LOG_DEBUG("target event %i (%s)", event
,
1571 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1573 target_handle_event(target
, event
);
1576 next_callback
= callback
->next
;
1577 callback
->callback(target
, event
, callback
->priv
);
1578 callback
= next_callback
;
1584 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1586 struct target_reset_callback
*callback
;
1588 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1589 Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1591 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1592 callback
->callback(target
, reset_mode
, callback
->priv
);
1597 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1599 struct target_trace_callback
*callback
;
1601 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1602 callback
->callback(target
, len
, data
, callback
->priv
);
1607 static int target_timer_callback_periodic_restart(
1608 struct target_timer_callback
*cb
, struct timeval
*now
)
1611 timeval_add_time(&cb
->when
, 0, cb
->time_ms
* 1000L);
1615 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1616 struct timeval
*now
)
1618 cb
->callback(cb
->priv
);
1621 return target_timer_callback_periodic_restart(cb
, now
);
1623 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1626 static int target_call_timer_callbacks_check_time(int checktime
)
1628 static bool callback_processing
;
1630 /* Do not allow nesting */
1631 if (callback_processing
)
1634 callback_processing
= true;
1639 gettimeofday(&now
, NULL
);
1641 /* Store an address of the place containing a pointer to the
1642 * next item; initially, that's a standalone "root of the
1643 * list" variable. */
1644 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1646 if ((*callback
)->removed
) {
1647 struct target_timer_callback
*p
= *callback
;
1648 *callback
= (*callback
)->next
;
1653 bool call_it
= (*callback
)->callback
&&
1654 ((!checktime
&& (*callback
)->periodic
) ||
1655 timeval_compare(&now
, &(*callback
)->when
) >= 0);
1658 target_call_timer_callback(*callback
, &now
);
1660 callback
= &(*callback
)->next
;
1663 callback_processing
= false;
1667 int target_call_timer_callbacks(void)
1669 return target_call_timer_callbacks_check_time(1);
1672 /* invoke periodic callbacks immediately */
1673 int target_call_timer_callbacks_now(void)
1675 return target_call_timer_callbacks_check_time(0);
1678 /* Prints the working area layout for debug purposes */
1679 static void print_wa_layout(struct target
*target
)
1681 struct working_area
*c
= target
->working_areas
;
1684 LOG_DEBUG("%c%c " TARGET_ADDR_FMT
"-" TARGET_ADDR_FMT
" (%" PRIu32
" bytes)",
1685 c
->backup ?
'b' : ' ', c
->free ?
' ' : '*',
1686 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1691 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1692 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1694 assert(area
->free
); /* Shouldn't split an allocated area */
1695 assert(size
<= area
->size
); /* Caller should guarantee this */
1697 /* Split only if not already the right size */
1698 if (size
< area
->size
) {
1699 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1704 new_wa
->next
= area
->next
;
1705 new_wa
->size
= area
->size
- size
;
1706 new_wa
->address
= area
->address
+ size
;
1707 new_wa
->backup
= NULL
;
1708 new_wa
->user
= NULL
;
1709 new_wa
->free
= true;
1711 area
->next
= new_wa
;
1714 /* If backup memory was allocated to this area, it has the wrong size
1715 * now so free it and it will be reallocated if/when needed */
1718 area
->backup
= NULL
;
1723 /* Merge all adjacent free areas into one */
1724 static void target_merge_working_areas(struct target
*target
)
1726 struct working_area
*c
= target
->working_areas
;
1728 while (c
&& c
->next
) {
1729 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1731 /* Find two adjacent free areas */
1732 if (c
->free
&& c
->next
->free
) {
1733 /* Merge the last into the first */
1734 c
->size
+= c
->next
->size
;
1736 /* Remove the last */
1737 struct working_area
*to_be_freed
= c
->next
;
1738 c
->next
= c
->next
->next
;
1739 if (to_be_freed
->backup
)
1740 free(to_be_freed
->backup
);
1743 /* If backup memory was allocated to the remaining area, it's has
1744 * the wrong size now */
1755 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1757 /* Reevaluate working area address based on MMU state*/
1758 if (target
->working_areas
== NULL
) {
1762 retval
= target
->type
->mmu(target
, &enabled
);
1763 if (retval
!= ERROR_OK
)
1767 if (target
->working_area_phys_spec
) {
1768 LOG_DEBUG("MMU disabled, using physical "
1769 "address for working memory " TARGET_ADDR_FMT
,
1770 target
->working_area_phys
);
1771 target
->working_area
= target
->working_area_phys
;
1773 LOG_ERROR("No working memory available. "
1774 "Specify -work-area-phys to target.");
1775 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1778 if (target
->working_area_virt_spec
) {
1779 LOG_DEBUG("MMU enabled, using virtual "
1780 "address for working memory " TARGET_ADDR_FMT
,
1781 target
->working_area_virt
);
1782 target
->working_area
= target
->working_area_virt
;
1784 LOG_ERROR("No working memory available. "
1785 "Specify -work-area-virt to target.");
1786 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1790 /* Set up initial working area on first call */
1791 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1793 new_wa
->next
= NULL
;
1794 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1795 new_wa
->address
= target
->working_area
;
1796 new_wa
->backup
= NULL
;
1797 new_wa
->user
= NULL
;
1798 new_wa
->free
= true;
1801 target
->working_areas
= new_wa
;
1804 /* only allocate multiples of 4 byte */
1806 size
= (size
+ 3) & (~3UL);
1808 struct working_area
*c
= target
->working_areas
;
1810 /* Find the first large enough working area */
1812 if (c
->free
&& c
->size
>= size
)
1818 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1820 /* Split the working area into the requested size */
1821 target_split_working_area(c
, size
);
1823 LOG_DEBUG("allocated new working area of %" PRIu32
" bytes at address " TARGET_ADDR_FMT
,
1826 if (target
->backup_working_area
) {
1827 if (c
->backup
== NULL
) {
1828 c
->backup
= malloc(c
->size
);
1829 if (c
->backup
== NULL
)
1833 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1834 if (retval
!= ERROR_OK
)
1838 /* mark as used, and return the new (reused) area */
1845 print_wa_layout(target
);
1850 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1854 retval
= target_alloc_working_area_try(target
, size
, area
);
1855 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1856 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1861 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1863 int retval
= ERROR_OK
;
1865 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1866 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1867 if (retval
!= ERROR_OK
)
1868 LOG_ERROR("failed to restore %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1869 area
->size
, area
->address
);
1875 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1876 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1878 int retval
= ERROR_OK
;
1884 retval
= target_restore_working_area(target
, area
);
1885 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1886 if (retval
!= ERROR_OK
)
1892 LOG_DEBUG("freed %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1893 area
->size
, area
->address
);
1895 /* mark user pointer invalid */
1896 /* TODO: Is this really safe? It points to some previous caller's memory.
1897 * How could we know that the area pointer is still in that place and not
1898 * some other vital data? What's the purpose of this, anyway? */
1902 target_merge_working_areas(target
);
1904 print_wa_layout(target
);
1909 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1911 return target_free_working_area_restore(target
, area
, 1);
1914 static void target_destroy(struct target
*target
)
1916 if (target
->type
->deinit_target
)
1917 target
->type
->deinit_target(target
);
1919 if (target
->semihosting
)
1920 free(target
->semihosting
);
1922 jtag_unregister_event_callback(jtag_enable_callback
, target
);
1924 struct target_event_action
*teap
= target
->event_action
;
1926 struct target_event_action
*next
= teap
->next
;
1927 Jim_DecrRefCount(teap
->interp
, teap
->body
);
1932 target_free_all_working_areas(target
);
1933 /* Now we have none or only one working area marked as free */
1934 if (target
->working_areas
) {
1935 free(target
->working_areas
->backup
);
1936 free(target
->working_areas
);
1939 /* release the targets SMP list */
1941 struct target_list
*head
= target
->head
;
1942 while (head
!= NULL
) {
1943 struct target_list
*pos
= head
->next
;
1944 head
->target
->smp
= 0;
1951 free(target
->gdb_port_override
);
1953 free(target
->trace_info
);
1954 free(target
->fileio_info
);
1955 free(target
->cmd_name
);
1959 void target_quit(void)
1961 struct target_event_callback
*pe
= target_event_callbacks
;
1963 struct target_event_callback
*t
= pe
->next
;
1967 target_event_callbacks
= NULL
;
1969 struct target_timer_callback
*pt
= target_timer_callbacks
;
1971 struct target_timer_callback
*t
= pt
->next
;
1975 target_timer_callbacks
= NULL
;
1977 for (struct target
*target
= all_targets
; target
;) {
1981 target_destroy(target
);
1988 /* free resources and restore memory, if restoring memory fails,
1989 * free up resources anyway
1991 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1993 struct working_area
*c
= target
->working_areas
;
1995 LOG_DEBUG("freeing all working areas");
1997 /* Loop through all areas, restoring the allocated ones and marking them as free */
2001 target_restore_working_area(target
, c
);
2003 *c
->user
= NULL
; /* Same as above */
2009 /* Run a merge pass to combine all areas into one */
2010 target_merge_working_areas(target
);
2012 print_wa_layout(target
);
2015 void target_free_all_working_areas(struct target
*target
)
2017 target_free_all_working_areas_restore(target
, 1);
2020 /* Find the largest number of bytes that can be allocated */
2021 uint32_t target_get_working_area_avail(struct target
*target
)
2023 struct working_area
*c
= target
->working_areas
;
2024 uint32_t max_size
= 0;
2027 return target
->working_area_size
;
2030 if (c
->free
&& max_size
< c
->size
)
2039 int target_arch_state(struct target
*target
)
2042 if (target
== NULL
) {
2043 LOG_WARNING("No target has been configured");
2047 if (target
->state
!= TARGET_HALTED
)
2050 retval
= target
->type
->arch_state(target
);
2054 static int target_get_gdb_fileio_info_default(struct target
*target
,
2055 struct gdb_fileio_info
*fileio_info
)
2057 /* If target does not support semi-hosting function, target
2058 has no need to provide .get_gdb_fileio_info callback.
2059 It just return ERROR_FAIL and gdb_server will return "Txx"
2060 as target halted every time. */
2064 static int target_gdb_fileio_end_default(struct target
*target
,
2065 int retcode
, int fileio_errno
, bool ctrl_c
)
2070 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
2071 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
2073 struct timeval timeout
, now
;
2075 gettimeofday(&timeout
, NULL
);
2076 timeval_add_time(&timeout
, seconds
, 0);
2078 LOG_INFO("Starting profiling. Halting and resuming the"
2079 " target as often as we can...");
2081 uint32_t sample_count
= 0;
2082 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2083 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2085 int retval
= ERROR_OK
;
2087 target_poll(target
);
2088 if (target
->state
== TARGET_HALTED
) {
2089 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
2090 samples
[sample_count
++] = t
;
2091 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2092 retval
= target_resume(target
, 1, 0, 0, 0);
2093 target_poll(target
);
2094 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2095 } else if (target
->state
== TARGET_RUNNING
) {
2096 /* We want to quickly sample the PC. */
2097 retval
= target_halt(target
);
2099 LOG_INFO("Target not halted or running");
2104 if (retval
!= ERROR_OK
)
2107 gettimeofday(&now
, NULL
);
2108 if ((sample_count
>= max_num_samples
) || timeval_compare(&now
, &timeout
) >= 0) {
2109 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2114 *num_samples
= sample_count
;
2118 /* Single aligned words are guaranteed to use 16 or 32 bit access
2119 * mode respectively, otherwise data is handled as quickly as
2122 int target_write_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, const uint8_t *buffer
)
2124 LOG_DEBUG("writing buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2127 if (!target_was_examined(target
)) {
2128 LOG_ERROR("Target not examined yet");
2135 if ((address
+ size
- 1) < address
) {
2136 /* GDB can request this when e.g. PC is 0xfffffffc */
2137 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2143 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2146 static int target_write_buffer_default(struct target
*target
,
2147 target_addr_t address
, uint32_t count
, const uint8_t *buffer
)
2151 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2152 * will have something to do with the size we leave to it. */
2153 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2154 if (address
& size
) {
2155 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2156 if (retval
!= ERROR_OK
)
2164 /* Write the data with as large access size as possible. */
2165 for (; size
> 0; size
/= 2) {
2166 uint32_t aligned
= count
- count
% size
;
2168 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2169 if (retval
!= ERROR_OK
)
2180 /* Single aligned words are guaranteed to use 16 or 32 bit access
2181 * mode respectively, otherwise data is handled as quickly as
2184 int target_read_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
2186 LOG_DEBUG("reading buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2189 if (!target_was_examined(target
)) {
2190 LOG_ERROR("Target not examined yet");
2197 if ((address
+ size
- 1) < address
) {
2198 /* GDB can request this when e.g. PC is 0xfffffffc */
2199 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2205 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2208 static int target_read_buffer_default(struct target
*target
, target_addr_t address
, uint32_t count
, uint8_t *buffer
)
2212 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2213 * will have something to do with the size we leave to it. */
2214 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2215 if (address
& size
) {
2216 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2217 if (retval
!= ERROR_OK
)
2225 /* Read the data with as large access size as possible. */
2226 for (; size
> 0; size
/= 2) {
2227 uint32_t aligned
= count
- count
% size
;
2229 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2230 if (retval
!= ERROR_OK
)
2241 int target_checksum_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* crc
)
2246 uint32_t checksum
= 0;
2247 if (!target_was_examined(target
)) {
2248 LOG_ERROR("Target not examined yet");
2252 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2253 if (retval
!= ERROR_OK
) {
2254 buffer
= malloc(size
);
2255 if (buffer
== NULL
) {
2256 LOG_ERROR("error allocating buffer for section (%" PRId32
" bytes)", size
);
2257 return ERROR_COMMAND_SYNTAX_ERROR
;
2259 retval
= target_read_buffer(target
, address
, size
, buffer
);
2260 if (retval
!= ERROR_OK
) {
2265 /* convert to target endianness */
2266 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2267 uint32_t target_data
;
2268 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2269 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2272 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2281 int target_blank_check_memory(struct target
*target
,
2282 struct target_memory_check_block
*blocks
, int num_blocks
,
2283 uint8_t erased_value
)
2285 if (!target_was_examined(target
)) {
2286 LOG_ERROR("Target not examined yet");
2290 if (target
->type
->blank_check_memory
== NULL
)
2291 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2293 return target
->type
->blank_check_memory(target
, blocks
, num_blocks
, erased_value
);
2296 int target_read_u64(struct target
*target
, target_addr_t address
, uint64_t *value
)
2298 uint8_t value_buf
[8];
2299 if (!target_was_examined(target
)) {
2300 LOG_ERROR("Target not examined yet");
2304 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2306 if (retval
== ERROR_OK
) {
2307 *value
= target_buffer_get_u64(target
, value_buf
);
2308 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2313 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2320 int target_read_u32(struct target
*target
, target_addr_t address
, uint32_t *value
)
2322 uint8_t value_buf
[4];
2323 if (!target_was_examined(target
)) {
2324 LOG_ERROR("Target not examined yet");
2328 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2330 if (retval
== ERROR_OK
) {
2331 *value
= target_buffer_get_u32(target
, value_buf
);
2332 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2337 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2344 int target_read_u16(struct target
*target
, target_addr_t address
, uint16_t *value
)
2346 uint8_t value_buf
[2];
2347 if (!target_was_examined(target
)) {
2348 LOG_ERROR("Target not examined yet");
2352 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2354 if (retval
== ERROR_OK
) {
2355 *value
= target_buffer_get_u16(target
, value_buf
);
2356 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%4.4" PRIx16
,
2361 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2368 int target_read_u8(struct target
*target
, target_addr_t address
, uint8_t *value
)
2370 if (!target_was_examined(target
)) {
2371 LOG_ERROR("Target not examined yet");
2375 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2377 if (retval
== ERROR_OK
) {
2378 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2383 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2390 int target_write_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2393 uint8_t value_buf
[8];
2394 if (!target_was_examined(target
)) {
2395 LOG_ERROR("Target not examined yet");
2399 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2403 target_buffer_set_u64(target
, value_buf
, value
);
2404 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2405 if (retval
!= ERROR_OK
)
2406 LOG_DEBUG("failed: %i", retval
);
2411 int target_write_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2414 uint8_t value_buf
[4];
2415 if (!target_was_examined(target
)) {
2416 LOG_ERROR("Target not examined yet");
2420 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2424 target_buffer_set_u32(target
, value_buf
, value
);
2425 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2426 if (retval
!= ERROR_OK
)
2427 LOG_DEBUG("failed: %i", retval
);
2432 int target_write_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2435 uint8_t value_buf
[2];
2436 if (!target_was_examined(target
)) {
2437 LOG_ERROR("Target not examined yet");
2441 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2445 target_buffer_set_u16(target
, value_buf
, value
);
2446 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2447 if (retval
!= ERROR_OK
)
2448 LOG_DEBUG("failed: %i", retval
);
2453 int target_write_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2456 if (!target_was_examined(target
)) {
2457 LOG_ERROR("Target not examined yet");
2461 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2464 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2465 if (retval
!= ERROR_OK
)
2466 LOG_DEBUG("failed: %i", retval
);
2471 int target_write_phys_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2474 uint8_t value_buf
[8];
2475 if (!target_was_examined(target
)) {
2476 LOG_ERROR("Target not examined yet");
2480 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2484 target_buffer_set_u64(target
, value_buf
, value
);
2485 retval
= target_write_phys_memory(target
, address
, 8, 1, value_buf
);
2486 if (retval
!= ERROR_OK
)
2487 LOG_DEBUG("failed: %i", retval
);
2492 int target_write_phys_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2495 uint8_t value_buf
[4];
2496 if (!target_was_examined(target
)) {
2497 LOG_ERROR("Target not examined yet");
2501 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2505 target_buffer_set_u32(target
, value_buf
, value
);
2506 retval
= target_write_phys_memory(target
, address
, 4, 1, value_buf
);
2507 if (retval
!= ERROR_OK
)
2508 LOG_DEBUG("failed: %i", retval
);
2513 int target_write_phys_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2516 uint8_t value_buf
[2];
2517 if (!target_was_examined(target
)) {
2518 LOG_ERROR("Target not examined yet");
2522 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2526 target_buffer_set_u16(target
, value_buf
, value
);
2527 retval
= target_write_phys_memory(target
, address
, 2, 1, value_buf
);
2528 if (retval
!= ERROR_OK
)
2529 LOG_DEBUG("failed: %i", retval
);
2534 int target_write_phys_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2537 if (!target_was_examined(target
)) {
2538 LOG_ERROR("Target not examined yet");
2542 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2545 retval
= target_write_phys_memory(target
, address
, 1, 1, &value
);
2546 if (retval
!= ERROR_OK
)
2547 LOG_DEBUG("failed: %i", retval
);
2552 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2554 struct target
*target
= get_target(name
);
2555 if (target
== NULL
) {
2556 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2559 if (!target
->tap
->enabled
) {
2560 LOG_USER("Target: TAP %s is disabled, "
2561 "can't be the current target\n",
2562 target
->tap
->dotted_name
);
2566 cmd_ctx
->current_target
= target
;
2567 if (cmd_ctx
->current_target_override
)
2568 cmd_ctx
->current_target_override
= target
;
2574 COMMAND_HANDLER(handle_targets_command
)
2576 int retval
= ERROR_OK
;
2577 if (CMD_ARGC
== 1) {
2578 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2579 if (retval
== ERROR_OK
) {
2585 struct target
*target
= all_targets
;
2586 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2587 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2592 if (target
->tap
->enabled
)
2593 state
= target_state_name(target
);
2595 state
= "tap-disabled";
2597 if (CMD_CTX
->current_target
== target
)
2600 /* keep columns lined up to match the headers above */
2601 command_print(CMD_CTX
,
2602 "%2d%c %-18s %-10s %-6s %-18s %s",
2603 target
->target_number
,
2605 target_name(target
),
2606 target_type_name(target
),
2607 Jim_Nvp_value2name_simple(nvp_target_endian
,
2608 target
->endianness
)->name
,
2609 target
->tap
->dotted_name
,
2611 target
= target
->next
;
2617 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2619 static int powerDropout
;
2620 static int srstAsserted
;
2622 static int runPowerRestore
;
2623 static int runPowerDropout
;
2624 static int runSrstAsserted
;
2625 static int runSrstDeasserted
;
2627 static int sense_handler(void)
2629 static int prevSrstAsserted
;
2630 static int prevPowerdropout
;
2632 int retval
= jtag_power_dropout(&powerDropout
);
2633 if (retval
!= ERROR_OK
)
2637 powerRestored
= prevPowerdropout
&& !powerDropout
;
2639 runPowerRestore
= 1;
2641 int64_t current
= timeval_ms();
2642 static int64_t lastPower
;
2643 bool waitMore
= lastPower
+ 2000 > current
;
2644 if (powerDropout
&& !waitMore
) {
2645 runPowerDropout
= 1;
2646 lastPower
= current
;
2649 retval
= jtag_srst_asserted(&srstAsserted
);
2650 if (retval
!= ERROR_OK
)
2654 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2656 static int64_t lastSrst
;
2657 waitMore
= lastSrst
+ 2000 > current
;
2658 if (srstDeasserted
&& !waitMore
) {
2659 runSrstDeasserted
= 1;
2663 if (!prevSrstAsserted
&& srstAsserted
)
2664 runSrstAsserted
= 1;
2666 prevSrstAsserted
= srstAsserted
;
2667 prevPowerdropout
= powerDropout
;
2669 if (srstDeasserted
|| powerRestored
) {
2670 /* Other than logging the event we can't do anything here.
2671 * Issuing a reset is a particularly bad idea as we might
2672 * be inside a reset already.
2679 /* process target state changes */
2680 static int handle_target(void *priv
)
2682 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2683 int retval
= ERROR_OK
;
2685 if (!is_jtag_poll_safe()) {
2686 /* polling is disabled currently */
2690 /* we do not want to recurse here... */
2691 static int recursive
;
2695 /* danger! running these procedures can trigger srst assertions and power dropouts.
2696 * We need to avoid an infinite loop/recursion here and we do that by
2697 * clearing the flags after running these events.
2699 int did_something
= 0;
2700 if (runSrstAsserted
) {
2701 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2702 Jim_Eval(interp
, "srst_asserted");
2705 if (runSrstDeasserted
) {
2706 Jim_Eval(interp
, "srst_deasserted");
2709 if (runPowerDropout
) {
2710 LOG_INFO("Power dropout detected, running power_dropout proc.");
2711 Jim_Eval(interp
, "power_dropout");
2714 if (runPowerRestore
) {
2715 Jim_Eval(interp
, "power_restore");
2719 if (did_something
) {
2720 /* clear detect flags */
2724 /* clear action flags */
2726 runSrstAsserted
= 0;
2727 runSrstDeasserted
= 0;
2728 runPowerRestore
= 0;
2729 runPowerDropout
= 0;
2734 /* Poll targets for state changes unless that's globally disabled.
2735 * Skip targets that are currently disabled.
2737 for (struct target
*target
= all_targets
;
2738 is_jtag_poll_safe() && target
;
2739 target
= target
->next
) {
2741 if (!target_was_examined(target
))
2744 if (!target
->tap
->enabled
)
2747 if (target
->backoff
.times
> target
->backoff
.count
) {
2748 /* do not poll this time as we failed previously */
2749 target
->backoff
.count
++;
2752 target
->backoff
.count
= 0;
2754 /* only poll target if we've got power and srst isn't asserted */
2755 if (!powerDropout
&& !srstAsserted
) {
2756 /* polling may fail silently until the target has been examined */
2757 retval
= target_poll(target
);
2758 if (retval
!= ERROR_OK
) {
2759 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2760 if (target
->backoff
.times
* polling_interval
< 5000) {
2761 target
->backoff
.times
*= 2;
2762 target
->backoff
.times
++;
2765 /* Tell GDB to halt the debugger. This allows the user to
2766 * run monitor commands to handle the situation.
2768 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2770 if (target
->backoff
.times
> 0) {
2771 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
2772 target_reset_examined(target
);
2773 retval
= target_examine_one(target
);
2774 /* Target examination could have failed due to unstable connection,
2775 * but we set the examined flag anyway to repoll it later */
2776 if (retval
!= ERROR_OK
) {
2777 target
->examined
= true;
2778 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2779 target
->backoff
.times
* polling_interval
);
2784 /* Since we succeeded, we reset backoff count */
2785 target
->backoff
.times
= 0;
2792 COMMAND_HANDLER(handle_reg_command
)
2794 struct target
*target
;
2795 struct reg
*reg
= NULL
;
2801 target
= get_current_target(CMD_CTX
);
2803 /* list all available registers for the current target */
2804 if (CMD_ARGC
== 0) {
2805 struct reg_cache
*cache
= target
->reg_cache
;
2811 command_print(CMD_CTX
, "===== %s", cache
->name
);
2813 for (i
= 0, reg
= cache
->reg_list
;
2814 i
< cache
->num_regs
;
2815 i
++, reg
++, count
++) {
2816 if (reg
->exist
== false)
2818 /* only print cached values if they are valid */
2820 value
= buf_to_str(reg
->value
,
2822 command_print(CMD_CTX
,
2823 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2831 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2836 cache
= cache
->next
;
2842 /* access a single register by its ordinal number */
2843 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2845 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2847 struct reg_cache
*cache
= target
->reg_cache
;
2851 for (i
= 0; i
< cache
->num_regs
; i
++) {
2852 if (count
++ == num
) {
2853 reg
= &cache
->reg_list
[i
];
2859 cache
= cache
->next
;
2863 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2864 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2868 /* access a single register by its name */
2869 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2875 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2880 /* display a register */
2881 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2882 && (CMD_ARGV
[1][0] <= '9')))) {
2883 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2886 if (reg
->valid
== 0)
2887 reg
->type
->get(reg
);
2888 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2889 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2894 /* set register value */
2895 if (CMD_ARGC
== 2) {
2896 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2899 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2901 reg
->type
->set(reg
, buf
);
2903 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2904 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2912 return ERROR_COMMAND_SYNTAX_ERROR
;
2915 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2919 COMMAND_HANDLER(handle_poll_command
)
2921 int retval
= ERROR_OK
;
2922 struct target
*target
= get_current_target(CMD_CTX
);
2924 if (CMD_ARGC
== 0) {
2925 command_print(CMD_CTX
, "background polling: %s",
2926 jtag_poll_get_enabled() ?
"on" : "off");
2927 command_print(CMD_CTX
, "TAP: %s (%s)",
2928 target
->tap
->dotted_name
,
2929 target
->tap
->enabled ?
"enabled" : "disabled");
2930 if (!target
->tap
->enabled
)
2932 retval
= target_poll(target
);
2933 if (retval
!= ERROR_OK
)
2935 retval
= target_arch_state(target
);
2936 if (retval
!= ERROR_OK
)
2938 } else if (CMD_ARGC
== 1) {
2940 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2941 jtag_poll_set_enabled(enable
);
2943 return ERROR_COMMAND_SYNTAX_ERROR
;
2948 COMMAND_HANDLER(handle_wait_halt_command
)
2951 return ERROR_COMMAND_SYNTAX_ERROR
;
2953 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2954 if (1 == CMD_ARGC
) {
2955 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2956 if (ERROR_OK
!= retval
)
2957 return ERROR_COMMAND_SYNTAX_ERROR
;
2960 struct target
*target
= get_current_target(CMD_CTX
);
2961 return target_wait_state(target
, TARGET_HALTED
, ms
);
2964 /* wait for target state to change. The trick here is to have a low
2965 * latency for short waits and not to suck up all the CPU time
2968 * After 500ms, keep_alive() is invoked
2970 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2973 int64_t then
= 0, cur
;
2977 retval
= target_poll(target
);
2978 if (retval
!= ERROR_OK
)
2980 if (target
->state
== state
)
2985 then
= timeval_ms();
2986 LOG_DEBUG("waiting for target %s...",
2987 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2993 if ((cur
-then
) > ms
) {
2994 LOG_ERROR("timed out while waiting for target %s",
2995 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
3003 COMMAND_HANDLER(handle_halt_command
)
3007 struct target
*target
= get_current_target(CMD_CTX
);
3009 target
->verbose_halt_msg
= true;
3011 int retval
= target_halt(target
);
3012 if (ERROR_OK
!= retval
)
3015 if (CMD_ARGC
== 1) {
3016 unsigned wait_local
;
3017 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
3018 if (ERROR_OK
!= retval
)
3019 return ERROR_COMMAND_SYNTAX_ERROR
;
3024 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
3027 COMMAND_HANDLER(handle_soft_reset_halt_command
)
3029 struct target
*target
= get_current_target(CMD_CTX
);
3031 LOG_USER("requesting target halt and executing a soft reset");
3033 target_soft_reset_halt(target
);
3038 COMMAND_HANDLER(handle_reset_command
)
3041 return ERROR_COMMAND_SYNTAX_ERROR
;
3043 enum target_reset_mode reset_mode
= RESET_RUN
;
3044 if (CMD_ARGC
== 1) {
3046 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
3047 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
3048 return ERROR_COMMAND_SYNTAX_ERROR
;
3049 reset_mode
= n
->value
;
3052 /* reset *all* targets */
3053 return target_process_reset(CMD_CTX
, reset_mode
);
3057 COMMAND_HANDLER(handle_resume_command
)
3061 return ERROR_COMMAND_SYNTAX_ERROR
;
3063 struct target
*target
= get_current_target(CMD_CTX
);
3065 /* with no CMD_ARGV, resume from current pc, addr = 0,
3066 * with one arguments, addr = CMD_ARGV[0],
3067 * handle breakpoints, not debugging */
3068 target_addr_t addr
= 0;
3069 if (CMD_ARGC
== 1) {
3070 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3074 return target_resume(target
, current
, addr
, 1, 0);
3077 COMMAND_HANDLER(handle_step_command
)
3080 return ERROR_COMMAND_SYNTAX_ERROR
;
3084 /* with no CMD_ARGV, step from current pc, addr = 0,
3085 * with one argument addr = CMD_ARGV[0],
3086 * handle breakpoints, debugging */
3087 target_addr_t addr
= 0;
3089 if (CMD_ARGC
== 1) {
3090 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3094 struct target
*target
= get_current_target(CMD_CTX
);
3096 return target
->type
->step(target
, current_pc
, addr
, 1);
3099 static void handle_md_output(struct command_context
*cmd_ctx
,
3100 struct target
*target
, target_addr_t address
, unsigned size
,
3101 unsigned count
, const uint8_t *buffer
)
3103 const unsigned line_bytecnt
= 32;
3104 unsigned line_modulo
= line_bytecnt
/ size
;
3106 char output
[line_bytecnt
* 4 + 1];
3107 unsigned output_len
= 0;
3109 const char *value_fmt
;
3112 value_fmt
= "%16.16"PRIx64
" ";
3115 value_fmt
= "%8.8"PRIx64
" ";
3118 value_fmt
= "%4.4"PRIx64
" ";
3121 value_fmt
= "%2.2"PRIx64
" ";
3124 /* "can't happen", caller checked */
3125 LOG_ERROR("invalid memory read size: %u", size
);
3129 for (unsigned i
= 0; i
< count
; i
++) {
3130 if (i
% line_modulo
== 0) {
3131 output_len
+= snprintf(output
+ output_len
,
3132 sizeof(output
) - output_len
,
3133 TARGET_ADDR_FMT
": ",
3134 (address
+ (i
* size
)));
3138 const uint8_t *value_ptr
= buffer
+ i
* size
;
3141 value
= target_buffer_get_u64(target
, value_ptr
);
3144 value
= target_buffer_get_u32(target
, value_ptr
);
3147 value
= target_buffer_get_u16(target
, value_ptr
);
3152 output_len
+= snprintf(output
+ output_len
,
3153 sizeof(output
) - output_len
,
3156 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
3157 command_print(cmd_ctx
, "%s", output
);
3163 COMMAND_HANDLER(handle_md_command
)
3166 return ERROR_COMMAND_SYNTAX_ERROR
;
3169 switch (CMD_NAME
[2]) {
3183 return ERROR_COMMAND_SYNTAX_ERROR
;
3186 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3187 int (*fn
)(struct target
*target
,
3188 target_addr_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
3192 fn
= target_read_phys_memory
;
3194 fn
= target_read_memory
;
3195 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
3196 return ERROR_COMMAND_SYNTAX_ERROR
;
3198 target_addr_t address
;
3199 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3203 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
3205 uint8_t *buffer
= calloc(count
, size
);
3206 if (buffer
== NULL
) {
3207 LOG_ERROR("Failed to allocate md read buffer");
3211 struct target
*target
= get_current_target(CMD_CTX
);
3212 int retval
= fn(target
, address
, size
, count
, buffer
);
3213 if (ERROR_OK
== retval
)
3214 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
3221 typedef int (*target_write_fn
)(struct target
*target
,
3222 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
3224 static int target_fill_mem(struct target
*target
,
3225 target_addr_t address
,
3233 /* We have to write in reasonably large chunks to be able
3234 * to fill large memory areas with any sane speed */
3235 const unsigned chunk_size
= 16384;
3236 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
3237 if (target_buf
== NULL
) {
3238 LOG_ERROR("Out of memory");
3242 for (unsigned i
= 0; i
< chunk_size
; i
++) {
3243 switch (data_size
) {
3245 target_buffer_set_u64(target
, target_buf
+ i
* data_size
, b
);
3248 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
3251 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
3254 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
3261 int retval
= ERROR_OK
;
3263 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
3266 if (current
> chunk_size
)
3267 current
= chunk_size
;
3268 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
3269 if (retval
!= ERROR_OK
)
3271 /* avoid GDB timeouts */
3280 COMMAND_HANDLER(handle_mw_command
)
3283 return ERROR_COMMAND_SYNTAX_ERROR
;
3284 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3289 fn
= target_write_phys_memory
;
3291 fn
= target_write_memory
;
3292 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
3293 return ERROR_COMMAND_SYNTAX_ERROR
;
3295 target_addr_t address
;
3296 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3298 target_addr_t value
;
3299 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], value
);
3303 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
3305 struct target
*target
= get_current_target(CMD_CTX
);
3307 switch (CMD_NAME
[2]) {
3321 return ERROR_COMMAND_SYNTAX_ERROR
;
3324 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
3327 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
3328 target_addr_t
*min_address
, target_addr_t
*max_address
)
3330 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
3331 return ERROR_COMMAND_SYNTAX_ERROR
;
3333 /* a base address isn't always necessary,
3334 * default to 0x0 (i.e. don't relocate) */
3335 if (CMD_ARGC
>= 2) {
3337 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3338 image
->base_address
= addr
;
3339 image
->base_address_set
= 1;
3341 image
->base_address_set
= 0;
3343 image
->start_address_set
= 0;
3346 COMMAND_PARSE_ADDRESS(CMD_ARGV
[3], *min_address
);
3347 if (CMD_ARGC
== 5) {
3348 COMMAND_PARSE_ADDRESS(CMD_ARGV
[4], *max_address
);
3349 /* use size (given) to find max (required) */
3350 *max_address
+= *min_address
;
3353 if (*min_address
> *max_address
)
3354 return ERROR_COMMAND_SYNTAX_ERROR
;
3359 COMMAND_HANDLER(handle_load_image_command
)
3363 uint32_t image_size
;
3364 target_addr_t min_address
= 0;
3365 target_addr_t max_address
= -1;
3369 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
3370 &image
, &min_address
, &max_address
);
3371 if (ERROR_OK
!= retval
)
3374 struct target
*target
= get_current_target(CMD_CTX
);
3376 struct duration bench
;
3377 duration_start(&bench
);
3379 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
3384 for (i
= 0; i
< image
.num_sections
; i
++) {
3385 buffer
= malloc(image
.sections
[i
].size
);
3386 if (buffer
== NULL
) {
3387 command_print(CMD_CTX
,
3388 "error allocating buffer for section (%d bytes)",
3389 (int)(image
.sections
[i
].size
));
3390 retval
= ERROR_FAIL
;
3394 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3395 if (retval
!= ERROR_OK
) {
3400 uint32_t offset
= 0;
3401 uint32_t length
= buf_cnt
;
3403 /* DANGER!!! beware of unsigned comparision here!!! */
3405 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3406 (image
.sections
[i
].base_address
< max_address
)) {
3408 if (image
.sections
[i
].base_address
< min_address
) {
3409 /* clip addresses below */
3410 offset
+= min_address
-image
.sections
[i
].base_address
;
3414 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3415 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3417 retval
= target_write_buffer(target
,
3418 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3419 if (retval
!= ERROR_OK
) {
3423 image_size
+= length
;
3424 command_print(CMD_CTX
, "%u bytes written at address " TARGET_ADDR_FMT
"",
3425 (unsigned int)length
,
3426 image
.sections
[i
].base_address
+ offset
);
3432 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3433 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
3434 "in %fs (%0.3f KiB/s)", image_size
,
3435 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3438 image_close(&image
);
3444 COMMAND_HANDLER(handle_dump_image_command
)
3446 struct fileio
*fileio
;
3448 int retval
, retvaltemp
;
3449 target_addr_t address
, size
;
3450 struct duration bench
;
3451 struct target
*target
= get_current_target(CMD_CTX
);
3454 return ERROR_COMMAND_SYNTAX_ERROR
;
3456 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], address
);
3457 COMMAND_PARSE_ADDRESS(CMD_ARGV
[2], size
);
3459 uint32_t buf_size
= (size
> 4096) ?
4096 : size
;
3460 buffer
= malloc(buf_size
);
3464 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3465 if (retval
!= ERROR_OK
) {
3470 duration_start(&bench
);
3473 size_t size_written
;
3474 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
3475 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
3476 if (retval
!= ERROR_OK
)
3479 retval
= fileio_write(fileio
, this_run_size
, buffer
, &size_written
);
3480 if (retval
!= ERROR_OK
)
3483 size
-= this_run_size
;
3484 address
+= this_run_size
;
3489 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3491 retval
= fileio_size(fileio
, &filesize
);
3492 if (retval
!= ERROR_OK
)
3494 command_print(CMD_CTX
,
3495 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize
,
3496 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3499 retvaltemp
= fileio_close(fileio
);
3500 if (retvaltemp
!= ERROR_OK
)
3509 IMAGE_CHECKSUM_ONLY
= 2
3512 static COMMAND_HELPER(handle_verify_image_command_internal
, enum verify_mode verify
)
3516 uint32_t image_size
;
3519 uint32_t checksum
= 0;
3520 uint32_t mem_checksum
= 0;
3524 struct target
*target
= get_current_target(CMD_CTX
);
3527 return ERROR_COMMAND_SYNTAX_ERROR
;
3530 LOG_ERROR("no target selected");
3534 struct duration bench
;
3535 duration_start(&bench
);
3537 if (CMD_ARGC
>= 2) {
3539 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3540 image
.base_address
= addr
;
3541 image
.base_address_set
= 1;
3543 image
.base_address_set
= 0;
3544 image
.base_address
= 0x0;
3547 image
.start_address_set
= 0;
3549 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3550 if (retval
!= ERROR_OK
)
3556 for (i
= 0; i
< image
.num_sections
; i
++) {
3557 buffer
= malloc(image
.sections
[i
].size
);
3558 if (buffer
== NULL
) {
3559 command_print(CMD_CTX
,
3560 "error allocating buffer for section (%d bytes)",
3561 (int)(image
.sections
[i
].size
));
3564 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3565 if (retval
!= ERROR_OK
) {
3570 if (verify
>= IMAGE_VERIFY
) {
3571 /* calculate checksum of image */
3572 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3573 if (retval
!= ERROR_OK
) {
3578 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3579 if (retval
!= ERROR_OK
) {
3583 if ((checksum
!= mem_checksum
) && (verify
== IMAGE_CHECKSUM_ONLY
)) {
3584 LOG_ERROR("checksum mismatch");
3586 retval
= ERROR_FAIL
;
3589 if (checksum
!= mem_checksum
) {
3590 /* failed crc checksum, fall back to a binary compare */
3594 LOG_ERROR("checksum mismatch - attempting binary compare");
3596 data
= malloc(buf_cnt
);
3598 /* Can we use 32bit word accesses? */
3600 int count
= buf_cnt
;
3601 if ((count
% 4) == 0) {
3605 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
3606 if (retval
== ERROR_OK
) {
3608 for (t
= 0; t
< buf_cnt
; t
++) {
3609 if (data
[t
] != buffer
[t
]) {
3610 command_print(CMD_CTX
,
3611 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3613 (unsigned)(t
+ image
.sections
[i
].base_address
),
3616 if (diffs
++ >= 127) {
3617 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
3629 command_print(CMD_CTX
, "address " TARGET_ADDR_FMT
" length 0x%08zx",
3630 image
.sections
[i
].base_address
,
3635 image_size
+= buf_cnt
;
3638 command_print(CMD_CTX
, "No more differences found.");
3641 retval
= ERROR_FAIL
;
3642 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3643 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3644 "in %fs (%0.3f KiB/s)", image_size
,
3645 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3648 image_close(&image
);
3653 COMMAND_HANDLER(handle_verify_image_checksum_command
)
3655 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_CHECKSUM_ONLY
);
3658 COMMAND_HANDLER(handle_verify_image_command
)
3660 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_VERIFY
);
3663 COMMAND_HANDLER(handle_test_image_command
)
3665 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_TEST
);
3668 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
3670 struct target
*target
= get_current_target(cmd_ctx
);
3671 struct breakpoint
*breakpoint
= target
->breakpoints
;
3672 while (breakpoint
) {
3673 if (breakpoint
->type
== BKPT_SOFT
) {
3674 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3675 breakpoint
->length
, 16);
3676 command_print(cmd_ctx
, "IVA breakpoint: " TARGET_ADDR_FMT
", 0x%x, %i, 0x%s",
3677 breakpoint
->address
,
3679 breakpoint
->set
, buf
);
3682 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3683 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3685 breakpoint
->length
, breakpoint
->set
);
3686 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3687 command_print(cmd_ctx
, "Hybrid br