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 { .name
= NULL
, .value
= -1 }
223 static const Jim_Nvp nvp_target_state
[] = {
224 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
225 { .name
= "running", .value
= TARGET_RUNNING
},
226 { .name
= "halted", .value
= TARGET_HALTED
},
227 { .name
= "reset", .value
= TARGET_RESET
},
228 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
229 { .name
= NULL
, .value
= -1 },
232 static const Jim_Nvp nvp_target_debug_reason
[] = {
233 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
234 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
235 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
236 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
237 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
238 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
239 { .name
= "program-exit" , .value
= DBG_REASON_EXIT
},
240 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
241 { .name
= NULL
, .value
= -1 },
244 static const Jim_Nvp nvp_target_endian
[] = {
245 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
246 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
247 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
248 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
249 { .name
= NULL
, .value
= -1 },
252 static const Jim_Nvp nvp_reset_modes
[] = {
253 { .name
= "unknown", .value
= RESET_UNKNOWN
},
254 { .name
= "run" , .value
= RESET_RUN
},
255 { .name
= "halt" , .value
= RESET_HALT
},
256 { .name
= "init" , .value
= RESET_INIT
},
257 { .name
= NULL
, .value
= -1 },
260 const char *debug_reason_name(struct target
*t
)
264 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
265 t
->debug_reason
)->name
;
267 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
268 cp
= "(*BUG*unknown*BUG*)";
273 const char *target_state_name(struct target
*t
)
276 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
278 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
279 cp
= "(*BUG*unknown*BUG*)";
284 const char *target_event_name(enum target_event event
)
287 cp
= Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
;
289 LOG_ERROR("Invalid target event: %d", (int)(event
));
290 cp
= "(*BUG*unknown*BUG*)";
295 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
298 cp
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
;
300 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
301 cp
= "(*BUG*unknown*BUG*)";
306 /* determine the number of the new target */
307 static int new_target_number(void)
312 /* number is 0 based */
316 if (x
< t
->target_number
)
317 x
= t
->target_number
;
323 /* read a uint64_t from a buffer in target memory endianness */
324 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
326 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
327 return le_to_h_u64(buffer
);
329 return be_to_h_u64(buffer
);
332 /* read a uint32_t from a buffer in target memory endianness */
333 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
335 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
336 return le_to_h_u32(buffer
);
338 return be_to_h_u32(buffer
);
341 /* read a uint24_t from a buffer in target memory endianness */
342 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
344 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
345 return le_to_h_u24(buffer
);
347 return be_to_h_u24(buffer
);
350 /* read a uint16_t from a buffer in target memory endianness */
351 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
353 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
354 return le_to_h_u16(buffer
);
356 return be_to_h_u16(buffer
);
359 /* read a uint8_t from a buffer in target memory endianness */
360 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
362 return *buffer
& 0x0ff;
365 /* write a uint64_t to a buffer in target memory endianness */
366 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
368 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
369 h_u64_to_le(buffer
, value
);
371 h_u64_to_be(buffer
, value
);
374 /* write a uint32_t to a buffer in target memory endianness */
375 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
377 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
378 h_u32_to_le(buffer
, value
);
380 h_u32_to_be(buffer
, value
);
383 /* write a uint24_t to a buffer in target memory endianness */
384 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
386 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
387 h_u24_to_le(buffer
, value
);
389 h_u24_to_be(buffer
, value
);
392 /* write a uint16_t to a buffer in target memory endianness */
393 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
395 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
396 h_u16_to_le(buffer
, value
);
398 h_u16_to_be(buffer
, value
);
401 /* write a uint8_t to a buffer in target memory endianness */
402 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
407 /* write a uint64_t array to a buffer in target memory endianness */
408 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
411 for (i
= 0; i
< count
; i
++)
412 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
415 /* write a uint32_t array to a buffer in target memory endianness */
416 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
419 for (i
= 0; i
< count
; i
++)
420 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
423 /* write a uint16_t array to a buffer in target memory endianness */
424 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
427 for (i
= 0; i
< count
; i
++)
428 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
431 /* write a uint64_t array to a buffer in target memory endianness */
432 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
435 for (i
= 0; i
< count
; i
++)
436 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
439 /* write a uint32_t array to a buffer in target memory endianness */
440 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
443 for (i
= 0; i
< count
; i
++)
444 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
447 /* write a uint16_t array to a buffer in target memory endianness */
448 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
451 for (i
= 0; i
< count
; i
++)
452 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
455 /* return a pointer to a configured target; id is name or number */
456 struct target
*get_target(const char *id
)
458 struct target
*target
;
460 /* try as tcltarget name */
461 for (target
= all_targets
; target
; target
= target
->next
) {
462 if (target_name(target
) == NULL
)
464 if (strcmp(id
, target_name(target
)) == 0)
468 /* It's OK to remove this fallback sometime after August 2010 or so */
470 /* no match, try as number */
472 if (parse_uint(id
, &num
) != ERROR_OK
)
475 for (target
= all_targets
; target
; target
= target
->next
) {
476 if (target
->target_number
== (int)num
) {
477 LOG_WARNING("use '%s' as target identifier, not '%u'",
478 target_name(target
), num
);
486 /* returns a pointer to the n-th configured target */
487 static struct target
*get_target_by_num(int num
)
489 struct target
*target
= all_targets
;
492 if (target
->target_number
== num
)
494 target
= target
->next
;
500 struct target
*get_current_target(struct command_context
*cmd_ctx
)
502 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
504 if (target
== NULL
) {
505 LOG_ERROR("BUG: current_target out of bounds");
512 int target_poll(struct target
*target
)
516 /* We can't poll until after examine */
517 if (!target_was_examined(target
)) {
518 /* Fail silently lest we pollute the log */
522 retval
= target
->type
->poll(target
);
523 if (retval
!= ERROR_OK
)
526 if (target
->halt_issued
) {
527 if (target
->state
== TARGET_HALTED
)
528 target
->halt_issued
= false;
530 long long t
= timeval_ms() - target
->halt_issued_time
;
531 if (t
> DEFAULT_HALT_TIMEOUT
) {
532 target
->halt_issued
= false;
533 LOG_INFO("Halt timed out, wake up GDB.");
534 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
542 int target_halt(struct target
*target
)
545 /* We can't poll until after examine */
546 if (!target_was_examined(target
)) {
547 LOG_ERROR("Target not examined yet");
551 retval
= target
->type
->halt(target
);
552 if (retval
!= ERROR_OK
)
555 target
->halt_issued
= true;
556 target
->halt_issued_time
= timeval_ms();
562 * Make the target (re)start executing using its saved execution
563 * context (possibly with some modifications).
565 * @param target Which target should start executing.
566 * @param current True to use the target's saved program counter instead
567 * of the address parameter
568 * @param address Optionally used as the program counter.
569 * @param handle_breakpoints True iff breakpoints at the resumption PC
570 * should be skipped. (For example, maybe execution was stopped by
571 * such a breakpoint, in which case it would be counterprodutive to
573 * @param debug_execution False if all working areas allocated by OpenOCD
574 * should be released and/or restored to their original contents.
575 * (This would for example be true to run some downloaded "helper"
576 * algorithm code, which resides in one such working buffer and uses
577 * another for data storage.)
579 * @todo Resolve the ambiguity about what the "debug_execution" flag
580 * signifies. For example, Target implementations don't agree on how
581 * it relates to invalidation of the register cache, or to whether
582 * breakpoints and watchpoints should be enabled. (It would seem wrong
583 * to enable breakpoints when running downloaded "helper" algorithms
584 * (debug_execution true), since the breakpoints would be set to match
585 * target firmware being debugged, not the helper algorithm.... and
586 * enabling them could cause such helpers to malfunction (for example,
587 * by overwriting data with a breakpoint instruction. On the other
588 * hand the infrastructure for running such helpers might use this
589 * procedure but rely on hardware breakpoint to detect termination.)
591 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
595 /* We can't poll until after examine */
596 if (!target_was_examined(target
)) {
597 LOG_ERROR("Target not examined yet");
601 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
603 /* note that resume *must* be asynchronous. The CPU can halt before
604 * we poll. The CPU can even halt at the current PC as a result of
605 * a software breakpoint being inserted by (a bug?) the application.
607 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
608 if (retval
!= ERROR_OK
)
611 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
616 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
621 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
622 if (n
->name
== NULL
) {
623 LOG_ERROR("invalid reset mode");
627 struct target
*target
;
628 for (target
= all_targets
; target
; target
= target
->next
)
629 target_call_reset_callbacks(target
, reset_mode
);
631 /* disable polling during reset to make reset event scripts
632 * more predictable, i.e. dr/irscan & pathmove in events will
633 * not have JTAG operations injected into the middle of a sequence.
635 bool save_poll
= jtag_poll_get_enabled();
637 jtag_poll_set_enabled(false);
639 sprintf(buf
, "ocd_process_reset %s", n
->name
);
640 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
642 jtag_poll_set_enabled(save_poll
);
644 if (retval
!= JIM_OK
) {
645 Jim_MakeErrorMessage(cmd_ctx
->interp
);
646 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
650 /* We want any events to be processed before the prompt */
651 retval
= target_call_timer_callbacks_now();
653 for (target
= all_targets
; target
; target
= target
->next
) {
654 target
->type
->check_reset(target
);
655 target
->running_alg
= false;
661 static int identity_virt2phys(struct target
*target
,
662 uint32_t virtual, uint32_t *physical
)
668 static int no_mmu(struct target
*target
, int *enabled
)
674 static int default_examine(struct target
*target
)
676 target_set_examined(target
);
680 /* no check by default */
681 static int default_check_reset(struct target
*target
)
686 int target_examine_one(struct target
*target
)
688 return target
->type
->examine(target
);
691 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
693 struct target
*target
= priv
;
695 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
698 jtag_unregister_event_callback(jtag_enable_callback
, target
);
700 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
702 int retval
= target_examine_one(target
);
703 if (retval
!= ERROR_OK
)
706 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
711 /* Targets that correctly implement init + examine, i.e.
712 * no communication with target during init:
716 int target_examine(void)
718 int retval
= ERROR_OK
;
719 struct target
*target
;
721 for (target
= all_targets
; target
; target
= target
->next
) {
722 /* defer examination, but don't skip it */
723 if (!target
->tap
->enabled
) {
724 jtag_register_event_callback(jtag_enable_callback
,
729 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
731 retval
= target_examine_one(target
);
732 if (retval
!= ERROR_OK
)
735 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
740 const char *target_type_name(struct target
*target
)
742 return target
->type
->name
;
745 static int target_soft_reset_halt(struct target
*target
)
747 if (!target_was_examined(target
)) {
748 LOG_ERROR("Target not examined yet");
751 if (!target
->type
->soft_reset_halt
) {
752 LOG_ERROR("Target %s does not support soft_reset_halt",
753 target_name(target
));
756 return target
->type
->soft_reset_halt(target
);
760 * Downloads a target-specific native code algorithm to the target,
761 * and executes it. * Note that some targets may need to set up, enable,
762 * and tear down a breakpoint (hard or * soft) to detect algorithm
763 * termination, while others may support lower overhead schemes where
764 * soft breakpoints embedded in the algorithm automatically terminate the
767 * @param target used to run the algorithm
768 * @param arch_info target-specific description of the algorithm.
770 int target_run_algorithm(struct target
*target
,
771 int num_mem_params
, struct mem_param
*mem_params
,
772 int num_reg_params
, struct reg_param
*reg_param
,
773 uint32_t entry_point
, uint32_t exit_point
,
774 int timeout_ms
, void *arch_info
)
776 int retval
= ERROR_FAIL
;
778 if (!target_was_examined(target
)) {
779 LOG_ERROR("Target not examined yet");
782 if (!target
->type
->run_algorithm
) {
783 LOG_ERROR("Target type '%s' does not support %s",
784 target_type_name(target
), __func__
);
788 target
->running_alg
= true;
789 retval
= target
->type
->run_algorithm(target
,
790 num_mem_params
, mem_params
,
791 num_reg_params
, reg_param
,
792 entry_point
, exit_point
, timeout_ms
, arch_info
);
793 target
->running_alg
= false;
800 * Downloads a target-specific native code algorithm to the target,
801 * executes and leaves it running.
803 * @param target used to run the algorithm
804 * @param arch_info target-specific description of the algorithm.
806 int target_start_algorithm(struct target
*target
,
807 int num_mem_params
, struct mem_param
*mem_params
,
808 int num_reg_params
, struct reg_param
*reg_params
,
809 uint32_t entry_point
, uint32_t exit_point
,
812 int retval
= ERROR_FAIL
;
814 if (!target_was_examined(target
)) {
815 LOG_ERROR("Target not examined yet");
818 if (!target
->type
->start_algorithm
) {
819 LOG_ERROR("Target type '%s' does not support %s",
820 target_type_name(target
), __func__
);
823 if (target
->running_alg
) {
824 LOG_ERROR("Target is already running an algorithm");
828 target
->running_alg
= true;
829 retval
= target
->type
->start_algorithm(target
,
830 num_mem_params
, mem_params
,
831 num_reg_params
, reg_params
,
832 entry_point
, exit_point
, arch_info
);
839 * Waits for an algorithm started with target_start_algorithm() to complete.
841 * @param target used to run the algorithm
842 * @param arch_info target-specific description of the algorithm.
844 int target_wait_algorithm(struct target
*target
,
845 int num_mem_params
, struct mem_param
*mem_params
,
846 int num_reg_params
, struct reg_param
*reg_params
,
847 uint32_t exit_point
, int timeout_ms
,
850 int retval
= ERROR_FAIL
;
852 if (!target
->type
->wait_algorithm
) {
853 LOG_ERROR("Target type '%s' does not support %s",
854 target_type_name(target
), __func__
);
857 if (!target
->running_alg
) {
858 LOG_ERROR("Target is not running an algorithm");
862 retval
= target
->type
->wait_algorithm(target
,
863 num_mem_params
, mem_params
,
864 num_reg_params
, reg_params
,
865 exit_point
, timeout_ms
, arch_info
);
866 if (retval
!= ERROR_TARGET_TIMEOUT
)
867 target
->running_alg
= false;
874 * Executes a target-specific native code algorithm in the target.
875 * It differs from target_run_algorithm in that the algorithm is asynchronous.
876 * Because of this it requires an compliant algorithm:
877 * see contrib/loaders/flash/stm32f1x.S for example.
879 * @param target used to run the algorithm
882 int target_run_flash_async_algorithm(struct target
*target
,
883 const uint8_t *buffer
, uint32_t count
, int block_size
,
884 int num_mem_params
, struct mem_param
*mem_params
,
885 int num_reg_params
, struct reg_param
*reg_params
,
886 uint32_t buffer_start
, uint32_t buffer_size
,
887 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
892 const uint8_t *buffer_orig
= buffer
;
894 /* Set up working area. First word is write pointer, second word is read pointer,
895 * rest is fifo data area. */
896 uint32_t wp_addr
= buffer_start
;
897 uint32_t rp_addr
= buffer_start
+ 4;
898 uint32_t fifo_start_addr
= buffer_start
+ 8;
899 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
901 uint32_t wp
= fifo_start_addr
;
902 uint32_t rp
= fifo_start_addr
;
904 /* validate block_size is 2^n */
905 assert(!block_size
|| !(block_size
& (block_size
- 1)));
907 retval
= target_write_u32(target
, wp_addr
, wp
);
908 if (retval
!= ERROR_OK
)
910 retval
= target_write_u32(target
, rp_addr
, rp
);
911 if (retval
!= ERROR_OK
)
914 /* Start up algorithm on target and let it idle while writing the first chunk */
915 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
916 num_reg_params
, reg_params
,
921 if (retval
!= ERROR_OK
) {
922 LOG_ERROR("error starting target flash write algorithm");
928 retval
= target_read_u32(target
, rp_addr
, &rp
);
929 if (retval
!= ERROR_OK
) {
930 LOG_ERROR("failed to get read pointer");
934 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
935 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
938 LOG_ERROR("flash write algorithm aborted by target");
939 retval
= ERROR_FLASH_OPERATION_FAILED
;
943 if ((rp
& (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
944 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
948 /* Count the number of bytes available in the fifo without
949 * crossing the wrap around. Make sure to not fill it completely,
950 * because that would make wp == rp and that's the empty condition. */
951 uint32_t thisrun_bytes
;
953 thisrun_bytes
= rp
- wp
- block_size
;
954 else if (rp
> fifo_start_addr
)
955 thisrun_bytes
= fifo_end_addr
- wp
;
957 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
959 if (thisrun_bytes
== 0) {
960 /* Throttle polling a bit if transfer is (much) faster than flash
961 * programming. The exact delay shouldn't matter as long as it's
962 * less than buffer size / flash speed. This is very unlikely to
963 * run when using high latency connections such as USB. */
966 /* to stop an infinite loop on some targets check and increment a timeout
967 * this issue was observed on a stellaris using the new ICDI interface */
968 if (timeout
++ >= 500) {
969 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
970 return ERROR_FLASH_OPERATION_FAILED
;
975 /* reset our timeout */
978 /* Limit to the amount of data we actually want to write */
979 if (thisrun_bytes
> count
* block_size
)
980 thisrun_bytes
= count
* block_size
;
982 /* Write data to fifo */
983 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
984 if (retval
!= ERROR_OK
)
987 /* Update counters and wrap write pointer */
988 buffer
+= thisrun_bytes
;
989 count
-= thisrun_bytes
/ block_size
;
991 if (wp
>= fifo_end_addr
)
992 wp
= fifo_start_addr
;
994 /* Store updated write pointer to target */
995 retval
= target_write_u32(target
, wp_addr
, wp
);
996 if (retval
!= ERROR_OK
)
1000 if (retval
!= ERROR_OK
) {
1001 /* abort flash write algorithm on target */
1002 target_write_u32(target
, wp_addr
, 0);
1005 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1006 num_reg_params
, reg_params
,
1011 if (retval2
!= ERROR_OK
) {
1012 LOG_ERROR("error waiting for target flash write algorithm");
1019 int target_read_memory(struct target
*target
,
1020 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1022 if (!target_was_examined(target
)) {
1023 LOG_ERROR("Target not examined yet");
1026 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1029 int target_read_phys_memory(struct target
*target
,
1030 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1032 if (!target_was_examined(target
)) {
1033 LOG_ERROR("Target not examined yet");
1036 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1039 int target_write_memory(struct target
*target
,
1040 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1042 if (!target_was_examined(target
)) {
1043 LOG_ERROR("Target not examined yet");
1046 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1049 int target_write_phys_memory(struct target
*target
,
1050 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1052 if (!target_was_examined(target
)) {
1053 LOG_ERROR("Target not examined yet");
1056 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1059 int target_add_breakpoint(struct target
*target
,
1060 struct breakpoint
*breakpoint
)
1062 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1063 LOG_WARNING("target %s is not halted", target_name(target
));
1064 return ERROR_TARGET_NOT_HALTED
;
1066 return target
->type
->add_breakpoint(target
, breakpoint
);
1069 int target_add_context_breakpoint(struct target
*target
,
1070 struct breakpoint
*breakpoint
)
1072 if (target
->state
!= TARGET_HALTED
) {
1073 LOG_WARNING("target %s is not halted", target_name(target
));
1074 return ERROR_TARGET_NOT_HALTED
;
1076 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1079 int target_add_hybrid_breakpoint(struct target
*target
,
1080 struct breakpoint
*breakpoint
)
1082 if (target
->state
!= TARGET_HALTED
) {
1083 LOG_WARNING("target %s is not halted", target_name(target
));
1084 return ERROR_TARGET_NOT_HALTED
;
1086 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1089 int target_remove_breakpoint(struct target
*target
,
1090 struct breakpoint
*breakpoint
)
1092 return target
->type
->remove_breakpoint(target
, breakpoint
);
1095 int target_add_watchpoint(struct target
*target
,
1096 struct watchpoint
*watchpoint
)
1098 if (target
->state
!= TARGET_HALTED
) {
1099 LOG_WARNING("target %s is not halted", target_name(target
));
1100 return ERROR_TARGET_NOT_HALTED
;
1102 return target
->type
->add_watchpoint(target
, watchpoint
);
1104 int target_remove_watchpoint(struct target
*target
,
1105 struct watchpoint
*watchpoint
)
1107 return target
->type
->remove_watchpoint(target
, watchpoint
);
1109 int target_hit_watchpoint(struct target
*target
,
1110 struct watchpoint
**hit_watchpoint
)
1112 if (target
->state
!= TARGET_HALTED
) {
1113 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1114 return ERROR_TARGET_NOT_HALTED
;
1117 if (target
->type
->hit_watchpoint
== NULL
) {
1118 /* For backward compatible, if hit_watchpoint is not implemented,
1119 * return ERROR_FAIL such that gdb_server will not take the nonsense
1124 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1127 int target_get_gdb_reg_list(struct target
*target
,
1128 struct reg
**reg_list
[], int *reg_list_size
,
1129 enum target_register_class reg_class
)
1131 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1133 int target_step(struct target
*target
,
1134 int current
, uint32_t address
, int handle_breakpoints
)
1136 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1139 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1141 if (target
->state
!= TARGET_HALTED
) {
1142 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1143 return ERROR_TARGET_NOT_HALTED
;
1145 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1148 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1150 if (target
->state
!= TARGET_HALTED
) {
1151 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1152 return ERROR_TARGET_NOT_HALTED
;
1154 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1157 int target_profiling(struct target
*target
, uint32_t *samples
,
1158 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1160 if (target
->state
!= TARGET_HALTED
) {
1161 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1162 return ERROR_TARGET_NOT_HALTED
;
1164 return target
->type
->profiling(target
, samples
, max_num_samples
,
1165 num_samples
, seconds
);
1169 * Reset the @c examined flag for the given target.
1170 * Pure paranoia -- targets are zeroed on allocation.
1172 static void target_reset_examined(struct target
*target
)
1174 target
->examined
= false;
1177 static int err_read_phys_memory(struct target
*target
, uint32_t address
,
1178 uint32_t size
, uint32_t count
, uint8_t *buffer
)
1180 LOG_ERROR("Not implemented: %s", __func__
);
1184 static int err_write_phys_memory(struct target
*target
, uint32_t address
,
1185 uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1187 LOG_ERROR("Not implemented: %s", __func__
);
1191 static int handle_target(void *priv
);
1193 static int target_init_one(struct command_context
*cmd_ctx
,
1194 struct target
*target
)
1196 target_reset_examined(target
);
1198 struct target_type
*type
= target
->type
;
1199 if (type
->examine
== NULL
)
1200 type
->examine
= default_examine
;
1202 if (type
->check_reset
== NULL
)
1203 type
->check_reset
= default_check_reset
;
1205 assert(type
->init_target
!= NULL
);
1207 int retval
= type
->init_target(cmd_ctx
, target
);
1208 if (ERROR_OK
!= retval
) {
1209 LOG_ERROR("target '%s' init failed", target_name(target
));
1213 /* Sanity-check MMU support ... stub in what we must, to help
1214 * implement it in stages, but warn if we need to do so.
1217 if (type
->write_phys_memory
== NULL
) {
1218 LOG_ERROR("type '%s' is missing write_phys_memory",
1220 type
->write_phys_memory
= err_write_phys_memory
;
1222 if (type
->read_phys_memory
== NULL
) {
1223 LOG_ERROR("type '%s' is missing read_phys_memory",
1225 type
->read_phys_memory
= err_read_phys_memory
;
1227 if (type
->virt2phys
== NULL
) {
1228 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1229 type
->virt2phys
= identity_virt2phys
;
1232 /* Make sure no-MMU targets all behave the same: make no
1233 * distinction between physical and virtual addresses, and
1234 * ensure that virt2phys() is always an identity mapping.
1236 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1237 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1240 type
->write_phys_memory
= type
->write_memory
;
1241 type
->read_phys_memory
= type
->read_memory
;
1242 type
->virt2phys
= identity_virt2phys
;
1245 if (target
->type
->read_buffer
== NULL
)
1246 target
->type
->read_buffer
= target_read_buffer_default
;
1248 if (target
->type
->write_buffer
== NULL
)
1249 target
->type
->write_buffer
= target_write_buffer_default
;
1251 if (target
->type
->get_gdb_fileio_info
== NULL
)
1252 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1254 if (target
->type
->gdb_fileio_end
== NULL
)
1255 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1257 if (target
->type
->profiling
== NULL
)
1258 target
->type
->profiling
= target_profiling_default
;
1263 static int target_init(struct command_context
*cmd_ctx
)
1265 struct target
*target
;
1268 for (target
= all_targets
; target
; target
= target
->next
) {
1269 retval
= target_init_one(cmd_ctx
, target
);
1270 if (ERROR_OK
!= retval
)
1277 retval
= target_register_user_commands(cmd_ctx
);
1278 if (ERROR_OK
!= retval
)
1281 retval
= target_register_timer_callback(&handle_target
,
1282 polling_interval
, 1, cmd_ctx
->interp
);
1283 if (ERROR_OK
!= retval
)
1289 COMMAND_HANDLER(handle_target_init_command
)
1294 return ERROR_COMMAND_SYNTAX_ERROR
;
1296 static bool target_initialized
;
1297 if (target_initialized
) {
1298 LOG_INFO("'target init' has already been called");
1301 target_initialized
= true;
1303 retval
= command_run_line(CMD_CTX
, "init_targets");
1304 if (ERROR_OK
!= retval
)
1307 retval
= command_run_line(CMD_CTX
, "init_target_events");
1308 if (ERROR_OK
!= retval
)
1311 retval
= command_run_line(CMD_CTX
, "init_board");
1312 if (ERROR_OK
!= retval
)
1315 LOG_DEBUG("Initializing targets...");
1316 return target_init(CMD_CTX
);
1319 int target_register_event_callback(int (*callback
)(struct target
*target
,
1320 enum target_event event
, void *priv
), void *priv
)
1322 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1324 if (callback
== NULL
)
1325 return ERROR_COMMAND_SYNTAX_ERROR
;
1328 while ((*callbacks_p
)->next
)
1329 callbacks_p
= &((*callbacks_p
)->next
);
1330 callbacks_p
= &((*callbacks_p
)->next
);
1333 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1334 (*callbacks_p
)->callback
= callback
;
1335 (*callbacks_p
)->priv
= priv
;
1336 (*callbacks_p
)->next
= NULL
;
1341 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1342 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1344 struct target_reset_callback
*entry
;
1346 if (callback
== NULL
)
1347 return ERROR_COMMAND_SYNTAX_ERROR
;
1349 entry
= malloc(sizeof(struct target_reset_callback
));
1350 if (entry
== NULL
) {
1351 LOG_ERROR("error allocating buffer for reset callback entry");
1352 return ERROR_COMMAND_SYNTAX_ERROR
;
1355 entry
->callback
= callback
;
1357 list_add(&entry
->list
, &target_reset_callback_list
);
1363 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1365 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1368 if (callback
== NULL
)
1369 return ERROR_COMMAND_SYNTAX_ERROR
;
1372 while ((*callbacks_p
)->next
)
1373 callbacks_p
= &((*callbacks_p
)->next
);
1374 callbacks_p
= &((*callbacks_p
)->next
);
1377 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1378 (*callbacks_p
)->callback
= callback
;
1379 (*callbacks_p
)->periodic
= periodic
;
1380 (*callbacks_p
)->time_ms
= time_ms
;
1381 (*callbacks_p
)->removed
= false;
1383 gettimeofday(&now
, NULL
);
1384 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
1385 time_ms
-= (time_ms
% 1000);
1386 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
1387 if ((*callbacks_p
)->when
.tv_usec
> 1000000) {
1388 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
1389 (*callbacks_p
)->when
.tv_sec
+= 1;
1392 (*callbacks_p
)->priv
= priv
;
1393 (*callbacks_p
)->next
= NULL
;
1398 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1399 enum target_event event
, void *priv
), void *priv
)
1401 struct target_event_callback
**p
= &target_event_callbacks
;
1402 struct target_event_callback
*c
= target_event_callbacks
;
1404 if (callback
== NULL
)
1405 return ERROR_COMMAND_SYNTAX_ERROR
;
1408 struct target_event_callback
*next
= c
->next
;
1409 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1421 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1422 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1424 struct target_reset_callback
*entry
;
1426 if (callback
== NULL
)
1427 return ERROR_COMMAND_SYNTAX_ERROR
;
1429 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1430 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1431 list_del(&entry
->list
);
1440 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1442 if (callback
== NULL
)
1443 return ERROR_COMMAND_SYNTAX_ERROR
;
1445 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1447 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1456 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1458 struct target_event_callback
*callback
= target_event_callbacks
;
1459 struct target_event_callback
*next_callback
;
1461 if (event
== TARGET_EVENT_HALTED
) {
1462 /* execute early halted first */
1463 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1466 LOG_DEBUG("target event %i (%s)", event
,
1467 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1469 target_handle_event(target
, event
);
1472 next_callback
= callback
->next
;
1473 callback
->callback(target
, event
, callback
->priv
);
1474 callback
= next_callback
;
1480 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1482 struct target_reset_callback
*callback
;
1484 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1485 Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1487 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1488 callback
->callback(target
, reset_mode
, callback
->priv
);
1493 static int target_timer_callback_periodic_restart(
1494 struct target_timer_callback
*cb
, struct timeval
*now
)
1496 int time_ms
= cb
->time_ms
;
1497 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1498 time_ms
-= (time_ms
% 1000);
1499 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1500 if (cb
->when
.tv_usec
> 1000000) {
1501 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1502 cb
->when
.tv_sec
+= 1;
1507 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1508 struct timeval
*now
)
1510 cb
->callback(cb
->priv
);
1513 return target_timer_callback_periodic_restart(cb
, now
);
1515 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1518 static int target_call_timer_callbacks_check_time(int checktime
)
1520 static bool callback_processing
;
1522 /* Do not allow nesting */
1523 if (callback_processing
)
1526 callback_processing
= true;
1531 gettimeofday(&now
, NULL
);
1533 /* Store an address of the place containing a pointer to the
1534 * next item; initially, that's a standalone "root of the
1535 * list" variable. */
1536 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1538 if ((*callback
)->removed
) {
1539 struct target_timer_callback
*p
= *callback
;
1540 *callback
= (*callback
)->next
;
1545 bool call_it
= (*callback
)->callback
&&
1546 ((!checktime
&& (*callback
)->periodic
) ||
1547 now
.tv_sec
> (*callback
)->when
.tv_sec
||
1548 (now
.tv_sec
== (*callback
)->when
.tv_sec
&&
1549 now
.tv_usec
>= (*callback
)->when
.tv_usec
));
1552 target_call_timer_callback(*callback
, &now
);
1554 callback
= &(*callback
)->next
;
1557 callback_processing
= false;
1561 int target_call_timer_callbacks(void)
1563 return target_call_timer_callbacks_check_time(1);
1566 /* invoke periodic callbacks immediately */
1567 int target_call_timer_callbacks_now(void)
1569 return target_call_timer_callbacks_check_time(0);
1572 /* Prints the working area layout for debug purposes */
1573 static void print_wa_layout(struct target
*target
)
1575 struct working_area
*c
= target
->working_areas
;
1578 LOG_DEBUG("%c%c 0x%08"PRIx32
"-0x%08"PRIx32
" (%"PRIu32
" bytes)",
1579 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1580 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1585 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1586 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1588 assert(area
->free
); /* Shouldn't split an allocated area */
1589 assert(size
<= area
->size
); /* Caller should guarantee this */
1591 /* Split only if not already the right size */
1592 if (size
< area
->size
) {
1593 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1598 new_wa
->next
= area
->next
;
1599 new_wa
->size
= area
->size
- size
;
1600 new_wa
->address
= area
->address
+ size
;
1601 new_wa
->backup
= NULL
;
1602 new_wa
->user
= NULL
;
1603 new_wa
->free
= true;
1605 area
->next
= new_wa
;
1608 /* If backup memory was allocated to this area, it has the wrong size
1609 * now so free it and it will be reallocated if/when needed */
1612 area
->backup
= NULL
;
1617 /* Merge all adjacent free areas into one */
1618 static void target_merge_working_areas(struct target
*target
)
1620 struct working_area
*c
= target
->working_areas
;
1622 while (c
&& c
->next
) {
1623 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1625 /* Find two adjacent free areas */
1626 if (c
->free
&& c
->next
->free
) {
1627 /* Merge the last into the first */
1628 c
->size
+= c
->next
->size
;
1630 /* Remove the last */
1631 struct working_area
*to_be_freed
= c
->next
;
1632 c
->next
= c
->next
->next
;
1633 if (to_be_freed
->backup
)
1634 free(to_be_freed
->backup
);
1637 /* If backup memory was allocated to the remaining area, it's has
1638 * the wrong size now */
1649 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1651 /* Reevaluate working area address based on MMU state*/
1652 if (target
->working_areas
== NULL
) {
1656 retval
= target
->type
->mmu(target
, &enabled
);
1657 if (retval
!= ERROR_OK
)
1661 if (target
->working_area_phys_spec
) {
1662 LOG_DEBUG("MMU disabled, using physical "
1663 "address for working memory 0x%08"PRIx32
,
1664 target
->working_area_phys
);
1665 target
->working_area
= target
->working_area_phys
;
1667 LOG_ERROR("No working memory available. "
1668 "Specify -work-area-phys to target.");
1669 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1672 if (target
->working_area_virt_spec
) {
1673 LOG_DEBUG("MMU enabled, using virtual "
1674 "address for working memory 0x%08"PRIx32
,
1675 target
->working_area_virt
);
1676 target
->working_area
= target
->working_area_virt
;
1678 LOG_ERROR("No working memory available. "
1679 "Specify -work-area-virt to target.");
1680 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1684 /* Set up initial working area on first call */
1685 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1687 new_wa
->next
= NULL
;
1688 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1689 new_wa
->address
= target
->working_area
;
1690 new_wa
->backup
= NULL
;
1691 new_wa
->user
= NULL
;
1692 new_wa
->free
= true;
1695 target
->working_areas
= new_wa
;
1698 /* only allocate multiples of 4 byte */
1700 size
= (size
+ 3) & (~3UL);
1702 struct working_area
*c
= target
->working_areas
;
1704 /* Find the first large enough working area */
1706 if (c
->free
&& c
->size
>= size
)
1712 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1714 /* Split the working area into the requested size */
1715 target_split_working_area(c
, size
);
1717 LOG_DEBUG("allocated new working area of %"PRIu32
" bytes at address 0x%08"PRIx32
, size
, c
->address
);
1719 if (target
->backup_working_area
) {
1720 if (c
->backup
== NULL
) {
1721 c
->backup
= malloc(c
->size
);
1722 if (c
->backup
== NULL
)
1726 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1727 if (retval
!= ERROR_OK
)
1731 /* mark as used, and return the new (reused) area */
1738 print_wa_layout(target
);
1743 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1747 retval
= target_alloc_working_area_try(target
, size
, area
);
1748 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1749 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1754 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1756 int retval
= ERROR_OK
;
1758 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1759 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1760 if (retval
!= ERROR_OK
)
1761 LOG_ERROR("failed to restore %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1762 area
->size
, area
->address
);
1768 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1769 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1771 int retval
= ERROR_OK
;
1777 retval
= target_restore_working_area(target
, area
);
1778 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1779 if (retval
!= ERROR_OK
)
1785 LOG_DEBUG("freed %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1786 area
->size
, area
->address
);
1788 /* mark user pointer invalid */
1789 /* TODO: Is this really safe? It points to some previous caller's memory.
1790 * How could we know that the area pointer is still in that place and not
1791 * some other vital data? What's the purpose of this, anyway? */
1795 target_merge_working_areas(target
);
1797 print_wa_layout(target
);
1802 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1804 return target_free_working_area_restore(target
, area
, 1);
1807 /* free resources and restore memory, if restoring memory fails,
1808 * free up resources anyway
1810 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1812 struct working_area
*c
= target
->working_areas
;
1814 LOG_DEBUG("freeing all working areas");
1816 /* Loop through all areas, restoring the allocated ones and marking them as free */
1820 target_restore_working_area(target
, c
);
1822 *c
->user
= NULL
; /* Same as above */
1828 /* Run a merge pass to combine all areas into one */
1829 target_merge_working_areas(target
);
1831 print_wa_layout(target
);
1834 void target_free_all_working_areas(struct target
*target
)
1836 target_free_all_working_areas_restore(target
, 1);
1839 /* Find the largest number of bytes that can be allocated */
1840 uint32_t target_get_working_area_avail(struct target
*target
)
1842 struct working_area
*c
= target
->working_areas
;
1843 uint32_t max_size
= 0;
1846 return target
->working_area_size
;
1849 if (c
->free
&& max_size
< c
->size
)
1858 int target_arch_state(struct target
*target
)
1861 if (target
== NULL
) {
1862 LOG_USER("No target has been configured");
1866 LOG_USER("target state: %s", target_state_name(target
));
1868 if (target
->state
!= TARGET_HALTED
)
1871 retval
= target
->type
->arch_state(target
);
1875 static int target_get_gdb_fileio_info_default(struct target
*target
,
1876 struct gdb_fileio_info
*fileio_info
)
1878 /* If target does not support semi-hosting function, target
1879 has no need to provide .get_gdb_fileio_info callback.
1880 It just return ERROR_FAIL and gdb_server will return "Txx"
1881 as target halted every time. */
1885 static int target_gdb_fileio_end_default(struct target
*target
,
1886 int retcode
, int fileio_errno
, bool ctrl_c
)
1891 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
1892 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1894 struct timeval timeout
, now
;
1896 gettimeofday(&timeout
, NULL
);
1897 timeval_add_time(&timeout
, seconds
, 0);
1899 LOG_INFO("Starting profiling. Halting and resuming the"
1900 " target as often as we can...");
1902 uint32_t sample_count
= 0;
1903 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
1904 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
1906 int retval
= ERROR_OK
;
1908 target_poll(target
);
1909 if (target
->state
== TARGET_HALTED
) {
1910 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
1911 samples
[sample_count
++] = t
;
1912 /* current pc, addr = 0, do not handle breakpoints, not debugging */
1913 retval
= target_resume(target
, 1, 0, 0, 0);
1914 target_poll(target
);
1915 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
1916 } else if (target
->state
== TARGET_RUNNING
) {
1917 /* We want to quickly sample the PC. */
1918 retval
= target_halt(target
);
1920 LOG_INFO("Target not halted or running");
1925 if (retval
!= ERROR_OK
)
1928 gettimeofday(&now
, NULL
);
1929 if ((sample_count
>= max_num_samples
) ||
1930 ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
))) {
1931 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
1936 *num_samples
= sample_count
;
1940 /* Single aligned words are guaranteed to use 16 or 32 bit access
1941 * mode respectively, otherwise data is handled as quickly as
1944 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
1946 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1947 (int)size
, (unsigned)address
);
1949 if (!target_was_examined(target
)) {
1950 LOG_ERROR("Target not examined yet");
1957 if ((address
+ size
- 1) < address
) {
1958 /* GDB can request this when e.g. PC is 0xfffffffc*/
1959 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1965 return target
->type
->write_buffer(target
, address
, size
, buffer
);
1968 static int target_write_buffer_default(struct target
*target
, uint32_t address
, uint32_t count
, const uint8_t *buffer
)
1972 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
1973 * will have something to do with the size we leave to it. */
1974 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
1975 if (address
& size
) {
1976 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
1977 if (retval
!= ERROR_OK
)
1985 /* Write the data with as large access size as possible. */
1986 for (; size
> 0; size
/= 2) {
1987 uint32_t aligned
= count
- count
% size
;
1989 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
1990 if (retval
!= ERROR_OK
)
2001 /* Single aligned words are guaranteed to use 16 or 32 bit access
2002 * mode respectively, otherwise data is handled as quickly as
2005 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
2007 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
2008 (int)size
, (unsigned)address
);
2010 if (!target_was_examined(target
)) {
2011 LOG_ERROR("Target not examined yet");
2018 if ((address
+ size
- 1) < address
) {
2019 /* GDB can request this when e.g. PC is 0xfffffffc*/
2020 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
2026 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2029 static int target_read_buffer_default(struct target
*target
, uint32_t address
, uint32_t count
, uint8_t *buffer
)
2033 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2034 * will have something to do with the size we leave to it. */
2035 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2036 if (address
& size
) {
2037 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2038 if (retval
!= ERROR_OK
)
2046 /* Read the data with as large access size as possible. */
2047 for (; size
> 0; size
/= 2) {
2048 uint32_t aligned
= count
- count
% size
;
2050 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2051 if (retval
!= ERROR_OK
)
2062 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
2067 uint32_t checksum
= 0;
2068 if (!target_was_examined(target
)) {
2069 LOG_ERROR("Target not examined yet");
2073 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2074 if (retval
!= ERROR_OK
) {
2075 buffer
= malloc(size
);
2076 if (buffer
== NULL
) {
2077 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
2078 return ERROR_COMMAND_SYNTAX_ERROR
;
2080 retval
= target_read_buffer(target
, address
, size
, buffer
);
2081 if (retval
!= ERROR_OK
) {
2086 /* convert to target endianness */
2087 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2088 uint32_t target_data
;
2089 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2090 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2093 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2102 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
2105 if (!target_was_examined(target
)) {
2106 LOG_ERROR("Target not examined yet");
2110 if (target
->type
->blank_check_memory
== 0)
2111 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2113 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
2118 int target_read_u64(struct target
*target
, uint64_t address
, uint64_t *value
)
2120 uint8_t value_buf
[8];
2121 if (!target_was_examined(target
)) {
2122 LOG_ERROR("Target not examined yet");
2126 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2128 if (retval
== ERROR_OK
) {
2129 *value
= target_buffer_get_u64(target
, value_buf
);
2130 LOG_DEBUG("address: 0x%" PRIx64
", value: 0x%16.16" PRIx64
"",
2135 LOG_DEBUG("address: 0x%" PRIx64
" failed",
2142 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
2144 uint8_t value_buf
[4];
2145 if (!target_was_examined(target
)) {
2146 LOG_ERROR("Target not examined yet");
2150 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2152 if (retval
== ERROR_OK
) {
2153 *value
= target_buffer_get_u32(target
, value_buf
);
2154 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
2159 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2166 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
2168 uint8_t value_buf
[2];
2169 if (!target_was_examined(target
)) {
2170 LOG_ERROR("Target not examined yet");
2174 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2176 if (retval
== ERROR_OK
) {
2177 *value
= target_buffer_get_u16(target
, value_buf
);
2178 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
2183 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2190 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
2192 if (!target_was_examined(target
)) {
2193 LOG_ERROR("Target not examined yet");
2197 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2199 if (retval
== ERROR_OK
) {
2200 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
2205 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2212 int target_write_u64(struct target
*target
, uint64_t address
, uint64_t value
)
2215 uint8_t value_buf
[8];
2216 if (!target_was_examined(target
)) {
2217 LOG_ERROR("Target not examined yet");
2221 LOG_DEBUG("address: 0x%" PRIx64
", value: 0x%16.16" PRIx64
"",
2225 target_buffer_set_u64(target
, value_buf
, value
);
2226 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2227 if (retval
!= ERROR_OK
)
2228 LOG_DEBUG("failed: %i", retval
);
2233 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
2236 uint8_t value_buf
[4];
2237 if (!target_was_examined(target
)) {
2238 LOG_ERROR("Target not examined yet");
2242 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
2246 target_buffer_set_u32(target
, value_buf
, value
);
2247 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2248 if (retval
!= ERROR_OK
)
2249 LOG_DEBUG("failed: %i", retval
);
2254 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
2257 uint8_t value_buf
[2];
2258 if (!target_was_examined(target
)) {
2259 LOG_ERROR("Target not examined yet");
2263 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
2267 target_buffer_set_u16(target
, value_buf
, value
);
2268 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2269 if (retval
!= ERROR_OK
)
2270 LOG_DEBUG("failed: %i", retval
);
2275 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
2278 if (!target_was_examined(target
)) {
2279 LOG_ERROR("Target not examined yet");
2283 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
2286 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2287 if (retval
!= ERROR_OK
)
2288 LOG_DEBUG("failed: %i", retval
);
2293 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2295 struct target
*target
= get_target(name
);
2296 if (target
== NULL
) {
2297 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2300 if (!target
->tap
->enabled
) {
2301 LOG_USER("Target: TAP %s is disabled, "
2302 "can't be the current target\n",
2303 target
->tap
->dotted_name
);
2307 cmd_ctx
->current_target
= target
->target_number
;
2312 COMMAND_HANDLER(handle_targets_command
)
2314 int retval
= ERROR_OK
;
2315 if (CMD_ARGC
== 1) {
2316 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2317 if (retval
== ERROR_OK
) {
2323 struct target
*target
= all_targets
;
2324 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2325 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2330 if (target
->tap
->enabled
)
2331 state
= target_state_name(target
);
2333 state
= "tap-disabled";
2335 if (CMD_CTX
->current_target
== target
->target_number
)
2338 /* keep columns lined up to match the headers above */
2339 command_print(CMD_CTX
,
2340 "%2d%c %-18s %-10s %-6s %-18s %s",
2341 target
->target_number
,
2343 target_name(target
),
2344 target_type_name(target
),
2345 Jim_Nvp_value2name_simple(nvp_target_endian
,
2346 target
->endianness
)->name
,
2347 target
->tap
->dotted_name
,
2349 target
= target
->next
;
2355 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2357 static int powerDropout
;
2358 static int srstAsserted
;
2360 static int runPowerRestore
;
2361 static int runPowerDropout
;
2362 static int runSrstAsserted
;
2363 static int runSrstDeasserted
;
2365 static int sense_handler(void)
2367 static int prevSrstAsserted
;
2368 static int prevPowerdropout
;
2370 int retval
= jtag_power_dropout(&powerDropout
);
2371 if (retval
!= ERROR_OK
)
2375 powerRestored
= prevPowerdropout
&& !powerDropout
;
2377 runPowerRestore
= 1;
2379 long long current
= timeval_ms();
2380 static long long lastPower
;
2381 int waitMore
= lastPower
+ 2000 > current
;
2382 if (powerDropout
&& !waitMore
) {
2383 runPowerDropout
= 1;
2384 lastPower
= current
;
2387 retval
= jtag_srst_asserted(&srstAsserted
);
2388 if (retval
!= ERROR_OK
)
2392 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2394 static long long lastSrst
;
2395 waitMore
= lastSrst
+ 2000 > current
;
2396 if (srstDeasserted
&& !waitMore
) {
2397 runSrstDeasserted
= 1;
2401 if (!prevSrstAsserted
&& srstAsserted
)
2402 runSrstAsserted
= 1;
2404 prevSrstAsserted
= srstAsserted
;
2405 prevPowerdropout
= powerDropout
;
2407 if (srstDeasserted
|| powerRestored
) {
2408 /* Other than logging the event we can't do anything here.
2409 * Issuing a reset is a particularly bad idea as we might
2410 * be inside a reset already.
2417 /* process target state changes */
2418 static int handle_target(void *priv
)
2420 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2421 int retval
= ERROR_OK
;
2423 if (!is_jtag_poll_safe()) {
2424 /* polling is disabled currently */
2428 /* we do not want to recurse here... */
2429 static int recursive
;
2433 /* danger! running these procedures can trigger srst assertions and power dropouts.
2434 * We need to avoid an infinite loop/recursion here and we do that by
2435 * clearing the flags after running these events.
2437 int did_something
= 0;
2438 if (runSrstAsserted
) {
2439 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2440 Jim_Eval(interp
, "srst_asserted");
2443 if (runSrstDeasserted
) {
2444 Jim_Eval(interp
, "srst_deasserted");
2447 if (runPowerDropout
) {
2448 LOG_INFO("Power dropout detected, running power_dropout proc.");
2449 Jim_Eval(interp
, "power_dropout");
2452 if (runPowerRestore
) {
2453 Jim_Eval(interp
, "power_restore");
2457 if (did_something
) {
2458 /* clear detect flags */
2462 /* clear action flags */
2464 runSrstAsserted
= 0;
2465 runSrstDeasserted
= 0;
2466 runPowerRestore
= 0;
2467 runPowerDropout
= 0;
2472 /* Poll targets for state changes unless that's globally disabled.
2473 * Skip targets that are currently disabled.
2475 for (struct target
*target
= all_targets
;
2476 is_jtag_poll_safe() && target
;
2477 target
= target
->next
) {
2479 if (!target_was_examined(target
))
2482 if (!target
->tap
->enabled
)
2485 if (target
->backoff
.times
> target
->backoff
.count
) {
2486 /* do not poll this time as we failed previously */
2487 target
->backoff
.count
++;
2490 target
->backoff
.count
= 0;
2492 /* only poll target if we've got power and srst isn't asserted */
2493 if (!powerDropout
&& !srstAsserted
) {
2494 /* polling may fail silently until the target has been examined */
2495 retval
= target_poll(target
);
2496 if (retval
!= ERROR_OK
) {
2497 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2498 if (target
->backoff
.times
* polling_interval
< 5000) {
2499 target
->backoff
.times
*= 2;
2500 target
->backoff
.times
++;
2502 LOG_USER("Polling target %s failed, GDB will be halted. Polling again in %dms",
2503 target_name(target
),
2504 target
->backoff
.times
* polling_interval
);
2506 /* Tell GDB to halt the debugger. This allows the user to
2507 * run monitor commands to handle the situation.
2509 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2512 /* Since we succeeded, we reset backoff count */
2513 if (target
->backoff
.times
> 0) {
2514 LOG_USER("Polling target %s succeeded again, trying to reexamine", target_name(target
));
2515 target_reset_examined(target
);
2516 retval
= target_examine_one(target
);
2517 /* Target examination could have failed due to unstable connection,
2518 * but we set the examined flag anyway to repoll it later */
2519 if (retval
!= ERROR_OK
) {
2520 target
->examined
= true;
2525 target
->backoff
.times
= 0;
2532 COMMAND_HANDLER(handle_reg_command
)
2534 struct target
*target
;
2535 struct reg
*reg
= NULL
;
2541 target
= get_current_target(CMD_CTX
);
2543 /* list all available registers for the current target */
2544 if (CMD_ARGC
== 0) {
2545 struct reg_cache
*cache
= target
->reg_cache
;
2551 command_print(CMD_CTX
, "===== %s", cache
->name
);
2553 for (i
= 0, reg
= cache
->reg_list
;
2554 i
< cache
->num_regs
;
2555 i
++, reg
++, count
++) {
2556 /* only print cached values if they are valid */
2558 value
= buf_to_str(reg
->value
,
2560 command_print(CMD_CTX
,
2561 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2569 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2574 cache
= cache
->next
;
2580 /* access a single register by its ordinal number */
2581 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2583 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2585 struct reg_cache
*cache
= target
->reg_cache
;
2589 for (i
= 0; i
< cache
->num_regs
; i
++) {
2590 if (count
++ == num
) {
2591 reg
= &cache
->reg_list
[i
];
2597 cache
= cache
->next
;
2601 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2602 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2606 /* access a single register by its name */
2607 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2610 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2615 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2617 /* display a register */
2618 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2619 && (CMD_ARGV
[1][0] <= '9')))) {
2620 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2623 if (reg
->valid
== 0)
2624 reg
->type
->get(reg
);
2625 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2626 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2631 /* set register value */
2632 if (CMD_ARGC
== 2) {
2633 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2636 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2638 reg
->type
->set(reg
, buf
);
2640 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2641 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2649 return ERROR_COMMAND_SYNTAX_ERROR
;
2652 COMMAND_HANDLER(handle_poll_command
)
2654 int retval
= ERROR_OK
;
2655 struct target
*target
= get_current_target(CMD_CTX
);
2657 if (CMD_ARGC
== 0) {
2658 command_print(CMD_CTX
, "background polling: %s",
2659 jtag_poll_get_enabled() ? "on" : "off");
2660 command_print(CMD_CTX
, "TAP: %s (%s)",
2661 target
->tap
->dotted_name
,
2662 target
->tap
->enabled
? "enabled" : "disabled");
2663 if (!target
->tap
->enabled
)
2665 retval
= target_poll(target
);
2666 if (retval
!= ERROR_OK
)
2668 retval
= target_arch_state(target
);
2669 if (retval
!= ERROR_OK
)
2671 } else if (CMD_ARGC
== 1) {
2673 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2674 jtag_poll_set_enabled(enable
);
2676 return ERROR_COMMAND_SYNTAX_ERROR
;
2681 COMMAND_HANDLER(handle_wait_halt_command
)
2684 return ERROR_COMMAND_SYNTAX_ERROR
;
2686 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2687 if (1 == CMD_ARGC
) {
2688 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2689 if (ERROR_OK
!= retval
)
2690 return ERROR_COMMAND_SYNTAX_ERROR
;
2693 struct target
*target
= get_current_target(CMD_CTX
);
2694 return target_wait_state(target
, TARGET_HALTED
, ms
);
2697 /* wait for target state to change. The trick here is to have a low
2698 * latency for short waits and not to suck up all the CPU time
2701 * After 500ms, keep_alive() is invoked
2703 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2706 long long then
= 0, cur
;
2710 retval
= target_poll(target
);
2711 if (retval
!= ERROR_OK
)
2713 if (target
->state
== state
)
2718 then
= timeval_ms();
2719 LOG_DEBUG("waiting for target %s...",
2720 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2726 if ((cur
-then
) > ms
) {
2727 LOG_ERROR("timed out while waiting for target %s",
2728 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2736 COMMAND_HANDLER(handle_halt_command
)
2740 struct target
*target
= get_current_target(CMD_CTX
);
2741 int retval
= target_halt(target
);
2742 if (ERROR_OK
!= retval
)
2745 if (CMD_ARGC
== 1) {
2746 unsigned wait_local
;
2747 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2748 if (ERROR_OK
!= retval
)
2749 return ERROR_COMMAND_SYNTAX_ERROR
;
2754 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2757 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2759 struct target
*target
= get_current_target(CMD_CTX
);
2761 LOG_USER("requesting target halt and executing a soft reset");
2763 target_soft_reset_halt(target
);
2768 COMMAND_HANDLER(handle_reset_command
)
2771 return ERROR_COMMAND_SYNTAX_ERROR
;
2773 enum target_reset_mode reset_mode
= RESET_RUN
;
2774 if (CMD_ARGC
== 1) {
2776 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2777 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
2778 return ERROR_COMMAND_SYNTAX_ERROR
;
2779 reset_mode
= n
->value
;
2782 /* reset *all* targets */
2783 return target_process_reset(CMD_CTX
, reset_mode
);
2787 COMMAND_HANDLER(handle_resume_command
)
2791 return ERROR_COMMAND_SYNTAX_ERROR
;
2793 struct target
*target
= get_current_target(CMD_CTX
);
2795 /* with no CMD_ARGV, resume from current pc, addr = 0,
2796 * with one arguments, addr = CMD_ARGV[0],
2797 * handle breakpoints, not debugging */
2799 if (CMD_ARGC
== 1) {
2800 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2804 return target_resume(target
, current
, addr
, 1, 0);
2807 COMMAND_HANDLER(handle_step_command
)
2810 return ERROR_COMMAND_SYNTAX_ERROR
;
2814 /* with no CMD_ARGV, step from current pc, addr = 0,
2815 * with one argument addr = CMD_ARGV[0],
2816 * handle breakpoints, debugging */
2819 if (CMD_ARGC
== 1) {
2820 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2824 struct target
*target
= get_current_target(CMD_CTX
);
2826 return target
->type
->step(target
, current_pc
, addr
, 1);
2829 static void handle_md_output(struct command_context
*cmd_ctx
,
2830 struct target
*target
, uint32_t address
, unsigned size
,
2831 unsigned count
, const uint8_t *buffer
)
2833 const unsigned line_bytecnt
= 32;
2834 unsigned line_modulo
= line_bytecnt
/ size
;
2836 char output
[line_bytecnt
* 4 + 1];
2837 unsigned output_len
= 0;
2839 const char *value_fmt
;
2842 value_fmt
= "%8.8x ";
2845 value_fmt
= "%4.4x ";
2848 value_fmt
= "%2.2x ";
2851 /* "can't happen", caller checked */
2852 LOG_ERROR("invalid memory read size: %u", size
);
2856 for (unsigned i
= 0; i
< count
; i
++) {
2857 if (i
% line_modulo
== 0) {
2858 output_len
+= snprintf(output
+ output_len
,
2859 sizeof(output
) - output_len
,
2861 (unsigned)(address
+ (i
*size
)));
2865 const uint8_t *value_ptr
= buffer
+ i
* size
;
2868 value
= target_buffer_get_u32(target
, value_ptr
);
2871 value
= target_buffer_get_u16(target
, value_ptr
);
2876 output_len
+= snprintf(output
+ output_len
,
2877 sizeof(output
) - output_len
,
2880 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
2881 command_print(cmd_ctx
, "%s", output
);
2887 COMMAND_HANDLER(handle_md_command
)
2890 return ERROR_COMMAND_SYNTAX_ERROR
;
2893 switch (CMD_NAME
[2]) {
2904 return ERROR_COMMAND_SYNTAX_ERROR
;
2907 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2908 int (*fn
)(struct target
*target
,
2909 uint32_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
2913 fn
= target_read_phys_memory
;
2915 fn
= target_read_memory
;
2916 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2917 return ERROR_COMMAND_SYNTAX_ERROR
;
2920 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2924 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2926 uint8_t *buffer
= calloc(count
, size
);
2928 struct target
*target
= get_current_target(CMD_CTX
);
2929 int retval
= fn(target
, address
, size
, count
, buffer
);
2930 if (ERROR_OK
== retval
)
2931 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2938 typedef int (*target_write_fn
)(struct target
*target
,
2939 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
2941 static int target_fill_mem(struct target
*target
,
2950 /* We have to write in reasonably large chunks to be able
2951 * to fill large memory areas with any sane speed */
2952 const unsigned chunk_size
= 16384;
2953 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
2954 if (target_buf
== NULL
) {
2955 LOG_ERROR("Out of memory");
2959 for (unsigned i
= 0; i
< chunk_size
; i
++) {
2960 switch (data_size
) {
2962 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
2965 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
2968 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
2975 int retval
= ERROR_OK
;
2977 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
2980 if (current
> chunk_size
)
2981 current
= chunk_size
;
2982 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
2983 if (retval
!= ERROR_OK
)
2985 /* avoid GDB timeouts */
2994 COMMAND_HANDLER(handle_mw_command
)
2997 return ERROR_COMMAND_SYNTAX_ERROR
;
2998 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3003 fn
= target_write_phys_memory
;
3005 fn
= target_write_memory
;
3006 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
3007 return ERROR_COMMAND_SYNTAX_ERROR
;
3010 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
3013 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
3017 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
3019 struct target
*target
= get_current_target(CMD_CTX
);
3021 switch (CMD_NAME
[2]) {
3032 return ERROR_COMMAND_SYNTAX_ERROR
;
3035 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
3038 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
3039 uint32_t *min_address
, uint32_t *max_address
)
3041 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
3042 return ERROR_COMMAND_SYNTAX_ERROR
;
3044 /* a base address isn't always necessary,
3045 * default to 0x0 (i.e. don't relocate) */
3046 if (CMD_ARGC
>= 2) {
3048 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
3049 image
->base_address
= addr
;
3050 image
->base_address_set
= 1;
3052 image
->base_address_set
= 0;
3054 image
->start_address_set
= 0;
3057 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
3058 if (CMD_ARGC
== 5) {
3059 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
3060 /* use size (given) to find max (required) */
3061 *max_address
+= *min_address
;
3064 if (*min_address
> *max_address
)
3065 return ERROR_COMMAND_SYNTAX_ERROR
;
3070 COMMAND_HANDLER(handle_load_image_command
)
3074 uint32_t image_size
;
3075 uint32_t min_address
= 0;
3076 uint32_t max_address
= 0xffffffff;
3080 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
3081 &image
, &min_address
, &max_address
);
3082 if (ERROR_OK
!= retval
)
3085 struct target
*target
= get_current_target(CMD_CTX
);
3087 struct duration bench
;
3088 duration_start(&bench
);
3090 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
3095 for (i
= 0; i
< image
.num_sections
; i
++) {
3096 buffer
= malloc(image
.sections
[i
].size
);
3097 if (buffer
== NULL
) {
3098 command_print(CMD_CTX
,
3099 "error allocating buffer for section (%d bytes)",
3100 (int)(image
.sections
[i
].size
));
3104 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3105 if (retval
!= ERROR_OK
) {
3110 uint32_t offset
= 0;
3111 uint32_t length
= buf_cnt
;
3113 /* DANGER!!! beware of unsigned comparision here!!! */
3115 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3116 (image
.sections
[i
].base_address
< max_address
)) {
3118 if (image
.sections
[i
].base_address
< min_address
) {
3119 /* clip addresses below */
3120 offset
+= min_address
-image
.sections
[i
].base_address
;
3124 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3125 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3127 retval
= target_write_buffer(target
,
3128 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3129 if (retval
!= ERROR_OK
) {
3133 image_size
+= length
;
3134 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
3135 (unsigned int)length
,
3136 image
.sections
[i
].base_address
+ offset
);
3142 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3143 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
3144 "in %fs (%0.3f KiB/s)", image_size
,
3145 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3148 image_close(&image
);
3154 COMMAND_HANDLER(handle_dump_image_command
)
3156 struct fileio fileio
;
3158 int retval
, retvaltemp
;
3159 uint32_t address
, size
;
3160 struct duration bench
;
3161 struct target
*target
= get_current_target(CMD_CTX
);
3164 return ERROR_COMMAND_SYNTAX_ERROR
;
3166 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
3167 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
3169 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
3170 buffer
= malloc(buf_size
);
3174 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3175 if (retval
!= ERROR_OK
) {