1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
17 * Copyright (C) 2011 by Broadcom Corporation *
18 * Evan Hunter - ehunter@broadcom.com *
20 * Copyright (C) ST-Ericsson SA 2011 *
21 * michel.jaouen@stericsson.com : smp minimum support *
23 * Copyright (C) 2011 Andreas Fritiofson *
24 * andreas.fritiofson@gmail.com *
26 * This program is free software; you can redistribute it and/or modify *
27 * it under the terms of the GNU General Public License as published by *
28 * the Free Software Foundation; either version 2 of the License, or *
29 * (at your option) any later version. *
31 * This program is distributed in the hope that it will be useful, *
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
34 * GNU General Public License for more details. *
36 * You should have received a copy of the GNU General Public License *
37 * along with this program; if not, write to the *
38 * Free Software Foundation, Inc., *
39 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
40 ***************************************************************************/
46 #include <helper/time_support.h>
47 #include <jtag/jtag.h>
48 #include <flash/nor/core.h>
51 #include "target_type.h"
52 #include "target_request.h"
53 #include "breakpoints.h"
57 #include "rtos/rtos.h"
58 #include "transport/transport.h"
60 /* default halt wait timeout (ms) */
61 #define DEFAULT_HALT_TIMEOUT 5000
63 static int target_read_buffer_default(struct target
*target
, uint32_t address
,
64 uint32_t count
, uint8_t *buffer
);
65 static int target_write_buffer_default(struct target
*target
, uint32_t address
,
66 uint32_t count
, const uint8_t *buffer
);
67 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
68 int argc
, Jim_Obj
* const *argv
);
69 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
70 int argc
, Jim_Obj
* const *argv
);
71 static int target_register_user_commands(struct command_context
*cmd_ctx
);
72 static int target_get_gdb_fileio_info_default(struct target
*target
,
73 struct gdb_fileio_info
*fileio_info
);
74 static int target_gdb_fileio_end_default(struct target
*target
, int retcode
,
75 int fileio_errno
, bool ctrl_c
);
76 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
77 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
);
80 extern struct target_type arm7tdmi_target
;
81 extern struct target_type arm720t_target
;
82 extern struct target_type arm9tdmi_target
;
83 extern struct target_type arm920t_target
;
84 extern struct target_type arm966e_target
;
85 extern struct target_type arm946e_target
;
86 extern struct target_type arm926ejs_target
;
87 extern struct target_type fa526_target
;
88 extern struct target_type feroceon_target
;
89 extern struct target_type dragonite_target
;
90 extern struct target_type xscale_target
;
91 extern struct target_type cortexm_target
;
92 extern struct target_type cortexa_target
;
93 extern struct target_type cortexr4_target
;
94 extern struct target_type arm11_target
;
95 extern struct target_type mips_m4k_target
;
96 extern struct target_type avr_target
;
97 extern struct target_type dsp563xx_target
;
98 extern struct target_type dsp5680xx_target
;
99 extern struct target_type testee_target
;
100 extern struct target_type avr32_ap7k_target
;
101 extern struct target_type hla_target
;
102 extern struct target_type nds32_v2_target
;
103 extern struct target_type nds32_v3_target
;
104 extern struct target_type nds32_v3m_target
;
105 extern struct target_type or1k_target
;
106 extern struct target_type quark_x10xx_target
;
108 static struct target_type
*target_types
[] = {
139 struct target
*all_targets
;
140 static struct target_event_callback
*target_event_callbacks
;
141 static struct target_timer_callback
*target_timer_callbacks
;
142 LIST_HEAD(target_reset_callback_list
);
143 LIST_HEAD(target_trace_callback_list
);
144 static const int polling_interval
= 100;
146 static const Jim_Nvp nvp_assert
[] = {
147 { .name
= "assert", NVP_ASSERT
},
148 { .name
= "deassert", NVP_DEASSERT
},
149 { .name
= "T", NVP_ASSERT
},
150 { .name
= "F", NVP_DEASSERT
},
151 { .name
= "t", NVP_ASSERT
},
152 { .name
= "f", NVP_DEASSERT
},
153 { .name
= NULL
, .value
= -1 }
156 static const Jim_Nvp nvp_error_target
[] = {
157 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
158 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
159 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
160 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
161 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
162 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
163 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
164 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
165 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
166 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
167 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
168 { .value
= -1, .name
= NULL
}
171 static const char *target_strerror_safe(int err
)
175 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
182 static const Jim_Nvp nvp_target_event
[] = {
184 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
185 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
186 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
187 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
188 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
190 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
191 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
193 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
194 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
195 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
196 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
197 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
198 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
199 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
200 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
201 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
202 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
203 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
204 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
206 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
207 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
209 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
210 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
212 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
213 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
215 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
216 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
218 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
219 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
221 { .value
= TARGET_EVENT_TRACE_CONFIG
, .name
= "trace-config" },
223 { .name
= NULL
, .value
= -1 }
226 static const Jim_Nvp nvp_target_state
[] = {
227 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
228 { .name
= "running", .value
= TARGET_RUNNING
},
229 { .name
= "halted", .value
= TARGET_HALTED
},
230 { .name
= "reset", .value
= TARGET_RESET
},
231 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
232 { .name
= NULL
, .value
= -1 },
235 static const Jim_Nvp nvp_target_debug_reason
[] = {
236 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
237 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
238 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
239 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
240 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
241 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
242 { .name
= "program-exit" , .value
= DBG_REASON_EXIT
},
243 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
244 { .name
= NULL
, .value
= -1 },
247 static const Jim_Nvp nvp_target_endian
[] = {
248 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
249 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
250 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
251 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
252 { .name
= NULL
, .value
= -1 },
255 static const Jim_Nvp nvp_reset_modes
[] = {
256 { .name
= "unknown", .value
= RESET_UNKNOWN
},
257 { .name
= "run" , .value
= RESET_RUN
},
258 { .name
= "halt" , .value
= RESET_HALT
},
259 { .name
= "init" , .value
= RESET_INIT
},
260 { .name
= NULL
, .value
= -1 },
263 const char *debug_reason_name(struct target
*t
)
267 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
268 t
->debug_reason
)->name
;
270 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
271 cp
= "(*BUG*unknown*BUG*)";
276 const char *target_state_name(struct target
*t
)
279 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
281 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
282 cp
= "(*BUG*unknown*BUG*)";
287 const char *target_event_name(enum target_event event
)
290 cp
= Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
;
292 LOG_ERROR("Invalid target event: %d", (int)(event
));
293 cp
= "(*BUG*unknown*BUG*)";
298 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
301 cp
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
;
303 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
304 cp
= "(*BUG*unknown*BUG*)";
309 /* determine the number of the new target */
310 static int new_target_number(void)
315 /* number is 0 based */
319 if (x
< t
->target_number
)
320 x
= t
->target_number
;
326 /* read a uint64_t from a buffer in target memory endianness */
327 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
329 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
330 return le_to_h_u64(buffer
);
332 return be_to_h_u64(buffer
);
335 /* read a uint32_t from a buffer in target memory endianness */
336 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
338 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
339 return le_to_h_u32(buffer
);
341 return be_to_h_u32(buffer
);
344 /* read a uint24_t from a buffer in target memory endianness */
345 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
347 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
348 return le_to_h_u24(buffer
);
350 return be_to_h_u24(buffer
);
353 /* read a uint16_t from a buffer in target memory endianness */
354 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
356 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
357 return le_to_h_u16(buffer
);
359 return be_to_h_u16(buffer
);
362 /* read a uint8_t from a buffer in target memory endianness */
363 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
365 return *buffer
& 0x0ff;
368 /* write a uint64_t to a buffer in target memory endianness */
369 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
371 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
372 h_u64_to_le(buffer
, value
);
374 h_u64_to_be(buffer
, value
);
377 /* write a uint32_t to a buffer in target memory endianness */
378 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
380 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
381 h_u32_to_le(buffer
, value
);
383 h_u32_to_be(buffer
, value
);
386 /* write a uint24_t to a buffer in target memory endianness */
387 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
389 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
390 h_u24_to_le(buffer
, value
);
392 h_u24_to_be(buffer
, value
);
395 /* write a uint16_t to a buffer in target memory endianness */
396 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
398 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
399 h_u16_to_le(buffer
, value
);
401 h_u16_to_be(buffer
, value
);
404 /* write a uint8_t to a buffer in target memory endianness */
405 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
410 /* write a uint64_t array to a buffer in target memory endianness */
411 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
414 for (i
= 0; i
< count
; i
++)
415 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
418 /* write a uint32_t array to a buffer in target memory endianness */
419 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
422 for (i
= 0; i
< count
; i
++)
423 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
426 /* write a uint16_t array to a buffer in target memory endianness */
427 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
430 for (i
= 0; i
< count
; i
++)
431 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
434 /* write a uint64_t array to a buffer in target memory endianness */
435 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
438 for (i
= 0; i
< count
; i
++)
439 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
442 /* write a uint32_t array to a buffer in target memory endianness */
443 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
446 for (i
= 0; i
< count
; i
++)
447 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
450 /* write a uint16_t array to a buffer in target memory endianness */
451 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
454 for (i
= 0; i
< count
; i
++)
455 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
458 /* return a pointer to a configured target; id is name or number */
459 struct target
*get_target(const char *id
)
461 struct target
*target
;
463 /* try as tcltarget name */
464 for (target
= all_targets
; target
; target
= target
->next
) {
465 if (target_name(target
) == NULL
)
467 if (strcmp(id
, target_name(target
)) == 0)
471 /* It's OK to remove this fallback sometime after August 2010 or so */
473 /* no match, try as number */
475 if (parse_uint(id
, &num
) != ERROR_OK
)
478 for (target
= all_targets
; target
; target
= target
->next
) {
479 if (target
->target_number
== (int)num
) {
480 LOG_WARNING("use '%s' as target identifier, not '%u'",
481 target_name(target
), num
);
489 /* returns a pointer to the n-th configured target */
490 struct target
*get_target_by_num(int num
)
492 struct target
*target
= all_targets
;
495 if (target
->target_number
== num
)
497 target
= target
->next
;
503 struct target
*get_current_target(struct command_context
*cmd_ctx
)
505 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
507 if (target
== NULL
) {
508 LOG_ERROR("BUG: current_target out of bounds");
515 int target_poll(struct target
*target
)
519 /* We can't poll until after examine */
520 if (!target_was_examined(target
)) {
521 /* Fail silently lest we pollute the log */
525 retval
= target
->type
->poll(target
);
526 if (retval
!= ERROR_OK
)
529 if (target
->halt_issued
) {
530 if (target
->state
== TARGET_HALTED
)
531 target
->halt_issued
= false;
533 long long t
= timeval_ms() - target
->halt_issued_time
;
534 if (t
> DEFAULT_HALT_TIMEOUT
) {
535 target
->halt_issued
= false;
536 LOG_INFO("Halt timed out, wake up GDB.");
537 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
545 int target_halt(struct target
*target
)
548 /* We can't poll until after examine */
549 if (!target_was_examined(target
)) {
550 LOG_ERROR("Target not examined yet");
554 retval
= target
->type
->halt(target
);
555 if (retval
!= ERROR_OK
)
558 target
->halt_issued
= true;
559 target
->halt_issued_time
= timeval_ms();
565 * Make the target (re)start executing using its saved execution
566 * context (possibly with some modifications).
568 * @param target Which target should start executing.
569 * @param current True to use the target's saved program counter instead
570 * of the address parameter
571 * @param address Optionally used as the program counter.
572 * @param handle_breakpoints True iff breakpoints at the resumption PC
573 * should be skipped. (For example, maybe execution was stopped by
574 * such a breakpoint, in which case it would be counterprodutive to
576 * @param debug_execution False if all working areas allocated by OpenOCD
577 * should be released and/or restored to their original contents.
578 * (This would for example be true to run some downloaded "helper"
579 * algorithm code, which resides in one such working buffer and uses
580 * another for data storage.)
582 * @todo Resolve the ambiguity about what the "debug_execution" flag
583 * signifies. For example, Target implementations don't agree on how
584 * it relates to invalidation of the register cache, or to whether
585 * breakpoints and watchpoints should be enabled. (It would seem wrong
586 * to enable breakpoints when running downloaded "helper" algorithms
587 * (debug_execution true), since the breakpoints would be set to match
588 * target firmware being debugged, not the helper algorithm.... and
589 * enabling them could cause such helpers to malfunction (for example,
590 * by overwriting data with a breakpoint instruction. On the other
591 * hand the infrastructure for running such helpers might use this
592 * procedure but rely on hardware breakpoint to detect termination.)
594 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
598 /* We can't poll until after examine */
599 if (!target_was_examined(target
)) {
600 LOG_ERROR("Target not examined yet");
604 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
606 /* note that resume *must* be asynchronous. The CPU can halt before
607 * we poll. The CPU can even halt at the current PC as a result of
608 * a software breakpoint being inserted by (a bug?) the application.
610 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
611 if (retval
!= ERROR_OK
)
614 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
619 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
624 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
625 if (n
->name
== NULL
) {
626 LOG_ERROR("invalid reset mode");
630 struct target
*target
;
631 for (target
= all_targets
; target
; target
= target
->next
)
632 target_call_reset_callbacks(target
, reset_mode
);
634 /* disable polling during reset to make reset event scripts
635 * more predictable, i.e. dr/irscan & pathmove in events will
636 * not have JTAG operations injected into the middle of a sequence.
638 bool save_poll
= jtag_poll_get_enabled();
640 jtag_poll_set_enabled(false);
642 sprintf(buf
, "ocd_process_reset %s", n
->name
);
643 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
645 jtag_poll_set_enabled(save_poll
);
647 if (retval
!= JIM_OK
) {
648 Jim_MakeErrorMessage(cmd_ctx
->interp
);
649 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
653 /* We want any events to be processed before the prompt */
654 retval
= target_call_timer_callbacks_now();
656 for (target
= all_targets
; target
; target
= target
->next
) {
657 target
->type
->check_reset(target
);
658 target
->running_alg
= false;
664 static int identity_virt2phys(struct target
*target
,
665 uint32_t virtual, uint32_t *physical
)
671 static int no_mmu(struct target
*target
, int *enabled
)
677 static int default_examine(struct target
*target
)
679 target_set_examined(target
);
683 /* no check by default */
684 static int default_check_reset(struct target
*target
)
689 int target_examine_one(struct target
*target
)
691 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
693 int retval
= target
->type
->examine(target
);
694 if (retval
!= ERROR_OK
)
697 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
702 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
704 struct target
*target
= priv
;
706 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
709 jtag_unregister_event_callback(jtag_enable_callback
, target
);
711 return target_examine_one(target
);
714 /* Targets that correctly implement init + examine, i.e.
715 * no communication with target during init:
719 int target_examine(void)
721 int retval
= ERROR_OK
;
722 struct target
*target
;
724 for (target
= all_targets
; target
; target
= target
->next
) {
725 /* defer examination, but don't skip it */
726 if (!target
->tap
->enabled
) {
727 jtag_register_event_callback(jtag_enable_callback
,
732 retval
= target_examine_one(target
);
733 if (retval
!= ERROR_OK
)
739 const char *target_type_name(struct target
*target
)
741 return target
->type
->name
;
744 static int target_soft_reset_halt(struct target
*target
)
746 if (!target_was_examined(target
)) {
747 LOG_ERROR("Target not examined yet");
750 if (!target
->type
->soft_reset_halt
) {
751 LOG_ERROR("Target %s does not support soft_reset_halt",
752 target_name(target
));
755 return target
->type
->soft_reset_halt(target
);
759 * Downloads a target-specific native code algorithm to the target,
760 * and executes it. * Note that some targets may need to set up, enable,
761 * and tear down a breakpoint (hard or * soft) to detect algorithm
762 * termination, while others may support lower overhead schemes where
763 * soft breakpoints embedded in the algorithm automatically terminate the
766 * @param target used to run the algorithm
767 * @param arch_info target-specific description of the algorithm.
769 int target_run_algorithm(struct target
*target
,
770 int num_mem_params
, struct mem_param
*mem_params
,
771 int num_reg_params
, struct reg_param
*reg_param
,
772 uint32_t entry_point
, uint32_t exit_point
,
773 int timeout_ms
, void *arch_info
)
775 int retval
= ERROR_FAIL
;
777 if (!target_was_examined(target
)) {
778 LOG_ERROR("Target not examined yet");
781 if (!target
->type
->run_algorithm
) {
782 LOG_ERROR("Target type '%s' does not support %s",
783 target_type_name(target
), __func__
);
787 target
->running_alg
= true;
788 retval
= target
->type
->run_algorithm(target
,
789 num_mem_params
, mem_params
,
790 num_reg_params
, reg_param
,
791 entry_point
, exit_point
, timeout_ms
, arch_info
);
792 target
->running_alg
= false;
799 * Downloads a target-specific native code algorithm to the target,
800 * executes and leaves it running.
802 * @param target used to run the algorithm
803 * @param arch_info target-specific description of the algorithm.
805 int target_start_algorithm(struct target
*target
,
806 int num_mem_params
, struct mem_param
*mem_params
,
807 int num_reg_params
, struct reg_param
*reg_params
,
808 uint32_t entry_point
, uint32_t exit_point
,
811 int retval
= ERROR_FAIL
;
813 if (!target_was_examined(target
)) {
814 LOG_ERROR("Target not examined yet");
817 if (!target
->type
->start_algorithm
) {
818 LOG_ERROR("Target type '%s' does not support %s",
819 target_type_name(target
), __func__
);
822 if (target
->running_alg
) {
823 LOG_ERROR("Target is already running an algorithm");
827 target
->running_alg
= true;
828 retval
= target
->type
->start_algorithm(target
,
829 num_mem_params
, mem_params
,
830 num_reg_params
, reg_params
,
831 entry_point
, exit_point
, arch_info
);
838 * Waits for an algorithm started with target_start_algorithm() to complete.
840 * @param target used to run the algorithm
841 * @param arch_info target-specific description of the algorithm.
843 int target_wait_algorithm(struct target
*target
,
844 int num_mem_params
, struct mem_param
*mem_params
,
845 int num_reg_params
, struct reg_param
*reg_params
,
846 uint32_t exit_point
, int timeout_ms
,
849 int retval
= ERROR_FAIL
;
851 if (!target
->type
->wait_algorithm
) {
852 LOG_ERROR("Target type '%s' does not support %s",
853 target_type_name(target
), __func__
);
856 if (!target
->running_alg
) {
857 LOG_ERROR("Target is not running an algorithm");
861 retval
= target
->type
->wait_algorithm(target
,
862 num_mem_params
, mem_params
,
863 num_reg_params
, reg_params
,
864 exit_point
, timeout_ms
, arch_info
);
865 if (retval
!= ERROR_TARGET_TIMEOUT
)
866 target
->running_alg
= false;
873 * Executes a target-specific native code algorithm in the target.
874 * It differs from target_run_algorithm in that the algorithm is asynchronous.
875 * Because of this it requires an compliant algorithm:
876 * see contrib/loaders/flash/stm32f1x.S for example.
878 * @param target used to run the algorithm
881 int target_run_flash_async_algorithm(struct target
*target
,
882 const uint8_t *buffer
, uint32_t count
, int block_size
,
883 int num_mem_params
, struct mem_param
*mem_params
,
884 int num_reg_params
, struct reg_param
*reg_params
,
885 uint32_t buffer_start
, uint32_t buffer_size
,
886 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
891 const uint8_t *buffer_orig
= buffer
;
893 /* Set up working area. First word is write pointer, second word is read pointer,
894 * rest is fifo data area. */
895 uint32_t wp_addr
= buffer_start
;
896 uint32_t rp_addr
= buffer_start
+ 4;
897 uint32_t fifo_start_addr
= buffer_start
+ 8;
898 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
900 uint32_t wp
= fifo_start_addr
;
901 uint32_t rp
= fifo_start_addr
;
903 /* validate block_size is 2^n */
904 assert(!block_size
|| !(block_size
& (block_size
- 1)));
906 retval
= target_write_u32(target
, wp_addr
, wp
);
907 if (retval
!= ERROR_OK
)
909 retval
= target_write_u32(target
, rp_addr
, rp
);
910 if (retval
!= ERROR_OK
)
913 /* Start up algorithm on target and let it idle while writing the first chunk */
914 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
915 num_reg_params
, reg_params
,
920 if (retval
!= ERROR_OK
) {
921 LOG_ERROR("error starting target flash write algorithm");
927 retval
= target_read_u32(target
, rp_addr
, &rp
);
928 if (retval
!= ERROR_OK
) {
929 LOG_ERROR("failed to get read pointer");
933 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
934 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
937 LOG_ERROR("flash write algorithm aborted by target");
938 retval
= ERROR_FLASH_OPERATION_FAILED
;
942 if (((rp
- fifo_start_addr
) & (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
943 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
947 /* Count the number of bytes available in the fifo without
948 * crossing the wrap around. Make sure to not fill it completely,
949 * because that would make wp == rp and that's the empty condition. */
950 uint32_t thisrun_bytes
;
952 thisrun_bytes
= rp
- wp
- block_size
;
953 else if (rp
> fifo_start_addr
)
954 thisrun_bytes
= fifo_end_addr
- wp
;
956 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
958 if (thisrun_bytes
== 0) {
959 /* Throttle polling a bit if transfer is (much) faster than flash
960 * programming. The exact delay shouldn't matter as long as it's
961 * less than buffer size / flash speed. This is very unlikely to
962 * run when using high latency connections such as USB. */
965 /* to stop an infinite loop on some targets check and increment a timeout
966 * this issue was observed on a stellaris using the new ICDI interface */
967 if (timeout
++ >= 500) {
968 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
969 return ERROR_FLASH_OPERATION_FAILED
;
974 /* reset our timeout */
977 /* Limit to the amount of data we actually want to write */
978 if (thisrun_bytes
> count
* block_size
)
979 thisrun_bytes
= count
* block_size
;
981 /* Write data to fifo */
982 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
983 if (retval
!= ERROR_OK
)
986 /* Update counters and wrap write pointer */
987 buffer
+= thisrun_bytes
;
988 count
-= thisrun_bytes
/ block_size
;
990 if (wp
>= fifo_end_addr
)
991 wp
= fifo_start_addr
;
993 /* Store updated write pointer to target */
994 retval
= target_write_u32(target
, wp_addr
, wp
);
995 if (retval
!= ERROR_OK
)
999 if (retval
!= ERROR_OK
) {
1000 /* abort flash write algorithm on target */
1001 target_write_u32(target
, wp_addr
, 0);
1004 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1005 num_reg_params
, reg_params
,
1010 if (retval2
!= ERROR_OK
) {
1011 LOG_ERROR("error waiting for target flash write algorithm");
1018 int target_read_memory(struct target
*target
,
1019 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1021 if (!target_was_examined(target
)) {
1022 LOG_ERROR("Target not examined yet");
1025 if (!target
->type
->read_memory
) {
1026 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1029 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1032 int target_read_phys_memory(struct target
*target
,
1033 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1035 if (!target_was_examined(target
)) {
1036 LOG_ERROR("Target not examined yet");
1039 if (!target
->type
->read_phys_memory
) {
1040 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1043 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1046 int target_write_memory(struct target
*target
,
1047 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1049 if (!target_was_examined(target
)) {
1050 LOG_ERROR("Target not examined yet");
1053 if (!target
->type
->write_memory
) {
1054 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1057 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1060 int target_write_phys_memory(struct target
*target
,
1061 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1063 if (!target_was_examined(target
)) {
1064 LOG_ERROR("Target not examined yet");
1067 if (!target
->type
->write_phys_memory
) {
1068 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1071 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1074 int target_add_breakpoint(struct target
*target
,
1075 struct breakpoint
*breakpoint
)
1077 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1078 LOG_WARNING("target %s is not halted", target_name(target
));
1079 return ERROR_TARGET_NOT_HALTED
;
1081 return target
->type
->add_breakpoint(target
, breakpoint
);
1084 int target_add_context_breakpoint(struct target
*target
,
1085 struct breakpoint
*breakpoint
)
1087 if (target
->state
!= TARGET_HALTED
) {
1088 LOG_WARNING("target %s is not halted", target_name(target
));
1089 return ERROR_TARGET_NOT_HALTED
;
1091 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1094 int target_add_hybrid_breakpoint(struct target
*target
,
1095 struct breakpoint
*breakpoint
)
1097 if (target
->state
!= TARGET_HALTED
) {
1098 LOG_WARNING("target %s is not halted", target_name(target
));
1099 return ERROR_TARGET_NOT_HALTED
;
1101 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1104 int target_remove_breakpoint(struct target
*target
,
1105 struct breakpoint
*breakpoint
)
1107 return target
->type
->remove_breakpoint(target
, breakpoint
);
1110 int target_add_watchpoint(struct target
*target
,
1111 struct watchpoint
*watchpoint
)
1113 if (target
->state
!= TARGET_HALTED
) {
1114 LOG_WARNING("target %s is not halted", target_name(target
));
1115 return ERROR_TARGET_NOT_HALTED
;
1117 return target
->type
->add_watchpoint(target
, watchpoint
);
1119 int target_remove_watchpoint(struct target
*target
,
1120 struct watchpoint
*watchpoint
)
1122 return target
->type
->remove_watchpoint(target
, watchpoint
);
1124 int target_hit_watchpoint(struct target
*target
,
1125 struct watchpoint
**hit_watchpoint
)
1127 if (target
->state
!= TARGET_HALTED
) {
1128 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1129 return ERROR_TARGET_NOT_HALTED
;
1132 if (target
->type
->hit_watchpoint
== NULL
) {
1133 /* For backward compatible, if hit_watchpoint is not implemented,
1134 * return ERROR_FAIL such that gdb_server will not take the nonsense
1139 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1142 int target_get_gdb_reg_list(struct target
*target
,
1143 struct reg
**reg_list
[], int *reg_list_size
,
1144 enum target_register_class reg_class
)
1146 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1148 int target_step(struct target
*target
,
1149 int current
, uint32_t address
, int handle_breakpoints
)
1151 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1154 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1156 if (target
->state
!= TARGET_HALTED
) {
1157 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1158 return ERROR_TARGET_NOT_HALTED
;
1160 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1163 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1165 if (target
->state
!= TARGET_HALTED
) {
1166 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1167 return ERROR_TARGET_NOT_HALTED
;
1169 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1172 int target_profiling(struct target
*target
, uint32_t *samples
,
1173 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1175 if (target
->state
!= TARGET_HALTED
) {
1176 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1177 return ERROR_TARGET_NOT_HALTED
;
1179 return target
->type
->profiling(target
, samples
, max_num_samples
,
1180 num_samples
, seconds
);
1184 * Reset the @c examined flag for the given target.
1185 * Pure paranoia -- targets are zeroed on allocation.
1187 static void target_reset_examined(struct target
*target
)
1189 target
->examined
= false;
1192 static int handle_target(void *priv
);
1194 static int target_init_one(struct command_context
*cmd_ctx
,
1195 struct target
*target
)
1197 target_reset_examined(target
);
1199 struct target_type
*type
= target
->type
;
1200 if (type
->examine
== NULL
)
1201 type
->examine
= default_examine
;
1203 if (type
->check_reset
== NULL
)
1204 type
->check_reset
= default_check_reset
;
1206 assert(type
->init_target
!= NULL
);
1208 int retval
= type
->init_target(cmd_ctx
, target
);
1209 if (ERROR_OK
!= retval
) {
1210 LOG_ERROR("target '%s' init failed", target_name(target
));
1214 /* Sanity-check MMU support ... stub in what we must, to help
1215 * implement it in stages, but warn if we need to do so.
1218 if (type
->virt2phys
== NULL
) {
1219 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1220 type
->virt2phys
= identity_virt2phys
;
1223 /* Make sure no-MMU targets all behave the same: make no
1224 * distinction between physical and virtual addresses, and
1225 * ensure that virt2phys() is always an identity mapping.
1227 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1228 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1231 type
->write_phys_memory
= type
->write_memory
;
1232 type
->read_phys_memory
= type
->read_memory
;
1233 type
->virt2phys
= identity_virt2phys
;
1236 if (target
->type
->read_buffer
== NULL
)
1237 target
->type
->read_buffer
= target_read_buffer_default
;
1239 if (target
->type
->write_buffer
== NULL
)
1240 target
->type
->write_buffer
= target_write_buffer_default
;
1242 if (target
->type
->get_gdb_fileio_info
== NULL
)
1243 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1245 if (target
->type
->gdb_fileio_end
== NULL
)
1246 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1248 if (target
->type
->profiling
== NULL
)
1249 target
->type
->profiling
= target_profiling_default
;
1254 static int target_init(struct command_context
*cmd_ctx
)
1256 struct target
*target
;
1259 for (target
= all_targets
; target
; target
= target
->next
) {
1260 retval
= target_init_one(cmd_ctx
, target
);
1261 if (ERROR_OK
!= retval
)
1268 retval
= target_register_user_commands(cmd_ctx
);
1269 if (ERROR_OK
!= retval
)
1272 retval
= target_register_timer_callback(&handle_target
,
1273 polling_interval
, 1, cmd_ctx
->interp
);
1274 if (ERROR_OK
!= retval
)
1280 COMMAND_HANDLER(handle_target_init_command
)
1285 return ERROR_COMMAND_SYNTAX_ERROR
;
1287 static bool target_initialized
;
1288 if (target_initialized
) {
1289 LOG_INFO("'target init' has already been called");
1292 target_initialized
= true;
1294 retval
= command_run_line(CMD_CTX
, "init_targets");
1295 if (ERROR_OK
!= retval
)
1298 retval
= command_run_line(CMD_CTX
, "init_target_events");
1299 if (ERROR_OK
!= retval
)
1302 retval
= command_run_line(CMD_CTX
, "init_board");
1303 if (ERROR_OK
!= retval
)
1306 LOG_DEBUG("Initializing targets...");
1307 return target_init(CMD_CTX
);
1310 int target_register_event_callback(int (*callback
)(struct target
*target
,
1311 enum target_event event
, void *priv
), void *priv
)
1313 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1315 if (callback
== NULL
)
1316 return ERROR_COMMAND_SYNTAX_ERROR
;
1319 while ((*callbacks_p
)->next
)
1320 callbacks_p
= &((*callbacks_p
)->next
);
1321 callbacks_p
= &((*callbacks_p
)->next
);
1324 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1325 (*callbacks_p
)->callback
= callback
;
1326 (*callbacks_p
)->priv
= priv
;
1327 (*callbacks_p
)->next
= NULL
;
1332 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1333 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1335 struct target_reset_callback
*entry
;
1337 if (callback
== NULL
)
1338 return ERROR_COMMAND_SYNTAX_ERROR
;
1340 entry
= malloc(sizeof(struct target_reset_callback
));
1341 if (entry
== NULL
) {
1342 LOG_ERROR("error allocating buffer for reset callback entry");
1343 return ERROR_COMMAND_SYNTAX_ERROR
;
1346 entry
->callback
= callback
;
1348 list_add(&entry
->list
, &target_reset_callback_list
);
1354 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1355 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1357 struct target_trace_callback
*entry
;
1359 if (callback
== NULL
)
1360 return ERROR_COMMAND_SYNTAX_ERROR
;
1362 entry
= malloc(sizeof(struct target_trace_callback
));
1363 if (entry
== NULL
) {
1364 LOG_ERROR("error allocating buffer for trace callback entry");
1365 return ERROR_COMMAND_SYNTAX_ERROR
;
1368 entry
->callback
= callback
;
1370 list_add(&entry
->list
, &target_trace_callback_list
);
1376 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1378 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1381 if (callback
== NULL
)
1382 return ERROR_COMMAND_SYNTAX_ERROR
;
1385 while ((*callbacks_p
)->next
)
1386 callbacks_p
= &((*callbacks_p
)->next
);
1387 callbacks_p
= &((*callbacks_p
)->next
);
1390 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1391 (*callbacks_p
)->callback
= callback
;
1392 (*callbacks_p
)->periodic
= periodic
;
1393 (*callbacks_p
)->time_ms
= time_ms
;
1394 (*callbacks_p
)->removed
= false;
1396 gettimeofday(&now
, NULL
);
1397 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
1398 time_ms
-= (time_ms
% 1000);
1399 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
1400 if ((*callbacks_p
)->when
.tv_usec
> 1000000) {
1401 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
1402 (*callbacks_p
)->when
.tv_sec
+= 1;
1405 (*callbacks_p
)->priv
= priv
;
1406 (*callbacks_p
)->next
= NULL
;
1411 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1412 enum target_event event
, void *priv
), void *priv
)
1414 struct target_event_callback
**p
= &target_event_callbacks
;
1415 struct target_event_callback
*c
= target_event_callbacks
;
1417 if (callback
== NULL
)
1418 return ERROR_COMMAND_SYNTAX_ERROR
;
1421 struct target_event_callback
*next
= c
->next
;
1422 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1434 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1435 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1437 struct target_reset_callback
*entry
;
1439 if (callback
== NULL
)
1440 return ERROR_COMMAND_SYNTAX_ERROR
;
1442 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1443 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1444 list_del(&entry
->list
);
1453 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1454 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1456 struct target_trace_callback
*entry
;
1458 if (callback
== NULL
)
1459 return ERROR_COMMAND_SYNTAX_ERROR
;
1461 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1462 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1463 list_del(&entry
->list
);
1472 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1474 if (callback
== NULL
)
1475 return ERROR_COMMAND_SYNTAX_ERROR
;
1477 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1479 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1488 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1490 struct target_event_callback
*callback
= target_event_callbacks
;
1491 struct target_event_callback
*next_callback
;
1493 if (event
== TARGET_EVENT_HALTED
) {
1494 /* execute early halted first */
1495 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1498 LOG_DEBUG("target event %i (%s)", event
,
1499 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1501 target_handle_event(target
, event
);
1504 next_callback
= callback
->next
;
1505 callback
->callback(target
, event
, callback
->priv
);
1506 callback
= next_callback
;
1512 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1514 struct target_reset_callback
*callback
;
1516 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1517 Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1519 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1520 callback
->callback(target
, reset_mode
, callback
->priv
);
1525 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1527 struct target_trace_callback
*callback
;
1529 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1530 callback
->callback(target
, len
, data
, callback
->priv
);
1535 static int target_timer_callback_periodic_restart(
1536 struct target_timer_callback
*cb
, struct timeval
*now
)
1538 int time_ms
= cb
->time_ms
;
1539 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1540 time_ms
-= (time_ms
% 1000);
1541 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1542 if (cb
->when
.tv_usec
> 1000000) {
1543 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1544 cb
->when
.tv_sec
+= 1;
1549 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1550 struct timeval
*now
)
1552 cb
->callback(cb
->priv
);
1555 return target_timer_callback_periodic_restart(cb
, now
);
1557 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1560 static int target_call_timer_callbacks_check_time(int checktime
)
1562 static bool callback_processing
;
1564 /* Do not allow nesting */
1565 if (callback_processing
)
1568 callback_processing
= true;
1573 gettimeofday(&now
, NULL
);
1575 /* Store an address of the place containing a pointer to the
1576 * next item; initially, that's a standalone "root of the
1577 * list" variable. */
1578 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1580 if ((*callback
)->removed
) {
1581 struct target_timer_callback
*p
= *callback
;
1582 *callback
= (*callback
)->next
;
1587 bool call_it
= (*callback
)->callback
&&
1588 ((!checktime
&& (*callback
)->periodic
) ||
1589 now
.tv_sec
> (*callback
)->when
.tv_sec
||
1590 (now
.tv_sec
== (*callback
)->when
.tv_sec
&&
1591 now
.tv_usec
>= (*callback
)->when
.tv_usec
));
1594 target_call_timer_callback(*callback
, &now
);
1596 callback
= &(*callback
)->next
;
1599 callback_processing
= false;
1603 int target_call_timer_callbacks(void)
1605 return target_call_timer_callbacks_check_time(1);
1608 /* invoke periodic callbacks immediately */
1609 int target_call_timer_callbacks_now(void)
1611 return target_call_timer_callbacks_check_time(0);
1614 /* Prints the working area layout for debug purposes */
1615 static void print_wa_layout(struct target
*target
)
1617 struct working_area
*c
= target
->working_areas
;
1620 LOG_DEBUG("%c%c 0x%08"PRIx32
"-0x%08"PRIx32
" (%"PRIu32
" bytes)",
1621 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1622 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1627 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1628 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1630 assert(area
->free
); /* Shouldn't split an allocated area */
1631 assert(size
<= area
->size
); /* Caller should guarantee this */
1633 /* Split only if not already the right size */
1634 if (size
< area
->size
) {
1635 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1640 new_wa
->next
= area
->next
;
1641 new_wa
->size
= area
->size
- size
;
1642 new_wa
->address
= area
->address
+ size
;
1643 new_wa
->backup
= NULL
;
1644 new_wa
->user
= NULL
;
1645 new_wa
->free
= true;
1647 area
->next
= new_wa
;
1650 /* If backup memory was allocated to this area, it has the wrong size
1651 * now so free it and it will be reallocated if/when needed */
1654 area
->backup
= NULL
;
1659 /* Merge all adjacent free areas into one */
1660 static void target_merge_working_areas(struct target
*target
)
1662 struct working_area
*c
= target
->working_areas
;
1664 while (c
&& c
->next
) {
1665 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1667 /* Find two adjacent free areas */
1668 if (c
->free
&& c
->next
->free
) {
1669 /* Merge the last into the first */
1670 c
->size
+= c
->next
->size
;
1672 /* Remove the last */
1673 struct working_area
*to_be_freed
= c
->next
;
1674 c
->next
= c
->next
->next
;
1675 if (to_be_freed
->backup
)
1676 free(to_be_freed
->backup
);
1679 /* If backup memory was allocated to the remaining area, it's has
1680 * the wrong size now */
1691 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1693 /* Reevaluate working area address based on MMU state*/
1694 if (target
->working_areas
== NULL
) {
1698 retval
= target
->type
->mmu(target
, &enabled
);
1699 if (retval
!= ERROR_OK
)
1703 if (target
->working_area_phys_spec
) {
1704 LOG_DEBUG("MMU disabled, using physical "
1705 "address for working memory 0x%08"PRIx32
,
1706 target
->working_area_phys
);
1707 target
->working_area
= target
->working_area_phys
;
1709 LOG_ERROR("No working memory available. "
1710 "Specify -work-area-phys to target.");
1711 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1714 if (target
->working_area_virt_spec
) {
1715 LOG_DEBUG("MMU enabled, using virtual "
1716 "address for working memory 0x%08"PRIx32
,
1717 target
->working_area_virt
);
1718 target
->working_area
= target
->working_area_virt
;
1720 LOG_ERROR("No working memory available. "
1721 "Specify -work-area-virt to target.");
1722 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1726 /* Set up initial working area on first call */
1727 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1729 new_wa
->next
= NULL
;
1730 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1731 new_wa
->address
= target
->working_area
;
1732 new_wa
->backup
= NULL
;
1733 new_wa
->user
= NULL
;
1734 new_wa
->free
= true;
1737 target
->working_areas
= new_wa
;
1740 /* only allocate multiples of 4 byte */
1742 size
= (size
+ 3) & (~3UL);
1744 struct working_area
*c
= target
->working_areas
;
1746 /* Find the first large enough working area */
1748 if (c
->free
&& c
->size
>= size
)
1754 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1756 /* Split the working area into the requested size */
1757 target_split_working_area(c
, size
);
1759 LOG_DEBUG("allocated new working area of %"PRIu32
" bytes at address 0x%08"PRIx32
, size
, c
->address
);
1761 if (target
->backup_working_area
) {
1762 if (c
->backup
== NULL
) {
1763 c
->backup
= malloc(c
->size
);
1764 if (c
->backup
== NULL
)
1768 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1769 if (retval
!= ERROR_OK
)
1773 /* mark as used, and return the new (reused) area */
1780 print_wa_layout(target
);
1785 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1789 retval
= target_alloc_working_area_try(target
, size
, area
);
1790 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1791 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1796 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1798 int retval
= ERROR_OK
;
1800 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1801 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1802 if (retval
!= ERROR_OK
)
1803 LOG_ERROR("failed to restore %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1804 area
->size
, area
->address
);
1810 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1811 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1813 int retval
= ERROR_OK
;
1819 retval
= target_restore_working_area(target
, area
);
1820 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1821 if (retval
!= ERROR_OK
)
1827 LOG_DEBUG("freed %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1828 area
->size
, area
->address
);
1830 /* mark user pointer invalid */
1831 /* TODO: Is this really safe? It points to some previous caller's memory.
1832 * How could we know that the area pointer is still in that place and not
1833 * some other vital data? What's the purpose of this, anyway? */
1837 target_merge_working_areas(target
);
1839 print_wa_layout(target
);
1844 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1846 return target_free_working_area_restore(target
, area
, 1);
1849 void target_quit(void)
1851 struct target_event_callback
*pe
= target_event_callbacks
;
1853 struct target_event_callback
*t
= pe
->next
;
1857 target_event_callbacks
= NULL
;
1859 struct target_timer_callback
*pt
= target_timer_callbacks
;
1861 struct target_timer_callback
*t
= pt
->next
;
1865 target_timer_callbacks
= NULL
;
1867 for (struct target
*target
= all_targets
;
1868 target
; target
= target
->next
) {
1869 if (target
->type
->deinit_target
)
1870 target
->type
->deinit_target(target
);
1874 /* free resources and restore memory, if restoring memory fails,
1875 * free up resources anyway
1877 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1879 struct working_area
*c
= target
->working_areas
;
1881 LOG_DEBUG("freeing all working areas");
1883 /* Loop through all areas, restoring the allocated ones and marking them as free */
1887 target_restore_working_area(target
, c
);
1889 *c
->user
= NULL
; /* Same as above */
1895 /* Run a merge pass to combine all areas into one */
1896 target_merge_working_areas(target
);
1898 print_wa_layout(target
);
1901 void target_free_all_working_areas(struct target
*target
)
1903 target_free_all_working_areas_restore(target
, 1);
1906 /* Find the largest number of bytes that can be allocated */
1907 uint32_t target_get_working_area_avail(struct target
*target
)
1909 struct working_area
*c
= target
->working_areas
;
1910 uint32_t max_size
= 0;
1913 return target
->working_area_size
;
1916 if (c
->free
&& max_size
< c
->size
)
1925 int target_arch_state(struct target
*target
)
1928 if (target
== NULL
) {
1929 LOG_USER("No target has been configured");
1933 LOG_USER("%s: target state: %s", target_name(target
),
1934 target_state_name(target
));
1936 if (target
->state
!= TARGET_HALTED
)
1939 retval
= target
->type
->arch_state(target
);
1943 static int target_get_gdb_fileio_info_default(struct target
*target
,
1944 struct gdb_fileio_info
*fileio_info
)
1946 /* If target does not support semi-hosting function, target
1947 has no need to provide .get_gdb_fileio_info callback.
1948 It just return ERROR_FAIL and gdb_server will return "Txx"
1949 as target halted every time. */
1953 static int target_gdb_fileio_end_default(struct target
*target
,
1954 int retcode
, int fileio_errno
, bool ctrl_c
)
1959 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
1960 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1962 struct timeval timeout
, now
;
1964 gettimeofday(&timeout
, NULL
);
1965 timeval_add_time(&timeout
, seconds
, 0);
1967 LOG_INFO("Starting profiling. Halting and resuming the"
1968 " target as often as we can...");
1970 uint32_t sample_count
= 0;
1971 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
1972 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
1974 int retval
= ERROR_OK
;
1976 target_poll(target
);
1977 if (target
->state
== TARGET_HALTED
) {
1978 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
1979 samples
[sample_count
++] = t
;
1980 /* current pc, addr = 0, do not handle breakpoints, not debugging */
1981 retval
= target_resume(target
, 1, 0, 0, 0);
1982 target_poll(target
);
1983 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
1984 } else if (target
->state
== TARGET_RUNNING
) {
1985 /* We want to quickly sample the PC. */
1986 retval
= target_halt(target
);
1988 LOG_INFO("Target not halted or running");
1993 if (retval
!= ERROR_OK
)
1996 gettimeofday(&now
, NULL
);
1997 if ((sample_count
>= max_num_samples
) ||
1998 ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
))) {
1999 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2004 *num_samples
= sample_count
;
2008 /* Single aligned words are guaranteed to use 16 or 32 bit access
2009 * mode respectively, otherwise data is handled as quickly as
2012 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
2014 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
2015 (int)size
, (unsigned)address
);
2017 if (!target_was_examined(target
)) {
2018 LOG_ERROR("Target not examined yet");
2025 if ((address
+ size
- 1) < address
) {
2026 /* GDB can request this when e.g. PC is 0xfffffffc*/
2027 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
2033 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2036 static int target_write_buffer_default(struct target
*target
, uint32_t address
, uint32_t count
, const uint8_t *buffer
)
2040 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2041 * will have something to do with the size we leave to it. */
2042 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2043 if (address
& size
) {
2044 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2045 if (retval
!= ERROR_OK
)
2053 /* Write the data with as large access size as possible. */
2054 for (; size
> 0; size
/= 2) {
2055 uint32_t aligned
= count
- count
% size
;
2057 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2058 if (retval
!= ERROR_OK
)
2069 /* Single aligned words are guaranteed to use 16 or 32 bit access
2070 * mode respectively, otherwise data is handled as quickly as
2073 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
2075 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
2076 (int)size
, (unsigned)address
);
2078 if (!target_was_examined(target
)) {
2079 LOG_ERROR("Target not examined yet");
2086 if ((address
+ size
- 1) < address
) {
2087 /* GDB can request this when e.g. PC is 0xfffffffc*/
2088 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
2094 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2097 static int target_read_buffer_default(struct target
*target
, uint32_t address
, uint32_t count
, uint8_t *buffer
)
2101 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2102 * will have something to do with the size we leave to it. */
2103 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2104 if (address
& size
) {
2105 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2106 if (retval
!= ERROR_OK
)
2114 /* Read the data with as large access size as possible. */
2115 for (; size
> 0; size
/= 2) {
2116 uint32_t aligned
= count
- count
% size
;
2118 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2119 if (retval
!= ERROR_OK
)
2130 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
2135 uint32_t checksum
= 0;
2136 if (!target_was_examined(target
)) {
2137 LOG_ERROR("Target not examined yet");
2141 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2142 if (retval
!= ERROR_OK
) {
2143 buffer
= malloc(size
);
2144 if (buffer
== NULL
) {
2145 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
2146 return ERROR_COMMAND_SYNTAX_ERROR
;
2148 retval
= target_read_buffer(target
, address
, size
, buffer
);
2149 if (retval
!= ERROR_OK
) {
2154 /* convert to target endianness */
2155 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2156 uint32_t target_data
;
2157 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2158 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2161 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2170 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
2173 if (!target_was_examined(target
)) {
2174 LOG_ERROR("Target not examined yet");
2178 if (target
->type
->blank_check_memory
== 0)
2179 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2181 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
2186 int target_read_u64(struct target
*target
, uint64_t address
, uint64_t *value
)
2188 uint8_t value_buf
[8];
2189 if (!target_was_examined(target
)) {
2190 LOG_ERROR("Target not examined yet");
2194 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2196 if (retval
== ERROR_OK
) {
2197 *value
= target_buffer_get_u64(target
, value_buf
);
2198 LOG_DEBUG("address: 0x%" PRIx64
", value: 0x%16.16" PRIx64
"",
2203 LOG_DEBUG("address: 0x%" PRIx64
" failed",
2210 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
2212 uint8_t value_buf
[4];
2213 if (!target_was_examined(target
)) {
2214 LOG_ERROR("Target not examined yet");
2218 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2220 if (retval
== ERROR_OK
) {
2221 *value
= target_buffer_get_u32(target
, value_buf
);
2222 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
2227 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2234 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
2236 uint8_t value_buf
[2];
2237 if (!target_was_examined(target
)) {
2238 LOG_ERROR("Target not examined yet");
2242 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2244 if (retval
== ERROR_OK
) {
2245 *value
= target_buffer_get_u16(target
, value_buf
);
2246 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
2251 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2258 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
2260 if (!target_was_examined(target
)) {
2261 LOG_ERROR("Target not examined yet");
2265 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2267 if (retval
== ERROR_OK
) {
2268 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
2273 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2280 int target_write_u64(struct target
*target
, uint64_t address
, uint64_t value
)
2283 uint8_t value_buf
[8];
2284 if (!target_was_examined(target
)) {
2285 LOG_ERROR("Target not examined yet");
2289 LOG_DEBUG("address: 0x%" PRIx64
", value: 0x%16.16" PRIx64
"",
2293 target_buffer_set_u64(target
, value_buf
, value
);
2294 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2295 if (retval
!= ERROR_OK
)
2296 LOG_DEBUG("failed: %i", retval
);
2301 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
2304 uint8_t value_buf
[4];
2305 if (!target_was_examined(target
)) {
2306 LOG_ERROR("Target not examined yet");
2310 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
2314 target_buffer_set_u32(target
, value_buf
, value
);
2315 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2316 if (retval
!= ERROR_OK
)
2317 LOG_DEBUG("failed: %i", retval
);
2322 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
2325 uint8_t value_buf
[2];
2326 if (!target_was_examined(target
)) {
2327 LOG_ERROR("Target not examined yet");
2331 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
2335 target_buffer_set_u16(target
, value_buf
, value
);
2336 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2337 if (retval
!= ERROR_OK
)
2338 LOG_DEBUG("failed: %i", retval
);
2343 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
2346 if (!target_was_examined(target
)) {
2347 LOG_ERROR("Target not examined yet");
2351 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
2354 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2355 if (retval
!= ERROR_OK
)
2356 LOG_DEBUG("failed: %i", retval
);
2361 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2363 struct target
*target
= get_target(name
);
2364 if (target
== NULL
) {
2365 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2368 if (!target
->tap
->enabled
) {
2369 LOG_USER("Target: TAP %s is disabled, "
2370 "can't be the current target\n",
2371 target
->tap
->dotted_name
);
2375 cmd_ctx
->current_target
= target
->target_number
;
2380 COMMAND_HANDLER(handle_targets_command
)
2382 int retval
= ERROR_OK
;
2383 if (CMD_ARGC
== 1) {
2384 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2385 if (retval
== ERROR_OK
) {
2391 struct target
*target
= all_targets
;
2392 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2393 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2398 if (target
->tap
->enabled
)
2399 state
= target_state_name(target
);
2401 state
= "tap-disabled";
2403 if (CMD_CTX
->current_target
== target
->target_number
)
2406 /* keep columns lined up to match the headers above */
2407 command_print(CMD_CTX
,
2408 "%2d%c %-18s %-10s %-6s %-18s %s",
2409 target
->target_number
,
2411 target_name(target
),
2412 target_type_name(target
),
2413 Jim_Nvp_value2name_simple(nvp_target_endian
,
2414 target
->endianness
)->name
,
2415 target
->tap
->dotted_name
,
2417 target
= target
->next
;
2423 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2425 static int powerDropout
;
2426 static int srstAsserted
;
2428 static int runPowerRestore
;
2429 static int runPowerDropout
;
2430 static int runSrstAsserted
;
2431 static int runSrstDeasserted
;
2433 static int sense_handler(void)
2435 static int prevSrstAsserted
;
2436 static int prevPowerdropout
;
2438 int retval
= jtag_power_dropout(&powerDropout
);
2439 if (retval
!= ERROR_OK
)
2443 powerRestored
= prevPowerdropout
&& !powerDropout
;
2445 runPowerRestore
= 1;
2447 long long current
= timeval_ms();
2448 static long long lastPower
;
2449 int waitMore
= lastPower
+ 2000 > current
;
2450 if (powerDropout
&& !waitMore
) {
2451 runPowerDropout
= 1;
2452 lastPower
= current
;
2455 retval
= jtag_srst_asserted(&srstAsserted
);
2456 if (retval
!= ERROR_OK
)
2460 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2462 static long long lastSrst
;
2463 waitMore
= lastSrst
+ 2000 > current
;
2464 if (srstDeasserted
&& !waitMore
) {
2465 runSrstDeasserted
= 1;
2469 if (!prevSrstAsserted
&& srstAsserted
)
2470 runSrstAsserted
= 1;
2472 prevSrstAsserted
= srstAsserted
;
2473 prevPowerdropout
= powerDropout
;
2475 if (srstDeasserted
|| powerRestored
) {
2476 /* Other than logging the event we can't do anything here.
2477 * Issuing a reset is a particularly bad idea as we might
2478 * be inside a reset already.
2485 /* process target state changes */
2486 static int handle_target(void *priv
)
2488 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2489 int retval
= ERROR_OK
;
2491 if (!is_jtag_poll_safe()) {
2492 /* polling is disabled currently */
2496 /* we do not want to recurse here... */
2497 static int recursive
;
2501 /* danger! running these procedures can trigger srst assertions and power dropouts.
2502 * We need to avoid an infinite loop/recursion here and we do that by
2503 * clearing the flags after running these events.
2505 int did_something
= 0;
2506 if (runSrstAsserted
) {
2507 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2508 Jim_Eval(interp
, "srst_asserted");
2511 if (runSrstDeasserted
) {
2512 Jim_Eval(interp
, "srst_deasserted");
2515 if (runPowerDropout
) {
2516 LOG_INFO("Power dropout detected, running power_dropout proc.");
2517 Jim_Eval(interp
, "power_dropout");
2520 if (runPowerRestore
) {
2521 Jim_Eval(interp
, "power_restore");
2525 if (did_something
) {
2526 /* clear detect flags */
2530 /* clear action flags */
2532 runSrstAsserted
= 0;
2533 runSrstDeasserted
= 0;
2534 runPowerRestore
= 0;
2535 runPowerDropout
= 0;
2540 /* Poll targets for state changes unless that's globally disabled.
2541 * Skip targets that are currently disabled.
2543 for (struct target
*target
= all_targets
;
2544 is_jtag_poll_safe() && target
;
2545 target
= target
->next
) {
2547 if (!target_was_examined(target
))
2550 if (!target
->tap
->enabled
)
2553 if (target
->backoff
.times
> target
->backoff
.count
) {
2554 /* do not poll this time as we failed previously */
2555 target
->backoff
.count
++;
2558 target
->backoff
.count
= 0;
2560 /* only poll target if we've got power and srst isn't asserted */
2561 if (!powerDropout
&& !srstAsserted
) {
2562 /* polling may fail silently until the target has been examined */
2563 retval
= target_poll(target
);
2564 if (retval
!= ERROR_OK
) {
2565 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2566 if (target
->backoff
.times
* polling_interval
< 5000) {
2567 target
->backoff
.times
*= 2;
2568 target
->backoff
.times
++;
2571 /* Tell GDB to halt the debugger. This allows the user to
2572 * run monitor commands to handle the situation.
2574 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2576 if (target
->backoff
.times
> 0) {
2577 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
2578 target_reset_examined(target
);
2579 retval
= target_examine_one(target
);
2580 /* Target examination could have failed due to unstable connection,
2581 * but we set the examined flag anyway to repoll it later */
2582 if (retval
!= ERROR_OK
) {
2583 target
->examined
= true;
2584 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2585 target
->backoff
.times
* polling_interval
);
2590 /* Since we succeeded, we reset backoff count */
2591 target
->backoff
.times
= 0;
2598 COMMAND_HANDLER(handle_reg_command
)
2600 struct target
*target
;
2601 struct reg
*reg
= NULL
;
2607 target
= get_current_target(CMD_CTX
);
2609 /* list all available registers for the current target */
2610 if (CMD_ARGC
== 0) {
2611 struct reg_cache
*cache
= target
->reg_cache
;
2617 command_print(CMD_CTX
, "===== %s", cache
->name
);
2619 for (i
= 0, reg
= cache
->reg_list
;
2620 i
< cache
->num_regs
;
2621 i
++, reg
++, count
++) {
2622 /* only print cached values if they are valid */
2624 value
= buf_to_str(reg
->value
,
2626 command_print(CMD_CTX
,
2627 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2635 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2640 cache
= cache
->next
;
2646 /* access a single register by its ordinal number */
2647 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2649 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2651 struct reg_cache
*cache
= target
->reg_cache
;
2655 for (i
= 0; i
< cache
->num_regs
; i
++) {
2656 if (count
++ == num
) {
2657 reg
= &cache
->reg_list
[i
];
2663 cache
= cache
->next
;
2667 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2668 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2672 /* access a single register by its name */
2673 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2676 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2681 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2683 /* display a register */
2684 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2685 && (CMD_ARGV
[1][0] <= '9')))) {
2686 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2689 if (reg
->valid
== 0)
2690 reg
->type
->get(reg
);
2691 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2692 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2697 /* set register value */
2698 if (CMD_ARGC
== 2) {
2699 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2702 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2704 reg
->type
->set(reg
, buf
);
2706 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2707 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2715 return ERROR_COMMAND_SYNTAX_ERROR
;
2718 COMMAND_HANDLER(handle_poll_command
)
2720 int retval
= ERROR_OK
;
2721 struct target
*target
= get_current_target(CMD_CTX
);
2723 if (CMD_ARGC
== 0) {
2724 command_print(CMD_CTX
, "background polling: %s",
2725 jtag_poll_get_enabled() ? "on" : "off");
2726 command_print(CMD_CTX
, "TAP: %s (%s)",
2727 target
->tap
->dotted_name
,
2728 target
->tap
->enabled
? "enabled" : "disabled");
2729 if (!target
->tap
->enabled
)
2731 retval
= target_poll(target
);
2732 if (retval
!= ERROR_OK
)
2734 retval
= target_arch_state(target
);
2735 if (retval
!= ERROR_OK
)
2737 } else if (CMD_ARGC
== 1) {
2739 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2740 jtag_poll_set_enabled(enable
);
2742 return ERROR_COMMAND_SYNTAX_ERROR
;
2747 COMMAND_HANDLER(handle_wait_halt_command
)
2750 return ERROR_COMMAND_SYNTAX_ERROR
;
2752 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2753 if (1 == CMD_ARGC
) {
2754 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2755 if (ERROR_OK
!= retval
)
2756 return ERROR_COMMAND_SYNTAX_ERROR
;
2759 struct target
*target
= get_current_target(CMD_CTX
);
2760 return target_wait_state(target
, TARGET_HALTED
, ms
);
2763 /* wait for target state to change. The trick here is to have a low
2764 * latency for short waits and not to suck up all the CPU time
2767 * After 500ms, keep_alive() is invoked
2769 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2772 long long then
= 0, cur
;
2776 retval
= target_poll(target
);
2777 if (retval
!= ERROR_OK
)
2779 if (target
->state
== state
)
2784 then
= timeval_ms();
2785 LOG_DEBUG("waiting for target %s...",
2786 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2792 if ((cur
-then
) > ms
) {
2793 LOG_ERROR("timed out while waiting for target %s",
2794 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2802 COMMAND_HANDLER(handle_halt_command
)
2806 struct target
*target
= get_current_target(CMD_CTX
);
2807 int retval
= target_halt(target
);
2808 if (ERROR_OK
!= retval
)
2811 if (CMD_ARGC
== 1) {
2812 unsigned wait_local
;
2813 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2814 if (ERROR_OK
!= retval
)
2815 return ERROR_COMMAND_SYNTAX_ERROR
;
2820 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2823 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2825 struct target
*target
= get_current_target(CMD_CTX
);
2827 LOG_USER("requesting target halt and executing a soft reset");
2829 target_soft_reset_halt(target
);
2834 COMMAND_HANDLER(handle_reset_command
)
2837 return ERROR_COMMAND_SYNTAX_ERROR
;
2839 enum target_reset_mode reset_mode
= RESET_RUN
;
2840 if (CMD_ARGC
== 1) {
2842 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2843 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
2844 return ERROR_COMMAND_SYNTAX_ERROR
;
2845 reset_mode
= n
->value
;
2848 /* reset *all* targets */
2849 return target_process_reset(CMD_CTX
, reset_mode
);
2853 COMMAND_HANDLER(handle_resume_command
)
2857 return ERROR_COMMAND_SYNTAX_ERROR
;
2859 struct target
*target
= get_current_target(CMD_CTX
);
2861 /* with no CMD_ARGV, resume from current pc, addr = 0,
2862 * with one arguments, addr = CMD_ARGV[0],
2863 * handle breakpoints, not debugging */
2865 if (CMD_ARGC
== 1) {
2866 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2870 return target_resume(target
, current
, addr
, 1, 0);
2873 COMMAND_HANDLER(handle_step_command
)
2876 return ERROR_COMMAND_SYNTAX_ERROR
;
2880 /* with no CMD_ARGV, step from current pc, addr = 0,
2881 * with one argument addr = CMD_ARGV[0],
2882 * handle breakpoints, debugging */
2885 if (CMD_ARGC
== 1) {
2886 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2890 struct target
*target
= get_current_target(CMD_CTX
);
2892 return target
->type
->step(target
, current_pc
, addr
, 1);
2895 static void handle_md_output(struct command_context
*cmd_ctx
,
2896 struct target
*target
, uint32_t address
, unsigned size
,
2897 unsigned count
, const uint8_t *buffer
)
2899 const unsigned line_bytecnt
= 32;
2900 unsigned line_modulo
= line_bytecnt
/ size
;
2902 char output
[line_bytecnt
* 4 + 1];
2903 unsigned output_len
= 0;
2905 const char *value_fmt
;
2908 value_fmt
= "%8.8x ";
2911 value_fmt
= "%4.4x ";
2914 value_fmt
= "%2.2x ";
2917 /* "can't happen", caller checked */
2918 LOG_ERROR("invalid memory read size: %u", size
);
2922 for (unsigned i
= 0; i
< count
; i
++) {
2923 if (i
% line_modulo
== 0) {
2924 output_len
+= snprintf(output
+ output_len
,
2925 sizeof(output
) - output_len
,
2927 (unsigned)(address
+ (i
*size
)));
2931 const uint8_t *value_ptr
= buffer
+ i
* size
;
2934 value
= target_buffer_get_u32(target
, value_ptr
);
2937 value
= target_buffer_get_u16(target
, value_ptr
);
2942 output_len
+= snprintf(output
+ output_len
,
2943 sizeof(output
) - output_len
,
2946 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
2947 command_print(cmd_ctx
, "%s", output
);
2953 COMMAND_HANDLER(handle_md_command
)
2956 return ERROR_COMMAND_SYNTAX_ERROR
;
2959 switch (CMD_NAME
[2]) {
2970 return ERROR_COMMAND_SYNTAX_ERROR
;
2973 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2974 int (*fn
)(struct target
*target
,
2975 uint32_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
2979 fn
= target_read_phys_memory
;
2981 fn
= target_read_memory
;
2982 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2983 return ERROR_COMMAND_SYNTAX_ERROR
;
2986 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2990 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2992 uint8_t *buffer
= calloc(count
, size
);
2994 struct target
*target
= get_current_target(CMD_CTX
);
2995 int retval
= fn(target
, address
, size
, count
, buffer
);
2996 if (ERROR_OK
== retval
)
2997 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
3004 typedef int (*target_write_fn
)(struct target
*target
,
3005 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
3007 static int target_fill_mem(struct target
*target
,
3016 /* We have to write in reasonably large chunks to be able
3017 * to fill large memory areas with any sane speed */
3018 const unsigned chunk_size
= 16384;
3019 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
3020 if (target_buf
== NULL
) {
3021 LOG_ERROR("Out of memory");
3025 for (unsigned i
= 0; i
< chunk_size
; i
++) {
3026 switch (data_size
) {
3028 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
3031 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
3034 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
3041 int retval
= ERROR_OK
;
3043 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
3046 if (current
> chunk_size
)
3047 current
= chunk_size
;
3048 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
3049 if (retval
!= ERROR_OK
)
3051 /* avoid GDB timeouts */
3060 COMMAND_HANDLER(handle_mw_command
)
3063 return ERROR_COMMAND_SYNTAX_ERROR
;
3064 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3069 fn
= target_write_phys_memory
;
3071 fn
= target_write_memory
;
3072 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
3073 return ERROR_COMMAND_SYNTAX_ERROR
;
3076 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
3079 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
3083 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
3085 struct target
*target
= get_current_target(CMD_CTX
);
3087 switch (CMD_NAME
[2]) {
3098 return ERROR_COMMAND_SYNTAX_ERROR
;
3101 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
3104 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
3105 uint32_t *min_address
, uint32_t *max_address
)
3107 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
3108 return ERROR_COMMAND_SYNTAX_ERROR
;
3110 /* a base address isn't always necessary,
3111 * default to 0x0 (i.e. don't relocate) */
3112 if (CMD_ARGC
>= 2) {
3114 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
3115 image
->base_address
= addr
;
3116 image
->base_address_set
= 1;
3118 image
->base_address_set
= 0;
3120 image
->start_address_set
= 0;
3123 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
3124 if (CMD_ARGC
== 5) {
3125 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
3126 /* use size (given) to find max (required) */
3127 *max_address
+= *min_address
;
3130 if (*min_address
> *max_address
)
3131 return ERROR_COMMAND_SYNTAX_ERROR
;
3136 COMMAND_HANDLER(handle_load_image_command
)
3140 uint32_t image_size
;
3141 uint32_t min_address
= 0;
3142 uint32_t max_address
= 0xffffffff;
3146 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
3147 &image
, &min_address
, &max_address
);
3148 if (ERROR_OK
!= retval
)
3151 struct target
*target
= get_current_target(CMD_CTX
);
3153 struct duration bench
;
3154 duration_start(&bench
);
3156 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
3161 for (i
= 0; i
< image
.num_sections
; i
++) {
3162 buffer
= malloc(image
.sections
[i
].size
);
3163 if (buffer
== NULL
) {
3164 command_print(CMD_CTX
,
3165 "error allocating buffer for section (%d bytes)",
3166 (int)(image
.sections
[i
].size
));
3170 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3171 if (retval
!= ERROR_OK
) {
3176 uint32_t offset
= 0;
3177 uint32_t length
= buf_cnt
;
3179 /* DANGER!!! beware of unsigned comparision here!!! */
3181 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3182 (image
.sections
[i
].base_address
< max_address
)) {
3184 if (image
.sections
[i
].base_address
< min_address
) {
3185 /* clip addresses below */
3186 offset
+= min_address
-image
.sections
[i
].base_address
;
3190 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3191 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3193 retval
= target_write_buffer(target
,
3194 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3195 if (retval
!= ERROR_OK
) {
3199 image_size
+= length
;
3200 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
3201 (unsigned int)length
,
3202 image
.sections
[i
].base_address
+ offset
);
3208 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3209 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
3210 "in %fs (%0.3f KiB/s)", image_size
,
3211 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3214 image_close(&image
);
3220 COMMAND_HANDLER(handle_dump_image_command
)
3222 struct fileio fileio
;
3224 int retval
, retvaltemp
;
3225 uint32_t address
, size
;
3226 struct duration bench
;
3227 struct target
*target
= get_current_target(CMD_CTX
);
3230 return ERROR_COMMAND_SYNTAX_ERROR
;
3232 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
3233 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
3235 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
3236 buffer
= malloc(buf_size
);
3240 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3241 if (retval
!= ERROR_OK
) {
3246 duration_start(&bench
);
3249 size_t size_written
;
3250 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
3251 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
3252 if (retval
!= ERROR_OK
)
3255 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
3256 if (retval
!= ERROR_OK
)
3259 size
-= this_run_size
;
3260 address
+= this_run_size
;
3265 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3267 retval
= fileio_size(&fileio
, &filesize
);
3268 if (retval
!= ERROR_OK
)
3270 command_print(CMD_CTX
,
3271 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize
,
3272 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3275 retvaltemp
= fileio_close(&fileio
);
3276 if (retvaltemp
!= ERROR_OK
)
3282 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
3286 uint32_t image_size
;
3289 uint32_t checksum
= 0;
3290 uint32_t mem_checksum
= 0;
3294 struct target
*target
= get_current_target(CMD_CTX
);
3297 return ERROR_COMMAND_SYNTAX_ERROR
;
3300 LOG_ERROR("no target selected");
3304 struct duration bench
;
3305 duration_start(&bench
);
3307 if (CMD_ARGC
>= 2) {
3309 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
3310 image
.base_address
= addr
;
3311 image
.base_address_set
= 1;
3313 image
.base_address_set
= 0;
3314 image
.base_address
= 0x0;
3317 image
.start_address_set
= 0;
3319 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3320 if (retval
!= ERROR_OK
)
3326 for (i
= 0; i
< image
.num_sections
; i
++) {
3327 buffer
= malloc(image
.sections
[i
].size
);
3328 if (buffer
== NULL
) {
3329 command_print(CMD_CTX
,
3330 "error allocating buffer for section (%d bytes)",
3331 (int)(image
.sections
[i
].size
));
3334 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3335 if (retval
!= ERROR_OK
) {
3341 /* calculate checksum of image */
3342 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3343 if (retval
!= ERROR_OK
) {
3348 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3349 if (retval
!= ERROR_OK
) {
3354 if (checksum
!= mem_checksum
) {
3355 /* failed crc checksum, fall back to a binary compare */
3359 LOG_ERROR("checksum mismatch - attempting binary compare");
3361 data
= malloc(buf_cnt
);
3363 /* Can we use 32bit word accesses? */
3365 int count
= buf_cnt
;
3366 if ((count
% 4) == 0) {
3370 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
3371 if (retval
== ERROR_OK
) {
3373 for (t
= 0; t
< buf_cnt
; t
++) {
3374 if (data
[t
] != buffer
[t
]) {
3375 command_print(CMD_CTX
,
3376 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3378 (unsigned)(t
+ image
.sections
[i
].base_address
),
3381 if (diffs
++ >= 127) {
3382 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
3394 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
3395 image
.sections
[i
].base_address
,
3400 image_size
+= buf_cnt
;
3403 command_print(CMD_CTX
, "No more differences found.");
3406 retval
= ERROR_FAIL
;
3407 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3408 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3409 "in %fs (%0.3f KiB/s)", image_size
,
3410 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3413 image_close(&image
);
3418 COMMAND_HANDLER(handle_verify_image_command
)
3420 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
3423 COMMAND_HANDLER(handle_test_image_command
)
3425 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
3428 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
3430 struct target
*target
= get_current_target(cmd_ctx
);
3431 struct breakpoint
*breakpoint
= target
->breakpoints
;
3432 while (breakpoint
) {
3433 if (breakpoint
->type
== BKPT_SOFT
) {
3434 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3435 breakpoint
->length
, 16);
3436 command_print(cmd_ctx
, "IVA breakpoint: 0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
3437 breakpoint
->address
,
3439 breakpoint
->set
, buf
);
3442 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3443 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3445 breakpoint
->length
, breakpoint
->set
);
3446 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3447 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3448 breakpoint
->address
,
3449 breakpoint
->length
, breakpoint
->set
);
3450 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3453 command_print(cmd_ctx
, "Breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3454 breakpoint
->address
,
3455 breakpoint
->length
, breakpoint
->set
);
3458 breakpoint
= breakpoint
->next
;
3463 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
3464 uint32_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3466 struct target
*target
= get_current_target(cmd_ctx
);
3470 retval
= breakpoint_add(target
, addr
, length
, hw
);
3471 if (ERROR_OK
== retval
)
3472 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
3474 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3477 } else if (addr
== 0) {
3478 if (target
->type
->add_context_breakpoint
== NULL
) {
3479 LOG_WARNING("Context breakpoint not available");
3482 retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3483 if (ERROR_OK
== retval
)
3484 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3486 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3490 if (target
->type
->add_hybrid_breakpoint
== NULL
) {
3491 LOG_WARNING("Hybrid breakpoint not available");
3494 retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3495 if (ERROR_OK
== retval
)
3496 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3498 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3505 COMMAND_HANDLER(handle_bp_command
)
3514 return handle_bp_command_list(CMD_CTX
);
3518 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3519 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3520 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3523 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
3525 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3527 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3530 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3531 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
3533 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
3534 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3536 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3541 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3542 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
3543 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
3544 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3547 return ERROR_COMMAND_SYNTAX_ERROR
;
3551 COMMAND_HANDLER(handle_rbp_command
)
3554 return ERROR_COMMAND_SYNTAX_ERROR
;
3557 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3559 struct target
*target
= get_current_target(CMD_CTX
);
3560 breakpoint_remove(target
, addr
);
3565 COMMAND_HANDLER(handle_wp_command
)
3567 struct target
*target
= get_current_target(CMD_CTX
);
3569 if (CMD_ARGC
== 0) {
3570 struct watchpoint
*watchpoint
= target
->watchpoints
;
3572 while (watchpoint
) {
3573 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
3574 ", len: 0x%8.8" PRIx32
3575 ", r/w/a: %i, value: 0x%8.8" PRIx32
3576 ", mask: 0x%8.8" PRIx32
,
3577 watchpoint
->address
,
3579 (int)watchpoint
->rw
,
3582 watchpoint
= watchpoint
->next
;
3587 enum watchpoint_rw type
= WPT_ACCESS
;
3589 uint32_t length
= 0;
3590 uint32_t data_value
= 0x0;
3591 uint32_t data_mask
= 0xffffffff;
3595 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
3598 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3601 switch (CMD_ARGV
[2][0]) {
3612 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3613 return ERROR_COMMAND_SYNTAX_ERROR
;
3617 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3618 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3622 return ERROR_COMMAND_SYNTAX_ERROR
;
3625 int retval
= watchpoint_add(target
, addr
, length
, type
,
3626 data_value
, data_mask
);
3627 if (ERROR_OK
!= retval
)
3628 LOG_ERROR("Failure setting watchpoints");
3633 COMMAND_HANDLER(handle_rwp_command
)
3636 return ERROR_COMMAND_SYNTAX_ERROR
;
3639 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3641 struct target
*target
= get_current_target(CMD_CTX
);
3642 watchpoint_remove(target
, addr
);
3648 * Translate a virtual address to a physical address.
3650 * The low-level target implementation must have logged a detailed error
3651 * which is forwarded to telnet/GDB session.
3653 COMMAND_HANDLER(handle_virt2phys_command
)
3656 return ERROR_COMMAND_SYNTAX_ERROR
;
3659 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
3662 struct target
*target
= get_current_target(CMD_CTX
);
3663 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3664 if (retval
== ERROR_OK
)
3665 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
3670 static void writeData(FILE *f
, const void *data
, size_t len
)
3672 size_t written
= fwrite(data
, 1, len
, f
);
3674 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3677 static void writeLong(FILE *f
, int l
, struct target
*target
)
3681 target_buffer_set_u32(target
, val
, l
);
3682 writeData(f
, val
, 4);
3685 static void writeString(FILE *f
, char *s
)
3687 writeData(f
, s
, strlen(s
));
3690 typedef unsigned char UNIT
[2]; /* unit of profiling */
3692 /* Dump a gmon.out histogram file. */
3693 static void write_gmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
, bool with_range
,
3694 uint32_t start_address
, uint32_t end_address
, struct target
*target
)
3697 FILE *f
= fopen(filename
, "w");
3700 writeString(f
, "gmon");
3701 writeLong(f
, 0x00000001, target
); /* Version */
3702 writeLong(f
, 0, target
); /* padding */
3703 writeLong(f
, 0, target
); /* padding */
3704 writeLong(f
, 0, target
); /* padding */
3706 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3707 writeData(f
, &zero
, 1);
3709 /* figure out bucket size */
3713 min
= start_address
;
3718 for (i
= 0; i
< sampleNum
; i
++) {
3719 if (min
> samples
[i
])
3721 if (max
< samples
[i
])
3725 /* max should be (largest sample + 1)
3726 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
3730 int addressSpace
= max
- min
;
3731 assert(addressSpace
>= 2);
3733 /* FIXME: What is the reasonable number of buckets?
3734 * The profiling result will be more accurate if there are enough buckets. */
3735 static const uint32_t maxBuckets
= 128 * 1024; /* maximum buckets. */
3736 uint32_t numBuckets
= addressSpace
/ sizeof(UNIT
);
3737 if (numBuckets
> maxBuckets
)
3738 numBuckets
= maxBuckets
;
3739 int *buckets
= malloc(sizeof(int) * numBuckets
);
3740 if (buckets
== NULL
) {
3744 memset(buckets
, 0, sizeof(int) * numBuckets
);
3745 for (i
= 0; i
< sampleNum
; i
++) {
3746 uint32_t address
= samples
[i
];
3748 if ((address
< min
) || (max
<= address
))
3751 long long a
= address
- min
;
3752 long long b
= numBuckets
;
3753 long long c
= addressSpace
;
3754 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
3758 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3759 writeLong(f
, min
, target
); /* low_pc */
3760 writeLong(f
, max
, target
); /* high_pc */
3761 writeLong(f
, numBuckets
, target
); /* # of buckets */
3762 writeLong(f
, 100, target
); /* KLUDGE! We lie, ca. 100Hz best case. */
3763 writeString(f
, "seconds");
3764 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3765 writeData(f
, &zero
, 1);
3766 writeString(f
, "s");
3768 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3770 char *data
= malloc(2 * numBuckets
);
3772 for (i
= 0; i
< numBuckets
; i
++) {
3777 data
[i
* 2] = val
&0xff;
3778 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
3781 writeData(f
, data
, numBuckets
* 2);
3789 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3790 * which will be used as a random sampling of PC */
3791 COMMAND_HANDLER(handle_profile_command
)
3793 struct target
*target
= get_current_target(CMD_CTX
);
3795 if ((CMD_ARGC
!= 2) && (CMD_ARGC
!= 4))
3796 return ERROR_COMMAND_SYNTAX_ERROR
;
3798 const uint32_t MAX_PROFILE_SAMPLE_NUM
= 10000;
3800 uint32_t num_of_samples
;
3801 int retval
= ERROR_OK
;
3803 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], offset
);
3805 uint32_t *samples
= malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM
);
3806 if (samples
== NULL
) {
3807 LOG_ERROR("No memory to store samples.");
3812 * Some cores let us sample the PC without the
3813 * annoying halt/resume step; for example, ARMv7 PCSR.
3814 * Provide a way to use that more efficient mechanism.
3816 retval
= target_profiling(target
, samples
, MAX_PROFILE_SAMPLE_NUM
,
3817 &num_of_samples
, offset
);
3818 if (retval
!= ERROR_OK
) {
3823 assert(num_of_samples
<= MAX_PROFILE_SAMPLE_NUM
);
3825 retval
= target_poll(target
);
3826 if (retval
!= ERROR_OK
) {
3830 if (target
->state
== TARGET_RUNNING
) {
3831 retval
= target_halt(target
);
3832 if (retval
!= ERROR_OK
) {
3838 retval
= target_poll(target
);
3839 if (retval
!= ERROR_OK
) {
3844 uint32_t start_address
= 0;
3845 uint32_t end_address
= 0;
3846 bool with_range
= false;
3847 if (CMD_ARGC
== 4) {
3849 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], start_address
);
3850 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], end_address
);
3853 write_gmon(samples
, num_of_samples
, CMD_ARGV
[1],
3854 with_range
, start_address
, end_address
, target
);
3855 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3861 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
3864 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3867 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3871 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3872 valObjPtr
= Jim_NewIntObj(interp
, val
);
3873 if (!nameObjPtr
|| !valObjPtr
) {
3878 Jim_IncrRefCount(nameObjPtr
);
3879 Jim_IncrRefCount(valObjPtr
);
3880 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3881 Jim_DecrRefCount(interp
, nameObjPtr
);
3882 Jim_DecrRefCount(interp
, valObjPtr
);
3884 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3888 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3890 struct command_context
*context
;
3891 struct target
*target
;
3893 context
= current_command_context(interp
);
3894 assert(context
!= NULL
);
3896 target
= get_current_target(context
);
3897 if (target
== NULL
) {
3898 LOG_ERROR("mem2array: no current target");
3902 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
3905 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3913 const char *varname
;
3917 /* argv[1] = name of array to receive the data
3918 * argv[2] = desired width
3919 * argv[3] = memory address
3920 * argv[4] = count of times to read
3923 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3926 varname
= Jim_GetString(argv
[0], &len
);
3927 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3929 e
= Jim_GetLong(interp
, argv
[1], &l
);
3934 e
= Jim_GetLong(interp
, argv
[2], &l
);
3938 e
= Jim_GetLong(interp
, argv
[3], &l
);
3953 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3954 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3958 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3959 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3962 if ((addr
+ (len
* width
)) < addr
) {
3963 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3964 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3967 /* absurd transfer size? */
3969 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3970 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3975 ((width
== 2) && ((addr
& 1) == 0)) ||
3976 ((width
== 4) && ((addr
& 3) == 0))) {
3980 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3981 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3984 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3993 size_t buffersize
= 4096;
3994 uint8_t *buffer
= malloc(buffersize
);
4001 /* Slurp... in buffer size chunks */
4003 count
= len
; /* in objects.. */
4004 if (count
> (buffersize
/ width
))
4005 count
= (buffersize
/ width
);
4007 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
4008 if (retval
!= ERROR_OK
) {
4010 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
4014 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4015 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
4019 v
= 0; /* shut up gcc */
4020 for (i
= 0; i
< count
; i
++, n
++) {
4023 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
4026 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
4029 v
= buffer
[i
] & 0x0ff;
4032 new_int_array_element(interp
, varname
, n
, v
);
4035 addr
+= count
* width
;
4041 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4046 static int get_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t *val
)
4049 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4053 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4057 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4063 Jim_IncrRefCount(nameObjPtr
);
4064 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
4065 Jim_DecrRefCount(interp
, nameObjPtr
);
4067 if (valObjPtr
== NULL
)
4070 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
4071 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
4076 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4078 struct command_context
*context
;
4079 struct target
*target
;
4081 context
= current_command_context(interp
);
4082 assert(context
!= NULL
);
4084 target
= get_current_target(context
);
4085 if (target
== NULL
) {
4086 LOG_ERROR("array2mem: no current target");
4090 return target_array2mem(interp
, target
, argc
-1, argv
+ 1);
4093 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
4094 int argc
, Jim_Obj
*const *argv
)
4102 const char *varname
;
4106 /* argv[1] = name of array to get the data
4107 * argv[2] = desired width
4108 * argv[3] = memory address
4109 * argv[4] = count to write
4112 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
4115 varname
= Jim_GetString(argv
[0], &len
);
4116 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4118 e
= Jim_GetLong(interp
, argv
[1], &l
);
4123 e
= Jim_GetLong(interp
, argv
[2], &l
);
4127 e
= Jim_GetLong(interp
, argv
[3], &l
);
4142 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4143 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4144 "Invalid width param, must be 8/16/32", NULL
);
4148 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4149 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4150 "array2mem: zero width read?", NULL
);
4153 if ((addr
+ (len
* width
)) < addr
) {
4154 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4155 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4156 "array2mem: addr + len - wraps to zero?", NULL
);
4159 /* absurd transfer size? */
4161 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4162 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4163 "array2mem: absurd > 64K item request", NULL
);
4168 ((width
== 2) && ((addr
& 1) == 0)) ||
4169 ((width
== 4) && ((addr
& 3) == 0))) {
4173 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4174 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
4177 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4188 size_t buffersize
= 4096;
4189 uint8_t *buffer
= malloc(buffersize
);
4194 /* Slurp... in buffer size chunks */
4196 count
= len
; /* in objects.. */
4197 if (count
> (buffersize
/ width
))
4198 count
= (buffersize
/ width
);
4200 v
= 0; /* shut up gcc */
4201 for (i
= 0; i
< count
; i
++, n
++) {
4202 get_int_array_element(interp
, varname
, n
, &v
);
4205 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
4208 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
4211 buffer
[i
] = v
& 0x0ff;
4217 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
4218 if (retval
!= ERROR_OK
) {
4220 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
4224 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4225 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
4229 addr
+= count
* width
;
4234 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4239 /* FIX? should we propagate errors here rather than printing them
4242 void target_handle_event(struct target
*target
, enum target_event e
)
4244 struct target_event_action
*teap
;
4246 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4247 if (teap
->event
== e
) {
4248 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
4249 target
->target_number
,
4250 target_name(target
),
4251 target_type_name(target
),
4253 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
4254 Jim_GetString(teap
->body
, NULL
));
4255 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
) {
4256 Jim_MakeErrorMessage(teap
->interp
);
4257 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
4264 * Returns true only if the target has a handler for the specified event.
4266 bool target_has_event_action(struct target
*target
, enum target_event event
)
4268 struct target_event_action
*teap
;
4270 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4271 if (teap
->event
== event
)
4277 enum target_cfg_param
{
4280 TCFG_WORK_AREA_VIRT
,
4281 TCFG_WORK_AREA_PHYS
,
4282 TCFG_WORK_AREA_SIZE
,
4283 TCFG_WORK_AREA_BACKUP
,
4286 TCFG_CHAIN_POSITION
,
4291 static Jim_Nvp nvp_config_opts
[] = {
4292 { .name
= "-type", .value
= TCFG_TYPE
},
4293 { .name
= "-event", .value
= TCFG_EVENT
},
4294 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
4295 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
4296 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
4297 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
4298 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
4299 { .name
= "-coreid", .value
= TCFG_COREID
},
4300 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
4301 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
4302 { .name
= "-rtos", .value
= TCFG_RTOS
},
4303 { .name
= NULL
, .value
= -1 }
4306 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
4313 /* parse config or cget options ... */
4314 while (goi
->argc
> 0) {
4315 Jim_SetEmptyResult(goi
->interp
);
4316 /* Jim_GetOpt_Debug(goi); */
4318 if (target
->type
->target_jim_configure
) {
4319 /* target defines a configure function */
4320 /* target gets first dibs on parameters */
4321 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
4330 /* otherwise we 'continue' below */
4332 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
4334 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
4340 if (goi
->isconfigure
) {
4341 Jim_SetResultFormatted(goi
->interp
,
4342 "not settable: %s", n
->name
);
4346 if (goi
->argc
!= 0) {
4347 Jim_WrongNumArgs(goi
->interp
,
4348 goi
->argc
, goi
->argv
,
4353 Jim_SetResultString(goi
->interp
,
4354 target_type_name(target
), -1);
4358 if (goi
->argc
== 0) {
4359 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
4363 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
4365 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
4369 if (goi
->isconfigure
) {
4370 if (goi
->argc
!= 1) {
4371 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
4375 if (goi
->argc
!= 0) {
4376 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
4382 struct target_event_action
*teap
;
4384 teap
= target
->event_action
;
4385 /* replace existing? */
4387 if (teap
->event
== (enum target_event
)n
->value
)
4392 if (goi
->isconfigure
) {
4393 bool replace
= true;
4396 teap
= calloc(1, sizeof(*teap
));
4399 teap
->event
= n
->value
;
4400 teap
->interp
= goi
->interp
;
4401 Jim_GetOpt_Obj(goi
, &o
);
4403 Jim_DecrRefCount(teap
->interp
, teap
->body
);
4404 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
4407 * Tcl/TK - "tk events" have a nice feature.
4408 * See the "BIND" command.
4409 * We should support that here.
4410 * You can specify %X and %Y in the event code.
4411 * The idea is: %T - target name.
4412 * The idea is: %N - target number
4413 * The idea is: %E - event name.
4415 Jim_IncrRefCount(teap
->body
);
4418 /* add to head of event list */
4419 teap
->next
= target
->event_action
;
4420 target
->event_action
= teap
;
4422 Jim_SetEmptyResult(goi
->interp
);
4426 Jim_SetEmptyResult(goi
->interp
);
4428 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
4434 case TCFG_WORK_AREA_VIRT
:
4435 if (goi
->isconfigure
) {
4436 target_free_all_working_areas(target
);
4437 e
= Jim_GetOpt_Wide(goi
, &w
);
4440 target
->working_area_virt
= w
;
4441 target
->working_area_virt_spec
= true;
4446 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
4450 case TCFG_WORK_AREA_PHYS
:
4451 if (goi
->isconfigure
) {
4452 target_free_all_working_areas(target
);
4453 e
= Jim_GetOpt_Wide(goi
, &w
);
4456 target
->working_area_phys
= w
;
4457 target
->working_area_phys_spec
= true;
4462 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
4466 case TCFG_WORK_AREA_SIZE
:
4467 if (goi
->isconfigure
) {
4468 target_free_all_working_areas(target
);
4469 e
= Jim_GetOpt_Wide(goi
, &w
);
4472 target
->working_area_size
= w
;
4477 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4481 case TCFG_WORK_AREA_BACKUP
:
4482 if (goi
->isconfigure
) {
4483 target_free_all_working_areas(target
);
4484 e
= Jim_GetOpt_Wide(goi
, &w
);
4487 /* make this exactly 1 or 0 */
4488 target
->backup_working_area
= (!!w
);
4493 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
4494 /* loop for more e*/
4499 if (goi
->isconfigure
) {
4500 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
4502 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
4505 target
->endianness
= n
->value
;
4510 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4511 if (n
->name
== NULL
) {
4512 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4513 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4515 Jim_SetResultString(goi
->interp
, n
->name
, -1);
4520 if (goi
->isconfigure
) {
4521 e
= Jim_GetOpt_Wide(goi
, &w
);
4524 target
->coreid
= (int32_t)w
;
4529 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4533 case TCFG_CHAIN_POSITION
:
4534 if (goi
->isconfigure
) {
4536 struct jtag_tap
*tap
;
4537 target_free_all_working_areas(target
);
4538 e
= Jim_GetOpt_Obj(goi
, &o_t
);
4541 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
4544 /* make this exactly 1 or 0 */
4550 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
4551 /* loop for more e*/
4554 if (goi
->isconfigure
) {
4555 e
= Jim_GetOpt_Wide(goi
, &w
);
4558 target
->dbgbase
= (uint32_t)w
;
4559 target
->dbgbase_set
= true;
4564 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
4571 int result
= rtos_create(goi
, target
);
4572 if (result
!= JIM_OK
)
4578 } /* while (goi->argc) */
4581 /* done - we return */
4585 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4589 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4590 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4591 int need_args
= 1 + goi
.isconfigure
;
4592 if (goi
.argc
< need_args
) {
4593 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4595 ? "missing: -option VALUE ..."
4596 : "missing: -option ...");
4599 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4600 return target_configure(&goi
, target
);
4603 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4605 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4608 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4610 if (goi
.argc
< 2 || goi
.argc
> 4) {
4611 Jim_SetResultFormatted(goi
.interp
,
4612 "usage: %s [phys] <address> <data> [<count>]", cmd_name
);
4617 fn
= target_write_memory
;
4620 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4622 struct Jim_Obj
*obj
;
4623 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4627 fn
= target_write_phys_memory
;
4631 e
= Jim_GetOpt_Wide(&goi
, &a
);
4636 e
= Jim_GetOpt_Wide(&goi
, &b
);
4641 if (goi
.argc
== 1) {
4642 e
= Jim_GetOpt_Wide(&goi
, &c
);
4647 /* all args must be consumed */
4651 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4653 if (strcasecmp(cmd_name
, "mww") == 0)
4655 else if (strcasecmp(cmd_name
, "mwh") == 0)
4657 else if (strcasecmp(cmd_name
, "mwb") == 0)
4660 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4664 return (target_fill_mem(target
, a
, fn
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4668 * @brief Reads an array of words/halfwords/bytes from target memory starting at specified address.
4670 * Usage: mdw [phys] <address> [<count>] - for 32 bit reads
4671 * mdh [phys] <address> [<count>] - for 16 bit reads
4672 * mdb [phys] <address> [<count>] - for 8 bit reads
4674 * Count defaults to 1.
4676 * Calls target_read_memory or target_read_phys_memory depending on
4677 * the presence of the "phys" argument
4678 * Reads the target memory in blocks of max. 32 bytes, and returns an array of ints formatted
4679 * to int representation in base16.
4680 * Also outputs read data in a human readable form using command_print
4682 * @param phys if present target_read_phys_memory will be used instead of target_read_memory
4683 * @param address address where to start the read. May be specified in decimal or hex using the standard "0x" prefix
4684 * @param count optional count parameter to read an array of values. If not specified, defaults to 1.
4685 * @returns: JIM_ERR on error or JIM_OK on success and sets the result string to an array of ascii formatted numbers
4686 * on success, with [<count>] number of elements.
4688 * In case of little endian target:
4689 * Example1: "mdw 0x00000000" returns "10123456"
4690 * Exmaple2: "mdh 0x00000000 1" returns "3456"
4691 * Example3: "mdb 0x00000000" returns "56"
4692 * Example4: "mdh 0x00000000 2" returns "3456 1012"
4693 * Example5: "mdb 0x00000000 3" returns "56 34 12"
4695 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4697 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4700 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4702 if ((goi
.argc
< 1) || (goi
.argc
> 3)) {
4703 Jim_SetResultFormatted(goi
.interp
,
4704 "usage: %s [phys] <address> [<count>]", cmd_name
);
4708 int (*fn
)(struct target
*target
,
4709 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
4710 fn
= target_read_memory
;
4713 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4715 struct Jim_Obj
*obj
;
4716 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4720 fn
= target_read_phys_memory
;
4723 /* Read address parameter */
4725 e
= Jim_GetOpt_Wide(&goi
, &addr
);
4729 /* If next parameter exists, read it out as the count parameter, if not, set it to 1 (default) */
4731 if (goi
.argc
== 1) {
4732 e
= Jim_GetOpt_Wide(&goi
, &count
);
4738 /* all args must be consumed */
4742 jim_wide dwidth
= 1; /* shut up gcc */
4743 if (strcasecmp(cmd_name
, "mdw") == 0)
4745 else if (strcasecmp(cmd_name
, "mdh") == 0)
4747 else if (strcasecmp(cmd_name
, "mdb") == 0)
4750 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4754 /* convert count to "bytes" */
4755 int bytes
= count
* dwidth
;
4757 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4758 uint8_t target_buf
[32];
4761 y
= (bytes
< 16) ? bytes
: 16; /* y = min(bytes, 16); */
4763 /* Try to read out next block */
4764 e
= fn(target
, addr
, dwidth
, y
/ dwidth
, target_buf
);
4766 if (e
!= ERROR_OK
) {
4767 Jim_SetResultFormatted(interp
, "error reading target @ 0x%08lx", (long)addr
);
4771 command_print_sameline(NULL
, "0x%08x ", (int)(addr
));
4774 for (x
= 0; x
< 16 && x
< y
; x
+= 4) {
4775 z
= target_buffer_get_u32(target
, &(target_buf
[x
]));
4776 command_print_sameline(NULL
, "%08x ", (int)(z
));
4778 for (; (x
< 16) ; x
+= 4)
4779 command_print_sameline(NULL
, " ");
4782 for (x
= 0; x
< 16 && x
< y
; x
+= 2) {
4783 z
= target_buffer_get_u16(target
, &(target_buf
[x
]));
4784 command_print_sameline(NULL
, "%04x ", (int)(z
));
4786 for (; (x
< 16) ; x
+= 2)
4787 command_print_sameline(NULL
, " ");
4791 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
4792 z
= target_buffer_get_u8(target
, &(target_buf
[x
]));
4793 command_print_sameline(NULL
, "%02x ", (int)(z
));
4795 for (; (x
< 16) ; x
+= 1)
4796 command_print_sameline(NULL
, " ");
4799 /* ascii-ify the bytes */
4800 for (x
= 0 ; x
< y
; x
++) {
4801 if ((target_buf
[x
] >= 0x20) &&
4802 (target_buf
[x
] <= 0x7e)) {
4806 target_buf
[x
] = '.';
4811 target_buf
[x
] = ' ';
4816 /* print - with a newline */
4817 command_print_sameline(NULL
, "%s\n", target_buf
);
4825 static int jim_target_mem2array(Jim_Interp
*interp
,
4826 int argc
, Jim_Obj
*const *argv
)
4828 struct target
*target
= Jim_CmdPrivData(interp
);
4829 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4832 static int jim_target_array2mem(Jim_Interp
*interp
,
4833 int argc
, Jim_Obj
*const *argv
)
4835 struct target
*target
= Jim_CmdPrivData(interp
);
4836 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4839 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4841 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
4845 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4848 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4851 struct target
*target
= Jim_CmdPrivData(interp
);
4852 if (!target
->tap
->enabled
)
4853 return jim_target_tap_disabled(interp
);
4855 int e
= target
->type
->examine(target
);
4861 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4864 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4867 struct target
*target
= Jim_CmdPrivData(interp
);
4869 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
4875 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4878 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4881 struct target
*target
= Jim_CmdPrivData(interp
);
4882 if (!target
->tap
->enabled
)
4883 return jim_target_tap_disabled(interp
);
4886 if (!(target_was_examined(target
)))
4887 e
= ERROR_TARGET_NOT_EXAMINED
;
4889 e
= target
->type
->poll(target
);
4895 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4898 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4900 if (goi
.argc
!= 2) {
4901 Jim_WrongNumArgs(interp
, 0, argv
,
4902 "([tT]|[fF]|assert|deassert) BOOL");
4907 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4909 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4912 /* the halt or not param */
4914 e
= Jim_GetOpt_Wide(&goi
, &a
);
4918 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4919 if (!target
->tap
->enabled
)
4920 return jim_target_tap_disabled(interp
);
4921 if (!(target_was_examined(target
))) {
4922 LOG_ERROR("Target not examined yet");
4923 return ERROR_TARGET_NOT_EXAMINED
;
4925 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
4926 Jim_SetResultFormatted(interp
,
4927 "No target-specific reset for %s",
4928 target_name(target
));
4931 /* determine if we should halt or not. */
4932 target
->reset_halt
= !!a
;
4933 /* When this happens - all workareas are invalid. */
4934 target_free_all_working_areas_restore(target
, 0);
4937 if (n
->value
== NVP_ASSERT
)
4938 e
= target
->type
->assert_reset(target
);
4940 e
= target
->type
->deassert_reset(target
);
4941 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4944 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4947 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4950 struct target
*target
= Jim_CmdPrivData(interp
);
4951 if (!target
->tap
->enabled
)
4952 return jim_target_tap_disabled(interp
);
4953 int e
= target
->type
->halt(target
);
4954 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4957 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4960 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4962 /* params: <name> statename timeoutmsecs */
4963 if (goi
.argc
!= 2) {
4964 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4965 Jim_SetResultFormatted(goi
.interp
,
4966 "%s <state_name> <timeout_in_msec>", cmd_name
);
4971 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4973 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
, 1);
4977 e
= Jim_GetOpt_Wide(&goi
, &a
);
4980 struct target
*target
= Jim_CmdPrivData(interp
);
4981 if (!target
->tap
->enabled
)
4982 return jim_target_tap_disabled(interp
);
4984 e
= target_wait_state(target
, n
->value
, a
);
4985 if (e
!= ERROR_OK
) {
4986 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
4987 Jim_SetResultFormatted(goi
.interp
,
4988 "target: %s wait %s fails (%#s) %s",
4989 target_name(target
), n
->name
,
4990 eObj
, target_strerror_safe(e
));
4991 Jim_FreeNewObj(interp
, eObj
);
4996 /* List for human, Events defined for this target.
4997 * scripts/programs should use 'name cget -event NAME'
4999 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5001 struct command_context
*cmd_ctx
= current_command_context(interp
);
5002 assert(cmd_ctx
!= NULL
);
5004 struct target
*target
= Jim_CmdPrivData(interp
);
5005 struct target_event_action
*teap
= target
->event_action
;
5006 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
5007 target
->target_number
,
5008 target_name(target
));
5009 command_print(cmd_ctx
, "%-25s | Body", "Event");
5010 command_print(cmd_ctx
, "------------------------- | "
5011 "----------------------------------------");
5013 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
5014 command_print(cmd_ctx
, "%-25s | %s",
5015 opt
->name
, Jim_GetString(teap
->body
, NULL
));
5018 command_print(cmd_ctx
, "***END***");
5021 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5024 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5027 struct target
*target
= Jim_CmdPrivData(interp
);
5028 Jim_SetResultString(interp
, target_state_name(target
), -1);
5031 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5034 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5035 if (goi
.argc
!= 1) {
5036 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5037 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
5041 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
5043 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
5046 struct target
*target
= Jim_CmdPrivData(interp
);
5047 target_handle_event(target
, n
->value
);
5051 static const struct command_registration target_instance_command_handlers
[] = {
5053 .name
= "configure",
5054 .mode
= COMMAND_CONFIG
,
5055 .jim_handler
= jim_target_configure
,
5056 .help
= "configure a new target for use",
5057 .usage
= "[target_attribute ...]",
5061 .mode
= COMMAND_ANY
,
5062 .jim_handler
= jim_target_configure
,
5063 .help
= "returns the specified target attribute",
5064 .usage
= "target_attribute",
5068 .mode
= COMMAND_EXEC
,
5069 .jim_handler
= jim_target_mw
,
5070 .help
= "Write 32-bit word(s) to target memory",
5071 .usage
= "address data [count]",
5075 .mode
= COMMAND_EXEC
,
5076 .jim_handler
= jim_target_mw
,
5077 .help
= "Write 16-bit half-word(s) to target memory",
5078 .usage
= "address data [count]",
5082 .mode
= COMMAND_EXEC
,
5083 .jim_handler
= jim_target_mw
,
5084 .help
= "Write byte(s) to target memory",
5085 .usage
= "address data [count]",
5089 .mode
= COMMAND_EXEC
,
5090 .jim_handler
= jim_target_md
,
5091 .help
= "Display target memory as 32-bit words",
5092 .usage
= "address [count]",
5096 .mode
= COMMAND_EXEC
,
5097 .jim_handler
= jim_target_md
,
5098 .help
= "Display target memory as 16-bit half-words",
5099 .usage
= "address [count]",
5103 .mode
= COMMAND_EXEC
,
5104 .jim_handler
= jim_target_md
,
5105 .help
= "Display target memory as 8-bit bytes",
5106 .usage
= "address [count]",
5109 .name
= "array2mem",
5110 .mode
= COMMAND_EXEC
,
5111 .jim_handler
= jim_target_array2mem
,
5112 .help
= "Writes Tcl array of 8/16/32 bit numbers "
5114 .usage
= "arrayname bitwidth address count",
5117 .name
= "mem2array",
5118 .mode
= COMMAND_EXEC
,
5119 .jim_handler
= jim_target_mem2array
,
5120 .help
= "Loads Tcl array of 8/16/32 bit numbers "
5121 "from target memory",
5122 .usage
= "arrayname bitwidth address count",
5125 .name
= "eventlist",
5126 .mode
= COMMAND_EXEC
,
5127 .jim_handler
= jim_target_event_list
,
5128 .help
= "displays a table of events defined for this target",
5132 .mode
= COMMAND_EXEC
,
5133 .jim_handler
= jim_target_current_state
,
5134 .help
= "displays the current state of this target",
5137 .name
= "arp_examine",
5138 .mode
= COMMAND_EXEC
,
5139 .jim_handler
= jim_target_examine
,
5140 .help
= "used internally for reset processing",
5143 .name
= "arp_halt_gdb",
5144 .mode
= COMMAND_EXEC
,
5145 .jim_handler
= jim_target_halt_gdb
,
5146 .help
= "used internally for reset processing to halt GDB",
5150 .mode
= COMMAND_EXEC
,
5151 .jim_handler
= jim_target_poll
,
5152 .help
= "used internally for reset processing",
5155 .name
= "arp_reset",
5156 .mode
= COMMAND_EXEC
,
5157 .jim_handler
= jim_target_reset
,
5158 .help
= "used internally for reset processing",
5162 .mode
= COMMAND_EXEC
,
5163 .jim_handler
= jim_target_halt
,
5164 .help
= "used internally for reset processing",
5167 .name
= "arp_waitstate",
5168 .mode
= COMMAND_EXEC
,
5169 .jim_handler
= jim_target_wait_state
,
5170 .help
= "used internally for reset processing",
5173 .name
= "invoke-event",
5174 .mode
= COMMAND_EXEC
,
5175 .jim_handler
= jim_target_invoke_event
,
5176 .help
= "invoke handler for specified event",
5177 .usage
= "event_name",
5179 COMMAND_REGISTRATION_DONE
5182 static int target_create(Jim_GetOptInfo
*goi
)
5190 struct target
*target
;
5191 struct command_context
*cmd_ctx
;
5193 cmd_ctx
= current_command_context(goi
->interp
);
5194 assert(cmd_ctx
!= NULL
);
5196 if (goi
->argc
< 3) {
5197 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
5202 Jim_GetOpt_Obj(goi
, &new_cmd
);
5203 /* does this command exist? */
5204 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
5206 cp
= Jim_GetString(new_cmd
, NULL
);
5207 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
5212 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
5216 struct transport
*tr
= get_current_transport();
5217 if (tr
->override_target
) {
5218 e
= tr
->override_target(&cp
);
5219 if (e
!= ERROR_OK
) {
5220 LOG_ERROR("The selected transport doesn't support this target");
5223 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
5225 /* now does target type exist */
5226 for (x
= 0 ; target_types
[x
] ; x
++) {
5227 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
5232 /* check for deprecated name */
5233 if (target_types
[x
]->deprecated_name
) {
5234 if (0 == strcmp(cp
, target_types
[x
]->deprecated_name
)) {
5236 LOG_WARNING("target name is deprecated use: \'%s\'", target_types
[x
]->name
);
5241 if (target_types
[x
] == NULL
) {
5242 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
5243 for (x
= 0 ; target_types
[x
] ; x
++) {
5244 if (target_types
[x
+ 1]) {
5245 Jim_AppendStrings(goi
->interp
,
5246 Jim_GetResult(goi
->interp
),
5247 target_types
[x
]->name
,
5250 Jim_AppendStrings(goi
->interp
,
5251 Jim_GetResult(goi
->interp
),
5253 target_types
[x
]->name
, NULL
);
5260 target
= calloc(1, sizeof(struct target
));
5261 /* set target number */
5262 target
->target_number
= new_target_number();
5263 cmd_ctx
->current_target
= target
->target_number
;
5265 /* allocate memory for each unique target type */
5266 target
->type
= calloc(1, sizeof(struct target_type
));
5268 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
5270 /* will be set by "-endian" */
5271 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5273 /* default to first core, override with -coreid */
5276 target
->working_area
= 0x0;
5277 target
->working_area_size
= 0x0;
5278 target
->working_areas
= NULL
;
5279 target
->backup_working_area
= 0;
5281 target
->state
= TARGET_UNKNOWN
;
5282 target
->debug_reason
= DBG_REASON_UNDEFINED
;
5283 target
->reg_cache
= NULL
;
5284 target
->breakpoints
= NULL
;
5285 target
->watchpoints
= NULL
;
5286 target
->next
= NULL
;
5287 target
->arch_info
= NULL
;
5289 target
->display
= 1;
5291 target
->halt_issued
= false;
5293 /* initialize trace information */
5294 target
->trace_info
= malloc(sizeof(struct trace
));
5295 target
->trace_info
->num_trace_points
= 0;
5296 target
->trace_info
->trace_points_size
= 0;
5297 target
->trace_info
->trace_points
= NULL
;
5298 target
->trace_info
->trace_history_size
= 0;
5299 target
->trace_info
->trace_history
= NULL
;
5300 target
->trace_info
->trace_history_pos
= 0;
5301 target
->trace_info
->trace_history_overflowed
= 0;
5303 target
->dbgmsg
= NULL
;
5304 target
->dbg_msg_enabled
= 0;
5306 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5308 target
->rtos
= NULL
;
5309 target
->rtos_auto_detect
= false;
5311 /* Do the rest as "configure" options */
5312 goi
->isconfigure
= 1;
5313 e
= target_configure(goi
, target
);
5315 if (target
->tap
== NULL
) {
5316 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
5326 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
5327 /* default endian to little if not specified */
5328 target
->endianness
= TARGET_LITTLE_ENDIAN
;
5331 cp
= Jim_GetString(new_cmd
, NULL
);
5332 target
->cmd_name
= strdup(cp
);
5334 /* create the target specific commands */
5335 if (target
->type
->commands
) {
5336 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
5338 LOG_ERROR("unable to register '%s' commands", cp
);
5340 if (target
->type
->target_create
)
5341 (*(target
->type
->target_create
))(target
, goi
->interp
);
5343 /* append to end of list */
5345 struct target
**tpp
;
5346 tpp
= &(all_targets
);
5348 tpp
= &((*tpp
)->next
);
5352 /* now - create the new target name command */
5353 const struct command_registration target_subcommands
[] = {
5355 .chain
= target_instance_command_handlers
,
5358 .chain
= target
->type
->commands
,
5360 COMMAND_REGISTRATION_DONE
5362 const struct command_registration target_commands
[] = {
5365 .mode
= COMMAND_ANY
,
5366 .help
= "target command group",
5368 .chain
= target_subcommands
,
5370 COMMAND_REGISTRATION_DONE
5372 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
5376 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
5378 command_set_handler_data(c
, target
);
5380 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
5383 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5386 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5389 struct command_context
*cmd_ctx
= current_command_context(interp
);
5390 assert(cmd_ctx
!= NULL
);
5392 Jim_SetResultString(interp
, target_name(get_current_target(cmd_ctx
)), -1);
5396 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5399 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5402 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5403 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++) {
5404 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5405 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
5410 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5413 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5416 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5417 struct target
*target
= all_targets
;
5419 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5420 Jim_NewStringObj(interp
, target_name(target
), -1));
5421 target
= target
->next
;
5426 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5429 const char *targetname
;
5431 struct target
*target
= (struct target
*) NULL
;
5432 struct target_list
*head
, *curr
, *new;
5433 curr
= (struct target_list
*) NULL
;
5434 head
= (struct target_list
*) NULL
;
5437 LOG_DEBUG("%d", argc
);
5438 /* argv[1] = target to associate in smp
5439 * argv[2] = target to assoicate in smp
5443 for (i
= 1; i
< argc
; i
++) {
5445 targetname
= Jim_GetString(argv
[i
], &len
);
5446 target
= get_target(targetname
);
5447 LOG_DEBUG("%s ", targetname
);
5449 new = malloc(sizeof(struct target_list
));
5450 new->target
= target
;
5451 new->next
= (struct target_list
*)NULL
;
5452 if (head
== (struct target_list
*)NULL
) {
5461 /* now parse the list of cpu and put the target in smp mode*/
5464 while (curr
!= (struct target_list
*)NULL
) {
5465 target
= curr
->target
;
5467 target
->head
= head
;
5471 if (target
&& target
->rtos
)
5472 retval
= rtos_smp_init(head
->target
);
5478 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5481 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5483 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5484 "<name> <target_type> [<target_options> ...]");
5487 return target_create(&goi
);
5490 static const struct command_registration target_subcommand_handlers
[] = {
5493 .mode
= COMMAND_CONFIG
,
5494 .handler
= handle_target_init_command
,
5495 .help
= "initialize targets",
5499 /* REVISIT this should be COMMAND_CONFIG ... */
5500 .mode
= COMMAND_ANY
,
5501 .jim_handler
= jim_target_create
,
5502 .usage
= "name type '-chain-position' name [options ...]",
5503 .help
= "Creates and selects a new target",
5507 .mode
= COMMAND_ANY
,
5508 .jim_handler
= jim_target_current
,
5509 .help
= "Returns the currently selected target",
5513 .mode
= COMMAND_ANY
,
5514 .jim_handler
= jim_target_types
,
5515 .help
= "Returns the available target types as "
5516 "a list of strings",
5520 .mode
= COMMAND_ANY
,
5521 .jim_handler
= jim_target_names
,
5522 .help
= "Returns the names of all targets as a list of strings",
5526 .mode
= COMMAND_ANY
,
5527 .jim_handler
= jim_target_smp
,
5528 .usage
= "targetname1 targetname2 ...",
5529 .help
= "gather several target in a smp list"
5532 COMMAND_REGISTRATION_DONE
5542 static int fastload_num
;
5543 static struct FastLoad
*fastload
;
5545 static void free_fastload(void)
5547 if (fastload
!= NULL
) {
5549 for (i
= 0; i
< fastload_num
; i
++) {
5550 if (fastload
[i
].data
)
5551 free(fastload
[i
].data
);
5558 COMMAND_HANDLER(handle_fast_load_image_command
)
5562 uint32_t image_size
;
5563 uint32_t min_address
= 0;
5564 uint32_t max_address
= 0xffffffff;
5569 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
5570 &image
, &min_address
, &max_address
);
5571 if (ERROR_OK
!= retval
)
5574 struct duration bench
;
5575 duration_start(&bench
);
5577 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5578 if (retval
!= ERROR_OK
)
5583 fastload_num
= image
.num_sections
;
5584 fastload
= malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5585 if (fastload
== NULL
) {
5586 command_print(CMD_CTX
, "out of memory");
5587 image_close(&image
);
5590 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5591 for (i
= 0; i
< image
.num_sections
; i
++) {
5592 buffer
= malloc(image
.sections
[i
].size
);
5593 if (buffer
== NULL
) {
5594 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5595 (int)(image
.sections
[i
].size
));
5596 retval
= ERROR_FAIL
;
5600 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
5601 if (retval
!= ERROR_OK
) {
5606 uint32_t offset
= 0;
5607 uint32_t length
= buf_cnt
;
5609 /* DANGER!!! beware of unsigned comparision here!!! */
5611 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
5612 (image
.sections
[i
].base_address
< max_address
)) {
5613 if (image
.sections
[i
].base_address
< min_address
) {
5614 /* clip addresses below */
5615 offset
+= min_address
-image
.sections
[i
].base_address
;
5619 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5620 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5622 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5623 fastload
[i
].data
= malloc(length
);
5624 if (fastload
[i
].data
== NULL
) {
5626 command_print(CMD_CTX
, "error allocating buffer for section (%" PRIu32
" bytes)",
5628 retval
= ERROR_FAIL
;
5631 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5632 fastload
[i
].length
= length
;
5634 image_size
+= length
;
5635 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
5636 (unsigned int)length
,
5637 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
5643 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
5644 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
5645 "in %fs (%0.3f KiB/s)", image_size
,
5646 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
5648 command_print(CMD_CTX
,
5649 "WARNING: image has not been loaded to target!"
5650 "You can issue a 'fast_load' to finish loading.");
5653 image_close(&image
);
5655 if (retval
!= ERROR_OK
)
5661 COMMAND_HANDLER(handle_fast_load_command
)
5664 return ERROR_COMMAND_SYNTAX_ERROR
;
5665 if (fastload
== NULL
) {
5666 LOG_ERROR("No image in memory");
5670 int ms
= timeval_ms();
5672 int retval
= ERROR_OK
;
5673 for (i
= 0; i
< fastload_num
; i
++) {
5674 struct target
*target
= get_current_target(CMD_CTX
);
5675 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
5676 (unsigned int)(fastload
[i
].address
),
5677 (unsigned int)(fastload
[i
].length
));
5678 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5679 if (retval
!= ERROR_OK
)
5681 size
+= fastload
[i
].length
;
5683 if (retval
== ERROR_OK
) {
5684 int after
= timeval_ms();
5685 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5690 static const struct command_registration target_command_handlers
[] = {
5693 .handler
= handle_targets_command
,
5694 .mode
= COMMAND_ANY
,
5695 .help
= "change current default target (one parameter) "
5696 "or prints table of all targets (no parameters)",
5697 .usage
= "[target]",
5701 .mode
= COMMAND_CONFIG
,
5702 .help
= "configure target",
5704 .chain
= target_subcommand_handlers
,
5706 COMMAND_REGISTRATION_DONE
5709 int target_register_commands(struct command_context
*cmd_ctx
)
5711 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5714 static bool target_reset_nag
= true;
5716 bool get_target_reset_nag(void)
5718 return target_reset_nag
;
5721 COMMAND_HANDLER(handle_target_reset_nag
)
5723 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
5724 &target_reset_nag
, "Nag after each reset about options to improve "
5728 COMMAND_HANDLER(handle_ps_command
)
5730 struct target
*target
= get_current_target(CMD_CTX
);
5732 if (target
->state
!= TARGET_HALTED
) {
5733 LOG_INFO("target not halted !!");
5737 if ((target
->rtos
) && (target
->rtos
->type
)
5738 && (target
->rtos
->type
->ps_command
)) {
5739 display
= target
->rtos
->type
->ps_command(target
);
5740 command_print(CMD_CTX
, "%s", display
);
5745 return ERROR_TARGET_FAILURE
;
5749 static void binprint(struct command_context
*cmd_ctx
, const char *text
, const uint8_t *buf
, int size
)
5752 command_print_sameline(cmd_ctx
, "%s", text
);
5753 for (int i
= 0; i
< size
; i
++)
5754 command_print_sameline(cmd_ctx
, " %02x", buf
[i
]);
5755 command_print(cmd_ctx
, " ");
5758 COMMAND_HANDLER(handle_test_mem_access_command
)
5760 struct target
*target
= get_current_target(CMD_CTX
);
5762 int retval
= ERROR_OK
;
5764 if (target
->state
!= TARGET_HALTED
) {
5765 LOG_INFO("target not halted !!");
5770 return ERROR_COMMAND_SYNTAX_ERROR
;
5772 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], test_size
);
5775 size_t num_bytes
= test_size
+ 4;
5777 struct working_area
*wa
= NULL
;
5778 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
5779 if (retval
!= ERROR_OK
) {
5780 LOG_ERROR("Not enough working area");
5784 uint8_t *test_pattern
= malloc(num_bytes
);
5786 for (size_t i
= 0; i
< num_bytes
; i
++)
5787 test_pattern
[i
] = rand();
5789 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
5790 if (retval
!= ERROR_OK
) {
5791 LOG_ERROR("Test pattern write failed");
5795 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
5796 for (int size
= 1; size
<= 4; size
*= 2) {
5797 for (int offset
= 0; offset
< 4; offset
++) {
5798 uint32_t count
= test_size
/ size
;
5799 size_t host_bufsiz
= (count
+ 2) * size
+ host_offset
;
5800 uint8_t *read_ref
= malloc(host_bufsiz
);
5801 uint8_t *read_buf
= malloc(host_bufsiz
);
5803 for (size_t i
= 0; i
< host_bufsiz
; i
++) {
5804 read_ref
[i
] = rand();
5805 read_buf
[i
] = read_ref
[i
];
5807 command_print_sameline(CMD_CTX
,
5808 "Test read %" PRIu32
" x %d @ %d to %saligned buffer: ", count
,
5809 size
, offset
, host_offset
? "un" : "");
5811 struct duration bench
;
5812 duration_start(&bench
);
5814 retval
= target_read_memory(target
, wa
->address
+ offset
, size
, count
,
5815 read_buf
+ size
+ host_offset
);
5817 duration_measure(&bench
);
5819 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
5820 command_print(CMD_CTX
, "Unsupported alignment");
5822 } else if (retval
!= ERROR_OK
) {
5823 command_print(CMD_CTX
, "Memory read failed");
5827 /* replay on host */
5828 memcpy(read_ref
+ size
+ host_offset
, test_pattern
+ offset
, count
* size
);
5831 int result
= memcmp(read_ref
, read_buf
, host_bufsiz
);
5833 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
5834 duration_elapsed(&bench
),
5835 duration_kbps(&bench
, count
* size
));
5837 command_print(CMD_CTX
, "Compare failed");
5838 binprint(CMD_CTX
, "ref:", read_ref
, host_bufsiz
);
5839 binprint(CMD_CTX
, "buf:", read_buf
, host_bufsiz
);
5852 target_free_working_area(target
, wa
);
5855 num_bytes
= test_size
+ 4 + 4 + 4;
5857 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
5858 if (retval
!= ERROR_OK
) {
5859 LOG_ERROR("Not enough working area");
5863 test_pattern
= malloc(num_bytes
);
5865 for (size_t i
= 0; i
< num_bytes
; i
++)
5866 test_pattern
[i
] = rand();
5868 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
5869 for (int size
= 1; size
<= 4; size
*= 2) {
5870 for (int offset
= 0; offset
< 4; offset
++) {
5871 uint32_t count
= test_size
/ size
;
5872 size_t host_bufsiz
= count
* size
+ host_offset
;
5873 uint8_t *read_ref
= malloc(num_bytes
);
5874 uint8_t *read_buf
= malloc(num_bytes
);
5875 uint8_t *write_buf
= malloc(host_bufsiz
);
5877 for (size_t i
= 0; i
< host_bufsiz
; i
++)
5878 write_buf
[i
] = rand();
5879 command_print_sameline(CMD_CTX
,
5880 "Test write %" PRIu32
" x %d @ %d from %saligned buffer: ", count
,
5881 size
, offset
, host_offset
? "un" : "");
5883 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
5884 if (retval
!= ERROR_OK
) {
5885 command_print(CMD_CTX
, "Test pattern write failed");
5889 /* replay on host */
5890 memcpy(read_ref
, test_pattern
, num_bytes
);
5891 memcpy(read_ref
+ size
+ offset
, write_buf
+ host_offset
, count
* size
);
5893 struct duration bench
;
5894 duration_start(&bench
);
5896 retval
= target_write_memory(target
, wa
->address
+ size
+ offset
, size
, count
,
5897 write_buf
+ host_offset
);
5899 duration_measure(&bench
);
5901 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
5902 command_print(CMD_CTX
, "Unsupported alignment");
5904 } else if (retval
!= ERROR_OK
) {
5905 command_print(CMD_CTX
, "Memory write failed");
5910 retval
= target_read_memory(target
, wa
->address
, 1, num_bytes
, read_buf
);
5911 if (retval
!= ERROR_OK
) {
5912 command_print(CMD_CTX
, "Test pattern write failed");
5917 int result
= memcmp(read_ref
, read_buf
, num_bytes
);
5919 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
5920 duration_elapsed(&bench
),
5921 duration_kbps(&bench
, count
* size
));
5923 command_print(CMD_CTX
, "Compare failed");
5924 binprint(CMD_CTX
, "ref:", read_ref
, num_bytes
);
5925 binprint(CMD_CTX
, "buf:", read_buf
, num_bytes
);
5937 target_free_working_area(target
, wa
);
5941 static const struct command_registration target_exec_command_handlers
[] = {
5943 .name
= "fast_load_image",
5944 .handler
= handle_fast_load_image_command
,
5945 .mode
= COMMAND_ANY
,
5946 .help
= "Load image into server memory for later use by "
5947 "fast_load; primarily for profiling",
5948 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5949 "[min_address [max_length]]",
5952 .name
= "fast_load",
5953 .handler
= handle_fast_load_command
,
5954 .mode
= COMMAND_EXEC
,
5955 .help
= "loads active fast load image to current target "
5956 "- mainly for profiling purposes",
5961 .handler
= handle_profile_command
,
5962 .mode
= COMMAND_EXEC
,
5963 .usage
= "seconds filename [start end]",
5964 .help
= "profiling samples the CPU PC",
5966 /** @todo don't register virt2phys() unless target supports it */
5968 .name
= "virt2phys",
5969 .handler
= handle_virt2phys_command
,
5970 .mode
= COMMAND_ANY
,
5971 .help
= "translate a virtual address into a physical address",
5972 .usage
= "virtual_address",
5976 .handler
= handle_reg_command
,
5977 .mode
= COMMAND_EXEC
,
5978 .help
= "display (reread from target with \"force\") or set a register; "
5979 "with no arguments, displays all registers and their values",
5980 .usage
= "[(register_number|register_name) [(value|'force')]]",
5984 .handler
= handle_poll_command
,
5985 .mode
= COMMAND_EXEC
,
5986 .help
= "poll target state; or reconfigure background polling",
5987 .usage
= "['on'|'off']",
5990 .name
= "wait_halt",
5991 .handler
= handle_wait_halt_command
,
5992 .mode
= COMMAND_EXEC
,
5993 .help
= "wait up to the specified number of milliseconds "
5994 "(default 5000) for a previously requested halt",
5995 .usage
= "[milliseconds]",
5999 .handler
= handle_halt_command
,
6000 .mode
= COMMAND_EXEC
,
6001 .help
= "request target to halt, then wait up to the specified"
6002 "number of milliseconds (default 5000) for it to complete",
6003 .usage
= "[milliseconds]",
6007 .handler
= handle_resume_command
,
6008 .mode
= COMMAND_EXEC
,
6009 .help
= "resume target execution from current PC or address",
6010 .usage
= "[address]",
6014 .handler
= handle_reset_command
,
6015 .mode
= COMMAND_EXEC
,
6016 .usage
= "[run|halt|init]",
6017 .help
= "Reset all targets into the specified mode."
6018 "Default reset mode is run, if not given.",
6021 .name
= "soft_reset_halt",
6022 .handler
= handle_soft_reset_halt_command
,
6023 .mode
= COMMAND_EXEC
,
6025 .help
= "halt the target and do a soft reset",
6029 .handler
= handle_step_command
,
6030 .mode
= COMMAND_EXEC
,
6031 .help
= "step one instruction from current PC or address",
6032 .usage
= "[address]",
6036 .handler
= handle_md_command
,
6037 .mode
= COMMAND_EXEC
,
6038 .help
= "display memory words",
6039 .usage
= "['phys'] address [count]",
6043 .handler
= handle_md_command
,
6044 .mode
= COMMAND_EXEC
,
6045 .help
= "display memory half-words",
6046 .usage
= "['phys'] address [count]",
6050 .handler
= handle_md_command
,
6051 .mode
= COMMAND_EXEC
,
6052 .help
= "display memory bytes",
6053 .usage
= "['phys'] address [count]",
6057 .handler
= handle_mw_command
,
6058 .mode
= COMMAND_EXEC
,
6059 .help
= "write memory word",
6060 .usage
= "['phys'] address value [count]",
6064 .handler
= handle_mw_command
,
6065 .mode
= COMMAND_EXEC
,
6066 .help
= "write memory half-word",
6067 .usage
= "['phys'] address value [count]",
6071 .handler
= handle_mw_command
,
6072 .mode
= COMMAND_EXEC
,
6073 .help
= "write memory byte",
6074 .usage
= "['phys'] address value [count]",
6078 .handler
= handle_bp_command
,
6079 .mode
= COMMAND_EXEC
,
6080 .help
= "list or set hardware or software breakpoint",
6081 .usage
= "<address> [<asid>]<length> ['hw'|'hw_ctx']",
6085 .handler
= handle_rbp_command
,
6086 .mode
= COMMAND_EXEC
,
6087 .help
= "remove breakpoint",
6092 .handler
= handle_wp_command
,
6093 .mode
= COMMAND_EXEC
,
6094 .help
= "list (no params) or create watchpoints",
6095 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
6099 .handler
= handle_rwp_command
,
6100 .mode
= COMMAND_EXEC
,
6101 .help
= "remove watchpoint",
6105 .name
= "load_image",
6106 .handler
= handle_load_image_command
,
6107 .mode
= COMMAND_EXEC
,
6108 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6109 "[min_address] [max_length]",
6112 .name
= "dump_image",
6113 .handler
= handle_dump_image_command
,
6114 .mode
= COMMAND_EXEC
,
6115 .usage
= "filename address size",
6118 .name
= "verify_image",
6119 .handler
= handle_verify_image_command
,
6120 .mode
= COMMAND_EXEC
,
6121 .usage
= "filename [offset [type]]",
6124 .name
= "test_image",
6125 .handler
= handle_test_image_command
,
6126 .mode
= COMMAND_EXEC
,
6127 .usage
= "filename [offset [type]]",
6130 .name
= "mem2array",
6131 .mode
= COMMAND_EXEC
,
6132 .jim_handler
= jim_mem2array
,
6133 .help
= "read 8/16/32 bit memory and return as a TCL array "
6134 "for script processing",
6135 .usage
= "arrayname bitwidth address count",
6138 .name
= "array2mem",
6139 .mode
= COMMAND_EXEC
,
6140 .jim_handler
= jim_array2mem
,
6141 .help
= "convert a TCL array to memory locations "
6142 "and write the 8/16/32 bit values",
6143 .usage
= "arrayname bitwidth address count",
6146 .name
= "reset_nag",
6147 .handler
= handle_target_reset_nag
,
6148 .mode
= COMMAND_ANY
,
6149 .help
= "Nag after each reset about options that could have been "
6150 "enabled to improve performance. ",
6151 .usage
= "['enable'|'disable']",
6155 .handler
= handle_ps_command
,
6156 .mode
= COMMAND_EXEC
,
6157 .help
= "list all tasks ",
6161 .name
= "test_mem_access",
6162 .handler
= handle_test_mem_access_command
,
6163 .mode
= COMMAND_EXEC
,
6164 .help
= "Test the target's memory access functions",
6168 COMMAND_REGISTRATION_DONE
6170 static int target_register_user_commands(struct command_context
*cmd_ctx
)
6172 int retval
= ERROR_OK
;
6173 retval
= target_request_register_commands(cmd_ctx
);
6174 if (retval
!= ERROR_OK
)
6177 retval
= trace_register_commands(cmd_ctx
);
6178 if (retval
!= ERROR_OK
)
6182 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);