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_HALT_PRE
, .name
= "reset-halt-pre" },
208 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
209 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
210 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
211 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
212 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
214 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
215 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
217 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
218 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
220 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
221 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
223 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
224 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
226 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
227 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
229 { .value
= TARGET_EVENT_TRACE_CONFIG
, .name
= "trace-config" },
231 { .name
= NULL
, .value
= -1 }
234 static const Jim_Nvp nvp_target_state
[] = {
235 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
236 { .name
= "running", .value
= TARGET_RUNNING
},
237 { .name
= "halted", .value
= TARGET_HALTED
},
238 { .name
= "reset", .value
= TARGET_RESET
},
239 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
240 { .name
= NULL
, .value
= -1 },
243 static const Jim_Nvp nvp_target_debug_reason
[] = {
244 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
245 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
246 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
247 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
248 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
249 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
250 { .name
= "program-exit" , .value
= DBG_REASON_EXIT
},
251 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
252 { .name
= NULL
, .value
= -1 },
255 static const Jim_Nvp nvp_target_endian
[] = {
256 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
257 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
258 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
259 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
260 { .name
= NULL
, .value
= -1 },
263 static const Jim_Nvp nvp_reset_modes
[] = {
264 { .name
= "unknown", .value
= RESET_UNKNOWN
},
265 { .name
= "run" , .value
= RESET_RUN
},
266 { .name
= "halt" , .value
= RESET_HALT
},
267 { .name
= "init" , .value
= RESET_INIT
},
268 { .name
= NULL
, .value
= -1 },
271 const char *debug_reason_name(struct target
*t
)
275 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
276 t
->debug_reason
)->name
;
278 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
279 cp
= "(*BUG*unknown*BUG*)";
284 const char *target_state_name(struct target
*t
)
287 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
289 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
290 cp
= "(*BUG*unknown*BUG*)";
293 if (!target_was_examined(t
) && t
->defer_examine
)
294 cp
= "examine deferred";
299 const char *target_event_name(enum target_event event
)
302 cp
= Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
;
304 LOG_ERROR("Invalid target event: %d", (int)(event
));
305 cp
= "(*BUG*unknown*BUG*)";
310 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
313 cp
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
;
315 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
316 cp
= "(*BUG*unknown*BUG*)";
321 /* determine the number of the new target */
322 static int new_target_number(void)
327 /* number is 0 based */
331 if (x
< t
->target_number
)
332 x
= t
->target_number
;
338 /* read a uint64_t from a buffer in target memory endianness */
339 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
341 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
342 return le_to_h_u64(buffer
);
344 return be_to_h_u64(buffer
);
347 /* read a uint32_t from a buffer in target memory endianness */
348 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
350 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
351 return le_to_h_u32(buffer
);
353 return be_to_h_u32(buffer
);
356 /* read a uint24_t from a buffer in target memory endianness */
357 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
359 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
360 return le_to_h_u24(buffer
);
362 return be_to_h_u24(buffer
);
365 /* read a uint16_t from a buffer in target memory endianness */
366 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
368 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
369 return le_to_h_u16(buffer
);
371 return be_to_h_u16(buffer
);
374 /* read a uint8_t from a buffer in target memory endianness */
375 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
377 return *buffer
& 0x0ff;
380 /* write a uint64_t to a buffer in target memory endianness */
381 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
383 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
384 h_u64_to_le(buffer
, value
);
386 h_u64_to_be(buffer
, value
);
389 /* write a uint32_t to a buffer in target memory endianness */
390 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
392 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
393 h_u32_to_le(buffer
, value
);
395 h_u32_to_be(buffer
, value
);
398 /* write a uint24_t to a buffer in target memory endianness */
399 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
401 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
402 h_u24_to_le(buffer
, value
);
404 h_u24_to_be(buffer
, value
);
407 /* write a uint16_t to a buffer in target memory endianness */
408 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
410 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
411 h_u16_to_le(buffer
, value
);
413 h_u16_to_be(buffer
, value
);
416 /* write a uint8_t to a buffer in target memory endianness */
417 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
422 /* write a uint64_t array to a buffer in target memory endianness */
423 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
426 for (i
= 0; i
< count
; i
++)
427 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
430 /* write a uint32_t array to a buffer in target memory endianness */
431 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
434 for (i
= 0; i
< count
; i
++)
435 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
438 /* write a uint16_t array to a buffer in target memory endianness */
439 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
442 for (i
= 0; i
< count
; i
++)
443 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
446 /* write a uint64_t array to a buffer in target memory endianness */
447 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
450 for (i
= 0; i
< count
; i
++)
451 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
454 /* write a uint32_t array to a buffer in target memory endianness */
455 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
458 for (i
= 0; i
< count
; i
++)
459 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
462 /* write a uint16_t array to a buffer in target memory endianness */
463 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
466 for (i
= 0; i
< count
; i
++)
467 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
470 /* return a pointer to a configured target; id is name or number */
471 struct target
*get_target(const char *id
)
473 struct target
*target
;
475 /* try as tcltarget name */
476 for (target
= all_targets
; target
; target
= target
->next
) {
477 if (target_name(target
) == NULL
)
479 if (strcmp(id
, target_name(target
)) == 0)
483 /* It's OK to remove this fallback sometime after August 2010 or so */
485 /* no match, try as number */
487 if (parse_uint(id
, &num
) != ERROR_OK
)
490 for (target
= all_targets
; target
; target
= target
->next
) {
491 if (target
->target_number
== (int)num
) {
492 LOG_WARNING("use '%s' as target identifier, not '%u'",
493 target_name(target
), num
);
501 /* returns a pointer to the n-th configured target */
502 struct target
*get_target_by_num(int num
)
504 struct target
*target
= all_targets
;
507 if (target
->target_number
== num
)
509 target
= target
->next
;
515 struct target
*get_current_target(struct command_context
*cmd_ctx
)
517 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
519 if (target
== NULL
) {
520 LOG_ERROR("BUG: current_target out of bounds");
527 int target_poll(struct target
*target
)
531 /* We can't poll until after examine */
532 if (!target_was_examined(target
)) {
533 /* Fail silently lest we pollute the log */
537 retval
= target
->type
->poll(target
);
538 if (retval
!= ERROR_OK
)
541 if (target
->halt_issued
) {
542 if (target
->state
== TARGET_HALTED
)
543 target
->halt_issued
= false;
545 int64_t t
= timeval_ms() - target
->halt_issued_time
;
546 if (t
> DEFAULT_HALT_TIMEOUT
) {
547 target
->halt_issued
= false;
548 LOG_INFO("Halt timed out, wake up GDB.");
549 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
557 int target_halt(struct target
*target
)
560 /* We can't poll until after examine */
561 if (!target_was_examined(target
)) {
562 LOG_ERROR("Target not examined yet");
566 retval
= target
->type
->halt(target
);
567 if (retval
!= ERROR_OK
)
570 target
->halt_issued
= true;
571 target
->halt_issued_time
= timeval_ms();
577 * Make the target (re)start executing using its saved execution
578 * context (possibly with some modifications).
580 * @param target Which target should start executing.
581 * @param current True to use the target's saved program counter instead
582 * of the address parameter
583 * @param address Optionally used as the program counter.
584 * @param handle_breakpoints True iff breakpoints at the resumption PC
585 * should be skipped. (For example, maybe execution was stopped by
586 * such a breakpoint, in which case it would be counterprodutive to
588 * @param debug_execution False if all working areas allocated by OpenOCD
589 * should be released and/or restored to their original contents.
590 * (This would for example be true to run some downloaded "helper"
591 * algorithm code, which resides in one such working buffer and uses
592 * another for data storage.)
594 * @todo Resolve the ambiguity about what the "debug_execution" flag
595 * signifies. For example, Target implementations don't agree on how
596 * it relates to invalidation of the register cache, or to whether
597 * breakpoints and watchpoints should be enabled. (It would seem wrong
598 * to enable breakpoints when running downloaded "helper" algorithms
599 * (debug_execution true), since the breakpoints would be set to match
600 * target firmware being debugged, not the helper algorithm.... and
601 * enabling them could cause such helpers to malfunction (for example,
602 * by overwriting data with a breakpoint instruction. On the other
603 * hand the infrastructure for running such helpers might use this
604 * procedure but rely on hardware breakpoint to detect termination.)
606 int target_resume(struct target
*target
, int current
, target_addr_t address
,
607 int handle_breakpoints
, int debug_execution
)
611 /* We can't poll until after examine */
612 if (!target_was_examined(target
)) {
613 LOG_ERROR("Target not examined yet");
617 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
619 /* note that resume *must* be asynchronous. The CPU can halt before
620 * we poll. The CPU can even halt at the current PC as a result of
621 * a software breakpoint being inserted by (a bug?) the application.
623 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
624 if (retval
!= ERROR_OK
)
627 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
632 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
637 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
638 if (n
->name
== NULL
) {
639 LOG_ERROR("invalid reset mode");
643 struct target
*target
;
644 for (target
= all_targets
; target
; target
= target
->next
)
645 target_call_reset_callbacks(target
, reset_mode
);
647 /* disable polling during reset to make reset event scripts
648 * more predictable, i.e. dr/irscan & pathmove in events will
649 * not have JTAG operations injected into the middle of a sequence.
651 bool save_poll
= jtag_poll_get_enabled();
653 jtag_poll_set_enabled(false);
655 sprintf(buf
, "ocd_process_reset %s", n
->name
);
656 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
658 jtag_poll_set_enabled(save_poll
);
660 if (retval
!= JIM_OK
) {
661 Jim_MakeErrorMessage(cmd_ctx
->interp
);
662 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
666 /* We want any events to be processed before the prompt */
667 retval
= target_call_timer_callbacks_now();
669 for (target
= all_targets
; target
; target
= target
->next
) {
670 target
->type
->check_reset(target
);
671 target
->running_alg
= false;
677 static int identity_virt2phys(struct target
*target
,
678 target_addr_t
virtual, target_addr_t
*physical
)
684 static int no_mmu(struct target
*target
, int *enabled
)
690 static int default_examine(struct target
*target
)
692 target_set_examined(target
);
696 /* no check by default */
697 static int default_check_reset(struct target
*target
)
702 int target_examine_one(struct target
*target
)
704 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
706 int retval
= target
->type
->examine(target
);
707 if (retval
!= ERROR_OK
)
710 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
715 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
717 struct target
*target
= priv
;
719 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
722 jtag_unregister_event_callback(jtag_enable_callback
, target
);
724 return target_examine_one(target
);
727 /* Targets that correctly implement init + examine, i.e.
728 * no communication with target during init:
732 int target_examine(void)
734 int retval
= ERROR_OK
;
735 struct target
*target
;
737 for (target
= all_targets
; target
; target
= target
->next
) {
738 /* defer examination, but don't skip it */
739 if (!target
->tap
->enabled
) {
740 jtag_register_event_callback(jtag_enable_callback
,
745 if (target
->defer_examine
)
748 retval
= target_examine_one(target
);
749 if (retval
!= ERROR_OK
)
755 const char *target_type_name(struct target
*target
)
757 return target
->type
->name
;
760 static int target_soft_reset_halt(struct target
*target
)
762 if (!target_was_examined(target
)) {
763 LOG_ERROR("Target not examined yet");
766 if (!target
->type
->soft_reset_halt
) {
767 LOG_ERROR("Target %s does not support soft_reset_halt",
768 target_name(target
));
771 return target
->type
->soft_reset_halt(target
);
775 * Downloads a target-specific native code algorithm to the target,
776 * and executes it. * Note that some targets may need to set up, enable,
777 * and tear down a breakpoint (hard or * soft) to detect algorithm
778 * termination, while others may support lower overhead schemes where
779 * soft breakpoints embedded in the algorithm automatically terminate the
782 * @param target used to run the algorithm
783 * @param arch_info target-specific description of the algorithm.
785 int target_run_algorithm(struct target
*target
,
786 int num_mem_params
, struct mem_param
*mem_params
,
787 int num_reg_params
, struct reg_param
*reg_param
,
788 uint32_t entry_point
, uint32_t exit_point
,
789 int timeout_ms
, void *arch_info
)
791 int retval
= ERROR_FAIL
;
793 if (!target_was_examined(target
)) {
794 LOG_ERROR("Target not examined yet");
797 if (!target
->type
->run_algorithm
) {
798 LOG_ERROR("Target type '%s' does not support %s",
799 target_type_name(target
), __func__
);
803 target
->running_alg
= true;
804 retval
= target
->type
->run_algorithm(target
,
805 num_mem_params
, mem_params
,
806 num_reg_params
, reg_param
,
807 entry_point
, exit_point
, timeout_ms
, arch_info
);
808 target
->running_alg
= false;
815 * Downloads a target-specific native code algorithm to the target,
816 * executes and leaves it running.
818 * @param target used to run the algorithm
819 * @param arch_info target-specific description of the algorithm.
821 int target_start_algorithm(struct target
*target
,
822 int num_mem_params
, struct mem_param
*mem_params
,
823 int num_reg_params
, struct reg_param
*reg_params
,
824 uint32_t entry_point
, uint32_t exit_point
,
827 int retval
= ERROR_FAIL
;
829 if (!target_was_examined(target
)) {
830 LOG_ERROR("Target not examined yet");
833 if (!target
->type
->start_algorithm
) {
834 LOG_ERROR("Target type '%s' does not support %s",
835 target_type_name(target
), __func__
);
838 if (target
->running_alg
) {
839 LOG_ERROR("Target is already running an algorithm");
843 target
->running_alg
= true;
844 retval
= target
->type
->start_algorithm(target
,
845 num_mem_params
, mem_params
,
846 num_reg_params
, reg_params
,
847 entry_point
, exit_point
, arch_info
);
854 * Waits for an algorithm started with target_start_algorithm() to complete.
856 * @param target used to run the algorithm
857 * @param arch_info target-specific description of the algorithm.
859 int target_wait_algorithm(struct target
*target
,
860 int num_mem_params
, struct mem_param
*mem_params
,
861 int num_reg_params
, struct reg_param
*reg_params
,
862 uint32_t exit_point
, int timeout_ms
,
865 int retval
= ERROR_FAIL
;
867 if (!target
->type
->wait_algorithm
) {
868 LOG_ERROR("Target type '%s' does not support %s",
869 target_type_name(target
), __func__
);
872 if (!target
->running_alg
) {
873 LOG_ERROR("Target is not running an algorithm");
877 retval
= target
->type
->wait_algorithm(target
,
878 num_mem_params
, mem_params
,
879 num_reg_params
, reg_params
,
880 exit_point
, timeout_ms
, arch_info
);
881 if (retval
!= ERROR_TARGET_TIMEOUT
)
882 target
->running_alg
= false;
889 * Executes a target-specific native code algorithm in the target.
890 * It differs from target_run_algorithm in that the algorithm is asynchronous.
891 * Because of this it requires an compliant algorithm:
892 * see contrib/loaders/flash/stm32f1x.S for example.
894 * @param target used to run the algorithm
897 int target_run_flash_async_algorithm(struct target
*target
,
898 const uint8_t *buffer
, uint32_t count
, int block_size
,
899 int num_mem_params
, struct mem_param
*mem_params
,
900 int num_reg_params
, struct reg_param
*reg_params
,
901 uint32_t buffer_start
, uint32_t buffer_size
,
902 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
907 const uint8_t *buffer_orig
= buffer
;
909 /* Set up working area. First word is write pointer, second word is read pointer,
910 * rest is fifo data area. */
911 uint32_t wp_addr
= buffer_start
;
912 uint32_t rp_addr
= buffer_start
+ 4;
913 uint32_t fifo_start_addr
= buffer_start
+ 8;
914 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
916 uint32_t wp
= fifo_start_addr
;
917 uint32_t rp
= fifo_start_addr
;
919 /* validate block_size is 2^n */
920 assert(!block_size
|| !(block_size
& (block_size
- 1)));
922 retval
= target_write_u32(target
, wp_addr
, wp
);
923 if (retval
!= ERROR_OK
)
925 retval
= target_write_u32(target
, rp_addr
, rp
);
926 if (retval
!= ERROR_OK
)
929 /* Start up algorithm on target and let it idle while writing the first chunk */
930 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
931 num_reg_params
, reg_params
,
936 if (retval
!= ERROR_OK
) {
937 LOG_ERROR("error starting target flash write algorithm");
943 retval
= target_read_u32(target
, rp_addr
, &rp
);
944 if (retval
!= ERROR_OK
) {
945 LOG_ERROR("failed to get read pointer");
949 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
950 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
953 LOG_ERROR("flash write algorithm aborted by target");
954 retval
= ERROR_FLASH_OPERATION_FAILED
;
958 if (((rp
- fifo_start_addr
) & (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
959 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
963 /* Count the number of bytes available in the fifo without
964 * crossing the wrap around. Make sure to not fill it completely,
965 * because that would make wp == rp and that's the empty condition. */
966 uint32_t thisrun_bytes
;
968 thisrun_bytes
= rp
- wp
- block_size
;
969 else if (rp
> fifo_start_addr
)
970 thisrun_bytes
= fifo_end_addr
- wp
;
972 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
974 if (thisrun_bytes
== 0) {
975 /* Throttle polling a bit if transfer is (much) faster than flash
976 * programming. The exact delay shouldn't matter as long as it's
977 * less than buffer size / flash speed. This is very unlikely to
978 * run when using high latency connections such as USB. */
981 /* to stop an infinite loop on some targets check and increment a timeout
982 * this issue was observed on a stellaris using the new ICDI interface */
983 if (timeout
++ >= 500) {
984 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
985 return ERROR_FLASH_OPERATION_FAILED
;
990 /* reset our timeout */
993 /* Limit to the amount of data we actually want to write */
994 if (thisrun_bytes
> count
* block_size
)
995 thisrun_bytes
= count
* block_size
;
997 /* Write data to fifo */
998 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
999 if (retval
!= ERROR_OK
)
1002 /* Update counters and wrap write pointer */
1003 buffer
+= thisrun_bytes
;
1004 count
-= thisrun_bytes
/ block_size
;
1005 wp
+= thisrun_bytes
;
1006 if (wp
>= fifo_end_addr
)
1007 wp
= fifo_start_addr
;
1009 /* Store updated write pointer to target */
1010 retval
= target_write_u32(target
, wp_addr
, wp
);
1011 if (retval
!= ERROR_OK
)
1015 if (retval
!= ERROR_OK
) {
1016 /* abort flash write algorithm on target */
1017 target_write_u32(target
, wp_addr
, 0);
1020 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1021 num_reg_params
, reg_params
,
1026 if (retval2
!= ERROR_OK
) {
1027 LOG_ERROR("error waiting for target flash write algorithm");
1031 if (retval
== ERROR_OK
) {
1032 /* check if algorithm set rp = 0 after fifo writer loop finished */
1033 retval
= target_read_u32(target
, rp_addr
, &rp
);
1034 if (retval
== ERROR_OK
&& rp
== 0) {
1035 LOG_ERROR("flash write algorithm aborted by target");
1036 retval
= ERROR_FLASH_OPERATION_FAILED
;
1043 int target_read_memory(struct target
*target
,
1044 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1046 if (!target_was_examined(target
)) {
1047 LOG_ERROR("Target not examined yet");
1050 if (!target
->type
->read_memory
) {
1051 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1054 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1057 int target_read_phys_memory(struct target
*target
,
1058 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1060 if (!target_was_examined(target
)) {
1061 LOG_ERROR("Target not examined yet");
1064 if (!target
->type
->read_phys_memory
) {
1065 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1068 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1071 int target_write_memory(struct target
*target
,
1072 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1074 if (!target_was_examined(target
)) {
1075 LOG_ERROR("Target not examined yet");
1078 if (!target
->type
->write_memory
) {
1079 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1082 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1085 int target_write_phys_memory(struct target
*target
,
1086 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1088 if (!target_was_examined(target
)) {
1089 LOG_ERROR("Target not examined yet");
1092 if (!target
->type
->write_phys_memory
) {
1093 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1096 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1099 int target_add_breakpoint(struct target
*target
,
1100 struct breakpoint
*breakpoint
)
1102 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1103 LOG_WARNING("target %s is not halted (add breakpoint)", target_name(target
));
1104 return ERROR_TARGET_NOT_HALTED
;
1106 return target
->type
->add_breakpoint(target
, breakpoint
);
1109 int target_add_context_breakpoint(struct target
*target
,
1110 struct breakpoint
*breakpoint
)
1112 if (target
->state
!= TARGET_HALTED
) {
1113 LOG_WARNING("target %s is not halted (add context breakpoint)", target_name(target
));
1114 return ERROR_TARGET_NOT_HALTED
;
1116 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1119 int target_add_hybrid_breakpoint(struct target
*target
,
1120 struct breakpoint
*breakpoint
)
1122 if (target
->state
!= TARGET_HALTED
) {
1123 LOG_WARNING("target %s is not halted (add hybrid breakpoint)", target_name(target
));
1124 return ERROR_TARGET_NOT_HALTED
;
1126 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1129 int target_remove_breakpoint(struct target
*target
,
1130 struct breakpoint
*breakpoint
)
1132 return target
->type
->remove_breakpoint(target
, breakpoint
);
1135 int target_add_watchpoint(struct target
*target
,
1136 struct watchpoint
*watchpoint
)
1138 if (target
->state
!= TARGET_HALTED
) {
1139 LOG_WARNING("target %s is not halted (add watchpoint)", target_name(target
));
1140 return ERROR_TARGET_NOT_HALTED
;
1142 return target
->type
->add_watchpoint(target
, watchpoint
);
1144 int target_remove_watchpoint(struct target
*target
,
1145 struct watchpoint
*watchpoint
)
1147 return target
->type
->remove_watchpoint(target
, watchpoint
);
1149 int target_hit_watchpoint(struct target
*target
,
1150 struct watchpoint
**hit_watchpoint
)
1152 if (target
->state
!= TARGET_HALTED
) {
1153 LOG_WARNING("target %s is not halted (hit watchpoint)", target
->cmd_name
);
1154 return ERROR_TARGET_NOT_HALTED
;
1157 if (target
->type
->hit_watchpoint
== NULL
) {
1158 /* For backward compatible, if hit_watchpoint is not implemented,
1159 * return ERROR_FAIL such that gdb_server will not take the nonsense
1164 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1167 int target_get_gdb_reg_list(struct target
*target
,
1168 struct reg
**reg_list
[], int *reg_list_size
,
1169 enum target_register_class reg_class
)
1171 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1173 int target_step(struct target
*target
,
1174 int current
, target_addr_t address
, int handle_breakpoints
)
1176 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1179 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1181 if (target
->state
!= TARGET_HALTED
) {
1182 LOG_WARNING("target %s is not halted (gdb fileio)", target
->cmd_name
);
1183 return ERROR_TARGET_NOT_HALTED
;
1185 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1188 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1190 if (target
->state
!= TARGET_HALTED
) {
1191 LOG_WARNING("target %s is not halted (gdb fileio end)", target
->cmd_name
);
1192 return ERROR_TARGET_NOT_HALTED
;
1194 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1197 int target_profiling(struct target
*target
, uint32_t *samples
,
1198 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1200 if (target
->state
!= TARGET_HALTED
) {
1201 LOG_WARNING("target %s is not halted (profiling)", target
->cmd_name
);
1202 return ERROR_TARGET_NOT_HALTED
;
1204 return target
->type
->profiling(target
, samples
, max_num_samples
,
1205 num_samples
, seconds
);
1209 * Reset the @c examined flag for the given target.
1210 * Pure paranoia -- targets are zeroed on allocation.
1212 static void target_reset_examined(struct target
*target
)
1214 target
->examined
= false;
1217 static int handle_target(void *priv
);
1219 static int target_init_one(struct command_context
*cmd_ctx
,
1220 struct target
*target
)
1222 target_reset_examined(target
);
1224 struct target_type
*type
= target
->type
;
1225 if (type
->examine
== NULL
)
1226 type
->examine
= default_examine
;
1228 if (type
->check_reset
== NULL
)
1229 type
->check_reset
= default_check_reset
;
1231 assert(type
->init_target
!= NULL
);
1233 int retval
= type
->init_target(cmd_ctx
, target
);
1234 if (ERROR_OK
!= retval
) {
1235 LOG_ERROR("target '%s' init failed", target_name(target
));
1239 /* Sanity-check MMU support ... stub in what we must, to help
1240 * implement it in stages, but warn if we need to do so.
1243 if (type
->virt2phys
== NULL
) {
1244 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1245 type
->virt2phys
= identity_virt2phys
;
1248 /* Make sure no-MMU targets all behave the same: make no
1249 * distinction between physical and virtual addresses, and
1250 * ensure that virt2phys() is always an identity mapping.
1252 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1253 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1256 type
->write_phys_memory
= type
->write_memory
;
1257 type
->read_phys_memory
= type
->read_memory
;
1258 type
->virt2phys
= identity_virt2phys
;
1261 if (target
->type
->read_buffer
== NULL
)
1262 target
->type
->read_buffer
= target_read_buffer_default
;
1264 if (target
->type
->write_buffer
== NULL
)
1265 target
->type
->write_buffer
= target_write_buffer_default
;
1267 if (target
->type
->get_gdb_fileio_info
== NULL
)
1268 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1270 if (target
->type
->gdb_fileio_end
== NULL
)
1271 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1273 if (target
->type
->profiling
== NULL
)
1274 target
->type
->profiling
= target_profiling_default
;
1279 static int target_init(struct command_context
*cmd_ctx
)
1281 struct target
*target
;
1284 for (target
= all_targets
; target
; target
= target
->next
) {
1285 retval
= target_init_one(cmd_ctx
, target
);
1286 if (ERROR_OK
!= retval
)
1293 retval
= target_register_user_commands(cmd_ctx
);
1294 if (ERROR_OK
!= retval
)
1297 retval
= target_register_timer_callback(&handle_target
,
1298 polling_interval
, 1, cmd_ctx
->interp
);
1299 if (ERROR_OK
!= retval
)
1305 COMMAND_HANDLER(handle_target_init_command
)
1310 return ERROR_COMMAND_SYNTAX_ERROR
;
1312 static bool target_initialized
;
1313 if (target_initialized
) {
1314 LOG_INFO("'target init' has already been called");
1317 target_initialized
= true;
1319 retval
= command_run_line(CMD_CTX
, "init_targets");
1320 if (ERROR_OK
!= retval
)
1323 retval
= command_run_line(CMD_CTX
, "init_target_events");
1324 if (ERROR_OK
!= retval
)
1327 retval
= command_run_line(CMD_CTX
, "init_board");
1328 if (ERROR_OK
!= retval
)
1331 LOG_DEBUG("Initializing targets...");
1332 return target_init(CMD_CTX
);
1335 int target_register_event_callback(int (*callback
)(struct target
*target
,
1336 enum target_event event
, void *priv
), void *priv
)
1338 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1340 if (callback
== NULL
)
1341 return ERROR_COMMAND_SYNTAX_ERROR
;
1344 while ((*callbacks_p
)->next
)
1345 callbacks_p
= &((*callbacks_p
)->next
);
1346 callbacks_p
= &((*callbacks_p
)->next
);
1349 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1350 (*callbacks_p
)->callback
= callback
;
1351 (*callbacks_p
)->priv
= priv
;
1352 (*callbacks_p
)->next
= NULL
;
1357 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1358 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1360 struct target_reset_callback
*entry
;
1362 if (callback
== NULL
)
1363 return ERROR_COMMAND_SYNTAX_ERROR
;
1365 entry
= malloc(sizeof(struct target_reset_callback
));
1366 if (entry
== NULL
) {
1367 LOG_ERROR("error allocating buffer for reset callback entry");
1368 return ERROR_COMMAND_SYNTAX_ERROR
;
1371 entry
->callback
= callback
;
1373 list_add(&entry
->list
, &target_reset_callback_list
);
1379 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1380 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1382 struct target_trace_callback
*entry
;
1384 if (callback
== NULL
)
1385 return ERROR_COMMAND_SYNTAX_ERROR
;
1387 entry
= malloc(sizeof(struct target_trace_callback
));
1388 if (entry
== NULL
) {
1389 LOG_ERROR("error allocating buffer for trace callback entry");
1390 return ERROR_COMMAND_SYNTAX_ERROR
;
1393 entry
->callback
= callback
;
1395 list_add(&entry
->list
, &target_trace_callback_list
);
1401 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1403 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1406 if (callback
== NULL
)
1407 return ERROR_COMMAND_SYNTAX_ERROR
;
1410 while ((*callbacks_p
)->next
)
1411 callbacks_p
= &((*callbacks_p
)->next
);
1412 callbacks_p
= &((*callbacks_p
)->next
);
1415 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1416 (*callbacks_p
)->callback
= callback
;
1417 (*callbacks_p
)->periodic
= periodic
;
1418 (*callbacks_p
)->time_ms
= time_ms
;
1419 (*callbacks_p
)->removed
= false;
1421 gettimeofday(&now
, NULL
);
1422 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
1423 time_ms
-= (time_ms
% 1000);
1424 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
1425 if ((*callbacks_p
)->when
.tv_usec
> 1000000) {
1426 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
1427 (*callbacks_p
)->when
.tv_sec
+= 1;
1430 (*callbacks_p
)->priv
= priv
;
1431 (*callbacks_p
)->next
= NULL
;
1436 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1437 enum target_event event
, void *priv
), void *priv
)
1439 struct target_event_callback
**p
= &target_event_callbacks
;
1440 struct target_event_callback
*c
= target_event_callbacks
;
1442 if (callback
== NULL
)
1443 return ERROR_COMMAND_SYNTAX_ERROR
;
1446 struct target_event_callback
*next
= c
->next
;
1447 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1459 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1460 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1462 struct target_reset_callback
*entry
;
1464 if (callback
== NULL
)
1465 return ERROR_COMMAND_SYNTAX_ERROR
;
1467 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1468 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1469 list_del(&entry
->list
);
1478 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1479 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1481 struct target_trace_callback
*entry
;
1483 if (callback
== NULL
)
1484 return ERROR_COMMAND_SYNTAX_ERROR
;
1486 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1487 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1488 list_del(&entry
->list
);
1497 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1499 if (callback
== NULL
)
1500 return ERROR_COMMAND_SYNTAX_ERROR
;
1502 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1504 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1513 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1515 struct target_event_callback
*callback
= target_event_callbacks
;
1516 struct target_event_callback
*next_callback
;
1518 if (event
== TARGET_EVENT_HALTED
) {
1519 /* execute early halted first */
1520 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1523 LOG_DEBUG("target event %i (%s)", event
,
1524 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1526 target_handle_event(target
, event
);
1529 next_callback
= callback
->next
;
1530 callback
->callback(target
, event
, callback
->priv
);
1531 callback
= next_callback
;
1537 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1539 struct target_reset_callback
*callback
;
1541 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1542 Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1544 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1545 callback
->callback(target
, reset_mode
, callback
->priv
);
1550 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1552 struct target_trace_callback
*callback
;
1554 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1555 callback
->callback(target
, len
, data
, callback
->priv
);
1560 static int target_timer_callback_periodic_restart(
1561 struct target_timer_callback
*cb
, struct timeval
*now
)
1563 int time_ms
= cb
->time_ms
;
1564 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1565 time_ms
-= (time_ms
% 1000);
1566 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1567 if (cb
->when
.tv_usec
> 1000000) {
1568 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1569 cb
->when
.tv_sec
+= 1;
1574 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1575 struct timeval
*now
)
1577 cb
->callback(cb
->priv
);
1580 return target_timer_callback_periodic_restart(cb
, now
);
1582 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1585 static int target_call_timer_callbacks_check_time(int checktime
)
1587 static bool callback_processing
;
1589 /* Do not allow nesting */
1590 if (callback_processing
)
1593 callback_processing
= true;
1598 gettimeofday(&now
, NULL
);
1600 /* Store an address of the place containing a pointer to the
1601 * next item; initially, that's a standalone "root of the
1602 * list" variable. */
1603 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1605 if ((*callback
)->removed
) {
1606 struct target_timer_callback
*p
= *callback
;
1607 *callback
= (*callback
)->next
;
1612 bool call_it
= (*callback
)->callback
&&
1613 ((!checktime
&& (*callback
)->periodic
) ||
1614 now
.tv_sec
> (*callback
)->when
.tv_sec
||
1615 (now
.tv_sec
== (*callback
)->when
.tv_sec
&&
1616 now
.tv_usec
>= (*callback
)->when
.tv_usec
));
1619 target_call_timer_callback(*callback
, &now
);
1621 callback
= &(*callback
)->next
;
1624 callback_processing
= false;
1628 int target_call_timer_callbacks(void)
1630 return target_call_timer_callbacks_check_time(1);
1633 /* invoke periodic callbacks immediately */
1634 int target_call_timer_callbacks_now(void)
1636 return target_call_timer_callbacks_check_time(0);
1639 /* Prints the working area layout for debug purposes */
1640 static void print_wa_layout(struct target
*target
)
1642 struct working_area
*c
= target
->working_areas
;
1645 LOG_DEBUG("%c%c " TARGET_ADDR_FMT
"-" TARGET_ADDR_FMT
" (%" PRIu32
" bytes)",
1646 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1647 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1652 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1653 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1655 assert(area
->free
); /* Shouldn't split an allocated area */
1656 assert(size
<= area
->size
); /* Caller should guarantee this */
1658 /* Split only if not already the right size */
1659 if (size
< area
->size
) {
1660 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1665 new_wa
->next
= area
->next
;
1666 new_wa
->size
= area
->size
- size
;
1667 new_wa
->address
= area
->address
+ size
;
1668 new_wa
->backup
= NULL
;
1669 new_wa
->user
= NULL
;
1670 new_wa
->free
= true;
1672 area
->next
= new_wa
;
1675 /* If backup memory was allocated to this area, it has the wrong size
1676 * now so free it and it will be reallocated if/when needed */
1679 area
->backup
= NULL
;
1684 /* Merge all adjacent free areas into one */
1685 static void target_merge_working_areas(struct target
*target
)
1687 struct working_area
*c
= target
->working_areas
;
1689 while (c
&& c
->next
) {
1690 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1692 /* Find two adjacent free areas */
1693 if (c
->free
&& c
->next
->free
) {
1694 /* Merge the last into the first */
1695 c
->size
+= c
->next
->size
;
1697 /* Remove the last */
1698 struct working_area
*to_be_freed
= c
->next
;
1699 c
->next
= c
->next
->next
;
1700 if (to_be_freed
->backup
)
1701 free(to_be_freed
->backup
);
1704 /* If backup memory was allocated to the remaining area, it's has
1705 * the wrong size now */
1716 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1718 /* Reevaluate working area address based on MMU state*/
1719 if (target
->working_areas
== NULL
) {
1723 retval
= target
->type
->mmu(target
, &enabled
);
1724 if (retval
!= ERROR_OK
)
1728 if (target
->working_area_phys_spec
) {
1729 LOG_DEBUG("MMU disabled, using physical "
1730 "address for working memory " TARGET_ADDR_FMT
,
1731 target
->working_area_phys
);
1732 target
->working_area
= target
->working_area_phys
;
1734 LOG_ERROR("No working memory available. "
1735 "Specify -work-area-phys to target.");
1736 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1739 if (target
->working_area_virt_spec
) {
1740 LOG_DEBUG("MMU enabled, using virtual "
1741 "address for working memory " TARGET_ADDR_FMT
,
1742 target
->working_area_virt
);
1743 target
->working_area
= target
->working_area_virt
;
1745 LOG_ERROR("No working memory available. "
1746 "Specify -work-area-virt to target.");
1747 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1751 /* Set up initial working area on first call */
1752 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1754 new_wa
->next
= NULL
;
1755 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1756 new_wa
->address
= target
->working_area
;
1757 new_wa
->backup
= NULL
;
1758 new_wa
->user
= NULL
;
1759 new_wa
->free
= true;
1762 target
->working_areas
= new_wa
;
1765 /* only allocate multiples of 4 byte */
1767 size
= (size
+ 3) & (~3UL);
1769 struct working_area
*c
= target
->working_areas
;
1771 /* Find the first large enough working area */
1773 if (c
->free
&& c
->size
>= size
)
1779 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1781 /* Split the working area into the requested size */
1782 target_split_working_area(c
, size
);
1784 LOG_DEBUG("allocated new working area of %" PRIu32
" bytes at address " TARGET_ADDR_FMT
,
1787 if (target
->backup_working_area
) {
1788 if (c
->backup
== NULL
) {
1789 c
->backup
= malloc(c
->size
);
1790 if (c
->backup
== NULL
)
1794 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1795 if (retval
!= ERROR_OK
)
1799 /* mark as used, and return the new (reused) area */
1806 print_wa_layout(target
);
1811 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1815 retval
= target_alloc_working_area_try(target
, size
, area
);
1816 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1817 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1822 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1824 int retval
= ERROR_OK
;
1826 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1827 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1828 if (retval
!= ERROR_OK
)
1829 LOG_ERROR("failed to restore %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1830 area
->size
, area
->address
);
1836 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1837 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1839 int retval
= ERROR_OK
;
1845 retval
= target_restore_working_area(target
, area
);
1846 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1847 if (retval
!= ERROR_OK
)
1853 LOG_DEBUG("freed %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1854 area
->size
, area
->address
);
1856 /* mark user pointer invalid */
1857 /* TODO: Is this really safe? It points to some previous caller's memory.
1858 * How could we know that the area pointer is still in that place and not
1859 * some other vital data? What's the purpose of this, anyway? */
1863 target_merge_working_areas(target
);
1865 print_wa_layout(target
);
1870 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1872 return target_free_working_area_restore(target
, area
, 1);
1875 static void target_destroy(struct target
*target
)
1877 if (target
->type
->deinit_target
)
1878 target
->type
->deinit_target(target
);
1881 free(target
->trace_info
);
1882 free(target
->cmd_name
);
1886 void target_quit(void)
1888 struct target_event_callback
*pe
= target_event_callbacks
;
1890 struct target_event_callback
*t
= pe
->next
;
1894 target_event_callbacks
= NULL
;
1896 struct target_timer_callback
*pt
= target_timer_callbacks
;
1898 struct target_timer_callback
*t
= pt
->next
;
1902 target_timer_callbacks
= NULL
;
1904 for (struct target
*target
= all_targets
; target
;) {
1908 target_destroy(target
);
1915 /* free resources and restore memory, if restoring memory fails,
1916 * free up resources anyway
1918 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1920 struct working_area
*c
= target
->working_areas
;
1922 LOG_DEBUG("freeing all working areas");
1924 /* Loop through all areas, restoring the allocated ones and marking them as free */
1928 target_restore_working_area(target
, c
);
1930 *c
->user
= NULL
; /* Same as above */
1936 /* Run a merge pass to combine all areas into one */
1937 target_merge_working_areas(target
);
1939 print_wa_layout(target
);
1942 void target_free_all_working_areas(struct target
*target
)
1944 target_free_all_working_areas_restore(target
, 1);
1947 /* Find the largest number of bytes that can be allocated */
1948 uint32_t target_get_working_area_avail(struct target
*target
)
1950 struct working_area
*c
= target
->working_areas
;
1951 uint32_t max_size
= 0;
1954 return target
->working_area_size
;
1957 if (c
->free
&& max_size
< c
->size
)
1966 int target_arch_state(struct target
*target
)
1969 if (target
== NULL
) {
1970 LOG_WARNING("No target has been configured");
1974 if (target
->state
!= TARGET_HALTED
)
1977 retval
= target
->type
->arch_state(target
);
1981 static int target_get_gdb_fileio_info_default(struct target
*target
,
1982 struct gdb_fileio_info
*fileio_info
)
1984 /* If target does not support semi-hosting function, target
1985 has no need to provide .get_gdb_fileio_info callback.
1986 It just return ERROR_FAIL and gdb_server will return "Txx"
1987 as target halted every time. */
1991 static int target_gdb_fileio_end_default(struct target
*target
,
1992 int retcode
, int fileio_errno
, bool ctrl_c
)
1997 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
1998 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
2000 struct timeval timeout
, now
;
2002 gettimeofday(&timeout
, NULL
);
2003 timeval_add_time(&timeout
, seconds
, 0);
2005 LOG_INFO("Starting profiling. Halting and resuming the"
2006 " target as often as we can...");
2008 uint32_t sample_count
= 0;
2009 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2010 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2012 int retval
= ERROR_OK
;
2014 target_poll(target
);
2015 if (target
->state
== TARGET_HALTED
) {
2016 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
2017 samples
[sample_count
++] = t
;
2018 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2019 retval
= target_resume(target
, 1, 0, 0, 0);
2020 target_poll(target
);
2021 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2022 } else if (target
->state
== TARGET_RUNNING
) {
2023 /* We want to quickly sample the PC. */
2024 retval
= target_halt(target
);
2026 LOG_INFO("Target not halted or running");
2031 if (retval
!= ERROR_OK
)
2034 gettimeofday(&now
, NULL
);
2035 if ((sample_count
>= max_num_samples
) ||
2036 ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
))) {
2037 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2042 *num_samples
= sample_count
;
2046 /* Single aligned words are guaranteed to use 16 or 32 bit access
2047 * mode respectively, otherwise data is handled as quickly as
2050 int target_write_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, const uint8_t *buffer
)
2052 LOG_DEBUG("writing buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2055 if (!target_was_examined(target
)) {
2056 LOG_ERROR("Target not examined yet");
2063 if ((address
+ size
- 1) < address
) {
2064 /* GDB can request this when e.g. PC is 0xfffffffc */
2065 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2071 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2074 static int target_write_buffer_default(struct target
*target
,
2075 target_addr_t address
, uint32_t count
, const uint8_t *buffer
)
2079 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2080 * will have something to do with the size we leave to it. */
2081 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2082 if (address
& size
) {
2083 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2084 if (retval
!= ERROR_OK
)
2092 /* Write the data with as large access size as possible. */
2093 for (; size
> 0; size
/= 2) {
2094 uint32_t aligned
= count
- count
% size
;
2096 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2097 if (retval
!= ERROR_OK
)
2108 /* Single aligned words are guaranteed to use 16 or 32 bit access
2109 * mode respectively, otherwise data is handled as quickly as
2112 int target_read_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
2114 LOG_DEBUG("reading buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2117 if (!target_was_examined(target
)) {
2118 LOG_ERROR("Target not examined yet");
2125 if ((address
+ size
- 1) < address
) {
2126 /* GDB can request this when e.g. PC is 0xfffffffc */
2127 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2133 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2136 static int target_read_buffer_default(struct target
*target
, target_addr_t address
, uint32_t count
, uint8_t *buffer
)
2140 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2141 * will have something to do with the size we leave to it. */
2142 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2143 if (address
& size
) {
2144 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2145 if (retval
!= ERROR_OK
)
2153 /* Read the data with as large access size as possible. */
2154 for (; size
> 0; size
/= 2) {
2155 uint32_t aligned
= count
- count
% size
;
2157 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2158 if (retval
!= ERROR_OK
)
2169 int target_checksum_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* crc
)
2174 uint32_t checksum
= 0;
2175 if (!target_was_examined(target
)) {
2176 LOG_ERROR("Target not examined yet");
2180 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2181 if (retval
!= ERROR_OK
) {
2182 buffer
= malloc(size
);
2183 if (buffer
== NULL
) {
2184 LOG_ERROR("error allocating buffer for section (%" PRId32
" bytes)", size
);
2185 return ERROR_COMMAND_SYNTAX_ERROR
;
2187 retval
= target_read_buffer(target
, address
, size
, buffer
);
2188 if (retval
!= ERROR_OK
) {
2193 /* convert to target endianness */
2194 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2195 uint32_t target_data
;
2196 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2197 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2200 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2209 int target_blank_check_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* blank
,
2210 uint8_t erased_value
)
2213 if (!target_was_examined(target
)) {
2214 LOG_ERROR("Target not examined yet");
2218 if (target
->type
->blank_check_memory
== 0)
2219 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2221 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
, erased_value
);
2226 int target_read_u64(struct target
*target
, target_addr_t address
, uint64_t *value
)
2228 uint8_t value_buf
[8];
2229 if (!target_was_examined(target
)) {
2230 LOG_ERROR("Target not examined yet");
2234 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2236 if (retval
== ERROR_OK
) {
2237 *value
= target_buffer_get_u64(target
, value_buf
);
2238 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2243 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2250 int target_read_u32(struct target
*target
, target_addr_t address
, uint32_t *value
)
2252 uint8_t value_buf
[4];
2253 if (!target_was_examined(target
)) {
2254 LOG_ERROR("Target not examined yet");
2258 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2260 if (retval
== ERROR_OK
) {
2261 *value
= target_buffer_get_u32(target
, value_buf
);
2262 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2267 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2274 int target_read_u16(struct target
*target
, target_addr_t address
, uint16_t *value
)
2276 uint8_t value_buf
[2];
2277 if (!target_was_examined(target
)) {
2278 LOG_ERROR("Target not examined yet");
2282 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2284 if (retval
== ERROR_OK
) {
2285 *value
= target_buffer_get_u16(target
, value_buf
);
2286 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%4.4" PRIx16
,
2291 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2298 int target_read_u8(struct target
*target
, target_addr_t address
, uint8_t *value
)
2300 if (!target_was_examined(target
)) {
2301 LOG_ERROR("Target not examined yet");
2305 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2307 if (retval
== ERROR_OK
) {
2308 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2313 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2320 int target_write_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2323 uint8_t value_buf
[8];
2324 if (!target_was_examined(target
)) {
2325 LOG_ERROR("Target not examined yet");
2329 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2333 target_buffer_set_u64(target
, value_buf
, value
);
2334 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2335 if (retval
!= ERROR_OK
)
2336 LOG_DEBUG("failed: %i", retval
);
2341 int target_write_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2344 uint8_t value_buf
[4];
2345 if (!target_was_examined(target
)) {
2346 LOG_ERROR("Target not examined yet");
2350 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2354 target_buffer_set_u32(target
, value_buf
, value
);
2355 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2356 if (retval
!= ERROR_OK
)
2357 LOG_DEBUG("failed: %i", retval
);
2362 int target_write_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2365 uint8_t value_buf
[2];
2366 if (!target_was_examined(target
)) {
2367 LOG_ERROR("Target not examined yet");
2371 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2375 target_buffer_set_u16(target
, value_buf
, value
);
2376 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2377 if (retval
!= ERROR_OK
)
2378 LOG_DEBUG("failed: %i", retval
);
2383 int target_write_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2386 if (!target_was_examined(target
)) {
2387 LOG_ERROR("Target not examined yet");
2391 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2394 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2395 if (retval
!= ERROR_OK
)
2396 LOG_DEBUG("failed: %i", retval
);
2401 int target_write_phys_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2404 uint8_t value_buf
[8];
2405 if (!target_was_examined(target
)) {
2406 LOG_ERROR("Target not examined yet");
2410 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2414 target_buffer_set_u64(target
, value_buf
, value
);
2415 retval
= target_write_phys_memory(target
, address
, 8, 1, value_buf
);
2416 if (retval
!= ERROR_OK
)
2417 LOG_DEBUG("failed: %i", retval
);
2422 int target_write_phys_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2425 uint8_t value_buf
[4];
2426 if (!target_was_examined(target
)) {
2427 LOG_ERROR("Target not examined yet");
2431 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2435 target_buffer_set_u32(target
, value_buf
, value
);
2436 retval
= target_write_phys_memory(target
, address
, 4, 1, value_buf
);
2437 if (retval
!= ERROR_OK
)
2438 LOG_DEBUG("failed: %i", retval
);
2443 int target_write_phys_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2446 uint8_t value_buf
[2];
2447 if (!target_was_examined(target
)) {
2448 LOG_ERROR("Target not examined yet");
2452 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2456 target_buffer_set_u16(target
, value_buf
, value
);
2457 retval
= target_write_phys_memory(target
, address
, 2, 1, value_buf
);
2458 if (retval
!= ERROR_OK
)
2459 LOG_DEBUG("failed: %i", retval
);
2464 int target_write_phys_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2467 if (!target_was_examined(target
)) {
2468 LOG_ERROR("Target not examined yet");
2472 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2475 retval
= target_write_phys_memory(target
, address
, 1, 1, &value
);
2476 if (retval
!= ERROR_OK
)
2477 LOG_DEBUG("failed: %i", retval
);
2482 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2484 struct target
*target
= get_target(name
);
2485 if (target
== NULL
) {
2486 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2489 if (!target
->tap
->enabled
) {
2490 LOG_USER("Target: TAP %s is disabled, "
2491 "can't be the current target\n",
2492 target
->tap
->dotted_name
);
2496 cmd_ctx
->current_target
= target
->target_number
;
2501 COMMAND_HANDLER(handle_targets_command
)
2503 int retval
= ERROR_OK
;
2504 if (CMD_ARGC
== 1) {
2505 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2506 if (retval
== ERROR_OK
) {
2512 struct target
*target
= all_targets
;
2513 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2514 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2519 if (target
->tap
->enabled
)
2520 state
= target_state_name(target
);
2522 state
= "tap-disabled";
2524 if (CMD_CTX
->current_target
== target
->target_number
)
2527 /* keep columns lined up to match the headers above */
2528 command_print(CMD_CTX
,
2529 "%2d%c %-18s %-10s %-6s %-18s %s",
2530 target
->target_number
,
2532 target_name(target
),
2533 target_type_name(target
),
2534 Jim_Nvp_value2name_simple(nvp_target_endian
,
2535 target
->endianness
)->name
,
2536 target
->tap
->dotted_name
,
2538 target
= target
->next
;
2544 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2546 static int powerDropout
;
2547 static int srstAsserted
;
2549 static int runPowerRestore
;
2550 static int runPowerDropout
;
2551 static int runSrstAsserted
;
2552 static int runSrstDeasserted
;
2554 static int sense_handler(void)
2556 static int prevSrstAsserted
;
2557 static int prevPowerdropout
;
2559 int retval
= jtag_power_dropout(&powerDropout
);
2560 if (retval
!= ERROR_OK
)
2564 powerRestored
= prevPowerdropout
&& !powerDropout
;
2566 runPowerRestore
= 1;
2568 int64_t current
= timeval_ms();
2569 static int64_t lastPower
;
2570 bool waitMore
= lastPower
+ 2000 > current
;
2571 if (powerDropout
&& !waitMore
) {
2572 runPowerDropout
= 1;
2573 lastPower
= current
;
2576 retval
= jtag_srst_asserted(&srstAsserted
);
2577 if (retval
!= ERROR_OK
)
2581 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2583 static int64_t lastSrst
;
2584 waitMore
= lastSrst
+ 2000 > current
;
2585 if (srstDeasserted
&& !waitMore
) {
2586 runSrstDeasserted
= 1;
2590 if (!prevSrstAsserted
&& srstAsserted
)
2591 runSrstAsserted
= 1;
2593 prevSrstAsserted
= srstAsserted
;
2594 prevPowerdropout
= powerDropout
;
2596 if (srstDeasserted
|| powerRestored
) {
2597 /* Other than logging the event we can't do anything here.
2598 * Issuing a reset is a particularly bad idea as we might
2599 * be inside a reset already.
2606 /* process target state changes */
2607 static int handle_target(void *priv
)
2609 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2610 int retval
= ERROR_OK
;
2612 if (!is_jtag_poll_safe()) {
2613 /* polling is disabled currently */
2617 /* we do not want to recurse here... */
2618 static int recursive
;
2622 /* danger! running these procedures can trigger srst assertions and power dropouts.
2623 * We need to avoid an infinite loop/recursion here and we do that by
2624 * clearing the flags after running these events.
2626 int did_something
= 0;
2627 if (runSrstAsserted
) {
2628 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2629 Jim_Eval(interp
, "srst_asserted");
2632 if (runSrstDeasserted
) {
2633 Jim_Eval(interp
, "srst_deasserted");
2636 if (runPowerDropout
) {
2637 LOG_INFO("Power dropout detected, running power_dropout proc.");
2638 Jim_Eval(interp
, "power_dropout");
2641 if (runPowerRestore
) {
2642 Jim_Eval(interp
, "power_restore");
2646 if (did_something
) {
2647 /* clear detect flags */
2651 /* clear action flags */
2653 runSrstAsserted
= 0;
2654 runSrstDeasserted
= 0;
2655 runPowerRestore
= 0;
2656 runPowerDropout
= 0;
2661 /* Poll targets for state changes unless that's globally disabled.
2662 * Skip targets that are currently disabled.
2664 for (struct target
*target
= all_targets
;
2665 is_jtag_poll_safe() && target
;
2666 target
= target
->next
) {
2668 if (!target_was_examined(target
))
2671 if (!target
->tap
->enabled
)
2674 if (target
->backoff
.times
> target
->backoff
.count
) {
2675 /* do not poll this time as we failed previously */
2676 target
->backoff
.count
++;
2679 target
->backoff
.count
= 0;
2681 /* only poll target if we've got power and srst isn't asserted */
2682 if (!powerDropout
&& !srstAsserted
) {
2683 /* polling may fail silently until the target has been examined */
2684 retval
= target_poll(target
);
2685 if (retval
!= ERROR_OK
) {
2686 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2687 if (target
->backoff
.times
* polling_interval
< 5000) {
2688 target
->backoff
.times
*= 2;
2689 target
->backoff
.times
++;
2692 /* Tell GDB to halt the debugger. This allows the user to
2693 * run monitor commands to handle the situation.
2695 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2697 if (target
->backoff
.times
> 0) {
2698 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
2699 target_reset_examined(target
);
2700 retval
= target_examine_one(target
);
2701 /* Target examination could have failed due to unstable connection,
2702 * but we set the examined flag anyway to repoll it later */
2703 if (retval
!= ERROR_OK
) {
2704 target
->examined
= true;
2705 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2706 target
->backoff
.times
* polling_interval
);
2711 /* Since we succeeded, we reset backoff count */
2712 target
->backoff
.times
= 0;
2719 COMMAND_HANDLER(handle_reg_command
)
2721 struct target
*target
;
2722 struct reg
*reg
= NULL
;
2728 target
= get_current_target(CMD_CTX
);
2730 /* list all available registers for the current target */
2731 if (CMD_ARGC
== 0) {
2732 struct reg_cache
*cache
= target
->reg_cache
;
2738 command_print(CMD_CTX
, "===== %s", cache
->name
);
2740 for (i
= 0, reg
= cache
->reg_list
;
2741 i
< cache
->num_regs
;
2742 i
++, reg
++, count
++) {
2743 /* only print cached values if they are valid */
2745 value
= buf_to_str(reg
->value
,
2747 command_print(CMD_CTX
,
2748 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2756 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2761 cache
= cache
->next
;
2767 /* access a single register by its ordinal number */
2768 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2770 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2772 struct reg_cache
*cache
= target
->reg_cache
;
2776 for (i
= 0; i
< cache
->num_regs
; i
++) {
2777 if (count
++ == num
) {
2778 reg
= &cache
->reg_list
[i
];
2784 cache
= cache
->next
;
2788 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2789 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2793 /* access a single register by its name */
2794 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2797 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2802 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2804 /* display a register */
2805 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2806 && (CMD_ARGV
[1][0] <= '9')))) {
2807 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2810 if (reg
->valid
== 0)
2811 reg
->type
->get(reg
);
2812 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2813 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2818 /* set register value */
2819 if (CMD_ARGC
== 2) {
2820 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2823 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2825 reg
->type
->set(reg
, buf
);
2827 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2828 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2836 return ERROR_COMMAND_SYNTAX_ERROR
;
2839 COMMAND_HANDLER(handle_poll_command
)
2841 int retval
= ERROR_OK
;
2842 struct target
*target
= get_current_target(CMD_CTX
);
2844 if (CMD_ARGC
== 0) {
2845 command_print(CMD_CTX
, "background polling: %s",
2846 jtag_poll_get_enabled() ? "on" : "off");
2847 command_print(CMD_CTX
, "TAP: %s (%s)",
2848 target
->tap
->dotted_name
,
2849 target
->tap
->enabled
? "enabled" : "disabled");
2850 if (!target
->tap
->enabled
)
2852 retval
= target_poll(target
);
2853 if (retval
!= ERROR_OK
)
2855 retval
= target_arch_state(target
);
2856 if (retval
!= ERROR_OK
)
2858 } else if (CMD_ARGC
== 1) {
2860 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2861 jtag_poll_set_enabled(enable
);
2863 return ERROR_COMMAND_SYNTAX_ERROR
;
2868 COMMAND_HANDLER(handle_wait_halt_command
)
2871 return ERROR_COMMAND_SYNTAX_ERROR
;
2873 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2874 if (1 == CMD_ARGC
) {
2875 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2876 if (ERROR_OK
!= retval
)
2877 return ERROR_COMMAND_SYNTAX_ERROR
;
2880 struct target
*target
= get_current_target(CMD_CTX
);
2881 return target_wait_state(target
, TARGET_HALTED
, ms
);
2884 /* wait for target state to change. The trick here is to have a low
2885 * latency for short waits and not to suck up all the CPU time
2888 * After 500ms, keep_alive() is invoked
2890 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2893 int64_t then
= 0, cur
;
2897 retval
= target_poll(target
);
2898 if (retval
!= ERROR_OK
)
2900 if (target
->state
== state
)
2905 then
= timeval_ms();
2906 LOG_DEBUG("waiting for target %s...",
2907 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2913 if ((cur
-then
) > ms
) {
2914 LOG_ERROR("timed out while waiting for target %s",
2915 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2923 COMMAND_HANDLER(handle_halt_command
)
2927 struct target
*target
= get_current_target(CMD_CTX
);
2928 int retval
= target_halt(target
);
2929 if (ERROR_OK
!= retval
)
2932 if (CMD_ARGC
== 1) {
2933 unsigned wait_local
;
2934 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2935 if (ERROR_OK
!= retval
)
2936 return ERROR_COMMAND_SYNTAX_ERROR
;
2941 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2944 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2946 struct target
*target
= get_current_target(CMD_CTX
);
2948 LOG_USER("requesting target halt and executing a soft reset");
2950 target_soft_reset_halt(target
);
2955 COMMAND_HANDLER(handle_reset_command
)
2958 return ERROR_COMMAND_SYNTAX_ERROR
;
2960 enum target_reset_mode reset_mode
= RESET_RUN
;
2961 if (CMD_ARGC
== 1) {
2963 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2964 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
2965 return ERROR_COMMAND_SYNTAX_ERROR
;
2966 reset_mode
= n
->value
;
2969 /* reset *all* targets */
2970 return target_process_reset(CMD_CTX
, reset_mode
);
2974 COMMAND_HANDLER(handle_resume_command
)
2978 return ERROR_COMMAND_SYNTAX_ERROR
;
2980 struct target
*target
= get_current_target(CMD_CTX
);
2982 /* with no CMD_ARGV, resume from current pc, addr = 0,
2983 * with one arguments, addr = CMD_ARGV[0],
2984 * handle breakpoints, not debugging */
2985 target_addr_t addr
= 0;
2986 if (CMD_ARGC
== 1) {
2987 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
2991 return target_resume(target
, current
, addr
, 1, 0);
2994 COMMAND_HANDLER(handle_step_command
)
2997 return ERROR_COMMAND_SYNTAX_ERROR
;
3001 /* with no CMD_ARGV, step from current pc, addr = 0,
3002 * with one argument addr = CMD_ARGV[0],
3003 * handle breakpoints, debugging */
3004 target_addr_t addr
= 0;
3006 if (CMD_ARGC
== 1) {
3007 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3011 struct target
*target
= get_current_target(CMD_CTX
);
3013 return target
->type
->step(target
, current_pc
, addr
, 1);
3016 static void handle_md_output(struct command_context
*cmd_ctx
,
3017 struct target
*target
, target_addr_t address
, unsigned size
,
3018 unsigned count
, const uint8_t *buffer
)
3020 const unsigned line_bytecnt
= 32;
3021 unsigned line_modulo
= line_bytecnt
/ size
;
3023 char output
[line_bytecnt
* 4 + 1];
3024 unsigned output_len
= 0;
3026 const char *value_fmt
;
3029 value_fmt
= "%16.16"PRIx64
" ";
3032 value_fmt
= "%8.8"PRIx64
" ";
3035 value_fmt
= "%4.4"PRIx64
" ";
3038 value_fmt
= "%2.2"PRIx64
" ";
3041 /* "can't happen", caller checked */
3042 LOG_ERROR("invalid memory read size: %u", size
);
3046 for (unsigned i
= 0; i
< count
; i
++) {
3047 if (i
% line_modulo
== 0) {
3048 output_len
+= snprintf(output
+ output_len
,
3049 sizeof(output
) - output_len
,
3050 TARGET_ADDR_FMT
": ",
3051 (address
+ (i
* size
)));
3055 const uint8_t *value_ptr
= buffer
+ i
* size
;
3058 value
= target_buffer_get_u64(target
, value_ptr
);
3061 value
= target_buffer_get_u32(target
, value_ptr
);
3064 value
= target_buffer_get_u16(target
, value_ptr
);
3069 output_len
+= snprintf(output
+ output_len
,
3070 sizeof(output
) - output_len
,
3073 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
3074 command_print(cmd_ctx
, "%s", output
);
3080 COMMAND_HANDLER(handle_md_command
)
3083 return ERROR_COMMAND_SYNTAX_ERROR
;
3086 switch (CMD_NAME
[2]) {
3100 return ERROR_COMMAND_SYNTAX_ERROR
;
3103 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3104 int (*fn
)(struct target
*target
,
3105 target_addr_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
3109 fn
= target_read_phys_memory
;
3111 fn
= target_read_memory
;
3112 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
3113 return ERROR_COMMAND_SYNTAX_ERROR
;
3115 target_addr_t address
;
3116 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3120 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
3122 uint8_t *buffer
= calloc(count
, size
);
3124 struct target
*target
= get_current_target(CMD_CTX
);
3125 int retval
= fn(target
, address
, size
, count
, buffer
);
3126 if (ERROR_OK
== retval
)
3127 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
3134 typedef int (*target_write_fn
)(struct target
*target
,
3135 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
3137 static int target_fill_mem(struct target
*target
,
3138 target_addr_t address
,
3146 /* We have to write in reasonably large chunks to be able
3147 * to fill large memory areas with any sane speed */
3148 const unsigned chunk_size
= 16384;
3149 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
3150 if (target_buf
== NULL
) {
3151 LOG_ERROR("Out of memory");
3155 for (unsigned i
= 0; i
< chunk_size
; i
++) {
3156 switch (data_size
) {
3158 target_buffer_set_u64(target
, target_buf
+ i
* data_size
, b
);
3161 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
3164 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
3167 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
3174 int retval
= ERROR_OK
;
3176 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
3179 if (current
> chunk_size
)
3180 current
= chunk_size
;
3181 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
3182 if (retval
!= ERROR_OK
)
3184 /* avoid GDB timeouts */
3193 COMMAND_HANDLER(handle_mw_command
)
3196 return ERROR_COMMAND_SYNTAX_ERROR
;
3197 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3202 fn
= target_write_phys_memory
;
3204 fn
= target_write_memory
;
3205 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
3206 return ERROR_COMMAND_SYNTAX_ERROR
;
3208 target_addr_t address
;
3209 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3211 target_addr_t value
;
3212 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], value
);
3216 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
3218 struct target
*target
= get_current_target(CMD_CTX
);
3220 switch (CMD_NAME
[2]) {
3234 return ERROR_COMMAND_SYNTAX_ERROR
;
3237 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
3240 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
3241 target_addr_t
*min_address
, target_addr_t
*max_address
)
3243 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
3244 return ERROR_COMMAND_SYNTAX_ERROR
;
3246 /* a base address isn't always necessary,
3247 * default to 0x0 (i.e. don't relocate) */
3248 if (CMD_ARGC
>= 2) {
3250 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3251 image
->base_address
= addr
;
3252 image
->base_address_set
= 1;
3254 image
->base_address_set
= 0;
3256 image
->start_address_set
= 0;
3259 COMMAND_PARSE_ADDRESS(CMD_ARGV
[3], *min_address
);
3260 if (CMD_ARGC
== 5) {
3261 COMMAND_PARSE_ADDRESS(CMD_ARGV
[4], *max_address
);
3262 /* use size (given) to find max (required) */
3263 *max_address
+= *min_address
;
3266 if (*min_address
> *max_address
)
3267 return ERROR_COMMAND_SYNTAX_ERROR
;
3272 COMMAND_HANDLER(handle_load_image_command
)
3276 uint32_t image_size
;
3277 target_addr_t min_address
= 0;
3278 target_addr_t max_address
= -1;
3282 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
3283 &image
, &min_address
, &max_address
);
3284 if (ERROR_OK
!= retval
)
3287 struct target
*target
= get_current_target(CMD_CTX
);
3289 struct duration bench
;
3290 duration_start(&bench
);
3292 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
3297 for (i
= 0; i
< image
.num_sections
; i
++) {
3298 buffer
= malloc(image
.sections
[i
].size
);
3299 if (buffer
== NULL
) {
3300 command_print(CMD_CTX
,
3301 "error allocating buffer for section (%d bytes)",
3302 (int)(image
.sections
[i
].size
));
3303 retval
= ERROR_FAIL
;
3307 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3308 if (retval
!= ERROR_OK
) {
3313 uint32_t offset
= 0;
3314 uint32_t length
= buf_cnt
;
3316 /* DANGER!!! beware of unsigned comparision here!!! */
3318 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3319 (image
.sections
[i
].base_address
< max_address
)) {
3321 if (image
.sections
[i
].base_address
< min_address
) {
3322 /* clip addresses below */
3323 offset
+= min_address
-image
.sections
[i
].base_address
;
3327 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3328 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3330 retval
= target_write_buffer(target
,
3331 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3332 if (retval
!= ERROR_OK
) {
3336 image_size
+= length
;
3337 command_print(CMD_CTX
, "%u bytes written at address " TARGET_ADDR_FMT
"",
3338 (unsigned int)length
,
3339 image
.sections
[i
].base_address
+ offset
);
3345 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3346 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
3347 "in %fs (%0.3f KiB/s)", image_size
,
3348 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3351 image_close(&image
);
3357 COMMAND_HANDLER(handle_dump_image_command
)
3359 struct fileio
*fileio
;
3361 int retval
, retvaltemp
;
3362 target_addr_t address
, size
;
3363 struct duration bench
;
3364 struct target
*target
= get_current_target(CMD_CTX
);
3367 return ERROR_COMMAND_SYNTAX_ERROR
;
3369 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], address
);
3370 COMMAND_PARSE_ADDRESS(CMD_ARGV
[2], size
);
3372 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
3373 buffer
= malloc(buf_size
);
3377 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3378 if (retval
!= ERROR_OK
) {
3383 duration_start(&bench
);
3386 size_t size_written
;
3387 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
3388 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
3389 if (retval
!= ERROR_OK
)
3392 retval
= fileio_write(fileio
, this_run_size
, buffer
, &size_written
);
3393 if (retval
!= ERROR_OK
)
3396 size
-= this_run_size
;
3397 address
+= this_run_size
;
3402 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3404 retval
= fileio_size(fileio
, &filesize
);
3405 if (retval
!= ERROR_OK
)
3407 command_print(CMD_CTX
,
3408 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize
,
3409 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3412 retvaltemp
= fileio_close(fileio
);
3413 if (retvaltemp
!= ERROR_OK
)
3422 IMAGE_CHECKSUM_ONLY
= 2
3425 static COMMAND_HELPER(handle_verify_image_command_internal
, enum verify_mode verify
)
3429 uint32_t image_size
;
3432 uint32_t checksum
= 0;
3433 uint32_t mem_checksum
= 0;
3437 struct target
*target
= get_current_target(CMD_CTX
);
3440 return ERROR_COMMAND_SYNTAX_ERROR
;
3443 LOG_ERROR("no target selected");
3447 struct duration bench
;
3448 duration_start(&bench
);
3450 if (CMD_ARGC
>= 2) {
3452 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3453 image
.base_address
= addr
;
3454 image
.base_address_set
= 1;
3456 image
.base_address_set
= 0;
3457 image
.base_address
= 0x0;
3460 image
.start_address_set
= 0;
3462 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3463 if (retval
!= ERROR_OK
)
3469 for (i
= 0; i
< image
.num_sections
; i
++) {
3470 buffer
= malloc(image
.sections
[i
].size
);
3471 if (buffer
== NULL
) {
3472 command_print(CMD_CTX
,
3473 "error allocating buffer for section (%d bytes)",
3474 (int)(image
.sections
[i
].size
));
3477 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3478 if (retval
!= ERROR_OK
) {
3483 if (verify
>= IMAGE_VERIFY
) {
3484 /* calculate checksum of image */
3485 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3486 if (retval
!= ERROR_OK
) {
3491 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3492 if (retval
!= ERROR_OK
) {
3496 if ((checksum
!= mem_checksum
) && (verify
== IMAGE_CHECKSUM_ONLY
)) {
3497 LOG_ERROR("checksum mismatch");
3499 retval
= ERROR_FAIL
;
3502 if (checksum
!= mem_checksum
) {
3503 /* failed crc checksum, fall back to a binary compare */
3507 LOG_ERROR("checksum mismatch - attempting binary compare");
3509 data
= malloc(buf_cnt
);
3511 /* Can we use 32bit word accesses? */
3513 int count
= buf_cnt
;
3514 if ((count
% 4) == 0) {
3518 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
3519 if (retval
== ERROR_OK
) {
3521 for (t
= 0; t
< buf_cnt
; t
++) {
3522 if (data
[t
] != buffer
[t
]) {
3523 command_print(CMD_CTX
,
3524 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3526 (unsigned)(t
+ image
.sections
[i
].base_address
),
3529 if (diffs
++ >= 127) {
3530 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
3542 command_print(CMD_CTX
, "address " TARGET_ADDR_FMT
" length 0x%08zx",
3543 image
.sections
[i
].base_address
,
3548 image_size
+= buf_cnt
;
3551 command_print(CMD_CTX
, "No more differences found.");
3554 retval
= ERROR_FAIL
;
3555 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3556 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3557 "in %fs (%0.3f KiB/s)", image_size
,
3558 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3561 image_close(&image
);
3566 COMMAND_HANDLER(handle_verify_image_checksum_command
)
3568 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_CHECKSUM_ONLY
);
3571 COMMAND_HANDLER(handle_verify_image_command
)
3573 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_VERIFY
);
3576 COMMAND_HANDLER(handle_test_image_command
)
3578 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_TEST
);
3581 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
3583 struct target
*target
= get_current_target(cmd_ctx
);
3584 struct breakpoint
*breakpoint
= target
->breakpoints
;
3585 while (breakpoint
) {
3586 if (breakpoint
->type
== BKPT_SOFT
) {
3587 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3588 breakpoint
->length
, 16);
3589 command_print(cmd_ctx
, "IVA breakpoint: " TARGET_ADDR_FMT
", 0x%x, %i, 0x%s",
3590 breakpoint
->address
,
3592 breakpoint
->set
, buf
);
3595 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3596 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3598 breakpoint
->length
, breakpoint
->set
);
3599 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3600 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3601 breakpoint
->address
,
3602 breakpoint
->length
, breakpoint
->set
);
3603 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3606 command_print(cmd_ctx
, "Breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3607 breakpoint
->address
,
3608 breakpoint
->length
, breakpoint
->set
);
3611 breakpoint
= breakpoint
->next
;
3616 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
3617 target_addr_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3619 struct target
*target
= get_current_target(cmd_ctx
);
3623 retval
= breakpoint_add(target
, addr
, length
, hw
);
3624 if (ERROR_OK
== retval
)
3625 command_print(cmd_ctx
, "breakpoint set at " TARGET_ADDR_FMT
"", addr
);
3627 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3630 } else if (addr
== 0) {
3631 if (target
->type
->add_context_breakpoint
== NULL
) {
3632 LOG_WARNING("Context breakpoint not available");
3635 retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3636 if (ERROR_OK
== retval
)
3637 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3639 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3643 if (target
->type
->add_hybrid_breakpoint
== NULL
) {
3644 LOG_WARNING("Hybrid breakpoint not available");
3647 retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3648 if (ERROR_OK
== retval
)
3649 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3651 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3658 COMMAND_HANDLER(handle_bp_command
)