1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
17 * Copyright (C) 2011 by Broadcom Corporation *
18 * Evan Hunter - ehunter@broadcom.com *
20 * Copyright (C) ST-Ericsson SA 2011 *
21 * michel.jaouen@stericsson.com : smp minimum support *
23 * Copyright (C) 2011 Andreas Fritiofson *
24 * andreas.fritiofson@gmail.com *
26 * This program is free software; you can redistribute it and/or modify *
27 * it under the terms of the GNU General Public License as published by *
28 * the Free Software Foundation; either version 2 of the License, or *
29 * (at your option) any later version. *
31 * This program is distributed in the hope that it will be useful, *
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
34 * GNU General Public License for more details. *
36 * You should have received a copy of the GNU General Public License *
37 * along with this program; if not, write to the *
38 * Free Software Foundation, Inc., *
39 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
40 ***************************************************************************/
46 #include <helper/time_support.h>
47 #include <jtag/jtag.h>
48 #include <flash/nor/core.h>
51 #include "target_type.h"
52 #include "target_request.h"
53 #include "breakpoints.h"
57 #include "rtos/rtos.h"
59 /* default halt wait timeout (ms) */
60 #define DEFAULT_HALT_TIMEOUT 5000
62 static int target_read_buffer_default(struct target
*target
, uint32_t address
,
63 uint32_t count
, uint8_t *buffer
);
64 static int target_write_buffer_default(struct target
*target
, uint32_t address
,
65 uint32_t count
, const uint8_t *buffer
);
66 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
67 int argc
, Jim_Obj
* const *argv
);
68 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
69 int argc
, Jim_Obj
* const *argv
);
70 static int target_register_user_commands(struct command_context
*cmd_ctx
);
71 static int target_get_gdb_fileio_info_default(struct target
*target
,
72 struct gdb_fileio_info
*fileio_info
);
73 static int target_gdb_fileio_end_default(struct target
*target
, int retcode
,
74 int fileio_errno
, bool ctrl_c
);
75 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
76 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
);
79 extern struct target_type arm7tdmi_target
;
80 extern struct target_type arm720t_target
;
81 extern struct target_type arm9tdmi_target
;
82 extern struct target_type arm920t_target
;
83 extern struct target_type arm966e_target
;
84 extern struct target_type arm946e_target
;
85 extern struct target_type arm926ejs_target
;
86 extern struct target_type fa526_target
;
87 extern struct target_type feroceon_target
;
88 extern struct target_type dragonite_target
;
89 extern struct target_type xscale_target
;
90 extern struct target_type cortexm_target
;
91 extern struct target_type cortexa8_target
;
92 extern struct target_type cortexr4_target
;
93 extern struct target_type arm11_target
;
94 extern struct target_type mips_m4k_target
;
95 extern struct target_type avr_target
;
96 extern struct target_type dsp563xx_target
;
97 extern struct target_type dsp5680xx_target
;
98 extern struct target_type testee_target
;
99 extern struct target_type avr32_ap7k_target
;
100 extern struct target_type hla_target
;
101 extern struct target_type nds32_v2_target
;
102 extern struct target_type nds32_v3_target
;
103 extern struct target_type nds32_v3m_target
;
104 extern struct target_type or1k_target
;
105 extern struct target_type quark_x10xx_target
;
107 static struct target_type
*target_types
[] = {
138 struct target
*all_targets
;
139 static struct target_event_callback
*target_event_callbacks
;
140 static struct target_timer_callback
*target_timer_callbacks
;
141 static const int polling_interval
= 100;
143 static const Jim_Nvp nvp_assert
[] = {
144 { .name
= "assert", NVP_ASSERT
},
145 { .name
= "deassert", NVP_DEASSERT
},
146 { .name
= "T", NVP_ASSERT
},
147 { .name
= "F", NVP_DEASSERT
},
148 { .name
= "t", NVP_ASSERT
},
149 { .name
= "f", NVP_DEASSERT
},
150 { .name
= NULL
, .value
= -1 }
153 static const Jim_Nvp nvp_error_target
[] = {
154 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
155 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
156 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
157 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
158 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
159 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
160 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
161 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
162 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
163 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
164 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
165 { .value
= -1, .name
= NULL
}
168 static const char *target_strerror_safe(int err
)
172 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
179 static const Jim_Nvp nvp_target_event
[] = {
181 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
182 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
183 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
184 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
185 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
187 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
188 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
190 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
191 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
192 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
193 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
194 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
195 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
196 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
197 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
198 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
199 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
200 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
201 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
203 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
204 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
206 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
207 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
209 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
210 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
212 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
213 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
215 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
216 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
218 { .name
= NULL
, .value
= -1 }
221 static const Jim_Nvp nvp_target_state
[] = {
222 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
223 { .name
= "running", .value
= TARGET_RUNNING
},
224 { .name
= "halted", .value
= TARGET_HALTED
},
225 { .name
= "reset", .value
= TARGET_RESET
},
226 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
227 { .name
= NULL
, .value
= -1 },
230 static const Jim_Nvp nvp_target_debug_reason
[] = {
231 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
232 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
233 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
234 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
235 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
236 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
237 { .name
= "program-exit" , .value
= DBG_REASON_EXIT
},
238 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
239 { .name
= NULL
, .value
= -1 },
242 static const Jim_Nvp nvp_target_endian
[] = {
243 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
244 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
245 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
246 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
247 { .name
= NULL
, .value
= -1 },
250 static const Jim_Nvp nvp_reset_modes
[] = {
251 { .name
= "unknown", .value
= RESET_UNKNOWN
},
252 { .name
= "run" , .value
= RESET_RUN
},
253 { .name
= "halt" , .value
= RESET_HALT
},
254 { .name
= "init" , .value
= RESET_INIT
},
255 { .name
= NULL
, .value
= -1 },
258 const char *debug_reason_name(struct target
*t
)
262 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
263 t
->debug_reason
)->name
;
265 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
266 cp
= "(*BUG*unknown*BUG*)";
271 const char *target_state_name(struct target
*t
)
274 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
276 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
277 cp
= "(*BUG*unknown*BUG*)";
282 /* determine the number of the new target */
283 static int new_target_number(void)
288 /* number is 0 based */
292 if (x
< t
->target_number
)
293 x
= t
->target_number
;
299 /* read a uint64_t from a buffer in target memory endianness */
300 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
302 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
303 return le_to_h_u64(buffer
);
305 return be_to_h_u64(buffer
);
308 /* read a uint32_t from a buffer in target memory endianness */
309 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
311 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
312 return le_to_h_u32(buffer
);
314 return be_to_h_u32(buffer
);
317 /* read a uint24_t from a buffer in target memory endianness */
318 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
320 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
321 return le_to_h_u24(buffer
);
323 return be_to_h_u24(buffer
);
326 /* read a uint16_t from a buffer in target memory endianness */
327 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
329 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
330 return le_to_h_u16(buffer
);
332 return be_to_h_u16(buffer
);
335 /* read a uint8_t from a buffer in target memory endianness */
336 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
338 return *buffer
& 0x0ff;
341 /* write a uint64_t to a buffer in target memory endianness */
342 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
344 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
345 h_u64_to_le(buffer
, value
);
347 h_u64_to_be(buffer
, value
);
350 /* write a uint32_t to a buffer in target memory endianness */
351 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
353 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
354 h_u32_to_le(buffer
, value
);
356 h_u32_to_be(buffer
, value
);
359 /* write a uint24_t to a buffer in target memory endianness */
360 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
362 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
363 h_u24_to_le(buffer
, value
);
365 h_u24_to_be(buffer
, value
);
368 /* write a uint16_t to a buffer in target memory endianness */
369 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
371 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
372 h_u16_to_le(buffer
, value
);
374 h_u16_to_be(buffer
, value
);
377 /* write a uint8_t to a buffer in target memory endianness */
378 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
383 /* write a uint64_t array to a buffer in target memory endianness */
384 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
387 for (i
= 0; i
< count
; i
++)
388 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
391 /* write a uint32_t array to a buffer in target memory endianness */
392 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
395 for (i
= 0; i
< count
; i
++)
396 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
399 /* write a uint16_t array to a buffer in target memory endianness */
400 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
403 for (i
= 0; i
< count
; i
++)
404 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
407 /* write a uint64_t array to a buffer in target memory endianness */
408 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
411 for (i
= 0; i
< count
; i
++)
412 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
415 /* write a uint32_t array to a buffer in target memory endianness */
416 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
419 for (i
= 0; i
< count
; i
++)
420 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
423 /* write a uint16_t array to a buffer in target memory endianness */
424 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
427 for (i
= 0; i
< count
; i
++)
428 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
431 /* return a pointer to a configured target; id is name or number */
432 struct target
*get_target(const char *id
)
434 struct target
*target
;
436 /* try as tcltarget name */
437 for (target
= all_targets
; target
; target
= target
->next
) {
438 if (target_name(target
) == NULL
)
440 if (strcmp(id
, target_name(target
)) == 0)
444 /* It's OK to remove this fallback sometime after August 2010 or so */
446 /* no match, try as number */
448 if (parse_uint(id
, &num
) != ERROR_OK
)
451 for (target
= all_targets
; target
; target
= target
->next
) {
452 if (target
->target_number
== (int)num
) {
453 LOG_WARNING("use '%s' as target identifier, not '%u'",
454 target_name(target
), num
);
462 /* returns a pointer to the n-th configured target */
463 static struct target
*get_target_by_num(int num
)
465 struct target
*target
= all_targets
;
468 if (target
->target_number
== num
)
470 target
= target
->next
;
476 struct target
*get_current_target(struct command_context
*cmd_ctx
)
478 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
480 if (target
== NULL
) {
481 LOG_ERROR("BUG: current_target out of bounds");
488 int target_poll(struct target
*target
)
492 /* We can't poll until after examine */
493 if (!target_was_examined(target
)) {
494 /* Fail silently lest we pollute the log */
498 retval
= target
->type
->poll(target
);
499 if (retval
!= ERROR_OK
)
502 if (target
->halt_issued
) {
503 if (target
->state
== TARGET_HALTED
)
504 target
->halt_issued
= false;
506 long long t
= timeval_ms() - target
->halt_issued_time
;
507 if (t
> DEFAULT_HALT_TIMEOUT
) {
508 target
->halt_issued
= false;
509 LOG_INFO("Halt timed out, wake up GDB.");
510 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
518 int target_halt(struct target
*target
)
521 /* We can't poll until after examine */
522 if (!target_was_examined(target
)) {
523 LOG_ERROR("Target not examined yet");
527 retval
= target
->type
->halt(target
);
528 if (retval
!= ERROR_OK
)
531 target
->halt_issued
= true;
532 target
->halt_issued_time
= timeval_ms();
538 * Make the target (re)start executing using its saved execution
539 * context (possibly with some modifications).
541 * @param target Which target should start executing.
542 * @param current True to use the target's saved program counter instead
543 * of the address parameter
544 * @param address Optionally used as the program counter.
545 * @param handle_breakpoints True iff breakpoints at the resumption PC
546 * should be skipped. (For example, maybe execution was stopped by
547 * such a breakpoint, in which case it would be counterprodutive to
549 * @param debug_execution False if all working areas allocated by OpenOCD
550 * should be released and/or restored to their original contents.
551 * (This would for example be true to run some downloaded "helper"
552 * algorithm code, which resides in one such working buffer and uses
553 * another for data storage.)
555 * @todo Resolve the ambiguity about what the "debug_execution" flag
556 * signifies. For example, Target implementations don't agree on how
557 * it relates to invalidation of the register cache, or to whether
558 * breakpoints and watchpoints should be enabled. (It would seem wrong
559 * to enable breakpoints when running downloaded "helper" algorithms
560 * (debug_execution true), since the breakpoints would be set to match
561 * target firmware being debugged, not the helper algorithm.... and
562 * enabling them could cause such helpers to malfunction (for example,
563 * by overwriting data with a breakpoint instruction. On the other
564 * hand the infrastructure for running such helpers might use this
565 * procedure but rely on hardware breakpoint to detect termination.)
567 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
571 /* We can't poll until after examine */
572 if (!target_was_examined(target
)) {
573 LOG_ERROR("Target not examined yet");
577 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
579 /* note that resume *must* be asynchronous. The CPU can halt before
580 * we poll. The CPU can even halt at the current PC as a result of
581 * a software breakpoint being inserted by (a bug?) the application.
583 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
584 if (retval
!= ERROR_OK
)
587 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
592 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
597 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
598 if (n
->name
== NULL
) {
599 LOG_ERROR("invalid reset mode");
603 /* disable polling during reset to make reset event scripts
604 * more predictable, i.e. dr/irscan & pathmove in events will
605 * not have JTAG operations injected into the middle of a sequence.
607 bool save_poll
= jtag_poll_get_enabled();
609 jtag_poll_set_enabled(false);
611 sprintf(buf
, "ocd_process_reset %s", n
->name
);
612 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
614 jtag_poll_set_enabled(save_poll
);
616 if (retval
!= JIM_OK
) {
617 Jim_MakeErrorMessage(cmd_ctx
->interp
);
618 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
622 /* We want any events to be processed before the prompt */
623 retval
= target_call_timer_callbacks_now();
625 struct target
*target
;
626 for (target
= all_targets
; target
; target
= target
->next
) {
627 target
->type
->check_reset(target
);
628 target
->running_alg
= false;
634 static int identity_virt2phys(struct target
*target
,
635 uint32_t virtual, uint32_t *physical
)
641 static int no_mmu(struct target
*target
, int *enabled
)
647 static int default_examine(struct target
*target
)
649 target_set_examined(target
);
653 /* no check by default */
654 static int default_check_reset(struct target
*target
)
659 int target_examine_one(struct target
*target
)
661 return target
->type
->examine(target
);
664 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
666 struct target
*target
= priv
;
668 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
671 jtag_unregister_event_callback(jtag_enable_callback
, target
);
673 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
675 int retval
= target_examine_one(target
);
676 if (retval
!= ERROR_OK
)
679 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
684 /* Targets that correctly implement init + examine, i.e.
685 * no communication with target during init:
689 int target_examine(void)
691 int retval
= ERROR_OK
;
692 struct target
*target
;
694 for (target
= all_targets
; target
; target
= target
->next
) {
695 /* defer examination, but don't skip it */
696 if (!target
->tap
->enabled
) {
697 jtag_register_event_callback(jtag_enable_callback
,
702 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
704 retval
= target_examine_one(target
);
705 if (retval
!= ERROR_OK
)
708 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
713 const char *target_type_name(struct target
*target
)
715 return target
->type
->name
;
718 static int target_soft_reset_halt(struct target
*target
)
720 if (!target_was_examined(target
)) {
721 LOG_ERROR("Target not examined yet");
724 if (!target
->type
->soft_reset_halt
) {
725 LOG_ERROR("Target %s does not support soft_reset_halt",
726 target_name(target
));
729 return target
->type
->soft_reset_halt(target
);
733 * Downloads a target-specific native code algorithm to the target,
734 * and executes it. * Note that some targets may need to set up, enable,
735 * and tear down a breakpoint (hard or * soft) to detect algorithm
736 * termination, while others may support lower overhead schemes where
737 * soft breakpoints embedded in the algorithm automatically terminate the
740 * @param target used to run the algorithm
741 * @param arch_info target-specific description of the algorithm.
743 int target_run_algorithm(struct target
*target
,
744 int num_mem_params
, struct mem_param
*mem_params
,
745 int num_reg_params
, struct reg_param
*reg_param
,
746 uint32_t entry_point
, uint32_t exit_point
,
747 int timeout_ms
, void *arch_info
)
749 int retval
= ERROR_FAIL
;
751 if (!target_was_examined(target
)) {
752 LOG_ERROR("Target not examined yet");
755 if (!target
->type
->run_algorithm
) {
756 LOG_ERROR("Target type '%s' does not support %s",
757 target_type_name(target
), __func__
);
761 target
->running_alg
= true;
762 retval
= target
->type
->run_algorithm(target
,
763 num_mem_params
, mem_params
,
764 num_reg_params
, reg_param
,
765 entry_point
, exit_point
, timeout_ms
, arch_info
);
766 target
->running_alg
= false;
773 * Downloads a target-specific native code algorithm to the target,
774 * executes and leaves it running.
776 * @param target used to run the algorithm
777 * @param arch_info target-specific description of the algorithm.
779 int target_start_algorithm(struct target
*target
,
780 int num_mem_params
, struct mem_param
*mem_params
,
781 int num_reg_params
, struct reg_param
*reg_params
,
782 uint32_t entry_point
, uint32_t exit_point
,
785 int retval
= ERROR_FAIL
;
787 if (!target_was_examined(target
)) {
788 LOG_ERROR("Target not examined yet");
791 if (!target
->type
->start_algorithm
) {
792 LOG_ERROR("Target type '%s' does not support %s",
793 target_type_name(target
), __func__
);
796 if (target
->running_alg
) {
797 LOG_ERROR("Target is already running an algorithm");
801 target
->running_alg
= true;
802 retval
= target
->type
->start_algorithm(target
,
803 num_mem_params
, mem_params
,
804 num_reg_params
, reg_params
,
805 entry_point
, exit_point
, arch_info
);
812 * Waits for an algorithm started with target_start_algorithm() to complete.
814 * @param target used to run the algorithm
815 * @param arch_info target-specific description of the algorithm.
817 int target_wait_algorithm(struct target
*target
,
818 int num_mem_params
, struct mem_param
*mem_params
,
819 int num_reg_params
, struct reg_param
*reg_params
,
820 uint32_t exit_point
, int timeout_ms
,
823 int retval
= ERROR_FAIL
;
825 if (!target
->type
->wait_algorithm
) {
826 LOG_ERROR("Target type '%s' does not support %s",
827 target_type_name(target
), __func__
);
830 if (!target
->running_alg
) {
831 LOG_ERROR("Target is not running an algorithm");
835 retval
= target
->type
->wait_algorithm(target
,
836 num_mem_params
, mem_params
,
837 num_reg_params
, reg_params
,
838 exit_point
, timeout_ms
, arch_info
);
839 if (retval
!= ERROR_TARGET_TIMEOUT
)
840 target
->running_alg
= false;
847 * Executes a target-specific native code algorithm in the target.
848 * It differs from target_run_algorithm in that the algorithm is asynchronous.
849 * Because of this it requires an compliant algorithm:
850 * see contrib/loaders/flash/stm32f1x.S for example.
852 * @param target used to run the algorithm
855 int target_run_flash_async_algorithm(struct target
*target
,
856 uint8_t *buffer
, uint32_t count
, int block_size
,
857 int num_mem_params
, struct mem_param
*mem_params
,
858 int num_reg_params
, struct reg_param
*reg_params
,
859 uint32_t buffer_start
, uint32_t buffer_size
,
860 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
865 /* Set up working area. First word is write pointer, second word is read pointer,
866 * rest is fifo data area. */
867 uint32_t wp_addr
= buffer_start
;
868 uint32_t rp_addr
= buffer_start
+ 4;
869 uint32_t fifo_start_addr
= buffer_start
+ 8;
870 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
872 uint32_t wp
= fifo_start_addr
;
873 uint32_t rp
= fifo_start_addr
;
875 /* validate block_size is 2^n */
876 assert(!block_size
|| !(block_size
& (block_size
- 1)));
878 retval
= target_write_u32(target
, wp_addr
, wp
);
879 if (retval
!= ERROR_OK
)
881 retval
= target_write_u32(target
, rp_addr
, rp
);
882 if (retval
!= ERROR_OK
)
885 /* Start up algorithm on target and let it idle while writing the first chunk */
886 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
887 num_reg_params
, reg_params
,
892 if (retval
!= ERROR_OK
) {
893 LOG_ERROR("error starting target flash write algorithm");
899 retval
= target_read_u32(target
, rp_addr
, &rp
);
900 if (retval
!= ERROR_OK
) {
901 LOG_ERROR("failed to get read pointer");
905 LOG_DEBUG("count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
, count
, wp
, rp
);
908 LOG_ERROR("flash write algorithm aborted by target");
909 retval
= ERROR_FLASH_OPERATION_FAILED
;
913 if ((rp
& (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
914 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
918 /* Count the number of bytes available in the fifo without
919 * crossing the wrap around. Make sure to not fill it completely,
920 * because that would make wp == rp and that's the empty condition. */
921 uint32_t thisrun_bytes
;
923 thisrun_bytes
= rp
- wp
- block_size
;
924 else if (rp
> fifo_start_addr
)
925 thisrun_bytes
= fifo_end_addr
- wp
;
927 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
929 if (thisrun_bytes
== 0) {
930 /* Throttle polling a bit if transfer is (much) faster than flash
931 * programming. The exact delay shouldn't matter as long as it's
932 * less than buffer size / flash speed. This is very unlikely to
933 * run when using high latency connections such as USB. */
936 /* to stop an infinite loop on some targets check and increment a timeout
937 * this issue was observed on a stellaris using the new ICDI interface */
938 if (timeout
++ >= 500) {
939 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
940 return ERROR_FLASH_OPERATION_FAILED
;
945 /* reset our timeout */
948 /* Limit to the amount of data we actually want to write */
949 if (thisrun_bytes
> count
* block_size
)
950 thisrun_bytes
= count
* block_size
;
952 /* Write data to fifo */
953 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
954 if (retval
!= ERROR_OK
)
957 /* Update counters and wrap write pointer */
958 buffer
+= thisrun_bytes
;
959 count
-= thisrun_bytes
/ block_size
;
961 if (wp
>= fifo_end_addr
)
962 wp
= fifo_start_addr
;
964 /* Store updated write pointer to target */
965 retval
= target_write_u32(target
, wp_addr
, wp
);
966 if (retval
!= ERROR_OK
)
970 if (retval
!= ERROR_OK
) {
971 /* abort flash write algorithm on target */
972 target_write_u32(target
, wp_addr
, 0);
975 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
976 num_reg_params
, reg_params
,
981 if (retval2
!= ERROR_OK
) {
982 LOG_ERROR("error waiting for target flash write algorithm");
989 int target_read_memory(struct target
*target
,
990 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
992 if (!target_was_examined(target
)) {
993 LOG_ERROR("Target not examined yet");
996 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
999 int target_read_phys_memory(struct target
*target
,
1000 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1002 if (!target_was_examined(target
)) {
1003 LOG_ERROR("Target not examined yet");
1006 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1009 int target_write_memory(struct target
*target
,
1010 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1012 if (!target_was_examined(target
)) {
1013 LOG_ERROR("Target not examined yet");
1016 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1019 int target_write_phys_memory(struct target
*target
,
1020 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1022 if (!target_was_examined(target
)) {
1023 LOG_ERROR("Target not examined yet");
1026 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1029 int target_add_breakpoint(struct target
*target
,
1030 struct breakpoint
*breakpoint
)
1032 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1033 LOG_WARNING("target %s is not halted", target_name(target
));
1034 return ERROR_TARGET_NOT_HALTED
;
1036 return target
->type
->add_breakpoint(target
, breakpoint
);
1039 int target_add_context_breakpoint(struct target
*target
,
1040 struct breakpoint
*breakpoint
)
1042 if (target
->state
!= TARGET_HALTED
) {
1043 LOG_WARNING("target %s is not halted", target_name(target
));
1044 return ERROR_TARGET_NOT_HALTED
;
1046 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1049 int target_add_hybrid_breakpoint(struct target
*target
,
1050 struct breakpoint
*breakpoint
)
1052 if (target
->state
!= TARGET_HALTED
) {
1053 LOG_WARNING("target %s is not halted", target_name(target
));
1054 return ERROR_TARGET_NOT_HALTED
;
1056 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1059 int target_remove_breakpoint(struct target
*target
,
1060 struct breakpoint
*breakpoint
)
1062 return target
->type
->remove_breakpoint(target
, breakpoint
);
1065 int target_add_watchpoint(struct target
*target
,
1066 struct watchpoint
*watchpoint
)
1068 if (target
->state
!= TARGET_HALTED
) {
1069 LOG_WARNING("target %s is not halted", target_name(target
));
1070 return ERROR_TARGET_NOT_HALTED
;
1072 return target
->type
->add_watchpoint(target
, watchpoint
);
1074 int target_remove_watchpoint(struct target
*target
,
1075 struct watchpoint
*watchpoint
)
1077 return target
->type
->remove_watchpoint(target
, watchpoint
);
1079 int target_hit_watchpoint(struct target
*target
,
1080 struct watchpoint
**hit_watchpoint
)
1082 if (target
->state
!= TARGET_HALTED
) {
1083 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1084 return ERROR_TARGET_NOT_HALTED
;
1087 if (target
->type
->hit_watchpoint
== NULL
) {
1088 /* For backward compatible, if hit_watchpoint is not implemented,
1089 * return ERROR_FAIL such that gdb_server will not take the nonsense
1094 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1097 int target_get_gdb_reg_list(struct target
*target
,
1098 struct reg
**reg_list
[], int *reg_list_size
,
1099 enum target_register_class reg_class
)
1101 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1103 int target_step(struct target
*target
,
1104 int current
, uint32_t address
, int handle_breakpoints
)
1106 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1109 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1111 if (target
->state
!= TARGET_HALTED
) {
1112 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1113 return ERROR_TARGET_NOT_HALTED
;
1115 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1118 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1120 if (target
->state
!= TARGET_HALTED
) {
1121 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1122 return ERROR_TARGET_NOT_HALTED
;
1124 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1127 int target_profiling(struct target
*target
, uint32_t *samples
,
1128 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1130 if (target
->state
!= TARGET_HALTED
) {
1131 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1132 return ERROR_TARGET_NOT_HALTED
;
1134 return target
->type
->profiling(target
, samples
, max_num_samples
,
1135 num_samples
, seconds
);
1139 * Reset the @c examined flag for the given target.
1140 * Pure paranoia -- targets are zeroed on allocation.
1142 static void target_reset_examined(struct target
*target
)
1144 target
->examined
= false;
1147 static int err_read_phys_memory(struct target
*target
, uint32_t address
,
1148 uint32_t size
, uint32_t count
, uint8_t *buffer
)
1150 LOG_ERROR("Not implemented: %s", __func__
);
1154 static int err_write_phys_memory(struct target
*target
, uint32_t address
,
1155 uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1157 LOG_ERROR("Not implemented: %s", __func__
);
1161 static int handle_target(void *priv
);
1163 static int target_init_one(struct command_context
*cmd_ctx
,
1164 struct target
*target
)
1166 target_reset_examined(target
);
1168 struct target_type
*type
= target
->type
;
1169 if (type
->examine
== NULL
)
1170 type
->examine
= default_examine
;
1172 if (type
->check_reset
== NULL
)
1173 type
->check_reset
= default_check_reset
;
1175 assert(type
->init_target
!= NULL
);
1177 int retval
= type
->init_target(cmd_ctx
, target
);
1178 if (ERROR_OK
!= retval
) {
1179 LOG_ERROR("target '%s' init failed", target_name(target
));
1183 /* Sanity-check MMU support ... stub in what we must, to help
1184 * implement it in stages, but warn if we need to do so.
1187 if (type
->write_phys_memory
== NULL
) {
1188 LOG_ERROR("type '%s' is missing write_phys_memory",
1190 type
->write_phys_memory
= err_write_phys_memory
;
1192 if (type
->read_phys_memory
== NULL
) {
1193 LOG_ERROR("type '%s' is missing read_phys_memory",
1195 type
->read_phys_memory
= err_read_phys_memory
;
1197 if (type
->virt2phys
== NULL
) {
1198 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1199 type
->virt2phys
= identity_virt2phys
;
1202 /* Make sure no-MMU targets all behave the same: make no
1203 * distinction between physical and virtual addresses, and
1204 * ensure that virt2phys() is always an identity mapping.
1206 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1207 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1210 type
->write_phys_memory
= type
->write_memory
;
1211 type
->read_phys_memory
= type
->read_memory
;
1212 type
->virt2phys
= identity_virt2phys
;
1215 if (target
->type
->read_buffer
== NULL
)
1216 target
->type
->read_buffer
= target_read_buffer_default
;
1218 if (target
->type
->write_buffer
== NULL
)
1219 target
->type
->write_buffer
= target_write_buffer_default
;
1221 if (target
->type
->get_gdb_fileio_info
== NULL
)
1222 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1224 if (target
->type
->gdb_fileio_end
== NULL
)
1225 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1227 if (target
->type
->profiling
== NULL
)
1228 target
->type
->profiling
= target_profiling_default
;
1233 static int target_init(struct command_context
*cmd_ctx
)
1235 struct target
*target
;
1238 for (target
= all_targets
; target
; target
= target
->next
) {
1239 retval
= target_init_one(cmd_ctx
, target
);
1240 if (ERROR_OK
!= retval
)
1247 retval
= target_register_user_commands(cmd_ctx
);
1248 if (ERROR_OK
!= retval
)
1251 retval
= target_register_timer_callback(&handle_target
,
1252 polling_interval
, 1, cmd_ctx
->interp
);
1253 if (ERROR_OK
!= retval
)
1259 COMMAND_HANDLER(handle_target_init_command
)
1264 return ERROR_COMMAND_SYNTAX_ERROR
;
1266 static bool target_initialized
;
1267 if (target_initialized
) {
1268 LOG_INFO("'target init' has already been called");
1271 target_initialized
= true;
1273 retval
= command_run_line(CMD_CTX
, "init_targets");
1274 if (ERROR_OK
!= retval
)
1277 retval
= command_run_line(CMD_CTX
, "init_board");
1278 if (ERROR_OK
!= retval
)
1281 LOG_DEBUG("Initializing targets...");
1282 return target_init(CMD_CTX
);
1285 int target_register_event_callback(int (*callback
)(struct target
*target
,
1286 enum target_event event
, void *priv
), void *priv
)
1288 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1290 if (callback
== NULL
)
1291 return ERROR_COMMAND_SYNTAX_ERROR
;
1294 while ((*callbacks_p
)->next
)
1295 callbacks_p
= &((*callbacks_p
)->next
);
1296 callbacks_p
= &((*callbacks_p
)->next
);
1299 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1300 (*callbacks_p
)->callback
= callback
;
1301 (*callbacks_p
)->priv
= priv
;
1302 (*callbacks_p
)->next
= NULL
;
1307 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1309 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1312 if (callback
== NULL
)
1313 return ERROR_COMMAND_SYNTAX_ERROR
;
1316 while ((*callbacks_p
)->next
)
1317 callbacks_p
= &((*callbacks_p
)->next
);
1318 callbacks_p
= &((*callbacks_p
)->next
);
1321 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1322 (*callbacks_p
)->callback
= callback
;
1323 (*callbacks_p
)->periodic
= periodic
;
1324 (*callbacks_p
)->time_ms
= time_ms
;
1326 gettimeofday(&now
, NULL
);
1327 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
1328 time_ms
-= (time_ms
% 1000);
1329 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
1330 if ((*callbacks_p
)->when
.tv_usec
> 1000000) {
1331 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
1332 (*callbacks_p
)->when
.tv_sec
+= 1;
1335 (*callbacks_p
)->priv
= priv
;
1336 (*callbacks_p
)->next
= NULL
;
1341 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1342 enum target_event event
, void *priv
), void *priv
)
1344 struct target_event_callback
**p
= &target_event_callbacks
;
1345 struct target_event_callback
*c
= target_event_callbacks
;
1347 if (callback
== NULL
)
1348 return ERROR_COMMAND_SYNTAX_ERROR
;
1351 struct target_event_callback
*next
= c
->next
;
1352 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1364 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1366 struct target_timer_callback
**p
= &target_timer_callbacks
;
1367 struct target_timer_callback
*c
= target_timer_callbacks
;
1369 if (callback
== NULL
)
1370 return ERROR_COMMAND_SYNTAX_ERROR
;
1373 struct target_timer_callback
*next
= c
->next
;
1374 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1386 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1388 struct target_event_callback
*callback
= target_event_callbacks
;
1389 struct target_event_callback
*next_callback
;
1391 if (event
== TARGET_EVENT_HALTED
) {
1392 /* execute early halted first */
1393 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1396 LOG_DEBUG("target event %i (%s)", event
,
1397 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1399 target_handle_event(target
, event
);
1402 next_callback
= callback
->next
;
1403 callback
->callback(target
, event
, callback
->priv
);
1404 callback
= next_callback
;
1410 static int target_timer_callback_periodic_restart(
1411 struct target_timer_callback
*cb
, struct timeval
*now
)
1413 int time_ms
= cb
->time_ms
;
1414 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1415 time_ms
-= (time_ms
% 1000);
1416 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1417 if (cb
->when
.tv_usec
> 1000000) {
1418 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1419 cb
->when
.tv_sec
+= 1;
1424 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1425 struct timeval
*now
)
1427 cb
->callback(cb
->priv
);
1430 return target_timer_callback_periodic_restart(cb
, now
);
1432 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1435 static int target_call_timer_callbacks_check_time(int checktime
)
1440 gettimeofday(&now
, NULL
);
1442 struct target_timer_callback
*callback
= target_timer_callbacks
;
1444 /* cleaning up may unregister and free this callback */
1445 struct target_timer_callback
*next_callback
= callback
->next
;
1447 bool call_it
= callback
->callback
&&
1448 ((!checktime
&& callback
->periodic
) ||
1449 now
.tv_sec
> callback
->when
.tv_sec
||
1450 (now
.tv_sec
== callback
->when
.tv_sec
&&
1451 now
.tv_usec
>= callback
->when
.tv_usec
));
1454 int retval
= target_call_timer_callback(callback
, &now
);
1455 if (retval
!= ERROR_OK
)
1459 callback
= next_callback
;
1465 int target_call_timer_callbacks(void)
1467 return target_call_timer_callbacks_check_time(1);
1470 /* invoke periodic callbacks immediately */
1471 int target_call_timer_callbacks_now(void)
1473 return target_call_timer_callbacks_check_time(0);
1476 /* Prints the working area layout for debug purposes */
1477 static void print_wa_layout(struct target
*target
)
1479 struct working_area
*c
= target
->working_areas
;
1482 LOG_DEBUG("%c%c 0x%08"PRIx32
"-0x%08"PRIx32
" (%"PRIu32
" bytes)",
1483 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1484 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1489 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1490 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1492 assert(area
->free
); /* Shouldn't split an allocated area */
1493 assert(size
<= area
->size
); /* Caller should guarantee this */
1495 /* Split only if not already the right size */
1496 if (size
< area
->size
) {
1497 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1502 new_wa
->next
= area
->next
;
1503 new_wa
->size
= area
->size
- size
;
1504 new_wa
->address
= area
->address
+ size
;
1505 new_wa
->backup
= NULL
;
1506 new_wa
->user
= NULL
;
1507 new_wa
->free
= true;
1509 area
->next
= new_wa
;
1512 /* If backup memory was allocated to this area, it has the wrong size
1513 * now so free it and it will be reallocated if/when needed */
1516 area
->backup
= NULL
;
1521 /* Merge all adjacent free areas into one */
1522 static void target_merge_working_areas(struct target
*target
)
1524 struct working_area
*c
= target
->working_areas
;
1526 while (c
&& c
->next
) {
1527 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1529 /* Find two adjacent free areas */
1530 if (c
->free
&& c
->next
->free
) {
1531 /* Merge the last into the first */
1532 c
->size
+= c
->next
->size
;
1534 /* Remove the last */
1535 struct working_area
*to_be_freed
= c
->next
;
1536 c
->next
= c
->next
->next
;
1537 if (to_be_freed
->backup
)
1538 free(to_be_freed
->backup
);
1541 /* If backup memory was allocated to the remaining area, it's has
1542 * the wrong size now */
1553 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1555 /* Reevaluate working area address based on MMU state*/
1556 if (target
->working_areas
== NULL
) {
1560 retval
= target
->type
->mmu(target
, &enabled
);
1561 if (retval
!= ERROR_OK
)
1565 if (target
->working_area_phys_spec
) {
1566 LOG_DEBUG("MMU disabled, using physical "
1567 "address for working memory 0x%08"PRIx32
,
1568 target
->working_area_phys
);
1569 target
->working_area
= target
->working_area_phys
;
1571 LOG_ERROR("No working memory available. "
1572 "Specify -work-area-phys to target.");
1573 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1576 if (target
->working_area_virt_spec
) {
1577 LOG_DEBUG("MMU enabled, using virtual "
1578 "address for working memory 0x%08"PRIx32
,
1579 target
->working_area_virt
);
1580 target
->working_area
= target
->working_area_virt
;
1582 LOG_ERROR("No working memory available. "
1583 "Specify -work-area-virt to target.");
1584 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1588 /* Set up initial working area on first call */
1589 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1591 new_wa
->next
= NULL
;
1592 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1593 new_wa
->address
= target
->working_area
;
1594 new_wa
->backup
= NULL
;
1595 new_wa
->user
= NULL
;
1596 new_wa
->free
= true;
1599 target
->working_areas
= new_wa
;
1602 /* only allocate multiples of 4 byte */
1604 size
= (size
+ 3) & (~3UL);
1606 struct working_area
*c
= target
->working_areas
;
1608 /* Find the first large enough working area */
1610 if (c
->free
&& c
->size
>= size
)
1616 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1618 /* Split the working area into the requested size */
1619 target_split_working_area(c
, size
);
1621 LOG_DEBUG("allocated new working area of %"PRIu32
" bytes at address 0x%08"PRIx32
, size
, c
->address
);
1623 if (target
->backup_working_area
) {
1624 if (c
->backup
== NULL
) {
1625 c
->backup
= malloc(c
->size
);
1626 if (c
->backup
== NULL
)
1630 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1631 if (retval
!= ERROR_OK
)
1635 /* mark as used, and return the new (reused) area */
1642 print_wa_layout(target
);
1647 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1651 retval
= target_alloc_working_area_try(target
, size
, area
);
1652 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1653 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1658 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1660 int retval
= ERROR_OK
;
1662 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1663 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1664 if (retval
!= ERROR_OK
)
1665 LOG_ERROR("failed to restore %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1666 area
->size
, area
->address
);
1672 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1673 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1675 int retval
= ERROR_OK
;
1681 retval
= target_restore_working_area(target
, area
);
1682 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1683 if (retval
!= ERROR_OK
)
1689 LOG_DEBUG("freed %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1690 area
->size
, area
->address
);
1692 /* mark user pointer invalid */
1693 /* TODO: Is this really safe? It points to some previous caller's memory.
1694 * How could we know that the area pointer is still in that place and not
1695 * some other vital data? What's the purpose of this, anyway? */
1699 target_merge_working_areas(target
);
1701 print_wa_layout(target
);
1706 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1708 return target_free_working_area_restore(target
, area
, 1);
1711 /* free resources and restore memory, if restoring memory fails,
1712 * free up resources anyway
1714 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1716 struct working_area
*c
= target
->working_areas
;
1718 LOG_DEBUG("freeing all working areas");
1720 /* Loop through all areas, restoring the allocated ones and marking them as free */
1724 target_restore_working_area(target
, c
);
1726 *c
->user
= NULL
; /* Same as above */
1732 /* Run a merge pass to combine all areas into one */
1733 target_merge_working_areas(target
);
1735 print_wa_layout(target
);
1738 void target_free_all_working_areas(struct target
*target
)
1740 target_free_all_working_areas_restore(target
, 1);
1743 /* Find the largest number of bytes that can be allocated */
1744 uint32_t target_get_working_area_avail(struct target
*target
)
1746 struct working_area
*c
= target
->working_areas
;
1747 uint32_t max_size
= 0;
1750 return target
->working_area_size
;
1753 if (c
->free
&& max_size
< c
->size
)
1762 int target_arch_state(struct target
*target
)
1765 if (target
== NULL
) {
1766 LOG_USER("No target has been configured");
1770 LOG_USER("target state: %s", target_state_name(target
));
1772 if (target
->state
!= TARGET_HALTED
)
1775 retval
= target
->type
->arch_state(target
);
1779 static int target_get_gdb_fileio_info_default(struct target
*target
,
1780 struct gdb_fileio_info
*fileio_info
)
1782 /* If target does not support semi-hosting function, target
1783 has no need to provide .get_gdb_fileio_info callback.
1784 It just return ERROR_FAIL and gdb_server will return "Txx"
1785 as target halted every time. */
1789 static int target_gdb_fileio_end_default(struct target
*target
,
1790 int retcode
, int fileio_errno
, bool ctrl_c
)
1795 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
1796 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1798 struct timeval timeout
, now
;
1800 gettimeofday(&timeout
, NULL
);
1801 timeval_add_time(&timeout
, seconds
, 0);
1803 LOG_INFO("Starting profiling. Halting and resuming the"
1804 " target as often as we can...");
1806 uint32_t sample_count
= 0;
1807 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
1808 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
1810 int retval
= ERROR_OK
;
1812 target_poll(target
);
1813 if (target
->state
== TARGET_HALTED
) {
1814 uint32_t t
= *((uint32_t *)reg
->value
);
1815 samples
[sample_count
++] = t
;
1816 /* current pc, addr = 0, do not handle breakpoints, not debugging */
1817 retval
= target_resume(target
, 1, 0, 0, 0);
1818 target_poll(target
);
1819 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
1820 } else if (target
->state
== TARGET_RUNNING
) {
1821 /* We want to quickly sample the PC. */
1822 retval
= target_halt(target
);
1824 LOG_INFO("Target not halted or running");
1829 if (retval
!= ERROR_OK
)
1832 gettimeofday(&now
, NULL
);
1833 if ((sample_count
>= max_num_samples
) ||
1834 ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
))) {
1835 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
1840 *num_samples
= sample_count
;
1844 /* Single aligned words are guaranteed to use 16 or 32 bit access
1845 * mode respectively, otherwise data is handled as quickly as
1848 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
1850 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1851 (int)size
, (unsigned)address
);
1853 if (!target_was_examined(target
)) {
1854 LOG_ERROR("Target not examined yet");
1861 if ((address
+ size
- 1) < address
) {
1862 /* GDB can request this when e.g. PC is 0xfffffffc*/
1863 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1869 return target
->type
->write_buffer(target
, address
, size
, buffer
);
1872 static int target_write_buffer_default(struct target
*target
, uint32_t address
, uint32_t count
, const uint8_t *buffer
)
1876 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
1877 * will have something to do with the size we leave to it. */
1878 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
1879 if (address
& size
) {
1880 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
1881 if (retval
!= ERROR_OK
)
1889 /* Write the data with as large access size as possible. */
1890 for (; size
> 0; size
/= 2) {
1891 uint32_t aligned
= count
- count
% size
;
1893 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
1894 if (retval
!= ERROR_OK
)
1905 /* Single aligned words are guaranteed to use 16 or 32 bit access
1906 * mode respectively, otherwise data is handled as quickly as
1909 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1911 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1912 (int)size
, (unsigned)address
);
1914 if (!target_was_examined(target
)) {
1915 LOG_ERROR("Target not examined yet");
1922 if ((address
+ size
- 1) < address
) {
1923 /* GDB can request this when e.g. PC is 0xfffffffc*/
1924 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1930 return target
->type
->read_buffer(target
, address
, size
, buffer
);
1933 static int target_read_buffer_default(struct target
*target
, uint32_t address
, uint32_t count
, uint8_t *buffer
)
1937 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
1938 * will have something to do with the size we leave to it. */
1939 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
1940 if (address
& size
) {
1941 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
1942 if (retval
!= ERROR_OK
)
1950 /* Read the data with as large access size as possible. */
1951 for (; size
> 0; size
/= 2) {
1952 uint32_t aligned
= count
- count
% size
;
1954 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
1955 if (retval
!= ERROR_OK
)
1966 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1971 uint32_t checksum
= 0;
1972 if (!target_was_examined(target
)) {
1973 LOG_ERROR("Target not examined yet");
1977 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
1978 if (retval
!= ERROR_OK
) {
1979 buffer
= malloc(size
);
1980 if (buffer
== NULL
) {
1981 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1982 return ERROR_COMMAND_SYNTAX_ERROR
;
1984 retval
= target_read_buffer(target
, address
, size
, buffer
);
1985 if (retval
!= ERROR_OK
) {
1990 /* convert to target endianness */
1991 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
1992 uint32_t target_data
;
1993 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1994 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1997 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2006 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
2009 if (!target_was_examined(target
)) {
2010 LOG_ERROR("Target not examined yet");
2014 if (target
->type
->blank_check_memory
== 0)
2015 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2017 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
2022 int target_read_u64(struct target
*target
, uint64_t address
, uint64_t *value
)
2024 uint8_t value_buf
[8];
2025 if (!target_was_examined(target
)) {
2026 LOG_ERROR("Target not examined yet");
2030 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2032 if (retval
== ERROR_OK
) {
2033 *value
= target_buffer_get_u64(target
, value_buf
);
2034 LOG_DEBUG("address: 0x%" PRIx64
", value: 0x%16.16" PRIx64
"",
2039 LOG_DEBUG("address: 0x%" PRIx64
" failed",
2046 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
2048 uint8_t value_buf
[4];
2049 if (!target_was_examined(target
)) {
2050 LOG_ERROR("Target not examined yet");
2054 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2056 if (retval
== ERROR_OK
) {
2057 *value
= target_buffer_get_u32(target
, value_buf
);
2058 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
2063 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2070 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
2072 uint8_t value_buf
[2];
2073 if (!target_was_examined(target
)) {
2074 LOG_ERROR("Target not examined yet");
2078 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2080 if (retval
== ERROR_OK
) {
2081 *value
= target_buffer_get_u16(target
, value_buf
);
2082 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
2087 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2094 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
2096 if (!target_was_examined(target
)) {
2097 LOG_ERROR("Target not examined yet");
2101 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2103 if (retval
== ERROR_OK
) {
2104 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
2109 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2116 int target_write_u64(struct target
*target
, uint64_t address
, uint64_t value
)
2119 uint8_t value_buf
[8];
2120 if (!target_was_examined(target
)) {
2121 LOG_ERROR("Target not examined yet");
2125 LOG_DEBUG("address: 0x%" PRIx64
", value: 0x%16.16" PRIx64
"",
2129 target_buffer_set_u64(target
, value_buf
, value
);
2130 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2131 if (retval
!= ERROR_OK
)
2132 LOG_DEBUG("failed: %i", retval
);
2137 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
2140 uint8_t value_buf
[4];
2141 if (!target_was_examined(target
)) {
2142 LOG_ERROR("Target not examined yet");
2146 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
2150 target_buffer_set_u32(target
, value_buf
, value
);
2151 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2152 if (retval
!= ERROR_OK
)
2153 LOG_DEBUG("failed: %i", retval
);
2158 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
2161 uint8_t value_buf
[2];
2162 if (!target_was_examined(target
)) {
2163 LOG_ERROR("Target not examined yet");
2167 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
2171 target_buffer_set_u16(target
, value_buf
, value
);
2172 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2173 if (retval
!= ERROR_OK
)
2174 LOG_DEBUG("failed: %i", retval
);
2179 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
2182 if (!target_was_examined(target
)) {
2183 LOG_ERROR("Target not examined yet");
2187 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
2190 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2191 if (retval
!= ERROR_OK
)
2192 LOG_DEBUG("failed: %i", retval
);
2197 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2199 struct target
*target
= get_target(name
);
2200 if (target
== NULL
) {
2201 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2204 if (!target
->tap
->enabled
) {
2205 LOG_USER("Target: TAP %s is disabled, "
2206 "can't be the current target\n",
2207 target
->tap
->dotted_name
);
2211 cmd_ctx
->current_target
= target
->target_number
;
2216 COMMAND_HANDLER(handle_targets_command
)
2218 int retval
= ERROR_OK
;
2219 if (CMD_ARGC
== 1) {
2220 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2221 if (retval
== ERROR_OK
) {
2227 struct target
*target
= all_targets
;
2228 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2229 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2234 if (target
->tap
->enabled
)
2235 state
= target_state_name(target
);
2237 state
= "tap-disabled";
2239 if (CMD_CTX
->current_target
== target
->target_number
)
2242 /* keep columns lined up to match the headers above */
2243 command_print(CMD_CTX
,
2244 "%2d%c %-18s %-10s %-6s %-18s %s",
2245 target
->target_number
,
2247 target_name(target
),
2248 target_type_name(target
),
2249 Jim_Nvp_value2name_simple(nvp_target_endian
,
2250 target
->endianness
)->name
,
2251 target
->tap
->dotted_name
,
2253 target
= target
->next
;
2259 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2261 static int powerDropout
;
2262 static int srstAsserted
;
2264 static int runPowerRestore
;
2265 static int runPowerDropout
;
2266 static int runSrstAsserted
;
2267 static int runSrstDeasserted
;
2269 static int sense_handler(void)
2271 static int prevSrstAsserted
;
2272 static int prevPowerdropout
;
2274 int retval
= jtag_power_dropout(&powerDropout
);
2275 if (retval
!= ERROR_OK
)
2279 powerRestored
= prevPowerdropout
&& !powerDropout
;
2281 runPowerRestore
= 1;
2283 long long current
= timeval_ms();
2284 static long long lastPower
;
2285 int waitMore
= lastPower
+ 2000 > current
;
2286 if (powerDropout
&& !waitMore
) {
2287 runPowerDropout
= 1;
2288 lastPower
= current
;
2291 retval
= jtag_srst_asserted(&srstAsserted
);
2292 if (retval
!= ERROR_OK
)
2296 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2298 static long long lastSrst
;
2299 waitMore
= lastSrst
+ 2000 > current
;
2300 if (srstDeasserted
&& !waitMore
) {
2301 runSrstDeasserted
= 1;
2305 if (!prevSrstAsserted
&& srstAsserted
)
2306 runSrstAsserted
= 1;
2308 prevSrstAsserted
= srstAsserted
;
2309 prevPowerdropout
= powerDropout
;
2311 if (srstDeasserted
|| powerRestored
) {
2312 /* Other than logging the event we can't do anything here.
2313 * Issuing a reset is a particularly bad idea as we might
2314 * be inside a reset already.
2321 /* process target state changes */
2322 static int handle_target(void *priv
)
2324 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2325 int retval
= ERROR_OK
;
2327 if (!is_jtag_poll_safe()) {
2328 /* polling is disabled currently */
2332 /* we do not want to recurse here... */
2333 static int recursive
;
2337 /* danger! running these procedures can trigger srst assertions and power dropouts.
2338 * We need to avoid an infinite loop/recursion here and we do that by
2339 * clearing the flags after running these events.
2341 int did_something
= 0;
2342 if (runSrstAsserted
) {
2343 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2344 Jim_Eval(interp
, "srst_asserted");
2347 if (runSrstDeasserted
) {
2348 Jim_Eval(interp
, "srst_deasserted");
2351 if (runPowerDropout
) {
2352 LOG_INFO("Power dropout detected, running power_dropout proc.");
2353 Jim_Eval(interp
, "power_dropout");
2356 if (runPowerRestore
) {
2357 Jim_Eval(interp
, "power_restore");
2361 if (did_something
) {
2362 /* clear detect flags */
2366 /* clear action flags */
2368 runSrstAsserted
= 0;
2369 runSrstDeasserted
= 0;
2370 runPowerRestore
= 0;
2371 runPowerDropout
= 0;
2376 /* Poll targets for state changes unless that's globally disabled.
2377 * Skip targets that are currently disabled.
2379 for (struct target
*target
= all_targets
;
2380 is_jtag_poll_safe() && target
;
2381 target
= target
->next
) {
2383 if (!target_was_examined(target
))
2386 if (!target
->tap
->enabled
)
2389 if (target
->backoff
.times
> target
->backoff
.count
) {
2390 /* do not poll this time as we failed previously */
2391 target
->backoff
.count
++;
2394 target
->backoff
.count
= 0;
2396 /* only poll target if we've got power and srst isn't asserted */
2397 if (!powerDropout
&& !srstAsserted
) {
2398 /* polling may fail silently until the target has been examined */
2399 retval
= target_poll(target
);
2400 if (retval
!= ERROR_OK
) {
2401 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2402 if (target
->backoff
.times
* polling_interval
< 5000) {
2403 target
->backoff
.times
*= 2;
2404 target
->backoff
.times
++;
2406 LOG_USER("Polling target %s failed, GDB will be halted. Polling again in %dms",
2407 target_name(target
),
2408 target
->backoff
.times
* polling_interval
);
2410 /* Tell GDB to halt the debugger. This allows the user to
2411 * run monitor commands to handle the situation.
2413 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2416 /* Since we succeeded, we reset backoff count */
2417 if (target
->backoff
.times
> 0)
2418 LOG_USER("Polling target %s succeeded again", target_name(target
));
2419 target
->backoff
.times
= 0;
2426 COMMAND_HANDLER(handle_reg_command
)
2428 struct target
*target
;
2429 struct reg
*reg
= NULL
;
2435 target
= get_current_target(CMD_CTX
);
2437 /* list all available registers for the current target */
2438 if (CMD_ARGC
== 0) {
2439 struct reg_cache
*cache
= target
->reg_cache
;
2445 command_print(CMD_CTX
, "===== %s", cache
->name
);
2447 for (i
= 0, reg
= cache
->reg_list
;
2448 i
< cache
->num_regs
;
2449 i
++, reg
++, count
++) {
2450 /* only print cached values if they are valid */
2452 value
= buf_to_str(reg
->value
,
2454 command_print(CMD_CTX
,
2455 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2463 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2468 cache
= cache
->next
;
2474 /* access a single register by its ordinal number */
2475 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2477 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2479 struct reg_cache
*cache
= target
->reg_cache
;
2483 for (i
= 0; i
< cache
->num_regs
; i
++) {
2484 if (count
++ == num
) {
2485 reg
= &cache
->reg_list
[i
];
2491 cache
= cache
->next
;
2495 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2496 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2500 /* access a single register by its name */
2501 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2504 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2509 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2511 /* display a register */
2512 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2513 && (CMD_ARGV
[1][0] <= '9')))) {
2514 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2517 if (reg
->valid
== 0)
2518 reg
->type
->get(reg
);
2519 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2520 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2525 /* set register value */
2526 if (CMD_ARGC
== 2) {
2527 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2530 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2532 reg
->type
->set(reg
, buf
);
2534 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2535 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2543 return ERROR_COMMAND_SYNTAX_ERROR
;
2546 COMMAND_HANDLER(handle_poll_command
)
2548 int retval
= ERROR_OK
;
2549 struct target
*target
= get_current_target(CMD_CTX
);
2551 if (CMD_ARGC
== 0) {
2552 command_print(CMD_CTX
, "background polling: %s",
2553 jtag_poll_get_enabled() ? "on" : "off");
2554 command_print(CMD_CTX
, "TAP: %s (%s)",
2555 target
->tap
->dotted_name
,
2556 target
->tap
->enabled
? "enabled" : "disabled");
2557 if (!target
->tap
->enabled
)
2559 retval
= target_poll(target
);
2560 if (retval
!= ERROR_OK
)
2562 retval
= target_arch_state(target
);
2563 if (retval
!= ERROR_OK
)
2565 } else if (CMD_ARGC
== 1) {
2567 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2568 jtag_poll_set_enabled(enable
);
2570 return ERROR_COMMAND_SYNTAX_ERROR
;
2575 COMMAND_HANDLER(handle_wait_halt_command
)
2578 return ERROR_COMMAND_SYNTAX_ERROR
;
2580 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2581 if (1 == CMD_ARGC
) {
2582 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2583 if (ERROR_OK
!= retval
)
2584 return ERROR_COMMAND_SYNTAX_ERROR
;
2587 struct target
*target
= get_current_target(CMD_CTX
);
2588 return target_wait_state(target
, TARGET_HALTED
, ms
);
2591 /* wait for target state to change. The trick here is to have a low
2592 * latency for short waits and not to suck up all the CPU time
2595 * After 500ms, keep_alive() is invoked
2597 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2600 long long then
= 0, cur
;
2604 retval
= target_poll(target
);
2605 if (retval
!= ERROR_OK
)
2607 if (target
->state
== state
)
2612 then
= timeval_ms();
2613 LOG_DEBUG("waiting for target %s...",
2614 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2620 if ((cur
-then
) > ms
) {
2621 LOG_ERROR("timed out while waiting for target %s",
2622 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2630 COMMAND_HANDLER(handle_halt_command
)
2634 struct target
*target
= get_current_target(CMD_CTX
);
2635 int retval
= target_halt(target
);
2636 if (ERROR_OK
!= retval
)
2639 if (CMD_ARGC
== 1) {
2640 unsigned wait_local
;
2641 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2642 if (ERROR_OK
!= retval
)
2643 return ERROR_COMMAND_SYNTAX_ERROR
;
2648 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2651 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2653 struct target
*target
= get_current_target(CMD_CTX
);
2655 LOG_USER("requesting target halt and executing a soft reset");
2657 target_soft_reset_halt(target
);
2662 COMMAND_HANDLER(handle_reset_command
)
2665 return ERROR_COMMAND_SYNTAX_ERROR
;
2667 enum target_reset_mode reset_mode
= RESET_RUN
;
2668 if (CMD_ARGC
== 1) {
2670 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2671 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
2672 return ERROR_COMMAND_SYNTAX_ERROR
;
2673 reset_mode
= n
->value
;
2676 /* reset *all* targets */
2677 return target_process_reset(CMD_CTX
, reset_mode
);
2681 COMMAND_HANDLER(handle_resume_command
)
2685 return ERROR_COMMAND_SYNTAX_ERROR
;
2687 struct target
*target
= get_current_target(CMD_CTX
);
2689 /* with no CMD_ARGV, resume from current pc, addr = 0,
2690 * with one arguments, addr = CMD_ARGV[0],
2691 * handle breakpoints, not debugging */
2693 if (CMD_ARGC
== 1) {
2694 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2698 return target_resume(target
, current
, addr
, 1, 0);
2701 COMMAND_HANDLER(handle_step_command
)
2704 return ERROR_COMMAND_SYNTAX_ERROR
;
2708 /* with no CMD_ARGV, step from current pc, addr = 0,
2709 * with one argument addr = CMD_ARGV[0],
2710 * handle breakpoints, debugging */
2713 if (CMD_ARGC
== 1) {
2714 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2718 struct target
*target
= get_current_target(CMD_CTX
);
2720 return target
->type
->step(target
, current_pc
, addr
, 1);
2723 static void handle_md_output(struct command_context
*cmd_ctx
,
2724 struct target
*target
, uint32_t address
, unsigned size
,
2725 unsigned count
, const uint8_t *buffer
)
2727 const unsigned line_bytecnt
= 32;
2728 unsigned line_modulo
= line_bytecnt
/ size
;
2730 char output
[line_bytecnt
* 4 + 1];
2731 unsigned output_len
= 0;
2733 const char *value_fmt
;
2736 value_fmt
= "%8.8x ";
2739 value_fmt
= "%4.4x ";
2742 value_fmt
= "%2.2x ";
2745 /* "can't happen", caller checked */
2746 LOG_ERROR("invalid memory read size: %u", size
);
2750 for (unsigned i
= 0; i
< count
; i
++) {
2751 if (i
% line_modulo
== 0) {
2752 output_len
+= snprintf(output
+ output_len
,
2753 sizeof(output
) - output_len
,
2755 (unsigned)(address
+ (i
*size
)));
2759 const uint8_t *value_ptr
= buffer
+ i
* size
;
2762 value
= target_buffer_get_u32(target
, value_ptr
);
2765 value
= target_buffer_get_u16(target
, value_ptr
);
2770 output_len
+= snprintf(output
+ output_len
,
2771 sizeof(output
) - output_len
,
2774 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
2775 command_print(cmd_ctx
, "%s", output
);
2781 COMMAND_HANDLER(handle_md_command
)
2784 return ERROR_COMMAND_SYNTAX_ERROR
;
2787 switch (CMD_NAME
[2]) {
2798 return ERROR_COMMAND_SYNTAX_ERROR
;
2801 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2802 int (*fn
)(struct target
*target
,
2803 uint32_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
2807 fn
= target_read_phys_memory
;
2809 fn
= target_read_memory
;
2810 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2811 return ERROR_COMMAND_SYNTAX_ERROR
;
2814 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2818 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2820 uint8_t *buffer
= calloc(count
, size
);
2822 struct target
*target
= get_current_target(CMD_CTX
);
2823 int retval
= fn(target
, address
, size
, count
, buffer
);
2824 if (ERROR_OK
== retval
)
2825 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2832 typedef int (*target_write_fn
)(struct target
*target
,
2833 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
2835 static int target_fill_mem(struct target
*target
,
2844 /* We have to write in reasonably large chunks to be able
2845 * to fill large memory areas with any sane speed */
2846 const unsigned chunk_size
= 16384;
2847 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
2848 if (target_buf
== NULL
) {
2849 LOG_ERROR("Out of memory");
2853 for (unsigned i
= 0; i
< chunk_size
; i
++) {
2854 switch (data_size
) {
2856 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
2859 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
2862 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
2869 int retval
= ERROR_OK
;
2871 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
2874 if (current
> chunk_size
)
2875 current
= chunk_size
;
2876 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
2877 if (retval
!= ERROR_OK
)
2879 /* avoid GDB timeouts */
2888 COMMAND_HANDLER(handle_mw_command
)
2891 return ERROR_COMMAND_SYNTAX_ERROR
;
2892 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2897 fn
= target_write_phys_memory
;
2899 fn
= target_write_memory
;
2900 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2901 return ERROR_COMMAND_SYNTAX_ERROR
;
2904 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2907 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2911 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2913 struct target
*target
= get_current_target(CMD_CTX
);
2915 switch (CMD_NAME
[2]) {
2926 return ERROR_COMMAND_SYNTAX_ERROR
;
2929 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
2932 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2933 uint32_t *min_address
, uint32_t *max_address
)
2935 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2936 return ERROR_COMMAND_SYNTAX_ERROR
;
2938 /* a base address isn't always necessary,
2939 * default to 0x0 (i.e. don't relocate) */
2940 if (CMD_ARGC
>= 2) {
2942 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2943 image
->base_address
= addr
;
2944 image
->base_address_set
= 1;
2946 image
->base_address_set
= 0;
2948 image
->start_address_set
= 0;
2951 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2952 if (CMD_ARGC
== 5) {
2953 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2954 /* use size (given) to find max (required) */
2955 *max_address
+= *min_address
;
2958 if (*min_address
> *max_address
)
2959 return ERROR_COMMAND_SYNTAX_ERROR
;
2964 COMMAND_HANDLER(handle_load_image_command
)
2968 uint32_t image_size
;
2969 uint32_t min_address
= 0;
2970 uint32_t max_address
= 0xffffffff;
2974 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2975 &image
, &min_address
, &max_address
);
2976 if (ERROR_OK
!= retval
)
2979 struct target
*target
= get_current_target(CMD_CTX
);
2981 struct duration bench
;
2982 duration_start(&bench
);
2984 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2989 for (i
= 0; i
< image
.num_sections
; i
++) {
2990 buffer
= malloc(image
.sections
[i
].size
);
2991 if (buffer
== NULL
) {
2992 command_print(CMD_CTX
,
2993 "error allocating buffer for section (%d bytes)",
2994 (int)(image
.sections
[i
].size
));
2998 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
2999 if (retval
!= ERROR_OK
) {
3004 uint32_t offset
= 0;
3005 uint32_t length
= buf_cnt
;
3007 /* DANGER!!! beware of unsigned comparision here!!! */
3009 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3010 (image
.sections
[i
].base_address
< max_address
)) {
3012 if (image
.sections
[i
].base_address
< min_address
) {
3013 /* clip addresses below */
3014 offset
+= min_address
-image
.sections
[i
].base_address
;
3018 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3019 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3021 retval
= target_write_buffer(target
,
3022 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3023 if (retval
!= ERROR_OK
) {
3027 image_size
+= length
;
3028 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
3029 (unsigned int)length
,
3030 image
.sections
[i
].base_address
+ offset
);
3036 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3037 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
3038 "in %fs (%0.3f KiB/s)", image_size
,
3039 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3042 image_close(&image
);
3048 COMMAND_HANDLER(handle_dump_image_command
)
3050 struct fileio fileio
;
3052 int retval
, retvaltemp
;
3053 uint32_t address
, size
;
3054 struct duration bench
;
3055 struct target
*target
= get_current_target(CMD_CTX
);
3058 return ERROR_COMMAND_SYNTAX_ERROR
;
3060 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
3061 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
3063 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
3064 buffer
= malloc(buf_size
);
3068 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3069 if (retval
!= ERROR_OK
) {
3074 duration_start(&bench
);
3077 size_t size_written
;
3078 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
3079 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
3080 if (retval
!= ERROR_OK
)
3083 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
3084 if (retval
!= ERROR_OK
)
3087 size
-= this_run_size
;
3088 address
+= this_run_size
;
3093 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3095 retval
= fileio_size(&fileio
, &filesize
);
3096 if (retval
!= ERROR_OK
)
3098 command_print(CMD_CTX
,
3099 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize
,
3100 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3103 retvaltemp
= fileio_close(&fileio
);
3104 if (retvaltemp
!= ERROR_OK
)
3110 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
3114 uint32_t image_size
;
3117 uint32_t checksum
= 0;
3118 uint32_t mem_checksum
= 0;
3122 struct target
*target
= get_current_target(CMD_CTX
);
3125 return ERROR_COMMAND_SYNTAX_ERROR
;
3128 LOG_ERROR("no target selected");
3132 struct duration bench
;
3133 duration_start(&bench
);
3135 if (CMD_ARGC
>= 2) {
3137 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
3138 image
.base_address
= addr
;
3139 image
.base_address_set
= 1;
3141 image
.base_address_set
= 0;
3142 image
.base_address
= 0x0;
3145 image
.start_address_set
= 0;
3147 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3148 if (retval
!= ERROR_OK
)
3154 for (i
= 0; i
< image
.num_sections
; i
++) {
3155 buffer
= malloc(image
.sections
[i
].size
);
3156 if (buffer
== NULL
) {
3157 command_print(CMD_CTX
,
3158 "error allocating buffer for section (%d bytes)",
3159 (int)(image
.sections
[i
].size
));
3162 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3163 if (retval
!= ERROR_OK
) {
3169 /* calculate checksum of image */
3170 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3171 if (retval
!= ERROR_OK
) {
3176 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3177 if (retval
!= ERROR_OK
) {
3182 if (checksum
!= mem_checksum
) {
3183 /* failed crc checksum, fall back to a binary compare */
3187 LOG_ERROR("checksum mismatch - attempting binary compare");
3189 data
= (uint8_t *)malloc(buf_cnt
);
3191 /* Can we use 32bit word accesses? */
3193 int count
= buf_cnt
;
3194 if ((count
% 4) == 0) {
3198 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
3199 if (retval
== ERROR_OK
) {
3201 for (t
= 0; t
< buf_cnt
; t
++) {
3202 if (data
[t
] != buffer
[t
]) {
3203 command_print(CMD_CTX
,
3204 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3206 (unsigned)(t
+ image
.sections
[i
].base_address
),
3209 if (diffs
++ >= 127) {
3210 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
3222 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
3223 image
.sections
[i
].base_address
,
3228 image_size
+= buf_cnt
;
3231 command_print(CMD_CTX
, "No more differences found.");
3234 retval
= ERROR_FAIL
;
3235 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3236 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3237 "in %fs (%0.3f KiB/s)", image_size
,
3238 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3241 image_close(&image
);
3246 COMMAND_HANDLER(handle_verify_image_command
)
3248 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
3251 COMMAND_HANDLER(handle_test_image_command
)
3253 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
3256 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
3258 struct target
*target
= get_current_target(cmd_ctx
);
3259 struct breakpoint
*breakpoint
= target
->breakpoints
;
3260 while (breakpoint
) {
3261 if (breakpoint
->type
== BKPT_SOFT
) {
3262 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3263 breakpoint
->length
, 16);
3264 command_print(cmd_ctx
, "IVA breakpoint: 0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
3265 breakpoint
->address
,
3267 breakpoint
->set
, buf
);
3270 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3271 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3273 breakpoint
->length
, breakpoint
->set
);
3274 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3275 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3276 breakpoint
->address
,
3277 breakpoint
->length
, breakpoint
->set
);
3278 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3281 command_print(cmd_ctx
, "Breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3282 breakpoint
->address
,
3283 breakpoint
->length
, breakpoint
->set
);
3286 breakpoint
= breakpoint
->next
;
3291 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
3292 uint32_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3294 struct target
*target
= get_current_target(cmd_ctx
);
3297 int retval
= breakpoint_add(target
, addr
, length
, hw
);
3298 if (ERROR_OK
== retval
)
3299 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
3301 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3304 } else if (addr
== 0) {
3305 int retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3306 if (ERROR_OK
== retval
)
3307 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3309 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3313 int retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3314 if (ERROR_OK
== retval
)
3315 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3317 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3324 COMMAND_HANDLER(handle_bp_command
)
3333 return handle_bp_command_list(CMD_CTX
);
3337 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3338 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3339 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3342 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
3344 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3346 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3349 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3350 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
3352 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
3353 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3355 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3360 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3361 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
3362 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
3363 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3366 return ERROR_COMMAND_SYNTAX_ERROR
;
3370 COMMAND_HANDLER(handle_rbp_command
)
3373 return ERROR_COMMAND_SYNTAX_ERROR
;
3376 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3378 struct target
*target
= get_current_target(CMD_CTX
);
3379 breakpoint_remove(target
, addr
);
3384 COMMAND_HANDLER(handle_wp_command
)
3386 struct target
*target
= get_current_target(CMD_CTX
);
3388 if (CMD_ARGC
== 0) {
3389 struct watchpoint
*watchpoint
= target
->watchpoints
;
3391 while (watchpoint
) {
3392 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
3393 ", len: 0x%8.8" PRIx32
3394 ", r/w/a: %i, value: 0x%8.8" PRIx32
3395 ", mask: 0x%8.8" PRIx32
,
3396 watchpoint
->address
,
3398 (int)watchpoint
->rw
,
3401 watchpoint
= watchpoint
->next
;
3406 enum watchpoint_rw type
= WPT_ACCESS
;
3408 uint32_t length
= 0;
3409 uint32_t data_value
= 0x0;
3410 uint32_t data_mask
= 0xffffffff;
3414 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
3417 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3420 switch (CMD_ARGV
[2][0]) {
3431 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3432 return ERROR_COMMAND_SYNTAX_ERROR
;
3436 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3437 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3441 return ERROR_COMMAND_SYNTAX_ERROR
;
3444 int retval
= watchpoint_add(target
, addr
, length
, type
,
3445 data_value
, data_mask
);
3446 if (ERROR_OK
!= retval
)
3447 LOG_ERROR("Failure setting watchpoints");
3452 COMMAND_HANDLER(handle_rwp_command
)
3455 return ERROR_COMMAND_SYNTAX_ERROR
;
3458 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3460 struct target
*target
= get_current_target(CMD_CTX
);
3461 watchpoint_remove(target
, addr
);
3467 * Translate a virtual address to a physical address.
3469 * The low-level target implementation must have logged a detailed error
3470 * which is forwarded to telnet/GDB session.
3472 COMMAND_HANDLER(handle_virt2phys_command
)
3475 return ERROR_COMMAND_SYNTAX_ERROR
;
3478 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
3481 struct target
*target
= get_current_target(CMD_CTX
);
3482 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3483 if (retval
== ERROR_OK
)
3484 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
3489 static void writeData(FILE *f
, const void *data
, size_t len
)
3491 size_t written
= fwrite(data
, 1, len
, f
);
3493 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3496 static void writeLong(FILE *f
, int l
)
3499 for (i
= 0; i
< 4; i
++) {
3500 char c
= (l
>> (i
*8))&0xff;
3501 writeData(f
, &c
, 1);
3506 static void writeString(FILE *f
, char *s
)
3508 writeData(f
, s
, strlen(s
));
3511 typedef unsigned char UNIT
[2]; /* unit of profiling */
3513 /* Dump a gmon.out histogram file. */
3514 static void write_gmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
,
3515 bool with_range
, uint32_t start_address
, uint32_t end_address
)
3518 FILE *f
= fopen(filename
, "w");
3521 writeString(f
, "gmon");
3522 writeLong(f
, 0x00000001); /* Version */
3523 writeLong(f
, 0); /* padding */
3524 writeLong(f
, 0); /* padding */
3525 writeLong(f
, 0); /* padding */
3527 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3528 writeData(f
, &zero
, 1);
3530 /* figure out bucket size */
3534 min
= start_address
;
3539 for (i
= 0; i
< sampleNum
; i
++) {
3540 if (min
> samples
[i
])
3542 if (max
< samples
[i
])
3546 /* max should be (largest sample + 1)
3547 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
3551 int addressSpace
= max
- min
;
3552 assert(addressSpace
>= 2);
3554 /* FIXME: What is the reasonable number of buckets?
3555 * The profiling result will be more accurate if there are enough buckets. */
3556 static const uint32_t maxBuckets
= 128 * 1024; /* maximum buckets. */
3557 uint32_t numBuckets
= addressSpace
/ sizeof(UNIT
);
3558 if (numBuckets
> maxBuckets
)
3559 numBuckets
= maxBuckets
;
3560 int *buckets
= malloc(sizeof(int) * numBuckets
);
3561 if (buckets
== NULL
) {
3565 memset(buckets
, 0, sizeof(int) * numBuckets
);
3566 for (i
= 0; i
< sampleNum
; i
++) {
3567 uint32_t address
= samples
[i
];
3569 if ((address
< min
) || (max
<= address
))
3572 long long a
= address
- min
;
3573 long long b
= numBuckets
;
3574 long long c
= addressSpace
;
3575 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
3579 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3580 writeLong(f
, min
); /* low_pc */
3581 writeLong(f
, max
); /* high_pc */
3582 writeLong(f
, numBuckets
); /* # of buckets */
3583 writeLong(f
, 100); /* KLUDGE! We lie, ca. 100Hz best case. */
3584 writeString(f
, "seconds");
3585 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3586 writeData(f
, &zero
, 1);
3587 writeString(f
, "s");
3589 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3591 char *data
= malloc(2 * numBuckets
);
3593 for (i
= 0; i
< numBuckets
; i
++) {
3598 data
[i
* 2] = val
&0xff;
3599 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
3602 writeData(f
, data
, numBuckets
* 2);
3610 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3611 * which will be used as a random sampling of PC */
3612 COMMAND_HANDLER(handle_profile_command
)
3614 struct target
*target
= get_current_target(CMD_CTX
);
3616 if ((CMD_ARGC
!= 2) && (CMD_ARGC
!= 4))
3617 return ERROR_COMMAND_SYNTAX_ERROR
;
3619 const uint32_t MAX_PROFILE_SAMPLE_NUM
= 10000;
3621 uint32_t num_of_sampels
;
3622 int retval
= ERROR_OK
;
3623 uint32_t *samples
= malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM
);
3624 if (samples
== NULL
) {
3625 LOG_ERROR("No memory to store samples.");
3629 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], offset
);
3632 * Some cores let us sample the PC without the
3633 * annoying halt/resume step; for example, ARMv7 PCSR.
3634 * Provide a way to use that more efficient mechanism.
3636 retval
= target_profiling(target
, samples
, MAX_PROFILE_SAMPLE_NUM
,
3637 &num_of_sampels
, offset
);
3638 if (retval
!= ERROR_OK
) {
3643 assert(num_of_sampels
<= MAX_PROFILE_SAMPLE_NUM
);
3645 retval
= target_poll(target
);
3646 if (retval
!= ERROR_OK
) {
3650 if (target
->state
== TARGET_RUNNING
) {
3651 retval
= target_halt(target
);
3652 if (retval
!= ERROR_OK
) {
3658 retval
= target_poll(target
);
3659 if (retval
!= ERROR_OK
) {
3664 uint32_t start_address
= 0;
3665 uint32_t end_address
= 0;
3666 bool with_range
= false;
3667 if (CMD_ARGC
== 4) {
3669 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], start_address
);
3670 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], end_address
);
3673 write_gmon(samples
, num_of_sampels
, CMD_ARGV
[1],
3674 with_range
, start_address
, end_address
);
3675 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3681 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
3684 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3687 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3691 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3692 valObjPtr
= Jim_NewIntObj(interp
, val
);
3693 if (!nameObjPtr
|| !valObjPtr
) {
3698 Jim_IncrRefCount(nameObjPtr
);
3699 Jim_IncrRefCount(valObjPtr
);
3700 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3701 Jim_DecrRefCount(interp
, nameObjPtr
);
3702 Jim_DecrRefCount(interp
, valObjPtr
);
3704 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3708 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3710 struct command_context
*context
;
3711 struct target
*target
;
3713 context
= current_command_context(interp
);
3714 assert(context
!= NULL
);
3716 target
= get_current_target(context
);
3717 if (target
== NULL
) {
3718 LOG_ERROR("mem2array: no current target");
3722 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
3725 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3733 const char *varname
;
3737 /* argv[1] = name of array to receive the data
3738 * argv[2] = desired width
3739 * argv[3] = memory address
3740 * argv[4] = count of times to read
3743 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3746 varname
= Jim_GetString(argv
[0], &len
);
3747 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3749 e
= Jim_GetLong(interp
, argv
[1], &l
);
3754 e
= Jim_GetLong(interp
, argv
[2], &l
);
3758 e
= Jim_GetLong(interp
, argv
[3], &l
);
3773 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3774 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3778 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3779 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3782 if ((addr
+ (len
* width
)) < addr
) {
3783 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3784 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3787 /* absurd transfer size? */
3789 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3790 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3795 ((width
== 2) && ((addr
& 1) == 0)) ||
3796 ((width
== 4) && ((addr
& 3) == 0))) {
3800 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3801 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3804 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3813 size_t buffersize
= 4096;
3814 uint8_t *buffer
= malloc(buffersize
);
3821 /* Slurp... in buffer size chunks */
3823 count
= len
; /* in objects.. */
3824 if (count
> (buffersize
/ width
))
3825 count
= (buffersize
/ width
);
3827 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3828 if (retval
!= ERROR_OK
) {
3830 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3834 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3835 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3839 v
= 0; /* shut up gcc */
3840 for (i
= 0; i
< count
; i
++, n
++) {
3843 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3846 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3849 v
= buffer
[i
] & 0x0ff;
3852 new_int_array_element(interp
, varname
, n
, v
);
3855 addr
+= count
* width
;
3861 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3866 static int get_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t *val
)
3869 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3873 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3877 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3883 Jim_IncrRefCount(nameObjPtr
);
3884 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3885 Jim_DecrRefCount(interp
, nameObjPtr
);
3887 if (valObjPtr
== NULL
)
3890 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3891 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3896 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3898 struct command_context
*context
;
3899 struct target
*target
;
3901 context
= current_command_context(interp
);
3902 assert(context
!= NULL
);
3904 target
= get_current_target(context
);
3905 if (target
== NULL
) {
3906 LOG_ERROR("array2mem: no current target");
3910 return target_array2mem(interp
, target
, argc
-1, argv
+ 1);
3913 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
3914 int argc
, Jim_Obj
*const *argv
)
3922 const char *varname
;
3926 /* argv[1] = name of array to get the data
3927 * argv[2] = desired width
3928 * argv[3] = memory address
3929 * argv[4] = count to write
3932 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
3935 varname
= Jim_GetString(argv
[0], &len
);
3936 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3938 e
= Jim_GetLong(interp
, argv
[1], &l
);
3943 e
= Jim_GetLong(interp
, argv
[2], &l
);
3947 e
= Jim_GetLong(interp
, argv
[3], &l
);
3962 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3963 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3964 "Invalid width param, must be 8/16/32", NULL
);
3968 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3969 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3970 "array2mem: zero width read?", NULL
);
3973 if ((addr
+ (len
* width
)) < addr
) {
3974 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3975 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3976 "array2mem: addr + len - wraps to zero?", NULL
);
3979 /* absurd transfer size? */
3981 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3982 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3983 "array2mem: absurd > 64K item request", NULL
);
3988 ((width
== 2) && ((addr
& 1) == 0)) ||
3989 ((width
== 4) && ((addr
& 3) == 0))) {
3993 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3994 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3997 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4008 size_t buffersize
= 4096;
4009 uint8_t *buffer
= malloc(buffersize
);
4014 /* Slurp... in buffer size chunks */
4016 count
= len
; /* in objects.. */
4017 if (count
> (buffersize
/ width
))
4018 count
= (buffersize
/ width
);
4020 v
= 0; /* shut up gcc */
4021 for (i
= 0; i
< count
; i
++, n
++) {
4022 get_int_array_element(interp
, varname
, n
, &v
);
4025 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
4028 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
4031 buffer
[i
] = v
& 0x0ff;
4037 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
4038 if (retval
!= ERROR_OK
) {
4040 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
4044 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4045 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
4049 addr
+= count
* width
;
4054 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4059 /* FIX? should we propagate errors here rather than printing them
4062 void target_handle_event(struct target
*target
, enum target_event e
)
4064 struct target_event_action
*teap
;
4066 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4067 if (teap
->event
== e
) {
4068 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
4069 target
->target_number
,
4070 target_name(target
),
4071 target_type_name(target
),
4073 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
4074 Jim_GetString(teap
->body
, NULL
));
4075 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
) {
4076 Jim_MakeErrorMessage(teap
->interp
);
4077 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
4084 * Returns true only if the target has a handler for the specified event.
4086 bool target_has_event_action(struct target
*target
, enum target_event event
)
4088 struct target_event_action
*teap
;
4090 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4091 if (teap
->event
== event
)
4097 enum target_cfg_param
{
4100 TCFG_WORK_AREA_VIRT
,
4101 TCFG_WORK_AREA_PHYS
,
4102 TCFG_WORK_AREA_SIZE
,
4103 TCFG_WORK_AREA_BACKUP
,
4107 TCFG_CHAIN_POSITION
,
4112 static Jim_Nvp nvp_config_opts
[] = {
4113 { .name
= "-type", .value
= TCFG_TYPE
},
4114 { .name
= "-event", .value
= TCFG_EVENT
},
4115 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
4116 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
4117 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
4118 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
4119 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
4120 { .name
= "-variant", .value
= TCFG_VARIANT
},
4121 { .name
= "-coreid", .value
= TCFG_COREID
},
4122 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
4123 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
4124 { .name
= "-rtos", .value
= TCFG_RTOS
},
4125 { .name
= NULL
, .value
= -1 }
4128 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
4136 /* parse config or cget options ... */
4137 while (goi
->argc
> 0) {
4138 Jim_SetEmptyResult(goi
->interp
);
4139 /* Jim_GetOpt_Debug(goi); */
4141 if (target
->type
->target_jim_configure
) {
4142 /* target defines a configure function */
4143 /* target gets first dibs on parameters */
4144 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
4153 /* otherwise we 'continue' below */
4155 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
4157 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
4163 if (goi
->isconfigure
) {
4164 Jim_SetResultFormatted(goi
->interp
,
4165 "not settable: %s", n
->name
);
4169 if (goi
->argc
!= 0) {
4170 Jim_WrongNumArgs(goi
->interp
,
4171 goi
->argc
, goi
->argv
,
4176 Jim_SetResultString(goi
->interp
,
4177 target_type_name(target
), -1);
4181 if (goi
->argc
== 0) {
4182 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
4186 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
4188 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
4192 if (goi
->isconfigure
) {
4193 if (goi
->argc
!= 1) {
4194 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
4198 if (goi
->argc
!= 0) {
4199 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
4205 struct target_event_action
*teap
;
4207 teap
= target
->event_action
;
4208 /* replace existing? */
4210 if (teap
->event
== (enum target_event
)n
->value
)
4215 if (goi
->isconfigure
) {
4216 bool replace
= true;
4219 teap
= calloc(1, sizeof(*teap
));
4222 teap
->event
= n
->value
;
4223 teap
->interp
= goi
->interp
;
4224 Jim_GetOpt_Obj(goi
, &o
);
4226 Jim_DecrRefCount(teap
->interp
, teap
->body
);
4227 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
4230 * Tcl/TK - "tk events" have a nice feature.
4231 * See the "BIND" command.
4232 * We should support that here.
4233 * You can specify %X and %Y in the event code.
4234 * The idea is: %T - target name.
4235 * The idea is: %N - target number
4236 * The idea is: %E - event name.
4238 Jim_IncrRefCount(teap
->body
);
4241 /* add to head of event list */
4242 teap
->next
= target
->event_action
;
4243 target
->event_action
= teap
;
4245 Jim_SetEmptyResult(goi
->interp
);
4249 Jim_SetEmptyResult(goi
->interp
);
4251 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
4257 case TCFG_WORK_AREA_VIRT
:
4258 if (goi
->isconfigure
) {
4259 target_free_all_working_areas(target
);
4260 e
= Jim_GetOpt_Wide(goi
, &w
);
4263 target
->working_area_virt
= w
;
4264 target
->working_area_virt_spec
= true;
4269 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
4273 case TCFG_WORK_AREA_PHYS
:
4274 if (goi
->isconfigure
) {
4275 target_free_all_working_areas(target
);
4276 e
= Jim_GetOpt_Wide(goi
, &w
);
4279 target
->working_area_phys
= w
;
4280 target
->working_area_phys_spec
= true;
4285 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
4289 case TCFG_WORK_AREA_SIZE
:
4290 if (goi
->isconfigure
) {
4291 target_free_all_working_areas(target
);
4292 e
= Jim_GetOpt_Wide(goi
, &w
);
4295 target
->working_area_size
= w
;
4300 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4304 case TCFG_WORK_AREA_BACKUP
:
4305 if (goi
->isconfigure
) {
4306 target_free_all_working_areas(target
);
4307 e
= Jim_GetOpt_Wide(goi
, &w
);
4310 /* make this exactly 1 or 0 */
4311 target
->backup_working_area
= (!!w
);
4316 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
4317 /* loop for more e*/
4322 if (goi
->isconfigure
) {
4323 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
4325 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
4328 target
->endianness
= n
->value
;
4333 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4334 if (n
->name
== NULL
) {
4335 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4336 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4338 Jim_SetResultString(goi
->interp
, n
->name
, -1);
4343 if (goi
->isconfigure
) {
4344 if (goi
->argc
< 1) {
4345 Jim_SetResultFormatted(goi
->interp
,
4350 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
4353 free(target
->variant
);
4354 target
->variant
= strdup(cp
);
4359 Jim_SetResultString(goi
->interp
, target
->variant
, -1);
4364 if (goi
->isconfigure
) {
4365 e
= Jim_GetOpt_Wide(goi
, &w
);
4368 target
->coreid
= (int32_t)w
;
4373 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4377 case TCFG_CHAIN_POSITION
:
4378 if (goi
->isconfigure
) {
4380 struct jtag_tap
*tap
;
4381 target_free_all_working_areas(target
);
4382 e
= Jim_GetOpt_Obj(goi
, &o_t
);
4385 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
4388 /* make this exactly 1 or 0 */
4394 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
4395 /* loop for more e*/
4398 if (goi
->isconfigure
) {
4399 e
= Jim_GetOpt_Wide(goi
, &w
);
4402 target
->dbgbase
= (uint32_t)w
;
4403 target
->dbgbase_set
= true;
4408 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
4415 int result
= rtos_create(goi
, target
);
4416 if (result
!= JIM_OK
)
4422 } /* while (goi->argc) */
4425 /* done - we return */
4429 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4433 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4434 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4435 int need_args
= 1 + goi
.isconfigure
;
4436 if (goi
.argc
< need_args
) {
4437 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4439 ? "missing: -option VALUE ..."
4440 : "missing: -option ...");
4443 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4444 return target_configure(&goi
, target
);
4447 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4449 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4452 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4454 if (goi
.argc
< 2 || goi
.argc
> 4) {
4455 Jim_SetResultFormatted(goi
.interp
,
4456 "usage: %s [phys] <address> <data> [<count>]", cmd_name
);
4461 fn
= target_write_memory
;
4464 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4466 struct Jim_Obj
*obj
;
4467 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4471 fn
= target_write_phys_memory
;
4475 e
= Jim_GetOpt_Wide(&goi
, &a
);
4480 e
= Jim_GetOpt_Wide(&goi
, &b
);
4485 if (goi
.argc
== 1) {
4486 e
= Jim_GetOpt_Wide(&goi
, &c
);
4491 /* all args must be consumed */
4495 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4497 if (strcasecmp(cmd_name
, "mww") == 0)
4499 else if (strcasecmp(cmd_name
, "mwh") == 0)
4501 else if (strcasecmp(cmd_name
, "mwb") == 0)
4504 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4508 return (target_fill_mem(target
, a
, fn
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4512 * @brief Reads an array of words/halfwords/bytes from target memory starting at specified address.
4514 * Usage: mdw [phys] <address> [<count>] - for 32 bit reads
4515 * mdh [phys] <address> [<count>] - for 16 bit reads
4516 * mdb [phys] <address> [<count>] - for 8 bit reads
4518 * Count defaults to 1.
4520 * Calls target_read_memory or target_read_phys_memory depending on
4521 * the presence of the "phys" argument
4522 * Reads the target memory in blocks of max. 32 bytes, and returns an array of ints formatted
4523 * to int representation in base16.
4524 * Also outputs read data in a human readable form using command_print
4526 * @param phys if present target_read_phys_memory will be used instead of target_read_memory
4527 * @param address address where to start the read. May be specified in decimal or hex using the standard "0x" prefix
4528 * @param count optional count parameter to read an array of values. If not specified, defaults to 1.
4529 * @returns: JIM_ERR on error or JIM_OK on success and sets the result string to an array of ascii formatted numbers
4530 * on success, with [<count>] number of elements.
4532 * In case of little endian target:
4533 * Example1: "mdw 0x00000000" returns "10123456"
4534 * Exmaple2: "mdh 0x00000000 1" returns "3456"
4535 * Example3: "mdb 0x00000000" returns "56"
4536 * Example4: "mdh 0x00000000 2" returns "3456 1012"
4537 * Example5: "mdb 0x00000000 3" returns "56 34 12"
4539 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4541 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4544 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4546 if ((goi
.argc
< 1) || (goi
.argc
> 3)) {
4547 Jim_SetResultFormatted(goi
.interp
,
4548 "usage: %s [phys] <address> [<count>]", cmd_name
);
4552 int (*fn
)(struct target
*target
,
4553 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
4554 fn
= target_read_memory
;
4557 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4559 struct Jim_Obj
*obj
;
4560 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4564 fn
= target_read_phys_memory
;
4567 /* Read address parameter */
4569 e
= Jim_GetOpt_Wide(&goi
, &addr
);
4573 /* If next parameter exists, read it out as the count parameter, if not, set it to 1 (default) */
4575 if (goi
.argc
== 1) {
4576 e
= Jim_GetOpt_Wide(&goi
, &count
);
4582 /* all args must be consumed */
4586 jim_wide dwidth
= 1; /* shut up gcc */
4587 if (strcasecmp(cmd_name
, "mdw") == 0)
4589 else if (strcasecmp(cmd_name
, "mdh") == 0)
4591 else if (strcasecmp(cmd_name
, "mdb") == 0)
4594 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4598 /* convert count to "bytes" */
4599 int bytes
= count
* dwidth
;
4601 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4602 uint8_t target_buf
[32];
4605 y
= (bytes
< 16) ? bytes
: 16; /* y = min(bytes, 16); */
4607 /* Try to read out next block */
4608 e
= fn(target
, addr
, dwidth
, y
/ dwidth
, target_buf
);
4610 if (e
!= ERROR_OK
) {
4611 Jim_SetResultFormatted(interp
, "error reading target @ 0x%08lx", (long)addr
);
4615 command_print_sameline(NULL
, "0x%08x ", (int)(addr
));
4618 for (x
= 0; x
< 16 && x
< y
; x
+= 4) {
4619 z
= target_buffer_get_u32(target
, &(target_buf
[x
]));
4620 command_print_sameline(NULL
, "%08x ", (int)(z
));
4622 for (; (x
< 16) ; x
+= 4)
4623 command_print_sameline(NULL
, " ");
4626 for (x
= 0; x
< 16 && x
< y
; x
+= 2) {
4627 z
= target_buffer_get_u16(target
, &(target_buf
[x
]));
4628 command_print_sameline(NULL
, "%04x ", (int)(z
));
4630 for (; (x
< 16) ; x
+= 2)
4631 command_print_sameline(NULL
, " ");
4635 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
4636 z
= target_buffer_get_u8(target
, &(target_buf
[x
]));
4637 command_print_sameline(NULL
, "%02x ", (int)(z
));
4639 for (; (x
< 16) ; x
+= 1)
4640 command_print_sameline(NULL
, " ");
4643 /* ascii-ify the bytes */
4644 for (x
= 0 ; x
< y
; x
++) {
4645 if ((target_buf
[x
] >= 0x20) &&
4646 (target_buf
[x
] <= 0x7e)) {
4650 target_buf
[x
] = '.';
4655 target_buf
[x
] = ' ';
4660 /* print - with a newline */
4661 command_print_sameline(NULL
, "%s\n", target_buf
);
4669 static int jim_target_mem2array(Jim_Interp
*interp
,
4670 int argc
, Jim_Obj
*const *argv
)
4672 struct target
*target
= Jim_CmdPrivData(interp
);
4673 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4676 static int jim_target_array2mem(Jim_Interp
*interp
,
4677 int argc
, Jim_Obj
*const *argv
)
4679 struct target
*target
= Jim_CmdPrivData(interp
);
4680 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4683 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4685 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
4689 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4692 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4695 struct target
*target
= Jim_CmdPrivData(interp
);
4696 if (!target
->tap
->enabled
)
4697 return jim_target_tap_disabled(interp
);
4699 int e
= target
->type
->examine(target
);
4705 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4708 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4711 struct target
*target
= Jim_CmdPrivData(interp
);
4713 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
4719 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4722 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4725 struct target
*target
= Jim_CmdPrivData(interp
);
4726 if (!target
->tap
->enabled
)
4727 return jim_target_tap_disabled(interp
);
4730 if (!(target_was_examined(target
)))
4731 e
= ERROR_TARGET_NOT_EXAMINED
;
4733 e
= target
->type
->poll(target
);
4739 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4742 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4744 if (goi
.argc
!= 2) {
4745 Jim_WrongNumArgs(interp
, 0, argv
,
4746 "([tT]|[fF]|assert|deassert) BOOL");
4751 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4753 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4756 /* the halt or not param */
4758 e
= Jim_GetOpt_Wide(&goi
, &a
);
4762 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4763 if (!target
->tap
->enabled
)
4764 return jim_target_tap_disabled(interp
);
4765 if (!(target_was_examined(target
))) {
4766 LOG_ERROR("Target not examined yet");
4767 return ERROR_TARGET_NOT_EXAMINED
;
4769 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
4770 Jim_SetResultFormatted(interp
,
4771 "No target-specific reset for %s",
4772 target_name(target
));
4775 /* determine if we should halt or not. */
4776 target
->reset_halt
= !!a
;
4777 /* When this happens - all workareas are invalid. */
4778 target_free_all_working_areas_restore(target
, 0);
4781 if (n
->value
== NVP_ASSERT
)
4782 e
= target
->type
->assert_reset(target
);
4784 e
= target
->type
->deassert_reset(target
);
4785 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4788 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4791 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4794 struct target
*target
= Jim_CmdPrivData(interp
);
4795 if (!target
->tap
->enabled
)
4796 return jim_target_tap_disabled(interp
);
4797 int e
= target
->type
->halt(target
);
4798 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4801 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4804 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4806 /* params: <name> statename timeoutmsecs */
4807 if (goi
.argc
!= 2) {
4808 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4809 Jim_SetResultFormatted(goi
.interp
,
4810 "%s <state_name> <timeout_in_msec>", cmd_name
);
4815 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4817 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
, 1);
4821 e
= Jim_GetOpt_Wide(&goi
, &a
);
4824 struct target
*target
= Jim_CmdPrivData(interp
);
4825 if (!target
->tap
->enabled
)
4826 return jim_target_tap_disabled(interp
);
4828 e
= target_wait_state(target
, n
->value
, a
);
4829 if (e
!= ERROR_OK
) {
4830 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
4831 Jim_SetResultFormatted(goi
.interp
,
4832 "target: %s wait %s fails (%#s) %s",
4833 target_name(target
), n
->name
,
4834 eObj
, target_strerror_safe(e
));
4835 Jim_FreeNewObj(interp
, eObj
);
4840 /* List for human, Events defined for this target.
4841 * scripts/programs should use 'name cget -event NAME'
4843 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4845 struct command_context
*cmd_ctx
= current_command_context(interp
);
4846 assert(cmd_ctx
!= NULL
);
4848 struct target
*target
= Jim_CmdPrivData(interp
);
4849 struct target_event_action
*teap
= target
->event_action
;
4850 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4851 target
->target_number
,
4852 target_name(target
));
4853 command_print(cmd_ctx
, "%-25s | Body", "Event");
4854 command_print(cmd_ctx
, "------------------------- | "
4855 "----------------------------------------");
4857 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
4858 command_print(cmd_ctx
, "%-25s | %s",
4859 opt
->name
, Jim_GetString(teap
->body
, NULL
));
4862 command_print(cmd_ctx
, "***END***");
4865 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4868 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4871 struct target
*target
= Jim_CmdPrivData(interp
);
4872 Jim_SetResultString(interp
, target_state_name(target
), -1);
4875 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4878 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4879 if (goi
.argc
!= 1) {
4880 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4881 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
4885 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4887 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4890 struct target
*target
= Jim_CmdPrivData(interp
);
4891 target_handle_event(target
, n
->value
);
4895 static const struct command_registration target_instance_command_handlers
[] = {
4897 .name
= "configure",
4898 .mode
= COMMAND_CONFIG
,
4899 .jim_handler
= jim_target_configure
,
4900 .help
= "configure a new target for use",
4901 .usage
= "[target_attribute ...]",
4905 .mode
= COMMAND_ANY
,
4906 .jim_handler
= jim_target_configure
,
4907 .help
= "returns the specified target attribute",
4908 .usage
= "target_attribute",
4912 .mode
= COMMAND_EXEC
,
4913 .jim_handler
= jim_target_mw
,
4914 .help
= "Write 32-bit word(s) to target memory",
4915 .usage
= "address data [count]",
4919 .mode
= COMMAND_EXEC
,
4920 .jim_handler
= jim_target_mw
,
4921 .help
= "Write 16-bit half-word(s) to target memory",
4922 .usage
= "address data [count]",
4926 .mode
= COMMAND_EXEC
,
4927 .jim_handler
= jim_target_mw
,
4928 .help
= "Write byte(s) to target memory",
4929 .usage
= "address data [count]",
4933 .mode
= COMMAND_EXEC
,
4934 .jim_handler
= jim_target_md
,
4935 .help
= "Display target memory as 32-bit words",
4936 .usage
= "address [count]",
4940 .mode
= COMMAND_EXEC
,
4941 .jim_handler
= jim_target_md
,
4942 .help
= "Display target memory as 16-bit half-words",
4943 .usage
= "address [count]",
4947 .mode
= COMMAND_EXEC
,
4948 .jim_handler
= jim_target_md
,
4949 .help
= "Display target memory as 8-bit bytes",
4950 .usage
= "address [count]",
4953 .name
= "array2mem",
4954 .mode
= COMMAND_EXEC
,
4955 .jim_handler
= jim_target_array2mem
,
4956 .help
= "Writes Tcl array of 8/16/32 bit numbers "
4958 .usage
= "arrayname bitwidth address count",
4961 .name
= "mem2array",
4962 .mode
= COMMAND_EXEC
,
4963 .jim_handler
= jim_target_mem2array
,
4964 .help
= "Loads Tcl array of 8/16/32 bit numbers "
4965 "from target memory",
4966 .usage
= "arrayname bitwidth address count",
4969 .name
= "eventlist",
4970 .mode
= COMMAND_EXEC
,
4971 .jim_handler
= jim_target_event_list
,
4972 .help
= "displays a table of events defined for this target",
4976 .mode
= COMMAND_EXEC
,
4977 .jim_handler
= jim_target_current_state
,
4978 .help
= "displays the current state of this target",
4981 .name
= "arp_examine",
4982 .mode
= COMMAND_EXEC
,
4983 .jim_handler
= jim_target_examine
,
4984 .help
= "used internally for reset processing",
4987 .name
= "arp_halt_gdb",
4988 .mode
= COMMAND_EXEC
,
4989 .jim_handler
= jim_target_halt_gdb
,
4990 .help
= "used internally for reset processing to halt GDB",
4994 .mode
= COMMAND_EXEC
,
4995 .jim_handler
= jim_target_poll
,
4996 .help
= "used internally for reset processing",
4999 .name
= "arp_reset",
5000 .mode
= COMMAND_EXEC
,
5001 .jim_handler
= jim_target_reset
,
5002 .help
= "used internally for reset processing",
5006 .mode
= COMMAND_EXEC
,
5007 .jim_handler
= jim_target_halt
,
5008 .help
= "used internally for reset processing",
5011 .name
= "arp_waitstate",
5012 .mode
= COMMAND_EXEC
,
5013 .jim_handler
= jim_target_wait_state
,
5014 .help
= "used internally for reset processing",
5017 .name
= "invoke-event",
5018 .mode
= COMMAND_EXEC
,
5019 .jim_handler
= jim_target_invoke_event
,
5020 .help
= "invoke handler for specified event",
5021 .usage
= "event_name",
5023 COMMAND_REGISTRATION_DONE
5026 static int target_create(Jim_GetOptInfo
*goi
)
5034 struct target
*target
;
5035 struct command_context
*cmd_ctx
;
5037 cmd_ctx
= current_command_context(goi
->interp
);
5038 assert(cmd_ctx
!= NULL
);
5040 if (goi
->argc
< 3) {
5041 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
5046 Jim_GetOpt_Obj(goi
, &new_cmd
);
5047 /* does this command exist? */
5048 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
5050 cp
= Jim_GetString(new_cmd
, NULL
);
5051 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
5056 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
5060 /* now does target type exist */
5061 for (x
= 0 ; target_types
[x
] ; x
++) {
5062 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
5067 /* check for deprecated name */
5068 if (target_types
[x
]->deprecated_name
) {
5069 if (0 == strcmp(cp
, target_types
[x
]->deprecated_name
)) {
5071 LOG_WARNING("target name is deprecated use: \'%s\'", target_types
[x
]->name
);
5076 if (target_types
[x
] == NULL
) {
5077 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
5078 for (x
= 0 ; target_types
[x
] ; x
++) {
5079 if (target_types
[x
+ 1]) {
5080 Jim_AppendStrings(goi
->interp
,
5081 Jim_GetResult(goi
->interp
),
5082 target_types
[x
]->name
,
5085 Jim_AppendStrings(goi
->interp
,
5086 Jim_GetResult(goi
->interp
),
5088 target_types
[x
]->name
, NULL
);
5095 target
= calloc(1, sizeof(struct target
));
5096 /* set target number */
5097 target
->target_number
= new_target_number();
5099 /* allocate memory for each unique target type */
5100 target
->type
= (struct target_type
*)calloc(1, sizeof(struct target_type
));
5102 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
5104 /* will be set by "-endian" */
5105 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5107 /* default to first core, override with -coreid */
5110 target
->working_area
= 0x0;
5111 target
->working_area_size
= 0x0;
5112 target
->working_areas
= NULL
;
5113 target
->backup_working_area
= 0;
5115 target
->state
= TARGET_UNKNOWN
;
5116 target
->debug_reason
= DBG_REASON_UNDEFINED
;
5117 target
->reg_cache
= NULL
;
5118 target
->breakpoints
= NULL
;
5119 target
->watchpoints
= NULL
;
5120 target
->next
= NULL
;
5121 target
->arch_info
= NULL
;
5123 target
->display
= 1;
5125 target
->halt_issued
= false;
5127 /* initialize trace information */
5128 target
->trace_info
= malloc(sizeof(struct trace
));
5129 target
->trace_info
->num_trace_points
= 0;
5130 target
->trace_info
->trace_points_size
= 0;
5131 target
->trace_info
->trace_points
= NULL
;
5132 target
->trace_info
->trace_history_size
= 0;
5133 target
->trace_info
->trace_history
= NULL
;
5134 target
->trace_info
->trace_history_pos
= 0;
5135 target
->trace_info
->trace_history_overflowed
= 0;
5137 target
->dbgmsg
= NULL
;
5138 target
->dbg_msg_enabled
= 0;
5140 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5142 target
->rtos
= NULL
;
5143 target
->rtos_auto_detect
= false;
5145 /* Do the rest as "configure" options */
5146 goi
->isconfigure
= 1;
5147 e
= target_configure(goi
, target
);
5149 if (target
->tap
== NULL
) {
5150 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
5160 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
5161 /* default endian to little if not specified */
5162 target
->endianness
= TARGET_LITTLE_ENDIAN
;
5165 /* incase variant is not set */
5166 if (!target
->variant
)
5167 target
->variant
= strdup("");
5169 cp
= Jim_GetString(new_cmd
, NULL
);
5170 target
->cmd_name
= strdup(cp
);
5172 /* create the target specific commands */
5173 if (target
->type
->commands
) {
5174 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
5176 LOG_ERROR("unable to register '%s' commands", cp
);
5178 if (target
->type
->target_create
)
5179 (*(target
->type
->target_create
))(target
, goi
->interp
);
5181 /* append to end of list */
5183 struct target
**tpp
;
5184 tpp
= &(all_targets
);
5186 tpp
= &((*tpp
)->next
);
5190 /* now - create the new target name command */
5191 const struct command_registration target_subcommands
[] = {
5193 .chain
= target_instance_command_handlers
,
5196 .chain
= target
->type
->commands
,
5198 COMMAND_REGISTRATION_DONE
5200 const struct command_registration target_commands
[] = {
5203 .mode
= COMMAND_ANY
,
5204 .help
= "target command group",
5206 .chain
= target_subcommands
,
5208 COMMAND_REGISTRATION_DONE
5210 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
5214 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
5216 command_set_handler_data(c
, target
);
5218 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
5221 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5224 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5227 struct command_context
*cmd_ctx
= current_command_context(interp
);
5228 assert(cmd_ctx
!= NULL
);
5230 Jim_SetResultString(interp
, target_name(get_current_target(cmd_ctx
)), -1);
5234 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5237 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5240 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5241 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++) {
5242 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5243 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
5248 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5251 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5254 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5255 struct target
*target
= all_targets
;
5257 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5258 Jim_NewStringObj(interp
, target_name(target
), -1));
5259 target
= target
->next
;
5264 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5267 const char *targetname
;
5269 struct target
*target
= (struct target
*) NULL
;
5270 struct target_list
*head
, *curr
, *new;
5271 curr
= (struct target_list
*) NULL
;
5272 head
= (struct target_list
*) NULL
;
5275 LOG_DEBUG("%d", argc
);
5276 /* argv[1] = target to associate in smp
5277 * argv[2] = target to assoicate in smp
5281 for (i
= 1; i
< argc
; i
++) {
5283 targetname
= Jim_GetString(argv
[i
], &len
);
5284 target
= get_target(targetname
);
5285 LOG_DEBUG("%s ", targetname
);
5287 new = malloc(sizeof(struct target_list
));
5288 new->target
= target
;
5289 new->next
= (struct target_list
*)NULL
;
5290 if (head
== (struct target_list
*)NULL
) {
5299 /* now parse the list of cpu and put the target in smp mode*/
5302 while (curr
!= (struct target_list
*)NULL
) {
5303 target
= curr
->target
;
5305 target
->head
= head
;
5309 if (target
&& target
->rtos
)
5310 retval
= rtos_smp_init(head
->target
);
5316 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5319 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5321 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5322 "<name> <target_type> [<target_options> ...]");
5325 return target_create(&goi
);
5328 static int jim_target_number(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5331 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5333 /* It's OK to remove this mechanism sometime after August 2010 or so */
5334 LOG_WARNING("don't use numbers as target identifiers; use names");
5335 if (goi
.argc
!= 1) {
5336 Jim_SetResultFormatted(goi
.interp
, "usage: target number <number>");
5340 int e
= Jim_GetOpt_Wide(&goi
, &w
);
5344 struct target
*target
;
5345 for (target
= all_targets
; NULL
!= target
; target
= target
->next
) {
5346 if (target
->target_number
!= w
)
5349 Jim_SetResultString(goi
.interp
, target_name(target
), -1);
5353 Jim_Obj
*wObj
= Jim_NewIntObj(goi
.interp
, w
);
5354 Jim_SetResultFormatted(goi
.interp
,
5355 "Target: number %#s does not exist", wObj
);
5356 Jim_FreeNewObj(interp
, wObj
);
5361 static int jim_target_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5364 Jim_WrongNumArgs(interp
, 1, argv
, "<no parameters>");
5368 struct target
*target
= all_targets
;
5369 while (NULL
!= target
) {
5370 target
= target
->next
;
5373 Jim_SetResult(interp
, Jim_NewIntObj(interp
, count
));
5377 static const struct command_registration target_subcommand_handlers
[] = {
5380 .mode
= COMMAND_CONFIG
,
5381 .handler
= handle_target_init_command
,
5382 .help
= "initialize targets",
5386 /* REVISIT this should be COMMAND_CONFIG ... */
5387 .mode
= COMMAND_ANY
,
5388 .jim_handler
= jim_target_create
,
5389 .usage
= "name type '-chain-position' name [options ...]",
5390 .help
= "Creates and selects a new target",
5394 .mode
= COMMAND_ANY
,
5395 .jim_handler
= jim_target_current
,
5396 .help
= "Returns the currently selected target",
5400 .mode
= COMMAND_ANY
,
5401 .jim_handler
= jim_target_types
,
5402 .help
= "Returns the available target types as "
5403 "a list of strings",
5407 .mode
= COMMAND_ANY
,
5408 .jim_handler
= jim_target_names
,
5409 .help
= "Returns the names of all targets as a list of strings",
5413 .mode
= COMMAND_ANY
,
5414 .jim_handler
= jim_target_number
,
5416 .help
= "Returns the name of the numbered target "
5421 .mode
= COMMAND_ANY
,
5422 .jim_handler
= jim_target_count
,
5423 .help
= "Returns the number of targets as an integer "
5428 .mode
= COMMAND_ANY
,
5429 .jim_handler
= jim_target_smp
,
5430 .usage
= "targetname1 targetname2 ...",
5431 .help
= "gather several target in a smp list"
5434 COMMAND_REGISTRATION_DONE
5444 static int fastload_num
;
5445 static struct FastLoad
*fastload
;
5447 static void free_fastload(void)
5449 if (fastload
!= NULL
) {
5451 for (i
= 0; i
< fastload_num
; i
++) {
5452 if (fastload
[i
].data
)
5453 free(fastload
[i
].data
);
5460 COMMAND_HANDLER(handle_fast_load_image_command
)
5464 uint32_t image_size
;
5465 uint32_t min_address
= 0;
5466 uint32_t max_address
= 0xffffffff;
5471 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
5472 &image
, &min_address
, &max_address
);
5473 if (ERROR_OK
!= retval
)
5476 struct duration bench
;
5477 duration_start(&bench
);
5479 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5480 if (retval
!= ERROR_OK
)
5485 fastload_num
= image
.num_sections
;
5486 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5487 if (fastload
== NULL
) {
5488 command_print(CMD_CTX
, "out of memory");
5489 image_close(&image
);
5492 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5493 for (i
= 0; i
< image
.num_sections
; i
++) {
5494 buffer
= malloc(image
.sections
[i
].size
);
5495 if (buffer
== NULL
) {
5496 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5497 (int)(image
.sections
[i
].size
));
5498 retval
= ERROR_FAIL
;
5502 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
5503 if (retval
!= ERROR_OK
) {
5508 uint32_t offset
= 0;
5509 uint32_t length
= buf_cnt
;
5511 /* DANGER!!! beware of unsigned comparision here!!! */
5513 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
5514 (image
.sections
[i
].base_address
< max_address
)) {
5515 if (image
.sections
[i
].base_address
< min_address
) {
5516 /* clip addresses below */
5517 offset
+= min_address
-image
.sections
[i
].base_address
;
5521 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5522 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5524 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5525 fastload
[i
].data
= malloc(length
);
5526 if (fastload
[i
].data
== NULL
) {
5528 command_print(CMD_CTX
, "error allocating buffer for section (%" PRIu32
" bytes)",
5530 retval
= ERROR_FAIL
;
5533 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5534 fastload
[i
].length
= length
;
5536 image_size
+= length
;
5537 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
5538 (unsigned int)length
,
5539 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
5545 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
5546 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
5547 "in %fs (%0.3f KiB/s)", image_size
,
5548 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
5550 command_print(CMD_CTX
,
5551 "WARNING: image has not been loaded to target!"
5552 "You can issue a 'fast_load' to finish loading.");
5555 image_close(&image
);
5557 if (retval
!= ERROR_OK
)
5563 COMMAND_HANDLER(handle_fast_load_command
)
5566 return ERROR_COMMAND_SYNTAX_ERROR
;
5567 if (fastload
== NULL
) {
5568 LOG_ERROR("No image in memory");
5572 int ms
= timeval_ms();
5574 int retval
= ERROR_OK
;
5575 for (i
= 0; i
< fastload_num
; i
++) {
5576 struct target
*target
= get_current_target(CMD_CTX
);
5577 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
5578 (unsigned int)(fastload
[i
].address
),
5579 (unsigned int)(fastload
[i
].length
));
5580 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5581 if (retval
!= ERROR_OK
)
5583 size
+= fastload
[i
].length
;
5585 if (retval
== ERROR_OK
) {
5586 int after
= timeval_ms();
5587 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5592 static const struct command_registration target_command_handlers
[] = {
5595 .handler
= handle_targets_command
,
5596 .mode
= COMMAND_ANY
,
5597 .help
= "change current default target (one parameter) "
5598 "or prints table of all targets (no parameters)",
5599 .usage
= "[target]",
5603 .mode
= COMMAND_CONFIG
,
5604 .help
= "configure target",
5606 .chain
= target_subcommand_handlers
,
5608 COMMAND_REGISTRATION_DONE
5611 int target_register_commands(struct command_context
*cmd_ctx
)
5613 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5616 static bool target_reset_nag
= true;
5618 bool get_target_reset_nag(void)
5620 return target_reset_nag
;
5623 COMMAND_HANDLER(handle_target_reset_nag
)
5625 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
5626 &target_reset_nag
, "Nag after each reset about options to improve "
5630 COMMAND_HANDLER(handle_ps_command
)
5632 struct target
*target
= get_current_target(CMD_CTX
);
5634 if (target
->state
!= TARGET_HALTED
) {
5635 LOG_INFO("target not halted !!");
5639 if ((target
->rtos
) && (target
->rtos
->type
)
5640 && (target
->rtos
->type
->ps_command
)) {
5641 display
= target
->rtos
->type
->ps_command(target
);
5642 command_print(CMD_CTX
, "%s", display
);
5647 return ERROR_TARGET_FAILURE
;
5651 static void binprint(struct command_context
*cmd_ctx
, const char *text
, const uint8_t *buf
, int size
)
5654 command_print_sameline(cmd_ctx
, "%s", text
);
5655 for (int i
= 0; i
< size
; i
++)
5656 command_print_sameline(cmd_ctx
, " %02x", buf
[i
]);
5657 command_print(cmd_ctx
, " ");
5660 COMMAND_HANDLER(handle_test_mem_access_command
)
5662 struct target
*target
= get_current_target(CMD_CTX
);
5664 int retval
= ERROR_OK
;
5666 if (target
->state
!= TARGET_HALTED
) {
5667 LOG_INFO("target not halted !!");
5672 return ERROR_COMMAND_SYNTAX_ERROR
;
5674 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], test_size
);
5677 size_t num_bytes
= test_size
+ 4;
5679 struct working_area
*wa
= NULL
;
5680 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
5681 if (retval
!= ERROR_OK
) {
5682 LOG_ERROR("Not enough working area");
5686 uint8_t *test_pattern
= malloc(num_bytes
);
5688 for (size_t i
= 0; i
< num_bytes
; i
++)
5689 test_pattern
[i
] = rand();
5691 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
5692 if (retval
!= ERROR_OK
) {
5693 LOG_ERROR("Test pattern write failed");
5697 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
5698 for (int size
= 1; size
<= 4; size
*= 2) {
5699 for (int offset
= 0; offset
< 4; offset
++) {
5700 uint32_t count
= test_size
/ size
;
5701 size_t host_bufsiz
= (count
+ 2) * size
+ host_offset
;
5702 uint8_t *read_ref
= malloc(host_bufsiz
);
5703 uint8_t *read_buf
= malloc(host_bufsiz
);
5705 for (size_t i
= 0; i
< host_bufsiz
; i
++) {
5706 read_ref
[i
] = rand();
5707 read_buf
[i
] = read_ref
[i
];
5709 command_print_sameline(CMD_CTX
,
5710 "Test read %d x %d @ %d to %saligned buffer: ", count
,
5711 size
, offset
, host_offset
? "un" : "");
5713 struct duration bench
;
5714 duration_start(&bench
);
5716 retval
= target_read_memory(target
, wa
->address
+ offset
, size
, count
,
5717 read_buf
+ size
+ host_offset
);
5719 duration_measure(&bench
);
5721 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
5722 command_print(CMD_CTX
, "Unsupported alignment");
5724 } else if (retval
!= ERROR_OK
) {
5725 command_print(CMD_CTX
, "Memory read failed");
5729 /* replay on host */
5730 memcpy(read_ref
+ size
+ host_offset
, test_pattern
+ offset
, count
* size
);
5733 int result
= memcmp(read_ref
, read_buf
, host_bufsiz
);
5735 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
5736 duration_elapsed(&bench
),
5737 duration_kbps(&bench
, count
* size
));
5739 command_print(CMD_CTX
, "Compare failed");
5740 binprint(CMD_CTX
, "ref:", read_ref
, host_bufsiz
);
5741 binprint(CMD_CTX
, "buf:", read_buf
, host_bufsiz
);
5754 target_free_working_area(target
, wa
);
5757 num_bytes
= test_size
+ 4 + 4 + 4;
5759 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
5760 if (retval
!= ERROR_OK
) {
5761 LOG_ERROR("Not enough working area");
5765 test_pattern
= malloc(num_bytes
);
5767 for (size_t i
= 0; i
< num_bytes
; i
++)
5768 test_pattern
[i
] = rand();
5770 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
5771 for (int size
= 1; size
<= 4; size
*= 2) {
5772 for (int offset
= 0; offset
< 4; offset
++) {
5773 uint32_t count
= test_size
/ size
;
5774 size_t host_bufsiz
= count
* size
+ host_offset
;
5775 uint8_t *read_ref
= malloc(num_bytes
);
5776 uint8_t *read_buf
= malloc(num_bytes
);
5777 uint8_t *write_buf
= malloc(host_bufsiz
);
5779 for (size_t i
= 0; i
< host_bufsiz
; i
++)
5780 write_buf
[i
] = rand();
5781 command_print_sameline(CMD_CTX
,
5782 "Test write %d x %d @ %d from %saligned buffer: ", count
,
5783 size
, offset
, host_offset
? "un" : "");
5785 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
5786 if (retval
!= ERROR_OK
) {
5787 command_print(CMD_CTX
, "Test pattern write failed");
5791 /* replay on host */
5792 memcpy(read_ref
, test_pattern
, num_bytes
);
5793 memcpy(read_ref
+ size
+ offset
, write_buf
+ host_offset
, count
* size
);
5795 struct duration bench
;
5796 duration_start(&bench
);
5798 retval
= target_write_memory(target
, wa
->address
+ size
+ offset
, size
, count
,
5799 write_buf
+ host_offset
);
5801 duration_measure(&bench
);
5803 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
5804 command_print(CMD_CTX
, "Unsupported alignment");
5806 } else if (retval
!= ERROR_OK
) {
5807 command_print(CMD_CTX
, "Memory write failed");
5812 retval
= target_read_memory(target
, wa
->address
, 1, num_bytes
, read_buf
);
5813 if (retval
!= ERROR_OK
) {
5814 command_print(CMD_CTX
, "Test pattern write failed");
5819 int result
= memcmp(read_ref
, read_buf
, num_bytes
);
5821 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
5822 duration_elapsed(&bench
),
5823 duration_kbps(&bench
, count
* size
));
5825 command_print(CMD_CTX
, "Compare failed");
5826 binprint(CMD_CTX
, "ref:", read_ref
, num_bytes
);
5827 binprint(CMD_CTX
, "buf:", read_buf
, num_bytes
);
5839 target_free_working_area(target
, wa
);
5843 static const struct command_registration target_exec_command_handlers
[] = {
5845 .name
= "fast_load_image",
5846 .handler
= handle_fast_load_image_command
,
5847 .mode
= COMMAND_ANY
,
5848 .help
= "Load image into server memory for later use by "
5849 "fast_load; primarily for profiling",
5850 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5851 "[min_address [max_length]]",
5854 .name
= "fast_load",
5855 .handler
= handle_fast_load_command
,
5856 .mode
= COMMAND_EXEC
,
5857 .help
= "loads active fast load image to current target "
5858 "- mainly for profiling purposes",
5863 .handler
= handle_profile_command
,
5864 .mode
= COMMAND_EXEC
,
5865 .usage
= "seconds filename [start end]",
5866 .help
= "profiling samples the CPU PC",
5868 /** @todo don't register virt2phys() unless target supports it */
5870 .name
= "virt2phys",
5871 .handler
= handle_virt2phys_command
,
5872 .mode
= COMMAND_ANY
,
5873 .help
= "translate a virtual address into a physical address",
5874 .usage
= "virtual_address",
5878 .handler
= handle_reg_command
,
5879 .mode
= COMMAND_EXEC
,
5880 .help
= "display (reread from target with \"force\") or set a register; "
5881 "with no arguments, displays all registers and their values",
5882 .usage
= "[(register_number|register_name) [(value|'force')]]",
5886 .handler
= handle_poll_command
,
5887 .mode
= COMMAND_EXEC
,
5888 .help
= "poll target state; or reconfigure background polling",
5889 .usage
= "['on'|'off']",
5892 .name
= "wait_halt",
5893 .handler
= handle_wait_halt_command
,
5894 .mode
= COMMAND_EXEC
,
5895 .help
= "wait up to the specified number of milliseconds "
5896 "(default 5000) for a previously requested halt",
5897 .usage
= "[milliseconds]",
5901 .handler
= handle_halt_command
,
5902 .mode
= COMMAND_EXEC
,
5903 .help
= "request target to halt, then wait up to the specified"
5904 "number of milliseconds (default 5000) for it to complete",
5905 .usage
= "[milliseconds]",
5909 .handler
= handle_resume_command
,
5910 .mode
= COMMAND_EXEC
,
5911 .help
= "resume target execution from current PC or address",
5912 .usage
= "[address]",
5916 .handler
= handle_reset_command
,
5917 .mode
= COMMAND_EXEC
,
5918 .usage
= "[run|halt|init]",
5919 .help
= "Reset all targets into the specified mode."
5920 "Default reset mode is run, if not given.",
5923 .name
= "soft_reset_halt",
5924 .handler
= handle_soft_reset_halt_command
,
5925 .mode
= COMMAND_EXEC
,
5927 .help
= "halt the target and do a soft reset",
5931 .handler
= handle_step_command
,
5932 .mode
= COMMAND_EXEC
,
5933 .help
= "step one instruction from current PC or address",
5934 .usage
= "[address]",
5938 .handler
= handle_md_command
,
5939 .mode
= COMMAND_EXEC
,
5940 .help
= "display memory words",
5941 .usage
= "['phys'] address [count]",
5945 .handler
= handle_md_command
,
5946 .mode
= COMMAND_EXEC
,
5947 .help
= "display memory half-words",
5948 .usage
= "['phys'] address [count]",
5952 .handler
= handle_md_command
,
5953 .mode
= COMMAND_EXEC
,
5954 .help
= "display memory bytes",
5955 .usage
= "['phys'] address [count]",
5959 .handler
= handle_mw_command
,
5960 .mode
= COMMAND_EXEC
,
5961 .help
= "write memory word",
5962 .usage
= "['phys'] address value [count]",
5966 .handler
= handle_mw_command
,
5967 .mode
= COMMAND_EXEC
,
5968 .help
= "write memory half-word",
5969 .usage
= "['phys'] address value [count]",
5973 .handler
= handle_mw_command
,
5974 .mode
= COMMAND_EXEC
,
5975 .help
= "write memory byte",
5976 .usage
= "['phys'] address value [count]",
5980 .handler
= handle_bp_command
,
5981 .mode
= COMMAND_EXEC
,
5982 .help
= "list or set hardware or software breakpoint",
5983 .usage
= "<address> [<asid>]<length> ['hw'|'hw_ctx']",
5987 .handler
= handle_rbp_command
,
5988 .mode
= COMMAND_EXEC
,
5989 .help
= "remove breakpoint",
5994 .handler
= handle_wp_command
,
5995 .mode
= COMMAND_EXEC
,
5996 .help
= "list (no params) or create watchpoints",
5997 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
6001 .handler
= handle_rwp_command
,
6002 .mode
= COMMAND_EXEC
,
6003 .help
= "remove watchpoint",
6007 .name
= "load_image",
6008 .handler
= handle_load_image_command
,
6009 .mode
= COMMAND_EXEC
,
6010 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6011 "[min_address] [max_length]",
6014 .name
= "dump_image",
6015 .handler
= handle_dump_image_command
,
6016 .mode
= COMMAND_EXEC
,
6017 .usage
= "filename address size",
6020 .name
= "verify_image",
6021 .handler
= handle_verify_image_command
,
6022 .mode
= COMMAND_EXEC
,
6023 .usage
= "filename [offset [type]]",
6026 .name
= "test_image",
6027 .handler
= handle_test_image_command
,
6028 .mode
= COMMAND_EXEC
,
6029 .usage
= "filename [offset [type]]",
6032 .name
= "mem2array",
6033 .mode
= COMMAND_EXEC
,
6034 .jim_handler
= jim_mem2array
,
6035 .help
= "read 8/16/32 bit memory and return as a TCL array "
6036 "for script processing",
6037 .usage
= "arrayname bitwidth address count",
6040 .name
= "array2mem",
6041 .mode
= COMMAND_EXEC
,
6042 .jim_handler
= jim_array2mem
,
6043 .help
= "convert a TCL array to memory locations "
6044 "and write the 8/16/32 bit values",
6045 .usage
= "arrayname bitwidth address count",
6048 .name
= "reset_nag",
6049 .handler
= handle_target_reset_nag
,
6050 .mode
= COMMAND_ANY
,
6051 .help
= "Nag after each reset about options that could have been "
6052 "enabled to improve performance. ",
6053 .usage
= "['enable'|'disable']",
6057 .handler
= handle_ps_command
,
6058 .mode
= COMMAND_EXEC
,
6059 .help
= "list all tasks ",
6063 .name
= "test_mem_access",
6064 .handler
= handle_test_mem_access_command
,
6065 .mode
= COMMAND_EXEC
,
6066 .help
= "Test the target's memory access functions",
6070 COMMAND_REGISTRATION_DONE
6072 static int target_register_user_commands(struct command_context
*cmd_ctx
)
6074 int retval
= ERROR_OK
;
6075 retval
= target_request_register_commands(cmd_ctx
);
6076 if (retval
!= ERROR_OK
)
6079 retval
= trace_register_commands(cmd_ctx
);
6080 if (retval
!= ERROR_OK
)
6084 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);