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"
59 /* default halt wait timeout (ms) */
60 #define DEFAULT_HALT_TIMEOUT 5000
62 static int target_read_buffer_default(struct target
*target
, uint32_t address
,
63 uint32_t count
, uint8_t *buffer
);
64 static int target_write_buffer_default(struct target
*target
, uint32_t address
,
65 uint32_t count
, const uint8_t *buffer
);
66 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
67 int argc
, Jim_Obj
* const *argv
);
68 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
69 int argc
, Jim_Obj
* const *argv
);
70 static int target_register_user_commands(struct command_context
*cmd_ctx
);
71 static int target_get_gdb_fileio_info_default(struct target
*target
,
72 struct gdb_fileio_info
*fileio_info
);
73 static int target_gdb_fileio_end_default(struct target
*target
, int retcode
,
74 int fileio_errno
, bool ctrl_c
);
75 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
76 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
);
79 extern struct target_type arm7tdmi_target
;
80 extern struct target_type arm720t_target
;
81 extern struct target_type arm9tdmi_target
;
82 extern struct target_type arm920t_target
;
83 extern struct target_type arm966e_target
;
84 extern struct target_type arm946e_target
;
85 extern struct target_type arm926ejs_target
;
86 extern struct target_type fa526_target
;
87 extern struct target_type feroceon_target
;
88 extern struct target_type dragonite_target
;
89 extern struct target_type xscale_target
;
90 extern struct target_type cortexm3_target
;
91 extern struct target_type cortexa8_target
;
92 extern struct target_type cortexr4_target
;
93 extern struct target_type arm11_target
;
94 extern struct target_type mips_m4k_target
;
95 extern struct target_type avr_target
;
96 extern struct target_type dsp563xx_target
;
97 extern struct target_type dsp5680xx_target
;
98 extern struct target_type testee_target
;
99 extern struct target_type avr32_ap7k_target
;
100 extern struct target_type hla_target
;
101 extern struct target_type nds32_v2_target
;
102 extern struct target_type nds32_v3_target
;
103 extern struct target_type nds32_v3m_target
;
105 static struct target_type
*target_types
[] = {
134 struct target
*all_targets
;
135 static struct target_event_callback
*target_event_callbacks
;
136 static struct target_timer_callback
*target_timer_callbacks
;
137 static const int polling_interval
= 100;
139 static const Jim_Nvp nvp_assert
[] = {
140 { .name
= "assert", NVP_ASSERT
},
141 { .name
= "deassert", NVP_DEASSERT
},
142 { .name
= "T", NVP_ASSERT
},
143 { .name
= "F", NVP_DEASSERT
},
144 { .name
= "t", NVP_ASSERT
},
145 { .name
= "f", NVP_DEASSERT
},
146 { .name
= NULL
, .value
= -1 }
149 static const Jim_Nvp nvp_error_target
[] = {
150 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
151 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
152 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
153 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
154 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
155 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
156 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
157 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
158 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
159 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
160 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
161 { .value
= -1, .name
= NULL
}
164 static const char *target_strerror_safe(int err
)
168 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
175 static const Jim_Nvp nvp_target_event
[] = {
177 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
178 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
179 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
180 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
181 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
183 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
184 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
186 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
187 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
188 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
189 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
190 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
191 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
192 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
193 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
194 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
195 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
196 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
197 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
199 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
200 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
202 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
203 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
205 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
206 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
208 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
209 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
211 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
212 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
214 { .name
= NULL
, .value
= -1 }
217 static const Jim_Nvp nvp_target_state
[] = {
218 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
219 { .name
= "running", .value
= TARGET_RUNNING
},
220 { .name
= "halted", .value
= TARGET_HALTED
},
221 { .name
= "reset", .value
= TARGET_RESET
},
222 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
223 { .name
= NULL
, .value
= -1 },
226 static const Jim_Nvp nvp_target_debug_reason
[] = {
227 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
228 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
229 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
230 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
231 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
232 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
233 { .name
= "program-exit" , .value
= DBG_REASON_EXIT
},
234 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
235 { .name
= NULL
, .value
= -1 },
238 static const Jim_Nvp nvp_target_endian
[] = {
239 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
240 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
241 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
242 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
243 { .name
= NULL
, .value
= -1 },
246 static const Jim_Nvp nvp_reset_modes
[] = {
247 { .name
= "unknown", .value
= RESET_UNKNOWN
},
248 { .name
= "run" , .value
= RESET_RUN
},
249 { .name
= "halt" , .value
= RESET_HALT
},
250 { .name
= "init" , .value
= RESET_INIT
},
251 { .name
= NULL
, .value
= -1 },
254 const char *debug_reason_name(struct target
*t
)
258 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
259 t
->debug_reason
)->name
;
261 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
262 cp
= "(*BUG*unknown*BUG*)";
267 const char *target_state_name(struct target
*t
)
270 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
272 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
273 cp
= "(*BUG*unknown*BUG*)";
278 /* determine the number of the new target */
279 static int new_target_number(void)
284 /* number is 0 based */
288 if (x
< t
->target_number
)
289 x
= t
->target_number
;
295 /* read a uint32_t from a buffer in target memory endianness */
296 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
298 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
299 return le_to_h_u32(buffer
);
301 return be_to_h_u32(buffer
);
304 /* read a uint24_t from a buffer in target memory endianness */
305 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
307 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
308 return le_to_h_u24(buffer
);
310 return be_to_h_u24(buffer
);
313 /* read a uint16_t from a buffer in target memory endianness */
314 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
316 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
317 return le_to_h_u16(buffer
);
319 return be_to_h_u16(buffer
);
322 /* read a uint8_t from a buffer in target memory endianness */
323 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
325 return *buffer
& 0x0ff;
328 /* write a uint32_t to a buffer in target memory endianness */
329 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
331 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
332 h_u32_to_le(buffer
, value
);
334 h_u32_to_be(buffer
, value
);
337 /* write a uint24_t to a buffer in target memory endianness */
338 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
340 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
341 h_u24_to_le(buffer
, value
);
343 h_u24_to_be(buffer
, value
);
346 /* write a uint16_t to a buffer in target memory endianness */
347 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
349 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
350 h_u16_to_le(buffer
, value
);
352 h_u16_to_be(buffer
, value
);
355 /* write a uint8_t to a buffer in target memory endianness */
356 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
361 /* write a uint32_t array to a buffer in target memory endianness */
362 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
365 for (i
= 0; i
< count
; i
++)
366 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
369 /* write a uint16_t array to a buffer in target memory endianness */
370 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
373 for (i
= 0; i
< count
; i
++)
374 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
377 /* write a uint32_t array to a buffer in target memory endianness */
378 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, uint32_t *srcbuf
)
381 for (i
= 0; i
< count
; i
++)
382 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
385 /* write a uint16_t array to a buffer in target memory endianness */
386 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, uint16_t *srcbuf
)
389 for (i
= 0; i
< count
; i
++)
390 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
393 /* return a pointer to a configured target; id is name or number */
394 struct target
*get_target(const char *id
)
396 struct target
*target
;
398 /* try as tcltarget name */
399 for (target
= all_targets
; target
; target
= target
->next
) {
400 if (target_name(target
) == NULL
)
402 if (strcmp(id
, target_name(target
)) == 0)
406 /* It's OK to remove this fallback sometime after August 2010 or so */
408 /* no match, try as number */
410 if (parse_uint(id
, &num
) != ERROR_OK
)
413 for (target
= all_targets
; target
; target
= target
->next
) {
414 if (target
->target_number
== (int)num
) {
415 LOG_WARNING("use '%s' as target identifier, not '%u'",
416 target_name(target
), num
);
424 /* returns a pointer to the n-th configured target */
425 static struct target
*get_target_by_num(int num
)
427 struct target
*target
= all_targets
;
430 if (target
->target_number
== num
)
432 target
= target
->next
;
438 struct target
*get_current_target(struct command_context
*cmd_ctx
)
440 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
442 if (target
== NULL
) {
443 LOG_ERROR("BUG: current_target out of bounds");
450 int target_poll(struct target
*target
)
454 /* We can't poll until after examine */
455 if (!target_was_examined(target
)) {
456 /* Fail silently lest we pollute the log */
460 retval
= target
->type
->poll(target
);
461 if (retval
!= ERROR_OK
)
464 if (target
->halt_issued
) {
465 if (target
->state
== TARGET_HALTED
)
466 target
->halt_issued
= false;
468 long long t
= timeval_ms() - target
->halt_issued_time
;
469 if (t
> DEFAULT_HALT_TIMEOUT
) {
470 target
->halt_issued
= false;
471 LOG_INFO("Halt timed out, wake up GDB.");
472 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
480 int target_halt(struct target
*target
)
483 /* We can't poll until after examine */
484 if (!target_was_examined(target
)) {
485 LOG_ERROR("Target not examined yet");
489 retval
= target
->type
->halt(target
);
490 if (retval
!= ERROR_OK
)
493 target
->halt_issued
= true;
494 target
->halt_issued_time
= timeval_ms();
500 * Make the target (re)start executing using its saved execution
501 * context (possibly with some modifications).
503 * @param target Which target should start executing.
504 * @param current True to use the target's saved program counter instead
505 * of the address parameter
506 * @param address Optionally used as the program counter.
507 * @param handle_breakpoints True iff breakpoints at the resumption PC
508 * should be skipped. (For example, maybe execution was stopped by
509 * such a breakpoint, in which case it would be counterprodutive to
511 * @param debug_execution False if all working areas allocated by OpenOCD
512 * should be released and/or restored to their original contents.
513 * (This would for example be true to run some downloaded "helper"
514 * algorithm code, which resides in one such working buffer and uses
515 * another for data storage.)
517 * @todo Resolve the ambiguity about what the "debug_execution" flag
518 * signifies. For example, Target implementations don't agree on how
519 * it relates to invalidation of the register cache, or to whether
520 * breakpoints and watchpoints should be enabled. (It would seem wrong
521 * to enable breakpoints when running downloaded "helper" algorithms
522 * (debug_execution true), since the breakpoints would be set to match
523 * target firmware being debugged, not the helper algorithm.... and
524 * enabling them could cause such helpers to malfunction (for example,
525 * by overwriting data with a breakpoint instruction. On the other
526 * hand the infrastructure for running such helpers might use this
527 * procedure but rely on hardware breakpoint to detect termination.)
529 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
533 /* We can't poll until after examine */
534 if (!target_was_examined(target
)) {
535 LOG_ERROR("Target not examined yet");
539 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
541 /* note that resume *must* be asynchronous. The CPU can halt before
542 * we poll. The CPU can even halt at the current PC as a result of
543 * a software breakpoint being inserted by (a bug?) the application.
545 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
546 if (retval
!= ERROR_OK
)
549 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
554 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
559 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
560 if (n
->name
== NULL
) {
561 LOG_ERROR("invalid reset mode");
565 /* disable polling during reset to make reset event scripts
566 * more predictable, i.e. dr/irscan & pathmove in events will
567 * not have JTAG operations injected into the middle of a sequence.
569 bool save_poll
= jtag_poll_get_enabled();
571 jtag_poll_set_enabled(false);
573 sprintf(buf
, "ocd_process_reset %s", n
->name
);
574 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
576 jtag_poll_set_enabled(save_poll
);
578 if (retval
!= JIM_OK
) {
579 Jim_MakeErrorMessage(cmd_ctx
->interp
);
580 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
584 /* We want any events to be processed before the prompt */
585 retval
= target_call_timer_callbacks_now();
587 struct target
*target
;
588 for (target
= all_targets
; target
; target
= target
->next
) {
589 target
->type
->check_reset(target
);
590 target
->running_alg
= false;
596 static int identity_virt2phys(struct target
*target
,
597 uint32_t virtual, uint32_t *physical
)
603 static int no_mmu(struct target
*target
, int *enabled
)
609 static int default_examine(struct target
*target
)
611 target_set_examined(target
);
615 /* no check by default */
616 static int default_check_reset(struct target
*target
)
621 int target_examine_one(struct target
*target
)
623 return target
->type
->examine(target
);
626 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
628 struct target
*target
= priv
;
630 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
633 jtag_unregister_event_callback(jtag_enable_callback
, target
);
635 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
637 int retval
= target_examine_one(target
);
638 if (retval
!= ERROR_OK
)
641 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
646 /* Targets that correctly implement init + examine, i.e.
647 * no communication with target during init:
651 int target_examine(void)
653 int retval
= ERROR_OK
;
654 struct target
*target
;
656 for (target
= all_targets
; target
; target
= target
->next
) {
657 /* defer examination, but don't skip it */
658 if (!target
->tap
->enabled
) {
659 jtag_register_event_callback(jtag_enable_callback
,
664 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
666 retval
= target_examine_one(target
);
667 if (retval
!= ERROR_OK
)
670 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
675 const char *target_type_name(struct target
*target
)
677 return target
->type
->name
;
680 static int target_soft_reset_halt(struct target
*target
)
682 if (!target_was_examined(target
)) {
683 LOG_ERROR("Target not examined yet");
686 if (!target
->type
->soft_reset_halt
) {
687 LOG_ERROR("Target %s does not support soft_reset_halt",
688 target_name(target
));
691 return target
->type
->soft_reset_halt(target
);
695 * Downloads a target-specific native code algorithm to the target,
696 * and executes it. * Note that some targets may need to set up, enable,
697 * and tear down a breakpoint (hard or * soft) to detect algorithm
698 * termination, while others may support lower overhead schemes where
699 * soft breakpoints embedded in the algorithm automatically terminate the
702 * @param target used to run the algorithm
703 * @param arch_info target-specific description of the algorithm.
705 int target_run_algorithm(struct target
*target
,
706 int num_mem_params
, struct mem_param
*mem_params
,
707 int num_reg_params
, struct reg_param
*reg_param
,
708 uint32_t entry_point
, uint32_t exit_point
,
709 int timeout_ms
, void *arch_info
)
711 int retval
= ERROR_FAIL
;
713 if (!target_was_examined(target
)) {
714 LOG_ERROR("Target not examined yet");
717 if (!target
->type
->run_algorithm
) {
718 LOG_ERROR("Target type '%s' does not support %s",
719 target_type_name(target
), __func__
);
723 target
->running_alg
= true;
724 retval
= target
->type
->run_algorithm(target
,
725 num_mem_params
, mem_params
,
726 num_reg_params
, reg_param
,
727 entry_point
, exit_point
, timeout_ms
, arch_info
);
728 target
->running_alg
= false;
735 * Downloads a target-specific native code algorithm to the target,
736 * executes and leaves it running.
738 * @param target used to run the algorithm
739 * @param arch_info target-specific description of the algorithm.
741 int target_start_algorithm(struct target
*target
,
742 int num_mem_params
, struct mem_param
*mem_params
,
743 int num_reg_params
, struct reg_param
*reg_params
,
744 uint32_t entry_point
, uint32_t exit_point
,
747 int retval
= ERROR_FAIL
;
749 if (!target_was_examined(target
)) {
750 LOG_ERROR("Target not examined yet");
753 if (!target
->type
->start_algorithm
) {
754 LOG_ERROR("Target type '%s' does not support %s",
755 target_type_name(target
), __func__
);
758 if (target
->running_alg
) {
759 LOG_ERROR("Target is already running an algorithm");
763 target
->running_alg
= true;
764 retval
= target
->type
->start_algorithm(target
,
765 num_mem_params
, mem_params
,
766 num_reg_params
, reg_params
,
767 entry_point
, exit_point
, arch_info
);
774 * Waits for an algorithm started with target_start_algorithm() to complete.
776 * @param target used to run the algorithm
777 * @param arch_info target-specific description of the algorithm.
779 int target_wait_algorithm(struct target
*target
,
780 int num_mem_params
, struct mem_param
*mem_params
,
781 int num_reg_params
, struct reg_param
*reg_params
,
782 uint32_t exit_point
, int timeout_ms
,
785 int retval
= ERROR_FAIL
;
787 if (!target
->type
->wait_algorithm
) {
788 LOG_ERROR("Target type '%s' does not support %s",
789 target_type_name(target
), __func__
);
792 if (!target
->running_alg
) {
793 LOG_ERROR("Target is not running an algorithm");
797 retval
= target
->type
->wait_algorithm(target
,
798 num_mem_params
, mem_params
,
799 num_reg_params
, reg_params
,
800 exit_point
, timeout_ms
, arch_info
);
801 if (retval
!= ERROR_TARGET_TIMEOUT
)
802 target
->running_alg
= false;
809 * Executes a target-specific native code algorithm in the target.
810 * It differs from target_run_algorithm in that the algorithm is asynchronous.
811 * Because of this it requires an compliant algorithm:
812 * see contrib/loaders/flash/stm32f1x.S for example.
814 * @param target used to run the algorithm
817 int target_run_flash_async_algorithm(struct target
*target
,
818 uint8_t *buffer
, uint32_t count
, int block_size
,
819 int num_mem_params
, struct mem_param
*mem_params
,
820 int num_reg_params
, struct reg_param
*reg_params
,
821 uint32_t buffer_start
, uint32_t buffer_size
,
822 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
827 /* Set up working area. First word is write pointer, second word is read pointer,
828 * rest is fifo data area. */
829 uint32_t wp_addr
= buffer_start
;
830 uint32_t rp_addr
= buffer_start
+ 4;
831 uint32_t fifo_start_addr
= buffer_start
+ 8;
832 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
834 uint32_t wp
= fifo_start_addr
;
835 uint32_t rp
= fifo_start_addr
;
837 /* validate block_size is 2^n */
838 assert(!block_size
|| !(block_size
& (block_size
- 1)));
840 retval
= target_write_u32(target
, wp_addr
, wp
);
841 if (retval
!= ERROR_OK
)
843 retval
= target_write_u32(target
, rp_addr
, rp
);
844 if (retval
!= ERROR_OK
)
847 /* Start up algorithm on target and let it idle while writing the first chunk */
848 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
849 num_reg_params
, reg_params
,
854 if (retval
!= ERROR_OK
) {
855 LOG_ERROR("error starting target flash write algorithm");
861 retval
= target_read_u32(target
, rp_addr
, &rp
);
862 if (retval
!= ERROR_OK
) {
863 LOG_ERROR("failed to get read pointer");
867 LOG_DEBUG("count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
, count
, wp
, rp
);
870 LOG_ERROR("flash write algorithm aborted by target");
871 retval
= ERROR_FLASH_OPERATION_FAILED
;
875 if ((rp
& (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
876 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
880 /* Count the number of bytes available in the fifo without
881 * crossing the wrap around. Make sure to not fill it completely,
882 * because that would make wp == rp and that's the empty condition. */
883 uint32_t thisrun_bytes
;
885 thisrun_bytes
= rp
- wp
- block_size
;
886 else if (rp
> fifo_start_addr
)
887 thisrun_bytes
= fifo_end_addr
- wp
;
889 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
891 if (thisrun_bytes
== 0) {
892 /* Throttle polling a bit if transfer is (much) faster than flash
893 * programming. The exact delay shouldn't matter as long as it's
894 * less than buffer size / flash speed. This is very unlikely to
895 * run when using high latency connections such as USB. */
898 /* to stop an infinite loop on some targets check and increment a timeout
899 * this issue was observed on a stellaris using the new ICDI interface */
900 if (timeout
++ >= 500) {
901 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
902 return ERROR_FLASH_OPERATION_FAILED
;
907 /* reset our timeout */
910 /* Limit to the amount of data we actually want to write */
911 if (thisrun_bytes
> count
* block_size
)
912 thisrun_bytes
= count
* block_size
;
914 /* Write data to fifo */
915 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
916 if (retval
!= ERROR_OK
)
919 /* Update counters and wrap write pointer */
920 buffer
+= thisrun_bytes
;
921 count
-= thisrun_bytes
/ block_size
;
923 if (wp
>= fifo_end_addr
)
924 wp
= fifo_start_addr
;
926 /* Store updated write pointer to target */
927 retval
= target_write_u32(target
, wp_addr
, wp
);
928 if (retval
!= ERROR_OK
)
932 if (retval
!= ERROR_OK
) {
933 /* abort flash write algorithm on target */
934 target_write_u32(target
, wp_addr
, 0);
937 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
938 num_reg_params
, reg_params
,
943 if (retval2
!= ERROR_OK
) {
944 LOG_ERROR("error waiting for target flash write algorithm");
951 int target_read_memory(struct target
*target
,
952 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
954 if (!target_was_examined(target
)) {
955 LOG_ERROR("Target not examined yet");
958 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
961 int target_read_phys_memory(struct target
*target
,
962 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
964 if (!target_was_examined(target
)) {
965 LOG_ERROR("Target not examined yet");
968 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
971 int target_write_memory(struct target
*target
,
972 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
974 if (!target_was_examined(target
)) {
975 LOG_ERROR("Target not examined yet");
978 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
981 int target_write_phys_memory(struct target
*target
,
982 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
984 if (!target_was_examined(target
)) {
985 LOG_ERROR("Target not examined yet");
988 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
991 int target_add_breakpoint(struct target
*target
,
992 struct breakpoint
*breakpoint
)
994 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
995 LOG_WARNING("target %s is not halted", target_name(target
));
996 return ERROR_TARGET_NOT_HALTED
;
998 return target
->type
->add_breakpoint(target
, breakpoint
);
1001 int target_add_context_breakpoint(struct target
*target
,
1002 struct breakpoint
*breakpoint
)
1004 if (target
->state
!= TARGET_HALTED
) {
1005 LOG_WARNING("target %s is not halted", target_name(target
));
1006 return ERROR_TARGET_NOT_HALTED
;
1008 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1011 int target_add_hybrid_breakpoint(struct target
*target
,
1012 struct breakpoint
*breakpoint
)
1014 if (target
->state
!= TARGET_HALTED
) {
1015 LOG_WARNING("target %s is not halted", target_name(target
));
1016 return ERROR_TARGET_NOT_HALTED
;
1018 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1021 int target_remove_breakpoint(struct target
*target
,
1022 struct breakpoint
*breakpoint
)
1024 return target
->type
->remove_breakpoint(target
, breakpoint
);
1027 int target_add_watchpoint(struct target
*target
,
1028 struct watchpoint
*watchpoint
)
1030 if (target
->state
!= TARGET_HALTED
) {
1031 LOG_WARNING("target %s is not halted", target_name(target
));
1032 return ERROR_TARGET_NOT_HALTED
;
1034 return target
->type
->add_watchpoint(target
, watchpoint
);
1036 int target_remove_watchpoint(struct target
*target
,
1037 struct watchpoint
*watchpoint
)
1039 return target
->type
->remove_watchpoint(target
, watchpoint
);
1041 int target_hit_watchpoint(struct target
*target
,
1042 struct watchpoint
**hit_watchpoint
)
1044 if (target
->state
!= TARGET_HALTED
) {
1045 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1046 return ERROR_TARGET_NOT_HALTED
;
1049 if (target
->type
->hit_watchpoint
== NULL
) {
1050 /* For backward compatible, if hit_watchpoint is not implemented,
1051 * return ERROR_FAIL such that gdb_server will not take the nonsense
1056 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1059 int target_get_gdb_reg_list(struct target
*target
,
1060 struct reg
**reg_list
[], int *reg_list_size
,
1061 enum target_register_class reg_class
)
1063 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1065 int target_step(struct target
*target
,
1066 int current
, uint32_t address
, int handle_breakpoints
)
1068 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1071 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1073 if (target
->state
!= TARGET_HALTED
) {
1074 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1075 return ERROR_TARGET_NOT_HALTED
;
1077 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1080 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1082 if (target
->state
!= TARGET_HALTED
) {
1083 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1084 return ERROR_TARGET_NOT_HALTED
;
1086 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1089 int target_profiling(struct target
*target
, uint32_t *samples
,
1090 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1092 if (target
->state
!= TARGET_HALTED
) {
1093 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1094 return ERROR_TARGET_NOT_HALTED
;
1096 return target
->type
->profiling(target
, samples
, max_num_samples
,
1097 num_samples
, seconds
);
1101 * Reset the @c examined flag for the given target.
1102 * Pure paranoia -- targets are zeroed on allocation.
1104 static void target_reset_examined(struct target
*target
)
1106 target
->examined
= false;
1109 static int err_read_phys_memory(struct target
*target
, uint32_t address
,
1110 uint32_t size
, uint32_t count
, uint8_t *buffer
)
1112 LOG_ERROR("Not implemented: %s", __func__
);
1116 static int err_write_phys_memory(struct target
*target
, uint32_t address
,
1117 uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1119 LOG_ERROR("Not implemented: %s", __func__
);
1123 static int handle_target(void *priv
);
1125 static int target_init_one(struct command_context
*cmd_ctx
,
1126 struct target
*target
)
1128 target_reset_examined(target
);
1130 struct target_type
*type
= target
->type
;
1131 if (type
->examine
== NULL
)
1132 type
->examine
= default_examine
;
1134 if (type
->check_reset
== NULL
)
1135 type
->check_reset
= default_check_reset
;
1137 assert(type
->init_target
!= NULL
);
1139 int retval
= type
->init_target(cmd_ctx
, target
);
1140 if (ERROR_OK
!= retval
) {
1141 LOG_ERROR("target '%s' init failed", target_name(target
));
1145 /* Sanity-check MMU support ... stub in what we must, to help
1146 * implement it in stages, but warn if we need to do so.
1149 if (type
->write_phys_memory
== NULL
) {
1150 LOG_ERROR("type '%s' is missing write_phys_memory",
1152 type
->write_phys_memory
= err_write_phys_memory
;
1154 if (type
->read_phys_memory
== NULL
) {
1155 LOG_ERROR("type '%s' is missing read_phys_memory",
1157 type
->read_phys_memory
= err_read_phys_memory
;
1159 if (type
->virt2phys
== NULL
) {
1160 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1161 type
->virt2phys
= identity_virt2phys
;
1164 /* Make sure no-MMU targets all behave the same: make no
1165 * distinction between physical and virtual addresses, and
1166 * ensure that virt2phys() is always an identity mapping.
1168 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1169 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1172 type
->write_phys_memory
= type
->write_memory
;
1173 type
->read_phys_memory
= type
->read_memory
;
1174 type
->virt2phys
= identity_virt2phys
;
1177 if (target
->type
->read_buffer
== NULL
)
1178 target
->type
->read_buffer
= target_read_buffer_default
;
1180 if (target
->type
->write_buffer
== NULL
)
1181 target
->type
->write_buffer
= target_write_buffer_default
;
1183 if (target
->type
->get_gdb_fileio_info
== NULL
)
1184 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1186 if (target
->type
->gdb_fileio_end
== NULL
)
1187 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1189 if (target
->type
->profiling
== NULL
)
1190 target
->type
->profiling
= target_profiling_default
;
1195 static int target_init(struct command_context
*cmd_ctx
)
1197 struct target
*target
;
1200 for (target
= all_targets
; target
; target
= target
->next
) {
1201 retval
= target_init_one(cmd_ctx
, target
);
1202 if (ERROR_OK
!= retval
)
1209 retval
= target_register_user_commands(cmd_ctx
);
1210 if (ERROR_OK
!= retval
)
1213 retval
= target_register_timer_callback(&handle_target
,
1214 polling_interval
, 1, cmd_ctx
->interp
);
1215 if (ERROR_OK
!= retval
)
1221 COMMAND_HANDLER(handle_target_init_command
)
1226 return ERROR_COMMAND_SYNTAX_ERROR
;
1228 static bool target_initialized
;
1229 if (target_initialized
) {
1230 LOG_INFO("'target init' has already been called");
1233 target_initialized
= true;
1235 retval
= command_run_line(CMD_CTX
, "init_targets");
1236 if (ERROR_OK
!= retval
)
1239 retval
= command_run_line(CMD_CTX
, "init_board");
1240 if (ERROR_OK
!= retval
)
1243 LOG_DEBUG("Initializing targets...");
1244 return target_init(CMD_CTX
);
1247 int target_register_event_callback(int (*callback
)(struct target
*target
,
1248 enum target_event event
, void *priv
), void *priv
)
1250 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1252 if (callback
== NULL
)
1253 return ERROR_COMMAND_SYNTAX_ERROR
;
1256 while ((*callbacks_p
)->next
)
1257 callbacks_p
= &((*callbacks_p
)->next
);
1258 callbacks_p
= &((*callbacks_p
)->next
);
1261 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1262 (*callbacks_p
)->callback
= callback
;
1263 (*callbacks_p
)->priv
= priv
;
1264 (*callbacks_p
)->next
= NULL
;
1269 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1271 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1274 if (callback
== NULL
)
1275 return ERROR_COMMAND_SYNTAX_ERROR
;
1278 while ((*callbacks_p
)->next
)
1279 callbacks_p
= &((*callbacks_p
)->next
);
1280 callbacks_p
= &((*callbacks_p
)->next
);
1283 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1284 (*callbacks_p
)->callback
= callback
;
1285 (*callbacks_p
)->periodic
= periodic
;
1286 (*callbacks_p
)->time_ms
= time_ms
;
1288 gettimeofday(&now
, NULL
);
1289 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
1290 time_ms
-= (time_ms
% 1000);
1291 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
1292 if ((*callbacks_p
)->when
.tv_usec
> 1000000) {
1293 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
1294 (*callbacks_p
)->when
.tv_sec
+= 1;
1297 (*callbacks_p
)->priv
= priv
;
1298 (*callbacks_p
)->next
= NULL
;
1303 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1304 enum target_event event
, void *priv
), void *priv
)
1306 struct target_event_callback
**p
= &target_event_callbacks
;
1307 struct target_event_callback
*c
= target_event_callbacks
;
1309 if (callback
== NULL
)
1310 return ERROR_COMMAND_SYNTAX_ERROR
;
1313 struct target_event_callback
*next
= c
->next
;
1314 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1326 static int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1328 struct target_timer_callback
**p
= &target_timer_callbacks
;
1329 struct target_timer_callback
*c
= target_timer_callbacks
;
1331 if (callback
== NULL
)
1332 return ERROR_COMMAND_SYNTAX_ERROR
;
1335 struct target_timer_callback
*next
= c
->next
;
1336 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1348 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1350 struct target_event_callback
*callback
= target_event_callbacks
;
1351 struct target_event_callback
*next_callback
;
1353 if (event
== TARGET_EVENT_HALTED
) {
1354 /* execute early halted first */
1355 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1358 LOG_DEBUG("target event %i (%s)", event
,
1359 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1361 target_handle_event(target
, event
);
1364 next_callback
= callback
->next
;
1365 callback
->callback(target
, event
, callback
->priv
);
1366 callback
= next_callback
;
1372 static int target_timer_callback_periodic_restart(
1373 struct target_timer_callback
*cb
, struct timeval
*now
)
1375 int time_ms
= cb
->time_ms
;
1376 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1377 time_ms
-= (time_ms
% 1000);
1378 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1379 if (cb
->when
.tv_usec
> 1000000) {
1380 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1381 cb
->when
.tv_sec
+= 1;
1386 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1387 struct timeval
*now
)
1389 cb
->callback(cb
->priv
);
1392 return target_timer_callback_periodic_restart(cb
, now
);
1394 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1397 static int target_call_timer_callbacks_check_time(int checktime
)
1402 gettimeofday(&now
, NULL
);
1404 struct target_timer_callback
*callback
= target_timer_callbacks
;
1406 /* cleaning up may unregister and free this callback */
1407 struct target_timer_callback
*next_callback
= callback
->next
;
1409 bool call_it
= callback
->callback
&&
1410 ((!checktime
&& callback
->periodic
) ||
1411 now
.tv_sec
> callback
->when
.tv_sec
||
1412 (now
.tv_sec
== callback
->when
.tv_sec
&&
1413 now
.tv_usec
>= callback
->when
.tv_usec
));
1416 int retval
= target_call_timer_callback(callback
, &now
);
1417 if (retval
!= ERROR_OK
)
1421 callback
= next_callback
;
1427 int target_call_timer_callbacks(void)
1429 return target_call_timer_callbacks_check_time(1);
1432 /* invoke periodic callbacks immediately */
1433 int target_call_timer_callbacks_now(void)
1435 return target_call_timer_callbacks_check_time(0);
1438 /* Prints the working area layout for debug purposes */
1439 static void print_wa_layout(struct target
*target
)
1441 struct working_area
*c
= target
->working_areas
;
1444 LOG_DEBUG("%c%c 0x%08"PRIx32
"-0x%08"PRIx32
" (%"PRIu32
" bytes)",
1445 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1446 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1451 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1452 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1454 assert(area
->free
); /* Shouldn't split an allocated area */
1455 assert(size
<= area
->size
); /* Caller should guarantee this */
1457 /* Split only if not already the right size */
1458 if (size
< area
->size
) {
1459 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1464 new_wa
->next
= area
->next
;
1465 new_wa
->size
= area
->size
- size
;
1466 new_wa
->address
= area
->address
+ size
;
1467 new_wa
->backup
= NULL
;
1468 new_wa
->user
= NULL
;
1469 new_wa
->free
= true;
1471 area
->next
= new_wa
;
1474 /* If backup memory was allocated to this area, it has the wrong size
1475 * now so free it and it will be reallocated if/when needed */
1478 area
->backup
= NULL
;
1483 /* Merge all adjacent free areas into one */
1484 static void target_merge_working_areas(struct target
*target
)
1486 struct working_area
*c
= target
->working_areas
;
1488 while (c
&& c
->next
) {
1489 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1491 /* Find two adjacent free areas */
1492 if (c
->free
&& c
->next
->free
) {
1493 /* Merge the last into the first */
1494 c
->size
+= c
->next
->size
;
1496 /* Remove the last */
1497 struct working_area
*to_be_freed
= c
->next
;
1498 c
->next
= c
->next
->next
;
1499 if (to_be_freed
->backup
)
1500 free(to_be_freed
->backup
);
1503 /* If backup memory was allocated to the remaining area, it's has
1504 * the wrong size now */
1515 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1517 /* Reevaluate working area address based on MMU state*/
1518 if (target
->working_areas
== NULL
) {
1522 retval
= target
->type
->mmu(target
, &enabled
);
1523 if (retval
!= ERROR_OK
)
1527 if (target
->working_area_phys_spec
) {
1528 LOG_DEBUG("MMU disabled, using physical "
1529 "address for working memory 0x%08"PRIx32
,
1530 target
->working_area_phys
);
1531 target
->working_area
= target
->working_area_phys
;
1533 LOG_ERROR("No working memory available. "
1534 "Specify -work-area-phys to target.");
1535 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1538 if (target
->working_area_virt_spec
) {
1539 LOG_DEBUG("MMU enabled, using virtual "
1540 "address for working memory 0x%08"PRIx32
,
1541 target
->working_area_virt
);
1542 target
->working_area
= target
->working_area_virt
;
1544 LOG_ERROR("No working memory available. "
1545 "Specify -work-area-virt to target.");
1546 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1550 /* Set up initial working area on first call */
1551 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1553 new_wa
->next
= NULL
;
1554 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1555 new_wa
->address
= target
->working_area
;
1556 new_wa
->backup
= NULL
;
1557 new_wa
->user
= NULL
;
1558 new_wa
->free
= true;
1561 target
->working_areas
= new_wa
;
1564 /* only allocate multiples of 4 byte */
1566 size
= (size
+ 3) & (~3UL);
1568 struct working_area
*c
= target
->working_areas
;
1570 /* Find the first large enough working area */
1572 if (c
->free
&& c
->size
>= size
)
1578 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1580 /* Split the working area into the requested size */
1581 target_split_working_area(c
, size
);
1583 LOG_DEBUG("allocated new working area of %"PRIu32
" bytes at address 0x%08"PRIx32
, size
, c
->address
);
1585 if (target
->backup_working_area
) {
1586 if (c
->backup
== NULL
) {
1587 c
->backup
= malloc(c
->size
);
1588 if (c
->backup
== NULL
)
1592 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1593 if (retval
!= ERROR_OK
)
1597 /* mark as used, and return the new (reused) area */
1604 print_wa_layout(target
);
1609 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1613 retval
= target_alloc_working_area_try(target
, size
, area
);
1614 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1615 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1620 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1622 int retval
= ERROR_OK
;
1624 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1625 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1626 if (retval
!= ERROR_OK
)
1627 LOG_ERROR("failed to restore %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1628 area
->size
, area
->address
);
1634 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1635 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1637 int retval
= ERROR_OK
;
1643 retval
= target_restore_working_area(target
, area
);
1644 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1645 if (retval
!= ERROR_OK
)
1651 LOG_DEBUG("freed %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1652 area
->size
, area
->address
);
1654 /* mark user pointer invalid */
1655 /* TODO: Is this really safe? It points to some previous caller's memory.
1656 * How could we know that the area pointer is still in that place and not
1657 * some other vital data? What's the purpose of this, anyway? */
1661 target_merge_working_areas(target
);
1663 print_wa_layout(target
);
1668 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1670 return target_free_working_area_restore(target
, area
, 1);
1673 /* free resources and restore memory, if restoring memory fails,
1674 * free up resources anyway
1676 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1678 struct working_area
*c
= target
->working_areas
;
1680 LOG_DEBUG("freeing all working areas");
1682 /* Loop through all areas, restoring the allocated ones and marking them as free */
1686 target_restore_working_area(target
, c
);
1688 *c
->user
= NULL
; /* Same as above */
1694 /* Run a merge pass to combine all areas into one */
1695 target_merge_working_areas(target
);
1697 print_wa_layout(target
);
1700 void target_free_all_working_areas(struct target
*target
)
1702 target_free_all_working_areas_restore(target
, 1);
1705 /* Find the largest number of bytes that can be allocated */
1706 uint32_t target_get_working_area_avail(struct target
*target
)
1708 struct working_area
*c
= target
->working_areas
;
1709 uint32_t max_size
= 0;
1712 return target
->working_area_size
;
1715 if (c
->free
&& max_size
< c
->size
)
1724 int target_arch_state(struct target
*target
)
1727 if (target
== NULL
) {
1728 LOG_USER("No target has been configured");
1732 LOG_USER("target state: %s", target_state_name(target
));
1734 if (target
->state
!= TARGET_HALTED
)
1737 retval
= target
->type
->arch_state(target
);
1741 static int target_get_gdb_fileio_info_default(struct target
*target
,
1742 struct gdb_fileio_info
*fileio_info
)
1744 /* If target does not support semi-hosting function, target
1745 has no need to provide .get_gdb_fileio_info callback.
1746 It just return ERROR_FAIL and gdb_server will return "Txx"
1747 as target halted every time. */
1751 static int target_gdb_fileio_end_default(struct target
*target
,
1752 int retcode
, int fileio_errno
, bool ctrl_c
)
1757 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
1758 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1760 struct timeval timeout
, now
;
1762 gettimeofday(&timeout
, NULL
);
1763 timeval_add_time(&timeout
, seconds
, 0);
1765 LOG_INFO("Starting profiling. Halting and resuming the"
1766 " target as often as we can...");
1768 uint32_t sample_count
= 0;
1769 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
1770 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
1772 int retval
= ERROR_OK
;
1774 target_poll(target
);
1775 if (target
->state
== TARGET_HALTED
) {
1776 uint32_t t
= *((uint32_t *)reg
->value
);
1777 samples
[sample_count
++] = t
;
1778 /* current pc, addr = 0, do not handle breakpoints, not debugging */
1779 retval
= target_resume(target
, 1, 0, 0, 0);
1780 target_poll(target
);
1781 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
1782 } else if (target
->state
== TARGET_RUNNING
) {
1783 /* We want to quickly sample the PC. */
1784 retval
= target_halt(target
);
1786 LOG_INFO("Target not halted or running");
1791 if (retval
!= ERROR_OK
)
1794 gettimeofday(&now
, NULL
);
1795 if ((sample_count
>= max_num_samples
) ||
1796 ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
))) {
1797 LOG_INFO("Profiling completed. %d samples.", sample_count
);
1802 *num_samples
= sample_count
;
1806 /* Single aligned words are guaranteed to use 16 or 32 bit access
1807 * mode respectively, otherwise data is handled as quickly as
1810 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
1812 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1813 (int)size
, (unsigned)address
);
1815 if (!target_was_examined(target
)) {
1816 LOG_ERROR("Target not examined yet");
1823 if ((address
+ size
- 1) < address
) {
1824 /* GDB can request this when e.g. PC is 0xfffffffc*/
1825 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1831 return target
->type
->write_buffer(target
, address
, size
, buffer
);
1834 static int target_write_buffer_default(struct target
*target
, uint32_t address
, uint32_t count
, const uint8_t *buffer
)
1838 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
1839 * will have something to do with the size we leave to it. */
1840 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
1841 if (address
& size
) {
1842 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
1843 if (retval
!= ERROR_OK
)
1851 /* Write the data with as large access size as possible. */
1852 for (; size
> 0; size
/= 2) {
1853 uint32_t aligned
= count
- count
% size
;
1855 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
1856 if (retval
!= ERROR_OK
)
1867 /* Single aligned words are guaranteed to use 16 or 32 bit access
1868 * mode respectively, otherwise data is handled as quickly as
1871 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1873 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1874 (int)size
, (unsigned)address
);
1876 if (!target_was_examined(target
)) {
1877 LOG_ERROR("Target not examined yet");
1884 if ((address
+ size
- 1) < address
) {
1885 /* GDB can request this when e.g. PC is 0xfffffffc*/
1886 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1892 return target
->type
->read_buffer(target
, address
, size
, buffer
);
1895 static int target_read_buffer_default(struct target
*target
, uint32_t address
, uint32_t count
, uint8_t *buffer
)
1899 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
1900 * will have something to do with the size we leave to it. */
1901 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
1902 if (address
& size
) {
1903 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
1904 if (retval
!= ERROR_OK
)
1912 /* Read the data with as large access size as possible. */
1913 for (; size
> 0; size
/= 2) {
1914 uint32_t aligned
= count
- count
% size
;
1916 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
1917 if (retval
!= ERROR_OK
)
1928 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1933 uint32_t checksum
= 0;
1934 if (!target_was_examined(target
)) {
1935 LOG_ERROR("Target not examined yet");
1939 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
1940 if (retval
!= ERROR_OK
) {
1941 buffer
= malloc(size
);
1942 if (buffer
== NULL
) {
1943 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1944 return ERROR_COMMAND_SYNTAX_ERROR
;
1946 retval
= target_read_buffer(target
, address
, size
, buffer
);
1947 if (retval
!= ERROR_OK
) {
1952 /* convert to target endianness */
1953 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
1954 uint32_t target_data
;
1955 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1956 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1959 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1968 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1971 if (!target_was_examined(target
)) {
1972 LOG_ERROR("Target not examined yet");
1976 if (target
->type
->blank_check_memory
== 0)
1977 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1979 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1984 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
1986 uint8_t value_buf
[4];
1987 if (!target_was_examined(target
)) {
1988 LOG_ERROR("Target not examined yet");
1992 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1994 if (retval
== ERROR_OK
) {
1995 *value
= target_buffer_get_u32(target
, value_buf
);
1996 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
2001 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2008 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
2010 uint8_t value_buf
[2];
2011 if (!target_was_examined(target
)) {
2012 LOG_ERROR("Target not examined yet");
2016 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2018 if (retval
== ERROR_OK
) {
2019 *value
= target_buffer_get_u16(target
, value_buf
);
2020 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
2025 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2032 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
2034 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2035 if (!target_was_examined(target
)) {
2036 LOG_ERROR("Target not examined yet");
2040 if (retval
== ERROR_OK
) {
2041 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
2046 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2053 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
2056 uint8_t value_buf
[4];
2057 if (!target_was_examined(target
)) {
2058 LOG_ERROR("Target not examined yet");
2062 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
2066 target_buffer_set_u32(target
, value_buf
, value
);
2067 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2068 if (retval
!= ERROR_OK
)
2069 LOG_DEBUG("failed: %i", retval
);
2074 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
2077 uint8_t value_buf
[2];
2078 if (!target_was_examined(target
)) {
2079 LOG_ERROR("Target not examined yet");
2083 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
2087 target_buffer_set_u16(target
, value_buf
, value
);
2088 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2089 if (retval
!= ERROR_OK
)
2090 LOG_DEBUG("failed: %i", retval
);
2095 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
2098 if (!target_was_examined(target
)) {
2099 LOG_ERROR("Target not examined yet");
2103 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
2106 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2107 if (retval
!= ERROR_OK
)
2108 LOG_DEBUG("failed: %i", retval
);
2113 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2115 struct target
*target
= get_target(name
);
2116 if (target
== NULL
) {
2117 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2120 if (!target
->tap
->enabled
) {
2121 LOG_USER("Target: TAP %s is disabled, "
2122 "can't be the current target\n",
2123 target
->tap
->dotted_name
);
2127 cmd_ctx
->current_target
= target
->target_number
;
2132 COMMAND_HANDLER(handle_targets_command
)
2134 int retval
= ERROR_OK
;
2135 if (CMD_ARGC
== 1) {
2136 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2137 if (retval
== ERROR_OK
) {
2143 struct target
*target
= all_targets
;
2144 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2145 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2150 if (target
->tap
->enabled
)
2151 state
= target_state_name(target
);
2153 state
= "tap-disabled";
2155 if (CMD_CTX
->current_target
== target
->target_number
)
2158 /* keep columns lined up to match the headers above */
2159 command_print(CMD_CTX
,
2160 "%2d%c %-18s %-10s %-6s %-18s %s",
2161 target
->target_number
,
2163 target_name(target
),
2164 target_type_name(target
),
2165 Jim_Nvp_value2name_simple(nvp_target_endian
,
2166 target
->endianness
)->name
,
2167 target
->tap
->dotted_name
,
2169 target
= target
->next
;
2175 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2177 static int powerDropout
;
2178 static int srstAsserted
;
2180 static int runPowerRestore
;
2181 static int runPowerDropout
;
2182 static int runSrstAsserted
;
2183 static int runSrstDeasserted
;
2185 static int sense_handler(void)
2187 static int prevSrstAsserted
;
2188 static int prevPowerdropout
;
2190 int retval
= jtag_power_dropout(&powerDropout
);
2191 if (retval
!= ERROR_OK
)
2195 powerRestored
= prevPowerdropout
&& !powerDropout
;
2197 runPowerRestore
= 1;
2199 long long current
= timeval_ms();
2200 static long long lastPower
;
2201 int waitMore
= lastPower
+ 2000 > current
;
2202 if (powerDropout
&& !waitMore
) {
2203 runPowerDropout
= 1;
2204 lastPower
= current
;
2207 retval
= jtag_srst_asserted(&srstAsserted
);
2208 if (retval
!= ERROR_OK
)
2212 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2214 static long long lastSrst
;
2215 waitMore
= lastSrst
+ 2000 > current
;
2216 if (srstDeasserted
&& !waitMore
) {
2217 runSrstDeasserted
= 1;
2221 if (!prevSrstAsserted
&& srstAsserted
)
2222 runSrstAsserted
= 1;
2224 prevSrstAsserted
= srstAsserted
;
2225 prevPowerdropout
= powerDropout
;
2227 if (srstDeasserted
|| powerRestored
) {
2228 /* Other than logging the event we can't do anything here.
2229 * Issuing a reset is a particularly bad idea as we might
2230 * be inside a reset already.
2237 /* process target state changes */
2238 static int handle_target(void *priv
)
2240 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2241 int retval
= ERROR_OK
;
2243 if (!is_jtag_poll_safe()) {
2244 /* polling is disabled currently */
2248 /* we do not want to recurse here... */
2249 static int recursive
;
2253 /* danger! running these procedures can trigger srst assertions and power dropouts.
2254 * We need to avoid an infinite loop/recursion here and we do that by
2255 * clearing the flags after running these events.
2257 int did_something
= 0;
2258 if (runSrstAsserted
) {
2259 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2260 Jim_Eval(interp
, "srst_asserted");
2263 if (runSrstDeasserted
) {
2264 Jim_Eval(interp
, "srst_deasserted");
2267 if (runPowerDropout
) {
2268 LOG_INFO("Power dropout detected, running power_dropout proc.");
2269 Jim_Eval(interp
, "power_dropout");
2272 if (runPowerRestore
) {
2273 Jim_Eval(interp
, "power_restore");
2277 if (did_something
) {
2278 /* clear detect flags */
2282 /* clear action flags */
2284 runSrstAsserted
= 0;
2285 runSrstDeasserted
= 0;
2286 runPowerRestore
= 0;
2287 runPowerDropout
= 0;
2292 /* Poll targets for state changes unless that's globally disabled.
2293 * Skip targets that are currently disabled.
2295 for (struct target
*target
= all_targets
;
2296 is_jtag_poll_safe() && target
;
2297 target
= target
->next
) {
2298 if (!target
->tap
->enabled
)
2301 if (target
->backoff
.times
> target
->backoff
.count
) {
2302 /* do not poll this time as we failed previously */
2303 target
->backoff
.count
++;
2306 target
->backoff
.count
= 0;
2308 /* only poll target if we've got power and srst isn't asserted */
2309 if (!powerDropout
&& !srstAsserted
) {
2310 /* polling may fail silently until the target has been examined */
2311 retval
= target_poll(target
);
2312 if (retval
!= ERROR_OK
) {
2313 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2314 if (target
->backoff
.times
* polling_interval
< 5000) {
2315 target
->backoff
.times
*= 2;
2316 target
->backoff
.times
++;
2318 LOG_USER("Polling target %s failed, GDB will be halted. Polling again in %dms",
2319 target_name(target
),
2320 target
->backoff
.times
* polling_interval
);
2322 /* Tell GDB to halt the debugger. This allows the user to
2323 * run monitor commands to handle the situation.
2325 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2328 /* Since we succeeded, we reset backoff count */
2329 if (target
->backoff
.times
> 0)
2330 LOG_USER("Polling target %s succeeded again", target_name(target
));
2331 target
->backoff
.times
= 0;
2338 COMMAND_HANDLER(handle_reg_command
)
2340 struct target
*target
;
2341 struct reg
*reg
= NULL
;
2347 target
= get_current_target(CMD_CTX
);
2349 /* list all available registers for the current target */
2350 if (CMD_ARGC
== 0) {
2351 struct reg_cache
*cache
= target
->reg_cache
;
2357 command_print(CMD_CTX
, "===== %s", cache
->name
);
2359 for (i
= 0, reg
= cache
->reg_list
;
2360 i
< cache
->num_regs
;
2361 i
++, reg
++, count
++) {
2362 /* only print cached values if they are valid */
2364 value
= buf_to_str(reg
->value
,
2366 command_print(CMD_CTX
,
2367 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2375 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2380 cache
= cache
->next
;
2386 /* access a single register by its ordinal number */
2387 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2389 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2391 struct reg_cache
*cache
= target
->reg_cache
;
2395 for (i
= 0; i
< cache
->num_regs
; i
++) {
2396 if (count
++ == num
) {
2397 reg
= &cache
->reg_list
[i
];
2403 cache
= cache
->next
;
2407 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2408 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2412 /* access a single register by its name */
2413 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2416 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2421 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2423 /* display a register */
2424 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2425 && (CMD_ARGV
[1][0] <= '9')))) {
2426 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2429 if (reg
->valid
== 0)
2430 reg
->type
->get(reg
);
2431 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2432 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2437 /* set register value */
2438 if (CMD_ARGC
== 2) {
2439 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2442 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2444 reg
->type
->set(reg
, buf
);
2446 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2447 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2455 return ERROR_COMMAND_SYNTAX_ERROR
;
2458 COMMAND_HANDLER(handle_poll_command
)
2460 int retval
= ERROR_OK
;
2461 struct target
*target
= get_current_target(CMD_CTX
);
2463 if (CMD_ARGC
== 0) {
2464 command_print(CMD_CTX
, "background polling: %s",
2465 jtag_poll_get_enabled() ? "on" : "off");
2466 command_print(CMD_CTX
, "TAP: %s (%s)",
2467 target
->tap
->dotted_name
,
2468 target
->tap
->enabled
? "enabled" : "disabled");
2469 if (!target
->tap
->enabled
)
2471 retval
= target_poll(target
);
2472 if (retval
!= ERROR_OK
)
2474 retval
= target_arch_state(target
);
2475 if (retval
!= ERROR_OK
)
2477 } else if (CMD_ARGC
== 1) {
2479 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2480 jtag_poll_set_enabled(enable
);
2482 return ERROR_COMMAND_SYNTAX_ERROR
;
2487 COMMAND_HANDLER(handle_wait_halt_command
)
2490 return ERROR_COMMAND_SYNTAX_ERROR
;
2492 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2493 if (1 == CMD_ARGC
) {
2494 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2495 if (ERROR_OK
!= retval
)
2496 return ERROR_COMMAND_SYNTAX_ERROR
;
2499 struct target
*target
= get_current_target(CMD_CTX
);
2500 return target_wait_state(target
, TARGET_HALTED
, ms
);
2503 /* wait for target state to change. The trick here is to have a low
2504 * latency for short waits and not to suck up all the CPU time
2507 * After 500ms, keep_alive() is invoked
2509 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2512 long long then
= 0, cur
;
2516 retval
= target_poll(target
);
2517 if (retval
!= ERROR_OK
)
2519 if (target
->state
== state
)
2524 then
= timeval_ms();
2525 LOG_DEBUG("waiting for target %s...",
2526 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2532 if ((cur
-then
) > ms
) {
2533 LOG_ERROR("timed out while waiting for target %s",
2534 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2542 COMMAND_HANDLER(handle_halt_command
)
2546 struct target
*target
= get_current_target(CMD_CTX
);
2547 int retval
= target_halt(target
);
2548 if (ERROR_OK
!= retval
)
2551 if (CMD_ARGC
== 1) {
2552 unsigned wait_local
;
2553 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2554 if (ERROR_OK
!= retval
)
2555 return ERROR_COMMAND_SYNTAX_ERROR
;
2560 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2563 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2565 struct target
*target
= get_current_target(CMD_CTX
);
2567 LOG_USER("requesting target halt and executing a soft reset");
2569 target_soft_reset_halt(target
);
2574 COMMAND_HANDLER(handle_reset_command
)
2577 return ERROR_COMMAND_SYNTAX_ERROR
;
2579 enum target_reset_mode reset_mode
= RESET_RUN
;
2580 if (CMD_ARGC
== 1) {
2582 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2583 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
2584 return ERROR_COMMAND_SYNTAX_ERROR
;
2585 reset_mode
= n
->value
;
2588 /* reset *all* targets */
2589 return target_process_reset(CMD_CTX
, reset_mode
);
2593 COMMAND_HANDLER(handle_resume_command
)
2597 return ERROR_COMMAND_SYNTAX_ERROR
;
2599 struct target
*target
= get_current_target(CMD_CTX
);
2601 /* with no CMD_ARGV, resume from current pc, addr = 0,
2602 * with one arguments, addr = CMD_ARGV[0],
2603 * handle breakpoints, not debugging */
2605 if (CMD_ARGC
== 1) {
2606 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2610 return target_resume(target
, current
, addr
, 1, 0);
2613 COMMAND_HANDLER(handle_step_command
)
2616 return ERROR_COMMAND_SYNTAX_ERROR
;
2620 /* with no CMD_ARGV, step from current pc, addr = 0,
2621 * with one argument addr = CMD_ARGV[0],
2622 * handle breakpoints, debugging */
2625 if (CMD_ARGC
== 1) {
2626 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2630 struct target
*target
= get_current_target(CMD_CTX
);
2632 return target
->type
->step(target
, current_pc
, addr
, 1);
2635 static void handle_md_output(struct command_context
*cmd_ctx
,
2636 struct target
*target
, uint32_t address
, unsigned size
,
2637 unsigned count
, const uint8_t *buffer
)
2639 const unsigned line_bytecnt
= 32;
2640 unsigned line_modulo
= line_bytecnt
/ size
;
2642 char output
[line_bytecnt
* 4 + 1];
2643 unsigned output_len
= 0;
2645 const char *value_fmt
;
2648 value_fmt
= "%8.8x ";
2651 value_fmt
= "%4.4x ";
2654 value_fmt
= "%2.2x ";
2657 /* "can't happen", caller checked */
2658 LOG_ERROR("invalid memory read size: %u", size
);
2662 for (unsigned i
= 0; i
< count
; i
++) {
2663 if (i
% line_modulo
== 0) {
2664 output_len
+= snprintf(output
+ output_len
,
2665 sizeof(output
) - output_len
,
2667 (unsigned)(address
+ (i
*size
)));
2671 const uint8_t *value_ptr
= buffer
+ i
* size
;
2674 value
= target_buffer_get_u32(target
, value_ptr
);
2677 value
= target_buffer_get_u16(target
, value_ptr
);
2682 output_len
+= snprintf(output
+ output_len
,
2683 sizeof(output
) - output_len
,
2686 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
2687 command_print(cmd_ctx
, "%s", output
);
2693 COMMAND_HANDLER(handle_md_command
)
2696 return ERROR_COMMAND_SYNTAX_ERROR
;
2699 switch (CMD_NAME
[2]) {
2710 return ERROR_COMMAND_SYNTAX_ERROR
;
2713 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2714 int (*fn
)(struct target
*target
,
2715 uint32_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
2719 fn
= target_read_phys_memory
;
2721 fn
= target_read_memory
;
2722 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2723 return ERROR_COMMAND_SYNTAX_ERROR
;
2726 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2730 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2732 uint8_t *buffer
= calloc(count
, size
);
2734 struct target
*target
= get_current_target(CMD_CTX
);
2735 int retval
= fn(target
, address
, size
, count
, buffer
);
2736 if (ERROR_OK
== retval
)
2737 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2744 typedef int (*target_write_fn
)(struct target
*target
,
2745 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
2747 static int target_fill_mem(struct target
*target
,
2756 /* We have to write in reasonably large chunks to be able
2757 * to fill large memory areas with any sane speed */
2758 const unsigned chunk_size
= 16384;
2759 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
2760 if (target_buf
== NULL
) {
2761 LOG_ERROR("Out of memory");
2765 for (unsigned i
= 0; i
< chunk_size
; i
++) {
2766 switch (data_size
) {
2768 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
2771 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
2774 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
2781 int retval
= ERROR_OK
;
2783 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
2786 if (current
> chunk_size
)
2787 current
= chunk_size
;
2788 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
2789 if (retval
!= ERROR_OK
)
2791 /* avoid GDB timeouts */
2800 COMMAND_HANDLER(handle_mw_command
)
2803 return ERROR_COMMAND_SYNTAX_ERROR
;
2804 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2809 fn
= target_write_phys_memory
;
2811 fn
= target_write_memory
;
2812 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2813 return ERROR_COMMAND_SYNTAX_ERROR
;
2816 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2819 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2823 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2825 struct target
*target
= get_current_target(CMD_CTX
);
2827 switch (CMD_NAME
[2]) {
2838 return ERROR_COMMAND_SYNTAX_ERROR
;
2841 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
2844 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2845 uint32_t *min_address
, uint32_t *max_address
)
2847 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2848 return ERROR_COMMAND_SYNTAX_ERROR
;
2850 /* a base address isn't always necessary,
2851 * default to 0x0 (i.e. don't relocate) */
2852 if (CMD_ARGC
>= 2) {
2854 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2855 image
->base_address
= addr
;
2856 image
->base_address_set
= 1;
2858 image
->base_address_set
= 0;
2860 image
->start_address_set
= 0;
2863 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2864 if (CMD_ARGC
== 5) {
2865 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2866 /* use size (given) to find max (required) */
2867 *max_address
+= *min_address
;
2870 if (*min_address
> *max_address
)
2871 return ERROR_COMMAND_SYNTAX_ERROR
;
2876 COMMAND_HANDLER(handle_load_image_command
)
2880 uint32_t image_size
;
2881 uint32_t min_address
= 0;
2882 uint32_t max_address
= 0xffffffff;
2886 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2887 &image
, &min_address
, &max_address
);
2888 if (ERROR_OK
!= retval
)
2891 struct target
*target
= get_current_target(CMD_CTX
);
2893 struct duration bench
;
2894 duration_start(&bench
);
2896 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2901 for (i
= 0; i
< image
.num_sections
; i
++) {
2902 buffer
= malloc(image
.sections
[i
].size
);
2903 if (buffer
== NULL
) {
2904 command_print(CMD_CTX
,
2905 "error allocating buffer for section (%d bytes)",
2906 (int)(image
.sections
[i
].size
));
2910 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
2911 if (retval
!= ERROR_OK
) {
2916 uint32_t offset
= 0;
2917 uint32_t length
= buf_cnt
;
2919 /* DANGER!!! beware of unsigned comparision here!!! */
2921 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
2922 (image
.sections
[i
].base_address
< max_address
)) {
2924 if (image
.sections
[i
].base_address
< min_address
) {
2925 /* clip addresses below */
2926 offset
+= min_address
-image
.sections
[i
].base_address
;
2930 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2931 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2933 retval
= target_write_buffer(target
,
2934 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
2935 if (retval
!= ERROR_OK
) {
2939 image_size
+= length
;
2940 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
2941 (unsigned int)length
,
2942 image
.sections
[i
].base_address
+ offset
);
2948 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
2949 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
2950 "in %fs (%0.3f KiB/s)", image_size
,
2951 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2954 image_close(&image
);
2960 COMMAND_HANDLER(handle_dump_image_command
)
2962 struct fileio fileio
;
2964 int retval
, retvaltemp
;
2965 uint32_t address
, size
;
2966 struct duration bench
;
2967 struct target
*target
= get_current_target(CMD_CTX
);
2970 return ERROR_COMMAND_SYNTAX_ERROR
;
2972 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
2973 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
2975 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
2976 buffer
= malloc(buf_size
);
2980 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
2981 if (retval
!= ERROR_OK
) {
2986 duration_start(&bench
);
2989 size_t size_written
;
2990 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
2991 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2992 if (retval
!= ERROR_OK
)
2995 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2996 if (retval
!= ERROR_OK
)
2999 size
-= this_run_size
;
3000 address
+= this_run_size
;
3005 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3007 retval
= fileio_size(&fileio
, &filesize
);
3008 if (retval
!= ERROR_OK
)
3010 command_print(CMD_CTX
,
3011 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize
,
3012 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3015 retvaltemp
= fileio_close(&fileio
);
3016 if (retvaltemp
!= ERROR_OK
)
3022 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
3026 uint32_t image_size
;
3029 uint32_t checksum
= 0;
3030 uint32_t mem_checksum
= 0;
3034 struct target
*target
= get_current_target(CMD_CTX
);
3037 return ERROR_COMMAND_SYNTAX_ERROR
;
3040 LOG_ERROR("no target selected");
3044 struct duration bench
;
3045 duration_start(&bench
);
3047 if (CMD_ARGC
>= 2) {
3049 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
3050 image
.base_address
= addr
;
3051 image
.base_address_set
= 1;
3053 image
.base_address_set
= 0;
3054 image
.base_address
= 0x0;
3057 image
.start_address_set
= 0;
3059 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3060 if (retval
!= ERROR_OK
)
3066 for (i
= 0; i
< image
.num_sections
; i
++) {
3067 buffer
= malloc(image
.sections
[i
].size
);
3068 if (buffer
== NULL
) {
3069 command_print(CMD_CTX
,
3070 "error allocating buffer for section (%d bytes)",
3071 (int)(image
.sections
[i
].size
));
3074 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3075 if (retval
!= ERROR_OK
) {
3081 /* calculate checksum of image */
3082 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3083 if (retval
!= ERROR_OK
) {
3088 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3089 if (retval
!= ERROR_OK
) {
3094 if (checksum
!= mem_checksum
) {
3095 /* failed crc checksum, fall back to a binary compare */
3099 LOG_ERROR("checksum mismatch - attempting binary compare");
3101 data
= (uint8_t *)malloc(buf_cnt
);
3103 /* Can we use 32bit word accesses? */
3105 int count
= buf_cnt
;
3106 if ((count
% 4) == 0) {
3110 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
3111 if (retval
== ERROR_OK
) {
3113 for (t
= 0; t
< buf_cnt
; t
++) {
3114 if (data
[t
] != buffer
[t
]) {
3115 command_print(CMD_CTX
,
3116 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3118 (unsigned)(t
+ image
.sections
[i
].base_address
),
3121 if (diffs
++ >= 127) {
3122 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
3134 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
3135 image
.sections
[i
].base_address
,
3140 image_size
+= buf_cnt
;
3143 command_print(CMD_CTX
, "No more differences found.");
3146 retval
= ERROR_FAIL
;
3147 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3148 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3149 "in %fs (%0.3f KiB/s)", image_size
,
3150 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3153 image_close(&image
);
3158 COMMAND_HANDLER(handle_verify_image_command
)
3160 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
3163 COMMAND_HANDLER(handle_test_image_command
)
3165 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
3168 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
3170 struct target
*target
= get_current_target(cmd_ctx
);
3171 struct breakpoint
*breakpoint
= target
->breakpoints
;
3172 while (breakpoint
) {
3173 if (breakpoint
->type
== BKPT_SOFT
) {
3174 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3175 breakpoint
->length
, 16);
3176 command_print(cmd_ctx
, "IVA breakpoint: 0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
3177 breakpoint
->address
,
3179 breakpoint
->set
, buf
);
3182 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3183 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3185 breakpoint
->length
, breakpoint
->set
);
3186 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3187 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3188 breakpoint
->address
,
3189 breakpoint
->length
, breakpoint
->set
);
3190 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3193 command_print(cmd_ctx
, "Breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3194 breakpoint
->address
,
3195 breakpoint
->length
, breakpoint
->set
);
3198 breakpoint
= breakpoint
->next
;
3203 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
3204 uint32_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3206 struct target
*target
= get_current_target(cmd_ctx
);
3209 int retval
= breakpoint_add(target
, addr
, length
, hw
);
3210 if (ERROR_OK
== retval
)
3211 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
3213 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3216 } else if (addr
== 0) {
3217 int retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3218 if (ERROR_OK
== retval
)
3219 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3221 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3225 int retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3226 if (ERROR_OK
== retval
)
3227 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3229 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3236 COMMAND_HANDLER(handle_bp_command
)
3245 return handle_bp_command_list(CMD_CTX
);
3249 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3250 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3251 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3254 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
3256 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3258 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3261 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3262 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
3264 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
3265 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3267 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3272 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3273 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
3274 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
3275 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3278 return ERROR_COMMAND_SYNTAX_ERROR
;
3282 COMMAND_HANDLER(handle_rbp_command
)
3285 return ERROR_COMMAND_SYNTAX_ERROR
;
3288 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3290 struct target
*target
= get_current_target(CMD_CTX
);
3291 breakpoint_remove(target
, addr
);
3296 COMMAND_HANDLER(handle_wp_command
)
3298 struct target
*target
= get_current_target(CMD_CTX
);
3300 if (CMD_ARGC
== 0) {
3301 struct watchpoint
*watchpoint
= target
->watchpoints
;
3303 while (watchpoint
) {
3304 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
3305 ", len: 0x%8.8" PRIx32
3306 ", r/w/a: %i, value: 0x%8.8" PRIx32
3307 ", mask: 0x%8.8" PRIx32
,
3308 watchpoint
->address
,
3310 (int)watchpoint
->rw
,
3313 watchpoint
= watchpoint
->next
;
3318 enum watchpoint_rw type
= WPT_ACCESS
;
3320 uint32_t length
= 0;
3321 uint32_t data_value
= 0x0;
3322 uint32_t data_mask
= 0xffffffff;
3326 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
3329 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3332 switch (CMD_ARGV
[2][0]) {
3343 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3344 return ERROR_COMMAND_SYNTAX_ERROR
;
3348 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3349 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3353 return ERROR_COMMAND_SYNTAX_ERROR
;
3356 int retval
= watchpoint_add(target
, addr
, length
, type
,
3357 data_value
, data_mask
);
3358 if (ERROR_OK
!= retval
)
3359 LOG_ERROR("Failure setting watchpoints");
3364 COMMAND_HANDLER(handle_rwp_command
)
3367 return ERROR_COMMAND_SYNTAX_ERROR
;
3370 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3372 struct target
*target
= get_current_target(CMD_CTX
);
3373 watchpoint_remove(target
, addr
);
3379 * Translate a virtual address to a physical address.
3381 * The low-level target implementation must have logged a detailed error
3382 * which is forwarded to telnet/GDB session.
3384 COMMAND_HANDLER(handle_virt2phys_command
)
3387 return ERROR_COMMAND_SYNTAX_ERROR
;
3390 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
3393 struct target
*target
= get_current_target(CMD_CTX
);
3394 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3395 if (retval
== ERROR_OK
)
3396 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
3401 static void writeData(FILE *f
, const void *data
, size_t len
)
3403 size_t written
= fwrite(data
, 1, len
, f
);
3405 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3408 static void writeLong(FILE *f
, int l
)
3411 for (i
= 0; i
< 4; i
++) {
3412 char c
= (l
>> (i
*8))&0xff;
3413 writeData(f
, &c
, 1);
3418 static void writeString(FILE *f
, char *s
)
3420 writeData(f
, s
, strlen(s
));
3423 typedef unsigned char UNIT
[2]; /* unit of profiling */
3425 /* Dump a gmon.out histogram file. */
3426 static void write_gmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
,
3427 bool with_range
, uint32_t start_address
, uint32_t end_address
)
3430 FILE *f
= fopen(filename
, "w");
3433 writeString(f
, "gmon");
3434 writeLong(f
, 0x00000001); /* Version */
3435 writeLong(f
, 0); /* padding */
3436 writeLong(f
, 0); /* padding */
3437 writeLong(f
, 0); /* padding */
3439 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3440 writeData(f
, &zero
, 1);
3442 /* figure out bucket size */
3446 min
= start_address
;
3451 for (i
= 0; i
< sampleNum
; i
++) {
3452 if (min
> samples
[i
])
3454 if (max
< samples
[i
])
3458 /* max should be (largest sample + 1)
3459 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
3463 int addressSpace
= max
- min
;
3464 assert(addressSpace
>= 2);
3466 /* FIXME: What is the reasonable number of buckets?
3467 * The profiling result will be more accurate if there are enough buckets. */
3468 static const uint32_t maxBuckets
= 128 * 1024; /* maximum buckets. */
3469 uint32_t numBuckets
= addressSpace
/ sizeof(UNIT
);
3470 if (numBuckets
> maxBuckets
)
3471 numBuckets
= maxBuckets
;
3472 int *buckets
= malloc(sizeof(int) * numBuckets
);
3473 if (buckets
== NULL
) {
3477 memset(buckets
, 0, sizeof(int) * numBuckets
);
3478 for (i
= 0; i
< sampleNum
; i
++) {
3479 uint32_t address
= samples
[i
];
3481 if ((address
< min
) || (max
<= address
))
3484 long long a
= address
- min
;
3485 long long b
= numBuckets
;
3486 long long c
= addressSpace
;
3487 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
3491 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3492 writeLong(f
, min
); /* low_pc */
3493 writeLong(f
, max
); /* high_pc */
3494 writeLong(f
, numBuckets
); /* # of buckets */
3495 writeLong(f
, 100); /* KLUDGE! We lie, ca. 100Hz best case. */
3496 writeString(f
, "seconds");
3497 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3498 writeData(f
, &zero
, 1);
3499 writeString(f
, "s");
3501 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3503 char *data
= malloc(2 * numBuckets
);
3505 for (i
= 0; i
< numBuckets
; i
++) {
3510 data
[i
* 2] = val
&0xff;
3511 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
3514 writeData(f
, data
, numBuckets
* 2);
3522 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3523 * which will be used as a random sampling of PC */
3524 COMMAND_HANDLER(handle_profile_command
)
3526 struct target
*target
= get_current_target(CMD_CTX
);
3528 if ((CMD_ARGC
!= 2) && (CMD_ARGC
!= 4))
3529 return ERROR_COMMAND_SYNTAX_ERROR
;
3531 const uint32_t MAX_PROFILE_SAMPLE_NUM
= 10000;
3533 uint32_t num_of_sampels
;
3534 int retval
= ERROR_OK
;
3535 uint32_t *samples
= malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM
);
3536 if (samples
== NULL
) {
3537 LOG_ERROR("No memory to store samples.");
3541 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], offset
);
3544 * Some cores let us sample the PC without the
3545 * annoying halt/resume step; for example, ARMv7 PCSR.
3546 * Provide a way to use that more efficient mechanism.
3548 retval
= target_profiling(target
, samples
, MAX_PROFILE_SAMPLE_NUM
,
3549 &num_of_sampels
, offset
);
3550 if (retval
!= ERROR_OK
) {
3555 assert(num_of_sampels
<= MAX_PROFILE_SAMPLE_NUM
);
3557 retval
= target_poll(target
);
3558 if (retval
!= ERROR_OK
) {
3562 if (target
->state
== TARGET_RUNNING
) {
3563 retval
= target_halt(target
);
3564 if (retval
!= ERROR_OK
) {
3570 retval
= target_poll(target
);
3571 if (retval
!= ERROR_OK
) {
3576 uint32_t start_address
= 0;
3577 uint32_t end_address
= 0;
3578 bool with_range
= false;
3579 if (CMD_ARGC
== 4) {
3581 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], start_address
);
3582 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[3], end_address
);
3585 write_gmon(samples
, num_of_sampels
, CMD_ARGV
[1],
3586 with_range
, start_address
, end_address
);
3587 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3593 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
3596 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3599 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3603 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3604 valObjPtr
= Jim_NewIntObj(interp
, val
);
3605 if (!nameObjPtr
|| !valObjPtr
) {
3610 Jim_IncrRefCount(nameObjPtr
);
3611 Jim_IncrRefCount(valObjPtr
);
3612 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3613 Jim_DecrRefCount(interp
, nameObjPtr
);
3614 Jim_DecrRefCount(interp
, valObjPtr
);
3616 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3620 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3622 struct command_context
*context
;
3623 struct target
*target
;
3625 context
= current_command_context(interp
);
3626 assert(context
!= NULL
);
3628 target
= get_current_target(context
);
3629 if (target
== NULL
) {
3630 LOG_ERROR("mem2array: no current target");
3634 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
3637 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3645 const char *varname
;
3649 /* argv[1] = name of array to receive the data
3650 * argv[2] = desired width
3651 * argv[3] = memory address
3652 * argv[4] = count of times to read
3655 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3658 varname
= Jim_GetString(argv
[0], &len
);
3659 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3661 e
= Jim_GetLong(interp
, argv
[1], &l
);
3666 e
= Jim_GetLong(interp
, argv
[2], &l
);
3670 e
= Jim_GetLong(interp
, argv
[3], &l
);
3685 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3686 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3690 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3691 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3694 if ((addr
+ (len
* width
)) < addr
) {
3695 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3696 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3699 /* absurd transfer size? */
3701 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3702 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3707 ((width
== 2) && ((addr
& 1) == 0)) ||
3708 ((width
== 4) && ((addr
& 3) == 0))) {
3712 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3713 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3716 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3725 size_t buffersize
= 4096;
3726 uint8_t *buffer
= malloc(buffersize
);
3733 /* Slurp... in buffer size chunks */
3735 count
= len
; /* in objects.. */
3736 if (count
> (buffersize
/ width
))
3737 count
= (buffersize
/ width
);
3739 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3740 if (retval
!= ERROR_OK
) {
3742 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3746 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3747 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3751 v
= 0; /* shut up gcc */
3752 for (i
= 0; i
< count
; i
++, n
++) {
3755 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3758 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3761 v
= buffer
[i
] & 0x0ff;
3764 new_int_array_element(interp
, varname
, n
, v
);
3772 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3777 static int get_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t *val
)
3780 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3784 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3788 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3794 Jim_IncrRefCount(nameObjPtr
);
3795 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3796 Jim_DecrRefCount(interp
, nameObjPtr
);
3798 if (valObjPtr
== NULL
)
3801 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3802 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3807 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3809 struct command_context
*context
;
3810 struct target
*target
;
3812 context
= current_command_context(interp
);
3813 assert(context
!= NULL
);
3815 target
= get_current_target(context
);
3816 if (target
== NULL
) {
3817 LOG_ERROR("array2mem: no current target");
3821 return target_array2mem(interp
, target
, argc
-1, argv
+ 1);
3824 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
3825 int argc
, Jim_Obj
*const *argv
)
3833 const char *varname
;
3837 /* argv[1] = name of array to get the data
3838 * argv[2] = desired width
3839 * argv[3] = memory address
3840 * argv[4] = count to write
3843 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
3846 varname
= Jim_GetString(argv
[0], &len
);
3847 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3849 e
= Jim_GetLong(interp
, argv
[1], &l
);
3854 e
= Jim_GetLong(interp
, argv
[2], &l
);
3858 e
= Jim_GetLong(interp
, argv
[3], &l
);
3873 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3874 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3875 "Invalid width param, must be 8/16/32", NULL
);
3879 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3880 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3881 "array2mem: zero width read?", NULL
);
3884 if ((addr
+ (len
* width
)) < addr
) {
3885 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3886 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3887 "array2mem: addr + len - wraps to zero?", NULL
);
3890 /* absurd transfer size? */
3892 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3893 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3894 "array2mem: absurd > 64K item request", NULL
);
3899 ((width
== 2) && ((addr
& 1) == 0)) ||
3900 ((width
== 4) && ((addr
& 3) == 0))) {
3904 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3905 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3908 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3919 size_t buffersize
= 4096;
3920 uint8_t *buffer
= malloc(buffersize
);
3925 /* Slurp... in buffer size chunks */
3927 count
= len
; /* in objects.. */
3928 if (count
> (buffersize
/ width
))
3929 count
= (buffersize
/ width
);
3931 v
= 0; /* shut up gcc */
3932 for (i
= 0; i
< count
; i
++, n
++) {
3933 get_int_array_element(interp
, varname
, n
, &v
);
3936 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
3939 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
3942 buffer
[i
] = v
& 0x0ff;
3948 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3949 if (retval
!= ERROR_OK
) {
3951 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3955 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3956 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3964 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3969 /* FIX? should we propagate errors here rather than printing them
3972 void target_handle_event(struct target
*target
, enum target_event e
)
3974 struct target_event_action
*teap
;
3976 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3977 if (teap
->event
== e
) {
3978 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3979 target
->target_number
,
3980 target_name(target
),
3981 target_type_name(target
),
3983 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3984 Jim_GetString(teap
->body
, NULL
));
3985 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
) {
3986 Jim_MakeErrorMessage(teap
->interp
);
3987 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
3994 * Returns true only if the target has a handler for the specified event.
3996 bool target_has_event_action(struct target
*target
, enum target_event event
)
3998 struct target_event_action
*teap
;
4000 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4001 if (teap
->event
== event
)
4007 enum target_cfg_param
{
4010 TCFG_WORK_AREA_VIRT
,
4011 TCFG_WORK_AREA_PHYS
,
4012 TCFG_WORK_AREA_SIZE
,
4013 TCFG_WORK_AREA_BACKUP
,
4017 TCFG_CHAIN_POSITION
,
4022 static Jim_Nvp nvp_config_opts
[] = {
4023 { .name
= "-type", .value
= TCFG_TYPE
},
4024 { .name
= "-event", .value
= TCFG_EVENT
},
4025 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
4026 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
4027 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
4028 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
4029 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
4030 { .name
= "-variant", .value
= TCFG_VARIANT
},
4031 { .name
= "-coreid", .value
= TCFG_COREID
},
4032 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
4033 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
4034 { .name
= "-rtos", .value
= TCFG_RTOS
},
4035 { .name
= NULL
, .value
= -1 }
4038 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
4046 /* parse config or cget options ... */
4047 while (goi
->argc
> 0) {
4048 Jim_SetEmptyResult(goi
->interp
);
4049 /* Jim_GetOpt_Debug(goi); */
4051 if (target
->type
->target_jim_configure
) {
4052 /* target defines a configure function */
4053 /* target gets first dibs on parameters */
4054 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
4063 /* otherwise we 'continue' below */
4065 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
4067 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
4073 if (goi
->isconfigure
) {
4074 Jim_SetResultFormatted(goi
->interp
,
4075 "not settable: %s", n
->name
);
4079 if (goi
->argc
!= 0) {
4080 Jim_WrongNumArgs(goi
->interp
,
4081 goi
->argc
, goi
->argv
,
4086 Jim_SetResultString(goi
->interp
,
4087 target_type_name(target
), -1);
4091 if (goi
->argc
== 0) {
4092 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
4096 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
4098 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
4102 if (goi
->isconfigure
) {
4103 if (goi
->argc
!= 1) {
4104 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
4108 if (goi
->argc
!= 0) {
4109 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
4115 struct target_event_action
*teap
;
4117 teap
= target
->event_action
;
4118 /* replace existing? */
4120 if (teap
->event
== (enum target_event
)n
->value
)
4125 if (goi
->isconfigure
) {
4126 bool replace
= true;
4129 teap
= calloc(1, sizeof(*teap
));
4132 teap
->event
= n
->value
;
4133 teap
->interp
= goi
->interp
;
4134 Jim_GetOpt_Obj(goi
, &o
);
4136 Jim_DecrRefCount(teap
->interp
, teap
->body
);
4137 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
4140 * Tcl/TK - "tk events" have a nice feature.
4141 * See the "BIND" command.
4142 * We should support that here.
4143 * You can specify %X and %Y in the event code.
4144 * The idea is: %T - target name.
4145 * The idea is: %N - target number
4146 * The idea is: %E - event name.
4148 Jim_IncrRefCount(teap
->body
);
4151 /* add to head of event list */
4152 teap
->next
= target
->event_action
;
4153 target
->event_action
= teap
;
4155 Jim_SetEmptyResult(goi
->interp
);
4159 Jim_SetEmptyResult(goi
->interp
);
4161 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
4167 case TCFG_WORK_AREA_VIRT
:
4168 if (goi
->isconfigure
) {
4169 target_free_all_working_areas(target
);
4170 e
= Jim_GetOpt_Wide(goi
, &w
);
4173 target
->working_area_virt
= w
;
4174 target
->working_area_virt_spec
= true;
4179 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
4183 case TCFG_WORK_AREA_PHYS
:
4184 if (goi
->isconfigure
) {
4185 target_free_all_working_areas(target
);
4186 e
= Jim_GetOpt_Wide(goi
, &w
);
4189 target
->working_area_phys
= w
;
4190 target
->working_area_phys_spec
= true;
4195 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
4199 case TCFG_WORK_AREA_SIZE
:
4200 if (goi
->isconfigure
) {
4201 target_free_all_working_areas(target
);
4202 e
= Jim_GetOpt_Wide(goi
, &w
);
4205 target
->working_area_size
= w
;
4210 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4214 case TCFG_WORK_AREA_BACKUP
:
4215 if (goi
->isconfigure
) {
4216 target_free_all_working_areas(target
);
4217 e
= Jim_GetOpt_Wide(goi
, &w
);
4220 /* make this exactly 1 or 0 */
4221 target
->backup_working_area
= (!!w
);
4226 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
4227 /* loop for more e*/
4232 if (goi
->isconfigure
) {
4233 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
4235 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
4238 target
->endianness
= n
->value
;
4243 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4244 if (n
->name
== NULL
) {
4245 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4246 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4248 Jim_SetResultString(goi
->interp
, n
->name
, -1);
4253 if (goi
->isconfigure
) {
4254 if (goi
->argc
< 1) {
4255 Jim_SetResultFormatted(goi
->interp
,
4260 if (target
->variant
)
4261 free((void *)(target
->variant
));
4262 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
4265 target
->variant
= strdup(cp
);
4270 Jim_SetResultString(goi
->interp
, target
->variant
, -1);
4275 if (goi
->isconfigure
) {
4276 e
= Jim_GetOpt_Wide(goi
, &w
);
4279 target
->coreid
= (int32_t)w
;
4284 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4288 case TCFG_CHAIN_POSITION
:
4289 if (goi
->isconfigure
) {
4291 struct jtag_tap
*tap
;
4292 target_free_all_working_areas(target
);
4293 e
= Jim_GetOpt_Obj(goi
, &o_t
);
4296 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
4299 /* make this exactly 1 or 0 */
4305 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
4306 /* loop for more e*/
4309 if (goi
->isconfigure
) {
4310 e
= Jim_GetOpt_Wide(goi
, &w
);
4313 target
->dbgbase
= (uint32_t)w
;
4314 target
->dbgbase_set
= true;
4319 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
4326 int result
= rtos_create(goi
, target
);
4327 if (result
!= JIM_OK
)
4333 } /* while (goi->argc) */
4336 /* done - we return */
4340 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4344 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4345 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4346 int need_args
= 1 + goi
.isconfigure
;
4347 if (goi
.argc
< need_args
) {
4348 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4350 ? "missing: -option VALUE ..."
4351 : "missing: -option ...");
4354 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4355 return target_configure(&goi
, target
);
4358 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4360 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4363 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4365 if (goi
.argc
< 2 || goi
.argc
> 4) {
4366 Jim_SetResultFormatted(goi
.interp
,
4367 "usage: %s [phys] <address> <data> [<count>]", cmd_name
);
4372 fn
= target_write_memory
;
4375 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4377 struct Jim_Obj
*obj
;
4378 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4382 fn
= target_write_phys_memory
;
4386 e
= Jim_GetOpt_Wide(&goi
, &a
);
4391 e
= Jim_GetOpt_Wide(&goi
, &b
);
4396 if (goi
.argc
== 1) {
4397 e
= Jim_GetOpt_Wide(&goi
, &c
);
4402 /* all args must be consumed */
4406 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4408 if (strcasecmp(cmd_name
, "mww") == 0)
4410 else if (strcasecmp(cmd_name
, "mwh") == 0)
4412 else if (strcasecmp(cmd_name
, "mwb") == 0)
4415 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4419 return (target_fill_mem(target
, a
, fn
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4423 * @brief Reads an array of words/halfwords/bytes from target memory starting at specified address.
4425 * Usage: mdw [phys] <address> [<count>] - for 32 bit reads
4426 * mdh [phys] <address> [<count>] - for 16 bit reads
4427 * mdb [phys] <address> [<count>] - for 8 bit reads
4429 * Count defaults to 1.
4431 * Calls target_read_memory or target_read_phys_memory depending on
4432 * the presence of the "phys" argument
4433 * Reads the target memory in blocks of max. 32 bytes, and returns an array of ints formatted
4434 * to int representation in base16.
4435 * Also outputs read data in a human readable form using command_print
4437 * @param phys if present target_read_phys_memory will be used instead of target_read_memory
4438 * @param address address where to start the read. May be specified in decimal or hex using the standard "0x" prefix
4439 * @param count optional count parameter to read an array of values. If not specified, defaults to 1.
4440 * @returns: JIM_ERR on error or JIM_OK on success and sets the result string to an array of ascii formatted numbers
4441 * on success, with [<count>] number of elements.
4443 * In case of little endian target:
4444 * Example1: "mdw 0x00000000" returns "10123456"
4445 * Exmaple2: "mdh 0x00000000 1" returns "3456"
4446 * Example3: "mdb 0x00000000" returns "56"
4447 * Example4: "mdh 0x00000000 2" returns "3456 1012"
4448 * Example5: "mdb 0x00000000 3" returns "56 34 12"
4450 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4452 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4455 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4457 if ((goi
.argc
< 1) || (goi
.argc
> 3)) {
4458 Jim_SetResultFormatted(goi
.interp
,
4459 "usage: %s [phys] <address> [<count>]", cmd_name
);
4463 int (*fn
)(struct target
*target
,
4464 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
4465 fn
= target_read_memory
;
4468 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4470 struct Jim_Obj
*obj
;
4471 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4475 fn
= target_read_phys_memory
;
4478 /* Read address parameter */
4480 e
= Jim_GetOpt_Wide(&goi
, &addr
);
4484 /* If next parameter exists, read it out as the count parameter, if not, set it to 1 (default) */
4486 if (goi
.argc
== 1) {
4487 e
= Jim_GetOpt_Wide(&goi
, &count
);
4493 /* all args must be consumed */
4497 jim_wide dwidth
= 1; /* shut up gcc */
4498 if (strcasecmp(cmd_name
, "mdw") == 0)
4500 else if (strcasecmp(cmd_name
, "mdh") == 0)
4502 else if (strcasecmp(cmd_name
, "mdb") == 0)
4505 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4509 /* convert count to "bytes" */
4510 int bytes
= count
* dwidth
;
4512 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4513 uint8_t target_buf
[32];
4516 y
= (bytes
< 16) ? bytes
: 16; /* y = min(bytes, 16); */
4518 /* Try to read out next block */
4519 e
= fn(target
, addr
, dwidth
, y
/ dwidth
, target_buf
);
4521 if (e
!= ERROR_OK
) {
4522 Jim_SetResultFormatted(interp
, "error reading target @ 0x%08lx", (long)addr
);
4526 command_print_sameline(NULL
, "0x%08x ", (int)(addr
));
4529 for (x
= 0; x
< 16 && x
< y
; x
+= 4) {
4530 z
= target_buffer_get_u32(target
, &(target_buf
[x
]));
4531 command_print_sameline(NULL
, "%08x ", (int)(z
));
4533 for (; (x
< 16) ; x
+= 4)
4534 command_print_sameline(NULL
, " ");
4537 for (x
= 0; x
< 16 && x
< y
; x
+= 2) {
4538 z
= target_buffer_get_u16(target
, &(target_buf
[x
]));
4539 command_print_sameline(NULL
, "%04x ", (int)(z
));
4541 for (; (x
< 16) ; x
+= 2)
4542 command_print_sameline(NULL
, " ");
4546 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
4547 z
= target_buffer_get_u8(target
, &(target_buf
[x
]));
4548 command_print_sameline(NULL
, "%02x ", (int)(z
));
4550 for (; (x
< 16) ; x
+= 1)
4551 command_print_sameline(NULL
, " ");
4554 /* ascii-ify the bytes */
4555 for (x
= 0 ; x
< y
; x
++) {
4556 if ((target_buf
[x
] >= 0x20) &&
4557 (target_buf
[x
] <= 0x7e)) {
4561 target_buf
[x
] = '.';
4566 target_buf
[x
] = ' ';
4571 /* print - with a newline */
4572 command_print_sameline(NULL
, "%s\n", target_buf
);
4580 static int jim_target_mem2array(Jim_Interp
*interp
,
4581 int argc
, Jim_Obj
*const *argv
)
4583 struct target
*target
= Jim_CmdPrivData(interp
);
4584 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4587 static int jim_target_array2mem(Jim_Interp
*interp
,
4588 int argc
, Jim_Obj
*const *argv
)
4590 struct target
*target
= Jim_CmdPrivData(interp
);
4591 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4594 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4596 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
4600 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4603 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4606 struct target
*target
= Jim_CmdPrivData(interp
);
4607 if (!target
->tap
->enabled
)
4608 return jim_target_tap_disabled(interp
);
4610 int e
= target
->type
->examine(target
);
4616 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4619 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4622 struct target
*target
= Jim_CmdPrivData(interp
);
4624 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
4630 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4633 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4636 struct target
*target
= Jim_CmdPrivData(interp
);
4637 if (!target
->tap
->enabled
)
4638 return jim_target_tap_disabled(interp
);
4641 if (!(target_was_examined(target
)))
4642 e
= ERROR_TARGET_NOT_EXAMINED
;
4644 e
= target
->type
->poll(target
);
4650 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4653 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4655 if (goi
.argc
!= 2) {
4656 Jim_WrongNumArgs(interp
, 0, argv
,
4657 "([tT]|[fF]|assert|deassert) BOOL");
4662 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4664 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4667 /* the halt or not param */
4669 e
= Jim_GetOpt_Wide(&goi
, &a
);
4673 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4674 if (!target
->tap
->enabled
)
4675 return jim_target_tap_disabled(interp
);
4676 if (!(target_was_examined(target
))) {
4677 LOG_ERROR("Target not examined yet");
4678 return ERROR_TARGET_NOT_EXAMINED
;
4680 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
4681 Jim_SetResultFormatted(interp
,
4682 "No target-specific reset for %s",
4683 target_name(target
));
4686 /* determine if we should halt or not. */
4687 target
->reset_halt
= !!a
;
4688 /* When this happens - all workareas are invalid. */
4689 target_free_all_working_areas_restore(target
, 0);
4692 if (n
->value
== NVP_ASSERT
)
4693 e
= target
->type
->assert_reset(target
);
4695 e
= target
->type
->deassert_reset(target
);
4696 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4699 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4702 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4705 struct target
*target
= Jim_CmdPrivData(interp
);
4706 if (!target
->tap
->enabled
)
4707 return jim_target_tap_disabled(interp
);
4708 int e
= target
->type
->halt(target
);
4709 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4712 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4715 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4717 /* params: <name> statename timeoutmsecs */
4718 if (goi
.argc
!= 2) {
4719 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4720 Jim_SetResultFormatted(goi
.interp
,
4721 "%s <state_name> <timeout_in_msec>", cmd_name
);
4726 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4728 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
, 1);
4732 e
= Jim_GetOpt_Wide(&goi
, &a
);
4735 struct target
*target
= Jim_CmdPrivData(interp
);
4736 if (!target
->tap
->enabled
)
4737 return jim_target_tap_disabled(interp
);
4739 e
= target_wait_state(target
, n
->value
, a
);
4740 if (e
!= ERROR_OK
) {
4741 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
4742 Jim_SetResultFormatted(goi
.interp
,
4743 "target: %s wait %s fails (%#s) %s",
4744 target_name(target
), n
->name
,
4745 eObj
, target_strerror_safe(e
));
4746 Jim_FreeNewObj(interp
, eObj
);
4751 /* List for human, Events defined for this target.
4752 * scripts/programs should use 'name cget -event NAME'
4754 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4756 struct command_context
*cmd_ctx
= current_command_context(interp
);
4757 assert(cmd_ctx
!= NULL
);
4759 struct target
*target
= Jim_CmdPrivData(interp
);
4760 struct target_event_action
*teap
= target
->event_action
;
4761 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4762 target
->target_number
,
4763 target_name(target
));
4764 command_print(cmd_ctx
, "%-25s | Body", "Event");
4765 command_print(cmd_ctx
, "------------------------- | "
4766 "----------------------------------------");
4768 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
4769 command_print(cmd_ctx
, "%-25s | %s",
4770 opt
->name
, Jim_GetString(teap
->body
, NULL
));
4773 command_print(cmd_ctx
, "***END***");
4776 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4779 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4782 struct target
*target
= Jim_CmdPrivData(interp
);
4783 Jim_SetResultString(interp
, target_state_name(target
), -1);
4786 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4789 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4790 if (goi
.argc
!= 1) {
4791 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4792 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
4796 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4798 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4801 struct target
*target
= Jim_CmdPrivData(interp
);
4802 target_handle_event(target
, n
->value
);
4806 static const struct command_registration target_instance_command_handlers
[] = {
4808 .name
= "configure",
4809 .mode
= COMMAND_CONFIG
,
4810 .jim_handler
= jim_target_configure
,
4811 .help
= "configure a new target for use",
4812 .usage
= "[target_attribute ...]",
4816 .mode
= COMMAND_ANY
,
4817 .jim_handler
= jim_target_configure
,
4818 .help
= "returns the specified target attribute",
4819 .usage
= "target_attribute",
4823 .mode
= COMMAND_EXEC
,
4824 .jim_handler
= jim_target_mw
,
4825 .help
= "Write 32-bit word(s) to target memory",
4826 .usage
= "address data [count]",
4830 .mode
= COMMAND_EXEC
,
4831 .jim_handler
= jim_target_mw
,
4832 .help
= "Write 16-bit half-word(s) to target memory",
4833 .usage
= "address data [count]",
4837 .mode
= COMMAND_EXEC
,
4838 .jim_handler
= jim_target_mw
,
4839 .help
= "Write byte(s) to target memory",
4840 .usage
= "address data [count]",
4844 .mode
= COMMAND_EXEC
,
4845 .jim_handler
= jim_target_md
,
4846 .help
= "Display target memory as 32-bit words",
4847 .usage
= "address [count]",
4851 .mode
= COMMAND_EXEC
,
4852 .jim_handler
= jim_target_md
,
4853 .help
= "Display target memory as 16-bit half-words",
4854 .usage
= "address [count]",
4858 .mode
= COMMAND_EXEC
,
4859 .jim_handler
= jim_target_md
,
4860 .help
= "Display target memory as 8-bit bytes",
4861 .usage
= "address [count]",
4864 .name
= "array2mem",
4865 .mode
= COMMAND_EXEC
,
4866 .jim_handler
= jim_target_array2mem
,
4867 .help
= "Writes Tcl array of 8/16/32 bit numbers "
4869 .usage
= "arrayname bitwidth address count",
4872 .name
= "mem2array",
4873 .mode
= COMMAND_EXEC
,
4874 .jim_handler
= jim_target_mem2array
,
4875 .help
= "Loads Tcl array of 8/16/32 bit numbers "
4876 "from target memory",
4877 .usage
= "arrayname bitwidth address count",
4880 .name
= "eventlist",
4881 .mode
= COMMAND_EXEC
,
4882 .jim_handler
= jim_target_event_list
,
4883 .help
= "displays a table of events defined for this target",
4887 .mode
= COMMAND_EXEC
,
4888 .jim_handler
= jim_target_current_state
,
4889 .help
= "displays the current state of this target",
4892 .name
= "arp_examine",
4893 .mode
= COMMAND_EXEC
,
4894 .jim_handler
= jim_target_examine
,
4895 .help
= "used internally for reset processing",
4898 .name
= "arp_halt_gdb",
4899 .mode
= COMMAND_EXEC
,
4900 .jim_handler
= jim_target_halt_gdb
,
4901 .help
= "used internally for reset processing to halt GDB",
4905 .mode
= COMMAND_EXEC
,
4906 .jim_handler
= jim_target_poll
,
4907 .help
= "used internally for reset processing",
4910 .name
= "arp_reset",
4911 .mode
= COMMAND_EXEC
,
4912 .jim_handler
= jim_target_reset
,
4913 .help
= "used internally for reset processing",
4917 .mode
= COMMAND_EXEC
,
4918 .jim_handler
= jim_target_halt
,
4919 .help
= "used internally for reset processing",
4922 .name
= "arp_waitstate",
4923 .mode
= COMMAND_EXEC
,
4924 .jim_handler
= jim_target_wait_state
,
4925 .help
= "used internally for reset processing",
4928 .name
= "invoke-event",
4929 .mode
= COMMAND_EXEC
,
4930 .jim_handler
= jim_target_invoke_event
,
4931 .help
= "invoke handler for specified event",
4932 .usage
= "event_name",
4934 COMMAND_REGISTRATION_DONE
4937 static int target_create(Jim_GetOptInfo
*goi
)
4945 struct target
*target
;
4946 struct command_context
*cmd_ctx
;
4948 cmd_ctx
= current_command_context(goi
->interp
);
4949 assert(cmd_ctx
!= NULL
);
4951 if (goi
->argc
< 3) {
4952 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
4957 Jim_GetOpt_Obj(goi
, &new_cmd
);
4958 /* does this command exist? */
4959 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
4961 cp
= Jim_GetString(new_cmd
, NULL
);
4962 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
4967 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
4971 /* now does target type exist */
4972 for (x
= 0 ; target_types
[x
] ; x
++) {
4973 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
4978 /* check for deprecated name */
4979 if (target_types
[x
]->deprecated_name
) {
4980 if (0 == strcmp(cp
, target_types
[x
]->deprecated_name
)) {
4982 LOG_WARNING("target name is deprecated use: \'%s\'", target_types
[x
]->name
);
4987 if (target_types
[x
] == NULL
) {
4988 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
4989 for (x
= 0 ; target_types
[x
] ; x
++) {
4990 if (target_types
[x
+ 1]) {
4991 Jim_AppendStrings(goi
->interp
,
4992 Jim_GetResult(goi
->interp
),
4993 target_types
[x
]->name
,
4996 Jim_AppendStrings(goi
->interp
,
4997 Jim_GetResult(goi
->interp
),
4999 target_types
[x
]->name
, NULL
);
5006 target
= calloc(1, sizeof(struct target
));
5007 /* set target number */
5008 target
->target_number
= new_target_number();
5010 /* allocate memory for each unique target type */
5011 target
->type
= (struct target_type
*)calloc(1, sizeof(struct target_type
));
5013 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
5015 /* will be set by "-endian" */
5016 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5018 /* default to first core, override with -coreid */
5021 target
->working_area
= 0x0;
5022 target
->working_area_size
= 0x0;
5023 target
->working_areas
= NULL
;
5024 target
->backup_working_area
= 0;
5026 target
->state
= TARGET_UNKNOWN
;
5027 target
->debug_reason
= DBG_REASON_UNDEFINED
;
5028 target
->reg_cache
= NULL
;
5029 target
->breakpoints
= NULL
;
5030 target
->watchpoints
= NULL
;
5031 target
->next
= NULL
;
5032 target
->arch_info
= NULL
;
5034 target
->display
= 1;
5036 target
->halt_issued
= false;
5038 /* initialize trace information */
5039 target
->trace_info
= malloc(sizeof(struct trace
));
5040 target
->trace_info
->num_trace_points
= 0;
5041 target
->trace_info
->trace_points_size
= 0;
5042 target
->trace_info
->trace_points
= NULL
;
5043 target
->trace_info
->trace_history_size
= 0;
5044 target
->trace_info
->trace_history
= NULL
;
5045 target
->trace_info
->trace_history_pos
= 0;
5046 target
->trace_info
->trace_history_overflowed
= 0;
5048 target
->dbgmsg
= NULL
;
5049 target
->dbg_msg_enabled
= 0;
5051 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5053 target
->rtos
= NULL
;
5054 target
->rtos_auto_detect
= false;
5056 /* Do the rest as "configure" options */
5057 goi
->isconfigure
= 1;
5058 e
= target_configure(goi
, target
);
5060 if (target
->tap
== NULL
) {
5061 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
5071 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
5072 /* default endian to little if not specified */
5073 target
->endianness
= TARGET_LITTLE_ENDIAN
;
5076 /* incase variant is not set */
5077 if (!target
->variant
)
5078 target
->variant
= strdup("");
5080 cp
= Jim_GetString(new_cmd
, NULL
);
5081 target
->cmd_name
= strdup(cp
);
5083 /* create the target specific commands */
5084 if (target
->type
->commands
) {
5085 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
5087 LOG_ERROR("unable to register '%s' commands", cp
);
5089 if (target
->type
->target_create
)
5090 (*(target
->type
->target_create
))(target
, goi
->interp
);
5092 /* append to end of list */
5094 struct target
**tpp
;
5095 tpp
= &(all_targets
);
5097 tpp
= &((*tpp
)->next
);
5101 /* now - create the new target name command */
5102 const struct command_registration target_subcommands
[] = {
5104 .chain
= target_instance_command_handlers
,
5107 .chain
= target
->type
->commands
,
5109 COMMAND_REGISTRATION_DONE
5111 const struct command_registration target_commands
[] = {
5114 .mode
= COMMAND_ANY
,
5115 .help
= "target command group",
5117 .chain
= target_subcommands
,
5119 COMMAND_REGISTRATION_DONE
5121 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
5125 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
5127 command_set_handler_data(c
, target
);
5129 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
5132 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5135 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5138 struct command_context
*cmd_ctx
= current_command_context(interp
);
5139 assert(cmd_ctx
!= NULL
);
5141 Jim_SetResultString(interp
, target_name(get_current_target(cmd_ctx
)), -1);
5145 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5148 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5151 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5152 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++) {
5153 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5154 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
5159 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5162 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5165 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5166 struct target
*target
= all_targets
;
5168 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5169 Jim_NewStringObj(interp
, target_name(target
), -1));
5170 target
= target
->next
;
5175 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5178 const char *targetname
;
5180 struct target
*target
= (struct target
*) NULL
;
5181 struct target_list
*head
, *curr
, *new;
5182 curr
= (struct target_list
*) NULL
;
5183 head
= (struct target_list
*) NULL
;
5186 LOG_DEBUG("%d", argc
);
5187 /* argv[1] = target to associate in smp
5188 * argv[2] = target to assoicate in smp
5192 for (i
= 1; i
< argc
; i
++) {
5194 targetname
= Jim_GetString(argv
[i
], &len
);
5195 target
= get_target(targetname
);
5196 LOG_DEBUG("%s ", targetname
);
5198 new = malloc(sizeof(struct target_list
));
5199 new->target
= target
;
5200 new->next
= (struct target_list
*)NULL
;
5201 if (head
== (struct target_list
*)NULL
) {
5210 /* now parse the list of cpu and put the target in smp mode*/
5213 while (curr
!= (struct target_list
*)NULL
) {
5214 target
= curr
->target
;
5216 target
->head
= head
;
5220 if (target
&& target
->rtos
)
5221 retval
= rtos_smp_init(head
->target
);
5227 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5230 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5232 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5233 "<name> <target_type> [<target_options> ...]");
5236 return target_create(&goi
);
5239 static int jim_target_number(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5242 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5244 /* It's OK to remove this mechanism sometime after August 2010 or so */
5245 LOG_WARNING("don't use numbers as target identifiers; use names");
5246 if (goi
.argc
!= 1) {
5247 Jim_SetResultFormatted(goi
.interp
, "usage: target number <number>");
5251 int e
= Jim_GetOpt_Wide(&goi
, &w
);
5255 struct target
*target
;
5256 for (target
= all_targets
; NULL
!= target
; target
= target
->next
) {
5257 if (target
->target_number
!= w
)
5260 Jim_SetResultString(goi
.interp
, target_name(target
), -1);
5264 Jim_Obj
*wObj
= Jim_NewIntObj(goi
.interp
, w
);
5265 Jim_SetResultFormatted(goi
.interp
,
5266 "Target: number %#s does not exist", wObj
);
5267 Jim_FreeNewObj(interp
, wObj
);
5272 static int jim_target_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5275 Jim_WrongNumArgs(interp
, 1, argv
, "<no parameters>");
5279 struct target
*target
= all_targets
;
5280 while (NULL
!= target
) {
5281 target
= target
->next
;
5284 Jim_SetResult(interp
, Jim_NewIntObj(interp
, count
));
5288 static const struct command_registration target_subcommand_handlers
[] = {
5291 .mode
= COMMAND_CONFIG
,
5292 .handler
= handle_target_init_command
,
5293 .help
= "initialize targets",
5297 /* REVISIT this should be COMMAND_CONFIG ... */
5298 .mode
= COMMAND_ANY
,
5299 .jim_handler
= jim_target_create
,
5300 .usage
= "name type '-chain-position' name [options ...]",
5301 .help
= "Creates and selects a new target",
5305 .mode
= COMMAND_ANY
,
5306 .jim_handler
= jim_target_current
,
5307 .help
= "Returns the currently selected target",
5311 .mode
= COMMAND_ANY
,
5312 .jim_handler
= jim_target_types
,
5313 .help
= "Returns the available target types as "
5314 "a list of strings",
5318 .mode
= COMMAND_ANY
,
5319 .jim_handler
= jim_target_names
,
5320 .help
= "Returns the names of all targets as a list of strings",
5324 .mode
= COMMAND_ANY
,
5325 .jim_handler
= jim_target_number
,
5327 .help
= "Returns the name of the numbered target "
5332 .mode
= COMMAND_ANY
,
5333 .jim_handler
= jim_target_count
,
5334 .help
= "Returns the number of targets as an integer "
5339 .mode
= COMMAND_ANY
,
5340 .jim_handler
= jim_target_smp
,
5341 .usage
= "targetname1 targetname2 ...",
5342 .help
= "gather several target in a smp list"
5345 COMMAND_REGISTRATION_DONE
5355 static int fastload_num
;
5356 static struct FastLoad
*fastload
;
5358 static void free_fastload(void)
5360 if (fastload
!= NULL
) {
5362 for (i
= 0; i
< fastload_num
; i
++) {
5363 if (fastload
[i
].data
)
5364 free(fastload
[i
].data
);
5371 COMMAND_HANDLER(handle_fast_load_image_command
)
5375 uint32_t image_size
;
5376 uint32_t min_address
= 0;
5377 uint32_t max_address
= 0xffffffff;
5382 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
5383 &image
, &min_address
, &max_address
);
5384 if (ERROR_OK
!= retval
)
5387 struct duration bench
;
5388 duration_start(&bench
);
5390 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5391 if (retval
!= ERROR_OK
)
5396 fastload_num
= image
.num_sections
;
5397 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5398 if (fastload
== NULL
) {
5399 command_print(CMD_CTX
, "out of memory");
5400 image_close(&image
);
5403 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5404 for (i
= 0; i
< image
.num_sections
; i
++) {
5405 buffer
= malloc(image
.sections
[i
].size
);
5406 if (buffer
== NULL
) {
5407 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5408 (int)(image
.sections
[i
].size
));
5409 retval
= ERROR_FAIL
;
5413 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
5414 if (retval
!= ERROR_OK
) {
5419 uint32_t offset
= 0;
5420 uint32_t length
= buf_cnt
;
5422 /* DANGER!!! beware of unsigned comparision here!!! */
5424 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
5425 (image
.sections
[i
].base_address
< max_address
)) {
5426 if (image
.sections
[i
].base_address
< min_address
) {
5427 /* clip addresses below */
5428 offset
+= min_address
-image
.sections
[i
].base_address
;
5432 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5433 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5435 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5436 fastload
[i
].data
= malloc(length
);
5437 if (fastload
[i
].data
== NULL
) {
5439 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5441 retval
= ERROR_FAIL
;
5444 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5445 fastload
[i
].length
= length
;
5447 image_size
+= length
;
5448 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
5449 (unsigned int)length
,
5450 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
5456 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
5457 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
5458 "in %fs (%0.3f KiB/s)", image_size
,
5459 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
5461 command_print(CMD_CTX
,
5462 "WARNING: image has not been loaded to target!"
5463 "You can issue a 'fast_load' to finish loading.");
5466 image_close(&image
);
5468 if (retval
!= ERROR_OK
)
5474 COMMAND_HANDLER(handle_fast_load_command
)
5477 return ERROR_COMMAND_SYNTAX_ERROR
;
5478 if (fastload
== NULL
) {
5479 LOG_ERROR("No image in memory");
5483 int ms
= timeval_ms();
5485 int retval
= ERROR_OK
;
5486 for (i
= 0; i
< fastload_num
; i
++) {
5487 struct target
*target
= get_current_target(CMD_CTX
);
5488 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
5489 (unsigned int)(fastload
[i
].address
),
5490 (unsigned int)(fastload
[i
].length
));
5491 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5492 if (retval
!= ERROR_OK
)
5494 size
+= fastload
[i
].length
;
5496 if (retval
== ERROR_OK
) {
5497 int after
= timeval_ms();
5498 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5503 static const struct command_registration target_command_handlers
[] = {
5506 .handler
= handle_targets_command
,
5507 .mode
= COMMAND_ANY
,
5508 .help
= "change current default target (one parameter) "
5509 "or prints table of all targets (no parameters)",
5510 .usage
= "[target]",
5514 .mode
= COMMAND_CONFIG
,
5515 .help
= "configure target",
5517 .chain
= target_subcommand_handlers
,
5519 COMMAND_REGISTRATION_DONE
5522 int target_register_commands(struct command_context
*cmd_ctx
)
5524 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5527 static bool target_reset_nag
= true;
5529 bool get_target_reset_nag(void)
5531 return target_reset_nag
;
5534 COMMAND_HANDLER(handle_target_reset_nag
)
5536 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
5537 &target_reset_nag
, "Nag after each reset about options to improve "
5541 COMMAND_HANDLER(handle_ps_command
)
5543 struct target
*target
= get_current_target(CMD_CTX
);
5545 if (target
->state
!= TARGET_HALTED
) {
5546 LOG_INFO("target not halted !!");
5550 if ((target
->rtos
) && (target
->rtos
->type
)
5551 && (target
->rtos
->type
->ps_command
)) {
5552 display
= target
->rtos
->type
->ps_command(target
);
5553 command_print(CMD_CTX
, "%s", display
);
5558 return ERROR_TARGET_FAILURE
;
5562 static const struct command_registration target_exec_command_handlers
[] = {
5564 .name
= "fast_load_image",
5565 .handler
= handle_fast_load_image_command
,
5566 .mode
= COMMAND_ANY
,
5567 .help
= "Load image into server memory for later use by "
5568 "fast_load; primarily for profiling",
5569 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5570 "[min_address [max_length]]",
5573 .name
= "fast_load",
5574 .handler
= handle_fast_load_command
,
5575 .mode
= COMMAND_EXEC
,
5576 .help
= "loads active fast load image to current target "
5577 "- mainly for profiling purposes",
5582 .handler
= handle_profile_command
,
5583 .mode
= COMMAND_EXEC
,
5584 .usage
= "seconds filename [start end]",
5585 .help
= "profiling samples the CPU PC",
5587 /** @todo don't register virt2phys() unless target supports it */
5589 .name
= "virt2phys",
5590 .handler
= handle_virt2phys_command
,
5591 .mode
= COMMAND_ANY
,
5592 .help
= "translate a virtual address into a physical address",
5593 .usage
= "virtual_address",
5597 .handler
= handle_reg_command
,
5598 .mode
= COMMAND_EXEC
,
5599 .help
= "display or set a register; with no arguments, "
5600 "displays all registers and their values",
5601 .usage
= "[(register_name|register_number) [value]]",
5605 .handler
= handle_poll_command
,
5606 .mode
= COMMAND_EXEC
,
5607 .help
= "poll target state; or reconfigure background polling",
5608 .usage
= "['on'|'off']",
5611 .name
= "wait_halt",
5612 .handler
= handle_wait_halt_command
,
5613 .mode
= COMMAND_EXEC
,
5614 .help
= "wait up to the specified number of milliseconds "
5615 "(default 5000) for a previously requested halt",
5616 .usage
= "[milliseconds]",
5620 .handler
= handle_halt_command
,
5621 .mode
= COMMAND_EXEC
,
5622 .help
= "request target to halt, then wait up to the specified"
5623 "number of milliseconds (default 5000) for it to complete",
5624 .usage
= "[milliseconds]",
5628 .handler
= handle_resume_command
,
5629 .mode
= COMMAND_EXEC
,
5630 .help
= "resume target execution from current PC or address",
5631 .usage
= "[address]",
5635 .handler
= handle_reset_command
,
5636 .mode
= COMMAND_EXEC
,
5637 .usage
= "[run|halt|init]",
5638 .help
= "Reset all targets into the specified mode."
5639 "Default reset mode is run, if not given.",
5642 .name
= "soft_reset_halt",
5643 .handler
= handle_soft_reset_halt_command
,
5644 .mode
= COMMAND_EXEC
,
5646 .help
= "halt the target and do a soft reset",
5650 .handler
= handle_step_command
,
5651 .mode
= COMMAND_EXEC
,
5652 .help
= "step one instruction from current PC or address",
5653 .usage
= "[address]",
5657 .handler
= handle_md_command
,
5658 .mode
= COMMAND_EXEC
,
5659 .help
= "display memory words",
5660 .usage
= "['phys'] address [count]",
5664 .handler
= handle_md_command
,
5665 .mode
= COMMAND_EXEC
,
5666 .help
= "display memory half-words",
5667 .usage
= "['phys'] address [count]",
5671 .handler
= handle_md_command
,
5672 .mode
= COMMAND_EXEC
,
5673 .help
= "display memory bytes",
5674 .usage
= "['phys'] address [count]",
5678 .handler
= handle_mw_command
,
5679 .mode
= COMMAND_EXEC
,
5680 .help
= "write memory word",
5681 .usage
= "['phys'] address value [count]",
5685 .handler
= handle_mw_command
,
5686 .mode
= COMMAND_EXEC
,
5687 .help
= "write memory half-word",
5688 .usage
= "['phys'] address value [count]",
5692 .handler
= handle_mw_command
,
5693 .mode
= COMMAND_EXEC
,
5694 .help
= "write memory byte",
5695 .usage
= "['phys'] address value [count]",
5699 .handler
= handle_bp_command
,
5700 .mode
= COMMAND_EXEC
,
5701 .help
= "list or set hardware or software breakpoint",
5702 .usage
= "<address> [<asid>]<length> ['hw'|'hw_ctx']",
5706 .handler
= handle_rbp_command
,
5707 .mode
= COMMAND_EXEC
,
5708 .help
= "remove breakpoint",
5713 .handler
= handle_wp_command
,
5714 .mode
= COMMAND_EXEC
,
5715 .help
= "list (no params) or create watchpoints",
5716 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
5720 .handler
= handle_rwp_command
,
5721 .mode
= COMMAND_EXEC
,
5722 .help
= "remove watchpoint",
5726 .name
= "load_image",
5727 .handler
= handle_load_image_command
,
5728 .mode
= COMMAND_EXEC
,
5729 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5730 "[min_address] [max_length]",
5733 .name
= "dump_image",
5734 .handler
= handle_dump_image_command
,
5735 .mode
= COMMAND_EXEC
,
5736 .usage
= "filename address size",
5739 .name
= "verify_image",
5740 .handler
= handle_verify_image_command
,
5741 .mode
= COMMAND_EXEC
,
5742 .usage
= "filename [offset [type]]",
5745 .name
= "test_image",
5746 .handler
= handle_test_image_command
,
5747 .mode
= COMMAND_EXEC
,
5748 .usage
= "filename [offset [type]]",
5751 .name
= "mem2array",
5752 .mode
= COMMAND_EXEC
,
5753 .jim_handler
= jim_mem2array
,
5754 .help
= "read 8/16/32 bit memory and return as a TCL array "
5755 "for script processing",
5756 .usage
= "arrayname bitwidth address count",
5759 .name
= "array2mem",
5760 .mode
= COMMAND_EXEC
,
5761 .jim_handler
= jim_array2mem
,
5762 .help
= "convert a TCL array to memory locations "
5763 "and write the 8/16/32 bit values",
5764 .usage
= "arrayname bitwidth address count",
5767 .name
= "reset_nag",
5768 .handler
= handle_target_reset_nag
,
5769 .mode
= COMMAND_ANY
,
5770 .help
= "Nag after each reset about options that could have been "
5771 "enabled to improve performance. ",
5772 .usage
= "['enable'|'disable']",
5776 .handler
= handle_ps_command
,
5777 .mode
= COMMAND_EXEC
,
5778 .help
= "list all tasks ",
5782 COMMAND_REGISTRATION_DONE
5784 static int target_register_user_commands(struct command_context
*cmd_ctx
)
5786 int retval
= ERROR_OK
;
5787 retval
= target_request_register_commands(cmd_ctx
);
5788 if (retval
!= ERROR_OK
)
5791 retval
= trace_register_commands(cmd_ctx
);
5792 if (retval
!= ERROR_OK
)
5796 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);