1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
17 * Copyright (C) 2011 by Broadcom Corporation *
18 * Evan Hunter - ehunter@broadcom.com *
20 * Copyright (C) ST-Ericsson SA 2011 *
21 * michel.jaouen@stericsson.com : smp minimum support *
23 * Copyright (C) 2011 Andreas Fritiofson *
24 * andreas.fritiofson@gmail.com *
26 * This program is free software; you can redistribute it and/or modify *
27 * it under the terms of the GNU General Public License as published by *
28 * the Free Software Foundation; either version 2 of the License, or *
29 * (at your option) any later version. *
31 * This program is distributed in the hope that it will be useful, *
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
34 * GNU General Public License for more details. *
36 * You should have received a copy of the GNU General Public License *
37 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
38 ***************************************************************************/
44 #include <helper/time_support.h>
45 #include <jtag/jtag.h>
46 #include <flash/nor/core.h>
49 #include "target_type.h"
50 #include "target_request.h"
51 #include "breakpoints.h"
55 #include "rtos/rtos.h"
56 #include "transport/transport.h"
58 /* default halt wait timeout (ms) */
59 #define DEFAULT_HALT_TIMEOUT 5000
61 static int target_read_buffer_default(struct target
*target
, target_addr_t address
,
62 uint32_t count
, uint8_t *buffer
);
63 static int target_write_buffer_default(struct target
*target
, target_addr_t address
,
64 uint32_t count
, const uint8_t *buffer
);
65 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
66 int argc
, Jim_Obj
* const *argv
);
67 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
68 int argc
, Jim_Obj
* const *argv
);
69 static int target_register_user_commands(struct command_context
*cmd_ctx
);
70 static int target_get_gdb_fileio_info_default(struct target
*target
,
71 struct gdb_fileio_info
*fileio_info
);
72 static int target_gdb_fileio_end_default(struct target
*target
, int retcode
,
73 int fileio_errno
, bool ctrl_c
);
74 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
75 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
);
78 extern struct target_type arm7tdmi_target
;
79 extern struct target_type arm720t_target
;
80 extern struct target_type arm9tdmi_target
;
81 extern struct target_type arm920t_target
;
82 extern struct target_type arm966e_target
;
83 extern struct target_type arm946e_target
;
84 extern struct target_type arm926ejs_target
;
85 extern struct target_type fa526_target
;
86 extern struct target_type feroceon_target
;
87 extern struct target_type dragonite_target
;
88 extern struct target_type xscale_target
;
89 extern struct target_type cortexm_target
;
90 extern struct target_type cortexa_target
;
91 extern struct target_type aarch64_target
;
92 extern struct target_type cortexr4_target
;
93 extern struct target_type arm11_target
;
94 extern struct target_type ls1_sap_target
;
95 extern struct target_type mips_m4k_target
;
96 extern struct target_type avr_target
;
97 extern struct target_type dsp563xx_target
;
98 extern struct target_type dsp5680xx_target
;
99 extern struct target_type testee_target
;
100 extern struct target_type avr32_ap7k_target
;
101 extern struct target_type hla_target
;
102 extern struct target_type nds32_v2_target
;
103 extern struct target_type nds32_v3_target
;
104 extern struct target_type nds32_v3m_target
;
105 extern struct target_type or1k_target
;
106 extern struct target_type quark_x10xx_target
;
107 extern struct target_type quark_d20xx_target
;
108 extern struct target_type stm8_target
;
110 static struct target_type
*target_types
[] = {
147 struct target
*all_targets
;
148 static struct target_event_callback
*target_event_callbacks
;
149 static struct target_timer_callback
*target_timer_callbacks
;
150 LIST_HEAD(target_reset_callback_list
);
151 LIST_HEAD(target_trace_callback_list
);
152 static const int polling_interval
= 100;
154 static const Jim_Nvp nvp_assert
[] = {
155 { .name
= "assert", NVP_ASSERT
},
156 { .name
= "deassert", NVP_DEASSERT
},
157 { .name
= "T", NVP_ASSERT
},
158 { .name
= "F", NVP_DEASSERT
},
159 { .name
= "t", NVP_ASSERT
},
160 { .name
= "f", NVP_DEASSERT
},
161 { .name
= NULL
, .value
= -1 }
164 static const Jim_Nvp nvp_error_target
[] = {
165 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
166 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
167 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
168 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
169 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
170 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
171 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
172 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
173 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
174 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
175 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
176 { .value
= -1, .name
= NULL
}
179 static const char *target_strerror_safe(int err
)
183 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
190 static const Jim_Nvp nvp_target_event
[] = {
192 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
193 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
194 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
195 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
196 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
198 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
199 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
201 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
202 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
203 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
204 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
205 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
206 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
207 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
208 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
210 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
211 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
213 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
214 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
216 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
217 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
219 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
220 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
222 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
223 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
225 { .value
= TARGET_EVENT_TRACE_CONFIG
, .name
= "trace-config" },
227 { .name
= NULL
, .value
= -1 }
230 static const Jim_Nvp nvp_target_state
[] = {
231 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
232 { .name
= "running", .value
= TARGET_RUNNING
},
233 { .name
= "halted", .value
= TARGET_HALTED
},
234 { .name
= "reset", .value
= TARGET_RESET
},
235 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
236 { .name
= NULL
, .value
= -1 },
239 static const Jim_Nvp nvp_target_debug_reason
[] = {
240 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
241 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
242 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
243 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
244 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
245 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
246 { .name
= "program-exit" , .value
= DBG_REASON_EXIT
},
247 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
248 { .name
= NULL
, .value
= -1 },
251 static const Jim_Nvp nvp_target_endian
[] = {
252 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
253 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
254 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
255 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
256 { .name
= NULL
, .value
= -1 },
259 static const Jim_Nvp nvp_reset_modes
[] = {
260 { .name
= "unknown", .value
= RESET_UNKNOWN
},
261 { .name
= "run" , .value
= RESET_RUN
},
262 { .name
= "halt" , .value
= RESET_HALT
},
263 { .name
= "init" , .value
= RESET_INIT
},
264 { .name
= NULL
, .value
= -1 },
267 const char *debug_reason_name(struct target
*t
)
271 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
272 t
->debug_reason
)->name
;
274 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
275 cp
= "(*BUG*unknown*BUG*)";
280 const char *target_state_name(struct target
*t
)
283 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
285 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
286 cp
= "(*BUG*unknown*BUG*)";
289 if (!target_was_examined(t
) && t
->defer_examine
)
290 cp
= "examine deferred";
295 const char *target_event_name(enum target_event event
)
298 cp
= Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
;
300 LOG_ERROR("Invalid target event: %d", (int)(event
));
301 cp
= "(*BUG*unknown*BUG*)";
306 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
309 cp
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
;
311 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
312 cp
= "(*BUG*unknown*BUG*)";
317 /* determine the number of the new target */
318 static int new_target_number(void)
323 /* number is 0 based */
327 if (x
< t
->target_number
)
328 x
= t
->target_number
;
334 /* read a uint64_t from a buffer in target memory endianness */
335 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
337 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
338 return le_to_h_u64(buffer
);
340 return be_to_h_u64(buffer
);
343 /* read a uint32_t from a buffer in target memory endianness */
344 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
346 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
347 return le_to_h_u32(buffer
);
349 return be_to_h_u32(buffer
);
352 /* read a uint24_t from a buffer in target memory endianness */
353 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
355 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
356 return le_to_h_u24(buffer
);
358 return be_to_h_u24(buffer
);
361 /* read a uint16_t from a buffer in target memory endianness */
362 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
364 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
365 return le_to_h_u16(buffer
);
367 return be_to_h_u16(buffer
);
370 /* read a uint8_t from a buffer in target memory endianness */
371 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
373 return *buffer
& 0x0ff;
376 /* write a uint64_t to a buffer in target memory endianness */
377 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
379 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
380 h_u64_to_le(buffer
, value
);
382 h_u64_to_be(buffer
, value
);
385 /* write a uint32_t to a buffer in target memory endianness */
386 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
388 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
389 h_u32_to_le(buffer
, value
);
391 h_u32_to_be(buffer
, value
);
394 /* write a uint24_t to a buffer in target memory endianness */
395 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
397 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
398 h_u24_to_le(buffer
, value
);
400 h_u24_to_be(buffer
, value
);
403 /* write a uint16_t to a buffer in target memory endianness */
404 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
406 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
407 h_u16_to_le(buffer
, value
);
409 h_u16_to_be(buffer
, value
);
412 /* write a uint8_t to a buffer in target memory endianness */
413 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
418 /* write a uint64_t array to a buffer in target memory endianness */
419 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
422 for (i
= 0; i
< count
; i
++)
423 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
426 /* write a uint32_t array to a buffer in target memory endianness */
427 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
430 for (i
= 0; i
< count
; i
++)
431 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
434 /* write a uint16_t array to a buffer in target memory endianness */
435 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
438 for (i
= 0; i
< count
; i
++)
439 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
442 /* write a uint64_t array to a buffer in target memory endianness */
443 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
446 for (i
= 0; i
< count
; i
++)
447 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
450 /* write a uint32_t array to a buffer in target memory endianness */
451 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
454 for (i
= 0; i
< count
; i
++)
455 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
458 /* write a uint16_t array to a buffer in target memory endianness */
459 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
462 for (i
= 0; i
< count
; i
++)
463 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
466 /* return a pointer to a configured target; id is name or number */
467 struct target
*get_target(const char *id
)
469 struct target
*target
;
471 /* try as tcltarget name */
472 for (target
= all_targets
; target
; target
= target
->next
) {
473 if (target_name(target
) == NULL
)
475 if (strcmp(id
, target_name(target
)) == 0)
479 /* It's OK to remove this fallback sometime after August 2010 or so */
481 /* no match, try as number */
483 if (parse_uint(id
, &num
) != ERROR_OK
)
486 for (target
= all_targets
; target
; target
= target
->next
) {
487 if (target
->target_number
== (int)num
) {
488 LOG_WARNING("use '%s' as target identifier, not '%u'",
489 target_name(target
), num
);
497 /* returns a pointer to the n-th configured target */
498 struct target
*get_target_by_num(int num
)
500 struct target
*target
= all_targets
;
503 if (target
->target_number
== num
)
505 target
= target
->next
;
511 struct target
*get_current_target(struct command_context
*cmd_ctx
)
513 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
515 if (target
== NULL
) {
516 LOG_ERROR("BUG: current_target out of bounds");
523 int target_poll(struct target
*target
)
527 /* We can't poll until after examine */
528 if (!target_was_examined(target
)) {
529 /* Fail silently lest we pollute the log */
533 retval
= target
->type
->poll(target
);
534 if (retval
!= ERROR_OK
)
537 if (target
->halt_issued
) {
538 if (target
->state
== TARGET_HALTED
)
539 target
->halt_issued
= false;
541 int64_t t
= timeval_ms() - target
->halt_issued_time
;
542 if (t
> DEFAULT_HALT_TIMEOUT
) {
543 target
->halt_issued
= false;
544 LOG_INFO("Halt timed out, wake up GDB.");
545 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
553 int target_halt(struct target
*target
)
556 /* We can't poll until after examine */
557 if (!target_was_examined(target
)) {
558 LOG_ERROR("Target not examined yet");
562 retval
= target
->type
->halt(target
);
563 if (retval
!= ERROR_OK
)
566 target
->halt_issued
= true;
567 target
->halt_issued_time
= timeval_ms();
573 * Make the target (re)start executing using its saved execution
574 * context (possibly with some modifications).
576 * @param target Which target should start executing.
577 * @param current True to use the target's saved program counter instead
578 * of the address parameter
579 * @param address Optionally used as the program counter.
580 * @param handle_breakpoints True iff breakpoints at the resumption PC
581 * should be skipped. (For example, maybe execution was stopped by
582 * such a breakpoint, in which case it would be counterprodutive to
584 * @param debug_execution False if all working areas allocated by OpenOCD
585 * should be released and/or restored to their original contents.
586 * (This would for example be true to run some downloaded "helper"
587 * algorithm code, which resides in one such working buffer and uses
588 * another for data storage.)
590 * @todo Resolve the ambiguity about what the "debug_execution" flag
591 * signifies. For example, Target implementations don't agree on how
592 * it relates to invalidation of the register cache, or to whether
593 * breakpoints and watchpoints should be enabled. (It would seem wrong
594 * to enable breakpoints when running downloaded "helper" algorithms
595 * (debug_execution true), since the breakpoints would be set to match
596 * target firmware being debugged, not the helper algorithm.... and
597 * enabling them could cause such helpers to malfunction (for example,
598 * by overwriting data with a breakpoint instruction. On the other
599 * hand the infrastructure for running such helpers might use this
600 * procedure but rely on hardware breakpoint to detect termination.)
602 int target_resume(struct target
*target
, int current
, target_addr_t address
,
603 int handle_breakpoints
, int debug_execution
)
607 /* We can't poll until after examine */
608 if (!target_was_examined(target
)) {
609 LOG_ERROR("Target not examined yet");
613 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
615 /* note that resume *must* be asynchronous. The CPU can halt before
616 * we poll. The CPU can even halt at the current PC as a result of
617 * a software breakpoint being inserted by (a bug?) the application.
619 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
620 if (retval
!= ERROR_OK
)
623 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
628 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
633 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
634 if (n
->name
== NULL
) {
635 LOG_ERROR("invalid reset mode");
639 struct target
*target
;
640 for (target
= all_targets
; target
; target
= target
->next
)
641 target_call_reset_callbacks(target
, reset_mode
);
643 /* disable polling during reset to make reset event scripts
644 * more predictable, i.e. dr/irscan & pathmove in events will
645 * not have JTAG operations injected into the middle of a sequence.
647 bool save_poll
= jtag_poll_get_enabled();
649 jtag_poll_set_enabled(false);
651 sprintf(buf
, "ocd_process_reset %s", n
->name
);
652 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
654 jtag_poll_set_enabled(save_poll
);
656 if (retval
!= JIM_OK
) {
657 Jim_MakeErrorMessage(cmd_ctx
->interp
);
658 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
662 /* We want any events to be processed before the prompt */
663 retval
= target_call_timer_callbacks_now();
665 for (target
= all_targets
; target
; target
= target
->next
) {
666 target
->type
->check_reset(target
);
667 target
->running_alg
= false;
673 static int identity_virt2phys(struct target
*target
,
674 target_addr_t
virtual, target_addr_t
*physical
)
680 static int no_mmu(struct target
*target
, int *enabled
)
686 static int default_examine(struct target
*target
)
688 target_set_examined(target
);
692 /* no check by default */
693 static int default_check_reset(struct target
*target
)
698 int target_examine_one(struct target
*target
)
700 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
702 int retval
= target
->type
->examine(target
);
703 if (retval
!= ERROR_OK
)
706 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
711 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
713 struct target
*target
= priv
;
715 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
718 jtag_unregister_event_callback(jtag_enable_callback
, target
);
720 return target_examine_one(target
);
723 /* Targets that correctly implement init + examine, i.e.
724 * no communication with target during init:
728 int target_examine(void)
730 int retval
= ERROR_OK
;
731 struct target
*target
;
733 for (target
= all_targets
; target
; target
= target
->next
) {
734 /* defer examination, but don't skip it */
735 if (!target
->tap
->enabled
) {
736 jtag_register_event_callback(jtag_enable_callback
,
741 if (target
->defer_examine
)
744 retval
= target_examine_one(target
);
745 if (retval
!= ERROR_OK
)
751 const char *target_type_name(struct target
*target
)
753 return target
->type
->name
;
756 static int target_soft_reset_halt(struct target
*target
)
758 if (!target_was_examined(target
)) {
759 LOG_ERROR("Target not examined yet");
762 if (!target
->type
->soft_reset_halt
) {
763 LOG_ERROR("Target %s does not support soft_reset_halt",
764 target_name(target
));
767 return target
->type
->soft_reset_halt(target
);
771 * Downloads a target-specific native code algorithm to the target,
772 * and executes it. * Note that some targets may need to set up, enable,
773 * and tear down a breakpoint (hard or * soft) to detect algorithm
774 * termination, while others may support lower overhead schemes where
775 * soft breakpoints embedded in the algorithm automatically terminate the
778 * @param target used to run the algorithm
779 * @param arch_info target-specific description of the algorithm.
781 int target_run_algorithm(struct target
*target
,
782 int num_mem_params
, struct mem_param
*mem_params
,
783 int num_reg_params
, struct reg_param
*reg_param
,
784 uint32_t entry_point
, uint32_t exit_point
,
785 int timeout_ms
, void *arch_info
)
787 int retval
= ERROR_FAIL
;
789 if (!target_was_examined(target
)) {
790 LOG_ERROR("Target not examined yet");
793 if (!target
->type
->run_algorithm
) {
794 LOG_ERROR("Target type '%s' does not support %s",
795 target_type_name(target
), __func__
);
799 target
->running_alg
= true;
800 retval
= target
->type
->run_algorithm(target
,
801 num_mem_params
, mem_params
,
802 num_reg_params
, reg_param
,
803 entry_point
, exit_point
, timeout_ms
, arch_info
);
804 target
->running_alg
= false;
811 * Downloads a target-specific native code algorithm to the target,
812 * executes and leaves it running.
814 * @param target used to run the algorithm
815 * @param arch_info target-specific description of the algorithm.
817 int target_start_algorithm(struct target
*target
,
818 int num_mem_params
, struct mem_param
*mem_params
,
819 int num_reg_params
, struct reg_param
*reg_params
,
820 uint32_t entry_point
, uint32_t exit_point
,
823 int retval
= ERROR_FAIL
;
825 if (!target_was_examined(target
)) {
826 LOG_ERROR("Target not examined yet");
829 if (!target
->type
->start_algorithm
) {
830 LOG_ERROR("Target type '%s' does not support %s",
831 target_type_name(target
), __func__
);
834 if (target
->running_alg
) {
835 LOG_ERROR("Target is already running an algorithm");
839 target
->running_alg
= true;
840 retval
= target
->type
->start_algorithm(target
,
841 num_mem_params
, mem_params
,
842 num_reg_params
, reg_params
,
843 entry_point
, exit_point
, arch_info
);
850 * Waits for an algorithm started with target_start_algorithm() to complete.
852 * @param target used to run the algorithm
853 * @param arch_info target-specific description of the algorithm.
855 int target_wait_algorithm(struct target
*target
,
856 int num_mem_params
, struct mem_param
*mem_params
,
857 int num_reg_params
, struct reg_param
*reg_params
,
858 uint32_t exit_point
, int timeout_ms
,
861 int retval
= ERROR_FAIL
;
863 if (!target
->type
->wait_algorithm
) {
864 LOG_ERROR("Target type '%s' does not support %s",
865 target_type_name(target
), __func__
);
868 if (!target
->running_alg
) {
869 LOG_ERROR("Target is not running an algorithm");
873 retval
= target
->type
->wait_algorithm(target
,
874 num_mem_params
, mem_params
,
875 num_reg_params
, reg_params
,
876 exit_point
, timeout_ms
, arch_info
);
877 if (retval
!= ERROR_TARGET_TIMEOUT
)
878 target
->running_alg
= false;
885 * Streams data to a circular buffer on target intended for consumption by code
886 * running asynchronously on target.
888 * This is intended for applications where target-specific native code runs
889 * on the target, receives data from the circular buffer, does something with
890 * it (most likely writing it to a flash memory), and advances the circular
893 * This assumes that the helper algorithm has already been loaded to the target,
894 * but has not been started yet. Given memory and register parameters are passed
897 * The buffer is defined by (buffer_start, buffer_size) arguments and has the
900 * [buffer_start + 0, buffer_start + 4):
901 * Write Pointer address (aka head). Written and updated by this
902 * routine when new data is written to the circular buffer.
903 * [buffer_start + 4, buffer_start + 8):
904 * Read Pointer address (aka tail). Updated by code running on the
905 * target after it consumes data.
906 * [buffer_start + 8, buffer_start + buffer_size):
907 * Circular buffer contents.
909 * See contrib/loaders/flash/stm32f1x.S for an example.
911 * @param target used to run the algorithm
912 * @param buffer address on the host where data to be sent is located
913 * @param count number of blocks to send
914 * @param block_size size in bytes of each block
915 * @param num_mem_params count of memory-based params to pass to algorithm
916 * @param mem_params memory-based params to pass to algorithm
917 * @param num_reg_params count of register-based params to pass to algorithm
918 * @param reg_params memory-based params to pass to algorithm
919 * @param buffer_start address on the target of the circular buffer structure
920 * @param buffer_size size of the circular buffer structure
921 * @param entry_point address on the target to execute to start the algorithm
922 * @param exit_point address at which to set a breakpoint to catch the
923 * end of the algorithm; can be 0 if target triggers a breakpoint itself
926 int target_run_flash_async_algorithm(struct target
*target
,
927 const uint8_t *buffer
, uint32_t count
, int block_size
,
928 int num_mem_params
, struct mem_param
*mem_params
,
929 int num_reg_params
, struct reg_param
*reg_params
,
930 uint32_t buffer_start
, uint32_t buffer_size
,
931 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
936 const uint8_t *buffer_orig
= buffer
;
938 /* Set up working area. First word is write pointer, second word is read pointer,
939 * rest is fifo data area. */
940 uint32_t wp_addr
= buffer_start
;
941 uint32_t rp_addr
= buffer_start
+ 4;
942 uint32_t fifo_start_addr
= buffer_start
+ 8;
943 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
945 uint32_t wp
= fifo_start_addr
;
946 uint32_t rp
= fifo_start_addr
;
948 /* validate block_size is 2^n */
949 assert(!block_size
|| !(block_size
& (block_size
- 1)));
951 retval
= target_write_u32(target
, wp_addr
, wp
);
952 if (retval
!= ERROR_OK
)
954 retval
= target_write_u32(target
, rp_addr
, rp
);
955 if (retval
!= ERROR_OK
)
958 /* Start up algorithm on target and let it idle while writing the first chunk */
959 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
960 num_reg_params
, reg_params
,
965 if (retval
!= ERROR_OK
) {
966 LOG_ERROR("error starting target flash write algorithm");
972 retval
= target_read_u32(target
, rp_addr
, &rp
);
973 if (retval
!= ERROR_OK
) {
974 LOG_ERROR("failed to get read pointer");
978 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
979 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
982 LOG_ERROR("flash write algorithm aborted by target");
983 retval
= ERROR_FLASH_OPERATION_FAILED
;
987 if (((rp
- fifo_start_addr
) & (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
988 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
992 /* Count the number of bytes available in the fifo without
993 * crossing the wrap around. Make sure to not fill it completely,
994 * because that would make wp == rp and that's the empty condition. */
995 uint32_t thisrun_bytes
;
997 thisrun_bytes
= rp
- wp
- block_size
;
998 else if (rp
> fifo_start_addr
)
999 thisrun_bytes
= fifo_end_addr
- wp
;
1001 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
1003 if (thisrun_bytes
== 0) {
1004 /* Throttle polling a bit if transfer is (much) faster than flash
1005 * programming. The exact delay shouldn't matter as long as it's
1006 * less than buffer size / flash speed. This is very unlikely to
1007 * run when using high latency connections such as USB. */
1010 /* to stop an infinite loop on some targets check and increment a timeout
1011 * this issue was observed on a stellaris using the new ICDI interface */
1012 if (timeout
++ >= 500) {
1013 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1014 return ERROR_FLASH_OPERATION_FAILED
;
1019 /* reset our timeout */
1022 /* Limit to the amount of data we actually want to write */
1023 if (thisrun_bytes
> count
* block_size
)
1024 thisrun_bytes
= count
* block_size
;
1026 /* Write data to fifo */
1027 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
1028 if (retval
!= ERROR_OK
)
1031 /* Update counters and wrap write pointer */
1032 buffer
+= thisrun_bytes
;
1033 count
-= thisrun_bytes
/ block_size
;
1034 wp
+= thisrun_bytes
;
1035 if (wp
>= fifo_end_addr
)
1036 wp
= fifo_start_addr
;
1038 /* Store updated write pointer to target */
1039 retval
= target_write_u32(target
, wp_addr
, wp
);
1040 if (retval
!= ERROR_OK
)
1044 if (retval
!= ERROR_OK
) {
1045 /* abort flash write algorithm on target */
1046 target_write_u32(target
, wp_addr
, 0);
1049 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1050 num_reg_params
, reg_params
,
1055 if (retval2
!= ERROR_OK
) {
1056 LOG_ERROR("error waiting for target flash write algorithm");
1060 if (retval
== ERROR_OK
) {
1061 /* check if algorithm set rp = 0 after fifo writer loop finished */
1062 retval
= target_read_u32(target
, rp_addr
, &rp
);
1063 if (retval
== ERROR_OK
&& rp
== 0) {
1064 LOG_ERROR("flash write algorithm aborted by target");
1065 retval
= ERROR_FLASH_OPERATION_FAILED
;
1072 int target_read_memory(struct target
*target
,
1073 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1075 if (!target_was_examined(target
)) {
1076 LOG_ERROR("Target not examined yet");
1079 if (!target
->type
->read_memory
) {
1080 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1083 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1086 int target_read_phys_memory(struct target
*target
,
1087 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1089 if (!target_was_examined(target
)) {
1090 LOG_ERROR("Target not examined yet");
1093 if (!target
->type
->read_phys_memory
) {
1094 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1097 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1100 int target_write_memory(struct target
*target
,
1101 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1103 if (!target_was_examined(target
)) {
1104 LOG_ERROR("Target not examined yet");
1107 if (!target
->type
->write_memory
) {
1108 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1111 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1114 int target_write_phys_memory(struct target
*target
,
1115 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1117 if (!target_was_examined(target
)) {
1118 LOG_ERROR("Target not examined yet");
1121 if (!target
->type
->write_phys_memory
) {
1122 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1125 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1128 int target_add_breakpoint(struct target
*target
,
1129 struct breakpoint
*breakpoint
)
1131 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1132 LOG_WARNING("target %s is not halted (add breakpoint)", target_name(target
));
1133 return ERROR_TARGET_NOT_HALTED
;
1135 return target
->type
->add_breakpoint(target
, breakpoint
);
1138 int target_add_context_breakpoint(struct target
*target
,
1139 struct breakpoint
*breakpoint
)
1141 if (target
->state
!= TARGET_HALTED
) {
1142 LOG_WARNING("target %s is not halted (add context breakpoint)", target_name(target
));
1143 return ERROR_TARGET_NOT_HALTED
;
1145 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1148 int target_add_hybrid_breakpoint(struct target
*target
,
1149 struct breakpoint
*breakpoint
)
1151 if (target
->state
!= TARGET_HALTED
) {
1152 LOG_WARNING("target %s is not halted (add hybrid breakpoint)", target_name(target
));
1153 return ERROR_TARGET_NOT_HALTED
;
1155 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1158 int target_remove_breakpoint(struct target
*target
,
1159 struct breakpoint
*breakpoint
)
1161 return target
->type
->remove_breakpoint(target
, breakpoint
);
1164 int target_add_watchpoint(struct target
*target
,
1165 struct watchpoint
*watchpoint
)
1167 if (target
->state
!= TARGET_HALTED
) {
1168 LOG_WARNING("target %s is not halted (add watchpoint)", target_name(target
));
1169 return ERROR_TARGET_NOT_HALTED
;
1171 return target
->type
->add_watchpoint(target
, watchpoint
);
1173 int target_remove_watchpoint(struct target
*target
,
1174 struct watchpoint
*watchpoint
)
1176 return target
->type
->remove_watchpoint(target
, watchpoint
);
1178 int target_hit_watchpoint(struct target
*target
,
1179 struct watchpoint
**hit_watchpoint
)
1181 if (target
->state
!= TARGET_HALTED
) {
1182 LOG_WARNING("target %s is not halted (hit watchpoint)", target
->cmd_name
);
1183 return ERROR_TARGET_NOT_HALTED
;
1186 if (target
->type
->hit_watchpoint
== NULL
) {
1187 /* For backward compatible, if hit_watchpoint is not implemented,
1188 * return ERROR_FAIL such that gdb_server will not take the nonsense
1193 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1196 int target_get_gdb_reg_list(struct target
*target
,
1197 struct reg
**reg_list
[], int *reg_list_size
,
1198 enum target_register_class reg_class
)
1200 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1202 int target_step(struct target
*target
,
1203 int current
, target_addr_t address
, int handle_breakpoints
)
1205 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1208 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1210 if (target
->state
!= TARGET_HALTED
) {
1211 LOG_WARNING("target %s is not halted (gdb fileio)", target
->cmd_name
);
1212 return ERROR_TARGET_NOT_HALTED
;
1214 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1217 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1219 if (target
->state
!= TARGET_HALTED
) {
1220 LOG_WARNING("target %s is not halted (gdb fileio end)", target
->cmd_name
);
1221 return ERROR_TARGET_NOT_HALTED
;
1223 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1226 int target_profiling(struct target
*target
, uint32_t *samples
,
1227 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1229 if (target
->state
!= TARGET_HALTED
) {
1230 LOG_WARNING("target %s is not halted (profiling)", target
->cmd_name
);
1231 return ERROR_TARGET_NOT_HALTED
;
1233 return target
->type
->profiling(target
, samples
, max_num_samples
,
1234 num_samples
, seconds
);
1238 * Reset the @c examined flag for the given target.
1239 * Pure paranoia -- targets are zeroed on allocation.
1241 static void target_reset_examined(struct target
*target
)
1243 target
->examined
= false;
1246 static int handle_target(void *priv
);
1248 static int target_init_one(struct command_context
*cmd_ctx
,
1249 struct target
*target
)
1251 target_reset_examined(target
);
1253 struct target_type
*type
= target
->type
;
1254 if (type
->examine
== NULL
)
1255 type
->examine
= default_examine
;
1257 if (type
->check_reset
== NULL
)
1258 type
->check_reset
= default_check_reset
;
1260 assert(type
->init_target
!= NULL
);
1262 int retval
= type
->init_target(cmd_ctx
, target
);
1263 if (ERROR_OK
!= retval
) {
1264 LOG_ERROR("target '%s' init failed", target_name(target
));
1268 /* Sanity-check MMU support ... stub in what we must, to help
1269 * implement it in stages, but warn if we need to do so.
1272 if (type
->virt2phys
== NULL
) {
1273 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1274 type
->virt2phys
= identity_virt2phys
;
1277 /* Make sure no-MMU targets all behave the same: make no
1278 * distinction between physical and virtual addresses, and
1279 * ensure that virt2phys() is always an identity mapping.
1281 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1282 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1285 type
->write_phys_memory
= type
->write_memory
;
1286 type
->read_phys_memory
= type
->read_memory
;
1287 type
->virt2phys
= identity_virt2phys
;
1290 if (target
->type
->read_buffer
== NULL
)
1291 target
->type
->read_buffer
= target_read_buffer_default
;
1293 if (target
->type
->write_buffer
== NULL
)
1294 target
->type
->write_buffer
= target_write_buffer_default
;
1296 if (target
->type
->get_gdb_fileio_info
== NULL
)
1297 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1299 if (target
->type
->gdb_fileio_end
== NULL
)
1300 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1302 if (target
->type
->profiling
== NULL
)
1303 target
->type
->profiling
= target_profiling_default
;
1308 static int target_init(struct command_context
*cmd_ctx
)
1310 struct target
*target
;
1313 for (target
= all_targets
; target
; target
= target
->next
) {
1314 retval
= target_init_one(cmd_ctx
, target
);
1315 if (ERROR_OK
!= retval
)
1322 retval
= target_register_user_commands(cmd_ctx
);
1323 if (ERROR_OK
!= retval
)
1326 retval
= target_register_timer_callback(&handle_target
,
1327 polling_interval
, 1, cmd_ctx
->interp
);
1328 if (ERROR_OK
!= retval
)
1334 COMMAND_HANDLER(handle_target_init_command
)
1339 return ERROR_COMMAND_SYNTAX_ERROR
;
1341 static bool target_initialized
;
1342 if (target_initialized
) {
1343 LOG_INFO("'target init' has already been called");
1346 target_initialized
= true;
1348 retval
= command_run_line(CMD_CTX
, "init_targets");
1349 if (ERROR_OK
!= retval
)
1352 retval
= command_run_line(CMD_CTX
, "init_target_events");
1353 if (ERROR_OK
!= retval
)
1356 retval
= command_run_line(CMD_CTX
, "init_board");
1357 if (ERROR_OK
!= retval
)
1360 LOG_DEBUG("Initializing targets...");
1361 return target_init(CMD_CTX
);
1364 int target_register_event_callback(int (*callback
)(struct target
*target
,
1365 enum target_event event
, void *priv
), void *priv
)
1367 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1369 if (callback
== NULL
)
1370 return ERROR_COMMAND_SYNTAX_ERROR
;
1373 while ((*callbacks_p
)->next
)
1374 callbacks_p
= &((*callbacks_p
)->next
);
1375 callbacks_p
= &((*callbacks_p
)->next
);
1378 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1379 (*callbacks_p
)->callback
= callback
;
1380 (*callbacks_p
)->priv
= priv
;
1381 (*callbacks_p
)->next
= NULL
;
1386 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1387 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1389 struct target_reset_callback
*entry
;
1391 if (callback
== NULL
)
1392 return ERROR_COMMAND_SYNTAX_ERROR
;
1394 entry
= malloc(sizeof(struct target_reset_callback
));
1395 if (entry
== NULL
) {
1396 LOG_ERROR("error allocating buffer for reset callback entry");
1397 return ERROR_COMMAND_SYNTAX_ERROR
;
1400 entry
->callback
= callback
;
1402 list_add(&entry
->list
, &target_reset_callback_list
);
1408 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1409 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1411 struct target_trace_callback
*entry
;
1413 if (callback
== NULL
)
1414 return ERROR_COMMAND_SYNTAX_ERROR
;
1416 entry
= malloc(sizeof(struct target_trace_callback
));
1417 if (entry
== NULL
) {
1418 LOG_ERROR("error allocating buffer for trace callback entry");
1419 return ERROR_COMMAND_SYNTAX_ERROR
;
1422 entry
->callback
= callback
;
1424 list_add(&entry
->list
, &target_trace_callback_list
);
1430 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1432 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1434 if (callback
== NULL
)
1435 return ERROR_COMMAND_SYNTAX_ERROR
;
1438 while ((*callbacks_p
)->next
)
1439 callbacks_p
= &((*callbacks_p
)->next
);
1440 callbacks_p
= &((*callbacks_p
)->next
);
1443 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1444 (*callbacks_p
)->callback
= callback
;
1445 (*callbacks_p
)->periodic
= periodic
;
1446 (*callbacks_p
)->time_ms
= time_ms
;
1447 (*callbacks_p
)->removed
= false;
1449 gettimeofday(&(*callbacks_p
)->when
, NULL
);
1450 timeval_add_time(&(*callbacks_p
)->when
, 0, time_ms
* 1000);
1452 (*callbacks_p
)->priv
= priv
;
1453 (*callbacks_p
)->next
= NULL
;
1458 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1459 enum target_event event
, void *priv
), void *priv
)
1461 struct target_event_callback
**p
= &target_event_callbacks
;
1462 struct target_event_callback
*c
= target_event_callbacks
;
1464 if (callback
== NULL
)
1465 return ERROR_COMMAND_SYNTAX_ERROR
;
1468 struct target_event_callback
*next
= c
->next
;
1469 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1481 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1482 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1484 struct target_reset_callback
*entry
;
1486 if (callback
== NULL
)
1487 return ERROR_COMMAND_SYNTAX_ERROR
;
1489 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1490 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1491 list_del(&entry
->list
);
1500 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1501 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1503 struct target_trace_callback
*entry
;
1505 if (callback
== NULL
)
1506 return ERROR_COMMAND_SYNTAX_ERROR
;
1508 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1509 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1510 list_del(&entry
->list
);
1519 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1521 if (callback
== NULL
)
1522 return ERROR_COMMAND_SYNTAX_ERROR
;
1524 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1526 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1535 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1537 struct target_event_callback
*callback
= target_event_callbacks
;
1538 struct target_event_callback
*next_callback
;
1540 if (event
== TARGET_EVENT_HALTED
) {
1541 /* execute early halted first */
1542 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1545 LOG_DEBUG("target event %i (%s)", event
,
1546 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1548 target_handle_event(target
, event
);
1551 next_callback
= callback
->next
;
1552 callback
->callback(target
, event
, callback
->priv
);
1553 callback
= next_callback
;
1559 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1561 struct target_reset_callback
*callback
;
1563 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1564 Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1566 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1567 callback
->callback(target
, reset_mode
, callback
->priv
);
1572 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1574 struct target_trace_callback
*callback
;
1576 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1577 callback
->callback(target
, len
, data
, callback
->priv
);
1582 static int target_timer_callback_periodic_restart(
1583 struct target_timer_callback
*cb
, struct timeval
*now
)
1586 timeval_add_time(&cb
->when
, 0, cb
->time_ms
* 1000L);
1590 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1591 struct timeval
*now
)
1593 cb
->callback(cb
->priv
);
1596 return target_timer_callback_periodic_restart(cb
, now
);
1598 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1601 static int target_call_timer_callbacks_check_time(int checktime
)
1603 static bool callback_processing
;
1605 /* Do not allow nesting */
1606 if (callback_processing
)
1609 callback_processing
= true;
1614 gettimeofday(&now
, NULL
);
1616 /* Store an address of the place containing a pointer to the
1617 * next item; initially, that's a standalone "root of the
1618 * list" variable. */
1619 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1621 if ((*callback
)->removed
) {
1622 struct target_timer_callback
*p
= *callback
;
1623 *callback
= (*callback
)->next
;
1628 bool call_it
= (*callback
)->callback
&&
1629 ((!checktime
&& (*callback
)->periodic
) ||
1630 timeval_compare(&now
, &(*callback
)->when
) >= 0);
1633 target_call_timer_callback(*callback
, &now
);
1635 callback
= &(*callback
)->next
;
1638 callback_processing
= false;
1642 int target_call_timer_callbacks(void)
1644 return target_call_timer_callbacks_check_time(1);
1647 /* invoke periodic callbacks immediately */
1648 int target_call_timer_callbacks_now(void)
1650 return target_call_timer_callbacks_check_time(0);
1653 /* Prints the working area layout for debug purposes */
1654 static void print_wa_layout(struct target
*target
)
1656 struct working_area
*c
= target
->working_areas
;
1659 LOG_DEBUG("%c%c " TARGET_ADDR_FMT
"-" TARGET_ADDR_FMT
" (%" PRIu32
" bytes)",
1660 c
->backup ?
'b' : ' ', c
->free ?
' ' : '*',
1661 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1666 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1667 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1669 assert(area
->free
); /* Shouldn't split an allocated area */
1670 assert(size
<= area
->size
); /* Caller should guarantee this */
1672 /* Split only if not already the right size */
1673 if (size
< area
->size
) {
1674 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1679 new_wa
->next
= area
->next
;
1680 new_wa
->size
= area
->size
- size
;
1681 new_wa
->address
= area
->address
+ size
;
1682 new_wa
->backup
= NULL
;
1683 new_wa
->user
= NULL
;
1684 new_wa
->free
= true;
1686 area
->next
= new_wa
;
1689 /* If backup memory was allocated to this area, it has the wrong size
1690 * now so free it and it will be reallocated if/when needed */
1693 area
->backup
= NULL
;
1698 /* Merge all adjacent free areas into one */
1699 static void target_merge_working_areas(struct target
*target
)
1701 struct working_area
*c
= target
->working_areas
;
1703 while (c
&& c
->next
) {
1704 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1706 /* Find two adjacent free areas */
1707 if (c
->free
&& c
->next
->free
) {
1708 /* Merge the last into the first */
1709 c
->size
+= c
->next
->size
;
1711 /* Remove the last */
1712 struct working_area
*to_be_freed
= c
->next
;
1713 c
->next
= c
->next
->next
;
1714 if (to_be_freed
->backup
)
1715 free(to_be_freed
->backup
);
1718 /* If backup memory was allocated to the remaining area, it's has
1719 * the wrong size now */
1730 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1732 /* Reevaluate working area address based on MMU state*/
1733 if (target
->working_areas
== NULL
) {
1737 retval
= target
->type
->mmu(target
, &enabled
);
1738 if (retval
!= ERROR_OK
)
1742 if (target
->working_area_phys_spec
) {
1743 LOG_DEBUG("MMU disabled, using physical "
1744 "address for working memory " TARGET_ADDR_FMT
,
1745 target
->working_area_phys
);
1746 target
->working_area
= target
->working_area_phys
;
1748 LOG_ERROR("No working memory available. "
1749 "Specify -work-area-phys to target.");
1750 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1753 if (target
->working_area_virt_spec
) {
1754 LOG_DEBUG("MMU enabled, using virtual "
1755 "address for working memory " TARGET_ADDR_FMT
,
1756 target
->working_area_virt
);
1757 target
->working_area
= target
->working_area_virt
;
1759 LOG_ERROR("No working memory available. "
1760 "Specify -work-area-virt to target.");
1761 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1765 /* Set up initial working area on first call */
1766 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1768 new_wa
->next
= NULL
;
1769 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1770 new_wa
->address
= target
->working_area
;
1771 new_wa
->backup
= NULL
;
1772 new_wa
->user
= NULL
;
1773 new_wa
->free
= true;
1776 target
->working_areas
= new_wa
;
1779 /* only allocate multiples of 4 byte */
1781 size
= (size
+ 3) & (~3UL);
1783 struct working_area
*c
= target
->working_areas
;
1785 /* Find the first large enough working area */
1787 if (c
->free
&& c
->size
>= size
)
1793 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1795 /* Split the working area into the requested size */
1796 target_split_working_area(c
, size
);
1798 LOG_DEBUG("allocated new working area of %" PRIu32
" bytes at address " TARGET_ADDR_FMT
,
1801 if (target
->backup_working_area
) {
1802 if (c
->backup
== NULL
) {
1803 c
->backup
= malloc(c
->size
);
1804 if (c
->backup
== NULL
)
1808 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1809 if (retval
!= ERROR_OK
)
1813 /* mark as used, and return the new (reused) area */
1820 print_wa_layout(target
);
1825 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1829 retval
= target_alloc_working_area_try(target
, size
, area
);
1830 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1831 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1836 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1838 int retval
= ERROR_OK
;
1840 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1841 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1842 if (retval
!= ERROR_OK
)
1843 LOG_ERROR("failed to restore %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1844 area
->size
, area
->address
);
1850 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1851 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1853 int retval
= ERROR_OK
;
1859 retval
= target_restore_working_area(target
, area
);
1860 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1861 if (retval
!= ERROR_OK
)
1867 LOG_DEBUG("freed %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1868 area
->size
, area
->address
);
1870 /* mark user pointer invalid */
1871 /* TODO: Is this really safe? It points to some previous caller's memory.
1872 * How could we know that the area pointer is still in that place and not
1873 * some other vital data? What's the purpose of this, anyway? */
1877 target_merge_working_areas(target
);
1879 print_wa_layout(target
);
1884 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1886 return target_free_working_area_restore(target
, area
, 1);
1889 static void target_destroy(struct target
*target
)
1891 if (target
->type
->deinit_target
)
1892 target
->type
->deinit_target(target
);
1895 free(target
->trace_info
);
1896 free(target
->cmd_name
);
1900 void target_quit(void)
1902 struct target_event_callback
*pe
= target_event_callbacks
;
1904 struct target_event_callback
*t
= pe
->next
;
1908 target_event_callbacks
= NULL
;
1910 struct target_timer_callback
*pt
= target_timer_callbacks
;
1912 struct target_timer_callback
*t
= pt
->next
;
1916 target_timer_callbacks
= NULL
;
1918 for (struct target
*target
= all_targets
; target
;) {
1922 target_destroy(target
);
1929 /* free resources and restore memory, if restoring memory fails,
1930 * free up resources anyway
1932 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1934 struct working_area
*c
= target
->working_areas
;
1936 LOG_DEBUG("freeing all working areas");
1938 /* Loop through all areas, restoring the allocated ones and marking them as free */
1942 target_restore_working_area(target
, c
);
1944 *c
->user
= NULL
; /* Same as above */
1950 /* Run a merge pass to combine all areas into one */
1951 target_merge_working_areas(target
);
1953 print_wa_layout(target
);
1956 void target_free_all_working_areas(struct target
*target
)
1958 target_free_all_working_areas_restore(target
, 1);
1961 /* Find the largest number of bytes that can be allocated */
1962 uint32_t target_get_working_area_avail(struct target
*target
)
1964 struct working_area
*c
= target
->working_areas
;
1965 uint32_t max_size
= 0;
1968 return target
->working_area_size
;
1971 if (c
->free
&& max_size
< c
->size
)
1980 int target_arch_state(struct target
*target
)
1983 if (target
== NULL
) {
1984 LOG_WARNING("No target has been configured");
1988 if (target
->state
!= TARGET_HALTED
)
1991 retval
= target
->type
->arch_state(target
);
1995 static int target_get_gdb_fileio_info_default(struct target
*target
,
1996 struct gdb_fileio_info
*fileio_info
)
1998 /* If target does not support semi-hosting function, target
1999 has no need to provide .get_gdb_fileio_info callback.
2000 It just return ERROR_FAIL and gdb_server will return "Txx"
2001 as target halted every time. */
2005 static int target_gdb_fileio_end_default(struct target
*target
,
2006 int retcode
, int fileio_errno
, bool ctrl_c
)
2011 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
2012 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
2014 struct timeval timeout
, now
;
2016 gettimeofday(&timeout
, NULL
);
2017 timeval_add_time(&timeout
, seconds
, 0);
2019 LOG_INFO("Starting profiling. Halting and resuming the"
2020 " target as often as we can...");
2022 uint32_t sample_count
= 0;
2023 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2024 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2026 int retval
= ERROR_OK
;
2028 target_poll(target
);
2029 if (target
->state
== TARGET_HALTED
) {
2030 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
2031 samples
[sample_count
++] = t
;
2032 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2033 retval
= target_resume(target
, 1, 0, 0, 0);
2034 target_poll(target
);
2035 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2036 } else if (target
->state
== TARGET_RUNNING
) {
2037 /* We want to quickly sample the PC. */
2038 retval
= target_halt(target
);
2040 LOG_INFO("Target not halted or running");
2045 if (retval
!= ERROR_OK
)
2048 gettimeofday(&now
, NULL
);
2049 if ((sample_count
>= max_num_samples
) || timeval_compare(&now
, &timeout
) >= 0) {
2050 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2055 *num_samples
= sample_count
;
2059 /* Single aligned words are guaranteed to use 16 or 32 bit access
2060 * mode respectively, otherwise data is handled as quickly as
2063 int target_write_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, const uint8_t *buffer
)
2065 LOG_DEBUG("writing buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2068 if (!target_was_examined(target
)) {
2069 LOG_ERROR("Target not examined yet");
2076 if ((address
+ size
- 1) < address
) {
2077 /* GDB can request this when e.g. PC is 0xfffffffc */
2078 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2084 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2087 static int target_write_buffer_default(struct target
*target
,
2088 target_addr_t address
, uint32_t count
, const uint8_t *buffer
)
2092 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2093 * will have something to do with the size we leave to it. */
2094 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2095 if (address
& size
) {
2096 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2097 if (retval
!= ERROR_OK
)
2105 /* Write the data with as large access size as possible. */
2106 for (; size
> 0; size
/= 2) {
2107 uint32_t aligned
= count
- count
% size
;
2109 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2110 if (retval
!= ERROR_OK
)
2121 /* Single aligned words are guaranteed to use 16 or 32 bit access
2122 * mode respectively, otherwise data is handled as quickly as
2125 int target_read_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
2127 LOG_DEBUG("reading buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2130 if (!target_was_examined(target
)) {
2131 LOG_ERROR("Target not examined yet");
2138 if ((address
+ size
- 1) < address
) {
2139 /* GDB can request this when e.g. PC is 0xfffffffc */
2140 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2146 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2149 static int target_read_buffer_default(struct target
*target
, target_addr_t address
, uint32_t count
, uint8_t *buffer
)
2153 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2154 * will have something to do with the size we leave to it. */
2155 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2156 if (address
& size
) {
2157 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2158 if (retval
!= ERROR_OK
)
2166 /* Read the data with as large access size as possible. */
2167 for (; size
> 0; size
/= 2) {
2168 uint32_t aligned
= count
- count
% size
;
2170 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2171 if (retval
!= ERROR_OK
)
2182 int target_checksum_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* crc
)
2187 uint32_t checksum
= 0;
2188 if (!target_was_examined(target
)) {
2189 LOG_ERROR("Target not examined yet");
2193 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2194 if (retval
!= ERROR_OK
) {
2195 buffer
= malloc(size
);
2196 if (buffer
== NULL
) {
2197 LOG_ERROR("error allocating buffer for section (%" PRId32
" bytes)", size
);
2198 return ERROR_COMMAND_SYNTAX_ERROR
;
2200 retval
= target_read_buffer(target
, address
, size
, buffer
);
2201 if (retval
!= ERROR_OK
) {
2206 /* convert to target endianness */
2207 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2208 uint32_t target_data
;
2209 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2210 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2213 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2222 int target_blank_check_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* blank
,
2223 uint8_t erased_value
)
2226 if (!target_was_examined(target
)) {
2227 LOG_ERROR("Target not examined yet");
2231 if (target
->type
->blank_check_memory
== 0)
2232 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2234 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
, erased_value
);
2239 int target_read_u64(struct target
*target
, target_addr_t address
, uint64_t *value
)
2241 uint8_t value_buf
[8];
2242 if (!target_was_examined(target
)) {
2243 LOG_ERROR("Target not examined yet");
2247 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2249 if (retval
== ERROR_OK
) {
2250 *value
= target_buffer_get_u64(target
, value_buf
);
2251 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2256 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2263 int target_read_u32(struct target
*target
, target_addr_t address
, uint32_t *value
)
2265 uint8_t value_buf
[4];
2266 if (!target_was_examined(target
)) {
2267 LOG_ERROR("Target not examined yet");
2271 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2273 if (retval
== ERROR_OK
) {
2274 *value
= target_buffer_get_u32(target
, value_buf
);
2275 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2280 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2287 int target_read_u16(struct target
*target
, target_addr_t address
, uint16_t *value
)
2289 uint8_t value_buf
[2];
2290 if (!target_was_examined(target
)) {
2291 LOG_ERROR("Target not examined yet");
2295 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2297 if (retval
== ERROR_OK
) {
2298 *value
= target_buffer_get_u16(target
, value_buf
);
2299 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%4.4" PRIx16
,
2304 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2311 int target_read_u8(struct target
*target
, target_addr_t address
, uint8_t *value
)
2313 if (!target_was_examined(target
)) {
2314 LOG_ERROR("Target not examined yet");
2318 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2320 if (retval
== ERROR_OK
) {
2321 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2326 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2333 int target_write_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2336 uint8_t value_buf
[8];
2337 if (!target_was_examined(target
)) {
2338 LOG_ERROR("Target not examined yet");
2342 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2346 target_buffer_set_u64(target
, value_buf
, value
);
2347 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2348 if (retval
!= ERROR_OK
)
2349 LOG_DEBUG("failed: %i", retval
);
2354 int target_write_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2357 uint8_t value_buf
[4];
2358 if (!target_was_examined(target
)) {
2359 LOG_ERROR("Target not examined yet");
2363 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2367 target_buffer_set_u32(target
, value_buf
, value
);
2368 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2369 if (retval
!= ERROR_OK
)
2370 LOG_DEBUG("failed: %i", retval
);
2375 int target_write_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2378 uint8_t value_buf
[2];
2379 if (!target_was_examined(target
)) {
2380 LOG_ERROR("Target not examined yet");
2384 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2388 target_buffer_set_u16(target
, value_buf
, value
);
2389 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2390 if (retval
!= ERROR_OK
)
2391 LOG_DEBUG("failed: %i", retval
);
2396 int target_write_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2399 if (!target_was_examined(target
)) {
2400 LOG_ERROR("Target not examined yet");
2404 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2407 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2408 if (retval
!= ERROR_OK
)
2409 LOG_DEBUG("failed: %i", retval
);
2414 int target_write_phys_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2417 uint8_t value_buf
[8];
2418 if (!target_was_examined(target
)) {
2419 LOG_ERROR("Target not examined yet");
2423 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2427 target_buffer_set_u64(target
, value_buf
, value
);
2428 retval
= target_write_phys_memory(target
, address
, 8, 1, value_buf
);
2429 if (retval
!= ERROR_OK
)
2430 LOG_DEBUG("failed: %i", retval
);
2435 int target_write_phys_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2438 uint8_t value_buf
[4];
2439 if (!target_was_examined(target
)) {
2440 LOG_ERROR("Target not examined yet");
2444 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2448 target_buffer_set_u32(target
, value_buf
, value
);
2449 retval
= target_write_phys_memory(target
, address
, 4, 1, value_buf
);
2450 if (retval
!= ERROR_OK
)
2451 LOG_DEBUG("failed: %i", retval
);
2456 int target_write_phys_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2459 uint8_t value_buf
[2];
2460 if (!target_was_examined(target
)) {
2461 LOG_ERROR("Target not examined yet");
2465 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2469 target_buffer_set_u16(target
, value_buf
, value
);
2470 retval
= target_write_phys_memory(target
, address
, 2, 1, value_buf
);
2471 if (retval
!= ERROR_OK
)
2472 LOG_DEBUG("failed: %i", retval
);
2477 int target_write_phys_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2480 if (!target_was_examined(target
)) {
2481 LOG_ERROR("Target not examined yet");
2485 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2488 retval
= target_write_phys_memory(target
, address
, 1, 1, &value
);
2489 if (retval
!= ERROR_OK
)
2490 LOG_DEBUG("failed: %i", retval
);
2495 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2497 struct target
*target
= get_target(name
);
2498 if (target
== NULL
) {
2499 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2502 if (!target
->tap
->enabled
) {
2503 LOG_USER("Target: TAP %s is disabled, "
2504 "can't be the current target\n",
2505 target
->tap
->dotted_name
);
2509 cmd_ctx
->current_target
= target
->target_number
;
2514 COMMAND_HANDLER(handle_targets_command
)
2516 int retval
= ERROR_OK
;
2517 if (CMD_ARGC
== 1) {
2518 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2519 if (retval
== ERROR_OK
) {
2525 struct target
*target
= all_targets
;
2526 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2527 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2532 if (target
->tap
->enabled
)
2533 state
= target_state_name(target
);
2535 state
= "tap-disabled";
2537 if (CMD_CTX
->current_target
== target
->target_number
)
2540 /* keep columns lined up to match the headers above */
2541 command_print(CMD_CTX
,
2542 "%2d%c %-18s %-10s %-6s %-18s %s",
2543 target
->target_number
,
2545 target_name(target
),
2546 target_type_name(target
),
2547 Jim_Nvp_value2name_simple(nvp_target_endian
,
2548 target
->endianness
)->name
,
2549 target
->tap
->dotted_name
,
2551 target
= target
->next
;
2557 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2559 static int powerDropout
;
2560 static int srstAsserted
;
2562 static int runPowerRestore
;
2563 static int runPowerDropout
;
2564 static int runSrstAsserted
;
2565 static int runSrstDeasserted
;
2567 static int sense_handler(void)
2569 static int prevSrstAsserted
;
2570 static int prevPowerdropout
;
2572 int retval
= jtag_power_dropout(&powerDropout
);
2573 if (retval
!= ERROR_OK
)
2577 powerRestored
= prevPowerdropout
&& !powerDropout
;
2579 runPowerRestore
= 1;
2581 int64_t current
= timeval_ms();
2582 static int64_t lastPower
;
2583 bool waitMore
= lastPower
+ 2000 > current
;
2584 if (powerDropout
&& !waitMore
) {
2585 runPowerDropout
= 1;
2586 lastPower
= current
;
2589 retval
= jtag_srst_asserted(&srstAsserted
);
2590 if (retval
!= ERROR_OK
)
2594 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2596 static int64_t lastSrst
;
2597 waitMore
= lastSrst
+ 2000 > current
;
2598 if (srstDeasserted
&& !waitMore
) {
2599 runSrstDeasserted
= 1;
2603 if (!prevSrstAsserted
&& srstAsserted
)
2604 runSrstAsserted
= 1;
2606 prevSrstAsserted
= srstAsserted
;
2607 prevPowerdropout
= powerDropout
;
2609 if (srstDeasserted
|| powerRestored
) {
2610 /* Other than logging the event we can't do anything here.
2611 * Issuing a reset is a particularly bad idea as we might
2612 * be inside a reset already.
2619 /* process target state changes */
2620 static int handle_target(void *priv
)
2622 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2623 int retval
= ERROR_OK
;
2625 if (!is_jtag_poll_safe()) {
2626 /* polling is disabled currently */
2630 /* we do not want to recurse here... */
2631 static int recursive
;
2635 /* danger! running these procedures can trigger srst assertions and power dropouts.
2636 * We need to avoid an infinite loop/recursion here and we do that by
2637 * clearing the flags after running these events.
2639 int did_something
= 0;
2640 if (runSrstAsserted
) {
2641 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2642 Jim_Eval(interp
, "srst_asserted");
2645 if (runSrstDeasserted
) {
2646 Jim_Eval(interp
, "srst_deasserted");
2649 if (runPowerDropout
) {
2650 LOG_INFO("Power dropout detected, running power_dropout proc.");
2651 Jim_Eval(interp
, "power_dropout");
2654 if (runPowerRestore
) {
2655 Jim_Eval(interp
, "power_restore");
2659 if (did_something
) {
2660 /* clear detect flags */
2664 /* clear action flags */
2666 runSrstAsserted
= 0;
2667 runSrstDeasserted
= 0;
2668 runPowerRestore
= 0;
2669 runPowerDropout
= 0;
2674 /* Poll targets for state changes unless that's globally disabled.
2675 * Skip targets that are currently disabled.
2677 for (struct target
*target
= all_targets
;
2678 is_jtag_poll_safe() && target
;
2679 target
= target
->next
) {
2681 if (!target_was_examined(target
))
2684 if (!target
->tap
->enabled
)
2687 if (target
->backoff
.times
> target
->backoff
.count
) {
2688 /* do not poll this time as we failed previously */
2689 target
->backoff
.count
++;
2692 target
->backoff
.count
= 0;
2694 /* only poll target if we've got power and srst isn't asserted */
2695 if (!powerDropout
&& !srstAsserted
) {
2696 /* polling may fail silently until the target has been examined */
2697 retval
= target_poll(target
);
2698 if (retval
!= ERROR_OK
) {
2699 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2700 if (target
->backoff
.times
* polling_interval
< 5000) {
2701 target
->backoff
.times
*= 2;
2702 target
->backoff
.times
++;
2705 /* Tell GDB to halt the debugger. This allows the user to
2706 * run monitor commands to handle the situation.
2708 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2710 if (target
->backoff
.times
> 0) {
2711 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
2712 target_reset_examined(target
);
2713 retval
= target_examine_one(target
);
2714 /* Target examination could have failed due to unstable connection,
2715 * but we set the examined flag anyway to repoll it later */
2716 if (retval
!= ERROR_OK
) {
2717 target
->examined
= true;
2718 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2719 target
->backoff
.times
* polling_interval
);
2724 /* Since we succeeded, we reset backoff count */
2725 target
->backoff
.times
= 0;
2732 COMMAND_HANDLER(handle_reg_command
)
2734 struct target
*target
;
2735 struct reg
*reg
= NULL
;
2741 target
= get_current_target(CMD_CTX
);
2743 /* list all available registers for the current target */
2744 if (CMD_ARGC
== 0) {
2745 struct reg_cache
*cache
= target
->reg_cache
;
2751 command_print(CMD_CTX
, "===== %s", cache
->name
);
2753 for (i
= 0, reg
= cache
->reg_list
;
2754 i
< cache
->num_regs
;
2755 i
++, reg
++, count
++) {
2756 /* only print cached values if they are valid */
2758 value
= buf_to_str(reg
->value
,
2760 command_print(CMD_CTX
,
2761 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2769 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2774 cache
= cache
->next
;
2780 /* access a single register by its ordinal number */
2781 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2783 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2785 struct reg_cache
*cache
= target
->reg_cache
;
2789 for (i
= 0; i
< cache
->num_regs
; i
++) {
2790 if (count
++ == num
) {
2791 reg
= &cache
->reg_list
[i
];
2797 cache
= cache
->next
;
2801 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2802 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2806 /* access a single register by its name */
2807 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2810 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2815 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2817 /* display a register */
2818 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2819 && (CMD_ARGV
[1][0] <= '9')))) {
2820 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2823 if (reg
->valid
== 0)
2824 reg
->type
->get(reg
);
2825 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2826 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2831 /* set register value */
2832 if (CMD_ARGC
== 2) {
2833 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2836 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2838 reg
->type
->set(reg
, buf
);
2840 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2841 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2849 return ERROR_COMMAND_SYNTAX_ERROR
;
2852 COMMAND_HANDLER(handle_poll_command
)
2854 int retval
= ERROR_OK
;
2855 struct target
*target
= get_current_target(CMD_CTX
);
2857 if (CMD_ARGC
== 0) {
2858 command_print(CMD_CTX
, "background polling: %s",
2859 jtag_poll_get_enabled() ?
"on" : "off");
2860 command_print(CMD_CTX
, "TAP: %s (%s)",
2861 target
->tap
->dotted_name
,
2862 target
->tap
->enabled ?
"enabled" : "disabled");
2863 if (!target
->tap
->enabled
)
2865 retval
= target_poll(target
);
2866 if (retval
!= ERROR_OK
)
2868 retval
= target_arch_state(target
);
2869 if (retval
!= ERROR_OK
)
2871 } else if (CMD_ARGC
== 1) {
2873 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2874 jtag_poll_set_enabled(enable
);
2876 return ERROR_COMMAND_SYNTAX_ERROR
;
2881 COMMAND_HANDLER(handle_wait_halt_command
)
2884 return ERROR_COMMAND_SYNTAX_ERROR
;
2886 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2887 if (1 == CMD_ARGC
) {
2888 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2889 if (ERROR_OK
!= retval
)
2890 return ERROR_COMMAND_SYNTAX_ERROR
;
2893 struct target
*target
= get_current_target(CMD_CTX
);
2894 return target_wait_state(target
, TARGET_HALTED
, ms
);
2897 /* wait for target state to change. The trick here is to have a low
2898 * latency for short waits and not to suck up all the CPU time
2901 * After 500ms, keep_alive() is invoked
2903 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2906 int64_t then
= 0, cur
;
2910 retval
= target_poll(target
);
2911 if (retval
!= ERROR_OK
)
2913 if (target
->state
== state
)
2918 then
= timeval_ms();
2919 LOG_DEBUG("waiting for target %s...",
2920 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2926 if ((cur
-then
) > ms
) {
2927 LOG_ERROR("timed out while waiting for target %s",
2928 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2936 COMMAND_HANDLER(handle_halt_command
)
2940 struct target
*target
= get_current_target(CMD_CTX
);
2941 int retval
= target_halt(target
);
2942 if (ERROR_OK
!= retval
)
2945 if (CMD_ARGC
== 1) {
2946 unsigned wait_local
;
2947 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2948 if (ERROR_OK
!= retval
)
2949 return ERROR_COMMAND_SYNTAX_ERROR
;
2954 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2957 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2959 struct target
*target
= get_current_target(CMD_CTX
);
2961 LOG_USER("requesting target halt and executing a soft reset");
2963 target_soft_reset_halt(target
);
2968 COMMAND_HANDLER(handle_reset_command
)
2971 return ERROR_COMMAND_SYNTAX_ERROR
;
2973 enum target_reset_mode reset_mode
= RESET_RUN
;
2974 if (CMD_ARGC
== 1) {
2976 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2977 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
2978 return ERROR_COMMAND_SYNTAX_ERROR
;
2979 reset_mode
= n
->value
;
2982 /* reset *all* targets */
2983 return target_process_reset(CMD_CTX
, reset_mode
);
2987 COMMAND_HANDLER(handle_resume_command
)
2991 return ERROR_COMMAND_SYNTAX_ERROR
;
2993 struct target
*target
= get_current_target(CMD_CTX
);
2995 /* with no CMD_ARGV, resume from current pc, addr = 0,
2996 * with one arguments, addr = CMD_ARGV[0],
2997 * handle breakpoints, not debugging */
2998 target_addr_t addr
= 0;
2999 if (CMD_ARGC
== 1) {
3000 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3004 return target_resume(target
, current
, addr
, 1, 0);
3007 COMMAND_HANDLER(handle_step_command
)
3010 return ERROR_COMMAND_SYNTAX_ERROR
;
3014 /* with no CMD_ARGV, step from current pc, addr = 0,
3015 * with one argument addr = CMD_ARGV[0],
3016 * handle breakpoints, debugging */
3017 target_addr_t addr
= 0;
3019 if (CMD_ARGC
== 1) {
3020 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3024 struct target
*target
= get_current_target(CMD_CTX
);
3026 return target
->type
->step(target
, current_pc
, addr
, 1);
3029 static void handle_md_output(struct command_context
*cmd_ctx
,
3030 struct target
*target
, target_addr_t address
, unsigned size
,
3031 unsigned count
, const uint8_t *buffer
)
3033 const unsigned line_bytecnt
= 32;
3034 unsigned line_modulo
= line_bytecnt
/ size
;
3036 char output
[line_bytecnt
* 4 + 1];
3037 unsigned output_len
= 0;
3039 const char *value_fmt
;
3042 value_fmt
= "%16.16"PRIx64
" ";
3045 value_fmt
= "%8.8"PRIx64
" ";
3048 value_fmt
= "%4.4"PRIx64
" ";
3051 value_fmt
= "%2.2"PRIx64
" ";
3054 /* "can't happen", caller checked */
3055 LOG_ERROR("invalid memory read size: %u", size
);
3059 for (unsigned i
= 0; i
< count
; i
++) {
3060 if (i
% line_modulo
== 0) {
3061 output_len
+= snprintf(output
+ output_len
,
3062 sizeof(output
) - output_len
,
3063 TARGET_ADDR_FMT
": ",
3064 (address
+ (i
* size
)));
3068 const uint8_t *value_ptr
= buffer
+ i
* size
;
3071 value
= target_buffer_get_u64(target
, value_ptr
);
3074 value
= target_buffer_get_u32(target
, value_ptr
);
3077 value
= target_buffer_get_u16(target
, value_ptr
);
3082 output_len
+= snprintf(output
+ output_len
,
3083 sizeof(output
) - output_len
,
3086 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
3087 command_print(cmd_ctx
, "%s", output
);
3093 COMMAND_HANDLER(handle_md_command
)
3096 return ERROR_COMMAND_SYNTAX_ERROR
;
3099 switch (CMD_NAME
[2]) {
3113 return ERROR_COMMAND_SYNTAX_ERROR
;
3116 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3117 int (*fn
)(struct target
*target
,
3118 target_addr_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
3122 fn
= target_read_phys_memory
;
3124 fn
= target_read_memory
;
3125 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
3126 return ERROR_COMMAND_SYNTAX_ERROR
;
3128 target_addr_t address
;
3129 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3133 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
3135 uint8_t *buffer
= calloc(count
, size
);
3136 if (buffer
== NULL
) {
3137 LOG_ERROR("Failed to allocate md read buffer");
3141 struct target
*target
= get_current_target(CMD_CTX
);
3142 int retval
= fn(target
, address
, size
, count
, buffer
);
3143 if (ERROR_OK
== retval
)
3144 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
3151 typedef int (*target_write_fn
)(struct target
*target
,
3152 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
3154 static int target_fill_mem(struct target
*target
,
3155 target_addr_t address
,
3163 /* We have to write in reasonably large chunks to be able
3164 * to fill large memory areas with any sane speed */
3165 const unsigned chunk_size
= 16384;
3166 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
3167 if (target_buf
== NULL
) {
3168 LOG_ERROR("Out of memory");
3172 for (unsigned i
= 0; i
< chunk_size
; i
++) {
3173 switch (data_size
) {
3175 target_buffer_set_u64(target
, target_buf
+ i
* data_size
, b
);
3178 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
3181 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
3184 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
3191 int retval
= ERROR_OK
;
3193 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
3196 if (current
> chunk_size
)
3197 current
= chunk_size
;
3198 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
3199 if (retval
!= ERROR_OK
)
3201 /* avoid GDB timeouts */
3210 COMMAND_HANDLER(handle_mw_command
)
3213 return ERROR_COMMAND_SYNTAX_ERROR
;
3214 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3219 fn
= target_write_phys_memory
;
3221 fn
= target_write_memory
;
3222 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
3223 return ERROR_COMMAND_SYNTAX_ERROR
;
3225 target_addr_t address
;
3226 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3228 target_addr_t value
;
3229 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], value
);
3233 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
3235 struct target
*target
= get_current_target(CMD_CTX
);
3237 switch (CMD_NAME
[2]) {
3251 return ERROR_COMMAND_SYNTAX_ERROR
;
3254 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
3257 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
3258 target_addr_t
*min_address
, target_addr_t
*max_address
)
3260 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
3261 return ERROR_COMMAND_SYNTAX_ERROR
;
3263 /* a base address isn't always necessary,
3264 * default to 0x0 (i.e. don't relocate) */
3265 if (CMD_ARGC
>= 2) {
3267 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3268 image
->base_address
= addr
;
3269 image
->base_address_set
= 1;
3271 image
->base_address_set
= 0;
3273 image
->start_address_set
= 0;
3276 COMMAND_PARSE_ADDRESS(CMD_ARGV
[3], *min_address
);
3277 if (CMD_ARGC
== 5) {
3278 COMMAND_PARSE_ADDRESS(CMD_ARGV
[4], *max_address
);
3279 /* use size (given) to find max (required) */
3280 *max_address
+= *min_address
;
3283 if (*min_address
> *max_address
)
3284 return ERROR_COMMAND_SYNTAX_ERROR
;
3289 COMMAND_HANDLER(handle_load_image_command
)
3293 uint32_t image_size
;
3294 target_addr_t min_address
= 0;
3295 target_addr_t max_address
= -1;
3299 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
3300 &image
, &min_address
, &max_address
);
3301 if (ERROR_OK
!= retval
)
3304 struct target
*target
= get_current_target(CMD_CTX
);
3306 struct duration bench
;
3307 duration_start(&bench
);
3309 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
3314 for (i
= 0; i
< image
.num_sections
; i
++) {
3315 buffer
= malloc(image
.sections
[i
].size
);
3316 if (buffer
== NULL
) {
3317 command_print(CMD_CTX
,
3318 "error allocating buffer for section (%d bytes)",
3319 (int)(image
.sections
[i
].size
));
3320 retval
= ERROR_FAIL
;
3324 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3325 if (retval
!= ERROR_OK
) {
3330 uint32_t offset
= 0;
3331 uint32_t length
= buf_cnt
;
3333 /* DANGER!!! beware of unsigned comparision here!!! */
3335 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3336 (image
.sections
[i
].base_address
< max_address
)) {
3338 if (image
.sections
[i
].base_address
< min_address
) {
3339 /* clip addresses below */
3340 offset
+= min_address
-image
.sections
[i
].base_address
;
3344 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3345 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3347 retval
= target_write_buffer(target
,
3348 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3349 if (retval
!= ERROR_OK
) {
3353 image_size
+= length
;
3354 command_print(CMD_CTX
, "%u bytes written at address " TARGET_ADDR_FMT
"",
3355 (unsigned int)length
,
3356 image
.sections
[i
].base_address
+ offset
);
3362 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3363 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
3364 "in %fs (%0.3f KiB/s)", image_size
,
3365 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3368 image_close(&image
);
3374 COMMAND_HANDLER(handle_dump_image_command
)
3376 struct fileio
*fileio
;
3378 int retval
, retvaltemp
;
3379 target_addr_t address
, size
;
3380 struct duration bench
;
3381 struct target
*target
= get_current_target(CMD_CTX
);
3384 return ERROR_COMMAND_SYNTAX_ERROR
;
3386 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], address
);
3387 COMMAND_PARSE_ADDRESS(CMD_ARGV
[2], size
);
3389 uint32_t buf_size
= (size
> 4096) ?
4096 : size
;
3390 buffer
= malloc(buf_size
);
3394 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3395 if (retval
!= ERROR_OK
) {
3400 duration_start(&bench
);
3403 size_t size_written
;
3404 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
3405 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
3406 if (retval
!= ERROR_OK
)
3409 retval
= fileio_write(fileio
, this_run_size
, buffer
, &size_written
);
3410 if (retval
!= ERROR_OK
)
3413 size
-= this_run_size
;
3414 address
+= this_run_size
;
3419 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3421 retval
= fileio_size(fileio
, &filesize
);
3422 if (retval
!= ERROR_OK
)
3424 command_print(CMD_CTX
,
3425 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize
,
3426 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3429 retvaltemp
= fileio_close(fileio
);
3430 if (retvaltemp
!= ERROR_OK
)
3439 IMAGE_CHECKSUM_ONLY
= 2
3442 static COMMAND_HELPER(handle_verify_image_command_internal
, enum verify_mode verify
)
3446 uint32_t image_size
;
3449 uint32_t checksum
= 0;
3450 uint32_t mem_checksum
= 0;
3454 struct target
*target
= get_current_target(CMD_CTX
);
3457 return ERROR_COMMAND_SYNTAX_ERROR
;
3460 LOG_ERROR("no target selected");
3464 struct duration bench
;
3465 duration_start(&bench
);
3467 if (CMD_ARGC
>= 2) {
3469 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3470 image
.base_address
= addr
;
3471 image
.base_address_set
= 1;
3473 image
.base_address_set
= 0;
3474 image
.base_address
= 0x0;
3477 image
.start_address_set
= 0;
3479 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3480 if (retval
!= ERROR_OK
)
3486 for (i
= 0; i
< image
.num_sections
; i
++) {
3487 buffer
= malloc(image
.sections
[i
].size
);
3488 if (buffer
== NULL
) {
3489 command_print(CMD_CTX
,
3490 "error allocating buffer for section (%d bytes)",
3491 (int)(image
.sections
[i
].size
));
3494 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3495 if (retval
!= ERROR_OK
) {
3500 if (verify
>= IMAGE_VERIFY
) {
3501 /* calculate checksum of image */
3502 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3503 if (retval
!= ERROR_OK
) {
3508 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3509 if (retval
!= ERROR_OK
) {
3513 if ((checksum
!= mem_checksum
) && (verify
== IMAGE_CHECKSUM_ONLY
)) {
3514 LOG_ERROR("checksum mismatch");
3516 retval
= ERROR_FAIL
;
3519 if (checksum
!= mem_checksum
) {
3520 /* failed crc checksum, fall back to a binary compare */
3524 LOG_ERROR("checksum mismatch - attempting binary compare");
3526 data
= malloc(buf_cnt
);
3528 /* Can we use 32bit word accesses? */
3530 int count
= buf_cnt
;
3531 if ((count
% 4) == 0) {
3535 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
3536 if (retval
== ERROR_OK
) {
3538 for (t
= 0; t
< buf_cnt
; t
++) {
3539 if (data
[t
] != buffer
[t
]) {
3540 command_print(CMD_CTX
,
3541 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3543 (unsigned)(t
+ image
.sections
[i
].base_address
),
3546 if (diffs
++ >= 127) {
3547 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
3559 command_print(CMD_CTX
, "address " TARGET_ADDR_FMT
" length 0x%08zx",
3560 image
.sections
[i
].base_address
,
3565 image_size
+= buf_cnt
;
3568 command_print(CMD_CTX
, "No more differences found.");
3571 retval
= ERROR_FAIL
;
3572 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3573 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3574 "in %fs (%0.3f KiB/s)", image_size
,
3575 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3578 image_close(&image
);
3583 COMMAND_HANDLER(handle_verify_image_checksum_command
)
3585 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_CHECKSUM_ONLY
);
3588 COMMAND_HANDLER(handle_verify_image_command
)
3590 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_VERIFY
);
3593 COMMAND_HANDLER(handle_test_image_command
)
3595 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_TEST
);
3598 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
3600 struct target
*target
= get_current_target(cmd_ctx
);
3601 struct breakpoint
*breakpoint
= target
->breakpoints
;
3602 while (breakpoint
) {
3603 if (breakpoint
->type
== BKPT_SOFT
) {
3604 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3605 breakpoint
->length
, 16);
3606 command_print(cmd_ctx
, "IVA breakpoint: " TARGET_ADDR_FMT
", 0x%x, %i, 0x%s",
3607 breakpoint
->address
,
3609 breakpoint
->set
, buf
);
3612 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3613 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3615 breakpoint
->length
, breakpoint
->set
);
3616 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3617 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3618 breakpoint
->address
,
3619 breakpoint
->length
, breakpoint
->set
);
3620 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3623 command_print(cmd_ctx
, "Breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3624 breakpoint
->address
,
3625 breakpoint
->length
, breakpoint
->set
);
3628 breakpoint
= breakpoint
->next
;
3633 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
3634 target_addr_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3636 struct target
*target
= get_current_target(cmd_ctx
);
3640 retval
= breakpoint_add(target
, addr
, length
, hw
);
3641 if (ERROR_OK
== retval
)
3642 command_print(cmd_ctx
, "breakpoint set at " TARGET_ADDR_FMT
"", addr
);
3644 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3647 } else if (addr
== 0) {
3648 if (target
->type
->add_context_breakpoint
== NULL
) {
3649 LOG_WARNING("Context breakpoint not available");
3652 retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3653 if (ERROR_OK
== retval
)
3654 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3656 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3660 if (target
->type
->add_hybrid_breakpoint
== NULL
) {
3661 LOG_WARNING("Hybrid breakpoint not available");
3664 retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3665 if (ERROR_OK
== retval
)
3666 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3668 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3675 COMMAND_HANDLER(handle_bp_command
)