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 size
, uint8_t *buffer
);
64 static int target_write_buffer_default(struct target
*target
, uint32_t address
,
65 uint32_t size
, 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
);
77 extern struct target_type arm7tdmi_target
;
78 extern struct target_type arm720t_target
;
79 extern struct target_type arm9tdmi_target
;
80 extern struct target_type arm920t_target
;
81 extern struct target_type arm966e_target
;
82 extern struct target_type arm946e_target
;
83 extern struct target_type arm926ejs_target
;
84 extern struct target_type fa526_target
;
85 extern struct target_type feroceon_target
;
86 extern struct target_type dragonite_target
;
87 extern struct target_type xscale_target
;
88 extern struct target_type cortexm3_target
;
89 extern struct target_type cortexa8_target
;
90 extern struct target_type cortexr4_target
;
91 extern struct target_type arm11_target
;
92 extern struct target_type mips_m4k_target
;
93 extern struct target_type avr_target
;
94 extern struct target_type dsp563xx_target
;
95 extern struct target_type dsp5680xx_target
;
96 extern struct target_type testee_target
;
97 extern struct target_type avr32_ap7k_target
;
98 extern struct target_type hla_target
;
99 extern struct target_type nds32_v2_target
;
100 extern struct target_type nds32_v3_target
;
101 extern struct target_type nds32_v3m_target
;
103 static struct target_type
*target_types
[] = {
132 struct target
*all_targets
;
133 static struct target_event_callback
*target_event_callbacks
;
134 static struct target_timer_callback
*target_timer_callbacks
;
135 static const int polling_interval
= 100;
137 static const Jim_Nvp nvp_assert
[] = {
138 { .name
= "assert", NVP_ASSERT
},
139 { .name
= "deassert", NVP_DEASSERT
},
140 { .name
= "T", NVP_ASSERT
},
141 { .name
= "F", NVP_DEASSERT
},
142 { .name
= "t", NVP_ASSERT
},
143 { .name
= "f", NVP_DEASSERT
},
144 { .name
= NULL
, .value
= -1 }
147 static const Jim_Nvp nvp_error_target
[] = {
148 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
149 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
150 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
151 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
152 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
153 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
154 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
155 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
156 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
157 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
158 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
159 { .value
= -1, .name
= NULL
}
162 static const char *target_strerror_safe(int err
)
166 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
173 static const Jim_Nvp nvp_target_event
[] = {
175 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
176 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
177 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
178 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
179 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
181 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
182 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
184 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
185 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
186 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
187 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
188 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
189 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
190 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
191 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
192 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
193 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
194 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
195 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
197 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
198 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
200 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
201 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
203 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
204 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
206 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
207 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
209 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
210 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
212 { .name
= NULL
, .value
= -1 }
215 static const Jim_Nvp nvp_target_state
[] = {
216 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
217 { .name
= "running", .value
= TARGET_RUNNING
},
218 { .name
= "halted", .value
= TARGET_HALTED
},
219 { .name
= "reset", .value
= TARGET_RESET
},
220 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
221 { .name
= NULL
, .value
= -1 },
224 static const Jim_Nvp nvp_target_debug_reason
[] = {
225 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
226 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
227 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
228 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
229 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
230 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
231 { .name
= "program-exit" , .value
= DBG_REASON_EXIT
},
232 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
233 { .name
= NULL
, .value
= -1 },
236 static const Jim_Nvp nvp_target_endian
[] = {
237 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
238 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
239 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
240 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
241 { .name
= NULL
, .value
= -1 },
244 static const Jim_Nvp nvp_reset_modes
[] = {
245 { .name
= "unknown", .value
= RESET_UNKNOWN
},
246 { .name
= "run" , .value
= RESET_RUN
},
247 { .name
= "halt" , .value
= RESET_HALT
},
248 { .name
= "init" , .value
= RESET_INIT
},
249 { .name
= NULL
, .value
= -1 },
252 const char *debug_reason_name(struct target
*t
)
256 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
257 t
->debug_reason
)->name
;
259 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
260 cp
= "(*BUG*unknown*BUG*)";
265 const char *target_state_name(struct target
*t
)
268 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
270 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
271 cp
= "(*BUG*unknown*BUG*)";
276 /* determine the number of the new target */
277 static int new_target_number(void)
282 /* number is 0 based */
286 if (x
< t
->target_number
)
287 x
= t
->target_number
;
293 /* read a uint32_t from a buffer in target memory endianness */
294 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
296 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
297 return le_to_h_u32(buffer
);
299 return be_to_h_u32(buffer
);
302 /* read a uint24_t from a buffer in target memory endianness */
303 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
305 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
306 return le_to_h_u24(buffer
);
308 return be_to_h_u24(buffer
);
311 /* read a uint16_t from a buffer in target memory endianness */
312 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
314 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
315 return le_to_h_u16(buffer
);
317 return be_to_h_u16(buffer
);
320 /* read a uint8_t from a buffer in target memory endianness */
321 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
323 return *buffer
& 0x0ff;
326 /* write a uint32_t to a buffer in target memory endianness */
327 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
329 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
330 h_u32_to_le(buffer
, value
);
332 h_u32_to_be(buffer
, value
);
335 /* write a uint24_t to a buffer in target memory endianness */
336 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
338 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
339 h_u24_to_le(buffer
, value
);
341 h_u24_to_be(buffer
, value
);
344 /* write a uint16_t to a buffer in target memory endianness */
345 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
347 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
348 h_u16_to_le(buffer
, value
);
350 h_u16_to_be(buffer
, value
);
353 /* write a uint8_t to a buffer in target memory endianness */
354 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
359 /* write a uint32_t array to a buffer in target memory endianness */
360 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
363 for (i
= 0; i
< count
; i
++)
364 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
367 /* write a uint16_t array to a buffer in target memory endianness */
368 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
371 for (i
= 0; i
< count
; i
++)
372 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
375 /* write a uint32_t array to a buffer in target memory endianness */
376 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, uint32_t *srcbuf
)
379 for (i
= 0; i
< count
; i
++)
380 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
383 /* write a uint16_t array to a buffer in target memory endianness */
384 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, uint16_t *srcbuf
)
387 for (i
= 0; i
< count
; i
++)
388 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
391 /* return a pointer to a configured target; id is name or number */
392 struct target
*get_target(const char *id
)
394 struct target
*target
;
396 /* try as tcltarget name */
397 for (target
= all_targets
; target
; target
= target
->next
) {
398 if (target_name(target
) == NULL
)
400 if (strcmp(id
, target_name(target
)) == 0)
404 /* It's OK to remove this fallback sometime after August 2010 or so */
406 /* no match, try as number */
408 if (parse_uint(id
, &num
) != ERROR_OK
)
411 for (target
= all_targets
; target
; target
= target
->next
) {
412 if (target
->target_number
== (int)num
) {
413 LOG_WARNING("use '%s' as target identifier, not '%u'",
414 target_name(target
), num
);
422 /* returns a pointer to the n-th configured target */
423 static struct target
*get_target_by_num(int num
)
425 struct target
*target
= all_targets
;
428 if (target
->target_number
== num
)
430 target
= target
->next
;
436 struct target
*get_current_target(struct command_context
*cmd_ctx
)
438 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
440 if (target
== NULL
) {
441 LOG_ERROR("BUG: current_target out of bounds");
448 int target_poll(struct target
*target
)
452 /* We can't poll until after examine */
453 if (!target_was_examined(target
)) {
454 /* Fail silently lest we pollute the log */
458 retval
= target
->type
->poll(target
);
459 if (retval
!= ERROR_OK
)
462 if (target
->halt_issued
) {
463 if (target
->state
== TARGET_HALTED
)
464 target
->halt_issued
= false;
466 long long t
= timeval_ms() - target
->halt_issued_time
;
467 if (t
> DEFAULT_HALT_TIMEOUT
) {
468 target
->halt_issued
= false;
469 LOG_INFO("Halt timed out, wake up GDB.");
470 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
478 int target_halt(struct target
*target
)
481 /* We can't poll until after examine */
482 if (!target_was_examined(target
)) {
483 LOG_ERROR("Target not examined yet");
487 retval
= target
->type
->halt(target
);
488 if (retval
!= ERROR_OK
)
491 target
->halt_issued
= true;
492 target
->halt_issued_time
= timeval_ms();
498 * Make the target (re)start executing using its saved execution
499 * context (possibly with some modifications).
501 * @param target Which target should start executing.
502 * @param current True to use the target's saved program counter instead
503 * of the address parameter
504 * @param address Optionally used as the program counter.
505 * @param handle_breakpoints True iff breakpoints at the resumption PC
506 * should be skipped. (For example, maybe execution was stopped by
507 * such a breakpoint, in which case it would be counterprodutive to
509 * @param debug_execution False if all working areas allocated by OpenOCD
510 * should be released and/or restored to their original contents.
511 * (This would for example be true to run some downloaded "helper"
512 * algorithm code, which resides in one such working buffer and uses
513 * another for data storage.)
515 * @todo Resolve the ambiguity about what the "debug_execution" flag
516 * signifies. For example, Target implementations don't agree on how
517 * it relates to invalidation of the register cache, or to whether
518 * breakpoints and watchpoints should be enabled. (It would seem wrong
519 * to enable breakpoints when running downloaded "helper" algorithms
520 * (debug_execution true), since the breakpoints would be set to match
521 * target firmware being debugged, not the helper algorithm.... and
522 * enabling them could cause such helpers to malfunction (for example,
523 * by overwriting data with a breakpoint instruction. On the other
524 * hand the infrastructure for running such helpers might use this
525 * procedure but rely on hardware breakpoint to detect termination.)
527 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
531 /* We can't poll until after examine */
532 if (!target_was_examined(target
)) {
533 LOG_ERROR("Target not examined yet");
537 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
539 /* note that resume *must* be asynchronous. The CPU can halt before
540 * we poll. The CPU can even halt at the current PC as a result of
541 * a software breakpoint being inserted by (a bug?) the application.
543 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
544 if (retval
!= ERROR_OK
)
547 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
552 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
557 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
558 if (n
->name
== NULL
) {
559 LOG_ERROR("invalid reset mode");
563 /* disable polling during reset to make reset event scripts
564 * more predictable, i.e. dr/irscan & pathmove in events will
565 * not have JTAG operations injected into the middle of a sequence.
567 bool save_poll
= jtag_poll_get_enabled();
569 jtag_poll_set_enabled(false);
571 sprintf(buf
, "ocd_process_reset %s", n
->name
);
572 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
574 jtag_poll_set_enabled(save_poll
);
576 if (retval
!= JIM_OK
) {
577 Jim_MakeErrorMessage(cmd_ctx
->interp
);
578 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
582 /* We want any events to be processed before the prompt */
583 retval
= target_call_timer_callbacks_now();
585 struct target
*target
;
586 for (target
= all_targets
; target
; target
= target
->next
)
587 target
->type
->check_reset(target
);
592 static int identity_virt2phys(struct target
*target
,
593 uint32_t virtual, uint32_t *physical
)
599 static int no_mmu(struct target
*target
, int *enabled
)
605 static int default_examine(struct target
*target
)
607 target_set_examined(target
);
611 /* no check by default */
612 static int default_check_reset(struct target
*target
)
617 int target_examine_one(struct target
*target
)
619 return target
->type
->examine(target
);
622 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
624 struct target
*target
= priv
;
626 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
629 jtag_unregister_event_callback(jtag_enable_callback
, target
);
631 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
633 int retval
= target_examine_one(target
);
634 if (retval
!= ERROR_OK
)
637 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
642 /* Targets that correctly implement init + examine, i.e.
643 * no communication with target during init:
647 int target_examine(void)
649 int retval
= ERROR_OK
;
650 struct target
*target
;
652 for (target
= all_targets
; target
; target
= target
->next
) {
653 /* defer examination, but don't skip it */
654 if (!target
->tap
->enabled
) {
655 jtag_register_event_callback(jtag_enable_callback
,
660 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
662 retval
= target_examine_one(target
);
663 if (retval
!= ERROR_OK
)
666 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
671 const char *target_type_name(struct target
*target
)
673 return target
->type
->name
;
676 static int target_soft_reset_halt(struct target
*target
)
678 if (!target_was_examined(target
)) {
679 LOG_ERROR("Target not examined yet");
682 if (!target
->type
->soft_reset_halt
) {
683 LOG_ERROR("Target %s does not support soft_reset_halt",
684 target_name(target
));
687 return target
->type
->soft_reset_halt(target
);
691 * Downloads a target-specific native code algorithm to the target,
692 * and executes it. * Note that some targets may need to set up, enable,
693 * and tear down a breakpoint (hard or * soft) to detect algorithm
694 * termination, while others may support lower overhead schemes where
695 * soft breakpoints embedded in the algorithm automatically terminate the
698 * @param target used to run the algorithm
699 * @param arch_info target-specific description of the algorithm.
701 int target_run_algorithm(struct target
*target
,
702 int num_mem_params
, struct mem_param
*mem_params
,
703 int num_reg_params
, struct reg_param
*reg_param
,
704 uint32_t entry_point
, uint32_t exit_point
,
705 int timeout_ms
, void *arch_info
)
707 int retval
= ERROR_FAIL
;
709 if (!target_was_examined(target
)) {
710 LOG_ERROR("Target not examined yet");
713 if (!target
->type
->run_algorithm
) {
714 LOG_ERROR("Target type '%s' does not support %s",
715 target_type_name(target
), __func__
);
719 target
->running_alg
= true;
720 retval
= target
->type
->run_algorithm(target
,
721 num_mem_params
, mem_params
,
722 num_reg_params
, reg_param
,
723 entry_point
, exit_point
, timeout_ms
, arch_info
);
724 target
->running_alg
= false;
731 * Downloads a target-specific native code algorithm to the target,
732 * executes and leaves it running.
734 * @param target used to run the algorithm
735 * @param arch_info target-specific description of the algorithm.
737 int target_start_algorithm(struct target
*target
,
738 int num_mem_params
, struct mem_param
*mem_params
,
739 int num_reg_params
, struct reg_param
*reg_params
,
740 uint32_t entry_point
, uint32_t exit_point
,
743 int retval
= ERROR_FAIL
;
745 if (!target_was_examined(target
)) {
746 LOG_ERROR("Target not examined yet");
749 if (!target
->type
->start_algorithm
) {
750 LOG_ERROR("Target type '%s' does not support %s",
751 target_type_name(target
), __func__
);
754 if (target
->running_alg
) {
755 LOG_ERROR("Target is already running an algorithm");
759 target
->running_alg
= true;
760 retval
= target
->type
->start_algorithm(target
,
761 num_mem_params
, mem_params
,
762 num_reg_params
, reg_params
,
763 entry_point
, exit_point
, arch_info
);
770 * Waits for an algorithm started with target_start_algorithm() to complete.
772 * @param target used to run the algorithm
773 * @param arch_info target-specific description of the algorithm.
775 int target_wait_algorithm(struct target
*target
,
776 int num_mem_params
, struct mem_param
*mem_params
,
777 int num_reg_params
, struct reg_param
*reg_params
,
778 uint32_t exit_point
, int timeout_ms
,
781 int retval
= ERROR_FAIL
;
783 if (!target
->type
->wait_algorithm
) {
784 LOG_ERROR("Target type '%s' does not support %s",
785 target_type_name(target
), __func__
);
788 if (!target
->running_alg
) {
789 LOG_ERROR("Target is not running an algorithm");
793 retval
= target
->type
->wait_algorithm(target
,
794 num_mem_params
, mem_params
,
795 num_reg_params
, reg_params
,
796 exit_point
, timeout_ms
, arch_info
);
797 if (retval
!= ERROR_TARGET_TIMEOUT
)
798 target
->running_alg
= false;
805 * Executes a target-specific native code algorithm in the target.
806 * It differs from target_run_algorithm in that the algorithm is asynchronous.
807 * Because of this it requires an compliant algorithm:
808 * see contrib/loaders/flash/stm32f1x.S for example.
810 * @param target used to run the algorithm
813 int target_run_flash_async_algorithm(struct target
*target
,
814 uint8_t *buffer
, uint32_t count
, int block_size
,
815 int num_mem_params
, struct mem_param
*mem_params
,
816 int num_reg_params
, struct reg_param
*reg_params
,
817 uint32_t buffer_start
, uint32_t buffer_size
,
818 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
823 /* Set up working area. First word is write pointer, second word is read pointer,
824 * rest is fifo data area. */
825 uint32_t wp_addr
= buffer_start
;
826 uint32_t rp_addr
= buffer_start
+ 4;
827 uint32_t fifo_start_addr
= buffer_start
+ 8;
828 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
830 uint32_t wp
= fifo_start_addr
;
831 uint32_t rp
= fifo_start_addr
;
833 /* validate block_size is 2^n */
834 assert(!block_size
|| !(block_size
& (block_size
- 1)));
836 retval
= target_write_u32(target
, wp_addr
, wp
);
837 if (retval
!= ERROR_OK
)
839 retval
= target_write_u32(target
, rp_addr
, rp
);
840 if (retval
!= ERROR_OK
)
843 /* Start up algorithm on target and let it idle while writing the first chunk */
844 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
845 num_reg_params
, reg_params
,
850 if (retval
!= ERROR_OK
) {
851 LOG_ERROR("error starting target flash write algorithm");
857 retval
= target_read_u32(target
, rp_addr
, &rp
);
858 if (retval
!= ERROR_OK
) {
859 LOG_ERROR("failed to get read pointer");
863 LOG_DEBUG("count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
, count
, wp
, rp
);
866 LOG_ERROR("flash write algorithm aborted by target");
867 retval
= ERROR_FLASH_OPERATION_FAILED
;
871 if ((rp
& (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
872 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
876 /* Count the number of bytes available in the fifo without
877 * crossing the wrap around. Make sure to not fill it completely,
878 * because that would make wp == rp and that's the empty condition. */
879 uint32_t thisrun_bytes
;
881 thisrun_bytes
= rp
- wp
- block_size
;
882 else if (rp
> fifo_start_addr
)
883 thisrun_bytes
= fifo_end_addr
- wp
;
885 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
887 if (thisrun_bytes
== 0) {
888 /* Throttle polling a bit if transfer is (much) faster than flash
889 * programming. The exact delay shouldn't matter as long as it's
890 * less than buffer size / flash speed. This is very unlikely to
891 * run when using high latency connections such as USB. */
894 /* to stop an infinite loop on some targets check and increment a timeout
895 * this issue was observed on a stellaris using the new ICDI interface */
896 if (timeout
++ >= 500) {
897 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
898 return ERROR_FLASH_OPERATION_FAILED
;
903 /* reset our timeout */
906 /* Limit to the amount of data we actually want to write */
907 if (thisrun_bytes
> count
* block_size
)
908 thisrun_bytes
= count
* block_size
;
910 /* Write data to fifo */
911 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
912 if (retval
!= ERROR_OK
)
915 /* Update counters and wrap write pointer */
916 buffer
+= thisrun_bytes
;
917 count
-= thisrun_bytes
/ block_size
;
919 if (wp
>= fifo_end_addr
)
920 wp
= fifo_start_addr
;
922 /* Store updated write pointer to target */
923 retval
= target_write_u32(target
, wp_addr
, wp
);
924 if (retval
!= ERROR_OK
)
928 if (retval
!= ERROR_OK
) {
929 /* abort flash write algorithm on target */
930 target_write_u32(target
, wp_addr
, 0);
933 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
934 num_reg_params
, reg_params
,
939 if (retval2
!= ERROR_OK
) {
940 LOG_ERROR("error waiting for target flash write algorithm");
947 int target_read_memory(struct target
*target
,
948 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
950 if (!target_was_examined(target
)) {
951 LOG_ERROR("Target not examined yet");
954 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
957 int target_read_phys_memory(struct target
*target
,
958 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
960 if (!target_was_examined(target
)) {
961 LOG_ERROR("Target not examined yet");
964 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
967 int target_write_memory(struct target
*target
,
968 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
970 if (!target_was_examined(target
)) {
971 LOG_ERROR("Target not examined yet");
974 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
977 int target_write_phys_memory(struct target
*target
,
978 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
980 if (!target_was_examined(target
)) {
981 LOG_ERROR("Target not examined yet");
984 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
987 static int target_bulk_write_memory_default(struct target
*target
,
988 uint32_t address
, uint32_t count
, const uint8_t *buffer
)
990 return target_write_memory(target
, address
, 4, count
, buffer
);
993 int target_add_breakpoint(struct target
*target
,
994 struct breakpoint
*breakpoint
)
996 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
997 LOG_WARNING("target %s is not halted", target_name(target
));
998 return ERROR_TARGET_NOT_HALTED
;
1000 return target
->type
->add_breakpoint(target
, breakpoint
);
1003 int target_add_context_breakpoint(struct target
*target
,
1004 struct breakpoint
*breakpoint
)
1006 if (target
->state
!= TARGET_HALTED
) {
1007 LOG_WARNING("target %s is not halted", target_name(target
));
1008 return ERROR_TARGET_NOT_HALTED
;
1010 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1013 int target_add_hybrid_breakpoint(struct target
*target
,
1014 struct breakpoint
*breakpoint
)
1016 if (target
->state
!= TARGET_HALTED
) {
1017 LOG_WARNING("target %s is not halted", target_name(target
));
1018 return ERROR_TARGET_NOT_HALTED
;
1020 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1023 int target_remove_breakpoint(struct target
*target
,
1024 struct breakpoint
*breakpoint
)
1026 return target
->type
->remove_breakpoint(target
, breakpoint
);
1029 int target_add_watchpoint(struct target
*target
,
1030 struct watchpoint
*watchpoint
)
1032 if (target
->state
!= TARGET_HALTED
) {
1033 LOG_WARNING("target %s is not halted", target_name(target
));
1034 return ERROR_TARGET_NOT_HALTED
;
1036 return target
->type
->add_watchpoint(target
, watchpoint
);
1038 int target_remove_watchpoint(struct target
*target
,
1039 struct watchpoint
*watchpoint
)
1041 return target
->type
->remove_watchpoint(target
, watchpoint
);
1043 int target_hit_watchpoint(struct target
*target
,
1044 struct watchpoint
**hit_watchpoint
)
1046 if (target
->state
!= TARGET_HALTED
) {
1047 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1048 return ERROR_TARGET_NOT_HALTED
;
1051 if (target
->type
->hit_watchpoint
== NULL
) {
1052 /* For backward compatible, if hit_watchpoint is not implemented,
1053 * return ERROR_FAIL such that gdb_server will not take the nonsense
1058 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1061 int target_get_gdb_reg_list(struct target
*target
,
1062 struct reg
**reg_list
[], int *reg_list_size
,
1063 enum target_register_class reg_class
)
1065 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1067 int target_step(struct target
*target
,
1068 int current
, uint32_t address
, int handle_breakpoints
)
1070 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1073 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1075 if (target
->state
!= TARGET_HALTED
) {
1076 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1077 return ERROR_TARGET_NOT_HALTED
;
1079 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1082 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1084 if (target
->state
!= TARGET_HALTED
) {
1085 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1086 return ERROR_TARGET_NOT_HALTED
;
1088 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1092 * Reset the @c examined flag for the given target.
1093 * Pure paranoia -- targets are zeroed on allocation.
1095 static void target_reset_examined(struct target
*target
)
1097 target
->examined
= false;
1100 static int err_read_phys_memory(struct target
*target
, uint32_t address
,
1101 uint32_t size
, uint32_t count
, uint8_t *buffer
)
1103 LOG_ERROR("Not implemented: %s", __func__
);
1107 static int err_write_phys_memory(struct target
*target
, uint32_t address
,
1108 uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1110 LOG_ERROR("Not implemented: %s", __func__
);
1114 static int handle_target(void *priv
);
1116 static int target_init_one(struct command_context
*cmd_ctx
,
1117 struct target
*target
)
1119 target_reset_examined(target
);
1121 struct target_type
*type
= target
->type
;
1122 if (type
->examine
== NULL
)
1123 type
->examine
= default_examine
;
1125 if (type
->check_reset
== NULL
)
1126 type
->check_reset
= default_check_reset
;
1128 assert(type
->init_target
!= NULL
);
1130 int retval
= type
->init_target(cmd_ctx
, target
);
1131 if (ERROR_OK
!= retval
) {
1132 LOG_ERROR("target '%s' init failed", target_name(target
));
1136 /* Sanity-check MMU support ... stub in what we must, to help
1137 * implement it in stages, but warn if we need to do so.
1140 if (type
->write_phys_memory
== NULL
) {
1141 LOG_ERROR("type '%s' is missing write_phys_memory",
1143 type
->write_phys_memory
= err_write_phys_memory
;
1145 if (type
->read_phys_memory
== NULL
) {
1146 LOG_ERROR("type '%s' is missing read_phys_memory",
1148 type
->read_phys_memory
= err_read_phys_memory
;
1150 if (type
->virt2phys
== NULL
) {
1151 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1152 type
->virt2phys
= identity_virt2phys
;
1155 /* Make sure no-MMU targets all behave the same: make no
1156 * distinction between physical and virtual addresses, and
1157 * ensure that virt2phys() is always an identity mapping.
1159 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1160 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1163 type
->write_phys_memory
= type
->write_memory
;
1164 type
->read_phys_memory
= type
->read_memory
;
1165 type
->virt2phys
= identity_virt2phys
;
1168 if (target
->type
->read_buffer
== NULL
)
1169 target
->type
->read_buffer
= target_read_buffer_default
;
1171 if (target
->type
->write_buffer
== NULL
)
1172 target
->type
->write_buffer
= target_write_buffer_default
;
1174 if (target
->type
->bulk_write_memory
== NULL
)
1175 target
->type
->bulk_write_memory
= target_bulk_write_memory_default
;
1177 if (target
->type
->get_gdb_fileio_info
== NULL
)
1178 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1180 if (target
->type
->gdb_fileio_end
== NULL
)
1181 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1186 static int target_init(struct command_context
*cmd_ctx
)
1188 struct target
*target
;
1191 for (target
= all_targets
; target
; target
= target
->next
) {
1192 retval
= target_init_one(cmd_ctx
, target
);
1193 if (ERROR_OK
!= retval
)
1200 retval
= target_register_user_commands(cmd_ctx
);
1201 if (ERROR_OK
!= retval
)
1204 retval
= target_register_timer_callback(&handle_target
,
1205 polling_interval
, 1, cmd_ctx
->interp
);
1206 if (ERROR_OK
!= retval
)
1212 COMMAND_HANDLER(handle_target_init_command
)
1217 return ERROR_COMMAND_SYNTAX_ERROR
;
1219 static bool target_initialized
;
1220 if (target_initialized
) {
1221 LOG_INFO("'target init' has already been called");
1224 target_initialized
= true;
1226 retval
= command_run_line(CMD_CTX
, "init_targets");
1227 if (ERROR_OK
!= retval
)
1230 retval
= command_run_line(CMD_CTX
, "init_board");
1231 if (ERROR_OK
!= retval
)
1234 LOG_DEBUG("Initializing targets...");
1235 return target_init(CMD_CTX
);
1238 int target_register_event_callback(int (*callback
)(struct target
*target
,
1239 enum target_event event
, void *priv
), void *priv
)
1241 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1243 if (callback
== NULL
)
1244 return ERROR_COMMAND_SYNTAX_ERROR
;
1247 while ((*callbacks_p
)->next
)
1248 callbacks_p
= &((*callbacks_p
)->next
);
1249 callbacks_p
= &((*callbacks_p
)->next
);
1252 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1253 (*callbacks_p
)->callback
= callback
;
1254 (*callbacks_p
)->priv
= priv
;
1255 (*callbacks_p
)->next
= NULL
;
1260 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1262 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1265 if (callback
== NULL
)
1266 return ERROR_COMMAND_SYNTAX_ERROR
;
1269 while ((*callbacks_p
)->next
)
1270 callbacks_p
= &((*callbacks_p
)->next
);
1271 callbacks_p
= &((*callbacks_p
)->next
);
1274 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1275 (*callbacks_p
)->callback
= callback
;
1276 (*callbacks_p
)->periodic
= periodic
;
1277 (*callbacks_p
)->time_ms
= time_ms
;
1279 gettimeofday(&now
, NULL
);
1280 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
1281 time_ms
-= (time_ms
% 1000);
1282 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
1283 if ((*callbacks_p
)->when
.tv_usec
> 1000000) {
1284 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
1285 (*callbacks_p
)->when
.tv_sec
+= 1;
1288 (*callbacks_p
)->priv
= priv
;
1289 (*callbacks_p
)->next
= NULL
;
1294 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1295 enum target_event event
, void *priv
), void *priv
)
1297 struct target_event_callback
**p
= &target_event_callbacks
;
1298 struct target_event_callback
*c
= target_event_callbacks
;
1300 if (callback
== NULL
)
1301 return ERROR_COMMAND_SYNTAX_ERROR
;
1304 struct target_event_callback
*next
= c
->next
;
1305 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1317 static int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1319 struct target_timer_callback
**p
= &target_timer_callbacks
;
1320 struct target_timer_callback
*c
= target_timer_callbacks
;
1322 if (callback
== NULL
)
1323 return ERROR_COMMAND_SYNTAX_ERROR
;
1326 struct target_timer_callback
*next
= c
->next
;
1327 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1339 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1341 struct target_event_callback
*callback
= target_event_callbacks
;
1342 struct target_event_callback
*next_callback
;
1344 if (event
== TARGET_EVENT_HALTED
) {
1345 /* execute early halted first */
1346 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1349 LOG_DEBUG("target event %i (%s)", event
,
1350 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1352 target_handle_event(target
, event
);
1355 next_callback
= callback
->next
;
1356 callback
->callback(target
, event
, callback
->priv
);
1357 callback
= next_callback
;
1363 static int target_timer_callback_periodic_restart(
1364 struct target_timer_callback
*cb
, struct timeval
*now
)
1366 int time_ms
= cb
->time_ms
;
1367 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1368 time_ms
-= (time_ms
% 1000);
1369 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1370 if (cb
->when
.tv_usec
> 1000000) {
1371 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1372 cb
->when
.tv_sec
+= 1;
1377 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1378 struct timeval
*now
)
1380 cb
->callback(cb
->priv
);
1383 return target_timer_callback_periodic_restart(cb
, now
);
1385 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1388 static int target_call_timer_callbacks_check_time(int checktime
)
1393 gettimeofday(&now
, NULL
);
1395 struct target_timer_callback
*callback
= target_timer_callbacks
;
1397 /* cleaning up may unregister and free this callback */
1398 struct target_timer_callback
*next_callback
= callback
->next
;
1400 bool call_it
= callback
->callback
&&
1401 ((!checktime
&& callback
->periodic
) ||
1402 now
.tv_sec
> callback
->when
.tv_sec
||
1403 (now
.tv_sec
== callback
->when
.tv_sec
&&
1404 now
.tv_usec
>= callback
->when
.tv_usec
));
1407 int retval
= target_call_timer_callback(callback
, &now
);
1408 if (retval
!= ERROR_OK
)
1412 callback
= next_callback
;
1418 int target_call_timer_callbacks(void)
1420 return target_call_timer_callbacks_check_time(1);
1423 /* invoke periodic callbacks immediately */
1424 int target_call_timer_callbacks_now(void)
1426 return target_call_timer_callbacks_check_time(0);
1429 /* Prints the working area layout for debug purposes */
1430 static void print_wa_layout(struct target
*target
)
1432 struct working_area
*c
= target
->working_areas
;
1435 LOG_DEBUG("%c%c 0x%08"PRIx32
"-0x%08"PRIx32
" (%"PRIu32
" bytes)",
1436 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1437 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1442 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1443 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1445 assert(area
->free
); /* Shouldn't split an allocated area */
1446 assert(size
<= area
->size
); /* Caller should guarantee this */
1448 /* Split only if not already the right size */
1449 if (size
< area
->size
) {
1450 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1455 new_wa
->next
= area
->next
;
1456 new_wa
->size
= area
->size
- size
;
1457 new_wa
->address
= area
->address
+ size
;
1458 new_wa
->backup
= NULL
;
1459 new_wa
->user
= NULL
;
1460 new_wa
->free
= true;
1462 area
->next
= new_wa
;
1465 /* If backup memory was allocated to this area, it has the wrong size
1466 * now so free it and it will be reallocated if/when needed */
1469 area
->backup
= NULL
;
1474 /* Merge all adjacent free areas into one */
1475 static void target_merge_working_areas(struct target
*target
)
1477 struct working_area
*c
= target
->working_areas
;
1479 while (c
&& c
->next
) {
1480 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1482 /* Find two adjacent free areas */
1483 if (c
->free
&& c
->next
->free
) {
1484 /* Merge the last into the first */
1485 c
->size
+= c
->next
->size
;
1487 /* Remove the last */
1488 struct working_area
*to_be_freed
= c
->next
;
1489 c
->next
= c
->next
->next
;
1490 if (to_be_freed
->backup
)
1491 free(to_be_freed
->backup
);
1494 /* If backup memory was allocated to the remaining area, it's has
1495 * the wrong size now */
1506 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1508 /* Reevaluate working area address based on MMU state*/
1509 if (target
->working_areas
== NULL
) {
1513 retval
= target
->type
->mmu(target
, &enabled
);
1514 if (retval
!= ERROR_OK
)
1518 if (target
->working_area_phys_spec
) {
1519 LOG_DEBUG("MMU disabled, using physical "
1520 "address for working memory 0x%08"PRIx32
,
1521 target
->working_area_phys
);
1522 target
->working_area
= target
->working_area_phys
;
1524 LOG_ERROR("No working memory available. "
1525 "Specify -work-area-phys to target.");
1526 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1529 if (target
->working_area_virt_spec
) {
1530 LOG_DEBUG("MMU enabled, using virtual "
1531 "address for working memory 0x%08"PRIx32
,
1532 target
->working_area_virt
);
1533 target
->working_area
= target
->working_area_virt
;
1535 LOG_ERROR("No working memory available. "
1536 "Specify -work-area-virt to target.");
1537 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1541 /* Set up initial working area on first call */
1542 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1544 new_wa
->next
= NULL
;
1545 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1546 new_wa
->address
= target
->working_area
;
1547 new_wa
->backup
= NULL
;
1548 new_wa
->user
= NULL
;
1549 new_wa
->free
= true;
1552 target
->working_areas
= new_wa
;
1555 /* only allocate multiples of 4 byte */
1557 size
= (size
+ 3) & (~3UL);
1559 struct working_area
*c
= target
->working_areas
;
1561 /* Find the first large enough working area */
1563 if (c
->free
&& c
->size
>= size
)
1569 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1571 /* Split the working area into the requested size */
1572 target_split_working_area(c
, size
);
1574 LOG_DEBUG("allocated new working area of %"PRIu32
" bytes at address 0x%08"PRIx32
, size
, c
->address
);
1576 if (target
->backup_working_area
) {
1577 if (c
->backup
== NULL
) {
1578 c
->backup
= malloc(c
->size
);
1579 if (c
->backup
== NULL
)
1583 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1584 if (retval
!= ERROR_OK
)
1588 /* mark as used, and return the new (reused) area */
1595 print_wa_layout(target
);
1600 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1604 retval
= target_alloc_working_area_try(target
, size
, area
);
1605 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1606 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1611 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1613 int retval
= ERROR_OK
;
1615 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1616 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1617 if (retval
!= ERROR_OK
)
1618 LOG_ERROR("failed to restore %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1619 area
->size
, area
->address
);
1625 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1626 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1628 int retval
= ERROR_OK
;
1634 retval
= target_restore_working_area(target
, area
);
1635 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1636 if (retval
!= ERROR_OK
)
1642 LOG_DEBUG("freed %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1643 area
->size
, area
->address
);
1645 /* mark user pointer invalid */
1646 /* TODO: Is this really safe? It points to some previous caller's memory.
1647 * How could we know that the area pointer is still in that place and not
1648 * some other vital data? What's the purpose of this, anyway? */
1652 target_merge_working_areas(target
);
1654 print_wa_layout(target
);
1659 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1661 return target_free_working_area_restore(target
, area
, 1);
1664 /* free resources and restore memory, if restoring memory fails,
1665 * free up resources anyway
1667 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1669 struct working_area
*c
= target
->working_areas
;
1671 LOG_DEBUG("freeing all working areas");
1673 /* Loop through all areas, restoring the allocated ones and marking them as free */
1677 target_restore_working_area(target
, c
);
1679 *c
->user
= NULL
; /* Same as above */
1685 /* Run a merge pass to combine all areas into one */
1686 target_merge_working_areas(target
);
1688 print_wa_layout(target
);
1691 void target_free_all_working_areas(struct target
*target
)
1693 target_free_all_working_areas_restore(target
, 1);
1696 /* Find the largest number of bytes that can be allocated */
1697 uint32_t target_get_working_area_avail(struct target
*target
)
1699 struct working_area
*c
= target
->working_areas
;
1700 uint32_t max_size
= 0;
1703 return target
->working_area_size
;
1706 if (c
->free
&& max_size
< c
->size
)
1715 int target_arch_state(struct target
*target
)
1718 if (target
== NULL
) {
1719 LOG_USER("No target has been configured");
1723 LOG_USER("target state: %s", target_state_name(target
));
1725 if (target
->state
!= TARGET_HALTED
)
1728 retval
= target
->type
->arch_state(target
);
1732 static int target_get_gdb_fileio_info_default(struct target
*target
,
1733 struct gdb_fileio_info
*fileio_info
)
1735 LOG_ERROR("Not implemented: %s", __func__
);
1739 static int target_gdb_fileio_end_default(struct target
*target
,
1740 int retcode
, int fileio_errno
, bool ctrl_c
)
1742 LOG_ERROR("Not implemented: %s", __func__
);
1746 /* Single aligned words are guaranteed to use 16 or 32 bit access
1747 * mode respectively, otherwise data is handled as quickly as
1750 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
1752 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1753 (int)size
, (unsigned)address
);
1755 if (!target_was_examined(target
)) {
1756 LOG_ERROR("Target not examined yet");
1763 if ((address
+ size
- 1) < address
) {
1764 /* GDB can request this when e.g. PC is 0xfffffffc*/
1765 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1771 return target
->type
->write_buffer(target
, address
, size
, buffer
);
1774 static int target_write_buffer_default(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
1776 int retval
= ERROR_OK
;
1778 if (((address
% 2) == 0) && (size
== 2))
1779 return target_write_memory(target
, address
, 2, 1, buffer
);
1781 /* handle unaligned head bytes */
1783 uint32_t unaligned
= 4 - (address
% 4);
1785 if (unaligned
> size
)
1788 retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
);
1789 if (retval
!= ERROR_OK
)
1792 buffer
+= unaligned
;
1793 address
+= unaligned
;
1797 /* handle aligned words */
1799 int aligned
= size
- (size
% 4);
1801 /* use bulk writes above a certain limit. This may have to be changed */
1802 if (aligned
> 128) {
1803 retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
);
1804 if (retval
!= ERROR_OK
)
1807 retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
);
1808 if (retval
!= ERROR_OK
)
1817 /* handle tail writes of less than 4 bytes */
1819 retval
= target_write_memory(target
, address
, 1, size
, buffer
);
1820 if (retval
!= ERROR_OK
)
1827 /* Single aligned words are guaranteed to use 16 or 32 bit access
1828 * mode respectively, otherwise data is handled as quickly as
1831 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1833 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1834 (int)size
, (unsigned)address
);
1836 if (!target_was_examined(target
)) {
1837 LOG_ERROR("Target not examined yet");
1844 if ((address
+ size
- 1) < address
) {
1845 /* GDB can request this when e.g. PC is 0xfffffffc*/
1846 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1852 return target
->type
->read_buffer(target
, address
, size
, buffer
);
1855 static int target_read_buffer_default(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1857 int retval
= ERROR_OK
;
1859 if (((address
% 2) == 0) && (size
== 2))
1860 return target_read_memory(target
, address
, 2, 1, buffer
);
1862 /* handle unaligned head bytes */
1864 uint32_t unaligned
= 4 - (address
% 4);
1866 if (unaligned
> size
)
1869 retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
);
1870 if (retval
!= ERROR_OK
)
1873 buffer
+= unaligned
;
1874 address
+= unaligned
;
1878 /* handle aligned words */
1880 int aligned
= size
- (size
% 4);
1882 retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
);
1883 if (retval
!= ERROR_OK
)
1891 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1893 int aligned
= size
- (size
% 2);
1894 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1895 if (retval
!= ERROR_OK
)
1902 /* handle tail writes of less than 4 bytes */
1904 retval
= target_read_memory(target
, address
, 1, size
, buffer
);
1905 if (retval
!= ERROR_OK
)
1912 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1917 uint32_t checksum
= 0;
1918 if (!target_was_examined(target
)) {
1919 LOG_ERROR("Target not examined yet");
1923 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
1924 if (retval
!= ERROR_OK
) {
1925 buffer
= malloc(size
);
1926 if (buffer
== NULL
) {
1927 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1928 return ERROR_COMMAND_SYNTAX_ERROR
;
1930 retval
= target_read_buffer(target
, address
, size
, buffer
);
1931 if (retval
!= ERROR_OK
) {
1936 /* convert to target endianness */
1937 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
1938 uint32_t target_data
;
1939 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1940 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1943 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1952 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1955 if (!target_was_examined(target
)) {
1956 LOG_ERROR("Target not examined yet");
1960 if (target
->type
->blank_check_memory
== 0)
1961 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1963 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1968 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
1970 uint8_t value_buf
[4];
1971 if (!target_was_examined(target
)) {
1972 LOG_ERROR("Target not examined yet");
1976 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1978 if (retval
== ERROR_OK
) {
1979 *value
= target_buffer_get_u32(target
, value_buf
);
1980 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1985 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1992 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
1994 uint8_t value_buf
[2];
1995 if (!target_was_examined(target
)) {
1996 LOG_ERROR("Target not examined yet");
2000 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2002 if (retval
== ERROR_OK
) {
2003 *value
= target_buffer_get_u16(target
, value_buf
);
2004 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
2009 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2016 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
2018 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2019 if (!target_was_examined(target
)) {
2020 LOG_ERROR("Target not examined yet");
2024 if (retval
== ERROR_OK
) {
2025 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
2030 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2037 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
2040 uint8_t value_buf
[4];
2041 if (!target_was_examined(target
)) {
2042 LOG_ERROR("Target not examined yet");
2046 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
2050 target_buffer_set_u32(target
, value_buf
, value
);
2051 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2052 if (retval
!= ERROR_OK
)
2053 LOG_DEBUG("failed: %i", retval
);
2058 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
2061 uint8_t value_buf
[2];
2062 if (!target_was_examined(target
)) {
2063 LOG_ERROR("Target not examined yet");
2067 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
2071 target_buffer_set_u16(target
, value_buf
, value
);
2072 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2073 if (retval
!= ERROR_OK
)
2074 LOG_DEBUG("failed: %i", retval
);
2079 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
2082 if (!target_was_examined(target
)) {
2083 LOG_ERROR("Target not examined yet");
2087 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
2090 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2091 if (retval
!= ERROR_OK
)
2092 LOG_DEBUG("failed: %i", retval
);
2097 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2099 struct target
*target
= get_target(name
);
2100 if (target
== NULL
) {
2101 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2104 if (!target
->tap
->enabled
) {
2105 LOG_USER("Target: TAP %s is disabled, "
2106 "can't be the current target\n",
2107 target
->tap
->dotted_name
);
2111 cmd_ctx
->current_target
= target
->target_number
;
2116 COMMAND_HANDLER(handle_targets_command
)
2118 int retval
= ERROR_OK
;
2119 if (CMD_ARGC
== 1) {
2120 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2121 if (retval
== ERROR_OK
) {
2127 struct target
*target
= all_targets
;
2128 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2129 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2134 if (target
->tap
->enabled
)
2135 state
= target_state_name(target
);
2137 state
= "tap-disabled";
2139 if (CMD_CTX
->current_target
== target
->target_number
)
2142 /* keep columns lined up to match the headers above */
2143 command_print(CMD_CTX
,
2144 "%2d%c %-18s %-10s %-6s %-18s %s",
2145 target
->target_number
,
2147 target_name(target
),
2148 target_type_name(target
),
2149 Jim_Nvp_value2name_simple(nvp_target_endian
,
2150 target
->endianness
)->name
,
2151 target
->tap
->dotted_name
,
2153 target
= target
->next
;
2159 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2161 static int powerDropout
;
2162 static int srstAsserted
;
2164 static int runPowerRestore
;
2165 static int runPowerDropout
;
2166 static int runSrstAsserted
;
2167 static int runSrstDeasserted
;
2169 static int sense_handler(void)
2171 static int prevSrstAsserted
;
2172 static int prevPowerdropout
;
2174 int retval
= jtag_power_dropout(&powerDropout
);
2175 if (retval
!= ERROR_OK
)
2179 powerRestored
= prevPowerdropout
&& !powerDropout
;
2181 runPowerRestore
= 1;
2183 long long current
= timeval_ms();
2184 static long long lastPower
;
2185 int waitMore
= lastPower
+ 2000 > current
;
2186 if (powerDropout
&& !waitMore
) {
2187 runPowerDropout
= 1;
2188 lastPower
= current
;
2191 retval
= jtag_srst_asserted(&srstAsserted
);
2192 if (retval
!= ERROR_OK
)
2196 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2198 static long long lastSrst
;
2199 waitMore
= lastSrst
+ 2000 > current
;
2200 if (srstDeasserted
&& !waitMore
) {
2201 runSrstDeasserted
= 1;
2205 if (!prevSrstAsserted
&& srstAsserted
)
2206 runSrstAsserted
= 1;
2208 prevSrstAsserted
= srstAsserted
;
2209 prevPowerdropout
= powerDropout
;
2211 if (srstDeasserted
|| powerRestored
) {
2212 /* Other than logging the event we can't do anything here.
2213 * Issuing a reset is a particularly bad idea as we might
2214 * be inside a reset already.
2221 /* process target state changes */
2222 static int handle_target(void *priv
)
2224 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2225 int retval
= ERROR_OK
;
2227 if (!is_jtag_poll_safe()) {
2228 /* polling is disabled currently */
2232 /* we do not want to recurse here... */
2233 static int recursive
;
2237 /* danger! running these procedures can trigger srst assertions and power dropouts.
2238 * We need to avoid an infinite loop/recursion here and we do that by
2239 * clearing the flags after running these events.
2241 int did_something
= 0;
2242 if (runSrstAsserted
) {
2243 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2244 Jim_Eval(interp
, "srst_asserted");
2247 if (runSrstDeasserted
) {
2248 Jim_Eval(interp
, "srst_deasserted");
2251 if (runPowerDropout
) {
2252 LOG_INFO("Power dropout detected, running power_dropout proc.");
2253 Jim_Eval(interp
, "power_dropout");
2256 if (runPowerRestore
) {
2257 Jim_Eval(interp
, "power_restore");
2261 if (did_something
) {
2262 /* clear detect flags */
2266 /* clear action flags */
2268 runSrstAsserted
= 0;
2269 runSrstDeasserted
= 0;
2270 runPowerRestore
= 0;
2271 runPowerDropout
= 0;
2276 /* Poll targets for state changes unless that's globally disabled.
2277 * Skip targets that are currently disabled.
2279 for (struct target
*target
= all_targets
;
2280 is_jtag_poll_safe() && target
;
2281 target
= target
->next
) {
2282 if (!target
->tap
->enabled
)
2285 if (target
->backoff
.times
> target
->backoff
.count
) {
2286 /* do not poll this time as we failed previously */
2287 target
->backoff
.count
++;
2290 target
->backoff
.count
= 0;
2292 /* only poll target if we've got power and srst isn't asserted */
2293 if (!powerDropout
&& !srstAsserted
) {
2294 /* polling may fail silently until the target has been examined */
2295 retval
= target_poll(target
);
2296 if (retval
!= ERROR_OK
) {
2297 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2298 if (target
->backoff
.times
* polling_interval
< 5000) {
2299 target
->backoff
.times
*= 2;
2300 target
->backoff
.times
++;
2302 LOG_USER("Polling target %s failed, GDB will be halted. Polling again in %dms",
2303 target_name(target
),
2304 target
->backoff
.times
* polling_interval
);
2306 /* Tell GDB to halt the debugger. This allows the user to
2307 * run monitor commands to handle the situation.
2309 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2312 /* Since we succeeded, we reset backoff count */
2313 if (target
->backoff
.times
> 0)
2314 LOG_USER("Polling target %s succeeded again", target_name(target
));
2315 target
->backoff
.times
= 0;
2322 COMMAND_HANDLER(handle_reg_command
)
2324 struct target
*target
;
2325 struct reg
*reg
= NULL
;
2331 target
= get_current_target(CMD_CTX
);
2333 /* list all available registers for the current target */
2334 if (CMD_ARGC
== 0) {
2335 struct reg_cache
*cache
= target
->reg_cache
;
2341 command_print(CMD_CTX
, "===== %s", cache
->name
);
2343 for (i
= 0, reg
= cache
->reg_list
;
2344 i
< cache
->num_regs
;
2345 i
++, reg
++, count
++) {
2346 /* only print cached values if they are valid */
2348 value
= buf_to_str(reg
->value
,
2350 command_print(CMD_CTX
,
2351 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2359 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2364 cache
= cache
->next
;
2370 /* access a single register by its ordinal number */
2371 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2373 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2375 struct reg_cache
*cache
= target
->reg_cache
;
2379 for (i
= 0; i
< cache
->num_regs
; i
++) {
2380 if (count
++ == num
) {
2381 reg
= &cache
->reg_list
[i
];
2387 cache
= cache
->next
;
2391 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2392 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2396 /* access a single register by its name */
2397 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2400 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2405 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2407 /* display a register */
2408 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2409 && (CMD_ARGV
[1][0] <= '9')))) {
2410 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2413 if (reg
->valid
== 0)
2414 reg
->type
->get(reg
);
2415 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2416 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2421 /* set register value */
2422 if (CMD_ARGC
== 2) {
2423 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2426 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2428 reg
->type
->set(reg
, buf
);
2430 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2431 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2439 return ERROR_COMMAND_SYNTAX_ERROR
;
2442 COMMAND_HANDLER(handle_poll_command
)
2444 int retval
= ERROR_OK
;
2445 struct target
*target
= get_current_target(CMD_CTX
);
2447 if (CMD_ARGC
== 0) {
2448 command_print(CMD_CTX
, "background polling: %s",
2449 jtag_poll_get_enabled() ? "on" : "off");
2450 command_print(CMD_CTX
, "TAP: %s (%s)",
2451 target
->tap
->dotted_name
,
2452 target
->tap
->enabled
? "enabled" : "disabled");
2453 if (!target
->tap
->enabled
)
2455 retval
= target_poll(target
);
2456 if (retval
!= ERROR_OK
)
2458 retval
= target_arch_state(target
);
2459 if (retval
!= ERROR_OK
)
2461 } else if (CMD_ARGC
== 1) {
2463 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2464 jtag_poll_set_enabled(enable
);
2466 return ERROR_COMMAND_SYNTAX_ERROR
;
2471 COMMAND_HANDLER(handle_wait_halt_command
)
2474 return ERROR_COMMAND_SYNTAX_ERROR
;
2476 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2477 if (1 == CMD_ARGC
) {
2478 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2479 if (ERROR_OK
!= retval
)
2480 return ERROR_COMMAND_SYNTAX_ERROR
;
2483 struct target
*target
= get_current_target(CMD_CTX
);
2484 return target_wait_state(target
, TARGET_HALTED
, ms
);
2487 /* wait for target state to change. The trick here is to have a low
2488 * latency for short waits and not to suck up all the CPU time
2491 * After 500ms, keep_alive() is invoked
2493 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2496 long long then
= 0, cur
;
2500 retval
= target_poll(target
);
2501 if (retval
!= ERROR_OK
)
2503 if (target
->state
== state
)
2508 then
= timeval_ms();
2509 LOG_DEBUG("waiting for target %s...",
2510 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2516 if ((cur
-then
) > ms
) {
2517 LOG_ERROR("timed out while waiting for target %s",
2518 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2526 COMMAND_HANDLER(handle_halt_command
)
2530 struct target
*target
= get_current_target(CMD_CTX
);
2531 int retval
= target_halt(target
);
2532 if (ERROR_OK
!= retval
)
2535 if (CMD_ARGC
== 1) {
2536 unsigned wait_local
;
2537 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2538 if (ERROR_OK
!= retval
)
2539 return ERROR_COMMAND_SYNTAX_ERROR
;
2544 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2547 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2549 struct target
*target
= get_current_target(CMD_CTX
);
2551 LOG_USER("requesting target halt and executing a soft reset");
2553 target_soft_reset_halt(target
);
2558 COMMAND_HANDLER(handle_reset_command
)
2561 return ERROR_COMMAND_SYNTAX_ERROR
;
2563 enum target_reset_mode reset_mode
= RESET_RUN
;
2564 if (CMD_ARGC
== 1) {
2566 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2567 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
2568 return ERROR_COMMAND_SYNTAX_ERROR
;
2569 reset_mode
= n
->value
;
2572 /* reset *all* targets */
2573 return target_process_reset(CMD_CTX
, reset_mode
);
2577 COMMAND_HANDLER(handle_resume_command
)
2581 return ERROR_COMMAND_SYNTAX_ERROR
;
2583 struct target
*target
= get_current_target(CMD_CTX
);
2585 /* with no CMD_ARGV, resume from current pc, addr = 0,
2586 * with one arguments, addr = CMD_ARGV[0],
2587 * handle breakpoints, not debugging */
2589 if (CMD_ARGC
== 1) {
2590 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2594 return target_resume(target
, current
, addr
, 1, 0);
2597 COMMAND_HANDLER(handle_step_command
)
2600 return ERROR_COMMAND_SYNTAX_ERROR
;
2604 /* with no CMD_ARGV, step from current pc, addr = 0,
2605 * with one argument addr = CMD_ARGV[0],
2606 * handle breakpoints, debugging */
2609 if (CMD_ARGC
== 1) {
2610 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2614 struct target
*target
= get_current_target(CMD_CTX
);
2616 return target
->type
->step(target
, current_pc
, addr
, 1);
2619 static void handle_md_output(struct command_context
*cmd_ctx
,
2620 struct target
*target
, uint32_t address
, unsigned size
,
2621 unsigned count
, const uint8_t *buffer
)
2623 const unsigned line_bytecnt
= 32;
2624 unsigned line_modulo
= line_bytecnt
/ size
;
2626 char output
[line_bytecnt
* 4 + 1];
2627 unsigned output_len
= 0;
2629 const char *value_fmt
;
2632 value_fmt
= "%8.8x ";
2635 value_fmt
= "%4.4x ";
2638 value_fmt
= "%2.2x ";
2641 /* "can't happen", caller checked */
2642 LOG_ERROR("invalid memory read size: %u", size
);
2646 for (unsigned i
= 0; i
< count
; i
++) {
2647 if (i
% line_modulo
== 0) {
2648 output_len
+= snprintf(output
+ output_len
,
2649 sizeof(output
) - output_len
,
2651 (unsigned)(address
+ (i
*size
)));
2655 const uint8_t *value_ptr
= buffer
+ i
* size
;
2658 value
= target_buffer_get_u32(target
, value_ptr
);
2661 value
= target_buffer_get_u16(target
, value_ptr
);
2666 output_len
+= snprintf(output
+ output_len
,
2667 sizeof(output
) - output_len
,
2670 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
2671 command_print(cmd_ctx
, "%s", output
);
2677 COMMAND_HANDLER(handle_md_command
)
2680 return ERROR_COMMAND_SYNTAX_ERROR
;
2683 switch (CMD_NAME
[2]) {
2694 return ERROR_COMMAND_SYNTAX_ERROR
;
2697 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2698 int (*fn
)(struct target
*target
,
2699 uint32_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
2703 fn
= target_read_phys_memory
;
2705 fn
= target_read_memory
;
2706 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2707 return ERROR_COMMAND_SYNTAX_ERROR
;
2710 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2714 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2716 uint8_t *buffer
= calloc(count
, size
);
2718 struct target
*target
= get_current_target(CMD_CTX
);
2719 int retval
= fn(target
, address
, size
, count
, buffer
);
2720 if (ERROR_OK
== retval
)
2721 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2728 typedef int (*target_write_fn
)(struct target
*target
,
2729 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
2731 static int target_write_memory_fast(struct target
*target
,
2732 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
2734 return target_write_buffer(target
, address
, size
* count
, buffer
);
2737 static int target_fill_mem(struct target
*target
,
2746 /* We have to write in reasonably large chunks to be able
2747 * to fill large memory areas with any sane speed */
2748 const unsigned chunk_size
= 16384;
2749 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
2750 if (target_buf
== NULL
) {
2751 LOG_ERROR("Out of memory");
2755 for (unsigned i
= 0; i
< chunk_size
; i
++) {
2756 switch (data_size
) {
2758 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
2761 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
2764 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
2771 int retval
= ERROR_OK
;
2773 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
2776 if (current
> chunk_size
)
2777 current
= chunk_size
;
2778 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
2779 if (retval
!= ERROR_OK
)
2781 /* avoid GDB timeouts */
2790 COMMAND_HANDLER(handle_mw_command
)
2793 return ERROR_COMMAND_SYNTAX_ERROR
;
2794 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2799 fn
= target_write_phys_memory
;
2801 fn
= target_write_memory_fast
;
2802 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2803 return ERROR_COMMAND_SYNTAX_ERROR
;
2806 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2809 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2813 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2815 struct target
*target
= get_current_target(CMD_CTX
);
2817 switch (CMD_NAME
[2]) {
2828 return ERROR_COMMAND_SYNTAX_ERROR
;
2831 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
2834 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2835 uint32_t *min_address
, uint32_t *max_address
)
2837 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2838 return ERROR_COMMAND_SYNTAX_ERROR
;
2840 /* a base address isn't always necessary,
2841 * default to 0x0 (i.e. don't relocate) */
2842 if (CMD_ARGC
>= 2) {
2844 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2845 image
->base_address
= addr
;
2846 image
->base_address_set
= 1;
2848 image
->base_address_set
= 0;
2850 image
->start_address_set
= 0;
2853 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2854 if (CMD_ARGC
== 5) {
2855 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2856 /* use size (given) to find max (required) */
2857 *max_address
+= *min_address
;
2860 if (*min_address
> *max_address
)
2861 return ERROR_COMMAND_SYNTAX_ERROR
;
2866 COMMAND_HANDLER(handle_load_image_command
)
2870 uint32_t image_size
;
2871 uint32_t min_address
= 0;
2872 uint32_t max_address
= 0xffffffff;
2876 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2877 &image
, &min_address
, &max_address
);
2878 if (ERROR_OK
!= retval
)
2881 struct target
*target
= get_current_target(CMD_CTX
);
2883 struct duration bench
;
2884 duration_start(&bench
);
2886 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2891 for (i
= 0; i
< image
.num_sections
; i
++) {
2892 buffer
= malloc(image
.sections
[i
].size
);
2893 if (buffer
== NULL
) {
2894 command_print(CMD_CTX
,
2895 "error allocating buffer for section (%d bytes)",
2896 (int)(image
.sections
[i
].size
));
2900 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
2901 if (retval
!= ERROR_OK
) {
2906 uint32_t offset
= 0;
2907 uint32_t length
= buf_cnt
;
2909 /* DANGER!!! beware of unsigned comparision here!!! */
2911 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
2912 (image
.sections
[i
].base_address
< max_address
)) {
2914 if (image
.sections
[i
].base_address
< min_address
) {
2915 /* clip addresses below */
2916 offset
+= min_address
-image
.sections
[i
].base_address
;
2920 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2921 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2923 retval
= target_write_buffer(target
,
2924 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
2925 if (retval
!= ERROR_OK
) {
2929 image_size
+= length
;
2930 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
2931 (unsigned int)length
,
2932 image
.sections
[i
].base_address
+ offset
);
2938 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
2939 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
2940 "in %fs (%0.3f KiB/s)", image_size
,
2941 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2944 image_close(&image
);
2950 COMMAND_HANDLER(handle_dump_image_command
)
2952 struct fileio fileio
;
2954 int retval
, retvaltemp
;
2955 uint32_t address
, size
;
2956 struct duration bench
;
2957 struct target
*target
= get_current_target(CMD_CTX
);
2960 return ERROR_COMMAND_SYNTAX_ERROR
;
2962 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
2963 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
2965 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
2966 buffer
= malloc(buf_size
);
2970 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
2971 if (retval
!= ERROR_OK
) {
2976 duration_start(&bench
);
2979 size_t size_written
;
2980 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
2981 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2982 if (retval
!= ERROR_OK
)
2985 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2986 if (retval
!= ERROR_OK
)
2989 size
-= this_run_size
;
2990 address
+= this_run_size
;
2995 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
2997 retval
= fileio_size(&fileio
, &filesize
);
2998 if (retval
!= ERROR_OK
)
3000 command_print(CMD_CTX
,
3001 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize
,
3002 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3005 retvaltemp
= fileio_close(&fileio
);
3006 if (retvaltemp
!= ERROR_OK
)
3012 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
3016 uint32_t image_size
;
3019 uint32_t checksum
= 0;
3020 uint32_t mem_checksum
= 0;
3024 struct target
*target
= get_current_target(CMD_CTX
);
3027 return ERROR_COMMAND_SYNTAX_ERROR
;
3030 LOG_ERROR("no target selected");
3034 struct duration bench
;
3035 duration_start(&bench
);
3037 if (CMD_ARGC
>= 2) {
3039 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
3040 image
.base_address
= addr
;
3041 image
.base_address_set
= 1;
3043 image
.base_address_set
= 0;
3044 image
.base_address
= 0x0;
3047 image
.start_address_set
= 0;
3049 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3050 if (retval
!= ERROR_OK
)
3056 for (i
= 0; i
< image
.num_sections
; i
++) {
3057 buffer
= malloc(image
.sections
[i
].size
);
3058 if (buffer
== NULL
) {
3059 command_print(CMD_CTX
,
3060 "error allocating buffer for section (%d bytes)",
3061 (int)(image
.sections
[i
].size
));
3064 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3065 if (retval
!= ERROR_OK
) {
3071 /* calculate checksum of image */
3072 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3073 if (retval
!= ERROR_OK
) {
3078 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3079 if (retval
!= ERROR_OK
) {
3084 if (checksum
!= mem_checksum
) {
3085 /* failed crc checksum, fall back to a binary compare */
3089 LOG_ERROR("checksum mismatch - attempting binary compare");
3091 data
= (uint8_t *)malloc(buf_cnt
);
3093 /* Can we use 32bit word accesses? */
3095 int count
= buf_cnt
;
3096 if ((count
% 4) == 0) {
3100 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
3101 if (retval
== ERROR_OK
) {
3103 for (t
= 0; t
< buf_cnt
; t
++) {
3104 if (data
[t
] != buffer
[t
]) {
3105 command_print(CMD_CTX
,
3106 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3108 (unsigned)(t
+ image
.sections
[i
].base_address
),
3111 if (diffs
++ >= 127) {
3112 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
3124 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
3125 image
.sections
[i
].base_address
,
3130 image_size
+= buf_cnt
;
3133 command_print(CMD_CTX
, "No more differences found.");
3136 retval
= ERROR_FAIL
;
3137 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3138 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3139 "in %fs (%0.3f KiB/s)", image_size
,
3140 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3143 image_close(&image
);
3148 COMMAND_HANDLER(handle_verify_image_command
)
3150 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
3153 COMMAND_HANDLER(handle_test_image_command
)
3155 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
3158 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
3160 struct target
*target
= get_current_target(cmd_ctx
);
3161 struct breakpoint
*breakpoint
= target
->breakpoints
;
3162 while (breakpoint
) {
3163 if (breakpoint
->type
== BKPT_SOFT
) {
3164 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3165 breakpoint
->length
, 16);
3166 command_print(cmd_ctx
, "IVA breakpoint: 0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
3167 breakpoint
->address
,
3169 breakpoint
->set
, buf
);
3172 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3173 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3175 breakpoint
->length
, breakpoint
->set
);
3176 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3177 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3178 breakpoint
->address
,
3179 breakpoint
->length
, breakpoint
->set
);
3180 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3183 command_print(cmd_ctx
, "Breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3184 breakpoint
->address
,
3185 breakpoint
->length
, breakpoint
->set
);
3188 breakpoint
= breakpoint
->next
;
3193 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
3194 uint32_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3196 struct target
*target
= get_current_target(cmd_ctx
);
3199 int retval
= breakpoint_add(target
, addr
, length
, hw
);
3200 if (ERROR_OK
== retval
)
3201 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
3203 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3206 } else if (addr
== 0) {
3207 int retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3208 if (ERROR_OK
== retval
)
3209 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3211 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3215 int retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3216 if (ERROR_OK
== retval
)
3217 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3219 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3226 COMMAND_HANDLER(handle_bp_command
)
3235 return handle_bp_command_list(CMD_CTX
);
3239 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3240 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3241 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3244 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
3246 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3248 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3251 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3252 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
3254 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
3255 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3257 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3262 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3263 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
3264 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
3265 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3268 return ERROR_COMMAND_SYNTAX_ERROR
;
3272 COMMAND_HANDLER(handle_rbp_command
)
3275 return ERROR_COMMAND_SYNTAX_ERROR
;
3278 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3280 struct target
*target
= get_current_target(CMD_CTX
);
3281 breakpoint_remove(target
, addr
);
3286 COMMAND_HANDLER(handle_wp_command
)
3288 struct target
*target
= get_current_target(CMD_CTX
);
3290 if (CMD_ARGC
== 0) {
3291 struct watchpoint
*watchpoint
= target
->watchpoints
;
3293 while (watchpoint
) {
3294 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
3295 ", len: 0x%8.8" PRIx32
3296 ", r/w/a: %i, value: 0x%8.8" PRIx32
3297 ", mask: 0x%8.8" PRIx32
,
3298 watchpoint
->address
,
3300 (int)watchpoint
->rw
,
3303 watchpoint
= watchpoint
->next
;
3308 enum watchpoint_rw type
= WPT_ACCESS
;
3310 uint32_t length
= 0;
3311 uint32_t data_value
= 0x0;
3312 uint32_t data_mask
= 0xffffffff;
3316 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
3319 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3322 switch (CMD_ARGV
[2][0]) {
3333 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3334 return ERROR_COMMAND_SYNTAX_ERROR
;
3338 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3339 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3343 return ERROR_COMMAND_SYNTAX_ERROR
;
3346 int retval
= watchpoint_add(target
, addr
, length
, type
,
3347 data_value
, data_mask
);
3348 if (ERROR_OK
!= retval
)
3349 LOG_ERROR("Failure setting watchpoints");
3354 COMMAND_HANDLER(handle_rwp_command
)
3357 return ERROR_COMMAND_SYNTAX_ERROR
;
3360 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3362 struct target
*target
= get_current_target(CMD_CTX
);
3363 watchpoint_remove(target
, addr
);
3369 * Translate a virtual address to a physical address.
3371 * The low-level target implementation must have logged a detailed error
3372 * which is forwarded to telnet/GDB session.
3374 COMMAND_HANDLER(handle_virt2phys_command
)
3377 return ERROR_COMMAND_SYNTAX_ERROR
;
3380 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
3383 struct target
*target
= get_current_target(CMD_CTX
);
3384 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3385 if (retval
== ERROR_OK
)
3386 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
3391 static void writeData(FILE *f
, const void *data
, size_t len
)
3393 size_t written
= fwrite(data
, 1, len
, f
);
3395 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3398 static void writeLong(FILE *f
, int l
)
3401 for (i
= 0; i
< 4; i
++) {
3402 char c
= (l
>> (i
*8))&0xff;
3403 writeData(f
, &c
, 1);
3408 static void writeString(FILE *f
, char *s
)
3410 writeData(f
, s
, strlen(s
));
3413 /* Dump a gmon.out histogram file. */
3414 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
)
3417 FILE *f
= fopen(filename
, "w");
3420 writeString(f
, "gmon");
3421 writeLong(f
, 0x00000001); /* Version */
3422 writeLong(f
, 0); /* padding */
3423 writeLong(f
, 0); /* padding */
3424 writeLong(f
, 0); /* padding */
3426 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3427 writeData(f
, &zero
, 1);
3429 /* figure out bucket size */
3430 uint32_t min
= samples
[0];
3431 uint32_t max
= samples
[0];
3432 for (i
= 0; i
< sampleNum
; i
++) {
3433 if (min
> samples
[i
])
3435 if (max
< samples
[i
])
3439 int addressSpace
= (max
- min
+ 1);
3440 assert(addressSpace
>= 2);
3442 static const uint32_t maxBuckets
= 16 * 1024; /* maximum buckets. */
3443 uint32_t length
= addressSpace
;
3444 if (length
> maxBuckets
)
3445 length
= maxBuckets
;
3446 int *buckets
= malloc(sizeof(int)*length
);
3447 if (buckets
== NULL
) {
3451 memset(buckets
, 0, sizeof(int) * length
);
3452 for (i
= 0; i
< sampleNum
; i
++) {
3453 uint32_t address
= samples
[i
];
3454 long long a
= address
- min
;
3455 long long b
= length
- 1;
3456 long long c
= addressSpace
- 1;
3457 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
3461 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3462 writeLong(f
, min
); /* low_pc */
3463 writeLong(f
, max
); /* high_pc */
3464 writeLong(f
, length
); /* # of samples */
3465 writeLong(f
, 100); /* KLUDGE! We lie, ca. 100Hz best case. */
3466 writeString(f
, "seconds");
3467 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3468 writeData(f
, &zero
, 1);
3469 writeString(f
, "s");
3471 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3473 char *data
= malloc(2 * length
);
3475 for (i
= 0; i
< length
; i
++) {
3480 data
[i
* 2] = val
&0xff;
3481 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
3484 writeData(f
, data
, length
* 2);
3492 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3493 * which will be used as a random sampling of PC */
3494 COMMAND_HANDLER(handle_profile_command
)
3496 struct target
*target
= get_current_target(CMD_CTX
);
3497 struct timeval timeout
, now
;
3499 gettimeofday(&timeout
, NULL
);
3501 return ERROR_COMMAND_SYNTAX_ERROR
;
3503 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], offset
);
3505 timeval_add_time(&timeout
, offset
, 0);
3508 * @todo: Some cores let us sample the PC without the
3509 * annoying halt/resume step; for example, ARMv7 PCSR.
3510 * Provide a way to use that more efficient mechanism.
3513 command_print(CMD_CTX
, "Starting profiling. Halting and resuming the target as often as we can...");
3515 static const int maxSample
= 10000;
3516 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
3517 if (samples
== NULL
)
3521 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3522 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
3524 int retval
= ERROR_OK
;
3526 target_poll(target
);
3527 if (target
->state
== TARGET_HALTED
) {
3528 uint32_t t
= *((uint32_t *)reg
->value
);
3529 samples
[numSamples
++] = t
;
3530 /* current pc, addr = 0, do not handle breakpoints, not debugging */
3531 retval
= target_resume(target
, 1, 0, 0, 0);
3532 target_poll(target
);
3533 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3534 } else if (target
->state
== TARGET_RUNNING
) {
3535 /* We want to quickly sample the PC. */
3536 retval
= target_halt(target
);
3537 if (retval
!= ERROR_OK
) {
3542 command_print(CMD_CTX
, "Target not halted or running");
3546 if (retval
!= ERROR_OK
)
3549 gettimeofday(&now
, NULL
);
3550 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
)
3551 && (now
.tv_usec
>= timeout
.tv_usec
))) {
3552 command_print(CMD_CTX
, "Profiling completed. %d samples.", numSamples
);
3553 retval
= target_poll(target
);
3554 if (retval
!= ERROR_OK
) {
3558 if (target
->state
== TARGET_HALTED
) {
3559 /* current pc, addr = 0, do not handle
3560 * breakpoints, not debugging */
3561 target_resume(target
, 1, 0, 0, 0);
3563 retval
= target_poll(target
);
3564 if (retval
!= ERROR_OK
) {
3568 writeGmon(samples
, numSamples
, CMD_ARGV
[1]);
3569 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3578 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
3581 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3584 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3588 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3589 valObjPtr
= Jim_NewIntObj(interp
, val
);
3590 if (!nameObjPtr
|| !valObjPtr
) {
3595 Jim_IncrRefCount(nameObjPtr
);
3596 Jim_IncrRefCount(valObjPtr
);
3597 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3598 Jim_DecrRefCount(interp
, nameObjPtr
);
3599 Jim_DecrRefCount(interp
, valObjPtr
);
3601 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3605 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3607 struct command_context
*context
;
3608 struct target
*target
;
3610 context
= current_command_context(interp
);
3611 assert(context
!= NULL
);
3613 target
= get_current_target(context
);
3614 if (target
== NULL
) {
3615 LOG_ERROR("mem2array: no current target");
3619 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
3622 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3630 const char *varname
;
3634 /* argv[1] = name of array to receive the data
3635 * argv[2] = desired width
3636 * argv[3] = memory address
3637 * argv[4] = count of times to read
3640 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3643 varname
= Jim_GetString(argv
[0], &len
);
3644 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3646 e
= Jim_GetLong(interp
, argv
[1], &l
);
3651 e
= Jim_GetLong(interp
, argv
[2], &l
);
3655 e
= Jim_GetLong(interp
, argv
[3], &l
);
3670 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3671 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3675 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3676 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3679 if ((addr
+ (len
* width
)) < addr
) {
3680 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3681 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3684 /* absurd transfer size? */
3686 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3687 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3692 ((width
== 2) && ((addr
& 1) == 0)) ||
3693 ((width
== 4) && ((addr
& 3) == 0))) {
3697 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3698 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3701 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3710 size_t buffersize
= 4096;
3711 uint8_t *buffer
= malloc(buffersize
);
3718 /* Slurp... in buffer size chunks */
3720 count
= len
; /* in objects.. */
3721 if (count
> (buffersize
/ width
))
3722 count
= (buffersize
/ width
);
3724 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3725 if (retval
!= ERROR_OK
) {
3727 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3731 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3732 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3736 v
= 0; /* shut up gcc */
3737 for (i
= 0; i
< count
; i
++, n
++) {
3740 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3743 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3746 v
= buffer
[i
] & 0x0ff;
3749 new_int_array_element(interp
, varname
, n
, v
);
3757 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3762 static int get_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t *val
)
3765 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3769 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3773 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3779 Jim_IncrRefCount(nameObjPtr
);
3780 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3781 Jim_DecrRefCount(interp
, nameObjPtr
);
3783 if (valObjPtr
== NULL
)
3786 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3787 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3792 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3794 struct command_context
*context
;
3795 struct target
*target
;
3797 context
= current_command_context(interp
);
3798 assert(context
!= NULL
);
3800 target
= get_current_target(context
);
3801 if (target
== NULL
) {
3802 LOG_ERROR("array2mem: no current target");
3806 return target_array2mem(interp
, target
, argc
-1, argv
+ 1);
3809 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
3810 int argc
, Jim_Obj
*const *argv
)
3818 const char *varname
;
3822 /* argv[1] = name of array to get the data
3823 * argv[2] = desired width
3824 * argv[3] = memory address
3825 * argv[4] = count to write
3828 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
3831 varname
= Jim_GetString(argv
[0], &len
);
3832 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3834 e
= Jim_GetLong(interp
, argv
[1], &l
);
3839 e
= Jim_GetLong(interp
, argv
[2], &l
);
3843 e
= Jim_GetLong(interp
, argv
[3], &l
);
3858 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3859 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3860 "Invalid width param, must be 8/16/32", NULL
);
3864 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3865 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3866 "array2mem: zero width read?", NULL
);
3869 if ((addr
+ (len
* width
)) < addr
) {
3870 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3871 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3872 "array2mem: addr + len - wraps to zero?", NULL
);
3875 /* absurd transfer size? */
3877 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3878 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3879 "array2mem: absurd > 64K item request", NULL
);
3884 ((width
== 2) && ((addr
& 1) == 0)) ||
3885 ((width
== 4) && ((addr
& 3) == 0))) {
3889 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3890 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3893 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3904 size_t buffersize
= 4096;
3905 uint8_t *buffer
= malloc(buffersize
);
3910 /* Slurp... in buffer size chunks */
3912 count
= len
; /* in objects.. */
3913 if (count
> (buffersize
/ width
))
3914 count
= (buffersize
/ width
);
3916 v
= 0; /* shut up gcc */
3917 for (i
= 0; i
< count
; i
++, n
++) {
3918 get_int_array_element(interp
, varname
, n
, &v
);
3921 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
3924 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
3927 buffer
[i
] = v
& 0x0ff;
3933 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3934 if (retval
!= ERROR_OK
) {
3936 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3940 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3941 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3949 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3954 /* FIX? should we propagate errors here rather than printing them
3957 void target_handle_event(struct target
*target
, enum target_event e
)
3959 struct target_event_action
*teap
;
3961 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3962 if (teap
->event
== e
) {
3963 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3964 target
->target_number
,
3965 target_name(target
),
3966 target_type_name(target
),
3968 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3969 Jim_GetString(teap
->body
, NULL
));
3970 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
) {
3971 Jim_MakeErrorMessage(teap
->interp
);
3972 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
3979 * Returns true only if the target has a handler for the specified event.
3981 bool target_has_event_action(struct target
*target
, enum target_event event
)
3983 struct target_event_action
*teap
;
3985 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3986 if (teap
->event
== event
)
3992 enum target_cfg_param
{
3995 TCFG_WORK_AREA_VIRT
,
3996 TCFG_WORK_AREA_PHYS
,
3997 TCFG_WORK_AREA_SIZE
,
3998 TCFG_WORK_AREA_BACKUP
,
4002 TCFG_CHAIN_POSITION
,
4007 static Jim_Nvp nvp_config_opts
[] = {
4008 { .name
= "-type", .value
= TCFG_TYPE
},
4009 { .name
= "-event", .value
= TCFG_EVENT
},
4010 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
4011 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
4012 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
4013 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
4014 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
4015 { .name
= "-variant", .value
= TCFG_VARIANT
},
4016 { .name
= "-coreid", .value
= TCFG_COREID
},
4017 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
4018 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
4019 { .name
= "-rtos", .value
= TCFG_RTOS
},
4020 { .name
= NULL
, .value
= -1 }
4023 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
4031 /* parse config or cget options ... */
4032 while (goi
->argc
> 0) {
4033 Jim_SetEmptyResult(goi
->interp
);
4034 /* Jim_GetOpt_Debug(goi); */
4036 if (target
->type
->target_jim_configure
) {
4037 /* target defines a configure function */
4038 /* target gets first dibs on parameters */
4039 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
4048 /* otherwise we 'continue' below */
4050 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
4052 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
4058 if (goi
->isconfigure
) {
4059 Jim_SetResultFormatted(goi
->interp
,
4060 "not settable: %s", n
->name
);
4064 if (goi
->argc
!= 0) {
4065 Jim_WrongNumArgs(goi
->interp
,
4066 goi
->argc
, goi
->argv
,
4071 Jim_SetResultString(goi
->interp
,
4072 target_type_name(target
), -1);
4076 if (goi
->argc
== 0) {
4077 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
4081 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
4083 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
4087 if (goi
->isconfigure
) {
4088 if (goi
->argc
!= 1) {
4089 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
4093 if (goi
->argc
!= 0) {
4094 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
4100 struct target_event_action
*teap
;
4102 teap
= target
->event_action
;
4103 /* replace existing? */
4105 if (teap
->event
== (enum target_event
)n
->value
)
4110 if (goi
->isconfigure
) {
4111 bool replace
= true;
4114 teap
= calloc(1, sizeof(*teap
));
4117 teap
->event
= n
->value
;
4118 teap
->interp
= goi
->interp
;
4119 Jim_GetOpt_Obj(goi
, &o
);
4121 Jim_DecrRefCount(teap
->interp
, teap
->body
);
4122 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
4125 * Tcl/TK - "tk events" have a nice feature.
4126 * See the "BIND" command.
4127 * We should support that here.
4128 * You can specify %X and %Y in the event code.
4129 * The idea is: %T - target name.
4130 * The idea is: %N - target number
4131 * The idea is: %E - event name.
4133 Jim_IncrRefCount(teap
->body
);
4136 /* add to head of event list */
4137 teap
->next
= target
->event_action
;
4138 target
->event_action
= teap
;
4140 Jim_SetEmptyResult(goi
->interp
);
4144 Jim_SetEmptyResult(goi
->interp
);
4146 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
4152 case TCFG_WORK_AREA_VIRT
:
4153 if (goi
->isconfigure
) {
4154 target_free_all_working_areas(target
);
4155 e
= Jim_GetOpt_Wide(goi
, &w
);
4158 target
->working_area_virt
= w
;
4159 target
->working_area_virt_spec
= true;
4164 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
4168 case TCFG_WORK_AREA_PHYS
:
4169 if (goi
->isconfigure
) {
4170 target_free_all_working_areas(target
);
4171 e
= Jim_GetOpt_Wide(goi
, &w
);
4174 target
->working_area_phys
= w
;
4175 target
->working_area_phys_spec
= true;
4180 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
4184 case TCFG_WORK_AREA_SIZE
:
4185 if (goi
->isconfigure
) {
4186 target_free_all_working_areas(target
);
4187 e
= Jim_GetOpt_Wide(goi
, &w
);
4190 target
->working_area_size
= w
;
4195 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4199 case TCFG_WORK_AREA_BACKUP
:
4200 if (goi
->isconfigure
) {
4201 target_free_all_working_areas(target
);
4202 e
= Jim_GetOpt_Wide(goi
, &w
);
4205 /* make this exactly 1 or 0 */
4206 target
->backup_working_area
= (!!w
);
4211 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
4212 /* loop for more e*/
4217 if (goi
->isconfigure
) {
4218 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
4220 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
4223 target
->endianness
= n
->value
;
4228 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4229 if (n
->name
== NULL
) {
4230 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4231 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4233 Jim_SetResultString(goi
->interp
, n
->name
, -1);
4238 if (goi
->isconfigure
) {
4239 if (goi
->argc
< 1) {
4240 Jim_SetResultFormatted(goi
->interp
,
4245 if (target
->variant
)
4246 free((void *)(target
->variant
));
4247 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
4250 target
->variant
= strdup(cp
);
4255 Jim_SetResultString(goi
->interp
, target
->variant
, -1);
4260 if (goi
->isconfigure
) {
4261 e
= Jim_GetOpt_Wide(goi
, &w
);
4264 target
->coreid
= (int32_t)w
;
4269 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4273 case TCFG_CHAIN_POSITION
:
4274 if (goi
->isconfigure
) {
4276 struct jtag_tap
*tap
;
4277 target_free_all_working_areas(target
);
4278 e
= Jim_GetOpt_Obj(goi
, &o_t
);
4281 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
4284 /* make this exactly 1 or 0 */
4290 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
4291 /* loop for more e*/
4294 if (goi
->isconfigure
) {
4295 e
= Jim_GetOpt_Wide(goi
, &w
);
4298 target
->dbgbase
= (uint32_t)w
;
4299 target
->dbgbase_set
= true;
4304 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
4311 int result
= rtos_create(goi
, target
);
4312 if (result
!= JIM_OK
)
4318 } /* while (goi->argc) */
4321 /* done - we return */
4325 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4329 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4330 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4331 int need_args
= 1 + goi
.isconfigure
;
4332 if (goi
.argc
< need_args
) {
4333 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4335 ? "missing: -option VALUE ..."
4336 : "missing: -option ...");
4339 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4340 return target_configure(&goi
, target
);
4343 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4345 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4348 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4350 if (goi
.argc
< 2 || goi
.argc
> 4) {
4351 Jim_SetResultFormatted(goi
.interp
,
4352 "usage: %s [phys] <address> <data> [<count>]", cmd_name
);
4357 fn
= target_write_memory_fast
;
4360 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4362 struct Jim_Obj
*obj
;
4363 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4367 fn
= target_write_phys_memory
;
4371 e
= Jim_GetOpt_Wide(&goi
, &a
);
4376 e
= Jim_GetOpt_Wide(&goi
, &b
);
4381 if (goi
.argc
== 1) {
4382 e
= Jim_GetOpt_Wide(&goi
, &c
);
4387 /* all args must be consumed */
4391 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4393 if (strcasecmp(cmd_name
, "mww") == 0)
4395 else if (strcasecmp(cmd_name
, "mwh") == 0)
4397 else if (strcasecmp(cmd_name
, "mwb") == 0)
4400 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4404 return (target_fill_mem(target
, a
, fn
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4408 * @brief Reads an array of words/halfwords/bytes from target memory starting at specified address.
4410 * Usage: mdw [phys] <address> [<count>] - for 32 bit reads
4411 * mdh [phys] <address> [<count>] - for 16 bit reads
4412 * mdb [phys] <address> [<count>] - for 8 bit reads
4414 * Count defaults to 1.
4416 * Calls target_read_memory or target_read_phys_memory depending on
4417 * the presence of the "phys" argument
4418 * Reads the target memory in blocks of max. 32 bytes, and returns an array of ints formatted
4419 * to int representation in base16.
4420 * Also outputs read data in a human readable form using command_print
4422 * @param phys if present target_read_phys_memory will be used instead of target_read_memory
4423 * @param address address where to start the read. May be specified in decimal or hex using the standard "0x" prefix
4424 * @param count optional count parameter to read an array of values. If not specified, defaults to 1.
4425 * @returns: JIM_ERR on error or JIM_OK on success and sets the result string to an array of ascii formatted numbers
4426 * on success, with [<count>] number of elements.
4428 * In case of little endian target:
4429 * Example1: "mdw 0x00000000" returns "10123456"
4430 * Exmaple2: "mdh 0x00000000 1" returns "3456"
4431 * Example3: "mdb 0x00000000" returns "56"
4432 * Example4: "mdh 0x00000000 2" returns "3456 1012"
4433 * Example5: "mdb 0x00000000 3" returns "56 34 12"
4435 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4437 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4440 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4442 if ((goi
.argc
< 1) || (goi
.argc
> 3)) {
4443 Jim_SetResultFormatted(goi
.interp
,
4444 "usage: %s [phys] <address> [<count>]", cmd_name
);
4448 int (*fn
)(struct target
*target
,
4449 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
4450 fn
= target_read_memory
;
4453 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4455 struct Jim_Obj
*obj
;
4456 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4460 fn
= target_read_phys_memory
;
4463 /* Read address parameter */
4465 e
= Jim_GetOpt_Wide(&goi
, &addr
);
4469 /* If next parameter exists, read it out as the count parameter, if not, set it to 1 (default) */
4471 if (goi
.argc
== 1) {
4472 e
= Jim_GetOpt_Wide(&goi
, &count
);
4478 /* all args must be consumed */
4482 jim_wide dwidth
= 1; /* shut up gcc */
4483 if (strcasecmp(cmd_name
, "mdw") == 0)
4485 else if (strcasecmp(cmd_name
, "mdh") == 0)
4487 else if (strcasecmp(cmd_name
, "mdb") == 0)
4490 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4494 /* convert count to "bytes" */
4495 int bytes
= count
* dwidth
;
4497 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4498 uint8_t target_buf
[32];
4501 y
= (bytes
< 16) ? bytes
: 16; /* y = min(bytes, 16); */
4503 /* Try to read out next block */
4504 e
= fn(target
, addr
, dwidth
, y
/ dwidth
, target_buf
);
4506 if (e
!= ERROR_OK
) {
4507 Jim_SetResultFormatted(interp
, "error reading target @ 0x%08lx", (long)addr
);
4511 command_print_sameline(NULL
, "0x%08x ", (int)(addr
));
4514 for (x
= 0; x
< 16 && x
< y
; x
+= 4) {
4515 z
= target_buffer_get_u32(target
, &(target_buf
[x
]));
4516 command_print_sameline(NULL
, "%08x ", (int)(z
));
4518 for (; (x
< 16) ; x
+= 4)
4519 command_print_sameline(NULL
, " ");
4522 for (x
= 0; x
< 16 && x
< y
; x
+= 2) {
4523 z
= target_buffer_get_u16(target
, &(target_buf
[x
]));
4524 command_print_sameline(NULL
, "%04x ", (int)(z
));
4526 for (; (x
< 16) ; x
+= 2)
4527 command_print_sameline(NULL
, " ");
4531 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
4532 z
= target_buffer_get_u8(target
, &(target_buf
[x
]));
4533 command_print_sameline(NULL
, "%02x ", (int)(z
));
4535 for (; (x
< 16) ; x
+= 1)
4536 command_print_sameline(NULL
, " ");
4539 /* ascii-ify the bytes */
4540 for (x
= 0 ; x
< y
; x
++) {
4541 if ((target_buf
[x
] >= 0x20) &&
4542 (target_buf
[x
] <= 0x7e)) {
4546 target_buf
[x
] = '.';
4551 target_buf
[x
] = ' ';
4556 /* print - with a newline */
4557 command_print_sameline(NULL
, "%s\n", target_buf
);
4565 static int jim_target_mem2array(Jim_Interp
*interp
,
4566 int argc
, Jim_Obj
*const *argv
)
4568 struct target
*target
= Jim_CmdPrivData(interp
);
4569 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4572 static int jim_target_array2mem(Jim_Interp
*interp
,
4573 int argc
, Jim_Obj
*const *argv
)
4575 struct target
*target
= Jim_CmdPrivData(interp
);
4576 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4579 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4581 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
4585 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4588 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4591 struct target
*target
= Jim_CmdPrivData(interp
);
4592 if (!target
->tap
->enabled
)
4593 return jim_target_tap_disabled(interp
);
4595 int e
= target
->type
->examine(target
);
4601 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4604 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4607 struct target
*target
= Jim_CmdPrivData(interp
);
4609 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
4615 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4618 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4621 struct target
*target
= Jim_CmdPrivData(interp
);
4622 if (!target
->tap
->enabled
)
4623 return jim_target_tap_disabled(interp
);
4626 if (!(target_was_examined(target
)))
4627 e
= ERROR_TARGET_NOT_EXAMINED
;
4629 e
= target
->type
->poll(target
);
4635 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4638 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4640 if (goi
.argc
!= 2) {
4641 Jim_WrongNumArgs(interp
, 0, argv
,
4642 "([tT]|[fF]|assert|deassert) BOOL");
4647 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4649 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4652 /* the halt or not param */
4654 e
= Jim_GetOpt_Wide(&goi
, &a
);
4658 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4659 if (!target
->tap
->enabled
)
4660 return jim_target_tap_disabled(interp
);
4661 if (!(target_was_examined(target
))) {
4662 LOG_ERROR("Target not examined yet");
4663 return ERROR_TARGET_NOT_EXAMINED
;
4665 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
4666 Jim_SetResultFormatted(interp
,
4667 "No target-specific reset for %s",
4668 target_name(target
));
4671 /* determine if we should halt or not. */
4672 target
->reset_halt
= !!a
;
4673 /* When this happens - all workareas are invalid. */
4674 target_free_all_working_areas_restore(target
, 0);
4677 if (n
->value
== NVP_ASSERT
)
4678 e
= target
->type
->assert_reset(target
);
4680 e
= target
->type
->deassert_reset(target
);
4681 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4684 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4687 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4690 struct target
*target
= Jim_CmdPrivData(interp
);
4691 if (!target
->tap
->enabled
)
4692 return jim_target_tap_disabled(interp
);
4693 int e
= target
->type
->halt(target
);
4694 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4697 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4700 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4702 /* params: <name> statename timeoutmsecs */
4703 if (goi
.argc
!= 2) {
4704 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4705 Jim_SetResultFormatted(goi
.interp
,
4706 "%s <state_name> <timeout_in_msec>", cmd_name
);
4711 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4713 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
, 1);
4717 e
= Jim_GetOpt_Wide(&goi
, &a
);
4720 struct target
*target
= Jim_CmdPrivData(interp
);
4721 if (!target
->tap
->enabled
)
4722 return jim_target_tap_disabled(interp
);
4724 e
= target_wait_state(target
, n
->value
, a
);
4725 if (e
!= ERROR_OK
) {
4726 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
4727 Jim_SetResultFormatted(goi
.interp
,
4728 "target: %s wait %s fails (%#s) %s",
4729 target_name(target
), n
->name
,
4730 eObj
, target_strerror_safe(e
));
4731 Jim_FreeNewObj(interp
, eObj
);
4736 /* List for human, Events defined for this target.
4737 * scripts/programs should use 'name cget -event NAME'
4739 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4741 struct command_context
*cmd_ctx
= current_command_context(interp
);
4742 assert(cmd_ctx
!= NULL
);
4744 struct target
*target
= Jim_CmdPrivData(interp
);
4745 struct target_event_action
*teap
= target
->event_action
;
4746 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4747 target
->target_number
,
4748 target_name(target
));
4749 command_print(cmd_ctx
, "%-25s | Body", "Event");
4750 command_print(cmd_ctx
, "------------------------- | "
4751 "----------------------------------------");
4753 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
4754 command_print(cmd_ctx
, "%-25s | %s",
4755 opt
->name
, Jim_GetString(teap
->body
, NULL
));
4758 command_print(cmd_ctx
, "***END***");
4761 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4764 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4767 struct target
*target
= Jim_CmdPrivData(interp
);
4768 Jim_SetResultString(interp
, target_state_name(target
), -1);
4771 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4774 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4775 if (goi
.argc
!= 1) {
4776 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4777 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
4781 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4783 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4786 struct target
*target
= Jim_CmdPrivData(interp
);
4787 target_handle_event(target
, n
->value
);
4791 static const struct command_registration target_instance_command_handlers
[] = {
4793 .name
= "configure",
4794 .mode
= COMMAND_CONFIG
,
4795 .jim_handler
= jim_target_configure
,
4796 .help
= "configure a new target for use",
4797 .usage
= "[target_attribute ...]",
4801 .mode
= COMMAND_ANY
,
4802 .jim_handler
= jim_target_configure
,
4803 .help
= "returns the specified target attribute",
4804 .usage
= "target_attribute",
4808 .mode
= COMMAND_EXEC
,
4809 .jim_handler
= jim_target_mw
,
4810 .help
= "Write 32-bit word(s) to target memory",
4811 .usage
= "address data [count]",
4815 .mode
= COMMAND_EXEC
,
4816 .jim_handler
= jim_target_mw
,
4817 .help
= "Write 16-bit half-word(s) to target memory",
4818 .usage
= "address data [count]",
4822 .mode
= COMMAND_EXEC
,
4823 .jim_handler
= jim_target_mw
,
4824 .help
= "Write byte(s) to target memory",
4825 .usage
= "address data [count]",
4829 .mode
= COMMAND_EXEC
,
4830 .jim_handler
= jim_target_md
,
4831 .help
= "Display target memory as 32-bit words",
4832 .usage
= "address [count]",
4836 .mode
= COMMAND_EXEC
,
4837 .jim_handler
= jim_target_md
,
4838 .help
= "Display target memory as 16-bit half-words",
4839 .usage
= "address [count]",
4843 .mode
= COMMAND_EXEC
,
4844 .jim_handler
= jim_target_md
,
4845 .help
= "Display target memory as 8-bit bytes",
4846 .usage
= "address [count]",
4849 .name
= "array2mem",
4850 .mode
= COMMAND_EXEC
,
4851 .jim_handler
= jim_target_array2mem
,
4852 .help
= "Writes Tcl array of 8/16/32 bit numbers "
4854 .usage
= "arrayname bitwidth address count",
4857 .name
= "mem2array",
4858 .mode
= COMMAND_EXEC
,
4859 .jim_handler
= jim_target_mem2array
,
4860 .help
= "Loads Tcl array of 8/16/32 bit numbers "
4861 "from target memory",
4862 .usage
= "arrayname bitwidth address count",
4865 .name
= "eventlist",
4866 .mode
= COMMAND_EXEC
,
4867 .jim_handler
= jim_target_event_list
,
4868 .help
= "displays a table of events defined for this target",
4872 .mode
= COMMAND_EXEC
,
4873 .jim_handler
= jim_target_current_state
,
4874 .help
= "displays the current state of this target",
4877 .name
= "arp_examine",
4878 .mode
= COMMAND_EXEC
,
4879 .jim_handler
= jim_target_examine
,
4880 .help
= "used internally for reset processing",
4883 .name
= "arp_halt_gdb",
4884 .mode
= COMMAND_EXEC
,
4885 .jim_handler
= jim_target_halt_gdb
,
4886 .help
= "used internally for reset processing to halt GDB",
4890 .mode
= COMMAND_EXEC
,
4891 .jim_handler
= jim_target_poll
,
4892 .help
= "used internally for reset processing",
4895 .name
= "arp_reset",
4896 .mode
= COMMAND_EXEC
,
4897 .jim_handler
= jim_target_reset
,
4898 .help
= "used internally for reset processing",
4902 .mode
= COMMAND_EXEC
,
4903 .jim_handler
= jim_target_halt
,
4904 .help
= "used internally for reset processing",
4907 .name
= "arp_waitstate",
4908 .mode
= COMMAND_EXEC
,
4909 .jim_handler
= jim_target_wait_state
,
4910 .help
= "used internally for reset processing",
4913 .name
= "invoke-event",
4914 .mode
= COMMAND_EXEC
,
4915 .jim_handler
= jim_target_invoke_event
,
4916 .help
= "invoke handler for specified event",
4917 .usage
= "event_name",
4919 COMMAND_REGISTRATION_DONE
4922 static int target_create(Jim_GetOptInfo
*goi
)
4930 struct target
*target
;
4931 struct command_context
*cmd_ctx
;
4933 cmd_ctx
= current_command_context(goi
->interp
);
4934 assert(cmd_ctx
!= NULL
);
4936 if (goi
->argc
< 3) {
4937 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
4942 Jim_GetOpt_Obj(goi
, &new_cmd
);
4943 /* does this command exist? */
4944 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
4946 cp
= Jim_GetString(new_cmd
, NULL
);
4947 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
4952 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
4956 /* now does target type exist */
4957 for (x
= 0 ; target_types
[x
] ; x
++) {
4958 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
4963 /* check for deprecated name */
4964 if (target_types
[x
]->deprecated_name
) {
4965 if (0 == strcmp(cp
, target_types
[x
]->deprecated_name
)) {
4967 LOG_WARNING("target name is deprecated use: \'%s\'", target_types
[x
]->name
);
4972 if (target_types
[x
] == NULL
) {
4973 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
4974 for (x
= 0 ; target_types
[x
] ; x
++) {
4975 if (target_types
[x
+ 1]) {
4976 Jim_AppendStrings(goi
->interp
,
4977 Jim_GetResult(goi
->interp
),
4978 target_types
[x
]->name
,
4981 Jim_AppendStrings(goi
->interp
,
4982 Jim_GetResult(goi
->interp
),
4984 target_types
[x
]->name
, NULL
);
4991 target
= calloc(1, sizeof(struct target
));
4992 /* set target number */
4993 target
->target_number
= new_target_number();
4995 /* allocate memory for each unique target type */
4996 target
->type
= (struct target_type
*)calloc(1, sizeof(struct target_type
));
4998 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
5000 /* will be set by "-endian" */
5001 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5003 /* default to first core, override with -coreid */
5006 target
->working_area
= 0x0;
5007 target
->working_area_size
= 0x0;
5008 target
->working_areas
= NULL
;
5009 target
->backup_working_area
= 0;
5011 target
->state
= TARGET_UNKNOWN
;
5012 target
->debug_reason
= DBG_REASON_UNDEFINED
;
5013 target
->reg_cache
= NULL
;
5014 target
->breakpoints
= NULL
;
5015 target
->watchpoints
= NULL
;
5016 target
->next
= NULL
;
5017 target
->arch_info
= NULL
;
5019 target
->display
= 1;
5021 target
->halt_issued
= false;
5023 /* initialize trace information */
5024 target
->trace_info
= malloc(sizeof(struct trace
));
5025 target
->trace_info
->num_trace_points
= 0;
5026 target
->trace_info
->trace_points_size
= 0;
5027 target
->trace_info
->trace_points
= NULL
;
5028 target
->trace_info
->trace_history_size
= 0;
5029 target
->trace_info
->trace_history
= NULL
;
5030 target
->trace_info
->trace_history_pos
= 0;
5031 target
->trace_info
->trace_history_overflowed
= 0;
5033 target
->dbgmsg
= NULL
;
5034 target
->dbg_msg_enabled
= 0;
5036 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5038 target
->rtos
= NULL
;
5039 target
->rtos_auto_detect
= false;
5041 /* Do the rest as "configure" options */
5042 goi
->isconfigure
= 1;
5043 e
= target_configure(goi
, target
);
5045 if (target
->tap
== NULL
) {
5046 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
5056 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
5057 /* default endian to little if not specified */
5058 target
->endianness
= TARGET_LITTLE_ENDIAN
;
5061 /* incase variant is not set */
5062 if (!target
->variant
)
5063 target
->variant
= strdup("");
5065 cp
= Jim_GetString(new_cmd
, NULL
);
5066 target
->cmd_name
= strdup(cp
);
5068 /* create the target specific commands */
5069 if (target
->type
->commands
) {
5070 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
5072 LOG_ERROR("unable to register '%s' commands", cp
);
5074 if (target
->type
->target_create
)
5075 (*(target
->type
->target_create
))(target
, goi
->interp
);
5077 /* append to end of list */
5079 struct target
**tpp
;
5080 tpp
= &(all_targets
);
5082 tpp
= &((*tpp
)->next
);
5086 /* now - create the new target name command */
5087 const struct command_registration target_subcommands
[] = {
5089 .chain
= target_instance_command_handlers
,
5092 .chain
= target
->type
->commands
,
5094 COMMAND_REGISTRATION_DONE
5096 const struct command_registration target_commands
[] = {
5099 .mode
= COMMAND_ANY
,
5100 .help
= "target command group",
5102 .chain
= target_subcommands
,
5104 COMMAND_REGISTRATION_DONE
5106 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
5110 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
5112 command_set_handler_data(c
, target
);
5114 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
5117 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5120 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5123 struct command_context
*cmd_ctx
= current_command_context(interp
);
5124 assert(cmd_ctx
!= NULL
);
5126 Jim_SetResultString(interp
, target_name(get_current_target(cmd_ctx
)), -1);
5130 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5133 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5136 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5137 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++) {
5138 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5139 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
5144 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5147 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5150 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5151 struct target
*target
= all_targets
;
5153 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5154 Jim_NewStringObj(interp
, target_name(target
), -1));
5155 target
= target
->next
;
5160 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5163 const char *targetname
;
5165 struct target
*target
= (struct target
*) NULL
;
5166 struct target_list
*head
, *curr
, *new;
5167 curr
= (struct target_list
*) NULL
;
5168 head
= (struct target_list
*) NULL
;
5171 LOG_DEBUG("%d", argc
);
5172 /* argv[1] = target to associate in smp
5173 * argv[2] = target to assoicate in smp
5177 for (i
= 1; i
< argc
; i
++) {
5179 targetname
= Jim_GetString(argv
[i
], &len
);
5180 target
= get_target(targetname
);
5181 LOG_DEBUG("%s ", targetname
);
5183 new = malloc(sizeof(struct target_list
));
5184 new->target
= target
;
5185 new->next
= (struct target_list
*)NULL
;
5186 if (head
== (struct target_list
*)NULL
) {
5195 /* now parse the list of cpu and put the target in smp mode*/
5198 while (curr
!= (struct target_list
*)NULL
) {
5199 target
= curr
->target
;
5201 target
->head
= head
;
5205 if (target
&& target
->rtos
)
5206 retval
= rtos_smp_init(head
->target
);
5212 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5215 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5217 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5218 "<name> <target_type> [<target_options> ...]");
5221 return target_create(&goi
);
5224 static int jim_target_number(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5227 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5229 /* It's OK to remove this mechanism sometime after August 2010 or so */
5230 LOG_WARNING("don't use numbers as target identifiers; use names");
5231 if (goi
.argc
!= 1) {
5232 Jim_SetResultFormatted(goi
.interp
, "usage: target number <number>");
5236 int e
= Jim_GetOpt_Wide(&goi
, &w
);
5240 struct target
*target
;
5241 for (target
= all_targets
; NULL
!= target
; target
= target
->next
) {
5242 if (target
->target_number
!= w
)
5245 Jim_SetResultString(goi
.interp
, target_name(target
), -1);
5249 Jim_Obj
*wObj
= Jim_NewIntObj(goi
.interp
, w
);
5250 Jim_SetResultFormatted(goi
.interp
,
5251 "Target: number %#s does not exist", wObj
);
5252 Jim_FreeNewObj(interp
, wObj
);
5257 static int jim_target_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5260 Jim_WrongNumArgs(interp
, 1, argv
, "<no parameters>");
5264 struct target
*target
= all_targets
;
5265 while (NULL
!= target
) {
5266 target
= target
->next
;
5269 Jim_SetResult(interp
, Jim_NewIntObj(interp
, count
));
5273 static const struct command_registration target_subcommand_handlers
[] = {
5276 .mode
= COMMAND_CONFIG
,
5277 .handler
= handle_target_init_command
,
5278 .help
= "initialize targets",
5282 /* REVISIT this should be COMMAND_CONFIG ... */
5283 .mode
= COMMAND_ANY
,
5284 .jim_handler
= jim_target_create
,
5285 .usage
= "name type '-chain-position' name [options ...]",
5286 .help
= "Creates and selects a new target",
5290 .mode
= COMMAND_ANY
,
5291 .jim_handler
= jim_target_current
,
5292 .help
= "Returns the currently selected target",
5296 .mode
= COMMAND_ANY
,
5297 .jim_handler
= jim_target_types
,
5298 .help
= "Returns the available target types as "
5299 "a list of strings",
5303 .mode
= COMMAND_ANY
,
5304 .jim_handler
= jim_target_names
,
5305 .help
= "Returns the names of all targets as a list of strings",
5309 .mode
= COMMAND_ANY
,
5310 .jim_handler
= jim_target_number
,
5312 .help
= "Returns the name of the numbered target "
5317 .mode
= COMMAND_ANY
,
5318 .jim_handler
= jim_target_count
,
5319 .help
= "Returns the number of targets as an integer "
5324 .mode
= COMMAND_ANY
,
5325 .jim_handler
= jim_target_smp
,
5326 .usage
= "targetname1 targetname2 ...",
5327 .help
= "gather several target in a smp list"
5330 COMMAND_REGISTRATION_DONE
5340 static int fastload_num
;
5341 static struct FastLoad
*fastload
;
5343 static void free_fastload(void)
5345 if (fastload
!= NULL
) {
5347 for (i
= 0; i
< fastload_num
; i
++) {
5348 if (fastload
[i
].data
)
5349 free(fastload
[i
].data
);
5356 COMMAND_HANDLER(handle_fast_load_image_command
)
5360 uint32_t image_size
;
5361 uint32_t min_address
= 0;
5362 uint32_t max_address
= 0xffffffff;
5367 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
5368 &image
, &min_address
, &max_address
);
5369 if (ERROR_OK
!= retval
)
5372 struct duration bench
;
5373 duration_start(&bench
);
5375 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5376 if (retval
!= ERROR_OK
)
5381 fastload_num
= image
.num_sections
;
5382 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5383 if (fastload
== NULL
) {
5384 command_print(CMD_CTX
, "out of memory");
5385 image_close(&image
);
5388 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5389 for (i
= 0; i
< image
.num_sections
; i
++) {
5390 buffer
= malloc(image
.sections
[i
].size
);
5391 if (buffer
== NULL
) {
5392 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5393 (int)(image
.sections
[i
].size
));
5394 retval
= ERROR_FAIL
;
5398 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
5399 if (retval
!= ERROR_OK
) {
5404 uint32_t offset
= 0;
5405 uint32_t length
= buf_cnt
;
5407 /* DANGER!!! beware of unsigned comparision here!!! */
5409 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
5410 (image
.sections
[i
].base_address
< max_address
)) {
5411 if (image
.sections
[i
].base_address
< min_address
) {
5412 /* clip addresses below */
5413 offset
+= min_address
-image
.sections
[i
].base_address
;
5417 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5418 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5420 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5421 fastload
[i
].data
= malloc(length
);
5422 if (fastload
[i
].data
== NULL
) {
5424 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5426 retval
= ERROR_FAIL
;
5429 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5430 fastload
[i
].length
= length
;
5432 image_size
+= length
;
5433 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
5434 (unsigned int)length
,
5435 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
5441 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
5442 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
5443 "in %fs (%0.3f KiB/s)", image_size
,
5444 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
5446 command_print(CMD_CTX
,
5447 "WARNING: image has not been loaded to target!"
5448 "You can issue a 'fast_load' to finish loading.");
5451 image_close(&image
);
5453 if (retval
!= ERROR_OK
)
5459 COMMAND_HANDLER(handle_fast_load_command
)
5462 return ERROR_COMMAND_SYNTAX_ERROR
;
5463 if (fastload
== NULL
) {
5464 LOG_ERROR("No image in memory");
5468 int ms
= timeval_ms();
5470 int retval
= ERROR_OK
;
5471 for (i
= 0; i
< fastload_num
; i
++) {
5472 struct target
*target
= get_current_target(CMD_CTX
);
5473 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
5474 (unsigned int)(fastload
[i
].address
),
5475 (unsigned int)(fastload
[i
].length
));
5476 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5477 if (retval
!= ERROR_OK
)
5479 size
+= fastload
[i
].length
;
5481 if (retval
== ERROR_OK
) {
5482 int after
= timeval_ms();
5483 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5488 static const struct command_registration target_command_handlers
[] = {
5491 .handler
= handle_targets_command
,
5492 .mode
= COMMAND_ANY
,
5493 .help
= "change current default target (one parameter) "
5494 "or prints table of all targets (no parameters)",
5495 .usage
= "[target]",
5499 .mode
= COMMAND_CONFIG
,
5500 .help
= "configure target",
5502 .chain
= target_subcommand_handlers
,
5504 COMMAND_REGISTRATION_DONE
5507 int target_register_commands(struct command_context
*cmd_ctx
)
5509 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5512 static bool target_reset_nag
= true;
5514 bool get_target_reset_nag(void)
5516 return target_reset_nag
;
5519 COMMAND_HANDLER(handle_target_reset_nag
)
5521 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
5522 &target_reset_nag
, "Nag after each reset about options to improve "
5526 COMMAND_HANDLER(handle_ps_command
)
5528 struct target
*target
= get_current_target(CMD_CTX
);
5530 if (target
->state
!= TARGET_HALTED
) {
5531 LOG_INFO("target not halted !!");
5535 if ((target
->rtos
) && (target
->rtos
->type
)
5536 && (target
->rtos
->type
->ps_command
)) {
5537 display
= target
->rtos
->type
->ps_command(target
);
5538 command_print(CMD_CTX
, "%s", display
);
5543 return ERROR_TARGET_FAILURE
;
5547 static const struct command_registration target_exec_command_handlers
[] = {
5549 .name
= "fast_load_image",
5550 .handler
= handle_fast_load_image_command
,
5551 .mode
= COMMAND_ANY
,
5552 .help
= "Load image into server memory for later use by "
5553 "fast_load; primarily for profiling",
5554 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5555 "[min_address [max_length]]",
5558 .name
= "fast_load",
5559 .handler
= handle_fast_load_command
,
5560 .mode
= COMMAND_EXEC
,
5561 .help
= "loads active fast load image to current target "
5562 "- mainly for profiling purposes",
5567 .handler
= handle_profile_command
,
5568 .mode
= COMMAND_EXEC
,
5569 .usage
= "seconds filename",
5570 .help
= "profiling samples the CPU PC",
5572 /** @todo don't register virt2phys() unless target supports it */
5574 .name
= "virt2phys",
5575 .handler
= handle_virt2phys_command
,
5576 .mode
= COMMAND_ANY
,
5577 .help
= "translate a virtual address into a physical address",
5578 .usage
= "virtual_address",
5582 .handler
= handle_reg_command
,
5583 .mode
= COMMAND_EXEC
,
5584 .help
= "display or set a register; with no arguments, "
5585 "displays all registers and their values",
5586 .usage
= "[(register_name|register_number) [value]]",
5590 .handler
= handle_poll_command
,
5591 .mode
= COMMAND_EXEC
,
5592 .help
= "poll target state; or reconfigure background polling",
5593 .usage
= "['on'|'off']",
5596 .name
= "wait_halt",
5597 .handler
= handle_wait_halt_command
,
5598 .mode
= COMMAND_EXEC
,
5599 .help
= "wait up to the specified number of milliseconds "
5600 "(default 5000) for a previously requested halt",
5601 .usage
= "[milliseconds]",
5605 .handler
= handle_halt_command
,
5606 .mode
= COMMAND_EXEC
,
5607 .help
= "request target to halt, then wait up to the specified"
5608 "number of milliseconds (default 5000) for it to complete",
5609 .usage
= "[milliseconds]",
5613 .handler
= handle_resume_command
,
5614 .mode
= COMMAND_EXEC
,
5615 .help
= "resume target execution from current PC or address",
5616 .usage
= "[address]",
5620 .handler
= handle_reset_command
,
5621 .mode
= COMMAND_EXEC
,
5622 .usage
= "[run|halt|init]",
5623 .help
= "Reset all targets into the specified mode."
5624 "Default reset mode is run, if not given.",
5627 .name
= "soft_reset_halt",
5628 .handler
= handle_soft_reset_halt_command
,
5629 .mode
= COMMAND_EXEC
,
5631 .help
= "halt the target and do a soft reset",
5635 .handler
= handle_step_command
,
5636 .mode
= COMMAND_EXEC
,
5637 .help
= "step one instruction from current PC or address",
5638 .usage
= "[address]",
5642 .handler
= handle_md_command
,
5643 .mode
= COMMAND_EXEC
,
5644 .help
= "display memory words",
5645 .usage
= "['phys'] address [count]",
5649 .handler
= handle_md_command
,
5650 .mode
= COMMAND_EXEC
,
5651 .help
= "display memory half-words",
5652 .usage
= "['phys'] address [count]",
5656 .handler
= handle_md_command
,
5657 .mode
= COMMAND_EXEC
,
5658 .help
= "display memory bytes",
5659 .usage
= "['phys'] address [count]",
5663 .handler
= handle_mw_command
,
5664 .mode
= COMMAND_EXEC
,
5665 .help
= "write memory word",
5666 .usage
= "['phys'] address value [count]",
5670 .handler
= handle_mw_command
,
5671 .mode
= COMMAND_EXEC
,
5672 .help
= "write memory half-word",
5673 .usage
= "['phys'] address value [count]",
5677 .handler
= handle_mw_command
,
5678 .mode
= COMMAND_EXEC
,
5679 .help
= "write memory byte",
5680 .usage
= "['phys'] address value [count]",
5684 .handler
= handle_bp_command
,
5685 .mode
= COMMAND_EXEC
,
5686 .help
= "list or set hardware or software breakpoint",
5687 .usage
= "<address> [<asid>]<length> ['hw'|'hw_ctx']",
5691 .handler
= handle_rbp_command
,
5692 .mode
= COMMAND_EXEC
,
5693 .help
= "remove breakpoint",
5698 .handler
= handle_wp_command
,
5699 .mode
= COMMAND_EXEC
,
5700 .help
= "list (no params) or create watchpoints",
5701 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
5705 .handler
= handle_rwp_command
,
5706 .mode
= COMMAND_EXEC
,
5707 .help
= "remove watchpoint",
5711 .name
= "load_image",
5712 .handler
= handle_load_image_command
,
5713 .mode
= COMMAND_EXEC
,
5714 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5715 "[min_address] [max_length]",
5718 .name
= "dump_image",
5719 .handler
= handle_dump_image_command
,
5720 .mode
= COMMAND_EXEC
,
5721 .usage
= "filename address size",
5724 .name
= "verify_image",
5725 .handler
= handle_verify_image_command
,
5726 .mode
= COMMAND_EXEC
,
5727 .usage
= "filename [offset [type]]",
5730 .name
= "test_image",
5731 .handler
= handle_test_image_command
,
5732 .mode
= COMMAND_EXEC
,
5733 .usage
= "filename [offset [type]]",
5736 .name
= "mem2array",
5737 .mode
= COMMAND_EXEC
,
5738 .jim_handler
= jim_mem2array
,
5739 .help
= "read 8/16/32 bit memory and return as a TCL array "
5740 "for script processing",
5741 .usage
= "arrayname bitwidth address count",
5744 .name
= "array2mem",
5745 .mode
= COMMAND_EXEC
,
5746 .jim_handler
= jim_array2mem
,
5747 .help
= "convert a TCL array to memory locations "
5748 "and write the 8/16/32 bit values",
5749 .usage
= "arrayname bitwidth address count",
5752 .name
= "reset_nag",
5753 .handler
= handle_target_reset_nag
,
5754 .mode
= COMMAND_ANY
,
5755 .help
= "Nag after each reset about options that could have been "
5756 "enabled to improve performance. ",
5757 .usage
= "['enable'|'disable']",
5761 .handler
= handle_ps_command
,
5762 .mode
= COMMAND_EXEC
,
5763 .help
= "list all tasks ",
5767 COMMAND_REGISTRATION_DONE
5769 static int target_register_user_commands(struct command_context
*cmd_ctx
)
5771 int retval
= ERROR_OK
;
5772 retval
= target_request_register_commands(cmd_ctx
);
5773 if (retval
!= ERROR_OK
)
5776 retval
= trace_register_commands(cmd_ctx
);
5777 if (retval
!= ERROR_OK
)
5781 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);