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
;
107 extern struct target_type quark_d20xx_target
;
109 static struct target_type
*target_types
[] = {
141 struct target
*all_targets
;
142 static struct target_event_callback
*target_event_callbacks
;
143 static struct target_timer_callback
*target_timer_callbacks
;
144 LIST_HEAD(target_reset_callback_list
);
145 LIST_HEAD(target_trace_callback_list
);
146 static const int polling_interval
= 100;
148 static const Jim_Nvp nvp_assert
[] = {
149 { .name
= "assert", NVP_ASSERT
},
150 { .name
= "deassert", NVP_DEASSERT
},
151 { .name
= "T", NVP_ASSERT
},
152 { .name
= "F", NVP_DEASSERT
},
153 { .name
= "t", NVP_ASSERT
},
154 { .name
= "f", NVP_DEASSERT
},
155 { .name
= NULL
, .value
= -1 }
158 static const Jim_Nvp nvp_error_target
[] = {
159 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
160 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
161 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
162 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
163 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
164 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
165 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
166 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
167 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
168 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
169 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
170 { .value
= -1, .name
= NULL
}
173 static const char *target_strerror_safe(int err
)
177 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
184 static const Jim_Nvp nvp_target_event
[] = {
186 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
187 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
188 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
189 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
190 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
192 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
193 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
195 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
196 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
197 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
198 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
199 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
200 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
201 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
202 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
203 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
204 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
205 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
206 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
208 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
209 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
211 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
212 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
214 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
215 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
217 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
218 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
220 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
221 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
223 { .value
= TARGET_EVENT_TRACE_CONFIG
, .name
= "trace-config" },
225 { .name
= NULL
, .value
= -1 }
228 static const Jim_Nvp nvp_target_state
[] = {
229 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
230 { .name
= "running", .value
= TARGET_RUNNING
},
231 { .name
= "halted", .value
= TARGET_HALTED
},
232 { .name
= "reset", .value
= TARGET_RESET
},
233 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
234 { .name
= NULL
, .value
= -1 },
237 static const Jim_Nvp nvp_target_debug_reason
[] = {
238 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
239 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
240 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
241 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
242 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
243 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
244 { .name
= "program-exit" , .value
= DBG_REASON_EXIT
},
245 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
246 { .name
= NULL
, .value
= -1 },
249 static const Jim_Nvp nvp_target_endian
[] = {
250 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
251 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
252 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
253 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
254 { .name
= NULL
, .value
= -1 },
257 static const Jim_Nvp nvp_reset_modes
[] = {
258 { .name
= "unknown", .value
= RESET_UNKNOWN
},
259 { .name
= "run" , .value
= RESET_RUN
},
260 { .name
= "halt" , .value
= RESET_HALT
},
261 { .name
= "init" , .value
= RESET_INIT
},
262 { .name
= NULL
, .value
= -1 },
265 const char *debug_reason_name(struct target
*t
)
269 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
270 t
->debug_reason
)->name
;
272 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
273 cp
= "(*BUG*unknown*BUG*)";
278 const char *target_state_name(struct target
*t
)
281 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
283 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
284 cp
= "(*BUG*unknown*BUG*)";
289 const char *target_event_name(enum target_event event
)
292 cp
= Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
;
294 LOG_ERROR("Invalid target event: %d", (int)(event
));
295 cp
= "(*BUG*unknown*BUG*)";
300 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
303 cp
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
;
305 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
306 cp
= "(*BUG*unknown*BUG*)";
311 /* determine the number of the new target */
312 static int new_target_number(void)
317 /* number is 0 based */
321 if (x
< t
->target_number
)
322 x
= t
->target_number
;
328 /* read a uint64_t from a buffer in target memory endianness */
329 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
331 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
332 return le_to_h_u64(buffer
);
334 return be_to_h_u64(buffer
);
337 /* read a uint32_t from a buffer in target memory endianness */
338 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
340 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
341 return le_to_h_u32(buffer
);
343 return be_to_h_u32(buffer
);
346 /* read a uint24_t from a buffer in target memory endianness */
347 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
349 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
350 return le_to_h_u24(buffer
);
352 return be_to_h_u24(buffer
);
355 /* read a uint16_t from a buffer in target memory endianness */
356 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
358 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
359 return le_to_h_u16(buffer
);
361 return be_to_h_u16(buffer
);
364 /* read a uint8_t from a buffer in target memory endianness */
365 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
367 return *buffer
& 0x0ff;
370 /* write a uint64_t to a buffer in target memory endianness */
371 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
373 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
374 h_u64_to_le(buffer
, value
);
376 h_u64_to_be(buffer
, value
);
379 /* write a uint32_t to a buffer in target memory endianness */
380 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
382 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
383 h_u32_to_le(buffer
, value
);
385 h_u32_to_be(buffer
, value
);
388 /* write a uint24_t to a buffer in target memory endianness */
389 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
391 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
392 h_u24_to_le(buffer
, value
);
394 h_u24_to_be(buffer
, value
);
397 /* write a uint16_t to a buffer in target memory endianness */
398 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
400 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
401 h_u16_to_le(buffer
, value
);
403 h_u16_to_be(buffer
, value
);
406 /* write a uint8_t to a buffer in target memory endianness */
407 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
412 /* write a uint64_t array to a buffer in target memory endianness */
413 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
416 for (i
= 0; i
< count
; i
++)
417 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
420 /* write a uint32_t array to a buffer in target memory endianness */
421 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
424 for (i
= 0; i
< count
; i
++)
425 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
428 /* write a uint16_t array to a buffer in target memory endianness */
429 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
432 for (i
= 0; i
< count
; i
++)
433 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
436 /* write a uint64_t array to a buffer in target memory endianness */
437 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
440 for (i
= 0; i
< count
; i
++)
441 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
444 /* write a uint32_t array to a buffer in target memory endianness */
445 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
448 for (i
= 0; i
< count
; i
++)
449 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
452 /* write a uint16_t array to a buffer in target memory endianness */
453 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
456 for (i
= 0; i
< count
; i
++)
457 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
460 /* return a pointer to a configured target; id is name or number */
461 struct target
*get_target(const char *id
)
463 struct target
*target
;
465 /* try as tcltarget name */
466 for (target
= all_targets
; target
; target
= target
->next
) {
467 if (target_name(target
) == NULL
)
469 if (strcmp(id
, target_name(target
)) == 0)
473 /* It's OK to remove this fallback sometime after August 2010 or so */
475 /* no match, try as number */
477 if (parse_uint(id
, &num
) != ERROR_OK
)
480 for (target
= all_targets
; target
; target
= target
->next
) {
481 if (target
->target_number
== (int)num
) {
482 LOG_WARNING("use '%s' as target identifier, not '%u'",
483 target_name(target
), num
);
491 /* returns a pointer to the n-th configured target */
492 struct target
*get_target_by_num(int num
)
494 struct target
*target
= all_targets
;
497 if (target
->target_number
== num
)
499 target
= target
->next
;
505 struct target
*get_current_target(struct command_context
*cmd_ctx
)
507 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
509 if (target
== NULL
) {
510 LOG_ERROR("BUG: current_target out of bounds");
517 int target_poll(struct target
*target
)
521 /* We can't poll until after examine */
522 if (!target_was_examined(target
)) {
523 /* Fail silently lest we pollute the log */
527 retval
= target
->type
->poll(target
);
528 if (retval
!= ERROR_OK
)
531 if (target
->halt_issued
) {
532 if (target
->state
== TARGET_HALTED
)
533 target
->halt_issued
= false;
535 long long t
= timeval_ms() - target
->halt_issued_time
;
536 if (t
> DEFAULT_HALT_TIMEOUT
) {
537 target
->halt_issued
= false;
538 LOG_INFO("Halt timed out, wake up GDB.");
539 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
547 int target_halt(struct target
*target
)
550 /* We can't poll until after examine */
551 if (!target_was_examined(target
)) {
552 LOG_ERROR("Target not examined yet");
556 retval
= target
->type
->halt(target
);
557 if (retval
!= ERROR_OK
)
560 target
->halt_issued
= true;
561 target
->halt_issued_time
= timeval_ms();
567 * Make the target (re)start executing using its saved execution
568 * context (possibly with some modifications).
570 * @param target Which target should start executing.
571 * @param current True to use the target's saved program counter instead
572 * of the address parameter
573 * @param address Optionally used as the program counter.
574 * @param handle_breakpoints True iff breakpoints at the resumption PC
575 * should be skipped. (For example, maybe execution was stopped by
576 * such a breakpoint, in which case it would be counterprodutive to
578 * @param debug_execution False if all working areas allocated by OpenOCD
579 * should be released and/or restored to their original contents.
580 * (This would for example be true to run some downloaded "helper"
581 * algorithm code, which resides in one such working buffer and uses
582 * another for data storage.)
584 * @todo Resolve the ambiguity about what the "debug_execution" flag
585 * signifies. For example, Target implementations don't agree on how
586 * it relates to invalidation of the register cache, or to whether
587 * breakpoints and watchpoints should be enabled. (It would seem wrong
588 * to enable breakpoints when running downloaded "helper" algorithms
589 * (debug_execution true), since the breakpoints would be set to match
590 * target firmware being debugged, not the helper algorithm.... and
591 * enabling them could cause such helpers to malfunction (for example,
592 * by overwriting data with a breakpoint instruction. On the other
593 * hand the infrastructure for running such helpers might use this
594 * procedure but rely on hardware breakpoint to detect termination.)
596 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
600 /* We can't poll until after examine */
601 if (!target_was_examined(target
)) {
602 LOG_ERROR("Target not examined yet");
606 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
608 /* note that resume *must* be asynchronous. The CPU can halt before
609 * we poll. The CPU can even halt at the current PC as a result of
610 * a software breakpoint being inserted by (a bug?) the application.
612 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
613 if (retval
!= ERROR_OK
)
616 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
621 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
626 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
627 if (n
->name
== NULL
) {
628 LOG_ERROR("invalid reset mode");
632 struct target
*target
;
633 for (target
= all_targets
; target
; target
= target
->next
)
634 target_call_reset_callbacks(target
, reset_mode
);
636 /* disable polling during reset to make reset event scripts
637 * more predictable, i.e. dr/irscan & pathmove in events will
638 * not have JTAG operations injected into the middle of a sequence.
640 bool save_poll
= jtag_poll_get_enabled();
642 jtag_poll_set_enabled(false);
644 sprintf(buf
, "ocd_process_reset %s", n
->name
);
645 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
647 jtag_poll_set_enabled(save_poll
);
649 if (retval
!= JIM_OK
) {
650 Jim_MakeErrorMessage(cmd_ctx
->interp
);
651 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
655 /* We want any events to be processed before the prompt */
656 retval
= target_call_timer_callbacks_now();
658 for (target
= all_targets
; target
; target
= target
->next
) {
659 target
->type
->check_reset(target
);
660 target
->running_alg
= false;
666 static int identity_virt2phys(struct target
*target
,
667 uint32_t virtual, uint32_t *physical
)
673 static int no_mmu(struct target
*target
, int *enabled
)
679 static int default_examine(struct target
*target
)
681 target_set_examined(target
);
685 /* no check by default */
686 static int default_check_reset(struct target
*target
)
691 int target_examine_one(struct target
*target
)
693 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
695 int retval
= target
->type
->examine(target
);
696 if (retval
!= ERROR_OK
)
699 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
704 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
706 struct target
*target
= priv
;
708 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
711 jtag_unregister_event_callback(jtag_enable_callback
, target
);
713 return target_examine_one(target
);
716 /* Targets that correctly implement init + examine, i.e.
717 * no communication with target during init:
721 int target_examine(void)
723 int retval
= ERROR_OK
;
724 struct target
*target
;
726 for (target
= all_targets
; target
; target
= target
->next
) {
727 /* defer examination, but don't skip it */
728 if (!target
->tap
->enabled
) {
729 jtag_register_event_callback(jtag_enable_callback
,
734 retval
= target_examine_one(target
);
735 if (retval
!= ERROR_OK
)
741 const char *target_type_name(struct target
*target
)
743 return target
->type
->name
;
746 static int target_soft_reset_halt(struct target
*target
)
748 if (!target_was_examined(target
)) {
749 LOG_ERROR("Target not examined yet");
752 if (!target
->type
->soft_reset_halt
) {
753 LOG_ERROR("Target %s does not support soft_reset_halt",
754 target_name(target
));
757 return target
->type
->soft_reset_halt(target
);
761 * Downloads a target-specific native code algorithm to the target,
762 * and executes it. * Note that some targets may need to set up, enable,
763 * and tear down a breakpoint (hard or * soft) to detect algorithm
764 * termination, while others may support lower overhead schemes where
765 * soft breakpoints embedded in the algorithm automatically terminate the
768 * @param target used to run the algorithm
769 * @param arch_info target-specific description of the algorithm.
771 int target_run_algorithm(struct target
*target
,
772 int num_mem_params
, struct mem_param
*mem_params
,
773 int num_reg_params
, struct reg_param
*reg_param
,
774 uint32_t entry_point
, uint32_t exit_point
,
775 int timeout_ms
, void *arch_info
)
777 int retval
= ERROR_FAIL
;
779 if (!target_was_examined(target
)) {
780 LOG_ERROR("Target not examined yet");
783 if (!target
->type
->run_algorithm
) {
784 LOG_ERROR("Target type '%s' does not support %s",
785 target_type_name(target
), __func__
);
789 target
->running_alg
= true;
790 retval
= target
->type
->run_algorithm(target
,
791 num_mem_params
, mem_params
,
792 num_reg_params
, reg_param
,
793 entry_point
, exit_point
, timeout_ms
, arch_info
);
794 target
->running_alg
= false;
801 * Downloads a target-specific native code algorithm to the target,
802 * executes and leaves it running.
804 * @param target used to run the algorithm
805 * @param arch_info target-specific description of the algorithm.
807 int target_start_algorithm(struct target
*target
,
808 int num_mem_params
, struct mem_param
*mem_params
,
809 int num_reg_params
, struct reg_param
*reg_params
,
810 uint32_t entry_point
, uint32_t exit_point
,
813 int retval
= ERROR_FAIL
;
815 if (!target_was_examined(target
)) {
816 LOG_ERROR("Target not examined yet");
819 if (!target
->type
->start_algorithm
) {
820 LOG_ERROR("Target type '%s' does not support %s",
821 target_type_name(target
), __func__
);
824 if (target
->running_alg
) {
825 LOG_ERROR("Target is already running an algorithm");
829 target
->running_alg
= true;
830 retval
= target
->type
->start_algorithm(target
,
831 num_mem_params
, mem_params
,
832 num_reg_params
, reg_params
,
833 entry_point
, exit_point
, arch_info
);
840 * Waits for an algorithm started with target_start_algorithm() to complete.
842 * @param target used to run the algorithm
843 * @param arch_info target-specific description of the algorithm.
845 int target_wait_algorithm(struct target
*target
,
846 int num_mem_params
, struct mem_param
*mem_params
,
847 int num_reg_params
, struct reg_param
*reg_params
,
848 uint32_t exit_point
, int timeout_ms
,
851 int retval
= ERROR_FAIL
;
853 if (!target
->type
->wait_algorithm
) {
854 LOG_ERROR("Target type '%s' does not support %s",
855 target_type_name(target
), __func__
);
858 if (!target
->running_alg
) {
859 LOG_ERROR("Target is not running an algorithm");
863 retval
= target
->type
->wait_algorithm(target
,
864 num_mem_params
, mem_params
,
865 num_reg_params
, reg_params
,
866 exit_point
, timeout_ms
, arch_info
);
867 if (retval
!= ERROR_TARGET_TIMEOUT
)
868 target
->running_alg
= false;
875 * Executes a target-specific native code algorithm in the target.
876 * It differs from target_run_algorithm in that the algorithm is asynchronous.
877 * Because of this it requires an compliant algorithm:
878 * see contrib/loaders/flash/stm32f1x.S for example.
880 * @param target used to run the algorithm
883 int target_run_flash_async_algorithm(struct target
*target
,
884 const uint8_t *buffer
, uint32_t count
, int block_size
,
885 int num_mem_params
, struct mem_param
*mem_params
,
886 int num_reg_params
, struct reg_param
*reg_params
,
887 uint32_t buffer_start
, uint32_t buffer_size
,
888 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
893 const uint8_t *buffer_orig
= buffer
;
895 /* Set up working area. First word is write pointer, second word is read pointer,
896 * rest is fifo data area. */
897 uint32_t wp_addr
= buffer_start
;
898 uint32_t rp_addr
= buffer_start
+ 4;
899 uint32_t fifo_start_addr
= buffer_start
+ 8;
900 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
902 uint32_t wp
= fifo_start_addr
;
903 uint32_t rp
= fifo_start_addr
;
905 /* validate block_size is 2^n */
906 assert(!block_size
|| !(block_size
& (block_size
- 1)));
908 retval
= target_write_u32(target
, wp_addr
, wp
);
909 if (retval
!= ERROR_OK
)
911 retval
= target_write_u32(target
, rp_addr
, rp
);
912 if (retval
!= ERROR_OK
)
915 /* Start up algorithm on target and let it idle while writing the first chunk */
916 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
917 num_reg_params
, reg_params
,
922 if (retval
!= ERROR_OK
) {
923 LOG_ERROR("error starting target flash write algorithm");
929 retval
= target_read_u32(target
, rp_addr
, &rp
);
930 if (retval
!= ERROR_OK
) {
931 LOG_ERROR("failed to get read pointer");
935 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
936 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
939 LOG_ERROR("flash write algorithm aborted by target");
940 retval
= ERROR_FLASH_OPERATION_FAILED
;
944 if (((rp
- fifo_start_addr
) & (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
945 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
949 /* Count the number of bytes available in the fifo without
950 * crossing the wrap around. Make sure to not fill it completely,
951 * because that would make wp == rp and that's the empty condition. */
952 uint32_t thisrun_bytes
;
954 thisrun_bytes
= rp
- wp
- block_size
;
955 else if (rp
> fifo_start_addr
)
956 thisrun_bytes
= fifo_end_addr
- wp
;
958 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
960 if (thisrun_bytes
== 0) {
961 /* Throttle polling a bit if transfer is (much) faster than flash
962 * programming. The exact delay shouldn't matter as long as it's
963 * less than buffer size / flash speed. This is very unlikely to
964 * run when using high latency connections such as USB. */
967 /* to stop an infinite loop on some targets check and increment a timeout
968 * this issue was observed on a stellaris using the new ICDI interface */
969 if (timeout
++ >= 500) {
970 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
971 return ERROR_FLASH_OPERATION_FAILED
;
976 /* reset our timeout */
979 /* Limit to the amount of data we actually want to write */
980 if (thisrun_bytes
> count
* block_size
)
981 thisrun_bytes
= count
* block_size
;
983 /* Write data to fifo */
984 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
985 if (retval
!= ERROR_OK
)
988 /* Update counters and wrap write pointer */
989 buffer
+= thisrun_bytes
;
990 count
-= thisrun_bytes
/ block_size
;
992 if (wp
>= fifo_end_addr
)
993 wp
= fifo_start_addr
;
995 /* Store updated write pointer to target */
996 retval
= target_write_u32(target
, wp_addr
, wp
);
997 if (retval
!= ERROR_OK
)
1001 if (retval
!= ERROR_OK
) {
1002 /* abort flash write algorithm on target */
1003 target_write_u32(target
, wp_addr
, 0);
1006 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1007 num_reg_params
, reg_params
,
1012 if (retval2
!= ERROR_OK
) {
1013 LOG_ERROR("error waiting for target flash write algorithm");
1020 int target_read_memory(struct target
*target
,
1021 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1023 if (!target_was_examined(target
)) {
1024 LOG_ERROR("Target not examined yet");
1027 if (!target
->type
->read_memory
) {
1028 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1031 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1034 int target_read_phys_memory(struct target
*target
,
1035 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1037 if (!target_was_examined(target
)) {
1038 LOG_ERROR("Target not examined yet");
1041 if (!target
->type
->read_phys_memory
) {
1042 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1045 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1048 int target_write_memory(struct target
*target
,
1049 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1051 if (!target_was_examined(target
)) {
1052 LOG_ERROR("Target not examined yet");
1055 if (!target
->type
->write_memory
) {
1056 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1059 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1062 int target_write_phys_memory(struct target
*target
,
1063 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1065 if (!target_was_examined(target
)) {
1066 LOG_ERROR("Target not examined yet");
1069 if (!target
->type
->write_phys_memory
) {
1070 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1073 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1076 int target_add_breakpoint(struct target
*target
,
1077 struct breakpoint
*breakpoint
)
1079 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1080 LOG_WARNING("target %s is not halted", target_name(target
));
1081 return ERROR_TARGET_NOT_HALTED
;
1083 return target
->type
->add_breakpoint(target
, breakpoint
);
1086 int target_add_context_breakpoint(struct target
*target
,
1087 struct breakpoint
*breakpoint
)
1089 if (target
->state
!= TARGET_HALTED
) {
1090 LOG_WARNING("target %s is not halted", target_name(target
));
1091 return ERROR_TARGET_NOT_HALTED
;
1093 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1096 int target_add_hybrid_breakpoint(struct target
*target
,
1097 struct breakpoint
*breakpoint
)
1099 if (target
->state
!= TARGET_HALTED
) {
1100 LOG_WARNING("target %s is not halted", target_name(target
));
1101 return ERROR_TARGET_NOT_HALTED
;
1103 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1106 int target_remove_breakpoint(struct target
*target
,
1107 struct breakpoint
*breakpoint
)
1109 return target
->type
->remove_breakpoint(target
, breakpoint
);
1112 int target_add_watchpoint(struct target
*target
,
1113 struct watchpoint
*watchpoint
)
1115 if (target
->state
!= TARGET_HALTED
) {
1116 LOG_WARNING("target %s is not halted", target_name(target
));
1117 return ERROR_TARGET_NOT_HALTED
;
1119 return target
->type
->add_watchpoint(target
, watchpoint
);
1121 int target_remove_watchpoint(struct target
*target
,
1122 struct watchpoint
*watchpoint
)
1124 return target
->type
->remove_watchpoint(target
, watchpoint
);
1126 int target_hit_watchpoint(struct target
*target
,
1127 struct watchpoint
**hit_watchpoint
)
1129 if (target
->state
!= TARGET_HALTED
) {
1130 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1131 return ERROR_TARGET_NOT_HALTED
;
1134 if (target
->type
->hit_watchpoint
== NULL
) {
1135 /* For backward compatible, if hit_watchpoint is not implemented,
1136 * return ERROR_FAIL such that gdb_server will not take the nonsense
1141 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1144 int target_get_gdb_reg_list(struct target
*target
,
1145 struct reg
**reg_list
[], int *reg_list_size
,
1146 enum target_register_class reg_class
)
1148 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1150 int target_step(struct target
*target
,
1151 int current
, uint32_t address
, int handle_breakpoints
)
1153 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1156 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1158 if (target
->state
!= TARGET_HALTED
) {
1159 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1160 return ERROR_TARGET_NOT_HALTED
;
1162 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1165 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1167 if (target
->state
!= TARGET_HALTED
) {
1168 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1169 return ERROR_TARGET_NOT_HALTED
;
1171 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1174 int target_profiling(struct target
*target
, uint32_t *samples
,
1175 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1177 if (target
->state
!= TARGET_HALTED
) {
1178 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1179 return ERROR_TARGET_NOT_HALTED
;
1181 return target
->type
->profiling(target
, samples
, max_num_samples
,
1182 num_samples
, seconds
);
1186 * Reset the @c examined flag for the given target.
1187 * Pure paranoia -- targets are zeroed on allocation.
1189 static void target_reset_examined(struct target
*target
)
1191 target
->examined
= false;
1194 static int handle_target(void *priv
);
1196 static int target_init_one(struct command_context
*cmd_ctx
,
1197 struct target
*target
)
1199 target_reset_examined(target
);
1201 struct target_type
*type
= target
->type
;
1202 if (type
->examine
== NULL
)
1203 type
->examine
= default_examine
;
1205 if (type
->check_reset
== NULL
)
1206 type
->check_reset
= default_check_reset
;
1208 assert(type
->init_target
!= NULL
);
1210 int retval
= type
->init_target(cmd_ctx
, target
);
1211 if (ERROR_OK
!= retval
) {
1212 LOG_ERROR("target '%s' init failed", target_name(target
));
1216 /* Sanity-check MMU support ... stub in what we must, to help
1217 * implement it in stages, but warn if we need to do so.
1220 if (type
->virt2phys
== NULL
) {
1221 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1222 type
->virt2phys
= identity_virt2phys
;
1225 /* Make sure no-MMU targets all behave the same: make no
1226 * distinction between physical and virtual addresses, and
1227 * ensure that virt2phys() is always an identity mapping.
1229 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1230 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1233 type
->write_phys_memory
= type
->write_memory
;
1234 type
->read_phys_memory
= type
->read_memory
;
1235 type
->virt2phys
= identity_virt2phys
;
1238 if (target
->type
->read_buffer
== NULL
)
1239 target
->type
->read_buffer
= target_read_buffer_default
;
1241 if (target
->type
->write_buffer
== NULL
)
1242 target
->type
->write_buffer
= target_write_buffer_default
;
1244 if (target
->type
->get_gdb_fileio_info
== NULL
)
1245 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1247 if (target
->type
->gdb_fileio_end
== NULL
)
1248 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1250 if (target
->type
->profiling
== NULL
)
1251 target
->type
->profiling
= target_profiling_default
;
1256 static int target_init(struct command_context
*cmd_ctx
)
1258 struct target
*target
;
1261 for (target
= all_targets
; target
; target
= target
->next
) {
1262 retval
= target_init_one(cmd_ctx
, target
);
1263 if (ERROR_OK
!= retval
)
1270 retval
= target_register_user_commands(cmd_ctx
);
1271 if (ERROR_OK
!= retval
)
1274 retval
= target_register_timer_callback(&handle_target
,
1275 polling_interval
, 1, cmd_ctx
->interp
);
1276 if (ERROR_OK
!= retval
)
1282 COMMAND_HANDLER(handle_target_init_command
)
1287 return ERROR_COMMAND_SYNTAX_ERROR
;
1289 static bool target_initialized
;
1290 if (target_initialized
) {
1291 LOG_INFO("'target init' has already been called");
1294 target_initialized
= true;
1296 retval
= command_run_line(CMD_CTX
, "init_targets");
1297 if (ERROR_OK
!= retval
)
1300 retval
= command_run_line(CMD_CTX
, "init_target_events");
1301 if (ERROR_OK
!= retval
)
1304 retval
= command_run_line(CMD_CTX
, "init_board");
1305 if (ERROR_OK
!= retval
)
1308 LOG_DEBUG("Initializing targets...");
1309 return target_init(CMD_CTX
);
1312 int target_register_event_callback(int (*callback
)(struct target
*target
,
1313 enum target_event event
, void *priv
), void *priv
)
1315 struct target_event_callback
**callbacks_p
= &target_event_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_event_callback
));
1327 (*callbacks_p
)->callback
= callback
;
1328 (*callbacks_p
)->priv
= priv
;
1329 (*callbacks_p
)->next
= NULL
;
1334 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1335 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1337 struct target_reset_callback
*entry
;
1339 if (callback
== NULL
)
1340 return ERROR_COMMAND_SYNTAX_ERROR
;
1342 entry
= malloc(sizeof(struct target_reset_callback
));
1343 if (entry
== NULL
) {
1344 LOG_ERROR("error allocating buffer for reset callback entry");
1345 return ERROR_COMMAND_SYNTAX_ERROR
;
1348 entry
->callback
= callback
;
1350 list_add(&entry
->list
, &target_reset_callback_list
);
1356 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1357 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1359 struct target_trace_callback
*entry
;
1361 if (callback
== NULL
)
1362 return ERROR_COMMAND_SYNTAX_ERROR
;
1364 entry
= malloc(sizeof(struct target_trace_callback
));
1365 if (entry
== NULL
) {
1366 LOG_ERROR("error allocating buffer for trace callback entry");
1367 return ERROR_COMMAND_SYNTAX_ERROR
;
1370 entry
->callback
= callback
;
1372 list_add(&entry
->list
, &target_trace_callback_list
);
1378 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1380 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1383 if (callback
== NULL
)
1384 return ERROR_COMMAND_SYNTAX_ERROR
;
1387 while ((*callbacks_p
)->next
)
1388 callbacks_p
= &((*callbacks_p
)->next
);
1389 callbacks_p
= &((*callbacks_p
)->next
);
1392 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1393 (*callbacks_p
)->callback
= callback
;
1394 (*callbacks_p
)->periodic
= periodic
;
1395 (*callbacks_p
)->time_ms
= time_ms
;
1396 (*callbacks_p
)->removed
= false;
1398 gettimeofday(&now
, NULL
);
1399 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
1400 time_ms
-= (time_ms
% 1000);
1401 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
1402 if ((*callbacks_p
)->when
.tv_usec
> 1000000) {
1403 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
1404 (*callbacks_p
)->when
.tv_sec
+= 1;
1407 (*callbacks_p
)->priv
= priv
;
1408 (*callbacks_p
)->next
= NULL
;
1413 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1414 enum target_event event
, void *priv
), void *priv
)
1416 struct target_event_callback
**p
= &target_event_callbacks
;
1417 struct target_event_callback
*c
= target_event_callbacks
;
1419 if (callback
== NULL
)
1420 return ERROR_COMMAND_SYNTAX_ERROR
;
1423 struct target_event_callback
*next
= c
->next
;
1424 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1436 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1437 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1439 struct target_reset_callback
*entry
;
1441 if (callback
== NULL
)
1442 return ERROR_COMMAND_SYNTAX_ERROR
;
1444 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1445 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1446 list_del(&entry
->list
);
1455 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1456 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1458 struct target_trace_callback
*entry
;
1460 if (callback
== NULL
)
1461 return ERROR_COMMAND_SYNTAX_ERROR
;
1463 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1464 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1465 list_del(&entry
->list
);
1474 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1476 if (callback
== NULL
)
1477 return ERROR_COMMAND_SYNTAX_ERROR
;
1479 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1481 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1490 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1492 struct target_event_callback
*callback
= target_event_callbacks
;
1493 struct target_event_callback
*next_callback
;
1495 if (event
== TARGET_EVENT_HALTED
) {
1496 /* execute early halted first */
1497 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1500 LOG_DEBUG("target event %i (%s)", event
,
1501 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1503 target_handle_event(target
, event
);
1506 next_callback
= callback
->next
;
1507 callback
->callback(target
, event
, callback
->priv
);
1508 callback
= next_callback
;
1514 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1516 struct target_reset_callback
*callback
;
1518 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1519 Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1521 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1522 callback
->callback(target
, reset_mode
, callback
->priv
);
1527 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1529 struct target_trace_callback
*callback
;
1531 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1532 callback
->callback(target
, len
, data
, callback
->priv
);
1537 static int target_timer_callback_periodic_restart(
1538 struct target_timer_callback
*cb
, struct timeval
*now
)
1540 int time_ms
= cb
->time_ms
;
1541 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1542 time_ms
-= (time_ms
% 1000);
1543 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1544 if (cb
->when
.tv_usec
> 1000000) {
1545 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1546 cb
->when
.tv_sec
+= 1;
1551 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1552 struct timeval
*now
)
1554 cb
->callback(cb
->priv
);
1557 return target_timer_callback_periodic_restart(cb
, now
);
1559 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1562 static int target_call_timer_callbacks_check_time(int checktime
)
1564 static bool callback_processing
;
1566 /* Do not allow nesting */
1567 if (callback_processing
)
1570 callback_processing
= true;
1575 gettimeofday(&now
, NULL
);
1577 /* Store an address of the place containing a pointer to the
1578 * next item; initially, that's a standalone "root of the
1579 * list" variable. */
1580 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1582 if ((*callback
)->removed
) {
1583 struct target_timer_callback
*p
= *callback
;
1584 *callback
= (*callback
)->next
;
1589 bool call_it
= (*callback
)->callback
&&
1590 ((!checktime
&& (*callback
)->periodic
) ||
1591 now
.tv_sec
> (*callback
)->when
.tv_sec
||
1592 (now
.tv_sec
== (*callback
)->when
.tv_sec
&&
1593 now
.tv_usec
>= (*callback
)->when
.tv_usec
));
1596 target_call_timer_callback(*callback
, &now
);
1598 callback
= &(*callback
)->next
;
1601 callback_processing
= false;
1605 int target_call_timer_callbacks(void)
1607 return target_call_timer_callbacks_check_time(1);
1610 /* invoke periodic callbacks immediately */
1611 int target_call_timer_callbacks_now(void)
1613 return target_call_timer_callbacks_check_time(0);
1616 /* Prints the working area layout for debug purposes */
1617 static void print_wa_layout(struct target
*target
)
1619 struct working_area
*c
= target
->working_areas
;
1622 LOG_DEBUG("%c%c 0x%08"PRIx32
"-0x%08"PRIx32
" (%"PRIu32
" bytes)",
1623 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1624 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1629 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1630 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1632 assert(area
->free
); /* Shouldn't split an allocated area */
1633 assert(size
<= area
->size
); /* Caller should guarantee this */
1635 /* Split only if not already the right size */
1636 if (size
< area
->size
) {
1637 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1642 new_wa
->next
= area
->next
;
1643 new_wa
->size
= area
->size
- size
;
1644 new_wa
->address
= area
->address
+ size
;
1645 new_wa
->backup
= NULL
;
1646 new_wa
->user
= NULL
;
1647 new_wa
->free
= true;
1649 area
->next
= new_wa
;
1652 /* If backup memory was allocated to this area, it has the wrong size
1653 * now so free it and it will be reallocated if/when needed */
1656 area
->backup
= NULL
;
1661 /* Merge all adjacent free areas into one */
1662 static void target_merge_working_areas(struct target
*target
)
1664 struct working_area
*c
= target
->working_areas
;
1666 while (c
&& c
->next
) {
1667 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1669 /* Find two adjacent free areas */
1670 if (c
->free
&& c
->next
->free
) {
1671 /* Merge the last into the first */
1672 c
->size
+= c
->next
->size
;
1674 /* Remove the last */
1675 struct working_area
*to_be_freed
= c
->next
;
1676 c
->next
= c
->next
->next
;
1677 if (to_be_freed
->backup
)
1678 free(to_be_freed
->backup
);
1681 /* If backup memory was allocated to the remaining area, it's has
1682 * the wrong size now */
1693 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1695 /* Reevaluate working area address based on MMU state*/
1696 if (target
->working_areas
== NULL
) {
1700 retval
= target
->type
->mmu(target
, &enabled
);
1701 if (retval
!= ERROR_OK
)
1705 if (target
->working_area_phys_spec
) {
1706 LOG_DEBUG("MMU disabled, using physical "
1707 "address for working memory 0x%08"PRIx32
,
1708 target
->working_area_phys
);
1709 target
->working_area
= target
->working_area_phys
;
1711 LOG_ERROR("No working memory available. "
1712 "Specify -work-area-phys to target.");
1713 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1716 if (target
->working_area_virt_spec
) {
1717 LOG_DEBUG("MMU enabled, using virtual "
1718 "address for working memory 0x%08"PRIx32
,
1719 target
->working_area_virt
);
1720 target
->working_area
= target
->working_area_virt
;
1722 LOG_ERROR("No working memory available. "
1723 "Specify -work-area-virt to target.");
1724 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1728 /* Set up initial working area on first call */
1729 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1731 new_wa
->next
= NULL
;
1732 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1733 new_wa
->address
= target
->working_area
;
1734 new_wa
->backup
= NULL
;
1735 new_wa
->user
= NULL
;
1736 new_wa
->free
= true;
1739 target
->working_areas
= new_wa
;
1742 /* only allocate multiples of 4 byte */
1744 size
= (size
+ 3) & (~3UL);
1746 struct working_area
*c
= target
->working_areas
;
1748 /* Find the first large enough working area */
1750 if (c
->free
&& c
->size
>= size
)
1756 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1758 /* Split the working area into the requested size */
1759 target_split_working_area(c
, size
);
1761 LOG_DEBUG("allocated new working area of %"PRIu32
" bytes at address 0x%08"PRIx32
, size
, c
->address
);
1763 if (target
->backup_working_area
) {
1764 if (c
->backup
== NULL
) {
1765 c
->backup
= malloc(c
->size
);
1766 if (c
->backup
== NULL
)
1770 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1771 if (retval
!= ERROR_OK
)
1775 /* mark as used, and return the new (reused) area */
1782 print_wa_layout(target
);
1787 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1791 retval
= target_alloc_working_area_try(target
, size
, area
);
1792 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1793 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1798 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1800 int retval
= ERROR_OK
;
1802 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1803 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1804 if (retval
!= ERROR_OK
)
1805 LOG_ERROR("failed to restore %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1806 area
->size
, area
->address
);
1812 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1813 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1815 int retval
= ERROR_OK
;
1821 retval
= target_restore_working_area(target
, area
);
1822 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1823 if (retval
!= ERROR_OK
)
1829 LOG_DEBUG("freed %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1830 area
->size
, area
->address
);
1832 /* mark user pointer invalid */
1833 /* TODO: Is this really safe? It points to some previous caller's memory.
1834 * How could we know that the area pointer is still in that place and not
1835 * some other vital data? What's the purpose of this, anyway? */
1839 target_merge_working_areas(target
);
1841 print_wa_layout(target
);
1846 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1848 return target_free_working_area_restore(target
, area
, 1);
1851 void target_quit(void)
1853 struct target_event_callback
*pe
= target_event_callbacks
;
1855 struct target_event_callback
*t
= pe
->next
;
1859 target_event_callbacks
= NULL
;
1861 struct target_timer_callback
*pt
= target_timer_callbacks
;
1863 struct target_timer_callback
*t
= pt
->next
;
1867 target_timer_callbacks
= NULL
;
1869 for (struct target
*target
= all_targets
;
1870 target
; target
= target
->next
) {
1871 if (target
->type
->deinit_target
)
1872 target
->type
->deinit_target(target
);
1876 /* free resources and restore memory, if restoring memory fails,
1877 * free up resources anyway
1879 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1881 struct working_area
*c
= target
->working_areas
;
1883 LOG_DEBUG("freeing all working areas");
1885 /* Loop through all areas, restoring the allocated ones and marking them as free */
1889 target_restore_working_area(target
, c
);
1891 *c
->user
= NULL
; /* Same as above */
1897 /* Run a merge pass to combine all areas into one */
1898 target_merge_working_areas(target
);
1900 print_wa_layout(target
);
1903 void target_free_all_working_areas(struct target
*target
)
1905 target_free_all_working_areas_restore(target
, 1);
1908 /* Find the largest number of bytes that can be allocated */
1909 uint32_t target_get_working_area_avail(struct target
*target
)
1911 struct working_area
*c
= target
->working_areas
;
1912 uint32_t max_size
= 0;
1915 return target
->working_area_size
;
1918 if (c
->free
&& max_size
< c
->size
)
1927 int target_arch_state(struct target
*target
)
1930 if (target
== NULL
) {
1931 LOG_USER("No target has been configured");
1935 LOG_USER("%s: target state: %s", target_name(target
),
1936 target_state_name(target
));
1938 if (target
->state
!= TARGET_HALTED
)
1941 retval
= target
->type
->arch_state(target
);
1945 static int target_get_gdb_fileio_info_default(struct target
*target
,
1946 struct gdb_fileio_info
*fileio_info
)
1948 /* If target does not support semi-hosting function, target
1949 has no need to provide .get_gdb_fileio_info callback.
1950 It just return ERROR_FAIL and gdb_server will return "Txx"
1951 as target halted every time. */
1955 static int target_gdb_fileio_end_default(struct target
*target
,
1956 int retcode
, int fileio_errno
, bool ctrl_c
)
1961 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
1962 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1964 struct timeval timeout
, now
;
1966 gettimeofday(&timeout
, NULL
);
1967 timeval_add_time(&timeout
, seconds
, 0);
1969 LOG_INFO("Starting profiling. Halting and resuming the"
1970 " target as often as we can...");
1972 uint32_t sample_count
= 0;
1973 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
1974 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
1976 int retval
= ERROR_OK
;
1978 target_poll(target
);
1979 if (target
->state
== TARGET_HALTED
) {
1980 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
1981 samples
[sample_count
++] = t
;
1982 /* current pc, addr = 0, do not handle breakpoints, not debugging */
1983 retval
= target_resume(target
, 1, 0, 0, 0);
1984 target_poll(target
);
1985 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
1986 } else if (target
->state
== TARGET_RUNNING
) {
1987 /* We want to quickly sample the PC. */
1988 retval
= target_halt(target
);
1990 LOG_INFO("Target not halted or running");
1995 if (retval
!= ERROR_OK
)
1998 gettimeofday(&now
, NULL
);
1999 if ((sample_count
>= max_num_samples
) ||
2000 ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
))) {
2001 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2006 *num_samples
= sample_count
;
2010 /* Single aligned words are guaranteed to use 16 or 32 bit access
2011 * mode respectively, otherwise data is handled as quickly as
2014 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
2016 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
2017 (int)size
, (unsigned)address
);
2019 if (!target_was_examined(target
)) {
2020 LOG_ERROR("Target not examined yet");
2027 if ((address
+ size
- 1) < address
) {
2028 /* GDB can request this when e.g. PC is 0xfffffffc*/
2029 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
2035 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2038 static int target_write_buffer_default(struct target
*target
, uint32_t address
, uint32_t count
, const uint8_t *buffer
)
2042 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2043 * will have something to do with the size we leave to it. */
2044 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2045 if (address
& size
) {
2046 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2047 if (retval
!= ERROR_OK
)
2055 /* Write the data with as large access size as possible. */
2056 for (; size
> 0; size
/= 2) {
2057 uint32_t aligned
= count
- count
% size
;
2059 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2060 if (retval
!= ERROR_OK
)
2071 /* Single aligned words are guaranteed to use 16 or 32 bit access
2072 * mode respectively, otherwise data is handled as quickly as
2075 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
2077 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
2078 (int)size
, (unsigned)address
);
2080 if (!target_was_examined(target
)) {
2081 LOG_ERROR("Target not examined yet");
2088 if ((address
+ size
- 1) < address
) {
2089 /* GDB can request this when e.g. PC is 0xfffffffc*/
2090 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
2096 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2099 static int target_read_buffer_default(struct target
*target
, uint32_t address
, uint32_t count
, uint8_t *buffer
)
2103 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2104 * will have something to do with the size we leave to it. */
2105 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2106 if (address
& size
) {
2107 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2108 if (retval
!= ERROR_OK
)
2116 /* Read the data with as large access size as possible. */
2117 for (; size
> 0; size
/= 2) {
2118 uint32_t aligned
= count
- count
% size
;
2120 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2121 if (retval
!= ERROR_OK
)
2132 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
2137 uint32_t checksum
= 0;
2138 if (!target_was_examined(target
)) {
2139 LOG_ERROR("Target not examined yet");
2143 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2144 if (retval
!= ERROR_OK
) {
2145 buffer
= malloc(size
);
2146 if (buffer
== NULL
) {
2147 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
2148 return ERROR_COMMAND_SYNTAX_ERROR
;
2150 retval
= target_read_buffer(target
, address
, size
, buffer
);
2151 if (retval
!= ERROR_OK
) {
2156 /* convert to target endianness */
2157 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2158 uint32_t target_data
;
2159 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2160 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2163 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2172 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
2175 if (!target_was_examined(target
)) {
2176 LOG_ERROR("Target not examined yet");
2180 if (target
->type
->blank_check_memory
== 0)
2181 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2183 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
2188 int target_read_u64(struct target
*target
, uint64_t address
, uint64_t *value
)
2190 uint8_t value_buf
[8];
2191 if (!target_was_examined(target
)) {
2192 LOG_ERROR("Target not examined yet");
2196 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2198 if (retval
== ERROR_OK
) {
2199 *value
= target_buffer_get_u64(target
, value_buf
);
2200 LOG_DEBUG("address: 0x%" PRIx64
", value: 0x%16.16" PRIx64
"",
2205 LOG_DEBUG("address: 0x%" PRIx64
" failed",
2212 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
2214 uint8_t value_buf
[4];
2215 if (!target_was_examined(target
)) {
2216 LOG_ERROR("Target not examined yet");
2220 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2222 if (retval
== ERROR_OK
) {
2223 *value
= target_buffer_get_u32(target
, value_buf
);
2224 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
2229 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2236 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
2238 uint8_t value_buf
[2];
2239 if (!target_was_examined(target
)) {
2240 LOG_ERROR("Target not examined yet");
2244 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2246 if (retval
== ERROR_OK
) {
2247 *value
= target_buffer_get_u16(target
, value_buf
);
2248 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
2253 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2260 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
2262 if (!target_was_examined(target
)) {
2263 LOG_ERROR("Target not examined yet");
2267 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2269 if (retval
== ERROR_OK
) {
2270 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
2275 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
2282 int target_write_u64(struct target
*target
, uint64_t address
, uint64_t value
)
2285 uint8_t value_buf
[8];
2286 if (!target_was_examined(target
)) {
2287 LOG_ERROR("Target not examined yet");
2291 LOG_DEBUG("address: 0x%" PRIx64
", value: 0x%16.16" PRIx64
"",
2295 target_buffer_set_u64(target
, value_buf
, value
);
2296 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2297 if (retval
!= ERROR_OK
)
2298 LOG_DEBUG("failed: %i", retval
);
2303 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
2306 uint8_t value_buf
[4];
2307 if (!target_was_examined(target
)) {
2308 LOG_ERROR("Target not examined yet");
2312 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
2316 target_buffer_set_u32(target
, value_buf
, value
);
2317 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2318 if (retval
!= ERROR_OK
)
2319 LOG_DEBUG("failed: %i", retval
);
2324 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
2327 uint8_t value_buf
[2];
2328 if (!target_was_examined(target
)) {
2329 LOG_ERROR("Target not examined yet");
2333 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
2337 target_buffer_set_u16(target
, value_buf
, value
);
2338 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2339 if (retval
!= ERROR_OK
)
2340 LOG_DEBUG("failed: %i", retval
);
2345 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
2348 if (!target_was_examined(target
)) {
2349 LOG_ERROR("Target not examined yet");
2353 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
2356 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2357 if (retval
!= ERROR_OK
)
2358 LOG_DEBUG("failed: %i", retval
);
2363 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2365 struct target
*target
= get_target(name
);
2366 if (target
== NULL
) {
2367 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2370 if (!target
->tap
->enabled
) {
2371 LOG_USER("Target: TAP %s is disabled, "
2372 "can't be the current target\n",
2373 target
->tap
->dotted_name
);
2377 cmd_ctx
->current_target
= target
->target_number
;
2382 COMMAND_HANDLER(handle_targets_command
)
2384 int retval
= ERROR_OK
;
2385 if (CMD_ARGC
== 1) {
2386 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2387 if (retval
== ERROR_OK
) {
2393 struct target
*target
= all_targets
;
2394 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2395 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2400 if (target
->tap
->enabled
)
2401 state
= target_state_name(target
);
2403 state
= "tap-disabled";
2405 if (CMD_CTX
->current_target
== target
->target_number
)
2408 /* keep columns lined up to match the headers above */
2409 command_print(CMD_CTX
,
2410 "%2d%c %-18s %-10s %-6s %-18s %s",
2411 target
->target_number
,
2413 target_name(target
),
2414 target_type_name(target
),
2415 Jim_Nvp_value2name_simple(nvp_target_endian
,
2416 target
->endianness
)->name
,
2417 target
->tap
->dotted_name
,
2419 target
= target
->next
;
2425 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2427 static int powerDropout
;
2428 static int srstAsserted
;
2430 static int runPowerRestore
;
2431 static int runPowerDropout
;
2432 static int runSrstAsserted
;
2433 static int runSrstDeasserted
;
2435 static int sense_handler(void)
2437 static int prevSrstAsserted
;
2438 static int prevPowerdropout
;
2440 int retval
= jtag_power_dropout(&powerDropout
);
2441 if (retval
!= ERROR_OK
)
2445 powerRestored
= prevPowerdropout
&& !powerDropout
;
2447 runPowerRestore
= 1;
2449 long long current
= timeval_ms();
2450 static long long lastPower
;
2451 int waitMore
= lastPower
+ 2000 > current
;
2452 if (powerDropout
&& !waitMore
) {
2453 runPowerDropout
= 1;
2454 lastPower
= current
;
2457 retval
= jtag_srst_asserted(&srstAsserted
);
2458 if (retval
!= ERROR_OK
)
2462 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2464 static long long lastSrst
;
2465 waitMore
= lastSrst
+ 2000 > current
;
2466 if (srstDeasserted
&& !waitMore
) {
2467 runSrstDeasserted
= 1;
2471 if (!prevSrstAsserted
&& srstAsserted
)
2472 runSrstAsserted
= 1;
2474 prevSrstAsserted
= srstAsserted
;
2475 prevPowerdropout
= powerDropout
;
2477 if (srstDeasserted
|| powerRestored
) {
2478 /* Other than logging the event we can't do anything here.
2479 * Issuing a reset is a particularly bad idea as we might
2480 * be inside a reset already.
2487 /* process target state changes */
2488 static int handle_target(void *priv
)
2490 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2491 int retval
= ERROR_OK
;
2493 if (!is_jtag_poll_safe()) {
2494 /* polling is disabled currently */
2498 /* we do not want to recurse here... */
2499 static int recursive
;
2503 /* danger! running these procedures can trigger srst assertions and power dropouts.
2504 * We need to avoid an infinite loop/recursion here and we do that by
2505 * clearing the flags after running these events.
2507 int did_something
= 0;
2508 if (runSrstAsserted
) {
2509 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2510 Jim_Eval(interp
, "srst_asserted");
2513 if (runSrstDeasserted
) {
2514 Jim_Eval(interp
, "srst_deasserted");
2517 if (runPowerDropout
) {
2518 LOG_INFO("Power dropout detected, running power_dropout proc.");
2519 Jim_Eval(interp
, "power_dropout");
2522 if (runPowerRestore
) {
2523 Jim_Eval(interp
, "power_restore");
2527 if (did_something
) {
2528 /* clear detect flags */
2532 /* clear action flags */
2534 runSrstAsserted
= 0;
2535 runSrstDeasserted
= 0;
2536 runPowerRestore
= 0;
2537 runPowerDropout
= 0;
2542 /* Poll targets for state changes unless that's globally disabled.
2543 * Skip targets that are currently disabled.
2545 for (struct target
*target
= all_targets
;
2546 is_jtag_poll_safe() && target
;
2547 target
= target
->next
) {
2549 if (!target_was_examined(target
))
2552 if (!target
->tap
->enabled
)
2555 if (target
->backoff
.times
> target
->backoff
.count
) {
2556 /* do not poll this time as we failed previously */
2557 target
->backoff
.count
++;
2560 target
->backoff
.count
= 0;
2562 /* only poll target if we've got power and srst isn't asserted */
2563 if (!powerDropout
&& !srstAsserted
) {
2564 /* polling may fail silently until the target has been examined */
2565 retval
= target_poll(target
);
2566 if (retval
!= ERROR_OK
) {
2567 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2568 if (target
->backoff
.times
* polling_interval
< 5000) {
2569 target
->backoff
.times
*= 2;
2570 target
->backoff
.times
++;
2573 /* Tell GDB to halt the debugger. This allows the user to
2574 * run monitor commands to handle the situation.
2576 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2578 if (target
->backoff
.times
> 0) {
2579 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
2580 target_reset_examined(target
);
2581 retval
= target_examine_one(target
);
2582 /* Target examination could have failed due to unstable connection,
2583 * but we set the examined flag anyway to repoll it later */
2584 if (retval
!= ERROR_OK
) {
2585 target
->examined
= true;
2586 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2587 target
->backoff
.times
* polling_interval
);
2592 /* Since we succeeded, we reset backoff count */
2593 target
->backoff
.times
= 0;
2600 COMMAND_HANDLER(handle_reg_command
)
2602 struct target
*target
;
2603 struct reg
*reg
= NULL
;
2609 target
= get_current_target(CMD_CTX
);
2611 /* list all available registers for the current target */
2612 if (CMD_ARGC
== 0) {
2613 struct reg_cache
*cache
= target
->reg_cache
;
2619 command_print(CMD_CTX
, "===== %s", cache
->name
);
2621 for (i
= 0, reg
= cache
->reg_list
;
2622 i
< cache
->num_regs
;
2623 i
++, reg
++, count
++) {
2624 /* only print cached values if they are valid */
2626 value
= buf_to_str(reg
->value
,
2628 command_print(CMD_CTX
,
2629 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2637 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2642 cache
= cache
->next
;
2648 /* access a single register by its ordinal number */
2649 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2651 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2653 struct reg_cache
*cache
= target
->reg_cache
;
2657 for (i
= 0; i
< cache
->num_regs
; i
++) {
2658 if (count
++ == num
) {
2659 reg
= &cache
->reg_list
[i
];
2665 cache
= cache
->next
;
2669 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2670 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2674 /* access a single register by its name */
2675 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2678 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2683 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2685 /* display a register */
2686 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2687 && (CMD_ARGV
[1][0] <= '9')))) {
2688 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2691 if (reg
->valid
== 0)
2692 reg
->type
->get(reg
);
2693 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2694 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2699 /* set register value */
2700 if (CMD_ARGC
== 2) {
2701 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2704 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2706 reg
->type
->set(reg
, buf
);
2708 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2709 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2717 return ERROR_COMMAND_SYNTAX_ERROR
;
2720 COMMAND_HANDLER(handle_poll_command
)
2722 int retval
= ERROR_OK
;
2723 struct target
*target
= get_current_target(CMD_CTX
);
2725 if (CMD_ARGC
== 0) {
2726 command_print(CMD_CTX
, "background polling: %s",
2727 jtag_poll_get_enabled() ? "on" : "off");
2728 command_print(CMD_CTX
, "TAP: %s (%s)",
2729 target
->tap
->dotted_name
,
2730 target
->tap
->enabled
? "enabled" : "disabled");
2731 if (!target
->tap
->enabled
)
2733 retval
= target_poll(target
);
2734 if (retval
!= ERROR_OK
)
2736 retval
= target_arch_state(target
);
2737 if (retval
!= ERROR_OK
)
2739 } else if (CMD_ARGC
== 1) {
2741 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2742 jtag_poll_set_enabled(enable
);
2744 return ERROR_COMMAND_SYNTAX_ERROR
;
2749 COMMAND_HANDLER(handle_wait_halt_command
)
2752 return ERROR_COMMAND_SYNTAX_ERROR
;
2754 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2755 if (1 == CMD_ARGC
) {
2756 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2757 if (ERROR_OK
!= retval
)
2758 return ERROR_COMMAND_SYNTAX_ERROR
;
2761 struct target
*target
= get_current_target(CMD_CTX
);
2762 return target_wait_state(target
, TARGET_HALTED
, ms
);
2765 /* wait for target state to change. The trick here is to have a low
2766 * latency for short waits and not to suck up all the CPU time
2769 * After 500ms, keep_alive() is invoked
2771 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2774 long long then
= 0, cur
;
2778 retval
= target_poll(target
);
2779 if (retval
!= ERROR_OK
)
2781 if (target
->state
== state
)
2786 then
= timeval_ms();
2787 LOG_DEBUG("waiting for target %s...",
2788 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2794 if ((cur
-then
) > ms
) {
2795 LOG_ERROR("timed out while waiting for target %s",
2796 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2804 COMMAND_HANDLER(handle_halt_command
)
2808 struct target
*target
= get_current_target(CMD_CTX
);
2809 int retval
= target_halt(target
);
2810 if (ERROR_OK
!= retval
)
2813 if (CMD_ARGC
== 1) {
2814 unsigned wait_local
;
2815 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2816 if (ERROR_OK
!= retval
)
2817 return ERROR_COMMAND_SYNTAX_ERROR
;
2822 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2825 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2827 struct target
*target
= get_current_target(CMD_CTX
);
2829 LOG_USER("requesting target halt and executing a soft reset");
2831 target_soft_reset_halt(target
);
2836 COMMAND_HANDLER(handle_reset_command
)
2839 return ERROR_COMMAND_SYNTAX_ERROR
;
2841 enum target_reset_mode reset_mode
= RESET_RUN
;
2842 if (CMD_ARGC
== 1) {
2844 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2845 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
2846 return ERROR_COMMAND_SYNTAX_ERROR
;
2847 reset_mode
= n
->value
;
2850 /* reset *all* targets */
2851 return target_process_reset(CMD_CTX
, reset_mode
);
2855 COMMAND_HANDLER(handle_resume_command
)
2859 return ERROR_COMMAND_SYNTAX_ERROR
;
2861 struct target
*target
= get_current_target(CMD_CTX
);
2863 /* with no CMD_ARGV, resume from current pc, addr = 0,
2864 * with one arguments, addr = CMD_ARGV[0],
2865 * handle breakpoints, not debugging */
2867 if (CMD_ARGC
== 1) {
2868 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2872 return target_resume(target
, current
, addr
, 1, 0);
2875 COMMAND_HANDLER(handle_step_command
)
2878 return ERROR_COMMAND_SYNTAX_ERROR
;
2882 /* with no CMD_ARGV, step from current pc, addr = 0,
2883 * with one argument addr = CMD_ARGV[0],
2884 * handle breakpoints, debugging */
2887 if (CMD_ARGC
== 1) {
2888 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2892 struct target
*target
= get_current_target(CMD_CTX
);
2894 return target
->type
->step(target
, current_pc
, addr
, 1);
2897 static void handle_md_output(struct command_context
*cmd_ctx
,
2898 struct target
*target
, uint32_t address
, unsigned size
,
2899 unsigned count
, const uint8_t *buffer
)
2901 const unsigned line_bytecnt
= 32;
2902 unsigned line_modulo
= line_bytecnt
/ size
;
2904 char output
[line_bytecnt
* 4 + 1];
2905 unsigned output_len
= 0;
2907 const char *value_fmt
;
2910 value_fmt
= "%8.8x ";
2913 value_fmt
= "%4.4x ";
2916 value_fmt
= "%2.2x ";
2919 /* "can't happen", caller checked */
2920 LOG_ERROR("invalid memory read size: %u", size
);
2924 for (unsigned i
= 0; i
< count
; i
++) {
2925 if (i
% line_modulo
== 0) {
2926 output_len
+= snprintf(output
+ output_len
,
2927 sizeof(output
) - output_len
,
2929 (unsigned)(address
+ (i
*size
)));
2933 const uint8_t *value_ptr
= buffer
+ i
* size
;
2936 value
= target_buffer_get_u32(target
, value_ptr
);
2939 value
= target_buffer_get_u16(target
, value_ptr
);
2944 output_len
+= snprintf(output
+ output_len
,
2945 sizeof(output
) - output_len
,
2948 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
2949 command_print(cmd_ctx
, "%s", output
);
2955 COMMAND_HANDLER(handle_md_command
)
2958 return ERROR_COMMAND_SYNTAX_ERROR
;
2961 switch (CMD_NAME
[2]) {
2972 return ERROR_COMMAND_SYNTAX_ERROR
;
2975 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2976 int (*fn
)(struct target
*target
,
2977 uint32_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
2981 fn
= target_read_phys_memory
;
2983 fn
= target_read_memory
;
2984 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2985 return ERROR_COMMAND_SYNTAX_ERROR
;
2988 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2992 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2994 uint8_t *buffer
= calloc(count
, size
);
2996 struct target
*target
= get_current_target(CMD_CTX
);
2997 int retval
= fn(target
, address
, size
, count
, buffer
);
2998 if (ERROR_OK
== retval
)
2999 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
3006 typedef int (*target_write_fn
)(struct target
*target
,
3007 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
3009 static int target_fill_mem(struct target
*target
,
3018 /* We have to write in reasonably large chunks to be able
3019 * to fill large memory areas with any sane speed */
3020 const unsigned chunk_size
= 16384;
3021 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
3022 if (target_buf
== NULL
) {
3023 LOG_ERROR("Out of memory");
3027 for (unsigned i
= 0; i
< chunk_size
; i
++) {
3028 switch (data_size
) {
3030 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
3033 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
3036 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
3043 int retval
= ERROR_OK
;
3045 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
3048 if (current
> chunk_size
)
3049 current
= chunk_size
;
3050 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
3051 if (retval
!= ERROR_OK
)
3053 /* avoid GDB timeouts */
3062 COMMAND_HANDLER(handle_mw_command
)
3065 return ERROR_COMMAND_SYNTAX_ERROR
;
3066 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3071 fn
= target_write_phys_memory
;
3073 fn
= target_write_memory
;
3074 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
3075 return ERROR_COMMAND_SYNTAX_ERROR
;
3078 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
3081 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
3085 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
3087 struct target
*target
= get_current_target(CMD_CTX
);
3089 switch (CMD_NAME
[2]) {
3100 return ERROR_COMMAND_SYNTAX_ERROR
;
3103 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
3106 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
3107 uint32_t *min_address
, uint32_t *max_address
)
3109 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
3110 return ERROR_COMMAND_SYNTAX_ERROR
;
3112 /* a base address isn't always necessary,
3113 * default to 0x0 (i.e. don't relocate) */
3114 if (CMD_ARGC
>= 2) {
3116 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
3117 image
->base_address
= addr
;
3118 image
->base_address_set
= 1;
3120 image
->base_address_set
= 0;
3122 image
->start_address_set
= 0;
3125 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
3126 if (CMD_ARGC
== 5) {
3127 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
3128 /* use size (given) to find max (required) */
3129 *max_address
+= *min_address
;
3132 if (*min_address
> *max_address
)
3133 return ERROR_COMMAND_SYNTAX_ERROR
;
3138 COMMAND_HANDLER(handle_load_image_command
)
3142 uint32_t image_size
;
3143 uint32_t min_address
= 0;
3144 uint32_t max_address
= 0xffffffff;
3148 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
3149 &image
, &min_address
, &max_address
);
3150 if (ERROR_OK
!= retval
)
3153 struct target
*target
= get_current_target(CMD_CTX
);
3155 struct duration bench
;
3156 duration_start(&bench
);
3158 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != 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
));
3172 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3173 if (retval
!= ERROR_OK
) {
3178 uint32_t offset
= 0;
3179 uint32_t length
= buf_cnt
;
3181 /* DANGER!!! beware of unsigned comparision here!!! */
3183 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3184 (image
.sections
[i
].base_address
< max_address
)) {
3186 if (image
.sections
[i
].base_address
< min_address
) {
3187 /* clip addresses below */
3188 offset
+= min_address
-image
.sections
[i
].base_address
;
3192 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3193 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3195 retval
= target_write_buffer(target
,
3196 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3197 if (retval
!= ERROR_OK
) {
3201 image_size
+= length
;
3202 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
3203 (unsigned int)length
,
3204 image
.sections
[i
].base_address
+ offset
);
3210 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3211 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
3212 "in %fs (%0.3f KiB/s)", image_size
,
3213 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3216 image_close(&image
);
3222 COMMAND_HANDLER(handle_dump_image_command
)
3224 struct fileio
*fileio
;
3226 int retval
, retvaltemp
;
3227 uint32_t address
, size
;
3228 struct duration bench
;
3229 struct target
*target
= get_current_target(CMD_CTX
);
3232 return ERROR_COMMAND_SYNTAX_ERROR
;
3234 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
3235 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
3237 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
3238 buffer
= malloc(buf_size
);
3242 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3243 if (retval
!= ERROR_OK
) {
3248 duration_start(&bench
);
3251 size_t size_written
;
3252 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
3253 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
3254 if (retval
!= ERROR_OK
)
3257 retval
= fileio_write(fileio
, this_run_size
, buffer
, &size_written
);
3258 if (retval
!= ERROR_OK
)
3261 size
-= this_run_size
;
3262 address
+= this_run_size
;
3267 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3269 retval
= fileio_size(fileio
, &filesize
);
3270 if (retval
!= ERROR_OK
)
3272 command_print(CMD_CTX
,
3273 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize
,
3274 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3277 retvaltemp
= fileio_close(fileio
);
3278 if (retvaltemp
!= ERROR_OK
)
3284 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
3288 uint32_t image_size
;
3291 uint32_t checksum
= 0;
3292 uint32_t mem_checksum
= 0;
3296 struct target
*target
= get_current_target(CMD_CTX
);
3299 return ERROR_COMMAND_SYNTAX_ERROR
;
3302 LOG_ERROR("no target selected");
3306 struct duration bench
;
3307 duration_start(&bench
);
3309 if (CMD_ARGC
>= 2) {
3311 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
3312 image
.base_address
= addr
;
3313 image
.base_address_set
= 1;
3315 image
.base_address_set
= 0;
3316 image
.base_address
= 0x0;
3319 image
.start_address_set
= 0;
3321 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3322 if (retval
!= ERROR_OK
)
3328 for (i
= 0; i
< image
.num_sections
; i
++) {
3329 buffer
= malloc(image
.sections
[i
].size
);
3330 if (buffer
== NULL
) {
3331 command_print(CMD_CTX
,
3332 "error allocating buffer for section (%d bytes)",
3333 (int)(image
.sections
[i
].size
));
3336 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3337 if (retval
!= ERROR_OK
) {
3343 /* calculate checksum of image */
3344 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3345 if (retval
!= ERROR_OK
) {
3350 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3351 if (retval
!= ERROR_OK
) {
3356 if (checksum
!= mem_checksum
) {
3357 /* failed crc checksum, fall back to a binary compare */
3361 LOG_ERROR("checksum mismatch - attempting binary compare");
3363 data
= malloc(buf_cnt
);
3365 /* Can we use 32bit word accesses? */
3367 int count
= buf_cnt
;
3368 if ((count
% 4) == 0) {
3372 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
3373 if (retval
== ERROR_OK
) {
3375 for (t
= 0; t
< buf_cnt
; t
++) {
3376 if (data
[t
] != buffer
[t
]) {
3377 command_print(CMD_CTX
,
3378 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3380 (unsigned)(t
+ image
.sections
[i
].base_address
),
3383 if (diffs
++ >= 127) {
3384 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
3396 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
3397 image
.sections
[i
].base_address
,
3402 image_size
+= buf_cnt
;
3405 command_print(CMD_CTX
, "No more differences found.");
3408 retval
= ERROR_FAIL
;
3409 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3410 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3411 "in %fs (%0.3f KiB/s)", image_size
,
3412 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3415 image_close(&image
);
3420 COMMAND_HANDLER(handle_verify_image_command
)
3422 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
3425 COMMAND_HANDLER(handle_test_image_command
)
3427 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
3430 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
3432 struct target
*target
= get_current_target(cmd_ctx
);
3433 struct breakpoint
*breakpoint
= target
->breakpoints
;
3434 while (breakpoint
) {
3435 if (breakpoint
->type
== BKPT_SOFT
) {
3436 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3437 breakpoint
->length
, 16);
3438 command_print(cmd_ctx
, "IVA breakpoint: 0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
3439 breakpoint
->address
,
3441 breakpoint
->set
, buf
);
3444 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3445 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3447 breakpoint
->length
, breakpoint
->set
);
3448 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3449 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3450 breakpoint
->address
,
3451 breakpoint
->length
, breakpoint
->set
);
3452 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3455 command_print(cmd_ctx
, "Breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3456 breakpoint
->address
,
3457 breakpoint
->length
, breakpoint
->set
);
3460 breakpoint
= breakpoint
->next
;
3465 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
3466 uint32_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3468 struct target
*target
= get_current_target(cmd_ctx
);
3472 retval
= breakpoint_add(target
, addr
, length
, hw
);
3473 if (ERROR_OK
== retval
)
3474 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
3476 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3479 } else if (addr
== 0) {
3480 if (target
->type
->add_context_breakpoint
== NULL
) {
3481 LOG_WARNING("Context breakpoint not available");
3484 retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3485 if (ERROR_OK
== retval
)
3486 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3488 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3492 if (target
->type
->add_hybrid_breakpoint
== NULL
) {
3493 LOG_WARNING("Hybrid breakpoint not available");
3496 retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3497 if (ERROR_OK
== retval
)
3498 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3500 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3507 COMMAND_HANDLER(handle_bp_command
)
3516 return handle_bp_command_list(CMD_CTX
);
3520 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3521 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3522 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3525 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
3527 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3529 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3532 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3533 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
3535 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
3536 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3538 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3543 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3544 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
3545 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
3546 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3549 return ERROR_COMMAND_SYNTAX_ERROR
;
3553 COMMAND_HANDLER(handle_rbp_command
)
3556 return ERROR_COMMAND_SYNTAX_ERROR
;
3559 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3561 struct target
*target
= get_current_target(CMD_CTX
);
3562 breakpoint_remove(target
, addr
);
3567 COMMAND_HANDLER(handle_wp_command
)
3569 struct target
*target
= get_current_target(CMD_CTX
);
3571 if (CMD_ARGC
== 0) {
3572 struct watchpoint
*watchpoint
= target
->watchpoints
;
3574 while (watchpoint
) {
3575 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
3576 ", len: 0x%8.8" PRIx32
3577 ", r/w/a: %i, value: 0x%8.8" PRIx32
3578 ", mask: 0x%8.8" PRIx32
,
3579 watchpoint
->address
,
3581 (int)watchpoint
->rw
,
3584 watchpoint
= watchpoint
->next
;
3589 enum watchpoint_rw type
= WPT_ACCESS
;
3591 uint32_t length
= 0;
3592 uint32_t data_value
= 0x0;
3593 uint32_t data_mask
= 0xffffffff;
3597 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
3600 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3603 switch (CMD_ARGV
[2][0]) {
3614 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3615 return ERROR_COMMAND_SYNTAX_ERROR
;
3619 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3620 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3624 return ERROR_COMMAND_SYNTAX_ERROR
;
3627 int retval
= watchpoint_add(target
, addr
, length
, type
,
3628 data_value
, data_mask
);
3629 if (ERROR_OK
!= retval
)
3630 LOG_ERROR("Failure setting watchpoints");
3635 COMMAND_HANDLER(handle_rwp_command
)
3638 return ERROR_COMMAND_SYNTAX_ERROR
;
3641 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3643 struct target
*target
= get_current_target(CMD_CTX
);
3644 watchpoint_remove(target
, addr
);
3650 * Translate a virtual address to a physical address.
3652 * The low-level target implementation must have logged a detailed error
3653 * which is forwarded to telnet/GDB session.
3655 COMMAND_HANDLER(handle_virt2phys_command
)
3658 return ERROR_COMMAND_SYNTAX_ERROR
;
3661 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
3664 struct target
*target
= get_current_target(CMD_CTX
);
3665 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3666 if (retval
== ERROR_OK
)
3667 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
3672 static void writeData(FILE *f
, const void *data
, size_t len
)
3674 size_t written
= fwrite(data
, 1, len
, f
);
3676 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3679 static void writeLong(FILE *f
, int l
, struct target
*target
)
3683 target_buffer_set_u32(target
, val
, l
);
3684 writeData(f
, val
, 4);
3687 static void writeString(FILE *f
, char *s
)
3689 writeData(f
, s
, strlen(s
));
3692 typedef unsigned char UNIT
[2]; /* unit of profiling */
3694 /* Dump a gmon.out histogram file. */
3695 static void write_gmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
, bool with_range
,
3696 uint32_t start_address
, uint32_t end_address
, struct target
*target
)
3699 FILE *f
= fopen(filename
, "w");
3702 writeString(f
, "gmon");
3703 writeLong(f
, 0x00000001, target
); /* Version */
3704 writeLong(f
, 0, target
); /* padding */
3705 writeLong(f
, 0, target
); /* padding */
3706 writeLong(f
, 0, target
); /* padding */
3708 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3709 writeData(f
, &zero
, 1);
3711 /* figure out bucket size */
3715 min
= start_address
;
3720 for (i
= 0; i
< sampleNum
; i
++) {
3721 if (min
> samples
[i
])
3723 if (max
< samples
[i
])
3727 /* max should be (largest sample + 1)
3728 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
3732 int addressSpace
= max
- min
;
3733 assert(addressSpace
>= 2);
3735 /* FIXME: What is the reasonable number of buckets?
3736 * The profiling result will be more accurate if there are enough buckets. */
3737 static const uint32_t maxBuckets
= 128 * 1024; /* maximum buckets. */
3738 uint32_t numBuckets
= addressSpace
/ sizeof(UNIT
);
3739 if (numBuckets
> maxBuckets
)
3740 numBuckets
= maxBuckets
;
3741 int *buckets
= malloc(sizeof(int) * numBuckets
);
3742 if (buckets
== NULL
) {
3746 memset(buckets
, 0, sizeof(int) * numBuckets
);
3747 for (i
= 0; i
< sampleNum
; i
++) {
3748 uint32_t address
= samples
[i
];
3750 if ((address
< min
) || (max
<= address
))
3753 long long a
= address
- min
;
3754 long long b
= numBuckets
;
3755 long long c
= addressSpace
;
3756 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
3760 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3761 writeLong(f
, min
, target
); /* low_pc */
3762 writeLong(f
, max
, target
); /* high_pc */
3763 writeLong(f
, numBuckets
, target
); /* # of buckets */
3764 writeLong(f
, 100, target
); /* KLUDGE! We lie, ca. 100Hz best case. */
3765 writeString(f
, "seconds");
3766 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3767 writeData(f
, &zero
, 1);
3768 writeString(f
, "s");
3770 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3772 char *data
= malloc(2 * numBuckets
);
3774 for (i
= 0; i
< numBuckets
; i
++) {
3779 data
[i
* 2] = val
&0xff;
3780 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
3783 writeData(f
, data
, numBuckets
* 2);
3791 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3792 * which will be used as a random sampling of PC */
3793 COMMAND_HANDLER(handle_profile_command
)
3795 struct target
*target
= get_current_target(CMD_CTX
);
3797 if ((CMD_ARGC
!= 2) && (CMD_ARGC
!= 4))
3798 return ERROR_COMMAND_SYNTAX_ERROR
;
3800 const uint32_t MAX_PROFILE_SAMPLE_NUM
= 10000;
3802 uint32_t num_of_samples
;
3803 int retval
= ERROR_OK
;
3805 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], offset
);
3807 uint32_t *samples
= malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM
);
3808 if (samples
== NULL
) {
3809 LOG_ERROR("No memory to store samples.");
3814 * Some cores let us sample the PC without the
3815 * annoying halt/resume step; for example, ARMv7 PCSR.
3816 * Provide a way to use that more efficient mechanism.
3818 retval
= target_profiling(target
, samples
, MAX_PROFILE_SAMPLE_NUM
,
3819 &num_of_samples
, offset
);
3820 if (retval
!= ERROR_OK
) {
3825 assert(num_of_samples
<= MAX_PROFILE_SAMPLE_NUM
);
3827 retval
= target_poll(target
);
3828 if (retval
!= ERROR_OK
) {
3832 if (target
->state
== TARGET_RUNNING
) {
3833 retval
= target_halt(target
);
3834 if (retval
!= ERROR_OK
) {
3840 retval
= target_poll(target
);
3841 if (retval
!= ERROR_OK
) {
3846 uint32_t start_address
= 0;
3847 uint32_t end_address
= 0;
3848 bool with_range
= false;
3849 if (CMD_ARGC
== 4) {
3851 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], start_address
);
3852 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], end_address
);
3855 write_gmon(samples
, num_of_samples
, CMD_ARGV
[1],
3856 with_range
, start_address
, end_address
, target
);
3857 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3863 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
3866 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3869 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3873 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3874 valObjPtr
= Jim_NewIntObj(interp
, val
);
3875 if (!nameObjPtr
|| !valObjPtr
) {
3880 Jim_IncrRefCount(nameObjPtr
);
3881 Jim_IncrRefCount(valObjPtr
);
3882 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3883 Jim_DecrRefCount(interp
, nameObjPtr
);
3884 Jim_DecrRefCount(interp
, valObjPtr
);
3886 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3890 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3892 struct command_context
*context
;
3893 struct target
*target
;
3895 context
= current_command_context(interp
);
3896 assert(context
!= NULL
);
3898 target
= get_current_target(context
);
3899 if (target
== NULL
) {
3900 LOG_ERROR("mem2array: no current target");
3904 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
3907 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3915 const char *varname
;
3919 /* argv[1] = name of array to receive the data
3920 * argv[2] = desired width
3921 * argv[3] = memory address
3922 * argv[4] = count of times to read
3925 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3928 varname
= Jim_GetString(argv
[0], &len
);
3929 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3931 e
= Jim_GetLong(interp
, argv
[1], &l
);
3936 e
= Jim_GetLong(interp
, argv
[2], &l
);
3940 e
= Jim_GetLong(interp
, argv
[3], &l
);
3955 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3956 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3960 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3961 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3964 if ((addr
+ (len
* width
)) < addr
) {
3965 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3966 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3969 /* absurd transfer size? */
3971 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3972 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3977 ((width
== 2) && ((addr
& 1) == 0)) ||
3978 ((width
== 4) && ((addr
& 3) == 0))) {
3982 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3983 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3986 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3995 size_t buffersize
= 4096;
3996 uint8_t *buffer
= malloc(buffersize
);
4003 /* Slurp... in buffer size chunks */
4005 count
= len
; /* in objects.. */
4006 if (count
> (buffersize
/ width
))
4007 count
= (buffersize
/ width
);
4009 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
4010 if (retval
!= ERROR_OK
) {
4012 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
4016 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4017 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
4021 v
= 0; /* shut up gcc */
4022 for (i
= 0; i
< count
; i
++, n
++) {
4025 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
4028 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
4031 v
= buffer
[i
] & 0x0ff;
4034 new_int_array_element(interp
, varname
, n
, v
);
4037 addr
+= count
* width
;
4043 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4048 static int get_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t *val
)
4051 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4055 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4059 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4065 Jim_IncrRefCount(nameObjPtr
);
4066 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
4067 Jim_DecrRefCount(interp
, nameObjPtr
);
4069 if (valObjPtr
== NULL
)
4072 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
4073 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
4078 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4080 struct command_context
*context
;
4081 struct target
*target
;
4083 context
= current_command_context(interp
);
4084 assert(context
!= NULL
);
4086 target
= get_current_target(context
);
4087 if (target
== NULL
) {
4088 LOG_ERROR("array2mem: no current target");
4092 return target_array2mem(interp
, target
, argc
-1, argv
+ 1);
4095 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
4096 int argc
, Jim_Obj
*const *argv
)
4104 const char *varname
;
4108 /* argv[1] = name of array to get the data
4109 * argv[2] = desired width
4110 * argv[3] = memory address
4111 * argv[4] = count to write
4114 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
4117 varname
= Jim_GetString(argv
[0], &len
);
4118 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4120 e
= Jim_GetLong(interp
, argv
[1], &l
);
4125 e
= Jim_GetLong(interp
, argv
[2], &l
);
4129 e
= Jim_GetLong(interp
, argv
[3], &l
);
4144 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4145 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4146 "Invalid width param, must be 8/16/32", NULL
);
4150 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4151 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4152 "array2mem: zero width read?", NULL
);
4155 if ((addr
+ (len
* width
)) < addr
) {
4156 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4157 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4158 "array2mem: addr + len - wraps to zero?", NULL
);
4161 /* absurd transfer size? */
4163 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4164 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4165 "array2mem: absurd > 64K item request", NULL
);
4170 ((width
== 2) && ((addr
& 1) == 0)) ||
4171 ((width
== 4) && ((addr
& 3) == 0))) {
4175 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4176 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
4179 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4190 size_t buffersize
= 4096;
4191 uint8_t *buffer
= malloc(buffersize
);
4196 /* Slurp... in buffer size chunks */
4198 count
= len
; /* in objects.. */
4199 if (count
> (buffersize
/ width
))
4200 count
= (buffersize
/ width
);
4202 v
= 0; /* shut up gcc */
4203 for (i
= 0; i
< count
; i
++, n
++) {
4204 get_int_array_element(interp
, varname
, n
, &v
);
4207 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
4210 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
4213 buffer
[i
] = v
& 0x0ff;
4219 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
4220 if (retval
!= ERROR_OK
) {
4222 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
4226 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4227 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
4231 addr
+= count
* width
;
4236 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4241 /* FIX? should we propagate errors here rather than printing them
4244 void target_handle_event(struct target
*target
, enum target_event e
)
4246 struct target_event_action
*teap
;
4248 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4249 if (teap
->event
== e
) {
4250 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
4251 target
->target_number
,
4252 target_name(target
),
4253 target_type_name(target
),
4255 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
4256 Jim_GetString(teap
->body
, NULL
));
4257 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
) {
4258 Jim_MakeErrorMessage(teap
->interp
);
4259 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
4266 * Returns true only if the target has a handler for the specified event.
4268 bool target_has_event_action(struct target
*target
, enum target_event event
)
4270 struct target_event_action
*teap
;
4272 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4273 if (teap
->event
== event
)
4279 enum target_cfg_param
{
4282 TCFG_WORK_AREA_VIRT
,
4283 TCFG_WORK_AREA_PHYS
,
4284 TCFG_WORK_AREA_SIZE
,
4285 TCFG_WORK_AREA_BACKUP
,
4288 TCFG_CHAIN_POSITION
,
4293 static Jim_Nvp nvp_config_opts
[] = {
4294 { .name
= "-type", .value
= TCFG_TYPE
},
4295 { .name
= "-event", .value
= TCFG_EVENT
},
4296 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
4297 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
4298 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
4299 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
4300 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
4301 { .name
= "-coreid", .value
= TCFG_COREID
},
4302 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
4303 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
4304 { .name
= "-rtos", .value
= TCFG_RTOS
},
4305 { .name
= NULL
, .value
= -1 }
4308 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
4315 /* parse config or cget options ... */
4316 while (goi
->argc
> 0) {
4317 Jim_SetEmptyResult(goi
->interp
);
4318 /* Jim_GetOpt_Debug(goi); */
4320 if (target
->type
->target_jim_configure
) {
4321 /* target defines a configure function */
4322 /* target gets first dibs on parameters */
4323 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
4332 /* otherwise we 'continue' below */
4334 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
4336 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
4342 if (goi
->isconfigure
) {
4343 Jim_SetResultFormatted(goi
->interp
,
4344 "not settable: %s", n
->name
);
4348 if (goi
->argc
!= 0) {
4349 Jim_WrongNumArgs(goi
->interp
,
4350 goi
->argc
, goi
->argv
,
4355 Jim_SetResultString(goi
->interp
,
4356 target_type_name(target
), -1);
4360 if (goi
->argc
== 0) {
4361 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
4365 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
4367 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
4371 if (goi
->isconfigure
) {
4372 if (goi
->argc
!= 1) {
4373 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
4377 if (goi
->argc
!= 0) {
4378 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
4384 struct target_event_action
*teap
;
4386 teap
= target
->event_action
;
4387 /* replace existing? */
4389 if (teap
->event
== (enum target_event
)n
->value
)
4394 if (goi
->isconfigure
) {
4395 bool replace
= true;
4398 teap
= calloc(1, sizeof(*teap
));
4401 teap
->event
= n
->value
;
4402 teap
->interp
= goi
->interp
;
4403 Jim_GetOpt_Obj(goi
, &o
);
4405 Jim_DecrRefCount(teap
->interp
, teap
->body
);
4406 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
4409 * Tcl/TK - "tk events" have a nice feature.
4410 * See the "BIND" command.
4411 * We should support that here.
4412 * You can specify %X and %Y in the event code.
4413 * The idea is: %T - target name.
4414 * The idea is: %N - target number
4415 * The idea is: %E - event name.
4417 Jim_IncrRefCount(teap
->body
);
4420 /* add to head of event list */
4421 teap
->next
= target
->event_action
;
4422 target
->event_action
= teap
;
4424 Jim_SetEmptyResult(goi
->interp
);
4428 Jim_SetEmptyResult(goi
->interp
);
4430 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
4436 case TCFG_WORK_AREA_VIRT
:
4437 if (goi
->isconfigure
) {
4438 target_free_all_working_areas(target
);
4439 e
= Jim_GetOpt_Wide(goi
, &w
);
4442 target
->working_area_virt
= w
;
4443 target
->working_area_virt_spec
= true;
4448 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
4452 case TCFG_WORK_AREA_PHYS
:
4453 if (goi
->isconfigure
) {
4454 target_free_all_working_areas(target
);
4455 e
= Jim_GetOpt_Wide(goi
, &w
);
4458 target
->working_area_phys
= w
;
4459 target
->working_area_phys_spec
= true;
4464 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
4468 case TCFG_WORK_AREA_SIZE
:
4469 if (goi
->isconfigure
) {
4470 target_free_all_working_areas(target
);
4471 e
= Jim_GetOpt_Wide(goi
, &w
);
4474 target
->working_area_size
= w
;
4479 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4483 case TCFG_WORK_AREA_BACKUP
:
4484 if (goi
->isconfigure
) {
4485 target_free_all_working_areas(target
);
4486 e
= Jim_GetOpt_Wide(goi
, &w
);
4489 /* make this exactly 1 or 0 */
4490 target
->backup_working_area
= (!!w
);
4495 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
4496 /* loop for more e*/
4501 if (goi
->isconfigure
) {
4502 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
4504 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
4507 target
->endianness
= n
->value
;
4512 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4513 if (n
->name
== NULL
) {
4514 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4515 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4517 Jim_SetResultString(goi
->interp
, n
->name
, -1);
4522 if (goi
->isconfigure
) {
4523 e
= Jim_GetOpt_Wide(goi
, &w
);
4526 target
->coreid
= (int32_t)w
;
4531 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4535 case TCFG_CHAIN_POSITION
:
4536 if (goi
->isconfigure
) {
4538 struct jtag_tap
*tap
;
4539 target_free_all_working_areas(target
);
4540 e
= Jim_GetOpt_Obj(goi
, &o_t
);
4543 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
4546 /* make this exactly 1 or 0 */
4552 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
4553 /* loop for more e*/
4556 if (goi
->isconfigure
) {
4557 e
= Jim_GetOpt_Wide(goi
, &w
);
4560 target
->dbgbase
= (uint32_t)w
;
4561 target
->dbgbase_set
= true;
4566 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
4573 int result
= rtos_create(goi
, target
);
4574 if (result
!= JIM_OK
)
4580 } /* while (goi->argc) */
4583 /* done - we return */
4587 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4591 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4592 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4593 int need_args
= 1 + goi
.isconfigure
;
4594 if (goi
.argc
< need_args
) {
4595 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4597 ? "missing: -option VALUE ..."
4598 : "missing: -option ...");
4601 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4602 return target_configure(&goi
, target
);
4605 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4607 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4610 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4612 if (goi
.argc
< 2 || goi
.argc
> 4) {
4613 Jim_SetResultFormatted(goi
.interp
,
4614 "usage: %s [phys] <address> <data> [<count>]", cmd_name
);
4619 fn
= target_write_memory
;
4622 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4624 struct Jim_Obj
*obj
;
4625 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4629 fn
= target_write_phys_memory
;
4633 e
= Jim_GetOpt_Wide(&goi
, &a
);
4638 e
= Jim_GetOpt_Wide(&goi
, &b
);
4643 if (goi
.argc
== 1) {
4644 e
= Jim_GetOpt_Wide(&goi
, &c
);
4649 /* all args must be consumed */
4653 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4655 if (strcasecmp(cmd_name
, "mww") == 0)
4657 else if (strcasecmp(cmd_name
, "mwh") == 0)
4659 else if (strcasecmp(cmd_name
, "mwb") == 0)
4662 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4666 return (target_fill_mem(target
, a
, fn
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4670 * @brief Reads an array of words/halfwords/bytes from target memory starting at specified address.
4672 * Usage: mdw [phys] <address> [<count>] - for 32 bit reads
4673 * mdh [phys] <address> [<count>] - for 16 bit reads
4674 * mdb [phys] <address> [<count>] - for 8 bit reads
4676 * Count defaults to 1.
4678 * Calls target_read_memory or target_read_phys_memory depending on
4679 * the presence of the "phys" argument
4680 * Reads the target memory in blocks of max. 32 bytes, and returns an array of ints formatted
4681 * to int representation in base16.
4682 * Also outputs read data in a human readable form using command_print
4684 * @param phys if present target_read_phys_memory will be used instead of target_read_memory
4685 * @param address address where to start the read. May be specified in decimal or hex using the standard "0x" prefix
4686 * @param count optional count parameter to read an array of values. If not specified, defaults to 1.
4687 * @returns: JIM_ERR on error or JIM_OK on success and sets the result string to an array of ascii formatted numbers
4688 * on success, with [<count>] number of elements.
4690 * In case of little endian target:
4691 * Example1: "mdw 0x00000000" returns "10123456"
4692 * Exmaple2: "mdh 0x00000000 1" returns "3456"
4693 * Example3: "mdb 0x00000000" returns "56"
4694 * Example4: "mdh 0x00000000 2" returns "3456 1012"
4695 * Example5: "mdb 0x00000000 3" returns "56 34 12"
4697 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4699 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4702 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4704 if ((goi
.argc
< 1) || (goi
.argc
> 3)) {
4705 Jim_SetResultFormatted(goi
.interp
,
4706 "usage: %s [phys] <address> [<count>]", cmd_name
);
4710 int (*fn
)(struct target
*target
,
4711 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
4712 fn
= target_read_memory
;
4715 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4717 struct Jim_Obj
*obj
;
4718 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4722 fn
= target_read_phys_memory
;
4725 /* Read address parameter */
4727 e
= Jim_GetOpt_Wide(&goi
, &addr
);
4731 /* If next parameter exists, read it out as the count parameter, if not, set it to 1 (default) */
4733 if (goi
.argc
== 1) {
4734 e
= Jim_GetOpt_Wide(&goi
, &count
);
4740 /* all args must be consumed */
4744 jim_wide dwidth
= 1; /* shut up gcc */
4745 if (strcasecmp(cmd_name
, "mdw") == 0)
4747 else if (strcasecmp(cmd_name
, "mdh") == 0)
4749 else if (strcasecmp(cmd_name
, "mdb") == 0)
4752 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4756 /* convert count to "bytes" */
4757 int bytes
= count
* dwidth
;
4759 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4760 uint8_t target_buf
[32];
4763 y
= (bytes
< 16) ? bytes
: 16; /* y = min(bytes, 16); */
4765 /* Try to read out next block */
4766 e
= fn(target
, addr
, dwidth
, y
/ dwidth
, target_buf
);
4768 if (e
!= ERROR_OK
) {
4769 Jim_SetResultFormatted(interp
, "error reading target @ 0x%08lx", (long)addr
);
4773 command_print_sameline(NULL
, "0x%08x ", (int)(addr
));
4776 for (x
= 0; x
< 16 && x
< y
; x
+= 4) {
4777 z
= target_buffer_get_u32(target
, &(target_buf
[x
]));
4778 command_print_sameline(NULL
, "%08x ", (int)(z
));
4780 for (; (x
< 16) ; x
+= 4)
4781 command_print_sameline(NULL
, " ");
4784 for (x
= 0; x
< 16 && x
< y
; x
+= 2) {
4785 z
= target_buffer_get_u16(target
, &(target_buf
[x
]));
4786 command_print_sameline(NULL
, "%04x ", (int)(z
));
4788 for (; (x
< 16) ; x
+= 2)
4789 command_print_sameline(NULL
, " ");
4793 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
4794 z
= target_buffer_get_u8(target
, &(target_buf
[x
]));
4795 command_print_sameline(NULL
, "%02x ", (int)(z
));
4797 for (; (x
< 16) ; x
+= 1)
4798 command_print_sameline(NULL
, " ");
4801 /* ascii-ify the bytes */
4802 for (x
= 0 ; x
< y
; x
++) {
4803 if ((target_buf
[x
] >= 0x20) &&
4804 (target_buf
[x
] <= 0x7e)) {
4808 target_buf
[x
] = '.';
4813 target_buf
[x
] = ' ';
4818 /* print - with a newline */
4819 command_print_sameline(NULL
, "%s\n", target_buf
);
4827 static int jim_target_mem2array(Jim_Interp
*interp
,
4828 int argc
, Jim_Obj
*const *argv
)
4830 struct target
*target
= Jim_CmdPrivData(interp
);
4831 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4834 static int jim_target_array2mem(Jim_Interp
*interp
,
4835 int argc
, Jim_Obj
*const *argv
)
4837 struct target
*target
= Jim_CmdPrivData(interp
);
4838 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4841 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4843 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
4847 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4850 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4853 struct target
*target
= Jim_CmdPrivData(interp
);
4854 if (!target
->tap
->enabled
)
4855 return jim_target_tap_disabled(interp
);
4857 int e
= target
->type
->examine(target
);
4863 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4866 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4869 struct target
*target
= Jim_CmdPrivData(interp
);
4871 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
4877 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4880 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4883 struct target
*target
= Jim_CmdPrivData(interp
);
4884 if (!target
->tap
->enabled
)
4885 return jim_target_tap_disabled(interp
);
4888 if (!(target_was_examined(target
)))
4889 e
= ERROR_TARGET_NOT_EXAMINED
;
4891 e
= target
->type
->poll(target
);
4897 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4900 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4902 if (goi
.argc
!= 2) {
4903 Jim_WrongNumArgs(interp
, 0, argv
,
4904 "([tT]|[fF]|assert|deassert) BOOL");
4909 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4911 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4914 /* the halt or not param */
4916 e
= Jim_GetOpt_Wide(&goi
, &a
);
4920 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4921 if (!target
->tap
->enabled
)
4922 return jim_target_tap_disabled(interp
);
4923 if (!(target_was_examined(target
))) {
4924 LOG_ERROR("Target not examined yet");
4925 return ERROR_TARGET_NOT_EXAMINED
;
4927 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
4928 Jim_SetResultFormatted(interp
,
4929 "No target-specific reset for %s",
4930 target_name(target
));
4933 /* determine if we should halt or not. */
4934 target
->reset_halt
= !!a
;
4935 /* When this happens - all workareas are invalid. */
4936 target_free_all_working_areas_restore(target
, 0);
4939 if (n
->value
== NVP_ASSERT
)
4940 e
= target
->type
->assert_reset(target
);
4942 e
= target
->type
->deassert_reset(target
);
4943 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4946 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4949 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4952 struct target
*target
= Jim_CmdPrivData(interp
);
4953 if (!target
->tap
->enabled
)
4954 return jim_target_tap_disabled(interp
);
4955 int e
= target
->type
->halt(target
);
4956 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4959 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4962 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4964 /* params: <name> statename timeoutmsecs */
4965 if (goi
.argc
!= 2) {
4966 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4967 Jim_SetResultFormatted(goi
.interp
,
4968 "%s <state_name> <timeout_in_msec>", cmd_name
);
4973 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4975 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
, 1);
4979 e
= Jim_GetOpt_Wide(&goi
, &a
);
4982 struct target
*target
= Jim_CmdPrivData(interp
);
4983 if (!target
->tap
->enabled
)
4984 return jim_target_tap_disabled(interp
);
4986 e
= target_wait_state(target
, n
->value
, a
);
4987 if (e
!= ERROR_OK
) {
4988 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
4989 Jim_SetResultFormatted(goi
.interp
,
4990 "target: %s wait %s fails (%#s) %s",
4991 target_name(target
), n
->name
,
4992 eObj
, target_strerror_safe(e
));
4993 Jim_FreeNewObj(interp
, eObj
);
4998 /* List for human, Events defined for this target.
4999 * scripts/programs should use 'name cget -event NAME'
5001 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5003 struct command_context
*cmd_ctx
= current_command_context(interp
);
5004 assert(cmd_ctx
!= NULL
);
5006 struct target
*target
= Jim_CmdPrivData(interp
);
5007 struct target_event_action
*teap
= target
->event_action
;
5008 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
5009 target
->target_number
,
5010 target_name(target
));
5011 command_print(cmd_ctx
, "%-25s | Body", "Event");
5012 command_print(cmd_ctx
, "------------------------- | "
5013 "----------------------------------------");
5015 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
5016 command_print(cmd_ctx
, "%-25s | %s",
5017 opt
->name
, Jim_GetString(teap
->body
, NULL
));
5020 command_print(cmd_ctx
, "***END***");
5023 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5026 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5029 struct target
*target
= Jim_CmdPrivData(interp
);
5030 Jim_SetResultString(interp
, target_state_name(target
), -1);
5033 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5036 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5037 if (goi
.argc
!= 1) {
5038 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5039 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
5043 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
5045 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
5048 struct target
*target
= Jim_CmdPrivData(interp
);
5049 target_handle_event(target
, n
->value
);
5053 static const struct command_registration target_instance_command_handlers
[] = {
5055 .name
= "configure",
5056 .mode
= COMMAND_CONFIG
,
5057 .jim_handler
= jim_target_configure
,
5058 .help
= "configure a new target for use",
5059 .usage
= "[target_attribute ...]",
5063 .mode
= COMMAND_ANY
,
5064 .jim_handler
= jim_target_configure
,
5065 .help
= "returns the specified target attribute",
5066 .usage
= "target_attribute",
5070 .mode
= COMMAND_EXEC
,
5071 .jim_handler
= jim_target_mw
,
5072 .help
= "Write 32-bit word(s) to target memory",
5073 .usage
= "address data [count]",
5077 .mode
= COMMAND_EXEC
,
5078 .jim_handler
= jim_target_mw
,
5079 .help
= "Write 16-bit half-word(s) to target memory",
5080 .usage
= "address data [count]",
5084 .mode
= COMMAND_EXEC
,
5085 .jim_handler
= jim_target_mw
,
5086 .help
= "Write byte(s) to target memory",
5087 .usage
= "address data [count]",
5091 .mode
= COMMAND_EXEC
,
5092 .jim_handler
= jim_target_md
,
5093 .help
= "Display target memory as 32-bit words",
5094 .usage
= "address [count]",
5098 .mode
= COMMAND_EXEC
,
5099 .jim_handler
= jim_target_md
,
5100 .help
= "Display target memory as 16-bit half-words",
5101 .usage
= "address [count]",
5105 .mode
= COMMAND_EXEC
,
5106 .jim_handler
= jim_target_md
,
5107 .help
= "Display target memory as 8-bit bytes",
5108 .usage
= "address [count]",
5111 .name
= "array2mem",
5112 .mode
= COMMAND_EXEC
,
5113 .jim_handler
= jim_target_array2mem
,
5114 .help
= "Writes Tcl array of 8/16/32 bit numbers "
5116 .usage
= "arrayname bitwidth address count",
5119 .name
= "mem2array",
5120 .mode
= COMMAND_EXEC
,
5121 .jim_handler
= jim_target_mem2array
,
5122 .help
= "Loads Tcl array of 8/16/32 bit numbers "
5123 "from target memory",
5124 .usage
= "arrayname bitwidth address count",
5127 .name
= "eventlist",
5128 .mode
= COMMAND_EXEC
,
5129 .jim_handler
= jim_target_event_list
,
5130 .help
= "displays a table of events defined for this target",
5134 .mode
= COMMAND_EXEC
,
5135 .jim_handler
= jim_target_current_state
,
5136 .help
= "displays the current state of this target",
5139 .name
= "arp_examine",
5140 .mode
= COMMAND_EXEC
,
5141 .jim_handler
= jim_target_examine
,
5142 .help
= "used internally for reset processing",
5145 .name
= "arp_halt_gdb",
5146 .mode
= COMMAND_EXEC
,
5147 .jim_handler
= jim_target_halt_gdb
,
5148 .help
= "used internally for reset processing to halt GDB",
5152 .mode
= COMMAND_EXEC
,
5153 .jim_handler
= jim_target_poll
,
5154 .help
= "used internally for reset processing",
5157 .name
= "arp_reset",
5158 .mode
= COMMAND_EXEC
,
5159 .jim_handler
= jim_target_reset
,
5160 .help
= "used internally for reset processing",
5164 .mode
= COMMAND_EXEC
,
5165 .jim_handler
= jim_target_halt
,
5166 .help
= "used internally for reset processing",
5169 .name
= "arp_waitstate",
5170 .mode
= COMMAND_EXEC
,
5171 .jim_handler
= jim_target_wait_state
,
5172 .help
= "used internally for reset processing",
5175 .name
= "invoke-event",
5176 .mode
= COMMAND_EXEC
,
5177 .jim_handler
= jim_target_invoke_event
,
5178 .help
= "invoke handler for specified event",
5179 .usage
= "event_name",
5181 COMMAND_REGISTRATION_DONE
5184 static int target_create(Jim_GetOptInfo
*goi
)
5191 struct target
*target
;
5192 struct command_context
*cmd_ctx
;
5194 cmd_ctx
= current_command_context(goi
->interp
);
5195 assert(cmd_ctx
!= NULL
);
5197 if (goi
->argc
< 3) {
5198 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
5203 Jim_GetOpt_Obj(goi
, &new_cmd
);
5204 /* does this command exist? */
5205 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
5207 cp
= Jim_GetString(new_cmd
, NULL
);
5208 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
5213 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
5216 struct transport
*tr
= get_current_transport();
5217 if (tr
->override_target
) {
5218 e
= tr
->override_target(&cp
);
5219 if (e
!= ERROR_OK
) {
5220 LOG_ERROR("The selected transport doesn't support this target");
5223 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
5225 /* now does target type exist */
5226 for (x
= 0 ; target_types
[x
] ; x
++) {
5227 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
5232 /* check for deprecated name */
5233 if (target_types
[x
]->deprecated_name
) {
5234 if (0 == strcmp(cp
, target_types
[x
]->deprecated_name
)) {
5236 LOG_WARNING("target name is deprecated use: \'%s\'", target_types
[x
]->name
);
5241 if (target_types
[x
] == NULL
) {
5242 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
5243 for (x
= 0 ; target_types
[x
] ; x
++) {
5244 if (target_types
[x
+ 1]) {
5245 Jim_AppendStrings(goi
->interp
,
5246 Jim_GetResult(goi
->interp
),
5247 target_types
[x
]->name
,
5250 Jim_AppendStrings(goi
->interp
,
5251 Jim_GetResult(goi
->interp
),
5253 target_types
[x
]->name
, NULL
);
5260 target
= calloc(1, sizeof(struct target
));
5261 /* set target number */
5262 target
->target_number
= new_target_number();
5263 cmd_ctx
->current_target
= target
->target_number
;
5265 /* allocate memory for each unique target type */
5266 target
->type
= calloc(1, sizeof(struct target_type
));
5268 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
5270 /* will be set by "-endian" */
5271 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5273 /* default to first core, override with -coreid */
5276 target
->working_area
= 0x0;
5277 target
->working_area_size
= 0x0;
5278 target
->working_areas
= NULL
;
5279 target
->backup_working_area
= 0;
5281 target
->state
= TARGET_UNKNOWN
;
5282 target
->debug_reason
= DBG_REASON_UNDEFINED
;
5283 target
->reg_cache
= NULL
;
5284 target
->breakpoints
= NULL
;
5285 target
->watchpoints
= NULL
;
5286 target
->next
= NULL
;
5287 target
->arch_info
= NULL
;
5289 target
->display
= 1;
5291 target
->halt_issued
= false;
5293 /* initialize trace information */
5294 target
->trace_info
= malloc(sizeof(struct trace
));
5295 target
->trace_info
->num_trace_points
= 0;
5296 target
->trace_info
->trace_points_size
= 0;
5297 target
->trace_info
->trace_points
= NULL
;
5298 target
->trace_info
->trace_history_size
= 0;
5299 target
->trace_info
->trace_history
= NULL
;
5300 target
->trace_info
->trace_history_pos
= 0;
5301 target
->trace_info
->trace_history_overflowed
= 0;
5303 target
->dbgmsg
= NULL
;
5304 target
->dbg_msg_enabled
= 0;
5306 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5308 target
->rtos
= NULL
;
5309 target
->rtos_auto_detect
= false;
5311 /* Do the rest as "configure" options */
5312 goi
->isconfigure
= 1;
5313 e
= target_configure(goi
, target
);
5315 if (target
->tap
== NULL
) {
5316 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
5326 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
5327 /* default endian to little if not specified */
5328 target
->endianness
= TARGET_LITTLE_ENDIAN
;
5331 cp
= Jim_GetString(new_cmd
, NULL
);
5332 target
->cmd_name
= strdup(cp
);
5334 /* create the target specific commands */
5335 if (target
->type
->commands
) {
5336 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
5338 LOG_ERROR("unable to register '%s' commands", cp
);
5340 if (target
->type
->target_create
)
5341 (*(target
->type
->target_create
))(target
, goi
->interp
);
5343 /* append to end of list */
5345 struct target
**tpp
;
5346 tpp
= &(all_targets
);
5348 tpp
= &((*tpp
)->next
);
5352 /* now - create the new target name command */
5353 const struct command_registration target_subcommands
[] = {
5355 .chain
= target_instance_command_handlers
,
5358 .chain
= target
->type
->commands
,
5360 COMMAND_REGISTRATION_DONE
5362 const struct command_registration target_commands
[] = {
5365 .mode
= COMMAND_ANY
,
5366 .help
= "target command group",
5368 .chain
= target_subcommands
,
5370 COMMAND_REGISTRATION_DONE
5372 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
5376 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
5378 command_set_handler_data(c
, target
);
5380 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
5383 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5386 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5389 struct command_context
*cmd_ctx
= current_command_context(interp
);
5390 assert(cmd_ctx
!= NULL
);
5392 Jim_SetResultString(interp
, target_name(get_current_target(cmd_ctx
)), -1);
5396 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5399 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5402 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5403 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++) {
5404 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5405 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
5410 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5413 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5416 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5417 struct target
*target
= all_targets
;
5419 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5420 Jim_NewStringObj(interp
, target_name(target
), -1));
5421 target
= target
->next
;
5426 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5429 const char *targetname
;
5431 struct target
*target
= (struct target
*) NULL
;
5432 struct target_list
*head
, *curr
, *new;
5433 curr
= (struct target_list
*) NULL
;
5434 head
= (struct target_list
*) NULL
;
5437 LOG_DEBUG("%d", argc
);
5438 /* argv[1] = target to associate in smp
5439 * argv[2] = target to assoicate in smp
5443 for (i
= 1; i
< argc
; i
++) {
5445 targetname
= Jim_GetString(argv
[i
], &len
);
5446 target
= get_target(targetname
);
5447 LOG_DEBUG("%s ", targetname
);
5449 new = malloc(sizeof(struct target_list
));
5450 new->target
= target
;
5451 new->next
= (struct target_list
*)NULL
;
5452 if (head
== (struct target_list
*)NULL
) {
5461 /* now parse the list of cpu and put the target in smp mode*/
5464 while (curr
!= (struct target_list
*)NULL
) {
5465 target
= curr
->target
;
5467 target
->head
= head
;
5471 if (target
&& target
->rtos
)
5472 retval
= rtos_smp_init(head
->target
);
5478 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5481 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5483 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5484 "<name> <target_type> [<target_options> ...]");
5487 return target_create(&goi
);
5490 static const struct command_registration target_subcommand_handlers
[] = {
5493 .mode
= COMMAND_CONFIG
,
5494 .handler
= handle_target_init_command
,
5495 .help
= "initialize targets",
5499 /* REVISIT this should be COMMAND_CONFIG ... */
5500 .mode
= COMMAND_ANY
,
5501 .jim_handler
= jim_target_create
,
5502 .usage
= "name type '-chain-position' name [options ...]",
5503 .help
= "Creates and selects a new target",
5507 .mode
= COMMAND_ANY
,
5508 .jim_handler
= jim_target_current
,
5509 .help
= "Returns the currently selected target",
5513 .mode
= COMMAND_ANY
,
5514 .jim_handler
= jim_target_types
,
5515 .help
= "Returns the available target types as "
5516 "a list of strings",
5520 .mode
= COMMAND_ANY
,
5521 .jim_handler
= jim_target_names
,
5522 .help
= "Returns the names of all targets as a list of strings",
5526 .mode
= COMMAND_ANY
,
5527 .jim_handler
= jim_target_smp
,
5528 .usage
= "targetname1 targetname2 ...",
5529 .help
= "gather several target in a smp list"
5532 COMMAND_REGISTRATION_DONE
5542 static int fastload_num
;
5543 static struct FastLoad
*fastload
;
5545 static void free_fastload(void)
5547 if (fastload
!= NULL
) {
5549 for (i
= 0; i
< fastload_num
; i
++) {
5550 if (fastload
[i
].data
)
5551 free(fastload
[i
].data
);
5558 COMMAND_HANDLER(handle_fast_load_image_command
)
5562 uint32_t image_size
;
5563 uint32_t min_address
= 0;
5564 uint32_t max_address
= 0xffffffff;
5569 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
5570 &image
, &min_address
, &max_address
);
5571 if (ERROR_OK
!= retval
)
5574 struct duration bench
;
5575 duration_start(&bench
);
5577 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5578 if (retval
!= ERROR_OK
)
5583 fastload_num
= image
.num_sections
;
5584 fastload
= malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5585 if (fastload
== NULL
) {
5586 command_print(CMD_CTX
, "out of memory");
5587 image_close(&image
);
5590 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5591 for (i
= 0; i
< image
.num_sections
; i
++) {
5592 buffer
= malloc(image
.sections
[i
].size
);
5593 if (buffer
== NULL
) {
5594 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5595 (int)(image
.sections
[i
].size
));
5596 retval
= ERROR_FAIL
;
5600 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
5601 if (retval
!= ERROR_OK
) {
5606 uint32_t offset
= 0;
5607 uint32_t length
= buf_cnt
;
5609 /* DANGER!!! beware of unsigned comparision here!!! */
5611 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
5612 (image
.sections
[i
].base_address
< max_address
)) {
5613 if (image
.sections
[i
].base_address
< min_address
) {
5614 /* clip addresses below */
5615 offset
+= min_address
-image
.sections
[i
].base_address
;
5619 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5620 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5622 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5623 fastload
[i
].data
= malloc(length
);
5624 if (fastload
[i
].data
== NULL
) {
5626 command_print(CMD_CTX
, "error allocating buffer for section (%" PRIu32
" bytes)",
5628 retval
= ERROR_FAIL
;
5631 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5632 fastload
[i
].length
= length
;
5634 image_size
+= length
;
5635 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
5636 (unsigned int)length
,
5637 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
5643 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
5644 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
5645 "in %fs (%0.3f KiB/s)", image_size
,
5646 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
5648 command_print(CMD_CTX
,
5649 "WARNING: image has not been loaded to target!"
5650 "You can issue a 'fast_load' to finish loading.");
5653 image_close(&image
);
5655 if (retval
!= ERROR_OK
)
5661 COMMAND_HANDLER(handle_fast_load_command
)
5664 return ERROR_COMMAND_SYNTAX_ERROR
;
5665 if (fastload
== NULL
) {
5666 LOG_ERROR("No image in memory");
5670 int ms
= timeval_ms();
5672 int retval
= ERROR_OK
;
5673 for (i
= 0; i
< fastload_num
; i
++) {
5674 struct target
*target
= get_current_target(CMD_CTX
);
5675 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
5676 (unsigned int)(fastload
[i
].address
),
5677 (unsigned int)(fastload
[i
].length
));
5678 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5679 if (retval
!= ERROR_OK
)
5681 size
+= fastload
[i
].length
;
5683 if (retval
== ERROR_OK
) {
5684 int after
= timeval_ms();
5685 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5690 static const struct command_registration target_command_handlers
[] = {
5693 .handler
= handle_targets_command
,
5694 .mode
= COMMAND_ANY
,
5695 .help
= "change current default target (one parameter) "
5696 "or prints table of all targets (no parameters)",
5697 .usage
= "[target]",
5701 .mode
= COMMAND_CONFIG
,
5702 .help
= "configure target",
5704 .chain
= target_subcommand_handlers
,
5706 COMMAND_REGISTRATION_DONE
5709 int target_register_commands(struct command_context
*cmd_ctx
)
5711 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5714 static bool target_reset_nag
= true;
5716 bool get_target_reset_nag(void)
5718 return target_reset_nag
;
5721 COMMAND_HANDLER(handle_target_reset_nag
)
5723 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
5724 &target_reset_nag
, "Nag after each reset about options to improve "
5728 COMMAND_HANDLER(handle_ps_command
)
5730 struct target
*target
= get_current_target(CMD_CTX
);
5732 if (target
->state
!= TARGET_HALTED
) {
5733 LOG_INFO("target not halted !!");
5737 if ((target
->rtos
) && (target
->rtos
->type
)
5738 && (target
->rtos
->type
->ps_command
)) {
5739 display
= target
->rtos
->type
->ps_command(target
);
5740 command_print(CMD_CTX
, "%s", display
);
5745 return ERROR_TARGET_FAILURE
;
5749 static void binprint(struct command_context
*cmd_ctx
, const char *text
, const uint8_t *buf
, int size
)
5752 command_print_sameline(cmd_ctx
, "%s", text
);
5753 for (int i
= 0; i
< size
; i
++)
5754 command_print_sameline(cmd_ctx
, " %02x", buf
[i
]);
5755 command_print(cmd_ctx
, " ");
5758 COMMAND_HANDLER(handle_test_mem_access_command
)
5760 struct target
*target
= get_current_target(CMD_CTX
);
5762 int retval
= ERROR_OK
;
5764 if (target
->state
!= TARGET_HALTED
) {
5765 LOG_INFO("target not halted !!");
5770 return ERROR_COMMAND_SYNTAX_ERROR
;
5772 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], test_size
);
5775 size_t num_bytes
= test_size
+ 4;
5777 struct working_area
*wa
= NULL
;
5778 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
5779 if (retval
!= ERROR_OK
) {
5780 LOG_ERROR("Not enough working area");
5784 uint8_t *test_pattern
= malloc(num_bytes
);
5786 for (size_t i
= 0; i
< num_bytes
; i
++)
5787 test_pattern
[i
] = rand();
5789 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
5790 if (retval
!= ERROR_OK
) {
5791 LOG_ERROR("Test pattern write failed");
5795 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
5796 for (int size
= 1; size
<= 4; size
*= 2) {
5797 for (int offset
= 0; offset
< 4; offset
++) {
5798 uint32_t count
= test_size
/ size
;
5799 size_t host_bufsiz
= (count
+ 2) * size
+ host_offset
;
5800 uint8_t *read_ref
= malloc(host_bufsiz
);
5801 uint8_t *read_buf
= malloc(host_bufsiz
);
5803 for (size_t i
= 0; i
< host_bufsiz
; i
++) {
5804 read_ref
[i
] = rand();
5805 read_buf
[i
] = read_ref
[i
];
5807 command_print_sameline(CMD_CTX
,
5808 "Test read %" PRIu32
" x %d @ %d to %saligned buffer: ", count
,
5809 size
, offset
, host_offset
? "un" : "");
5811 struct duration bench
;
5812 duration_start(&bench
);
5814 retval
= target_read_memory(target
, wa
->address
+ offset
, size
, count
,
5815 read_buf
+ size
+ host_offset
);
5817 duration_measure(&bench
);
5819 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
5820 command_print(CMD_CTX
, "Unsupported alignment");
5822 } else if (retval
!= ERROR_OK
) {
5823 command_print(CMD_CTX
, "Memory read failed");
5827 /* replay on host */
5828 memcpy(read_ref
+ size
+ host_offset
, test_pattern
+ offset
, count
* size
);
5831 int result
= memcmp(read_ref
, read_buf
, host_bufsiz
);
5833 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
5834 duration_elapsed(&bench
),
5835 duration_kbps(&bench
, count
* size
));
5837 command_print(CMD_CTX
, "Compare failed");
5838 binprint(CMD_CTX
, "ref:", read_ref
, host_bufsiz
);
5839 binprint(CMD_CTX
, "buf:", read_buf
, host_bufsiz
);
5852 target_free_working_area(target
, wa
);
5855 num_bytes
= test_size
+ 4 + 4 + 4;
5857 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
5858 if (retval
!= ERROR_OK
) {
5859 LOG_ERROR("Not enough working area");
5863 test_pattern
= malloc(num_bytes
);
5865 for (size_t i
= 0; i
< num_bytes
; i
++)
5866 test_pattern
[i
] = rand();
5868 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
5869 for (int size
= 1; size
<= 4; size
*= 2) {
5870 for (int offset
= 0; offset
< 4; offset
++) {
5871 uint32_t count
= test_size
/ size
;
5872 size_t host_bufsiz
= count
* size
+ host_offset
;
5873 uint8_t *read_ref
= malloc(num_bytes
);
5874 uint8_t *read_buf
= malloc(num_bytes
);
5875 uint8_t *write_buf
= malloc(host_bufsiz
);
5877 for (size_t i
= 0; i
< host_bufsiz
; i
++)
5878 write_buf
[i
] = rand();
5879 command_print_sameline(CMD_CTX
,
5880 "Test write %" PRIu32
" x %d @ %d from %saligned buffer: ", count
,
5881 size
, offset
, host_offset
? "un" : "");
5883 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
5884 if (retval
!= ERROR_OK
) {
5885 command_print(CMD_CTX
, "Test pattern write failed");
5889 /* replay on host */
5890 memcpy(read_ref
, test_pattern
, num_bytes
);
5891 memcpy(read_ref
+ size
+ offset
, write_buf
+ host_offset
, count
* size
);
5893 struct duration bench
;
5894 duration_start(&bench
);
5896 retval
= target_write_memory(target
, wa
->address
+ size
+ offset
, size
, count
,
5897 write_buf
+ host_offset
);
5899 duration_measure(&bench
);
5901 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
5902 command_print(CMD_CTX
, "Unsupported alignment");
5904 } else if (retval
!= ERROR_OK
) {
5905 command_print(CMD_CTX
, "Memory write failed");
5910 retval
= target_read_memory(target
, wa
->address
, 1, num_bytes
, read_buf
);
5911 if (retval
!= ERROR_OK
) {
5912 command_print(CMD_CTX
, "Test pattern write failed");
5917 int result
= memcmp(read_ref
, read_buf
, num_bytes
);
5919 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
5920 duration_elapsed(&bench
),
5921 duration_kbps(&bench
, count
* size
));
5923 command_print(CMD_CTX
, "Compare failed");
5924 binprint(CMD_CTX
, "ref:", read_ref
, num_bytes
);
5925 binprint(CMD_CTX
, "buf:", read_buf
, num_bytes
);
5937 target_free_working_area(target
, wa
);
5941 static const struct command_registration target_exec_command_handlers
[] = {
5943 .name
= "fast_load_image",
5944 .handler
= handle_fast_load_image_command
,
5945 .mode
= COMMAND_ANY
,
5946 .help
= "Load image into server memory for later use by "
5947 "fast_load; primarily for profiling",
5948 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5949 "[min_address [max_length]]",
5952 .name
= "fast_load",
5953 .handler
= handle_fast_load_command
,
5954 .mode
= COMMAND_EXEC
,
5955 .help
= "loads active fast load image to current target "
5956 "- mainly for profiling purposes",
5961 .handler
= handle_profile_command
,
5962 .mode
= COMMAND_EXEC
,
5963 .usage
= "seconds filename [start end]",
5964 .help
= "profiling samples the CPU PC",
5966 /** @todo don't register virt2phys() unless target supports it */
5968 .name
= "virt2phys",
5969 .handler
= handle_virt2phys_command
,
5970 .mode
= COMMAND_ANY
,
5971 .help
= "translate a virtual address into a physical address",
5972 .usage
= "virtual_address",
5976 .handler
= handle_reg_command
,
5977 .mode
= COMMAND_EXEC
,
5978 .help
= "display (reread from target with \"force\") or set a register; "
5979 "with no arguments, displays all registers and their values",
5980 .usage
= "[(register_number|register_name) [(value|'force')]]",
5984 .handler
= handle_poll_command
,
5985 .mode
= COMMAND_EXEC
,
5986 .help
= "poll target state; or reconfigure background polling",
5987 .usage
= "['on'|'off']",
5990 .name
= "wait_halt",
5991 .handler
= handle_wait_halt_command
,
5992 .mode
= COMMAND_EXEC
,
5993 .help
= "wait up to the specified number of milliseconds "
5994 "(default 5000) for a previously requested halt",
5995 .usage
= "[milliseconds]",
5999 .handler
= handle_halt_command
,
6000 .mode
= COMMAND_EXEC
,
6001 .help
= "request target to halt, then wait up to the specified"
6002 "number of milliseconds (default 5000) for it to complete",
6003 .usage
= "[milliseconds]",
6007 .handler
= handle_resume_command
,
6008 .mode
= COMMAND_EXEC
,
6009 .help
= "resume target execution from current PC or address",
6010 .usage
= "[address]",
6014 .handler
= handle_reset_command
,
6015 .mode
= COMMAND_EXEC
,
6016 .usage
= "[run|halt|init]",
6017 .help
= "Reset all targets into the specified mode."
6018 "Default reset mode is run, if not given.",
6021 .name
= "soft_reset_halt",
6022 .handler
= handle_soft_reset_halt_command
,
6023 .mode
= COMMAND_EXEC
,
6025 .help
= "halt the target and do a soft reset",
6029 .handler
= handle_step_command
,
6030 .mode
= COMMAND_EXEC
,
6031 .help
= "step one instruction from current PC or address",
6032 .usage
= "[address]",
6036 .handler
= handle_md_command
,
6037 .mode
= COMMAND_EXEC
,
6038 .help
= "display memory words",
6039 .usage
= "['phys'] address [count]",
6043 .handler
= handle_md_command
,
6044 .mode
= COMMAND_EXEC
,
6045 .help
= "display memory half-words",
6046 .usage
= "['phys'] address [count]",
6050 .handler
= handle_md_command
,
6051 .mode
= COMMAND_EXEC
,
6052 .help
= "display memory bytes",
6053 .usage
= "['phys'] address [count]",
6057 .handler
= handle_mw_command
,
6058 .mode
= COMMAND_EXEC
,
6059 .help
= "write memory word",
6060 .usage
= "['phys'] address value [count]",
6064 .handler
= handle_mw_command
,
6065 .mode
= COMMAND_EXEC
,
6066 .help
= "write memory half-word",
6067 .usage
= "['phys'] address value [count]",
6071 .handler
= handle_mw_command
,
6072 .mode
= COMMAND_EXEC
,
6073 .help
= "write memory byte",
6074 .usage
= "['phys'] address value [count]",
6078 .handler
= handle_bp_command
,
6079 .mode
= COMMAND_EXEC
,
6080 .help
= "list or set hardware or software breakpoint",
6081 .usage
= "<address> [<asid>]<length> ['hw'|'hw_ctx']",
6085 .handler
= handle_rbp_command
,
6086 .mode
= COMMAND_EXEC
,
6087 .help
= "remove breakpoint",
6092 .handler
= handle_wp_command
,
6093 .mode
= COMMAND_EXEC
,
6094 .help
= "list (no params) or create watchpoints",
6095 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
6099 .handler
= handle_rwp_command
,
6100 .mode
= COMMAND_EXEC
,
6101 .help
= "remove watchpoint",
6105 .name
= "load_image",
6106 .handler
= handle_load_image_command
,
6107 .mode
= COMMAND_EXEC
,
6108 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6109 "[min_address] [max_length]",
6112 .name
= "dump_image",
6113 .handler
= handle_dump_image_command
,
6114 .mode
= COMMAND_EXEC
,
6115 .usage
= "filename address size",
6118 .name
= "verify_image",
6119 .handler
= handle_verify_image_command
,
6120 .mode
= COMMAND_EXEC
,
6121 .usage
= "filename [offset [type]]",
6124 .name
= "test_image",
6125 .handler
= handle_test_image_command
,
6126 .mode
= COMMAND_EXEC
,
6127 .usage
= "filename [offset [type]]",
6130 .name
= "mem2array",
6131 .mode
= COMMAND_EXEC
,
6132 .jim_handler
= jim_mem2array
,
6133 .help
= "read 8/16/32 bit memory and return as a TCL array "
6134 "for script processing",
6135 .usage
= "arrayname bitwidth address count",
6138 .name
= "array2mem",
6139 .mode
= COMMAND_EXEC
,
6140 .jim_handler
= jim_array2mem
,
6141 .help
= "convert a TCL array to memory locations "
6142 "and write the 8/16/32 bit values",
6143 .usage
= "arrayname bitwidth address count",
6146 .name
= "reset_nag",
6147 .handler
= handle_target_reset_nag
,
6148 .mode
= COMMAND_ANY
,
6149 .help
= "Nag after each reset about options that could have been "
6150 "enabled to improve performance. ",
6151 .usage
= "['enable'|'disable']",
6155 .handler
= handle_ps_command
,
6156 .mode
= COMMAND_EXEC
,
6157 .help
= "list all tasks ",
6161 .name
= "test_mem_access",
6162 .handler
= handle_test_mem_access_command
,
6163 .mode
= COMMAND_EXEC
,
6164 .help
= "Test the target's memory access functions",
6168 COMMAND_REGISTRATION_DONE
6170 static int target_register_user_commands(struct command_context
*cmd_ctx
)
6172 int retval
= ERROR_OK
;
6173 retval
= target_request_register_commands(cmd_ctx
);
6174 if (retval
!= ERROR_OK
)
6177 retval
= trace_register_commands(cmd_ctx
);
6178 if (retval
!= ERROR_OK
)
6182 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);