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 static const int polling_interval
= 100;
144 static const Jim_Nvp nvp_assert
[] = {
145 { .name
= "assert", NVP_ASSERT
},
146 { .name
= "deassert", NVP_DEASSERT
},
147 { .name
= "T", NVP_ASSERT
},
148 { .name
= "F", NVP_DEASSERT
},
149 { .name
= "t", NVP_ASSERT
},
150 { .name
= "f", NVP_DEASSERT
},
151 { .name
= NULL
, .value
= -1 }
154 static const Jim_Nvp nvp_error_target
[] = {
155 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
156 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
157 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
158 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
159 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
160 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
161 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
162 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
163 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
164 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
165 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
166 { .value
= -1, .name
= NULL
}
169 static const char *target_strerror_safe(int err
)
173 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
180 static const Jim_Nvp nvp_target_event
[] = {
182 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
183 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
184 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
185 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
186 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
188 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
189 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
191 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
192 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
193 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
194 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
195 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
196 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
197 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
198 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
199 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
200 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
201 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
202 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
204 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
205 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
207 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
208 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
210 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
211 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
213 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
214 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
216 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
217 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
219 { .name
= NULL
, .value
= -1 }
222 static const Jim_Nvp nvp_target_state
[] = {
223 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
224 { .name
= "running", .value
= TARGET_RUNNING
},
225 { .name
= "halted", .value
= TARGET_HALTED
},
226 { .name
= "reset", .value
= TARGET_RESET
},
227 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
228 { .name
= NULL
, .value
= -1 },
231 static const Jim_Nvp nvp_target_debug_reason
[] = {
232 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
233 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
234 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
235 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
236 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
237 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
238 { .name
= "program-exit" , .value
= DBG_REASON_EXIT
},
239 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
240 { .name
= NULL
, .value
= -1 },
243 static const Jim_Nvp nvp_target_endian
[] = {
244 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
245 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
246 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
247 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
248 { .name
= NULL
, .value
= -1 },
251 static const Jim_Nvp nvp_reset_modes
[] = {
252 { .name
= "unknown", .value
= RESET_UNKNOWN
},
253 { .name
= "run" , .value
= RESET_RUN
},
254 { .name
= "halt" , .value
= RESET_HALT
},
255 { .name
= "init" , .value
= RESET_INIT
},
256 { .name
= NULL
, .value
= -1 },
259 const char *debug_reason_name(struct target
*t
)
263 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
264 t
->debug_reason
)->name
;
266 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
267 cp
= "(*BUG*unknown*BUG*)";
272 const char *target_state_name(struct target
*t
)
275 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
277 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
278 cp
= "(*BUG*unknown*BUG*)";
283 /* determine the number of the new target */
284 static int new_target_number(void)
289 /* number is 0 based */
293 if (x
< t
->target_number
)
294 x
= t
->target_number
;
300 /* read a uint64_t from a buffer in target memory endianness */
301 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
303 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
304 return le_to_h_u64(buffer
);
306 return be_to_h_u64(buffer
);
309 /* read a uint32_t from a buffer in target memory endianness */
310 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
312 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
313 return le_to_h_u32(buffer
);
315 return be_to_h_u32(buffer
);
318 /* read a uint24_t from a buffer in target memory endianness */
319 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
321 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
322 return le_to_h_u24(buffer
);
324 return be_to_h_u24(buffer
);
327 /* read a uint16_t from a buffer in target memory endianness */
328 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
330 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
331 return le_to_h_u16(buffer
);
333 return be_to_h_u16(buffer
);
336 /* read a uint8_t from a buffer in target memory endianness */
337 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
339 return *buffer
& 0x0ff;
342 /* write a uint64_t to a buffer in target memory endianness */
343 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
345 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
346 h_u64_to_le(buffer
, value
);
348 h_u64_to_be(buffer
, value
);
351 /* write a uint32_t to a buffer in target memory endianness */
352 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
354 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
355 h_u32_to_le(buffer
, value
);
357 h_u32_to_be(buffer
, value
);
360 /* write a uint24_t to a buffer in target memory endianness */
361 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
363 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
364 h_u24_to_le(buffer
, value
);
366 h_u24_to_be(buffer
, value
);
369 /* write a uint16_t to a buffer in target memory endianness */
370 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
372 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
373 h_u16_to_le(buffer
, value
);
375 h_u16_to_be(buffer
, value
);
378 /* write a uint8_t to a buffer in target memory endianness */
379 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
384 /* write a uint64_t array to a buffer in target memory endianness */
385 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
388 for (i
= 0; i
< count
; i
++)
389 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
392 /* write a uint32_t array to a buffer in target memory endianness */
393 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
396 for (i
= 0; i
< count
; i
++)
397 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
400 /* write a uint16_t array to a buffer in target memory endianness */
401 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
404 for (i
= 0; i
< count
; i
++)
405 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
408 /* write a uint64_t array to a buffer in target memory endianness */
409 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
412 for (i
= 0; i
< count
; i
++)
413 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
416 /* write a uint32_t array to a buffer in target memory endianness */
417 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
420 for (i
= 0; i
< count
; i
++)
421 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
424 /* write a uint16_t array to a buffer in target memory endianness */
425 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
428 for (i
= 0; i
< count
; i
++)
429 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
432 /* return a pointer to a configured target; id is name or number */
433 struct target
*get_target(const char *id
)
435 struct target
*target
;
437 /* try as tcltarget name */
438 for (target
= all_targets
; target
; target
= target
->next
) {
439 if (target_name(target
) == NULL
)
441 if (strcmp(id
, target_name(target
)) == 0)
445 /* It's OK to remove this fallback sometime after August 2010 or so */
447 /* no match, try as number */
449 if (parse_uint(id
, &num
) != ERROR_OK
)
452 for (target
= all_targets
; target
; target
= target
->next
) {
453 if (target
->target_number
== (int)num
) {
454 LOG_WARNING("use '%s' as target identifier, not '%u'",
455 target_name(target
), num
);
463 /* returns a pointer to the n-th configured target */
464 static struct target
*get_target_by_num(int num
)
466 struct target
*target
= all_targets
;
469 if (target
->target_number
== num
)
471 target
= target
->next
;
477 struct target
*get_current_target(struct command_context
*cmd_ctx
)
479 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
481 if (target
== NULL
) {
482 LOG_ERROR("BUG: current_target out of bounds");
489 int target_poll(struct target
*target
)
493 /* We can't poll until after examine */
494 if (!target_was_examined(target
)) {
495 /* Fail silently lest we pollute the log */
499 retval
= target
->type
->poll(target
);
500 if (retval
!= ERROR_OK
)
503 if (target
->halt_issued
) {
504 if (target
->state
== TARGET_HALTED
)
505 target
->halt_issued
= false;
507 long long t
= timeval_ms() - target
->halt_issued_time
;
508 if (t
> DEFAULT_HALT_TIMEOUT
) {
509 target
->halt_issued
= false;
510 LOG_INFO("Halt timed out, wake up GDB.");
511 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
519 int target_halt(struct target
*target
)
522 /* We can't poll until after examine */
523 if (!target_was_examined(target
)) {
524 LOG_ERROR("Target not examined yet");
528 retval
= target
->type
->halt(target
);
529 if (retval
!= ERROR_OK
)
532 target
->halt_issued
= true;
533 target
->halt_issued_time
= timeval_ms();
539 * Make the target (re)start executing using its saved execution
540 * context (possibly with some modifications).
542 * @param target Which target should start executing.
543 * @param current True to use the target's saved program counter instead
544 * of the address parameter
545 * @param address Optionally used as the program counter.
546 * @param handle_breakpoints True iff breakpoints at the resumption PC
547 * should be skipped. (For example, maybe execution was stopped by
548 * such a breakpoint, in which case it would be counterprodutive to
550 * @param debug_execution False if all working areas allocated by OpenOCD
551 * should be released and/or restored to their original contents.
552 * (This would for example be true to run some downloaded "helper"
553 * algorithm code, which resides in one such working buffer and uses
554 * another for data storage.)
556 * @todo Resolve the ambiguity about what the "debug_execution" flag
557 * signifies. For example, Target implementations don't agree on how
558 * it relates to invalidation of the register cache, or to whether
559 * breakpoints and watchpoints should be enabled. (It would seem wrong
560 * to enable breakpoints when running downloaded "helper" algorithms
561 * (debug_execution true), since the breakpoints would be set to match
562 * target firmware being debugged, not the helper algorithm.... and
563 * enabling them could cause such helpers to malfunction (for example,
564 * by overwriting data with a breakpoint instruction. On the other
565 * hand the infrastructure for running such helpers might use this
566 * procedure but rely on hardware breakpoint to detect termination.)
568 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
572 /* We can't poll until after examine */
573 if (!target_was_examined(target
)) {
574 LOG_ERROR("Target not examined yet");
578 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
580 /* note that resume *must* be asynchronous. The CPU can halt before
581 * we poll. The CPU can even halt at the current PC as a result of
582 * a software breakpoint being inserted by (a bug?) the application.
584 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
585 if (retval
!= ERROR_OK
)
588 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
593 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
598 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
599 if (n
->name
== NULL
) {
600 LOG_ERROR("invalid reset mode");
604 /* disable polling during reset to make reset event scripts
605 * more predictable, i.e. dr/irscan & pathmove in events will
606 * not have JTAG operations injected into the middle of a sequence.
608 bool save_poll
= jtag_poll_get_enabled();
610 jtag_poll_set_enabled(false);
612 sprintf(buf
, "ocd_process_reset %s", n
->name
);
613 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
615 jtag_poll_set_enabled(save_poll
);
617 if (retval
!= JIM_OK
) {
618 Jim_MakeErrorMessage(cmd_ctx
->interp
);
619 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
623 /* We want any events to be processed before the prompt */
624 retval
= target_call_timer_callbacks_now();
626 struct target
*target
;
627 for (target
= all_targets
; target
; target
= target
->next
) {
628 target
->type
->check_reset(target
);
629 target
->running_alg
= false;
635 static int identity_virt2phys(struct target
*target
,
636 uint32_t virtual, uint32_t *physical
)
642 static int no_mmu(struct target
*target
, int *enabled
)
648 static int default_examine(struct target
*target
)
650 target_set_examined(target
);
654 /* no check by default */
655 static int default_check_reset(struct target
*target
)
660 int target_examine_one(struct target
*target
)
662 return target
->type
->examine(target
);
665 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
667 struct target
*target
= priv
;
669 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
672 jtag_unregister_event_callback(jtag_enable_callback
, target
);
674 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
676 int retval
= target_examine_one(target
);
677 if (retval
!= ERROR_OK
)
680 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
685 /* Targets that correctly implement init + examine, i.e.
686 * no communication with target during init:
690 int target_examine(void)
692 int retval
= ERROR_OK
;
693 struct target
*target
;
695 for (target
= all_targets
; target
; target
= target
->next
) {
696 /* defer examination, but don't skip it */
697 if (!target
->tap
->enabled
) {
698 jtag_register_event_callback(jtag_enable_callback
,
703 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
705 retval
= target_examine_one(target
);
706 if (retval
!= ERROR_OK
)
709 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
714 const char *target_type_name(struct target
*target
)
716 return target
->type
->name
;
719 static int target_soft_reset_halt(struct target
*target
)
721 if (!target_was_examined(target
)) {
722 LOG_ERROR("Target not examined yet");
725 if (!target
->type
->soft_reset_halt
) {
726 LOG_ERROR("Target %s does not support soft_reset_halt",
727 target_name(target
));
730 return target
->type
->soft_reset_halt(target
);
734 * Downloads a target-specific native code algorithm to the target,
735 * and executes it. * Note that some targets may need to set up, enable,
736 * and tear down a breakpoint (hard or * soft) to detect algorithm
737 * termination, while others may support lower overhead schemes where
738 * soft breakpoints embedded in the algorithm automatically terminate the
741 * @param target used to run the algorithm
742 * @param arch_info target-specific description of the algorithm.
744 int target_run_algorithm(struct target
*target
,
745 int num_mem_params
, struct mem_param
*mem_params
,
746 int num_reg_params
, struct reg_param
*reg_param
,
747 uint32_t entry_point
, uint32_t exit_point
,
748 int timeout_ms
, void *arch_info
)
750 int retval
= ERROR_FAIL
;
752 if (!target_was_examined(target
)) {
753 LOG_ERROR("Target not examined yet");
756 if (!target
->type
->run_algorithm
) {
757 LOG_ERROR("Target type '%s' does not support %s",
758 target_type_name(target
), __func__
);
762 target
->running_alg
= true;
763 retval
= target
->type
->run_algorithm(target
,
764 num_mem_params
, mem_params
,
765 num_reg_params
, reg_param
,
766 entry_point
, exit_point
, timeout_ms
, arch_info
);
767 target
->running_alg
= false;
774 * Downloads a target-specific native code algorithm to the target,
775 * executes and leaves it running.
777 * @param target used to run the algorithm
778 * @param arch_info target-specific description of the algorithm.
780 int target_start_algorithm(struct target
*target
,
781 int num_mem_params
, struct mem_param
*mem_params
,
782 int num_reg_params
, struct reg_param
*reg_params
,
783 uint32_t entry_point
, uint32_t exit_point
,
786 int retval
= ERROR_FAIL
;
788 if (!target_was_examined(target
)) {
789 LOG_ERROR("Target not examined yet");
792 if (!target
->type
->start_algorithm
) {
793 LOG_ERROR("Target type '%s' does not support %s",
794 target_type_name(target
), __func__
);
797 if (target
->running_alg
) {
798 LOG_ERROR("Target is already running an algorithm");
802 target
->running_alg
= true;
803 retval
= target
->type
->start_algorithm(target
,
804 num_mem_params
, mem_params
,
805 num_reg_params
, reg_params
,
806 entry_point
, exit_point
, arch_info
);
813 * Waits for an algorithm started with target_start_algorithm() to complete.
815 * @param target used to run the algorithm
816 * @param arch_info target-specific description of the algorithm.
818 int target_wait_algorithm(struct target
*target
,
819 int num_mem_params
, struct mem_param
*mem_params
,
820 int num_reg_params
, struct reg_param
*reg_params
,
821 uint32_t exit_point
, int timeout_ms
,
824 int retval
= ERROR_FAIL
;
826 if (!target
->type
->wait_algorithm
) {
827 LOG_ERROR("Target type '%s' does not support %s",
828 target_type_name(target
), __func__
);
831 if (!target
->running_alg
) {
832 LOG_ERROR("Target is not running an algorithm");
836 retval
= target
->type
->wait_algorithm(target
,
837 num_mem_params
, mem_params
,
838 num_reg_params
, reg_params
,
839 exit_point
, timeout_ms
, arch_info
);
840 if (retval
!= ERROR_TARGET_TIMEOUT
)
841 target
->running_alg
= false;
848 * Executes a target-specific native code algorithm in the target.
849 * It differs from target_run_algorithm in that the algorithm is asynchronous.
850 * Because of this it requires an compliant algorithm:
851 * see contrib/loaders/flash/stm32f1x.S for example.
853 * @param target used to run the algorithm
856 int target_run_flash_async_algorithm(struct target
*target
,
857 const uint8_t *buffer
, uint32_t count
, int block_size
,
858 int num_mem_params
, struct mem_param
*mem_params
,
859 int num_reg_params
, struct reg_param
*reg_params
,
860 uint32_t buffer_start
, uint32_t buffer_size
,
861 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
866 /* Set up working area. First word is write pointer, second word is read pointer,
867 * rest is fifo data area. */
868 uint32_t wp_addr
= buffer_start
;
869 uint32_t rp_addr
= buffer_start
+ 4;
870 uint32_t fifo_start_addr
= buffer_start
+ 8;
871 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
873 uint32_t wp
= fifo_start_addr
;
874 uint32_t rp
= fifo_start_addr
;
876 /* validate block_size is 2^n */
877 assert(!block_size
|| !(block_size
& (block_size
- 1)));
879 retval
= target_write_u32(target
, wp_addr
, wp
);
880 if (retval
!= ERROR_OK
)
882 retval
= target_write_u32(target
, rp_addr
, rp
);
883 if (retval
!= ERROR_OK
)
886 /* Start up algorithm on target and let it idle while writing the first chunk */
887 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
888 num_reg_params
, reg_params
,
893 if (retval
!= ERROR_OK
) {
894 LOG_ERROR("error starting target flash write algorithm");
900 retval
= target_read_u32(target
, rp_addr
, &rp
);
901 if (retval
!= ERROR_OK
) {
902 LOG_ERROR("failed to get read pointer");
906 LOG_DEBUG("count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
, count
, wp
, rp
);
909 LOG_ERROR("flash write algorithm aborted by target");
910 retval
= ERROR_FLASH_OPERATION_FAILED
;
914 if ((rp
& (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
915 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
919 /* Count the number of bytes available in the fifo without
920 * crossing the wrap around. Make sure to not fill it completely,
921 * because that would make wp == rp and that's the empty condition. */
922 uint32_t thisrun_bytes
;
924 thisrun_bytes
= rp
- wp
- block_size
;
925 else if (rp
> fifo_start_addr
)
926 thisrun_bytes
= fifo_end_addr
- wp
;
928 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
930 if (thisrun_bytes
== 0) {
931 /* Throttle polling a bit if transfer is (much) faster than flash
932 * programming. The exact delay shouldn't matter as long as it's
933 * less than buffer size / flash speed. This is very unlikely to
934 * run when using high latency connections such as USB. */
937 /* to stop an infinite loop on some targets check and increment a timeout
938 * this issue was observed on a stellaris using the new ICDI interface */
939 if (timeout
++ >= 500) {
940 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
941 return ERROR_FLASH_OPERATION_FAILED
;
946 /* reset our timeout */
949 /* Limit to the amount of data we actually want to write */
950 if (thisrun_bytes
> count
* block_size
)
951 thisrun_bytes
= count
* block_size
;
953 /* Write data to fifo */
954 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
955 if (retval
!= ERROR_OK
)
958 /* Update counters and wrap write pointer */
959 buffer
+= thisrun_bytes
;
960 count
-= thisrun_bytes
/ block_size
;
962 if (wp
>= fifo_end_addr
)
963 wp
= fifo_start_addr
;
965 /* Store updated write pointer to target */
966 retval
= target_write_u32(target
, wp_addr
, wp
);
967 if (retval
!= ERROR_OK
)
971 if (retval
!= ERROR_OK
) {
972 /* abort flash write algorithm on target */
973 target_write_u32(target
, wp_addr
, 0);
976 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
977 num_reg_params
, reg_params
,
982 if (retval2
!= ERROR_OK
) {
983 LOG_ERROR("error waiting for target flash write algorithm");
990 int target_read_memory(struct target
*target
,
991 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
993 if (!target_was_examined(target
)) {
994 LOG_ERROR("Target not examined yet");
997 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1000 int target_read_phys_memory(struct target
*target
,
1001 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1003 if (!target_was_examined(target
)) {
1004 LOG_ERROR("Target not examined yet");
1007 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1010 int target_write_memory(struct target
*target
,
1011 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1013 if (!target_was_examined(target
)) {
1014 LOG_ERROR("Target not examined yet");
1017 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1020 int target_write_phys_memory(struct target
*target
,
1021 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1023 if (!target_was_examined(target
)) {
1024 LOG_ERROR("Target not examined yet");
1027 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1030 int target_add_breakpoint(struct target
*target
,
1031 struct breakpoint
*breakpoint
)
1033 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1034 LOG_WARNING("target %s is not halted", target_name(target
));
1035 return ERROR_TARGET_NOT_HALTED
;
1037 return target
->type
->add_breakpoint(target
, breakpoint
);
1040 int target_add_context_breakpoint(struct target
*target
,
1041 struct breakpoint
*breakpoint
)
1043 if (target
->state
!= TARGET_HALTED
) {
1044 LOG_WARNING("target %s is not halted", target_name(target
));
1045 return ERROR_TARGET_NOT_HALTED
;
1047 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1050 int target_add_hybrid_breakpoint(struct target
*target
,
1051 struct breakpoint
*breakpoint
)
1053 if (target
->state
!= TARGET_HALTED
) {
1054 LOG_WARNING("target %s is not halted", target_name(target
));
1055 return ERROR_TARGET_NOT_HALTED
;
1057 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1060 int target_remove_breakpoint(struct target
*target
,
1061 struct breakpoint
*breakpoint
)
1063 return target
->type
->remove_breakpoint(target
, breakpoint
);
1066 int target_add_watchpoint(struct target
*target
,
1067 struct watchpoint
*watchpoint
)
1069 if (target
->state
!= TARGET_HALTED
) {
1070 LOG_WARNING("target %s is not halted", target_name(target
));
1071 return ERROR_TARGET_NOT_HALTED
;
1073 return target
->type
->add_watchpoint(target
, watchpoint
);
1075 int target_remove_watchpoint(struct target
*target
,
1076 struct watchpoint
*watchpoint
)
1078 return target
->type
->remove_watchpoint(target
, watchpoint
);
1080 int target_hit_watchpoint(struct target
*target
,
1081 struct watchpoint
**hit_watchpoint
)
1083 if (target
->state
!= TARGET_HALTED
) {
1084 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1085 return ERROR_TARGET_NOT_HALTED
;
1088 if (target
->type
->hit_watchpoint
== NULL
) {
1089 /* For backward compatible, if hit_watchpoint is not implemented,
1090 * return ERROR_FAIL such that gdb_server will not take the nonsense
1095 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1098 int target_get_gdb_reg_list(struct target
*target
,
1099 struct reg
**reg_list
[], int *reg_list_size
,
1100 enum target_register_class reg_class
)
1102 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1104 int target_step(struct target
*target
,
1105 int current
, uint32_t address
, int handle_breakpoints
)
1107 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1110 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1112 if (target
->state
!= TARGET_HALTED
) {
1113 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1114 return ERROR_TARGET_NOT_HALTED
;
1116 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1119 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1121 if (target
->state
!= TARGET_HALTED
) {
1122 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1123 return ERROR_TARGET_NOT_HALTED
;
1125 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1128 int target_profiling(struct target
*target
, uint32_t *samples
,
1129 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1131 if (target
->state
!= TARGET_HALTED
) {
1132 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1133 return ERROR_TARGET_NOT_HALTED
;
1135 return target
->type
->profiling(target
, samples
, max_num_samples
,
1136 num_samples
, seconds
);
1140 * Reset the @c examined flag for the given target.
1141 * Pure paranoia -- targets are zeroed on allocation.
1143 static void target_reset_examined(struct target
*target
)
1145 target
->examined
= false;
1148 static int err_read_phys_memory(struct target
*target
, uint32_t address
,
1149 uint32_t size
, uint32_t count
, uint8_t *buffer
)
1151 LOG_ERROR("Not implemented: %s", __func__
);
1155 static int err_write_phys_memory(struct target
*target
, uint32_t address
,
1156 uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1158 LOG_ERROR("Not implemented: %s", __func__
);
1162 static int handle_target(void *priv
);
1164 static int target_init_one(struct command_context
*cmd_ctx
,
1165 struct target
*target
)
1167 target_reset_examined(target
);
1169 struct target_type
*type
= target
->type
;
1170 if (type
->examine
== NULL
)
1171 type
->examine
= default_examine
;
1173 if (type
->check_reset
== NULL
)
1174 type
->check_reset
= default_check_reset
;
1176 assert(type
->init_target
!= NULL
);
1178 int retval
= type
->init_target(cmd_ctx
, target
);
1179 if (ERROR_OK
!= retval
) {
1180 LOG_ERROR("target '%s' init failed", target_name(target
));
1184 /* Sanity-check MMU support ... stub in what we must, to help
1185 * implement it in stages, but warn if we need to do so.
1188 if (type
->write_phys_memory
== NULL
) {
1189 LOG_ERROR("type '%s' is missing write_phys_memory",
1191 type
->write_phys_memory
= err_write_phys_memory
;
1193 if (type
->read_phys_memory
== NULL
) {
1194 LOG_ERROR("type '%s' is missing read_phys_memory",
1196 type
->read_phys_memory
= err_read_phys_memory
;
1198 if (type
->virt2phys
== NULL
) {
1199 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1200 type
->virt2phys
= identity_virt2phys
;
1203 /* Make sure no-MMU targets all behave the same: make no
1204 * distinction between physical and virtual addresses, and
1205 * ensure that virt2phys() is always an identity mapping.
1207 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1208 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1211 type
->write_phys_memory
= type
->write_memory
;
1212 type
->read_phys_memory
= type
->read_memory
;
1213 type
->virt2phys
= identity_virt2phys
;
1216 if (target
->type
->read_buffer
== NULL
)
1217 target
->type
->read_buffer
= target_read_buffer_default
;
1219 if (target
->type
->write_buffer
== NULL
)
1220 target
->type
->write_buffer
= target_write_buffer_default
;
1222 if (target
->type
->get_gdb_fileio_info
== NULL
)
1223 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1225 if (target
->type
->gdb_fileio_end
== NULL
)
1226 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1228 if (target
->type
->profiling
== NULL
)
1229 target
->type
->profiling
= target_profiling_default
;
1234 static int target_init(struct command_context
*cmd_ctx
)
1236 struct target
*target
;
1239 for (target
= all_targets
; target
; target
= target
->next
) {
1240 retval
= target_init_one(cmd_ctx
, target
);
1241 if (ERROR_OK
!= retval
)
1248 retval
= target_register_user_commands(cmd_ctx
);
1249 if (ERROR_OK
!= retval
)
1252 retval
= target_register_timer_callback(&handle_target
,
1253 polling_interval
, 1, cmd_ctx
->interp
);
1254 if (ERROR_OK
!= retval
)
1260 COMMAND_HANDLER(handle_target_init_command
)
1265 return ERROR_COMMAND_SYNTAX_ERROR
;
1267 static bool target_initialized
;
1268 if (target_initialized
) {
1269 LOG_INFO("'target init' has already been called");
1272 target_initialized
= true;
1274 retval
= command_run_line(CMD_CTX
, "init_targets");
1275 if (ERROR_OK
!= retval
)
1278 retval
= command_run_line(CMD_CTX
, "init_target_events");
1279 if (ERROR_OK
!= retval
)
1282 retval
= command_run_line(CMD_CTX
, "init_board");
1283 if (ERROR_OK
!= retval
)
1286 LOG_DEBUG("Initializing targets...");
1287 return target_init(CMD_CTX
);
1290 int target_register_event_callback(int (*callback
)(struct target
*target
,
1291 enum target_event event
, void *priv
), void *priv
)
1293 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1295 if (callback
== NULL
)
1296 return ERROR_COMMAND_SYNTAX_ERROR
;
1299 while ((*callbacks_p
)->next
)
1300 callbacks_p
= &((*callbacks_p
)->next
);
1301 callbacks_p
= &((*callbacks_p
)->next
);
1304 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1305 (*callbacks_p
)->callback
= callback
;
1306 (*callbacks_p
)->priv
= priv
;
1307 (*callbacks_p
)->next
= NULL
;
1312 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1314 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1317 if (callback
== NULL
)
1318 return ERROR_COMMAND_SYNTAX_ERROR
;
1321 while ((*callbacks_p
)->next
)
1322 callbacks_p
= &((*callbacks_p
)->next
);
1323 callbacks_p
= &((*callbacks_p
)->next
);
1326 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1327 (*callbacks_p
)->callback
= callback
;
1328 (*callbacks_p
)->periodic
= periodic
;
1329 (*callbacks_p
)->time_ms
= time_ms
;
1331 gettimeofday(&now
, NULL
);
1332 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
1333 time_ms
-= (time_ms
% 1000);
1334 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
1335 if ((*callbacks_p
)->when
.tv_usec
> 1000000) {
1336 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
1337 (*callbacks_p
)->when
.tv_sec
+= 1;
1340 (*callbacks_p
)->priv
= priv
;
1341 (*callbacks_p
)->next
= NULL
;
1346 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1347 enum target_event event
, void *priv
), void *priv
)
1349 struct target_event_callback
**p
= &target_event_callbacks
;
1350 struct target_event_callback
*c
= target_event_callbacks
;
1352 if (callback
== NULL
)
1353 return ERROR_COMMAND_SYNTAX_ERROR
;
1356 struct target_event_callback
*next
= c
->next
;
1357 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1369 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1371 struct target_timer_callback
**p
= &target_timer_callbacks
;
1372 struct target_timer_callback
*c
= target_timer_callbacks
;
1374 if (callback
== NULL
)
1375 return ERROR_COMMAND_SYNTAX_ERROR
;
1378 struct target_timer_callback
*next
= c
->next
;
1379 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1391 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1393 struct target_event_callback
*callback
= target_event_callbacks
;
1394 struct target_event_callback
*next_callback
;
1396 if (event
== TARGET_EVENT_HALTED
) {
1397 /* execute early halted first */
1398 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1401 LOG_DEBUG("target event %i (%s)", event
,
1402 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1404 target_handle_event(target
, event
);
1407 next_callback
= callback
->next
;
1408 callback
->callback(target
, event
, callback
->priv
);
1409 callback
= next_callback
;
1415 static int target_timer_callback_periodic_restart(
1416 struct target_timer_callback
*cb
, struct timeval
*now
)
1418 int time_ms
= cb
->time_ms
;
1419 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1420 time_ms
-= (time_ms
% 1000);
1421 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1422 if (cb
->when
.tv_usec
> 1000000) {
1423 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1424 cb
->when
.tv_sec
+= 1;
1429 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1430 struct timeval
*now
)
1432 cb
->callback(cb
->priv
);
1435 return target_timer_callback_periodic_restart(cb
, now
);
1437 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1440 static int target_call_timer_callbacks_check_time(int checktime
)
1445 gettimeofday(&now
, NULL
);
1447 struct target_timer_callback
*callback
= target_timer_callbacks
;
1449 /* cleaning up may unregister and free this callback */
1450 struct target_timer_callback
*next_callback
= callback
->next
;
1452 bool call_it
= callback
->callback
&&
1453 ((!checktime
&& callback
->periodic
) ||
1454 now
.tv_sec
> callback
->when
.tv_sec
||
1455 (now
.tv_sec
== callback
->when
.tv_sec
&&
1456 now
.tv_usec
>= callback
->when
.tv_usec
));
1459 int retval
= target_call_timer_callback(callback
, &now
);
1460 if (retval
!= ERROR_OK
)
1464 callback
= next_callback
;
1470 int target_call_timer_callbacks(void)
1472 return target_call_timer_callbacks_check_time(1);
1475 /* invoke periodic callbacks immediately */
1476 int target_call_timer_callbacks_now(void)
1478 return target_call_timer_callbacks_check_time(0);
1481 /* Prints the working area layout for debug purposes */
1482 static void print_wa_layout(struct target
*target
)
1484 struct working_area
*c
= target
->working_areas
;
1487 LOG_DEBUG("%c%c 0x%08"PRIx32
"-0x%08"PRIx32
" (%"PRIu32
" bytes)",
1488 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1489 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1494 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1495 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1497 assert(area
->free
); /* Shouldn't split an allocated area */
1498 assert(size
<= area
->size
); /* Caller should guarantee this */
1500 /* Split only if not already the right size */
1501 if (size
< area
->size
) {
1502 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1507 new_wa
->next
= area
->next
;
1508 new_wa
->size
= area
->size
- size
;
1509 new_wa
->address
= area
->address
+ size
;
1510 new_wa
->backup
= NULL
;
1511 new_wa
->user
= NULL
;
1512 new_wa
->free
= true;
1514 area
->next
= new_wa
;
1517 /* If backup memory was allocated to this area, it has the wrong size
1518 * now so free it and it will be reallocated if/when needed */
1521 area
->backup
= NULL
;
1526 /* Merge all adjacent free areas into one */
1527 static void target_merge_working_areas(struct target
*target
)
1529 struct working_area
*c
= target
->working_areas
;
1531 while (c
&& c
->next
) {
1532 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1534 /* Find two adjacent free areas */
1535 if (c
->free
&& c
->next
->free
) {
1536 /* Merge the last into the first */
1537 c
->size
+= c
->next
->size
;
1539 /* Remove the last */
1540 struct working_area
*to_be_freed
= c
->next
;
1541 c
->next
= c
->next
->next
;
1542 if (to_be_freed
->backup
)
1543 free(to_be_freed
->backup
);
1546 /* If backup memory was allocated to the remaining area, it's has
1547 * the wrong size now */
1558 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1560 /* Reevaluate working area address based on MMU state*/
1561 if (target
->working_areas
== NULL
) {
1565 retval
= target
->type
->mmu(target
, &enabled
);
1566 if (retval
!= ERROR_OK
)
1570 if (target
->working_area_phys_spec
) {
1571 LOG_DEBUG("MMU disabled, using physical "
1572 "address for working memory 0x%08"PRIx32
,
1573 target
->working_area_phys
);
1574 target
->working_area
= target
->working_area_phys
;
1576 LOG_ERROR("No working memory available. "
1577 "Specify -work-area-phys to target.");
1578 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1581 if (target
->working_area_virt_spec
) {
1582 LOG_DEBUG("MMU enabled, using virtual "
1583 "address for working memory 0x%08"PRIx32
,
1584 target
->working_area_virt
);
1585 target
->working_area
= target
->working_area_virt
;
1587 LOG_ERROR("No working memory available. "
1588 "Specify -work-area-virt to target.");
1589 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1593 /* Set up initial working area on first call */
1594 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1596 new_wa
->next
= NULL
;
1597 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1598 new_wa
->address
= target
->working_area
;
1599 new_wa
->backup
= NULL
;
1600 new_wa
->user
= NULL
;
1601 new_wa
->free
= true;
1604 target
->working_areas
= new_wa
;
1607 /* only allocate multiples of 4 byte */
1609 size
= (size
+ 3) & (~3UL);
1611 struct working_area
*c
= target
->working_areas
;
1613 /* Find the first large enough working area */
1615 if (c
->free
&& c
->size
>= size
)
1621 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1623 /* Split the working area into the requested size */
1624 target_split_working_area(c
, size
);
1626 LOG_DEBUG("allocated new working area of %"PRIu32
" bytes at address 0x%08"PRIx32
, size
, c
->address
);
1628 if (target
->backup_working_area
) {
1629 if (c
->backup
== NULL
) {
1630 c
->backup
= malloc(c
->size
);
1631 if (c
->backup
== NULL
)
1635 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1636 if (retval
!= ERROR_OK
)
1640 /* mark as used, and return the new (reused) area */
1647 print_wa_layout(target
);
1652 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1656 retval
= target_alloc_working_area_try(target
, size
, area
);
1657 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1658 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1663 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1665 int retval
= ERROR_OK
;
1667 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1668 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1669 if (retval
!= ERROR_OK
)
1670 LOG_ERROR("failed to restore %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1671 area
->size
, area
->address
);
1677 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1678 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1680 int retval
= ERROR_OK
;
1686 retval
= target_restore_working_area(target
, area
);
1687 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1688 if (retval
!= ERROR_OK
)
1694 LOG_DEBUG("freed %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1695 area
->size
, area
->address
);
1697 /* mark user pointer invalid */
1698 /* TODO: Is this really safe? It points to some previous caller's memory.
1699 * How could we know that the area pointer is still in that place and not
1700 * some other vital data? What's the purpose of this, anyway? */
1704 target_merge_working_areas(target
);
1706 print_wa_layout(target
);
1711 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1713 return target_free_working_area_restore(target
, area
, 1);
1716 /* free resources and restore memory, if restoring memory fails,
1717 * free up resources anyway
1719 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1721 struct working_area
*c
= target
->working_areas
;
1723 LOG_DEBUG("freeing all working areas");
1725 /* Loop through all areas, restoring the allocated ones and marking them as free */
1729 target_restore_working_area(target
, c
);
1731 *c
->user
= NULL
; /* Same as above */
1737 /* Run a merge pass to combine all areas into one */
1738 target_merge_working_areas(target
);
1740 print_wa_layout(target
);
1743 void target_free_all_working_areas(struct target
*target
)
1745 target_free_all_working_areas_restore(target
, 1);
1748 /* Find the largest number of bytes that can be allocated */
1749 uint32_t target_get_working_area_avail(struct target
*target
)
1751 struct working_area
*c
= target
->working_areas
;
1752 uint32_t max_size
= 0;
1755 return target
->working_area_size
;
1758 if (c
->free
&& max_size
< c
->size
)
1767 int target_arch_state(struct target
*target
)
1770 if (target
== NULL
) {
1771 LOG_USER("No target has been configured");
1775 LOG_USER("target state: %s", target_state_name(target
));
1777 if (target
->state
!= TARGET_HALTED
)
1780 retval
= target
->type
->arch_state(target
);
1784 static int target_get_gdb_fileio_info_default(struct target
*target
,
1785 struct gdb_fileio_info
*fileio_info
)
1787 /* If target does not support semi-hosting function, target
1788 has no need to provide .get_gdb_fileio_info callback.
1789 It just return ERROR_FAIL and gdb_server will return "Txx"
1790 as target halted every time. */
1794 static int target_gdb_fileio_end_default(struct target
*target
,
1795 int retcode
, int fileio_errno
, bool ctrl_c
)
1800 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
1801 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1803 struct timeval timeout
, now
;
1805 gettimeofday(&timeout
, NULL
);
1806 timeval_add_time(&timeout
, seconds
, 0);
1808 LOG_INFO("Starting profiling. Halting and resuming the"
1809 " target as often as we can...");
1811 uint32_t sample_count
= 0;
1812 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
1813 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
1815 int retval
= ERROR_OK
;
1817 target_poll(target
);
1818 if (target
->state
== TARGET_HALTED
) {
1819 uint32_t t
= *((uint32_t *)reg
->value
);
1820 samples
[sample_count
++] = t
;
1821 /* current pc, addr = 0, do not handle breakpoints, not debugging */
1822 retval
= target_resume(target
, 1, 0, 0, 0);
1823 target_poll(target
);
1824 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
1825 } else if (target
->state
== TARGET_RUNNING
) {
1826 /* We want to quickly sample the PC. */
1827 retval
= target_halt(target
);
1829 LOG_INFO("Target not halted or running");
1834 if (retval
!= ERROR_OK
)
1837 gettimeofday(&now
, NULL
);
1838 if ((sample_count
>= max_num_samples
) ||
1839 ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
))) {
1840 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
1845 *num_samples
= sample_count
;
1849 /* Single aligned words are guaranteed to use 16 or 32 bit access
1850 * mode respectively, otherwise data is handled as quickly as
1853 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
1855 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1856 (int)size
, (unsigned)address
);
1858 if (!target_was_examined(target
)) {
1859 LOG_ERROR("Target not examined yet");
1866 if ((address
+ size
- 1) < address
) {
1867 /* GDB can request this when e.g. PC is 0xfffffffc*/
1868 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1874 return target
->type
->write_buffer(target
, address
, size
, buffer
);
1877 static int target_write_buffer_default(struct target
*target
, uint32_t address
, uint32_t count
, const uint8_t *buffer
)
1881 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
1882 * will have something to do with the size we leave to it. */
1883 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
1884 if (address
& size
) {
1885 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
1886 if (retval
!= ERROR_OK
)
1894 /* Write the data with as large access size as possible. */
1895 for (; size
> 0; size
/= 2) {
1896 uint32_t aligned
= count
- count
% size
;
1898 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
1899 if (retval
!= ERROR_OK
)
1910 /* Single aligned words are guaranteed to use 16 or 32 bit access
1911 * mode respectively, otherwise data is handled as quickly as
1914 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1916 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1917 (int)size
, (unsigned)address
);
1919 if (!target_was_examined(target
)) {
1920 LOG_ERROR("Target not examined yet");
1927 if ((address
+ size
- 1) < address
) {
1928 /* GDB can request this when e.g. PC is 0xfffffffc*/
1929 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1935 return target
->type
->read_buffer(target
, address
, size
, buffer
);
1938 static int target_read_buffer_default(struct target
*target
, uint32_t address
, uint32_t count
, uint8_t *buffer
)
1942 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
1943 * will have something to do with the size we leave to it. */
1944 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
1945 if (address
& size
) {
1946 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
1947 if (retval
!= ERROR_OK
)
1955 /* Read the data with as large access size as possible. */
1956 for (; size
> 0; size
/= 2) {
1957 uint32_t aligned
= count
- count
% size
;
1959 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
1960 if (retval
!= ERROR_OK
)
1971 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1976 uint32_t checksum
= 0;
1977 if (!target_was_examined(target
)) {
1978 LOG_ERROR("Target not examined yet");
1982 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
1983 if (retval
!= ERROR_OK
) {
1984 buffer
= malloc(size
);
1985 if (buffer
== NULL
) {
1986 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1987 return ERROR_COMMAND_SYNTAX_ERROR
;
1989 retval
= target_read_buffer(target
, address
, size
, buffer
);
1990 if (retval
!= ERROR_OK
) {
1995 /* convert to target endianness */
1996 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
1997 uint32_t target_data
;
1998 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1999 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2002 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2011 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
2014 if (!target_was_examined(target
)) {
2015 LOG_ERROR("Target not examined yet");
2019 if (target
->type
->blank_check_memory
== 0)
2020 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2022 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
2027 int target_read_u64(struct target
*target
, uint64_t address
, uint64_t *value
)
2029 uint8_t value_buf
[8];
2030 if (!target_was_examined(target
)) {
2031 LOG_ERROR("Target not examined yet");
2035 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2037 if (retval
== ERROR_OK
) {
2038 *value
= target_buffer_get_u64(target
, value_buf
);
2039 LOG_DEBUG("address: 0x%" PRIx64
", value: 0x%16.16" PRIx64
"",
2044 LOG_DEBUG("address: 0x%" PRIx64
" failed",
2051 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
2053 uint8_t value_buf
[4];
2054 if (!target_was_examined(target
)) {
2055 LOG_ERROR("Target not examined yet");
2059 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2061 if (retval
== ERROR_OK
) {
2062 *value
= target_buffer_get_u32(target
, value_buf
);
2063 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
2068 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2075 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
2077 uint8_t value_buf
[2];
2078 if (!target_was_examined(target
)) {
2079 LOG_ERROR("Target not examined yet");
2083 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2085 if (retval
== ERROR_OK
) {
2086 *value
= target_buffer_get_u16(target
, value_buf
);
2087 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
2092 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2099 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
2101 if (!target_was_examined(target
)) {
2102 LOG_ERROR("Target not examined yet");
2106 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2108 if (retval
== ERROR_OK
) {
2109 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
2114 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2121 int target_write_u64(struct target
*target
, uint64_t address
, uint64_t value
)
2124 uint8_t value_buf
[8];
2125 if (!target_was_examined(target
)) {
2126 LOG_ERROR("Target not examined yet");
2130 LOG_DEBUG("address: 0x%" PRIx64
", value: 0x%16.16" PRIx64
"",
2134 target_buffer_set_u64(target
, value_buf
, value
);
2135 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2136 if (retval
!= ERROR_OK
)
2137 LOG_DEBUG("failed: %i", retval
);
2142 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
2145 uint8_t value_buf
[4];
2146 if (!target_was_examined(target
)) {
2147 LOG_ERROR("Target not examined yet");
2151 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
2155 target_buffer_set_u32(target
, value_buf
, value
);
2156 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2157 if (retval
!= ERROR_OK
)
2158 LOG_DEBUG("failed: %i", retval
);
2163 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
2166 uint8_t value_buf
[2];
2167 if (!target_was_examined(target
)) {
2168 LOG_ERROR("Target not examined yet");
2172 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
2176 target_buffer_set_u16(target
, value_buf
, value
);
2177 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2178 if (retval
!= ERROR_OK
)
2179 LOG_DEBUG("failed: %i", retval
);
2184 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
2187 if (!target_was_examined(target
)) {
2188 LOG_ERROR("Target not examined yet");
2192 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
2195 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2196 if (retval
!= ERROR_OK
)
2197 LOG_DEBUG("failed: %i", retval
);
2202 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2204 struct target
*target
= get_target(name
);
2205 if (target
== NULL
) {
2206 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2209 if (!target
->tap
->enabled
) {
2210 LOG_USER("Target: TAP %s is disabled, "
2211 "can't be the current target\n",
2212 target
->tap
->dotted_name
);
2216 cmd_ctx
->current_target
= target
->target_number
;
2221 COMMAND_HANDLER(handle_targets_command
)
2223 int retval
= ERROR_OK
;
2224 if (CMD_ARGC
== 1) {
2225 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2226 if (retval
== ERROR_OK
) {
2232 struct target
*target
= all_targets
;
2233 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2234 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2239 if (target
->tap
->enabled
)
2240 state
= target_state_name(target
);
2242 state
= "tap-disabled";
2244 if (CMD_CTX
->current_target
== target
->target_number
)
2247 /* keep columns lined up to match the headers above */
2248 command_print(CMD_CTX
,
2249 "%2d%c %-18s %-10s %-6s %-18s %s",
2250 target
->target_number
,
2252 target_name(target
),
2253 target_type_name(target
),
2254 Jim_Nvp_value2name_simple(nvp_target_endian
,
2255 target
->endianness
)->name
,
2256 target
->tap
->dotted_name
,
2258 target
= target
->next
;
2264 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2266 static int powerDropout
;
2267 static int srstAsserted
;
2269 static int runPowerRestore
;
2270 static int runPowerDropout
;
2271 static int runSrstAsserted
;
2272 static int runSrstDeasserted
;
2274 static int sense_handler(void)
2276 static int prevSrstAsserted
;
2277 static int prevPowerdropout
;
2279 int retval
= jtag_power_dropout(&powerDropout
);
2280 if (retval
!= ERROR_OK
)
2284 powerRestored
= prevPowerdropout
&& !powerDropout
;
2286 runPowerRestore
= 1;
2288 long long current
= timeval_ms();
2289 static long long lastPower
;
2290 int waitMore
= lastPower
+ 2000 > current
;
2291 if (powerDropout
&& !waitMore
) {
2292 runPowerDropout
= 1;
2293 lastPower
= current
;
2296 retval
= jtag_srst_asserted(&srstAsserted
);
2297 if (retval
!= ERROR_OK
)
2301 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2303 static long long lastSrst
;
2304 waitMore
= lastSrst
+ 2000 > current
;
2305 if (srstDeasserted
&& !waitMore
) {
2306 runSrstDeasserted
= 1;
2310 if (!prevSrstAsserted
&& srstAsserted
)
2311 runSrstAsserted
= 1;
2313 prevSrstAsserted
= srstAsserted
;
2314 prevPowerdropout
= powerDropout
;
2316 if (srstDeasserted
|| powerRestored
) {
2317 /* Other than logging the event we can't do anything here.
2318 * Issuing a reset is a particularly bad idea as we might
2319 * be inside a reset already.
2326 /* process target state changes */
2327 static int handle_target(void *priv
)
2329 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2330 int retval
= ERROR_OK
;
2332 if (!is_jtag_poll_safe()) {
2333 /* polling is disabled currently */
2337 /* we do not want to recurse here... */
2338 static int recursive
;
2342 /* danger! running these procedures can trigger srst assertions and power dropouts.
2343 * We need to avoid an infinite loop/recursion here and we do that by
2344 * clearing the flags after running these events.
2346 int did_something
= 0;
2347 if (runSrstAsserted
) {
2348 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2349 Jim_Eval(interp
, "srst_asserted");
2352 if (runSrstDeasserted
) {
2353 Jim_Eval(interp
, "srst_deasserted");
2356 if (runPowerDropout
) {
2357 LOG_INFO("Power dropout detected, running power_dropout proc.");
2358 Jim_Eval(interp
, "power_dropout");
2361 if (runPowerRestore
) {
2362 Jim_Eval(interp
, "power_restore");
2366 if (did_something
) {
2367 /* clear detect flags */
2371 /* clear action flags */
2373 runSrstAsserted
= 0;
2374 runSrstDeasserted
= 0;
2375 runPowerRestore
= 0;
2376 runPowerDropout
= 0;
2381 /* Poll targets for state changes unless that's globally disabled.
2382 * Skip targets that are currently disabled.
2384 for (struct target
*target
= all_targets
;
2385 is_jtag_poll_safe() && target
;
2386 target
= target
->next
) {
2388 if (!target_was_examined(target
))
2391 if (!target
->tap
->enabled
)
2394 if (target
->backoff
.times
> target
->backoff
.count
) {
2395 /* do not poll this time as we failed previously */
2396 target
->backoff
.count
++;
2399 target
->backoff
.count
= 0;
2401 /* only poll target if we've got power and srst isn't asserted */
2402 if (!powerDropout
&& !srstAsserted
) {
2403 /* polling may fail silently until the target has been examined */
2404 retval
= target_poll(target
);
2405 if (retval
!= ERROR_OK
) {
2406 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2407 if (target
->backoff
.times
* polling_interval
< 5000) {
2408 target
->backoff
.times
*= 2;
2409 target
->backoff
.times
++;
2411 LOG_USER("Polling target %s failed, GDB will be halted. Polling again in %dms",
2412 target_name(target
),
2413 target
->backoff
.times
* polling_interval
);
2415 /* Tell GDB to halt the debugger. This allows the user to
2416 * run monitor commands to handle the situation.
2418 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2421 /* Since we succeeded, we reset backoff count */
2422 if (target
->backoff
.times
> 0) {
2423 LOG_USER("Polling target %s succeeded again, trying to reexamine", target_name(target
));
2424 target_reset_examined(target
);
2425 target_examine_one(target
);
2428 target
->backoff
.times
= 0;
2435 COMMAND_HANDLER(handle_reg_command
)
2437 struct target
*target
;
2438 struct reg
*reg
= NULL
;
2444 target
= get_current_target(CMD_CTX
);
2446 /* list all available registers for the current target */
2447 if (CMD_ARGC
== 0) {
2448 struct reg_cache
*cache
= target
->reg_cache
;
2454 command_print(CMD_CTX
, "===== %s", cache
->name
);
2456 for (i
= 0, reg
= cache
->reg_list
;
2457 i
< cache
->num_regs
;
2458 i
++, reg
++, count
++) {
2459 /* only print cached values if they are valid */
2461 value
= buf_to_str(reg
->value
,
2463 command_print(CMD_CTX
,
2464 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2472 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2477 cache
= cache
->next
;
2483 /* access a single register by its ordinal number */
2484 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2486 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2488 struct reg_cache
*cache
= target
->reg_cache
;
2492 for (i
= 0; i
< cache
->num_regs
; i
++) {
2493 if (count
++ == num
) {
2494 reg
= &cache
->reg_list
[i
];
2500 cache
= cache
->next
;
2504 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2505 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2509 /* access a single register by its name */
2510 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2513 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2518 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2520 /* display a register */
2521 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2522 && (CMD_ARGV
[1][0] <= '9')))) {
2523 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2526 if (reg
->valid
== 0)
2527 reg
->type
->get(reg
);
2528 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2529 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2534 /* set register value */
2535 if (CMD_ARGC
== 2) {
2536 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2539 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2541 reg
->type
->set(reg
, buf
);
2543 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2544 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2552 return ERROR_COMMAND_SYNTAX_ERROR
;
2555 COMMAND_HANDLER(handle_poll_command
)
2557 int retval
= ERROR_OK
;
2558 struct target
*target
= get_current_target(CMD_CTX
);
2560 if (CMD_ARGC
== 0) {
2561 command_print(CMD_CTX
, "background polling: %s",
2562 jtag_poll_get_enabled() ? "on" : "off");
2563 command_print(CMD_CTX
, "TAP: %s (%s)",
2564 target
->tap
->dotted_name
,
2565 target
->tap
->enabled
? "enabled" : "disabled");
2566 if (!target
->tap
->enabled
)
2568 retval
= target_poll(target
);
2569 if (retval
!= ERROR_OK
)
2571 retval
= target_arch_state(target
);
2572 if (retval
!= ERROR_OK
)
2574 } else if (CMD_ARGC
== 1) {
2576 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2577 jtag_poll_set_enabled(enable
);
2579 return ERROR_COMMAND_SYNTAX_ERROR
;
2584 COMMAND_HANDLER(handle_wait_halt_command
)
2587 return ERROR_COMMAND_SYNTAX_ERROR
;
2589 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2590 if (1 == CMD_ARGC
) {
2591 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2592 if (ERROR_OK
!= retval
)
2593 return ERROR_COMMAND_SYNTAX_ERROR
;
2596 struct target
*target
= get_current_target(CMD_CTX
);
2597 return target_wait_state(target
, TARGET_HALTED
, ms
);
2600 /* wait for target state to change. The trick here is to have a low
2601 * latency for short waits and not to suck up all the CPU time
2604 * After 500ms, keep_alive() is invoked
2606 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2609 long long then
= 0, cur
;
2613 retval
= target_poll(target
);
2614 if (retval
!= ERROR_OK
)
2616 if (target
->state
== state
)
2621 then
= timeval_ms();
2622 LOG_DEBUG("waiting for target %s...",
2623 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2629 if ((cur
-then
) > ms
) {
2630 LOG_ERROR("timed out while waiting for target %s",
2631 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2639 COMMAND_HANDLER(handle_halt_command
)
2643 struct target
*target
= get_current_target(CMD_CTX
);
2644 int retval
= target_halt(target
);
2645 if (ERROR_OK
!= retval
)
2648 if (CMD_ARGC
== 1) {
2649 unsigned wait_local
;
2650 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2651 if (ERROR_OK
!= retval
)
2652 return ERROR_COMMAND_SYNTAX_ERROR
;
2657 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2660 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2662 struct target
*target
= get_current_target(CMD_CTX
);
2664 LOG_USER("requesting target halt and executing a soft reset");
2666 target_soft_reset_halt(target
);
2671 COMMAND_HANDLER(handle_reset_command
)
2674 return ERROR_COMMAND_SYNTAX_ERROR
;
2676 enum target_reset_mode reset_mode
= RESET_RUN
;
2677 if (CMD_ARGC
== 1) {
2679 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2680 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
2681 return ERROR_COMMAND_SYNTAX_ERROR
;
2682 reset_mode
= n
->value
;
2685 /* reset *all* targets */
2686 return target_process_reset(CMD_CTX
, reset_mode
);
2690 COMMAND_HANDLER(handle_resume_command
)
2694 return ERROR_COMMAND_SYNTAX_ERROR
;
2696 struct target
*target
= get_current_target(CMD_CTX
);
2698 /* with no CMD_ARGV, resume from current pc, addr = 0,
2699 * with one arguments, addr = CMD_ARGV[0],
2700 * handle breakpoints, not debugging */
2702 if (CMD_ARGC
== 1) {
2703 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2707 return target_resume(target
, current
, addr
, 1, 0);
2710 COMMAND_HANDLER(handle_step_command
)
2713 return ERROR_COMMAND_SYNTAX_ERROR
;
2717 /* with no CMD_ARGV, step from current pc, addr = 0,
2718 * with one argument addr = CMD_ARGV[0],
2719 * handle breakpoints, debugging */
2722 if (CMD_ARGC
== 1) {
2723 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2727 struct target
*target
= get_current_target(CMD_CTX
);
2729 return target
->type
->step(target
, current_pc
, addr
, 1);
2732 static void handle_md_output(struct command_context
*cmd_ctx
,
2733 struct target
*target
, uint32_t address
, unsigned size
,
2734 unsigned count
, const uint8_t *buffer
)
2736 const unsigned line_bytecnt
= 32;
2737 unsigned line_modulo
= line_bytecnt
/ size
;
2739 char output
[line_bytecnt
* 4 + 1];
2740 unsigned output_len
= 0;
2742 const char *value_fmt
;
2745 value_fmt
= "%8.8x ";
2748 value_fmt
= "%4.4x ";
2751 value_fmt
= "%2.2x ";
2754 /* "can't happen", caller checked */
2755 LOG_ERROR("invalid memory read size: %u", size
);
2759 for (unsigned i
= 0; i
< count
; i
++) {
2760 if (i
% line_modulo
== 0) {
2761 output_len
+= snprintf(output
+ output_len
,
2762 sizeof(output
) - output_len
,
2764 (unsigned)(address
+ (i
*size
)));
2768 const uint8_t *value_ptr
= buffer
+ i
* size
;
2771 value
= target_buffer_get_u32(target
, value_ptr
);
2774 value
= target_buffer_get_u16(target
, value_ptr
);
2779 output_len
+= snprintf(output
+ output_len
,
2780 sizeof(output
) - output_len
,
2783 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
2784 command_print(cmd_ctx
, "%s", output
);
2790 COMMAND_HANDLER(handle_md_command
)
2793 return ERROR_COMMAND_SYNTAX_ERROR
;
2796 switch (CMD_NAME
[2]) {
2807 return ERROR_COMMAND_SYNTAX_ERROR
;
2810 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2811 int (*fn
)(struct target
*target
,
2812 uint32_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
2816 fn
= target_read_phys_memory
;
2818 fn
= target_read_memory
;
2819 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2820 return ERROR_COMMAND_SYNTAX_ERROR
;
2823 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2827 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2829 uint8_t *buffer
= calloc(count
, size
);
2831 struct target
*target
= get_current_target(CMD_CTX
);
2832 int retval
= fn(target
, address
, size
, count
, buffer
);
2833 if (ERROR_OK
== retval
)
2834 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2841 typedef int (*target_write_fn
)(struct target
*target
,
2842 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
2844 static int target_fill_mem(struct target
*target
,
2853 /* We have to write in reasonably large chunks to be able
2854 * to fill large memory areas with any sane speed */
2855 const unsigned chunk_size
= 16384;
2856 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
2857 if (target_buf
== NULL
) {
2858 LOG_ERROR("Out of memory");
2862 for (unsigned i
= 0; i
< chunk_size
; i
++) {
2863 switch (data_size
) {
2865 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
2868 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
2871 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
2878 int retval
= ERROR_OK
;
2880 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
2883 if (current
> chunk_size
)
2884 current
= chunk_size
;
2885 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
2886 if (retval
!= ERROR_OK
)
2888 /* avoid GDB timeouts */
2897 COMMAND_HANDLER(handle_mw_command
)
2900 return ERROR_COMMAND_SYNTAX_ERROR
;
2901 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2906 fn
= target_write_phys_memory
;
2908 fn
= target_write_memory
;
2909 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2910 return ERROR_COMMAND_SYNTAX_ERROR
;
2913 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2916 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2920 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2922 struct target
*target
= get_current_target(CMD_CTX
);
2924 switch (CMD_NAME
[2]) {
2935 return ERROR_COMMAND_SYNTAX_ERROR
;
2938 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
2941 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2942 uint32_t *min_address
, uint32_t *max_address
)
2944 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2945 return ERROR_COMMAND_SYNTAX_ERROR
;
2947 /* a base address isn't always necessary,
2948 * default to 0x0 (i.e. don't relocate) */
2949 if (CMD_ARGC
>= 2) {
2951 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2952 image
->base_address
= addr
;
2953 image
->base_address_set
= 1;
2955 image
->base_address_set
= 0;
2957 image
->start_address_set
= 0;
2960 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2961 if (CMD_ARGC
== 5) {
2962 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2963 /* use size (given) to find max (required) */
2964 *max_address
+= *min_address
;
2967 if (*min_address
> *max_address
)
2968 return ERROR_COMMAND_SYNTAX_ERROR
;
2973 COMMAND_HANDLER(handle_load_image_command
)
2977 uint32_t image_size
;
2978 uint32_t min_address
= 0;
2979 uint32_t max_address
= 0xffffffff;
2983 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2984 &image
, &min_address
, &max_address
);
2985 if (ERROR_OK
!= retval
)
2988 struct target
*target
= get_current_target(CMD_CTX
);
2990 struct duration bench
;
2991 duration_start(&bench
);
2993 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2998 for (i
= 0; i
< image
.num_sections
; i
++) {
2999 buffer
= malloc(image
.sections
[i
].size
);
3000 if (buffer
== NULL
) {
3001 command_print(CMD_CTX
,
3002 "error allocating buffer for section (%d bytes)",
3003 (int)(image
.sections
[i
].size
));
3007 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3008 if (retval
!= ERROR_OK
) {
3013 uint32_t offset
= 0;
3014 uint32_t length
= buf_cnt
;
3016 /* DANGER!!! beware of unsigned comparision here!!! */
3018 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3019 (image
.sections
[i
].base_address
< max_address
)) {
3021 if (image
.sections
[i
].base_address
< min_address
) {
3022 /* clip addresses below */
3023 offset
+= min_address
-image
.sections
[i
].base_address
;
3027 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3028 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3030 retval
= target_write_buffer(target
,
3031 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3032 if (retval
!= ERROR_OK
) {
3036 image_size
+= length
;
3037 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
3038 (unsigned int)length
,
3039 image
.sections
[i
].base_address
+ offset
);
3045 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3046 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
3047 "in %fs (%0.3f KiB/s)", image_size
,
3048 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3051 image_close(&image
);
3057 COMMAND_HANDLER(handle_dump_image_command
)
3059 struct fileio fileio
;
3061 int retval
, retvaltemp
;
3062 uint32_t address
, size
;
3063 struct duration bench
;
3064 struct target
*target
= get_current_target(CMD_CTX
);
3067 return ERROR_COMMAND_SYNTAX_ERROR
;
3069 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
3070 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
3072 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
3073 buffer
= malloc(buf_size
);
3077 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3078 if (retval
!= ERROR_OK
) {
3083 duration_start(&bench
);
3086 size_t size_written
;
3087 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
3088 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
3089 if (retval
!= ERROR_OK
)
3092 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
3093 if (retval
!= ERROR_OK
)
3096 size
-= this_run_size
;
3097 address
+= this_run_size
;
3102 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3104 retval
= fileio_size(&fileio
, &filesize
);
3105 if (retval
!= ERROR_OK
)
3107 command_print(CMD_CTX
,
3108 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize
,
3109 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3112 retvaltemp
= fileio_close(&fileio
);
3113 if (retvaltemp
!= ERROR_OK
)
3119 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
3123 uint32_t image_size
;
3126 uint32_t checksum
= 0;
3127 uint32_t mem_checksum
= 0;
3131 struct target
*target
= get_current_target(CMD_CTX
);
3134 return ERROR_COMMAND_SYNTAX_ERROR
;
3137 LOG_ERROR("no target selected");
3141 struct duration bench
;
3142 duration_start(&bench
);
3144 if (CMD_ARGC
>= 2) {
3146 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
3147 image
.base_address
= addr
;
3148 image
.base_address_set
= 1;
3150 image
.base_address_set
= 0;
3151 image
.base_address
= 0x0;
3154 image
.start_address_set
= 0;
3156 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3157 if (retval
!= ERROR_OK
)
3163 for (i
= 0; i
< image
.num_sections
; i
++) {
3164 buffer
= malloc(image
.sections
[i
].size
);
3165 if (buffer
== NULL
) {
3166 command_print(CMD_CTX
,
3167 "error allocating buffer for section (%d bytes)",
3168 (int)(image
.sections
[i
].size
));
3171 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3172 if (retval
!= ERROR_OK
) {
3178 /* calculate checksum of image */
3179 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3180 if (retval
!= ERROR_OK
) {
3185 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3186 if (retval
!= ERROR_OK
) {
3191 if (checksum
!= mem_checksum
) {
3192 /* failed crc checksum, fall back to a binary compare */
3196 LOG_ERROR("checksum mismatch - attempting binary compare");
3198 data
= malloc(buf_cnt
);
3200 /* Can we use 32bit word accesses? */
3202 int count
= buf_cnt
;
3203 if ((count
% 4) == 0) {
3207 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
3208 if (retval
== ERROR_OK
) {
3210 for (t
= 0; t
< buf_cnt
; t
++) {
3211 if (data
[t
] != buffer
[t
]) {
3212 command_print(CMD_CTX
,
3213 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3215 (unsigned)(t
+ image
.sections
[i
].base_address
),
3218 if (diffs
++ >= 127) {
3219 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
3231 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
3232 image
.sections
[i
].base_address
,
3237 image_size
+= buf_cnt
;
3240 command_print(CMD_CTX
, "No more differences found.");
3243 retval
= ERROR_FAIL
;
3244 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3245 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3246 "in %fs (%0.3f KiB/s)", image_size
,
3247 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3250 image_close(&image
);
3255 COMMAND_HANDLER(handle_verify_image_command
)
3257 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
3260 COMMAND_HANDLER(handle_test_image_command
)
3262 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
3265 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
3267 struct target
*target
= get_current_target(cmd_ctx
);
3268 struct breakpoint
*breakpoint
= target
->breakpoints
;
3269 while (breakpoint
) {
3270 if (breakpoint
->type
== BKPT_SOFT
) {
3271 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3272 breakpoint
->length
, 16);
3273 command_print(cmd_ctx
, "IVA breakpoint: 0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
3274 breakpoint
->address
,
3276 breakpoint
->set
, buf
);
3279 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3280 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3282 breakpoint
->length
, breakpoint
->set
);
3283 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3284 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3285 breakpoint
->address
,
3286 breakpoint
->length
, breakpoint
->set
);
3287 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3290 command_print(cmd_ctx
, "Breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3291 breakpoint
->address
,
3292 breakpoint
->length
, breakpoint
->set
);
3295 breakpoint
= breakpoint
->next
;
3300 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
3301 uint32_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3303 struct target
*target
= get_current_target(cmd_ctx
);
3306 int retval
= breakpoint_add(target
, addr
, length
, hw
);
3307 if (ERROR_OK
== retval
)
3308 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
3310 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3313 } else if (addr
== 0) {
3314 int retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3315 if (ERROR_OK
== retval
)
3316 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3318 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3322 int retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3323 if (ERROR_OK
== retval
)
3324 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3326 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3333 COMMAND_HANDLER(handle_bp_command
)
3342 return handle_bp_command_list(CMD_CTX
);
3346 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3347 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3348 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3351 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
3353 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3355 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3358 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3359 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
3361 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
3362 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3364 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3369 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3370 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
3371 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
3372 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3375 return ERROR_COMMAND_SYNTAX_ERROR
;
3379 COMMAND_HANDLER(handle_rbp_command
)
3382 return ERROR_COMMAND_SYNTAX_ERROR
;
3385 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3387 struct target
*target
= get_current_target(CMD_CTX
);
3388 breakpoint_remove(target
, addr
);
3393 COMMAND_HANDLER(handle_wp_command
)
3395 struct target
*target
= get_current_target(CMD_CTX
);
3397 if (CMD_ARGC
== 0) {
3398 struct watchpoint
*watchpoint
= target
->watchpoints
;
3400 while (watchpoint
) {
3401 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
3402 ", len: 0x%8.8" PRIx32
3403 ", r/w/a: %i, value: 0x%8.8" PRIx32
3404 ", mask: 0x%8.8" PRIx32
,
3405 watchpoint
->address
,
3407 (int)watchpoint
->rw
,
3410 watchpoint
= watchpoint
->next
;
3415 enum watchpoint_rw type
= WPT_ACCESS
;
3417 uint32_t length
= 0;
3418 uint32_t data_value
= 0x0;
3419 uint32_t data_mask
= 0xffffffff;
3423 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
3426 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3429 switch (CMD_ARGV
[2][0]) {
3440 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3441 return ERROR_COMMAND_SYNTAX_ERROR
;
3445 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3446 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3450 return ERROR_COMMAND_SYNTAX_ERROR
;
3453 int retval
= watchpoint_add(target
, addr
, length
, type
,
3454 data_value
, data_mask
);
3455 if (ERROR_OK
!= retval
)
3456 LOG_ERROR("Failure setting watchpoints");
3461 COMMAND_HANDLER(handle_rwp_command
)
3464 return ERROR_COMMAND_SYNTAX_ERROR
;
3467 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3469 struct target
*target
= get_current_target(CMD_CTX
);
3470 watchpoint_remove(target
, addr
);
3476 * Translate a virtual address to a physical address.
3478 * The low-level target implementation must have logged a detailed error
3479 * which is forwarded to telnet/GDB session.
3481 COMMAND_HANDLER(handle_virt2phys_command
)
3484 return ERROR_COMMAND_SYNTAX_ERROR
;
3487 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
3490 struct target
*target
= get_current_target(CMD_CTX
);
3491 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3492 if (retval
== ERROR_OK
)
3493 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
3498 static void writeData(FILE *f
, const void *data
, size_t len
)
3500 size_t written
= fwrite(data
, 1, len
, f
);
3502 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3505 static void writeLong(FILE *f
, int l
)
3508 for (i
= 0; i
< 4; i
++) {
3509 char c
= (l
>> (i
*8))&0xff;
3510 writeData(f
, &c
, 1);
3515 static void writeString(FILE *f
, char *s
)
3517 writeData(f
, s
, strlen(s
));
3520 typedef unsigned char UNIT
[2]; /* unit of profiling */
3522 /* Dump a gmon.out histogram file. */
3523 static void write_gmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
,
3524 bool with_range
, uint32_t start_address
, uint32_t end_address
)
3527 FILE *f
= fopen(filename
, "w");
3530 writeString(f
, "gmon");
3531 writeLong(f
, 0x00000001); /* Version */
3532 writeLong(f
, 0); /* padding */
3533 writeLong(f
, 0); /* padding */
3534 writeLong(f
, 0); /* padding */
3536 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3537 writeData(f
, &zero
, 1);
3539 /* figure out bucket size */
3543 min
= start_address
;
3548 for (i
= 0; i
< sampleNum
; i
++) {
3549 if (min
> samples
[i
])
3551 if (max
< samples
[i
])
3555 /* max should be (largest sample + 1)
3556 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
3560 int addressSpace
= max
- min
;
3561 assert(addressSpace
>= 2);
3563 /* FIXME: What is the reasonable number of buckets?
3564 * The profiling result will be more accurate if there are enough buckets. */
3565 static const uint32_t maxBuckets
= 128 * 1024; /* maximum buckets. */
3566 uint32_t numBuckets
= addressSpace
/ sizeof(UNIT
);
3567 if (numBuckets
> maxBuckets
)
3568 numBuckets
= maxBuckets
;
3569 int *buckets
= malloc(sizeof(int) * numBuckets
);
3570 if (buckets
== NULL
) {
3574 memset(buckets
, 0, sizeof(int) * numBuckets
);
3575 for (i
= 0; i
< sampleNum
; i
++) {
3576 uint32_t address
= samples
[i
];
3578 if ((address
< min
) || (max
<= address
))
3581 long long a
= address
- min
;
3582 long long b
= numBuckets
;
3583 long long c
= addressSpace
;
3584 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
3588 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3589 writeLong(f
, min
); /* low_pc */
3590 writeLong(f
, max
); /* high_pc */
3591 writeLong(f
, numBuckets
); /* # of buckets */
3592 writeLong(f
, 100); /* KLUDGE! We lie, ca. 100Hz best case. */
3593 writeString(f
, "seconds");
3594 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3595 writeData(f
, &zero
, 1);
3596 writeString(f
, "s");
3598 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3600 char *data
= malloc(2 * numBuckets
);
3602 for (i
= 0; i
< numBuckets
; i
++) {
3607 data
[i
* 2] = val
&0xff;
3608 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
3611 writeData(f
, data
, numBuckets
* 2);
3619 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3620 * which will be used as a random sampling of PC */
3621 COMMAND_HANDLER(handle_profile_command
)
3623 struct target
*target
= get_current_target(CMD_CTX
);
3625 if ((CMD_ARGC
!= 2) && (CMD_ARGC
!= 4))
3626 return ERROR_COMMAND_SYNTAX_ERROR
;
3628 const uint32_t MAX_PROFILE_SAMPLE_NUM
= 10000;
3630 uint32_t num_of_samples
;
3631 int retval
= ERROR_OK
;
3633 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], offset
);
3635 uint32_t *samples
= malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM
);
3636 if (samples
== NULL
) {
3637 LOG_ERROR("No memory to store samples.");
3642 * Some cores let us sample the PC without the
3643 * annoying halt/resume step; for example, ARMv7 PCSR.
3644 * Provide a way to use that more efficient mechanism.
3646 retval
= target_profiling(target
, samples
, MAX_PROFILE_SAMPLE_NUM
,
3647 &num_of_samples
, offset
);
3648 if (retval
!= ERROR_OK
) {
3653 assert(num_of_samples
<= MAX_PROFILE_SAMPLE_NUM
);
3655 retval
= target_poll(target
);
3656 if (retval
!= ERROR_OK
) {
3660 if (target
->state
== TARGET_RUNNING
) {
3661 retval
= target_halt(target
);
3662 if (retval
!= ERROR_OK
) {
3668 retval
= target_poll(target
);
3669 if (retval
!= ERROR_OK
) {
3674 uint32_t start_address
= 0;
3675 uint32_t end_address
= 0;
3676 bool with_range
= false;
3677 if (CMD_ARGC
== 4) {
3679 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], start_address
);
3680 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], end_address
);
3683 write_gmon(samples
, num_of_samples
, CMD_ARGV
[1],
3684 with_range
, start_address
, end_address
);
3685 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3691 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
3694 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3697 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3701 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3702 valObjPtr
= Jim_NewIntObj(interp
, val
);
3703 if (!nameObjPtr
|| !valObjPtr
) {
3708 Jim_IncrRefCount(nameObjPtr
);
3709 Jim_IncrRefCount(valObjPtr
);
3710 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3711 Jim_DecrRefCount(interp
, nameObjPtr
);
3712 Jim_DecrRefCount(interp
, valObjPtr
);
3714 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3718 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3720 struct command_context
*context
;
3721 struct target
*target
;
3723 context
= current_command_context(interp
);
3724 assert(context
!= NULL
);
3726 target
= get_current_target(context
);
3727 if (target
== NULL
) {
3728 LOG_ERROR("mem2array: no current target");
3732 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
3735 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3743 const char *varname
;
3747 /* argv[1] = name of array to receive the data
3748 * argv[2] = desired width
3749 * argv[3] = memory address
3750 * argv[4] = count of times to read
3753 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3756 varname
= Jim_GetString(argv
[0], &len
);
3757 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3759 e
= Jim_GetLong(interp
, argv
[1], &l
);
3764 e
= Jim_GetLong(interp
, argv
[2], &l
);
3768 e
= Jim_GetLong(interp
, argv
[3], &l
);
3783 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3784 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3788 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3789 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3792 if ((addr
+ (len
* width
)) < addr
) {
3793 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3794 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3797 /* absurd transfer size? */
3799 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3800 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3805 ((width
== 2) && ((addr
& 1) == 0)) ||
3806 ((width
== 4) && ((addr
& 3) == 0))) {
3810 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3811 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3814 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3823 size_t buffersize
= 4096;
3824 uint8_t *buffer
= malloc(buffersize
);
3831 /* Slurp... in buffer size chunks */
3833 count
= len
; /* in objects.. */
3834 if (count
> (buffersize
/ width
))
3835 count
= (buffersize
/ width
);
3837 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3838 if (retval
!= ERROR_OK
) {
3840 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3844 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3845 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3849 v
= 0; /* shut up gcc */
3850 for (i
= 0; i
< count
; i
++, n
++) {
3853 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3856 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3859 v
= buffer
[i
] & 0x0ff;
3862 new_int_array_element(interp
, varname
, n
, v
);
3865 addr
+= count
* width
;
3871 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3876 static int get_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t *val
)
3879 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3883 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3887 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3893 Jim_IncrRefCount(nameObjPtr
);
3894 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3895 Jim_DecrRefCount(interp
, nameObjPtr
);
3897 if (valObjPtr
== NULL
)
3900 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3901 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3906 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3908 struct command_context
*context
;
3909 struct target
*target
;
3911 context
= current_command_context(interp
);
3912 assert(context
!= NULL
);
3914 target
= get_current_target(context
);
3915 if (target
== NULL
) {
3916 LOG_ERROR("array2mem: no current target");
3920 return target_array2mem(interp
, target
, argc
-1, argv
+ 1);
3923 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
3924 int argc
, Jim_Obj
*const *argv
)
3932 const char *varname
;
3936 /* argv[1] = name of array to get the data
3937 * argv[2] = desired width
3938 * argv[3] = memory address
3939 * argv[4] = count to write
3942 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
3945 varname
= Jim_GetString(argv
[0], &len
);
3946 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3948 e
= Jim_GetLong(interp
, argv
[1], &l
);
3953 e
= Jim_GetLong(interp
, argv
[2], &l
);
3957 e
= Jim_GetLong(interp
, argv
[3], &l
);
3972 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3973 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3974 "Invalid width param, must be 8/16/32", NULL
);
3978 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3979 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3980 "array2mem: zero width read?", NULL
);
3983 if ((addr
+ (len
* width
)) < addr
) {
3984 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3985 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3986 "array2mem: addr + len - wraps to zero?", NULL
);
3989 /* absurd transfer size? */
3991 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3992 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3993 "array2mem: absurd > 64K item request", NULL
);
3998 ((width
== 2) && ((addr
& 1) == 0)) ||
3999 ((width
== 4) && ((addr
& 3) == 0))) {
4003 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4004 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
4007 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4018 size_t buffersize
= 4096;
4019 uint8_t *buffer
= malloc(buffersize
);
4024 /* Slurp... in buffer size chunks */
4026 count
= len
; /* in objects.. */
4027 if (count
> (buffersize
/ width
))
4028 count
= (buffersize
/ width
);
4030 v
= 0; /* shut up gcc */
4031 for (i
= 0; i
< count
; i
++, n
++) {
4032 get_int_array_element(interp
, varname
, n
, &v
);
4035 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
4038 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
4041 buffer
[i
] = v
& 0x0ff;
4047 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
4048 if (retval
!= ERROR_OK
) {
4050 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
4054 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4055 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
4059 addr
+= count
* width
;
4064 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4069 /* FIX? should we propagate errors here rather than printing them
4072 void target_handle_event(struct target
*target
, enum target_event e
)
4074 struct target_event_action
*teap
;
4076 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4077 if (teap
->event
== e
) {
4078 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
4079 target
->target_number
,
4080 target_name(target
),
4081 target_type_name(target
),
4083 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
4084 Jim_GetString(teap
->body
, NULL
));
4085 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
) {
4086 Jim_MakeErrorMessage(teap
->interp
);
4087 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
4094 * Returns true only if the target has a handler for the specified event.
4096 bool target_has_event_action(struct target
*target
, enum target_event event
)
4098 struct target_event_action
*teap
;
4100 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4101 if (teap
->event
== event
)
4107 enum target_cfg_param
{
4110 TCFG_WORK_AREA_VIRT
,
4111 TCFG_WORK_AREA_PHYS
,
4112 TCFG_WORK_AREA_SIZE
,
4113 TCFG_WORK_AREA_BACKUP
,
4117 TCFG_CHAIN_POSITION
,
4122 static Jim_Nvp nvp_config_opts
[] = {
4123 { .name
= "-type", .value
= TCFG_TYPE
},
4124 { .name
= "-event", .value
= TCFG_EVENT
},
4125 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
4126 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
4127 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
4128 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
4129 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
4130 { .name
= "-variant", .value
= TCFG_VARIANT
},
4131 { .name
= "-coreid", .value
= TCFG_COREID
},
4132 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
4133 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
4134 { .name
= "-rtos", .value
= TCFG_RTOS
},
4135 { .name
= NULL
, .value
= -1 }
4138 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
4146 /* parse config or cget options ... */
4147 while (goi
->argc
> 0) {
4148 Jim_SetEmptyResult(goi
->interp
);
4149 /* Jim_GetOpt_Debug(goi); */
4151 if (target
->type
->target_jim_configure
) {
4152 /* target defines a configure function */
4153 /* target gets first dibs on parameters */
4154 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
4163 /* otherwise we 'continue' below */
4165 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
4167 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
4173 if (goi
->isconfigure
) {
4174 Jim_SetResultFormatted(goi
->interp
,
4175 "not settable: %s", n
->name
);
4179 if (goi
->argc
!= 0) {
4180 Jim_WrongNumArgs(goi
->interp
,
4181 goi
->argc
, goi
->argv
,
4186 Jim_SetResultString(goi
->interp
,
4187 target_type_name(target
), -1);
4191 if (goi
->argc
== 0) {
4192 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
4196 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
4198 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
4202 if (goi
->isconfigure
) {
4203 if (goi
->argc
!= 1) {
4204 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
4208 if (goi
->argc
!= 0) {
4209 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
4215 struct target_event_action
*teap
;
4217 teap
= target
->event_action
;
4218 /* replace existing? */
4220 if (teap
->event
== (enum target_event
)n
->value
)
4225 if (goi
->isconfigure
) {
4226 bool replace
= true;
4229 teap
= calloc(1, sizeof(*teap
));
4232 teap
->event
= n
->value
;
4233 teap
->interp
= goi
->interp
;
4234 Jim_GetOpt_Obj(goi
, &o
);
4236 Jim_DecrRefCount(teap
->interp
, teap
->body
);
4237 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
4240 * Tcl/TK - "tk events" have a nice feature.
4241 * See the "BIND" command.
4242 * We should support that here.
4243 * You can specify %X and %Y in the event code.
4244 * The idea is: %T - target name.
4245 * The idea is: %N - target number
4246 * The idea is: %E - event name.
4248 Jim_IncrRefCount(teap
->body
);
4251 /* add to head of event list */
4252 teap
->next
= target
->event_action
;
4253 target
->event_action
= teap
;
4255 Jim_SetEmptyResult(goi
->interp
);
4259 Jim_SetEmptyResult(goi
->interp
);
4261 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
4267 case TCFG_WORK_AREA_VIRT
:
4268 if (goi
->isconfigure
) {
4269 target_free_all_working_areas(target
);
4270 e
= Jim_GetOpt_Wide(goi
, &w
);
4273 target
->working_area_virt
= w
;
4274 target
->working_area_virt_spec
= true;
4279 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
4283 case TCFG_WORK_AREA_PHYS
:
4284 if (goi
->isconfigure
) {
4285 target_free_all_working_areas(target
);
4286 e
= Jim_GetOpt_Wide(goi
, &w
);
4289 target
->working_area_phys
= w
;
4290 target
->working_area_phys_spec
= true;
4295 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
4299 case TCFG_WORK_AREA_SIZE
:
4300 if (goi
->isconfigure
) {
4301 target_free_all_working_areas(target
);
4302 e
= Jim_GetOpt_Wide(goi
, &w
);
4305 target
->working_area_size
= w
;
4310 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4314 case TCFG_WORK_AREA_BACKUP
:
4315 if (goi
->isconfigure
) {
4316 target_free_all_working_areas(target
);
4317 e
= Jim_GetOpt_Wide(goi
, &w
);
4320 /* make this exactly 1 or 0 */
4321 target
->backup_working_area
= (!!w
);
4326 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
4327 /* loop for more e*/
4332 if (goi
->isconfigure
) {
4333 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
4335 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
4338 target
->endianness
= n
->value
;
4343 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4344 if (n
->name
== NULL
) {
4345 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4346 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4348 Jim_SetResultString(goi
->interp
, n
->name
, -1);
4353 if (goi
->isconfigure
) {
4354 if (goi
->argc
< 1) {
4355 Jim_SetResultFormatted(goi
->interp
,
4360 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
4363 free(target
->variant
);
4364 target
->variant
= strdup(cp
);
4369 Jim_SetResultString(goi
->interp
, target
->variant
, -1);
4374 if (goi
->isconfigure
) {
4375 e
= Jim_GetOpt_Wide(goi
, &w
);
4378 target
->coreid
= (int32_t)w
;
4383 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4387 case TCFG_CHAIN_POSITION
:
4388 if (goi
->isconfigure
) {
4390 struct jtag_tap
*tap
;
4391 target_free_all_working_areas(target
);
4392 e
= Jim_GetOpt_Obj(goi
, &o_t
);
4395 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
4398 /* make this exactly 1 or 0 */
4404 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
4405 /* loop for more e*/
4408 if (goi
->isconfigure
) {
4409 e
= Jim_GetOpt_Wide(goi
, &w
);
4412 target
->dbgbase
= (uint32_t)w
;
4413 target
->dbgbase_set
= true;
4418 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
4425 int result
= rtos_create(goi
, target
);
4426 if (result
!= JIM_OK
)
4432 } /* while (goi->argc) */
4435 /* done - we return */
4439 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4443 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4444 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4445 int need_args
= 1 + goi
.isconfigure
;
4446 if (goi
.argc
< need_args
) {
4447 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4449 ? "missing: -option VALUE ..."
4450 : "missing: -option ...");
4453 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4454 return target_configure(&goi
, target
);
4457 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4459 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4462 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4464 if (goi
.argc
< 2 || goi
.argc
> 4) {
4465 Jim_SetResultFormatted(goi
.interp
,
4466 "usage: %s [phys] <address> <data> [<count>]", cmd_name
);
4471 fn
= target_write_memory
;
4474 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4476 struct Jim_Obj
*obj
;
4477 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4481 fn
= target_write_phys_memory
;
4485 e
= Jim_GetOpt_Wide(&goi
, &a
);
4490 e
= Jim_GetOpt_Wide(&goi
, &b
);
4495 if (goi
.argc
== 1) {
4496 e
= Jim_GetOpt_Wide(&goi
, &c
);
4501 /* all args must be consumed */
4505 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4507 if (strcasecmp(cmd_name
, "mww") == 0)
4509 else if (strcasecmp(cmd_name
, "mwh") == 0)
4511 else if (strcasecmp(cmd_name
, "mwb") == 0)
4514 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4518 return (target_fill_mem(target
, a
, fn
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4522 * @brief Reads an array of words/halfwords/bytes from target memory starting at specified address.
4524 * Usage: mdw [phys] <address> [<count>] - for 32 bit reads
4525 * mdh [phys] <address> [<count>] - for 16 bit reads
4526 * mdb [phys] <address> [<count>] - for 8 bit reads
4528 * Count defaults to 1.
4530 * Calls target_read_memory or target_read_phys_memory depending on
4531 * the presence of the "phys" argument
4532 * Reads the target memory in blocks of max. 32 bytes, and returns an array of ints formatted
4533 * to int representation in base16.
4534 * Also outputs read data in a human readable form using command_print
4536 * @param phys if present target_read_phys_memory will be used instead of target_read_memory
4537 * @param address address where to start the read. May be specified in decimal or hex using the standard "0x" prefix
4538 * @param count optional count parameter to read an array of values. If not specified, defaults to 1.
4539 * @returns: JIM_ERR on error or JIM_OK on success and sets the result string to an array of ascii formatted numbers
4540 * on success, with [<count>] number of elements.
4542 * In case of little endian target:
4543 * Example1: "mdw 0x00000000" returns "10123456"
4544 * Exmaple2: "mdh 0x00000000 1" returns "3456"
4545 * Example3: "mdb 0x00000000" returns "56"
4546 * Example4: "mdh 0x00000000 2" returns "3456 1012"
4547 * Example5: "mdb 0x00000000 3" returns "56 34 12"
4549 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4551 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4554 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4556 if ((goi
.argc
< 1) || (goi
.argc
> 3)) {
4557 Jim_SetResultFormatted(goi
.interp
,
4558 "usage: %s [phys] <address> [<count>]", cmd_name
);
4562 int (*fn
)(struct target
*target
,
4563 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
4564 fn
= target_read_memory
;
4567 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4569 struct Jim_Obj
*obj
;
4570 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4574 fn
= target_read_phys_memory
;
4577 /* Read address parameter */
4579 e
= Jim_GetOpt_Wide(&goi
, &addr
);
4583 /* If next parameter exists, read it out as the count parameter, if not, set it to 1 (default) */
4585 if (goi
.argc
== 1) {
4586 e
= Jim_GetOpt_Wide(&goi
, &count
);
4592 /* all args must be consumed */
4596 jim_wide dwidth
= 1; /* shut up gcc */
4597 if (strcasecmp(cmd_name
, "mdw") == 0)
4599 else if (strcasecmp(cmd_name
, "mdh") == 0)
4601 else if (strcasecmp(cmd_name
, "mdb") == 0)
4604 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4608 /* convert count to "bytes" */
4609 int bytes
= count
* dwidth
;
4611 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4612 uint8_t target_buf
[32];
4615 y
= (bytes
< 16) ? bytes
: 16; /* y = min(bytes, 16); */
4617 /* Try to read out next block */
4618 e
= fn(target
, addr
, dwidth
, y
/ dwidth
, target_buf
);
4620 if (e
!= ERROR_OK
) {
4621 Jim_SetResultFormatted(interp
, "error reading target @ 0x%08lx", (long)addr
);
4625 command_print_sameline(NULL
, "0x%08x ", (int)(addr
));
4628 for (x
= 0; x
< 16 && x
< y
; x
+= 4) {
4629 z
= target_buffer_get_u32(target
, &(target_buf
[x
]));
4630 command_print_sameline(NULL
, "%08x ", (int)(z
));
4632 for (; (x
< 16) ; x
+= 4)
4633 command_print_sameline(NULL
, " ");
4636 for (x
= 0; x
< 16 && x
< y
; x
+= 2) {
4637 z
= target_buffer_get_u16(target
, &(target_buf
[x
]));
4638 command_print_sameline(NULL
, "%04x ", (int)(z
));
4640 for (; (x
< 16) ; x
+= 2)
4641 command_print_sameline(NULL
, " ");
4645 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
4646 z
= target_buffer_get_u8(target
, &(target_buf
[x
]));
4647 command_print_sameline(NULL
, "%02x ", (int)(z
));
4649 for (; (x
< 16) ; x
+= 1)
4650 command_print_sameline(NULL
, " ");
4653 /* ascii-ify the bytes */
4654 for (x
= 0 ; x
< y
; x
++) {
4655 if ((target_buf
[x
] >= 0x20) &&
4656 (target_buf
[x
] <= 0x7e)) {
4660 target_buf
[x
] = '.';
4665 target_buf
[x
] = ' ';
4670 /* print - with a newline */
4671 command_print_sameline(NULL
, "%s\n", target_buf
);
4679 static int jim_target_mem2array(Jim_Interp
*interp
,
4680 int argc
, Jim_Obj
*const *argv
)
4682 struct target
*target
= Jim_CmdPrivData(interp
);
4683 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4686 static int jim_target_array2mem(Jim_Interp
*interp
,
4687 int argc
, Jim_Obj
*const *argv
)
4689 struct target
*target
= Jim_CmdPrivData(interp
);
4690 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4693 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4695 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
4699 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4702 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4705 struct target
*target
= Jim_CmdPrivData(interp
);
4706 if (!target
->tap
->enabled
)
4707 return jim_target_tap_disabled(interp
);
4709 int e
= target
->type
->examine(target
);
4715 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4718 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4721 struct target
*target
= Jim_CmdPrivData(interp
);
4723 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
4729 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4732 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4735 struct target
*target
= Jim_CmdPrivData(interp
);
4736 if (!target
->tap
->enabled
)
4737 return jim_target_tap_disabled(interp
);
4740 if (!(target_was_examined(target
)))
4741 e
= ERROR_TARGET_NOT_EXAMINED
;
4743 e
= target
->type
->poll(target
);
4749 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4752 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4754 if (goi
.argc
!= 2) {
4755 Jim_WrongNumArgs(interp
, 0, argv
,
4756 "([tT]|[fF]|assert|deassert) BOOL");
4761 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4763 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4766 /* the halt or not param */
4768 e
= Jim_GetOpt_Wide(&goi
, &a
);
4772 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4773 if (!target
->tap
->enabled
)
4774 return jim_target_tap_disabled(interp
);
4775 if (!(target_was_examined(target
))) {
4776 LOG_ERROR("Target not examined yet");
4777 return ERROR_TARGET_NOT_EXAMINED
;
4779 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
4780 Jim_SetResultFormatted(interp
,
4781 "No target-specific reset for %s",
4782 target_name(target
));
4785 /* determine if we should halt or not. */
4786 target
->reset_halt
= !!a
;
4787 /* When this happens - all workareas are invalid. */
4788 target_free_all_working_areas_restore(target
, 0);
4791 if (n
->value
== NVP_ASSERT
)
4792 e
= target
->type
->assert_reset(target
);
4794 e
= target
->type
->deassert_reset(target
);
4795 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4798 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4801 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4804 struct target
*target
= Jim_CmdPrivData(interp
);
4805 if (!target
->tap
->enabled
)
4806 return jim_target_tap_disabled(interp
);
4807 int e
= target
->type
->halt(target
);
4808 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4811 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4814 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4816 /* params: <name> statename timeoutmsecs */
4817 if (goi
.argc
!= 2) {
4818 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4819 Jim_SetResultFormatted(goi
.interp
,
4820 "%s <state_name> <timeout_in_msec>", cmd_name
);
4825 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4827 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
, 1);
4831 e
= Jim_GetOpt_Wide(&goi
, &a
);
4834 struct target
*target
= Jim_CmdPrivData(interp
);
4835 if (!target
->tap
->enabled
)
4836 return jim_target_tap_disabled(interp
);
4838 e
= target_wait_state(target
, n
->value
, a
);
4839 if (e
!= ERROR_OK
) {
4840 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
4841 Jim_SetResultFormatted(goi
.interp
,
4842 "target: %s wait %s fails (%#s) %s",
4843 target_name(target
), n
->name
,
4844 eObj
, target_strerror_safe(e
));
4845 Jim_FreeNewObj(interp
, eObj
);
4850 /* List for human, Events defined for this target.
4851 * scripts/programs should use 'name cget -event NAME'
4853 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4855 struct command_context
*cmd_ctx
= current_command_context(interp
);
4856 assert(cmd_ctx
!= NULL
);
4858 struct target
*target
= Jim_CmdPrivData(interp
);
4859 struct target_event_action
*teap
= target
->event_action
;
4860 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4861 target
->target_number
,
4862 target_name(target
));
4863 command_print(cmd_ctx
, "%-25s | Body", "Event");
4864 command_print(cmd_ctx
, "------------------------- | "
4865 "----------------------------------------");
4867 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
4868 command_print(cmd_ctx
, "%-25s | %s",
4869 opt
->name
, Jim_GetString(teap
->body
, NULL
));
4872 command_print(cmd_ctx
, "***END***");
4875 static int jim_target_current_state(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 Jim_SetResultString(interp
, target_state_name(target
), -1);
4885 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4888 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4889 if (goi
.argc
!= 1) {
4890 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4891 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
4895 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4897 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4900 struct target
*target
= Jim_CmdPrivData(interp
);
4901 target_handle_event(target
, n
->value
);
4905 static const struct command_registration target_instance_command_handlers
[] = {
4907 .name
= "configure",
4908 .mode
= COMMAND_CONFIG
,
4909 .jim_handler
= jim_target_configure
,
4910 .help
= "configure a new target for use",
4911 .usage
= "[target_attribute ...]",
4915 .mode
= COMMAND_ANY
,
4916 .jim_handler
= jim_target_configure
,
4917 .help
= "returns the specified target attribute",
4918 .usage
= "target_attribute",
4922 .mode
= COMMAND_EXEC
,
4923 .jim_handler
= jim_target_mw
,
4924 .help
= "Write 32-bit word(s) to target memory",
4925 .usage
= "address data [count]",
4929 .mode
= COMMAND_EXEC
,
4930 .jim_handler
= jim_target_mw
,
4931 .help
= "Write 16-bit half-word(s) to target memory",
4932 .usage
= "address data [count]",
4936 .mode
= COMMAND_EXEC
,
4937 .jim_handler
= jim_target_mw
,
4938 .help
= "Write byte(s) to target memory",
4939 .usage
= "address data [count]",
4943 .mode
= COMMAND_EXEC
,
4944 .jim_handler
= jim_target_md
,
4945 .help
= "Display target memory as 32-bit words",
4946 .usage
= "address [count]",
4950 .mode
= COMMAND_EXEC
,
4951 .jim_handler
= jim_target_md
,
4952 .help
= "Display target memory as 16-bit half-words",
4953 .usage
= "address [count]",
4957 .mode
= COMMAND_EXEC
,
4958 .jim_handler
= jim_target_md
,
4959 .help
= "Display target memory as 8-bit bytes",
4960 .usage
= "address [count]",
4963 .name
= "array2mem",
4964 .mode
= COMMAND_EXEC
,
4965 .jim_handler
= jim_target_array2mem
,
4966 .help
= "Writes Tcl array of 8/16/32 bit numbers "
4968 .usage
= "arrayname bitwidth address count",
4971 .name
= "mem2array",
4972 .mode
= COMMAND_EXEC
,
4973 .jim_handler
= jim_target_mem2array
,
4974 .help
= "Loads Tcl array of 8/16/32 bit numbers "
4975 "from target memory",
4976 .usage
= "arrayname bitwidth address count",
4979 .name
= "eventlist",
4980 .mode
= COMMAND_EXEC
,
4981 .jim_handler
= jim_target_event_list
,
4982 .help
= "displays a table of events defined for this target",
4986 .mode
= COMMAND_EXEC
,
4987 .jim_handler
= jim_target_current_state
,
4988 .help
= "displays the current state of this target",
4991 .name
= "arp_examine",
4992 .mode
= COMMAND_EXEC
,
4993 .jim_handler
= jim_target_examine
,
4994 .help
= "used internally for reset processing",
4997 .name
= "arp_halt_gdb",
4998 .mode
= COMMAND_EXEC
,
4999 .jim_handler
= jim_target_halt_gdb
,
5000 .help
= "used internally for reset processing to halt GDB",
5004 .mode
= COMMAND_EXEC
,
5005 .jim_handler
= jim_target_poll
,
5006 .help
= "used internally for reset processing",
5009 .name
= "arp_reset",
5010 .mode
= COMMAND_EXEC
,
5011 .jim_handler
= jim_target_reset
,
5012 .help
= "used internally for reset processing",
5016 .mode
= COMMAND_EXEC
,
5017 .jim_handler
= jim_target_halt
,
5018 .help
= "used internally for reset processing",
5021 .name
= "arp_waitstate",
5022 .mode
= COMMAND_EXEC
,
5023 .jim_handler
= jim_target_wait_state
,
5024 .help
= "used internally for reset processing",
5027 .name
= "invoke-event",
5028 .mode
= COMMAND_EXEC
,
5029 .jim_handler
= jim_target_invoke_event
,
5030 .help
= "invoke handler for specified event",
5031 .usage
= "event_name",
5033 COMMAND_REGISTRATION_DONE
5036 static int target_create(Jim_GetOptInfo
*goi
)
5044 struct target
*target
;
5045 struct command_context
*cmd_ctx
;
5047 cmd_ctx
= current_command_context(goi
->interp
);
5048 assert(cmd_ctx
!= NULL
);
5050 if (goi
->argc
< 3) {
5051 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
5056 Jim_GetOpt_Obj(goi
, &new_cmd
);
5057 /* does this command exist? */
5058 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
5060 cp
= Jim_GetString(new_cmd
, NULL
);
5061 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
5066 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
5070 struct transport
*tr
= get_current_transport();
5071 if (tr
->override_target
) {
5072 e
= tr
->override_target(&cp
);
5073 if (e
!= ERROR_OK
) {
5074 LOG_ERROR("The selected transport doesn't support this target");
5077 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
5079 /* now does target type exist */
5080 for (x
= 0 ; target_types
[x
] ; x
++) {
5081 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
5086 /* check for deprecated name */
5087 if (target_types
[x
]->deprecated_name
) {
5088 if (0 == strcmp(cp
, target_types
[x
]->deprecated_name
)) {
5090 LOG_WARNING("target name is deprecated use: \'%s\'", target_types
[x
]->name
);
5095 if (target_types
[x
] == NULL
) {
5096 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
5097 for (x
= 0 ; target_types
[x
] ; x
++) {
5098 if (target_types
[x
+ 1]) {
5099 Jim_AppendStrings(goi
->interp
,
5100 Jim_GetResult(goi
->interp
),
5101 target_types
[x
]->name
,
5104 Jim_AppendStrings(goi
->interp
,
5105 Jim_GetResult(goi
->interp
),
5107 target_types
[x
]->name
, NULL
);
5114 target
= calloc(1, sizeof(struct target
));
5115 /* set target number */
5116 target
->target_number
= new_target_number();
5117 cmd_ctx
->current_target
= target
->target_number
;
5119 /* allocate memory for each unique target type */
5120 target
->type
= calloc(1, sizeof(struct target_type
));
5122 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
5124 /* will be set by "-endian" */
5125 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5127 /* default to first core, override with -coreid */
5130 target
->working_area
= 0x0;
5131 target
->working_area_size
= 0x0;
5132 target
->working_areas
= NULL
;
5133 target
->backup_working_area
= 0;
5135 target
->state
= TARGET_UNKNOWN
;
5136 target
->debug_reason
= DBG_REASON_UNDEFINED
;
5137 target
->reg_cache
= NULL
;
5138 target
->breakpoints
= NULL
;
5139 target
->watchpoints
= NULL
;
5140 target
->next
= NULL
;
5141 target
->arch_info
= NULL
;
5143 target
->display
= 1;
5145 target
->halt_issued
= false;
5147 /* initialize trace information */
5148 target
->trace_info
= malloc(sizeof(struct trace
));
5149 target
->trace_info
->num_trace_points
= 0;
5150 target
->trace_info
->trace_points_size
= 0;
5151 target
->trace_info
->trace_points
= NULL
;
5152 target
->trace_info
->trace_history_size
= 0;
5153 target
->trace_info
->trace_history
= NULL
;
5154 target
->trace_info
->trace_history_pos
= 0;
5155 target
->trace_info
->trace_history_overflowed
= 0;
5157 target
->dbgmsg
= NULL
;
5158 target
->dbg_msg_enabled
= 0;
5160 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5162 target
->rtos
= NULL
;
5163 target
->rtos_auto_detect
= false;
5165 /* Do the rest as "configure" options */
5166 goi
->isconfigure
= 1;
5167 e
= target_configure(goi
, target
);
5169 if (target
->tap
== NULL
) {
5170 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
5180 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
5181 /* default endian to little if not specified */
5182 target
->endianness
= TARGET_LITTLE_ENDIAN
;
5185 /* incase variant is not set */
5186 if (!target
->variant
)
5187 target
->variant
= strdup("");
5189 cp
= Jim_GetString(new_cmd
, NULL
);
5190 target
->cmd_name
= strdup(cp
);
5192 /* create the target specific commands */
5193 if (target
->type
->commands
) {
5194 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
5196 LOG_ERROR("unable to register '%s' commands", cp
);
5198 if (target
->type
->target_create
)
5199 (*(target
->type
->target_create
))(target
, goi
->interp
);
5201 /* append to end of list */
5203 struct target
**tpp
;
5204 tpp
= &(all_targets
);
5206 tpp
= &((*tpp
)->next
);
5210 /* now - create the new target name command */
5211 const struct command_registration target_subcommands
[] = {
5213 .chain
= target_instance_command_handlers
,
5216 .chain
= target
->type
->commands
,
5218 COMMAND_REGISTRATION_DONE
5220 const struct command_registration target_commands
[] = {
5223 .mode
= COMMAND_ANY
,
5224 .help
= "target command group",
5226 .chain
= target_subcommands
,
5228 COMMAND_REGISTRATION_DONE
5230 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
5234 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
5236 command_set_handler_data(c
, target
);
5238 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
5241 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5244 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5247 struct command_context
*cmd_ctx
= current_command_context(interp
);
5248 assert(cmd_ctx
!= NULL
);
5250 Jim_SetResultString(interp
, target_name(get_current_target(cmd_ctx
)), -1);
5254 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5257 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5260 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5261 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++) {
5262 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5263 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
5268 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5271 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5274 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5275 struct target
*target
= all_targets
;
5277 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5278 Jim_NewStringObj(interp
, target_name(target
), -1));
5279 target
= target
->next
;
5284 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5287 const char *targetname
;
5289 struct target
*target
= (struct target
*) NULL
;
5290 struct target_list
*head
, *curr
, *new;
5291 curr
= (struct target_list
*) NULL
;
5292 head
= (struct target_list
*) NULL
;
5295 LOG_DEBUG("%d", argc
);
5296 /* argv[1] = target to associate in smp
5297 * argv[2] = target to assoicate in smp
5301 for (i
= 1; i
< argc
; i
++) {
5303 targetname
= Jim_GetString(argv
[i
], &len
);
5304 target
= get_target(targetname
);
5305 LOG_DEBUG("%s ", targetname
);
5307 new = malloc(sizeof(struct target_list
));
5308 new->target
= target
;
5309 new->next
= (struct target_list
*)NULL
;
5310 if (head
== (struct target_list
*)NULL
) {
5319 /* now parse the list of cpu and put the target in smp mode*/
5322 while (curr
!= (struct target_list
*)NULL
) {
5323 target
= curr
->target
;
5325 target
->head
= head
;
5329 if (target
&& target
->rtos
)
5330 retval
= rtos_smp_init(head
->target
);
5336 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5339 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5341 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5342 "<name> <target_type> [<target_options> ...]");
5345 return target_create(&goi
);
5348 static int jim_target_number(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5351 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5353 /* It's OK to remove this mechanism sometime after August 2010 or so */
5354 LOG_WARNING("don't use numbers as target identifiers; use names");
5355 if (goi
.argc
!= 1) {
5356 Jim_SetResultFormatted(goi
.interp
, "usage: target number <number>");
5360 int e
= Jim_GetOpt_Wide(&goi
, &w
);
5364 struct target
*target
;
5365 for (target
= all_targets
; NULL
!= target
; target
= target
->next
) {
5366 if (target
->target_number
!= w
)
5369 Jim_SetResultString(goi
.interp
, target_name(target
), -1);
5373 Jim_Obj
*wObj
= Jim_NewIntObj(goi
.interp
, w
);
5374 Jim_SetResultFormatted(goi
.interp
,
5375 "Target: number %#s does not exist", wObj
);
5376 Jim_FreeNewObj(interp
, wObj
);
5381 static int jim_target_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5384 Jim_WrongNumArgs(interp
, 1, argv
, "<no parameters>");
5388 struct target
*target
= all_targets
;
5389 while (NULL
!= target
) {
5390 target
= target
->next
;
5393 Jim_SetResult(interp
, Jim_NewIntObj(interp
, count
));
5397 static const struct command_registration target_subcommand_handlers
[] = {
5400 .mode
= COMMAND_CONFIG
,
5401 .handler
= handle_target_init_command
,
5402 .help
= "initialize targets",
5406 /* REVISIT this should be COMMAND_CONFIG ... */
5407 .mode
= COMMAND_ANY
,
5408 .jim_handler
= jim_target_create
,
5409 .usage
= "name type '-chain-position' name [options ...]",
5410 .help
= "Creates and selects a new target",
5414 .mode
= COMMAND_ANY
,
5415 .jim_handler
= jim_target_current
,
5416 .help
= "Returns the currently selected target",
5420 .mode
= COMMAND_ANY
,
5421 .jim_handler
= jim_target_types
,
5422 .help
= "Returns the available target types as "
5423 "a list of strings",
5427 .mode
= COMMAND_ANY
,
5428 .jim_handler
= jim_target_names
,
5429 .help
= "Returns the names of all targets as a list of strings",
5433 .mode
= COMMAND_ANY
,
5434 .jim_handler
= jim_target_number
,
5436 .help
= "Returns the name of the numbered target "
5441 .mode
= COMMAND_ANY
,
5442 .jim_handler
= jim_target_count
,
5443 .help
= "Returns the number of targets as an integer "
5448 .mode
= COMMAND_ANY
,
5449 .jim_handler
= jim_target_smp
,
5450 .usage
= "targetname1 targetname2 ...",
5451 .help
= "gather several target in a smp list"
5454 COMMAND_REGISTRATION_DONE
5464 static int fastload_num
;
5465 static struct FastLoad
*fastload
;
5467 static void free_fastload(void)
5469 if (fastload
!= NULL
) {
5471 for (i
= 0; i
< fastload_num
; i
++) {
5472 if (fastload
[i
].data
)
5473 free(fastload
[i
].data
);
5480 COMMAND_HANDLER(handle_fast_load_image_command
)
5484 uint32_t image_size
;
5485 uint32_t min_address
= 0;
5486 uint32_t max_address
= 0xffffffff;
5491 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
5492 &image
, &min_address
, &max_address
);
5493 if (ERROR_OK
!= retval
)
5496 struct duration bench
;
5497 duration_start(&bench
);
5499 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5500 if (retval
!= ERROR_OK
)
5505 fastload_num
= image
.num_sections
;
5506 fastload
= malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5507 if (fastload
== NULL
) {
5508 command_print(CMD_CTX
, "out of memory");
5509 image_close(&image
);
5512 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5513 for (i
= 0; i
< image
.num_sections
; i
++) {
5514 buffer
= malloc(image
.sections
[i
].size
);
5515 if (buffer
== NULL
) {
5516 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5517 (int)(image
.sections
[i
].size
));
5518 retval
= ERROR_FAIL
;
5522 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
5523 if (retval
!= ERROR_OK
) {
5528 uint32_t offset
= 0;
5529 uint32_t length
= buf_cnt
;
5531 /* DANGER!!! beware of unsigned comparision here!!! */
5533 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
5534 (image
.sections
[i
].base_address
< max_address
)) {
5535 if (image
.sections
[i
].base_address
< min_address
) {
5536 /* clip addresses below */
5537 offset
+= min_address
-image
.sections
[i
].base_address
;
5541 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5542 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5544 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5545 fastload
[i
].data
= malloc(length
);
5546 if (fastload
[i
].data
== NULL
) {
5548 command_print(CMD_CTX
, "error allocating buffer for section (%" PRIu32
" bytes)",
5550 retval
= ERROR_FAIL
;
5553 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5554 fastload
[i
].length
= length
;
5556 image_size
+= length
;
5557 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
5558 (unsigned int)length
,
5559 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
5565 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
5566 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
5567 "in %fs (%0.3f KiB/s)", image_size
,
5568 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
5570 command_print(CMD_CTX
,
5571 "WARNING: image has not been loaded to target!"
5572 "You can issue a 'fast_load' to finish loading.");
5575 image_close(&image
);
5577 if (retval
!= ERROR_OK
)
5583 COMMAND_HANDLER(handle_fast_load_command
)
5586 return ERROR_COMMAND_SYNTAX_ERROR
;
5587 if (fastload
== NULL
) {
5588 LOG_ERROR("No image in memory");
5592 int ms
= timeval_ms();
5594 int retval
= ERROR_OK
;
5595 for (i
= 0; i
< fastload_num
; i
++) {
5596 struct target
*target
= get_current_target(CMD_CTX
);
5597 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
5598 (unsigned int)(fastload
[i
].address
),
5599 (unsigned int)(fastload
[i
].length
));
5600 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5601 if (retval
!= ERROR_OK
)
5603 size
+= fastload
[i
].length
;
5605 if (retval
== ERROR_OK
) {
5606 int after
= timeval_ms();
5607 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5612 static const struct command_registration target_command_handlers
[] = {
5615 .handler
= handle_targets_command
,
5616 .mode
= COMMAND_ANY
,
5617 .help
= "change current default target (one parameter) "
5618 "or prints table of all targets (no parameters)",
5619 .usage
= "[target]",
5623 .mode
= COMMAND_CONFIG
,
5624 .help
= "configure target",
5626 .chain
= target_subcommand_handlers
,
5628 COMMAND_REGISTRATION_DONE
5631 int target_register_commands(struct command_context
*cmd_ctx
)
5633 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5636 static bool target_reset_nag
= true;
5638 bool get_target_reset_nag(void)
5640 return target_reset_nag
;
5643 COMMAND_HANDLER(handle_target_reset_nag
)
5645 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
5646 &target_reset_nag
, "Nag after each reset about options to improve "
5650 COMMAND_HANDLER(handle_ps_command
)
5652 struct target
*target
= get_current_target(CMD_CTX
);
5654 if (target
->state
!= TARGET_HALTED
) {
5655 LOG_INFO("target not halted !!");
5659 if ((target
->rtos
) && (target
->rtos
->type
)
5660 && (target
->rtos
->type
->ps_command
)) {
5661 display
= target
->rtos
->type
->ps_command(target
);
5662 command_print(CMD_CTX
, "%s", display
);
5667 return ERROR_TARGET_FAILURE
;
5671 static void binprint(struct command_context
*cmd_ctx
, const char *text
, const uint8_t *buf
, int size
)
5674 command_print_sameline(cmd_ctx
, "%s", text
);
5675 for (int i
= 0; i
< size
; i
++)
5676 command_print_sameline(cmd_ctx
, " %02x", buf
[i
]);
5677 command_print(cmd_ctx
, " ");
5680 COMMAND_HANDLER(handle_test_mem_access_command
)
5682 struct target
*target
= get_current_target(CMD_CTX
);
5684 int retval
= ERROR_OK
;
5686 if (target
->state
!= TARGET_HALTED
) {
5687 LOG_INFO("target not halted !!");
5692 return ERROR_COMMAND_SYNTAX_ERROR
;
5694 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], test_size
);
5697 size_t num_bytes
= test_size
+ 4;
5699 struct working_area
*wa
= NULL
;
5700 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
5701 if (retval
!= ERROR_OK
) {
5702 LOG_ERROR("Not enough working area");
5706 uint8_t *test_pattern
= malloc(num_bytes
);
5708 for (size_t i
= 0; i
< num_bytes
; i
++)
5709 test_pattern
[i
] = rand();
5711 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
5712 if (retval
!= ERROR_OK
) {
5713 LOG_ERROR("Test pattern write failed");
5717 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
5718 for (int size
= 1; size
<= 4; size
*= 2) {
5719 for (int offset
= 0; offset
< 4; offset
++) {
5720 uint32_t count
= test_size
/ size
;
5721 size_t host_bufsiz
= (count
+ 2) * size
+ host_offset
;
5722 uint8_t *read_ref
= malloc(host_bufsiz
);
5723 uint8_t *read_buf
= malloc(host_bufsiz
);
5725 for (size_t i
= 0; i
< host_bufsiz
; i
++) {
5726 read_ref
[i
] = rand();
5727 read_buf
[i
] = read_ref
[i
];
5729 command_print_sameline(CMD_CTX
,
5730 "Test read %" PRIu32
" x %d @ %d to %saligned buffer: ", count
,
5731 size
, offset
, host_offset
? "un" : "");
5733 struct duration bench
;
5734 duration_start(&bench
);
5736 retval
= target_read_memory(target
, wa
->address
+ offset
, size
, count
,
5737 read_buf
+ size
+ host_offset
);
5739 duration_measure(&bench
);
5741 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
5742 command_print(CMD_CTX
, "Unsupported alignment");
5744 } else if (retval
!= ERROR_OK
) {
5745 command_print(CMD_CTX
, "Memory read failed");
5749 /* replay on host */
5750 memcpy(read_ref
+ size
+ host_offset
, test_pattern
+ offset
, count
* size
);
5753 int result
= memcmp(read_ref
, read_buf
, host_bufsiz
);
5755 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
5756 duration_elapsed(&bench
),
5757 duration_kbps(&bench
, count
* size
));
5759 command_print(CMD_CTX
, "Compare failed");
5760 binprint(CMD_CTX
, "ref:", read_ref
, host_bufsiz
);
5761 binprint(CMD_CTX
, "buf:", read_buf
, host_bufsiz
);
5774 target_free_working_area(target
, wa
);
5777 num_bytes
= test_size
+ 4 + 4 + 4;
5779 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
5780 if (retval
!= ERROR_OK
) {
5781 LOG_ERROR("Not enough working area");
5785 test_pattern
= malloc(num_bytes
);
5787 for (size_t i
= 0; i
< num_bytes
; i
++)
5788 test_pattern
[i
] = rand();
5790 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
5791 for (int size
= 1; size
<= 4; size
*= 2) {
5792 for (int offset
= 0; offset
< 4; offset
++) {
5793 uint32_t count
= test_size
/ size
;
5794 size_t host_bufsiz
= count
* size
+ host_offset
;
5795 uint8_t *read_ref
= malloc(num_bytes
);
5796 uint8_t *read_buf
= malloc(num_bytes
);
5797 uint8_t *write_buf
= malloc(host_bufsiz
);
5799 for (size_t i
= 0; i
< host_bufsiz
; i
++)
5800 write_buf
[i
] = rand();
5801 command_print_sameline(CMD_CTX
,
5802 "Test write %" PRIu32
" x %d @ %d from %saligned buffer: ", count
,
5803 size
, offset
, host_offset
? "un" : "");
5805 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
5806 if (retval
!= ERROR_OK
) {
5807 command_print(CMD_CTX
, "Test pattern write failed");
5811 /* replay on host */
5812 memcpy(read_ref
, test_pattern
, num_bytes
);
5813 memcpy(read_ref
+ size
+ offset
, write_buf
+ host_offset
, count
* size
);
5815 struct duration bench
;
5816 duration_start(&bench
);
5818 retval
= target_write_memory(target
, wa
->address
+ size
+ offset
, size
, count
,
5819 write_buf
+ host_offset
);
5821 duration_measure(&bench
);
5823 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
5824 command_print(CMD_CTX
, "Unsupported alignment");
5826 } else if (retval
!= ERROR_OK
) {
5827 command_print(CMD_CTX
, "Memory write failed");
5832 retval
= target_read_memory(target
, wa
->address
, 1, num_bytes
, read_buf
);
5833 if (retval
!= ERROR_OK
) {
5834 command_print(CMD_CTX
, "Test pattern write failed");
5839 int result
= memcmp(read_ref
, read_buf
, num_bytes
);
5841 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
5842 duration_elapsed(&bench
),
5843 duration_kbps(&bench
, count
* size
));
5845 command_print(CMD_CTX
, "Compare failed");
5846 binprint(CMD_CTX
, "ref:", read_ref
, num_bytes
);
5847 binprint(CMD_CTX
, "buf:", read_buf
, num_bytes
);
5859 target_free_working_area(target
, wa
);
5863 static const struct command_registration target_exec_command_handlers
[] = {
5865 .name
= "fast_load_image",
5866 .handler
= handle_fast_load_image_command
,
5867 .mode
= COMMAND_ANY
,
5868 .help
= "Load image into server memory for later use by "
5869 "fast_load; primarily for profiling",
5870 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5871 "[min_address [max_length]]",
5874 .name
= "fast_load",
5875 .handler
= handle_fast_load_command
,
5876 .mode
= COMMAND_EXEC
,
5877 .help
= "loads active fast load image to current target "
5878 "- mainly for profiling purposes",
5883 .handler
= handle_profile_command
,
5884 .mode
= COMMAND_EXEC
,
5885 .usage
= "seconds filename [start end]",
5886 .help
= "profiling samples the CPU PC",
5888 /** @todo don't register virt2phys() unless target supports it */
5890 .name
= "virt2phys",
5891 .handler
= handle_virt2phys_command
,
5892 .mode
= COMMAND_ANY
,
5893 .help
= "translate a virtual address into a physical address",
5894 .usage
= "virtual_address",
5898 .handler
= handle_reg_command
,
5899 .mode
= COMMAND_EXEC
,
5900 .help
= "display (reread from target with \"force\") or set a register; "
5901 "with no arguments, displays all registers and their values",
5902 .usage
= "[(register_number|register_name) [(value|'force')]]",
5906 .handler
= handle_poll_command
,
5907 .mode
= COMMAND_EXEC
,
5908 .help
= "poll target state; or reconfigure background polling",
5909 .usage
= "['on'|'off']",
5912 .name
= "wait_halt",
5913 .handler
= handle_wait_halt_command
,
5914 .mode
= COMMAND_EXEC
,
5915 .help
= "wait up to the specified number of milliseconds "
5916 "(default 5000) for a previously requested halt",
5917 .usage
= "[milliseconds]",
5921 .handler
= handle_halt_command
,
5922 .mode
= COMMAND_EXEC
,
5923 .help
= "request target to halt, then wait up to the specified"
5924 "number of milliseconds (default 5000) for it to complete",
5925 .usage
= "[milliseconds]",
5929 .handler
= handle_resume_command
,
5930 .mode
= COMMAND_EXEC
,
5931 .help
= "resume target execution from current PC or address",
5932 .usage
= "[address]",
5936 .handler
= handle_reset_command
,
5937 .mode
= COMMAND_EXEC
,
5938 .usage
= "[run|halt|init]",
5939 .help
= "Reset all targets into the specified mode."
5940 "Default reset mode is run, if not given.",
5943 .name
= "soft_reset_halt",
5944 .handler
= handle_soft_reset_halt_command
,
5945 .mode
= COMMAND_EXEC
,
5947 .help
= "halt the target and do a soft reset",
5951 .handler
= handle_step_command
,
5952 .mode
= COMMAND_EXEC
,
5953 .help
= "step one instruction from current PC or address",
5954 .usage
= "[address]",
5958 .handler
= handle_md_command
,
5959 .mode
= COMMAND_EXEC
,
5960 .help
= "display memory words",
5961 .usage
= "['phys'] address [count]",
5965 .handler
= handle_md_command
,
5966 .mode
= COMMAND_EXEC
,
5967 .help
= "display memory half-words",
5968 .usage
= "['phys'] address [count]",
5972 .handler
= handle_md_command
,
5973 .mode
= COMMAND_EXEC
,
5974 .help
= "display memory bytes",
5975 .usage
= "['phys'] address [count]",
5979 .handler
= handle_mw_command
,
5980 .mode
= COMMAND_EXEC
,
5981 .help
= "write memory word",
5982 .usage
= "['phys'] address value [count]",
5986 .handler
= handle_mw_command
,
5987 .mode
= COMMAND_EXEC
,
5988 .help
= "write memory half-word",
5989 .usage
= "['phys'] address value [count]",
5993 .handler
= handle_mw_command
,
5994 .mode
= COMMAND_EXEC
,
5995 .help
= "write memory byte",
5996 .usage
= "['phys'] address value [count]",
6000 .handler
= handle_bp_command
,
6001 .mode
= COMMAND_EXEC
,
6002 .help
= "list or set hardware or software breakpoint",
6003 .usage
= "<address> [<asid>]<length> ['hw'|'hw_ctx']",
6007 .handler
= handle_rbp_command
,
6008 .mode
= COMMAND_EXEC
,
6009 .help
= "remove breakpoint",
6014 .handler
= handle_wp_command
,
6015 .mode
= COMMAND_EXEC
,
6016 .help
= "list (no params) or create watchpoints",
6017 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
6021 .handler
= handle_rwp_command
,
6022 .mode
= COMMAND_EXEC
,
6023 .help
= "remove watchpoint",
6027 .name
= "load_image",
6028 .handler
= handle_load_image_command
,
6029 .mode
= COMMAND_EXEC
,
6030 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6031 "[min_address] [max_length]",
6034 .name
= "dump_image",
6035 .handler
= handle_dump_image_command
,
6036 .mode
= COMMAND_EXEC
,
6037 .usage
= "filename address size",
6040 .name
= "verify_image",
6041 .handler
= handle_verify_image_command
,
6042 .mode
= COMMAND_EXEC
,
6043 .usage
= "filename [offset [type]]",
6046 .name
= "test_image",
6047 .handler
= handle_test_image_command
,
6048 .mode
= COMMAND_EXEC
,
6049 .usage
= "filename [offset [type]]",
6052 .name
= "mem2array",
6053 .mode
= COMMAND_EXEC
,
6054 .jim_handler
= jim_mem2array
,
6055 .help
= "read 8/16/32 bit memory and return as a TCL array "
6056 "for script processing",
6057 .usage
= "arrayname bitwidth address count",
6060 .name
= "array2mem",
6061 .mode
= COMMAND_EXEC
,
6062 .jim_handler
= jim_array2mem
,
6063 .help
= "convert a TCL array to memory locations "
6064 "and write the 8/16/32 bit values",
6065 .usage
= "arrayname bitwidth address count",
6068 .name
= "reset_nag",
6069 .handler
= handle_target_reset_nag
,
6070 .mode
= COMMAND_ANY
,
6071 .help
= "Nag after each reset about options that could have been "
6072 "enabled to improve performance. ",
6073 .usage
= "['enable'|'disable']",
6077 .handler
= handle_ps_command
,
6078 .mode
= COMMAND_EXEC
,
6079 .help
= "list all tasks ",
6083 .name
= "test_mem_access",
6084 .handler
= handle_test_mem_access_command
,
6085 .mode
= COMMAND_EXEC
,
6086 .help
= "Test the target's memory access functions",
6090 COMMAND_REGISTRATION_DONE
6092 static int target_register_user_commands(struct command_context
*cmd_ctx
)
6094 int retval
= ERROR_OK
;
6095 retval
= target_request_register_commands(cmd_ctx
);
6096 if (retval
!= ERROR_OK
)
6099 retval
= trace_register_commands(cmd_ctx
);
6100 if (retval
!= ERROR_OK
)
6104 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);