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, write to the *
38 * Free Software Foundation, Inc., *
39 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
40 ***************************************************************************/
46 #include <helper/time_support.h>
47 #include <jtag/jtag.h>
48 #include <flash/nor/core.h>
51 #include "target_type.h"
52 #include "target_request.h"
53 #include "breakpoints.h"
57 #include "rtos/rtos.h"
58 #include "transport/transport.h"
60 /* default halt wait timeout (ms) */
61 #define DEFAULT_HALT_TIMEOUT 5000
63 static int target_read_buffer_default(struct target
*target
, uint32_t address
,
64 uint32_t count
, uint8_t *buffer
);
65 static int target_write_buffer_default(struct target
*target
, uint32_t address
,
66 uint32_t count
, const uint8_t *buffer
);
67 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
68 int argc
, Jim_Obj
* const *argv
);
69 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
70 int argc
, Jim_Obj
* const *argv
);
71 static int target_register_user_commands(struct command_context
*cmd_ctx
);
72 static int target_get_gdb_fileio_info_default(struct target
*target
,
73 struct gdb_fileio_info
*fileio_info
);
74 static int target_gdb_fileio_end_default(struct target
*target
, int retcode
,
75 int fileio_errno
, bool ctrl_c
);
76 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
77 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
);
80 extern struct target_type arm7tdmi_target
;
81 extern struct target_type arm720t_target
;
82 extern struct target_type arm9tdmi_target
;
83 extern struct target_type arm920t_target
;
84 extern struct target_type arm966e_target
;
85 extern struct target_type arm946e_target
;
86 extern struct target_type arm926ejs_target
;
87 extern struct target_type fa526_target
;
88 extern struct target_type feroceon_target
;
89 extern struct target_type dragonite_target
;
90 extern struct target_type xscale_target
;
91 extern struct target_type cortexm_target
;
92 extern struct target_type cortexa_target
;
93 extern struct target_type cortexr4_target
;
94 extern struct target_type arm11_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
;
108 static struct target_type
*target_types
[] = {
139 struct target
*all_targets
;
140 static struct target_event_callback
*target_event_callbacks
;
141 static struct target_timer_callback
*target_timer_callbacks
;
142 LIST_HEAD(target_reset_callback_list
);
143 static const int polling_interval
= 100;
145 static const Jim_Nvp nvp_assert
[] = {
146 { .name
= "assert", NVP_ASSERT
},
147 { .name
= "deassert", NVP_DEASSERT
},
148 { .name
= "T", NVP_ASSERT
},
149 { .name
= "F", NVP_DEASSERT
},
150 { .name
= "t", NVP_ASSERT
},
151 { .name
= "f", NVP_DEASSERT
},
152 { .name
= NULL
, .value
= -1 }
155 static const Jim_Nvp nvp_error_target
[] = {
156 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
157 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
158 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
159 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
160 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
161 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
162 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
163 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
164 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
165 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
166 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
167 { .value
= -1, .name
= NULL
}
170 static const char *target_strerror_safe(int err
)
174 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
181 static const Jim_Nvp nvp_target_event
[] = {
183 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
184 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
185 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
186 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
187 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
189 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
190 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
192 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
193 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
194 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
195 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
196 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
197 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
198 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
199 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
200 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
201 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
202 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
203 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
205 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
206 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
208 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
209 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
211 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
212 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
214 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
215 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
217 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
218 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
220 { .value
= TARGET_EVENT_TRACE_CONFIG
, .name
= "trace-config" },
222 { .name
= NULL
, .value
= -1 }
225 static const Jim_Nvp nvp_target_state
[] = {
226 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
227 { .name
= "running", .value
= TARGET_RUNNING
},
228 { .name
= "halted", .value
= TARGET_HALTED
},
229 { .name
= "reset", .value
= TARGET_RESET
},
230 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
231 { .name
= NULL
, .value
= -1 },
234 static const Jim_Nvp nvp_target_debug_reason
[] = {
235 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
236 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
237 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
238 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
239 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
240 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
241 { .name
= "program-exit" , .value
= DBG_REASON_EXIT
},
242 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
243 { .name
= NULL
, .value
= -1 },
246 static const Jim_Nvp nvp_target_endian
[] = {
247 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
248 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
249 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
250 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
251 { .name
= NULL
, .value
= -1 },
254 static const Jim_Nvp nvp_reset_modes
[] = {
255 { .name
= "unknown", .value
= RESET_UNKNOWN
},
256 { .name
= "run" , .value
= RESET_RUN
},
257 { .name
= "halt" , .value
= RESET_HALT
},
258 { .name
= "init" , .value
= RESET_INIT
},
259 { .name
= NULL
, .value
= -1 },
262 const char *debug_reason_name(struct target
*t
)
266 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
267 t
->debug_reason
)->name
;
269 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
270 cp
= "(*BUG*unknown*BUG*)";
275 const char *target_state_name(struct target
*t
)
278 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
280 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
281 cp
= "(*BUG*unknown*BUG*)";
286 const char *target_event_name(enum target_event event
)
289 cp
= Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
;
291 LOG_ERROR("Invalid target event: %d", (int)(event
));
292 cp
= "(*BUG*unknown*BUG*)";
297 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
300 cp
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
;
302 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
303 cp
= "(*BUG*unknown*BUG*)";
308 /* determine the number of the new target */
309 static int new_target_number(void)
314 /* number is 0 based */
318 if (x
< t
->target_number
)
319 x
= t
->target_number
;
325 /* read a uint64_t from a buffer in target memory endianness */
326 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
328 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
329 return le_to_h_u64(buffer
);
331 return be_to_h_u64(buffer
);
334 /* read a uint32_t from a buffer in target memory endianness */
335 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
337 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
338 return le_to_h_u32(buffer
);
340 return be_to_h_u32(buffer
);
343 /* read a uint24_t from a buffer in target memory endianness */
344 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
346 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
347 return le_to_h_u24(buffer
);
349 return be_to_h_u24(buffer
);
352 /* read a uint16_t from a buffer in target memory endianness */
353 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
355 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
356 return le_to_h_u16(buffer
);
358 return be_to_h_u16(buffer
);
361 /* read a uint8_t from a buffer in target memory endianness */
362 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
364 return *buffer
& 0x0ff;
367 /* write a uint64_t to a buffer in target memory endianness */
368 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
370 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
371 h_u64_to_le(buffer
, value
);
373 h_u64_to_be(buffer
, value
);
376 /* write a uint32_t to a buffer in target memory endianness */
377 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
379 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
380 h_u32_to_le(buffer
, value
);
382 h_u32_to_be(buffer
, value
);
385 /* write a uint24_t to a buffer in target memory endianness */
386 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
388 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
389 h_u24_to_le(buffer
, value
);
391 h_u24_to_be(buffer
, value
);
394 /* write a uint16_t to a buffer in target memory endianness */
395 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
397 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
398 h_u16_to_le(buffer
, value
);
400 h_u16_to_be(buffer
, value
);
403 /* write a uint8_t to a buffer in target memory endianness */
404 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
409 /* write a uint64_t array to a buffer in target memory endianness */
410 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
413 for (i
= 0; i
< count
; i
++)
414 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
417 /* write a uint32_t array to a buffer in target memory endianness */
418 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
421 for (i
= 0; i
< count
; i
++)
422 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
425 /* write a uint16_t array to a buffer in target memory endianness */
426 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
429 for (i
= 0; i
< count
; i
++)
430 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
433 /* write a uint64_t array to a buffer in target memory endianness */
434 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
437 for (i
= 0; i
< count
; i
++)
438 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
441 /* write a uint32_t array to a buffer in target memory endianness */
442 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
445 for (i
= 0; i
< count
; i
++)
446 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
449 /* write a uint16_t array to a buffer in target memory endianness */
450 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
453 for (i
= 0; i
< count
; i
++)
454 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
457 /* return a pointer to a configured target; id is name or number */
458 struct target
*get_target(const char *id
)
460 struct target
*target
;
462 /* try as tcltarget name */
463 for (target
= all_targets
; target
; target
= target
->next
) {
464 if (target_name(target
) == NULL
)
466 if (strcmp(id
, target_name(target
)) == 0)
470 /* It's OK to remove this fallback sometime after August 2010 or so */
472 /* no match, try as number */
474 if (parse_uint(id
, &num
) != ERROR_OK
)
477 for (target
= all_targets
; target
; target
= target
->next
) {
478 if (target
->target_number
== (int)num
) {
479 LOG_WARNING("use '%s' as target identifier, not '%u'",
480 target_name(target
), num
);
488 /* returns a pointer to the n-th configured target */
489 static struct target
*get_target_by_num(int num
)
491 struct target
*target
= all_targets
;
494 if (target
->target_number
== num
)
496 target
= target
->next
;
502 struct target
*get_current_target(struct command_context
*cmd_ctx
)
504 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
506 if (target
== NULL
) {
507 LOG_ERROR("BUG: current_target out of bounds");
514 int target_poll(struct target
*target
)
518 /* We can't poll until after examine */
519 if (!target_was_examined(target
)) {
520 /* Fail silently lest we pollute the log */
524 retval
= target
->type
->poll(target
);
525 if (retval
!= ERROR_OK
)
528 if (target
->halt_issued
) {
529 if (target
->state
== TARGET_HALTED
)
530 target
->halt_issued
= false;
532 long long t
= timeval_ms() - target
->halt_issued_time
;
533 if (t
> DEFAULT_HALT_TIMEOUT
) {
534 target
->halt_issued
= false;
535 LOG_INFO("Halt timed out, wake up GDB.");
536 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
544 int target_halt(struct target
*target
)
547 /* We can't poll until after examine */
548 if (!target_was_examined(target
)) {
549 LOG_ERROR("Target not examined yet");
553 retval
= target
->type
->halt(target
);
554 if (retval
!= ERROR_OK
)
557 target
->halt_issued
= true;
558 target
->halt_issued_time
= timeval_ms();
564 * Make the target (re)start executing using its saved execution
565 * context (possibly with some modifications).
567 * @param target Which target should start executing.
568 * @param current True to use the target's saved program counter instead
569 * of the address parameter
570 * @param address Optionally used as the program counter.
571 * @param handle_breakpoints True iff breakpoints at the resumption PC
572 * should be skipped. (For example, maybe execution was stopped by
573 * such a breakpoint, in which case it would be counterprodutive to
575 * @param debug_execution False if all working areas allocated by OpenOCD
576 * should be released and/or restored to their original contents.
577 * (This would for example be true to run some downloaded "helper"
578 * algorithm code, which resides in one such working buffer and uses
579 * another for data storage.)
581 * @todo Resolve the ambiguity about what the "debug_execution" flag
582 * signifies. For example, Target implementations don't agree on how
583 * it relates to invalidation of the register cache, or to whether
584 * breakpoints and watchpoints should be enabled. (It would seem wrong
585 * to enable breakpoints when running downloaded "helper" algorithms
586 * (debug_execution true), since the breakpoints would be set to match
587 * target firmware being debugged, not the helper algorithm.... and
588 * enabling them could cause such helpers to malfunction (for example,
589 * by overwriting data with a breakpoint instruction. On the other
590 * hand the infrastructure for running such helpers might use this
591 * procedure but rely on hardware breakpoint to detect termination.)
593 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
597 /* We can't poll until after examine */
598 if (!target_was_examined(target
)) {
599 LOG_ERROR("Target not examined yet");
603 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
605 /* note that resume *must* be asynchronous. The CPU can halt before
606 * we poll. The CPU can even halt at the current PC as a result of
607 * a software breakpoint being inserted by (a bug?) the application.
609 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
610 if (retval
!= ERROR_OK
)
613 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
618 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
623 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
624 if (n
->name
== NULL
) {
625 LOG_ERROR("invalid reset mode");
629 struct target
*target
;
630 for (target
= all_targets
; target
; target
= target
->next
)
631 target_call_reset_callbacks(target
, reset_mode
);
633 /* disable polling during reset to make reset event scripts
634 * more predictable, i.e. dr/irscan & pathmove in events will
635 * not have JTAG operations injected into the middle of a sequence.
637 bool save_poll
= jtag_poll_get_enabled();
639 jtag_poll_set_enabled(false);
641 sprintf(buf
, "ocd_process_reset %s", n
->name
);
642 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
644 jtag_poll_set_enabled(save_poll
);
646 if (retval
!= JIM_OK
) {
647 Jim_MakeErrorMessage(cmd_ctx
->interp
);
648 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
652 /* We want any events to be processed before the prompt */
653 retval
= target_call_timer_callbacks_now();
655 for (target
= all_targets
; target
; target
= target
->next
) {
656 target
->type
->check_reset(target
);
657 target
->running_alg
= false;
663 static int identity_virt2phys(struct target
*target
,
664 uint32_t virtual, uint32_t *physical
)
670 static int no_mmu(struct target
*target
, int *enabled
)
676 static int default_examine(struct target
*target
)
678 target_set_examined(target
);
682 /* no check by default */
683 static int default_check_reset(struct target
*target
)
688 int target_examine_one(struct target
*target
)
690 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
692 int retval
= target
->type
->examine(target
);
693 if (retval
!= ERROR_OK
)
696 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
701 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
703 struct target
*target
= priv
;
705 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
708 jtag_unregister_event_callback(jtag_enable_callback
, target
);
710 return target_examine_one(target
);
713 /* Targets that correctly implement init + examine, i.e.
714 * no communication with target during init:
718 int target_examine(void)
720 int retval
= ERROR_OK
;
721 struct target
*target
;
723 for (target
= all_targets
; target
; target
= target
->next
) {
724 /* defer examination, but don't skip it */
725 if (!target
->tap
->enabled
) {
726 jtag_register_event_callback(jtag_enable_callback
,
731 retval
= target_examine_one(target
);
732 if (retval
!= ERROR_OK
)
738 const char *target_type_name(struct target
*target
)
740 return target
->type
->name
;
743 static int target_soft_reset_halt(struct target
*target
)
745 if (!target_was_examined(target
)) {
746 LOG_ERROR("Target not examined yet");
749 if (!target
->type
->soft_reset_halt
) {
750 LOG_ERROR("Target %s does not support soft_reset_halt",
751 target_name(target
));
754 return target
->type
->soft_reset_halt(target
);
758 * Downloads a target-specific native code algorithm to the target,
759 * and executes it. * Note that some targets may need to set up, enable,
760 * and tear down a breakpoint (hard or * soft) to detect algorithm
761 * termination, while others may support lower overhead schemes where
762 * soft breakpoints embedded in the algorithm automatically terminate the
765 * @param target used to run the algorithm
766 * @param arch_info target-specific description of the algorithm.
768 int target_run_algorithm(struct target
*target
,
769 int num_mem_params
, struct mem_param
*mem_params
,
770 int num_reg_params
, struct reg_param
*reg_param
,
771 uint32_t entry_point
, uint32_t exit_point
,
772 int timeout_ms
, void *arch_info
)
774 int retval
= ERROR_FAIL
;
776 if (!target_was_examined(target
)) {
777 LOG_ERROR("Target not examined yet");
780 if (!target
->type
->run_algorithm
) {
781 LOG_ERROR("Target type '%s' does not support %s",
782 target_type_name(target
), __func__
);
786 target
->running_alg
= true;
787 retval
= target
->type
->run_algorithm(target
,
788 num_mem_params
, mem_params
,
789 num_reg_params
, reg_param
,
790 entry_point
, exit_point
, timeout_ms
, arch_info
);
791 target
->running_alg
= false;
798 * Downloads a target-specific native code algorithm to the target,
799 * executes and leaves it running.
801 * @param target used to run the algorithm
802 * @param arch_info target-specific description of the algorithm.
804 int target_start_algorithm(struct target
*target
,
805 int num_mem_params
, struct mem_param
*mem_params
,
806 int num_reg_params
, struct reg_param
*reg_params
,
807 uint32_t entry_point
, uint32_t exit_point
,
810 int retval
= ERROR_FAIL
;
812 if (!target_was_examined(target
)) {
813 LOG_ERROR("Target not examined yet");
816 if (!target
->type
->start_algorithm
) {
817 LOG_ERROR("Target type '%s' does not support %s",
818 target_type_name(target
), __func__
);
821 if (target
->running_alg
) {
822 LOG_ERROR("Target is already running an algorithm");
826 target
->running_alg
= true;
827 retval
= target
->type
->start_algorithm(target
,
828 num_mem_params
, mem_params
,
829 num_reg_params
, reg_params
,
830 entry_point
, exit_point
, arch_info
);
837 * Waits for an algorithm started with target_start_algorithm() to complete.
839 * @param target used to run the algorithm
840 * @param arch_info target-specific description of the algorithm.
842 int target_wait_algorithm(struct target
*target
,
843 int num_mem_params
, struct mem_param
*mem_params
,
844 int num_reg_params
, struct reg_param
*reg_params
,
845 uint32_t exit_point
, int timeout_ms
,
848 int retval
= ERROR_FAIL
;
850 if (!target
->type
->wait_algorithm
) {
851 LOG_ERROR("Target type '%s' does not support %s",
852 target_type_name(target
), __func__
);
855 if (!target
->running_alg
) {
856 LOG_ERROR("Target is not running an algorithm");
860 retval
= target
->type
->wait_algorithm(target
,
861 num_mem_params
, mem_params
,
862 num_reg_params
, reg_params
,
863 exit_point
, timeout_ms
, arch_info
);
864 if (retval
!= ERROR_TARGET_TIMEOUT
)
865 target
->running_alg
= false;
872 * Executes a target-specific native code algorithm in the target.
873 * It differs from target_run_algorithm in that the algorithm is asynchronous.
874 * Because of this it requires an compliant algorithm:
875 * see contrib/loaders/flash/stm32f1x.S for example.
877 * @param target used to run the algorithm
880 int target_run_flash_async_algorithm(struct target
*target
,
881 const uint8_t *buffer
, uint32_t count
, int block_size
,
882 int num_mem_params
, struct mem_param
*mem_params
,
883 int num_reg_params
, struct reg_param
*reg_params
,
884 uint32_t buffer_start
, uint32_t buffer_size
,
885 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
890 const uint8_t *buffer_orig
= buffer
;
892 /* Set up working area. First word is write pointer, second word is read pointer,
893 * rest is fifo data area. */
894 uint32_t wp_addr
= buffer_start
;
895 uint32_t rp_addr
= buffer_start
+ 4;
896 uint32_t fifo_start_addr
= buffer_start
+ 8;
897 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
899 uint32_t wp
= fifo_start_addr
;
900 uint32_t rp
= fifo_start_addr
;
902 /* validate block_size is 2^n */
903 assert(!block_size
|| !(block_size
& (block_size
- 1)));
905 retval
= target_write_u32(target
, wp_addr
, wp
);
906 if (retval
!= ERROR_OK
)
908 retval
= target_write_u32(target
, rp_addr
, rp
);
909 if (retval
!= ERROR_OK
)
912 /* Start up algorithm on target and let it idle while writing the first chunk */
913 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
914 num_reg_params
, reg_params
,
919 if (retval
!= ERROR_OK
) {
920 LOG_ERROR("error starting target flash write algorithm");
926 retval
= target_read_u32(target
, rp_addr
, &rp
);
927 if (retval
!= ERROR_OK
) {
928 LOG_ERROR("failed to get read pointer");
932 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
933 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
936 LOG_ERROR("flash write algorithm aborted by target");
937 retval
= ERROR_FLASH_OPERATION_FAILED
;
941 if ((rp
& (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
942 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
946 /* Count the number of bytes available in the fifo without
947 * crossing the wrap around. Make sure to not fill it completely,
948 * because that would make wp == rp and that's the empty condition. */
949 uint32_t thisrun_bytes
;
951 thisrun_bytes
= rp
- wp
- block_size
;
952 else if (rp
> fifo_start_addr
)
953 thisrun_bytes
= fifo_end_addr
- wp
;
955 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
957 if (thisrun_bytes
== 0) {
958 /* Throttle polling a bit if transfer is (much) faster than flash
959 * programming. The exact delay shouldn't matter as long as it's
960 * less than buffer size / flash speed. This is very unlikely to
961 * run when using high latency connections such as USB. */
964 /* to stop an infinite loop on some targets check and increment a timeout
965 * this issue was observed on a stellaris using the new ICDI interface */
966 if (timeout
++ >= 500) {
967 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
968 return ERROR_FLASH_OPERATION_FAILED
;
973 /* reset our timeout */
976 /* Limit to the amount of data we actually want to write */
977 if (thisrun_bytes
> count
* block_size
)
978 thisrun_bytes
= count
* block_size
;
980 /* Write data to fifo */
981 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
982 if (retval
!= ERROR_OK
)
985 /* Update counters and wrap write pointer */
986 buffer
+= thisrun_bytes
;
987 count
-= thisrun_bytes
/ block_size
;
989 if (wp
>= fifo_end_addr
)
990 wp
= fifo_start_addr
;
992 /* Store updated write pointer to target */
993 retval
= target_write_u32(target
, wp_addr
, wp
);
994 if (retval
!= ERROR_OK
)
998 if (retval
!= ERROR_OK
) {
999 /* abort flash write algorithm on target */
1000 target_write_u32(target
, wp_addr
, 0);
1003 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1004 num_reg_params
, reg_params
,
1009 if (retval2
!= ERROR_OK
) {
1010 LOG_ERROR("error waiting for target flash write algorithm");
1017 int target_read_memory(struct target
*target
,
1018 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1020 if (!target_was_examined(target
)) {
1021 LOG_ERROR("Target not examined yet");
1024 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1027 int target_read_phys_memory(struct target
*target
,
1028 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1030 if (!target_was_examined(target
)) {
1031 LOG_ERROR("Target not examined yet");
1034 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1037 int target_write_memory(struct target
*target
,
1038 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1040 if (!target_was_examined(target
)) {
1041 LOG_ERROR("Target not examined yet");
1044 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1047 int target_write_phys_memory(struct target
*target
,
1048 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1050 if (!target_was_examined(target
)) {
1051 LOG_ERROR("Target not examined yet");
1054 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1057 int target_add_breakpoint(struct target
*target
,
1058 struct breakpoint
*breakpoint
)
1060 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1061 LOG_WARNING("target %s is not halted", target_name(target
));
1062 return ERROR_TARGET_NOT_HALTED
;
1064 return target
->type
->add_breakpoint(target
, breakpoint
);
1067 int target_add_context_breakpoint(struct target
*target
,
1068 struct breakpoint
*breakpoint
)
1070 if (target
->state
!= TARGET_HALTED
) {
1071 LOG_WARNING("target %s is not halted", target_name(target
));
1072 return ERROR_TARGET_NOT_HALTED
;
1074 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1077 int target_add_hybrid_breakpoint(struct target
*target
,
1078 struct breakpoint
*breakpoint
)
1080 if (target
->state
!= TARGET_HALTED
) {
1081 LOG_WARNING("target %s is not halted", target_name(target
));
1082 return ERROR_TARGET_NOT_HALTED
;
1084 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1087 int target_remove_breakpoint(struct target
*target
,
1088 struct breakpoint
*breakpoint
)
1090 return target
->type
->remove_breakpoint(target
, breakpoint
);
1093 int target_add_watchpoint(struct target
*target
,
1094 struct watchpoint
*watchpoint
)
1096 if (target
->state
!= TARGET_HALTED
) {
1097 LOG_WARNING("target %s is not halted", target_name(target
));
1098 return ERROR_TARGET_NOT_HALTED
;
1100 return target
->type
->add_watchpoint(target
, watchpoint
);
1102 int target_remove_watchpoint(struct target
*target
,
1103 struct watchpoint
*watchpoint
)
1105 return target
->type
->remove_watchpoint(target
, watchpoint
);
1107 int target_hit_watchpoint(struct target
*target
,
1108 struct watchpoint
**hit_watchpoint
)
1110 if (target
->state
!= TARGET_HALTED
) {
1111 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1112 return ERROR_TARGET_NOT_HALTED
;
1115 if (target
->type
->hit_watchpoint
== NULL
) {
1116 /* For backward compatible, if hit_watchpoint is not implemented,
1117 * return ERROR_FAIL such that gdb_server will not take the nonsense
1122 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1125 int target_get_gdb_reg_list(struct target
*target
,
1126 struct reg
**reg_list
[], int *reg_list_size
,
1127 enum target_register_class reg_class
)
1129 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1131 int target_step(struct target
*target
,
1132 int current
, uint32_t address
, int handle_breakpoints
)
1134 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1137 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1139 if (target
->state
!= TARGET_HALTED
) {
1140 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1141 return ERROR_TARGET_NOT_HALTED
;
1143 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1146 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1148 if (target
->state
!= TARGET_HALTED
) {
1149 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1150 return ERROR_TARGET_NOT_HALTED
;
1152 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1155 int target_profiling(struct target
*target
, uint32_t *samples
,
1156 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1158 if (target
->state
!= TARGET_HALTED
) {
1159 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1160 return ERROR_TARGET_NOT_HALTED
;
1162 return target
->type
->profiling(target
, samples
, max_num_samples
,
1163 num_samples
, seconds
);
1167 * Reset the @c examined flag for the given target.
1168 * Pure paranoia -- targets are zeroed on allocation.
1170 static void target_reset_examined(struct target
*target
)
1172 target
->examined
= false;
1175 static int err_read_phys_memory(struct target
*target
, uint32_t address
,
1176 uint32_t size
, uint32_t count
, uint8_t *buffer
)
1178 LOG_ERROR("Not implemented: %s", __func__
);
1182 static int err_write_phys_memory(struct target
*target
, uint32_t address
,
1183 uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1185 LOG_ERROR("Not implemented: %s", __func__
);
1189 static int handle_target(void *priv
);
1191 static int target_init_one(struct command_context
*cmd_ctx
,
1192 struct target
*target
)
1194 target_reset_examined(target
);
1196 struct target_type
*type
= target
->type
;
1197 if (type
->examine
== NULL
)
1198 type
->examine
= default_examine
;
1200 if (type
->check_reset
== NULL
)
1201 type
->check_reset
= default_check_reset
;
1203 assert(type
->init_target
!= NULL
);
1205 int retval
= type
->init_target(cmd_ctx
, target
);
1206 if (ERROR_OK
!= retval
) {
1207 LOG_ERROR("target '%s' init failed", target_name(target
));
1211 /* Sanity-check MMU support ... stub in what we must, to help
1212 * implement it in stages, but warn if we need to do so.
1215 if (type
->write_phys_memory
== NULL
) {
1216 LOG_ERROR("type '%s' is missing write_phys_memory",
1218 type
->write_phys_memory
= err_write_phys_memory
;
1220 if (type
->read_phys_memory
== NULL
) {
1221 LOG_ERROR("type '%s' is missing read_phys_memory",
1223 type
->read_phys_memory
= err_read_phys_memory
;
1225 if (type
->virt2phys
== NULL
) {
1226 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1227 type
->virt2phys
= identity_virt2phys
;
1230 /* Make sure no-MMU targets all behave the same: make no
1231 * distinction between physical and virtual addresses, and
1232 * ensure that virt2phys() is always an identity mapping.
1234 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1235 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1238 type
->write_phys_memory
= type
->write_memory
;
1239 type
->read_phys_memory
= type
->read_memory
;
1240 type
->virt2phys
= identity_virt2phys
;
1243 if (target
->type
->read_buffer
== NULL
)
1244 target
->type
->read_buffer
= target_read_buffer_default
;
1246 if (target
->type
->write_buffer
== NULL
)
1247 target
->type
->write_buffer
= target_write_buffer_default
;
1249 if (target
->type
->get_gdb_fileio_info
== NULL
)
1250 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1252 if (target
->type
->gdb_fileio_end
== NULL
)
1253 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1255 if (target
->type
->profiling
== NULL
)
1256 target
->type
->profiling
= target_profiling_default
;
1261 static int target_init(struct command_context
*cmd_ctx
)
1263 struct target
*target
;
1266 for (target
= all_targets
; target
; target
= target
->next
) {
1267 retval
= target_init_one(cmd_ctx
, target
);
1268 if (ERROR_OK
!= retval
)
1275 retval
= target_register_user_commands(cmd_ctx
);
1276 if (ERROR_OK
!= retval
)
1279 retval
= target_register_timer_callback(&handle_target
,
1280 polling_interval
, 1, cmd_ctx
->interp
);
1281 if (ERROR_OK
!= retval
)
1287 COMMAND_HANDLER(handle_target_init_command
)
1292 return ERROR_COMMAND_SYNTAX_ERROR
;
1294 static bool target_initialized
;
1295 if (target_initialized
) {
1296 LOG_INFO("'target init' has already been called");
1299 target_initialized
= true;
1301 retval
= command_run_line(CMD_CTX
, "init_targets");
1302 if (ERROR_OK
!= retval
)
1305 retval
= command_run_line(CMD_CTX
, "init_target_events");
1306 if (ERROR_OK
!= retval
)
1309 retval
= command_run_line(CMD_CTX
, "init_board");
1310 if (ERROR_OK
!= retval
)
1313 LOG_DEBUG("Initializing targets...");
1314 return target_init(CMD_CTX
);
1317 int target_register_event_callback(int (*callback
)(struct target
*target
,
1318 enum target_event event
, void *priv
), void *priv
)
1320 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1322 if (callback
== NULL
)
1323 return ERROR_COMMAND_SYNTAX_ERROR
;
1326 while ((*callbacks_p
)->next
)
1327 callbacks_p
= &((*callbacks_p
)->next
);
1328 callbacks_p
= &((*callbacks_p
)->next
);
1331 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1332 (*callbacks_p
)->callback
= callback
;
1333 (*callbacks_p
)->priv
= priv
;
1334 (*callbacks_p
)->next
= NULL
;
1339 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1340 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1342 struct target_reset_callback
*entry
;
1344 if (callback
== NULL
)
1345 return ERROR_COMMAND_SYNTAX_ERROR
;
1347 entry
= malloc(sizeof(struct target_reset_callback
));
1348 if (entry
== NULL
) {
1349 LOG_ERROR("error allocating buffer for reset callback entry");
1350 return ERROR_COMMAND_SYNTAX_ERROR
;
1353 entry
->callback
= callback
;
1355 list_add(&entry
->list
, &target_reset_callback_list
);
1361 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1363 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1366 if (callback
== NULL
)
1367 return ERROR_COMMAND_SYNTAX_ERROR
;
1370 while ((*callbacks_p
)->next
)
1371 callbacks_p
= &((*callbacks_p
)->next
);
1372 callbacks_p
= &((*callbacks_p
)->next
);
1375 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1376 (*callbacks_p
)->callback
= callback
;
1377 (*callbacks_p
)->periodic
= periodic
;
1378 (*callbacks_p
)->time_ms
= time_ms
;
1379 (*callbacks_p
)->removed
= false;
1381 gettimeofday(&now
, NULL
);
1382 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
1383 time_ms
-= (time_ms
% 1000);
1384 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
1385 if ((*callbacks_p
)->when
.tv_usec
> 1000000) {
1386 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
1387 (*callbacks_p
)->when
.tv_sec
+= 1;
1390 (*callbacks_p
)->priv
= priv
;
1391 (*callbacks_p
)->next
= NULL
;
1396 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1397 enum target_event event
, void *priv
), void *priv
)
1399 struct target_event_callback
**p
= &target_event_callbacks
;
1400 struct target_event_callback
*c
= target_event_callbacks
;
1402 if (callback
== NULL
)
1403 return ERROR_COMMAND_SYNTAX_ERROR
;
1406 struct target_event_callback
*next
= c
->next
;
1407 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1419 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1420 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1422 struct target_reset_callback
*entry
;
1424 if (callback
== NULL
)
1425 return ERROR_COMMAND_SYNTAX_ERROR
;
1427 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1428 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1429 list_del(&entry
->list
);
1438 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1440 if (callback
== NULL
)
1441 return ERROR_COMMAND_SYNTAX_ERROR
;
1443 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1445 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1454 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1456 struct target_event_callback
*callback
= target_event_callbacks
;
1457 struct target_event_callback
*next_callback
;
1459 if (event
== TARGET_EVENT_HALTED
) {
1460 /* execute early halted first */
1461 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1464 LOG_DEBUG("target event %i (%s)", event
,
1465 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1467 target_handle_event(target
, event
);
1470 next_callback
= callback
->next
;
1471 callback
->callback(target
, event
, callback
->priv
);
1472 callback
= next_callback
;
1478 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1480 struct target_reset_callback
*callback
;
1482 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1483 Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1485 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1486 callback
->callback(target
, reset_mode
, callback
->priv
);
1491 static int target_timer_callback_periodic_restart(
1492 struct target_timer_callback
*cb
, struct timeval
*now
)
1494 int time_ms
= cb
->time_ms
;
1495 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1496 time_ms
-= (time_ms
% 1000);
1497 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1498 if (cb
->when
.tv_usec
> 1000000) {
1499 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1500 cb
->when
.tv_sec
+= 1;
1505 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1506 struct timeval
*now
)
1508 cb
->callback(cb
->priv
);
1511 return target_timer_callback_periodic_restart(cb
, now
);
1513 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1516 static int target_call_timer_callbacks_check_time(int checktime
)
1518 static bool callback_processing
;
1520 /* Do not allow nesting */
1521 if (callback_processing
)
1524 callback_processing
= true;
1529 gettimeofday(&now
, NULL
);
1531 /* Store an address of the place containing a pointer to the
1532 * next item; initially, that's a standalone "root of the
1533 * list" variable. */
1534 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1536 if ((*callback
)->removed
) {
1537 struct target_timer_callback
*p
= *callback
;
1538 *callback
= (*callback
)->next
;
1543 bool call_it
= (*callback
)->callback
&&
1544 ((!checktime
&& (*callback
)->periodic
) ||
1545 now
.tv_sec
> (*callback
)->when
.tv_sec
||
1546 (now
.tv_sec
== (*callback
)->when
.tv_sec
&&
1547 now
.tv_usec
>= (*callback
)->when
.tv_usec
));
1550 target_call_timer_callback(*callback
, &now
);
1552 callback
= &(*callback
)->next
;
1555 callback_processing
= false;
1559 int target_call_timer_callbacks(void)
1561 return target_call_timer_callbacks_check_time(1);
1564 /* invoke periodic callbacks immediately */
1565 int target_call_timer_callbacks_now(void)
1567 return target_call_timer_callbacks_check_time(0);
1570 /* Prints the working area layout for debug purposes */
1571 static void print_wa_layout(struct target
*target
)
1573 struct working_area
*c
= target
->working_areas
;
1576 LOG_DEBUG("%c%c 0x%08"PRIx32
"-0x%08"PRIx32
" (%"PRIu32
" bytes)",
1577 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1578 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1583 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1584 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1586 assert(area
->free
); /* Shouldn't split an allocated area */
1587 assert(size
<= area
->size
); /* Caller should guarantee this */
1589 /* Split only if not already the right size */
1590 if (size
< area
->size
) {
1591 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1596 new_wa
->next
= area
->next
;
1597 new_wa
->size
= area
->size
- size
;
1598 new_wa
->address
= area
->address
+ size
;
1599 new_wa
->backup
= NULL
;
1600 new_wa
->user
= NULL
;
1601 new_wa
->free
= true;
1603 area
->next
= new_wa
;
1606 /* If backup memory was allocated to this area, it has the wrong size
1607 * now so free it and it will be reallocated if/when needed */
1610 area
->backup
= NULL
;
1615 /* Merge all adjacent free areas into one */
1616 static void target_merge_working_areas(struct target
*target
)
1618 struct working_area
*c
= target
->working_areas
;
1620 while (c
&& c
->next
) {
1621 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1623 /* Find two adjacent free areas */
1624 if (c
->free
&& c
->next
->free
) {
1625 /* Merge the last into the first */
1626 c
->size
+= c
->next
->size
;
1628 /* Remove the last */
1629 struct working_area
*to_be_freed
= c
->next
;
1630 c
->next
= c
->next
->next
;
1631 if (to_be_freed
->backup
)
1632 free(to_be_freed
->backup
);
1635 /* If backup memory was allocated to the remaining area, it's has
1636 * the wrong size now */
1647 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1649 /* Reevaluate working area address based on MMU state*/
1650 if (target
->working_areas
== NULL
) {
1654 retval
= target
->type
->mmu(target
, &enabled
);
1655 if (retval
!= ERROR_OK
)
1659 if (target
->working_area_phys_spec
) {
1660 LOG_DEBUG("MMU disabled, using physical "
1661 "address for working memory 0x%08"PRIx32
,
1662 target
->working_area_phys
);
1663 target
->working_area
= target
->working_area_phys
;
1665 LOG_ERROR("No working memory available. "
1666 "Specify -work-area-phys to target.");
1667 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1670 if (target
->working_area_virt_spec
) {
1671 LOG_DEBUG("MMU enabled, using virtual "
1672 "address for working memory 0x%08"PRIx32
,
1673 target
->working_area_virt
);
1674 target
->working_area
= target
->working_area_virt
;
1676 LOG_ERROR("No working memory available. "
1677 "Specify -work-area-virt to target.");
1678 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1682 /* Set up initial working area on first call */
1683 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1685 new_wa
->next
= NULL
;
1686 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1687 new_wa
->address
= target
->working_area
;
1688 new_wa
->backup
= NULL
;
1689 new_wa
->user
= NULL
;
1690 new_wa
->free
= true;
1693 target
->working_areas
= new_wa
;
1696 /* only allocate multiples of 4 byte */
1698 size
= (size
+ 3) & (~3UL);
1700 struct working_area
*c
= target
->working_areas
;
1702 /* Find the first large enough working area */
1704 if (c
->free
&& c
->size
>= size
)
1710 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1712 /* Split the working area into the requested size */
1713 target_split_working_area(c
, size
);
1715 LOG_DEBUG("allocated new working area of %"PRIu32
" bytes at address 0x%08"PRIx32
, size
, c
->address
);
1717 if (target
->backup_working_area
) {
1718 if (c
->backup
== NULL
) {
1719 c
->backup
= malloc(c
->size
);
1720 if (c
->backup
== NULL
)
1724 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1725 if (retval
!= ERROR_OK
)
1729 /* mark as used, and return the new (reused) area */
1736 print_wa_layout(target
);
1741 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1745 retval
= target_alloc_working_area_try(target
, size
, area
);
1746 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1747 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1752 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1754 int retval
= ERROR_OK
;
1756 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1757 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1758 if (retval
!= ERROR_OK
)
1759 LOG_ERROR("failed to restore %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1760 area
->size
, area
->address
);
1766 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1767 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1769 int retval
= ERROR_OK
;
1775 retval
= target_restore_working_area(target
, area
);
1776 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1777 if (retval
!= ERROR_OK
)
1783 LOG_DEBUG("freed %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1784 area
->size
, area
->address
);
1786 /* mark user pointer invalid */
1787 /* TODO: Is this really safe? It points to some previous caller's memory.
1788 * How could we know that the area pointer is still in that place and not
1789 * some other vital data? What's the purpose of this, anyway? */
1793 target_merge_working_areas(target
);
1795 print_wa_layout(target
);
1800 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1802 return target_free_working_area_restore(target
, area
, 1);
1805 void target_quit(void)
1807 struct target_event_callback
*pe
= target_event_callbacks
;
1809 struct target_event_callback
*t
= pe
->next
;
1813 target_event_callbacks
= NULL
;
1815 struct target_timer_callback
*pt
= target_timer_callbacks
;
1817 struct target_timer_callback
*t
= pt
->next
;
1821 target_timer_callbacks
= NULL
;
1823 for (struct target
*target
= all_targets
;
1824 target
; target
= target
->next
) {
1825 if (target
->type
->deinit_target
)
1826 target
->type
->deinit_target(target
);
1830 /* free resources and restore memory, if restoring memory fails,
1831 * free up resources anyway
1833 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1835 struct working_area
*c
= target
->working_areas
;
1837 LOG_DEBUG("freeing all working areas");
1839 /* Loop through all areas, restoring the allocated ones and marking them as free */
1843 target_restore_working_area(target
, c
);
1845 *c
->user
= NULL
; /* Same as above */
1851 /* Run a merge pass to combine all areas into one */
1852 target_merge_working_areas(target
);
1854 print_wa_layout(target
);
1857 void target_free_all_working_areas(struct target
*target
)
1859 target_free_all_working_areas_restore(target
, 1);
1862 /* Find the largest number of bytes that can be allocated */
1863 uint32_t target_get_working_area_avail(struct target
*target
)
1865 struct working_area
*c
= target
->working_areas
;
1866 uint32_t max_size
= 0;
1869 return target
->working_area_size
;
1872 if (c
->free
&& max_size
< c
->size
)
1881 int target_arch_state(struct target
*target
)
1884 if (target
== NULL
) {
1885 LOG_USER("No target has been configured");
1889 LOG_USER("target state: %s", target_state_name(target
));
1891 if (target
->state
!= TARGET_HALTED
)
1894 retval
= target
->type
->arch_state(target
);
1898 static int target_get_gdb_fileio_info_default(struct target
*target
,
1899 struct gdb_fileio_info
*fileio_info
)
1901 /* If target does not support semi-hosting function, target
1902 has no need to provide .get_gdb_fileio_info callback.
1903 It just return ERROR_FAIL and gdb_server will return "Txx"
1904 as target halted every time. */
1908 static int target_gdb_fileio_end_default(struct target
*target
,
1909 int retcode
, int fileio_errno
, bool ctrl_c
)
1914 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
1915 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1917 struct timeval timeout
, now
;
1919 gettimeofday(&timeout
, NULL
);
1920 timeval_add_time(&timeout
, seconds
, 0);
1922 LOG_INFO("Starting profiling. Halting and resuming the"
1923 " target as often as we can...");
1925 uint32_t sample_count
= 0;
1926 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
1927 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
1929 int retval
= ERROR_OK
;
1931 target_poll(target
);
1932 if (target
->state
== TARGET_HALTED
) {
1933 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
1934 samples
[sample_count
++] = t
;
1935 /* current pc, addr = 0, do not handle breakpoints, not debugging */
1936 retval
= target_resume(target
, 1, 0, 0, 0);
1937 target_poll(target
);
1938 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
1939 } else if (target
->state
== TARGET_RUNNING
) {
1940 /* We want to quickly sample the PC. */
1941 retval
= target_halt(target
);
1943 LOG_INFO("Target not halted or running");
1948 if (retval
!= ERROR_OK
)
1951 gettimeofday(&now
, NULL
);
1952 if ((sample_count
>= max_num_samples
) ||
1953 ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
))) {
1954 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
1959 *num_samples
= sample_count
;
1963 /* Single aligned words are guaranteed to use 16 or 32 bit access
1964 * mode respectively, otherwise data is handled as quickly as
1967 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
1969 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1970 (int)size
, (unsigned)address
);
1972 if (!target_was_examined(target
)) {
1973 LOG_ERROR("Target not examined yet");
1980 if ((address
+ size
- 1) < address
) {
1981 /* GDB can request this when e.g. PC is 0xfffffffc*/
1982 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1988 return target
->type
->write_buffer(target
, address
, size
, buffer
);
1991 static int target_write_buffer_default(struct target
*target
, uint32_t address
, uint32_t count
, const uint8_t *buffer
)
1995 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
1996 * will have something to do with the size we leave to it. */
1997 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
1998 if (address
& size
) {
1999 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2000 if (retval
!= ERROR_OK
)
2008 /* Write the data with as large access size as possible. */
2009 for (; size
> 0; size
/= 2) {
2010 uint32_t aligned
= count
- count
% size
;
2012 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2013 if (retval
!= ERROR_OK
)
2024 /* Single aligned words are guaranteed to use 16 or 32 bit access
2025 * mode respectively, otherwise data is handled as quickly as
2028 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
2030 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
2031 (int)size
, (unsigned)address
);
2033 if (!target_was_examined(target
)) {
2034 LOG_ERROR("Target not examined yet");
2041 if ((address
+ size
- 1) < address
) {
2042 /* GDB can request this when e.g. PC is 0xfffffffc*/
2043 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
2049 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2052 static int target_read_buffer_default(struct target
*target
, uint32_t address
, uint32_t count
, uint8_t *buffer
)
2056 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2057 * will have something to do with the size we leave to it. */
2058 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2059 if (address
& size
) {
2060 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2061 if (retval
!= ERROR_OK
)
2069 /* Read the data with as large access size as possible. */
2070 for (; size
> 0; size
/= 2) {
2071 uint32_t aligned
= count
- count
% size
;
2073 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2074 if (retval
!= ERROR_OK
)
2085 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
2090 uint32_t checksum
= 0;
2091 if (!target_was_examined(target
)) {
2092 LOG_ERROR("Target not examined yet");
2096 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2097 if (retval
!= ERROR_OK
) {
2098 buffer
= malloc(size
);
2099 if (buffer
== NULL
) {
2100 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
2101 return ERROR_COMMAND_SYNTAX_ERROR
;
2103 retval
= target_read_buffer(target
, address
, size
, buffer
);
2104 if (retval
!= ERROR_OK
) {
2109 /* convert to target endianness */
2110 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2111 uint32_t target_data
;
2112 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2113 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2116 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2125 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
2128 if (!target_was_examined(target
)) {
2129 LOG_ERROR("Target not examined yet");
2133 if (target
->type
->blank_check_memory
== 0)
2134 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2136 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
2141 int target_read_u64(struct target
*target
, uint64_t address
, uint64_t *value
)
2143 uint8_t value_buf
[8];
2144 if (!target_was_examined(target
)) {
2145 LOG_ERROR("Target not examined yet");
2149 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2151 if (retval
== ERROR_OK
) {
2152 *value
= target_buffer_get_u64(target
, value_buf
);
2153 LOG_DEBUG("address: 0x%" PRIx64
", value: 0x%16.16" PRIx64
"",
2158 LOG_DEBUG("address: 0x%" PRIx64
" failed",
2165 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
2167 uint8_t value_buf
[4];
2168 if (!target_was_examined(target
)) {
2169 LOG_ERROR("Target not examined yet");
2173 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2175 if (retval
== ERROR_OK
) {
2176 *value
= target_buffer_get_u32(target
, value_buf
);
2177 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
2182 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2189 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
2191 uint8_t value_buf
[2];
2192 if (!target_was_examined(target
)) {
2193 LOG_ERROR("Target not examined yet");
2197 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2199 if (retval
== ERROR_OK
) {
2200 *value
= target_buffer_get_u16(target
, value_buf
);
2201 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
2206 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2213 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
2215 if (!target_was_examined(target
)) {
2216 LOG_ERROR("Target not examined yet");
2220 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2222 if (retval
== ERROR_OK
) {
2223 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
2228 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2235 int target_write_u64(struct target
*target
, uint64_t address
, uint64_t value
)
2238 uint8_t value_buf
[8];
2239 if (!target_was_examined(target
)) {
2240 LOG_ERROR("Target not examined yet");
2244 LOG_DEBUG("address: 0x%" PRIx64
", value: 0x%16.16" PRIx64
"",
2248 target_buffer_set_u64(target
, value_buf
, value
);
2249 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2250 if (retval
!= ERROR_OK
)
2251 LOG_DEBUG("failed: %i", retval
);
2256 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
2259 uint8_t value_buf
[4];
2260 if (!target_was_examined(target
)) {
2261 LOG_ERROR("Target not examined yet");
2265 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
2269 target_buffer_set_u32(target
, value_buf
, value
);
2270 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2271 if (retval
!= ERROR_OK
)
2272 LOG_DEBUG("failed: %i", retval
);
2277 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
2280 uint8_t value_buf
[2];
2281 if (!target_was_examined(target
)) {
2282 LOG_ERROR("Target not examined yet");
2286 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
2290 target_buffer_set_u16(target
, value_buf
, value
);
2291 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2292 if (retval
!= ERROR_OK
)
2293 LOG_DEBUG("failed: %i", retval
);
2298 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
2301 if (!target_was_examined(target
)) {
2302 LOG_ERROR("Target not examined yet");
2306 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
2309 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2310 if (retval
!= ERROR_OK
)
2311 LOG_DEBUG("failed: %i", retval
);
2316 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2318 struct target
*target
= get_target(name
);
2319 if (target
== NULL
) {
2320 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2323 if (!target
->tap
->enabled
) {
2324 LOG_USER("Target: TAP %s is disabled, "
2325 "can't be the current target\n",
2326 target
->tap
->dotted_name
);
2330 cmd_ctx
->current_target
= target
->target_number
;
2335 COMMAND_HANDLER(handle_targets_command
)
2337 int retval
= ERROR_OK
;
2338 if (CMD_ARGC
== 1) {
2339 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2340 if (retval
== ERROR_OK
) {
2346 struct target
*target
= all_targets
;
2347 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2348 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2353 if (target
->tap
->enabled
)
2354 state
= target_state_name(target
);
2356 state
= "tap-disabled";
2358 if (CMD_CTX
->current_target
== target
->target_number
)
2361 /* keep columns lined up to match the headers above */
2362 command_print(CMD_CTX
,
2363 "%2d%c %-18s %-10s %-6s %-18s %s",
2364 target
->target_number
,
2366 target_name(target
),
2367 target_type_name(target
),
2368 Jim_Nvp_value2name_simple(nvp_target_endian
,
2369 target
->endianness
)->name
,
2370 target
->tap
->dotted_name
,
2372 target
= target
->next
;
2378 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2380 static int powerDropout
;
2381 static int srstAsserted
;
2383 static int runPowerRestore
;
2384 static int runPowerDropout
;
2385 static int runSrstAsserted
;
2386 static int runSrstDeasserted
;
2388 static int sense_handler(void)
2390 static int prevSrstAsserted
;
2391 static int prevPowerdropout
;
2393 int retval
= jtag_power_dropout(&powerDropout
);
2394 if (retval
!= ERROR_OK
)
2398 powerRestored
= prevPowerdropout
&& !powerDropout
;
2400 runPowerRestore
= 1;
2402 long long current
= timeval_ms();
2403 static long long lastPower
;
2404 int waitMore
= lastPower
+ 2000 > current
;
2405 if (powerDropout
&& !waitMore
) {
2406 runPowerDropout
= 1;
2407 lastPower
= current
;
2410 retval
= jtag_srst_asserted(&srstAsserted
);
2411 if (retval
!= ERROR_OK
)
2415 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2417 static long long lastSrst
;
2418 waitMore
= lastSrst
+ 2000 > current
;
2419 if (srstDeasserted
&& !waitMore
) {
2420 runSrstDeasserted
= 1;
2424 if (!prevSrstAsserted
&& srstAsserted
)
2425 runSrstAsserted
= 1;
2427 prevSrstAsserted
= srstAsserted
;
2428 prevPowerdropout
= powerDropout
;
2430 if (srstDeasserted
|| powerRestored
) {
2431 /* Other than logging the event we can't do anything here.
2432 * Issuing a reset is a particularly bad idea as we might
2433 * be inside a reset already.
2440 /* process target state changes */
2441 static int handle_target(void *priv
)
2443 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2444 int retval
= ERROR_OK
;
2446 if (!is_jtag_poll_safe()) {
2447 /* polling is disabled currently */
2451 /* we do not want to recurse here... */
2452 static int recursive
;
2456 /* danger! running these procedures can trigger srst assertions and power dropouts.
2457 * We need to avoid an infinite loop/recursion here and we do that by
2458 * clearing the flags after running these events.
2460 int did_something
= 0;
2461 if (runSrstAsserted
) {
2462 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2463 Jim_Eval(interp
, "srst_asserted");
2466 if (runSrstDeasserted
) {
2467 Jim_Eval(interp
, "srst_deasserted");
2470 if (runPowerDropout
) {
2471 LOG_INFO("Power dropout detected, running power_dropout proc.");
2472 Jim_Eval(interp
, "power_dropout");
2475 if (runPowerRestore
) {
2476 Jim_Eval(interp
, "power_restore");
2480 if (did_something
) {
2481 /* clear detect flags */
2485 /* clear action flags */
2487 runSrstAsserted
= 0;
2488 runSrstDeasserted
= 0;
2489 runPowerRestore
= 0;
2490 runPowerDropout
= 0;
2495 /* Poll targets for state changes unless that's globally disabled.
2496 * Skip targets that are currently disabled.
2498 for (struct target
*target
= all_targets
;
2499 is_jtag_poll_safe() && target
;
2500 target
= target
->next
) {
2502 if (!target_was_examined(target
))
2505 if (!target
->tap
->enabled
)
2508 if (target
->backoff
.times
> target
->backoff
.count
) {
2509 /* do not poll this time as we failed previously */
2510 target
->backoff
.count
++;
2513 target
->backoff
.count
= 0;
2515 /* only poll target if we've got power and srst isn't asserted */
2516 if (!powerDropout
&& !srstAsserted
) {
2517 /* polling may fail silently until the target has been examined */
2518 retval
= target_poll(target
);
2519 if (retval
!= ERROR_OK
) {
2520 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2521 if (target
->backoff
.times
* polling_interval
< 5000) {
2522 target
->backoff
.times
*= 2;
2523 target
->backoff
.times
++;
2525 LOG_USER("Polling target %s failed, GDB will be halted. Polling again in %dms",
2526 target_name(target
),
2527 target
->backoff
.times
* polling_interval
);
2529 /* Tell GDB to halt the debugger. This allows the user to
2530 * run monitor commands to handle the situation.
2532 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2535 /* Since we succeeded, we reset backoff count */
2536 if (target
->backoff
.times
> 0) {
2537 LOG_USER("Polling target %s succeeded again, trying to reexamine", target_name(target
));
2538 target_reset_examined(target
);
2539 retval
= target_examine_one(target
);
2540 /* Target examination could have failed due to unstable connection,
2541 * but we set the examined flag anyway to repoll it later */
2542 if (retval
!= ERROR_OK
) {
2543 target
->examined
= true;
2548 target
->backoff
.times
= 0;
2555 COMMAND_HANDLER(handle_reg_command
)
2557 struct target
*target
;
2558 struct reg
*reg
= NULL
;
2564 target
= get_current_target(CMD_CTX
);
2566 /* list all available registers for the current target */
2567 if (CMD_ARGC
== 0) {
2568 struct reg_cache
*cache
= target
->reg_cache
;
2574 command_print(CMD_CTX
, "===== %s", cache
->name
);
2576 for (i
= 0, reg
= cache
->reg_list
;
2577 i
< cache
->num_regs
;
2578 i
++, reg
++, count
++) {
2579 /* only print cached values if they are valid */
2581 value
= buf_to_str(reg
->value
,
2583 command_print(CMD_CTX
,
2584 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2592 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2597 cache
= cache
->next
;
2603 /* access a single register by its ordinal number */
2604 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2606 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2608 struct reg_cache
*cache
= target
->reg_cache
;
2612 for (i
= 0; i
< cache
->num_regs
; i
++) {
2613 if (count
++ == num
) {
2614 reg
= &cache
->reg_list
[i
];
2620 cache
= cache
->next
;
2624 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2625 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2629 /* access a single register by its name */
2630 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2633 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2638 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2640 /* display a register */
2641 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2642 && (CMD_ARGV
[1][0] <= '9')))) {
2643 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2646 if (reg
->valid
== 0)
2647 reg
->type
->get(reg
);
2648 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2649 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2654 /* set register value */
2655 if (CMD_ARGC
== 2) {
2656 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2659 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2661 reg
->type
->set(reg
, buf
);
2663 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2664 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2672 return ERROR_COMMAND_SYNTAX_ERROR
;
2675 COMMAND_HANDLER(handle_poll_command
)
2677 int retval
= ERROR_OK
;
2678 struct target
*target
= get_current_target(CMD_CTX
);
2680 if (CMD_ARGC
== 0) {
2681 command_print(CMD_CTX
, "background polling: %s",
2682 jtag_poll_get_enabled() ? "on" : "off");
2683 command_print(CMD_CTX
, "TAP: %s (%s)",
2684 target
->tap
->dotted_name
,
2685 target
->tap
->enabled
? "enabled" : "disabled");
2686 if (!target
->tap
->enabled
)
2688 retval
= target_poll(target
);
2689 if (retval
!= ERROR_OK
)
2691 retval
= target_arch_state(target
);
2692 if (retval
!= ERROR_OK
)
2694 } else if (CMD_ARGC
== 1) {
2696 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2697 jtag_poll_set_enabled(enable
);
2699 return ERROR_COMMAND_SYNTAX_ERROR
;
2704 COMMAND_HANDLER(handle_wait_halt_command
)
2707 return ERROR_COMMAND_SYNTAX_ERROR
;
2709 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2710 if (1 == CMD_ARGC
) {
2711 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2712 if (ERROR_OK
!= retval
)
2713 return ERROR_COMMAND_SYNTAX_ERROR
;
2716 struct target
*target
= get_current_target(CMD_CTX
);
2717 return target_wait_state(target
, TARGET_HALTED
, ms
);
2720 /* wait for target state to change. The trick here is to have a low
2721 * latency for short waits and not to suck up all the CPU time
2724 * After 500ms, keep_alive() is invoked
2726 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2729 long long then
= 0, cur
;
2733 retval
= target_poll(target
);
2734 if (retval
!= ERROR_OK
)
2736 if (target
->state
== state
)
2741 then
= timeval_ms();
2742 LOG_DEBUG("waiting for target %s...",
2743 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2749 if ((cur
-then
) > ms
) {
2750 LOG_ERROR("timed out while waiting for target %s",
2751 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2759 COMMAND_HANDLER(handle_halt_command
)
2763 struct target
*target
= get_current_target(CMD_CTX
);
2764 int retval
= target_halt(target
);
2765 if (ERROR_OK
!= retval
)
2768 if (CMD_ARGC
== 1) {
2769 unsigned wait_local
;
2770 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2771 if (ERROR_OK
!= retval
)
2772 return ERROR_COMMAND_SYNTAX_ERROR
;
2777 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2780 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2782 struct target
*target
= get_current_target(CMD_CTX
);
2784 LOG_USER("requesting target halt and executing a soft reset");
2786 target_soft_reset_halt(target
);
2791 COMMAND_HANDLER(handle_reset_command
)
2794 return ERROR_COMMAND_SYNTAX_ERROR
;
2796 enum target_reset_mode reset_mode
= RESET_RUN
;
2797 if (CMD_ARGC
== 1) {
2799 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2800 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
2801 return ERROR_COMMAND_SYNTAX_ERROR
;
2802 reset_mode
= n
->value
;
2805 /* reset *all* targets */
2806 return target_process_reset(CMD_CTX
, reset_mode
);
2810 COMMAND_HANDLER(handle_resume_command
)
2814 return ERROR_COMMAND_SYNTAX_ERROR
;
2816 struct target
*target
= get_current_target(CMD_CTX
);
2818 /* with no CMD_ARGV, resume from current pc, addr = 0,
2819 * with one arguments, addr = CMD_ARGV[0],
2820 * handle breakpoints, not debugging */
2822 if (CMD_ARGC
== 1) {
2823 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2827 return target_resume(target
, current
, addr
, 1, 0);
2830 COMMAND_HANDLER(handle_step_command
)
2833 return ERROR_COMMAND_SYNTAX_ERROR
;
2837 /* with no CMD_ARGV, step from current pc, addr = 0,
2838 * with one argument addr = CMD_ARGV[0],
2839 * handle breakpoints, debugging */
2842 if (CMD_ARGC
== 1) {
2843 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2847 struct target
*target
= get_current_target(CMD_CTX
);
2849 return target
->type
->step(target
, current_pc
, addr
, 1);
2852 static void handle_md_output(struct command_context
*cmd_ctx
,
2853 struct target
*target
, uint32_t address
, unsigned size
,
2854 unsigned count
, const uint8_t *buffer
)
2856 const unsigned line_bytecnt
= 32;
2857 unsigned line_modulo
= line_bytecnt
/ size
;
2859 char output
[line_bytecnt
* 4 + 1];
2860 unsigned output_len
= 0;
2862 const char *value_fmt
;
2865 value_fmt
= "%8.8x ";
2868 value_fmt
= "%4.4x ";
2871 value_fmt
= "%2.2x ";
2874 /* "can't happen", caller checked */
2875 LOG_ERROR("invalid memory read size: %u", size
);
2879 for (unsigned i
= 0; i
< count
; i
++) {
2880 if (i
% line_modulo
== 0) {
2881 output_len
+= snprintf(output
+ output_len
,
2882 sizeof(output
) - output_len
,
2884 (unsigned)(address
+ (i
*size
)));
2888 const uint8_t *value_ptr
= buffer
+ i
* size
;
2891 value
= target_buffer_get_u32(target
, value_ptr
);
2894 value
= target_buffer_get_u16(target
, value_ptr
);
2899 output_len
+= snprintf(output
+ output_len
,
2900 sizeof(output
) - output_len
,
2903 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
2904 command_print(cmd_ctx
, "%s", output
);
2910 COMMAND_HANDLER(handle_md_command
)
2913 return ERROR_COMMAND_SYNTAX_ERROR
;
2916 switch (CMD_NAME
[2]) {
2927 return ERROR_COMMAND_SYNTAX_ERROR
;
2930 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2931 int (*fn
)(struct target
*target
,
2932 uint32_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
2936 fn
= target_read_phys_memory
;
2938 fn
= target_read_memory
;
2939 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2940 return ERROR_COMMAND_SYNTAX_ERROR
;
2943 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2947 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2949 uint8_t *buffer
= calloc(count
, size
);
2951 struct target
*target
= get_current_target(CMD_CTX
);
2952 int retval
= fn(target
, address
, size
, count
, buffer
);
2953 if (ERROR_OK
== retval
)
2954 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2961 typedef int (*target_write_fn
)(struct target
*target
,
2962 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
2964 static int target_fill_mem(struct target
*target
,
2973 /* We have to write in reasonably large chunks to be able
2974 * to fill large memory areas with any sane speed */
2975 const unsigned chunk_size
= 16384;
2976 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
2977 if (target_buf
== NULL
) {
2978 LOG_ERROR("Out of memory");
2982 for (unsigned i
= 0; i
< chunk_size
; i
++) {
2983 switch (data_size
) {
2985 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
2988 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
2991 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
2998 int retval
= ERROR_OK
;
3000 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
3003 if (current
> chunk_size
)
3004 current
= chunk_size
;
3005 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
3006 if (retval
!= ERROR_OK
)
3008 /* avoid GDB timeouts */
3017 COMMAND_HANDLER(handle_mw_command
)
3020 return ERROR_COMMAND_SYNTAX_ERROR
;
3021 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3026 fn
= target_write_phys_memory
;
3028 fn
= target_write_memory
;
3029 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
3030 return ERROR_COMMAND_SYNTAX_ERROR
;
3033 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
3036 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
3040 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
3042 struct target
*target
= get_current_target(CMD_CTX
);
3044 switch (CMD_NAME
[2]) {
3055 return ERROR_COMMAND_SYNTAX_ERROR
;
3058 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
3061 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
3062 uint32_t *min_address
, uint32_t *max_address
)
3064 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
3065 return ERROR_COMMAND_SYNTAX_ERROR
;
3067 /* a base address isn't always necessary,
3068 * default to 0x0 (i.e. don't relocate) */
3069 if (CMD_ARGC
>= 2) {
3071 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
3072 image
->base_address
= addr
;
3073 image
->base_address_set
= 1;
3075 image
->base_address_set
= 0;
3077 image
->start_address_set
= 0;
3080 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
3081 if (CMD_ARGC
== 5) {
3082 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
3083 /* use size (given) to find max (required) */
3084 *max_address
+= *min_address
;
3087 if (*min_address
> *max_address
)
3088 return ERROR_COMMAND_SYNTAX_ERROR
;
3093 COMMAND_HANDLER(handle_load_image_command
)
3097 uint32_t image_size
;
3098 uint32_t min_address
= 0;
3099 uint32_t max_address
= 0xffffffff;
3103 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
3104 &image
, &min_address
, &max_address
);
3105 if (ERROR_OK
!= retval
)
3108 struct target
*target
= get_current_target(CMD_CTX
);
3110 struct duration bench
;
3111 duration_start(&bench
);
3113 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
3118 for (i
= 0; i
< image
.num_sections
; i
++) {
3119 buffer
= malloc(image
.sections
[i
].size
);
3120 if (buffer
== NULL
) {
3121 command_print(CMD_CTX
,
3122 "error allocating buffer for section (%d bytes)",
3123 (int)(image
.sections
[i
].size
));
3127 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3128 if (retval
!= ERROR_OK
) {
3133 uint32_t offset
= 0;
3134 uint32_t length
= buf_cnt
;
3136 /* DANGER!!! beware of unsigned comparision here!!! */
3138 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3139 (image
.sections
[i
].base_address
< max_address
)) {
3141 if (image
.sections
[i
].base_address
< min_address
) {
3142 /* clip addresses below */
3143 offset
+= min_address
-image
.sections
[i
].base_address
;
3147 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3148 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3150 retval
= target_write_buffer(target
,
3151 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3152 if (retval
!= ERROR_OK
) {
3156 image_size
+= length
;
3157 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
3158 (unsigned int)length
,
3159 image
.sections
[i
].base_address
+ offset
);
3165 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3166 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
3167 "in %fs (%0.3f KiB/s)", image_size
,
3168 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3171 image_close(&image
);
3177 COMMAND_HANDLER(handle_dump_image_command
)
3179 struct fileio fileio
;
3181 int retval
, retvaltemp
;
3182 uint32_t address
, size
;
3183 struct duration bench
;
3184 struct target
*target
= get_current_target(CMD_CTX
);
3187 return ERROR_COMMAND_SYNTAX_ERROR
;
3189 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
3190 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
3192 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
3193 buffer
= malloc(buf_size
);
3197 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3198 if (retval
!= ERROR_OK
) {
3203 duration_start(&bench
);
3206 size_t size_written
;
3207 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
3208 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
3209 if (retval
!= ERROR_OK
)
3212 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
3213 if (retval
!= ERROR_OK
)
3216 size
-= this_run_size
;
3217 address
+= this_run_size
;
3222 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3224 retval
= fileio_size(&fileio
, &filesize
);
3225 if (retval
!= ERROR_OK
)
3227 command_print(CMD_CTX
,
3228 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize
,
3229 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3232 retvaltemp
= fileio_close(&fileio
);
3233 if (retvaltemp
!= ERROR_OK
)
3239 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
3243 uint32_t image_size
;
3246 uint32_t checksum
= 0;
3247 uint32_t mem_checksum
= 0;
3251 struct target
*target
= get_current_target(CMD_CTX
);
3254 return ERROR_COMMAND_SYNTAX_ERROR
;
3257 LOG_ERROR("no target selected");
3261 struct duration bench
;
3262 duration_start(&bench
);
3264 if (CMD_ARGC
>= 2) {
3266 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
3267 image
.base_address
= addr
;
3268 image
.base_address_set
= 1;
3270 image
.base_address_set
= 0;
3271 image
.base_address
= 0x0;
3274 image
.start_address_set
= 0;
3276 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3277 if (retval
!= ERROR_OK
)
3283 for (i
= 0; i
< image
.num_sections
; i
++) {
3284 buffer
= malloc(image
.sections
[i
].size
);
3285 if (buffer
== NULL
) {
3286 command_print(CMD_CTX
,
3287 "error allocating buffer for section (%d bytes)",
3288 (int)(image
.sections
[i
].size
));
3291 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3292 if (retval
!= ERROR_OK
) {
3298 /* calculate checksum of image */
3299 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3300 if (retval
!= ERROR_OK
) {
3305 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3306 if (retval
!= ERROR_OK
) {
3311 if (checksum
!= mem_checksum
) {
3312 /* failed crc checksum, fall back to a binary compare */
3316 LOG_ERROR("checksum mismatch - attempting binary compare");
3318 data
= malloc(buf_cnt
);
3320 /* Can we use 32bit word accesses? */
3322 int count
= buf_cnt
;
3323 if ((count
% 4) == 0) {
3327 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
3328 if (retval
== ERROR_OK
) {
3330 for (t
= 0; t
< buf_cnt
; t
++) {
3331 if (data
[t
] != buffer
[t
]) {
3332 command_print(CMD_CTX
,
3333 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3335 (unsigned)(t
+ image
.sections
[i
].base_address
),
3338 if (diffs
++ >= 127) {
3339 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
3351 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
3352 image
.sections
[i
].base_address
,
3357 image_size
+= buf_cnt
;
3360 command_print(CMD_CTX
, "No more differences found.");
3363 retval
= ERROR_FAIL
;
3364 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3365 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3366 "in %fs (%0.3f KiB/s)", image_size
,
3367 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3370 image_close(&image
);
3375 COMMAND_HANDLER(handle_verify_image_command
)
3377 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
3380 COMMAND_HANDLER(handle_test_image_command
)
3382 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
3385 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
3387 struct target
*target
= get_current_target(cmd_ctx
);
3388 struct breakpoint
*breakpoint
= target
->breakpoints
;
3389 while (breakpoint
) {
3390 if (breakpoint
->type
== BKPT_SOFT
) {
3391 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3392 breakpoint
->length
, 16);
3393 command_print(cmd_ctx
, "IVA breakpoint: 0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
3394 breakpoint
->address
,
3396 breakpoint
->set
, buf
);
3399 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3400 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3402 breakpoint
->length
, breakpoint
->set
);
3403 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3404 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3405 breakpoint
->address
,
3406 breakpoint
->length
, breakpoint
->set
);
3407 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3410 command_print(cmd_ctx
, "Breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3411 breakpoint
->address
,
3412 breakpoint
->length
, breakpoint
->set
);
3415 breakpoint
= breakpoint
->next
;
3420 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
3421 uint32_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3423 struct target
*target
= get_current_target(cmd_ctx
);
3427 retval
= breakpoint_add(target
, addr
, length
, hw
);
3428 if (ERROR_OK
== retval
)
3429 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
3431 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3434 } else if (addr
== 0) {
3435 if (target
->type
->add_context_breakpoint
== NULL
) {
3436 LOG_WARNING("Context breakpoint not available");
3439 retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3440 if (ERROR_OK
== retval
)
3441 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3443 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3447 if (target
->type
->add_hybrid_breakpoint
== NULL
) {
3448 LOG_WARNING("Hybrid breakpoint not available");
3451 retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3452 if (ERROR_OK
== retval
)
3453 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3455 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3462 COMMAND_HANDLER(handle_bp_command
)
3471 return handle_bp_command_list(CMD_CTX
);
3475 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3476 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3477 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3480 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
3482 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3484 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3487 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3488 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
3490 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
3491 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3493 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3498 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3499 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
3500 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
3501 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3504 return ERROR_COMMAND_SYNTAX_ERROR
;
3508 COMMAND_HANDLER(handle_rbp_command
)
3511 return ERROR_COMMAND_SYNTAX_ERROR
;
3514 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3516 struct target
*target
= get_current_target(CMD_CTX
);
3517 breakpoint_remove(target
, addr
);
3522 COMMAND_HANDLER(handle_wp_command
)
3524 struct target
*target
= get_current_target(CMD_CTX
);
3526 if (CMD_ARGC
== 0) {
3527 struct watchpoint
*watchpoint
= target
->watchpoints
;
3529 while (watchpoint
) {
3530 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
3531 ", len: 0x%8.8" PRIx32
3532 ", r/w/a: %i, value: 0x%8.8" PRIx32
3533 ", mask: 0x%8.8" PRIx32
,
3534 watchpoint
->address
,
3536 (int)watchpoint
->rw
,
3539 watchpoint
= watchpoint
->next
;
3544 enum watchpoint_rw type
= WPT_ACCESS
;
3546 uint32_t length
= 0;
3547 uint32_t data_value
= 0x0;
3548 uint32_t data_mask
= 0xffffffff;
3552 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
3555 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3558 switch (CMD_ARGV
[2][0]) {
3569 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3570 return ERROR_COMMAND_SYNTAX_ERROR
;
3574 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3575 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3579 return ERROR_COMMAND_SYNTAX_ERROR
;
3582 int retval
= watchpoint_add(target
, addr
, length
, type
,
3583 data_value
, data_mask
);
3584 if (ERROR_OK
!= retval
)
3585 LOG_ERROR("Failure setting watchpoints");
3590 COMMAND_HANDLER(handle_rwp_command
)
3593 return ERROR_COMMAND_SYNTAX_ERROR
;
3596 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3598 struct target
*target
= get_current_target(CMD_CTX
);
3599 watchpoint_remove(target
, addr
);
3605 * Translate a virtual address to a physical address.
3607 * The low-level target implementation must have logged a detailed error
3608 * which is forwarded to telnet/GDB session.
3610 COMMAND_HANDLER(handle_virt2phys_command
)
3613 return ERROR_COMMAND_SYNTAX_ERROR
;
3616 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
3619 struct target
*target
= get_current_target(CMD_CTX
);
3620 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3621 if (retval
== ERROR_OK
)
3622 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
3627 static void writeData(FILE *f
, const void *data
, size_t len
)
3629 size_t written
= fwrite(data
, 1, len
, f
);
3631 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3634 static void writeLong(FILE *f
, int l
, struct target
*target
)
3638 target_buffer_set_u32(target
, val
, l
);
3639 writeData(f
, val
, 4);
3642 static void writeString(FILE *f
, char *s
)
3644 writeData(f
, s
, strlen(s
));
3647 typedef unsigned char UNIT
[2]; /* unit of profiling */
3649 /* Dump a gmon.out histogram file. */
3650 static void write_gmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
, bool with_range
,
3651 uint32_t start_address
, uint32_t end_address
, struct target
*target
)
3654 FILE *f
= fopen(filename
, "w");