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
;
109 static struct target_type
*target_types
[] = {
145 struct target
*all_targets
;
146 static struct target_event_callback
*target_event_callbacks
;
147 static struct target_timer_callback
*target_timer_callbacks
;
148 LIST_HEAD(target_reset_callback_list
);
149 LIST_HEAD(target_trace_callback_list
);
150 static const int polling_interval
= 100;
152 static const Jim_Nvp nvp_assert
[] = {
153 { .name
= "assert", NVP_ASSERT
},
154 { .name
= "deassert", NVP_DEASSERT
},
155 { .name
= "T", NVP_ASSERT
},
156 { .name
= "F", NVP_DEASSERT
},
157 { .name
= "t", NVP_ASSERT
},
158 { .name
= "f", NVP_DEASSERT
},
159 { .name
= NULL
, .value
= -1 }
162 static const Jim_Nvp nvp_error_target
[] = {
163 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
164 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
165 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
166 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
167 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
168 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
169 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
170 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
171 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
172 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
173 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
174 { .value
= -1, .name
= NULL
}
177 static const char *target_strerror_safe(int err
)
181 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
188 static const Jim_Nvp nvp_target_event
[] = {
190 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
191 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
192 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
193 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
194 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
196 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
197 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
199 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
200 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
201 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
202 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
203 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
204 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
205 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
206 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
207 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
208 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
209 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
210 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
212 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
213 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
215 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
216 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
218 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
219 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
221 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
222 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
224 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
225 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
227 { .value
= TARGET_EVENT_TRACE_CONFIG
, .name
= "trace-config" },
229 { .name
= NULL
, .value
= -1 }
232 static const Jim_Nvp nvp_target_state
[] = {
233 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
234 { .name
= "running", .value
= TARGET_RUNNING
},
235 { .name
= "halted", .value
= TARGET_HALTED
},
236 { .name
= "reset", .value
= TARGET_RESET
},
237 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
238 { .name
= NULL
, .value
= -1 },
241 static const Jim_Nvp nvp_target_debug_reason
[] = {
242 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
243 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
244 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
245 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
246 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
247 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
248 { .name
= "program-exit" , .value
= DBG_REASON_EXIT
},
249 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
250 { .name
= NULL
, .value
= -1 },
253 static const Jim_Nvp nvp_target_endian
[] = {
254 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
255 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
256 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
257 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
258 { .name
= NULL
, .value
= -1 },
261 static const Jim_Nvp nvp_reset_modes
[] = {
262 { .name
= "unknown", .value
= RESET_UNKNOWN
},
263 { .name
= "run" , .value
= RESET_RUN
},
264 { .name
= "halt" , .value
= RESET_HALT
},
265 { .name
= "init" , .value
= RESET_INIT
},
266 { .name
= NULL
, .value
= -1 },
269 const char *debug_reason_name(struct target
*t
)
273 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
274 t
->debug_reason
)->name
;
276 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
277 cp
= "(*BUG*unknown*BUG*)";
282 const char *target_state_name(struct target
*t
)
285 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
287 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
288 cp
= "(*BUG*unknown*BUG*)";
291 if (!target_was_examined(t
) && t
->defer_examine
)
292 cp
= "examine deferred";
297 const char *target_event_name(enum target_event event
)
300 cp
= Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
;
302 LOG_ERROR("Invalid target event: %d", (int)(event
));
303 cp
= "(*BUG*unknown*BUG*)";
308 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
311 cp
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
;
313 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
314 cp
= "(*BUG*unknown*BUG*)";
319 /* determine the number of the new target */
320 static int new_target_number(void)
325 /* number is 0 based */
329 if (x
< t
->target_number
)
330 x
= t
->target_number
;
336 /* read a uint64_t from a buffer in target memory endianness */
337 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
339 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
340 return le_to_h_u64(buffer
);
342 return be_to_h_u64(buffer
);
345 /* read a uint32_t from a buffer in target memory endianness */
346 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
348 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
349 return le_to_h_u32(buffer
);
351 return be_to_h_u32(buffer
);
354 /* read a uint24_t from a buffer in target memory endianness */
355 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
357 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
358 return le_to_h_u24(buffer
);
360 return be_to_h_u24(buffer
);
363 /* read a uint16_t from a buffer in target memory endianness */
364 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
366 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
367 return le_to_h_u16(buffer
);
369 return be_to_h_u16(buffer
);
372 /* read a uint8_t from a buffer in target memory endianness */
373 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
375 return *buffer
& 0x0ff;
378 /* write a uint64_t to a buffer in target memory endianness */
379 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
381 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
382 h_u64_to_le(buffer
, value
);
384 h_u64_to_be(buffer
, value
);
387 /* write a uint32_t to a buffer in target memory endianness */
388 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
390 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
391 h_u32_to_le(buffer
, value
);
393 h_u32_to_be(buffer
, value
);
396 /* write a uint24_t to a buffer in target memory endianness */
397 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
399 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
400 h_u24_to_le(buffer
, value
);
402 h_u24_to_be(buffer
, value
);
405 /* write a uint16_t to a buffer in target memory endianness */
406 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
408 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
409 h_u16_to_le(buffer
, value
);
411 h_u16_to_be(buffer
, value
);
414 /* write a uint8_t to a buffer in target memory endianness */
415 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
420 /* write a uint64_t array to a buffer in target memory endianness */
421 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
424 for (i
= 0; i
< count
; i
++)
425 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
428 /* write a uint32_t array to a buffer in target memory endianness */
429 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
432 for (i
= 0; i
< count
; i
++)
433 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
436 /* write a uint16_t array to a buffer in target memory endianness */
437 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
440 for (i
= 0; i
< count
; i
++)
441 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
444 /* write a uint64_t array to a buffer in target memory endianness */
445 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
448 for (i
= 0; i
< count
; i
++)
449 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
452 /* write a uint32_t array to a buffer in target memory endianness */
453 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
456 for (i
= 0; i
< count
; i
++)
457 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
460 /* write a uint16_t array to a buffer in target memory endianness */
461 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
464 for (i
= 0; i
< count
; i
++)
465 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
468 /* return a pointer to a configured target; id is name or number */
469 struct target
*get_target(const char *id
)
471 struct target
*target
;
473 /* try as tcltarget name */
474 for (target
= all_targets
; target
; target
= target
->next
) {
475 if (target_name(target
) == NULL
)
477 if (strcmp(id
, target_name(target
)) == 0)
481 /* It's OK to remove this fallback sometime after August 2010 or so */
483 /* no match, try as number */
485 if (parse_uint(id
, &num
) != ERROR_OK
)
488 for (target
= all_targets
; target
; target
= target
->next
) {
489 if (target
->target_number
== (int)num
) {
490 LOG_WARNING("use '%s' as target identifier, not '%u'",
491 target_name(target
), num
);
499 /* returns a pointer to the n-th configured target */
500 struct target
*get_target_by_num(int num
)
502 struct target
*target
= all_targets
;
505 if (target
->target_number
== num
)
507 target
= target
->next
;
513 struct target
*get_current_target(struct command_context
*cmd_ctx
)
515 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
517 if (target
== NULL
) {
518 LOG_ERROR("BUG: current_target out of bounds");
525 int target_poll(struct target
*target
)
529 /* We can't poll until after examine */
530 if (!target_was_examined(target
)) {
531 /* Fail silently lest we pollute the log */
535 retval
= target
->type
->poll(target
);
536 if (retval
!= ERROR_OK
)
539 if (target
->halt_issued
) {
540 if (target
->state
== TARGET_HALTED
)
541 target
->halt_issued
= false;
543 int64_t t
= timeval_ms() - target
->halt_issued_time
;
544 if (t
> DEFAULT_HALT_TIMEOUT
) {
545 target
->halt_issued
= false;
546 LOG_INFO("Halt timed out, wake up GDB.");
547 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
555 int target_halt(struct target
*target
)
558 /* We can't poll until after examine */
559 if (!target_was_examined(target
)) {
560 LOG_ERROR("Target not examined yet");
564 retval
= target
->type
->halt(target
);
565 if (retval
!= ERROR_OK
)
568 target
->halt_issued
= true;
569 target
->halt_issued_time
= timeval_ms();
575 * Make the target (re)start executing using its saved execution
576 * context (possibly with some modifications).
578 * @param target Which target should start executing.
579 * @param current True to use the target's saved program counter instead
580 * of the address parameter
581 * @param address Optionally used as the program counter.
582 * @param handle_breakpoints True iff breakpoints at the resumption PC
583 * should be skipped. (For example, maybe execution was stopped by
584 * such a breakpoint, in which case it would be counterprodutive to
586 * @param debug_execution False if all working areas allocated by OpenOCD
587 * should be released and/or restored to their original contents.
588 * (This would for example be true to run some downloaded "helper"
589 * algorithm code, which resides in one such working buffer and uses
590 * another for data storage.)
592 * @todo Resolve the ambiguity about what the "debug_execution" flag
593 * signifies. For example, Target implementations don't agree on how
594 * it relates to invalidation of the register cache, or to whether
595 * breakpoints and watchpoints should be enabled. (It would seem wrong
596 * to enable breakpoints when running downloaded "helper" algorithms
597 * (debug_execution true), since the breakpoints would be set to match
598 * target firmware being debugged, not the helper algorithm.... and
599 * enabling them could cause such helpers to malfunction (for example,
600 * by overwriting data with a breakpoint instruction. On the other
601 * hand the infrastructure for running such helpers might use this
602 * procedure but rely on hardware breakpoint to detect termination.)
604 int target_resume(struct target
*target
, int current
, target_addr_t address
,
605 int handle_breakpoints
, int debug_execution
)
609 /* We can't poll until after examine */
610 if (!target_was_examined(target
)) {
611 LOG_ERROR("Target not examined yet");
615 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
617 /* note that resume *must* be asynchronous. The CPU can halt before
618 * we poll. The CPU can even halt at the current PC as a result of
619 * a software breakpoint being inserted by (a bug?) the application.
621 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
622 if (retval
!= ERROR_OK
)
625 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
630 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
635 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
636 if (n
->name
== NULL
) {
637 LOG_ERROR("invalid reset mode");
641 struct target
*target
;
642 for (target
= all_targets
; target
; target
= target
->next
)
643 target_call_reset_callbacks(target
, reset_mode
);
645 /* disable polling during reset to make reset event scripts
646 * more predictable, i.e. dr/irscan & pathmove in events will
647 * not have JTAG operations injected into the middle of a sequence.
649 bool save_poll
= jtag_poll_get_enabled();
651 jtag_poll_set_enabled(false);
653 sprintf(buf
, "ocd_process_reset %s", n
->name
);
654 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
656 jtag_poll_set_enabled(save_poll
);
658 if (retval
!= JIM_OK
) {
659 Jim_MakeErrorMessage(cmd_ctx
->interp
);
660 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
664 /* We want any events to be processed before the prompt */
665 retval
= target_call_timer_callbacks_now();
667 for (target
= all_targets
; target
; target
= target
->next
) {
668 target
->type
->check_reset(target
);
669 target
->running_alg
= false;
675 static int identity_virt2phys(struct target
*target
,
676 target_addr_t
virtual, target_addr_t
*physical
)
682 static int no_mmu(struct target
*target
, int *enabled
)
688 static int default_examine(struct target
*target
)
690 target_set_examined(target
);
694 /* no check by default */
695 static int default_check_reset(struct target
*target
)
700 int target_examine_one(struct target
*target
)
702 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
704 int retval
= target
->type
->examine(target
);
705 if (retval
!= ERROR_OK
)
708 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
713 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
715 struct target
*target
= priv
;
717 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
720 jtag_unregister_event_callback(jtag_enable_callback
, target
);
722 return target_examine_one(target
);
725 /* Targets that correctly implement init + examine, i.e.
726 * no communication with target during init:
730 int target_examine(void)
732 int retval
= ERROR_OK
;
733 struct target
*target
;
735 for (target
= all_targets
; target
; target
= target
->next
) {
736 /* defer examination, but don't skip it */
737 if (!target
->tap
->enabled
) {
738 jtag_register_event_callback(jtag_enable_callback
,
743 if (target
->defer_examine
)
746 retval
= target_examine_one(target
);
747 if (retval
!= ERROR_OK
)
753 const char *target_type_name(struct target
*target
)
755 return target
->type
->name
;
758 static int target_soft_reset_halt(struct target
*target
)
760 if (!target_was_examined(target
)) {
761 LOG_ERROR("Target not examined yet");
764 if (!target
->type
->soft_reset_halt
) {
765 LOG_ERROR("Target %s does not support soft_reset_halt",
766 target_name(target
));
769 return target
->type
->soft_reset_halt(target
);
773 * Downloads a target-specific native code algorithm to the target,
774 * and executes it. * Note that some targets may need to set up, enable,
775 * and tear down a breakpoint (hard or * soft) to detect algorithm
776 * termination, while others may support lower overhead schemes where
777 * soft breakpoints embedded in the algorithm automatically terminate the
780 * @param target used to run the algorithm
781 * @param arch_info target-specific description of the algorithm.
783 int target_run_algorithm(struct target
*target
,
784 int num_mem_params
, struct mem_param
*mem_params
,
785 int num_reg_params
, struct reg_param
*reg_param
,
786 uint32_t entry_point
, uint32_t exit_point
,
787 int timeout_ms
, void *arch_info
)
789 int retval
= ERROR_FAIL
;
791 if (!target_was_examined(target
)) {
792 LOG_ERROR("Target not examined yet");
795 if (!target
->type
->run_algorithm
) {
796 LOG_ERROR("Target type '%s' does not support %s",
797 target_type_name(target
), __func__
);
801 target
->running_alg
= true;
802 retval
= target
->type
->run_algorithm(target
,
803 num_mem_params
, mem_params
,
804 num_reg_params
, reg_param
,
805 entry_point
, exit_point
, timeout_ms
, arch_info
);
806 target
->running_alg
= false;
813 * Downloads a target-specific native code algorithm to the target,
814 * executes and leaves it running.
816 * @param target used to run the algorithm
817 * @param arch_info target-specific description of the algorithm.
819 int target_start_algorithm(struct target
*target
,
820 int num_mem_params
, struct mem_param
*mem_params
,
821 int num_reg_params
, struct reg_param
*reg_params
,
822 uint32_t entry_point
, uint32_t exit_point
,
825 int retval
= ERROR_FAIL
;
827 if (!target_was_examined(target
)) {
828 LOG_ERROR("Target not examined yet");
831 if (!target
->type
->start_algorithm
) {
832 LOG_ERROR("Target type '%s' does not support %s",
833 target_type_name(target
), __func__
);
836 if (target
->running_alg
) {
837 LOG_ERROR("Target is already running an algorithm");
841 target
->running_alg
= true;
842 retval
= target
->type
->start_algorithm(target
,
843 num_mem_params
, mem_params
,
844 num_reg_params
, reg_params
,
845 entry_point
, exit_point
, arch_info
);
852 * Waits for an algorithm started with target_start_algorithm() to complete.
854 * @param target used to run the algorithm
855 * @param arch_info target-specific description of the algorithm.
857 int target_wait_algorithm(struct target
*target
,
858 int num_mem_params
, struct mem_param
*mem_params
,
859 int num_reg_params
, struct reg_param
*reg_params
,
860 uint32_t exit_point
, int timeout_ms
,
863 int retval
= ERROR_FAIL
;
865 if (!target
->type
->wait_algorithm
) {
866 LOG_ERROR("Target type '%s' does not support %s",
867 target_type_name(target
), __func__
);
870 if (!target
->running_alg
) {
871 LOG_ERROR("Target is not running an algorithm");
875 retval
= target
->type
->wait_algorithm(target
,
876 num_mem_params
, mem_params
,
877 num_reg_params
, reg_params
,
878 exit_point
, timeout_ms
, arch_info
);
879 if (retval
!= ERROR_TARGET_TIMEOUT
)
880 target
->running_alg
= false;
887 * Executes a target-specific native code algorithm in the target.
888 * It differs from target_run_algorithm in that the algorithm is asynchronous.
889 * Because of this it requires an compliant algorithm:
890 * see contrib/loaders/flash/stm32f1x.S for example.
892 * @param target used to run the algorithm
895 int target_run_flash_async_algorithm(struct target
*target
,
896 const uint8_t *buffer
, uint32_t count
, int block_size
,
897 int num_mem_params
, struct mem_param
*mem_params
,
898 int num_reg_params
, struct reg_param
*reg_params
,
899 uint32_t buffer_start
, uint32_t buffer_size
,
900 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
905 const uint8_t *buffer_orig
= buffer
;
907 /* Set up working area. First word is write pointer, second word is read pointer,
908 * rest is fifo data area. */
909 uint32_t wp_addr
= buffer_start
;
910 uint32_t rp_addr
= buffer_start
+ 4;
911 uint32_t fifo_start_addr
= buffer_start
+ 8;
912 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
914 uint32_t wp
= fifo_start_addr
;
915 uint32_t rp
= fifo_start_addr
;
917 /* validate block_size is 2^n */
918 assert(!block_size
|| !(block_size
& (block_size
- 1)));
920 retval
= target_write_u32(target
, wp_addr
, wp
);
921 if (retval
!= ERROR_OK
)
923 retval
= target_write_u32(target
, rp_addr
, rp
);
924 if (retval
!= ERROR_OK
)
927 /* Start up algorithm on target and let it idle while writing the first chunk */
928 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
929 num_reg_params
, reg_params
,
934 if (retval
!= ERROR_OK
) {
935 LOG_ERROR("error starting target flash write algorithm");
941 retval
= target_read_u32(target
, rp_addr
, &rp
);
942 if (retval
!= ERROR_OK
) {
943 LOG_ERROR("failed to get read pointer");
947 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
948 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
951 LOG_ERROR("flash write algorithm aborted by target");
952 retval
= ERROR_FLASH_OPERATION_FAILED
;
956 if (((rp
- fifo_start_addr
) & (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
957 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
961 /* Count the number of bytes available in the fifo without
962 * crossing the wrap around. Make sure to not fill it completely,
963 * because that would make wp == rp and that's the empty condition. */
964 uint32_t thisrun_bytes
;
966 thisrun_bytes
= rp
- wp
- block_size
;
967 else if (rp
> fifo_start_addr
)
968 thisrun_bytes
= fifo_end_addr
- wp
;
970 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
972 if (thisrun_bytes
== 0) {
973 /* Throttle polling a bit if transfer is (much) faster than flash
974 * programming. The exact delay shouldn't matter as long as it's
975 * less than buffer size / flash speed. This is very unlikely to
976 * run when using high latency connections such as USB. */
979 /* to stop an infinite loop on some targets check and increment a timeout
980 * this issue was observed on a stellaris using the new ICDI interface */
981 if (timeout
++ >= 500) {
982 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
983 return ERROR_FLASH_OPERATION_FAILED
;
988 /* reset our timeout */
991 /* Limit to the amount of data we actually want to write */
992 if (thisrun_bytes
> count
* block_size
)
993 thisrun_bytes
= count
* block_size
;
995 /* Write data to fifo */
996 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
997 if (retval
!= ERROR_OK
)
1000 /* Update counters and wrap write pointer */
1001 buffer
+= thisrun_bytes
;
1002 count
-= thisrun_bytes
/ block_size
;
1003 wp
+= thisrun_bytes
;
1004 if (wp
>= fifo_end_addr
)
1005 wp
= fifo_start_addr
;
1007 /* Store updated write pointer to target */
1008 retval
= target_write_u32(target
, wp_addr
, wp
);
1009 if (retval
!= ERROR_OK
)
1013 if (retval
!= ERROR_OK
) {
1014 /* abort flash write algorithm on target */
1015 target_write_u32(target
, wp_addr
, 0);
1018 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1019 num_reg_params
, reg_params
,
1024 if (retval2
!= ERROR_OK
) {
1025 LOG_ERROR("error waiting for target flash write algorithm");
1029 if (retval
== ERROR_OK
) {
1030 /* check if algorithm set rp = 0 after fifo writer loop finished */
1031 retval
= target_read_u32(target
, rp_addr
, &rp
);
1032 if (retval
== ERROR_OK
&& rp
== 0) {
1033 LOG_ERROR("flash write algorithm aborted by target");
1034 retval
= ERROR_FLASH_OPERATION_FAILED
;
1041 int target_read_memory(struct target
*target
,
1042 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1044 if (!target_was_examined(target
)) {
1045 LOG_ERROR("Target not examined yet");
1048 if (!target
->type
->read_memory
) {
1049 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1052 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1055 int target_read_phys_memory(struct target
*target
,
1056 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1058 if (!target_was_examined(target
)) {
1059 LOG_ERROR("Target not examined yet");
1062 if (!target
->type
->read_phys_memory
) {
1063 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1066 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1069 int target_write_memory(struct target
*target
,
1070 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1072 if (!target_was_examined(target
)) {
1073 LOG_ERROR("Target not examined yet");
1076 if (!target
->type
->write_memory
) {
1077 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1080 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1083 int target_write_phys_memory(struct target
*target
,
1084 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1086 if (!target_was_examined(target
)) {
1087 LOG_ERROR("Target not examined yet");
1090 if (!target
->type
->write_phys_memory
) {
1091 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1094 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1097 int target_add_breakpoint(struct target
*target
,
1098 struct breakpoint
*breakpoint
)
1100 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1101 LOG_WARNING("target %s is not halted", target_name(target
));
1102 return ERROR_TARGET_NOT_HALTED
;
1104 return target
->type
->add_breakpoint(target
, breakpoint
);
1107 int target_add_context_breakpoint(struct target
*target
,
1108 struct breakpoint
*breakpoint
)
1110 if (target
->state
!= TARGET_HALTED
) {
1111 LOG_WARNING("target %s is not halted", target_name(target
));
1112 return ERROR_TARGET_NOT_HALTED
;
1114 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1117 int target_add_hybrid_breakpoint(struct target
*target
,
1118 struct breakpoint
*breakpoint
)
1120 if (target
->state
!= TARGET_HALTED
) {
1121 LOG_WARNING("target %s is not halted", target_name(target
));
1122 return ERROR_TARGET_NOT_HALTED
;
1124 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1127 int target_remove_breakpoint(struct target
*target
,
1128 struct breakpoint
*breakpoint
)
1130 return target
->type
->remove_breakpoint(target
, breakpoint
);
1133 int target_add_watchpoint(struct target
*target
,
1134 struct watchpoint
*watchpoint
)
1136 if (target
->state
!= TARGET_HALTED
) {
1137 LOG_WARNING("target %s is not halted", target_name(target
));
1138 return ERROR_TARGET_NOT_HALTED
;
1140 return target
->type
->add_watchpoint(target
, watchpoint
);
1142 int target_remove_watchpoint(struct target
*target
,
1143 struct watchpoint
*watchpoint
)
1145 return target
->type
->remove_watchpoint(target
, watchpoint
);
1147 int target_hit_watchpoint(struct target
*target
,
1148 struct watchpoint
**hit_watchpoint
)
1150 if (target
->state
!= TARGET_HALTED
) {
1151 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1152 return ERROR_TARGET_NOT_HALTED
;
1155 if (target
->type
->hit_watchpoint
== NULL
) {
1156 /* For backward compatible, if hit_watchpoint is not implemented,
1157 * return ERROR_FAIL such that gdb_server will not take the nonsense
1162 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1165 int target_get_gdb_reg_list(struct target
*target
,
1166 struct reg
**reg_list
[], int *reg_list_size
,
1167 enum target_register_class reg_class
)
1169 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1171 int target_step(struct target
*target
,
1172 int current
, target_addr_t address
, int handle_breakpoints
)
1174 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1177 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1179 if (target
->state
!= TARGET_HALTED
) {
1180 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1181 return ERROR_TARGET_NOT_HALTED
;
1183 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1186 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1188 if (target
->state
!= TARGET_HALTED
) {
1189 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1190 return ERROR_TARGET_NOT_HALTED
;
1192 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1195 int target_profiling(struct target
*target
, uint32_t *samples
,
1196 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1198 if (target
->state
!= TARGET_HALTED
) {
1199 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1200 return ERROR_TARGET_NOT_HALTED
;
1202 return target
->type
->profiling(target
, samples
, max_num_samples
,
1203 num_samples
, seconds
);
1207 * Reset the @c examined flag for the given target.
1208 * Pure paranoia -- targets are zeroed on allocation.
1210 static void target_reset_examined(struct target
*target
)
1212 target
->examined
= false;
1215 static int handle_target(void *priv
);
1217 static int target_init_one(struct command_context
*cmd_ctx
,
1218 struct target
*target
)
1220 target_reset_examined(target
);
1222 struct target_type
*type
= target
->type
;
1223 if (type
->examine
== NULL
)
1224 type
->examine
= default_examine
;
1226 if (type
->check_reset
== NULL
)
1227 type
->check_reset
= default_check_reset
;
1229 assert(type
->init_target
!= NULL
);
1231 int retval
= type
->init_target(cmd_ctx
, target
);
1232 if (ERROR_OK
!= retval
) {
1233 LOG_ERROR("target '%s' init failed", target_name(target
));
1237 /* Sanity-check MMU support ... stub in what we must, to help
1238 * implement it in stages, but warn if we need to do so.
1241 if (type
->virt2phys
== NULL
) {
1242 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1243 type
->virt2phys
= identity_virt2phys
;
1246 /* Make sure no-MMU targets all behave the same: make no
1247 * distinction between physical and virtual addresses, and
1248 * ensure that virt2phys() is always an identity mapping.
1250 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1251 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1254 type
->write_phys_memory
= type
->write_memory
;
1255 type
->read_phys_memory
= type
->read_memory
;
1256 type
->virt2phys
= identity_virt2phys
;
1259 if (target
->type
->read_buffer
== NULL
)
1260 target
->type
->read_buffer
= target_read_buffer_default
;
1262 if (target
->type
->write_buffer
== NULL
)
1263 target
->type
->write_buffer
= target_write_buffer_default
;
1265 if (target
->type
->get_gdb_fileio_info
== NULL
)
1266 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1268 if (target
->type
->gdb_fileio_end
== NULL
)
1269 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1271 if (target
->type
->profiling
== NULL
)
1272 target
->type
->profiling
= target_profiling_default
;
1277 static int target_init(struct command_context
*cmd_ctx
)
1279 struct target
*target
;
1282 for (target
= all_targets
; target
; target
= target
->next
) {
1283 retval
= target_init_one(cmd_ctx
, target
);
1284 if (ERROR_OK
!= retval
)
1291 retval
= target_register_user_commands(cmd_ctx
);
1292 if (ERROR_OK
!= retval
)
1295 retval
= target_register_timer_callback(&handle_target
,
1296 polling_interval
, 1, cmd_ctx
->interp
);
1297 if (ERROR_OK
!= retval
)
1303 COMMAND_HANDLER(handle_target_init_command
)
1308 return ERROR_COMMAND_SYNTAX_ERROR
;
1310 static bool target_initialized
;
1311 if (target_initialized
) {
1312 LOG_INFO("'target init' has already been called");
1315 target_initialized
= true;
1317 retval
= command_run_line(CMD_CTX
, "init_targets");
1318 if (ERROR_OK
!= retval
)
1321 retval
= command_run_line(CMD_CTX
, "init_target_events");
1322 if (ERROR_OK
!= retval
)
1325 retval
= command_run_line(CMD_CTX
, "init_board");
1326 if (ERROR_OK
!= retval
)
1329 LOG_DEBUG("Initializing targets...");
1330 return target_init(CMD_CTX
);
1333 int target_register_event_callback(int (*callback
)(struct target
*target
,
1334 enum target_event event
, void *priv
), void *priv
)
1336 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1338 if (callback
== NULL
)
1339 return ERROR_COMMAND_SYNTAX_ERROR
;
1342 while ((*callbacks_p
)->next
)
1343 callbacks_p
= &((*callbacks_p
)->next
);
1344 callbacks_p
= &((*callbacks_p
)->next
);
1347 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1348 (*callbacks_p
)->callback
= callback
;
1349 (*callbacks_p
)->priv
= priv
;
1350 (*callbacks_p
)->next
= NULL
;
1355 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1356 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1358 struct target_reset_callback
*entry
;
1360 if (callback
== NULL
)
1361 return ERROR_COMMAND_SYNTAX_ERROR
;
1363 entry
= malloc(sizeof(struct target_reset_callback
));
1364 if (entry
== NULL
) {
1365 LOG_ERROR("error allocating buffer for reset callback entry");
1366 return ERROR_COMMAND_SYNTAX_ERROR
;
1369 entry
->callback
= callback
;
1371 list_add(&entry
->list
, &target_reset_callback_list
);
1377 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1378 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1380 struct target_trace_callback
*entry
;
1382 if (callback
== NULL
)
1383 return ERROR_COMMAND_SYNTAX_ERROR
;
1385 entry
= malloc(sizeof(struct target_trace_callback
));
1386 if (entry
== NULL
) {
1387 LOG_ERROR("error allocating buffer for trace callback entry");
1388 return ERROR_COMMAND_SYNTAX_ERROR
;
1391 entry
->callback
= callback
;
1393 list_add(&entry
->list
, &target_trace_callback_list
);
1399 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1401 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1404 if (callback
== NULL
)
1405 return ERROR_COMMAND_SYNTAX_ERROR
;
1408 while ((*callbacks_p
)->next
)
1409 callbacks_p
= &((*callbacks_p
)->next
);
1410 callbacks_p
= &((*callbacks_p
)->next
);
1413 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1414 (*callbacks_p
)->callback
= callback
;
1415 (*callbacks_p
)->periodic
= periodic
;
1416 (*callbacks_p
)->time_ms
= time_ms
;
1417 (*callbacks_p
)->removed
= false;
1419 gettimeofday(&now
, NULL
);
1420 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
1421 time_ms
-= (time_ms
% 1000);
1422 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
1423 if ((*callbacks_p
)->when
.tv_usec
> 1000000) {
1424 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
1425 (*callbacks_p
)->when
.tv_sec
+= 1;
1428 (*callbacks_p
)->priv
= priv
;
1429 (*callbacks_p
)->next
= NULL
;
1434 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1435 enum target_event event
, void *priv
), void *priv
)
1437 struct target_event_callback
**p
= &target_event_callbacks
;
1438 struct target_event_callback
*c
= target_event_callbacks
;
1440 if (callback
== NULL
)
1441 return ERROR_COMMAND_SYNTAX_ERROR
;
1444 struct target_event_callback
*next
= c
->next
;
1445 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1457 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1458 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1460 struct target_reset_callback
*entry
;
1462 if (callback
== NULL
)
1463 return ERROR_COMMAND_SYNTAX_ERROR
;
1465 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1466 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1467 list_del(&entry
->list
);
1476 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1477 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1479 struct target_trace_callback
*entry
;
1481 if (callback
== NULL
)
1482 return ERROR_COMMAND_SYNTAX_ERROR
;
1484 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1485 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1486 list_del(&entry
->list
);
1495 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1497 if (callback
== NULL
)
1498 return ERROR_COMMAND_SYNTAX_ERROR
;
1500 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1502 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1511 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1513 struct target_event_callback
*callback
= target_event_callbacks
;
1514 struct target_event_callback
*next_callback
;
1516 if (event
== TARGET_EVENT_HALTED
) {
1517 /* execute early halted first */
1518 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1521 LOG_DEBUG("target event %i (%s)", event
,
1522 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1524 target_handle_event(target
, event
);
1527 next_callback
= callback
->next
;
1528 callback
->callback(target
, event
, callback
->priv
);
1529 callback
= next_callback
;
1535 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1537 struct target_reset_callback
*callback
;
1539 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1540 Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1542 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1543 callback
->callback(target
, reset_mode
, callback
->priv
);
1548 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1550 struct target_trace_callback
*callback
;
1552 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1553 callback
->callback(target
, len
, data
, callback
->priv
);
1558 static int target_timer_callback_periodic_restart(
1559 struct target_timer_callback
*cb
, struct timeval
*now
)
1561 int time_ms
= cb
->time_ms
;
1562 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1563 time_ms
-= (time_ms
% 1000);
1564 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1565 if (cb
->when
.tv_usec
> 1000000) {
1566 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1567 cb
->when
.tv_sec
+= 1;
1572 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1573 struct timeval
*now
)
1575 cb
->callback(cb
->priv
);
1578 return target_timer_callback_periodic_restart(cb
, now
);
1580 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1583 static int target_call_timer_callbacks_check_time(int checktime
)
1585 static bool callback_processing
;
1587 /* Do not allow nesting */
1588 if (callback_processing
)
1591 callback_processing
= true;
1596 gettimeofday(&now
, NULL
);
1598 /* Store an address of the place containing a pointer to the
1599 * next item; initially, that's a standalone "root of the
1600 * list" variable. */
1601 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1603 if ((*callback
)->removed
) {
1604 struct target_timer_callback
*p
= *callback
;
1605 *callback
= (*callback
)->next
;
1610 bool call_it
= (*callback
)->callback
&&
1611 ((!checktime
&& (*callback
)->periodic
) ||
1612 now
.tv_sec
> (*callback
)->when
.tv_sec
||
1613 (now
.tv_sec
== (*callback
)->when
.tv_sec
&&
1614 now
.tv_usec
>= (*callback
)->when
.tv_usec
));
1617 target_call_timer_callback(*callback
, &now
);
1619 callback
= &(*callback
)->next
;
1622 callback_processing
= false;
1626 int target_call_timer_callbacks(void)
1628 return target_call_timer_callbacks_check_time(1);
1631 /* invoke periodic callbacks immediately */
1632 int target_call_timer_callbacks_now(void)
1634 return target_call_timer_callbacks_check_time(0);
1637 /* Prints the working area layout for debug purposes */
1638 static void print_wa_layout(struct target
*target
)
1640 struct working_area
*c
= target
->working_areas
;
1643 LOG_DEBUG("%c%c " TARGET_ADDR_FMT
"-" TARGET_ADDR_FMT
" (%" PRIu32
" bytes)",
1644 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1645 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1650 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1651 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1653 assert(area
->free
); /* Shouldn't split an allocated area */
1654 assert(size
<= area
->size
); /* Caller should guarantee this */
1656 /* Split only if not already the right size */
1657 if (size
< area
->size
) {
1658 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1663 new_wa
->next
= area
->next
;
1664 new_wa
->size
= area
->size
- size
;
1665 new_wa
->address
= area
->address
+ size
;
1666 new_wa
->backup
= NULL
;
1667 new_wa
->user
= NULL
;
1668 new_wa
->free
= true;
1670 area
->next
= new_wa
;
1673 /* If backup memory was allocated to this area, it has the wrong size
1674 * now so free it and it will be reallocated if/when needed */
1677 area
->backup
= NULL
;
1682 /* Merge all adjacent free areas into one */
1683 static void target_merge_working_areas(struct target
*target
)
1685 struct working_area
*c
= target
->working_areas
;
1687 while (c
&& c
->next
) {
1688 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1690 /* Find two adjacent free areas */
1691 if (c
->free
&& c
->next
->free
) {
1692 /* Merge the last into the first */
1693 c
->size
+= c
->next
->size
;
1695 /* Remove the last */
1696 struct working_area
*to_be_freed
= c
->next
;
1697 c
->next
= c
->next
->next
;
1698 if (to_be_freed
->backup
)
1699 free(to_be_freed
->backup
);
1702 /* If backup memory was allocated to the remaining area, it's has
1703 * the wrong size now */
1714 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1716 /* Reevaluate working area address based on MMU state*/
1717 if (target
->working_areas
== NULL
) {
1721 retval
= target
->type
->mmu(target
, &enabled
);
1722 if (retval
!= ERROR_OK
)
1726 if (target
->working_area_phys_spec
) {
1727 LOG_DEBUG("MMU disabled, using physical "
1728 "address for working memory " TARGET_ADDR_FMT
,
1729 target
->working_area_phys
);
1730 target
->working_area
= target
->working_area_phys
;
1732 LOG_ERROR("No working memory available. "
1733 "Specify -work-area-phys to target.");
1734 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1737 if (target
->working_area_virt_spec
) {
1738 LOG_DEBUG("MMU enabled, using virtual "
1739 "address for working memory " TARGET_ADDR_FMT
,
1740 target
->working_area_virt
);
1741 target
->working_area
= target
->working_area_virt
;
1743 LOG_ERROR("No working memory available. "
1744 "Specify -work-area-virt to target.");
1745 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1749 /* Set up initial working area on first call */
1750 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1752 new_wa
->next
= NULL
;
1753 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1754 new_wa
->address
= target
->working_area
;
1755 new_wa
->backup
= NULL
;
1756 new_wa
->user
= NULL
;
1757 new_wa
->free
= true;
1760 target
->working_areas
= new_wa
;
1763 /* only allocate multiples of 4 byte */
1765 size
= (size
+ 3) & (~3UL);
1767 struct working_area
*c
= target
->working_areas
;
1769 /* Find the first large enough working area */
1771 if (c
->free
&& c
->size
>= size
)
1777 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1779 /* Split the working area into the requested size */
1780 target_split_working_area(c
, size
);
1782 LOG_DEBUG("allocated new working area of %" PRIu32
" bytes at address " TARGET_ADDR_FMT
,
1785 if (target
->backup_working_area
) {
1786 if (c
->backup
== NULL
) {
1787 c
->backup
= malloc(c
->size
);
1788 if (c
->backup
== NULL
)
1792 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1793 if (retval
!= ERROR_OK
)
1797 /* mark as used, and return the new (reused) area */
1804 print_wa_layout(target
);
1809 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1813 retval
= target_alloc_working_area_try(target
, size
, area
);
1814 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1815 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1820 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1822 int retval
= ERROR_OK
;
1824 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1825 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1826 if (retval
!= ERROR_OK
)
1827 LOG_ERROR("failed to restore %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1828 area
->size
, area
->address
);
1834 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1835 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1837 int retval
= ERROR_OK
;
1843 retval
= target_restore_working_area(target
, area
);
1844 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1845 if (retval
!= ERROR_OK
)
1851 LOG_DEBUG("freed %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1852 area
->size
, area
->address
);
1854 /* mark user pointer invalid */
1855 /* TODO: Is this really safe? It points to some previous caller's memory.
1856 * How could we know that the area pointer is still in that place and not
1857 * some other vital data? What's the purpose of this, anyway? */
1861 target_merge_working_areas(target
);
1863 print_wa_layout(target
);
1868 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1870 return target_free_working_area_restore(target
, area
, 1);
1873 void target_quit(void)
1875 struct target_event_callback
*pe
= target_event_callbacks
;
1877 struct target_event_callback
*t
= pe
->next
;
1881 target_event_callbacks
= NULL
;
1883 struct target_timer_callback
*pt
= target_timer_callbacks
;
1885 struct target_timer_callback
*t
= pt
->next
;
1889 target_timer_callbacks
= NULL
;
1891 for (struct target
*target
= all_targets
;
1892 target
; target
= target
->next
) {
1893 if (target
->type
->deinit_target
)
1894 target
->type
->deinit_target(target
);
1898 /* free resources and restore memory, if restoring memory fails,
1899 * free up resources anyway
1901 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1903 struct working_area
*c
= target
->working_areas
;
1905 LOG_DEBUG("freeing all working areas");
1907 /* Loop through all areas, restoring the allocated ones and marking them as free */
1911 target_restore_working_area(target
, c
);
1913 *c
->user
= NULL
; /* Same as above */
1919 /* Run a merge pass to combine all areas into one */
1920 target_merge_working_areas(target
);
1922 print_wa_layout(target
);
1925 void target_free_all_working_areas(struct target
*target
)
1927 target_free_all_working_areas_restore(target
, 1);
1930 /* Find the largest number of bytes that can be allocated */
1931 uint32_t target_get_working_area_avail(struct target
*target
)
1933 struct working_area
*c
= target
->working_areas
;
1934 uint32_t max_size
= 0;
1937 return target
->working_area_size
;
1940 if (c
->free
&& max_size
< c
->size
)
1949 int target_arch_state(struct target
*target
)
1952 if (target
== NULL
) {
1953 LOG_WARNING("No target has been configured");
1957 if (target
->state
!= TARGET_HALTED
)
1960 retval
= target
->type
->arch_state(target
);
1964 static int target_get_gdb_fileio_info_default(struct target
*target
,
1965 struct gdb_fileio_info
*fileio_info
)
1967 /* If target does not support semi-hosting function, target
1968 has no need to provide .get_gdb_fileio_info callback.
1969 It just return ERROR_FAIL and gdb_server will return "Txx"
1970 as target halted every time. */
1974 static int target_gdb_fileio_end_default(struct target
*target
,
1975 int retcode
, int fileio_errno
, bool ctrl_c
)
1980 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
1981 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1983 struct timeval timeout
, now
;
1985 gettimeofday(&timeout
, NULL
);
1986 timeval_add_time(&timeout
, seconds
, 0);
1988 LOG_INFO("Starting profiling. Halting and resuming the"
1989 " target as often as we can...");
1991 uint32_t sample_count
= 0;
1992 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
1993 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
1995 int retval
= ERROR_OK
;
1997 target_poll(target
);
1998 if (target
->state
== TARGET_HALTED
) {
1999 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
2000 samples
[sample_count
++] = t
;
2001 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2002 retval
= target_resume(target
, 1, 0, 0, 0);
2003 target_poll(target
);
2004 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2005 } else if (target
->state
== TARGET_RUNNING
) {
2006 /* We want to quickly sample the PC. */
2007 retval
= target_halt(target
);
2009 LOG_INFO("Target not halted or running");
2014 if (retval
!= ERROR_OK
)
2017 gettimeofday(&now
, NULL
);
2018 if ((sample_count
>= max_num_samples
) ||
2019 ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
))) {
2020 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2025 *num_samples
= sample_count
;
2029 /* Single aligned words are guaranteed to use 16 or 32 bit access
2030 * mode respectively, otherwise data is handled as quickly as
2033 int target_write_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, const uint8_t *buffer
)
2035 LOG_DEBUG("writing buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2038 if (!target_was_examined(target
)) {
2039 LOG_ERROR("Target not examined yet");
2046 if ((address
+ size
- 1) < address
) {
2047 /* GDB can request this when e.g. PC is 0xfffffffc */
2048 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2054 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2057 static int target_write_buffer_default(struct target
*target
,
2058 target_addr_t address
, uint32_t count
, const uint8_t *buffer
)
2062 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2063 * will have something to do with the size we leave to it. */
2064 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2065 if (address
& size
) {
2066 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2067 if (retval
!= ERROR_OK
)
2075 /* Write the data with as large access size as possible. */
2076 for (; size
> 0; size
/= 2) {
2077 uint32_t aligned
= count
- count
% size
;
2079 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2080 if (retval
!= ERROR_OK
)
2091 /* Single aligned words are guaranteed to use 16 or 32 bit access
2092 * mode respectively, otherwise data is handled as quickly as
2095 int target_read_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
2097 LOG_DEBUG("reading buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2100 if (!target_was_examined(target
)) {
2101 LOG_ERROR("Target not examined yet");
2108 if ((address
+ size
- 1) < address
) {
2109 /* GDB can request this when e.g. PC is 0xfffffffc */
2110 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2116 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2119 static int target_read_buffer_default(struct target
*target
, target_addr_t address
, uint32_t count
, uint8_t *buffer
)
2123 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2124 * will have something to do with the size we leave to it. */
2125 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2126 if (address
& size
) {
2127 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2128 if (retval
!= ERROR_OK
)
2136 /* Read the data with as large access size as possible. */
2137 for (; size
> 0; size
/= 2) {
2138 uint32_t aligned
= count
- count
% size
;
2140 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2141 if (retval
!= ERROR_OK
)
2152 int target_checksum_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* crc
)
2157 uint32_t checksum
= 0;
2158 if (!target_was_examined(target
)) {
2159 LOG_ERROR("Target not examined yet");
2163 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2164 if (retval
!= ERROR_OK
) {
2165 buffer
= malloc(size
);
2166 if (buffer
== NULL
) {
2167 LOG_ERROR("error allocating buffer for section (%" PRId32
" bytes)", size
);
2168 return ERROR_COMMAND_SYNTAX_ERROR
;
2170 retval
= target_read_buffer(target
, address
, size
, buffer
);
2171 if (retval
!= ERROR_OK
) {
2176 /* convert to target endianness */
2177 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2178 uint32_t target_data
;
2179 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2180 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2183 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2192 int target_blank_check_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* blank
,
2193 uint8_t erased_value
)
2196 if (!target_was_examined(target
)) {
2197 LOG_ERROR("Target not examined yet");
2201 if (target
->type
->blank_check_memory
== 0)
2202 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2204 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
, erased_value
);
2209 int target_read_u64(struct target
*target
, target_addr_t address
, uint64_t *value
)
2211 uint8_t value_buf
[8];
2212 if (!target_was_examined(target
)) {
2213 LOG_ERROR("Target not examined yet");
2217 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2219 if (retval
== ERROR_OK
) {
2220 *value
= target_buffer_get_u64(target
, value_buf
);
2221 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2226 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2233 int target_read_u32(struct target
*target
, target_addr_t address
, uint32_t *value
)
2235 uint8_t value_buf
[4];
2236 if (!target_was_examined(target
)) {
2237 LOG_ERROR("Target not examined yet");
2241 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2243 if (retval
== ERROR_OK
) {
2244 *value
= target_buffer_get_u32(target
, value_buf
);
2245 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2250 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2257 int target_read_u16(struct target
*target
, target_addr_t address
, uint16_t *value
)
2259 uint8_t value_buf
[2];
2260 if (!target_was_examined(target
)) {
2261 LOG_ERROR("Target not examined yet");
2265 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2267 if (retval
== ERROR_OK
) {
2268 *value
= target_buffer_get_u16(target
, value_buf
);
2269 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%4.4" PRIx16
,
2274 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2281 int target_read_u8(struct target
*target
, target_addr_t address
, uint8_t *value
)
2283 if (!target_was_examined(target
)) {
2284 LOG_ERROR("Target not examined yet");
2288 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2290 if (retval
== ERROR_OK
) {
2291 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2296 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2303 int target_write_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2306 uint8_t value_buf
[8];
2307 if (!target_was_examined(target
)) {
2308 LOG_ERROR("Target not examined yet");
2312 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2316 target_buffer_set_u64(target
, value_buf
, value
);
2317 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2318 if (retval
!= ERROR_OK
)
2319 LOG_DEBUG("failed: %i", retval
);
2324 int target_write_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2327 uint8_t value_buf
[4];
2328 if (!target_was_examined(target
)) {
2329 LOG_ERROR("Target not examined yet");
2333 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2337 target_buffer_set_u32(target
, value_buf
, value
);
2338 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2339 if (retval
!= ERROR_OK
)
2340 LOG_DEBUG("failed: %i", retval
);
2345 int target_write_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2348 uint8_t value_buf
[2];
2349 if (!target_was_examined(target
)) {
2350 LOG_ERROR("Target not examined yet");
2354 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2358 target_buffer_set_u16(target
, value_buf
, value
);
2359 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2360 if (retval
!= ERROR_OK
)
2361 LOG_DEBUG("failed: %i", retval
);
2366 int target_write_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2369 if (!target_was_examined(target
)) {
2370 LOG_ERROR("Target not examined yet");
2374 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2377 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2378 if (retval
!= ERROR_OK
)
2379 LOG_DEBUG("failed: %i", retval
);
2384 int target_write_phys_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2387 uint8_t value_buf
[8];
2388 if (!target_was_examined(target
)) {
2389 LOG_ERROR("Target not examined yet");
2393 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2397 target_buffer_set_u64(target
, value_buf
, value
);
2398 retval
= target_write_phys_memory(target
, address
, 8, 1, value_buf
);
2399 if (retval
!= ERROR_OK
)
2400 LOG_DEBUG("failed: %i", retval
);
2405 int target_write_phys_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2408 uint8_t value_buf
[4];
2409 if (!target_was_examined(target
)) {
2410 LOG_ERROR("Target not examined yet");
2414 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2418 target_buffer_set_u32(target
, value_buf
, value
);
2419 retval
= target_write_phys_memory(target
, address
, 4, 1, value_buf
);
2420 if (retval
!= ERROR_OK
)
2421 LOG_DEBUG("failed: %i", retval
);
2426 int target_write_phys_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2429 uint8_t value_buf
[2];
2430 if (!target_was_examined(target
)) {
2431 LOG_ERROR("Target not examined yet");
2435 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2439 target_buffer_set_u16(target
, value_buf
, value
);
2440 retval
= target_write_phys_memory(target
, address
, 2, 1, value_buf
);
2441 if (retval
!= ERROR_OK
)
2442 LOG_DEBUG("failed: %i", retval
);
2447 int target_write_phys_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2450 if (!target_was_examined(target
)) {
2451 LOG_ERROR("Target not examined yet");
2455 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2458 retval
= target_write_phys_memory(target
, address
, 1, 1, &value
);
2459 if (retval
!= ERROR_OK
)
2460 LOG_DEBUG("failed: %i", retval
);
2465 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2467 struct target
*target
= get_target(name
);
2468 if (target
== NULL
) {
2469 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2472 if (!target
->tap
->enabled
) {
2473 LOG_USER("Target: TAP %s is disabled, "
2474 "can't be the current target\n",
2475 target
->tap
->dotted_name
);
2479 cmd_ctx
->current_target
= target
->target_number
;
2484 COMMAND_HANDLER(handle_targets_command
)
2486 int retval
= ERROR_OK
;
2487 if (CMD_ARGC
== 1) {
2488 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2489 if (retval
== ERROR_OK
) {
2495 struct target
*target
= all_targets
;
2496 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2497 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2502 if (target
->tap
->enabled
)
2503 state
= target_state_name(target
);
2505 state
= "tap-disabled";
2507 if (CMD_CTX
->current_target
== target
->target_number
)
2510 /* keep columns lined up to match the headers above */
2511 command_print(CMD_CTX
,
2512 "%2d%c %-18s %-10s %-6s %-18s %s",
2513 target
->target_number
,
2515 target_name(target
),
2516 target_type_name(target
),
2517 Jim_Nvp_value2name_simple(nvp_target_endian
,
2518 target
->endianness
)->name
,
2519 target
->tap
->dotted_name
,
2521 target
= target
->next
;
2527 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2529 static int powerDropout
;
2530 static int srstAsserted
;
2532 static int runPowerRestore
;
2533 static int runPowerDropout
;
2534 static int runSrstAsserted
;
2535 static int runSrstDeasserted
;
2537 static int sense_handler(void)
2539 static int prevSrstAsserted
;
2540 static int prevPowerdropout
;
2542 int retval
= jtag_power_dropout(&powerDropout
);
2543 if (retval
!= ERROR_OK
)
2547 powerRestored
= prevPowerdropout
&& !powerDropout
;
2549 runPowerRestore
= 1;
2551 int64_t current
= timeval_ms();
2552 static int64_t lastPower
;
2553 bool waitMore
= lastPower
+ 2000 > current
;
2554 if (powerDropout
&& !waitMore
) {
2555 runPowerDropout
= 1;
2556 lastPower
= current
;
2559 retval
= jtag_srst_asserted(&srstAsserted
);
2560 if (retval
!= ERROR_OK
)
2564 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2566 static int64_t lastSrst
;
2567 waitMore
= lastSrst
+ 2000 > current
;
2568 if (srstDeasserted
&& !waitMore
) {
2569 runSrstDeasserted
= 1;
2573 if (!prevSrstAsserted
&& srstAsserted
)
2574 runSrstAsserted
= 1;
2576 prevSrstAsserted
= srstAsserted
;
2577 prevPowerdropout
= powerDropout
;
2579 if (srstDeasserted
|| powerRestored
) {
2580 /* Other than logging the event we can't do anything here.
2581 * Issuing a reset is a particularly bad idea as we might
2582 * be inside a reset already.
2589 /* process target state changes */
2590 static int handle_target(void *priv
)
2592 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2593 int retval
= ERROR_OK
;
2595 if (!is_jtag_poll_safe()) {
2596 /* polling is disabled currently */
2600 /* we do not want to recurse here... */
2601 static int recursive
;
2605 /* danger! running these procedures can trigger srst assertions and power dropouts.
2606 * We need to avoid an infinite loop/recursion here and we do that by
2607 * clearing the flags after running these events.
2609 int did_something
= 0;
2610 if (runSrstAsserted
) {
2611 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2612 Jim_Eval(interp
, "srst_asserted");
2615 if (runSrstDeasserted
) {
2616 Jim_Eval(interp
, "srst_deasserted");
2619 if (runPowerDropout
) {
2620 LOG_INFO("Power dropout detected, running power_dropout proc.");
2621 Jim_Eval(interp
, "power_dropout");
2624 if (runPowerRestore
) {
2625 Jim_Eval(interp
, "power_restore");
2629 if (did_something
) {
2630 /* clear detect flags */
2634 /* clear action flags */
2636 runSrstAsserted
= 0;
2637 runSrstDeasserted
= 0;
2638 runPowerRestore
= 0;
2639 runPowerDropout
= 0;
2644 /* Poll targets for state changes unless that's globally disabled.
2645 * Skip targets that are currently disabled.
2647 for (struct target
*target
= all_targets
;
2648 is_jtag_poll_safe() && target
;
2649 target
= target
->next
) {
2651 if (!target_was_examined(target
))
2654 if (!target
->tap
->enabled
)
2657 if (target
->backoff
.times
> target
->backoff
.count
) {
2658 /* do not poll this time as we failed previously */
2659 target
->backoff
.count
++;
2662 target
->backoff
.count
= 0;
2664 /* only poll target if we've got power and srst isn't asserted */
2665 if (!powerDropout
&& !srstAsserted
) {
2666 /* polling may fail silently until the target has been examined */
2667 retval
= target_poll(target
);
2668 if (retval
!= ERROR_OK
) {
2669 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2670 if (target
->backoff
.times
* polling_interval
< 5000) {
2671 target
->backoff
.times
*= 2;
2672 target
->backoff
.times
++;
2675 /* Tell GDB to halt the debugger. This allows the user to
2676 * run monitor commands to handle the situation.
2678 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2680 if (target
->backoff
.times
> 0) {
2681 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
2682 target_reset_examined(target
);
2683 retval
= target_examine_one(target
);
2684 /* Target examination could have failed due to unstable connection,
2685 * but we set the examined flag anyway to repoll it later */
2686 if (retval
!= ERROR_OK
) {
2687 target
->examined
= true;
2688 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2689 target
->backoff
.times
* polling_interval
);
2694 /* Since we succeeded, we reset backoff count */
2695 target
->backoff
.times
= 0;
2702 COMMAND_HANDLER(handle_reg_command
)
2704 struct target
*target
;
2705 struct reg
*reg
= NULL
;
2711 target
= get_current_target(CMD_CTX
);
2713 /* list all available registers for the current target */
2714 if (CMD_ARGC
== 0) {
2715 struct reg_cache
*cache
= target
->reg_cache
;
2721 command_print(CMD_CTX
, "===== %s", cache
->name
);
2723 for (i
= 0, reg
= cache
->reg_list
;
2724 i
< cache
->num_regs
;
2725 i
++, reg
++, count
++) {
2726 /* only print cached values if they are valid */
2728 value
= buf_to_str(reg
->value
,
2730 command_print(CMD_CTX
,
2731 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2739 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2744 cache
= cache
->next
;
2750 /* access a single register by its ordinal number */
2751 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2753 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2755 struct reg_cache
*cache
= target
->reg_cache
;
2759 for (i
= 0; i
< cache
->num_regs
; i
++) {
2760 if (count
++ == num
) {
2761 reg
= &cache
->reg_list
[i
];
2767 cache
= cache
->next
;
2771 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2772 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2776 /* access a single register by its name */
2777 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2780 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2785 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2787 /* display a register */
2788 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2789 && (CMD_ARGV
[1][0] <= '9')))) {
2790 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2793 if (reg
->valid
== 0)
2794 reg
->type
->get(reg
);
2795 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2796 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2801 /* set register value */
2802 if (CMD_ARGC
== 2) {
2803 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2806 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2808 reg
->type
->set(reg
, buf
);
2810 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2811 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2819 return ERROR_COMMAND_SYNTAX_ERROR
;
2822 COMMAND_HANDLER(handle_poll_command
)
2824 int retval
= ERROR_OK
;
2825 struct target
*target
= get_current_target(CMD_CTX
);
2827 if (CMD_ARGC
== 0) {
2828 command_print(CMD_CTX
, "background polling: %s",
2829 jtag_poll_get_enabled() ? "on" : "off");
2830 command_print(CMD_CTX
, "TAP: %s (%s)",
2831 target
->tap
->dotted_name
,
2832 target
->tap
->enabled
? "enabled" : "disabled");
2833 if (!target
->tap
->enabled
)
2835 retval
= target_poll(target
);
2836 if (retval
!= ERROR_OK
)
2838 retval
= target_arch_state(target
);
2839 if (retval
!= ERROR_OK
)
2841 } else if (CMD_ARGC
== 1) {
2843 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2844 jtag_poll_set_enabled(enable
);
2846 return ERROR_COMMAND_SYNTAX_ERROR
;
2851 COMMAND_HANDLER(handle_wait_halt_command
)
2854 return ERROR_COMMAND_SYNTAX_ERROR
;
2856 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2857 if (1 == CMD_ARGC
) {
2858 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2859 if (ERROR_OK
!= retval
)
2860 return ERROR_COMMAND_SYNTAX_ERROR
;
2863 struct target
*target
= get_current_target(CMD_CTX
);
2864 return target_wait_state(target
, TARGET_HALTED
, ms
);
2867 /* wait for target state to change. The trick here is to have a low
2868 * latency for short waits and not to suck up all the CPU time
2871 * After 500ms, keep_alive() is invoked
2873 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2876 int64_t then
= 0, cur
;
2880 retval
= target_poll(target
);
2881 if (retval
!= ERROR_OK
)
2883 if (target
->state
== state
)
2888 then
= timeval_ms();
2889 LOG_DEBUG("waiting for target %s...",
2890 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2896 if ((cur
-then
) > ms
) {
2897 LOG_ERROR("timed out while waiting for target %s",
2898 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2906 COMMAND_HANDLER(handle_halt_command
)
2910 struct target
*target
= get_current_target(CMD_CTX
);
2911 int retval
= target_halt(target
);
2912 if (ERROR_OK
!= retval
)
2915 if (CMD_ARGC
== 1) {
2916 unsigned wait_local
;
2917 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2918 if (ERROR_OK
!= retval
)
2919 return ERROR_COMMAND_SYNTAX_ERROR
;
2924 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2927 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2929 struct target
*target
= get_current_target(CMD_CTX
);
2931 LOG_USER("requesting target halt and executing a soft reset");
2933 target_soft_reset_halt(target
);
2938 COMMAND_HANDLER(handle_reset_command
)
2941 return ERROR_COMMAND_SYNTAX_ERROR
;
2943 enum target_reset_mode reset_mode
= RESET_RUN
;
2944 if (CMD_ARGC
== 1) {
2946 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2947 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
2948 return ERROR_COMMAND_SYNTAX_ERROR
;
2949 reset_mode
= n
->value
;
2952 /* reset *all* targets */
2953 return target_process_reset(CMD_CTX
, reset_mode
);
2957 COMMAND_HANDLER(handle_resume_command
)
2961 return ERROR_COMMAND_SYNTAX_ERROR
;
2963 struct target
*target
= get_current_target(CMD_CTX
);
2965 /* with no CMD_ARGV, resume from current pc, addr = 0,
2966 * with one arguments, addr = CMD_ARGV[0],
2967 * handle breakpoints, not debugging */
2968 target_addr_t addr
= 0;
2969 if (CMD_ARGC
== 1) {
2970 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
2974 return target_resume(target
, current
, addr
, 1, 0);
2977 COMMAND_HANDLER(handle_step_command
)
2980 return ERROR_COMMAND_SYNTAX_ERROR
;
2984 /* with no CMD_ARGV, step from current pc, addr = 0,
2985 * with one argument addr = CMD_ARGV[0],
2986 * handle breakpoints, debugging */
2987 target_addr_t addr
= 0;
2989 if (CMD_ARGC
== 1) {
2990 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
2994 struct target
*target
= get_current_target(CMD_CTX
);
2996 return target
->type
->step(target
, current_pc
, addr
, 1);
2999 static void handle_md_output(struct command_context
*cmd_ctx
,
3000 struct target
*target
, target_addr_t address
, unsigned size
,
3001 unsigned count
, const uint8_t *buffer
)
3003 const unsigned line_bytecnt
= 32;
3004 unsigned line_modulo
= line_bytecnt
/ size
;
3006 char output
[line_bytecnt
* 4 + 1];
3007 unsigned output_len
= 0;
3009 const char *value_fmt
;
3012 value_fmt
= "%16.16llx ";
3015 value_fmt
= "%8.8x ";
3018 value_fmt
= "%4.4x ";
3021 value_fmt
= "%2.2x ";
3024 /* "can't happen", caller checked */
3025 LOG_ERROR("invalid memory read size: %u", size
);
3029 for (unsigned i
= 0; i
< count
; i
++) {
3030 if (i
% line_modulo
== 0) {
3031 output_len
+= snprintf(output
+ output_len
,
3032 sizeof(output
) - output_len
,
3033 TARGET_ADDR_FMT
": ",
3034 (address
+ (i
* size
)));
3038 const uint8_t *value_ptr
= buffer
+ i
* size
;
3041 value
= target_buffer_get_u64(target
, value_ptr
);
3044 value
= target_buffer_get_u32(target
, value_ptr
);
3047 value
= target_buffer_get_u16(target
, value_ptr
);
3052 output_len
+= snprintf(output
+ output_len
,
3053 sizeof(output
) - output_len
,
3056 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
3057 command_print(cmd_ctx
, "%s", output
);
3063 COMMAND_HANDLER(handle_md_command
)
3066 return ERROR_COMMAND_SYNTAX_ERROR
;
3069 switch (CMD_NAME
[2]) {
3083 return ERROR_COMMAND_SYNTAX_ERROR
;
3086 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3087 int (*fn
)(struct target
*target
,
3088 target_addr_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
3092 fn
= target_read_phys_memory
;
3094 fn
= target_read_memory
;
3095 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
3096 return ERROR_COMMAND_SYNTAX_ERROR
;
3098 target_addr_t address
;
3099 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3103 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
3105 uint8_t *buffer
= calloc(count
, size
);
3107 struct target
*target
= get_current_target(CMD_CTX
);
3108 int retval
= fn(target
, address
, size
, count
, buffer
);
3109 if (ERROR_OK
== retval
)
3110 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
3117 typedef int (*target_write_fn
)(struct target
*target
,
3118 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
3120 static int target_fill_mem(struct target
*target
,
3121 target_addr_t address
,
3129 /* We have to write in reasonably large chunks to be able
3130 * to fill large memory areas with any sane speed */
3131 const unsigned chunk_size
= 16384;
3132 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
3133 if (target_buf
== NULL
) {
3134 LOG_ERROR("Out of memory");
3138 for (unsigned i
= 0; i
< chunk_size
; i
++) {
3139 switch (data_size
) {
3141 target_buffer_set_u64(target
, target_buf
+ i
* data_size
, b
);
3144 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
3147 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
3150 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
3157 int retval
= ERROR_OK
;
3159 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
3162 if (current
> chunk_size
)
3163 current
= chunk_size
;
3164 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
3165 if (retval
!= ERROR_OK
)
3167 /* avoid GDB timeouts */
3176 COMMAND_HANDLER(handle_mw_command
)
3179 return ERROR_COMMAND_SYNTAX_ERROR
;
3180 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3185 fn
= target_write_phys_memory
;
3187 fn
= target_write_memory
;
3188 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
3189 return ERROR_COMMAND_SYNTAX_ERROR
;
3191 target_addr_t address
;
3192 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3194 target_addr_t value
;
3195 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], value
);
3199 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
3201 struct target
*target
= get_current_target(CMD_CTX
);
3203 switch (CMD_NAME
[2]) {
3217 return ERROR_COMMAND_SYNTAX_ERROR
;
3220 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
3223 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
3224 target_addr_t
*min_address
, target_addr_t
*max_address
)
3226 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
3227 return ERROR_COMMAND_SYNTAX_ERROR
;
3229 /* a base address isn't always necessary,
3230 * default to 0x0 (i.e. don't relocate) */
3231 if (CMD_ARGC
>= 2) {
3233 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3234 image
->base_address
= addr
;
3235 image
->base_address_set
= 1;
3237 image
->base_address_set
= 0;
3239 image
->start_address_set
= 0;
3242 COMMAND_PARSE_ADDRESS(CMD_ARGV
[3], *min_address
);
3243 if (CMD_ARGC
== 5) {
3244 COMMAND_PARSE_ADDRESS(CMD_ARGV
[4], *max_address
);
3245 /* use size (given) to find max (required) */
3246 *max_address
+= *min_address
;
3249 if (*min_address
> *max_address
)
3250 return ERROR_COMMAND_SYNTAX_ERROR
;
3255 COMMAND_HANDLER(handle_load_image_command
)
3259 uint32_t image_size
;
3260 target_addr_t min_address
= 0;
3261 target_addr_t max_address
= -1;
3265 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
3266 &image
, &min_address
, &max_address
);
3267 if (ERROR_OK
!= retval
)
3270 struct target
*target
= get_current_target(CMD_CTX
);
3272 struct duration bench
;
3273 duration_start(&bench
);
3275 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
3280 for (i
= 0; i
< image
.num_sections
; i
++) {
3281 buffer
= malloc(image
.sections
[i
].size
);
3282 if (buffer
== NULL
) {
3283 command_print(CMD_CTX
,
3284 "error allocating buffer for section (%d bytes)",
3285 (int)(image
.sections
[i
].size
));
3286 retval
= ERROR_FAIL
;
3290 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3291 if (retval
!= ERROR_OK
) {
3296 uint32_t offset
= 0;
3297 uint32_t length
= buf_cnt
;
3299 /* DANGER!!! beware of unsigned comparision here!!! */
3301 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3302 (image
.sections
[i
].base_address
< max_address
)) {
3304 if (image
.sections
[i
].base_address
< min_address
) {
3305 /* clip addresses below */
3306 offset
+= min_address
-image
.sections
[i
].base_address
;
3310 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3311 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3313 retval
= target_write_buffer(target
,
3314 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3315 if (retval
!= ERROR_OK
) {
3319 image_size
+= length
;
3320 command_print(CMD_CTX
, "%u bytes written at address " TARGET_ADDR_FMT
"",
3321 (unsigned int)length
,
3322 image
.sections
[i
].base_address
+ offset
);
3328 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3329 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
3330 "in %fs (%0.3f KiB/s)", image_size
,
3331 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3334 image_close(&image
);
3340 COMMAND_HANDLER(handle_dump_image_command
)
3342 struct fileio
*fileio
;
3344 int retval
, retvaltemp
;
3345 target_addr_t address
, size
;
3346 struct duration bench
;
3347 struct target
*target
= get_current_target(CMD_CTX
);
3350 return ERROR_COMMAND_SYNTAX_ERROR
;
3352 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], address
);
3353 COMMAND_PARSE_ADDRESS(CMD_ARGV
[2], size
);
3355 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
3356 buffer
= malloc(buf_size
);
3360 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3361 if (retval
!= ERROR_OK
) {
3366 duration_start(&bench
);
3369 size_t size_written
;
3370 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
3371 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
3372 if (retval
!= ERROR_OK
)
3375 retval
= fileio_write(fileio
, this_run_size
, buffer
, &size_written
);
3376 if (retval
!= ERROR_OK
)
3379 size
-= this_run_size
;
3380 address
+= this_run_size
;
3385 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3387 retval
= fileio_size(fileio
, &filesize
);
3388 if (retval
!= ERROR_OK
)
3390 command_print(CMD_CTX
,
3391 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize
,
3392 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3395 retvaltemp
= fileio_close(fileio
);
3396 if (retvaltemp
!= ERROR_OK
)
3405 IMAGE_CHECKSUM_ONLY
= 2
3408 static COMMAND_HELPER(handle_verify_image_command_internal
, enum verify_mode verify
)
3412 uint32_t image_size
;
3415 uint32_t checksum
= 0;
3416 uint32_t mem_checksum
= 0;
3420 struct target
*target
= get_current_target(CMD_CTX
);
3423 return ERROR_COMMAND_SYNTAX_ERROR
;
3426 LOG_ERROR("no target selected");
3430 struct duration bench
;
3431 duration_start(&bench
);
3433 if (CMD_ARGC
>= 2) {
3435 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3436 image
.base_address
= addr
;
3437 image
.base_address_set
= 1;
3439 image
.base_address_set
= 0;
3440 image
.base_address
= 0x0;
3443 image
.start_address_set
= 0;
3445 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3446 if (retval
!= ERROR_OK
)
3452 for (i
= 0; i
< image
.num_sections
; i
++) {
3453 buffer
= malloc(image
.sections
[i
].size
);
3454 if (buffer
== NULL
) {
3455 command_print(CMD_CTX
,
3456 "error allocating buffer for section (%d bytes)",
3457 (int)(image
.sections
[i
].size
));
3460 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3461 if (retval
!= ERROR_OK
) {
3466 if (verify
>= IMAGE_VERIFY
) {
3467 /* calculate checksum of image */
3468 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3469 if (retval
!= ERROR_OK
) {
3474 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3475 if (retval
!= ERROR_OK
) {
3479 if ((checksum
!= mem_checksum
) && (verify
== IMAGE_CHECKSUM_ONLY
)) {
3480 LOG_ERROR("checksum mismatch");
3482 retval
= ERROR_FAIL
;
3485 if (checksum
!= mem_checksum
) {
3486 /* failed crc checksum, fall back to a binary compare */
3490 LOG_ERROR("checksum mismatch - attempting binary compare");
3492 data
= malloc(buf_cnt
);
3494 /* Can we use 32bit word accesses? */
3496 int count
= buf_cnt
;
3497 if ((count
% 4) == 0) {
3501 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
3502 if (retval
== ERROR_OK
) {
3504 for (t
= 0; t
< buf_cnt
; t
++) {
3505 if (data
[t
] != buffer
[t
]) {
3506 command_print(CMD_CTX
,
3507 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3509 (unsigned)(t
+ image
.sections
[i
].base_address
),
3512 if (diffs
++ >= 127) {
3513 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
3525 command_print(CMD_CTX
, "address " TARGET_ADDR_FMT
" length 0x%08zx",
3526 image
.sections
[i
].base_address
,
3531 image_size
+= buf_cnt
;
3534 command_print(CMD_CTX
, "No more differences found.");
3537 retval
= ERROR_FAIL
;
3538 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3539 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3540 "in %fs (%0.3f KiB/s)", image_size
,
3541 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3544 image_close(&image
);
3549 COMMAND_HANDLER(handle_verify_image_checksum_command
)
3551 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_CHECKSUM_ONLY
);
3554 COMMAND_HANDLER(handle_verify_image_command
)
3556 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_VERIFY
);
3559 COMMAND_HANDLER(handle_test_image_command
)
3561 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_TEST
);
3564 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
3566 struct target
*target
= get_current_target(cmd_ctx
);
3567 struct breakpoint
*breakpoint
= target
->breakpoints
;
3568 while (breakpoint
) {
3569 if (breakpoint
->type
== BKPT_SOFT
) {
3570 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3571 breakpoint
->length
, 16);
3572 command_print(cmd_ctx
, "IVA breakpoint: " TARGET_ADDR_FMT
", 0x%x, %i, 0x%s",
3573 breakpoint
->address
,
3575 breakpoint
->set
, buf
);
3578 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3579 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3581 breakpoint
->length
, breakpoint
->set
);
3582 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3583 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3584 breakpoint
->address
,
3585 breakpoint
->length
, breakpoint
->set
);
3586 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3589 command_print(cmd_ctx
, "Breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3590 breakpoint
->address
,
3591 breakpoint
->length
, breakpoint
->set
);
3594 breakpoint
= breakpoint
->next
;
3599 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
3600 target_addr_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3602 struct target
*target
= get_current_target(cmd_ctx
);
3606 retval
= breakpoint_add(target
, addr
, length
, hw
);
3607 if (ERROR_OK
== retval
)
3608 command_print(cmd_ctx
, "breakpoint set at " TARGET_ADDR_FMT
"", addr
);
3610 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3613 } else if (addr
== 0) {
3614 if (target
->type
->add_context_breakpoint
== NULL
) {
3615 LOG_WARNING("Context breakpoint not available");
3618 retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3619 if (ERROR_OK
== retval
)
3620 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3622 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3626 if (target
->type
->add_hybrid_breakpoint
== NULL
) {
3627 LOG_WARNING("Hybrid breakpoint not available");
3630 retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3631 if (ERROR_OK
== retval
)
3632 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3634 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3641 COMMAND_HANDLER(handle_bp_command
)
3650 return handle_bp_command_list(CMD_CTX
);
3654 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3655 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3656 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3659 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
3661 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3662 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);