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
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
232 { .name
= NULL
, .value
= -1 },
235 static const Jim_Nvp nvp_target_endian
[] = {
236 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
237 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
238 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
239 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
240 { .name
= NULL
, .value
= -1 },
243 static const Jim_Nvp nvp_reset_modes
[] = {
244 { .name
= "unknown", .value
= RESET_UNKNOWN
},
245 { .name
= "run" , .value
= RESET_RUN
},
246 { .name
= "halt" , .value
= RESET_HALT
},
247 { .name
= "init" , .value
= RESET_INIT
},
248 { .name
= NULL
, .value
= -1 },
251 const char *debug_reason_name(struct target
*t
)
255 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
256 t
->debug_reason
)->name
;
258 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
259 cp
= "(*BUG*unknown*BUG*)";
264 const char *target_state_name(struct target
*t
)
267 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
269 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
270 cp
= "(*BUG*unknown*BUG*)";
275 /* determine the number of the new target */
276 static int new_target_number(void)
281 /* number is 0 based */
285 if (x
< t
->target_number
)
286 x
= t
->target_number
;
292 /* read a uint32_t from a buffer in target memory endianness */
293 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
295 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
296 return le_to_h_u32(buffer
);
298 return be_to_h_u32(buffer
);
301 /* read a uint24_t from a buffer in target memory endianness */
302 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
304 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
305 return le_to_h_u24(buffer
);
307 return be_to_h_u24(buffer
);
310 /* read a uint16_t from a buffer in target memory endianness */
311 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
313 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
314 return le_to_h_u16(buffer
);
316 return be_to_h_u16(buffer
);
319 /* read a uint8_t from a buffer in target memory endianness */
320 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
322 return *buffer
& 0x0ff;
325 /* write a uint32_t to a buffer in target memory endianness */
326 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
328 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
329 h_u32_to_le(buffer
, value
);
331 h_u32_to_be(buffer
, value
);
334 /* write a uint24_t to a buffer in target memory endianness */
335 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
337 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
338 h_u24_to_le(buffer
, value
);
340 h_u24_to_be(buffer
, value
);
343 /* write a uint16_t to a buffer in target memory endianness */
344 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
346 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
347 h_u16_to_le(buffer
, value
);
349 h_u16_to_be(buffer
, value
);
352 /* write a uint8_t to a buffer in target memory endianness */
353 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
358 /* write a uint32_t array to a buffer in target memory endianness */
359 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
362 for (i
= 0; i
< count
; i
++)
363 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
366 /* write a uint16_t array to a buffer in target memory endianness */
367 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
370 for (i
= 0; i
< count
; i
++)
371 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
374 /* write a uint32_t array to a buffer in target memory endianness */
375 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, uint32_t *srcbuf
)
378 for (i
= 0; i
< count
; i
++)
379 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
382 /* write a uint16_t array to a buffer in target memory endianness */
383 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, uint16_t *srcbuf
)
386 for (i
= 0; i
< count
; i
++)
387 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
390 /* return a pointer to a configured target; id is name or number */
391 struct target
*get_target(const char *id
)
393 struct target
*target
;
395 /* try as tcltarget name */
396 for (target
= all_targets
; target
; target
= target
->next
) {
397 if (target_name(target
) == NULL
)
399 if (strcmp(id
, target_name(target
)) == 0)
403 /* It's OK to remove this fallback sometime after August 2010 or so */
405 /* no match, try as number */
407 if (parse_uint(id
, &num
) != ERROR_OK
)
410 for (target
= all_targets
; target
; target
= target
->next
) {
411 if (target
->target_number
== (int)num
) {
412 LOG_WARNING("use '%s' as target identifier, not '%u'",
413 target_name(target
), num
);
421 /* returns a pointer to the n-th configured target */
422 static struct target
*get_target_by_num(int num
)
424 struct target
*target
= all_targets
;
427 if (target
->target_number
== num
)
429 target
= target
->next
;
435 struct target
*get_current_target(struct command_context
*cmd_ctx
)
437 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
439 if (target
== NULL
) {
440 LOG_ERROR("BUG: current_target out of bounds");
447 int target_poll(struct target
*target
)
451 /* We can't poll until after examine */
452 if (!target_was_examined(target
)) {
453 /* Fail silently lest we pollute the log */
457 retval
= target
->type
->poll(target
);
458 if (retval
!= ERROR_OK
)
461 if (target
->halt_issued
) {
462 if (target
->state
== TARGET_HALTED
)
463 target
->halt_issued
= false;
465 long long t
= timeval_ms() - target
->halt_issued_time
;
466 if (t
> DEFAULT_HALT_TIMEOUT
) {
467 target
->halt_issued
= false;
468 LOG_INFO("Halt timed out, wake up GDB.");
469 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
477 int target_halt(struct target
*target
)
480 /* We can't poll until after examine */
481 if (!target_was_examined(target
)) {
482 LOG_ERROR("Target not examined yet");
486 retval
= target
->type
->halt(target
);
487 if (retval
!= ERROR_OK
)
490 target
->halt_issued
= true;
491 target
->halt_issued_time
= timeval_ms();
497 * Make the target (re)start executing using its saved execution
498 * context (possibly with some modifications).
500 * @param target Which target should start executing.
501 * @param current True to use the target's saved program counter instead
502 * of the address parameter
503 * @param address Optionally used as the program counter.
504 * @param handle_breakpoints True iff breakpoints at the resumption PC
505 * should be skipped. (For example, maybe execution was stopped by
506 * such a breakpoint, in which case it would be counterprodutive to
508 * @param debug_execution False if all working areas allocated by OpenOCD
509 * should be released and/or restored to their original contents.
510 * (This would for example be true to run some downloaded "helper"
511 * algorithm code, which resides in one such working buffer and uses
512 * another for data storage.)
514 * @todo Resolve the ambiguity about what the "debug_execution" flag
515 * signifies. For example, Target implementations don't agree on how
516 * it relates to invalidation of the register cache, or to whether
517 * breakpoints and watchpoints should be enabled. (It would seem wrong
518 * to enable breakpoints when running downloaded "helper" algorithms
519 * (debug_execution true), since the breakpoints would be set to match
520 * target firmware being debugged, not the helper algorithm.... and
521 * enabling them could cause such helpers to malfunction (for example,
522 * by overwriting data with a breakpoint instruction. On the other
523 * hand the infrastructure for running such helpers might use this
524 * procedure but rely on hardware breakpoint to detect termination.)
526 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
530 /* We can't poll until after examine */
531 if (!target_was_examined(target
)) {
532 LOG_ERROR("Target not examined yet");
536 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
538 /* note that resume *must* be asynchronous. The CPU can halt before
539 * we poll. The CPU can even halt at the current PC as a result of
540 * a software breakpoint being inserted by (a bug?) the application.
542 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
543 if (retval
!= ERROR_OK
)
546 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
551 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
556 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
557 if (n
->name
== NULL
) {
558 LOG_ERROR("invalid reset mode");
562 /* disable polling during reset to make reset event scripts
563 * more predictable, i.e. dr/irscan & pathmove in events will
564 * not have JTAG operations injected into the middle of a sequence.
566 bool save_poll
= jtag_poll_get_enabled();
568 jtag_poll_set_enabled(false);
570 sprintf(buf
, "ocd_process_reset %s", n
->name
);
571 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
573 jtag_poll_set_enabled(save_poll
);
575 if (retval
!= JIM_OK
) {
576 Jim_MakeErrorMessage(cmd_ctx
->interp
);
577 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
581 /* We want any events to be processed before the prompt */
582 retval
= target_call_timer_callbacks_now();
584 struct target
*target
;
585 for (target
= all_targets
; target
; target
= target
->next
)
586 target
->type
->check_reset(target
);
591 static int identity_virt2phys(struct target
*target
,
592 uint32_t virtual, uint32_t *physical
)
598 static int no_mmu(struct target
*target
, int *enabled
)
604 static int default_examine(struct target
*target
)
606 target_set_examined(target
);
610 /* no check by default */
611 static int default_check_reset(struct target
*target
)
616 int target_examine_one(struct target
*target
)
618 return target
->type
->examine(target
);
621 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
623 struct target
*target
= priv
;
625 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
628 jtag_unregister_event_callback(jtag_enable_callback
, target
);
630 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
632 int retval
= target_examine_one(target
);
633 if (retval
!= ERROR_OK
)
636 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
641 /* Targets that correctly implement init + examine, i.e.
642 * no communication with target during init:
646 int target_examine(void)
648 int retval
= ERROR_OK
;
649 struct target
*target
;
651 for (target
= all_targets
; target
; target
= target
->next
) {
652 /* defer examination, but don't skip it */
653 if (!target
->tap
->enabled
) {
654 jtag_register_event_callback(jtag_enable_callback
,
659 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
661 retval
= target_examine_one(target
);
662 if (retval
!= ERROR_OK
)
665 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
670 const char *target_type_name(struct target
*target
)
672 return target
->type
->name
;
675 static int target_soft_reset_halt(struct target
*target
)
677 if (!target_was_examined(target
)) {
678 LOG_ERROR("Target not examined yet");
681 if (!target
->type
->soft_reset_halt
) {
682 LOG_ERROR("Target %s does not support soft_reset_halt",
683 target_name(target
));
686 return target
->type
->soft_reset_halt(target
);
690 * Downloads a target-specific native code algorithm to the target,
691 * and executes it. * Note that some targets may need to set up, enable,
692 * and tear down a breakpoint (hard or * soft) to detect algorithm
693 * termination, while others may support lower overhead schemes where
694 * soft breakpoints embedded in the algorithm automatically terminate the
697 * @param target used to run the algorithm
698 * @param arch_info target-specific description of the algorithm.
700 int target_run_algorithm(struct target
*target
,
701 int num_mem_params
, struct mem_param
*mem_params
,
702 int num_reg_params
, struct reg_param
*reg_param
,
703 uint32_t entry_point
, uint32_t exit_point
,
704 int timeout_ms
, void *arch_info
)
706 int retval
= ERROR_FAIL
;
708 if (!target_was_examined(target
)) {
709 LOG_ERROR("Target not examined yet");
712 if (!target
->type
->run_algorithm
) {
713 LOG_ERROR("Target type '%s' does not support %s",
714 target_type_name(target
), __func__
);
718 target
->running_alg
= true;
719 retval
= target
->type
->run_algorithm(target
,
720 num_mem_params
, mem_params
,
721 num_reg_params
, reg_param
,
722 entry_point
, exit_point
, timeout_ms
, arch_info
);
723 target
->running_alg
= false;
730 * Downloads a target-specific native code algorithm to the target,
731 * executes and leaves it running.
733 * @param target used to run the algorithm
734 * @param arch_info target-specific description of the algorithm.
736 int target_start_algorithm(struct target
*target
,
737 int num_mem_params
, struct mem_param
*mem_params
,
738 int num_reg_params
, struct reg_param
*reg_params
,
739 uint32_t entry_point
, uint32_t exit_point
,
742 int retval
= ERROR_FAIL
;
744 if (!target_was_examined(target
)) {
745 LOG_ERROR("Target not examined yet");
748 if (!target
->type
->start_algorithm
) {
749 LOG_ERROR("Target type '%s' does not support %s",
750 target_type_name(target
), __func__
);
753 if (target
->running_alg
) {
754 LOG_ERROR("Target is already running an algorithm");
758 target
->running_alg
= true;
759 retval
= target
->type
->start_algorithm(target
,
760 num_mem_params
, mem_params
,
761 num_reg_params
, reg_params
,
762 entry_point
, exit_point
, arch_info
);
769 * Waits for an algorithm started with target_start_algorithm() to complete.
771 * @param target used to run the algorithm
772 * @param arch_info target-specific description of the algorithm.
774 int target_wait_algorithm(struct target
*target
,
775 int num_mem_params
, struct mem_param
*mem_params
,
776 int num_reg_params
, struct reg_param
*reg_params
,
777 uint32_t exit_point
, int timeout_ms
,
780 int retval
= ERROR_FAIL
;
782 if (!target
->type
->wait_algorithm
) {
783 LOG_ERROR("Target type '%s' does not support %s",
784 target_type_name(target
), __func__
);
787 if (!target
->running_alg
) {
788 LOG_ERROR("Target is not running an algorithm");
792 retval
= target
->type
->wait_algorithm(target
,
793 num_mem_params
, mem_params
,
794 num_reg_params
, reg_params
,
795 exit_point
, timeout_ms
, arch_info
);
796 if (retval
!= ERROR_TARGET_TIMEOUT
)
797 target
->running_alg
= false;
804 * Executes a target-specific native code algorithm in the target.
805 * It differs from target_run_algorithm in that the algorithm is asynchronous.
806 * Because of this it requires an compliant algorithm:
807 * see contrib/loaders/flash/stm32f1x.S for example.
809 * @param target used to run the algorithm
812 int target_run_flash_async_algorithm(struct target
*target
,
813 uint8_t *buffer
, uint32_t count
, int block_size
,
814 int num_mem_params
, struct mem_param
*mem_params
,
815 int num_reg_params
, struct reg_param
*reg_params
,
816 uint32_t buffer_start
, uint32_t buffer_size
,
817 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
822 /* Set up working area. First word is write pointer, second word is read pointer,
823 * rest is fifo data area. */
824 uint32_t wp_addr
= buffer_start
;
825 uint32_t rp_addr
= buffer_start
+ 4;
826 uint32_t fifo_start_addr
= buffer_start
+ 8;
827 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
829 uint32_t wp
= fifo_start_addr
;
830 uint32_t rp
= fifo_start_addr
;
832 /* validate block_size is 2^n */
833 assert(!block_size
|| !(block_size
& (block_size
- 1)));
835 retval
= target_write_u32(target
, wp_addr
, wp
);
836 if (retval
!= ERROR_OK
)
838 retval
= target_write_u32(target
, rp_addr
, rp
);
839 if (retval
!= ERROR_OK
)
842 /* Start up algorithm on target and let it idle while writing the first chunk */
843 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
844 num_reg_params
, reg_params
,
849 if (retval
!= ERROR_OK
) {
850 LOG_ERROR("error starting target flash write algorithm");
856 retval
= target_read_u32(target
, rp_addr
, &rp
);
857 if (retval
!= ERROR_OK
) {
858 LOG_ERROR("failed to get read pointer");
862 LOG_DEBUG("count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
, count
, wp
, rp
);
865 LOG_ERROR("flash write algorithm aborted by target");
866 retval
= ERROR_FLASH_OPERATION_FAILED
;
870 if ((rp
& (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
871 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
875 /* Count the number of bytes available in the fifo without
876 * crossing the wrap around. Make sure to not fill it completely,
877 * because that would make wp == rp and that's the empty condition. */
878 uint32_t thisrun_bytes
;
880 thisrun_bytes
= rp
- wp
- block_size
;
881 else if (rp
> fifo_start_addr
)
882 thisrun_bytes
= fifo_end_addr
- wp
;
884 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
886 if (thisrun_bytes
== 0) {
887 /* Throttle polling a bit if transfer is (much) faster than flash
888 * programming. The exact delay shouldn't matter as long as it's
889 * less than buffer size / flash speed. This is very unlikely to
890 * run when using high latency connections such as USB. */
893 /* to stop an infinite loop on some targets check and increment a timeout
894 * this issue was observed on a stellaris using the new ICDI interface */
895 if (timeout
++ >= 500) {
896 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
897 return ERROR_FLASH_OPERATION_FAILED
;
902 /* reset our timeout */
905 /* Limit to the amount of data we actually want to write */
906 if (thisrun_bytes
> count
* block_size
)
907 thisrun_bytes
= count
* block_size
;
909 /* Write data to fifo */
910 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
911 if (retval
!= ERROR_OK
)
914 /* Update counters and wrap write pointer */
915 buffer
+= thisrun_bytes
;
916 count
-= thisrun_bytes
/ block_size
;
918 if (wp
>= fifo_end_addr
)
919 wp
= fifo_start_addr
;
921 /* Store updated write pointer to target */
922 retval
= target_write_u32(target
, wp_addr
, wp
);
923 if (retval
!= ERROR_OK
)
927 if (retval
!= ERROR_OK
) {
928 /* abort flash write algorithm on target */
929 target_write_u32(target
, wp_addr
, 0);
932 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
933 num_reg_params
, reg_params
,
938 if (retval2
!= ERROR_OK
) {
939 LOG_ERROR("error waiting for target flash write algorithm");
946 int target_read_memory(struct target
*target
,
947 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
949 if (!target_was_examined(target
)) {
950 LOG_ERROR("Target not examined yet");
953 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
956 int target_read_phys_memory(struct target
*target
,
957 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
959 if (!target_was_examined(target
)) {
960 LOG_ERROR("Target not examined yet");
963 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
966 int target_write_memory(struct target
*target
,
967 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
969 if (!target_was_examined(target
)) {
970 LOG_ERROR("Target not examined yet");
973 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
976 int target_write_phys_memory(struct target
*target
,
977 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
979 if (!target_was_examined(target
)) {
980 LOG_ERROR("Target not examined yet");
983 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
986 static int target_bulk_write_memory_default(struct target
*target
,
987 uint32_t address
, uint32_t count
, const uint8_t *buffer
)
989 return target_write_memory(target
, address
, 4, count
, buffer
);
992 int target_add_breakpoint(struct target
*target
,
993 struct breakpoint
*breakpoint
)
995 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
996 LOG_WARNING("target %s is not halted", target_name(target
));
997 return ERROR_TARGET_NOT_HALTED
;
999 return target
->type
->add_breakpoint(target
, breakpoint
);
1002 int target_add_context_breakpoint(struct target
*target
,
1003 struct breakpoint
*breakpoint
)
1005 if (target
->state
!= TARGET_HALTED
) {
1006 LOG_WARNING("target %s is not halted", target_name(target
));
1007 return ERROR_TARGET_NOT_HALTED
;
1009 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1012 int target_add_hybrid_breakpoint(struct target
*target
,
1013 struct breakpoint
*breakpoint
)
1015 if (target
->state
!= TARGET_HALTED
) {
1016 LOG_WARNING("target %s is not halted", target_name(target
));
1017 return ERROR_TARGET_NOT_HALTED
;
1019 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1022 int target_remove_breakpoint(struct target
*target
,
1023 struct breakpoint
*breakpoint
)
1025 return target
->type
->remove_breakpoint(target
, breakpoint
);
1028 int target_add_watchpoint(struct target
*target
,
1029 struct watchpoint
*watchpoint
)
1031 if (target
->state
!= TARGET_HALTED
) {
1032 LOG_WARNING("target %s is not halted", target_name(target
));
1033 return ERROR_TARGET_NOT_HALTED
;
1035 return target
->type
->add_watchpoint(target
, watchpoint
);
1037 int target_remove_watchpoint(struct target
*target
,
1038 struct watchpoint
*watchpoint
)
1040 return target
->type
->remove_watchpoint(target
, watchpoint
);
1042 int target_hit_watchpoint(struct target
*target
,
1043 struct watchpoint
**hit_watchpoint
)
1045 if (target
->state
!= TARGET_HALTED
) {
1046 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1047 return ERROR_TARGET_NOT_HALTED
;
1050 if (target
->type
->hit_watchpoint
== NULL
) {
1051 /* For backward compatible, if hit_watchpoint is not implemented,
1052 * return ERROR_FAIL such that gdb_server will not take the nonsense
1057 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1060 int target_get_gdb_reg_list(struct target
*target
,
1061 struct reg
**reg_list
[], int *reg_list_size
,
1062 enum target_register_class reg_class
)
1064 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1066 int target_step(struct target
*target
,
1067 int current
, uint32_t address
, int handle_breakpoints
)
1069 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1072 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1074 if (target
->state
!= TARGET_HALTED
) {
1075 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1076 return ERROR_TARGET_NOT_HALTED
;
1078 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1081 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1083 if (target
->state
!= TARGET_HALTED
) {
1084 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1085 return ERROR_TARGET_NOT_HALTED
;
1087 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1091 * Reset the @c examined flag for the given target.
1092 * Pure paranoia -- targets are zeroed on allocation.
1094 static void target_reset_examined(struct target
*target
)
1096 target
->examined
= false;
1099 static int err_read_phys_memory(struct target
*target
, uint32_t address
,
1100 uint32_t size
, uint32_t count
, uint8_t *buffer
)
1102 LOG_ERROR("Not implemented: %s", __func__
);
1106 static int err_write_phys_memory(struct target
*target
, uint32_t address
,
1107 uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1109 LOG_ERROR("Not implemented: %s", __func__
);
1113 static int handle_target(void *priv
);
1115 static int target_init_one(struct command_context
*cmd_ctx
,
1116 struct target
*target
)
1118 target_reset_examined(target
);
1120 struct target_type
*type
= target
->type
;
1121 if (type
->examine
== NULL
)
1122 type
->examine
= default_examine
;
1124 if (type
->check_reset
== NULL
)
1125 type
->check_reset
= default_check_reset
;
1127 assert(type
->init_target
!= NULL
);
1129 int retval
= type
->init_target(cmd_ctx
, target
);
1130 if (ERROR_OK
!= retval
) {
1131 LOG_ERROR("target '%s' init failed", target_name(target
));
1135 /* Sanity-check MMU support ... stub in what we must, to help
1136 * implement it in stages, but warn if we need to do so.
1139 if (type
->write_phys_memory
== NULL
) {
1140 LOG_ERROR("type '%s' is missing write_phys_memory",
1142 type
->write_phys_memory
= err_write_phys_memory
;
1144 if (type
->read_phys_memory
== NULL
) {
1145 LOG_ERROR("type '%s' is missing read_phys_memory",
1147 type
->read_phys_memory
= err_read_phys_memory
;
1149 if (type
->virt2phys
== NULL
) {
1150 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1151 type
->virt2phys
= identity_virt2phys
;
1154 /* Make sure no-MMU targets all behave the same: make no
1155 * distinction between physical and virtual addresses, and
1156 * ensure that virt2phys() is always an identity mapping.
1158 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1159 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1162 type
->write_phys_memory
= type
->write_memory
;
1163 type
->read_phys_memory
= type
->read_memory
;
1164 type
->virt2phys
= identity_virt2phys
;
1167 if (target
->type
->read_buffer
== NULL
)
1168 target
->type
->read_buffer
= target_read_buffer_default
;
1170 if (target
->type
->write_buffer
== NULL
)
1171 target
->type
->write_buffer
= target_write_buffer_default
;
1173 if (target
->type
->bulk_write_memory
== NULL
)
1174 target
->type
->bulk_write_memory
= target_bulk_write_memory_default
;
1176 if (target
->type
->get_gdb_fileio_info
== NULL
)
1177 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1179 if (target
->type
->gdb_fileio_end
== NULL
)
1180 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1185 static int target_init(struct command_context
*cmd_ctx
)
1187 struct target
*target
;
1190 for (target
= all_targets
; target
; target
= target
->next
) {
1191 retval
= target_init_one(cmd_ctx
, target
);
1192 if (ERROR_OK
!= retval
)
1199 retval
= target_register_user_commands(cmd_ctx
);
1200 if (ERROR_OK
!= retval
)
1203 retval
= target_register_timer_callback(&handle_target
,
1204 polling_interval
, 1, cmd_ctx
->interp
);
1205 if (ERROR_OK
!= retval
)
1211 COMMAND_HANDLER(handle_target_init_command
)
1216 return ERROR_COMMAND_SYNTAX_ERROR
;
1218 static bool target_initialized
;
1219 if (target_initialized
) {
1220 LOG_INFO("'target init' has already been called");
1223 target_initialized
= true;
1225 retval
= command_run_line(CMD_CTX
, "init_targets");
1226 if (ERROR_OK
!= retval
)
1229 retval
= command_run_line(CMD_CTX
, "init_board");
1230 if (ERROR_OK
!= retval
)
1233 LOG_DEBUG("Initializing targets...");
1234 return target_init(CMD_CTX
);
1237 int target_register_event_callback(int (*callback
)(struct target
*target
,
1238 enum target_event event
, void *priv
), void *priv
)
1240 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1242 if (callback
== NULL
)
1243 return ERROR_COMMAND_SYNTAX_ERROR
;
1246 while ((*callbacks_p
)->next
)
1247 callbacks_p
= &((*callbacks_p
)->next
);
1248 callbacks_p
= &((*callbacks_p
)->next
);
1251 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1252 (*callbacks_p
)->callback
= callback
;
1253 (*callbacks_p
)->priv
= priv
;
1254 (*callbacks_p
)->next
= NULL
;
1259 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1261 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1264 if (callback
== NULL
)
1265 return ERROR_COMMAND_SYNTAX_ERROR
;
1268 while ((*callbacks_p
)->next
)
1269 callbacks_p
= &((*callbacks_p
)->next
);
1270 callbacks_p
= &((*callbacks_p
)->next
);
1273 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1274 (*callbacks_p
)->callback
= callback
;
1275 (*callbacks_p
)->periodic
= periodic
;
1276 (*callbacks_p
)->time_ms
= time_ms
;
1278 gettimeofday(&now
, NULL
);
1279 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
1280 time_ms
-= (time_ms
% 1000);
1281 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
1282 if ((*callbacks_p
)->when
.tv_usec
> 1000000) {
1283 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
1284 (*callbacks_p
)->when
.tv_sec
+= 1;
1287 (*callbacks_p
)->priv
= priv
;
1288 (*callbacks_p
)->next
= NULL
;
1293 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1294 enum target_event event
, void *priv
), void *priv
)
1296 struct target_event_callback
**p
= &target_event_callbacks
;
1297 struct target_event_callback
*c
= target_event_callbacks
;
1299 if (callback
== NULL
)
1300 return ERROR_COMMAND_SYNTAX_ERROR
;
1303 struct target_event_callback
*next
= c
->next
;
1304 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1316 static int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1318 struct target_timer_callback
**p
= &target_timer_callbacks
;
1319 struct target_timer_callback
*c
= target_timer_callbacks
;
1321 if (callback
== NULL
)
1322 return ERROR_COMMAND_SYNTAX_ERROR
;
1325 struct target_timer_callback
*next
= c
->next
;
1326 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1338 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1340 struct target_event_callback
*callback
= target_event_callbacks
;
1341 struct target_event_callback
*next_callback
;
1343 if (event
== TARGET_EVENT_HALTED
) {
1344 /* execute early halted first */
1345 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1348 LOG_DEBUG("target event %i (%s)", event
,
1349 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1351 target_handle_event(target
, event
);
1354 next_callback
= callback
->next
;
1355 callback
->callback(target
, event
, callback
->priv
);
1356 callback
= next_callback
;
1362 static int target_timer_callback_periodic_restart(
1363 struct target_timer_callback
*cb
, struct timeval
*now
)
1365 int time_ms
= cb
->time_ms
;
1366 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1367 time_ms
-= (time_ms
% 1000);
1368 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1369 if (cb
->when
.tv_usec
> 1000000) {
1370 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1371 cb
->when
.tv_sec
+= 1;
1376 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1377 struct timeval
*now
)
1379 cb
->callback(cb
->priv
);
1382 return target_timer_callback_periodic_restart(cb
, now
);
1384 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1387 static int target_call_timer_callbacks_check_time(int checktime
)
1392 gettimeofday(&now
, NULL
);
1394 struct target_timer_callback
*callback
= target_timer_callbacks
;
1396 /* cleaning up may unregister and free this callback */
1397 struct target_timer_callback
*next_callback
= callback
->next
;
1399 bool call_it
= callback
->callback
&&
1400 ((!checktime
&& callback
->periodic
) ||
1401 now
.tv_sec
> callback
->when
.tv_sec
||
1402 (now
.tv_sec
== callback
->when
.tv_sec
&&
1403 now
.tv_usec
>= callback
->when
.tv_usec
));
1406 int retval
= target_call_timer_callback(callback
, &now
);
1407 if (retval
!= ERROR_OK
)
1411 callback
= next_callback
;
1417 int target_call_timer_callbacks(void)
1419 return target_call_timer_callbacks_check_time(1);
1422 /* invoke periodic callbacks immediately */
1423 int target_call_timer_callbacks_now(void)
1425 return target_call_timer_callbacks_check_time(0);
1428 /* Prints the working area layout for debug purposes */
1429 static void print_wa_layout(struct target
*target
)
1431 struct working_area
*c
= target
->working_areas
;
1434 LOG_DEBUG("%c%c 0x%08"PRIx32
"-0x%08"PRIx32
" (%"PRIu32
" bytes)",
1435 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1436 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1441 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1442 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1444 assert(area
->free
); /* Shouldn't split an allocated area */
1445 assert(size
<= area
->size
); /* Caller should guarantee this */
1447 /* Split only if not already the right size */
1448 if (size
< area
->size
) {
1449 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1454 new_wa
->next
= area
->next
;
1455 new_wa
->size
= area
->size
- size
;
1456 new_wa
->address
= area
->address
+ size
;
1457 new_wa
->backup
= NULL
;
1458 new_wa
->user
= NULL
;
1459 new_wa
->free
= true;
1461 area
->next
= new_wa
;
1464 /* If backup memory was allocated to this area, it has the wrong size
1465 * now so free it and it will be reallocated if/when needed */
1468 area
->backup
= NULL
;
1473 /* Merge all adjacent free areas into one */
1474 static void target_merge_working_areas(struct target
*target
)
1476 struct working_area
*c
= target
->working_areas
;
1478 while (c
&& c
->next
) {
1479 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1481 /* Find two adjacent free areas */
1482 if (c
->free
&& c
->next
->free
) {
1483 /* Merge the last into the first */
1484 c
->size
+= c
->next
->size
;
1486 /* Remove the last */
1487 struct working_area
*to_be_freed
= c
->next
;
1488 c
->next
= c
->next
->next
;
1489 if (to_be_freed
->backup
)
1490 free(to_be_freed
->backup
);
1493 /* If backup memory was allocated to the remaining area, it's has
1494 * the wrong size now */
1505 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1507 /* Reevaluate working area address based on MMU state*/
1508 if (target
->working_areas
== NULL
) {
1512 retval
= target
->type
->mmu(target
, &enabled
);
1513 if (retval
!= ERROR_OK
)
1517 if (target
->working_area_phys_spec
) {
1518 LOG_DEBUG("MMU disabled, using physical "
1519 "address for working memory 0x%08"PRIx32
,
1520 target
->working_area_phys
);
1521 target
->working_area
= target
->working_area_phys
;
1523 LOG_ERROR("No working memory available. "
1524 "Specify -work-area-phys to target.");
1525 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1528 if (target
->working_area_virt_spec
) {
1529 LOG_DEBUG("MMU enabled, using virtual "
1530 "address for working memory 0x%08"PRIx32
,
1531 target
->working_area_virt
);
1532 target
->working_area
= target
->working_area_virt
;
1534 LOG_ERROR("No working memory available. "
1535 "Specify -work-area-virt to target.");
1536 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1540 /* Set up initial working area on first call */
1541 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1543 new_wa
->next
= NULL
;
1544 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1545 new_wa
->address
= target
->working_area
;
1546 new_wa
->backup
= NULL
;
1547 new_wa
->user
= NULL
;
1548 new_wa
->free
= true;
1551 target
->working_areas
= new_wa
;
1554 /* only allocate multiples of 4 byte */
1556 size
= (size
+ 3) & (~3UL);
1558 struct working_area
*c
= target
->working_areas
;
1560 /* Find the first large enough working area */
1562 if (c
->free
&& c
->size
>= size
)
1568 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1570 /* Split the working area into the requested size */
1571 target_split_working_area(c
, size
);
1573 LOG_DEBUG("allocated new working area of %"PRIu32
" bytes at address 0x%08"PRIx32
, size
, c
->address
);
1575 if (target
->backup_working_area
) {
1576 if (c
->backup
== NULL
) {
1577 c
->backup
= malloc(c
->size
);
1578 if (c
->backup
== NULL
)
1582 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1583 if (retval
!= ERROR_OK
)
1587 /* mark as used, and return the new (reused) area */
1594 print_wa_layout(target
);
1599 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1603 retval
= target_alloc_working_area_try(target
, size
, area
);
1604 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1605 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1610 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1612 int retval
= ERROR_OK
;
1614 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1615 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1616 if (retval
!= ERROR_OK
)
1617 LOG_ERROR("failed to restore %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1618 area
->size
, area
->address
);
1624 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1625 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1627 int retval
= ERROR_OK
;
1633 retval
= target_restore_working_area(target
, area
);
1634 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1635 if (retval
!= ERROR_OK
)
1641 LOG_DEBUG("freed %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1642 area
->size
, area
->address
);
1644 /* mark user pointer invalid */
1645 /* TODO: Is this really safe? It points to some previous caller's memory.
1646 * How could we know that the area pointer is still in that place and not
1647 * some other vital data? What's the purpose of this, anyway? */
1651 target_merge_working_areas(target
);
1653 print_wa_layout(target
);
1658 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1660 return target_free_working_area_restore(target
, area
, 1);
1663 /* free resources and restore memory, if restoring memory fails,
1664 * free up resources anyway
1666 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1668 struct working_area
*c
= target
->working_areas
;
1670 LOG_DEBUG("freeing all working areas");
1672 /* Loop through all areas, restoring the allocated ones and marking them as free */
1676 target_restore_working_area(target
, c
);
1678 *c
->user
= NULL
; /* Same as above */
1684 /* Run a merge pass to combine all areas into one */
1685 target_merge_working_areas(target
);
1687 print_wa_layout(target
);
1690 void target_free_all_working_areas(struct target
*target
)
1692 target_free_all_working_areas_restore(target
, 1);
1695 /* Find the largest number of bytes that can be allocated */
1696 uint32_t target_get_working_area_avail(struct target
*target
)
1698 struct working_area
*c
= target
->working_areas
;
1699 uint32_t max_size
= 0;
1702 return target
->working_area_size
;
1705 if (c
->free
&& max_size
< c
->size
)
1714 int target_arch_state(struct target
*target
)
1717 if (target
== NULL
) {
1718 LOG_USER("No target has been configured");
1722 LOG_USER("target state: %s", target_state_name(target
));
1724 if (target
->state
!= TARGET_HALTED
)
1727 retval
= target
->type
->arch_state(target
);
1731 static int target_get_gdb_fileio_info_default(struct target
*target
,
1732 struct gdb_fileio_info
*fileio_info
)
1734 LOG_ERROR("Not implemented: %s", __func__
);
1738 static int target_gdb_fileio_end_default(struct target
*target
,
1739 int retcode
, int fileio_errno
, bool ctrl_c
)
1741 LOG_ERROR("Not implemented: %s", __func__
);
1745 /* Single aligned words are guaranteed to use 16 or 32 bit access
1746 * mode respectively, otherwise data is handled as quickly as
1749 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
1751 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1752 (int)size
, (unsigned)address
);
1754 if (!target_was_examined(target
)) {
1755 LOG_ERROR("Target not examined yet");
1762 if ((address
+ size
- 1) < address
) {
1763 /* GDB can request this when e.g. PC is 0xfffffffc*/
1764 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1770 return target
->type
->write_buffer(target
, address
, size
, buffer
);
1773 static int target_write_buffer_default(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
1775 int retval
= ERROR_OK
;
1777 if (((address
% 2) == 0) && (size
== 2))
1778 return target_write_memory(target
, address
, 2, 1, buffer
);
1780 /* handle unaligned head bytes */
1782 uint32_t unaligned
= 4 - (address
% 4);
1784 if (unaligned
> size
)
1787 retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
);
1788 if (retval
!= ERROR_OK
)
1791 buffer
+= unaligned
;
1792 address
+= unaligned
;
1796 /* handle aligned words */
1798 int aligned
= size
- (size
% 4);
1800 /* use bulk writes above a certain limit. This may have to be changed */
1801 if (aligned
> 128) {
1802 retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
);
1803 if (retval
!= ERROR_OK
)
1806 retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
);
1807 if (retval
!= ERROR_OK
)
1816 /* handle tail writes of less than 4 bytes */
1818 retval
= target_write_memory(target
, address
, 1, size
, buffer
);
1819 if (retval
!= ERROR_OK
)
1826 /* Single aligned words are guaranteed to use 16 or 32 bit access
1827 * mode respectively, otherwise data is handled as quickly as
1830 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1832 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1833 (int)size
, (unsigned)address
);
1835 if (!target_was_examined(target
)) {
1836 LOG_ERROR("Target not examined yet");
1843 if ((address
+ size
- 1) < address
) {
1844 /* GDB can request this when e.g. PC is 0xfffffffc*/
1845 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1851 return target
->type
->read_buffer(target
, address
, size
, buffer
);
1854 static int target_read_buffer_default(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1856 int retval
= ERROR_OK
;
1858 if (((address
% 2) == 0) && (size
== 2))
1859 return target_read_memory(target
, address
, 2, 1, buffer
);
1861 /* handle unaligned head bytes */
1863 uint32_t unaligned
= 4 - (address
% 4);
1865 if (unaligned
> size
)
1868 retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
);
1869 if (retval
!= ERROR_OK
)
1872 buffer
+= unaligned
;
1873 address
+= unaligned
;
1877 /* handle aligned words */
1879 int aligned
= size
- (size
% 4);
1881 retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
);
1882 if (retval
!= ERROR_OK
)
1890 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1892 int aligned
= size
- (size
% 2);
1893 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1894 if (retval
!= ERROR_OK
)
1901 /* handle tail writes of less than 4 bytes */
1903 retval
= target_read_memory(target
, address
, 1, size
, buffer
);
1904 if (retval
!= ERROR_OK
)
1911 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1916 uint32_t checksum
= 0;
1917 if (!target_was_examined(target
)) {
1918 LOG_ERROR("Target not examined yet");
1922 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
1923 if (retval
!= ERROR_OK
) {
1924 buffer
= malloc(size
);
1925 if (buffer
== NULL
) {
1926 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1927 return ERROR_COMMAND_SYNTAX_ERROR
;
1929 retval
= target_read_buffer(target
, address
, size
, buffer
);
1930 if (retval
!= ERROR_OK
) {
1935 /* convert to target endianness */
1936 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
1937 uint32_t target_data
;
1938 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1939 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1942 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1951 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1954 if (!target_was_examined(target
)) {
1955 LOG_ERROR("Target not examined yet");
1959 if (target
->type
->blank_check_memory
== 0)
1960 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1962 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1967 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
1969 uint8_t value_buf
[4];
1970 if (!target_was_examined(target
)) {
1971 LOG_ERROR("Target not examined yet");
1975 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1977 if (retval
== ERROR_OK
) {
1978 *value
= target_buffer_get_u32(target
, value_buf
);
1979 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1984 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1991 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
1993 uint8_t value_buf
[2];
1994 if (!target_was_examined(target
)) {
1995 LOG_ERROR("Target not examined yet");
1999 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2001 if (retval
== ERROR_OK
) {
2002 *value
= target_buffer_get_u16(target
, value_buf
);
2003 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
2008 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2015 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
2017 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2018 if (!target_was_examined(target
)) {
2019 LOG_ERROR("Target not examined yet");
2023 if (retval
== ERROR_OK
) {
2024 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
2029 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2036 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
2039 uint8_t value_buf
[4];
2040 if (!target_was_examined(target
)) {
2041 LOG_ERROR("Target not examined yet");
2045 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
2049 target_buffer_set_u32(target
, value_buf
, value
);
2050 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2051 if (retval
!= ERROR_OK
)
2052 LOG_DEBUG("failed: %i", retval
);
2057 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
2060 uint8_t value_buf
[2];
2061 if (!target_was_examined(target
)) {
2062 LOG_ERROR("Target not examined yet");
2066 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
2070 target_buffer_set_u16(target
, value_buf
, value
);
2071 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2072 if (retval
!= ERROR_OK
)
2073 LOG_DEBUG("failed: %i", retval
);
2078 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
2081 if (!target_was_examined(target
)) {
2082 LOG_ERROR("Target not examined yet");
2086 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
2089 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2090 if (retval
!= ERROR_OK
)
2091 LOG_DEBUG("failed: %i", retval
);
2096 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2098 struct target
*target
= get_target(name
);
2099 if (target
== NULL
) {
2100 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2103 if (!target
->tap
->enabled
) {
2104 LOG_USER("Target: TAP %s is disabled, "
2105 "can't be the current target\n",
2106 target
->tap
->dotted_name
);
2110 cmd_ctx
->current_target
= target
->target_number
;
2115 COMMAND_HANDLER(handle_targets_command
)
2117 int retval
= ERROR_OK
;
2118 if (CMD_ARGC
== 1) {
2119 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2120 if (retval
== ERROR_OK
) {
2126 struct target
*target
= all_targets
;
2127 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2128 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2133 if (target
->tap
->enabled
)
2134 state
= target_state_name(target
);
2136 state
= "tap-disabled";
2138 if (CMD_CTX
->current_target
== target
->target_number
)
2141 /* keep columns lined up to match the headers above */
2142 command_print(CMD_CTX
,
2143 "%2d%c %-18s %-10s %-6s %-18s %s",
2144 target
->target_number
,
2146 target_name(target
),
2147 target_type_name(target
),
2148 Jim_Nvp_value2name_simple(nvp_target_endian
,
2149 target
->endianness
)->name
,
2150 target
->tap
->dotted_name
,
2152 target
= target
->next
;
2158 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2160 static int powerDropout
;
2161 static int srstAsserted
;
2163 static int runPowerRestore
;
2164 static int runPowerDropout
;
2165 static int runSrstAsserted
;
2166 static int runSrstDeasserted
;
2168 static int sense_handler(void)
2170 static int prevSrstAsserted
;
2171 static int prevPowerdropout
;
2173 int retval
= jtag_power_dropout(&powerDropout
);
2174 if (retval
!= ERROR_OK
)
2178 powerRestored
= prevPowerdropout
&& !powerDropout
;
2180 runPowerRestore
= 1;
2182 long long current
= timeval_ms();
2183 static long long lastPower
;
2184 int waitMore
= lastPower
+ 2000 > current
;
2185 if (powerDropout
&& !waitMore
) {
2186 runPowerDropout
= 1;
2187 lastPower
= current
;
2190 retval
= jtag_srst_asserted(&srstAsserted
);
2191 if (retval
!= ERROR_OK
)
2195 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2197 static long long lastSrst
;
2198 waitMore
= lastSrst
+ 2000 > current
;
2199 if (srstDeasserted
&& !waitMore
) {
2200 runSrstDeasserted
= 1;
2204 if (!prevSrstAsserted
&& srstAsserted
)
2205 runSrstAsserted
= 1;
2207 prevSrstAsserted
= srstAsserted
;
2208 prevPowerdropout
= powerDropout
;
2210 if (srstDeasserted
|| powerRestored
) {
2211 /* Other than logging the event we can't do anything here.
2212 * Issuing a reset is a particularly bad idea as we might
2213 * be inside a reset already.
2220 /* process target state changes */
2221 static int handle_target(void *priv
)
2223 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2224 int retval
= ERROR_OK
;
2226 if (!is_jtag_poll_safe()) {
2227 /* polling is disabled currently */
2231 /* we do not want to recurse here... */
2232 static int recursive
;
2236 /* danger! running these procedures can trigger srst assertions and power dropouts.
2237 * We need to avoid an infinite loop/recursion here and we do that by
2238 * clearing the flags after running these events.
2240 int did_something
= 0;
2241 if (runSrstAsserted
) {
2242 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2243 Jim_Eval(interp
, "srst_asserted");
2246 if (runSrstDeasserted
) {
2247 Jim_Eval(interp
, "srst_deasserted");
2250 if (runPowerDropout
) {
2251 LOG_INFO("Power dropout detected, running power_dropout proc.");
2252 Jim_Eval(interp
, "power_dropout");
2255 if (runPowerRestore
) {
2256 Jim_Eval(interp
, "power_restore");
2260 if (did_something
) {
2261 /* clear detect flags */
2265 /* clear action flags */
2267 runSrstAsserted
= 0;
2268 runSrstDeasserted
= 0;
2269 runPowerRestore
= 0;
2270 runPowerDropout
= 0;
2275 /* Poll targets for state changes unless that's globally disabled.
2276 * Skip targets that are currently disabled.
2278 for (struct target
*target
= all_targets
;
2279 is_jtag_poll_safe() && target
;
2280 target
= target
->next
) {
2281 if (!target
->tap
->enabled
)
2284 if (target
->backoff
.times
> target
->backoff
.count
) {
2285 /* do not poll this time as we failed previously */
2286 target
->backoff
.count
++;
2289 target
->backoff
.count
= 0;
2291 /* only poll target if we've got power and srst isn't asserted */
2292 if (!powerDropout
&& !srstAsserted
) {
2293 /* polling may fail silently until the target has been examined */
2294 retval
= target_poll(target
);
2295 if (retval
!= ERROR_OK
) {
2296 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2297 if (target
->backoff
.times
* polling_interval
< 5000) {
2298 target
->backoff
.times
*= 2;
2299 target
->backoff
.times
++;
2301 LOG_USER("Polling target %s failed, GDB will be halted. Polling again in %dms",
2302 target_name(target
),
2303 target
->backoff
.times
* polling_interval
);
2305 /* Tell GDB to halt the debugger. This allows the user to
2306 * run monitor commands to handle the situation.
2308 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2311 /* Since we succeeded, we reset backoff count */
2312 if (target
->backoff
.times
> 0)
2313 LOG_USER("Polling target %s succeeded again", target_name(target
));
2314 target
->backoff
.times
= 0;
2321 COMMAND_HANDLER(handle_reg_command
)
2323 struct target
*target
;
2324 struct reg
*reg
= NULL
;
2330 target
= get_current_target(CMD_CTX
);
2332 /* list all available registers for the current target */
2333 if (CMD_ARGC
== 0) {
2334 struct reg_cache
*cache
= target
->reg_cache
;
2340 command_print(CMD_CTX
, "===== %s", cache
->name
);
2342 for (i
= 0, reg
= cache
->reg_list
;
2343 i
< cache
->num_regs
;
2344 i
++, reg
++, count
++) {
2345 /* only print cached values if they are valid */
2347 value
= buf_to_str(reg
->value
,
2349 command_print(CMD_CTX
,
2350 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2358 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2363 cache
= cache
->next
;
2369 /* access a single register by its ordinal number */
2370 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2372 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2374 struct reg_cache
*cache
= target
->reg_cache
;
2378 for (i
= 0; i
< cache
->num_regs
; i
++) {
2379 if (count
++ == num
) {
2380 reg
= &cache
->reg_list
[i
];
2386 cache
= cache
->next
;
2390 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2391 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2395 /* access a single register by its name */
2396 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2399 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2404 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2406 /* display a register */
2407 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2408 && (CMD_ARGV
[1][0] <= '9')))) {
2409 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2412 if (reg
->valid
== 0)
2413 reg
->type
->get(reg
);
2414 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2415 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2420 /* set register value */
2421 if (CMD_ARGC
== 2) {
2422 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2425 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2427 reg
->type
->set(reg
, buf
);
2429 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2430 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2438 return ERROR_COMMAND_SYNTAX_ERROR
;
2441 COMMAND_HANDLER(handle_poll_command
)
2443 int retval
= ERROR_OK
;
2444 struct target
*target
= get_current_target(CMD_CTX
);
2446 if (CMD_ARGC
== 0) {
2447 command_print(CMD_CTX
, "background polling: %s",
2448 jtag_poll_get_enabled() ? "on" : "off");
2449 command_print(CMD_CTX
, "TAP: %s (%s)",
2450 target
->tap
->dotted_name
,
2451 target
->tap
->enabled
? "enabled" : "disabled");
2452 if (!target
->tap
->enabled
)
2454 retval
= target_poll(target
);
2455 if (retval
!= ERROR_OK
)
2457 retval
= target_arch_state(target
);
2458 if (retval
!= ERROR_OK
)
2460 } else if (CMD_ARGC
== 1) {
2462 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2463 jtag_poll_set_enabled(enable
);
2465 return ERROR_COMMAND_SYNTAX_ERROR
;
2470 COMMAND_HANDLER(handle_wait_halt_command
)
2473 return ERROR_COMMAND_SYNTAX_ERROR
;
2475 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2476 if (1 == CMD_ARGC
) {
2477 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2478 if (ERROR_OK
!= retval
)
2479 return ERROR_COMMAND_SYNTAX_ERROR
;
2482 struct target
*target
= get_current_target(CMD_CTX
);
2483 return target_wait_state(target
, TARGET_HALTED
, ms
);
2486 /* wait for target state to change. The trick here is to have a low
2487 * latency for short waits and not to suck up all the CPU time
2490 * After 500ms, keep_alive() is invoked
2492 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2495 long long then
= 0, cur
;
2499 retval
= target_poll(target
);
2500 if (retval
!= ERROR_OK
)
2502 if (target
->state
== state
)
2507 then
= timeval_ms();
2508 LOG_DEBUG("waiting for target %s...",
2509 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2515 if ((cur
-then
) > ms
) {
2516 LOG_ERROR("timed out while waiting for target %s",
2517 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2525 COMMAND_HANDLER(handle_halt_command
)
2529 struct target
*target
= get_current_target(CMD_CTX
);
2530 int retval
= target_halt(target
);
2531 if (ERROR_OK
!= retval
)
2534 if (CMD_ARGC
== 1) {
2535 unsigned wait_local
;
2536 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2537 if (ERROR_OK
!= retval
)
2538 return ERROR_COMMAND_SYNTAX_ERROR
;
2543 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2546 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2548 struct target
*target
= get_current_target(CMD_CTX
);
2550 LOG_USER("requesting target halt and executing a soft reset");
2552 target_soft_reset_halt(target
);
2557 COMMAND_HANDLER(handle_reset_command
)
2560 return ERROR_COMMAND_SYNTAX_ERROR
;
2562 enum target_reset_mode reset_mode
= RESET_RUN
;
2563 if (CMD_ARGC
== 1) {
2565 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2566 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
2567 return ERROR_COMMAND_SYNTAX_ERROR
;
2568 reset_mode
= n
->value
;
2571 /* reset *all* targets */
2572 return target_process_reset(CMD_CTX
, reset_mode
);
2576 COMMAND_HANDLER(handle_resume_command
)
2580 return ERROR_COMMAND_SYNTAX_ERROR
;
2582 struct target
*target
= get_current_target(CMD_CTX
);
2584 /* with no CMD_ARGV, resume from current pc, addr = 0,
2585 * with one arguments, addr = CMD_ARGV[0],
2586 * handle breakpoints, not debugging */
2588 if (CMD_ARGC
== 1) {
2589 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2593 return target_resume(target
, current
, addr
, 1, 0);
2596 COMMAND_HANDLER(handle_step_command
)
2599 return ERROR_COMMAND_SYNTAX_ERROR
;
2603 /* with no CMD_ARGV, step from current pc, addr = 0,
2604 * with one argument addr = CMD_ARGV[0],
2605 * handle breakpoints, debugging */
2608 if (CMD_ARGC
== 1) {
2609 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2613 struct target
*target
= get_current_target(CMD_CTX
);
2615 return target
->type
->step(target
, current_pc
, addr
, 1);
2618 static void handle_md_output(struct command_context
*cmd_ctx
,
2619 struct target
*target
, uint32_t address
, unsigned size
,
2620 unsigned count
, const uint8_t *buffer
)
2622 const unsigned line_bytecnt
= 32;
2623 unsigned line_modulo
= line_bytecnt
/ size
;
2625 char output
[line_bytecnt
* 4 + 1];
2626 unsigned output_len
= 0;
2628 const char *value_fmt
;
2631 value_fmt
= "%8.8x ";
2634 value_fmt
= "%4.4x ";
2637 value_fmt
= "%2.2x ";
2640 /* "can't happen", caller checked */
2641 LOG_ERROR("invalid memory read size: %u", size
);
2645 for (unsigned i
= 0; i
< count
; i
++) {
2646 if (i
% line_modulo
== 0) {
2647 output_len
+= snprintf(output
+ output_len
,
2648 sizeof(output
) - output_len
,
2650 (unsigned)(address
+ (i
*size
)));
2654 const uint8_t *value_ptr
= buffer
+ i
* size
;
2657 value
= target_buffer_get_u32(target
, value_ptr
);
2660 value
= target_buffer_get_u16(target
, value_ptr
);
2665 output_len
+= snprintf(output
+ output_len
,
2666 sizeof(output
) - output_len
,
2669 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
2670 command_print(cmd_ctx
, "%s", output
);
2676 COMMAND_HANDLER(handle_md_command
)
2679 return ERROR_COMMAND_SYNTAX_ERROR
;
2682 switch (CMD_NAME
[2]) {
2693 return ERROR_COMMAND_SYNTAX_ERROR
;
2696 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2697 int (*fn
)(struct target
*target
,
2698 uint32_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
2702 fn
= target_read_phys_memory
;
2704 fn
= target_read_memory
;
2705 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2706 return ERROR_COMMAND_SYNTAX_ERROR
;
2709 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2713 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2715 uint8_t *buffer
= calloc(count
, size
);
2717 struct target
*target
= get_current_target(CMD_CTX
);
2718 int retval
= fn(target
, address
, size
, count
, buffer
);
2719 if (ERROR_OK
== retval
)
2720 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2727 typedef int (*target_write_fn
)(struct target
*target
,
2728 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
2730 static int target_write_memory_fast(struct target
*target
,
2731 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
2733 return target_write_buffer(target
, address
, size
* count
, buffer
);
2736 static int target_fill_mem(struct target
*target
,
2745 /* We have to write in reasonably large chunks to be able
2746 * to fill large memory areas with any sane speed */
2747 const unsigned chunk_size
= 16384;
2748 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
2749 if (target_buf
== NULL
) {
2750 LOG_ERROR("Out of memory");
2754 for (unsigned i
= 0; i
< chunk_size
; i
++) {
2755 switch (data_size
) {
2757 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
2760 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
2763 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
2770 int retval
= ERROR_OK
;
2772 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
2775 if (current
> chunk_size
)
2776 current
= chunk_size
;
2777 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
2778 if (retval
!= ERROR_OK
)
2780 /* avoid GDB timeouts */
2789 COMMAND_HANDLER(handle_mw_command
)
2792 return ERROR_COMMAND_SYNTAX_ERROR
;
2793 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2798 fn
= target_write_phys_memory
;
2800 fn
= target_write_memory_fast
;
2801 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2802 return ERROR_COMMAND_SYNTAX_ERROR
;
2805 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2808 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2812 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2814 struct target
*target
= get_current_target(CMD_CTX
);
2816 switch (CMD_NAME
[2]) {
2827 return ERROR_COMMAND_SYNTAX_ERROR
;
2830 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
2833 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2834 uint32_t *min_address
, uint32_t *max_address
)
2836 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2837 return ERROR_COMMAND_SYNTAX_ERROR
;
2839 /* a base address isn't always necessary,
2840 * default to 0x0 (i.e. don't relocate) */
2841 if (CMD_ARGC
>= 2) {
2843 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2844 image
->base_address
= addr
;
2845 image
->base_address_set
= 1;
2847 image
->base_address_set
= 0;
2849 image
->start_address_set
= 0;
2852 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2853 if (CMD_ARGC
== 5) {
2854 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2855 /* use size (given) to find max (required) */
2856 *max_address
+= *min_address
;
2859 if (*min_address
> *max_address
)
2860 return ERROR_COMMAND_SYNTAX_ERROR
;
2865 COMMAND_HANDLER(handle_load_image_command
)
2869 uint32_t image_size
;
2870 uint32_t min_address
= 0;
2871 uint32_t max_address
= 0xffffffff;
2875 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2876 &image
, &min_address
, &max_address
);
2877 if (ERROR_OK
!= retval
)
2880 struct target
*target
= get_current_target(CMD_CTX
);
2882 struct duration bench
;
2883 duration_start(&bench
);
2885 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2890 for (i
= 0; i
< image
.num_sections
; i
++) {
2891 buffer
= malloc(image
.sections
[i
].size
);
2892 if (buffer
== NULL
) {
2893 command_print(CMD_CTX
,
2894 "error allocating buffer for section (%d bytes)",
2895 (int)(image
.sections
[i
].size
));
2899 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
2900 if (retval
!= ERROR_OK
) {
2905 uint32_t offset
= 0;
2906 uint32_t length
= buf_cnt
;
2908 /* DANGER!!! beware of unsigned comparision here!!! */
2910 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
2911 (image
.sections
[i
].base_address
< max_address
)) {
2913 if (image
.sections
[i
].base_address
< min_address
) {
2914 /* clip addresses below */
2915 offset
+= min_address
-image
.sections
[i
].base_address
;
2919 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2920 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2922 retval
= target_write_buffer(target
,
2923 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
2924 if (retval
!= ERROR_OK
) {
2928 image_size
+= length
;
2929 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
2930 (unsigned int)length
,
2931 image
.sections
[i
].base_address
+ offset
);
2937 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
2938 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
2939 "in %fs (%0.3f KiB/s)", image_size
,
2940 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2943 image_close(&image
);
2949 COMMAND_HANDLER(handle_dump_image_command
)
2951 struct fileio fileio
;
2953 int retval
, retvaltemp
;
2954 uint32_t address
, size
;
2955 struct duration bench
;
2956 struct target
*target
= get_current_target(CMD_CTX
);
2959 return ERROR_COMMAND_SYNTAX_ERROR
;
2961 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
2962 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
2964 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
2965 buffer
= malloc(buf_size
);
2969 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
2970 if (retval
!= ERROR_OK
) {
2975 duration_start(&bench
);
2978 size_t size_written
;
2979 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
2980 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2981 if (retval
!= ERROR_OK
)
2984 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2985 if (retval
!= ERROR_OK
)
2988 size
-= this_run_size
;
2989 address
+= this_run_size
;
2994 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
2996 retval
= fileio_size(&fileio
, &filesize
);
2997 if (retval
!= ERROR_OK
)
2999 command_print(CMD_CTX
,
3000 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize
,
3001 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3004 retvaltemp
= fileio_close(&fileio
);
3005 if (retvaltemp
!= ERROR_OK
)
3011 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
3015 uint32_t image_size
;
3018 uint32_t checksum
= 0;
3019 uint32_t mem_checksum
= 0;
3023 struct target
*target
= get_current_target(CMD_CTX
);
3026 return ERROR_COMMAND_SYNTAX_ERROR
;
3029 LOG_ERROR("no target selected");
3033 struct duration bench
;
3034 duration_start(&bench
);
3036 if (CMD_ARGC
>= 2) {
3038 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
3039 image
.base_address
= addr
;
3040 image
.base_address_set
= 1;
3042 image
.base_address_set
= 0;
3043 image
.base_address
= 0x0;
3046 image
.start_address_set
= 0;
3048 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3049 if (retval
!= ERROR_OK
)
3055 for (i
= 0; i
< image
.num_sections
; i
++) {
3056 buffer
= malloc(image
.sections
[i
].size
);
3057 if (buffer
== NULL
) {
3058 command_print(CMD_CTX
,
3059 "error allocating buffer for section (%d bytes)",
3060 (int)(image
.sections
[i
].size
));
3063 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3064 if (retval
!= ERROR_OK
) {
3070 /* calculate checksum of image */
3071 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3072 if (retval
!= ERROR_OK
) {
3077 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3078 if (retval
!= ERROR_OK
) {
3083 if (checksum
!= mem_checksum
) {
3084 /* failed crc checksum, fall back to a binary compare */
3088 LOG_ERROR("checksum mismatch - attempting binary compare");
3090 data
= (uint8_t *)malloc(buf_cnt
);
3092 /* Can we use 32bit word accesses? */
3094 int count
= buf_cnt
;
3095 if ((count
% 4) == 0) {
3099 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
3100 if (retval
== ERROR_OK
) {
3102 for (t
= 0; t
< buf_cnt
; t
++) {
3103 if (data
[t
] != buffer
[t
]) {
3104 command_print(CMD_CTX
,
3105 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3107 (unsigned)(t
+ image
.sections
[i
].base_address
),
3110 if (diffs
++ >= 127) {
3111 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
3123 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
3124 image
.sections
[i
].base_address
,
3129 image_size
+= buf_cnt
;
3132 command_print(CMD_CTX
, "No more differences found.");
3135 retval
= ERROR_FAIL
;
3136 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3137 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3138 "in %fs (%0.3f KiB/s)", image_size
,
3139 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3142 image_close(&image
);
3147 COMMAND_HANDLER(handle_verify_image_command
)
3149 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
3152 COMMAND_HANDLER(handle_test_image_command
)
3154 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
3157 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
3159 struct target
*target
= get_current_target(cmd_ctx
);
3160 struct breakpoint
*breakpoint
= target
->breakpoints
;
3161 while (breakpoint
) {
3162 if (breakpoint
->type
== BKPT_SOFT
) {
3163 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3164 breakpoint
->length
, 16);
3165 command_print(cmd_ctx
, "IVA breakpoint: 0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
3166 breakpoint
->address
,
3168 breakpoint
->set
, buf
);
3171 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3172 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3174 breakpoint
->length
, breakpoint
->set
);
3175 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3176 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3177 breakpoint
->address
,
3178 breakpoint
->length
, breakpoint
->set
);
3179 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3182 command_print(cmd_ctx
, "Breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3183 breakpoint
->address
,
3184 breakpoint
->length
, breakpoint
->set
);
3187 breakpoint
= breakpoint
->next
;
3192 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
3193 uint32_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3195 struct target
*target
= get_current_target(cmd_ctx
);
3198 int retval
= breakpoint_add(target
, addr
, length
, hw
);
3199 if (ERROR_OK
== retval
)
3200 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
3202 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3205 } else if (addr
== 0) {
3206 int retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3207 if (ERROR_OK
== retval
)
3208 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3210 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3214 int retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3215 if (ERROR_OK
== retval
)
3216 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3218 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3225 COMMAND_HANDLER(handle_bp_command
)
3234 return handle_bp_command_list(CMD_CTX
);
3238 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3239 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3240 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3243 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
3245 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3247 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3250 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3251 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
3253 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
3254 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3256 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3261 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3262 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
3263 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
3264 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3267 return ERROR_COMMAND_SYNTAX_ERROR
;
3271 COMMAND_HANDLER(handle_rbp_command
)
3274 return ERROR_COMMAND_SYNTAX_ERROR
;
3277 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3279 struct target
*target
= get_current_target(CMD_CTX
);
3280 breakpoint_remove(target
, addr
);
3285 COMMAND_HANDLER(handle_wp_command
)
3287 struct target
*target
= get_current_target(CMD_CTX
);
3289 if (CMD_ARGC
== 0) {
3290 struct watchpoint
*watchpoint
= target
->watchpoints
;
3292 while (watchpoint
) {
3293 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
3294 ", len: 0x%8.8" PRIx32
3295 ", r/w/a: %i, value: 0x%8.8" PRIx32
3296 ", mask: 0x%8.8" PRIx32
,
3297 watchpoint
->address
,
3299 (int)watchpoint
->rw
,
3302 watchpoint
= watchpoint
->next
;
3307 enum watchpoint_rw type
= WPT_ACCESS
;
3309 uint32_t length
= 0;
3310 uint32_t data_value
= 0x0;
3311 uint32_t data_mask
= 0xffffffff;
3315 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
3318 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3321 switch (CMD_ARGV
[2][0]) {
3332 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3333 return ERROR_COMMAND_SYNTAX_ERROR
;
3337 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3338 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3342 return ERROR_COMMAND_SYNTAX_ERROR
;
3345 int retval
= watchpoint_add(target
, addr
, length
, type
,
3346 data_value
, data_mask
);
3347 if (ERROR_OK
!= retval
)
3348 LOG_ERROR("Failure setting watchpoints");
3353 COMMAND_HANDLER(handle_rwp_command
)
3356 return ERROR_COMMAND_SYNTAX_ERROR
;
3359 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3361 struct target
*target
= get_current_target(CMD_CTX
);
3362 watchpoint_remove(target
, addr
);
3368 * Translate a virtual address to a physical address.
3370 * The low-level target implementation must have logged a detailed error
3371 * which is forwarded to telnet/GDB session.
3373 COMMAND_HANDLER(handle_virt2phys_command
)
3376 return ERROR_COMMAND_SYNTAX_ERROR
;
3379 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
3382 struct target
*target
= get_current_target(CMD_CTX
);
3383 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3384 if (retval
== ERROR_OK
)
3385 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
3390 static void writeData(FILE *f
, const void *data
, size_t len
)
3392 size_t written
= fwrite(data
, 1, len
, f
);
3394 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3397 static void writeLong(FILE *f
, int l
)
3400 for (i
= 0; i
< 4; i
++) {
3401 char c
= (l
>> (i
*8))&0xff;
3402 writeData(f
, &c
, 1);
3407 static void writeString(FILE *f
, char *s
)
3409 writeData(f
, s
, strlen(s
));
3412 /* Dump a gmon.out histogram file. */
3413 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
)
3416 FILE *f
= fopen(filename
, "w");
3419 writeString(f
, "gmon");
3420 writeLong(f
, 0x00000001); /* Version */
3421 writeLong(f
, 0); /* padding */
3422 writeLong(f
, 0); /* padding */
3423 writeLong(f
, 0); /* padding */
3425 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3426 writeData(f
, &zero
, 1);
3428 /* figure out bucket size */
3429 uint32_t min
= samples
[0];
3430 uint32_t max
= samples
[0];
3431 for (i
= 0; i
< sampleNum
; i
++) {
3432 if (min
> samples
[i
])
3434 if (max
< samples
[i
])
3438 int addressSpace
= (max
- min
+ 1);
3439 assert(addressSpace
>= 2);
3441 static const uint32_t maxBuckets
= 16 * 1024; /* maximum buckets. */
3442 uint32_t length
= addressSpace
;
3443 if (length
> maxBuckets
)
3444 length
= maxBuckets
;
3445 int *buckets
= malloc(sizeof(int)*length
);
3446 if (buckets
== NULL
) {
3450 memset(buckets
, 0, sizeof(int) * length
);
3451 for (i
= 0; i
< sampleNum
; i
++) {
3452 uint32_t address
= samples
[i
];
3453 long long a
= address
- min
;
3454 long long b
= length
- 1;
3455 long long c
= addressSpace
- 1;
3456 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
3460 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3461 writeLong(f
, min
); /* low_pc */
3462 writeLong(f
, max
); /* high_pc */
3463 writeLong(f
, length
); /* # of samples */
3464 writeLong(f
, 100); /* KLUDGE! We lie, ca. 100Hz best case. */
3465 writeString(f
, "seconds");
3466 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3467 writeData(f
, &zero
, 1);
3468 writeString(f
, "s");
3470 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3472 char *data
= malloc(2 * length
);
3474 for (i
= 0; i
< length
; i
++) {
3479 data
[i
* 2] = val
&0xff;
3480 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
3483 writeData(f
, data
, length
* 2);
3491 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3492 * which will be used as a random sampling of PC */
3493 COMMAND_HANDLER(handle_profile_command
)
3495 struct target
*target
= get_current_target(CMD_CTX
);
3496 struct timeval timeout
, now
;
3498 gettimeofday(&timeout
, NULL
);
3500 return ERROR_COMMAND_SYNTAX_ERROR
;
3502 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], offset
);
3504 timeval_add_time(&timeout
, offset
, 0);
3507 * @todo: Some cores let us sample the PC without the
3508 * annoying halt/resume step; for example, ARMv7 PCSR.
3509 * Provide a way to use that more efficient mechanism.
3512 command_print(CMD_CTX
, "Starting profiling. Halting and resuming the target as often as we can...");
3514 static const int maxSample
= 10000;
3515 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
3516 if (samples
== NULL
)
3520 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3521 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
3523 int retval
= ERROR_OK
;
3525 target_poll(target
);
3526 if (target
->state
== TARGET_HALTED
) {
3527 uint32_t t
= *((uint32_t *)reg
->value
);
3528 samples
[numSamples
++] = t
;
3529 /* current pc, addr = 0, do not handle breakpoints, not debugging */
3530 retval
= target_resume(target
, 1, 0, 0, 0);
3531 target_poll(target
);
3532 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3533 } else if (target
->state
== TARGET_RUNNING
) {
3534 /* We want to quickly sample the PC. */
3535 retval
= target_halt(target
);
3536 if (retval
!= ERROR_OK
) {
3541 command_print(CMD_CTX
, "Target not halted or running");
3545 if (retval
!= ERROR_OK
)
3548 gettimeofday(&now
, NULL
);
3549 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
)
3550 && (now
.tv_usec
>= timeout
.tv_usec
))) {
3551 command_print(CMD_CTX
, "Profiling completed. %d samples.", numSamples
);
3552 retval
= target_poll(target
);
3553 if (retval
!= ERROR_OK
) {
3557 if (target
->state
== TARGET_HALTED
) {
3558 /* current pc, addr = 0, do not handle
3559 * breakpoints, not debugging */
3560 target_resume(target
, 1, 0, 0, 0);
3562 retval
= target_poll(target
);
3563 if (retval
!= ERROR_OK
) {
3567 writeGmon(samples
, numSamples
, CMD_ARGV
[1]);
3568 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3577 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
3580 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3583 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3587 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3588 valObjPtr
= Jim_NewIntObj(interp
, val
);
3589 if (!nameObjPtr
|| !valObjPtr
) {
3594 Jim_IncrRefCount(nameObjPtr
);
3595 Jim_IncrRefCount(valObjPtr
);
3596 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3597 Jim_DecrRefCount(interp
, nameObjPtr
);
3598 Jim_DecrRefCount(interp
, valObjPtr
);
3600 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3604 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3606 struct command_context
*context
;
3607 struct target
*target
;
3609 context
= current_command_context(interp
);
3610 assert(context
!= NULL
);
3612 target
= get_current_target(context
);
3613 if (target
== NULL
) {
3614 LOG_ERROR("mem2array: no current target");
3618 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
3621 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3629 const char *varname
;
3633 /* argv[1] = name of array to receive the data
3634 * argv[2] = desired width
3635 * argv[3] = memory address
3636 * argv[4] = count of times to read
3639 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3642 varname
= Jim_GetString(argv
[0], &len
);
3643 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3645 e
= Jim_GetLong(interp
, argv
[1], &l
);
3650 e
= Jim_GetLong(interp
, argv
[2], &l
);
3654 e
= Jim_GetLong(interp
, argv
[3], &l
);
3669 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3670 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3674 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3675 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3678 if ((addr
+ (len
* width
)) < addr
) {
3679 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3680 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3683 /* absurd transfer size? */
3685 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3686 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3691 ((width
== 2) && ((addr
& 1) == 0)) ||
3692 ((width
== 4) && ((addr
& 3) == 0))) {
3696 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3697 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3700 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3709 size_t buffersize
= 4096;
3710 uint8_t *buffer
= malloc(buffersize
);
3717 /* Slurp... in buffer size chunks */
3719 count
= len
; /* in objects.. */
3720 if (count
> (buffersize
/ width
))
3721 count
= (buffersize
/ width
);
3723 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3724 if (retval
!= ERROR_OK
) {
3726 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3730 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3731 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3735 v
= 0; /* shut up gcc */
3736 for (i
= 0; i
< count
; i
++, n
++) {
3739 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3742 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3745 v
= buffer
[i
] & 0x0ff;
3748 new_int_array_element(interp
, varname
, n
, v
);
3756 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3761 static int get_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t *val
)
3764 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3768 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3772 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3778 Jim_IncrRefCount(nameObjPtr
);
3779 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3780 Jim_DecrRefCount(interp
, nameObjPtr
);
3782 if (valObjPtr
== NULL
)
3785 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3786 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3791 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3793 struct command_context
*context
;
3794 struct target
*target
;
3796 context
= current_command_context(interp
);
3797 assert(context
!= NULL
);
3799 target
= get_current_target(context
);
3800 if (target
== NULL
) {
3801 LOG_ERROR("array2mem: no current target");
3805 return target_array2mem(interp
, target
, argc
-1, argv
+ 1);
3808 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
3809 int argc
, Jim_Obj
*const *argv
)
3817 const char *varname
;
3821 /* argv[1] = name of array to get the data
3822 * argv[2] = desired width
3823 * argv[3] = memory address
3824 * argv[4] = count to write
3827 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
3830 varname
= Jim_GetString(argv
[0], &len
);
3831 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3833 e
= Jim_GetLong(interp
, argv
[1], &l
);
3838 e
= Jim_GetLong(interp
, argv
[2], &l
);
3842 e
= Jim_GetLong(interp
, argv
[3], &l
);
3857 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3858 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3859 "Invalid width param, must be 8/16/32", NULL
);
3863 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3864 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3865 "array2mem: zero width read?", NULL
);
3868 if ((addr
+ (len
* width
)) < addr
) {
3869 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3870 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3871 "array2mem: addr + len - wraps to zero?", NULL
);
3874 /* absurd transfer size? */
3876 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3877 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3878 "array2mem: absurd > 64K item request", NULL
);
3883 ((width
== 2) && ((addr
& 1) == 0)) ||
3884 ((width
== 4) && ((addr
& 3) == 0))) {
3888 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3889 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3892 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3903 size_t buffersize
= 4096;
3904 uint8_t *buffer
= malloc(buffersize
);
3909 /* Slurp... in buffer size chunks */
3911 count
= len
; /* in objects.. */
3912 if (count
> (buffersize
/ width
))
3913 count
= (buffersize
/ width
);
3915 v
= 0; /* shut up gcc */
3916 for (i
= 0; i
< count
; i
++, n
++) {
3917 get_int_array_element(interp
, varname
, n
, &v
);
3920 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
3923 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
3926 buffer
[i
] = v
& 0x0ff;
3932 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3933 if (retval
!= ERROR_OK
) {
3935 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3939 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3940 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3948 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3953 /* FIX? should we propagate errors here rather than printing them
3956 void target_handle_event(struct target
*target
, enum target_event e
)
3958 struct target_event_action
*teap
;
3960 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3961 if (teap
->event
== e
) {
3962 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3963 target
->target_number
,
3964 target_name(target
),
3965 target_type_name(target
),
3967 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3968 Jim_GetString(teap
->body
, NULL
));
3969 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
) {
3970 Jim_MakeErrorMessage(teap
->interp
);
3971 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
3978 * Returns true only if the target has a handler for the specified event.
3980 bool target_has_event_action(struct target
*target
, enum target_event event
)
3982 struct target_event_action
*teap
;
3984 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3985 if (teap
->event
== event
)
3991 enum target_cfg_param
{
3994 TCFG_WORK_AREA_VIRT
,
3995 TCFG_WORK_AREA_PHYS
,
3996 TCFG_WORK_AREA_SIZE
,
3997 TCFG_WORK_AREA_BACKUP
,
4001 TCFG_CHAIN_POSITION
,
4006 static Jim_Nvp nvp_config_opts
[] = {
4007 { .name
= "-type", .value
= TCFG_TYPE
},
4008 { .name
= "-event", .value
= TCFG_EVENT
},
4009 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
4010 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
4011 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
4012 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
4013 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
4014 { .name
= "-variant", .value
= TCFG_VARIANT
},
4015 { .name
= "-coreid", .value
= TCFG_COREID
},
4016 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
4017 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
4018 { .name
= "-rtos", .value
= TCFG_RTOS
},
4019 { .name
= NULL
, .value
= -1 }
4022 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
4030 /* parse config or cget options ... */
4031 while (goi
->argc
> 0) {
4032 Jim_SetEmptyResult(goi
->interp
);
4033 /* Jim_GetOpt_Debug(goi); */
4035 if (target
->type
->target_jim_configure
) {
4036 /* target defines a configure function */
4037 /* target gets first dibs on parameters */
4038 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
4047 /* otherwise we 'continue' below */
4049 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
4051 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
4057 if (goi
->isconfigure
) {
4058 Jim_SetResultFormatted(goi
->interp
,
4059 "not settable: %s", n
->name
);
4063 if (goi
->argc
!= 0) {
4064 Jim_WrongNumArgs(goi
->interp
,
4065 goi
->argc
, goi
->argv
,
4070 Jim_SetResultString(goi
->interp
,
4071 target_type_name(target
), -1);
4075 if (goi
->argc
== 0) {
4076 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
4080 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
4082 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
4086 if (goi
->isconfigure
) {
4087 if (goi
->argc
!= 1) {
4088 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
4092 if (goi
->argc
!= 0) {
4093 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
4099 struct target_event_action
*teap
;
4101 teap
= target
->event_action
;
4102 /* replace existing? */
4104 if (teap
->event
== (enum target_event
)n
->value
)
4109 if (goi
->isconfigure
) {
4110 bool replace
= true;
4113 teap
= calloc(1, sizeof(*teap
));
4116 teap
->event
= n
->value
;
4117 teap
->interp
= goi
->interp
;
4118 Jim_GetOpt_Obj(goi
, &o
);
4120 Jim_DecrRefCount(teap
->interp
, teap
->body
);
4121 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
4124 * Tcl/TK - "tk events" have a nice feature.
4125 * See the "BIND" command.
4126 * We should support that here.
4127 * You can specify %X and %Y in the event code.
4128 * The idea is: %T - target name.
4129 * The idea is: %N - target number
4130 * The idea is: %E - event name.
4132 Jim_IncrRefCount(teap
->body
);
4135 /* add to head of event list */
4136 teap
->next
= target
->event_action
;
4137 target
->event_action
= teap
;
4139 Jim_SetEmptyResult(goi
->interp
);
4143 Jim_SetEmptyResult(goi
->interp
);
4145 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
4151 case TCFG_WORK_AREA_VIRT
:
4152 if (goi
->isconfigure
) {
4153 target_free_all_working_areas(target
);
4154 e
= Jim_GetOpt_Wide(goi
, &w
);
4157 target
->working_area_virt
= w
;
4158 target
->working_area_virt_spec
= true;
4163 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
4167 case TCFG_WORK_AREA_PHYS
:
4168 if (goi
->isconfigure
) {
4169 target_free_all_working_areas(target
);
4170 e
= Jim_GetOpt_Wide(goi
, &w
);
4173 target
->working_area_phys
= w
;
4174 target
->working_area_phys_spec
= true;
4179 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
4183 case TCFG_WORK_AREA_SIZE
:
4184 if (goi
->isconfigure
) {
4185 target_free_all_working_areas(target
);
4186 e
= Jim_GetOpt_Wide(goi
, &w
);
4189 target
->working_area_size
= w
;
4194 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4198 case TCFG_WORK_AREA_BACKUP
:
4199 if (goi
->isconfigure
) {
4200 target_free_all_working_areas(target
);
4201 e
= Jim_GetOpt_Wide(goi
, &w
);
4204 /* make this exactly 1 or 0 */
4205 target
->backup_working_area
= (!!w
);
4210 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
4211 /* loop for more e*/
4216 if (goi
->isconfigure
) {
4217 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
4219 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
4222 target
->endianness
= n
->value
;
4227 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4228 if (n
->name
== NULL
) {
4229 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4230 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4232 Jim_SetResultString(goi
->interp
, n
->name
, -1);
4237 if (goi
->isconfigure
) {
4238 if (goi
->argc
< 1) {
4239 Jim_SetResultFormatted(goi
->interp
,
4244 if (target
->variant
)
4245 free((void *)(target
->variant
));
4246 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
4249 target
->variant
= strdup(cp
);
4254 Jim_SetResultString(goi
->interp
, target
->variant
, -1);
4259 if (goi
->isconfigure
) {
4260 e
= Jim_GetOpt_Wide(goi
, &w
);
4263 target
->coreid
= (int32_t)w
;
4268 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4272 case TCFG_CHAIN_POSITION
:
4273 if (goi
->isconfigure
) {
4275 struct jtag_tap
*tap
;
4276 target_free_all_working_areas(target
);
4277 e
= Jim_GetOpt_Obj(goi
, &o_t
);
4280 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
4283 /* make this exactly 1 or 0 */
4289 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
4290 /* loop for more e*/
4293 if (goi
->isconfigure
) {
4294 e
= Jim_GetOpt_Wide(goi
, &w
);
4297 target
->dbgbase
= (uint32_t)w
;
4298 target
->dbgbase_set
= true;
4303 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
4310 int result
= rtos_create(goi
, target
);
4311 if (result
!= JIM_OK
)
4317 } /* while (goi->argc) */
4320 /* done - we return */
4324 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4328 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4329 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4330 int need_args
= 1 + goi
.isconfigure
;
4331 if (goi
.argc
< need_args
) {
4332 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4334 ? "missing: -option VALUE ..."
4335 : "missing: -option ...");
4338 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4339 return target_configure(&goi
, target
);
4342 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4344 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4347 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4349 if (goi
.argc
< 2 || goi
.argc
> 4) {
4350 Jim_SetResultFormatted(goi
.interp
,
4351 "usage: %s [phys] <address> <data> [<count>]", cmd_name
);
4356 fn
= target_write_memory_fast
;
4359 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4361 struct Jim_Obj
*obj
;
4362 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4366 fn
= target_write_phys_memory
;
4370 e
= Jim_GetOpt_Wide(&goi
, &a
);
4375 e
= Jim_GetOpt_Wide(&goi
, &b
);
4380 if (goi
.argc
== 1) {
4381 e
= Jim_GetOpt_Wide(&goi
, &c
);
4386 /* all args must be consumed */
4390 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4392 if (strcasecmp(cmd_name
, "mww") == 0)
4394 else if (strcasecmp(cmd_name
, "mwh") == 0)
4396 else if (strcasecmp(cmd_name
, "mwb") == 0)
4399 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4403 return (target_fill_mem(target
, a
, fn
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4407 * @brief Reads an array of words/halfwords/bytes from target memory starting at specified address.
4409 * Usage: mdw [phys] <address> [<count>] - for 32 bit reads
4410 * mdh [phys] <address> [<count>] - for 16 bit reads
4411 * mdb [phys] <address> [<count>] - for 8 bit reads
4413 * Count defaults to 1.
4415 * Calls target_read_memory or target_read_phys_memory depending on
4416 * the presence of the "phys" argument
4417 * Reads the target memory in blocks of max. 32 bytes, and returns an array of ints formatted
4418 * to int representation in base16.
4419 * Also outputs read data in a human readable form using command_print
4421 * @param phys if present target_read_phys_memory will be used instead of target_read_memory
4422 * @param address address where to start the read. May be specified in decimal or hex using the standard "0x" prefix
4423 * @param count optional count parameter to read an array of values. If not specified, defaults to 1.
4424 * @returns: JIM_ERR on error or JIM_OK on success and sets the result string to an array of ascii formatted numbers
4425 * on success, with [<count>] number of elements.
4427 * In case of little endian target:
4428 * Example1: "mdw 0x00000000" returns "10123456"
4429 * Exmaple2: "mdh 0x00000000 1" returns "3456"
4430 * Example3: "mdb 0x00000000" returns "56"
4431 * Example4: "mdh 0x00000000 2" returns "3456 1012"
4432 * Example5: "mdb 0x00000000 3" returns "56 34 12"
4434 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4436 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4439 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4441 if ((goi
.argc
< 1) || (goi
.argc
> 3)) {
4442 Jim_SetResultFormatted(goi
.interp
,
4443 "usage: %s [phys] <address> [<count>]", cmd_name
);
4447 int (*fn
)(struct target
*target
,
4448 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
4449 fn
= target_read_memory
;
4452 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4454 struct Jim_Obj
*obj
;
4455 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4459 fn
= target_read_phys_memory
;
4462 /* Read address parameter */
4464 e
= Jim_GetOpt_Wide(&goi
, &addr
);
4468 /* If next parameter exists, read it out as the count parameter, if not, set it to 1 (default) */
4470 if (goi
.argc
== 1) {
4471 e
= Jim_GetOpt_Wide(&goi
, &count
);
4477 /* all args must be consumed */
4481 jim_wide dwidth
= 1; /* shut up gcc */
4482 if (strcasecmp(cmd_name
, "mdw") == 0)
4484 else if (strcasecmp(cmd_name
, "mdh") == 0)
4486 else if (strcasecmp(cmd_name
, "mdb") == 0)
4489 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4493 /* convert count to "bytes" */
4494 int bytes
= count
* dwidth
;
4496 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4497 uint8_t target_buf
[32];
4500 y
= (bytes
< 16) ? bytes
: 16; /* y = min(bytes, 16); */
4502 /* Try to read out next block */
4503 e
= fn(target
, addr
, dwidth
, y
/ dwidth
, target_buf
);
4505 if (e
!= ERROR_OK
) {
4506 Jim_SetResultFormatted(interp
, "error reading target @ 0x%08lx", (long)addr
);
4510 command_print_sameline(NULL
, "0x%08x ", (int)(addr
));
4513 for (x
= 0; x
< 16 && x
< y
; x
+= 4) {
4514 z
= target_buffer_get_u32(target
, &(target_buf
[x
]));
4515 command_print_sameline(NULL
, "%08x ", (int)(z
));
4517 for (; (x
< 16) ; x
+= 4)
4518 command_print_sameline(NULL
, " ");
4521 for (x
= 0; x
< 16 && x
< y
; x
+= 2) {
4522 z
= target_buffer_get_u16(target
, &(target_buf
[x
]));
4523 command_print_sameline(NULL
, "%04x ", (int)(z
));
4525 for (; (x
< 16) ; x
+= 2)
4526 command_print_sameline(NULL
, " ");
4530 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
4531 z
= target_buffer_get_u8(target
, &(target_buf
[x
]));
4532 command_print_sameline(NULL
, "%02x ", (int)(z
));
4534 for (; (x
< 16) ; x
+= 1)
4535 command_print_sameline(NULL
, " ");
4538 /* ascii-ify the bytes */
4539 for (x
= 0 ; x
< y
; x
++) {
4540 if ((target_buf
[x
] >= 0x20) &&
4541 (target_buf
[x
] <= 0x7e)) {
4545 target_buf
[x
] = '.';
4550 target_buf
[x
] = ' ';
4555 /* print - with a newline */
4556 command_print_sameline(NULL
, "%s\n", target_buf
);
4564 static int jim_target_mem2array(Jim_Interp
*interp
,
4565 int argc
, Jim_Obj
*const *argv
)
4567 struct target
*target
= Jim_CmdPrivData(interp
);
4568 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4571 static int jim_target_array2mem(Jim_Interp
*interp
,
4572 int argc
, Jim_Obj
*const *argv
)
4574 struct target
*target
= Jim_CmdPrivData(interp
);
4575 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4578 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4580 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
4584 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4587 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4590 struct target
*target
= Jim_CmdPrivData(interp
);
4591 if (!target
->tap
->enabled
)
4592 return jim_target_tap_disabled(interp
);
4594 int e
= target
->type
->examine(target
);
4600 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4603 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4606 struct target
*target
= Jim_CmdPrivData(interp
);
4608 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
4614 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4617 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4620 struct target
*target
= Jim_CmdPrivData(interp
);
4621 if (!target
->tap
->enabled
)
4622 return jim_target_tap_disabled(interp
);
4625 if (!(target_was_examined(target
)))
4626 e
= ERROR_TARGET_NOT_EXAMINED
;
4628 e
= target
->type
->poll(target
);
4634 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4637 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4639 if (goi
.argc
!= 2) {
4640 Jim_WrongNumArgs(interp
, 0, argv
,
4641 "([tT]|[fF]|assert|deassert) BOOL");
4646 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4648 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4651 /* the halt or not param */
4653 e
= Jim_GetOpt_Wide(&goi
, &a
);
4657 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4658 if (!target
->tap
->enabled
)
4659 return jim_target_tap_disabled(interp
);
4660 if (!(target_was_examined(target
))) {
4661 LOG_ERROR("Target not examined yet");
4662 return ERROR_TARGET_NOT_EXAMINED
;
4664 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
4665 Jim_SetResultFormatted(interp
,
4666 "No target-specific reset for %s",
4667 target_name(target
));
4670 /* determine if we should halt or not. */
4671 target
->reset_halt
= !!a
;
4672 /* When this happens - all workareas are invalid. */
4673 target_free_all_working_areas_restore(target
, 0);
4676 if (n
->value
== NVP_ASSERT
)
4677 e
= target
->type
->assert_reset(target
);
4679 e
= target
->type
->deassert_reset(target
);
4680 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4683 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4686 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4689 struct target
*target
= Jim_CmdPrivData(interp
);
4690 if (!target
->tap
->enabled
)
4691 return jim_target_tap_disabled(interp
);
4692 int e
= target
->type
->halt(target
);
4693 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4696 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4699 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4701 /* params: <name> statename timeoutmsecs */
4702 if (goi
.argc
!= 2) {
4703 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4704 Jim_SetResultFormatted(goi
.interp
,
4705 "%s <state_name> <timeout_in_msec>", cmd_name
);
4710 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4712 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
, 1);
4716 e
= Jim_GetOpt_Wide(&goi
, &a
);
4719 struct target
*target
= Jim_CmdPrivData(interp
);
4720 if (!target
->tap
->enabled
)
4721 return jim_target_tap_disabled(interp
);
4723 e
= target_wait_state(target
, n
->value
, a
);
4724 if (e
!= ERROR_OK
) {
4725 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
4726 Jim_SetResultFormatted(goi
.interp
,
4727 "target: %s wait %s fails (%#s) %s",
4728 target_name(target
), n
->name
,
4729 eObj
, target_strerror_safe(e
));
4730 Jim_FreeNewObj(interp
, eObj
);
4735 /* List for human, Events defined for this target.
4736 * scripts/programs should use 'name cget -event NAME'
4738 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4740 struct command_context
*cmd_ctx
= current_command_context(interp
);
4741 assert(cmd_ctx
!= NULL
);
4743 struct target
*target
= Jim_CmdPrivData(interp
);
4744 struct target_event_action
*teap
= target
->event_action
;
4745 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4746 target
->target_number
,
4747 target_name(target
));
4748 command_print(cmd_ctx
, "%-25s | Body", "Event");
4749 command_print(cmd_ctx
, "------------------------- | "
4750 "----------------------------------------");
4752 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
4753 command_print(cmd_ctx
, "%-25s | %s",
4754 opt
->name
, Jim_GetString(teap
->body
, NULL
));
4757 command_print(cmd_ctx
, "***END***");
4760 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4763 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4766 struct target
*target
= Jim_CmdPrivData(interp
);
4767 Jim_SetResultString(interp
, target_state_name(target
), -1);
4770 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4773 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4774 if (goi
.argc
!= 1) {
4775 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4776 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
4780 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4782 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4785 struct target
*target
= Jim_CmdPrivData(interp
);
4786 target_handle_event(target
, n
->value
);
4790 static const struct command_registration target_instance_command_handlers
[] = {
4792 .name
= "configure",
4793 .mode
= COMMAND_CONFIG
,
4794 .jim_handler
= jim_target_configure
,
4795 .help
= "configure a new target for use",
4796 .usage
= "[target_attribute ...]",
4800 .mode
= COMMAND_ANY
,
4801 .jim_handler
= jim_target_configure
,
4802 .help
= "returns the specified target attribute",
4803 .usage
= "target_attribute",
4807 .mode
= COMMAND_EXEC
,
4808 .jim_handler
= jim_target_mw
,
4809 .help
= "Write 32-bit word(s) to target memory",
4810 .usage
= "address data [count]",
4814 .mode
= COMMAND_EXEC
,
4815 .jim_handler
= jim_target_mw
,
4816 .help
= "Write 16-bit half-word(s) to target memory",
4817 .usage
= "address data [count]",
4821 .mode
= COMMAND_EXEC
,
4822 .jim_handler
= jim_target_mw
,
4823 .help
= "Write byte(s) to target memory",
4824 .usage
= "address data [count]",
4828 .mode
= COMMAND_EXEC
,
4829 .jim_handler
= jim_target_md
,
4830 .help
= "Display target memory as 32-bit words",
4831 .usage
= "address [count]",
4835 .mode
= COMMAND_EXEC
,
4836 .jim_handler
= jim_target_md
,
4837 .help
= "Display target memory as 16-bit half-words",
4838 .usage
= "address [count]",
4842 .mode
= COMMAND_EXEC
,
4843 .jim_handler
= jim_target_md
,
4844 .help
= "Display target memory as 8-bit bytes",
4845 .usage
= "address [count]",
4848 .name
= "array2mem",
4849 .mode
= COMMAND_EXEC
,
4850 .jim_handler
= jim_target_array2mem
,
4851 .help
= "Writes Tcl array of 8/16/32 bit numbers "
4853 .usage
= "arrayname bitwidth address count",
4856 .name
= "mem2array",
4857 .mode
= COMMAND_EXEC
,
4858 .jim_handler
= jim_target_mem2array
,
4859 .help
= "Loads Tcl array of 8/16/32 bit numbers "
4860 "from target memory",
4861 .usage
= "arrayname bitwidth address count",
4864 .name
= "eventlist",
4865 .mode
= COMMAND_EXEC
,
4866 .jim_handler
= jim_target_event_list
,
4867 .help
= "displays a table of events defined for this target",
4871 .mode
= COMMAND_EXEC
,
4872 .jim_handler
= jim_target_current_state
,
4873 .help
= "displays the current state of this target",
4876 .name
= "arp_examine",
4877 .mode
= COMMAND_EXEC
,
4878 .jim_handler
= jim_target_examine
,
4879 .help
= "used internally for reset processing",
4882 .name
= "arp_halt_gdb",
4883 .mode
= COMMAND_EXEC
,
4884 .jim_handler
= jim_target_halt_gdb
,
4885 .help
= "used internally for reset processing to halt GDB",
4889 .mode
= COMMAND_EXEC
,
4890 .jim_handler
= jim_target_poll
,
4891 .help
= "used internally for reset processing",
4894 .name
= "arp_reset",
4895 .mode
= COMMAND_EXEC
,
4896 .jim_handler
= jim_target_reset
,
4897 .help
= "used internally for reset processing",
4901 .mode
= COMMAND_EXEC
,
4902 .jim_handler
= jim_target_halt
,
4903 .help
= "used internally for reset processing",
4906 .name
= "arp_waitstate",
4907 .mode
= COMMAND_EXEC
,
4908 .jim_handler
= jim_target_wait_state
,
4909 .help
= "used internally for reset processing",
4912 .name
= "invoke-event",
4913 .mode
= COMMAND_EXEC
,
4914 .jim_handler
= jim_target_invoke_event
,
4915 .help
= "invoke handler for specified event",
4916 .usage
= "event_name",
4918 COMMAND_REGISTRATION_DONE
4921 static int target_create(Jim_GetOptInfo
*goi
)
4929 struct target
*target
;
4930 struct command_context
*cmd_ctx
;
4932 cmd_ctx
= current_command_context(goi
->interp
);
4933 assert(cmd_ctx
!= NULL
);
4935 if (goi
->argc
< 3) {
4936 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
4941 Jim_GetOpt_Obj(goi
, &new_cmd
);
4942 /* does this command exist? */
4943 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
4945 cp
= Jim_GetString(new_cmd
, NULL
);
4946 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
4951 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
4955 /* now does target type exist */
4956 for (x
= 0 ; target_types
[x
] ; x
++) {
4957 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
4962 /* check for deprecated name */
4963 if (target_types
[x
]->deprecated_name
) {
4964 if (0 == strcmp(cp
, target_types
[x
]->deprecated_name
)) {
4966 LOG_WARNING("target name is deprecated use: \'%s\'", target_types
[x
]->name
);
4971 if (target_types
[x
] == NULL
) {
4972 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
4973 for (x
= 0 ; target_types
[x
] ; x
++) {
4974 if (target_types
[x
+ 1]) {
4975 Jim_AppendStrings(goi
->interp
,
4976 Jim_GetResult(goi
->interp
),
4977 target_types
[x
]->name
,
4980 Jim_AppendStrings(goi
->interp
,
4981 Jim_GetResult(goi
->interp
),
4983 target_types
[x
]->name
, NULL
);
4990 target
= calloc(1, sizeof(struct target
));
4991 /* set target number */
4992 target
->target_number
= new_target_number();
4994 /* allocate memory for each unique target type */
4995 target
->type
= (struct target_type
*)calloc(1, sizeof(struct target_type
));
4997 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
4999 /* will be set by "-endian" */
5000 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5002 /* default to first core, override with -coreid */
5005 target
->working_area
= 0x0;
5006 target
->working_area_size
= 0x0;
5007 target
->working_areas
= NULL
;
5008 target
->backup_working_area
= 0;
5010 target
->state
= TARGET_UNKNOWN
;
5011 target
->debug_reason
= DBG_REASON_UNDEFINED
;
5012 target
->reg_cache
= NULL
;
5013 target
->breakpoints
= NULL
;
5014 target
->watchpoints
= NULL
;
5015 target
->next
= NULL
;
5016 target
->arch_info
= NULL
;
5018 target
->display
= 1;
5020 target
->halt_issued
= false;
5022 /* initialize trace information */
5023 target
->trace_info
= malloc(sizeof(struct trace
));
5024 target
->trace_info
->num_trace_points
= 0;
5025 target
->trace_info
->trace_points_size
= 0;
5026 target
->trace_info
->trace_points
= NULL
;
5027 target
->trace_info
->trace_history_size
= 0;
5028 target
->trace_info
->trace_history
= NULL
;
5029 target
->trace_info
->trace_history_pos
= 0;
5030 target
->trace_info
->trace_history_overflowed
= 0;
5032 target
->dbgmsg
= NULL
;
5033 target
->dbg_msg_enabled
= 0;
5035 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5037 target
->rtos
= NULL
;
5038 target
->rtos_auto_detect
= false;
5040 /* Do the rest as "configure" options */
5041 goi
->isconfigure
= 1;
5042 e
= target_configure(goi
, target
);
5044 if (target
->tap
== NULL
) {
5045 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
5055 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
5056 /* default endian to little if not specified */
5057 target
->endianness
= TARGET_LITTLE_ENDIAN
;
5060 /* incase variant is not set */
5061 if (!target
->variant
)
5062 target
->variant
= strdup("");
5064 cp
= Jim_GetString(new_cmd
, NULL
);
5065 target
->cmd_name
= strdup(cp
);
5067 /* create the target specific commands */
5068 if (target
->type
->commands
) {
5069 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
5071 LOG_ERROR("unable to register '%s' commands", cp
);
5073 if (target
->type
->target_create
)
5074 (*(target
->type
->target_create
))(target
, goi
->interp
);
5076 /* append to end of list */
5078 struct target
**tpp
;
5079 tpp
= &(all_targets
);
5081 tpp
= &((*tpp
)->next
);
5085 /* now - create the new target name command */
5086 const struct command_registration target_subcommands
[] = {
5088 .chain
= target_instance_command_handlers
,
5091 .chain
= target
->type
->commands
,
5093 COMMAND_REGISTRATION_DONE
5095 const struct command_registration target_commands
[] = {
5098 .mode
= COMMAND_ANY
,
5099 .help
= "target command group",
5101 .chain
= target_subcommands
,
5103 COMMAND_REGISTRATION_DONE
5105 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
5109 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
5111 command_set_handler_data(c
, target
);
5113 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
5116 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5119 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5122 struct command_context
*cmd_ctx
= current_command_context(interp
);
5123 assert(cmd_ctx
!= NULL
);
5125 Jim_SetResultString(interp
, target_name(get_current_target(cmd_ctx
)), -1);
5129 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5132 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5135 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5136 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++) {
5137 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5138 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
5143 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5146 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5149 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5150 struct target
*target
= all_targets
;
5152 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5153 Jim_NewStringObj(interp
, target_name(target
), -1));
5154 target
= target
->next
;
5159 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5162 const char *targetname
;
5164 struct target
*target
= (struct target
*) NULL
;
5165 struct target_list
*head
, *curr
, *new;
5166 curr
= (struct target_list
*) NULL
;
5167 head
= (struct target_list
*) NULL
;
5170 LOG_DEBUG("%d", argc
);
5171 /* argv[1] = target to associate in smp
5172 * argv[2] = target to assoicate in smp
5176 for (i
= 1; i
< argc
; i
++) {
5178 targetname
= Jim_GetString(argv
[i
], &len
);
5179 target
= get_target(targetname
);
5180 LOG_DEBUG("%s ", targetname
);
5182 new = malloc(sizeof(struct target_list
));
5183 new->target
= target
;
5184 new->next
= (struct target_list
*)NULL
;
5185 if (head
== (struct target_list
*)NULL
) {
5194 /* now parse the list of cpu and put the target in smp mode*/
5197 while (curr
!= (struct target_list
*)NULL
) {
5198 target
= curr
->target
;
5200 target
->head
= head
;
5204 if (target
&& target
->rtos
)
5205 retval
= rtos_smp_init(head
->target
);
5211 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5214 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5216 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5217 "<name> <target_type> [<target_options> ...]");
5220 return target_create(&goi
);
5223 static int jim_target_number(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5226 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5228 /* It's OK to remove this mechanism sometime after August 2010 or so */
5229 LOG_WARNING("don't use numbers as target identifiers; use names");
5230 if (goi
.argc
!= 1) {
5231 Jim_SetResultFormatted(goi
.interp
, "usage: target number <number>");
5235 int e
= Jim_GetOpt_Wide(&goi
, &w
);
5239 struct target
*target
;
5240 for (target
= all_targets
; NULL
!= target
; target
= target
->next
) {
5241 if (target
->target_number
!= w
)
5244 Jim_SetResultString(goi
.interp
, target_name(target
), -1);
5248 Jim_Obj
*wObj
= Jim_NewIntObj(goi
.interp
, w
);
5249 Jim_SetResultFormatted(goi
.interp
,
5250 "Target: number %#s does not exist", wObj
);
5251 Jim_FreeNewObj(interp
, wObj
);
5256 static int jim_target_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5259 Jim_WrongNumArgs(interp
, 1, argv
, "<no parameters>");
5263 struct target
*target
= all_targets
;
5264 while (NULL
!= target
) {
5265 target
= target
->next
;
5268 Jim_SetResult(interp
, Jim_NewIntObj(interp
, count
));
5272 static const struct command_registration target_subcommand_handlers
[] = {
5275 .mode
= COMMAND_CONFIG
,
5276 .handler
= handle_target_init_command
,
5277 .help
= "initialize targets",
5281 /* REVISIT this should be COMMAND_CONFIG ... */
5282 .mode
= COMMAND_ANY
,
5283 .jim_handler
= jim_target_create
,
5284 .usage
= "name type '-chain-position' name [options ...]",
5285 .help
= "Creates and selects a new target",
5289 .mode
= COMMAND_ANY
,
5290 .jim_handler
= jim_target_current
,
5291 .help
= "Returns the currently selected target",
5295 .mode
= COMMAND_ANY
,
5296 .jim_handler
= jim_target_types
,
5297 .help
= "Returns the available target types as "
5298 "a list of strings",
5302 .mode
= COMMAND_ANY
,
5303 .jim_handler
= jim_target_names
,
5304 .help
= "Returns the names of all targets as a list of strings",
5308 .mode
= COMMAND_ANY
,
5309 .jim_handler
= jim_target_number
,
5311 .help
= "Returns the name of the numbered target "
5316 .mode
= COMMAND_ANY
,
5317 .jim_handler
= jim_target_count
,
5318 .help
= "Returns the number of targets as an integer "
5323 .mode
= COMMAND_ANY
,
5324 .jim_handler
= jim_target_smp
,
5325 .usage
= "targetname1 targetname2 ...",
5326 .help
= "gather several target in a smp list"
5329 COMMAND_REGISTRATION_DONE
5339 static int fastload_num
;
5340 static struct FastLoad
*fastload
;
5342 static void free_fastload(void)
5344 if (fastload
!= NULL
) {
5346 for (i
= 0; i
< fastload_num
; i
++) {
5347 if (fastload
[i
].data
)
5348 free(fastload
[i
].data
);
5355 COMMAND_HANDLER(handle_fast_load_image_command
)
5359 uint32_t image_size
;
5360 uint32_t min_address
= 0;
5361 uint32_t max_address
= 0xffffffff;
5366 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
5367 &image
, &min_address
, &max_address
);
5368 if (ERROR_OK
!= retval
)
5371 struct duration bench
;
5372 duration_start(&bench
);
5374 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5375 if (retval
!= ERROR_OK
)
5380 fastload_num
= image
.num_sections
;
5381 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5382 if (fastload
== NULL
) {
5383 command_print(CMD_CTX
, "out of memory");
5384 image_close(&image
);
5387 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5388 for (i
= 0; i
< image
.num_sections
; i
++) {
5389 buffer
= malloc(image
.sections
[i
].size
);
5390 if (buffer
== NULL
) {
5391 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5392 (int)(image
.sections
[i
].size
));
5393 retval
= ERROR_FAIL
;
5397 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
5398 if (retval
!= ERROR_OK
) {
5403 uint32_t offset
= 0;
5404 uint32_t length
= buf_cnt
;
5406 /* DANGER!!! beware of unsigned comparision here!!! */
5408 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
5409 (image
.sections
[i
].base_address
< max_address
)) {
5410 if (image
.sections
[i
].base_address
< min_address
) {
5411 /* clip addresses below */
5412 offset
+= min_address
-image
.sections
[i
].base_address
;
5416 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5417 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5419 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5420 fastload
[i
].data
= malloc(length
);
5421 if (fastload
[i
].data
== NULL
) {
5423 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5425 retval
= ERROR_FAIL
;
5428 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5429 fastload
[i
].length
= length
;
5431 image_size
+= length
;
5432 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
5433 (unsigned int)length
,
5434 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
5440 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
5441 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
5442 "in %fs (%0.3f KiB/s)", image_size
,
5443 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
5445 command_print(CMD_CTX
,
5446 "WARNING: image has not been loaded to target!"
5447 "You can issue a 'fast_load' to finish loading.");
5450 image_close(&image
);
5452 if (retval
!= ERROR_OK
)
5458 COMMAND_HANDLER(handle_fast_load_command
)
5461 return ERROR_COMMAND_SYNTAX_ERROR
;
5462 if (fastload
== NULL
) {
5463 LOG_ERROR("No image in memory");
5467 int ms
= timeval_ms();
5469 int retval
= ERROR_OK
;
5470 for (i
= 0; i
< fastload_num
; i
++) {
5471 struct target
*target
= get_current_target(CMD_CTX
);
5472 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
5473 (unsigned int)(fastload
[i
].address
),
5474 (unsigned int)(fastload
[i
].length
));
5475 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5476 if (retval
!= ERROR_OK
)
5478 size
+= fastload
[i
].length
;
5480 if (retval
== ERROR_OK
) {
5481 int after
= timeval_ms();
5482 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5487 static const struct command_registration target_command_handlers
[] = {
5490 .handler
= handle_targets_command
,
5491 .mode
= COMMAND_ANY
,
5492 .help
= "change current default target (one parameter) "
5493 "or prints table of all targets (no parameters)",
5494 .usage
= "[target]",
5498 .mode
= COMMAND_CONFIG
,
5499 .help
= "configure target",
5501 .chain
= target_subcommand_handlers
,
5503 COMMAND_REGISTRATION_DONE
5506 int target_register_commands(struct command_context
*cmd_ctx
)
5508 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5511 static bool target_reset_nag
= true;
5513 bool get_target_reset_nag(void)
5515 return target_reset_nag
;
5518 COMMAND_HANDLER(handle_target_reset_nag
)
5520 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
5521 &target_reset_nag
, "Nag after each reset about options to improve "
5525 COMMAND_HANDLER(handle_ps_command
)
5527 struct target
*target
= get_current_target(CMD_CTX
);
5529 if (target
->state
!= TARGET_HALTED
) {
5530 LOG_INFO("target not halted !!");
5534 if ((target
->rtos
) && (target
->rtos
->type
)
5535 && (target
->rtos
->type
->ps_command
)) {
5536 display
= target
->rtos
->type
->ps_command(target
);
5537 command_print(CMD_CTX
, "%s", display
);
5542 return ERROR_TARGET_FAILURE
;
5546 static const struct command_registration target_exec_command_handlers
[] = {
5548 .name
= "fast_load_image",
5549 .handler
= handle_fast_load_image_command
,
5550 .mode
= COMMAND_ANY
,
5551 .help
= "Load image into server memory for later use by "
5552 "fast_load; primarily for profiling",
5553 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5554 "[min_address [max_length]]",
5557 .name
= "fast_load",
5558 .handler
= handle_fast_load_command
,
5559 .mode
= COMMAND_EXEC
,
5560 .help
= "loads active fast load image to current target "
5561 "- mainly for profiling purposes",
5566 .handler
= handle_profile_command
,
5567 .mode
= COMMAND_EXEC
,
5568 .usage
= "seconds filename",
5569 .help
= "profiling samples the CPU PC",
5571 /** @todo don't register virt2phys() unless target supports it */
5573 .name
= "virt2phys",
5574 .handler
= handle_virt2phys_command
,
5575 .mode
= COMMAND_ANY
,
5576 .help
= "translate a virtual address into a physical address",
5577 .usage
= "virtual_address",
5581 .handler
= handle_reg_command
,
5582 .mode
= COMMAND_EXEC
,
5583 .help
= "display or set a register; with no arguments, "
5584 "displays all registers and their values",
5585 .usage
= "[(register_name|register_number) [value]]",
5589 .handler
= handle_poll_command
,
5590 .mode
= COMMAND_EXEC
,
5591 .help
= "poll target state; or reconfigure background polling",
5592 .usage
= "['on'|'off']",
5595 .name
= "wait_halt",
5596 .handler
= handle_wait_halt_command
,
5597 .mode
= COMMAND_EXEC
,
5598 .help
= "wait up to the specified number of milliseconds "
5599 "(default 5000) for a previously requested halt",
5600 .usage
= "[milliseconds]",
5604 .handler
= handle_halt_command
,
5605 .mode
= COMMAND_EXEC
,
5606 .help
= "request target to halt, then wait up to the specified"
5607 "number of milliseconds (default 5000) for it to complete",
5608 .usage
= "[milliseconds]",
5612 .handler
= handle_resume_command
,
5613 .mode
= COMMAND_EXEC
,
5614 .help
= "resume target execution from current PC or address",
5615 .usage
= "[address]",
5619 .handler
= handle_reset_command
,
5620 .mode
= COMMAND_EXEC
,
5621 .usage
= "[run|halt|init]",
5622 .help
= "Reset all targets into the specified mode."
5623 "Default reset mode is run, if not given.",
5626 .name
= "soft_reset_halt",
5627 .handler
= handle_soft_reset_halt_command
,
5628 .mode
= COMMAND_EXEC
,
5630 .help
= "halt the target and do a soft reset",
5634 .handler
= handle_step_command
,
5635 .mode
= COMMAND_EXEC
,
5636 .help
= "step one instruction from current PC or address",
5637 .usage
= "[address]",
5641 .handler
= handle_md_command
,
5642 .mode
= COMMAND_EXEC
,
5643 .help
= "display memory words",
5644 .usage
= "['phys'] address [count]",
5648 .handler
= handle_md_command
,
5649 .mode
= COMMAND_EXEC
,
5650 .help
= "display memory half-words",
5651 .usage
= "['phys'] address [count]",
5655 .handler
= handle_md_command
,
5656 .mode
= COMMAND_EXEC
,
5657 .help
= "display memory bytes",
5658 .usage
= "['phys'] address [count]",
5662 .handler
= handle_mw_command
,
5663 .mode
= COMMAND_EXEC
,
5664 .help
= "write memory word",
5665 .usage
= "['phys'] address value [count]",
5669 .handler
= handle_mw_command
,
5670 .mode
= COMMAND_EXEC
,
5671 .help
= "write memory half-word",
5672 .usage
= "['phys'] address value [count]",
5676 .handler
= handle_mw_command
,
5677 .mode
= COMMAND_EXEC
,
5678 .help
= "write memory byte",
5679 .usage
= "['phys'] address value [count]",
5683 .handler
= handle_bp_command
,
5684 .mode
= COMMAND_EXEC
,
5685 .help
= "list or set hardware or software breakpoint",
5686 .usage
= "<address> [<asid>]<length> ['hw'|'hw_ctx']",
5690 .handler
= handle_rbp_command
,
5691 .mode
= COMMAND_EXEC
,
5692 .help
= "remove breakpoint",
5697 .handler
= handle_wp_command
,
5698 .mode
= COMMAND_EXEC
,
5699 .help
= "list (no params) or create watchpoints",
5700 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
5704 .handler
= handle_rwp_command
,
5705 .mode
= COMMAND_EXEC
,
5706 .help
= "remove watchpoint",
5710 .name
= "load_image",
5711 .handler
= handle_load_image_command
,
5712 .mode
= COMMAND_EXEC
,
5713 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5714 "[min_address] [max_length]",
5717 .name
= "dump_image",
5718 .handler
= handle_dump_image_command
,
5719 .mode
= COMMAND_EXEC
,
5720 .usage
= "filename address size",
5723 .name
= "verify_image",
5724 .handler
= handle_verify_image_command
,
5725 .mode
= COMMAND_EXEC
,
5726 .usage
= "filename [offset [type]]",
5729 .name
= "test_image",
5730 .handler
= handle_test_image_command
,
5731 .mode
= COMMAND_EXEC
,
5732 .usage
= "filename [offset [type]]",
5735 .name
= "mem2array",
5736 .mode
= COMMAND_EXEC
,
5737 .jim_handler
= jim_mem2array
,
5738 .help
= "read 8/16/32 bit memory and return as a TCL array "
5739 "for script processing",
5740 .usage
= "arrayname bitwidth address count",
5743 .name
= "array2mem",
5744 .mode
= COMMAND_EXEC
,
5745 .jim_handler
= jim_array2mem
,
5746 .help
= "convert a TCL array to memory locations "
5747 "and write the 8/16/32 bit values",
5748 .usage
= "arrayname bitwidth address count",
5751 .name
= "reset_nag",
5752 .handler
= handle_target_reset_nag
,
5753 .mode
= COMMAND_ANY
,
5754 .help
= "Nag after each reset about options that could have been "
5755 "enabled to improve performance. ",
5756 .usage
= "['enable'|'disable']",
5760 .handler
= handle_ps_command
,
5761 .mode
= COMMAND_EXEC
,
5762 .help
= "list all tasks ",
5766 COMMAND_REGISTRATION_DONE
5768 static int target_register_user_commands(struct command_context
*cmd_ctx
)
5770 int retval
= ERROR_OK
;
5771 retval
= target_request_register_commands(cmd_ctx
);
5772 if (retval
!= ERROR_OK
)
5775 retval
= trace_register_commands(cmd_ctx
);
5776 if (retval
!= ERROR_OK
)
5780 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);