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, see <http://www.gnu.org/licenses/>. *
38 ***************************************************************************/
44 #include <helper/time_support.h>
45 #include <jtag/jtag.h>
46 #include <flash/nor/core.h>
49 #include "target_type.h"
50 #include "target_request.h"
51 #include "breakpoints.h"
55 #include "rtos/rtos.h"
56 #include "transport/transport.h"
58 /* default halt wait timeout (ms) */
59 #define DEFAULT_HALT_TIMEOUT 5000
61 static int target_read_buffer_default(struct target
*target
, uint32_t address
,
62 uint32_t count
, uint8_t *buffer
);
63 static int target_write_buffer_default(struct target
*target
, uint32_t address
,
64 uint32_t count
, const uint8_t *buffer
);
65 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
66 int argc
, Jim_Obj
* const *argv
);
67 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
68 int argc
, Jim_Obj
* const *argv
);
69 static int target_register_user_commands(struct command_context
*cmd_ctx
);
70 static int target_get_gdb_fileio_info_default(struct target
*target
,
71 struct gdb_fileio_info
*fileio_info
);
72 static int target_gdb_fileio_end_default(struct target
*target
, int retcode
,
73 int fileio_errno
, bool ctrl_c
);
74 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
75 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
);
78 extern struct target_type arm7tdmi_target
;
79 extern struct target_type arm720t_target
;
80 extern struct target_type arm9tdmi_target
;
81 extern struct target_type arm920t_target
;
82 extern struct target_type arm966e_target
;
83 extern struct target_type arm946e_target
;
84 extern struct target_type arm926ejs_target
;
85 extern struct target_type fa526_target
;
86 extern struct target_type feroceon_target
;
87 extern struct target_type dragonite_target
;
88 extern struct target_type xscale_target
;
89 extern struct target_type cortexm_target
;
90 extern struct target_type cortexa_target
;
91 extern struct target_type cortexr4_target
;
92 extern struct target_type arm11_target
;
93 extern struct target_type ls1_sap_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
;
106 extern struct target_type quark_d20xx_target
;
108 static struct target_type
*target_types
[] = {
141 struct target
*all_targets
;
142 static struct target_event_callback
*target_event_callbacks
;
143 static struct target_timer_callback
*target_timer_callbacks
;
144 LIST_HEAD(target_reset_callback_list
);
145 LIST_HEAD(target_trace_callback_list
);
146 static const int polling_interval
= 100;
148 static const Jim_Nvp nvp_assert
[] = {
149 { .name
= "assert", NVP_ASSERT
},
150 { .name
= "deassert", NVP_DEASSERT
},
151 { .name
= "T", NVP_ASSERT
},
152 { .name
= "F", NVP_DEASSERT
},
153 { .name
= "t", NVP_ASSERT
},
154 { .name
= "f", NVP_DEASSERT
},
155 { .name
= NULL
, .value
= -1 }
158 static const Jim_Nvp nvp_error_target
[] = {
159 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
160 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
161 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
162 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
163 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
164 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
165 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
166 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
167 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
168 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
169 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
170 { .value
= -1, .name
= NULL
}
173 static const char *target_strerror_safe(int err
)
177 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
184 static const Jim_Nvp nvp_target_event
[] = {
186 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
187 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
188 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
189 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
190 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
192 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
193 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
195 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
196 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
197 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
198 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
199 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
200 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
201 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
202 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
203 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
204 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
205 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
206 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
208 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
209 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
211 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
212 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
214 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
215 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
217 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
218 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
220 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
221 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
223 { .value
= TARGET_EVENT_TRACE_CONFIG
, .name
= "trace-config" },
225 { .name
= NULL
, .value
= -1 }
228 static const Jim_Nvp nvp_target_state
[] = {
229 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
230 { .name
= "running", .value
= TARGET_RUNNING
},
231 { .name
= "halted", .value
= TARGET_HALTED
},
232 { .name
= "reset", .value
= TARGET_RESET
},
233 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
234 { .name
= NULL
, .value
= -1 },
237 static const Jim_Nvp nvp_target_debug_reason
[] = {
238 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
239 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
240 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
241 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
242 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
243 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
244 { .name
= "program-exit" , .value
= DBG_REASON_EXIT
},
245 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
246 { .name
= NULL
, .value
= -1 },
249 static const Jim_Nvp nvp_target_endian
[] = {
250 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
251 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
252 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
253 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
254 { .name
= NULL
, .value
= -1 },
257 static const Jim_Nvp nvp_reset_modes
[] = {
258 { .name
= "unknown", .value
= RESET_UNKNOWN
},
259 { .name
= "run" , .value
= RESET_RUN
},
260 { .name
= "halt" , .value
= RESET_HALT
},
261 { .name
= "init" , .value
= RESET_INIT
},
262 { .name
= NULL
, .value
= -1 },
265 const char *debug_reason_name(struct target
*t
)
269 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
270 t
->debug_reason
)->name
;
272 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
273 cp
= "(*BUG*unknown*BUG*)";
278 const char *target_state_name(struct target
*t
)
281 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
283 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
284 cp
= "(*BUG*unknown*BUG*)";
287 if (!target_was_examined(t
) && t
->defer_examine
)
288 cp
= "examine deferred";
293 const char *target_event_name(enum target_event event
)
296 cp
= Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
;
298 LOG_ERROR("Invalid target event: %d", (int)(event
));
299 cp
= "(*BUG*unknown*BUG*)";
304 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
307 cp
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
;
309 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
310 cp
= "(*BUG*unknown*BUG*)";
315 /* determine the number of the new target */
316 static int new_target_number(void)
321 /* number is 0 based */
325 if (x
< t
->target_number
)
326 x
= t
->target_number
;
332 /* read a uint64_t from a buffer in target memory endianness */
333 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
335 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
336 return le_to_h_u64(buffer
);
338 return be_to_h_u64(buffer
);
341 /* read a uint32_t from a buffer in target memory endianness */
342 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
344 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
345 return le_to_h_u32(buffer
);
347 return be_to_h_u32(buffer
);
350 /* read a uint24_t from a buffer in target memory endianness */
351 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
353 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
354 return le_to_h_u24(buffer
);
356 return be_to_h_u24(buffer
);
359 /* read a uint16_t from a buffer in target memory endianness */
360 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
362 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
363 return le_to_h_u16(buffer
);
365 return be_to_h_u16(buffer
);
368 /* read a uint8_t from a buffer in target memory endianness */
369 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
371 return *buffer
& 0x0ff;
374 /* write a uint64_t to a buffer in target memory endianness */
375 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
377 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
378 h_u64_to_le(buffer
, value
);
380 h_u64_to_be(buffer
, value
);
383 /* write a uint32_t to a buffer in target memory endianness */
384 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
386 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
387 h_u32_to_le(buffer
, value
);
389 h_u32_to_be(buffer
, value
);
392 /* write a uint24_t to a buffer in target memory endianness */
393 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
395 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
396 h_u24_to_le(buffer
, value
);
398 h_u24_to_be(buffer
, value
);
401 /* write a uint16_t to a buffer in target memory endianness */
402 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
404 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
405 h_u16_to_le(buffer
, value
);
407 h_u16_to_be(buffer
, value
);
410 /* write a uint8_t to a buffer in target memory endianness */
411 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
416 /* write a uint64_t array to a buffer in target memory endianness */
417 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
420 for (i
= 0; i
< count
; i
++)
421 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
424 /* write a uint32_t array to a buffer in target memory endianness */
425 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
428 for (i
= 0; i
< count
; i
++)
429 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
432 /* write a uint16_t array to a buffer in target memory endianness */
433 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
436 for (i
= 0; i
< count
; i
++)
437 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
440 /* write a uint64_t array to a buffer in target memory endianness */
441 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
444 for (i
= 0; i
< count
; i
++)
445 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
448 /* write a uint32_t array to a buffer in target memory endianness */
449 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
452 for (i
= 0; i
< count
; i
++)
453 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
456 /* write a uint16_t array to a buffer in target memory endianness */
457 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
460 for (i
= 0; i
< count
; i
++)
461 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
464 /* return a pointer to a configured target; id is name or number */
465 struct target
*get_target(const char *id
)
467 struct target
*target
;
469 /* try as tcltarget name */
470 for (target
= all_targets
; target
; target
= target
->next
) {
471 if (target_name(target
) == NULL
)
473 if (strcmp(id
, target_name(target
)) == 0)
477 /* It's OK to remove this fallback sometime after August 2010 or so */
479 /* no match, try as number */
481 if (parse_uint(id
, &num
) != ERROR_OK
)
484 for (target
= all_targets
; target
; target
= target
->next
) {
485 if (target
->target_number
== (int)num
) {
486 LOG_WARNING("use '%s' as target identifier, not '%u'",
487 target_name(target
), num
);
495 /* returns a pointer to the n-th configured target */
496 struct target
*get_target_by_num(int num
)
498 struct target
*target
= all_targets
;
501 if (target
->target_number
== num
)
503 target
= target
->next
;
509 struct target
*get_current_target(struct command_context
*cmd_ctx
)
511 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
513 if (target
== NULL
) {
514 LOG_ERROR("BUG: current_target out of bounds");
521 int target_poll(struct target
*target
)
525 /* We can't poll until after examine */
526 if (!target_was_examined(target
)) {
527 /* Fail silently lest we pollute the log */
531 retval
= target
->type
->poll(target
);
532 if (retval
!= ERROR_OK
)
535 if (target
->halt_issued
) {
536 if (target
->state
== TARGET_HALTED
)
537 target
->halt_issued
= false;
539 int64_t t
= timeval_ms() - target
->halt_issued_time
;
540 if (t
> DEFAULT_HALT_TIMEOUT
) {
541 target
->halt_issued
= false;
542 LOG_INFO("Halt timed out, wake up GDB.");
543 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
551 int target_halt(struct target
*target
)
554 /* We can't poll until after examine */
555 if (!target_was_examined(target
)) {
556 LOG_ERROR("Target not examined yet");
560 retval
= target
->type
->halt(target
);
561 if (retval
!= ERROR_OK
)
564 target
->halt_issued
= true;
565 target
->halt_issued_time
= timeval_ms();
571 * Make the target (re)start executing using its saved execution
572 * context (possibly with some modifications).
574 * @param target Which target should start executing.
575 * @param current True to use the target's saved program counter instead
576 * of the address parameter
577 * @param address Optionally used as the program counter.
578 * @param handle_breakpoints True iff breakpoints at the resumption PC
579 * should be skipped. (For example, maybe execution was stopped by
580 * such a breakpoint, in which case it would be counterprodutive to
582 * @param debug_execution False if all working areas allocated by OpenOCD
583 * should be released and/or restored to their original contents.
584 * (This would for example be true to run some downloaded "helper"
585 * algorithm code, which resides in one such working buffer and uses
586 * another for data storage.)
588 * @todo Resolve the ambiguity about what the "debug_execution" flag
589 * signifies. For example, Target implementations don't agree on how
590 * it relates to invalidation of the register cache, or to whether
591 * breakpoints and watchpoints should be enabled. (It would seem wrong
592 * to enable breakpoints when running downloaded "helper" algorithms
593 * (debug_execution true), since the breakpoints would be set to match
594 * target firmware being debugged, not the helper algorithm.... and
595 * enabling them could cause such helpers to malfunction (for example,
596 * by overwriting data with a breakpoint instruction. On the other
597 * hand the infrastructure for running such helpers might use this
598 * procedure but rely on hardware breakpoint to detect termination.)
600 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
604 /* We can't poll until after examine */
605 if (!target_was_examined(target
)) {
606 LOG_ERROR("Target not examined yet");
610 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
612 /* note that resume *must* be asynchronous. The CPU can halt before
613 * we poll. The CPU can even halt at the current PC as a result of
614 * a software breakpoint being inserted by (a bug?) the application.
616 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
617 if (retval
!= ERROR_OK
)
620 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
625 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
630 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
631 if (n
->name
== NULL
) {
632 LOG_ERROR("invalid reset mode");
636 struct target
*target
;
637 for (target
= all_targets
; target
; target
= target
->next
)
638 target_call_reset_callbacks(target
, reset_mode
);
640 /* disable polling during reset to make reset event scripts
641 * more predictable, i.e. dr/irscan & pathmove in events will
642 * not have JTAG operations injected into the middle of a sequence.
644 bool save_poll
= jtag_poll_get_enabled();
646 jtag_poll_set_enabled(false);
648 sprintf(buf
, "ocd_process_reset %s", n
->name
);
649 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
651 jtag_poll_set_enabled(save_poll
);
653 if (retval
!= JIM_OK
) {
654 Jim_MakeErrorMessage(cmd_ctx
->interp
);
655 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
659 /* We want any events to be processed before the prompt */
660 retval
= target_call_timer_callbacks_now();
662 for (target
= all_targets
; target
; target
= target
->next
) {
663 target
->type
->check_reset(target
);
664 target
->running_alg
= false;
670 static int identity_virt2phys(struct target
*target
,
671 uint32_t virtual, uint32_t *physical
)
677 static int no_mmu(struct target
*target
, int *enabled
)
683 static int default_examine(struct target
*target
)
685 target_set_examined(target
);
689 /* no check by default */
690 static int default_check_reset(struct target
*target
)
695 int target_examine_one(struct target
*target
)
697 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
699 int retval
= target
->type
->examine(target
);
700 if (retval
!= ERROR_OK
)
703 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
708 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
710 struct target
*target
= priv
;
712 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
715 jtag_unregister_event_callback(jtag_enable_callback
, target
);
717 return target_examine_one(target
);
720 /* Targets that correctly implement init + examine, i.e.
721 * no communication with target during init:
725 int target_examine(void)
727 int retval
= ERROR_OK
;
728 struct target
*target
;
730 for (target
= all_targets
; target
; target
= target
->next
) {
731 /* defer examination, but don't skip it */
732 if (!target
->tap
->enabled
) {
733 jtag_register_event_callback(jtag_enable_callback
,
738 if (target
->defer_examine
)
741 retval
= target_examine_one(target
);
742 if (retval
!= ERROR_OK
)
748 const char *target_type_name(struct target
*target
)
750 return target
->type
->name
;
753 static int target_soft_reset_halt(struct target
*target
)
755 if (!target_was_examined(target
)) {
756 LOG_ERROR("Target not examined yet");
759 if (!target
->type
->soft_reset_halt
) {
760 LOG_ERROR("Target %s does not support soft_reset_halt",
761 target_name(target
));
764 return target
->type
->soft_reset_halt(target
);
768 * Downloads a target-specific native code algorithm to the target,
769 * and executes it. * Note that some targets may need to set up, enable,
770 * and tear down a breakpoint (hard or * soft) to detect algorithm
771 * termination, while others may support lower overhead schemes where
772 * soft breakpoints embedded in the algorithm automatically terminate the
775 * @param target used to run the algorithm
776 * @param arch_info target-specific description of the algorithm.
778 int target_run_algorithm(struct target
*target
,
779 int num_mem_params
, struct mem_param
*mem_params
,
780 int num_reg_params
, struct reg_param
*reg_param
,
781 uint32_t entry_point
, uint32_t exit_point
,
782 int timeout_ms
, void *arch_info
)
784 int retval
= ERROR_FAIL
;
786 if (!target_was_examined(target
)) {
787 LOG_ERROR("Target not examined yet");
790 if (!target
->type
->run_algorithm
) {
791 LOG_ERROR("Target type '%s' does not support %s",
792 target_type_name(target
), __func__
);
796 target
->running_alg
= true;
797 retval
= target
->type
->run_algorithm(target
,
798 num_mem_params
, mem_params
,
799 num_reg_params
, reg_param
,
800 entry_point
, exit_point
, timeout_ms
, arch_info
);
801 target
->running_alg
= false;
808 * Downloads a target-specific native code algorithm to the target,
809 * executes and leaves it running.
811 * @param target used to run the algorithm
812 * @param arch_info target-specific description of the algorithm.
814 int target_start_algorithm(struct target
*target
,
815 int num_mem_params
, struct mem_param
*mem_params
,
816 int num_reg_params
, struct reg_param
*reg_params
,
817 uint32_t entry_point
, uint32_t exit_point
,
820 int retval
= ERROR_FAIL
;
822 if (!target_was_examined(target
)) {
823 LOG_ERROR("Target not examined yet");
826 if (!target
->type
->start_algorithm
) {
827 LOG_ERROR("Target type '%s' does not support %s",
828 target_type_name(target
), __func__
);
831 if (target
->running_alg
) {
832 LOG_ERROR("Target is already running an algorithm");
836 target
->running_alg
= true;
837 retval
= target
->type
->start_algorithm(target
,
838 num_mem_params
, mem_params
,
839 num_reg_params
, reg_params
,
840 entry_point
, exit_point
, arch_info
);
847 * Waits for an algorithm started with target_start_algorithm() to complete.
849 * @param target used to run the algorithm
850 * @param arch_info target-specific description of the algorithm.
852 int target_wait_algorithm(struct target
*target
,
853 int num_mem_params
, struct mem_param
*mem_params
,
854 int num_reg_params
, struct reg_param
*reg_params
,
855 uint32_t exit_point
, int timeout_ms
,
858 int retval
= ERROR_FAIL
;
860 if (!target
->type
->wait_algorithm
) {
861 LOG_ERROR("Target type '%s' does not support %s",
862 target_type_name(target
), __func__
);
865 if (!target
->running_alg
) {
866 LOG_ERROR("Target is not running an algorithm");
870 retval
= target
->type
->wait_algorithm(target
,
871 num_mem_params
, mem_params
,
872 num_reg_params
, reg_params
,
873 exit_point
, timeout_ms
, arch_info
);
874 if (retval
!= ERROR_TARGET_TIMEOUT
)
875 target
->running_alg
= false;
882 * Executes a target-specific native code algorithm in the target.
883 * It differs from target_run_algorithm in that the algorithm is asynchronous.
884 * Because of this it requires an compliant algorithm:
885 * see contrib/loaders/flash/stm32f1x.S for example.
887 * @param target used to run the algorithm
890 int target_run_flash_async_algorithm(struct target
*target
,
891 const uint8_t *buffer
, uint32_t count
, int block_size
,
892 int num_mem_params
, struct mem_param
*mem_params
,
893 int num_reg_params
, struct reg_param
*reg_params
,
894 uint32_t buffer_start
, uint32_t buffer_size
,
895 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
900 const uint8_t *buffer_orig
= buffer
;
902 /* Set up working area. First word is write pointer, second word is read pointer,
903 * rest is fifo data area. */
904 uint32_t wp_addr
= buffer_start
;
905 uint32_t rp_addr
= buffer_start
+ 4;
906 uint32_t fifo_start_addr
= buffer_start
+ 8;
907 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
909 uint32_t wp
= fifo_start_addr
;
910 uint32_t rp
= fifo_start_addr
;
912 /* validate block_size is 2^n */
913 assert(!block_size
|| !(block_size
& (block_size
- 1)));
915 retval
= target_write_u32(target
, wp_addr
, wp
);
916 if (retval
!= ERROR_OK
)
918 retval
= target_write_u32(target
, rp_addr
, rp
);
919 if (retval
!= ERROR_OK
)
922 /* Start up algorithm on target and let it idle while writing the first chunk */
923 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
924 num_reg_params
, reg_params
,
929 if (retval
!= ERROR_OK
) {
930 LOG_ERROR("error starting target flash write algorithm");
936 retval
= target_read_u32(target
, rp_addr
, &rp
);
937 if (retval
!= ERROR_OK
) {
938 LOG_ERROR("failed to get read pointer");
942 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
943 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
946 LOG_ERROR("flash write algorithm aborted by target");
947 retval
= ERROR_FLASH_OPERATION_FAILED
;
951 if (((rp
- fifo_start_addr
) & (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
952 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
956 /* Count the number of bytes available in the fifo without
957 * crossing the wrap around. Make sure to not fill it completely,
958 * because that would make wp == rp and that's the empty condition. */
959 uint32_t thisrun_bytes
;
961 thisrun_bytes
= rp
- wp
- block_size
;
962 else if (rp
> fifo_start_addr
)
963 thisrun_bytes
= fifo_end_addr
- wp
;
965 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
967 if (thisrun_bytes
== 0) {
968 /* Throttle polling a bit if transfer is (much) faster than flash
969 * programming. The exact delay shouldn't matter as long as it's
970 * less than buffer size / flash speed. This is very unlikely to
971 * run when using high latency connections such as USB. */
974 /* to stop an infinite loop on some targets check and increment a timeout
975 * this issue was observed on a stellaris using the new ICDI interface */
976 if (timeout
++ >= 500) {
977 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
978 return ERROR_FLASH_OPERATION_FAILED
;
983 /* reset our timeout */
986 /* Limit to the amount of data we actually want to write */
987 if (thisrun_bytes
> count
* block_size
)
988 thisrun_bytes
= count
* block_size
;
990 /* Write data to fifo */
991 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
992 if (retval
!= ERROR_OK
)
995 /* Update counters and wrap write pointer */
996 buffer
+= thisrun_bytes
;
997 count
-= thisrun_bytes
/ block_size
;
999 if (wp
>= fifo_end_addr
)
1000 wp
= fifo_start_addr
;
1002 /* Store updated write pointer to target */
1003 retval
= target_write_u32(target
, wp_addr
, wp
);
1004 if (retval
!= ERROR_OK
)
1008 if (retval
!= ERROR_OK
) {
1009 /* abort flash write algorithm on target */
1010 target_write_u32(target
, wp_addr
, 0);
1013 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1014 num_reg_params
, reg_params
,
1019 if (retval2
!= ERROR_OK
) {
1020 LOG_ERROR("error waiting for target flash write algorithm");
1024 if (retval
== ERROR_OK
) {
1025 /* check if algorithm set rp = 0 after fifo writer loop finished */
1026 retval
= target_read_u32(target
, rp_addr
, &rp
);
1027 if (retval
== ERROR_OK
&& rp
== 0) {
1028 LOG_ERROR("flash write algorithm aborted by target");
1029 retval
= ERROR_FLASH_OPERATION_FAILED
;
1036 int target_read_memory(struct target
*target
,
1037 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1039 if (!target_was_examined(target
)) {
1040 LOG_ERROR("Target not examined yet");
1043 if (!target
->type
->read_memory
) {
1044 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1047 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1050 int target_read_phys_memory(struct target
*target
,
1051 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1053 if (!target_was_examined(target
)) {
1054 LOG_ERROR("Target not examined yet");
1057 if (!target
->type
->read_phys_memory
) {
1058 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1061 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1064 int target_write_memory(struct target
*target
,
1065 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1067 if (!target_was_examined(target
)) {
1068 LOG_ERROR("Target not examined yet");
1071 if (!target
->type
->write_memory
) {
1072 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1075 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1078 int target_write_phys_memory(struct target
*target
,
1079 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1081 if (!target_was_examined(target
)) {
1082 LOG_ERROR("Target not examined yet");
1085 if (!target
->type
->write_phys_memory
) {
1086 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1089 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1092 int target_add_breakpoint(struct target
*target
,
1093 struct breakpoint
*breakpoint
)
1095 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1096 LOG_WARNING("target %s is not halted", target_name(target
));
1097 return ERROR_TARGET_NOT_HALTED
;
1099 return target
->type
->add_breakpoint(target
, breakpoint
);
1102 int target_add_context_breakpoint(struct target
*target
,
1103 struct breakpoint
*breakpoint
)
1105 if (target
->state
!= TARGET_HALTED
) {
1106 LOG_WARNING("target %s is not halted", target_name(target
));
1107 return ERROR_TARGET_NOT_HALTED
;
1109 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1112 int target_add_hybrid_breakpoint(struct target
*target
,
1113 struct breakpoint
*breakpoint
)
1115 if (target
->state
!= TARGET_HALTED
) {
1116 LOG_WARNING("target %s is not halted", target_name(target
));
1117 return ERROR_TARGET_NOT_HALTED
;
1119 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1122 int target_remove_breakpoint(struct target
*target
,
1123 struct breakpoint
*breakpoint
)
1125 return target
->type
->remove_breakpoint(target
, breakpoint
);
1128 int target_add_watchpoint(struct target
*target
,
1129 struct watchpoint
*watchpoint
)
1131 if (target
->state
!= TARGET_HALTED
) {
1132 LOG_WARNING("target %s is not halted", target_name(target
));
1133 return ERROR_TARGET_NOT_HALTED
;
1135 return target
->type
->add_watchpoint(target
, watchpoint
);
1137 int target_remove_watchpoint(struct target
*target
,
1138 struct watchpoint
*watchpoint
)
1140 return target
->type
->remove_watchpoint(target
, watchpoint
);
1142 int target_hit_watchpoint(struct target
*target
,
1143 struct watchpoint
**hit_watchpoint
)
1145 if (target
->state
!= TARGET_HALTED
) {
1146 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1147 return ERROR_TARGET_NOT_HALTED
;
1150 if (target
->type
->hit_watchpoint
== NULL
) {
1151 /* For backward compatible, if hit_watchpoint is not implemented,
1152 * return ERROR_FAIL such that gdb_server will not take the nonsense
1157 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1160 int target_get_gdb_reg_list(struct target
*target
,
1161 struct reg
**reg_list
[], int *reg_list_size
,
1162 enum target_register_class reg_class
)
1164 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1166 int target_step(struct target
*target
,
1167 int current
, uint32_t address
, int handle_breakpoints
)
1169 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1172 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1174 if (target
->state
!= TARGET_HALTED
) {
1175 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1176 return ERROR_TARGET_NOT_HALTED
;
1178 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1181 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1183 if (target
->state
!= TARGET_HALTED
) {
1184 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1185 return ERROR_TARGET_NOT_HALTED
;
1187 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1190 int target_profiling(struct target
*target
, uint32_t *samples
,
1191 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1193 if (target
->state
!= TARGET_HALTED
) {
1194 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1195 return ERROR_TARGET_NOT_HALTED
;
1197 return target
->type
->profiling(target
, samples
, max_num_samples
,
1198 num_samples
, seconds
);
1202 * Reset the @c examined flag for the given target.
1203 * Pure paranoia -- targets are zeroed on allocation.
1205 static void target_reset_examined(struct target
*target
)
1207 target
->examined
= false;
1210 static int handle_target(void *priv
);
1212 static int target_init_one(struct command_context
*cmd_ctx
,
1213 struct target
*target
)
1215 target_reset_examined(target
);
1217 struct target_type
*type
= target
->type
;
1218 if (type
->examine
== NULL
)
1219 type
->examine
= default_examine
;
1221 if (type
->check_reset
== NULL
)
1222 type
->check_reset
= default_check_reset
;
1224 assert(type
->init_target
!= NULL
);
1226 int retval
= type
->init_target(cmd_ctx
, target
);
1227 if (ERROR_OK
!= retval
) {
1228 LOG_ERROR("target '%s' init failed", target_name(target
));
1232 /* Sanity-check MMU support ... stub in what we must, to help
1233 * implement it in stages, but warn if we need to do so.
1236 if (type
->virt2phys
== NULL
) {
1237 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1238 type
->virt2phys
= identity_virt2phys
;
1241 /* Make sure no-MMU targets all behave the same: make no
1242 * distinction between physical and virtual addresses, and
1243 * ensure that virt2phys() is always an identity mapping.
1245 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1246 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1249 type
->write_phys_memory
= type
->write_memory
;
1250 type
->read_phys_memory
= type
->read_memory
;
1251 type
->virt2phys
= identity_virt2phys
;
1254 if (target
->type
->read_buffer
== NULL
)
1255 target
->type
->read_buffer
= target_read_buffer_default
;
1257 if (target
->type
->write_buffer
== NULL
)
1258 target
->type
->write_buffer
= target_write_buffer_default
;
1260 if (target
->type
->get_gdb_fileio_info
== NULL
)
1261 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1263 if (target
->type
->gdb_fileio_end
== NULL
)
1264 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1266 if (target
->type
->profiling
== NULL
)
1267 target
->type
->profiling
= target_profiling_default
;
1272 static int target_init(struct command_context
*cmd_ctx
)
1274 struct target
*target
;
1277 for (target
= all_targets
; target
; target
= target
->next
) {
1278 retval
= target_init_one(cmd_ctx
, target
);
1279 if (ERROR_OK
!= retval
)
1286 retval
= target_register_user_commands(cmd_ctx
);
1287 if (ERROR_OK
!= retval
)
1290 retval
= target_register_timer_callback(&handle_target
,
1291 polling_interval
, 1, cmd_ctx
->interp
);
1292 if (ERROR_OK
!= retval
)
1298 COMMAND_HANDLER(handle_target_init_command
)
1303 return ERROR_COMMAND_SYNTAX_ERROR
;
1305 static bool target_initialized
;
1306 if (target_initialized
) {
1307 LOG_INFO("'target init' has already been called");
1310 target_initialized
= true;
1312 retval
= command_run_line(CMD_CTX
, "init_targets");
1313 if (ERROR_OK
!= retval
)
1316 retval
= command_run_line(CMD_CTX
, "init_target_events");
1317 if (ERROR_OK
!= retval
)
1320 retval
= command_run_line(CMD_CTX
, "init_board");
1321 if (ERROR_OK
!= retval
)
1324 LOG_DEBUG("Initializing targets...");
1325 return target_init(CMD_CTX
);
1328 int target_register_event_callback(int (*callback
)(struct target
*target
,
1329 enum target_event event
, void *priv
), void *priv
)
1331 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1333 if (callback
== NULL
)
1334 return ERROR_COMMAND_SYNTAX_ERROR
;
1337 while ((*callbacks_p
)->next
)
1338 callbacks_p
= &((*callbacks_p
)->next
);
1339 callbacks_p
= &((*callbacks_p
)->next
);
1342 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1343 (*callbacks_p
)->callback
= callback
;
1344 (*callbacks_p
)->priv
= priv
;
1345 (*callbacks_p
)->next
= NULL
;
1350 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1351 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1353 struct target_reset_callback
*entry
;
1355 if (callback
== NULL
)
1356 return ERROR_COMMAND_SYNTAX_ERROR
;
1358 entry
= malloc(sizeof(struct target_reset_callback
));
1359 if (entry
== NULL
) {
1360 LOG_ERROR("error allocating buffer for reset callback entry");
1361 return ERROR_COMMAND_SYNTAX_ERROR
;
1364 entry
->callback
= callback
;
1366 list_add(&entry
->list
, &target_reset_callback_list
);
1372 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1373 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1375 struct target_trace_callback
*entry
;
1377 if (callback
== NULL
)
1378 return ERROR_COMMAND_SYNTAX_ERROR
;
1380 entry
= malloc(sizeof(struct target_trace_callback
));
1381 if (entry
== NULL
) {
1382 LOG_ERROR("error allocating buffer for trace callback entry");
1383 return ERROR_COMMAND_SYNTAX_ERROR
;
1386 entry
->callback
= callback
;
1388 list_add(&entry
->list
, &target_trace_callback_list
);
1394 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1396 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1399 if (callback
== NULL
)
1400 return ERROR_COMMAND_SYNTAX_ERROR
;
1403 while ((*callbacks_p
)->next
)
1404 callbacks_p
= &((*callbacks_p
)->next
);
1405 callbacks_p
= &((*callbacks_p
)->next
);
1408 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1409 (*callbacks_p
)->callback
= callback
;
1410 (*callbacks_p
)->periodic
= periodic
;
1411 (*callbacks_p
)->time_ms
= time_ms
;
1412 (*callbacks_p
)->removed
= false;
1414 gettimeofday(&now
, NULL
);
1415 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
1416 time_ms
-= (time_ms
% 1000);
1417 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
1418 if ((*callbacks_p
)->when
.tv_usec
> 1000000) {
1419 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
1420 (*callbacks_p
)->when
.tv_sec
+= 1;
1423 (*callbacks_p
)->priv
= priv
;
1424 (*callbacks_p
)->next
= NULL
;
1429 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1430 enum target_event event
, void *priv
), void *priv
)
1432 struct target_event_callback
**p
= &target_event_callbacks
;
1433 struct target_event_callback
*c
= target_event_callbacks
;
1435 if (callback
== NULL
)
1436 return ERROR_COMMAND_SYNTAX_ERROR
;
1439 struct target_event_callback
*next
= c
->next
;
1440 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1452 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1453 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1455 struct target_reset_callback
*entry
;
1457 if (callback
== NULL
)
1458 return ERROR_COMMAND_SYNTAX_ERROR
;
1460 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1461 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1462 list_del(&entry
->list
);
1471 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1472 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1474 struct target_trace_callback
*entry
;
1476 if (callback
== NULL
)
1477 return ERROR_COMMAND_SYNTAX_ERROR
;
1479 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1480 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1481 list_del(&entry
->list
);
1490 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1492 if (callback
== NULL
)
1493 return ERROR_COMMAND_SYNTAX_ERROR
;
1495 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1497 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1506 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1508 struct target_event_callback
*callback
= target_event_callbacks
;
1509 struct target_event_callback
*next_callback
;
1511 if (event
== TARGET_EVENT_HALTED
) {
1512 /* execute early halted first */
1513 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1516 LOG_DEBUG("target event %i (%s)", event
,
1517 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1519 target_handle_event(target
, event
);
1522 next_callback
= callback
->next
;
1523 callback
->callback(target
, event
, callback
->priv
);
1524 callback
= next_callback
;
1530 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1532 struct target_reset_callback
*callback
;
1534 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1535 Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1537 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1538 callback
->callback(target
, reset_mode
, callback
->priv
);
1543 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1545 struct target_trace_callback
*callback
;
1547 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1548 callback
->callback(target
, len
, data
, callback
->priv
);
1553 static int target_timer_callback_periodic_restart(
1554 struct target_timer_callback
*cb
, struct timeval
*now
)
1556 int time_ms
= cb
->time_ms
;
1557 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1558 time_ms
-= (time_ms
% 1000);
1559 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1560 if (cb
->when
.tv_usec
> 1000000) {
1561 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1562 cb
->when
.tv_sec
+= 1;
1567 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1568 struct timeval
*now
)
1570 cb
->callback(cb
->priv
);
1573 return target_timer_callback_periodic_restart(cb
, now
);
1575 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1578 static int target_call_timer_callbacks_check_time(int checktime
)
1580 static bool callback_processing
;
1582 /* Do not allow nesting */
1583 if (callback_processing
)
1586 callback_processing
= true;
1591 gettimeofday(&now
, NULL
);
1593 /* Store an address of the place containing a pointer to the
1594 * next item; initially, that's a standalone "root of the
1595 * list" variable. */
1596 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1598 if ((*callback
)->removed
) {
1599 struct target_timer_callback
*p
= *callback
;
1600 *callback
= (*callback
)->next
;
1605 bool call_it
= (*callback
)->callback
&&
1606 ((!checktime
&& (*callback
)->periodic
) ||
1607 now
.tv_sec
> (*callback
)->when
.tv_sec
||
1608 (now
.tv_sec
== (*callback
)->when
.tv_sec
&&
1609 now
.tv_usec
>= (*callback
)->when
.tv_usec
));
1612 target_call_timer_callback(*callback
, &now
);
1614 callback
= &(*callback
)->next
;
1617 callback_processing
= false;
1621 int target_call_timer_callbacks(void)
1623 return target_call_timer_callbacks_check_time(1);
1626 /* invoke periodic callbacks immediately */
1627 int target_call_timer_callbacks_now(void)
1629 return target_call_timer_callbacks_check_time(0);
1632 /* Prints the working area layout for debug purposes */
1633 static void print_wa_layout(struct target
*target
)
1635 struct working_area
*c
= target
->working_areas
;
1638 LOG_DEBUG("%c%c 0x%08"PRIx32
"-0x%08"PRIx32
" (%"PRIu32
" bytes)",
1639 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1640 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1645 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1646 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1648 assert(area
->free
); /* Shouldn't split an allocated area */
1649 assert(size
<= area
->size
); /* Caller should guarantee this */
1651 /* Split only if not already the right size */
1652 if (size
< area
->size
) {
1653 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1658 new_wa
->next
= area
->next
;
1659 new_wa
->size
= area
->size
- size
;
1660 new_wa
->address
= area
->address
+ size
;
1661 new_wa
->backup
= NULL
;
1662 new_wa
->user
= NULL
;
1663 new_wa
->free
= true;
1665 area
->next
= new_wa
;
1668 /* If backup memory was allocated to this area, it has the wrong size
1669 * now so free it and it will be reallocated if/when needed */
1672 area
->backup
= NULL
;
1677 /* Merge all adjacent free areas into one */
1678 static void target_merge_working_areas(struct target
*target
)
1680 struct working_area
*c
= target
->working_areas
;
1682 while (c
&& c
->next
) {
1683 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1685 /* Find two adjacent free areas */
1686 if (c
->free
&& c
->next
->free
) {
1687 /* Merge the last into the first */
1688 c
->size
+= c
->next
->size
;
1690 /* Remove the last */
1691 struct working_area
*to_be_freed
= c
->next
;
1692 c
->next
= c
->next
->next
;
1693 if (to_be_freed
->backup
)
1694 free(to_be_freed
->backup
);
1697 /* If backup memory was allocated to the remaining area, it's has
1698 * the wrong size now */
1709 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1711 /* Reevaluate working area address based on MMU state*/
1712 if (target
->working_areas
== NULL
) {
1716 retval
= target
->type
->mmu(target
, &enabled
);
1717 if (retval
!= ERROR_OK
)
1721 if (target
->working_area_phys_spec
) {
1722 LOG_DEBUG("MMU disabled, using physical "
1723 "address for working memory 0x%08"PRIx32
,
1724 target
->working_area_phys
);
1725 target
->working_area
= target
->working_area_phys
;
1727 LOG_ERROR("No working memory available. "
1728 "Specify -work-area-phys to target.");
1729 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1732 if (target
->working_area_virt_spec
) {
1733 LOG_DEBUG("MMU enabled, using virtual "
1734 "address for working memory 0x%08"PRIx32
,
1735 target
->working_area_virt
);
1736 target
->working_area
= target
->working_area_virt
;
1738 LOG_ERROR("No working memory available. "
1739 "Specify -work-area-virt to target.");
1740 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1744 /* Set up initial working area on first call */
1745 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1747 new_wa
->next
= NULL
;
1748 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1749 new_wa
->address
= target
->working_area
;
1750 new_wa
->backup
= NULL
;
1751 new_wa
->user
= NULL
;
1752 new_wa
->free
= true;
1755 target
->working_areas
= new_wa
;
1758 /* only allocate multiples of 4 byte */
1760 size
= (size
+ 3) & (~3UL);
1762 struct working_area
*c
= target
->working_areas
;
1764 /* Find the first large enough working area */
1766 if (c
->free
&& c
->size
>= size
)
1772 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1774 /* Split the working area into the requested size */
1775 target_split_working_area(c
, size
);
1777 LOG_DEBUG("allocated new working area of %"PRIu32
" bytes at address 0x%08"PRIx32
, size
, c
->address
);
1779 if (target
->backup_working_area
) {
1780 if (c
->backup
== NULL
) {
1781 c
->backup
= malloc(c
->size
);
1782 if (c
->backup
== NULL
)
1786 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1787 if (retval
!= ERROR_OK
)
1791 /* mark as used, and return the new (reused) area */
1798 print_wa_layout(target
);
1803 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1807 retval
= target_alloc_working_area_try(target
, size
, area
);
1808 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1809 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1814 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1816 int retval
= ERROR_OK
;
1818 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1819 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1820 if (retval
!= ERROR_OK
)
1821 LOG_ERROR("failed to restore %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1822 area
->size
, area
->address
);
1828 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1829 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1831 int retval
= ERROR_OK
;
1837 retval
= target_restore_working_area(target
, area
);
1838 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1839 if (retval
!= ERROR_OK
)
1845 LOG_DEBUG("freed %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1846 area
->size
, area
->address
);
1848 /* mark user pointer invalid */
1849 /* TODO: Is this really safe? It points to some previous caller's memory.
1850 * How could we know that the area pointer is still in that place and not
1851 * some other vital data? What's the purpose of this, anyway? */
1855 target_merge_working_areas(target
);
1857 print_wa_layout(target
);
1862 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1864 return target_free_working_area_restore(target
, area
, 1);
1867 void target_quit(void)
1869 struct target_event_callback
*pe
= target_event_callbacks
;
1871 struct target_event_callback
*t
= pe
->next
;
1875 target_event_callbacks
= NULL
;
1877 struct target_timer_callback
*pt
= target_timer_callbacks
;
1879 struct target_timer_callback
*t
= pt
->next
;
1883 target_timer_callbacks
= NULL
;
1885 for (struct target
*target
= all_targets
;
1886 target
; target
= target
->next
) {
1887 if (target
->type
->deinit_target
)
1888 target
->type
->deinit_target(target
);
1892 /* free resources and restore memory, if restoring memory fails,
1893 * free up resources anyway
1895 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1897 struct working_area
*c
= target
->working_areas
;
1899 LOG_DEBUG("freeing all working areas");
1901 /* Loop through all areas, restoring the allocated ones and marking them as free */
1905 target_restore_working_area(target
, c
);
1907 *c
->user
= NULL
; /* Same as above */
1913 /* Run a merge pass to combine all areas into one */
1914 target_merge_working_areas(target
);
1916 print_wa_layout(target
);
1919 void target_free_all_working_areas(struct target
*target
)
1921 target_free_all_working_areas_restore(target
, 1);
1924 /* Find the largest number of bytes that can be allocated */
1925 uint32_t target_get_working_area_avail(struct target
*target
)
1927 struct working_area
*c
= target
->working_areas
;
1928 uint32_t max_size
= 0;
1931 return target
->working_area_size
;
1934 if (c
->free
&& max_size
< c
->size
)
1943 int target_arch_state(struct target
*target
)
1946 if (target
== NULL
) {
1947 LOG_USER("No target has been configured");
1951 LOG_USER("%s: target state: %s", target_name(target
),
1952 target_state_name(target
));
1954 if (target
->state
!= TARGET_HALTED
)
1957 retval
= target
->type
->arch_state(target
);
1961 static int target_get_gdb_fileio_info_default(struct target
*target
,
1962 struct gdb_fileio_info
*fileio_info
)
1964 /* If target does not support semi-hosting function, target
1965 has no need to provide .get_gdb_fileio_info callback.
1966 It just return ERROR_FAIL and gdb_server will return "Txx"
1967 as target halted every time. */
1971 static int target_gdb_fileio_end_default(struct target
*target
,
1972 int retcode
, int fileio_errno
, bool ctrl_c
)
1977 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
1978 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1980 struct timeval timeout
, now
;
1982 gettimeofday(&timeout
, NULL
);
1983 timeval_add_time(&timeout
, seconds
, 0);
1985 LOG_INFO("Starting profiling. Halting and resuming the"
1986 " target as often as we can...");
1988 uint32_t sample_count
= 0;
1989 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
1990 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
1992 int retval
= ERROR_OK
;
1994 target_poll(target
);
1995 if (target
->state
== TARGET_HALTED
) {
1996 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
1997 samples
[sample_count
++] = t
;
1998 /* current pc, addr = 0, do not handle breakpoints, not debugging */
1999 retval
= target_resume(target
, 1, 0, 0, 0);
2000 target_poll(target
);
2001 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2002 } else if (target
->state
== TARGET_RUNNING
) {
2003 /* We want to quickly sample the PC. */
2004 retval
= target_halt(target
);
2006 LOG_INFO("Target not halted or running");
2011 if (retval
!= ERROR_OK
)
2014 gettimeofday(&now
, NULL
);
2015 if ((sample_count
>= max_num_samples
) ||
2016 ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
))) {
2017 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2022 *num_samples
= sample_count
;
2026 /* Single aligned words are guaranteed to use 16 or 32 bit access
2027 * mode respectively, otherwise data is handled as quickly as
2030 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
2032 LOG_DEBUG("writing buffer of %" PRIi32
" byte at 0x%8.8" PRIx32
,
2035 if (!target_was_examined(target
)) {
2036 LOG_ERROR("Target not examined yet");
2043 if ((address
+ size
- 1) < address
) {
2044 /* GDB can request this when e.g. PC is 0xfffffffc */
2045 LOG_ERROR("address + size wrapped (0x%08" PRIx32
", 0x%08" PRIx32
")",
2051 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2054 static int target_write_buffer_default(struct target
*target
, uint32_t address
, uint32_t count
, const uint8_t *buffer
)
2058 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2059 * will have something to do with the size we leave to it. */
2060 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2061 if (address
& size
) {
2062 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2063 if (retval
!= ERROR_OK
)
2071 /* Write the data with as large access size as possible. */
2072 for (; size
> 0; size
/= 2) {
2073 uint32_t aligned
= count
- count
% size
;
2075 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2076 if (retval
!= ERROR_OK
)
2087 /* Single aligned words are guaranteed to use 16 or 32 bit access
2088 * mode respectively, otherwise data is handled as quickly as
2091 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
2093 LOG_DEBUG("reading buffer of %" PRIi32
" byte at 0x%8.8" PRIx32
,
2096 if (!target_was_examined(target
)) {
2097 LOG_ERROR("Target not examined yet");
2104 if ((address
+ size
- 1) < address
) {
2105 /* GDB can request this when e.g. PC is 0xfffffffc */
2106 LOG_ERROR("address + size wrapped (0x%08" PRIx32
", 0x%08" PRIx32
")",
2112 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2115 static int target_read_buffer_default(struct target
*target
, uint32_t address
, uint32_t count
, uint8_t *buffer
)
2119 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2120 * will have something to do with the size we leave to it. */
2121 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2122 if (address
& size
) {
2123 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2124 if (retval
!= ERROR_OK
)
2132 /* Read the data with as large access size as possible. */
2133 for (; size
> 0; size
/= 2) {
2134 uint32_t aligned
= count
- count
% size
;
2136 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2137 if (retval
!= ERROR_OK
)
2148 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
2153 uint32_t checksum
= 0;
2154 if (!target_was_examined(target
)) {
2155 LOG_ERROR("Target not examined yet");
2159 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2160 if (retval
!= ERROR_OK
) {
2161 buffer
= malloc(size
);
2162 if (buffer
== NULL
) {
2163 LOG_ERROR("error allocating buffer for section (%" PRId32
" bytes)", size
);
2164 return ERROR_COMMAND_SYNTAX_ERROR
;
2166 retval
= target_read_buffer(target
, address
, size
, buffer
);
2167 if (retval
!= ERROR_OK
) {
2172 /* convert to target endianness */
2173 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2174 uint32_t target_data
;
2175 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2176 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2179 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2188 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
,
2189 uint8_t erased_value
)
2192 if (!target_was_examined(target
)) {
2193 LOG_ERROR("Target not examined yet");
2197 if (target
->type
->blank_check_memory
== 0)
2198 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2200 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
, erased_value
);
2205 int target_read_u64(struct target
*target
, uint64_t address
, uint64_t *value
)
2207 uint8_t value_buf
[8];
2208 if (!target_was_examined(target
)) {
2209 LOG_ERROR("Target not examined yet");
2213 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2215 if (retval
== ERROR_OK
) {
2216 *value
= target_buffer_get_u64(target
, value_buf
);
2217 LOG_DEBUG("address: 0x%" PRIx64
", value: 0x%16.16" PRIx64
"",
2222 LOG_DEBUG("address: 0x%" PRIx64
" failed",
2229 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
2231 uint8_t value_buf
[4];
2232 if (!target_was_examined(target
)) {
2233 LOG_ERROR("Target not examined yet");
2237 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2239 if (retval
== ERROR_OK
) {
2240 *value
= target_buffer_get_u32(target
, value_buf
);
2241 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
2246 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2253 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
2255 uint8_t value_buf
[2];
2256 if (!target_was_examined(target
)) {
2257 LOG_ERROR("Target not examined yet");
2261 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2263 if (retval
== ERROR_OK
) {
2264 *value
= target_buffer_get_u16(target
, value_buf
);
2265 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4" PRIx16
,
2270 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2277 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
2279 if (!target_was_examined(target
)) {
2280 LOG_ERROR("Target not examined yet");
2284 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2286 if (retval
== ERROR_OK
) {
2287 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2" PRIx8
,
2292 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2299 int target_write_u64(struct target
*target
, uint64_t address
, uint64_t value
)
2302 uint8_t value_buf
[8];
2303 if (!target_was_examined(target
)) {
2304 LOG_ERROR("Target not examined yet");
2308 LOG_DEBUG("address: 0x%" PRIx64
", value: 0x%16.16" PRIx64
"",
2312 target_buffer_set_u64(target
, value_buf
, value
);
2313 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2314 if (retval
!= ERROR_OK
)
2315 LOG_DEBUG("failed: %i", retval
);
2320 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
2323 uint8_t value_buf
[4];
2324 if (!target_was_examined(target
)) {
2325 LOG_ERROR("Target not examined yet");
2329 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
2333 target_buffer_set_u32(target
, value_buf
, value
);
2334 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2335 if (retval
!= ERROR_OK
)
2336 LOG_DEBUG("failed: %i", retval
);
2341 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
2344 uint8_t value_buf
[2];
2345 if (!target_was_examined(target
)) {
2346 LOG_ERROR("Target not examined yet");
2350 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx16
,
2354 target_buffer_set_u16(target
, value_buf
, value
);
2355 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2356 if (retval
!= ERROR_OK
)
2357 LOG_DEBUG("failed: %i", retval
);
2362 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
2365 if (!target_was_examined(target
)) {
2366 LOG_ERROR("Target not examined yet");
2370 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2" PRIx8
,
2373 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2374 if (retval
!= ERROR_OK
)
2375 LOG_DEBUG("failed: %i", retval
);
2380 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2382 struct target
*target
= get_target(name
);
2383 if (target
== NULL
) {
2384 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2387 if (!target
->tap
->enabled
) {
2388 LOG_USER("Target: TAP %s is disabled, "
2389 "can't be the current target\n",
2390 target
->tap
->dotted_name
);
2394 cmd_ctx
->current_target
= target
->target_number
;
2399 COMMAND_HANDLER(handle_targets_command
)
2401 int retval
= ERROR_OK
;
2402 if (CMD_ARGC
== 1) {
2403 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2404 if (retval
== ERROR_OK
) {
2410 struct target
*target
= all_targets
;
2411 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2412 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2417 if (target
->tap
->enabled
)
2418 state
= target_state_name(target
);
2420 state
= "tap-disabled";
2422 if (CMD_CTX
->current_target
== target
->target_number
)
2425 /* keep columns lined up to match the headers above */
2426 command_print(CMD_CTX
,
2427 "%2d%c %-18s %-10s %-6s %-18s %s",
2428 target
->target_number
,
2430 target_name(target
),
2431 target_type_name(target
),
2432 Jim_Nvp_value2name_simple(nvp_target_endian
,
2433 target
->endianness
)->name
,
2434 target
->tap
->dotted_name
,
2436 target
= target
->next
;
2442 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2444 static int powerDropout
;
2445 static int srstAsserted
;
2447 static int runPowerRestore
;
2448 static int runPowerDropout
;
2449 static int runSrstAsserted
;
2450 static int runSrstDeasserted
;
2452 static int sense_handler(void)
2454 static int prevSrstAsserted
;
2455 static int prevPowerdropout
;
2457 int retval
= jtag_power_dropout(&powerDropout
);
2458 if (retval
!= ERROR_OK
)
2462 powerRestored
= prevPowerdropout
&& !powerDropout
;
2464 runPowerRestore
= 1;
2466 int64_t current
= timeval_ms();
2467 static int64_t lastPower
;
2468 bool waitMore
= lastPower
+ 2000 > current
;
2469 if (powerDropout
&& !waitMore
) {
2470 runPowerDropout
= 1;
2471 lastPower
= current
;
2474 retval
= jtag_srst_asserted(&srstAsserted
);
2475 if (retval
!= ERROR_OK
)
2479 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2481 static int64_t lastSrst
;
2482 waitMore
= lastSrst
+ 2000 > current
;
2483 if (srstDeasserted
&& !waitMore
) {
2484 runSrstDeasserted
= 1;
2488 if (!prevSrstAsserted
&& srstAsserted
)
2489 runSrstAsserted
= 1;
2491 prevSrstAsserted
= srstAsserted
;
2492 prevPowerdropout
= powerDropout
;
2494 if (srstDeasserted
|| powerRestored
) {
2495 /* Other than logging the event we can't do anything here.
2496 * Issuing a reset is a particularly bad idea as we might
2497 * be inside a reset already.
2504 /* process target state changes */
2505 static int handle_target(void *priv
)
2507 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2508 int retval
= ERROR_OK
;
2510 if (!is_jtag_poll_safe()) {
2511 /* polling is disabled currently */
2515 /* we do not want to recurse here... */
2516 static int recursive
;
2520 /* danger! running these procedures can trigger srst assertions and power dropouts.
2521 * We need to avoid an infinite loop/recursion here and we do that by
2522 * clearing the flags after running these events.
2524 int did_something
= 0;
2525 if (runSrstAsserted
) {
2526 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2527 Jim_Eval(interp
, "srst_asserted");
2530 if (runSrstDeasserted
) {
2531 Jim_Eval(interp
, "srst_deasserted");
2534 if (runPowerDropout
) {
2535 LOG_INFO("Power dropout detected, running power_dropout proc.");
2536 Jim_Eval(interp
, "power_dropout");
2539 if (runPowerRestore
) {
2540 Jim_Eval(interp
, "power_restore");
2544 if (did_something
) {
2545 /* clear detect flags */
2549 /* clear action flags */
2551 runSrstAsserted
= 0;
2552 runSrstDeasserted
= 0;
2553 runPowerRestore
= 0;
2554 runPowerDropout
= 0;
2559 /* Poll targets for state changes unless that's globally disabled.
2560 * Skip targets that are currently disabled.
2562 for (struct target
*target
= all_targets
;
2563 is_jtag_poll_safe() && target
;
2564 target
= target
->next
) {
2566 if (!target_was_examined(target
))
2569 if (!target
->tap
->enabled
)
2572 if (target
->backoff
.times
> target
->backoff
.count
) {
2573 /* do not poll this time as we failed previously */
2574 target
->backoff
.count
++;
2577 target
->backoff
.count
= 0;
2579 /* only poll target if we've got power and srst isn't asserted */
2580 if (!powerDropout
&& !srstAsserted
) {
2581 /* polling may fail silently until the target has been examined */
2582 retval
= target_poll(target
);
2583 if (retval
!= ERROR_OK
) {
2584 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2585 if (target
->backoff
.times
* polling_interval
< 5000) {
2586 target
->backoff
.times
*= 2;
2587 target
->backoff
.times
++;
2590 /* Tell GDB to halt the debugger. This allows the user to
2591 * run monitor commands to handle the situation.
2593 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2595 if (target
->backoff
.times
> 0) {
2596 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
2597 target_reset_examined(target
);
2598 retval
= target_examine_one(target
);
2599 /* Target examination could have failed due to unstable connection,
2600 * but we set the examined flag anyway to repoll it later */
2601 if (retval
!= ERROR_OK
) {
2602 target
->examined
= true;
2603 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2604 target
->backoff
.times
* polling_interval
);
2609 /* Since we succeeded, we reset backoff count */
2610 target
->backoff
.times
= 0;
2617 COMMAND_HANDLER(handle_reg_command
)
2619 struct target
*target
;
2620 struct reg
*reg
= NULL
;
2626 target
= get_current_target(CMD_CTX
);
2628 /* list all available registers for the current target */
2629 if (CMD_ARGC
== 0) {
2630 struct reg_cache
*cache
= target
->reg_cache
;
2636 command_print(CMD_CTX
, "===== %s", cache
->name
);
2638 for (i
= 0, reg
= cache
->reg_list
;
2639 i
< cache
->num_regs
;
2640 i
++, reg
++, count
++) {
2641 /* only print cached values if they are valid */
2643 value
= buf_to_str(reg
->value
,
2645 command_print(CMD_CTX
,
2646 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2654 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2659 cache
= cache
->next
;
2665 /* access a single register by its ordinal number */
2666 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2668 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2670 struct reg_cache
*cache
= target
->reg_cache
;
2674 for (i
= 0; i
< cache
->num_regs
; i
++) {
2675 if (count
++ == num
) {
2676 reg
= &cache
->reg_list
[i
];
2682 cache
= cache
->next
;
2686 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2687 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2691 /* access a single register by its name */
2692 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2695 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2700 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2702 /* display a register */
2703 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2704 && (CMD_ARGV
[1][0] <= '9')))) {
2705 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2708 if (reg
->valid
== 0)
2709 reg
->type
->get(reg
);
2710 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2711 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2716 /* set register value */
2717 if (CMD_ARGC
== 2) {
2718 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2721 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2723 reg
->type
->set(reg
, buf
);
2725 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2726 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2734 return ERROR_COMMAND_SYNTAX_ERROR
;
2737 COMMAND_HANDLER(handle_poll_command
)
2739 int retval
= ERROR_OK
;
2740 struct target
*target
= get_current_target(CMD_CTX
);
2742 if (CMD_ARGC
== 0) {
2743 command_print(CMD_CTX
, "background polling: %s",
2744 jtag_poll_get_enabled() ? "on" : "off");
2745 command_print(CMD_CTX
, "TAP: %s (%s)",
2746 target
->tap
->dotted_name
,
2747 target
->tap
->enabled
? "enabled" : "disabled");
2748 if (!target
->tap
->enabled
)
2750 retval
= target_poll(target
);
2751 if (retval
!= ERROR_OK
)
2753 retval
= target_arch_state(target
);
2754 if (retval
!= ERROR_OK
)
2756 } else if (CMD_ARGC
== 1) {
2758 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2759 jtag_poll_set_enabled(enable
);
2761 return ERROR_COMMAND_SYNTAX_ERROR
;
2766 COMMAND_HANDLER(handle_wait_halt_command
)
2769 return ERROR_COMMAND_SYNTAX_ERROR
;
2771 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2772 if (1 == CMD_ARGC
) {
2773 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2774 if (ERROR_OK
!= retval
)
2775 return ERROR_COMMAND_SYNTAX_ERROR
;
2778 struct target
*target
= get_current_target(CMD_CTX
);
2779 return target_wait_state(target
, TARGET_HALTED
, ms
);
2782 /* wait for target state to change. The trick here is to have a low
2783 * latency for short waits and not to suck up all the CPU time
2786 * After 500ms, keep_alive() is invoked
2788 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2791 int64_t then
= 0, cur
;
2795 retval
= target_poll(target
);
2796 if (retval
!= ERROR_OK
)
2798 if (target
->state
== state
)
2803 then
= timeval_ms();
2804 LOG_DEBUG("waiting for target %s...",
2805 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2811 if ((cur
-then
) > ms
) {
2812 LOG_ERROR("timed out while waiting for target %s",
2813 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2821 COMMAND_HANDLER(handle_halt_command
)
2825 struct target
*target
= get_current_target(CMD_CTX
);
2826 int retval
= target_halt(target
);
2827 if (ERROR_OK
!= retval
)
2830 if (CMD_ARGC
== 1) {
2831 unsigned wait_local
;
2832 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2833 if (ERROR_OK
!= retval
)
2834 return ERROR_COMMAND_SYNTAX_ERROR
;
2839 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2842 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2844 struct target
*target
= get_current_target(CMD_CTX
);
2846 LOG_USER("requesting target halt and executing a soft reset");
2848 target_soft_reset_halt(target
);
2853 COMMAND_HANDLER(handle_reset_command
)
2856 return ERROR_COMMAND_SYNTAX_ERROR
;
2858 enum target_reset_mode reset_mode
= RESET_RUN
;
2859 if (CMD_ARGC
== 1) {
2861 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2862 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
2863 return ERROR_COMMAND_SYNTAX_ERROR
;
2864 reset_mode
= n
->value
;
2867 /* reset *all* targets */
2868 return target_process_reset(CMD_CTX
, reset_mode
);
2872 COMMAND_HANDLER(handle_resume_command
)
2876 return ERROR_COMMAND_SYNTAX_ERROR
;
2878 struct target
*target
= get_current_target(CMD_CTX
);
2880 /* with no CMD_ARGV, resume from current pc, addr = 0,
2881 * with one arguments, addr = CMD_ARGV[0],
2882 * handle breakpoints, not debugging */
2884 if (CMD_ARGC
== 1) {
2885 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2889 return target_resume(target
, current
, addr
, 1, 0);
2892 COMMAND_HANDLER(handle_step_command
)
2895 return ERROR_COMMAND_SYNTAX_ERROR
;
2899 /* with no CMD_ARGV, step from current pc, addr = 0,
2900 * with one argument addr = CMD_ARGV[0],
2901 * handle breakpoints, debugging */
2904 if (CMD_ARGC
== 1) {
2905 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2909 struct target
*target
= get_current_target(CMD_CTX
);
2911 return target
->type
->step(target
, current_pc
, addr
, 1);
2914 static void handle_md_output(struct command_context
*cmd_ctx
,
2915 struct target
*target
, uint32_t address
, unsigned size
,
2916 unsigned count
, const uint8_t *buffer
)
2918 const unsigned line_bytecnt
= 32;
2919 unsigned line_modulo
= line_bytecnt
/ size
;
2921 char output
[line_bytecnt
* 4 + 1];
2922 unsigned output_len
= 0;
2924 const char *value_fmt
;
2927 value_fmt
= "%8.8x ";
2930 value_fmt
= "%4.4x ";
2933 value_fmt
= "%2.2x ";
2936 /* "can't happen", caller checked */
2937 LOG_ERROR("invalid memory read size: %u", size
);
2941 for (unsigned i
= 0; i
< count
; i
++) {
2942 if (i
% line_modulo
== 0) {
2943 output_len
+= snprintf(output
+ output_len
,
2944 sizeof(output
) - output_len
,
2946 (unsigned)(address
+ (i
*size
)));
2950 const uint8_t *value_ptr
= buffer
+ i
* size
;
2953 value
= target_buffer_get_u32(target
, value_ptr
);
2956 value
= target_buffer_get_u16(target
, value_ptr
);
2961 output_len
+= snprintf(output
+ output_len
,
2962 sizeof(output
) - output_len
,
2965 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
2966 command_print(cmd_ctx
, "%s", output
);
2972 COMMAND_HANDLER(handle_md_command
)
2975 return ERROR_COMMAND_SYNTAX_ERROR
;
2978 switch (CMD_NAME
[2]) {
2989 return ERROR_COMMAND_SYNTAX_ERROR
;
2992 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2993 int (*fn
)(struct target
*target
,
2994 uint32_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
2998 fn
= target_read_phys_memory
;
3000 fn
= target_read_memory
;
3001 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
3002 return ERROR_COMMAND_SYNTAX_ERROR
;
3005 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
3009 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
3011 uint8_t *buffer
= calloc(count
, size
);
3013 struct target
*target
= get_current_target(CMD_CTX
);
3014 int retval
= fn(target
, address
, size
, count
, buffer
);
3015 if (ERROR_OK
== retval
)
3016 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
3023 typedef int (*target_write_fn
)(struct target
*target
,
3024 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
3026 static int target_fill_mem(struct target
*target
,
3035 /* We have to write in reasonably large chunks to be able
3036 * to fill large memory areas with any sane speed */
3037 const unsigned chunk_size
= 16384;
3038 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
3039 if (target_buf
== NULL
) {
3040 LOG_ERROR("Out of memory");
3044 for (unsigned i
= 0; i
< chunk_size
; i
++) {
3045 switch (data_size
) {
3047 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
3050 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
3053 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
3060 int retval
= ERROR_OK
;
3062 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
3065 if (current
> chunk_size
)
3066 current
= chunk_size
;
3067 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
3068 if (retval
!= ERROR_OK
)
3070 /* avoid GDB timeouts */
3079 COMMAND_HANDLER(handle_mw_command
)
3082 return ERROR_COMMAND_SYNTAX_ERROR
;
3083 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3088 fn
= target_write_phys_memory
;
3090 fn
= target_write_memory
;
3091 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
3092 return ERROR_COMMAND_SYNTAX_ERROR
;
3095 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
3098 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
3102 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
3104 struct target
*target
= get_current_target(CMD_CTX
);
3106 switch (CMD_NAME
[2]) {
3117 return ERROR_COMMAND_SYNTAX_ERROR
;
3120 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
3123 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
3124 uint32_t *min_address
, uint32_t *max_address
)
3126 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
3127 return ERROR_COMMAND_SYNTAX_ERROR
;
3129 /* a base address isn't always necessary,
3130 * default to 0x0 (i.e. don't relocate) */
3131 if (CMD_ARGC
>= 2) {
3133 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
3134 image
->base_address
= addr
;
3135 image
->base_address_set
= 1;
3137 image
->base_address_set
= 0;
3139 image
->start_address_set
= 0;
3142 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
3143 if (CMD_ARGC
== 5) {
3144 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
3145 /* use size (given) to find max (required) */
3146 *max_address
+= *min_address
;
3149 if (*min_address
> *max_address
)
3150 return ERROR_COMMAND_SYNTAX_ERROR
;
3155 COMMAND_HANDLER(handle_load_image_command
)
3159 uint32_t image_size
;
3160 uint32_t min_address
= 0;
3161 uint32_t max_address
= 0xffffffff;
3165 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
3166 &image
, &min_address
, &max_address
);
3167 if (ERROR_OK
!= retval
)
3170 struct target
*target
= get_current_target(CMD_CTX
);
3172 struct duration bench
;
3173 duration_start(&bench
);
3175 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
3180 for (i
= 0; i
< image
.num_sections
; i
++) {
3181 buffer
= malloc(image
.sections
[i
].size
);
3182 if (buffer
== NULL
) {
3183 command_print(CMD_CTX
,
3184 "error allocating buffer for section (%d bytes)",
3185 (int)(image
.sections
[i
].size
));
3186 retval
= ERROR_FAIL
;
3190 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3191 if (retval
!= ERROR_OK
) {
3196 uint32_t offset
= 0;
3197 uint32_t length
= buf_cnt
;
3199 /* DANGER!!! beware of unsigned comparision here!!! */
3201 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3202 (image
.sections
[i
].base_address
< max_address
)) {
3204 if (image
.sections
[i
].base_address
< min_address
) {
3205 /* clip addresses below */
3206 offset
+= min_address
-image
.sections
[i
].base_address
;
3210 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3211 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3213 retval
= target_write_buffer(target
,
3214 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3215 if (retval
!= ERROR_OK
) {
3219 image_size
+= length
;
3220 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
3221 (unsigned int)length
,
3222 image
.sections
[i
].base_address
+ offset
);
3228 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3229 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
3230 "in %fs (%0.3f KiB/s)", image_size
,
3231 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3234 image_close(&image
);
3240 COMMAND_HANDLER(handle_dump_image_command
)
3242 struct fileio
*fileio
;
3244 int retval
, retvaltemp
;
3245 uint32_t address
, size
;
3246 struct duration bench
;
3247 struct target
*target
= get_current_target(CMD_CTX
);
3250 return ERROR_COMMAND_SYNTAX_ERROR
;
3252 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
3253 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
3255 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
3256 buffer
= malloc(buf_size
);
3260 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3261 if (retval
!= ERROR_OK
) {
3266 duration_start(&bench
);
3269 size_t size_written
;
3270 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
3271 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
3272 if (retval
!= ERROR_OK
)
3275 retval
= fileio_write(fileio
, this_run_size
, buffer
, &size_written
);
3276 if (retval
!= ERROR_OK
)
3279 size
-= this_run_size
;
3280 address
+= this_run_size
;
3285 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3287 retval
= fileio_size(fileio
, &filesize
);
3288 if (retval
!= ERROR_OK
)
3290 command_print(CMD_CTX
,
3291 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize
,
3292 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3295 retvaltemp
= fileio_close(fileio
);
3296 if (retvaltemp
!= ERROR_OK
)
3302 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
3306 uint32_t image_size
;
3309 uint32_t checksum
= 0;
3310 uint32_t mem_checksum
= 0;
3314 struct target
*target
= get_current_target(CMD_CTX
);
3317 return ERROR_COMMAND_SYNTAX_ERROR
;
3320 LOG_ERROR("no target selected");
3324 struct duration bench
;
3325 duration_start(&bench
);
3327 if (CMD_ARGC
>= 2) {
3329 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
3330 image
.base_address
= addr
;
3331 image
.base_address_set
= 1;
3333 image
.base_address_set
= 0;
3334 image
.base_address
= 0x0;
3337 image
.start_address_set
= 0;
3339 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3340 if (retval
!= ERROR_OK
)
3346 for (i
= 0; i
< image
.num_sections
; i
++) {
3347 buffer
= malloc(image
.sections
[i
].size
);
3348 if (buffer
== NULL
) {
3349 command_print(CMD_CTX
,
3350 "error allocating buffer for section (%d bytes)",
3351 (int)(image
.sections
[i
].size
));
3354 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3355 if (retval
!= ERROR_OK
) {
3361 /* calculate checksum of image */
3362 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3363 if (retval
!= ERROR_OK
) {
3368 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3369 if (retval
!= ERROR_OK
) {
3374 if (checksum
!= mem_checksum
) {
3375 /* failed crc checksum, fall back to a binary compare */
3379 LOG_ERROR("checksum mismatch - attempting binary compare");
3381 data
= malloc(buf_cnt
);
3383 /* Can we use 32bit word accesses? */
3385 int count
= buf_cnt
;
3386 if ((count
% 4) == 0) {
3390 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
3391 if (retval
== ERROR_OK
) {
3393 for (t
= 0; t
< buf_cnt
; t
++) {
3394 if (data
[t
] != buffer
[t
]) {
3395 command_print(CMD_CTX
,
3396 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3398 (unsigned)(t
+ image
.sections
[i
].base_address
),
3401 if (diffs
++ >= 127) {
3402 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
3414 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
3415 image
.sections
[i
].base_address
,
3420 image_size
+= buf_cnt
;
3423 command_print(CMD_CTX
, "No more differences found.");
3426 retval
= ERROR_FAIL
;
3427 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3428 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3429 "in %fs (%0.3f KiB/s)", image_size
,
3430 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3433 image_close(&image
);
3438 COMMAND_HANDLER(handle_verify_image_command
)
3440 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
3443 COMMAND_HANDLER(handle_test_image_command
)
3445 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
3448 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
3450 struct target
*target
= get_current_target(cmd_ctx
);
3451 struct breakpoint
*breakpoint
= target
->breakpoints
;
3452 while (breakpoint
) {
3453 if (breakpoint
->type
== BKPT_SOFT
) {
3454 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3455 breakpoint
->length
, 16);
3456 command_print(cmd_ctx
, "IVA breakpoint: 0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
3457 breakpoint
->address
,
3459 breakpoint
->set
, buf
);
3462 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3463 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3465 breakpoint
->length
, breakpoint
->set
);
3466 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3467 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3468 breakpoint
->address
,
3469 breakpoint
->length
, breakpoint
->set
);
3470 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3473 command_print(cmd_ctx
, "Breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3474 breakpoint
->address
,
3475 breakpoint
->length
, breakpoint
->set
);
3478 breakpoint
= breakpoint
->next
;
3483 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
3484 uint32_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3486 struct target
*target
= get_current_target(cmd_ctx
);
3490 retval
= breakpoint_add(target
, addr
, length
, hw
);
3491 if (ERROR_OK
== retval
)
3492 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
3494 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3497 } else if (addr
== 0) {
3498 if (target
->type
->add_context_breakpoint
== NULL
) {
3499 LOG_WARNING("Context breakpoint not available");
3502 retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3503 if (ERROR_OK
== retval
)
3504 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3506 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3510 if (target
->type
->add_hybrid_breakpoint
== NULL
) {
3511 LOG_WARNING("Hybrid breakpoint not available");
3514 retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3515 if (ERROR_OK
== retval
)
3516 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3518 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3525 COMMAND_HANDLER(handle_bp_command
)
3534 return handle_bp_command_list(CMD_CTX
);
3538 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3539 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3540 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3543 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
3545 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3547 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3550 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3551 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
3553 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
3554 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3556 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3561 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3562 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
3563 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
3564 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3567 return ERROR_COMMAND_SYNTAX_ERROR
;
3571 COMMAND_HANDLER(handle_rbp_command
)
3574 return ERROR_COMMAND_SYNTAX_ERROR
;
3577 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3579 struct target
*target
= get_current_target(CMD_CTX
);
3580 breakpoint_remove(target
, addr
);
3585 COMMAND_HANDLER(handle_wp_command
)
3587 struct target
*target
= get_current_target(CMD_CTX
);
3589 if (CMD_ARGC
== 0) {
3590 struct watchpoint
*watchpoint
= target
->watchpoints
;
3592 while (watchpoint
) {
3593 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
3594 ", len: 0x%8.8" PRIx32
3595 ", r/w/a: %i, value: 0x%8.8" PRIx32
3596 ", mask: 0x%8.8" PRIx32
,
3597 watchpoint
->address
,
3599 (int)watchpoint
->rw
,
3602 watchpoint
= watchpoint
->next
;
3607 enum watchpoint_rw type
= WPT_ACCESS
;
3609 uint32_t length
= 0;
3610 uint32_t data_value
= 0x0;
3611 uint32_t data_mask
= 0xffffffff;
3615 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
3618 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3621 switch (CMD_ARGV
[2][0]) {
3632 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3633 return ERROR_COMMAND_SYNTAX_ERROR
;
3637 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3638 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3642 return ERROR_COMMAND_SYNTAX_ERROR
;
3645 int retval
= watchpoint_add(target
, addr
, length
, type
,
3646 data_value
, data_mask
);
3647 if (ERROR_OK
!= retval
)
3648 LOG_ERROR("Failure setting watchpoints");
3653 COMMAND_HANDLER(handle_rwp_command
)
3656 return ERROR_COMMAND_SYNTAX_ERROR
;
3659 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3661 struct target
*target
= get_current_target(CMD_CTX
);
3662 watchpoint_remove(target
, addr
);
3668 * Translate a virtual address to a physical address.
3670 * The low-level target implementation must have logged a detailed error
3671 * which is forwarded to telnet/GDB session.
3673 COMMAND_HANDLER(handle_virt2phys_command
)
3676 return ERROR_COMMAND_SYNTAX_ERROR
;
3679 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
3682 struct target
*target
= get_current_target(CMD_CTX
);
3683 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3684 if (retval
== ERROR_OK
)
3685 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
3690 static void writeData(FILE *f
, const void *data
, size_t len
)
3692 size_t written
= fwrite(data
, 1, len
, f
);
3694 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3697 static void writeLong(FILE *f
, int l
, struct target
*target
)
3701 target_buffer_set_u32(target
, val
, l
);
3702 writeData(f
, val
, 4);
3705 static void writeString(FILE *f
, char *s
)
3707 writeData(f
, s
, strlen(s
));
3710 typedef unsigned char UNIT
[2]; /* unit of profiling */
3712 /* Dump a gmon.out histogram file. */
3713 static void write_gmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
, bool with_range
,
3714 uint32_t start_address
, uint32_t end_address
, struct target
*target
)
3717 FILE *f
= fopen(filename
, "w");
3720 writeString(f
, "gmon");
3721 writeLong(f
, 0x00000001, target
); /* Version */
3722 writeLong(f
, 0, target
); /* padding */
3723 writeLong(f
, 0, target
); /* padding */
3724 writeLong(f
, 0, target
); /* padding */
3726 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3727 writeData(f
, &zero
, 1);
3729 /* figure out bucket size */
3733 min
= start_address
;
3738 for (i
= 0; i
< sampleNum
; i
++) {
3739 if (min
> samples
[i
])
3741 if (max
< samples
[i
])
3745 /* max should be (largest sample + 1)
3746 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
3750 int addressSpace
= max
- min
;
3751 assert(addressSpace
>= 2);
3753 /* FIXME: What is the reasonable number of buckets?
3754 * The profiling result will be more accurate if there are enough buckets. */
3755 static const uint32_t maxBuckets
= 128 * 1024; /* maximum buckets. */
3756 uint32_t numBuckets
= addressSpace
/ sizeof(UNIT
);
3757 if (numBuckets
> maxBuckets
)
3758 numBuckets
= maxBuckets
;
3759 int *buckets
= malloc(sizeof(int) * numBuckets
);
3760 if (buckets
== NULL
) {
3764 memset(buckets
, 0, sizeof(int) * numBuckets
);
3765 for (i
= 0; i
< sampleNum
; i
++) {
3766 uint32_t address
= samples
[i
];
3768 if ((address
< min
) || (max
<= address
))
3771 long long a
= address
- min
;
3772 long long b
= numBuckets
;
3773 long long c
= addressSpace
;
3774 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
3778 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3779 writeLong(f
, min
, target
); /* low_pc */
3780 writeLong(f
, max
, target
); /* high_pc */
3781 writeLong(f
, numBuckets
, target
); /* # of buckets */
3782 writeLong(f
, 100, target
); /* KLUDGE! We lie, ca. 100Hz best case. */
3783 writeString(f
, "seconds");
3784 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3785 writeData(f
, &zero
, 1);
3786 writeString(f
, "s");
3788 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3790 char *data
= malloc(2 * numBuckets
);
3792 for (i
= 0; i
< numBuckets
; i
++) {
3797 data
[i
* 2] = val
&0xff;
3798 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
3801 writeData(f
, data
, numBuckets
* 2);
3809 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3810 * which will be used as a random sampling of PC */
3811 COMMAND_HANDLER(handle_profile_command
)
3813 struct target
*target
= get_current_target(CMD_CTX
);
3815 if ((CMD_ARGC
!= 2) && (CMD_ARGC
!= 4))
3816 return ERROR_COMMAND_SYNTAX_ERROR
;
3818 const uint32_t MAX_PROFILE_SAMPLE_NUM
= 10000;
3820 uint32_t num_of_samples
;
3821 int retval
= ERROR_OK
;
3823 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], offset
);
3825 uint32_t *samples
= malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM
);
3826 if (samples
== NULL
) {
3827 LOG_ERROR("No memory to store samples.");
3832 * Some cores let us sample the PC without the
3833 * annoying halt/resume step; for example, ARMv7 PCSR.
3834 * Provide a way to use that more efficient mechanism.
3836 retval
= target_profiling(target
, samples
, MAX_PROFILE_SAMPLE_NUM
,
3837 &num_of_samples
, offset
);
3838 if (retval
!= ERROR_OK
) {
3843 assert(num_of_samples
<= MAX_PROFILE_SAMPLE_NUM
);
3845 retval
= target_poll(target
);
3846 if (retval
!= ERROR_OK
) {
3850 if (target
->state
== TARGET_RUNNING
) {
3851 retval
= target_halt(target
);
3852 if (retval
!= ERROR_OK
) {
3858 retval
= target_poll(target
);
3859 if (retval
!= ERROR_OK
) {
3864 uint32_t start_address
= 0;
3865 uint32_t end_address
= 0;
3866 bool with_range
= false;
3867 if (CMD_ARGC
== 4) {
3869 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], start_address
);
3870 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], end_address
);
3873 write_gmon(samples
, num_of_samples
, CMD_ARGV
[1],
3874 with_range
, start_address
, end_address
, target
);
3875 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3881 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
3884 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3887 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3891 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3892 valObjPtr
= Jim_NewIntObj(interp
, val
);
3893 if (!nameObjPtr
|| !valObjPtr
) {
3898 Jim_IncrRefCount(nameObjPtr
);
3899 Jim_IncrRefCount(valObjPtr
);
3900 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3901 Jim_DecrRefCount(interp
, nameObjPtr
);
3902 Jim_DecrRefCount(interp
, valObjPtr
);
3904 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3908 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3910 struct command_context
*context
;
3911 struct target
*target
;
3913 context
= current_command_context(interp
);
3914 assert(context
!= NULL
);
3916 target
= get_current_target(context
);
3917 if (target
== NULL
) {
3918 LOG_ERROR("mem2array: no current target");
3922 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
3925 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3933 const char *varname
;
3939 /* argv[1] = name of array to receive the data
3940 * argv[2] = desired width
3941 * argv[3] = memory address
3942 * argv[4] = count of times to read
3944 if (argc
< 4 || argc
> 5) {
3945 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems [phys]");
3948 varname
= Jim_GetString(argv
[0], &len
);
3949 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3951 e
= Jim_GetLong(interp
, argv
[1], &l
);
3956 e
= Jim_GetLong(interp
, argv
[2], &l
);
3960 e
= Jim_GetLong(interp
, argv
[3], &l
);
3966 phys
= Jim_GetString(argv
[4], &n
);
3967 if (!strncmp(phys
, "phys", n
))
3983 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3984 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3988 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3989 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3992 if ((addr
+ (len
* width
)) < addr
) {
3993 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3994 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3997 /* absurd transfer size? */
3999 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4000 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
4005 ((width
== 2) && ((addr
& 1) == 0)) ||
4006 ((width
== 4) && ((addr
& 3) == 0))) {
4010 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4011 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
4014 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4023 size_t buffersize
= 4096;
4024 uint8_t *buffer
= malloc(buffersize
);
4031 /* Slurp... in buffer size chunks */
4033 count
= len
; /* in objects.. */
4034 if (count
> (buffersize
/ width
))
4035 count
= (buffersize
/ width
);
4038 retval
= target_read_phys_memory(target
, addr
, width
, count
, buffer
);
4040 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
4041 if (retval
!= ERROR_OK
) {
4043 LOG_ERROR("mem2array: Read @ 0x%08" PRIx32
", w=%" PRId32
", cnt=%" PRId32
", failed",
4047 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4048 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
4052 v
= 0; /* shut up gcc */
4053 for (i
= 0; i
< count
; i
++, n
++) {
4056 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
4059 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
4062 v
= buffer
[i
] & 0x0ff;
4065 new_int_array_element(interp
, varname
, n
, v
);
4068 addr
+= count
* width
;
4074 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4079 static int get_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t *val
)
4082 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4086 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4090 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4096 Jim_IncrRefCount(nameObjPtr
);
4097 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
4098 Jim_DecrRefCount(interp
, nameObjPtr
);
4100 if (valObjPtr
== NULL
)
4103 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
4104 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
4109 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4111 struct command_context
*context
;
4112 struct target
*target
;
4114 context
= current_command_context(interp
);
4115 assert(context
!= NULL
);
4117 target
= get_current_target(context
);
4118 if (target
== NULL
) {
4119 LOG_ERROR("array2mem: no current target");
4123 return target_array2mem(interp
, target
, argc
-1, argv
+ 1);
4126 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
4127 int argc
, Jim_Obj
*const *argv
)
4135 const char *varname
;
4141 /* argv[1] = name of array to get the data
4142 * argv[2] = desired width
4143 * argv[3] = memory address
4144 * argv[4] = count to write
4146 if (argc
< 4 || argc
> 5) {
4147 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4150 varname
= Jim_GetString(argv
[0], &len
);
4151 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4153 e
= Jim_GetLong(interp
, argv
[1], &l
);
4158 e
= Jim_GetLong(interp
, argv
[2], &l
);
4162 e
= Jim_GetLong(interp
, argv
[3], &l
);
4168 phys
= Jim_GetString(argv
[4], &n
);
4169 if (!strncmp(phys
, "phys", n
))
4185 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4186 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4187 "Invalid width param, must be 8/16/32", NULL
);
4191 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4192 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4193 "array2mem: zero width read?", NULL
);
4196 if ((addr
+ (len
* width
)) < addr
) {
4197 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4198 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4199 "array2mem: addr + len - wraps to zero?", NULL
);
4202 /* absurd transfer size? */
4204 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4205 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4206 "array2mem: absurd > 64K item request", NULL
);
4211 ((width
== 2) && ((addr
& 1) == 0)) ||
4212 ((width
== 4) && ((addr
& 3) == 0))) {
4216 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4217 sprintf(buf
, "array2mem address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
4220 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4231 size_t buffersize
= 4096;
4232 uint8_t *buffer
= malloc(buffersize
);
4237 /* Slurp... in buffer size chunks */
4239 count
= len
; /* in objects.. */
4240 if (count
> (buffersize
/ width
))
4241 count
= (buffersize
/ width
);
4243 v
= 0; /* shut up gcc */
4244 for (i
= 0; i
< count
; i
++, n
++) {
4245 get_int_array_element(interp
, varname
, n
, &v
);
4248 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
4251 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
4254 buffer
[i
] = v
& 0x0ff;
4261 retval
= target_write_phys_memory(target
, addr
, width
, count
, buffer
);
4263 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
4264 if (retval
!= ERROR_OK
) {
4266 LOG_ERROR("array2mem: Write @ 0x%08" PRIx32
", w=%" PRId32
", cnt=%" PRId32
", failed",
4270 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4271 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
4275 addr
+= count
* width
;
4280 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4285 /* FIX? should we propagate errors here rather than printing them
4288 void target_handle_event(struct target
*target
, enum target_event e
)
4290 struct target_event_action
*teap
;
4292 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4293 if (teap
->event
== e
) {
4294 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
4295 target
->target_number
,
4296 target_name(target
),
4297 target_type_name(target
),
4299 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
4300 Jim_GetString(teap
->body
, NULL
));
4301 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
) {
4302 Jim_MakeErrorMessage(teap
->interp
);
4303 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
4310 * Returns true only if the target has a handler for the specified event.
4312 bool target_has_event_action(struct target
*target
, enum target_event event
)
4314 struct target_event_action
*teap
;
4316 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4317 if (teap
->event
== event
)
4323 enum target_cfg_param
{
4326 TCFG_WORK_AREA_VIRT
,
4327 TCFG_WORK_AREA_PHYS
,
4328 TCFG_WORK_AREA_SIZE
,
4329 TCFG_WORK_AREA_BACKUP
,
4332 TCFG_CHAIN_POSITION
,
4338 static Jim_Nvp nvp_config_opts
[] = {
4339 { .name
= "-type", .value
= TCFG_TYPE
},
4340 { .name
= "-event", .value
= TCFG_EVENT
},
4341 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
4342 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
4343 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
4344 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
4345 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
4346 { .name
= "-coreid", .value
= TCFG_COREID
},
4347 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
4348 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
4349 { .name
= "-rtos", .value
= TCFG_RTOS
},
4350 { .name
= "-defer-examine", .value
= TCFG_DEFER_EXAMINE
},
4351 { .name
= NULL
, .value
= -1 }
4354 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
4361 /* parse config or cget options ... */
4362 while (goi
->argc
> 0) {
4363 Jim_SetEmptyResult(goi
->interp
);
4364 /* Jim_GetOpt_Debug(goi); */
4366 if (target
->type
->target_jim_configure
) {
4367 /* target defines a configure function */
4368 /* target gets first dibs on parameters */
4369 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
4378 /* otherwise we 'continue' below */
4380 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
4382 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
4388 if (goi
->isconfigure
) {
4389 Jim_SetResultFormatted(goi
->interp
,
4390 "not settable: %s", n
->name
);
4394 if (goi
->argc
!= 0) {
4395 Jim_WrongNumArgs(goi
->interp
,
4396 goi
->argc
, goi
->argv
,
4401 Jim_SetResultString(goi
->interp
,
4402 target_type_name(target
), -1);
4406 if (goi
->argc
== 0) {
4407 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
4411 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
4413 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
4417 if (goi
->isconfigure
) {
4418 if (goi
->argc
!= 1) {
4419 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
4423 if (goi
->argc
!= 0) {
4424 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
4430 struct target_event_action
*teap
;
4432 teap
= target
->event_action
;
4433 /* replace existing? */
4435 if (teap
->event
== (enum target_event
)n
->value
)
4440 if (goi
->isconfigure
) {
4441 bool replace
= true;
4444 teap
= calloc(1, sizeof(*teap
));
4447 teap
->event
= n
->value
;
4448 teap
->interp
= goi
->interp
;
4449 Jim_GetOpt_Obj(goi
, &o
);
4451 Jim_DecrRefCount(teap
->interp
, teap
->body
);
4452 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
4455 * Tcl/TK - "tk events" have a nice feature.
4456 * See the "BIND" command.
4457 * We should support that here.
4458 * You can specify %X and %Y in the event code.
4459 * The idea is: %T - target name.
4460 * The idea is: %N - target number
4461 * The idea is: %E - event name.
4463 Jim_IncrRefCount(teap
->body
);
4466 /* add to head of event list */
4467 teap
->next
= target
->event_action
;
4468 target
->event_action
= teap
;
4470 Jim_SetEmptyResult(goi
->interp
);
4474 Jim_SetEmptyResult(goi
->interp
);
4476 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
4482 case TCFG_WORK_AREA_VIRT
:
4483 if (goi
->isconfigure
) {
4484 target_free_all_working_areas(target
);
4485 e
= Jim_GetOpt_Wide(goi
, &w
);
4488 target
->working_area_virt
= w
;
4489 target
->working_area_virt_spec
= true;
4494 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
4498 case TCFG_WORK_AREA_PHYS
:
4499 if (goi
->isconfigure
) {
4500 target_free_all_working_areas(target
);
4501 e
= Jim_GetOpt_Wide(goi
, &w
);
4504 target
->working_area_phys
= w
;
4505 target
->working_area_phys_spec
= true;
4510 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
4514 case TCFG_WORK_AREA_SIZE
:
4515 if (goi
->isconfigure
) {
4516 target_free_all_working_areas(target
);
4517 e
= Jim_GetOpt_Wide(goi
, &w
);
4520 target
->working_area_size
= w
;
4525 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4529 case TCFG_WORK_AREA_BACKUP
:
4530 if (goi
->isconfigure
) {
4531 target_free_all_working_areas(target
);
4532 e
= Jim_GetOpt_Wide(goi
, &w
);
4535 /* make this exactly 1 or 0 */
4536 target
->backup_working_area
= (!!w
);
4541 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
4542 /* loop for more e*/
4547 if (goi
->isconfigure
) {
4548 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
4550 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
4553 target
->endianness
= n
->value
;
4558 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4559 if (n
->name
== NULL
) {
4560 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4561 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4563 Jim_SetResultString(goi
->interp
, n
->name
, -1);
4568 if (goi
->isconfigure
) {
4569 e
= Jim_GetOpt_Wide(goi
, &w
);
4572 target
->coreid
= (int32_t)w
;
4577 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4581 case TCFG_CHAIN_POSITION
:
4582 if (goi
->isconfigure
) {
4584 struct jtag_tap
*tap
;
4585 target_free_all_working_areas(target
);
4586 e
= Jim_GetOpt_Obj(goi
, &o_t
);
4589 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
4592 /* make this exactly 1 or 0 */
4598 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
4599 /* loop for more e*/
4602 if (goi
->isconfigure
) {
4603 e
= Jim_GetOpt_Wide(goi
, &w
);
4606 target
->dbgbase
= (uint32_t)w
;
4607 target
->dbgbase_set
= true;
4612 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
4619 int result
= rtos_create(goi
, target
);
4620 if (result
!= JIM_OK
)
4626 case TCFG_DEFER_EXAMINE
:
4628 target
->defer_examine
= true;
4633 } /* while (goi->argc) */
4636 /* done - we return */
4640 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4644 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4645 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4647 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4648 "missing: -option ...");
4651 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4652 return target_configure(&goi
, target
);
4655 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4657 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4660 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4662 if (goi
.argc
< 2 || goi
.argc
> 4) {
4663 Jim_SetResultFormatted(goi
.interp
,
4664 "usage: %s [phys] <address> <data> [<count>]", cmd_name
);
4669 fn
= target_write_memory
;
4672 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4674 struct Jim_Obj
*obj
;
4675 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4679 fn
= target_write_phys_memory
;
4683 e
= Jim_GetOpt_Wide(&goi
, &a
);
4688 e
= Jim_GetOpt_Wide(&goi
, &b
);
4693 if (goi
.argc
== 1) {
4694 e
= Jim_GetOpt_Wide(&goi
, &c
);
4699 /* all args must be consumed */
4703 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4705 if (strcasecmp(cmd_name
, "mww") == 0)
4707 else if (strcasecmp(cmd_name
, "mwh") == 0)
4709 else if (strcasecmp(cmd_name
, "mwb") == 0)
4712 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4716 return (target_fill_mem(target
, a
, fn
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4720 * @brief Reads an array of words/halfwords/bytes from target memory starting at specified address.
4722 * Usage: mdw [phys] <address> [<count>] - for 32 bit reads
4723 * mdh [phys] <address> [<count>] - for 16 bit reads
4724 * mdb [phys] <address> [<count>] - for 8 bit reads
4726 * Count defaults to 1.
4728 * Calls target_read_memory or target_read_phys_memory depending on
4729 * the presence of the "phys" argument
4730 * Reads the target memory in blocks of max. 32 bytes, and returns an array of ints formatted
4731 * to int representation in base16.
4732 * Also outputs read data in a human readable form using command_print
4734 * @param phys if present target_read_phys_memory will be used instead of target_read_memory
4735 * @param address address where to start the read. May be specified in decimal or hex using the standard "0x" prefix
4736 * @param count optional count parameter to read an array of values. If not specified, defaults to 1.
4737 * @returns: JIM_ERR on error or JIM_OK on success and sets the result string to an array of ascii formatted numbers
4738 * on success, with [<count>] number of elements.
4740 * In case of little endian target:
4741 * Example1: "mdw 0x00000000" returns "10123456"
4742 * Exmaple2: "mdh 0x00000000 1" returns "3456"
4743 * Example3: "mdb 0x00000000" returns "56"
4744 * Example4: "mdh 0x00000000 2" returns "3456 1012"
4745 * Example5: "mdb 0x00000000 3" returns "56 34 12"
4747 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4749 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4752 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4754 if ((goi
.argc
< 1) || (goi
.argc
> 3)) {
4755 Jim_SetResultFormatted(goi
.interp
,
4756 "usage: %s [phys] <address> [<count>]", cmd_name
);
4760 int (*fn
)(struct target
*target
,
4761 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
4762 fn
= target_read_memory
;
4765 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4767 struct Jim_Obj
*obj
;
4768 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4772 fn
= target_read_phys_memory
;
4775 /* Read address parameter */
4777 e
= Jim_GetOpt_Wide(&goi
, &addr
);
4781 /* If next parameter exists, read it out as the count parameter, if not, set it to 1 (default) */
4783 if (goi
.argc
== 1) {
4784 e
= Jim_GetOpt_Wide(&goi
, &count
);
4790 /* all args must be consumed */
4794 jim_wide dwidth
= 1; /* shut up gcc */
4795 if (strcasecmp(cmd_name
, "mdw") == 0)
4797 else if (strcasecmp(cmd_name
, "mdh") == 0)
4799 else if (strcasecmp(cmd_name
, "mdb") == 0)
4802 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4806 /* convert count to "bytes" */
4807 int bytes
= count
* dwidth
;
4809 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4810 uint8_t target_buf
[32];
4813 y
= (bytes
< 16) ? bytes
: 16; /* y = min(bytes, 16); */
4815 /* Try to read out next block */
4816 e
= fn(target
, addr
, dwidth
, y
/ dwidth
, target_buf
);
4818 if (e
!= ERROR_OK
) {
4819 Jim_SetResultFormatted(interp
, "error reading target @ 0x%08lx", (long)addr
);
4823 command_print_sameline(NULL
, "0x%08x ", (int)(addr
));
4826 for (x
= 0; x
< 16 && x
< y
; x
+= 4) {
4827 z
= target_buffer_get_u32(target
, &(target_buf
[x
]));
4828 command_print_sameline(NULL
, "%08x ", (int)(z
));
4830 for (; (x
< 16) ; x
+= 4)
4831 command_print_sameline(NULL
, " ");
4834 for (x
= 0; x
< 16 && x
< y
; x
+= 2) {
4835 z
= target_buffer_get_u16(target
, &(target_buf
[x
]));
4836 command_print_sameline(NULL
, "%04x ", (int)(z
));
4838 for (; (x
< 16) ; x
+= 2)
4839 command_print_sameline(NULL
, " ");
4843 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
4844 z
= target_buffer_get_u8(target
, &(target_buf
[x
]));
4845 command_print_sameline(NULL
, "%02x ", (int)(z
));
4847 for (; (x
< 16) ; x
+= 1)
4848 command_print_sameline(NULL
, " ");
4851 /* ascii-ify the bytes */
4852 for (x
= 0 ; x
< y
; x
++) {
4853 if ((target_buf
[x
] >= 0x20) &&
4854 (target_buf
[x
] <= 0x7e)) {
4858 target_buf
[x
] = '.';
4863 target_buf
[x
] = ' ';
4868 /* print - with a newline */
4869 command_print_sameline(NULL
, "%s\n", target_buf
);
4877 static int jim_target_mem2array(Jim_Interp
*interp
,
4878 int argc
, Jim_Obj
*const *argv
)
4880 struct target
*target
= Jim_CmdPrivData(interp
);
4881 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4884 static int jim_target_array2mem(Jim_Interp
*interp
,
4885 int argc
, Jim_Obj
*const *argv
)
4887 struct target
*target
= Jim_CmdPrivData(interp
);
4888 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4891 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4893 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
4897 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4899 bool allow_defer
= false;
4902 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4904 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4905 Jim_SetResultFormatted(goi
.interp
,
4906 "usage: %s ['allow-defer']", cmd_name
);
4910 strcmp(Jim_GetString(argv
[1], NULL
), "allow-defer") == 0) {
4912 struct Jim_Obj
*obj
;
4913 int e
= Jim_GetOpt_Obj(&goi
, &obj
);
4919 struct target
*target
= Jim_CmdPrivData(interp
);
4920 if (!target
->tap
->enabled
)
4921 return jim_target_tap_disabled(interp
);
4923 if (allow_defer
&& target
->defer_examine
) {
4924 LOG_INFO("Deferring arp_examine of %s", target_name(target
));
4925 LOG_INFO("Use arp_examine command to examine it manually!");
4929 int e
= target
->type
->examine(target
);
4935 static int jim_target_was_examined(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4937 struct target
*target
= Jim_CmdPrivData(interp
);
4939 Jim_SetResultBool(interp
, target_was_examined(target
));
4943 static int jim_target_examine_deferred(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4945 struct target
*target
= Jim_CmdPrivData(interp
);
4947 Jim_SetResultBool(interp
, target
->defer_examine
);
4951 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4954 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4957 struct target
*target
= Jim_CmdPrivData(interp
);
4959 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
4965 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4968 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4971 struct target
*target
= Jim_CmdPrivData(interp
);
4972 if (!target
->tap
->enabled
)
4973 return jim_target_tap_disabled(interp
);
4976 if (!(target_was_examined(target
)))
4977 e
= ERROR_TARGET_NOT_EXAMINED
;
4979 e
= target
->type
->poll(target
);
4985 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4988 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4990 if (goi
.argc
!= 2) {
4991 Jim_WrongNumArgs(interp
, 0, argv
,
4992 "([tT]|[fF]|assert|deassert) BOOL");
4997 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4999 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
5002 /* the halt or not param */
5004 e
= Jim_GetOpt_Wide(&goi
, &a
);
5008 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
5009 if (!target
->tap
->enabled
)
5010 return jim_target_tap_disabled(interp
);
5012 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
5013 Jim_SetResultFormatted(interp
,
5014 "No target-specific reset for %s",
5015 target_name(target
));
5019 if (target
->defer_examine
)
5020 target_reset_examined(target
);
5022 /* determine if we should halt or not. */
5023 target
->reset_halt
= !!a
;
5024 /* When this happens - all workareas are invalid. */
5025 target_free_all_working_areas_restore(target
, 0);
5028 if (n
->value
== NVP_ASSERT
)
5029 e
= target
->type
->assert_reset(target
);
5031 e
= target
->type
->deassert_reset(target
);
5032 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5035 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5038 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5041 struct target
*target
= Jim_CmdPrivData(interp
);
5042 if (!target
->tap
->enabled
)
5043 return jim_target_tap_disabled(interp
);
5044 int e
= target
->type
->halt(target
);
5045 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5048 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5051 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5053 /* params: <name> statename timeoutmsecs */
5054 if (goi
.argc
!= 2) {
5055 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5056 Jim_SetResultFormatted(goi
.interp
,
5057 "%s <state_name> <timeout_in_msec>", cmd_name
);
5062 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
5064 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
, 1);
5068 e
= Jim_GetOpt_Wide(&goi
, &a
);
5071 struct target
*target
= Jim_CmdPrivData(interp
);
5072 if (!target
->tap
->enabled
)
5073 return jim_target_tap_disabled(interp
);
5075 e
= target_wait_state(target
, n
->value
, a
);
5076 if (e
!= ERROR_OK
) {
5077 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
5078 Jim_SetResultFormatted(goi
.interp
,
5079 "target: %s wait %s fails (%#s) %s",
5080 target_name(target
), n
->name
,
5081 eObj
, target_strerror_safe(e
));
5082 Jim_FreeNewObj(interp
, eObj
);
5087 /* List for human, Events defined for this target.
5088 * scripts/programs should use 'name cget -event NAME'
5090 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5092 struct command_context
*cmd_ctx
= current_command_context(interp
);
5093 assert(cmd_ctx
!= NULL
);
5095 struct target
*target
= Jim_CmdPrivData(interp
);
5096 struct target_event_action
*teap
= target
->event_action
;
5097 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
5098 target
->target_number
,
5099 target_name(target
));
5100 command_print(cmd_ctx
, "%-25s | Body", "Event");
5101 command_print(cmd_ctx
, "------------------------- | "
5102 "----------------------------------------");
5104 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
5105 command_print(cmd_ctx
, "%-25s | %s",
5106 opt
->name
, Jim_GetString(teap
->body
, NULL
));
5109 command_print(cmd_ctx
, "***END***");
5112 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5115 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5118 struct target
*target
= Jim_CmdPrivData(interp
);
5119 Jim_SetResultString(interp
, target_state_name(target
), -1);
5122 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5125 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5126 if (goi
.argc
!= 1) {
5127 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5128 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
5132 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
5134 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
5137 struct target
*target
= Jim_CmdPrivData(interp
);
5138 target_handle_event(target
, n
->value
);
5142 static const struct command_registration target_instance_command_handlers
[] = {
5144 .name
= "configure",
5145 .mode
= COMMAND_CONFIG
,
5146 .jim_handler
= jim_target_configure
,
5147 .help
= "configure a new target for use",
5148 .usage
= "[target_attribute ...]",
5152 .mode
= COMMAND_ANY
,
5153 .jim_handler
= jim_target_configure
,
5154 .help
= "returns the specified target attribute",
5155 .usage
= "target_attribute",
5159 .mode
= COMMAND_EXEC
,
5160 .jim_handler
= jim_target_mw
,
5161 .help
= "Write 32-bit word(s) to target memory",
5162 .usage
= "address data [count]",
5166 .mode
= COMMAND_EXEC
,
5167 .jim_handler
= jim_target_mw
,
5168 .help
= "Write 16-bit half-word(s) to target memory",
5169 .usage
= "address data [count]",
5173 .mode
= COMMAND_EXEC
,
5174 .jim_handler
= jim_target_mw
,
5175 .help
= "Write byte(s) to target memory",
5176 .usage
= "address data [count]",
5180 .mode
= COMMAND_EXEC
,
5181 .jim_handler
= jim_target_md
,
5182 .help
= "Display target memory as 32-bit words",
5183 .usage
= "address [count]",
5187 .mode
= COMMAND_EXEC
,
5188 .jim_handler
= jim_target_md
,
5189 .help
= "Display target memory as 16-bit half-words",
5190 .usage
= "address [count]",
5194 .mode
= COMMAND_EXEC
,
5195 .jim_handler
= jim_target_md
,
5196 .help
= "Display target memory as 8-bit bytes",
5197 .usage
= "address [count]",
5200 .name
= "array2mem",
5201 .mode
= COMMAND_EXEC
,
5202 .jim_handler
= jim_target_array2mem
,
5203 .help
= "Writes Tcl array of 8/16/32 bit numbers "
5205 .usage
= "arrayname bitwidth address count",
5208 .name
= "mem2array",
5209 .mode
= COMMAND_EXEC
,
5210 .jim_handler
= jim_target_mem2array
,
5211 .help
= "Loads Tcl array of 8/16/32 bit numbers "
5212 "from target memory",
5213 .usage
= "arrayname bitwidth address count",
5216 .name
= "eventlist",
5217 .mode
= COMMAND_EXEC
,
5218 .jim_handler
= jim_target_event_list
,
5219 .help
= "displays a table of events defined for this target",
5223 .mode
= COMMAND_EXEC
,
5224 .jim_handler
= jim_target_current_state
,
5225 .help
= "displays the current state of this target",
5228 .name
= "arp_examine",
5229 .mode
= COMMAND_EXEC
,
5230 .jim_handler
= jim_target_examine
,
5231 .help
= "used internally for reset processing",
5232 .usage
= "arp_examine ['allow-defer']",
5235 .name
= "was_examined",
5236 .mode
= COMMAND_EXEC
,
5237 .jim_handler
= jim_target_was_examined
,
5238 .help
= "used internally for reset processing",
5239 .usage
= "was_examined",
5242 .name
= "examine_deferred",
5243 .mode
= COMMAND_EXEC
,
5244 .jim_handler
= jim_target_examine_deferred
,
5245 .help
= "used internally for reset processing",
5246 .usage
= "examine_deferred",
5249 .name
= "arp_halt_gdb",
5250 .mode
= COMMAND_EXEC
,
5251 .jim_handler
= jim_target_halt_gdb
,
5252 .help
= "used internally for reset processing to halt GDB",
5256 .mode
= COMMAND_EXEC
,
5257 .jim_handler
= jim_target_poll
,
5258 .help
= "used internally for reset processing",
5261 .name
= "arp_reset",
5262 .mode
= COMMAND_EXEC
,
5263 .jim_handler
= jim_target_reset
,
5264 .help
= "used internally for reset processing",
5268 .mode
= COMMAND_EXEC
,
5269 .jim_handler
= jim_target_halt
,
5270 .help
= "used internally for reset processing",
5273 .name
= "arp_waitstate",
5274 .mode
= COMMAND_EXEC
,
5275 .jim_handler
= jim_target_wait_state
,
5276 .help
= "used internally for reset processing",
5279 .name
= "invoke-event",
5280 .mode
= COMMAND_EXEC
,
5281 .jim_handler
= jim_target_invoke_event
,
5282 .help
= "invoke handler for specified event",
5283 .usage
= "event_name",
5285 COMMAND_REGISTRATION_DONE
5288 static int target_create(Jim_GetOptInfo
*goi
)
5295 struct target
*target
;
5296 struct command_context
*cmd_ctx
;
5298 cmd_ctx
= current_command_context(goi
->interp
);
5299 assert(cmd_ctx
!= NULL
);
5301 if (goi
->argc
< 3) {
5302 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
5307 Jim_GetOpt_Obj(goi
, &new_cmd
);
5308 /* does this command exist? */
5309 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
5311 cp
= Jim_GetString(new_cmd
, NULL
);
5312 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
5317 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
5320 struct transport
*tr
= get_current_transport();
5321 if (tr
->override_target
) {
5322 e
= tr
->override_target(&cp
);
5323 if (e
!= ERROR_OK
) {
5324 LOG_ERROR("The selected transport doesn't support this target");
5327 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
5329 /* now does target type exist */
5330 for (x
= 0 ; target_types
[x
] ; x
++) {
5331 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
5336 /* check for deprecated name */
5337 if (target_types
[x
]->deprecated_name
) {
5338 if (0 == strcmp(cp
, target_types
[x
]->deprecated_name
)) {
5340 LOG_WARNING("target name is deprecated use: \'%s\'", target_types
[x
]->name
);
5345 if (target_types
[x
] == NULL
) {
5346 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
5347 for (x
= 0 ; target_types
[x
] ; x
++) {
5348 if (target_types
[x
+ 1]) {
5349 Jim_AppendStrings(goi
->interp
,
5350 Jim_GetResult(goi
->interp
),
5351 target_types
[x
]->name
,
5354 Jim_AppendStrings(goi
->interp
,
5355 Jim_GetResult(goi
->interp
),
5357 target_types
[x
]->name
, NULL
);
5364 target
= calloc(1, sizeof(struct target
));
5365 /* set target number */
5366 target
->target_number
= new_target_number();
5367 cmd_ctx
->current_target
= target
->target_number
;
5369 /* allocate memory for each unique target type */
5370 target
->type
= calloc(1, sizeof(struct target_type
));
5372 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
5374 /* will be set by "-endian" */
5375 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5377 /* default to first core, override with -coreid */
5380 target
->working_area
= 0x0;
5381 target
->working_area_size
= 0x0;
5382 target
->working_areas
= NULL
;
5383 target
->backup_working_area
= 0;
5385 target
->state
= TARGET_UNKNOWN
;
5386 target
->debug_reason
= DBG_REASON_UNDEFINED
;
5387 target
->reg_cache
= NULL
;
5388 target
->breakpoints
= NULL
;
5389 target
->watchpoints
= NULL
;
5390 target
->next
= NULL
;
5391 target
->arch_info
= NULL
;
5393 target
->display
= 1;
5395 target
->halt_issued
= false;
5397 /* initialize trace information */
5398 target
->trace_info
= malloc(sizeof(struct trace
));
5399 target
->trace_info
->num_trace_points
= 0;
5400 target
->trace_info
->trace_points_size
= 0;
5401 target
->trace_info
->trace_points
= NULL
;
5402 target
->trace_info
->trace_history_size
= 0;
5403 target
->trace_info
->trace_history
= NULL
;
5404 target
->trace_info
->trace_history_pos
= 0;
5405 target
->trace_info
->trace_history_overflowed
= 0;
5407 target
->dbgmsg
= NULL
;
5408 target
->dbg_msg_enabled
= 0;
5410 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5412 target
->rtos
= NULL
;
5413 target
->rtos_auto_detect
= false;
5415 /* Do the rest as "configure" options */
5416 goi
->isconfigure
= 1;
5417 e
= target_configure(goi
, target
);
5419 if (target
->tap
== NULL
) {
5420 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
5430 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
5431 /* default endian to little if not specified */
5432 target
->endianness
= TARGET_LITTLE_ENDIAN
;
5435 cp
= Jim_GetString(new_cmd
, NULL
);
5436 target
->cmd_name
= strdup(cp
);
5438 /* create the target specific commands */
5439 if (target
->type
->commands
) {
5440 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
5442 LOG_ERROR("unable to register '%s' commands", cp
);
5444 if (target
->type
->target_create
)
5445 (*(target
->type
->target_create
))(target
, goi
->interp
);
5447 /* append to end of list */
5449 struct target
**tpp
;
5450 tpp
= &(all_targets
);
5452 tpp
= &((*tpp
)->next
);
5456 /* now - create the new target name command */
5457 const struct command_registration target_subcommands
[] = {
5459 .chain
= target_instance_command_handlers
,
5462 .chain
= target
->type
->commands
,
5464 COMMAND_REGISTRATION_DONE
5466 const struct command_registration target_commands
[] = {
5469 .mode
= COMMAND_ANY
,
5470 .help
= "target command group",
5472 .chain
= target_subcommands
,
5474 COMMAND_REGISTRATION_DONE
5476 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
5480 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
5482 command_set_handler_data(c
, target
);
5484 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
5487 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5490 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5493 struct command_context
*cmd_ctx
= current_command_context(interp
);
5494 assert(cmd_ctx
!= NULL
);
5496 Jim_SetResultString(interp
, target_name(get_current_target(cmd_ctx
)), -1);
5500 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5503 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5506 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5507 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++) {
5508 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5509 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
5514 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5517 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5520 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5521 struct target
*target
= all_targets
;
5523 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5524 Jim_NewStringObj(interp
, target_name(target
), -1));
5525 target
= target
->next
;
5530 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5533 const char *targetname
;
5535 struct target
*target
= (struct target
*) NULL
;
5536 struct target_list
*head
, *curr
, *new;
5537 curr
= (struct target_list
*) NULL
;
5538 head
= (struct target_list
*) NULL
;
5541 LOG_DEBUG("%d", argc
);
5542 /* argv[1] = target to associate in smp
5543 * argv[2] = target to assoicate in smp
5547 for (i
= 1; i
< argc
; i
++) {
5549 targetname
= Jim_GetString(argv
[i
], &len
);
5550 target
= get_target(targetname
);
5551 LOG_DEBUG("%s ", targetname
);
5553 new = malloc(sizeof(struct target_list
));
5554 new->target
= target
;
5555 new->next
= (struct target_list
*)NULL
;
5556 if (head
== (struct target_list
*)NULL
) {
5565 /* now parse the list of cpu and put the target in smp mode*/
5568 while (curr
!= (struct target_list
*)NULL
) {
5569 target
= curr
->target
;
5571 target
->head
= head
;
5575 if (target
&& target
->rtos
)
5576 retval
= rtos_smp_init(head
->target
);
5582 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5585 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5587 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5588 "<name> <target_type> [<target_options> ...]");
5591 return target_create(&goi
);
5594 static const struct command_registration target_subcommand_handlers
[] = {
5597 .mode
= COMMAND_CONFIG
,
5598 .handler
= handle_target_init_command
,
5599 .help
= "initialize targets",
5603 /* REVISIT this should be COMMAND_CONFIG ... */
5604 .mode
= COMMAND_ANY
,
5605 .jim_handler
= jim_target_create
,
5606 .usage
= "name type '-chain-position' name [options ...]",
5607 .help
= "Creates and selects a new target",
5611 .mode
= COMMAND_ANY
,
5612 .jim_handler
= jim_target_current
,
5613 .help
= "Returns the currently selected target",
5617 .mode
= COMMAND_ANY
,
5618 .jim_handler
= jim_target_types
,
5619 .help
= "Returns the available target types as "
5620 "a list of strings",
5624 .mode
= COMMAND_ANY
,
5625 .jim_handler
= jim_target_names
,
5626 .help
= "Returns the names of all targets as a list of strings",
5630 .mode
= COMMAND_ANY
,
5631 .jim_handler
= jim_target_smp
,
5632 .usage
= "targetname1 targetname2 ...",
5633 .help
= "gather several target in a smp list"
5636 COMMAND_REGISTRATION_DONE
5646 static int fastload_num
;
5647 static struct FastLoad
*fastload
;
5649 static void free_fastload(void)
5651 if (fastload
!= NULL
) {
5653 for (i
= 0; i
< fastload_num
; i
++) {
5654 if (fastload
[i
].data
)
5655 free(fastload
[i
].data
);
5662 COMMAND_HANDLER(handle_fast_load_image_command
)
5666 uint32_t image_size
;
5667 uint32_t min_address
= 0;
5668 uint32_t max_address
= 0xffffffff;
5673 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
5674 &image
, &min_address
, &max_address
);
5675 if (ERROR_OK
!= retval
)
5678 struct duration bench
;
5679 duration_start(&bench
);
5681 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5682 if (retval
!= ERROR_OK
)
5687 fastload_num
= image
.num_sections
;
5688 fastload
= malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5689 if (fastload
== NULL
) {
5690 command_print(CMD_CTX
, "out of memory");
5691 image_close(&image
);
5694 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5695 for (i
= 0; i
< image
.num_sections
; i
++) {
5696 buffer
= malloc(image
.sections
[i
].size
);
5697 if (buffer
== NULL
) {
5698 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5699 (int)(image
.sections
[i
].size
));
5700 retval
= ERROR_FAIL
;
5704 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
5705 if (retval
!= ERROR_OK
) {
5710 uint32_t offset
= 0;
5711 uint32_t length
= buf_cnt
;
5713 /* DANGER!!! beware of unsigned comparision here!!! */
5715 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
5716 (image
.sections
[i
].base_address
< max_address
)) {
5717 if (image
.sections
[i
].base_address
< min_address
) {
5718 /* clip addresses below */
5719 offset
+= min_address
-image
.sections
[i
].base_address
;
5723 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5724 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5726 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5727 fastload
[i
].data
= malloc(length
);
5728 if (fastload
[i
].data
== NULL
) {
5730 command_print(CMD_CTX
, "error allocating buffer for section (%" PRIu32
" bytes)",
5732 retval
= ERROR_FAIL
;
5735 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5736 fastload
[i
].length
= length
;
5738 image_size
+= length
;
5739 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
5740 (unsigned int)length
,
5741 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
5747 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
5748 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
5749 "in %fs (%0.3f KiB/s)", image_size
,
5750 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
5752 command_print(CMD_CTX
,
5753 "WARNING: image has not been loaded to target!"
5754 "You can issue a 'fast_load' to finish loading.");
5757 image_close(&image
);
5759 if (retval
!= ERROR_OK
)
5765 COMMAND_HANDLER(handle_fast_load_command
)
5768 return ERROR_COMMAND_SYNTAX_ERROR
;
5769 if (fastload
== NULL
) {
5770 LOG_ERROR("No image in memory");
5774 int64_t ms
= timeval_ms();
5776 int retval
= ERROR_OK
;
5777 for (i
= 0; i
< fastload_num
; i
++) {
5778 struct target
*target
= get_current_target(CMD_CTX
);
5779 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
5780 (unsigned int)(fastload
[i
].address
),
5781 (unsigned int)(fastload
[i
].length
));
5782 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5783 if (retval
!= ERROR_OK
)
5785 size
+= fastload
[i
].length
;
5787 if (retval
== ERROR_OK
) {
5788 int64_t after
= timeval_ms();
5789 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5794 static const struct command_registration target_command_handlers
[] = {
5797 .handler
= handle_targets_command
,
5798 .mode
= COMMAND_ANY
,
5799 .help
= "change current default target (one parameter) "
5800 "or prints table of all targets (no parameters)",
5801 .usage
= "[target]",
5805 .mode
= COMMAND_CONFIG
,
5806 .help
= "configure target",
5808 .chain
= target_subcommand_handlers
,
5810 COMMAND_REGISTRATION_DONE
5813 int target_register_commands(struct command_context
*cmd_ctx
)
5815 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5818 static bool target_reset_nag
= true;
5820 bool get_target_reset_nag(void)
5822 return target_reset_nag
;
5825 COMMAND_HANDLER(handle_target_reset_nag
)
5827 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
5828 &target_reset_nag
, "Nag after each reset about options to improve "
5832 COMMAND_HANDLER(handle_ps_command
)
5834 struct target
*target
= get_current_target(CMD_CTX
);
5836 if (target
->state
!= TARGET_HALTED
) {
5837 LOG_INFO("target not halted !!");
5841 if ((target
->rtos
) && (target
->rtos
->type
)
5842 && (target
->rtos
->type
->ps_command
)) {
5843 display
= target
->rtos
->type
->ps_command(target
);
5844 command_print(CMD_CTX
, "%s", display
);
5849 return ERROR_TARGET_FAILURE
;
5853 static void binprint(struct command_context
*cmd_ctx
, const char *text
, const uint8_t *buf
, int size
)
5856 command_print_sameline(cmd_ctx
, "%s", text
);
5857 for (int i
= 0; i
< size
; i
++)
5858 command_print_sameline(cmd_ctx
, " %02x", buf
[i
]);
5859 command_print(cmd_ctx
, " ");
5862 COMMAND_HANDLER(handle_test_mem_access_command
)
5864 struct target
*target
= get_current_target(CMD_CTX
);
5866 int retval
= ERROR_OK
;
5868 if (target
->state
!= TARGET_HALTED
) {
5869 LOG_INFO("target not halted !!");
5874 return ERROR_COMMAND_SYNTAX_ERROR
;
5876 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], test_size
);
5879 size_t num_bytes
= test_size
+ 4;
5881 struct working_area
*wa
= NULL
;
5882 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
5883 if (retval
!= ERROR_OK
) {
5884 LOG_ERROR("Not enough working area");
5888 uint8_t *test_pattern
= malloc(num_bytes
);
5890 for (size_t i
= 0; i
< num_bytes
; i
++)
5891 test_pattern
[i
] = rand();
5893 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
5894 if (retval
!= ERROR_OK
) {
5895 LOG_ERROR("Test pattern write failed");
5899 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
5900 for (int size
= 1; size
<= 4; size
*= 2) {
5901 for (int offset
= 0; offset
< 4; offset
++) {
5902 uint32_t count
= test_size
/ size
;
5903 size_t host_bufsiz
= (count
+ 2) * size
+ host_offset
;
5904 uint8_t *read_ref
= malloc(host_bufsiz
);
5905 uint8_t *read_buf
= malloc(host_bufsiz
);
5907 for (size_t i
= 0; i
< host_bufsiz
; i
++) {
5908 read_ref
[i
] = rand();
5909 read_buf
[i
] = read_ref
[i
];
5911 command_print_sameline(CMD_CTX
,
5912 "Test read %" PRIu32
" x %d @ %d to %saligned buffer: ", count
,
5913 size
, offset
, host_offset
? "un" : "");
5915 struct duration bench
;
5916 duration_start(&bench
);
5918 retval
= target_read_memory(target
, wa
->address
+ offset
, size
, count
,
5919 read_buf
+ size
+ host_offset
);
5921 duration_measure(&bench
);
5923 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
5924 command_print(CMD_CTX
, "Unsupported alignment");
5926 } else if (retval
!= ERROR_OK
) {
5927 command_print(CMD_CTX
, "Memory read failed");
5931 /* replay on host */
5932 memcpy(read_ref
+ size
+ host_offset
, test_pattern
+ offset
, count
* size
);
5935 int result
= memcmp(read_ref
, read_buf
, host_bufsiz
);
5937 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
5938 duration_elapsed(&bench
),
5939 duration_kbps(&bench
, count
* size
));
5941 command_print(CMD_CTX
, "Compare failed");
5942 binprint(CMD_CTX
, "ref:", read_ref
, host_bufsiz
);
5943 binprint(CMD_CTX
, "buf:", read_buf
, host_bufsiz
);
5956 target_free_working_area(target
, wa
);
5959 num_bytes
= test_size
+ 4 + 4 + 4;
5961 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
5962 if (retval
!= ERROR_OK
) {
5963 LOG_ERROR("Not enough working area");
5967 test_pattern
= malloc(num_bytes
);
5969 for (size_t i
= 0; i
< num_bytes
; i
++)
5970 test_pattern
[i
] = rand();
5972 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
5973 for (int size
= 1; size
<= 4; size
*= 2) {
5974 for (int offset
= 0; offset
< 4; offset
++) {
5975 uint32_t count
= test_size
/ size
;
5976 size_t host_bufsiz
= count
* size
+ host_offset
;
5977 uint8_t *read_ref
= malloc(num_bytes
);
5978 uint8_t *read_buf
= malloc(num_bytes
);
5979 uint8_t *write_buf
= malloc(host_bufsiz
);
5981 for (size_t i
= 0; i
< host_bufsiz
; i
++)
5982 write_buf
[i
] = rand();
5983 command_print_sameline(CMD_CTX
,
5984 "Test write %" PRIu32
" x %d @ %d from %saligned buffer: ", count
,
5985 size
, offset
, host_offset
? "un" : "");
5987 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
5988 if (retval
!= ERROR_OK
) {
5989 command_print(CMD_CTX
, "Test pattern write failed");
5993 /* replay on host */
5994 memcpy(read_ref
, test_pattern
, num_bytes
);
5995 memcpy(read_ref
+ size
+ offset
, write_buf
+ host_offset
, count
* size
);
5997 struct duration bench
;
5998 duration_start(&bench
);
6000 retval
= target_write_memory(target
, wa
->address
+ size
+ offset
, size
, count
,
6001 write_buf
+ host_offset
);
6003 duration_measure(&bench
);
6005 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6006 command_print(CMD_CTX
, "Unsupported alignment");
6008 } else if (retval
!= ERROR_OK
) {
6009 command_print(CMD_CTX
, "Memory write failed");
6014 retval
= target_read_memory(target
, wa
->address
, 1, num_bytes
, read_buf
);
6015 if (retval
!= ERROR_OK
) {
6016 command_print(CMD_CTX
, "Test pattern write failed");
6021 int result
= memcmp(read_ref
, read_buf
, num_bytes
);
6023 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
6024 duration_elapsed(&bench
),
6025 duration_kbps(&bench
, count
* size
));
6027 command_print(CMD_CTX
, "Compare failed");
6028 binprint(CMD_CTX
, "ref:", read_ref
, num_bytes
);
6029 binprint(CMD_CTX
, "buf:", read_buf
, num_bytes
);
6041 target_free_working_area(target
, wa
);
6045 static const struct command_registration target_exec_command_handlers
[] = {
6047 .name
= "fast_load_image",
6048 .handler
= handle_fast_load_image_command
,
6049 .mode
= COMMAND_ANY
,
6050 .help
= "Load image into server memory for later use by "
6051 "fast_load; primarily for profiling",
6052 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6053 "[min_address [max_length]]",
6056 .name
= "fast_load",
6057 .handler
= handle_fast_load_command
,
6058 .mode
= COMMAND_EXEC
,
6059 .help
= "loads active fast load image to current target "
6060 "- mainly for profiling purposes",
6065 .handler
= handle_profile_command
,
6066 .mode
= COMMAND_EXEC
,
6067 .usage
= "seconds filename [start end]",
6068 .help
= "profiling samples the CPU PC",
6070 /** @todo don't register virt2phys() unless target supports it */
6072 .name
= "virt2phys",
6073 .handler
= handle_virt2phys_command
,
6074 .mode
= COMMAND_ANY
,
6075 .help
= "translate a virtual address into a physical address",
6076 .usage
= "virtual_address",
6080 .handler
= handle_reg_command
,
6081 .mode
= COMMAND_EXEC
,
6082 .help
= "display (reread from target with \"force\") or set a register; "
6083 "with no arguments, displays all registers and their values",
6084 .usage
= "[(register_number|register_name) [(value|'force')]]",
6088 .handler
= handle_poll_command
,
6089 .mode
= COMMAND_EXEC
,
6090 .help
= "poll target state; or reconfigure background polling",
6091 .usage
= "['on'|'off']",
6094 .name
= "wait_halt",
6095 .handler
= handle_wait_halt_command
,
6096 .mode
= COMMAND_EXEC
,
6097 .help
= "wait up to the specified number of milliseconds "
6098 "(default 5000) for a previously requested halt",
6099 .usage
= "[milliseconds]",
6103 .handler
= handle_halt_command
,
6104 .mode
= COMMAND_EXEC
,
6105 .help
= "request target to halt, then wait up to the specified"
6106 "number of milliseconds (default 5000) for it to complete",
6107 .usage
= "[milliseconds]",
6111 .handler
= handle_resume_command
,
6112 .mode
= COMMAND_EXEC
,
6113 .help
= "resume target execution from current PC or address",
6114 .usage
= "[address]",
6118 .handler
= handle_reset_command
,
6119 .mode
= COMMAND_EXEC
,
6120 .usage
= "[run|halt|init]",
6121 .help
= "Reset all targets into the specified mode."
6122 "Default reset mode is run, if not given.",
6125 .name
= "soft_reset_halt",
6126 .handler
= handle_soft_reset_halt_command
,
6127 .mode
= COMMAND_EXEC
,
6129 .help
= "halt the target and do a soft reset",
6133 .handler
= handle_step_command
,
6134 .mode
= COMMAND_EXEC
,
6135 .help
= "step one instruction from current PC or address",
6136 .usage
= "[address]",
6140 .handler
= handle_md_command
,
6141 .mode
= COMMAND_EXEC
,
6142 .help
= "display memory words",
6143 .usage
= "['phys'] address [count]",
6147 .handler
= handle_md_command
,
6148 .mode
= COMMAND_EXEC
,
6149 .help
= "display memory half-words",
6150 .usage
= "['phys'] address [count]",
6154 .handler
= handle_md_command
,
6155 .mode
= COMMAND_EXEC
,
6156 .help
= "display memory bytes",
6157 .usage
= "['phys'] address [count]",
6161 .handler
= handle_mw_command
,
6162 .mode
= COMMAND_EXEC
,
6163 .help
= "write memory word",
6164 .usage
= "['phys'] address value [count]",
6168 .handler
= handle_mw_command
,
6169 .mode
= COMMAND_EXEC
,
6170 .help
= "write memory half-word",
6171 .usage
= "['phys'] address value [count]",
6175 .handler
= handle_mw_command
,
6176 .mode
= COMMAND_EXEC
,
6177 .help
= "write memory byte",
6178 .usage
= "['phys'] address value [count]",
6182 .handler
= handle_bp_command
,
6183 .mode
= COMMAND_EXEC
,
6184 .help
= "list or set hardware or software breakpoint",
6185 .usage
= "<address> [<asid>]<length> ['hw'|'hw_ctx']",
6189 .handler
= handle_rbp_command
,
6190 .mode
= COMMAND_EXEC
,
6191 .help
= "remove breakpoint",
6196 .handler
= handle_wp_command
,
6197 .mode
= COMMAND_EXEC
,
6198 .help
= "list (no params) or create watchpoints",
6199 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
6203 .handler
= handle_rwp_command
,
6204 .mode
= COMMAND_EXEC
,
6205 .help
= "remove watchpoint",
6209 .name
= "load_image",
6210 .handler
= handle_load_image_command
,
6211 .mode
= COMMAND_EXEC
,
6212 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6213 "[min_address] [max_length]",
6216 .name
= "dump_image",
6217 .handler
= handle_dump_image_command
,
6218 .mode
= COMMAND_EXEC
,
6219 .usage
= "filename address size",
6222 .name
= "verify_image",
6223 .handler
= handle_verify_image_command
,
6224 .mode
= COMMAND_EXEC
,
6225 .usage
= "filename [offset [type]]",
6228 .name
= "test_image",
6229 .handler
= handle_test_image_command
,
6230 .mode
= COMMAND_EXEC
,
6231 .usage
= "filename [offset [type]]",
6234 .name
= "mem2array",
6235 .mode
= COMMAND_EXEC
,
6236 .jim_handler
= jim_mem2array
,
6237 .help
= "read 8/16/32 bit memory and return as a TCL array "
6238 "for script processing",
6239 .usage
= "arrayname bitwidth address count",
6242 .name
= "array2mem",
6243 .mode
= COMMAND_EXEC
,
6244 .jim_handler
= jim_array2mem
,
6245 .help
= "convert a TCL array to memory locations "
6246 "and write the 8/16/32 bit values",
6247 .usage
= "arrayname bitwidth address count",
6250 .name
= "reset_nag",
6251 .handler
= handle_target_reset_nag
,
6252 .mode
= COMMAND_ANY
,
6253 .help
= "Nag after each reset about options that could have been "
6254 "enabled to improve performance. ",
6255 .usage
= "['enable'|'disable']",
6259 .handler
= handle_ps_command
,
6260 .mode
= COMMAND_EXEC
,
6261 .help
= "list all tasks ",
6265 .name
= "test_mem_access",
6266 .handler
= handle_test_mem_access_command
,
6267 .mode
= COMMAND_EXEC
,
6268 .help
= "Test the target's memory access functions",
6272 COMMAND_REGISTRATION_DONE
6274 static int target_register_user_commands(struct command_context
*cmd_ctx
)
6276 int retval
= ERROR_OK
;
6277 retval
= target_request_register_commands(cmd_ctx
);
6278 if (retval
!= ERROR_OK
)
6281 retval
= trace_register_commands(cmd_ctx
);
6282 if (retval
!= ERROR_OK
)
6286 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);